blob: 7a00245ebada3780d195db78b507d86e7ed0d2a4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/types.h>
40#include <linux/mtd/mtd.h>
41#include <linux/mtd/nand.h>
42#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010043#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/interrupt.h>
45#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020046#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020048#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010049
Huang Shijie6a8214a2012-11-19 14:43:30 +080050static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020052static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
53 struct mtd_oob_ops *ops);
54
Boris Brezillon41b207a2016-02-03 19:06:15 +010055/* Define default oob placement schemes for large and small page devices */
56static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
57 struct mtd_oob_region *oobregion)
58{
59 struct nand_chip *chip = mtd_to_nand(mtd);
60 struct nand_ecc_ctrl *ecc = &chip->ecc;
61
62 if (section > 1)
63 return -ERANGE;
64
65 if (!section) {
66 oobregion->offset = 0;
67 oobregion->length = 4;
68 } else {
69 oobregion->offset = 6;
70 oobregion->length = ecc->total - 4;
71 }
72
73 return 0;
74}
75
76static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
77 struct mtd_oob_region *oobregion)
78{
79 if (section > 1)
80 return -ERANGE;
81
82 if (mtd->oobsize == 16) {
83 if (section)
84 return -ERANGE;
85
86 oobregion->length = 8;
87 oobregion->offset = 8;
88 } else {
89 oobregion->length = 2;
90 if (!section)
91 oobregion->offset = 3;
92 else
93 oobregion->offset = 6;
94 }
95
96 return 0;
97}
98
99const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
100 .ecc = nand_ooblayout_ecc_sp,
101 .free = nand_ooblayout_free_sp,
102};
103EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
104
105static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
106 struct mtd_oob_region *oobregion)
107{
108 struct nand_chip *chip = mtd_to_nand(mtd);
109 struct nand_ecc_ctrl *ecc = &chip->ecc;
110
111 if (section)
112 return -ERANGE;
113
114 oobregion->length = ecc->total;
115 oobregion->offset = mtd->oobsize - oobregion->length;
116
117 return 0;
118}
119
120static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
121 struct mtd_oob_region *oobregion)
122{
123 struct nand_chip *chip = mtd_to_nand(mtd);
124 struct nand_ecc_ctrl *ecc = &chip->ecc;
125
126 if (section)
127 return -ERANGE;
128
129 oobregion->length = mtd->oobsize - ecc->total - 2;
130 oobregion->offset = 2;
131
132 return 0;
133}
134
135const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
136 .ecc = nand_ooblayout_ecc_lp,
137 .free = nand_ooblayout_free_lp,
138};
139EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200140
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530141static int check_offs_len(struct mtd_info *mtd,
142 loff_t ofs, uint64_t len)
143{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100144 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530145 int ret = 0;
146
147 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300148 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700149 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530150 ret = -EINVAL;
151 }
152
153 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300154 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700155 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530156 ret = -EINVAL;
157 }
158
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530159 return ret;
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/**
163 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700164 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000165 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800166 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100168static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100170 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200172 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200173 spin_lock(&chip->controller->lock);
174 chip->controller->active = NULL;
175 chip->state = FL_READY;
176 wake_up(&chip->controller->wq);
177 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180/**
181 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700182 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700184 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200186static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100188 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200189 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192/**
Masanari Iida064a7692012-11-09 23:20:58 +0900193 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700194 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700196 * Default read function for 16bit buswidth with endianness conversion.
197 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200199static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100201 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200202 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700207 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700209 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 */
211static u16 nand_read_word(struct mtd_info *mtd)
212{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100213 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200214 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700219 * @mtd: MTD device structure
220 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 *
222 * Default select function for 1 chip devices.
223 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200224static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100226 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200227
228 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200230 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 break;
232 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 break;
234
235 default:
236 BUG();
237 }
238}
239
240/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100241 * nand_write_byte - [DEFAULT] write single byte to chip
242 * @mtd: MTD device structure
243 * @byte: value to write
244 *
245 * Default function to write a byte to I/O[7:0]
246 */
247static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
248{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100249 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100250
251 chip->write_buf(mtd, &byte, 1);
252}
253
254/**
255 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
256 * @mtd: MTD device structure
257 * @byte: value to write
258 *
259 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
260 */
261static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
262{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100263 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100264 uint16_t word = byte;
265
266 /*
267 * It's not entirely clear what should happen to I/O[15:8] when writing
268 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
269 *
270 * When the host supports a 16-bit bus width, only data is
271 * transferred at the 16-bit width. All address and command line
272 * transfers shall use only the lower 8-bits of the data bus. During
273 * command transfers, the host may place any value on the upper
274 * 8-bits of the data bus. During address transfers, the host shall
275 * set the upper 8-bits of the data bus to 00h.
276 *
277 * One user of the write_byte callback is nand_onfi_set_features. The
278 * four parameters are specified to be written to I/O[7:0], but this is
279 * neither an address nor a command transfer. Let's assume a 0 on the
280 * upper I/O lines is OK.
281 */
282 chip->write_buf(mtd, (uint8_t *)&word, 2);
283}
284
285/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700287 * @mtd: MTD device structure
288 * @buf: data buffer
289 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700291 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200293static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100295 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Alexander Shiyan76413832013-04-13 09:32:13 +0400297 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000301 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700302 * @mtd: MTD device structure
303 * @buf: buffer to store date
304 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700306 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200308static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100310 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Alexander Shiyan76413832013-04-13 09:32:13 +0400312 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700317 * @mtd: MTD device structure
318 * @buf: data buffer
319 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700321 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200323static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100325 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000327
Alexander Shiyan76413832013-04-13 09:32:13 +0400328 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000332 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700333 * @mtd: MTD device structure
334 * @buf: buffer to store date
335 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700337 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200339static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100341 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Alexander Shiyan76413832013-04-13 09:32:13 +0400344 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700349 * @mtd: MTD device structure
350 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000352 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530354static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Archit Taneja9f3e0422016-02-03 14:29:49 +0530356 int page, res = 0, i = 0;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100357 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 u16 bad;
359
Brian Norris5fb15492011-05-31 16:31:21 -0700360 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700361 ofs += mtd->erasesize - mtd->writesize;
362
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100363 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
364
Brian Norriscdbec052012-01-13 18:11:48 -0800365 do {
366 if (chip->options & NAND_BUSWIDTH_16) {
367 chip->cmdfunc(mtd, NAND_CMD_READOOB,
368 chip->badblockpos & 0xFE, page);
369 bad = cpu_to_le16(chip->read_word(mtd));
370 if (chip->badblockpos & 0x1)
371 bad >>= 8;
372 else
373 bad &= 0xFF;
374 } else {
375 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
376 page);
377 bad = chip->read_byte(mtd);
378 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000379
Brian Norriscdbec052012-01-13 18:11:48 -0800380 if (likely(chip->badblockbits == 8))
381 res = bad != 0xFF;
382 else
383 res = hweight8(bad) < chip->badblockbits;
384 ofs += mtd->writesize;
385 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
386 i++;
387 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return res;
390}
391
392/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700393 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700394 * @mtd: MTD device structure
395 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700397 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700398 * specific driver. It provides the details for writing a bad block marker to a
399 * block.
400 */
401static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
402{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100403 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700404 struct mtd_oob_ops ops;
405 uint8_t buf[2] = { 0, 0 };
406 int ret = 0, res, i = 0;
407
Brian Norris0ec56dc2015-02-28 02:02:30 -0800408 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700409 ops.oobbuf = buf;
410 ops.ooboffs = chip->badblockpos;
411 if (chip->options & NAND_BUSWIDTH_16) {
412 ops.ooboffs &= ~0x01;
413 ops.len = ops.ooblen = 2;
414 } else {
415 ops.len = ops.ooblen = 1;
416 }
417 ops.mode = MTD_OPS_PLACE_OOB;
418
419 /* Write to first/last page(s) if necessary */
420 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
421 ofs += mtd->erasesize - mtd->writesize;
422 do {
423 res = nand_do_write_oob(mtd, ofs, &ops);
424 if (!ret)
425 ret = res;
426
427 i++;
428 ofs += mtd->writesize;
429 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
430
431 return ret;
432}
433
434/**
435 * nand_block_markbad_lowlevel - mark a block bad
436 * @mtd: MTD device structure
437 * @ofs: offset from device start
438 *
439 * This function performs the generic NAND bad block marking steps (i.e., bad
440 * block table(s) and/or marker(s)). We only allow the hardware driver to
441 * specify how to write bad block markers to OOB (chip->block_markbad).
442 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700443 * We try operations in the following order:
Brian Norrise2414f42012-02-06 13:44:00 -0800444 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700445 * (2) write bad block marker to OOB area of affected block (unless flag
446 * NAND_BBT_NO_OOB_BBM is present)
447 * (3) update the BBT
448 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800449 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700451static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100453 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700454 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000455
Brian Norrisb32843b2013-07-30 17:52:59 -0700456 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800457 struct erase_info einfo;
458
459 /* Attempt erase before marking OOB */
460 memset(&einfo, 0, sizeof(einfo));
461 einfo.mtd = mtd;
462 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300463 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800464 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800465
Brian Norrisb32843b2013-07-30 17:52:59 -0700466 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800467 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700468 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300469 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200470 }
Brian Norrise2414f42012-02-06 13:44:00 -0800471
Brian Norrisb32843b2013-07-30 17:52:59 -0700472 /* Mark block bad in BBT */
473 if (chip->bbt) {
474 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800475 if (!ret)
476 ret = res;
477 }
478
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200479 if (!ret)
480 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300481
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200482 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000485/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700487 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700489 * Check, if the device is write protected. The function expects, that the
490 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100492static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100494 struct nand_chip *chip = mtd_to_nand(mtd);
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200495
Brian Norris8b6e50c2011-05-25 14:59:01 -0700496 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200497 if (chip->options & NAND_BROKEN_XD)
498 return 0;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200501 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
502 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800506 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700507 * @mtd: MTD device structure
508 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300509 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800510 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300511 */
512static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
513{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100514 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300515
516 if (!chip->bbt)
517 return 0;
518 /* Return info from the table */
519 return nand_isreserved_bbt(mtd, ofs);
520}
521
522/**
523 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
524 * @mtd: MTD device structure
525 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700526 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 *
528 * Check, if the block is bad. Either by reading the bad block table or
529 * calling of the scan function.
530 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530531static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100533 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000534
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200535 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530536 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100539 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200542/**
543 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700544 * @mtd: MTD device structure
545 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200546 *
547 * Helper function for nand_wait_ready used when needing to wait in interrupt
548 * context.
549 */
550static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
551{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100552 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200553 int i;
554
555 /* Wait for the device to get ready */
556 for (i = 0; i < timeo; i++) {
557 if (chip->dev_ready(mtd))
558 break;
559 touch_softlockup_watchdog();
560 mdelay(1);
561 }
562}
563
Alex Smithb70af9b2015-10-06 14:52:07 +0100564/**
565 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
566 * @mtd: MTD device structure
567 *
568 * Wait for the ready pin after a command, and warn if a timeout occurs.
569 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100570void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000571{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100572 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100573 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000574
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200575 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100576 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200577
Brian Norris7854d3f2011-06-23 14:12:08 -0700578 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100579 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000580 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200581 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300582 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100583 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000584 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100585
Brian Norris9ebfdf52016-03-04 17:19:23 -0800586 if (!chip->dev_ready(mtd))
587 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000588}
David Woodhouse4b648b02006-09-25 17:05:24 +0100589EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200592 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
593 * @mtd: MTD device structure
594 * @timeo: Timeout in ms
595 *
596 * Wait for status ready (i.e. command done) or timeout.
597 */
598static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
599{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100600 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200601
602 timeo = jiffies + msecs_to_jiffies(timeo);
603 do {
604 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
605 break;
606 touch_softlockup_watchdog();
607 } while (time_before(jiffies, timeo));
608};
609
610/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700612 * @mtd: MTD device structure
613 * @command: the command to be sent
614 * @column: the column address for this command, -1 if none
615 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700617 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200618 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200620static void nand_command(struct mtd_info *mtd, unsigned int command,
621 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100623 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200624 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Brian Norris8b6e50c2011-05-25 14:59:01 -0700626 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (command == NAND_CMD_SEQIN) {
628 int readcmd;
629
Joern Engel28318772006-05-22 23:18:05 +0200630 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200632 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 readcmd = NAND_CMD_READOOB;
634 } else if (column < 256) {
635 /* First 256 bytes --> READ0 */
636 readcmd = NAND_CMD_READ0;
637 } else {
638 column -= 256;
639 readcmd = NAND_CMD_READ1;
640 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200641 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200642 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200644 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Brian Norris8b6e50c2011-05-25 14:59:01 -0700646 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200647 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
648 /* Serially input address */
649 if (column != -1) {
650 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800651 if (chip->options & NAND_BUSWIDTH_16 &&
652 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200653 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200654 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200655 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200657 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200658 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200659 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200660 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200661 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200662 if (chip->chipsize > (32 << 20))
663 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200664 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200665 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000666
667 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700668 * Program and erase have their own busy handlers status and sequential
669 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100670 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 case NAND_CMD_PAGEPROG:
674 case NAND_CMD_ERASE1:
675 case NAND_CMD_ERASE2:
676 case NAND_CMD_SEQIN:
677 case NAND_CMD_STATUS:
678 return;
679
680 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200681 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200683 udelay(chip->chip_delay);
684 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200685 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200686 chip->cmd_ctrl(mtd,
687 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200688 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
689 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return;
691
David Woodhousee0c7d762006-05-13 18:07:53 +0100692 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000694 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 * If we don't have access to the busy pin, we apply the given
696 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100697 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200698 if (!chip->dev_ready) {
699 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700703 /*
704 * Apply this short delay always to ensure that we do wait tWB in
705 * any case on any machine.
706 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100707 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000708
709 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712/**
713 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700714 * @mtd: MTD device structure
715 * @command: the command to be sent
716 * @column: the column address for this command, -1 if none
717 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200719 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700720 * devices. We don't have the separate regions as we have in the small page
721 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200723static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
724 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100726 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 /* Emulate NAND_CMD_READOOB */
729 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200730 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 command = NAND_CMD_READ0;
732 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000733
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200734 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400735 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200738 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 /* Serially input address */
741 if (column != -1) {
742 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800743 if (chip->options & NAND_BUSWIDTH_16 &&
744 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200746 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200747 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200748
749 /* Only ouput a single addr cycle for 8bits opcodes. */
750 if (!nand_opcode_8bits(command))
751 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200754 chip->cmd_ctrl(mtd, page_addr, ctrl);
755 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200756 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200758 if (chip->chipsize > (128 << 20))
759 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200760 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200763 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000764
765 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700766 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100767 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 case NAND_CMD_CACHEDPROG:
772 case NAND_CMD_PAGEPROG:
773 case NAND_CMD_ERASE1:
774 case NAND_CMD_ERASE2:
775 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200776 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000778 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200781 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200783 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200784 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
785 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
786 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
787 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200788 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
789 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return;
791
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200792 case NAND_CMD_RNDOUT:
793 /* No ready / busy check necessary */
794 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
795 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
796 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
797 NAND_NCE | NAND_CTRL_CHANGE);
798 return;
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200801 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
802 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
803 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
804 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000805
David Woodhousee0c7d762006-05-13 18:07:53 +0100806 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000808 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700810 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100811 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200812 if (!chip->dev_ready) {
813 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000817
Brian Norris8b6e50c2011-05-25 14:59:01 -0700818 /*
819 * Apply this short delay always to ensure that we do wait tWB in
820 * any case on any machine.
821 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100822 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000823
824 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
827/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200828 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700829 * @chip: the nand chip descriptor
830 * @mtd: MTD device structure
831 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200832 *
833 * Used when in panic, no locks are taken.
834 */
835static void panic_nand_get_device(struct nand_chip *chip,
836 struct mtd_info *mtd, int new_state)
837{
Brian Norris7854d3f2011-06-23 14:12:08 -0700838 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200839 chip->controller->active = chip;
840 chip->state = new_state;
841}
842
843/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700845 * @mtd: MTD device structure
846 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 *
848 * Get the device and lock it for exclusive access
849 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200850static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800851nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100853 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200854 spinlock_t *lock = &chip->controller->lock;
855 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100856 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200857retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100858 spin_lock(lock);
859
vimal singhb8b3ee92009-07-09 20:41:22 +0530860 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200861 if (!chip->controller->active)
862 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200863
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200864 if (chip->controller->active == chip && chip->state == FL_READY) {
865 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100866 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100867 return 0;
868 }
869 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800870 if (chip->controller->active->state == FL_PM_SUSPENDED) {
871 chip->state = FL_PM_SUSPENDED;
872 spin_unlock(lock);
873 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800874 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100875 }
876 set_current_state(TASK_UNINTERRUPTIBLE);
877 add_wait_queue(wq, &wait);
878 spin_unlock(lock);
879 schedule();
880 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 goto retry;
882}
883
884/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700885 * panic_nand_wait - [GENERIC] wait until the command is done
886 * @mtd: MTD device structure
887 * @chip: NAND chip structure
888 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200889 *
890 * Wait for command done. This is a helper function for nand_wait used when
891 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400892 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200893 */
894static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
895 unsigned long timeo)
896{
897 int i;
898 for (i = 0; i < timeo; i++) {
899 if (chip->dev_ready) {
900 if (chip->dev_ready(mtd))
901 break;
902 } else {
903 if (chip->read_byte(mtd) & NAND_STATUS_READY)
904 break;
905 }
906 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200907 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200908}
909
910/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700911 * nand_wait - [DEFAULT] wait until the command is done
912 * @mtd: MTD device structure
913 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 *
Alex Smithb70af9b2015-10-06 14:52:07 +0100915 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700916 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200917static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919
Alex Smithb70af9b2015-10-06 14:52:07 +0100920 int status;
921 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Brian Norris8b6e50c2011-05-25 14:59:01 -0700923 /*
924 * Apply this short delay always to ensure that we do wait tWB in any
925 * case on any machine.
926 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100927 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200929 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200931 if (in_interrupt() || oops_in_progress)
932 panic_nand_wait(mtd, chip, timeo);
933 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800934 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +0100935 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200936 if (chip->dev_ready) {
937 if (chip->dev_ready(mtd))
938 break;
939 } else {
940 if (chip->read_byte(mtd) & NAND_STATUS_READY)
941 break;
942 }
943 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +0100944 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800946
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200947 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100948 /* This can happen if in case of timeout or buggy dev_ready */
949 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return status;
951}
952
953/**
Boris Brezillond8e725d2016-09-15 10:32:50 +0200954 * nand_reset_data_interface - Reset data interface and timings
955 * @chip: The NAND chip
956 *
957 * Reset the Data interface and timings to ONFI mode 0.
958 *
959 * Returns 0 for success or negative error code otherwise.
960 */
961static int nand_reset_data_interface(struct nand_chip *chip)
962{
963 struct mtd_info *mtd = nand_to_mtd(chip);
964 const struct nand_data_interface *conf;
965 int ret;
966
967 if (!chip->setup_data_interface)
968 return 0;
969
970 /*
971 * The ONFI specification says:
972 * "
973 * To transition from NV-DDR or NV-DDR2 to the SDR data
974 * interface, the host shall use the Reset (FFh) command
975 * using SDR timing mode 0. A device in any timing mode is
976 * required to recognize Reset (FFh) command issued in SDR
977 * timing mode 0.
978 * "
979 *
980 * Configure the data interface in SDR mode and set the
981 * timings to timing mode 0.
982 */
983
984 conf = nand_get_default_data_interface();
985 ret = chip->setup_data_interface(mtd, conf, false);
986 if (ret)
987 pr_err("Failed to configure data interface to SDR timing mode 0\n");
988
989 return ret;
990}
991
992/**
993 * nand_setup_data_interface - Setup the best data interface and timings
994 * @chip: The NAND chip
995 *
996 * Find and configure the best data interface and NAND timings supported by
997 * the chip and the driver.
998 * First tries to retrieve supported timing modes from ONFI information,
999 * and if the NAND chip does not support ONFI, relies on the
1000 * ->onfi_timing_mode_default specified in the nand_ids table.
1001 *
1002 * Returns 0 for success or negative error code otherwise.
1003 */
1004static int nand_setup_data_interface(struct nand_chip *chip)
1005{
1006 struct mtd_info *mtd = nand_to_mtd(chip);
1007 int ret;
1008
1009 if (!chip->setup_data_interface || !chip->data_interface)
1010 return 0;
1011
1012 /*
1013 * Ensure the timing mode has been changed on the chip side
1014 * before changing timings on the controller side.
1015 */
1016 if (chip->onfi_version) {
1017 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1018 chip->onfi_timing_mode_default,
1019 };
1020
1021 ret = chip->onfi_set_features(mtd, chip,
1022 ONFI_FEATURE_ADDR_TIMING_MODE,
1023 tmode_param);
1024 if (ret)
1025 goto err;
1026 }
1027
1028 ret = chip->setup_data_interface(mtd, chip->data_interface, false);
1029err:
1030 return ret;
1031}
1032
1033/**
1034 * nand_init_data_interface - find the best data interface and timings
1035 * @chip: The NAND chip
1036 *
1037 * Find the best data interface and NAND timings supported by the chip
1038 * and the driver.
1039 * First tries to retrieve supported timing modes from ONFI information,
1040 * and if the NAND chip does not support ONFI, relies on the
1041 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1042 * function nand_chip->data_interface is initialized with the best timing mode
1043 * available.
1044 *
1045 * Returns 0 for success or negative error code otherwise.
1046 */
1047static int nand_init_data_interface(struct nand_chip *chip)
1048{
1049 struct mtd_info *mtd = nand_to_mtd(chip);
1050 int modes, mode, ret;
1051
1052 if (!chip->setup_data_interface)
1053 return 0;
1054
1055 /*
1056 * First try to identify the best timings from ONFI parameters and
1057 * if the NAND does not support ONFI, fallback to the default ONFI
1058 * timing mode.
1059 */
1060 modes = onfi_get_async_timing_mode(chip);
1061 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1062 if (!chip->onfi_timing_mode_default)
1063 return 0;
1064
1065 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1066 }
1067
1068 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1069 GFP_KERNEL);
1070 if (!chip->data_interface)
1071 return -ENOMEM;
1072
1073 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1074 ret = onfi_init_data_interface(chip, chip->data_interface,
1075 NAND_SDR_IFACE, mode);
1076 if (ret)
1077 continue;
1078
1079 ret = chip->setup_data_interface(mtd, chip->data_interface,
1080 true);
1081 if (!ret) {
1082 chip->onfi_timing_mode_default = mode;
1083 break;
1084 }
1085 }
1086
1087 return 0;
1088}
1089
1090static void nand_release_data_interface(struct nand_chip *chip)
1091{
1092 kfree(chip->data_interface);
1093}
1094
1095/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001096 * nand_reset - Reset and initialize a NAND device
1097 * @chip: The NAND chip
1098 *
1099 * Returns 0 for success or negative error code otherwise
1100 */
1101int nand_reset(struct nand_chip *chip)
1102{
1103 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001104 int ret;
1105
1106 ret = nand_reset_data_interface(chip);
1107 if (ret)
1108 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001109
1110 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1111
Boris Brezillond8e725d2016-09-15 10:32:50 +02001112 ret = nand_setup_data_interface(chip);
1113 if (ret)
1114 return ret;
1115
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001116 return 0;
1117}
1118
1119/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001120 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001121 * @mtd: mtd info
1122 * @ofs: offset to start unlock from
1123 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -07001124 * @invert: when = 0, unlock the range of blocks within the lower and
1125 * upper boundary address
1126 * when = 1, unlock the range of blocks outside the boundaries
1127 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +05301128 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001129 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301130 */
1131static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1132 uint64_t len, int invert)
1133{
1134 int ret = 0;
1135 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001136 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301137
1138 /* Submit address of first page to unlock */
1139 page = ofs >> chip->page_shift;
1140 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1141
1142 /* Submit address of last page to unlock */
1143 page = (ofs + len) >> chip->page_shift;
1144 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1145 (page | invert) & chip->pagemask);
1146
1147 /* Call wait ready function */
1148 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301149 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001150 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001151 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301152 __func__, status);
1153 ret = -EIO;
1154 }
1155
1156 return ret;
1157}
1158
1159/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001160 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001161 * @mtd: mtd info
1162 * @ofs: offset to start unlock from
1163 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301164 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001165 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301166 */
1167int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1168{
1169 int ret = 0;
1170 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001171 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301172
Brian Norris289c0522011-07-19 10:06:09 -07001173 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301174 __func__, (unsigned long long)ofs, len);
1175
1176 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001177 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301178
1179 /* Align to last block address if size addresses end of the device */
1180 if (ofs + len == mtd->size)
1181 len -= mtd->erasesize;
1182
Huang Shijie6a8214a2012-11-19 14:43:30 +08001183 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301184
1185 /* Shift to get chip number */
1186 chipnr = ofs >> chip->chip_shift;
1187
1188 chip->select_chip(mtd, chipnr);
1189
White Ding57d3a9a2014-07-24 00:10:45 +08001190 /*
1191 * Reset the chip.
1192 * If we want to check the WP through READ STATUS and check the bit 7
1193 * we must reset the chip
1194 * some operation can also clear the bit 7 of status register
1195 * eg. erase/program a locked block
1196 */
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001197 nand_reset(chip);
White Ding57d3a9a2014-07-24 00:10:45 +08001198
Vimal Singh7d70f332010-02-08 15:50:49 +05301199 /* Check, if it is write protected */
1200 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001201 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301202 __func__);
1203 ret = -EIO;
1204 goto out;
1205 }
1206
1207 ret = __nand_unlock(mtd, ofs, len, 0);
1208
1209out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001210 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301211 nand_release_device(mtd);
1212
1213 return ret;
1214}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001215EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301216
1217/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001218 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001219 * @mtd: mtd info
1220 * @ofs: offset to start unlock from
1221 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301222 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001223 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1224 * have this feature, but it allows only to lock all blocks, not for specified
1225 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1226 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301227 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001228 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301229 */
1230int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1231{
1232 int ret = 0;
1233 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001234 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301235
Brian Norris289c0522011-07-19 10:06:09 -07001236 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301237 __func__, (unsigned long long)ofs, len);
1238
1239 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001240 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301241
Huang Shijie6a8214a2012-11-19 14:43:30 +08001242 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301243
1244 /* Shift to get chip number */
1245 chipnr = ofs >> chip->chip_shift;
1246
1247 chip->select_chip(mtd, chipnr);
1248
White Ding57d3a9a2014-07-24 00:10:45 +08001249 /*
1250 * Reset the chip.
1251 * If we want to check the WP through READ STATUS and check the bit 7
1252 * we must reset the chip
1253 * some operation can also clear the bit 7 of status register
1254 * eg. erase/program a locked block
1255 */
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001256 nand_reset(chip);
White Ding57d3a9a2014-07-24 00:10:45 +08001257
Vimal Singh7d70f332010-02-08 15:50:49 +05301258 /* Check, if it is write protected */
1259 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001260 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301261 __func__);
1262 status = MTD_ERASE_FAILED;
1263 ret = -EIO;
1264 goto out;
1265 }
1266
1267 /* Submit address of first page to lock */
1268 page = ofs >> chip->page_shift;
1269 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1270
1271 /* Call wait ready function */
1272 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301273 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001274 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001275 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301276 __func__, status);
1277 ret = -EIO;
1278 goto out;
1279 }
1280
1281 ret = __nand_unlock(mtd, ofs, len, 0x1);
1282
1283out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001284 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301285 nand_release_device(mtd);
1286
1287 return ret;
1288}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001289EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301290
1291/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001292 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1293 * @buf: buffer to test
1294 * @len: buffer length
1295 * @bitflips_threshold: maximum number of bitflips
1296 *
1297 * Check if a buffer contains only 0xff, which means the underlying region
1298 * has been erased and is ready to be programmed.
1299 * The bitflips_threshold specify the maximum number of bitflips before
1300 * considering the region is not erased.
1301 * Note: The logic of this function has been extracted from the memweight
1302 * implementation, except that nand_check_erased_buf function exit before
1303 * testing the whole buffer if the number of bitflips exceed the
1304 * bitflips_threshold value.
1305 *
1306 * Returns a positive number of bitflips less than or equal to
1307 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1308 * threshold.
1309 */
1310static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1311{
1312 const unsigned char *bitmap = buf;
1313 int bitflips = 0;
1314 int weight;
1315
1316 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1317 len--, bitmap++) {
1318 weight = hweight8(*bitmap);
1319 bitflips += BITS_PER_BYTE - weight;
1320 if (unlikely(bitflips > bitflips_threshold))
1321 return -EBADMSG;
1322 }
1323
1324 for (; len >= sizeof(long);
1325 len -= sizeof(long), bitmap += sizeof(long)) {
1326 weight = hweight_long(*((unsigned long *)bitmap));
1327 bitflips += BITS_PER_LONG - weight;
1328 if (unlikely(bitflips > bitflips_threshold))
1329 return -EBADMSG;
1330 }
1331
1332 for (; len > 0; len--, bitmap++) {
1333 weight = hweight8(*bitmap);
1334 bitflips += BITS_PER_BYTE - weight;
1335 if (unlikely(bitflips > bitflips_threshold))
1336 return -EBADMSG;
1337 }
1338
1339 return bitflips;
1340}
1341
1342/**
1343 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1344 * 0xff data
1345 * @data: data buffer to test
1346 * @datalen: data length
1347 * @ecc: ECC buffer
1348 * @ecclen: ECC length
1349 * @extraoob: extra OOB buffer
1350 * @extraooblen: extra OOB length
1351 * @bitflips_threshold: maximum number of bitflips
1352 *
1353 * Check if a data buffer and its associated ECC and OOB data contains only
1354 * 0xff pattern, which means the underlying region has been erased and is
1355 * ready to be programmed.
1356 * The bitflips_threshold specify the maximum number of bitflips before
1357 * considering the region as not erased.
1358 *
1359 * Note:
1360 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1361 * different from the NAND page size. When fixing bitflips, ECC engines will
1362 * report the number of errors per chunk, and the NAND core infrastructure
1363 * expect you to return the maximum number of bitflips for the whole page.
1364 * This is why you should always use this function on a single chunk and
1365 * not on the whole page. After checking each chunk you should update your
1366 * max_bitflips value accordingly.
1367 * 2/ When checking for bitflips in erased pages you should not only check
1368 * the payload data but also their associated ECC data, because a user might
1369 * have programmed almost all bits to 1 but a few. In this case, we
1370 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1371 * this case.
1372 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1373 * data are protected by the ECC engine.
1374 * It could also be used if you support subpages and want to attach some
1375 * extra OOB data to an ECC chunk.
1376 *
1377 * Returns a positive number of bitflips less than or equal to
1378 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1379 * threshold. In case of success, the passed buffers are filled with 0xff.
1380 */
1381int nand_check_erased_ecc_chunk(void *data, int datalen,
1382 void *ecc, int ecclen,
1383 void *extraoob, int extraooblen,
1384 int bitflips_threshold)
1385{
1386 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1387
1388 data_bitflips = nand_check_erased_buf(data, datalen,
1389 bitflips_threshold);
1390 if (data_bitflips < 0)
1391 return data_bitflips;
1392
1393 bitflips_threshold -= data_bitflips;
1394
1395 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1396 if (ecc_bitflips < 0)
1397 return ecc_bitflips;
1398
1399 bitflips_threshold -= ecc_bitflips;
1400
1401 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1402 bitflips_threshold);
1403 if (extraoob_bitflips < 0)
1404 return extraoob_bitflips;
1405
1406 if (data_bitflips)
1407 memset(data, 0xff, datalen);
1408
1409 if (ecc_bitflips)
1410 memset(ecc, 0xff, ecclen);
1411
1412 if (extraoob_bitflips)
1413 memset(extraoob, 0xff, extraooblen);
1414
1415 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1416}
1417EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1418
1419/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001420 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001421 * @mtd: mtd info structure
1422 * @chip: nand chip info structure
1423 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001424 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001425 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001426 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001427 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001428 */
1429static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001430 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001431{
1432 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001433 if (oob_required)
1434 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001435 return 0;
1436}
1437
1438/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001439 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001440 * @mtd: mtd info structure
1441 * @chip: nand chip info structure
1442 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001443 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001444 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001445 *
1446 * We need a special oob layout and handling even when OOB isn't used.
1447 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001448static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001449 struct nand_chip *chip, uint8_t *buf,
1450 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001451{
1452 int eccsize = chip->ecc.size;
1453 int eccbytes = chip->ecc.bytes;
1454 uint8_t *oob = chip->oob_poi;
1455 int steps, size;
1456
1457 for (steps = chip->ecc.steps; steps > 0; steps--) {
1458 chip->read_buf(mtd, buf, eccsize);
1459 buf += eccsize;
1460
1461 if (chip->ecc.prepad) {
1462 chip->read_buf(mtd, oob, chip->ecc.prepad);
1463 oob += chip->ecc.prepad;
1464 }
1465
1466 chip->read_buf(mtd, oob, eccbytes);
1467 oob += eccbytes;
1468
1469 if (chip->ecc.postpad) {
1470 chip->read_buf(mtd, oob, chip->ecc.postpad);
1471 oob += chip->ecc.postpad;
1472 }
1473 }
1474
1475 size = mtd->oobsize - (oob - chip->oob_poi);
1476 if (size)
1477 chip->read_buf(mtd, oob, size);
1478
1479 return 0;
1480}
1481
1482/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001483 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001484 * @mtd: mtd info structure
1485 * @chip: nand chip info structure
1486 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001487 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001488 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001489 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001490static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001491 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Boris Brezillon846031d2016-02-03 20:11:00 +01001493 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001494 int eccbytes = chip->ecc.bytes;
1495 int eccsteps = chip->ecc.steps;
1496 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001497 uint8_t *ecc_calc = chip->buffers->ecccalc;
1498 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001499 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001500
Brian Norris1fbb9382012-05-02 10:14:55 -07001501 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001502
1503 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1504 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1505
Boris Brezillon846031d2016-02-03 20:11:00 +01001506 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1507 chip->ecc.total);
1508 if (ret)
1509 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001510
1511 eccsteps = chip->ecc.steps;
1512 p = buf;
1513
1514 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1515 int stat;
1516
1517 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001518 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001519 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001520 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001521 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001522 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1523 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001524 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001525 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001526}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301529 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001530 * @mtd: mtd info structure
1531 * @chip: nand chip info structure
1532 * @data_offs: offset of requested data within the page
1533 * @readlen: data length
1534 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001535 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001536 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001537static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001538 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1539 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001540{
Boris Brezillon846031d2016-02-03 20:11:00 +01001541 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001542 uint8_t *p;
1543 int data_col_addr, i, gaps = 0;
1544 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1545 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001546 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001547 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001548 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001549
Brian Norris7854d3f2011-06-23 14:12:08 -07001550 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001551 start_step = data_offs / chip->ecc.size;
1552 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1553 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301554 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001555
Brian Norris8b6e50c2011-05-25 14:59:01 -07001556 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001557 datafrag_len = num_steps * chip->ecc.size;
1558 eccfrag_len = num_steps * chip->ecc.bytes;
1559
1560 data_col_addr = start_step * chip->ecc.size;
1561 /* If we read not a page aligned data */
1562 if (data_col_addr != 0)
1563 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1564
1565 p = bufpoi + data_col_addr;
1566 chip->read_buf(mtd, p, datafrag_len);
1567
Brian Norris8b6e50c2011-05-25 14:59:01 -07001568 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001569 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1570 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1571
Brian Norris8b6e50c2011-05-25 14:59:01 -07001572 /*
1573 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001574 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001575 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001576 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1577 if (ret)
1578 return ret;
1579
1580 if (oobregion.length < eccfrag_len)
1581 gaps = 1;
1582
Alexey Korolev3d459552008-05-15 17:23:18 +01001583 if (gaps) {
1584 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1585 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1586 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001587 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001588 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001589 * about buswidth alignment in read_buf.
1590 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001591 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001592 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001593 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001594 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001595 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1596 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001597 aligned_len++;
1598
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001599 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001600 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001601 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1602 }
1603
Boris Brezillon846031d2016-02-03 20:11:00 +01001604 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1605 chip->oob_poi, index, eccfrag_len);
1606 if (ret)
1607 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001608
1609 p = bufpoi + data_col_addr;
1610 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1611 int stat;
1612
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001613 stat = chip->ecc.correct(mtd, p,
1614 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001615 if (stat == -EBADMSG &&
1616 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1617 /* check for empty pages with bitflips */
1618 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1619 &chip->buffers->ecccode[i],
1620 chip->ecc.bytes,
1621 NULL, 0,
1622 chip->ecc.strength);
1623 }
1624
Mike Dunn3f91e942012-04-25 12:06:09 -07001625 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001626 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001627 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001628 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001629 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1630 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001631 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001632 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001633}
1634
1635/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001636 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001637 * @mtd: mtd info structure
1638 * @chip: nand chip info structure
1639 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001640 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001641 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001642 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001643 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001644 */
1645static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001646 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001647{
Boris Brezillon846031d2016-02-03 20:11:00 +01001648 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001649 int eccbytes = chip->ecc.bytes;
1650 int eccsteps = chip->ecc.steps;
1651 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001652 uint8_t *ecc_calc = chip->buffers->ecccalc;
1653 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001654 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001655
1656 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1657 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1658 chip->read_buf(mtd, p, eccsize);
1659 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1660 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001661 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001662
Boris Brezillon846031d2016-02-03 20:11:00 +01001663 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1664 chip->ecc.total);
1665 if (ret)
1666 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001667
1668 eccsteps = chip->ecc.steps;
1669 p = buf;
1670
1671 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1672 int stat;
1673
1674 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001675 if (stat == -EBADMSG &&
1676 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1677 /* check for empty pages with bitflips */
1678 stat = nand_check_erased_ecc_chunk(p, eccsize,
1679 &ecc_code[i], eccbytes,
1680 NULL, 0,
1681 chip->ecc.strength);
1682 }
1683
Mike Dunn3f91e942012-04-25 12:06:09 -07001684 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001685 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001686 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001687 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001688 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1689 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001690 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001691 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001692}
1693
1694/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001695 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001696 * @mtd: mtd info structure
1697 * @chip: nand chip info structure
1698 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001699 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001700 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001701 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001702 * Hardware ECC for large page chips, require OOB to be read first. For this
1703 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1704 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1705 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1706 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001707 */
1708static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001709 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001710{
Boris Brezillon846031d2016-02-03 20:11:00 +01001711 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001712 int eccbytes = chip->ecc.bytes;
1713 int eccsteps = chip->ecc.steps;
1714 uint8_t *p = buf;
1715 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001716 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001717 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001718
1719 /* Read the OOB area first */
1720 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1721 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1722 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1723
Boris Brezillon846031d2016-02-03 20:11:00 +01001724 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1725 chip->ecc.total);
1726 if (ret)
1727 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001728
1729 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1730 int stat;
1731
1732 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1733 chip->read_buf(mtd, p, eccsize);
1734 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1735
1736 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001737 if (stat == -EBADMSG &&
1738 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1739 /* check for empty pages with bitflips */
1740 stat = nand_check_erased_ecc_chunk(p, eccsize,
1741 &ecc_code[i], eccbytes,
1742 NULL, 0,
1743 chip->ecc.strength);
1744 }
1745
Mike Dunn3f91e942012-04-25 12:06:09 -07001746 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001747 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001748 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001749 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001750 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1751 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001752 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001753 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001754}
1755
1756/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001757 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001758 * @mtd: mtd info structure
1759 * @chip: nand chip info structure
1760 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001761 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001762 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001763 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001764 * The hw generator calculates the error syndrome automatically. Therefore we
1765 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001766 */
1767static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001768 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001769{
1770 int i, eccsize = chip->ecc.size;
1771 int eccbytes = chip->ecc.bytes;
1772 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001773 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001774 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001775 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001776 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001777
1778 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1779 int stat;
1780
1781 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1782 chip->read_buf(mtd, p, eccsize);
1783
1784 if (chip->ecc.prepad) {
1785 chip->read_buf(mtd, oob, chip->ecc.prepad);
1786 oob += chip->ecc.prepad;
1787 }
1788
1789 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1790 chip->read_buf(mtd, oob, eccbytes);
1791 stat = chip->ecc.correct(mtd, p, oob, NULL);
1792
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001793 oob += eccbytes;
1794
1795 if (chip->ecc.postpad) {
1796 chip->read_buf(mtd, oob, chip->ecc.postpad);
1797 oob += chip->ecc.postpad;
1798 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001799
1800 if (stat == -EBADMSG &&
1801 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1802 /* check for empty pages with bitflips */
1803 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1804 oob - eccpadbytes,
1805 eccpadbytes,
1806 NULL, 0,
1807 chip->ecc.strength);
1808 }
1809
1810 if (stat < 0) {
1811 mtd->ecc_stats.failed++;
1812 } else {
1813 mtd->ecc_stats.corrected += stat;
1814 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1815 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001816 }
1817
1818 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001819 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001820 if (i)
1821 chip->read_buf(mtd, oob, i);
1822
Mike Dunn3f91e942012-04-25 12:06:09 -07001823 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001824}
1825
1826/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001827 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001828 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001829 * @oob: oob destination address
1830 * @ops: oob ops structure
1831 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001832 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001833static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001834 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001835{
Boris Brezillon846031d2016-02-03 20:11:00 +01001836 struct nand_chip *chip = mtd_to_nand(mtd);
1837 int ret;
1838
Florian Fainellif8ac0412010-09-07 13:23:43 +02001839 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001840
Brian Norris0612b9d2011-08-30 18:45:40 -07001841 case MTD_OPS_PLACE_OOB:
1842 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001843 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1844 return oob + len;
1845
Boris Brezillon846031d2016-02-03 20:11:00 +01001846 case MTD_OPS_AUTO_OOB:
1847 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1848 ops->ooboffs, len);
1849 BUG_ON(ret);
1850 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001851
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001852 default:
1853 BUG();
1854 }
1855 return NULL;
1856}
1857
1858/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001859 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1860 * @mtd: MTD device structure
1861 * @retry_mode: the retry mode to use
1862 *
1863 * Some vendors supply a special command to shift the Vt threshold, to be used
1864 * when there are too many bitflips in a page (i.e., ECC error). After setting
1865 * a new threshold, the host should retry reading the page.
1866 */
1867static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1868{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001869 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001870
1871 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1872
1873 if (retry_mode >= chip->read_retries)
1874 return -EINVAL;
1875
1876 if (!chip->setup_read_retry)
1877 return -EOPNOTSUPP;
1878
1879 return chip->setup_read_retry(mtd, retry_mode);
1880}
1881
1882/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001883 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001884 * @mtd: MTD device structure
1885 * @from: offset to read from
1886 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001887 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001888 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001889 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001890static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1891 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001892{
Brian Norrise47f3db2012-05-02 10:14:56 -07001893 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001894 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001895 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001896 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001897 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001898 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001899
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001900 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001901 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001902 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001903 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001904 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001906 chipnr = (int)(from >> chip->chip_shift);
1907 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001909 realpage = (int)(from >> chip->page_shift);
1910 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001912 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001914 buf = ops->datbuf;
1915 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001916 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001917
Florian Fainellif8ac0412010-09-07 13:23:43 +02001918 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001919 unsigned int ecc_failures = mtd->ecc_stats.failed;
1920
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001921 bytes = min(mtd->writesize - col, readlen);
1922 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001923
Kamal Dasu66507c72014-05-01 20:51:19 -04001924 if (!aligned)
1925 use_bufpoi = 1;
1926 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1927 use_bufpoi = !virt_addr_valid(buf);
1928 else
1929 use_bufpoi = 0;
1930
Brian Norris8b6e50c2011-05-25 14:59:01 -07001931 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001932 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001933 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1934
1935 if (use_bufpoi && aligned)
1936 pr_debug("%s: using read bounce buffer for buf@%p\n",
1937 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Brian Norrisba84fb52014-01-03 15:13:33 -08001939read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001940 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Mike Dunnedbc45402012-04-25 12:06:11 -07001942 /*
1943 * Now read the page into the buffer. Absent an error,
1944 * the read methods return max bitflips per ecc step.
1945 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001946 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001947 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001948 oob_required,
1949 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001950 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1951 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001952 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001953 col, bytes, bufpoi,
1954 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001955 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001956 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001957 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001958 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001959 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001960 /* Invalidate page cache */
1961 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001962 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001963 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001964
Mike Dunnedbc45402012-04-25 12:06:11 -07001965 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1966
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001967 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001968 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001969 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001970 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001971 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001972 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001973 chip->pagebuf_bitflips = ret;
1974 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001975 /* Invalidate page cache */
1976 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001977 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001978 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001980
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001981 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001982 int toread = min(oobreadlen, max_oobsize);
1983
1984 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01001985 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001986 oob, ops, toread);
1987 oobreadlen -= toread;
1988 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001989 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001990
1991 if (chip->options & NAND_NEED_READRDY) {
1992 /* Apply delay or wait for ready/busy pin */
1993 if (!chip->dev_ready)
1994 udelay(chip->chip_delay);
1995 else
1996 nand_wait_ready(mtd);
1997 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001998
Brian Norrisba84fb52014-01-03 15:13:33 -08001999 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08002000 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08002001 retry_mode++;
2002 ret = nand_setup_read_retry(mtd,
2003 retry_mode);
2004 if (ret < 0)
2005 break;
2006
2007 /* Reset failures; retry */
2008 mtd->ecc_stats.failed = ecc_failures;
2009 goto read_retry;
2010 } else {
2011 /* No more retry modes; real failure */
2012 ecc_fail = true;
2013 }
2014 }
2015
2016 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002017 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002018 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002019 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07002020 max_bitflips = max_t(unsigned int, max_bitflips,
2021 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002024 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002025
Brian Norrisba84fb52014-01-03 15:13:33 -08002026 /* Reset to retry mode 0 */
2027 if (retry_mode) {
2028 ret = nand_setup_read_retry(mtd, 0);
2029 if (ret < 0)
2030 break;
2031 retry_mode = 0;
2032 }
2033
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002034 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002035 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Brian Norris8b6e50c2011-05-25 14:59:01 -07002037 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 col = 0;
2039 /* Increment page address */
2040 realpage++;
2041
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002042 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 /* Check, if we cross a chip boundary */
2044 if (!page) {
2045 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002046 chip->select_chip(mtd, -1);
2047 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002050 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002052 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002053 if (oob)
2054 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Mike Dunn3f91e942012-04-25 12:06:09 -07002056 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002057 return ret;
2058
Brian Norrisb72f3df2013-12-03 11:04:14 -08002059 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002060 return -EBADMSG;
2061
Mike Dunnedbc45402012-04-25 12:06:11 -07002062 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002063}
2064
2065/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002066 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002067 * @mtd: MTD device structure
2068 * @from: offset to read from
2069 * @len: number of bytes to read
2070 * @retlen: pointer to variable to store the number of read bytes
2071 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002072 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002073 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002074 */
2075static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2076 size_t *retlen, uint8_t *buf)
2077{
Brian Norris4a89ff82011-08-30 18:45:45 -07002078 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002079 int ret;
2080
Huang Shijie6a8214a2012-11-19 14:43:30 +08002081 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002082 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002083 ops.len = len;
2084 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002085 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002086 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002087 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002088 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002089 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
2092/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002093 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002094 * @mtd: mtd info structure
2095 * @chip: nand chip info structure
2096 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002097 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002098int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002099{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002100 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002101 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002102 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002103}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002104EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002105
2106/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002107 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002108 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002109 * @mtd: mtd info structure
2110 * @chip: nand chip info structure
2111 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002112 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002113int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2114 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002115{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002116 int length = mtd->oobsize;
2117 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2118 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002119 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002120 int i, toread, sndrnd = 0, pos;
2121
2122 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2123 for (i = 0; i < chip->ecc.steps; i++) {
2124 if (sndrnd) {
2125 pos = eccsize + i * (eccsize + chunk);
2126 if (mtd->writesize > 512)
2127 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2128 else
2129 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2130 } else
2131 sndrnd = 1;
2132 toread = min_t(int, length, chunk);
2133 chip->read_buf(mtd, bufpoi, toread);
2134 bufpoi += toread;
2135 length -= toread;
2136 }
2137 if (length > 0)
2138 chip->read_buf(mtd, bufpoi, length);
2139
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002140 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002141}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002142EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002143
2144/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002145 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002146 * @mtd: mtd info structure
2147 * @chip: nand chip info structure
2148 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002149 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002150int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002151{
2152 int status = 0;
2153 const uint8_t *buf = chip->oob_poi;
2154 int length = mtd->oobsize;
2155
2156 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2157 chip->write_buf(mtd, buf, length);
2158 /* Send command to program the OOB data */
2159 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2160
2161 status = chip->waitfunc(mtd, chip);
2162
Savin Zlobec0d420f92006-06-21 11:51:20 +02002163 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002164}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002165EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002166
2167/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002168 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002169 * with syndrome - only for large page flash
2170 * @mtd: mtd info structure
2171 * @chip: nand chip info structure
2172 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002173 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002174int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2175 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002176{
2177 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2178 int eccsize = chip->ecc.size, length = mtd->oobsize;
2179 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2180 const uint8_t *bufpoi = chip->oob_poi;
2181
2182 /*
2183 * data-ecc-data-ecc ... ecc-oob
2184 * or
2185 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2186 */
2187 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2188 pos = steps * (eccsize + chunk);
2189 steps = 0;
2190 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002191 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002192
2193 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2194 for (i = 0; i < steps; i++) {
2195 if (sndcmd) {
2196 if (mtd->writesize <= 512) {
2197 uint32_t fill = 0xFFFFFFFF;
2198
2199 len = eccsize;
2200 while (len > 0) {
2201 int num = min_t(int, len, 4);
2202 chip->write_buf(mtd, (uint8_t *)&fill,
2203 num);
2204 len -= num;
2205 }
2206 } else {
2207 pos = eccsize + i * (eccsize + chunk);
2208 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2209 }
2210 } else
2211 sndcmd = 1;
2212 len = min_t(int, length, chunk);
2213 chip->write_buf(mtd, bufpoi, len);
2214 bufpoi += len;
2215 length -= len;
2216 }
2217 if (length > 0)
2218 chip->write_buf(mtd, bufpoi, length);
2219
2220 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2221 status = chip->waitfunc(mtd, chip);
2222
2223 return status & NAND_STATUS_FAIL ? -EIO : 0;
2224}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002225EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002226
2227/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002228 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002229 * @mtd: MTD device structure
2230 * @from: offset to read from
2231 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002233 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002235static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2236 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
Brian Norrisc00a0992012-05-01 17:12:54 -07002238 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002239 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002240 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002241 int readlen = ops->ooblen;
2242 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002243 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002244 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Brian Norris289c0522011-07-19 10:06:09 -07002246 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302247 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Brian Norris041e4572011-06-23 16:45:24 -07002249 stats = mtd->ecc_stats;
2250
Boris BREZILLON29f10582016-03-07 10:46:52 +01002251 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002252
2253 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002254 pr_debug("%s: attempt to start read outside oob\n",
2255 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002256 return -EINVAL;
2257 }
2258
2259 /* Do not allow reads past end of device */
2260 if (unlikely(from >= mtd->size ||
2261 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2262 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002263 pr_debug("%s: attempt to read beyond end of device\n",
2264 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002265 return -EINVAL;
2266 }
Vitaly Wool70145682006-11-03 18:20:38 +03002267
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002268 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002269 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002271 /* Shift to get page */
2272 realpage = (int)(from >> chip->page_shift);
2273 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Florian Fainellif8ac0412010-09-07 13:23:43 +02002275 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002276 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002277 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002278 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002279 ret = chip->ecc.read_oob(mtd, chip, page);
2280
2281 if (ret < 0)
2282 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002283
2284 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002285 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002286
Brian Norris5bc7c332013-03-13 09:51:31 -07002287 if (chip->options & NAND_NEED_READRDY) {
2288 /* Apply delay or wait for ready/busy pin */
2289 if (!chip->dev_ready)
2290 udelay(chip->chip_delay);
2291 else
2292 nand_wait_ready(mtd);
2293 }
2294
Vitaly Wool70145682006-11-03 18:20:38 +03002295 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002296 if (!readlen)
2297 break;
2298
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002299 /* Increment page address */
2300 realpage++;
2301
2302 page = realpage & chip->pagemask;
2303 /* Check, if we cross a chip boundary */
2304 if (!page) {
2305 chipnr++;
2306 chip->select_chip(mtd, -1);
2307 chip->select_chip(mtd, chipnr);
2308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002310 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002312 ops->oobretlen = ops->ooblen - readlen;
2313
2314 if (ret < 0)
2315 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002316
2317 if (mtd->ecc_stats.failed - stats.failed)
2318 return -EBADMSG;
2319
2320 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
2323/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002324 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002325 * @mtd: MTD device structure
2326 * @from: offset to read from
2327 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002329 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002331static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2332 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002334 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002335
2336 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002339 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002340 pr_debug("%s: attempt to read beyond end of device\n",
2341 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 return -EINVAL;
2343 }
2344
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002345 if (ops->mode != MTD_OPS_PLACE_OOB &&
2346 ops->mode != MTD_OPS_AUTO_OOB &&
2347 ops->mode != MTD_OPS_RAW)
2348 return -ENOTSUPP;
2349
Huang Shijie6a8214a2012-11-19 14:43:30 +08002350 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002352 if (!ops->datbuf)
2353 ret = nand_do_read_oob(mtd, from, ops);
2354 else
2355 ret = nand_do_read_ops(mtd, from, ops);
2356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002358 return ret;
2359}
2360
2361
2362/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002363 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002364 * @mtd: mtd info structure
2365 * @chip: nand chip info structure
2366 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002367 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002368 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002369 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002370 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002371 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002372static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002373 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002374{
2375 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002376 if (oob_required)
2377 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002378
2379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
2381
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002382/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002383 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002384 * @mtd: mtd info structure
2385 * @chip: nand chip info structure
2386 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002387 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002388 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002389 *
2390 * We need a special oob layout and handling even when ECC isn't checked.
2391 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002392static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002393 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002394 const uint8_t *buf, int oob_required,
2395 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002396{
2397 int eccsize = chip->ecc.size;
2398 int eccbytes = chip->ecc.bytes;
2399 uint8_t *oob = chip->oob_poi;
2400 int steps, size;
2401
2402 for (steps = chip->ecc.steps; steps > 0; steps--) {
2403 chip->write_buf(mtd, buf, eccsize);
2404 buf += eccsize;
2405
2406 if (chip->ecc.prepad) {
2407 chip->write_buf(mtd, oob, chip->ecc.prepad);
2408 oob += chip->ecc.prepad;
2409 }
2410
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002411 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002412 oob += eccbytes;
2413
2414 if (chip->ecc.postpad) {
2415 chip->write_buf(mtd, oob, chip->ecc.postpad);
2416 oob += chip->ecc.postpad;
2417 }
2418 }
2419
2420 size = mtd->oobsize - (oob - chip->oob_poi);
2421 if (size)
2422 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002423
2424 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002425}
2426/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002427 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002428 * @mtd: mtd info structure
2429 * @chip: nand chip info structure
2430 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002431 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002432 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002433 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002434static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002435 const uint8_t *buf, int oob_required,
2436 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002437{
Boris Brezillon846031d2016-02-03 20:11:00 +01002438 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002439 int eccbytes = chip->ecc.bytes;
2440 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002441 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002442 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002443
Brian Norris7854d3f2011-06-23 14:12:08 -07002444 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002445 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2446 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002447
Boris Brezillon846031d2016-02-03 20:11:00 +01002448 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2449 chip->ecc.total);
2450 if (ret)
2451 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002452
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002453 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002454}
2455
2456/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002457 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002458 * @mtd: mtd info structure
2459 * @chip: nand chip info structure
2460 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002461 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002462 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002463 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002464static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002465 const uint8_t *buf, int oob_required,
2466 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002467{
Boris Brezillon846031d2016-02-03 20:11:00 +01002468 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002469 int eccbytes = chip->ecc.bytes;
2470 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002471 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002472 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002473
2474 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2475 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002476 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002477 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2478 }
2479
Boris Brezillon846031d2016-02-03 20:11:00 +01002480 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2481 chip->ecc.total);
2482 if (ret)
2483 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002484
2485 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002486
2487 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002488}
2489
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302490
2491/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002492 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302493 * @mtd: mtd info structure
2494 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002495 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302496 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002497 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302498 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002499 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302500 */
2501static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2502 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002503 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002504 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302505{
2506 uint8_t *oob_buf = chip->oob_poi;
2507 uint8_t *ecc_calc = chip->buffers->ecccalc;
2508 int ecc_size = chip->ecc.size;
2509 int ecc_bytes = chip->ecc.bytes;
2510 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302511 uint32_t start_step = offset / ecc_size;
2512 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2513 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002514 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302515
2516 for (step = 0; step < ecc_steps; step++) {
2517 /* configure controller for WRITE access */
2518 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2519
2520 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002521 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302522
2523 /* mask ECC of un-touched subpages by padding 0xFF */
2524 if ((step < start_step) || (step > end_step))
2525 memset(ecc_calc, 0xff, ecc_bytes);
2526 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002527 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302528
2529 /* mask OOB of un-touched subpages by padding 0xFF */
2530 /* if oob_required, preserve OOB metadata of written subpage */
2531 if (!oob_required || (step < start_step) || (step > end_step))
2532 memset(oob_buf, 0xff, oob_bytes);
2533
Brian Norrisd6a950802013-08-08 17:16:36 -07002534 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302535 ecc_calc += ecc_bytes;
2536 oob_buf += oob_bytes;
2537 }
2538
2539 /* copy calculated ECC for whole page to chip->buffer->oob */
2540 /* this include masked-value(0xFF) for unwritten subpages */
2541 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002542 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2543 chip->ecc.total);
2544 if (ret)
2545 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302546
2547 /* write OOB buffer to NAND device */
2548 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2549
2550 return 0;
2551}
2552
2553
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002554/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002555 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002556 * @mtd: mtd info structure
2557 * @chip: nand chip info structure
2558 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002559 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002560 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002561 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002562 * The hw generator calculates the error syndrome automatically. Therefore we
2563 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002564 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002565static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002566 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002567 const uint8_t *buf, int oob_required,
2568 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002569{
2570 int i, eccsize = chip->ecc.size;
2571 int eccbytes = chip->ecc.bytes;
2572 int eccsteps = chip->ecc.steps;
2573 const uint8_t *p = buf;
2574 uint8_t *oob = chip->oob_poi;
2575
2576 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2577
2578 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2579 chip->write_buf(mtd, p, eccsize);
2580
2581 if (chip->ecc.prepad) {
2582 chip->write_buf(mtd, oob, chip->ecc.prepad);
2583 oob += chip->ecc.prepad;
2584 }
2585
2586 chip->ecc.calculate(mtd, p, oob);
2587 chip->write_buf(mtd, oob, eccbytes);
2588 oob += eccbytes;
2589
2590 if (chip->ecc.postpad) {
2591 chip->write_buf(mtd, oob, chip->ecc.postpad);
2592 oob += chip->ecc.postpad;
2593 }
2594 }
2595
2596 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002597 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002598 if (i)
2599 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002600
2601 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002602}
2603
2604/**
David Woodhouse956e9442006-09-25 17:12:39 +01002605 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002606 * @mtd: MTD device structure
2607 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302608 * @offset: address offset within the page
2609 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002610 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002611 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002612 * @page: page number to write
2613 * @cached: cached programming
2614 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002615 */
2616static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302617 uint32_t offset, int data_len, const uint8_t *buf,
2618 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002619{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302620 int status, subpage;
2621
2622 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2623 chip->ecc.write_subpage)
2624 subpage = offset || (data_len < mtd->writesize);
2625 else
2626 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002627
2628 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2629
David Woodhouse956e9442006-09-25 17:12:39 +01002630 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302631 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002632 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302633 else if (subpage)
2634 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002635 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002636 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002637 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2638 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002639
2640 if (status < 0)
2641 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002642
2643 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002644 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002645 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002646 */
2647 cached = 0;
2648
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002649 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002650
2651 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002652 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002653 /*
2654 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002655 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002656 */
2657 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2658 status = chip->errstat(mtd, chip, FL_WRITING, status,
2659 page);
2660
2661 if (status & NAND_STATUS_FAIL)
2662 return -EIO;
2663 } else {
2664 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002665 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002666 }
2667
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002668 return 0;
2669}
2670
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002671/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002672 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002673 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002674 * @oob: oob data buffer
2675 * @len: oob data write length
2676 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002677 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002678static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2679 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002680{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002681 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002682 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002683
2684 /*
2685 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2686 * data from a previous OOB read.
2687 */
2688 memset(chip->oob_poi, 0xff, mtd->oobsize);
2689
Florian Fainellif8ac0412010-09-07 13:23:43 +02002690 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002691
Brian Norris0612b9d2011-08-30 18:45:40 -07002692 case MTD_OPS_PLACE_OOB:
2693 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002694 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2695 return oob + len;
2696
Boris Brezillon846031d2016-02-03 20:11:00 +01002697 case MTD_OPS_AUTO_OOB:
2698 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2699 ops->ooboffs, len);
2700 BUG_ON(ret);
2701 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002702
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002703 default:
2704 BUG();
2705 }
2706 return NULL;
2707}
2708
Florian Fainellif8ac0412010-09-07 13:23:43 +02002709#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002710
2711/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002712 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002713 * @mtd: MTD device structure
2714 * @to: offset to write to
2715 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002716 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002717 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002718 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002719static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2720 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002721{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002722 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002723 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002724 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002725
2726 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002727 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002728
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002729 uint8_t *oob = ops->oobbuf;
2730 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302731 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002732 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002733
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002734 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002735 if (!writelen)
2736 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002737
Brian Norris8b6e50c2011-05-25 14:59:01 -07002738 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002739 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002740 pr_notice("%s: attempt to write non page aligned data\n",
2741 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002742 return -EINVAL;
2743 }
2744
Thomas Gleixner29072b92006-09-28 15:38:36 +02002745 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002746
Thomas Gleixner6a930962006-06-28 00:11:45 +02002747 chipnr = (int)(to >> chip->chip_shift);
2748 chip->select_chip(mtd, chipnr);
2749
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002750 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002751 if (nand_check_wp(mtd)) {
2752 ret = -EIO;
2753 goto err_out;
2754 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002755
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002756 realpage = (int)(to >> chip->page_shift);
2757 page = realpage & chip->pagemask;
2758 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2759
2760 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002761 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2762 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002763 chip->pagebuf = -1;
2764
Maxim Levitsky782ce792010-02-22 20:39:36 +02002765 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002766 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2767 ret = -EINVAL;
2768 goto err_out;
2769 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002770
Florian Fainellif8ac0412010-09-07 13:23:43 +02002771 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002772 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002773 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002774 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002775 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002776 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002777
Kamal Dasu66507c72014-05-01 20:51:19 -04002778 if (part_pagewr)
2779 use_bufpoi = 1;
2780 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2781 use_bufpoi = !virt_addr_valid(buf);
2782 else
2783 use_bufpoi = 0;
2784
2785 /* Partial page write?, or need to use bounce buffer */
2786 if (use_bufpoi) {
2787 pr_debug("%s: using write bounce buffer for buf@%p\n",
2788 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002789 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002790 if (part_pagewr)
2791 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002792 chip->pagebuf = -1;
2793 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2794 memcpy(&chip->buffers->databuf[column], buf, bytes);
2795 wbuf = chip->buffers->databuf;
2796 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002797
Maxim Levitsky782ce792010-02-22 20:39:36 +02002798 if (unlikely(oob)) {
2799 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002800 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002801 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002802 } else {
2803 /* We still need to erase leftover OOB data */
2804 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002805 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302806 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2807 oob_required, page, cached,
2808 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002809 if (ret)
2810 break;
2811
2812 writelen -= bytes;
2813 if (!writelen)
2814 break;
2815
Thomas Gleixner29072b92006-09-28 15:38:36 +02002816 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002817 buf += bytes;
2818 realpage++;
2819
2820 page = realpage & chip->pagemask;
2821 /* Check, if we cross a chip boundary */
2822 if (!page) {
2823 chipnr++;
2824 chip->select_chip(mtd, -1);
2825 chip->select_chip(mtd, chipnr);
2826 }
2827 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002828
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002829 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002830 if (unlikely(oob))
2831 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002832
2833err_out:
2834 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002835 return ret;
2836}
2837
2838/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002839 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002840 * @mtd: MTD device structure
2841 * @to: offset to write to
2842 * @len: number of bytes to write
2843 * @retlen: pointer to variable to store the number of written bytes
2844 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002845 *
2846 * NAND write with ECC. Used when performing writes in interrupt context, this
2847 * may for example be called by mtdoops when writing an oops while in panic.
2848 */
2849static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2850 size_t *retlen, const uint8_t *buf)
2851{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002852 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002853 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002854 int ret;
2855
Brian Norris8b6e50c2011-05-25 14:59:01 -07002856 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002857 panic_nand_wait(mtd, chip, 400);
2858
Brian Norris8b6e50c2011-05-25 14:59:01 -07002859 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002860 panic_nand_get_device(chip, mtd, FL_WRITING);
2861
Brian Norris0ec56dc2015-02-28 02:02:30 -08002862 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002863 ops.len = len;
2864 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002865 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002866
Brian Norris4a89ff82011-08-30 18:45:45 -07002867 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002868
Brian Norris4a89ff82011-08-30 18:45:45 -07002869 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002870 return ret;
2871}
2872
2873/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002874 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002875 * @mtd: MTD device structure
2876 * @to: offset to write to
2877 * @len: number of bytes to write
2878 * @retlen: pointer to variable to store the number of written bytes
2879 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002881 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002883static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002884 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
Brian Norris4a89ff82011-08-30 18:45:45 -07002886 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002887 int ret;
2888
Huang Shijie6a8214a2012-11-19 14:43:30 +08002889 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002890 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002891 ops.len = len;
2892 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002893 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002894 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002895 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002896 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002897 return ret;
2898}
2899
2900/**
2901 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002902 * @mtd: MTD device structure
2903 * @to: offset to write to
2904 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002905 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002906 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002907 */
2908static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2909 struct mtd_oob_ops *ops)
2910{
Adrian Hunter03736152007-01-31 17:58:29 +02002911 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002912 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Brian Norris289c0522011-07-19 10:06:09 -07002914 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302915 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Boris BREZILLON29f10582016-03-07 10:46:52 +01002917 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002920 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002921 pr_debug("%s: attempt to write past end of page\n",
2922 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 return -EINVAL;
2924 }
2925
Adrian Hunter03736152007-01-31 17:58:29 +02002926 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002927 pr_debug("%s: attempt to start write outside oob\n",
2928 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002929 return -EINVAL;
2930 }
2931
Jason Liu775adc3d42011-02-25 13:06:18 +08002932 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002933 if (unlikely(to >= mtd->size ||
2934 ops->ooboffs + ops->ooblen >
2935 ((mtd->size >> chip->page_shift) -
2936 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002937 pr_debug("%s: attempt to write beyond end of device\n",
2938 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002939 return -EINVAL;
2940 }
2941
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002942 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002943 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002945 /* Shift to get page */
2946 page = (int)(to >> chip->page_shift);
2947
2948 /*
2949 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2950 * of my DiskOnChip 2000 test units) will clear the whole data page too
2951 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2952 * it in the doc2000 driver in August 1999. dwmw2.
2953 */
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002954 nand_reset(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002957 if (nand_check_wp(mtd)) {
2958 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002959 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002960 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002963 if (page == chip->pagebuf)
2964 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002966 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002967
Brian Norris0612b9d2011-08-30 18:45:40 -07002968 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002969 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2970 else
2971 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002972
Huang Shijieb0bb6902012-11-19 14:43:29 +08002973 chip->select_chip(mtd, -1);
2974
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002975 if (status)
2976 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Vitaly Wool70145682006-11-03 18:20:38 +03002978 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002980 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002981}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002983/**
2984 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002985 * @mtd: MTD device structure
2986 * @to: offset to write to
2987 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002988 */
2989static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2990 struct mtd_oob_ops *ops)
2991{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002992 int ret = -ENOTSUPP;
2993
2994 ops->retlen = 0;
2995
2996 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002997 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002998 pr_debug("%s: attempt to write beyond end of device\n",
2999 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003000 return -EINVAL;
3001 }
3002
Huang Shijie6a8214a2012-11-19 14:43:30 +08003003 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003004
Florian Fainellif8ac0412010-09-07 13:23:43 +02003005 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003006 case MTD_OPS_PLACE_OOB:
3007 case MTD_OPS_AUTO_OOB:
3008 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003009 break;
3010
3011 default:
3012 goto out;
3013 }
3014
3015 if (!ops->datbuf)
3016 ret = nand_do_write_oob(mtd, to, ops);
3017 else
3018 ret = nand_do_write_ops(mtd, to, ops);
3019
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003020out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003021 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return ret;
3023}
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025/**
Brian Norris49c50b92014-05-06 16:02:19 -07003026 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003027 * @mtd: MTD device structure
3028 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 *
Brian Norris49c50b92014-05-06 16:02:19 -07003030 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 */
Brian Norris49c50b92014-05-06 16:02:19 -07003032static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003034 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003036 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
3037 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07003038
3039 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
3042/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003044 * @mtd: MTD device structure
3045 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003047 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003049static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
David Woodhousee0c7d762006-05-13 18:07:53 +01003051 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003053
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003055 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003056 * @mtd: MTD device structure
3057 * @instr: erase instruction
3058 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003060 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003062int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3063 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Adrian Hunter69423d92008-12-10 13:37:21 +00003065 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003066 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003067 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Brian Norris289c0522011-07-19 10:06:09 -07003069 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3070 __func__, (unsigned long long)instr->addr,
3071 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303073 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003077 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003080 page = (int)(instr->addr >> chip->page_shift);
3081 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
3083 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003084 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
3086 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003087 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 /* Check, if it is write protected */
3090 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003091 pr_debug("%s: device is write protected!\n",
3092 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 instr->state = MTD_ERASE_FAILED;
3094 goto erase_exit;
3095 }
3096
3097 /* Loop through the pages */
3098 len = instr->len;
3099
3100 instr->state = MTD_ERASING;
3101
3102 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003103 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003104 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303105 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003106 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3107 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 instr->state = MTD_ERASE_FAILED;
3109 goto erase_exit;
3110 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003111
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003112 /*
3113 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003114 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003115 */
3116 if (page <= chip->pagebuf && chip->pagebuf <
3117 (page + pages_per_block))
3118 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Brian Norris49c50b92014-05-06 16:02:19 -07003120 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003122 /*
3123 * See if operation failed and additional status checks are
3124 * available
3125 */
3126 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
3127 status = chip->errstat(mtd, chip, FL_ERASING,
3128 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00003129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003131 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003132 pr_debug("%s: failed erase, page 0x%08x\n",
3133 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003135 instr->fail_addr =
3136 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 goto erase_exit;
3138 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003141 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 page += pages_per_block;
3143
3144 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003145 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003147 chip->select_chip(mtd, -1);
3148 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 }
3150 }
3151 instr->state = MTD_ERASE_DONE;
3152
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003153erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
3157 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003158 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 nand_release_device(mtd);
3160
David Woodhouse49defc02007-10-06 15:01:59 -04003161 /* Do call back function */
3162 if (!ret)
3163 mtd_erase_callback(instr);
3164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 /* Return more or less happy */
3166 return ret;
3167}
3168
3169/**
3170 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003171 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003173 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003175static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
Brian Norris289c0522011-07-19 10:06:09 -07003177 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
3179 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003180 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003182 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183}
3184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003186 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003187 * @mtd: MTD device structure
3188 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003190static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303192 struct nand_chip *chip = mtd_to_nand(mtd);
3193 int chipnr = (int)(offs >> chip->chip_shift);
3194 int ret;
3195
3196 /* Select the NAND device */
3197 nand_get_device(mtd, FL_READING);
3198 chip->select_chip(mtd, chipnr);
3199
3200 ret = nand_block_checkbad(mtd, offs, 0);
3201
3202 chip->select_chip(mtd, -1);
3203 nand_release_device(mtd);
3204
3205 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206}
3207
3208/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003209 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003210 * @mtd: MTD device structure
3211 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003213static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 int ret;
3216
Florian Fainellif8ac0412010-09-07 13:23:43 +02003217 ret = nand_block_isbad(mtd, ofs);
3218 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003219 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 if (ret > 0)
3221 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003222 return ret;
3223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
Brian Norris5a0edb22013-07-30 17:52:58 -07003225 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226}
3227
3228/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003229 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3230 * @mtd: MTD device structure
3231 * @chip: nand chip info structure
3232 * @addr: feature address.
3233 * @subfeature_param: the subfeature parameters, a four bytes array.
3234 */
3235static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3236 int addr, uint8_t *subfeature_param)
3237{
3238 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003239 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003240
David Mosbergerd914c932013-05-29 15:30:13 +03003241 if (!chip->onfi_version ||
3242 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3243 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003244 return -EINVAL;
3245
3246 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003247 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3248 chip->write_byte(mtd, subfeature_param[i]);
3249
Huang Shijie7db03ec2012-09-13 14:57:52 +08003250 status = chip->waitfunc(mtd, chip);
3251 if (status & NAND_STATUS_FAIL)
3252 return -EIO;
3253 return 0;
3254}
3255
3256/**
3257 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3258 * @mtd: MTD device structure
3259 * @chip: nand chip info structure
3260 * @addr: feature address.
3261 * @subfeature_param: the subfeature parameters, a four bytes array.
3262 */
3263static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3264 int addr, uint8_t *subfeature_param)
3265{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003266 int i;
3267
David Mosbergerd914c932013-05-29 15:30:13 +03003268 if (!chip->onfi_version ||
3269 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3270 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003271 return -EINVAL;
3272
Huang Shijie7db03ec2012-09-13 14:57:52 +08003273 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003274 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3275 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003276 return 0;
3277}
3278
3279/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003280 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003281 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003282 */
3283static int nand_suspend(struct mtd_info *mtd)
3284{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003285 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003286}
3287
3288/**
3289 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003290 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003291 */
3292static void nand_resume(struct mtd_info *mtd)
3293{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003294 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003295
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003296 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003297 nand_release_device(mtd);
3298 else
Brian Norrisd0370212011-07-19 10:06:08 -07003299 pr_err("%s called for a chip which is not in suspended state\n",
3300 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003301}
3302
Scott Branden72ea4032014-11-20 11:18:05 -08003303/**
3304 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3305 * prevent further operations
3306 * @mtd: MTD device structure
3307 */
3308static void nand_shutdown(struct mtd_info *mtd)
3309{
Brian Norris9ca641b2015-11-09 16:37:28 -08003310 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003311}
3312
Brian Norris8b6e50c2011-05-25 14:59:01 -07003313/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003314static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003315{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003317 if (!chip->chip_delay)
3318 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
3320 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003321 if (chip->cmdfunc == NULL)
3322 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
3324 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003325 if (chip->waitfunc == NULL)
3326 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003328 if (!chip->select_chip)
3329 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003330
Huang Shijie4204ccc2013-08-16 10:10:07 +08003331 /* set for ONFI nand */
3332 if (!chip->onfi_set_features)
3333 chip->onfi_set_features = nand_onfi_set_features;
3334 if (!chip->onfi_get_features)
3335 chip->onfi_get_features = nand_onfi_get_features;
3336
Brian Norris68e80782013-07-18 01:17:02 -07003337 /* If called twice, pointers that depend on busw may need to be reset */
3338 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003339 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3340 if (!chip->read_word)
3341 chip->read_word = nand_read_word;
3342 if (!chip->block_bad)
3343 chip->block_bad = nand_block_bad;
3344 if (!chip->block_markbad)
3345 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003346 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003347 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003348 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3349 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003350 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003351 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003352 if (!chip->scan_bbt)
3353 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003354
3355 if (!chip->controller) {
3356 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003357 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003358 }
3359
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003360}
3361
Brian Norris8b6e50c2011-05-25 14:59:01 -07003362/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003363static void sanitize_string(uint8_t *s, size_t len)
3364{
3365 ssize_t i;
3366
Brian Norris8b6e50c2011-05-25 14:59:01 -07003367 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003368 s[len - 1] = 0;
3369
Brian Norris8b6e50c2011-05-25 14:59:01 -07003370 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003371 for (i = 0; i < len - 1; i++) {
3372 if (s[i] < ' ' || s[i] > 127)
3373 s[i] = '?';
3374 }
3375
Brian Norris8b6e50c2011-05-25 14:59:01 -07003376 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003377 strim(s);
3378}
3379
3380static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3381{
3382 int i;
3383 while (len--) {
3384 crc ^= *p++ << 8;
3385 for (i = 0; i < 8; i++)
3386 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3387 }
3388
3389 return crc;
3390}
3391
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003392/* Parse the Extended Parameter Page. */
3393static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3394 struct nand_chip *chip, struct nand_onfi_params *p)
3395{
3396 struct onfi_ext_param_page *ep;
3397 struct onfi_ext_section *s;
3398 struct onfi_ext_ecc_info *ecc;
3399 uint8_t *cursor;
3400 int ret = -EINVAL;
3401 int len;
3402 int i;
3403
3404 len = le16_to_cpu(p->ext_param_page_length) * 16;
3405 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003406 if (!ep)
3407 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003408
3409 /* Send our own NAND_CMD_PARAM. */
3410 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3411
3412 /* Use the Change Read Column command to skip the ONFI param pages. */
3413 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3414 sizeof(*p) * p->num_of_param_pages , -1);
3415
3416 /* Read out the Extended Parameter Page. */
3417 chip->read_buf(mtd, (uint8_t *)ep, len);
3418 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3419 != le16_to_cpu(ep->crc))) {
3420 pr_debug("fail in the CRC.\n");
3421 goto ext_out;
3422 }
3423
3424 /*
3425 * Check the signature.
3426 * Do not strictly follow the ONFI spec, maybe changed in future.
3427 */
3428 if (strncmp(ep->sig, "EPPS", 4)) {
3429 pr_debug("The signature is invalid.\n");
3430 goto ext_out;
3431 }
3432
3433 /* find the ECC section. */
3434 cursor = (uint8_t *)(ep + 1);
3435 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3436 s = ep->sections + i;
3437 if (s->type == ONFI_SECTION_TYPE_2)
3438 break;
3439 cursor += s->length * 16;
3440 }
3441 if (i == ONFI_EXT_SECTION_MAX) {
3442 pr_debug("We can not find the ECC section.\n");
3443 goto ext_out;
3444 }
3445
3446 /* get the info we want. */
3447 ecc = (struct onfi_ext_ecc_info *)cursor;
3448
Brian Norris4ae7d222013-09-16 18:20:21 -07003449 if (!ecc->codeword_size) {
3450 pr_debug("Invalid codeword size\n");
3451 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003452 }
3453
Brian Norris4ae7d222013-09-16 18:20:21 -07003454 chip->ecc_strength_ds = ecc->ecc_bits;
3455 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003456 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003457
3458ext_out:
3459 kfree(ep);
3460 return ret;
3461}
3462
Brian Norris8429bb32013-12-03 15:51:09 -08003463static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3464{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003465 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris8429bb32013-12-03 15:51:09 -08003466 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3467
3468 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3469 feature);
3470}
3471
3472/*
3473 * Configure chip properties from Micron vendor-specific ONFI table
3474 */
3475static void nand_onfi_detect_micron(struct nand_chip *chip,
3476 struct nand_onfi_params *p)
3477{
3478 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3479
3480 if (le16_to_cpu(p->vendor_revision) < 1)
3481 return;
3482
3483 chip->read_retries = micron->read_retry_options;
3484 chip->setup_read_retry = nand_setup_read_retry_micron;
3485}
3486
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003487/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003488 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003489 */
3490static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003491 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003492{
3493 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003494 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003495 int val;
3496
Brian Norris7854d3f2011-06-23 14:12:08 -07003497 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003498 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3499 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3500 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3501 return 0;
3502
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003503 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3504 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003505 for (j = 0; j < sizeof(*p); j++)
3506 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003507 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3508 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003509 break;
3510 }
3511 }
3512
Brian Norrisc7f23a72013-08-13 10:51:55 -07003513 if (i == 3) {
3514 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003515 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003516 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003517
Brian Norris8b6e50c2011-05-25 14:59:01 -07003518 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003519 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003520 if (val & (1 << 5))
3521 chip->onfi_version = 23;
3522 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003523 chip->onfi_version = 22;
3524 else if (val & (1 << 3))
3525 chip->onfi_version = 21;
3526 else if (val & (1 << 2))
3527 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003528 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003529 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003530
3531 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003532 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003533 return 0;
3534 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003535
3536 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3537 sanitize_string(p->model, sizeof(p->model));
3538 if (!mtd->name)
3539 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003540
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003541 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003542
3543 /*
3544 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3545 * (don't ask me who thought of this...). MTD assumes that these
3546 * dimensions will be power-of-2, so just truncate the remaining area.
3547 */
3548 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3549 mtd->erasesize *= mtd->writesize;
3550
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003551 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003552
3553 /* See erasesize comment */
3554 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003555 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003556 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003557
3558 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003559 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003560 else
3561 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003562
Huang Shijie10c86ba2013-05-17 11:17:26 +08003563 if (p->ecc_bits != 0xff) {
3564 chip->ecc_strength_ds = p->ecc_bits;
3565 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003566 } else if (chip->onfi_version >= 21 &&
3567 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3568
3569 /*
3570 * The nand_flash_detect_ext_param_page() uses the
3571 * Change Read Column command which maybe not supported
3572 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3573 * now. We do not replace user supplied command function.
3574 */
3575 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3576 chip->cmdfunc = nand_command_lp;
3577
3578 /* The Extended Parameter Page is supported since ONFI 2.1. */
3579 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003580 pr_warn("Failed to detect ONFI extended param page\n");
3581 } else {
3582 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003583 }
3584
Brian Norris8429bb32013-12-03 15:51:09 -08003585 if (p->jedec_id == NAND_MFR_MICRON)
3586 nand_onfi_detect_micron(chip, p);
3587
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003588 return 1;
3589}
3590
3591/*
Huang Shijie91361812014-02-21 13:39:40 +08003592 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3593 */
3594static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3595 int *busw)
3596{
3597 struct nand_jedec_params *p = &chip->jedec_params;
3598 struct jedec_ecc_info *ecc;
3599 int val;
3600 int i, j;
3601
3602 /* Try JEDEC for unknown chip or LP */
3603 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3604 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3605 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3606 chip->read_byte(mtd) != 'C')
3607 return 0;
3608
3609 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3610 for (i = 0; i < 3; i++) {
3611 for (j = 0; j < sizeof(*p); j++)
3612 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3613
3614 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3615 le16_to_cpu(p->crc))
3616 break;
3617 }
3618
3619 if (i == 3) {
3620 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3621 return 0;
3622 }
3623
3624 /* Check version */
3625 val = le16_to_cpu(p->revision);
3626 if (val & (1 << 2))
3627 chip->jedec_version = 10;
3628 else if (val & (1 << 1))
3629 chip->jedec_version = 1; /* vendor specific version */
3630
3631 if (!chip->jedec_version) {
3632 pr_info("unsupported JEDEC version: %d\n", val);
3633 return 0;
3634 }
3635
3636 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3637 sanitize_string(p->model, sizeof(p->model));
3638 if (!mtd->name)
3639 mtd->name = p->model;
3640
3641 mtd->writesize = le32_to_cpu(p->byte_per_page);
3642
3643 /* Please reference to the comment for nand_flash_detect_onfi. */
3644 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3645 mtd->erasesize *= mtd->writesize;
3646
3647 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3648
3649 /* Please reference to the comment for nand_flash_detect_onfi. */
3650 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3651 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3652 chip->bits_per_cell = p->bits_per_cell;
3653
3654 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3655 *busw = NAND_BUSWIDTH_16;
3656 else
3657 *busw = 0;
3658
3659 /* ECC info */
3660 ecc = &p->ecc_info[0];
3661
3662 if (ecc->codeword_size >= 9) {
3663 chip->ecc_strength_ds = ecc->ecc_bits;
3664 chip->ecc_step_ds = 1 << ecc->codeword_size;
3665 } else {
3666 pr_warn("Invalid codeword size\n");
3667 }
3668
3669 return 1;
3670}
3671
3672/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003673 * nand_id_has_period - Check if an ID string has a given wraparound period
3674 * @id_data: the ID string
3675 * @arrlen: the length of the @id_data array
3676 * @period: the period of repitition
3677 *
3678 * Check if an ID string is repeated within a given sequence of bytes at
3679 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003680 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003681 * if the repetition has a period of @period; otherwise, returns zero.
3682 */
3683static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3684{
3685 int i, j;
3686 for (i = 0; i < period; i++)
3687 for (j = i + period; j < arrlen; j += period)
3688 if (id_data[i] != id_data[j])
3689 return 0;
3690 return 1;
3691}
3692
3693/*
3694 * nand_id_len - Get the length of an ID string returned by CMD_READID
3695 * @id_data: the ID string
3696 * @arrlen: the length of the @id_data array
3697
3698 * Returns the length of the ID string, according to known wraparound/trailing
3699 * zero patterns. If no pattern exists, returns the length of the array.
3700 */
3701static int nand_id_len(u8 *id_data, int arrlen)
3702{
3703 int last_nonzero, period;
3704
3705 /* Find last non-zero byte */
3706 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3707 if (id_data[last_nonzero])
3708 break;
3709
3710 /* All zeros */
3711 if (last_nonzero < 0)
3712 return 0;
3713
3714 /* Calculate wraparound period */
3715 for (period = 1; period < arrlen; period++)
3716 if (nand_id_has_period(id_data, arrlen, period))
3717 break;
3718
3719 /* There's a repeated pattern */
3720 if (period < arrlen)
3721 return period;
3722
3723 /* There are trailing zeros */
3724 if (last_nonzero < arrlen - 1)
3725 return last_nonzero + 1;
3726
3727 /* No pattern detected */
3728 return arrlen;
3729}
3730
Huang Shijie7db906b2013-09-25 14:58:11 +08003731/* Extract the bits of per cell from the 3rd byte of the extended ID */
3732static int nand_get_bits_per_cell(u8 cellinfo)
3733{
3734 int bits;
3735
3736 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3737 bits >>= NAND_CI_CELLTYPE_SHIFT;
3738 return bits + 1;
3739}
3740
Brian Norrise3b88bd2012-09-24 20:40:52 -07003741/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003742 * Many new NAND share similar device ID codes, which represent the size of the
3743 * chip. The rest of the parameters must be decoded according to generic or
3744 * manufacturer-specific "extended ID" decoding patterns.
3745 */
3746static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3747 u8 id_data[8], int *busw)
3748{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003749 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003750 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003751 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003752 /* The 4th id byte is the important one */
3753 extid = id_data[3];
3754
Brian Norrise3b88bd2012-09-24 20:40:52 -07003755 id_len = nand_id_len(id_data, 8);
3756
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003757 /*
3758 * Field definitions are in the following datasheets:
3759 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003760 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003761 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003762 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003763 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3764 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003765 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003766 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003767 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003768 /* Calc pagesize */
3769 mtd->writesize = 2048 << (extid & 0x03);
3770 extid >>= 2;
3771 /* Calc oobsize */
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003772 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003773 case 1:
3774 mtd->oobsize = 128;
3775 break;
3776 case 2:
3777 mtd->oobsize = 218;
3778 break;
3779 case 3:
3780 mtd->oobsize = 400;
3781 break;
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003782 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003783 mtd->oobsize = 436;
3784 break;
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003785 case 5:
3786 mtd->oobsize = 512;
3787 break;
3788 case 6:
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003789 mtd->oobsize = 640;
3790 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003791 case 7:
3792 default: /* Other cases are "reserved" (unknown) */
3793 mtd->oobsize = 1024;
3794 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003795 }
3796 extid >>= 2;
3797 /* Calc blocksize */
3798 mtd->erasesize = (128 * 1024) <<
3799 (((extid >> 1) & 0x04) | (extid & 0x03));
3800 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003801 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003802 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003803 unsigned int tmp;
3804
3805 /* Calc pagesize */
3806 mtd->writesize = 2048 << (extid & 0x03);
3807 extid >>= 2;
3808 /* Calc oobsize */
3809 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3810 case 0:
3811 mtd->oobsize = 128;
3812 break;
3813 case 1:
3814 mtd->oobsize = 224;
3815 break;
3816 case 2:
3817 mtd->oobsize = 448;
3818 break;
3819 case 3:
3820 mtd->oobsize = 64;
3821 break;
3822 case 4:
3823 mtd->oobsize = 32;
3824 break;
3825 case 5:
3826 mtd->oobsize = 16;
3827 break;
3828 default:
3829 mtd->oobsize = 640;
3830 break;
3831 }
3832 extid >>= 2;
3833 /* Calc blocksize */
3834 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3835 if (tmp < 0x03)
3836 mtd->erasesize = (128 * 1024) << tmp;
3837 else if (tmp == 0x03)
3838 mtd->erasesize = 768 * 1024;
3839 else
3840 mtd->erasesize = (64 * 1024) << tmp;
3841 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003842 } else {
3843 /* Calc pagesize */
3844 mtd->writesize = 1024 << (extid & 0x03);
3845 extid >>= 2;
3846 /* Calc oobsize */
3847 mtd->oobsize = (8 << (extid & 0x01)) *
3848 (mtd->writesize >> 9);
3849 extid >>= 2;
3850 /* Calc blocksize. Blocksize is multiples of 64KiB */
3851 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3852 extid >>= 2;
3853 /* Get buswidth information */
3854 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003855
3856 /*
3857 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3858 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3859 * follows:
3860 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3861 * 110b -> 24nm
3862 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3863 */
3864 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003865 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003866 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3867 !(id_data[4] & 0x80) /* !BENAND */) {
3868 mtd->oobsize = 32 * mtd->writesize >> 9;
3869 }
3870
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003871 }
3872}
3873
3874/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003875 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3876 * decodes a matching ID table entry and assigns the MTD size parameters for
3877 * the chip.
3878 */
3879static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3880 struct nand_flash_dev *type, u8 id_data[8],
3881 int *busw)
3882{
3883 int maf_id = id_data[0];
3884
3885 mtd->erasesize = type->erasesize;
3886 mtd->writesize = type->pagesize;
3887 mtd->oobsize = mtd->writesize / 32;
3888 *busw = type->options & NAND_BUSWIDTH_16;
3889
Huang Shijie1c195e92013-09-25 14:58:12 +08003890 /* All legacy ID NAND are small-page, SLC */
3891 chip->bits_per_cell = 1;
3892
Brian Norrisf23a4812012-09-24 20:40:51 -07003893 /*
3894 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3895 * some Spansion chips have erasesize that conflicts with size
3896 * listed in nand_ids table.
3897 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3898 */
3899 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3900 && id_data[6] == 0x00 && id_data[7] == 0x00
3901 && mtd->writesize == 512) {
3902 mtd->erasesize = 128 * 1024;
3903 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3904 }
3905}
3906
3907/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003908 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3909 * heuristic patterns using various detected parameters (e.g., manufacturer,
3910 * page size, cell-type information).
3911 */
3912static void nand_decode_bbm_options(struct mtd_info *mtd,
3913 struct nand_chip *chip, u8 id_data[8])
3914{
3915 int maf_id = id_data[0];
3916
3917 /* Set the bad block position */
3918 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3919 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3920 else
3921 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3922
3923 /*
3924 * Bad block marker is stored in the last page of each block on Samsung
3925 * and Hynix MLC devices; stored in first two pages of each block on
3926 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3927 * AMD/Spansion, and Macronix. All others scan only the first page.
3928 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003929 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003930 (maf_id == NAND_MFR_SAMSUNG ||
3931 maf_id == NAND_MFR_HYNIX))
3932 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003933 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003934 (maf_id == NAND_MFR_SAMSUNG ||
3935 maf_id == NAND_MFR_HYNIX ||
3936 maf_id == NAND_MFR_TOSHIBA ||
3937 maf_id == NAND_MFR_AMD ||
3938 maf_id == NAND_MFR_MACRONIX)) ||
3939 (mtd->writesize == 2048 &&
3940 maf_id == NAND_MFR_MICRON))
3941 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3942}
3943
Huang Shijieec6e87e2013-03-15 11:01:00 +08003944static inline bool is_full_id_nand(struct nand_flash_dev *type)
3945{
3946 return type->id_len;
3947}
3948
3949static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3950 struct nand_flash_dev *type, u8 *id_data, int *busw)
3951{
3952 if (!strncmp(type->id, id_data, type->id_len)) {
3953 mtd->writesize = type->pagesize;
3954 mtd->erasesize = type->erasesize;
3955 mtd->oobsize = type->oobsize;
3956
Huang Shijie7db906b2013-09-25 14:58:11 +08003957 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003958 chip->chipsize = (uint64_t)type->chipsize << 20;
3959 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003960 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3961 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003962 chip->onfi_timing_mode_default =
3963 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003964
3965 *busw = type->options & NAND_BUSWIDTH_16;
3966
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003967 if (!mtd->name)
3968 mtd->name = type->name;
3969
Huang Shijieec6e87e2013-03-15 11:01:00 +08003970 return true;
3971 }
3972 return false;
3973}
3974
Brian Norris7e74c2d2012-09-24 20:40:49 -07003975/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003976 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003977 */
3978static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003979 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003980 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003981 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003982{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003983 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003984 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003985 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
3987 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003988 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Karl Beldanef89a882008-09-15 14:37:29 +02003990 /*
3991 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003992 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003993 */
Sascha Hauer2f94abf2016-09-15 10:32:45 +02003994 nand_reset(chip);
Karl Beldanef89a882008-09-15 14:37:29 +02003995
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003997 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
3999 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004000 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004001 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
Brian Norris8b6e50c2011-05-25 14:59:01 -07004003 /*
4004 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004005 * interface concerns can cause random data which looks like a
4006 * possibly credible NAND flash to appear. If the two results do
4007 * not match, ignore the device completely.
4008 */
4009
4010 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4011
Brian Norris4aef9b72012-09-24 20:40:48 -07004012 /* Read entire ID string */
4013 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07004014 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01004015
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004016 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004017 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07004018 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01004019 return ERR_PTR(-ENODEV);
4020 }
4021
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004022 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004023 type = nand_flash_ids;
4024
Huang Shijieec6e87e2013-03-15 11:01:00 +08004025 for (; type->name != NULL; type++) {
4026 if (is_full_id_nand(type)) {
4027 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
4028 goto ident_done;
4029 } else if (*dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004030 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004031 }
4032 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004033
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004034 chip->onfi_version = 0;
4035 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004036 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07004037 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004038 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004039
4040 /* Check if the chip is JEDEC compliant */
4041 if (nand_flash_detect_jedec(mtd, chip, &busw))
4042 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004043 }
4044
David Woodhouse5e81e882010-02-26 18:32:56 +00004045 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004046 return ERR_PTR(-ENODEV);
4047
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004048 if (!mtd->name)
4049 mtd->name = type->name;
4050
Adrian Hunter69423d92008-12-10 13:37:21 +00004051 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004052
Boris BREZILLONa7f5ba42015-10-01 16:58:27 +02004053 if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004054 /* Decode parameters from extended ID */
4055 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004056 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07004057 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004058 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004059 /* Get chip options */
4060 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004061
Brian Norris8b6e50c2011-05-25 14:59:01 -07004062 /*
4063 * Check if chip is not a Samsung device. Do not clear the
4064 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004065 */
4066 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
4067 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
4068ident_done:
4069
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004070 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01004071 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004072 if (nand_manuf_ids[maf_idx].id == *maf_id)
4073 break;
4074 }
4075
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004076 if (chip->options & NAND_BUSWIDTH_AUTO) {
4077 WARN_ON(chip->options & NAND_BUSWIDTH_16);
4078 chip->options |= busw;
4079 nand_set_defaults(chip, busw);
4080 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4081 /*
4082 * Check, if buswidth is correct. Hardware drivers should set
4083 * chip correct!
4084 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004085 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4086 *maf_id, *dev_id);
4087 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
4088 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07004089 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
4090 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004091 return ERR_PTR(-EINVAL);
4092 }
4093
Brian Norris7e74c2d2012-09-24 20:40:49 -07004094 nand_decode_bbm_options(mtd, chip, id_data);
4095
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004096 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004097 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004098 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004099 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004100
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004101 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004102 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004103 if (chip->chipsize & 0xffffffff)
4104 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004105 else {
4106 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4107 chip->chip_shift += 32 - 1;
4108 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004109
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004110 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004111 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004112
Brian Norris8b6e50c2011-05-25 14:59:01 -07004113 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004114 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4115 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004116
Ezequiel Garcia20171642013-11-25 08:30:31 -03004117 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4118 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004119
4120 if (chip->onfi_version)
4121 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4122 chip->onfi_params.model);
4123 else if (chip->jedec_version)
4124 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4125 chip->jedec_params.model);
4126 else
4127 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4128 type->name);
4129
Rafał Miłecki3755a992014-10-21 00:01:04 +02004130 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004131 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004132 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004133 return type;
4134}
4135
Boris Brezillond48f62b2016-04-01 14:54:32 +02004136static const char * const nand_ecc_modes[] = {
4137 [NAND_ECC_NONE] = "none",
4138 [NAND_ECC_SOFT] = "soft",
4139 [NAND_ECC_HW] = "hw",
4140 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4141 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004142};
4143
4144static int of_get_nand_ecc_mode(struct device_node *np)
4145{
4146 const char *pm;
4147 int err, i;
4148
4149 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4150 if (err < 0)
4151 return err;
4152
4153 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4154 if (!strcasecmp(pm, nand_ecc_modes[i]))
4155 return i;
4156
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004157 /*
4158 * For backward compatibility we support few obsoleted values that don't
4159 * have their mappings into nand_ecc_modes_t anymore (they were merged
4160 * with other enums).
4161 */
4162 if (!strcasecmp(pm, "soft_bch"))
4163 return NAND_ECC_SOFT;
4164
Boris Brezillond48f62b2016-04-01 14:54:32 +02004165 return -ENODEV;
4166}
4167
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004168static const char * const nand_ecc_algos[] = {
4169 [NAND_ECC_HAMMING] = "hamming",
4170 [NAND_ECC_BCH] = "bch",
4171};
4172
Boris Brezillond48f62b2016-04-01 14:54:32 +02004173static int of_get_nand_ecc_algo(struct device_node *np)
4174{
4175 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004176 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004177
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004178 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4179 if (!err) {
4180 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4181 if (!strcasecmp(pm, nand_ecc_algos[i]))
4182 return i;
4183 return -ENODEV;
4184 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004185
4186 /*
4187 * For backward compatibility we also read "nand-ecc-mode" checking
4188 * for some obsoleted values that were specifying ECC algorithm.
4189 */
4190 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4191 if (err < 0)
4192 return err;
4193
4194 if (!strcasecmp(pm, "soft"))
4195 return NAND_ECC_HAMMING;
4196 else if (!strcasecmp(pm, "soft_bch"))
4197 return NAND_ECC_BCH;
4198
4199 return -ENODEV;
4200}
4201
4202static int of_get_nand_ecc_step_size(struct device_node *np)
4203{
4204 int ret;
4205 u32 val;
4206
4207 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4208 return ret ? ret : val;
4209}
4210
4211static int of_get_nand_ecc_strength(struct device_node *np)
4212{
4213 int ret;
4214 u32 val;
4215
4216 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4217 return ret ? ret : val;
4218}
4219
4220static int of_get_nand_bus_width(struct device_node *np)
4221{
4222 u32 val;
4223
4224 if (of_property_read_u32(np, "nand-bus-width", &val))
4225 return 8;
4226
4227 switch (val) {
4228 case 8:
4229 case 16:
4230 return val;
4231 default:
4232 return -EIO;
4233 }
4234}
4235
4236static bool of_get_nand_on_flash_bbt(struct device_node *np)
4237{
4238 return of_property_read_bool(np, "nand-on-flash-bbt");
4239}
4240
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004241static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004242{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004243 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004244 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004245
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004246 if (!dn)
4247 return 0;
4248
Brian Norris5844fee2015-01-23 00:22:27 -08004249 if (of_get_nand_bus_width(dn) == 16)
4250 chip->options |= NAND_BUSWIDTH_16;
4251
4252 if (of_get_nand_on_flash_bbt(dn))
4253 chip->bbt_options |= NAND_BBT_USE_FLASH;
4254
4255 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004256 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004257 ecc_strength = of_get_nand_ecc_strength(dn);
4258 ecc_step = of_get_nand_ecc_step_size(dn);
4259
4260 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4261 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4262 pr_err("must set both strength and step size in DT\n");
4263 return -EINVAL;
4264 }
4265
4266 if (ecc_mode >= 0)
4267 chip->ecc.mode = ecc_mode;
4268
Rafał Miłecki79082452016-03-23 11:19:02 +01004269 if (ecc_algo >= 0)
4270 chip->ecc.algo = ecc_algo;
4271
Brian Norris5844fee2015-01-23 00:22:27 -08004272 if (ecc_strength >= 0)
4273 chip->ecc.strength = ecc_strength;
4274
4275 if (ecc_step > 0)
4276 chip->ecc.size = ecc_step;
4277
Boris Brezillonba78ee02016-06-08 17:04:22 +02004278 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4279 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4280
Brian Norris5844fee2015-01-23 00:22:27 -08004281 return 0;
4282}
4283
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004284/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004285 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004286 * @mtd: MTD device structure
4287 * @maxchips: number of chips to scan for
4288 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004289 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004290 * This is the first phase of the normal nand_scan() function. It reads the
4291 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004292 *
4293 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004294int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4295 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004296{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004297 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004298 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004299 struct nand_flash_dev *type;
Brian Norris5844fee2015-01-23 00:22:27 -08004300 int ret;
4301
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004302 ret = nand_dt_init(chip);
4303 if (ret)
4304 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004305
Brian Norrisf7a8e382016-01-05 10:39:45 -08004306 if (!mtd->name && mtd->dev.parent)
4307 mtd->name = dev_name(mtd->dev.parent);
4308
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004309 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4310 /*
4311 * Default functions assigned for chip_select() and
4312 * cmdfunc() both expect cmd_ctrl() to be populated,
4313 * so we need to check that that's the case
4314 */
4315 pr_err("chip.cmd_ctrl() callback is not provided");
4316 return -EINVAL;
4317 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004318 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004319 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004320
4321 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004322 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
4323 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004324
4325 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004326 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004327 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004328 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004329 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 }
4331
Boris Brezillond8e725d2016-09-15 10:32:50 +02004332 ret = nand_init_data_interface(chip);
4333 if (ret)
4334 return ret;
4335
Huang Shijie07300162012-11-09 16:23:45 +08004336 chip->select_chip(mtd, -1);
4337
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004338 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004339 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004340 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02004341 /* See comment in nand_get_flash_type for reset */
Sascha Hauer2f94abf2016-09-15 10:32:45 +02004342 nand_reset(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004344 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004346 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004347 nand_dev_id != chip->read_byte(mtd)) {
4348 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 break;
Huang Shijie07300162012-11-09 16:23:45 +08004350 }
4351 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 }
4353 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004354 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004355
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004357 chip->numchips = i;
4358 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
David Woodhouse3b85c322006-09-25 17:06:53 +01004360 return 0;
4361}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004362EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004363
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004364static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4365{
4366 struct nand_chip *chip = mtd_to_nand(mtd);
4367 struct nand_ecc_ctrl *ecc = &chip->ecc;
4368
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004369 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004370 return -EINVAL;
4371
4372 switch (ecc->algo) {
4373 case NAND_ECC_HAMMING:
4374 ecc->calculate = nand_calculate_ecc;
4375 ecc->correct = nand_correct_data;
4376 ecc->read_page = nand_read_page_swecc;
4377 ecc->read_subpage = nand_read_subpage;
4378 ecc->write_page = nand_write_page_swecc;
4379 ecc->read_page_raw = nand_read_page_raw;
4380 ecc->write_page_raw = nand_write_page_raw;
4381 ecc->read_oob = nand_read_oob_std;
4382 ecc->write_oob = nand_write_oob_std;
4383 if (!ecc->size)
4384 ecc->size = 256;
4385 ecc->bytes = 3;
4386 ecc->strength = 1;
4387 return 0;
4388 case NAND_ECC_BCH:
4389 if (!mtd_nand_has_bch()) {
4390 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4391 return -EINVAL;
4392 }
4393 ecc->calculate = nand_bch_calculate_ecc;
4394 ecc->correct = nand_bch_correct_data;
4395 ecc->read_page = nand_read_page_swecc;
4396 ecc->read_subpage = nand_read_subpage;
4397 ecc->write_page = nand_write_page_swecc;
4398 ecc->read_page_raw = nand_read_page_raw;
4399 ecc->write_page_raw = nand_write_page_raw;
4400 ecc->read_oob = nand_read_oob_std;
4401 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004402
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004403 /*
4404 * Board driver should supply ecc.size and ecc.strength
4405 * values to select how many bits are correctable.
4406 * Otherwise, default to 4 bits for large page devices.
4407 */
4408 if (!ecc->size && (mtd->oobsize >= 64)) {
4409 ecc->size = 512;
4410 ecc->strength = 4;
4411 }
4412
4413 /*
4414 * if no ecc placement scheme was provided pickup the default
4415 * large page one.
4416 */
4417 if (!mtd->ooblayout) {
4418 /* handle large page devices only */
4419 if (mtd->oobsize < 64) {
4420 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4421 return -EINVAL;
4422 }
4423
4424 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004425
4426 }
4427
4428 /*
4429 * We can only maximize ECC config when the default layout is
4430 * used, otherwise we don't know how many bytes can really be
4431 * used.
4432 */
4433 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4434 ecc->options & NAND_ECC_MAXIMIZE) {
4435 int steps, bytes;
4436
4437 /* Always prefer 1k blocks over 512bytes ones */
4438 ecc->size = 1024;
4439 steps = mtd->writesize / ecc->size;
4440
4441 /* Reserve 2 bytes for the BBM */
4442 bytes = (mtd->oobsize - 2) / steps;
4443 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004444 }
4445
4446 /* See nand_bch_init() for details. */
4447 ecc->bytes = 0;
4448 ecc->priv = nand_bch_init(mtd);
4449 if (!ecc->priv) {
4450 WARN(1, "BCH ECC initialization failed!\n");
4451 return -EINVAL;
4452 }
4453 return 0;
4454 default:
4455 WARN(1, "Unsupported ECC algorithm!\n");
4456 return -EINVAL;
4457 }
4458}
4459
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004460/*
4461 * Check if the chip configuration meet the datasheet requirements.
4462
4463 * If our configuration corrects A bits per B bytes and the minimum
4464 * required correction level is X bits per Y bytes, then we must ensure
4465 * both of the following are true:
4466 *
4467 * (1) A / B >= X / Y
4468 * (2) A >= X
4469 *
4470 * Requirement (1) ensures we can correct for the required bitflip density.
4471 * Requirement (2) ensures we can correct even when all bitflips are clumped
4472 * in the same sector.
4473 */
4474static bool nand_ecc_strength_good(struct mtd_info *mtd)
4475{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004476 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004477 struct nand_ecc_ctrl *ecc = &chip->ecc;
4478 int corr, ds_corr;
4479
4480 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4481 /* Not enough information */
4482 return true;
4483
4484 /*
4485 * We get the number of corrected bits per page to compare
4486 * the correction density.
4487 */
4488 corr = (mtd->writesize * ecc->strength) / ecc->size;
4489 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4490
4491 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4492}
David Woodhouse3b85c322006-09-25 17:06:53 +01004493
4494/**
4495 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004496 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004497 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004498 * This is the second phase of the normal nand_scan() function. It fills out
4499 * all the uninitialized function pointers with the defaults and scans for a
4500 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004501 */
4502int nand_scan_tail(struct mtd_info *mtd)
4503{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004504 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004505 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004506 struct nand_buffers *nbuf;
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004507 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004508
Brian Norrise2414f42012-02-06 13:44:00 -08004509 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004510 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4511 !(chip->bbt_options & NAND_BBT_USE_FLASH)))
4512 return -EINVAL;
Brian Norrise2414f42012-02-06 13:44:00 -08004513
Huang Shijief02ea4e2014-01-13 14:27:12 +08004514 if (!(chip->options & NAND_OWN_BUFFERS)) {
4515 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
4516 + mtd->oobsize * 3, GFP_KERNEL);
4517 if (!nbuf)
4518 return -ENOMEM;
4519 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
4520 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
4521 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
4522
4523 chip->buffers = nbuf;
4524 } else {
4525 if (!chip->buffers)
4526 return -ENOMEM;
4527 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004528
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004529 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004530 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004531
4532 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004533 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004534 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004535 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004536 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004537 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004540 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 break;
4542 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004543 case 128:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004544 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004545 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004547 WARN(1, "No oob scheme defined for oobsize %d\n",
4548 mtd->oobsize);
4549 ret = -EINVAL;
4550 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 }
4552 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004553
David Woodhouse956e9442006-09-25 17:12:39 +01004554 if (!chip->write_page)
4555 chip->write_page = nand_write_page;
4556
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004557 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004558 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004559 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004560 */
David Woodhouse956e9442006-09-25 17:12:39 +01004561
Huang Shijie97de79e02013-10-18 14:20:53 +08004562 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004563 case NAND_ECC_HW_OOB_FIRST:
4564 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004565 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004566 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4567 ret = -EINVAL;
4568 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004569 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004570 if (!ecc->read_page)
4571 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004572
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004573 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004574 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004575 if (!ecc->read_page)
4576 ecc->read_page = nand_read_page_hwecc;
4577 if (!ecc->write_page)
4578 ecc->write_page = nand_write_page_hwecc;
4579 if (!ecc->read_page_raw)
4580 ecc->read_page_raw = nand_read_page_raw;
4581 if (!ecc->write_page_raw)
4582 ecc->write_page_raw = nand_write_page_raw;
4583 if (!ecc->read_oob)
4584 ecc->read_oob = nand_read_oob_std;
4585 if (!ecc->write_oob)
4586 ecc->write_oob = nand_write_oob_std;
4587 if (!ecc->read_subpage)
4588 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004589 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004590 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004591
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004592 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004593 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4594 (!ecc->read_page ||
4595 ecc->read_page == nand_read_page_hwecc ||
4596 !ecc->write_page ||
4597 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004598 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4599 ret = -EINVAL;
4600 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004601 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004602 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004603 if (!ecc->read_page)
4604 ecc->read_page = nand_read_page_syndrome;
4605 if (!ecc->write_page)
4606 ecc->write_page = nand_write_page_syndrome;
4607 if (!ecc->read_page_raw)
4608 ecc->read_page_raw = nand_read_page_raw_syndrome;
4609 if (!ecc->write_page_raw)
4610 ecc->write_page_raw = nand_write_page_raw_syndrome;
4611 if (!ecc->read_oob)
4612 ecc->read_oob = nand_read_oob_syndrome;
4613 if (!ecc->write_oob)
4614 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004615
Huang Shijie97de79e02013-10-18 14:20:53 +08004616 if (mtd->writesize >= ecc->size) {
4617 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004618 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4619 ret = -EINVAL;
4620 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004621 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004622 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004623 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004624 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4625 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004626 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004627 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004629 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004630 ret = nand_set_ecc_soft_ops(mtd);
4631 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004632 ret = -EINVAL;
4633 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004634 }
4635 break;
4636
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004637 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004638 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004639 ecc->read_page = nand_read_page_raw;
4640 ecc->write_page = nand_write_page_raw;
4641 ecc->read_oob = nand_read_oob_std;
4642 ecc->read_page_raw = nand_read_page_raw;
4643 ecc->write_page_raw = nand_write_page_raw;
4644 ecc->write_oob = nand_write_oob_std;
4645 ecc->size = mtd->writesize;
4646 ecc->bytes = 0;
4647 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004649
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004651 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4652 ret = -EINVAL;
4653 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655
Brian Norris9ce244b2011-08-30 18:45:37 -07004656 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004657 if (!ecc->read_oob_raw)
4658 ecc->read_oob_raw = ecc->read_oob;
4659 if (!ecc->write_oob_raw)
4660 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004661
Boris Brezillon846031d2016-02-03 20:11:00 +01004662 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004663 mtd->ecc_strength = ecc->strength;
4664 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004665
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004666 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004667 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004668 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004669 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004670 ecc->steps = mtd->writesize / ecc->size;
4671 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004672 WARN(1, "Invalid ECC parameters\n");
4673 ret = -EINVAL;
4674 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004676 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004677
Boris Brezillon846031d2016-02-03 20:11:00 +01004678 /*
4679 * The number of bytes available for a client to place data into
4680 * the out of band area.
4681 */
4682 ret = mtd_ooblayout_count_freebytes(mtd);
4683 if (ret < 0)
4684 ret = 0;
4685
4686 mtd->oobavail = ret;
4687
4688 /* ECC sanity check: warn if it's too weak */
4689 if (!nand_ecc_strength_good(mtd))
4690 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4691 mtd->name);
4692
Brian Norris8b6e50c2011-05-25 14:59:01 -07004693 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004694 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004695 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004696 case 2:
4697 mtd->subpage_sft = 1;
4698 break;
4699 case 4:
4700 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004701 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004702 mtd->subpage_sft = 2;
4703 break;
4704 }
4705 }
4706 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4707
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004708 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004709 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004712 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004714 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304715 switch (ecc->mode) {
4716 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304717 if (chip->page_shift > 9)
4718 chip->options |= NAND_SUBPAGE_READ;
4719 break;
4720
4721 default:
4722 break;
4723 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004724
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004726 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004727 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4728 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004729 mtd->_erase = nand_erase;
4730 mtd->_point = NULL;
4731 mtd->_unpoint = NULL;
4732 mtd->_read = nand_read;
4733 mtd->_write = nand_write;
4734 mtd->_panic_write = panic_nand_write;
4735 mtd->_read_oob = nand_read_oob;
4736 mtd->_write_oob = nand_write_oob;
4737 mtd->_sync = nand_sync;
4738 mtd->_lock = NULL;
4739 mtd->_unlock = NULL;
4740 mtd->_suspend = nand_suspend;
4741 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004742 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004743 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004744 mtd->_block_isbad = nand_block_isbad;
4745 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004746 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004748 /*
4749 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4750 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4751 * properly set.
4752 */
4753 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004754 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004756 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004757 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759
4760 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004761 return chip->scan_bbt(mtd);
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004762err_free:
4763 if (!(chip->options & NAND_OWN_BUFFERS))
4764 kfree(chip->buffers);
4765 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004767EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
Brian Norris8b6e50c2011-05-25 14:59:01 -07004769/*
4770 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004771 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004772 * to call us from in-kernel code if the core NAND support is modular.
4773 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004774#ifdef MODULE
4775#define caller_is_module() (1)
4776#else
4777#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004778 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004779#endif
4780
4781/**
4782 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004783 * @mtd: MTD device structure
4784 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004785 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004786 * This fills out all the uninitialized function pointers with the defaults.
4787 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03004788 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01004789 */
4790int nand_scan(struct mtd_info *mtd, int maxchips)
4791{
4792 int ret;
4793
David Woodhouse5e81e882010-02-26 18:32:56 +00004794 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004795 if (!ret)
4796 ret = nand_scan_tail(mtd);
4797 return ret;
4798}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004799EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004800
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004802 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
4803 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004804 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004805void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004807 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004808 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01004809 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4810
Boris Brezillond8e725d2016-09-15 10:32:50 +02004811 nand_release_data_interface(chip);
4812
Jesper Juhlfa671642005-11-07 01:01:27 -08004813 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004814 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004815 if (!(chip->options & NAND_OWN_BUFFERS))
4816 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004817
4818 /* Free bad block descriptor memory */
4819 if (chip->badblock_pattern && chip->badblock_pattern->options
4820 & NAND_BBT_DYNAMICSTRUCT)
4821 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004823EXPORT_SYMBOL_GPL(nand_cleanup);
4824
4825/**
4826 * nand_release - [NAND Interface] Unregister the MTD device and free resources
4827 * held by the NAND device
4828 * @mtd: MTD device structure
4829 */
4830void nand_release(struct mtd_info *mtd)
4831{
4832 mtd_device_unregister(mtd);
4833 nand_cleanup(mtd_to_nand(mtd));
4834}
David Woodhousee0c7d762006-05-13 18:07:53 +01004835EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004836
David Woodhousee0c7d762006-05-13 18:07:53 +01004837MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004838MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4839MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004840MODULE_DESCRIPTION("Generic NAND flash driver code");