xref: /openbmc/u-boot/drivers/mtd/nand/raw/nand_base.c (revision a430fa06a4ac50e785fdbfb7f43c3cb14b35619c)
1*a430fa06SMiquel Raynal /*
2*a430fa06SMiquel Raynal  *  Overview:
3*a430fa06SMiquel Raynal  *   This is the generic MTD driver for NAND flash devices. It should be
4*a430fa06SMiquel Raynal  *   capable of working with almost all NAND chips currently available.
5*a430fa06SMiquel Raynal  *
6*a430fa06SMiquel Raynal  *	Additional technical information is available on
7*a430fa06SMiquel Raynal  *	http://www.linux-mtd.infradead.org/doc/nand.html
8*a430fa06SMiquel Raynal  *
9*a430fa06SMiquel Raynal  *  Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
10*a430fa06SMiquel Raynal  *		  2002-2006 Thomas Gleixner (tglx@linutronix.de)
11*a430fa06SMiquel Raynal  *
12*a430fa06SMiquel Raynal  *  Credits:
13*a430fa06SMiquel Raynal  *	David Woodhouse for adding multichip support
14*a430fa06SMiquel Raynal  *
15*a430fa06SMiquel Raynal  *	Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16*a430fa06SMiquel Raynal  *	rework for 2K page size chips
17*a430fa06SMiquel Raynal  *
18*a430fa06SMiquel Raynal  *  TODO:
19*a430fa06SMiquel Raynal  *	Enable cached programming for 2k page size chips
20*a430fa06SMiquel Raynal  *	Check, if mtd->ecctype should be set to MTD_ECC_HW
21*a430fa06SMiquel Raynal  *	if we have HW ECC support.
22*a430fa06SMiquel Raynal  *	BBT table is not serialized, has to be fixed
23*a430fa06SMiquel Raynal  *
24*a430fa06SMiquel Raynal  * This program is free software; you can redistribute it and/or modify
25*a430fa06SMiquel Raynal  * it under the terms of the GNU General Public License version 2 as
26*a430fa06SMiquel Raynal  * published by the Free Software Foundation.
27*a430fa06SMiquel Raynal  *
28*a430fa06SMiquel Raynal  */
29*a430fa06SMiquel Raynal 
30*a430fa06SMiquel Raynal #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31*a430fa06SMiquel Raynal #include <common.h>
32*a430fa06SMiquel Raynal #if CONFIG_IS_ENABLED(OF_CONTROL)
33*a430fa06SMiquel Raynal #include <fdtdec.h>
34*a430fa06SMiquel Raynal #endif
35*a430fa06SMiquel Raynal #include <malloc.h>
36*a430fa06SMiquel Raynal #include <watchdog.h>
37*a430fa06SMiquel Raynal #include <linux/err.h>
38*a430fa06SMiquel Raynal #include <linux/compat.h>
39*a430fa06SMiquel Raynal #include <linux/mtd/mtd.h>
40*a430fa06SMiquel Raynal #include <linux/mtd/rawnand.h>
41*a430fa06SMiquel Raynal #include <linux/mtd/nand_ecc.h>
42*a430fa06SMiquel Raynal #include <linux/mtd/nand_bch.h>
43*a430fa06SMiquel Raynal #ifdef CONFIG_MTD_PARTITIONS
44*a430fa06SMiquel Raynal #include <linux/mtd/partitions.h>
45*a430fa06SMiquel Raynal #endif
46*a430fa06SMiquel Raynal #include <asm/io.h>
47*a430fa06SMiquel Raynal #include <linux/errno.h>
48*a430fa06SMiquel Raynal 
49*a430fa06SMiquel Raynal /* Define default oob placement schemes for large and small page devices */
50*a430fa06SMiquel Raynal static struct nand_ecclayout nand_oob_8 = {
51*a430fa06SMiquel Raynal 	.eccbytes = 3,
52*a430fa06SMiquel Raynal 	.eccpos = {0, 1, 2},
53*a430fa06SMiquel Raynal 	.oobfree = {
54*a430fa06SMiquel Raynal 		{.offset = 3,
55*a430fa06SMiquel Raynal 		 .length = 2},
56*a430fa06SMiquel Raynal 		{.offset = 6,
57*a430fa06SMiquel Raynal 		 .length = 2} }
58*a430fa06SMiquel Raynal };
59*a430fa06SMiquel Raynal 
60*a430fa06SMiquel Raynal static struct nand_ecclayout nand_oob_16 = {
61*a430fa06SMiquel Raynal 	.eccbytes = 6,
62*a430fa06SMiquel Raynal 	.eccpos = {0, 1, 2, 3, 6, 7},
63*a430fa06SMiquel Raynal 	.oobfree = {
64*a430fa06SMiquel Raynal 		{.offset = 8,
65*a430fa06SMiquel Raynal 		 . length = 8} }
66*a430fa06SMiquel Raynal };
67*a430fa06SMiquel Raynal 
68*a430fa06SMiquel Raynal static struct nand_ecclayout nand_oob_64 = {
69*a430fa06SMiquel Raynal 	.eccbytes = 24,
70*a430fa06SMiquel Raynal 	.eccpos = {
71*a430fa06SMiquel Raynal 		   40, 41, 42, 43, 44, 45, 46, 47,
72*a430fa06SMiquel Raynal 		   48, 49, 50, 51, 52, 53, 54, 55,
73*a430fa06SMiquel Raynal 		   56, 57, 58, 59, 60, 61, 62, 63},
74*a430fa06SMiquel Raynal 	.oobfree = {
75*a430fa06SMiquel Raynal 		{.offset = 2,
76*a430fa06SMiquel Raynal 		 .length = 38} }
77*a430fa06SMiquel Raynal };
78*a430fa06SMiquel Raynal 
79*a430fa06SMiquel Raynal static struct nand_ecclayout nand_oob_128 = {
80*a430fa06SMiquel Raynal 	.eccbytes = 48,
81*a430fa06SMiquel Raynal 	.eccpos = {
82*a430fa06SMiquel Raynal 		   80, 81, 82, 83, 84, 85, 86, 87,
83*a430fa06SMiquel Raynal 		   88, 89, 90, 91, 92, 93, 94, 95,
84*a430fa06SMiquel Raynal 		   96, 97, 98, 99, 100, 101, 102, 103,
85*a430fa06SMiquel Raynal 		   104, 105, 106, 107, 108, 109, 110, 111,
86*a430fa06SMiquel Raynal 		   112, 113, 114, 115, 116, 117, 118, 119,
87*a430fa06SMiquel Raynal 		   120, 121, 122, 123, 124, 125, 126, 127},
88*a430fa06SMiquel Raynal 	.oobfree = {
89*a430fa06SMiquel Raynal 		{.offset = 2,
90*a430fa06SMiquel Raynal 		 .length = 78} }
91*a430fa06SMiquel Raynal };
92*a430fa06SMiquel Raynal 
93*a430fa06SMiquel Raynal static int nand_get_device(struct mtd_info *mtd, int new_state);
94*a430fa06SMiquel Raynal 
95*a430fa06SMiquel Raynal static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
96*a430fa06SMiquel Raynal 			     struct mtd_oob_ops *ops);
97*a430fa06SMiquel Raynal 
98*a430fa06SMiquel Raynal /*
99*a430fa06SMiquel Raynal  * For devices which display every fart in the system on a separate LED. Is
100*a430fa06SMiquel Raynal  * compiled away when LED support is disabled.
101*a430fa06SMiquel Raynal  */
102*a430fa06SMiquel Raynal DEFINE_LED_TRIGGER(nand_led_trigger);
103*a430fa06SMiquel Raynal 
104*a430fa06SMiquel Raynal static int check_offs_len(struct mtd_info *mtd,
105*a430fa06SMiquel Raynal 					loff_t ofs, uint64_t len)
106*a430fa06SMiquel Raynal {
107*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
108*a430fa06SMiquel Raynal 	int ret = 0;
109*a430fa06SMiquel Raynal 
110*a430fa06SMiquel Raynal 	/* Start address must align on block boundary */
111*a430fa06SMiquel Raynal 	if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
112*a430fa06SMiquel Raynal 		pr_debug("%s: unaligned address\n", __func__);
113*a430fa06SMiquel Raynal 		ret = -EINVAL;
114*a430fa06SMiquel Raynal 	}
115*a430fa06SMiquel Raynal 
116*a430fa06SMiquel Raynal 	/* Length must align on block boundary */
117*a430fa06SMiquel Raynal 	if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
118*a430fa06SMiquel Raynal 		pr_debug("%s: length not block aligned\n", __func__);
119*a430fa06SMiquel Raynal 		ret = -EINVAL;
120*a430fa06SMiquel Raynal 	}
121*a430fa06SMiquel Raynal 
122*a430fa06SMiquel Raynal 	return ret;
123*a430fa06SMiquel Raynal }
124*a430fa06SMiquel Raynal 
125*a430fa06SMiquel Raynal /**
126*a430fa06SMiquel Raynal  * nand_release_device - [GENERIC] release chip
127*a430fa06SMiquel Raynal  * @mtd: MTD device structure
128*a430fa06SMiquel Raynal  *
129*a430fa06SMiquel Raynal  * Release chip lock and wake up anyone waiting on the device.
130*a430fa06SMiquel Raynal  */
131*a430fa06SMiquel Raynal static void nand_release_device(struct mtd_info *mtd)
132*a430fa06SMiquel Raynal {
133*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
134*a430fa06SMiquel Raynal 
135*a430fa06SMiquel Raynal 	/* De-select the NAND device */
136*a430fa06SMiquel Raynal 	chip->select_chip(mtd, -1);
137*a430fa06SMiquel Raynal }
138*a430fa06SMiquel Raynal 
139*a430fa06SMiquel Raynal /**
140*a430fa06SMiquel Raynal  * nand_read_byte - [DEFAULT] read one byte from the chip
141*a430fa06SMiquel Raynal  * @mtd: MTD device structure
142*a430fa06SMiquel Raynal  *
143*a430fa06SMiquel Raynal  * Default read function for 8bit buswidth
144*a430fa06SMiquel Raynal  */
145*a430fa06SMiquel Raynal uint8_t nand_read_byte(struct mtd_info *mtd)
146*a430fa06SMiquel Raynal {
147*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
148*a430fa06SMiquel Raynal 	return readb(chip->IO_ADDR_R);
149*a430fa06SMiquel Raynal }
150*a430fa06SMiquel Raynal 
151*a430fa06SMiquel Raynal /**
152*a430fa06SMiquel Raynal  * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
153*a430fa06SMiquel Raynal  * @mtd: MTD device structure
154*a430fa06SMiquel Raynal  *
155*a430fa06SMiquel Raynal  * Default read function for 16bit buswidth with endianness conversion.
156*a430fa06SMiquel Raynal  *
157*a430fa06SMiquel Raynal  */
158*a430fa06SMiquel Raynal static uint8_t nand_read_byte16(struct mtd_info *mtd)
159*a430fa06SMiquel Raynal {
160*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
161*a430fa06SMiquel Raynal 	return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
162*a430fa06SMiquel Raynal }
163*a430fa06SMiquel Raynal 
164*a430fa06SMiquel Raynal /**
165*a430fa06SMiquel Raynal  * nand_read_word - [DEFAULT] read one word from the chip
166*a430fa06SMiquel Raynal  * @mtd: MTD device structure
167*a430fa06SMiquel Raynal  *
168*a430fa06SMiquel Raynal  * Default read function for 16bit buswidth without endianness conversion.
169*a430fa06SMiquel Raynal  */
170*a430fa06SMiquel Raynal static u16 nand_read_word(struct mtd_info *mtd)
171*a430fa06SMiquel Raynal {
172*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
173*a430fa06SMiquel Raynal 	return readw(chip->IO_ADDR_R);
174*a430fa06SMiquel Raynal }
175*a430fa06SMiquel Raynal 
176*a430fa06SMiquel Raynal /**
177*a430fa06SMiquel Raynal  * nand_select_chip - [DEFAULT] control CE line
178*a430fa06SMiquel Raynal  * @mtd: MTD device structure
179*a430fa06SMiquel Raynal  * @chipnr: chipnumber to select, -1 for deselect
180*a430fa06SMiquel Raynal  *
181*a430fa06SMiquel Raynal  * Default select function for 1 chip devices.
182*a430fa06SMiquel Raynal  */
183*a430fa06SMiquel Raynal static void nand_select_chip(struct mtd_info *mtd, int chipnr)
184*a430fa06SMiquel Raynal {
185*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
186*a430fa06SMiquel Raynal 
187*a430fa06SMiquel Raynal 	switch (chipnr) {
188*a430fa06SMiquel Raynal 	case -1:
189*a430fa06SMiquel Raynal 		chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
190*a430fa06SMiquel Raynal 		break;
191*a430fa06SMiquel Raynal 	case 0:
192*a430fa06SMiquel Raynal 		break;
193*a430fa06SMiquel Raynal 
194*a430fa06SMiquel Raynal 	default:
195*a430fa06SMiquel Raynal 		BUG();
196*a430fa06SMiquel Raynal 	}
197*a430fa06SMiquel Raynal }
198*a430fa06SMiquel Raynal 
199*a430fa06SMiquel Raynal /**
200*a430fa06SMiquel Raynal  * nand_write_byte - [DEFAULT] write single byte to chip
201*a430fa06SMiquel Raynal  * @mtd: MTD device structure
202*a430fa06SMiquel Raynal  * @byte: value to write
203*a430fa06SMiquel Raynal  *
204*a430fa06SMiquel Raynal  * Default function to write a byte to I/O[7:0]
205*a430fa06SMiquel Raynal  */
206*a430fa06SMiquel Raynal static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
207*a430fa06SMiquel Raynal {
208*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
209*a430fa06SMiquel Raynal 
210*a430fa06SMiquel Raynal 	chip->write_buf(mtd, &byte, 1);
211*a430fa06SMiquel Raynal }
212*a430fa06SMiquel Raynal 
213*a430fa06SMiquel Raynal /**
214*a430fa06SMiquel Raynal  * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
215*a430fa06SMiquel Raynal  * @mtd: MTD device structure
216*a430fa06SMiquel Raynal  * @byte: value to write
217*a430fa06SMiquel Raynal  *
218*a430fa06SMiquel Raynal  * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
219*a430fa06SMiquel Raynal  */
220*a430fa06SMiquel Raynal static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
221*a430fa06SMiquel Raynal {
222*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
223*a430fa06SMiquel Raynal 	uint16_t word = byte;
224*a430fa06SMiquel Raynal 
225*a430fa06SMiquel Raynal 	/*
226*a430fa06SMiquel Raynal 	 * It's not entirely clear what should happen to I/O[15:8] when writing
227*a430fa06SMiquel Raynal 	 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
228*a430fa06SMiquel Raynal 	 *
229*a430fa06SMiquel Raynal 	 *    When the host supports a 16-bit bus width, only data is
230*a430fa06SMiquel Raynal 	 *    transferred at the 16-bit width. All address and command line
231*a430fa06SMiquel Raynal 	 *    transfers shall use only the lower 8-bits of the data bus. During
232*a430fa06SMiquel Raynal 	 *    command transfers, the host may place any value on the upper
233*a430fa06SMiquel Raynal 	 *    8-bits of the data bus. During address transfers, the host shall
234*a430fa06SMiquel Raynal 	 *    set the upper 8-bits of the data bus to 00h.
235*a430fa06SMiquel Raynal 	 *
236*a430fa06SMiquel Raynal 	 * One user of the write_byte callback is nand_onfi_set_features. The
237*a430fa06SMiquel Raynal 	 * four parameters are specified to be written to I/O[7:0], but this is
238*a430fa06SMiquel Raynal 	 * neither an address nor a command transfer. Let's assume a 0 on the
239*a430fa06SMiquel Raynal 	 * upper I/O lines is OK.
240*a430fa06SMiquel Raynal 	 */
241*a430fa06SMiquel Raynal 	chip->write_buf(mtd, (uint8_t *)&word, 2);
242*a430fa06SMiquel Raynal }
243*a430fa06SMiquel Raynal 
244*a430fa06SMiquel Raynal static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
245*a430fa06SMiquel Raynal {
246*a430fa06SMiquel Raynal 	int i;
247*a430fa06SMiquel Raynal 
248*a430fa06SMiquel Raynal 	for (i = 0; i < len; i++)
249*a430fa06SMiquel Raynal 		writeb(buf[i], addr);
250*a430fa06SMiquel Raynal }
251*a430fa06SMiquel Raynal static void ioread8_rep(void *addr, uint8_t *buf, int len)
252*a430fa06SMiquel Raynal {
253*a430fa06SMiquel Raynal 	int i;
254*a430fa06SMiquel Raynal 
255*a430fa06SMiquel Raynal 	for (i = 0; i < len; i++)
256*a430fa06SMiquel Raynal 		buf[i] = readb(addr);
257*a430fa06SMiquel Raynal }
258*a430fa06SMiquel Raynal 
259*a430fa06SMiquel Raynal static void ioread16_rep(void *addr, void *buf, int len)
260*a430fa06SMiquel Raynal {
261*a430fa06SMiquel Raynal 	int i;
262*a430fa06SMiquel Raynal  	u16 *p = (u16 *) buf;
263*a430fa06SMiquel Raynal 
264*a430fa06SMiquel Raynal 	for (i = 0; i < len; i++)
265*a430fa06SMiquel Raynal 		p[i] = readw(addr);
266*a430fa06SMiquel Raynal }
267*a430fa06SMiquel Raynal 
268*a430fa06SMiquel Raynal static void iowrite16_rep(void *addr, void *buf, int len)
269*a430fa06SMiquel Raynal {
270*a430fa06SMiquel Raynal 	int i;
271*a430fa06SMiquel Raynal         u16 *p = (u16 *) buf;
272*a430fa06SMiquel Raynal 
273*a430fa06SMiquel Raynal         for (i = 0; i < len; i++)
274*a430fa06SMiquel Raynal                 writew(p[i], addr);
275*a430fa06SMiquel Raynal }
276*a430fa06SMiquel Raynal 
277*a430fa06SMiquel Raynal /**
278*a430fa06SMiquel Raynal  * nand_write_buf - [DEFAULT] write buffer to chip
279*a430fa06SMiquel Raynal  * @mtd: MTD device structure
280*a430fa06SMiquel Raynal  * @buf: data buffer
281*a430fa06SMiquel Raynal  * @len: number of bytes to write
282*a430fa06SMiquel Raynal  *
283*a430fa06SMiquel Raynal  * Default write function for 8bit buswidth.
284*a430fa06SMiquel Raynal  */
285*a430fa06SMiquel Raynal void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
286*a430fa06SMiquel Raynal {
287*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
288*a430fa06SMiquel Raynal 
289*a430fa06SMiquel Raynal 	iowrite8_rep(chip->IO_ADDR_W, buf, len);
290*a430fa06SMiquel Raynal }
291*a430fa06SMiquel Raynal 
292*a430fa06SMiquel Raynal /**
293*a430fa06SMiquel Raynal  * nand_read_buf - [DEFAULT] read chip data into buffer
294*a430fa06SMiquel Raynal  * @mtd: MTD device structure
295*a430fa06SMiquel Raynal  * @buf: buffer to store date
296*a430fa06SMiquel Raynal  * @len: number of bytes to read
297*a430fa06SMiquel Raynal  *
298*a430fa06SMiquel Raynal  * Default read function for 8bit buswidth.
299*a430fa06SMiquel Raynal  */
300*a430fa06SMiquel Raynal void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
301*a430fa06SMiquel Raynal {
302*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
303*a430fa06SMiquel Raynal 
304*a430fa06SMiquel Raynal 	ioread8_rep(chip->IO_ADDR_R, buf, len);
305*a430fa06SMiquel Raynal }
306*a430fa06SMiquel Raynal 
307*a430fa06SMiquel Raynal /**
308*a430fa06SMiquel Raynal  * nand_write_buf16 - [DEFAULT] write buffer to chip
309*a430fa06SMiquel Raynal  * @mtd: MTD device structure
310*a430fa06SMiquel Raynal  * @buf: data buffer
311*a430fa06SMiquel Raynal  * @len: number of bytes to write
312*a430fa06SMiquel Raynal  *
313*a430fa06SMiquel Raynal  * Default write function for 16bit buswidth.
314*a430fa06SMiquel Raynal  */
315*a430fa06SMiquel Raynal void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
316*a430fa06SMiquel Raynal {
317*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
318*a430fa06SMiquel Raynal 	u16 *p = (u16 *) buf;
319*a430fa06SMiquel Raynal 
320*a430fa06SMiquel Raynal 	iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
321*a430fa06SMiquel Raynal }
322*a430fa06SMiquel Raynal 
323*a430fa06SMiquel Raynal /**
324*a430fa06SMiquel Raynal  * nand_read_buf16 - [DEFAULT] read chip data into buffer
325*a430fa06SMiquel Raynal  * @mtd: MTD device structure
326*a430fa06SMiquel Raynal  * @buf: buffer to store date
327*a430fa06SMiquel Raynal  * @len: number of bytes to read
328*a430fa06SMiquel Raynal  *
329*a430fa06SMiquel Raynal  * Default read function for 16bit buswidth.
330*a430fa06SMiquel Raynal  */
331*a430fa06SMiquel Raynal void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
332*a430fa06SMiquel Raynal {
333*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
334*a430fa06SMiquel Raynal 	u16 *p = (u16 *) buf;
335*a430fa06SMiquel Raynal 
336*a430fa06SMiquel Raynal 	ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
337*a430fa06SMiquel Raynal }
338*a430fa06SMiquel Raynal 
339*a430fa06SMiquel Raynal /**
340*a430fa06SMiquel Raynal  * nand_block_bad - [DEFAULT] Read bad block marker from the chip
341*a430fa06SMiquel Raynal  * @mtd: MTD device structure
342*a430fa06SMiquel Raynal  * @ofs: offset from device start
343*a430fa06SMiquel Raynal  *
344*a430fa06SMiquel Raynal  * Check, if the block is bad.
345*a430fa06SMiquel Raynal  */
346*a430fa06SMiquel Raynal static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
347*a430fa06SMiquel Raynal {
348*a430fa06SMiquel Raynal 	int page, res = 0, i = 0;
349*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
350*a430fa06SMiquel Raynal 	u16 bad;
351*a430fa06SMiquel Raynal 
352*a430fa06SMiquel Raynal 	if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
353*a430fa06SMiquel Raynal 		ofs += mtd->erasesize - mtd->writesize;
354*a430fa06SMiquel Raynal 
355*a430fa06SMiquel Raynal 	page = (int)(ofs >> chip->page_shift) & chip->pagemask;
356*a430fa06SMiquel Raynal 
357*a430fa06SMiquel Raynal 	do {
358*a430fa06SMiquel Raynal 		if (chip->options & NAND_BUSWIDTH_16) {
359*a430fa06SMiquel Raynal 			chip->cmdfunc(mtd, NAND_CMD_READOOB,
360*a430fa06SMiquel Raynal 					chip->badblockpos & 0xFE, page);
361*a430fa06SMiquel Raynal 			bad = cpu_to_le16(chip->read_word(mtd));
362*a430fa06SMiquel Raynal 			if (chip->badblockpos & 0x1)
363*a430fa06SMiquel Raynal 				bad >>= 8;
364*a430fa06SMiquel Raynal 			else
365*a430fa06SMiquel Raynal 				bad &= 0xFF;
366*a430fa06SMiquel Raynal 		} else {
367*a430fa06SMiquel Raynal 			chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
368*a430fa06SMiquel Raynal 					page);
369*a430fa06SMiquel Raynal 			bad = chip->read_byte(mtd);
370*a430fa06SMiquel Raynal 		}
371*a430fa06SMiquel Raynal 
372*a430fa06SMiquel Raynal 		if (likely(chip->badblockbits == 8))
373*a430fa06SMiquel Raynal 			res = bad != 0xFF;
374*a430fa06SMiquel Raynal 		else
375*a430fa06SMiquel Raynal 			res = hweight8(bad) < chip->badblockbits;
376*a430fa06SMiquel Raynal 		ofs += mtd->writesize;
377*a430fa06SMiquel Raynal 		page = (int)(ofs >> chip->page_shift) & chip->pagemask;
378*a430fa06SMiquel Raynal 		i++;
379*a430fa06SMiquel Raynal 	} while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
380*a430fa06SMiquel Raynal 
381*a430fa06SMiquel Raynal 	return res;
382*a430fa06SMiquel Raynal }
383*a430fa06SMiquel Raynal 
384*a430fa06SMiquel Raynal /**
385*a430fa06SMiquel Raynal  * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
386*a430fa06SMiquel Raynal  * @mtd: MTD device structure
387*a430fa06SMiquel Raynal  * @ofs: offset from device start
388*a430fa06SMiquel Raynal  *
389*a430fa06SMiquel Raynal  * This is the default implementation, which can be overridden by a hardware
390*a430fa06SMiquel Raynal  * specific driver. It provides the details for writing a bad block marker to a
391*a430fa06SMiquel Raynal  * block.
392*a430fa06SMiquel Raynal  */
393*a430fa06SMiquel Raynal static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
394*a430fa06SMiquel Raynal {
395*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
396*a430fa06SMiquel Raynal 	struct mtd_oob_ops ops;
397*a430fa06SMiquel Raynal 	uint8_t buf[2] = { 0, 0 };
398*a430fa06SMiquel Raynal 	int ret = 0, res, i = 0;
399*a430fa06SMiquel Raynal 
400*a430fa06SMiquel Raynal 	memset(&ops, 0, sizeof(ops));
401*a430fa06SMiquel Raynal 	ops.oobbuf = buf;
402*a430fa06SMiquel Raynal 	ops.ooboffs = chip->badblockpos;
403*a430fa06SMiquel Raynal 	if (chip->options & NAND_BUSWIDTH_16) {
404*a430fa06SMiquel Raynal 		ops.ooboffs &= ~0x01;
405*a430fa06SMiquel Raynal 		ops.len = ops.ooblen = 2;
406*a430fa06SMiquel Raynal 	} else {
407*a430fa06SMiquel Raynal 		ops.len = ops.ooblen = 1;
408*a430fa06SMiquel Raynal 	}
409*a430fa06SMiquel Raynal 	ops.mode = MTD_OPS_PLACE_OOB;
410*a430fa06SMiquel Raynal 
411*a430fa06SMiquel Raynal 	/* Write to first/last page(s) if necessary */
412*a430fa06SMiquel Raynal 	if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
413*a430fa06SMiquel Raynal 		ofs += mtd->erasesize - mtd->writesize;
414*a430fa06SMiquel Raynal 	do {
415*a430fa06SMiquel Raynal 		res = nand_do_write_oob(mtd, ofs, &ops);
416*a430fa06SMiquel Raynal 		if (!ret)
417*a430fa06SMiquel Raynal 			ret = res;
418*a430fa06SMiquel Raynal 
419*a430fa06SMiquel Raynal 		i++;
420*a430fa06SMiquel Raynal 		ofs += mtd->writesize;
421*a430fa06SMiquel Raynal 	} while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
422*a430fa06SMiquel Raynal 
423*a430fa06SMiquel Raynal 	return ret;
424*a430fa06SMiquel Raynal }
425*a430fa06SMiquel Raynal 
426*a430fa06SMiquel Raynal /**
427*a430fa06SMiquel Raynal  * nand_block_markbad_lowlevel - mark a block bad
428*a430fa06SMiquel Raynal  * @mtd: MTD device structure
429*a430fa06SMiquel Raynal  * @ofs: offset from device start
430*a430fa06SMiquel Raynal  *
431*a430fa06SMiquel Raynal  * This function performs the generic NAND bad block marking steps (i.e., bad
432*a430fa06SMiquel Raynal  * block table(s) and/or marker(s)). We only allow the hardware driver to
433*a430fa06SMiquel Raynal  * specify how to write bad block markers to OOB (chip->block_markbad).
434*a430fa06SMiquel Raynal  *
435*a430fa06SMiquel Raynal  * We try operations in the following order:
436*a430fa06SMiquel Raynal  *  (1) erase the affected block, to allow OOB marker to be written cleanly
437*a430fa06SMiquel Raynal  *  (2) write bad block marker to OOB area of affected block (unless flag
438*a430fa06SMiquel Raynal  *      NAND_BBT_NO_OOB_BBM is present)
439*a430fa06SMiquel Raynal  *  (3) update the BBT
440*a430fa06SMiquel Raynal  * Note that we retain the first error encountered in (2) or (3), finish the
441*a430fa06SMiquel Raynal  * procedures, and dump the error in the end.
442*a430fa06SMiquel Raynal */
443*a430fa06SMiquel Raynal static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
444*a430fa06SMiquel Raynal {
445*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
446*a430fa06SMiquel Raynal 	int res, ret = 0;
447*a430fa06SMiquel Raynal 
448*a430fa06SMiquel Raynal 	if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
449*a430fa06SMiquel Raynal 		struct erase_info einfo;
450*a430fa06SMiquel Raynal 
451*a430fa06SMiquel Raynal 		/* Attempt erase before marking OOB */
452*a430fa06SMiquel Raynal 		memset(&einfo, 0, sizeof(einfo));
453*a430fa06SMiquel Raynal 		einfo.mtd = mtd;
454*a430fa06SMiquel Raynal 		einfo.addr = ofs;
455*a430fa06SMiquel Raynal 		einfo.len = 1ULL << chip->phys_erase_shift;
456*a430fa06SMiquel Raynal 		nand_erase_nand(mtd, &einfo, 0);
457*a430fa06SMiquel Raynal 
458*a430fa06SMiquel Raynal 		/* Write bad block marker to OOB */
459*a430fa06SMiquel Raynal 		nand_get_device(mtd, FL_WRITING);
460*a430fa06SMiquel Raynal 		ret = chip->block_markbad(mtd, ofs);
461*a430fa06SMiquel Raynal 		nand_release_device(mtd);
462*a430fa06SMiquel Raynal 	}
463*a430fa06SMiquel Raynal 
464*a430fa06SMiquel Raynal 	/* Mark block bad in BBT */
465*a430fa06SMiquel Raynal 	if (chip->bbt) {
466*a430fa06SMiquel Raynal 		res = nand_markbad_bbt(mtd, ofs);
467*a430fa06SMiquel Raynal 		if (!ret)
468*a430fa06SMiquel Raynal 			ret = res;
469*a430fa06SMiquel Raynal 	}
470*a430fa06SMiquel Raynal 
471*a430fa06SMiquel Raynal 	if (!ret)
472*a430fa06SMiquel Raynal 		mtd->ecc_stats.badblocks++;
473*a430fa06SMiquel Raynal 
474*a430fa06SMiquel Raynal 	return ret;
475*a430fa06SMiquel Raynal }
476*a430fa06SMiquel Raynal 
477*a430fa06SMiquel Raynal /**
478*a430fa06SMiquel Raynal  * nand_check_wp - [GENERIC] check if the chip is write protected
479*a430fa06SMiquel Raynal  * @mtd: MTD device structure
480*a430fa06SMiquel Raynal  *
481*a430fa06SMiquel Raynal  * Check, if the device is write protected. The function expects, that the
482*a430fa06SMiquel Raynal  * device is already selected.
483*a430fa06SMiquel Raynal  */
484*a430fa06SMiquel Raynal static int nand_check_wp(struct mtd_info *mtd)
485*a430fa06SMiquel Raynal {
486*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
487*a430fa06SMiquel Raynal 
488*a430fa06SMiquel Raynal 	/* Broken xD cards report WP despite being writable */
489*a430fa06SMiquel Raynal 	if (chip->options & NAND_BROKEN_XD)
490*a430fa06SMiquel Raynal 		return 0;
491*a430fa06SMiquel Raynal 
492*a430fa06SMiquel Raynal 	/* Check the WP bit */
493*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
494*a430fa06SMiquel Raynal 	return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
495*a430fa06SMiquel Raynal }
496*a430fa06SMiquel Raynal 
497*a430fa06SMiquel Raynal /**
498*a430fa06SMiquel Raynal  * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
499*a430fa06SMiquel Raynal  * @mtd: MTD device structure
500*a430fa06SMiquel Raynal  * @ofs: offset from device start
501*a430fa06SMiquel Raynal  *
502*a430fa06SMiquel Raynal  * Check if the block is marked as reserved.
503*a430fa06SMiquel Raynal  */
504*a430fa06SMiquel Raynal static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
505*a430fa06SMiquel Raynal {
506*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
507*a430fa06SMiquel Raynal 
508*a430fa06SMiquel Raynal 	if (!chip->bbt)
509*a430fa06SMiquel Raynal 		return 0;
510*a430fa06SMiquel Raynal 	/* Return info from the table */
511*a430fa06SMiquel Raynal 	return nand_isreserved_bbt(mtd, ofs);
512*a430fa06SMiquel Raynal }
513*a430fa06SMiquel Raynal 
514*a430fa06SMiquel Raynal /**
515*a430fa06SMiquel Raynal  * nand_block_checkbad - [GENERIC] Check if a block is marked bad
516*a430fa06SMiquel Raynal  * @mtd: MTD device structure
517*a430fa06SMiquel Raynal  * @ofs: offset from device start
518*a430fa06SMiquel Raynal  * @allowbbt: 1, if its allowed to access the bbt area
519*a430fa06SMiquel Raynal  *
520*a430fa06SMiquel Raynal  * Check, if the block is bad. Either by reading the bad block table or
521*a430fa06SMiquel Raynal  * calling of the scan function.
522*a430fa06SMiquel Raynal  */
523*a430fa06SMiquel Raynal static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
524*a430fa06SMiquel Raynal {
525*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
526*a430fa06SMiquel Raynal 
527*a430fa06SMiquel Raynal 	if (!(chip->options & NAND_SKIP_BBTSCAN) &&
528*a430fa06SMiquel Raynal 	    !(chip->options & NAND_BBT_SCANNED)) {
529*a430fa06SMiquel Raynal 		chip->options |= NAND_BBT_SCANNED;
530*a430fa06SMiquel Raynal 		chip->scan_bbt(mtd);
531*a430fa06SMiquel Raynal 	}
532*a430fa06SMiquel Raynal 
533*a430fa06SMiquel Raynal 	if (!chip->bbt)
534*a430fa06SMiquel Raynal 		return chip->block_bad(mtd, ofs);
535*a430fa06SMiquel Raynal 
536*a430fa06SMiquel Raynal 	/* Return info from the table */
537*a430fa06SMiquel Raynal 	return nand_isbad_bbt(mtd, ofs, allowbbt);
538*a430fa06SMiquel Raynal }
539*a430fa06SMiquel Raynal 
540*a430fa06SMiquel Raynal /**
541*a430fa06SMiquel Raynal  * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
542*a430fa06SMiquel Raynal  * @mtd: MTD device structure
543*a430fa06SMiquel Raynal  *
544*a430fa06SMiquel Raynal  * Wait for the ready pin after a command, and warn if a timeout occurs.
545*a430fa06SMiquel Raynal  */
546*a430fa06SMiquel Raynal void nand_wait_ready(struct mtd_info *mtd)
547*a430fa06SMiquel Raynal {
548*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
549*a430fa06SMiquel Raynal 	u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
550*a430fa06SMiquel Raynal 	u32 time_start;
551*a430fa06SMiquel Raynal 
552*a430fa06SMiquel Raynal 	time_start = get_timer(0);
553*a430fa06SMiquel Raynal 	/* Wait until command is processed or timeout occurs */
554*a430fa06SMiquel Raynal 	while (get_timer(time_start) < timeo) {
555*a430fa06SMiquel Raynal 		if (chip->dev_ready)
556*a430fa06SMiquel Raynal 			if (chip->dev_ready(mtd))
557*a430fa06SMiquel Raynal 				break;
558*a430fa06SMiquel Raynal 	}
559*a430fa06SMiquel Raynal 
560*a430fa06SMiquel Raynal 	if (!chip->dev_ready(mtd))
561*a430fa06SMiquel Raynal 		pr_warn("timeout while waiting for chip to become ready\n");
562*a430fa06SMiquel Raynal }
563*a430fa06SMiquel Raynal EXPORT_SYMBOL_GPL(nand_wait_ready);
564*a430fa06SMiquel Raynal 
565*a430fa06SMiquel Raynal /**
566*a430fa06SMiquel Raynal  * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
567*a430fa06SMiquel Raynal  * @mtd: MTD device structure
568*a430fa06SMiquel Raynal  * @timeo: Timeout in ms
569*a430fa06SMiquel Raynal  *
570*a430fa06SMiquel Raynal  * Wait for status ready (i.e. command done) or timeout.
571*a430fa06SMiquel Raynal  */
572*a430fa06SMiquel Raynal static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
573*a430fa06SMiquel Raynal {
574*a430fa06SMiquel Raynal 	register struct nand_chip *chip = mtd_to_nand(mtd);
575*a430fa06SMiquel Raynal 	u32 time_start;
576*a430fa06SMiquel Raynal 
577*a430fa06SMiquel Raynal 	timeo = (CONFIG_SYS_HZ * timeo) / 1000;
578*a430fa06SMiquel Raynal 	time_start = get_timer(0);
579*a430fa06SMiquel Raynal 	while (get_timer(time_start) < timeo) {
580*a430fa06SMiquel Raynal 		if ((chip->read_byte(mtd) & NAND_STATUS_READY))
581*a430fa06SMiquel Raynal 			break;
582*a430fa06SMiquel Raynal 		WATCHDOG_RESET();
583*a430fa06SMiquel Raynal 	}
584*a430fa06SMiquel Raynal };
585*a430fa06SMiquel Raynal 
586*a430fa06SMiquel Raynal /**
587*a430fa06SMiquel Raynal  * nand_command - [DEFAULT] Send command to NAND device
588*a430fa06SMiquel Raynal  * @mtd: MTD device structure
589*a430fa06SMiquel Raynal  * @command: the command to be sent
590*a430fa06SMiquel Raynal  * @column: the column address for this command, -1 if none
591*a430fa06SMiquel Raynal  * @page_addr: the page address for this command, -1 if none
592*a430fa06SMiquel Raynal  *
593*a430fa06SMiquel Raynal  * Send command to NAND device. This function is used for small page devices
594*a430fa06SMiquel Raynal  * (512 Bytes per page).
595*a430fa06SMiquel Raynal  */
596*a430fa06SMiquel Raynal static void nand_command(struct mtd_info *mtd, unsigned int command,
597*a430fa06SMiquel Raynal 			 int column, int page_addr)
598*a430fa06SMiquel Raynal {
599*a430fa06SMiquel Raynal 	register struct nand_chip *chip = mtd_to_nand(mtd);
600*a430fa06SMiquel Raynal 	int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
601*a430fa06SMiquel Raynal 
602*a430fa06SMiquel Raynal 	/* Write out the command to the device */
603*a430fa06SMiquel Raynal 	if (command == NAND_CMD_SEQIN) {
604*a430fa06SMiquel Raynal 		int readcmd;
605*a430fa06SMiquel Raynal 
606*a430fa06SMiquel Raynal 		if (column >= mtd->writesize) {
607*a430fa06SMiquel Raynal 			/* OOB area */
608*a430fa06SMiquel Raynal 			column -= mtd->writesize;
609*a430fa06SMiquel Raynal 			readcmd = NAND_CMD_READOOB;
610*a430fa06SMiquel Raynal 		} else if (column < 256) {
611*a430fa06SMiquel Raynal 			/* First 256 bytes --> READ0 */
612*a430fa06SMiquel Raynal 			readcmd = NAND_CMD_READ0;
613*a430fa06SMiquel Raynal 		} else {
614*a430fa06SMiquel Raynal 			column -= 256;
615*a430fa06SMiquel Raynal 			readcmd = NAND_CMD_READ1;
616*a430fa06SMiquel Raynal 		}
617*a430fa06SMiquel Raynal 		chip->cmd_ctrl(mtd, readcmd, ctrl);
618*a430fa06SMiquel Raynal 		ctrl &= ~NAND_CTRL_CHANGE;
619*a430fa06SMiquel Raynal 	}
620*a430fa06SMiquel Raynal 	chip->cmd_ctrl(mtd, command, ctrl);
621*a430fa06SMiquel Raynal 
622*a430fa06SMiquel Raynal 	/* Address cycle, when necessary */
623*a430fa06SMiquel Raynal 	ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
624*a430fa06SMiquel Raynal 	/* Serially input address */
625*a430fa06SMiquel Raynal 	if (column != -1) {
626*a430fa06SMiquel Raynal 		/* Adjust columns for 16 bit buswidth */
627*a430fa06SMiquel Raynal 		if (chip->options & NAND_BUSWIDTH_16 &&
628*a430fa06SMiquel Raynal 				!nand_opcode_8bits(command))
629*a430fa06SMiquel Raynal 			column >>= 1;
630*a430fa06SMiquel Raynal 		chip->cmd_ctrl(mtd, column, ctrl);
631*a430fa06SMiquel Raynal 		ctrl &= ~NAND_CTRL_CHANGE;
632*a430fa06SMiquel Raynal 	}
633*a430fa06SMiquel Raynal 	if (page_addr != -1) {
634*a430fa06SMiquel Raynal 		chip->cmd_ctrl(mtd, page_addr, ctrl);
635*a430fa06SMiquel Raynal 		ctrl &= ~NAND_CTRL_CHANGE;
636*a430fa06SMiquel Raynal 		chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
637*a430fa06SMiquel Raynal 		if (chip->options & NAND_ROW_ADDR_3)
638*a430fa06SMiquel Raynal 			chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
639*a430fa06SMiquel Raynal 	}
640*a430fa06SMiquel Raynal 	chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
641*a430fa06SMiquel Raynal 
642*a430fa06SMiquel Raynal 	/*
643*a430fa06SMiquel Raynal 	 * Program and erase have their own busy handlers status and sequential
644*a430fa06SMiquel Raynal 	 * in needs no delay
645*a430fa06SMiquel Raynal 	 */
646*a430fa06SMiquel Raynal 	switch (command) {
647*a430fa06SMiquel Raynal 
648*a430fa06SMiquel Raynal 	case NAND_CMD_PAGEPROG:
649*a430fa06SMiquel Raynal 	case NAND_CMD_ERASE1:
650*a430fa06SMiquel Raynal 	case NAND_CMD_ERASE2:
651*a430fa06SMiquel Raynal 	case NAND_CMD_SEQIN:
652*a430fa06SMiquel Raynal 	case NAND_CMD_STATUS:
653*a430fa06SMiquel Raynal 	case NAND_CMD_READID:
654*a430fa06SMiquel Raynal 	case NAND_CMD_SET_FEATURES:
655*a430fa06SMiquel Raynal 		return;
656*a430fa06SMiquel Raynal 
657*a430fa06SMiquel Raynal 	case NAND_CMD_RESET:
658*a430fa06SMiquel Raynal 		if (chip->dev_ready)
659*a430fa06SMiquel Raynal 			break;
660*a430fa06SMiquel Raynal 		udelay(chip->chip_delay);
661*a430fa06SMiquel Raynal 		chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
662*a430fa06SMiquel Raynal 			       NAND_CTRL_CLE | NAND_CTRL_CHANGE);
663*a430fa06SMiquel Raynal 		chip->cmd_ctrl(mtd,
664*a430fa06SMiquel Raynal 			       NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
665*a430fa06SMiquel Raynal 		/* EZ-NAND can take upto 250ms as per ONFi v4.0 */
666*a430fa06SMiquel Raynal 		nand_wait_status_ready(mtd, 250);
667*a430fa06SMiquel Raynal 		return;
668*a430fa06SMiquel Raynal 
669*a430fa06SMiquel Raynal 		/* This applies to read commands */
670*a430fa06SMiquel Raynal 	default:
671*a430fa06SMiquel Raynal 		/*
672*a430fa06SMiquel Raynal 		 * If we don't have access to the busy pin, we apply the given
673*a430fa06SMiquel Raynal 		 * command delay
674*a430fa06SMiquel Raynal 		 */
675*a430fa06SMiquel Raynal 		if (!chip->dev_ready) {
676*a430fa06SMiquel Raynal 			udelay(chip->chip_delay);
677*a430fa06SMiquel Raynal 			return;
678*a430fa06SMiquel Raynal 		}
679*a430fa06SMiquel Raynal 	}
680*a430fa06SMiquel Raynal 	/*
681*a430fa06SMiquel Raynal 	 * Apply this short delay always to ensure that we do wait tWB in
682*a430fa06SMiquel Raynal 	 * any case on any machine.
683*a430fa06SMiquel Raynal 	 */
684*a430fa06SMiquel Raynal 	ndelay(100);
685*a430fa06SMiquel Raynal 
686*a430fa06SMiquel Raynal 	nand_wait_ready(mtd);
687*a430fa06SMiquel Raynal }
688*a430fa06SMiquel Raynal 
689*a430fa06SMiquel Raynal /**
690*a430fa06SMiquel Raynal  * nand_command_lp - [DEFAULT] Send command to NAND large page device
691*a430fa06SMiquel Raynal  * @mtd: MTD device structure
692*a430fa06SMiquel Raynal  * @command: the command to be sent
693*a430fa06SMiquel Raynal  * @column: the column address for this command, -1 if none
694*a430fa06SMiquel Raynal  * @page_addr: the page address for this command, -1 if none
695*a430fa06SMiquel Raynal  *
696*a430fa06SMiquel Raynal  * Send command to NAND device. This is the version for the new large page
697*a430fa06SMiquel Raynal  * devices. We don't have the separate regions as we have in the small page
698*a430fa06SMiquel Raynal  * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
699*a430fa06SMiquel Raynal  */
700*a430fa06SMiquel Raynal static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
701*a430fa06SMiquel Raynal 			    int column, int page_addr)
702*a430fa06SMiquel Raynal {
703*a430fa06SMiquel Raynal 	register struct nand_chip *chip = mtd_to_nand(mtd);
704*a430fa06SMiquel Raynal 
705*a430fa06SMiquel Raynal 	/* Emulate NAND_CMD_READOOB */
706*a430fa06SMiquel Raynal 	if (command == NAND_CMD_READOOB) {
707*a430fa06SMiquel Raynal 		column += mtd->writesize;
708*a430fa06SMiquel Raynal 		command = NAND_CMD_READ0;
709*a430fa06SMiquel Raynal 	}
710*a430fa06SMiquel Raynal 
711*a430fa06SMiquel Raynal 	/* Command latch cycle */
712*a430fa06SMiquel Raynal 	chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
713*a430fa06SMiquel Raynal 
714*a430fa06SMiquel Raynal 	if (column != -1 || page_addr != -1) {
715*a430fa06SMiquel Raynal 		int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
716*a430fa06SMiquel Raynal 
717*a430fa06SMiquel Raynal 		/* Serially input address */
718*a430fa06SMiquel Raynal 		if (column != -1) {
719*a430fa06SMiquel Raynal 			/* Adjust columns for 16 bit buswidth */
720*a430fa06SMiquel Raynal 			if (chip->options & NAND_BUSWIDTH_16 &&
721*a430fa06SMiquel Raynal 					!nand_opcode_8bits(command))
722*a430fa06SMiquel Raynal 				column >>= 1;
723*a430fa06SMiquel Raynal 			chip->cmd_ctrl(mtd, column, ctrl);
724*a430fa06SMiquel Raynal 			ctrl &= ~NAND_CTRL_CHANGE;
725*a430fa06SMiquel Raynal 			chip->cmd_ctrl(mtd, column >> 8, ctrl);
726*a430fa06SMiquel Raynal 		}
727*a430fa06SMiquel Raynal 		if (page_addr != -1) {
728*a430fa06SMiquel Raynal 			chip->cmd_ctrl(mtd, page_addr, ctrl);
729*a430fa06SMiquel Raynal 			chip->cmd_ctrl(mtd, page_addr >> 8,
730*a430fa06SMiquel Raynal 				       NAND_NCE | NAND_ALE);
731*a430fa06SMiquel Raynal 			if (chip->options & NAND_ROW_ADDR_3)
732*a430fa06SMiquel Raynal 				chip->cmd_ctrl(mtd, page_addr >> 16,
733*a430fa06SMiquel Raynal 					       NAND_NCE | NAND_ALE);
734*a430fa06SMiquel Raynal 		}
735*a430fa06SMiquel Raynal 	}
736*a430fa06SMiquel Raynal 	chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
737*a430fa06SMiquel Raynal 
738*a430fa06SMiquel Raynal 	/*
739*a430fa06SMiquel Raynal 	 * Program and erase have their own busy handlers status, sequential
740*a430fa06SMiquel Raynal 	 * in and status need no delay.
741*a430fa06SMiquel Raynal 	 */
742*a430fa06SMiquel Raynal 	switch (command) {
743*a430fa06SMiquel Raynal 
744*a430fa06SMiquel Raynal 	case NAND_CMD_CACHEDPROG:
745*a430fa06SMiquel Raynal 	case NAND_CMD_PAGEPROG:
746*a430fa06SMiquel Raynal 	case NAND_CMD_ERASE1:
747*a430fa06SMiquel Raynal 	case NAND_CMD_ERASE2:
748*a430fa06SMiquel Raynal 	case NAND_CMD_SEQIN:
749*a430fa06SMiquel Raynal 	case NAND_CMD_RNDIN:
750*a430fa06SMiquel Raynal 	case NAND_CMD_STATUS:
751*a430fa06SMiquel Raynal 	case NAND_CMD_READID:
752*a430fa06SMiquel Raynal 	case NAND_CMD_SET_FEATURES:
753*a430fa06SMiquel Raynal 		return;
754*a430fa06SMiquel Raynal 
755*a430fa06SMiquel Raynal 	case NAND_CMD_RESET:
756*a430fa06SMiquel Raynal 		if (chip->dev_ready)
757*a430fa06SMiquel Raynal 			break;
758*a430fa06SMiquel Raynal 		udelay(chip->chip_delay);
759*a430fa06SMiquel Raynal 		chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
760*a430fa06SMiquel Raynal 			       NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
761*a430fa06SMiquel Raynal 		chip->cmd_ctrl(mtd, NAND_CMD_NONE,
762*a430fa06SMiquel Raynal 			       NAND_NCE | NAND_CTRL_CHANGE);
763*a430fa06SMiquel Raynal 		/* EZ-NAND can take upto 250ms as per ONFi v4.0 */
764*a430fa06SMiquel Raynal 		nand_wait_status_ready(mtd, 250);
765*a430fa06SMiquel Raynal 		return;
766*a430fa06SMiquel Raynal 
767*a430fa06SMiquel Raynal 	case NAND_CMD_RNDOUT:
768*a430fa06SMiquel Raynal 		/* No ready / busy check necessary */
769*a430fa06SMiquel Raynal 		chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
770*a430fa06SMiquel Raynal 			       NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
771*a430fa06SMiquel Raynal 		chip->cmd_ctrl(mtd, NAND_CMD_NONE,
772*a430fa06SMiquel Raynal 			       NAND_NCE | NAND_CTRL_CHANGE);
773*a430fa06SMiquel Raynal 		return;
774*a430fa06SMiquel Raynal 
775*a430fa06SMiquel Raynal 	case NAND_CMD_READ0:
776*a430fa06SMiquel Raynal 		chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
777*a430fa06SMiquel Raynal 			       NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
778*a430fa06SMiquel Raynal 		chip->cmd_ctrl(mtd, NAND_CMD_NONE,
779*a430fa06SMiquel Raynal 			       NAND_NCE | NAND_CTRL_CHANGE);
780*a430fa06SMiquel Raynal 
781*a430fa06SMiquel Raynal 		/* This applies to read commands */
782*a430fa06SMiquel Raynal 	default:
783*a430fa06SMiquel Raynal 		/*
784*a430fa06SMiquel Raynal 		 * If we don't have access to the busy pin, we apply the given
785*a430fa06SMiquel Raynal 		 * command delay.
786*a430fa06SMiquel Raynal 		 */
787*a430fa06SMiquel Raynal 		if (!chip->dev_ready) {
788*a430fa06SMiquel Raynal 			udelay(chip->chip_delay);
789*a430fa06SMiquel Raynal 			return;
790*a430fa06SMiquel Raynal 		}
791*a430fa06SMiquel Raynal 	}
792*a430fa06SMiquel Raynal 
793*a430fa06SMiquel Raynal 	/*
794*a430fa06SMiquel Raynal 	 * Apply this short delay always to ensure that we do wait tWB in
795*a430fa06SMiquel Raynal 	 * any case on any machine.
796*a430fa06SMiquel Raynal 	 */
797*a430fa06SMiquel Raynal 	ndelay(100);
798*a430fa06SMiquel Raynal 
799*a430fa06SMiquel Raynal 	nand_wait_ready(mtd);
800*a430fa06SMiquel Raynal }
801*a430fa06SMiquel Raynal 
802*a430fa06SMiquel Raynal /**
803*a430fa06SMiquel Raynal  * panic_nand_get_device - [GENERIC] Get chip for selected access
804*a430fa06SMiquel Raynal  * @chip: the nand chip descriptor
805*a430fa06SMiquel Raynal  * @mtd: MTD device structure
806*a430fa06SMiquel Raynal  * @new_state: the state which is requested
807*a430fa06SMiquel Raynal  *
808*a430fa06SMiquel Raynal  * Used when in panic, no locks are taken.
809*a430fa06SMiquel Raynal  */
810*a430fa06SMiquel Raynal static void panic_nand_get_device(struct nand_chip *chip,
811*a430fa06SMiquel Raynal 		      struct mtd_info *mtd, int new_state)
812*a430fa06SMiquel Raynal {
813*a430fa06SMiquel Raynal 	/* Hardware controller shared among independent devices */
814*a430fa06SMiquel Raynal 	chip->controller->active = chip;
815*a430fa06SMiquel Raynal 	chip->state = new_state;
816*a430fa06SMiquel Raynal }
817*a430fa06SMiquel Raynal 
818*a430fa06SMiquel Raynal /**
819*a430fa06SMiquel Raynal  * nand_get_device - [GENERIC] Get chip for selected access
820*a430fa06SMiquel Raynal  * @mtd: MTD device structure
821*a430fa06SMiquel Raynal  * @new_state: the state which is requested
822*a430fa06SMiquel Raynal  *
823*a430fa06SMiquel Raynal  * Get the device and lock it for exclusive access
824*a430fa06SMiquel Raynal  */
825*a430fa06SMiquel Raynal static int
826*a430fa06SMiquel Raynal nand_get_device(struct mtd_info *mtd, int new_state)
827*a430fa06SMiquel Raynal {
828*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
829*a430fa06SMiquel Raynal 	chip->state = new_state;
830*a430fa06SMiquel Raynal 	return 0;
831*a430fa06SMiquel Raynal }
832*a430fa06SMiquel Raynal 
833*a430fa06SMiquel Raynal /**
834*a430fa06SMiquel Raynal  * panic_nand_wait - [GENERIC] wait until the command is done
835*a430fa06SMiquel Raynal  * @mtd: MTD device structure
836*a430fa06SMiquel Raynal  * @chip: NAND chip structure
837*a430fa06SMiquel Raynal  * @timeo: timeout
838*a430fa06SMiquel Raynal  *
839*a430fa06SMiquel Raynal  * Wait for command done. This is a helper function for nand_wait used when
840*a430fa06SMiquel Raynal  * we are in interrupt context. May happen when in panic and trying to write
841*a430fa06SMiquel Raynal  * an oops through mtdoops.
842*a430fa06SMiquel Raynal  */
843*a430fa06SMiquel Raynal static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
844*a430fa06SMiquel Raynal 			    unsigned long timeo)
845*a430fa06SMiquel Raynal {
846*a430fa06SMiquel Raynal 	int i;
847*a430fa06SMiquel Raynal 	for (i = 0; i < timeo; i++) {
848*a430fa06SMiquel Raynal 		if (chip->dev_ready) {
849*a430fa06SMiquel Raynal 			if (chip->dev_ready(mtd))
850*a430fa06SMiquel Raynal 				break;
851*a430fa06SMiquel Raynal 		} else {
852*a430fa06SMiquel Raynal 			if (chip->read_byte(mtd) & NAND_STATUS_READY)
853*a430fa06SMiquel Raynal 				break;
854*a430fa06SMiquel Raynal 		}
855*a430fa06SMiquel Raynal 		mdelay(1);
856*a430fa06SMiquel Raynal 	}
857*a430fa06SMiquel Raynal }
858*a430fa06SMiquel Raynal 
859*a430fa06SMiquel Raynal /**
860*a430fa06SMiquel Raynal  * nand_wait - [DEFAULT] wait until the command is done
861*a430fa06SMiquel Raynal  * @mtd: MTD device structure
862*a430fa06SMiquel Raynal  * @chip: NAND chip structure
863*a430fa06SMiquel Raynal  *
864*a430fa06SMiquel Raynal  * Wait for command done. This applies to erase and program only.
865*a430fa06SMiquel Raynal  */
866*a430fa06SMiquel Raynal static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
867*a430fa06SMiquel Raynal {
868*a430fa06SMiquel Raynal 	int status;
869*a430fa06SMiquel Raynal 	unsigned long timeo = 400;
870*a430fa06SMiquel Raynal 
871*a430fa06SMiquel Raynal 	led_trigger_event(nand_led_trigger, LED_FULL);
872*a430fa06SMiquel Raynal 
873*a430fa06SMiquel Raynal 	/*
874*a430fa06SMiquel Raynal 	 * Apply this short delay always to ensure that we do wait tWB in any
875*a430fa06SMiquel Raynal 	 * case on any machine.
876*a430fa06SMiquel Raynal 	 */
877*a430fa06SMiquel Raynal 	ndelay(100);
878*a430fa06SMiquel Raynal 
879*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
880*a430fa06SMiquel Raynal 
881*a430fa06SMiquel Raynal  	u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
882*a430fa06SMiquel Raynal  	u32 time_start;
883*a430fa06SMiquel Raynal 
884*a430fa06SMiquel Raynal  	time_start = get_timer(0);
885*a430fa06SMiquel Raynal  	while (get_timer(time_start) < timer) {
886*a430fa06SMiquel Raynal 		if (chip->dev_ready) {
887*a430fa06SMiquel Raynal 			if (chip->dev_ready(mtd))
888*a430fa06SMiquel Raynal 				break;
889*a430fa06SMiquel Raynal 		} else {
890*a430fa06SMiquel Raynal 			if (chip->read_byte(mtd) & NAND_STATUS_READY)
891*a430fa06SMiquel Raynal 				break;
892*a430fa06SMiquel Raynal 		}
893*a430fa06SMiquel Raynal 	}
894*a430fa06SMiquel Raynal 	led_trigger_event(nand_led_trigger, LED_OFF);
895*a430fa06SMiquel Raynal 
896*a430fa06SMiquel Raynal 	status = (int)chip->read_byte(mtd);
897*a430fa06SMiquel Raynal 	/* This can happen if in case of timeout or buggy dev_ready */
898*a430fa06SMiquel Raynal 	WARN_ON(!(status & NAND_STATUS_READY));
899*a430fa06SMiquel Raynal 	return status;
900*a430fa06SMiquel Raynal }
901*a430fa06SMiquel Raynal 
902*a430fa06SMiquel Raynal /**
903*a430fa06SMiquel Raynal  * nand_reset_data_interface - Reset data interface and timings
904*a430fa06SMiquel Raynal  * @chip: The NAND chip
905*a430fa06SMiquel Raynal  * @chipnr: Internal die id
906*a430fa06SMiquel Raynal  *
907*a430fa06SMiquel Raynal  * Reset the Data interface and timings to ONFI mode 0.
908*a430fa06SMiquel Raynal  *
909*a430fa06SMiquel Raynal  * Returns 0 for success or negative error code otherwise.
910*a430fa06SMiquel Raynal  */
911*a430fa06SMiquel Raynal static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
912*a430fa06SMiquel Raynal {
913*a430fa06SMiquel Raynal 	struct mtd_info *mtd = nand_to_mtd(chip);
914*a430fa06SMiquel Raynal 	const struct nand_data_interface *conf;
915*a430fa06SMiquel Raynal 	int ret;
916*a430fa06SMiquel Raynal 
917*a430fa06SMiquel Raynal 	if (!chip->setup_data_interface)
918*a430fa06SMiquel Raynal 		return 0;
919*a430fa06SMiquel Raynal 
920*a430fa06SMiquel Raynal 	/*
921*a430fa06SMiquel Raynal 	 * The ONFI specification says:
922*a430fa06SMiquel Raynal 	 * "
923*a430fa06SMiquel Raynal 	 * To transition from NV-DDR or NV-DDR2 to the SDR data
924*a430fa06SMiquel Raynal 	 * interface, the host shall use the Reset (FFh) command
925*a430fa06SMiquel Raynal 	 * using SDR timing mode 0. A device in any timing mode is
926*a430fa06SMiquel Raynal 	 * required to recognize Reset (FFh) command issued in SDR
927*a430fa06SMiquel Raynal 	 * timing mode 0.
928*a430fa06SMiquel Raynal 	 * "
929*a430fa06SMiquel Raynal 	 *
930*a430fa06SMiquel Raynal 	 * Configure the data interface in SDR mode and set the
931*a430fa06SMiquel Raynal 	 * timings to timing mode 0.
932*a430fa06SMiquel Raynal 	 */
933*a430fa06SMiquel Raynal 
934*a430fa06SMiquel Raynal 	conf = nand_get_default_data_interface();
935*a430fa06SMiquel Raynal 	ret = chip->setup_data_interface(mtd, chipnr, conf);
936*a430fa06SMiquel Raynal 	if (ret)
937*a430fa06SMiquel Raynal 		pr_err("Failed to configure data interface to SDR timing mode 0\n");
938*a430fa06SMiquel Raynal 
939*a430fa06SMiquel Raynal 	return ret;
940*a430fa06SMiquel Raynal }
941*a430fa06SMiquel Raynal 
942*a430fa06SMiquel Raynal /**
943*a430fa06SMiquel Raynal  * nand_setup_data_interface - Setup the best data interface and timings
944*a430fa06SMiquel Raynal  * @chip: The NAND chip
945*a430fa06SMiquel Raynal  * @chipnr: Internal die id
946*a430fa06SMiquel Raynal  *
947*a430fa06SMiquel Raynal  * Find and configure the best data interface and NAND timings supported by
948*a430fa06SMiquel Raynal  * the chip and the driver.
949*a430fa06SMiquel Raynal  * First tries to retrieve supported timing modes from ONFI information,
950*a430fa06SMiquel Raynal  * and if the NAND chip does not support ONFI, relies on the
951*a430fa06SMiquel Raynal  * ->onfi_timing_mode_default specified in the nand_ids table.
952*a430fa06SMiquel Raynal  *
953*a430fa06SMiquel Raynal  * Returns 0 for success or negative error code otherwise.
954*a430fa06SMiquel Raynal  */
955*a430fa06SMiquel Raynal static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
956*a430fa06SMiquel Raynal {
957*a430fa06SMiquel Raynal 	struct mtd_info *mtd = nand_to_mtd(chip);
958*a430fa06SMiquel Raynal 	int ret;
959*a430fa06SMiquel Raynal 
960*a430fa06SMiquel Raynal 	if (!chip->setup_data_interface || !chip->data_interface)
961*a430fa06SMiquel Raynal 		return 0;
962*a430fa06SMiquel Raynal 
963*a430fa06SMiquel Raynal 	/*
964*a430fa06SMiquel Raynal 	 * Ensure the timing mode has been changed on the chip side
965*a430fa06SMiquel Raynal 	 * before changing timings on the controller side.
966*a430fa06SMiquel Raynal 	 */
967*a430fa06SMiquel Raynal 	if (chip->onfi_version) {
968*a430fa06SMiquel Raynal 		u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
969*a430fa06SMiquel Raynal 			chip->onfi_timing_mode_default,
970*a430fa06SMiquel Raynal 		};
971*a430fa06SMiquel Raynal 
972*a430fa06SMiquel Raynal 		ret = chip->onfi_set_features(mtd, chip,
973*a430fa06SMiquel Raynal 				ONFI_FEATURE_ADDR_TIMING_MODE,
974*a430fa06SMiquel Raynal 				tmode_param);
975*a430fa06SMiquel Raynal 		if (ret)
976*a430fa06SMiquel Raynal 			goto err;
977*a430fa06SMiquel Raynal 	}
978*a430fa06SMiquel Raynal 
979*a430fa06SMiquel Raynal 	ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
980*a430fa06SMiquel Raynal err:
981*a430fa06SMiquel Raynal 	return ret;
982*a430fa06SMiquel Raynal }
983*a430fa06SMiquel Raynal 
984*a430fa06SMiquel Raynal /**
985*a430fa06SMiquel Raynal  * nand_init_data_interface - find the best data interface and timings
986*a430fa06SMiquel Raynal  * @chip: The NAND chip
987*a430fa06SMiquel Raynal  *
988*a430fa06SMiquel Raynal  * Find the best data interface and NAND timings supported by the chip
989*a430fa06SMiquel Raynal  * and the driver.
990*a430fa06SMiquel Raynal  * First tries to retrieve supported timing modes from ONFI information,
991*a430fa06SMiquel Raynal  * and if the NAND chip does not support ONFI, relies on the
992*a430fa06SMiquel Raynal  * ->onfi_timing_mode_default specified in the nand_ids table. After this
993*a430fa06SMiquel Raynal  * function nand_chip->data_interface is initialized with the best timing mode
994*a430fa06SMiquel Raynal  * available.
995*a430fa06SMiquel Raynal  *
996*a430fa06SMiquel Raynal  * Returns 0 for success or negative error code otherwise.
997*a430fa06SMiquel Raynal  */
998*a430fa06SMiquel Raynal static int nand_init_data_interface(struct nand_chip *chip)
999*a430fa06SMiquel Raynal {
1000*a430fa06SMiquel Raynal 	struct mtd_info *mtd = nand_to_mtd(chip);
1001*a430fa06SMiquel Raynal 	int modes, mode, ret;
1002*a430fa06SMiquel Raynal 
1003*a430fa06SMiquel Raynal 	if (!chip->setup_data_interface)
1004*a430fa06SMiquel Raynal 		return 0;
1005*a430fa06SMiquel Raynal 
1006*a430fa06SMiquel Raynal 	/*
1007*a430fa06SMiquel Raynal 	 * First try to identify the best timings from ONFI parameters and
1008*a430fa06SMiquel Raynal 	 * if the NAND does not support ONFI, fallback to the default ONFI
1009*a430fa06SMiquel Raynal 	 * timing mode.
1010*a430fa06SMiquel Raynal 	 */
1011*a430fa06SMiquel Raynal 	modes = onfi_get_async_timing_mode(chip);
1012*a430fa06SMiquel Raynal 	if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1013*a430fa06SMiquel Raynal 		if (!chip->onfi_timing_mode_default)
1014*a430fa06SMiquel Raynal 			return 0;
1015*a430fa06SMiquel Raynal 
1016*a430fa06SMiquel Raynal 		modes = GENMASK(chip->onfi_timing_mode_default, 0);
1017*a430fa06SMiquel Raynal 	}
1018*a430fa06SMiquel Raynal 
1019*a430fa06SMiquel Raynal 	chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1020*a430fa06SMiquel Raynal 				       GFP_KERNEL);
1021*a430fa06SMiquel Raynal 	if (!chip->data_interface)
1022*a430fa06SMiquel Raynal 		return -ENOMEM;
1023*a430fa06SMiquel Raynal 
1024*a430fa06SMiquel Raynal 	for (mode = fls(modes) - 1; mode >= 0; mode--) {
1025*a430fa06SMiquel Raynal 		ret = onfi_init_data_interface(chip, chip->data_interface,
1026*a430fa06SMiquel Raynal 					       NAND_SDR_IFACE, mode);
1027*a430fa06SMiquel Raynal 		if (ret)
1028*a430fa06SMiquel Raynal 			continue;
1029*a430fa06SMiquel Raynal 
1030*a430fa06SMiquel Raynal 		/* Pass -1 to only */
1031*a430fa06SMiquel Raynal 		ret = chip->setup_data_interface(mtd,
1032*a430fa06SMiquel Raynal 						 NAND_DATA_IFACE_CHECK_ONLY,
1033*a430fa06SMiquel Raynal 						 chip->data_interface);
1034*a430fa06SMiquel Raynal 		if (!ret) {
1035*a430fa06SMiquel Raynal 			chip->onfi_timing_mode_default = mode;
1036*a430fa06SMiquel Raynal 			break;
1037*a430fa06SMiquel Raynal 		}
1038*a430fa06SMiquel Raynal 	}
1039*a430fa06SMiquel Raynal 
1040*a430fa06SMiquel Raynal 	return 0;
1041*a430fa06SMiquel Raynal }
1042*a430fa06SMiquel Raynal 
1043*a430fa06SMiquel Raynal static void __maybe_unused nand_release_data_interface(struct nand_chip *chip)
1044*a430fa06SMiquel Raynal {
1045*a430fa06SMiquel Raynal 	kfree(chip->data_interface);
1046*a430fa06SMiquel Raynal }
1047*a430fa06SMiquel Raynal 
1048*a430fa06SMiquel Raynal /**
1049*a430fa06SMiquel Raynal  * nand_reset - Reset and initialize a NAND device
1050*a430fa06SMiquel Raynal  * @chip: The NAND chip
1051*a430fa06SMiquel Raynal  * @chipnr: Internal die id
1052*a430fa06SMiquel Raynal  *
1053*a430fa06SMiquel Raynal  * Returns 0 for success or negative error code otherwise
1054*a430fa06SMiquel Raynal  */
1055*a430fa06SMiquel Raynal int nand_reset(struct nand_chip *chip, int chipnr)
1056*a430fa06SMiquel Raynal {
1057*a430fa06SMiquel Raynal 	struct mtd_info *mtd = nand_to_mtd(chip);
1058*a430fa06SMiquel Raynal 	int ret;
1059*a430fa06SMiquel Raynal 
1060*a430fa06SMiquel Raynal 	ret = nand_reset_data_interface(chip, chipnr);
1061*a430fa06SMiquel Raynal 	if (ret)
1062*a430fa06SMiquel Raynal 		return ret;
1063*a430fa06SMiquel Raynal 
1064*a430fa06SMiquel Raynal 	/*
1065*a430fa06SMiquel Raynal 	 * The CS line has to be released before we can apply the new NAND
1066*a430fa06SMiquel Raynal 	 * interface settings, hence this weird ->select_chip() dance.
1067*a430fa06SMiquel Raynal 	 */
1068*a430fa06SMiquel Raynal 	chip->select_chip(mtd, chipnr);
1069*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1070*a430fa06SMiquel Raynal 	chip->select_chip(mtd, -1);
1071*a430fa06SMiquel Raynal 
1072*a430fa06SMiquel Raynal 	chip->select_chip(mtd, chipnr);
1073*a430fa06SMiquel Raynal 	ret = nand_setup_data_interface(chip, chipnr);
1074*a430fa06SMiquel Raynal 	chip->select_chip(mtd, -1);
1075*a430fa06SMiquel Raynal 	if (ret)
1076*a430fa06SMiquel Raynal 		return ret;
1077*a430fa06SMiquel Raynal 
1078*a430fa06SMiquel Raynal 	return 0;
1079*a430fa06SMiquel Raynal }
1080*a430fa06SMiquel Raynal 
1081*a430fa06SMiquel Raynal /**
1082*a430fa06SMiquel Raynal  * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1083*a430fa06SMiquel Raynal  * @buf: buffer to test
1084*a430fa06SMiquel Raynal  * @len: buffer length
1085*a430fa06SMiquel Raynal  * @bitflips_threshold: maximum number of bitflips
1086*a430fa06SMiquel Raynal  *
1087*a430fa06SMiquel Raynal  * Check if a buffer contains only 0xff, which means the underlying region
1088*a430fa06SMiquel Raynal  * has been erased and is ready to be programmed.
1089*a430fa06SMiquel Raynal  * The bitflips_threshold specify the maximum number of bitflips before
1090*a430fa06SMiquel Raynal  * considering the region is not erased.
1091*a430fa06SMiquel Raynal  * Note: The logic of this function has been extracted from the memweight
1092*a430fa06SMiquel Raynal  * implementation, except that nand_check_erased_buf function exit before
1093*a430fa06SMiquel Raynal  * testing the whole buffer if the number of bitflips exceed the
1094*a430fa06SMiquel Raynal  * bitflips_threshold value.
1095*a430fa06SMiquel Raynal  *
1096*a430fa06SMiquel Raynal  * Returns a positive number of bitflips less than or equal to
1097*a430fa06SMiquel Raynal  * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1098*a430fa06SMiquel Raynal  * threshold.
1099*a430fa06SMiquel Raynal  */
1100*a430fa06SMiquel Raynal static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1101*a430fa06SMiquel Raynal {
1102*a430fa06SMiquel Raynal 	const unsigned char *bitmap = buf;
1103*a430fa06SMiquel Raynal 	int bitflips = 0;
1104*a430fa06SMiquel Raynal 	int weight;
1105*a430fa06SMiquel Raynal 
1106*a430fa06SMiquel Raynal 	for (; len && ((uintptr_t)bitmap) % sizeof(long);
1107*a430fa06SMiquel Raynal 	     len--, bitmap++) {
1108*a430fa06SMiquel Raynal 		weight = hweight8(*bitmap);
1109*a430fa06SMiquel Raynal 		bitflips += BITS_PER_BYTE - weight;
1110*a430fa06SMiquel Raynal 		if (unlikely(bitflips > bitflips_threshold))
1111*a430fa06SMiquel Raynal 			return -EBADMSG;
1112*a430fa06SMiquel Raynal 	}
1113*a430fa06SMiquel Raynal 
1114*a430fa06SMiquel Raynal 	for (; len >= 4; len -= 4, bitmap += 4) {
1115*a430fa06SMiquel Raynal 		weight = hweight32(*((u32 *)bitmap));
1116*a430fa06SMiquel Raynal 		bitflips += 32 - weight;
1117*a430fa06SMiquel Raynal 		if (unlikely(bitflips > bitflips_threshold))
1118*a430fa06SMiquel Raynal 			return -EBADMSG;
1119*a430fa06SMiquel Raynal 	}
1120*a430fa06SMiquel Raynal 
1121*a430fa06SMiquel Raynal 	for (; len > 0; len--, bitmap++) {
1122*a430fa06SMiquel Raynal 		weight = hweight8(*bitmap);
1123*a430fa06SMiquel Raynal 		bitflips += BITS_PER_BYTE - weight;
1124*a430fa06SMiquel Raynal 		if (unlikely(bitflips > bitflips_threshold))
1125*a430fa06SMiquel Raynal 			return -EBADMSG;
1126*a430fa06SMiquel Raynal 	}
1127*a430fa06SMiquel Raynal 
1128*a430fa06SMiquel Raynal 	return bitflips;
1129*a430fa06SMiquel Raynal }
1130*a430fa06SMiquel Raynal 
1131*a430fa06SMiquel Raynal /**
1132*a430fa06SMiquel Raynal  * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1133*a430fa06SMiquel Raynal  *				 0xff data
1134*a430fa06SMiquel Raynal  * @data: data buffer to test
1135*a430fa06SMiquel Raynal  * @datalen: data length
1136*a430fa06SMiquel Raynal  * @ecc: ECC buffer
1137*a430fa06SMiquel Raynal  * @ecclen: ECC length
1138*a430fa06SMiquel Raynal  * @extraoob: extra OOB buffer
1139*a430fa06SMiquel Raynal  * @extraooblen: extra OOB length
1140*a430fa06SMiquel Raynal  * @bitflips_threshold: maximum number of bitflips
1141*a430fa06SMiquel Raynal  *
1142*a430fa06SMiquel Raynal  * Check if a data buffer and its associated ECC and OOB data contains only
1143*a430fa06SMiquel Raynal  * 0xff pattern, which means the underlying region has been erased and is
1144*a430fa06SMiquel Raynal  * ready to be programmed.
1145*a430fa06SMiquel Raynal  * The bitflips_threshold specify the maximum number of bitflips before
1146*a430fa06SMiquel Raynal  * considering the region as not erased.
1147*a430fa06SMiquel Raynal  *
1148*a430fa06SMiquel Raynal  * Note:
1149*a430fa06SMiquel Raynal  * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1150*a430fa06SMiquel Raynal  *    different from the NAND page size. When fixing bitflips, ECC engines will
1151*a430fa06SMiquel Raynal  *    report the number of errors per chunk, and the NAND core infrastructure
1152*a430fa06SMiquel Raynal  *    expect you to return the maximum number of bitflips for the whole page.
1153*a430fa06SMiquel Raynal  *    This is why you should always use this function on a single chunk and
1154*a430fa06SMiquel Raynal  *    not on the whole page. After checking each chunk you should update your
1155*a430fa06SMiquel Raynal  *    max_bitflips value accordingly.
1156*a430fa06SMiquel Raynal  * 2/ When checking for bitflips in erased pages you should not only check
1157*a430fa06SMiquel Raynal  *    the payload data but also their associated ECC data, because a user might
1158*a430fa06SMiquel Raynal  *    have programmed almost all bits to 1 but a few. In this case, we
1159*a430fa06SMiquel Raynal  *    shouldn't consider the chunk as erased, and checking ECC bytes prevent
1160*a430fa06SMiquel Raynal  *    this case.
1161*a430fa06SMiquel Raynal  * 3/ The extraoob argument is optional, and should be used if some of your OOB
1162*a430fa06SMiquel Raynal  *    data are protected by the ECC engine.
1163*a430fa06SMiquel Raynal  *    It could also be used if you support subpages and want to attach some
1164*a430fa06SMiquel Raynal  *    extra OOB data to an ECC chunk.
1165*a430fa06SMiquel Raynal  *
1166*a430fa06SMiquel Raynal  * Returns a positive number of bitflips less than or equal to
1167*a430fa06SMiquel Raynal  * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1168*a430fa06SMiquel Raynal  * threshold. In case of success, the passed buffers are filled with 0xff.
1169*a430fa06SMiquel Raynal  */
1170*a430fa06SMiquel Raynal int nand_check_erased_ecc_chunk(void *data, int datalen,
1171*a430fa06SMiquel Raynal 				void *ecc, int ecclen,
1172*a430fa06SMiquel Raynal 				void *extraoob, int extraooblen,
1173*a430fa06SMiquel Raynal 				int bitflips_threshold)
1174*a430fa06SMiquel Raynal {
1175*a430fa06SMiquel Raynal 	int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1176*a430fa06SMiquel Raynal 
1177*a430fa06SMiquel Raynal 	data_bitflips = nand_check_erased_buf(data, datalen,
1178*a430fa06SMiquel Raynal 					      bitflips_threshold);
1179*a430fa06SMiquel Raynal 	if (data_bitflips < 0)
1180*a430fa06SMiquel Raynal 		return data_bitflips;
1181*a430fa06SMiquel Raynal 
1182*a430fa06SMiquel Raynal 	bitflips_threshold -= data_bitflips;
1183*a430fa06SMiquel Raynal 
1184*a430fa06SMiquel Raynal 	ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1185*a430fa06SMiquel Raynal 	if (ecc_bitflips < 0)
1186*a430fa06SMiquel Raynal 		return ecc_bitflips;
1187*a430fa06SMiquel Raynal 
1188*a430fa06SMiquel Raynal 	bitflips_threshold -= ecc_bitflips;
1189*a430fa06SMiquel Raynal 
1190*a430fa06SMiquel Raynal 	extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1191*a430fa06SMiquel Raynal 						  bitflips_threshold);
1192*a430fa06SMiquel Raynal 	if (extraoob_bitflips < 0)
1193*a430fa06SMiquel Raynal 		return extraoob_bitflips;
1194*a430fa06SMiquel Raynal 
1195*a430fa06SMiquel Raynal 	if (data_bitflips)
1196*a430fa06SMiquel Raynal 		memset(data, 0xff, datalen);
1197*a430fa06SMiquel Raynal 
1198*a430fa06SMiquel Raynal 	if (ecc_bitflips)
1199*a430fa06SMiquel Raynal 		memset(ecc, 0xff, ecclen);
1200*a430fa06SMiquel Raynal 
1201*a430fa06SMiquel Raynal 	if (extraoob_bitflips)
1202*a430fa06SMiquel Raynal 		memset(extraoob, 0xff, extraooblen);
1203*a430fa06SMiquel Raynal 
1204*a430fa06SMiquel Raynal 	return data_bitflips + ecc_bitflips + extraoob_bitflips;
1205*a430fa06SMiquel Raynal }
1206*a430fa06SMiquel Raynal EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1207*a430fa06SMiquel Raynal 
1208*a430fa06SMiquel Raynal /**
1209*a430fa06SMiquel Raynal  * nand_read_page_raw - [INTERN] read raw page data without ecc
1210*a430fa06SMiquel Raynal  * @mtd: mtd info structure
1211*a430fa06SMiquel Raynal  * @chip: nand chip info structure
1212*a430fa06SMiquel Raynal  * @buf: buffer to store read data
1213*a430fa06SMiquel Raynal  * @oob_required: caller requires OOB data read to chip->oob_poi
1214*a430fa06SMiquel Raynal  * @page: page number to read
1215*a430fa06SMiquel Raynal  *
1216*a430fa06SMiquel Raynal  * Not for syndrome calculating ECC controllers, which use a special oob layout.
1217*a430fa06SMiquel Raynal  */
1218*a430fa06SMiquel Raynal static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1219*a430fa06SMiquel Raynal 			      uint8_t *buf, int oob_required, int page)
1220*a430fa06SMiquel Raynal {
1221*a430fa06SMiquel Raynal 	chip->read_buf(mtd, buf, mtd->writesize);
1222*a430fa06SMiquel Raynal 	if (oob_required)
1223*a430fa06SMiquel Raynal 		chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1224*a430fa06SMiquel Raynal 	return 0;
1225*a430fa06SMiquel Raynal }
1226*a430fa06SMiquel Raynal 
1227*a430fa06SMiquel Raynal /**
1228*a430fa06SMiquel Raynal  * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1229*a430fa06SMiquel Raynal  * @mtd: mtd info structure
1230*a430fa06SMiquel Raynal  * @chip: nand chip info structure
1231*a430fa06SMiquel Raynal  * @buf: buffer to store read data
1232*a430fa06SMiquel Raynal  * @oob_required: caller requires OOB data read to chip->oob_poi
1233*a430fa06SMiquel Raynal  * @page: page number to read
1234*a430fa06SMiquel Raynal  *
1235*a430fa06SMiquel Raynal  * We need a special oob layout and handling even when OOB isn't used.
1236*a430fa06SMiquel Raynal  */
1237*a430fa06SMiquel Raynal static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
1238*a430fa06SMiquel Raynal 				       struct nand_chip *chip, uint8_t *buf,
1239*a430fa06SMiquel Raynal 				       int oob_required, int page)
1240*a430fa06SMiquel Raynal {
1241*a430fa06SMiquel Raynal 	int eccsize = chip->ecc.size;
1242*a430fa06SMiquel Raynal 	int eccbytes = chip->ecc.bytes;
1243*a430fa06SMiquel Raynal 	uint8_t *oob = chip->oob_poi;
1244*a430fa06SMiquel Raynal 	int steps, size;
1245*a430fa06SMiquel Raynal 
1246*a430fa06SMiquel Raynal 	for (steps = chip->ecc.steps; steps > 0; steps--) {
1247*a430fa06SMiquel Raynal 		chip->read_buf(mtd, buf, eccsize);
1248*a430fa06SMiquel Raynal 		buf += eccsize;
1249*a430fa06SMiquel Raynal 
1250*a430fa06SMiquel Raynal 		if (chip->ecc.prepad) {
1251*a430fa06SMiquel Raynal 			chip->read_buf(mtd, oob, chip->ecc.prepad);
1252*a430fa06SMiquel Raynal 			oob += chip->ecc.prepad;
1253*a430fa06SMiquel Raynal 		}
1254*a430fa06SMiquel Raynal 
1255*a430fa06SMiquel Raynal 		chip->read_buf(mtd, oob, eccbytes);
1256*a430fa06SMiquel Raynal 		oob += eccbytes;
1257*a430fa06SMiquel Raynal 
1258*a430fa06SMiquel Raynal 		if (chip->ecc.postpad) {
1259*a430fa06SMiquel Raynal 			chip->read_buf(mtd, oob, chip->ecc.postpad);
1260*a430fa06SMiquel Raynal 			oob += chip->ecc.postpad;
1261*a430fa06SMiquel Raynal 		}
1262*a430fa06SMiquel Raynal 	}
1263*a430fa06SMiquel Raynal 
1264*a430fa06SMiquel Raynal 	size = mtd->oobsize - (oob - chip->oob_poi);
1265*a430fa06SMiquel Raynal 	if (size)
1266*a430fa06SMiquel Raynal 		chip->read_buf(mtd, oob, size);
1267*a430fa06SMiquel Raynal 
1268*a430fa06SMiquel Raynal 	return 0;
1269*a430fa06SMiquel Raynal }
1270*a430fa06SMiquel Raynal 
1271*a430fa06SMiquel Raynal /**
1272*a430fa06SMiquel Raynal  * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1273*a430fa06SMiquel Raynal  * @mtd: mtd info structure
1274*a430fa06SMiquel Raynal  * @chip: nand chip info structure
1275*a430fa06SMiquel Raynal  * @buf: buffer to store read data
1276*a430fa06SMiquel Raynal  * @oob_required: caller requires OOB data read to chip->oob_poi
1277*a430fa06SMiquel Raynal  * @page: page number to read
1278*a430fa06SMiquel Raynal  */
1279*a430fa06SMiquel Raynal static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1280*a430fa06SMiquel Raynal 				uint8_t *buf, int oob_required, int page)
1281*a430fa06SMiquel Raynal {
1282*a430fa06SMiquel Raynal 	int i, eccsize = chip->ecc.size;
1283*a430fa06SMiquel Raynal 	int eccbytes = chip->ecc.bytes;
1284*a430fa06SMiquel Raynal 	int eccsteps = chip->ecc.steps;
1285*a430fa06SMiquel Raynal 	uint8_t *p = buf;
1286*a430fa06SMiquel Raynal 	uint8_t *ecc_calc = chip->buffers->ecccalc;
1287*a430fa06SMiquel Raynal 	uint8_t *ecc_code = chip->buffers->ecccode;
1288*a430fa06SMiquel Raynal 	uint32_t *eccpos = chip->ecc.layout->eccpos;
1289*a430fa06SMiquel Raynal 	unsigned int max_bitflips = 0;
1290*a430fa06SMiquel Raynal 
1291*a430fa06SMiquel Raynal 	chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
1292*a430fa06SMiquel Raynal 
1293*a430fa06SMiquel Raynal 	for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1294*a430fa06SMiquel Raynal 		chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1295*a430fa06SMiquel Raynal 
1296*a430fa06SMiquel Raynal 	for (i = 0; i < chip->ecc.total; i++)
1297*a430fa06SMiquel Raynal 		ecc_code[i] = chip->oob_poi[eccpos[i]];
1298*a430fa06SMiquel Raynal 
1299*a430fa06SMiquel Raynal 	eccsteps = chip->ecc.steps;
1300*a430fa06SMiquel Raynal 	p = buf;
1301*a430fa06SMiquel Raynal 
1302*a430fa06SMiquel Raynal 	for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1303*a430fa06SMiquel Raynal 		int stat;
1304*a430fa06SMiquel Raynal 
1305*a430fa06SMiquel Raynal 		stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1306*a430fa06SMiquel Raynal 		if (stat < 0) {
1307*a430fa06SMiquel Raynal 			mtd->ecc_stats.failed++;
1308*a430fa06SMiquel Raynal 		} else {
1309*a430fa06SMiquel Raynal 			mtd->ecc_stats.corrected += stat;
1310*a430fa06SMiquel Raynal 			max_bitflips = max_t(unsigned int, max_bitflips, stat);
1311*a430fa06SMiquel Raynal 		}
1312*a430fa06SMiquel Raynal 	}
1313*a430fa06SMiquel Raynal 	return max_bitflips;
1314*a430fa06SMiquel Raynal }
1315*a430fa06SMiquel Raynal 
1316*a430fa06SMiquel Raynal /**
1317*a430fa06SMiquel Raynal  * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
1318*a430fa06SMiquel Raynal  * @mtd: mtd info structure
1319*a430fa06SMiquel Raynal  * @chip: nand chip info structure
1320*a430fa06SMiquel Raynal  * @data_offs: offset of requested data within the page
1321*a430fa06SMiquel Raynal  * @readlen: data length
1322*a430fa06SMiquel Raynal  * @bufpoi: buffer to store read data
1323*a430fa06SMiquel Raynal  * @page: page number to read
1324*a430fa06SMiquel Raynal  */
1325*a430fa06SMiquel Raynal static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1326*a430fa06SMiquel Raynal 			uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1327*a430fa06SMiquel Raynal 			int page)
1328*a430fa06SMiquel Raynal {
1329*a430fa06SMiquel Raynal 	int start_step, end_step, num_steps;
1330*a430fa06SMiquel Raynal 	uint32_t *eccpos = chip->ecc.layout->eccpos;
1331*a430fa06SMiquel Raynal 	uint8_t *p;
1332*a430fa06SMiquel Raynal 	int data_col_addr, i, gaps = 0;
1333*a430fa06SMiquel Raynal 	int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1334*a430fa06SMiquel Raynal 	int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
1335*a430fa06SMiquel Raynal 	int index;
1336*a430fa06SMiquel Raynal 	unsigned int max_bitflips = 0;
1337*a430fa06SMiquel Raynal 
1338*a430fa06SMiquel Raynal 	/* Column address within the page aligned to ECC size (256bytes) */
1339*a430fa06SMiquel Raynal 	start_step = data_offs / chip->ecc.size;
1340*a430fa06SMiquel Raynal 	end_step = (data_offs + readlen - 1) / chip->ecc.size;
1341*a430fa06SMiquel Raynal 	num_steps = end_step - start_step + 1;
1342*a430fa06SMiquel Raynal 	index = start_step * chip->ecc.bytes;
1343*a430fa06SMiquel Raynal 
1344*a430fa06SMiquel Raynal 	/* Data size aligned to ECC ecc.size */
1345*a430fa06SMiquel Raynal 	datafrag_len = num_steps * chip->ecc.size;
1346*a430fa06SMiquel Raynal 	eccfrag_len = num_steps * chip->ecc.bytes;
1347*a430fa06SMiquel Raynal 
1348*a430fa06SMiquel Raynal 	data_col_addr = start_step * chip->ecc.size;
1349*a430fa06SMiquel Raynal 	/* If we read not a page aligned data */
1350*a430fa06SMiquel Raynal 	if (data_col_addr != 0)
1351*a430fa06SMiquel Raynal 		chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1352*a430fa06SMiquel Raynal 
1353*a430fa06SMiquel Raynal 	p = bufpoi + data_col_addr;
1354*a430fa06SMiquel Raynal 	chip->read_buf(mtd, p, datafrag_len);
1355*a430fa06SMiquel Raynal 
1356*a430fa06SMiquel Raynal 	/* Calculate ECC */
1357*a430fa06SMiquel Raynal 	for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1358*a430fa06SMiquel Raynal 		chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1359*a430fa06SMiquel Raynal 
1360*a430fa06SMiquel Raynal 	/*
1361*a430fa06SMiquel Raynal 	 * The performance is faster if we position offsets according to
1362*a430fa06SMiquel Raynal 	 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1363*a430fa06SMiquel Raynal 	 */
1364*a430fa06SMiquel Raynal 	for (i = 0; i < eccfrag_len - 1; i++) {
1365*a430fa06SMiquel Raynal 		if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
1366*a430fa06SMiquel Raynal 			gaps = 1;
1367*a430fa06SMiquel Raynal 			break;
1368*a430fa06SMiquel Raynal 		}
1369*a430fa06SMiquel Raynal 	}
1370*a430fa06SMiquel Raynal 	if (gaps) {
1371*a430fa06SMiquel Raynal 		chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1372*a430fa06SMiquel Raynal 		chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1373*a430fa06SMiquel Raynal 	} else {
1374*a430fa06SMiquel Raynal 		/*
1375*a430fa06SMiquel Raynal 		 * Send the command to read the particular ECC bytes take care
1376*a430fa06SMiquel Raynal 		 * about buswidth alignment in read_buf.
1377*a430fa06SMiquel Raynal 		 */
1378*a430fa06SMiquel Raynal 		aligned_pos = eccpos[index] & ~(busw - 1);
1379*a430fa06SMiquel Raynal 		aligned_len = eccfrag_len;
1380*a430fa06SMiquel Raynal 		if (eccpos[index] & (busw - 1))
1381*a430fa06SMiquel Raynal 			aligned_len++;
1382*a430fa06SMiquel Raynal 		if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
1383*a430fa06SMiquel Raynal 			aligned_len++;
1384*a430fa06SMiquel Raynal 
1385*a430fa06SMiquel Raynal 		chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1386*a430fa06SMiquel Raynal 					mtd->writesize + aligned_pos, -1);
1387*a430fa06SMiquel Raynal 		chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1388*a430fa06SMiquel Raynal 	}
1389*a430fa06SMiquel Raynal 
1390*a430fa06SMiquel Raynal 	for (i = 0; i < eccfrag_len; i++)
1391*a430fa06SMiquel Raynal 		chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
1392*a430fa06SMiquel Raynal 
1393*a430fa06SMiquel Raynal 	p = bufpoi + data_col_addr;
1394*a430fa06SMiquel Raynal 	for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1395*a430fa06SMiquel Raynal 		int stat;
1396*a430fa06SMiquel Raynal 
1397*a430fa06SMiquel Raynal 		stat = chip->ecc.correct(mtd, p,
1398*a430fa06SMiquel Raynal 			&chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1399*a430fa06SMiquel Raynal 		if (stat == -EBADMSG &&
1400*a430fa06SMiquel Raynal 		    (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1401*a430fa06SMiquel Raynal 			/* check for empty pages with bitflips */
1402*a430fa06SMiquel Raynal 			stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1403*a430fa06SMiquel Raynal 						&chip->buffers->ecccode[i],
1404*a430fa06SMiquel Raynal 						chip->ecc.bytes,
1405*a430fa06SMiquel Raynal 						NULL, 0,
1406*a430fa06SMiquel Raynal 						chip->ecc.strength);
1407*a430fa06SMiquel Raynal 		}
1408*a430fa06SMiquel Raynal 
1409*a430fa06SMiquel Raynal 		if (stat < 0) {
1410*a430fa06SMiquel Raynal 			mtd->ecc_stats.failed++;
1411*a430fa06SMiquel Raynal 		} else {
1412*a430fa06SMiquel Raynal 			mtd->ecc_stats.corrected += stat;
1413*a430fa06SMiquel Raynal 			max_bitflips = max_t(unsigned int, max_bitflips, stat);
1414*a430fa06SMiquel Raynal 		}
1415*a430fa06SMiquel Raynal 	}
1416*a430fa06SMiquel Raynal 	return max_bitflips;
1417*a430fa06SMiquel Raynal }
1418*a430fa06SMiquel Raynal 
1419*a430fa06SMiquel Raynal /**
1420*a430fa06SMiquel Raynal  * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1421*a430fa06SMiquel Raynal  * @mtd: mtd info structure
1422*a430fa06SMiquel Raynal  * @chip: nand chip info structure
1423*a430fa06SMiquel Raynal  * @buf: buffer to store read data
1424*a430fa06SMiquel Raynal  * @oob_required: caller requires OOB data read to chip->oob_poi
1425*a430fa06SMiquel Raynal  * @page: page number to read
1426*a430fa06SMiquel Raynal  *
1427*a430fa06SMiquel Raynal  * Not for syndrome calculating ECC controllers which need a special oob layout.
1428*a430fa06SMiquel Raynal  */
1429*a430fa06SMiquel Raynal static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1430*a430fa06SMiquel Raynal 				uint8_t *buf, int oob_required, int page)
1431*a430fa06SMiquel Raynal {
1432*a430fa06SMiquel Raynal 	int i, eccsize = chip->ecc.size;
1433*a430fa06SMiquel Raynal 	int eccbytes = chip->ecc.bytes;
1434*a430fa06SMiquel Raynal 	int eccsteps = chip->ecc.steps;
1435*a430fa06SMiquel Raynal 	uint8_t *p = buf;
1436*a430fa06SMiquel Raynal 	uint8_t *ecc_calc = chip->buffers->ecccalc;
1437*a430fa06SMiquel Raynal 	uint8_t *ecc_code = chip->buffers->ecccode;
1438*a430fa06SMiquel Raynal 	uint32_t *eccpos = chip->ecc.layout->eccpos;
1439*a430fa06SMiquel Raynal 	unsigned int max_bitflips = 0;
1440*a430fa06SMiquel Raynal 
1441*a430fa06SMiquel Raynal 	for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1442*a430fa06SMiquel Raynal 		chip->ecc.hwctl(mtd, NAND_ECC_READ);
1443*a430fa06SMiquel Raynal 		chip->read_buf(mtd, p, eccsize);
1444*a430fa06SMiquel Raynal 		chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1445*a430fa06SMiquel Raynal 	}
1446*a430fa06SMiquel Raynal 	chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1447*a430fa06SMiquel Raynal 
1448*a430fa06SMiquel Raynal 	for (i = 0; i < chip->ecc.total; i++)
1449*a430fa06SMiquel Raynal 		ecc_code[i] = chip->oob_poi[eccpos[i]];
1450*a430fa06SMiquel Raynal 
1451*a430fa06SMiquel Raynal 	eccsteps = chip->ecc.steps;
1452*a430fa06SMiquel Raynal 	p = buf;
1453*a430fa06SMiquel Raynal 
1454*a430fa06SMiquel Raynal 	for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1455*a430fa06SMiquel Raynal 		int stat;
1456*a430fa06SMiquel Raynal 
1457*a430fa06SMiquel Raynal 		stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1458*a430fa06SMiquel Raynal 		if (stat == -EBADMSG &&
1459*a430fa06SMiquel Raynal 		    (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1460*a430fa06SMiquel Raynal 			/* check for empty pages with bitflips */
1461*a430fa06SMiquel Raynal 			stat = nand_check_erased_ecc_chunk(p, eccsize,
1462*a430fa06SMiquel Raynal 						&ecc_code[i], eccbytes,
1463*a430fa06SMiquel Raynal 						NULL, 0,
1464*a430fa06SMiquel Raynal 						chip->ecc.strength);
1465*a430fa06SMiquel Raynal 		}
1466*a430fa06SMiquel Raynal 
1467*a430fa06SMiquel Raynal 		if (stat < 0) {
1468*a430fa06SMiquel Raynal 			mtd->ecc_stats.failed++;
1469*a430fa06SMiquel Raynal 		} else {
1470*a430fa06SMiquel Raynal 			mtd->ecc_stats.corrected += stat;
1471*a430fa06SMiquel Raynal 			max_bitflips = max_t(unsigned int, max_bitflips, stat);
1472*a430fa06SMiquel Raynal 		}
1473*a430fa06SMiquel Raynal 	}
1474*a430fa06SMiquel Raynal 	return max_bitflips;
1475*a430fa06SMiquel Raynal }
1476*a430fa06SMiquel Raynal 
1477*a430fa06SMiquel Raynal /**
1478*a430fa06SMiquel Raynal  * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1479*a430fa06SMiquel Raynal  * @mtd: mtd info structure
1480*a430fa06SMiquel Raynal  * @chip: nand chip info structure
1481*a430fa06SMiquel Raynal  * @buf: buffer to store read data
1482*a430fa06SMiquel Raynal  * @oob_required: caller requires OOB data read to chip->oob_poi
1483*a430fa06SMiquel Raynal  * @page: page number to read
1484*a430fa06SMiquel Raynal  *
1485*a430fa06SMiquel Raynal  * Hardware ECC for large page chips, require OOB to be read first. For this
1486*a430fa06SMiquel Raynal  * ECC mode, the write_page method is re-used from ECC_HW. These methods
1487*a430fa06SMiquel Raynal  * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1488*a430fa06SMiquel Raynal  * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1489*a430fa06SMiquel Raynal  * the data area, by overwriting the NAND manufacturer bad block markings.
1490*a430fa06SMiquel Raynal  */
1491*a430fa06SMiquel Raynal static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1492*a430fa06SMiquel Raynal 	struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
1493*a430fa06SMiquel Raynal {
1494*a430fa06SMiquel Raynal 	int i, eccsize = chip->ecc.size;
1495*a430fa06SMiquel Raynal 	int eccbytes = chip->ecc.bytes;
1496*a430fa06SMiquel Raynal 	int eccsteps = chip->ecc.steps;
1497*a430fa06SMiquel Raynal 	uint8_t *p = buf;
1498*a430fa06SMiquel Raynal 	uint8_t *ecc_code = chip->buffers->ecccode;
1499*a430fa06SMiquel Raynal 	uint32_t *eccpos = chip->ecc.layout->eccpos;
1500*a430fa06SMiquel Raynal 	uint8_t *ecc_calc = chip->buffers->ecccalc;
1501*a430fa06SMiquel Raynal 	unsigned int max_bitflips = 0;
1502*a430fa06SMiquel Raynal 
1503*a430fa06SMiquel Raynal 	/* Read the OOB area first */
1504*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1505*a430fa06SMiquel Raynal 	chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1506*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1507*a430fa06SMiquel Raynal 
1508*a430fa06SMiquel Raynal 	for (i = 0; i < chip->ecc.total; i++)
1509*a430fa06SMiquel Raynal 		ecc_code[i] = chip->oob_poi[eccpos[i]];
1510*a430fa06SMiquel Raynal 
1511*a430fa06SMiquel Raynal 	for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1512*a430fa06SMiquel Raynal 		int stat;
1513*a430fa06SMiquel Raynal 
1514*a430fa06SMiquel Raynal 		chip->ecc.hwctl(mtd, NAND_ECC_READ);
1515*a430fa06SMiquel Raynal 		chip->read_buf(mtd, p, eccsize);
1516*a430fa06SMiquel Raynal 		chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1517*a430fa06SMiquel Raynal 
1518*a430fa06SMiquel Raynal 		stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1519*a430fa06SMiquel Raynal 		if (stat == -EBADMSG &&
1520*a430fa06SMiquel Raynal 		    (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1521*a430fa06SMiquel Raynal 			/* check for empty pages with bitflips */
1522*a430fa06SMiquel Raynal 			stat = nand_check_erased_ecc_chunk(p, eccsize,
1523*a430fa06SMiquel Raynal 						&ecc_code[i], eccbytes,
1524*a430fa06SMiquel Raynal 						NULL, 0,
1525*a430fa06SMiquel Raynal 						chip->ecc.strength);
1526*a430fa06SMiquel Raynal 		}
1527*a430fa06SMiquel Raynal 
1528*a430fa06SMiquel Raynal 		if (stat < 0) {
1529*a430fa06SMiquel Raynal 			mtd->ecc_stats.failed++;
1530*a430fa06SMiquel Raynal 		} else {
1531*a430fa06SMiquel Raynal 			mtd->ecc_stats.corrected += stat;
1532*a430fa06SMiquel Raynal 			max_bitflips = max_t(unsigned int, max_bitflips, stat);
1533*a430fa06SMiquel Raynal 		}
1534*a430fa06SMiquel Raynal 	}
1535*a430fa06SMiquel Raynal 	return max_bitflips;
1536*a430fa06SMiquel Raynal }
1537*a430fa06SMiquel Raynal 
1538*a430fa06SMiquel Raynal /**
1539*a430fa06SMiquel Raynal  * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1540*a430fa06SMiquel Raynal  * @mtd: mtd info structure
1541*a430fa06SMiquel Raynal  * @chip: nand chip info structure
1542*a430fa06SMiquel Raynal  * @buf: buffer to store read data
1543*a430fa06SMiquel Raynal  * @oob_required: caller requires OOB data read to chip->oob_poi
1544*a430fa06SMiquel Raynal  * @page: page number to read
1545*a430fa06SMiquel Raynal  *
1546*a430fa06SMiquel Raynal  * The hw generator calculates the error syndrome automatically. Therefore we
1547*a430fa06SMiquel Raynal  * need a special oob layout and handling.
1548*a430fa06SMiquel Raynal  */
1549*a430fa06SMiquel Raynal static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1550*a430fa06SMiquel Raynal 				   uint8_t *buf, int oob_required, int page)
1551*a430fa06SMiquel Raynal {
1552*a430fa06SMiquel Raynal 	int i, eccsize = chip->ecc.size;
1553*a430fa06SMiquel Raynal 	int eccbytes = chip->ecc.bytes;
1554*a430fa06SMiquel Raynal 	int eccsteps = chip->ecc.steps;
1555*a430fa06SMiquel Raynal 	int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
1556*a430fa06SMiquel Raynal 	uint8_t *p = buf;
1557*a430fa06SMiquel Raynal 	uint8_t *oob = chip->oob_poi;
1558*a430fa06SMiquel Raynal 	unsigned int max_bitflips = 0;
1559*a430fa06SMiquel Raynal 
1560*a430fa06SMiquel Raynal 	for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1561*a430fa06SMiquel Raynal 		int stat;
1562*a430fa06SMiquel Raynal 
1563*a430fa06SMiquel Raynal 		chip->ecc.hwctl(mtd, NAND_ECC_READ);
1564*a430fa06SMiquel Raynal 		chip->read_buf(mtd, p, eccsize);
1565*a430fa06SMiquel Raynal 
1566*a430fa06SMiquel Raynal 		if (chip->ecc.prepad) {
1567*a430fa06SMiquel Raynal 			chip->read_buf(mtd, oob, chip->ecc.prepad);
1568*a430fa06SMiquel Raynal 			oob += chip->ecc.prepad;
1569*a430fa06SMiquel Raynal 		}
1570*a430fa06SMiquel Raynal 
1571*a430fa06SMiquel Raynal 		chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1572*a430fa06SMiquel Raynal 		chip->read_buf(mtd, oob, eccbytes);
1573*a430fa06SMiquel Raynal 		stat = chip->ecc.correct(mtd, p, oob, NULL);
1574*a430fa06SMiquel Raynal 
1575*a430fa06SMiquel Raynal 		oob += eccbytes;
1576*a430fa06SMiquel Raynal 
1577*a430fa06SMiquel Raynal 		if (chip->ecc.postpad) {
1578*a430fa06SMiquel Raynal 			chip->read_buf(mtd, oob, chip->ecc.postpad);
1579*a430fa06SMiquel Raynal 			oob += chip->ecc.postpad;
1580*a430fa06SMiquel Raynal 		}
1581*a430fa06SMiquel Raynal 
1582*a430fa06SMiquel Raynal 		if (stat == -EBADMSG &&
1583*a430fa06SMiquel Raynal 		    (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1584*a430fa06SMiquel Raynal 			/* check for empty pages with bitflips */
1585*a430fa06SMiquel Raynal 			stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1586*a430fa06SMiquel Raynal 							   oob - eccpadbytes,
1587*a430fa06SMiquel Raynal 							   eccpadbytes,
1588*a430fa06SMiquel Raynal 							   NULL, 0,
1589*a430fa06SMiquel Raynal 							   chip->ecc.strength);
1590*a430fa06SMiquel Raynal 		}
1591*a430fa06SMiquel Raynal 
1592*a430fa06SMiquel Raynal 		if (stat < 0) {
1593*a430fa06SMiquel Raynal 			mtd->ecc_stats.failed++;
1594*a430fa06SMiquel Raynal 		} else {
1595*a430fa06SMiquel Raynal 			mtd->ecc_stats.corrected += stat;
1596*a430fa06SMiquel Raynal 			max_bitflips = max_t(unsigned int, max_bitflips, stat);
1597*a430fa06SMiquel Raynal 		}
1598*a430fa06SMiquel Raynal 	}
1599*a430fa06SMiquel Raynal 
1600*a430fa06SMiquel Raynal 	/* Calculate remaining oob bytes */
1601*a430fa06SMiquel Raynal 	i = mtd->oobsize - (oob - chip->oob_poi);
1602*a430fa06SMiquel Raynal 	if (i)
1603*a430fa06SMiquel Raynal 		chip->read_buf(mtd, oob, i);
1604*a430fa06SMiquel Raynal 
1605*a430fa06SMiquel Raynal 	return max_bitflips;
1606*a430fa06SMiquel Raynal }
1607*a430fa06SMiquel Raynal 
1608*a430fa06SMiquel Raynal /**
1609*a430fa06SMiquel Raynal  * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1610*a430fa06SMiquel Raynal  * @chip: nand chip structure
1611*a430fa06SMiquel Raynal  * @oob: oob destination address
1612*a430fa06SMiquel Raynal  * @ops: oob ops structure
1613*a430fa06SMiquel Raynal  * @len: size of oob to transfer
1614*a430fa06SMiquel Raynal  */
1615*a430fa06SMiquel Raynal static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1616*a430fa06SMiquel Raynal 				  struct mtd_oob_ops *ops, size_t len)
1617*a430fa06SMiquel Raynal {
1618*a430fa06SMiquel Raynal 	switch (ops->mode) {
1619*a430fa06SMiquel Raynal 
1620*a430fa06SMiquel Raynal 	case MTD_OPS_PLACE_OOB:
1621*a430fa06SMiquel Raynal 	case MTD_OPS_RAW:
1622*a430fa06SMiquel Raynal 		memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1623*a430fa06SMiquel Raynal 		return oob + len;
1624*a430fa06SMiquel Raynal 
1625*a430fa06SMiquel Raynal 	case MTD_OPS_AUTO_OOB: {
1626*a430fa06SMiquel Raynal 		struct nand_oobfree *free = chip->ecc.layout->oobfree;
1627*a430fa06SMiquel Raynal 		uint32_t boffs = 0, roffs = ops->ooboffs;
1628*a430fa06SMiquel Raynal 		size_t bytes = 0;
1629*a430fa06SMiquel Raynal 
1630*a430fa06SMiquel Raynal 		for (; free->length && len; free++, len -= bytes) {
1631*a430fa06SMiquel Raynal 			/* Read request not from offset 0? */
1632*a430fa06SMiquel Raynal 			if (unlikely(roffs)) {
1633*a430fa06SMiquel Raynal 				if (roffs >= free->length) {
1634*a430fa06SMiquel Raynal 					roffs -= free->length;
1635*a430fa06SMiquel Raynal 					continue;
1636*a430fa06SMiquel Raynal 				}
1637*a430fa06SMiquel Raynal 				boffs = free->offset + roffs;
1638*a430fa06SMiquel Raynal 				bytes = min_t(size_t, len,
1639*a430fa06SMiquel Raynal 					      (free->length - roffs));
1640*a430fa06SMiquel Raynal 				roffs = 0;
1641*a430fa06SMiquel Raynal 			} else {
1642*a430fa06SMiquel Raynal 				bytes = min_t(size_t, len, free->length);
1643*a430fa06SMiquel Raynal 				boffs = free->offset;
1644*a430fa06SMiquel Raynal 			}
1645*a430fa06SMiquel Raynal 			memcpy(oob, chip->oob_poi + boffs, bytes);
1646*a430fa06SMiquel Raynal 			oob += bytes;
1647*a430fa06SMiquel Raynal 		}
1648*a430fa06SMiquel Raynal 		return oob;
1649*a430fa06SMiquel Raynal 	}
1650*a430fa06SMiquel Raynal 	default:
1651*a430fa06SMiquel Raynal 		BUG();
1652*a430fa06SMiquel Raynal 	}
1653*a430fa06SMiquel Raynal 	return NULL;
1654*a430fa06SMiquel Raynal }
1655*a430fa06SMiquel Raynal 
1656*a430fa06SMiquel Raynal /**
1657*a430fa06SMiquel Raynal  * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1658*a430fa06SMiquel Raynal  * @mtd: MTD device structure
1659*a430fa06SMiquel Raynal  * @retry_mode: the retry mode to use
1660*a430fa06SMiquel Raynal  *
1661*a430fa06SMiquel Raynal  * Some vendors supply a special command to shift the Vt threshold, to be used
1662*a430fa06SMiquel Raynal  * when there are too many bitflips in a page (i.e., ECC error). After setting
1663*a430fa06SMiquel Raynal  * a new threshold, the host should retry reading the page.
1664*a430fa06SMiquel Raynal  */
1665*a430fa06SMiquel Raynal static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1666*a430fa06SMiquel Raynal {
1667*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
1668*a430fa06SMiquel Raynal 
1669*a430fa06SMiquel Raynal 	pr_debug("setting READ RETRY mode %d\n", retry_mode);
1670*a430fa06SMiquel Raynal 
1671*a430fa06SMiquel Raynal 	if (retry_mode >= chip->read_retries)
1672*a430fa06SMiquel Raynal 		return -EINVAL;
1673*a430fa06SMiquel Raynal 
1674*a430fa06SMiquel Raynal 	if (!chip->setup_read_retry)
1675*a430fa06SMiquel Raynal 		return -EOPNOTSUPP;
1676*a430fa06SMiquel Raynal 
1677*a430fa06SMiquel Raynal 	return chip->setup_read_retry(mtd, retry_mode);
1678*a430fa06SMiquel Raynal }
1679*a430fa06SMiquel Raynal 
1680*a430fa06SMiquel Raynal /**
1681*a430fa06SMiquel Raynal  * nand_do_read_ops - [INTERN] Read data with ECC
1682*a430fa06SMiquel Raynal  * @mtd: MTD device structure
1683*a430fa06SMiquel Raynal  * @from: offset to read from
1684*a430fa06SMiquel Raynal  * @ops: oob ops structure
1685*a430fa06SMiquel Raynal  *
1686*a430fa06SMiquel Raynal  * Internal function. Called with chip held.
1687*a430fa06SMiquel Raynal  */
1688*a430fa06SMiquel Raynal static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1689*a430fa06SMiquel Raynal 			    struct mtd_oob_ops *ops)
1690*a430fa06SMiquel Raynal {
1691*a430fa06SMiquel Raynal 	int chipnr, page, realpage, col, bytes, aligned, oob_required;
1692*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
1693*a430fa06SMiquel Raynal 	int ret = 0;
1694*a430fa06SMiquel Raynal 	uint32_t readlen = ops->len;
1695*a430fa06SMiquel Raynal 	uint32_t oobreadlen = ops->ooblen;
1696*a430fa06SMiquel Raynal 	uint32_t max_oobsize = mtd_oobavail(mtd, ops);
1697*a430fa06SMiquel Raynal 
1698*a430fa06SMiquel Raynal 	uint8_t *bufpoi, *oob, *buf;
1699*a430fa06SMiquel Raynal 	int use_bufpoi;
1700*a430fa06SMiquel Raynal 	unsigned int max_bitflips = 0;
1701*a430fa06SMiquel Raynal 	int retry_mode = 0;
1702*a430fa06SMiquel Raynal 	bool ecc_fail = false;
1703*a430fa06SMiquel Raynal 
1704*a430fa06SMiquel Raynal 	chipnr = (int)(from >> chip->chip_shift);
1705*a430fa06SMiquel Raynal 	chip->select_chip(mtd, chipnr);
1706*a430fa06SMiquel Raynal 
1707*a430fa06SMiquel Raynal 	realpage = (int)(from >> chip->page_shift);
1708*a430fa06SMiquel Raynal 	page = realpage & chip->pagemask;
1709*a430fa06SMiquel Raynal 
1710*a430fa06SMiquel Raynal 	col = (int)(from & (mtd->writesize - 1));
1711*a430fa06SMiquel Raynal 
1712*a430fa06SMiquel Raynal 	buf = ops->datbuf;
1713*a430fa06SMiquel Raynal 	oob = ops->oobbuf;
1714*a430fa06SMiquel Raynal 	oob_required = oob ? 1 : 0;
1715*a430fa06SMiquel Raynal 
1716*a430fa06SMiquel Raynal 	while (1) {
1717*a430fa06SMiquel Raynal 		unsigned int ecc_failures = mtd->ecc_stats.failed;
1718*a430fa06SMiquel Raynal 
1719*a430fa06SMiquel Raynal 		WATCHDOG_RESET();
1720*a430fa06SMiquel Raynal 		bytes = min(mtd->writesize - col, readlen);
1721*a430fa06SMiquel Raynal 		aligned = (bytes == mtd->writesize);
1722*a430fa06SMiquel Raynal 
1723*a430fa06SMiquel Raynal 		if (!aligned)
1724*a430fa06SMiquel Raynal 			use_bufpoi = 1;
1725*a430fa06SMiquel Raynal 		else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1726*a430fa06SMiquel Raynal 			use_bufpoi = !IS_ALIGNED((unsigned long)buf,
1727*a430fa06SMiquel Raynal 						 chip->buf_align);
1728*a430fa06SMiquel Raynal 		else
1729*a430fa06SMiquel Raynal 			use_bufpoi = 0;
1730*a430fa06SMiquel Raynal 
1731*a430fa06SMiquel Raynal 		/* Is the current page in the buffer? */
1732*a430fa06SMiquel Raynal 		if (realpage != chip->pagebuf || oob) {
1733*a430fa06SMiquel Raynal 			bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1734*a430fa06SMiquel Raynal 
1735*a430fa06SMiquel Raynal 			if (use_bufpoi && aligned)
1736*a430fa06SMiquel Raynal 				pr_debug("%s: using read bounce buffer for buf@%p\n",
1737*a430fa06SMiquel Raynal 						 __func__, buf);
1738*a430fa06SMiquel Raynal 
1739*a430fa06SMiquel Raynal read_retry:
1740*a430fa06SMiquel Raynal 			if (nand_standard_page_accessors(&chip->ecc))
1741*a430fa06SMiquel Raynal 				chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1742*a430fa06SMiquel Raynal 
1743*a430fa06SMiquel Raynal 			/*
1744*a430fa06SMiquel Raynal 			 * Now read the page into the buffer.  Absent an error,
1745*a430fa06SMiquel Raynal 			 * the read methods return max bitflips per ecc step.
1746*a430fa06SMiquel Raynal 			 */
1747*a430fa06SMiquel Raynal 			if (unlikely(ops->mode == MTD_OPS_RAW))
1748*a430fa06SMiquel Raynal 				ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1749*a430fa06SMiquel Raynal 							      oob_required,
1750*a430fa06SMiquel Raynal 							      page);
1751*a430fa06SMiquel Raynal 			else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1752*a430fa06SMiquel Raynal 				 !oob)
1753*a430fa06SMiquel Raynal 				ret = chip->ecc.read_subpage(mtd, chip,
1754*a430fa06SMiquel Raynal 							col, bytes, bufpoi,
1755*a430fa06SMiquel Raynal 							page);
1756*a430fa06SMiquel Raynal 			else
1757*a430fa06SMiquel Raynal 				ret = chip->ecc.read_page(mtd, chip, bufpoi,
1758*a430fa06SMiquel Raynal 							  oob_required, page);
1759*a430fa06SMiquel Raynal 			if (ret < 0) {
1760*a430fa06SMiquel Raynal 				if (use_bufpoi)
1761*a430fa06SMiquel Raynal 					/* Invalidate page cache */
1762*a430fa06SMiquel Raynal 					chip->pagebuf = -1;
1763*a430fa06SMiquel Raynal 				break;
1764*a430fa06SMiquel Raynal 			}
1765*a430fa06SMiquel Raynal 
1766*a430fa06SMiquel Raynal 			max_bitflips = max_t(unsigned int, max_bitflips, ret);
1767*a430fa06SMiquel Raynal 
1768*a430fa06SMiquel Raynal 			/* Transfer not aligned data */
1769*a430fa06SMiquel Raynal 			if (use_bufpoi) {
1770*a430fa06SMiquel Raynal 				if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
1771*a430fa06SMiquel Raynal 				    !(mtd->ecc_stats.failed - ecc_failures) &&
1772*a430fa06SMiquel Raynal 				    (ops->mode != MTD_OPS_RAW)) {
1773*a430fa06SMiquel Raynal 					chip->pagebuf = realpage;
1774*a430fa06SMiquel Raynal 					chip->pagebuf_bitflips = ret;
1775*a430fa06SMiquel Raynal 				} else {
1776*a430fa06SMiquel Raynal 					/* Invalidate page cache */
1777*a430fa06SMiquel Raynal 					chip->pagebuf = -1;
1778*a430fa06SMiquel Raynal 				}
1779*a430fa06SMiquel Raynal 				memcpy(buf, chip->buffers->databuf + col, bytes);
1780*a430fa06SMiquel Raynal 			}
1781*a430fa06SMiquel Raynal 
1782*a430fa06SMiquel Raynal 			if (unlikely(oob)) {
1783*a430fa06SMiquel Raynal 				int toread = min(oobreadlen, max_oobsize);
1784*a430fa06SMiquel Raynal 
1785*a430fa06SMiquel Raynal 				if (toread) {
1786*a430fa06SMiquel Raynal 					oob = nand_transfer_oob(chip,
1787*a430fa06SMiquel Raynal 						oob, ops, toread);
1788*a430fa06SMiquel Raynal 					oobreadlen -= toread;
1789*a430fa06SMiquel Raynal 				}
1790*a430fa06SMiquel Raynal 			}
1791*a430fa06SMiquel Raynal 
1792*a430fa06SMiquel Raynal 			if (chip->options & NAND_NEED_READRDY) {
1793*a430fa06SMiquel Raynal 				/* Apply delay or wait for ready/busy pin */
1794*a430fa06SMiquel Raynal 				if (!chip->dev_ready)
1795*a430fa06SMiquel Raynal 					udelay(chip->chip_delay);
1796*a430fa06SMiquel Raynal 				else
1797*a430fa06SMiquel Raynal 					nand_wait_ready(mtd);
1798*a430fa06SMiquel Raynal 			}
1799*a430fa06SMiquel Raynal 
1800*a430fa06SMiquel Raynal 			if (mtd->ecc_stats.failed - ecc_failures) {
1801*a430fa06SMiquel Raynal 				if (retry_mode + 1 < chip->read_retries) {
1802*a430fa06SMiquel Raynal 					retry_mode++;
1803*a430fa06SMiquel Raynal 					ret = nand_setup_read_retry(mtd,
1804*a430fa06SMiquel Raynal 							retry_mode);
1805*a430fa06SMiquel Raynal 					if (ret < 0)
1806*a430fa06SMiquel Raynal 						break;
1807*a430fa06SMiquel Raynal 
1808*a430fa06SMiquel Raynal 					/* Reset failures; retry */
1809*a430fa06SMiquel Raynal 					mtd->ecc_stats.failed = ecc_failures;
1810*a430fa06SMiquel Raynal 					goto read_retry;
1811*a430fa06SMiquel Raynal 				} else {
1812*a430fa06SMiquel Raynal 					/* No more retry modes; real failure */
1813*a430fa06SMiquel Raynal 					ecc_fail = true;
1814*a430fa06SMiquel Raynal 				}
1815*a430fa06SMiquel Raynal 			}
1816*a430fa06SMiquel Raynal 
1817*a430fa06SMiquel Raynal 			buf += bytes;
1818*a430fa06SMiquel Raynal 		} else {
1819*a430fa06SMiquel Raynal 			memcpy(buf, chip->buffers->databuf + col, bytes);
1820*a430fa06SMiquel Raynal 			buf += bytes;
1821*a430fa06SMiquel Raynal 			max_bitflips = max_t(unsigned int, max_bitflips,
1822*a430fa06SMiquel Raynal 					     chip->pagebuf_bitflips);
1823*a430fa06SMiquel Raynal 		}
1824*a430fa06SMiquel Raynal 
1825*a430fa06SMiquel Raynal 		readlen -= bytes;
1826*a430fa06SMiquel Raynal 
1827*a430fa06SMiquel Raynal 		/* Reset to retry mode 0 */
1828*a430fa06SMiquel Raynal 		if (retry_mode) {
1829*a430fa06SMiquel Raynal 			ret = nand_setup_read_retry(mtd, 0);
1830*a430fa06SMiquel Raynal 			if (ret < 0)
1831*a430fa06SMiquel Raynal 				break;
1832*a430fa06SMiquel Raynal 			retry_mode = 0;
1833*a430fa06SMiquel Raynal 		}
1834*a430fa06SMiquel Raynal 
1835*a430fa06SMiquel Raynal 		if (!readlen)
1836*a430fa06SMiquel Raynal 			break;
1837*a430fa06SMiquel Raynal 
1838*a430fa06SMiquel Raynal 		/* For subsequent reads align to page boundary */
1839*a430fa06SMiquel Raynal 		col = 0;
1840*a430fa06SMiquel Raynal 		/* Increment page address */
1841*a430fa06SMiquel Raynal 		realpage++;
1842*a430fa06SMiquel Raynal 
1843*a430fa06SMiquel Raynal 		page = realpage & chip->pagemask;
1844*a430fa06SMiquel Raynal 		/* Check, if we cross a chip boundary */
1845*a430fa06SMiquel Raynal 		if (!page) {
1846*a430fa06SMiquel Raynal 			chipnr++;
1847*a430fa06SMiquel Raynal 			chip->select_chip(mtd, -1);
1848*a430fa06SMiquel Raynal 			chip->select_chip(mtd, chipnr);
1849*a430fa06SMiquel Raynal 		}
1850*a430fa06SMiquel Raynal 	}
1851*a430fa06SMiquel Raynal 	chip->select_chip(mtd, -1);
1852*a430fa06SMiquel Raynal 
1853*a430fa06SMiquel Raynal 	ops->retlen = ops->len - (size_t) readlen;
1854*a430fa06SMiquel Raynal 	if (oob)
1855*a430fa06SMiquel Raynal 		ops->oobretlen = ops->ooblen - oobreadlen;
1856*a430fa06SMiquel Raynal 
1857*a430fa06SMiquel Raynal 	if (ret < 0)
1858*a430fa06SMiquel Raynal 		return ret;
1859*a430fa06SMiquel Raynal 
1860*a430fa06SMiquel Raynal 	if (ecc_fail)
1861*a430fa06SMiquel Raynal 		return -EBADMSG;
1862*a430fa06SMiquel Raynal 
1863*a430fa06SMiquel Raynal 	return max_bitflips;
1864*a430fa06SMiquel Raynal }
1865*a430fa06SMiquel Raynal 
1866*a430fa06SMiquel Raynal /**
1867*a430fa06SMiquel Raynal  * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1868*a430fa06SMiquel Raynal  * @mtd: mtd info structure
1869*a430fa06SMiquel Raynal  * @chip: nand chip info structure
1870*a430fa06SMiquel Raynal  * @page: page number to read
1871*a430fa06SMiquel Raynal  */
1872*a430fa06SMiquel Raynal static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1873*a430fa06SMiquel Raynal 			     int page)
1874*a430fa06SMiquel Raynal {
1875*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1876*a430fa06SMiquel Raynal 	chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1877*a430fa06SMiquel Raynal 	return 0;
1878*a430fa06SMiquel Raynal }
1879*a430fa06SMiquel Raynal 
1880*a430fa06SMiquel Raynal /**
1881*a430fa06SMiquel Raynal  * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
1882*a430fa06SMiquel Raynal  *			    with syndromes
1883*a430fa06SMiquel Raynal  * @mtd: mtd info structure
1884*a430fa06SMiquel Raynal  * @chip: nand chip info structure
1885*a430fa06SMiquel Raynal  * @page: page number to read
1886*a430fa06SMiquel Raynal  */
1887*a430fa06SMiquel Raynal static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1888*a430fa06SMiquel Raynal 				  int page)
1889*a430fa06SMiquel Raynal {
1890*a430fa06SMiquel Raynal 	int length = mtd->oobsize;
1891*a430fa06SMiquel Raynal 	int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1892*a430fa06SMiquel Raynal 	int eccsize = chip->ecc.size;
1893*a430fa06SMiquel Raynal 	uint8_t *bufpoi = chip->oob_poi;
1894*a430fa06SMiquel Raynal 	int i, toread, sndrnd = 0, pos;
1895*a430fa06SMiquel Raynal 
1896*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1897*a430fa06SMiquel Raynal 	for (i = 0; i < chip->ecc.steps; i++) {
1898*a430fa06SMiquel Raynal 		if (sndrnd) {
1899*a430fa06SMiquel Raynal 			pos = eccsize + i * (eccsize + chunk);
1900*a430fa06SMiquel Raynal 			if (mtd->writesize > 512)
1901*a430fa06SMiquel Raynal 				chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1902*a430fa06SMiquel Raynal 			else
1903*a430fa06SMiquel Raynal 				chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1904*a430fa06SMiquel Raynal 		} else
1905*a430fa06SMiquel Raynal 			sndrnd = 1;
1906*a430fa06SMiquel Raynal 		toread = min_t(int, length, chunk);
1907*a430fa06SMiquel Raynal 		chip->read_buf(mtd, bufpoi, toread);
1908*a430fa06SMiquel Raynal 		bufpoi += toread;
1909*a430fa06SMiquel Raynal 		length -= toread;
1910*a430fa06SMiquel Raynal 	}
1911*a430fa06SMiquel Raynal 	if (length > 0)
1912*a430fa06SMiquel Raynal 		chip->read_buf(mtd, bufpoi, length);
1913*a430fa06SMiquel Raynal 
1914*a430fa06SMiquel Raynal 	return 0;
1915*a430fa06SMiquel Raynal }
1916*a430fa06SMiquel Raynal 
1917*a430fa06SMiquel Raynal /**
1918*a430fa06SMiquel Raynal  * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1919*a430fa06SMiquel Raynal  * @mtd: mtd info structure
1920*a430fa06SMiquel Raynal  * @chip: nand chip info structure
1921*a430fa06SMiquel Raynal  * @page: page number to write
1922*a430fa06SMiquel Raynal  */
1923*a430fa06SMiquel Raynal static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1924*a430fa06SMiquel Raynal 			      int page)
1925*a430fa06SMiquel Raynal {
1926*a430fa06SMiquel Raynal 	int status = 0;
1927*a430fa06SMiquel Raynal 	const uint8_t *buf = chip->oob_poi;
1928*a430fa06SMiquel Raynal 	int length = mtd->oobsize;
1929*a430fa06SMiquel Raynal 
1930*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1931*a430fa06SMiquel Raynal 	chip->write_buf(mtd, buf, length);
1932*a430fa06SMiquel Raynal 	/* Send command to program the OOB data */
1933*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1934*a430fa06SMiquel Raynal 
1935*a430fa06SMiquel Raynal 	status = chip->waitfunc(mtd, chip);
1936*a430fa06SMiquel Raynal 
1937*a430fa06SMiquel Raynal 	return status & NAND_STATUS_FAIL ? -EIO : 0;
1938*a430fa06SMiquel Raynal }
1939*a430fa06SMiquel Raynal 
1940*a430fa06SMiquel Raynal /**
1941*a430fa06SMiquel Raynal  * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1942*a430fa06SMiquel Raynal  *			     with syndrome - only for large page flash
1943*a430fa06SMiquel Raynal  * @mtd: mtd info structure
1944*a430fa06SMiquel Raynal  * @chip: nand chip info structure
1945*a430fa06SMiquel Raynal  * @page: page number to write
1946*a430fa06SMiquel Raynal  */
1947*a430fa06SMiquel Raynal static int nand_write_oob_syndrome(struct mtd_info *mtd,
1948*a430fa06SMiquel Raynal 				   struct nand_chip *chip, int page)
1949*a430fa06SMiquel Raynal {
1950*a430fa06SMiquel Raynal 	int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1951*a430fa06SMiquel Raynal 	int eccsize = chip->ecc.size, length = mtd->oobsize;
1952*a430fa06SMiquel Raynal 	int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1953*a430fa06SMiquel Raynal 	const uint8_t *bufpoi = chip->oob_poi;
1954*a430fa06SMiquel Raynal 
1955*a430fa06SMiquel Raynal 	/*
1956*a430fa06SMiquel Raynal 	 * data-ecc-data-ecc ... ecc-oob
1957*a430fa06SMiquel Raynal 	 * or
1958*a430fa06SMiquel Raynal 	 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1959*a430fa06SMiquel Raynal 	 */
1960*a430fa06SMiquel Raynal 	if (!chip->ecc.prepad && !chip->ecc.postpad) {
1961*a430fa06SMiquel Raynal 		pos = steps * (eccsize + chunk);
1962*a430fa06SMiquel Raynal 		steps = 0;
1963*a430fa06SMiquel Raynal 	} else
1964*a430fa06SMiquel Raynal 		pos = eccsize;
1965*a430fa06SMiquel Raynal 
1966*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1967*a430fa06SMiquel Raynal 	for (i = 0; i < steps; i++) {
1968*a430fa06SMiquel Raynal 		if (sndcmd) {
1969*a430fa06SMiquel Raynal 			if (mtd->writesize <= 512) {
1970*a430fa06SMiquel Raynal 				uint32_t fill = 0xFFFFFFFF;
1971*a430fa06SMiquel Raynal 
1972*a430fa06SMiquel Raynal 				len = eccsize;
1973*a430fa06SMiquel Raynal 				while (len > 0) {
1974*a430fa06SMiquel Raynal 					int num = min_t(int, len, 4);
1975*a430fa06SMiquel Raynal 					chip->write_buf(mtd, (uint8_t *)&fill,
1976*a430fa06SMiquel Raynal 							num);
1977*a430fa06SMiquel Raynal 					len -= num;
1978*a430fa06SMiquel Raynal 				}
1979*a430fa06SMiquel Raynal 			} else {
1980*a430fa06SMiquel Raynal 				pos = eccsize + i * (eccsize + chunk);
1981*a430fa06SMiquel Raynal 				chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1982*a430fa06SMiquel Raynal 			}
1983*a430fa06SMiquel Raynal 		} else
1984*a430fa06SMiquel Raynal 			sndcmd = 1;
1985*a430fa06SMiquel Raynal 		len = min_t(int, length, chunk);
1986*a430fa06SMiquel Raynal 		chip->write_buf(mtd, bufpoi, len);
1987*a430fa06SMiquel Raynal 		bufpoi += len;
1988*a430fa06SMiquel Raynal 		length -= len;
1989*a430fa06SMiquel Raynal 	}
1990*a430fa06SMiquel Raynal 	if (length > 0)
1991*a430fa06SMiquel Raynal 		chip->write_buf(mtd, bufpoi, length);
1992*a430fa06SMiquel Raynal 
1993*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1994*a430fa06SMiquel Raynal 	status = chip->waitfunc(mtd, chip);
1995*a430fa06SMiquel Raynal 
1996*a430fa06SMiquel Raynal 	return status & NAND_STATUS_FAIL ? -EIO : 0;
1997*a430fa06SMiquel Raynal }
1998*a430fa06SMiquel Raynal 
1999*a430fa06SMiquel Raynal /**
2000*a430fa06SMiquel Raynal  * nand_do_read_oob - [INTERN] NAND read out-of-band
2001*a430fa06SMiquel Raynal  * @mtd: MTD device structure
2002*a430fa06SMiquel Raynal  * @from: offset to read from
2003*a430fa06SMiquel Raynal  * @ops: oob operations description structure
2004*a430fa06SMiquel Raynal  *
2005*a430fa06SMiquel Raynal  * NAND read out-of-band data from the spare area.
2006*a430fa06SMiquel Raynal  */
2007*a430fa06SMiquel Raynal static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2008*a430fa06SMiquel Raynal 			    struct mtd_oob_ops *ops)
2009*a430fa06SMiquel Raynal {
2010*a430fa06SMiquel Raynal 	int page, realpage, chipnr;
2011*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
2012*a430fa06SMiquel Raynal 	struct mtd_ecc_stats stats;
2013*a430fa06SMiquel Raynal 	int readlen = ops->ooblen;
2014*a430fa06SMiquel Raynal 	int len;
2015*a430fa06SMiquel Raynal 	uint8_t *buf = ops->oobbuf;
2016*a430fa06SMiquel Raynal 	int ret = 0;
2017*a430fa06SMiquel Raynal 
2018*a430fa06SMiquel Raynal 	pr_debug("%s: from = 0x%08Lx, len = %i\n",
2019*a430fa06SMiquel Raynal 			__func__, (unsigned long long)from, readlen);
2020*a430fa06SMiquel Raynal 
2021*a430fa06SMiquel Raynal 	stats = mtd->ecc_stats;
2022*a430fa06SMiquel Raynal 
2023*a430fa06SMiquel Raynal 	len = mtd_oobavail(mtd, ops);
2024*a430fa06SMiquel Raynal 
2025*a430fa06SMiquel Raynal 	if (unlikely(ops->ooboffs >= len)) {
2026*a430fa06SMiquel Raynal 		pr_debug("%s: attempt to start read outside oob\n",
2027*a430fa06SMiquel Raynal 				__func__);
2028*a430fa06SMiquel Raynal 		return -EINVAL;
2029*a430fa06SMiquel Raynal 	}
2030*a430fa06SMiquel Raynal 
2031*a430fa06SMiquel Raynal 	/* Do not allow reads past end of device */
2032*a430fa06SMiquel Raynal 	if (unlikely(from >= mtd->size ||
2033*a430fa06SMiquel Raynal 		     ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2034*a430fa06SMiquel Raynal 					(from >> chip->page_shift)) * len)) {
2035*a430fa06SMiquel Raynal 		pr_debug("%s: attempt to read beyond end of device\n",
2036*a430fa06SMiquel Raynal 				__func__);
2037*a430fa06SMiquel Raynal 		return -EINVAL;
2038*a430fa06SMiquel Raynal 	}
2039*a430fa06SMiquel Raynal 
2040*a430fa06SMiquel Raynal 	chipnr = (int)(from >> chip->chip_shift);
2041*a430fa06SMiquel Raynal 	chip->select_chip(mtd, chipnr);
2042*a430fa06SMiquel Raynal 
2043*a430fa06SMiquel Raynal 	/* Shift to get page */
2044*a430fa06SMiquel Raynal 	realpage = (int)(from >> chip->page_shift);
2045*a430fa06SMiquel Raynal 	page = realpage & chip->pagemask;
2046*a430fa06SMiquel Raynal 
2047*a430fa06SMiquel Raynal 	while (1) {
2048*a430fa06SMiquel Raynal 		WATCHDOG_RESET();
2049*a430fa06SMiquel Raynal 
2050*a430fa06SMiquel Raynal 		if (ops->mode == MTD_OPS_RAW)
2051*a430fa06SMiquel Raynal 			ret = chip->ecc.read_oob_raw(mtd, chip, page);
2052*a430fa06SMiquel Raynal 		else
2053*a430fa06SMiquel Raynal 			ret = chip->ecc.read_oob(mtd, chip, page);
2054*a430fa06SMiquel Raynal 
2055*a430fa06SMiquel Raynal 		if (ret < 0)
2056*a430fa06SMiquel Raynal 			break;
2057*a430fa06SMiquel Raynal 
2058*a430fa06SMiquel Raynal 		len = min(len, readlen);
2059*a430fa06SMiquel Raynal 		buf = nand_transfer_oob(chip, buf, ops, len);
2060*a430fa06SMiquel Raynal 
2061*a430fa06SMiquel Raynal 		if (chip->options & NAND_NEED_READRDY) {
2062*a430fa06SMiquel Raynal 			/* Apply delay or wait for ready/busy pin */
2063*a430fa06SMiquel Raynal 			if (!chip->dev_ready)
2064*a430fa06SMiquel Raynal 				udelay(chip->chip_delay);
2065*a430fa06SMiquel Raynal 			else
2066*a430fa06SMiquel Raynal 				nand_wait_ready(mtd);
2067*a430fa06SMiquel Raynal 		}
2068*a430fa06SMiquel Raynal 
2069*a430fa06SMiquel Raynal 		readlen -= len;
2070*a430fa06SMiquel Raynal 		if (!readlen)
2071*a430fa06SMiquel Raynal 			break;
2072*a430fa06SMiquel Raynal 
2073*a430fa06SMiquel Raynal 		/* Increment page address */
2074*a430fa06SMiquel Raynal 		realpage++;
2075*a430fa06SMiquel Raynal 
2076*a430fa06SMiquel Raynal 		page = realpage & chip->pagemask;
2077*a430fa06SMiquel Raynal 		/* Check, if we cross a chip boundary */
2078*a430fa06SMiquel Raynal 		if (!page) {
2079*a430fa06SMiquel Raynal 			chipnr++;
2080*a430fa06SMiquel Raynal 			chip->select_chip(mtd, -1);
2081*a430fa06SMiquel Raynal 			chip->select_chip(mtd, chipnr);
2082*a430fa06SMiquel Raynal 		}
2083*a430fa06SMiquel Raynal 	}
2084*a430fa06SMiquel Raynal 	chip->select_chip(mtd, -1);
2085*a430fa06SMiquel Raynal 
2086*a430fa06SMiquel Raynal 	ops->oobretlen = ops->ooblen - readlen;
2087*a430fa06SMiquel Raynal 
2088*a430fa06SMiquel Raynal 	if (ret < 0)
2089*a430fa06SMiquel Raynal 		return ret;
2090*a430fa06SMiquel Raynal 
2091*a430fa06SMiquel Raynal 	if (mtd->ecc_stats.failed - stats.failed)
2092*a430fa06SMiquel Raynal 		return -EBADMSG;
2093*a430fa06SMiquel Raynal 
2094*a430fa06SMiquel Raynal 	return  mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
2095*a430fa06SMiquel Raynal }
2096*a430fa06SMiquel Raynal 
2097*a430fa06SMiquel Raynal /**
2098*a430fa06SMiquel Raynal  * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
2099*a430fa06SMiquel Raynal  * @mtd: MTD device structure
2100*a430fa06SMiquel Raynal  * @from: offset to read from
2101*a430fa06SMiquel Raynal  * @ops: oob operation description structure
2102*a430fa06SMiquel Raynal  *
2103*a430fa06SMiquel Raynal  * NAND read data and/or out-of-band data.
2104*a430fa06SMiquel Raynal  */
2105*a430fa06SMiquel Raynal static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2106*a430fa06SMiquel Raynal 			 struct mtd_oob_ops *ops)
2107*a430fa06SMiquel Raynal {
2108*a430fa06SMiquel Raynal 	int ret = -ENOTSUPP;
2109*a430fa06SMiquel Raynal 
2110*a430fa06SMiquel Raynal 	ops->retlen = 0;
2111*a430fa06SMiquel Raynal 
2112*a430fa06SMiquel Raynal 	/* Do not allow reads past end of device */
2113*a430fa06SMiquel Raynal 	if (ops->datbuf && (from + ops->len) > mtd->size) {
2114*a430fa06SMiquel Raynal 		pr_debug("%s: attempt to read beyond end of device\n",
2115*a430fa06SMiquel Raynal 				__func__);
2116*a430fa06SMiquel Raynal 		return -EINVAL;
2117*a430fa06SMiquel Raynal 	}
2118*a430fa06SMiquel Raynal 
2119*a430fa06SMiquel Raynal 	nand_get_device(mtd, FL_READING);
2120*a430fa06SMiquel Raynal 
2121*a430fa06SMiquel Raynal 	switch (ops->mode) {
2122*a430fa06SMiquel Raynal 	case MTD_OPS_PLACE_OOB:
2123*a430fa06SMiquel Raynal 	case MTD_OPS_AUTO_OOB:
2124*a430fa06SMiquel Raynal 	case MTD_OPS_RAW:
2125*a430fa06SMiquel Raynal 		break;
2126*a430fa06SMiquel Raynal 
2127*a430fa06SMiquel Raynal 	default:
2128*a430fa06SMiquel Raynal 		goto out;
2129*a430fa06SMiquel Raynal 	}
2130*a430fa06SMiquel Raynal 
2131*a430fa06SMiquel Raynal 	if (!ops->datbuf)
2132*a430fa06SMiquel Raynal 		ret = nand_do_read_oob(mtd, from, ops);
2133*a430fa06SMiquel Raynal 	else
2134*a430fa06SMiquel Raynal 		ret = nand_do_read_ops(mtd, from, ops);
2135*a430fa06SMiquel Raynal 
2136*a430fa06SMiquel Raynal out:
2137*a430fa06SMiquel Raynal 	nand_release_device(mtd);
2138*a430fa06SMiquel Raynal 	return ret;
2139*a430fa06SMiquel Raynal }
2140*a430fa06SMiquel Raynal 
2141*a430fa06SMiquel Raynal 
2142*a430fa06SMiquel Raynal /**
2143*a430fa06SMiquel Raynal  * nand_write_page_raw - [INTERN] raw page write function
2144*a430fa06SMiquel Raynal  * @mtd: mtd info structure
2145*a430fa06SMiquel Raynal  * @chip: nand chip info structure
2146*a430fa06SMiquel Raynal  * @buf: data buffer
2147*a430fa06SMiquel Raynal  * @oob_required: must write chip->oob_poi to OOB
2148*a430fa06SMiquel Raynal  * @page: page number to write
2149*a430fa06SMiquel Raynal  *
2150*a430fa06SMiquel Raynal  * Not for syndrome calculating ECC controllers, which use a special oob layout.
2151*a430fa06SMiquel Raynal  */
2152*a430fa06SMiquel Raynal static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2153*a430fa06SMiquel Raynal 			       const uint8_t *buf, int oob_required, int page)
2154*a430fa06SMiquel Raynal {
2155*a430fa06SMiquel Raynal 	chip->write_buf(mtd, buf, mtd->writesize);
2156*a430fa06SMiquel Raynal 	if (oob_required)
2157*a430fa06SMiquel Raynal 		chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2158*a430fa06SMiquel Raynal 
2159*a430fa06SMiquel Raynal 	return 0;
2160*a430fa06SMiquel Raynal }
2161*a430fa06SMiquel Raynal 
2162*a430fa06SMiquel Raynal /**
2163*a430fa06SMiquel Raynal  * nand_write_page_raw_syndrome - [INTERN] raw page write function
2164*a430fa06SMiquel Raynal  * @mtd: mtd info structure
2165*a430fa06SMiquel Raynal  * @chip: nand chip info structure
2166*a430fa06SMiquel Raynal  * @buf: data buffer
2167*a430fa06SMiquel Raynal  * @oob_required: must write chip->oob_poi to OOB
2168*a430fa06SMiquel Raynal  * @page: page number to write
2169*a430fa06SMiquel Raynal  *
2170*a430fa06SMiquel Raynal  * We need a special oob layout and handling even when ECC isn't checked.
2171*a430fa06SMiquel Raynal  */
2172*a430fa06SMiquel Raynal static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
2173*a430fa06SMiquel Raynal 					struct nand_chip *chip,
2174*a430fa06SMiquel Raynal 					const uint8_t *buf, int oob_required,
2175*a430fa06SMiquel Raynal 					int page)
2176*a430fa06SMiquel Raynal {
2177*a430fa06SMiquel Raynal 	int eccsize = chip->ecc.size;
2178*a430fa06SMiquel Raynal 	int eccbytes = chip->ecc.bytes;
2179*a430fa06SMiquel Raynal 	uint8_t *oob = chip->oob_poi;
2180*a430fa06SMiquel Raynal 	int steps, size;
2181*a430fa06SMiquel Raynal 
2182*a430fa06SMiquel Raynal 	for (steps = chip->ecc.steps; steps > 0; steps--) {
2183*a430fa06SMiquel Raynal 		chip->write_buf(mtd, buf, eccsize);
2184*a430fa06SMiquel Raynal 		buf += eccsize;
2185*a430fa06SMiquel Raynal 
2186*a430fa06SMiquel Raynal 		if (chip->ecc.prepad) {
2187*a430fa06SMiquel Raynal 			chip->write_buf(mtd, oob, chip->ecc.prepad);
2188*a430fa06SMiquel Raynal 			oob += chip->ecc.prepad;
2189*a430fa06SMiquel Raynal 		}
2190*a430fa06SMiquel Raynal 
2191*a430fa06SMiquel Raynal 		chip->write_buf(mtd, oob, eccbytes);
2192*a430fa06SMiquel Raynal 		oob += eccbytes;
2193*a430fa06SMiquel Raynal 
2194*a430fa06SMiquel Raynal 		if (chip->ecc.postpad) {
2195*a430fa06SMiquel Raynal 			chip->write_buf(mtd, oob, chip->ecc.postpad);
2196*a430fa06SMiquel Raynal 			oob += chip->ecc.postpad;
2197*a430fa06SMiquel Raynal 		}
2198*a430fa06SMiquel Raynal 	}
2199*a430fa06SMiquel Raynal 
2200*a430fa06SMiquel Raynal 	size = mtd->oobsize - (oob - chip->oob_poi);
2201*a430fa06SMiquel Raynal 	if (size)
2202*a430fa06SMiquel Raynal 		chip->write_buf(mtd, oob, size);
2203*a430fa06SMiquel Raynal 
2204*a430fa06SMiquel Raynal 	return 0;
2205*a430fa06SMiquel Raynal }
2206*a430fa06SMiquel Raynal /**
2207*a430fa06SMiquel Raynal  * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2208*a430fa06SMiquel Raynal  * @mtd: mtd info structure
2209*a430fa06SMiquel Raynal  * @chip: nand chip info structure
2210*a430fa06SMiquel Raynal  * @buf: data buffer
2211*a430fa06SMiquel Raynal  * @oob_required: must write chip->oob_poi to OOB
2212*a430fa06SMiquel Raynal  * @page: page number to write
2213*a430fa06SMiquel Raynal  */
2214*a430fa06SMiquel Raynal static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
2215*a430fa06SMiquel Raynal 				 const uint8_t *buf, int oob_required,
2216*a430fa06SMiquel Raynal 				 int page)
2217*a430fa06SMiquel Raynal {
2218*a430fa06SMiquel Raynal 	int i, eccsize = chip->ecc.size;
2219*a430fa06SMiquel Raynal 	int eccbytes = chip->ecc.bytes;
2220*a430fa06SMiquel Raynal 	int eccsteps = chip->ecc.steps;
2221*a430fa06SMiquel Raynal 	uint8_t *ecc_calc = chip->buffers->ecccalc;
2222*a430fa06SMiquel Raynal 	const uint8_t *p = buf;
2223*a430fa06SMiquel Raynal 	uint32_t *eccpos = chip->ecc.layout->eccpos;
2224*a430fa06SMiquel Raynal 
2225*a430fa06SMiquel Raynal 	/* Software ECC calculation */
2226*a430fa06SMiquel Raynal 	for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2227*a430fa06SMiquel Raynal 		chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2228*a430fa06SMiquel Raynal 
2229*a430fa06SMiquel Raynal 	for (i = 0; i < chip->ecc.total; i++)
2230*a430fa06SMiquel Raynal 		chip->oob_poi[eccpos[i]] = ecc_calc[i];
2231*a430fa06SMiquel Raynal 
2232*a430fa06SMiquel Raynal 	return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
2233*a430fa06SMiquel Raynal }
2234*a430fa06SMiquel Raynal 
2235*a430fa06SMiquel Raynal /**
2236*a430fa06SMiquel Raynal  * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2237*a430fa06SMiquel Raynal  * @mtd: mtd info structure
2238*a430fa06SMiquel Raynal  * @chip: nand chip info structure
2239*a430fa06SMiquel Raynal  * @buf: data buffer
2240*a430fa06SMiquel Raynal  * @oob_required: must write chip->oob_poi to OOB
2241*a430fa06SMiquel Raynal  * @page: page number to write
2242*a430fa06SMiquel Raynal  */
2243*a430fa06SMiquel Raynal static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
2244*a430fa06SMiquel Raynal 				  const uint8_t *buf, int oob_required,
2245*a430fa06SMiquel Raynal 				  int page)
2246*a430fa06SMiquel Raynal {
2247*a430fa06SMiquel Raynal 	int i, eccsize = chip->ecc.size;
2248*a430fa06SMiquel Raynal 	int eccbytes = chip->ecc.bytes;
2249*a430fa06SMiquel Raynal 	int eccsteps = chip->ecc.steps;
2250*a430fa06SMiquel Raynal 	uint8_t *ecc_calc = chip->buffers->ecccalc;
2251*a430fa06SMiquel Raynal 	const uint8_t *p = buf;
2252*a430fa06SMiquel Raynal 	uint32_t *eccpos = chip->ecc.layout->eccpos;
2253*a430fa06SMiquel Raynal 
2254*a430fa06SMiquel Raynal 	for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2255*a430fa06SMiquel Raynal 		chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2256*a430fa06SMiquel Raynal 		chip->write_buf(mtd, p, eccsize);
2257*a430fa06SMiquel Raynal 		chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2258*a430fa06SMiquel Raynal 	}
2259*a430fa06SMiquel Raynal 
2260*a430fa06SMiquel Raynal 	for (i = 0; i < chip->ecc.total; i++)
2261*a430fa06SMiquel Raynal 		chip->oob_poi[eccpos[i]] = ecc_calc[i];
2262*a430fa06SMiquel Raynal 
2263*a430fa06SMiquel Raynal 	chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2264*a430fa06SMiquel Raynal 
2265*a430fa06SMiquel Raynal 	return 0;
2266*a430fa06SMiquel Raynal }
2267*a430fa06SMiquel Raynal 
2268*a430fa06SMiquel Raynal 
2269*a430fa06SMiquel Raynal /**
2270*a430fa06SMiquel Raynal  * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
2271*a430fa06SMiquel Raynal  * @mtd:	mtd info structure
2272*a430fa06SMiquel Raynal  * @chip:	nand chip info structure
2273*a430fa06SMiquel Raynal  * @offset:	column address of subpage within the page
2274*a430fa06SMiquel Raynal  * @data_len:	data length
2275*a430fa06SMiquel Raynal  * @buf:	data buffer
2276*a430fa06SMiquel Raynal  * @oob_required: must write chip->oob_poi to OOB
2277*a430fa06SMiquel Raynal  * @page: page number to write
2278*a430fa06SMiquel Raynal  */
2279*a430fa06SMiquel Raynal static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2280*a430fa06SMiquel Raynal 				struct nand_chip *chip, uint32_t offset,
2281*a430fa06SMiquel Raynal 				uint32_t data_len, const uint8_t *buf,
2282*a430fa06SMiquel Raynal 				int oob_required, int page)
2283*a430fa06SMiquel Raynal {
2284*a430fa06SMiquel Raynal 	uint8_t *oob_buf  = chip->oob_poi;
2285*a430fa06SMiquel Raynal 	uint8_t *ecc_calc = chip->buffers->ecccalc;
2286*a430fa06SMiquel Raynal 	int ecc_size      = chip->ecc.size;
2287*a430fa06SMiquel Raynal 	int ecc_bytes     = chip->ecc.bytes;
2288*a430fa06SMiquel Raynal 	int ecc_steps     = chip->ecc.steps;
2289*a430fa06SMiquel Raynal 	uint32_t *eccpos  = chip->ecc.layout->eccpos;
2290*a430fa06SMiquel Raynal 	uint32_t start_step = offset / ecc_size;
2291*a430fa06SMiquel Raynal 	uint32_t end_step   = (offset + data_len - 1) / ecc_size;
2292*a430fa06SMiquel Raynal 	int oob_bytes       = mtd->oobsize / ecc_steps;
2293*a430fa06SMiquel Raynal 	int step, i;
2294*a430fa06SMiquel Raynal 
2295*a430fa06SMiquel Raynal 	for (step = 0; step < ecc_steps; step++) {
2296*a430fa06SMiquel Raynal 		/* configure controller for WRITE access */
2297*a430fa06SMiquel Raynal 		chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2298*a430fa06SMiquel Raynal 
2299*a430fa06SMiquel Raynal 		/* write data (untouched subpages already masked by 0xFF) */
2300*a430fa06SMiquel Raynal 		chip->write_buf(mtd, buf, ecc_size);
2301*a430fa06SMiquel Raynal 
2302*a430fa06SMiquel Raynal 		/* mask ECC of un-touched subpages by padding 0xFF */
2303*a430fa06SMiquel Raynal 		if ((step < start_step) || (step > end_step))
2304*a430fa06SMiquel Raynal 			memset(ecc_calc, 0xff, ecc_bytes);
2305*a430fa06SMiquel Raynal 		else
2306*a430fa06SMiquel Raynal 			chip->ecc.calculate(mtd, buf, ecc_calc);
2307*a430fa06SMiquel Raynal 
2308*a430fa06SMiquel Raynal 		/* mask OOB of un-touched subpages by padding 0xFF */
2309*a430fa06SMiquel Raynal 		/* if oob_required, preserve OOB metadata of written subpage */
2310*a430fa06SMiquel Raynal 		if (!oob_required || (step < start_step) || (step > end_step))
2311*a430fa06SMiquel Raynal 			memset(oob_buf, 0xff, oob_bytes);
2312*a430fa06SMiquel Raynal 
2313*a430fa06SMiquel Raynal 		buf += ecc_size;
2314*a430fa06SMiquel Raynal 		ecc_calc += ecc_bytes;
2315*a430fa06SMiquel Raynal 		oob_buf  += oob_bytes;
2316*a430fa06SMiquel Raynal 	}
2317*a430fa06SMiquel Raynal 
2318*a430fa06SMiquel Raynal 	/* copy calculated ECC for whole page to chip->buffer->oob */
2319*a430fa06SMiquel Raynal 	/* this include masked-value(0xFF) for unwritten subpages */
2320*a430fa06SMiquel Raynal 	ecc_calc = chip->buffers->ecccalc;
2321*a430fa06SMiquel Raynal 	for (i = 0; i < chip->ecc.total; i++)
2322*a430fa06SMiquel Raynal 		chip->oob_poi[eccpos[i]] = ecc_calc[i];
2323*a430fa06SMiquel Raynal 
2324*a430fa06SMiquel Raynal 	/* write OOB buffer to NAND device */
2325*a430fa06SMiquel Raynal 	chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2326*a430fa06SMiquel Raynal 
2327*a430fa06SMiquel Raynal 	return 0;
2328*a430fa06SMiquel Raynal }
2329*a430fa06SMiquel Raynal 
2330*a430fa06SMiquel Raynal 
2331*a430fa06SMiquel Raynal /**
2332*a430fa06SMiquel Raynal  * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2333*a430fa06SMiquel Raynal  * @mtd: mtd info structure
2334*a430fa06SMiquel Raynal  * @chip: nand chip info structure
2335*a430fa06SMiquel Raynal  * @buf: data buffer
2336*a430fa06SMiquel Raynal  * @oob_required: must write chip->oob_poi to OOB
2337*a430fa06SMiquel Raynal  * @page: page number to write
2338*a430fa06SMiquel Raynal  *
2339*a430fa06SMiquel Raynal  * The hw generator calculates the error syndrome automatically. Therefore we
2340*a430fa06SMiquel Raynal  * need a special oob layout and handling.
2341*a430fa06SMiquel Raynal  */
2342*a430fa06SMiquel Raynal static int nand_write_page_syndrome(struct mtd_info *mtd,
2343*a430fa06SMiquel Raynal 				    struct nand_chip *chip,
2344*a430fa06SMiquel Raynal 				    const uint8_t *buf, int oob_required,
2345*a430fa06SMiquel Raynal 				    int page)
2346*a430fa06SMiquel Raynal {
2347*a430fa06SMiquel Raynal 	int i, eccsize = chip->ecc.size;
2348*a430fa06SMiquel Raynal 	int eccbytes = chip->ecc.bytes;
2349*a430fa06SMiquel Raynal 	int eccsteps = chip->ecc.steps;
2350*a430fa06SMiquel Raynal 	const uint8_t *p = buf;
2351*a430fa06SMiquel Raynal 	uint8_t *oob = chip->oob_poi;
2352*a430fa06SMiquel Raynal 
2353*a430fa06SMiquel Raynal 	for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2354*a430fa06SMiquel Raynal 
2355*a430fa06SMiquel Raynal 		chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2356*a430fa06SMiquel Raynal 		chip->write_buf(mtd, p, eccsize);
2357*a430fa06SMiquel Raynal 
2358*a430fa06SMiquel Raynal 		if (chip->ecc.prepad) {
2359*a430fa06SMiquel Raynal 			chip->write_buf(mtd, oob, chip->ecc.prepad);
2360*a430fa06SMiquel Raynal 			oob += chip->ecc.prepad;
2361*a430fa06SMiquel Raynal 		}
2362*a430fa06SMiquel Raynal 
2363*a430fa06SMiquel Raynal 		chip->ecc.calculate(mtd, p, oob);
2364*a430fa06SMiquel Raynal 		chip->write_buf(mtd, oob, eccbytes);
2365*a430fa06SMiquel Raynal 		oob += eccbytes;
2366*a430fa06SMiquel Raynal 
2367*a430fa06SMiquel Raynal 		if (chip->ecc.postpad) {
2368*a430fa06SMiquel Raynal 			chip->write_buf(mtd, oob, chip->ecc.postpad);
2369*a430fa06SMiquel Raynal 			oob += chip->ecc.postpad;
2370*a430fa06SMiquel Raynal 		}
2371*a430fa06SMiquel Raynal 	}
2372*a430fa06SMiquel Raynal 
2373*a430fa06SMiquel Raynal 	/* Calculate remaining oob bytes */
2374*a430fa06SMiquel Raynal 	i = mtd->oobsize - (oob - chip->oob_poi);
2375*a430fa06SMiquel Raynal 	if (i)
2376*a430fa06SMiquel Raynal 		chip->write_buf(mtd, oob, i);
2377*a430fa06SMiquel Raynal 
2378*a430fa06SMiquel Raynal 	return 0;
2379*a430fa06SMiquel Raynal }
2380*a430fa06SMiquel Raynal 
2381*a430fa06SMiquel Raynal /**
2382*a430fa06SMiquel Raynal  * nand_write_page - [REPLACEABLE] write one page
2383*a430fa06SMiquel Raynal  * @mtd: MTD device structure
2384*a430fa06SMiquel Raynal  * @chip: NAND chip descriptor
2385*a430fa06SMiquel Raynal  * @offset: address offset within the page
2386*a430fa06SMiquel Raynal  * @data_len: length of actual data to be written
2387*a430fa06SMiquel Raynal  * @buf: the data to write
2388*a430fa06SMiquel Raynal  * @oob_required: must write chip->oob_poi to OOB
2389*a430fa06SMiquel Raynal  * @page: page number to write
2390*a430fa06SMiquel Raynal  * @raw: use _raw version of write_page
2391*a430fa06SMiquel Raynal  */
2392*a430fa06SMiquel Raynal static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
2393*a430fa06SMiquel Raynal 		uint32_t offset, int data_len, const uint8_t *buf,
2394*a430fa06SMiquel Raynal 		int oob_required, int page, int raw)
2395*a430fa06SMiquel Raynal {
2396*a430fa06SMiquel Raynal 	int status, subpage;
2397*a430fa06SMiquel Raynal 
2398*a430fa06SMiquel Raynal 	if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2399*a430fa06SMiquel Raynal 		chip->ecc.write_subpage)
2400*a430fa06SMiquel Raynal 		subpage = offset || (data_len < mtd->writesize);
2401*a430fa06SMiquel Raynal 	else
2402*a430fa06SMiquel Raynal 		subpage = 0;
2403*a430fa06SMiquel Raynal 
2404*a430fa06SMiquel Raynal 	if (nand_standard_page_accessors(&chip->ecc))
2405*a430fa06SMiquel Raynal 		chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2406*a430fa06SMiquel Raynal 
2407*a430fa06SMiquel Raynal 	if (unlikely(raw))
2408*a430fa06SMiquel Raynal 		status = chip->ecc.write_page_raw(mtd, chip, buf,
2409*a430fa06SMiquel Raynal 						  oob_required, page);
2410*a430fa06SMiquel Raynal 	else if (subpage)
2411*a430fa06SMiquel Raynal 		status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2412*a430fa06SMiquel Raynal 						 buf, oob_required, page);
2413*a430fa06SMiquel Raynal 	else
2414*a430fa06SMiquel Raynal 		status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2415*a430fa06SMiquel Raynal 					      page);
2416*a430fa06SMiquel Raynal 
2417*a430fa06SMiquel Raynal 	if (status < 0)
2418*a430fa06SMiquel Raynal 		return status;
2419*a430fa06SMiquel Raynal 
2420*a430fa06SMiquel Raynal 	if (nand_standard_page_accessors(&chip->ecc)) {
2421*a430fa06SMiquel Raynal 		chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2422*a430fa06SMiquel Raynal 
2423*a430fa06SMiquel Raynal 		status = chip->waitfunc(mtd, chip);
2424*a430fa06SMiquel Raynal 		if (status & NAND_STATUS_FAIL)
2425*a430fa06SMiquel Raynal 			return -EIO;
2426*a430fa06SMiquel Raynal 	}
2427*a430fa06SMiquel Raynal 
2428*a430fa06SMiquel Raynal 	return 0;
2429*a430fa06SMiquel Raynal }
2430*a430fa06SMiquel Raynal 
2431*a430fa06SMiquel Raynal /**
2432*a430fa06SMiquel Raynal  * nand_fill_oob - [INTERN] Transfer client buffer to oob
2433*a430fa06SMiquel Raynal  * @mtd: MTD device structure
2434*a430fa06SMiquel Raynal  * @oob: oob data buffer
2435*a430fa06SMiquel Raynal  * @len: oob data write length
2436*a430fa06SMiquel Raynal  * @ops: oob ops structure
2437*a430fa06SMiquel Raynal  */
2438*a430fa06SMiquel Raynal static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2439*a430fa06SMiquel Raynal 			      struct mtd_oob_ops *ops)
2440*a430fa06SMiquel Raynal {
2441*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
2442*a430fa06SMiquel Raynal 
2443*a430fa06SMiquel Raynal 	/*
2444*a430fa06SMiquel Raynal 	 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2445*a430fa06SMiquel Raynal 	 * data from a previous OOB read.
2446*a430fa06SMiquel Raynal 	 */
2447*a430fa06SMiquel Raynal 	memset(chip->oob_poi, 0xff, mtd->oobsize);
2448*a430fa06SMiquel Raynal 
2449*a430fa06SMiquel Raynal 	switch (ops->mode) {
2450*a430fa06SMiquel Raynal 
2451*a430fa06SMiquel Raynal 	case MTD_OPS_PLACE_OOB:
2452*a430fa06SMiquel Raynal 	case MTD_OPS_RAW:
2453*a430fa06SMiquel Raynal 		memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2454*a430fa06SMiquel Raynal 		return oob + len;
2455*a430fa06SMiquel Raynal 
2456*a430fa06SMiquel Raynal 	case MTD_OPS_AUTO_OOB: {
2457*a430fa06SMiquel Raynal 		struct nand_oobfree *free = chip->ecc.layout->oobfree;
2458*a430fa06SMiquel Raynal 		uint32_t boffs = 0, woffs = ops->ooboffs;
2459*a430fa06SMiquel Raynal 		size_t bytes = 0;
2460*a430fa06SMiquel Raynal 
2461*a430fa06SMiquel Raynal 		for (; free->length && len; free++, len -= bytes) {
2462*a430fa06SMiquel Raynal 			/* Write request not from offset 0? */
2463*a430fa06SMiquel Raynal 			if (unlikely(woffs)) {
2464*a430fa06SMiquel Raynal 				if (woffs >= free->length) {
2465*a430fa06SMiquel Raynal 					woffs -= free->length;
2466*a430fa06SMiquel Raynal 					continue;
2467*a430fa06SMiquel Raynal 				}
2468*a430fa06SMiquel Raynal 				boffs = free->offset + woffs;
2469*a430fa06SMiquel Raynal 				bytes = min_t(size_t, len,
2470*a430fa06SMiquel Raynal 					      (free->length - woffs));
2471*a430fa06SMiquel Raynal 				woffs = 0;
2472*a430fa06SMiquel Raynal 			} else {
2473*a430fa06SMiquel Raynal 				bytes = min_t(size_t, len, free->length);
2474*a430fa06SMiquel Raynal 				boffs = free->offset;
2475*a430fa06SMiquel Raynal 			}
2476*a430fa06SMiquel Raynal 			memcpy(chip->oob_poi + boffs, oob, bytes);
2477*a430fa06SMiquel Raynal 			oob += bytes;
2478*a430fa06SMiquel Raynal 		}
2479*a430fa06SMiquel Raynal 		return oob;
2480*a430fa06SMiquel Raynal 	}
2481*a430fa06SMiquel Raynal 	default:
2482*a430fa06SMiquel Raynal 		BUG();
2483*a430fa06SMiquel Raynal 	}
2484*a430fa06SMiquel Raynal 	return NULL;
2485*a430fa06SMiquel Raynal }
2486*a430fa06SMiquel Raynal 
2487*a430fa06SMiquel Raynal #define NOTALIGNED(x)	((x & (chip->subpagesize - 1)) != 0)
2488*a430fa06SMiquel Raynal 
2489*a430fa06SMiquel Raynal /**
2490*a430fa06SMiquel Raynal  * nand_do_write_ops - [INTERN] NAND write with ECC
2491*a430fa06SMiquel Raynal  * @mtd: MTD device structure
2492*a430fa06SMiquel Raynal  * @to: offset to write to
2493*a430fa06SMiquel Raynal  * @ops: oob operations description structure
2494*a430fa06SMiquel Raynal  *
2495*a430fa06SMiquel Raynal  * NAND write with ECC.
2496*a430fa06SMiquel Raynal  */
2497*a430fa06SMiquel Raynal static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2498*a430fa06SMiquel Raynal 			     struct mtd_oob_ops *ops)
2499*a430fa06SMiquel Raynal {
2500*a430fa06SMiquel Raynal 	int chipnr, realpage, page, column;
2501*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
2502*a430fa06SMiquel Raynal 	uint32_t writelen = ops->len;
2503*a430fa06SMiquel Raynal 
2504*a430fa06SMiquel Raynal 	uint32_t oobwritelen = ops->ooblen;
2505*a430fa06SMiquel Raynal 	uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
2506*a430fa06SMiquel Raynal 
2507*a430fa06SMiquel Raynal 	uint8_t *oob = ops->oobbuf;
2508*a430fa06SMiquel Raynal 	uint8_t *buf = ops->datbuf;
2509*a430fa06SMiquel Raynal 	int ret;
2510*a430fa06SMiquel Raynal 	int oob_required = oob ? 1 : 0;
2511*a430fa06SMiquel Raynal 
2512*a430fa06SMiquel Raynal 	ops->retlen = 0;
2513*a430fa06SMiquel Raynal 	if (!writelen)
2514*a430fa06SMiquel Raynal 		return 0;
2515*a430fa06SMiquel Raynal 
2516*a430fa06SMiquel Raynal 	/* Reject writes, which are not page aligned */
2517*a430fa06SMiquel Raynal 	if (NOTALIGNED(to)) {
2518*a430fa06SMiquel Raynal 		pr_notice("%s: attempt to write non page aligned data\n",
2519*a430fa06SMiquel Raynal 			   __func__);
2520*a430fa06SMiquel Raynal 		return -EINVAL;
2521*a430fa06SMiquel Raynal 	}
2522*a430fa06SMiquel Raynal 
2523*a430fa06SMiquel Raynal 	column = to & (mtd->writesize - 1);
2524*a430fa06SMiquel Raynal 
2525*a430fa06SMiquel Raynal 	chipnr = (int)(to >> chip->chip_shift);
2526*a430fa06SMiquel Raynal 	chip->select_chip(mtd, chipnr);
2527*a430fa06SMiquel Raynal 
2528*a430fa06SMiquel Raynal 	/* Check, if it is write protected */
2529*a430fa06SMiquel Raynal 	if (nand_check_wp(mtd)) {
2530*a430fa06SMiquel Raynal 		ret = -EIO;
2531*a430fa06SMiquel Raynal 		goto err_out;
2532*a430fa06SMiquel Raynal 	}
2533*a430fa06SMiquel Raynal 
2534*a430fa06SMiquel Raynal 	realpage = (int)(to >> chip->page_shift);
2535*a430fa06SMiquel Raynal 	page = realpage & chip->pagemask;
2536*a430fa06SMiquel Raynal 
2537*a430fa06SMiquel Raynal 	/* Invalidate the page cache, when we write to the cached page */
2538*a430fa06SMiquel Raynal 	if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2539*a430fa06SMiquel Raynal 	    ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
2540*a430fa06SMiquel Raynal 		chip->pagebuf = -1;
2541*a430fa06SMiquel Raynal 
2542*a430fa06SMiquel Raynal 	/* Don't allow multipage oob writes with offset */
2543*a430fa06SMiquel Raynal 	if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2544*a430fa06SMiquel Raynal 		ret = -EINVAL;
2545*a430fa06SMiquel Raynal 		goto err_out;
2546*a430fa06SMiquel Raynal 	}
2547*a430fa06SMiquel Raynal 
2548*a430fa06SMiquel Raynal 	while (1) {
2549*a430fa06SMiquel Raynal 		int bytes = mtd->writesize;
2550*a430fa06SMiquel Raynal 		uint8_t *wbuf = buf;
2551*a430fa06SMiquel Raynal 		int use_bufpoi;
2552*a430fa06SMiquel Raynal 		int part_pagewr = (column || writelen < mtd->writesize);
2553*a430fa06SMiquel Raynal 
2554*a430fa06SMiquel Raynal 		if (part_pagewr)
2555*a430fa06SMiquel Raynal 			use_bufpoi = 1;
2556*a430fa06SMiquel Raynal 		else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2557*a430fa06SMiquel Raynal 			use_bufpoi = !IS_ALIGNED((unsigned long)buf,
2558*a430fa06SMiquel Raynal 						 chip->buf_align);
2559*a430fa06SMiquel Raynal 		else
2560*a430fa06SMiquel Raynal 			use_bufpoi = 0;
2561*a430fa06SMiquel Raynal 
2562*a430fa06SMiquel Raynal 		WATCHDOG_RESET();
2563*a430fa06SMiquel Raynal 		/* Partial page write?, or need to use bounce buffer */
2564*a430fa06SMiquel Raynal 		if (use_bufpoi) {
2565*a430fa06SMiquel Raynal 			pr_debug("%s: using write bounce buffer for buf@%p\n",
2566*a430fa06SMiquel Raynal 					 __func__, buf);
2567*a430fa06SMiquel Raynal 			if (part_pagewr)
2568*a430fa06SMiquel Raynal 				bytes = min_t(int, bytes - column, writelen);
2569*a430fa06SMiquel Raynal 			chip->pagebuf = -1;
2570*a430fa06SMiquel Raynal 			memset(chip->buffers->databuf, 0xff, mtd->writesize);
2571*a430fa06SMiquel Raynal 			memcpy(&chip->buffers->databuf[column], buf, bytes);
2572*a430fa06SMiquel Raynal 			wbuf = chip->buffers->databuf;
2573*a430fa06SMiquel Raynal 		}
2574*a430fa06SMiquel Raynal 
2575*a430fa06SMiquel Raynal 		if (unlikely(oob)) {
2576*a430fa06SMiquel Raynal 			size_t len = min(oobwritelen, oobmaxlen);
2577*a430fa06SMiquel Raynal 			oob = nand_fill_oob(mtd, oob, len, ops);
2578*a430fa06SMiquel Raynal 			oobwritelen -= len;
2579*a430fa06SMiquel Raynal 		} else {
2580*a430fa06SMiquel Raynal 			/* We still need to erase leftover OOB data */
2581*a430fa06SMiquel Raynal 			memset(chip->oob_poi, 0xff, mtd->oobsize);
2582*a430fa06SMiquel Raynal 		}
2583*a430fa06SMiquel Raynal 		ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2584*a430fa06SMiquel Raynal 					oob_required, page,
2585*a430fa06SMiquel Raynal 					(ops->mode == MTD_OPS_RAW));
2586*a430fa06SMiquel Raynal 		if (ret)
2587*a430fa06SMiquel Raynal 			break;
2588*a430fa06SMiquel Raynal 
2589*a430fa06SMiquel Raynal 		writelen -= bytes;
2590*a430fa06SMiquel Raynal 		if (!writelen)
2591*a430fa06SMiquel Raynal 			break;
2592*a430fa06SMiquel Raynal 
2593*a430fa06SMiquel Raynal 		column = 0;
2594*a430fa06SMiquel Raynal 		buf += bytes;
2595*a430fa06SMiquel Raynal 		realpage++;
2596*a430fa06SMiquel Raynal 
2597*a430fa06SMiquel Raynal 		page = realpage & chip->pagemask;
2598*a430fa06SMiquel Raynal 		/* Check, if we cross a chip boundary */
2599*a430fa06SMiquel Raynal 		if (!page) {
2600*a430fa06SMiquel Raynal 			chipnr++;
2601*a430fa06SMiquel Raynal 			chip->select_chip(mtd, -1);
2602*a430fa06SMiquel Raynal 			chip->select_chip(mtd, chipnr);
2603*a430fa06SMiquel Raynal 		}
2604*a430fa06SMiquel Raynal 	}
2605*a430fa06SMiquel Raynal 
2606*a430fa06SMiquel Raynal 	ops->retlen = ops->len - writelen;
2607*a430fa06SMiquel Raynal 	if (unlikely(oob))
2608*a430fa06SMiquel Raynal 		ops->oobretlen = ops->ooblen;
2609*a430fa06SMiquel Raynal 
2610*a430fa06SMiquel Raynal err_out:
2611*a430fa06SMiquel Raynal 	chip->select_chip(mtd, -1);
2612*a430fa06SMiquel Raynal 	return ret;
2613*a430fa06SMiquel Raynal }
2614*a430fa06SMiquel Raynal 
2615*a430fa06SMiquel Raynal /**
2616*a430fa06SMiquel Raynal  * panic_nand_write - [MTD Interface] NAND write with ECC
2617*a430fa06SMiquel Raynal  * @mtd: MTD device structure
2618*a430fa06SMiquel Raynal  * @to: offset to write to
2619*a430fa06SMiquel Raynal  * @len: number of bytes to write
2620*a430fa06SMiquel Raynal  * @retlen: pointer to variable to store the number of written bytes
2621*a430fa06SMiquel Raynal  * @buf: the data to write
2622*a430fa06SMiquel Raynal  *
2623*a430fa06SMiquel Raynal  * NAND write with ECC. Used when performing writes in interrupt context, this
2624*a430fa06SMiquel Raynal  * may for example be called by mtdoops when writing an oops while in panic.
2625*a430fa06SMiquel Raynal  */
2626*a430fa06SMiquel Raynal static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2627*a430fa06SMiquel Raynal 			    size_t *retlen, const uint8_t *buf)
2628*a430fa06SMiquel Raynal {
2629*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
2630*a430fa06SMiquel Raynal 	struct mtd_oob_ops ops;
2631*a430fa06SMiquel Raynal 	int ret;
2632*a430fa06SMiquel Raynal 
2633*a430fa06SMiquel Raynal 	/* Wait for the device to get ready */
2634*a430fa06SMiquel Raynal 	panic_nand_wait(mtd, chip, 400);
2635*a430fa06SMiquel Raynal 
2636*a430fa06SMiquel Raynal 	/* Grab the device */
2637*a430fa06SMiquel Raynal 	panic_nand_get_device(chip, mtd, FL_WRITING);
2638*a430fa06SMiquel Raynal 
2639*a430fa06SMiquel Raynal 	memset(&ops, 0, sizeof(ops));
2640*a430fa06SMiquel Raynal 	ops.len = len;
2641*a430fa06SMiquel Raynal 	ops.datbuf = (uint8_t *)buf;
2642*a430fa06SMiquel Raynal 	ops.mode = MTD_OPS_PLACE_OOB;
2643*a430fa06SMiquel Raynal 
2644*a430fa06SMiquel Raynal 	ret = nand_do_write_ops(mtd, to, &ops);
2645*a430fa06SMiquel Raynal 
2646*a430fa06SMiquel Raynal 	*retlen = ops.retlen;
2647*a430fa06SMiquel Raynal 	return ret;
2648*a430fa06SMiquel Raynal }
2649*a430fa06SMiquel Raynal 
2650*a430fa06SMiquel Raynal /**
2651*a430fa06SMiquel Raynal  * nand_do_write_oob - [MTD Interface] NAND write out-of-band
2652*a430fa06SMiquel Raynal  * @mtd: MTD device structure
2653*a430fa06SMiquel Raynal  * @to: offset to write to
2654*a430fa06SMiquel Raynal  * @ops: oob operation description structure
2655*a430fa06SMiquel Raynal  *
2656*a430fa06SMiquel Raynal  * NAND write out-of-band.
2657*a430fa06SMiquel Raynal  */
2658*a430fa06SMiquel Raynal static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2659*a430fa06SMiquel Raynal 			     struct mtd_oob_ops *ops)
2660*a430fa06SMiquel Raynal {
2661*a430fa06SMiquel Raynal 	int chipnr, page, status, len;
2662*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
2663*a430fa06SMiquel Raynal 
2664*a430fa06SMiquel Raynal 	pr_debug("%s: to = 0x%08x, len = %i\n",
2665*a430fa06SMiquel Raynal 			 __func__, (unsigned int)to, (int)ops->ooblen);
2666*a430fa06SMiquel Raynal 
2667*a430fa06SMiquel Raynal 	len = mtd_oobavail(mtd, ops);
2668*a430fa06SMiquel Raynal 
2669*a430fa06SMiquel Raynal 	/* Do not allow write past end of page */
2670*a430fa06SMiquel Raynal 	if ((ops->ooboffs + ops->ooblen) > len) {
2671*a430fa06SMiquel Raynal 		pr_debug("%s: attempt to write past end of page\n",
2672*a430fa06SMiquel Raynal 				__func__);
2673*a430fa06SMiquel Raynal 		return -EINVAL;
2674*a430fa06SMiquel Raynal 	}
2675*a430fa06SMiquel Raynal 
2676*a430fa06SMiquel Raynal 	if (unlikely(ops->ooboffs >= len)) {
2677*a430fa06SMiquel Raynal 		pr_debug("%s: attempt to start write outside oob\n",
2678*a430fa06SMiquel Raynal 				__func__);
2679*a430fa06SMiquel Raynal 		return -EINVAL;
2680*a430fa06SMiquel Raynal 	}
2681*a430fa06SMiquel Raynal 
2682*a430fa06SMiquel Raynal 	/* Do not allow write past end of device */
2683*a430fa06SMiquel Raynal 	if (unlikely(to >= mtd->size ||
2684*a430fa06SMiquel Raynal 		     ops->ooboffs + ops->ooblen >
2685*a430fa06SMiquel Raynal 			((mtd->size >> chip->page_shift) -
2686*a430fa06SMiquel Raynal 			 (to >> chip->page_shift)) * len)) {
2687*a430fa06SMiquel Raynal 		pr_debug("%s: attempt to write beyond end of device\n",
2688*a430fa06SMiquel Raynal 				__func__);
2689*a430fa06SMiquel Raynal 		return -EINVAL;
2690*a430fa06SMiquel Raynal 	}
2691*a430fa06SMiquel Raynal 
2692*a430fa06SMiquel Raynal 	chipnr = (int)(to >> chip->chip_shift);
2693*a430fa06SMiquel Raynal 
2694*a430fa06SMiquel Raynal 	/*
2695*a430fa06SMiquel Raynal 	 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2696*a430fa06SMiquel Raynal 	 * of my DiskOnChip 2000 test units) will clear the whole data page too
2697*a430fa06SMiquel Raynal 	 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2698*a430fa06SMiquel Raynal 	 * it in the doc2000 driver in August 1999.  dwmw2.
2699*a430fa06SMiquel Raynal 	 */
2700*a430fa06SMiquel Raynal 	nand_reset(chip, chipnr);
2701*a430fa06SMiquel Raynal 
2702*a430fa06SMiquel Raynal 	chip->select_chip(mtd, chipnr);
2703*a430fa06SMiquel Raynal 
2704*a430fa06SMiquel Raynal 	/* Shift to get page */
2705*a430fa06SMiquel Raynal 	page = (int)(to >> chip->page_shift);
2706*a430fa06SMiquel Raynal 
2707*a430fa06SMiquel Raynal 	/* Check, if it is write protected */
2708*a430fa06SMiquel Raynal 	if (nand_check_wp(mtd)) {
2709*a430fa06SMiquel Raynal 		chip->select_chip(mtd, -1);
2710*a430fa06SMiquel Raynal 		return -EROFS;
2711*a430fa06SMiquel Raynal 	}
2712*a430fa06SMiquel Raynal 
2713*a430fa06SMiquel Raynal 	/* Invalidate the page cache, if we write to the cached page */
2714*a430fa06SMiquel Raynal 	if (page == chip->pagebuf)
2715*a430fa06SMiquel Raynal 		chip->pagebuf = -1;
2716*a430fa06SMiquel Raynal 
2717*a430fa06SMiquel Raynal 	nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2718*a430fa06SMiquel Raynal 
2719*a430fa06SMiquel Raynal 	if (ops->mode == MTD_OPS_RAW)
2720*a430fa06SMiquel Raynal 		status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2721*a430fa06SMiquel Raynal 	else
2722*a430fa06SMiquel Raynal 		status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2723*a430fa06SMiquel Raynal 
2724*a430fa06SMiquel Raynal 	chip->select_chip(mtd, -1);
2725*a430fa06SMiquel Raynal 
2726*a430fa06SMiquel Raynal 	if (status)
2727*a430fa06SMiquel Raynal 		return status;
2728*a430fa06SMiquel Raynal 
2729*a430fa06SMiquel Raynal 	ops->oobretlen = ops->ooblen;
2730*a430fa06SMiquel Raynal 
2731*a430fa06SMiquel Raynal 	return 0;
2732*a430fa06SMiquel Raynal }
2733*a430fa06SMiquel Raynal 
2734*a430fa06SMiquel Raynal /**
2735*a430fa06SMiquel Raynal  * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2736*a430fa06SMiquel Raynal  * @mtd: MTD device structure
2737*a430fa06SMiquel Raynal  * @to: offset to write to
2738*a430fa06SMiquel Raynal  * @ops: oob operation description structure
2739*a430fa06SMiquel Raynal  */
2740*a430fa06SMiquel Raynal static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2741*a430fa06SMiquel Raynal 			  struct mtd_oob_ops *ops)
2742*a430fa06SMiquel Raynal {
2743*a430fa06SMiquel Raynal 	int ret = -ENOTSUPP;
2744*a430fa06SMiquel Raynal 
2745*a430fa06SMiquel Raynal 	ops->retlen = 0;
2746*a430fa06SMiquel Raynal 
2747*a430fa06SMiquel Raynal 	/* Do not allow writes past end of device */
2748*a430fa06SMiquel Raynal 	if (ops->datbuf && (to + ops->len) > mtd->size) {
2749*a430fa06SMiquel Raynal 		pr_debug("%s: attempt to write beyond end of device\n",
2750*a430fa06SMiquel Raynal 				__func__);
2751*a430fa06SMiquel Raynal 		return -EINVAL;
2752*a430fa06SMiquel Raynal 	}
2753*a430fa06SMiquel Raynal 
2754*a430fa06SMiquel Raynal 	nand_get_device(mtd, FL_WRITING);
2755*a430fa06SMiquel Raynal 
2756*a430fa06SMiquel Raynal 	switch (ops->mode) {
2757*a430fa06SMiquel Raynal 	case MTD_OPS_PLACE_OOB:
2758*a430fa06SMiquel Raynal 	case MTD_OPS_AUTO_OOB:
2759*a430fa06SMiquel Raynal 	case MTD_OPS_RAW:
2760*a430fa06SMiquel Raynal 		break;
2761*a430fa06SMiquel Raynal 
2762*a430fa06SMiquel Raynal 	default:
2763*a430fa06SMiquel Raynal 		goto out;
2764*a430fa06SMiquel Raynal 	}
2765*a430fa06SMiquel Raynal 
2766*a430fa06SMiquel Raynal 	if (!ops->datbuf)
2767*a430fa06SMiquel Raynal 		ret = nand_do_write_oob(mtd, to, ops);
2768*a430fa06SMiquel Raynal 	else
2769*a430fa06SMiquel Raynal 		ret = nand_do_write_ops(mtd, to, ops);
2770*a430fa06SMiquel Raynal 
2771*a430fa06SMiquel Raynal out:
2772*a430fa06SMiquel Raynal 	nand_release_device(mtd);
2773*a430fa06SMiquel Raynal 	return ret;
2774*a430fa06SMiquel Raynal }
2775*a430fa06SMiquel Raynal 
2776*a430fa06SMiquel Raynal /**
2777*a430fa06SMiquel Raynal  * single_erase - [GENERIC] NAND standard block erase command function
2778*a430fa06SMiquel Raynal  * @mtd: MTD device structure
2779*a430fa06SMiquel Raynal  * @page: the page address of the block which will be erased
2780*a430fa06SMiquel Raynal  *
2781*a430fa06SMiquel Raynal  * Standard erase command for NAND chips. Returns NAND status.
2782*a430fa06SMiquel Raynal  */
2783*a430fa06SMiquel Raynal static int single_erase(struct mtd_info *mtd, int page)
2784*a430fa06SMiquel Raynal {
2785*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
2786*a430fa06SMiquel Raynal 	/* Send commands to erase a block */
2787*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2788*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2789*a430fa06SMiquel Raynal 
2790*a430fa06SMiquel Raynal 	return chip->waitfunc(mtd, chip);
2791*a430fa06SMiquel Raynal }
2792*a430fa06SMiquel Raynal 
2793*a430fa06SMiquel Raynal /**
2794*a430fa06SMiquel Raynal  * nand_erase - [MTD Interface] erase block(s)
2795*a430fa06SMiquel Raynal  * @mtd: MTD device structure
2796*a430fa06SMiquel Raynal  * @instr: erase instruction
2797*a430fa06SMiquel Raynal  *
2798*a430fa06SMiquel Raynal  * Erase one ore more blocks.
2799*a430fa06SMiquel Raynal  */
2800*a430fa06SMiquel Raynal static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
2801*a430fa06SMiquel Raynal {
2802*a430fa06SMiquel Raynal 	return nand_erase_nand(mtd, instr, 0);
2803*a430fa06SMiquel Raynal }
2804*a430fa06SMiquel Raynal 
2805*a430fa06SMiquel Raynal /**
2806*a430fa06SMiquel Raynal  * nand_erase_nand - [INTERN] erase block(s)
2807*a430fa06SMiquel Raynal  * @mtd: MTD device structure
2808*a430fa06SMiquel Raynal  * @instr: erase instruction
2809*a430fa06SMiquel Raynal  * @allowbbt: allow erasing the bbt area
2810*a430fa06SMiquel Raynal  *
2811*a430fa06SMiquel Raynal  * Erase one ore more blocks.
2812*a430fa06SMiquel Raynal  */
2813*a430fa06SMiquel Raynal int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2814*a430fa06SMiquel Raynal 		    int allowbbt)
2815*a430fa06SMiquel Raynal {
2816*a430fa06SMiquel Raynal 	int page, status, pages_per_block, ret, chipnr;
2817*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
2818*a430fa06SMiquel Raynal 	loff_t len;
2819*a430fa06SMiquel Raynal 
2820*a430fa06SMiquel Raynal 	pr_debug("%s: start = 0x%012llx, len = %llu\n",
2821*a430fa06SMiquel Raynal 			__func__, (unsigned long long)instr->addr,
2822*a430fa06SMiquel Raynal 			(unsigned long long)instr->len);
2823*a430fa06SMiquel Raynal 
2824*a430fa06SMiquel Raynal 	if (check_offs_len(mtd, instr->addr, instr->len))
2825*a430fa06SMiquel Raynal 		return -EINVAL;
2826*a430fa06SMiquel Raynal 
2827*a430fa06SMiquel Raynal 	/* Grab the lock and see if the device is available */
2828*a430fa06SMiquel Raynal 	nand_get_device(mtd, FL_ERASING);
2829*a430fa06SMiquel Raynal 
2830*a430fa06SMiquel Raynal 	/* Shift to get first page */
2831*a430fa06SMiquel Raynal 	page = (int)(instr->addr >> chip->page_shift);
2832*a430fa06SMiquel Raynal 	chipnr = (int)(instr->addr >> chip->chip_shift);
2833*a430fa06SMiquel Raynal 
2834*a430fa06SMiquel Raynal 	/* Calculate pages in each block */
2835*a430fa06SMiquel Raynal 	pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
2836*a430fa06SMiquel Raynal 
2837*a430fa06SMiquel Raynal 	/* Select the NAND device */
2838*a430fa06SMiquel Raynal 	chip->select_chip(mtd, chipnr);
2839*a430fa06SMiquel Raynal 
2840*a430fa06SMiquel Raynal 	/* Check, if it is write protected */
2841*a430fa06SMiquel Raynal 	if (nand_check_wp(mtd)) {
2842*a430fa06SMiquel Raynal 		pr_debug("%s: device is write protected!\n",
2843*a430fa06SMiquel Raynal 				__func__);
2844*a430fa06SMiquel Raynal 		instr->state = MTD_ERASE_FAILED;
2845*a430fa06SMiquel Raynal 		goto erase_exit;
2846*a430fa06SMiquel Raynal 	}
2847*a430fa06SMiquel Raynal 
2848*a430fa06SMiquel Raynal 	/* Loop through the pages */
2849*a430fa06SMiquel Raynal 	len = instr->len;
2850*a430fa06SMiquel Raynal 
2851*a430fa06SMiquel Raynal 	instr->state = MTD_ERASING;
2852*a430fa06SMiquel Raynal 
2853*a430fa06SMiquel Raynal 	while (len) {
2854*a430fa06SMiquel Raynal 		WATCHDOG_RESET();
2855*a430fa06SMiquel Raynal 
2856*a430fa06SMiquel Raynal 		/* Check if we have a bad block, we do not erase bad blocks! */
2857*a430fa06SMiquel Raynal 		if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
2858*a430fa06SMiquel Raynal 					chip->page_shift, allowbbt)) {
2859*a430fa06SMiquel Raynal 			pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2860*a430fa06SMiquel Raynal 				    __func__, page);
2861*a430fa06SMiquel Raynal 			instr->state = MTD_ERASE_FAILED;
2862*a430fa06SMiquel Raynal 			goto erase_exit;
2863*a430fa06SMiquel Raynal 		}
2864*a430fa06SMiquel Raynal 
2865*a430fa06SMiquel Raynal 		/*
2866*a430fa06SMiquel Raynal 		 * Invalidate the page cache, if we erase the block which
2867*a430fa06SMiquel Raynal 		 * contains the current cached page.
2868*a430fa06SMiquel Raynal 		 */
2869*a430fa06SMiquel Raynal 		if (page <= chip->pagebuf && chip->pagebuf <
2870*a430fa06SMiquel Raynal 		    (page + pages_per_block))
2871*a430fa06SMiquel Raynal 			chip->pagebuf = -1;
2872*a430fa06SMiquel Raynal 
2873*a430fa06SMiquel Raynal 		status = chip->erase(mtd, page & chip->pagemask);
2874*a430fa06SMiquel Raynal 
2875*a430fa06SMiquel Raynal 		/* See if block erase succeeded */
2876*a430fa06SMiquel Raynal 		if (status & NAND_STATUS_FAIL) {
2877*a430fa06SMiquel Raynal 			pr_debug("%s: failed erase, page 0x%08x\n",
2878*a430fa06SMiquel Raynal 					__func__, page);
2879*a430fa06SMiquel Raynal 			instr->state = MTD_ERASE_FAILED;
2880*a430fa06SMiquel Raynal 			instr->fail_addr =
2881*a430fa06SMiquel Raynal 				((loff_t)page << chip->page_shift);
2882*a430fa06SMiquel Raynal 			goto erase_exit;
2883*a430fa06SMiquel Raynal 		}
2884*a430fa06SMiquel Raynal 
2885*a430fa06SMiquel Raynal 		/* Increment page address and decrement length */
2886*a430fa06SMiquel Raynal 		len -= (1ULL << chip->phys_erase_shift);
2887*a430fa06SMiquel Raynal 		page += pages_per_block;
2888*a430fa06SMiquel Raynal 
2889*a430fa06SMiquel Raynal 		/* Check, if we cross a chip boundary */
2890*a430fa06SMiquel Raynal 		if (len && !(page & chip->pagemask)) {
2891*a430fa06SMiquel Raynal 			chipnr++;
2892*a430fa06SMiquel Raynal 			chip->select_chip(mtd, -1);
2893*a430fa06SMiquel Raynal 			chip->select_chip(mtd, chipnr);
2894*a430fa06SMiquel Raynal 		}
2895*a430fa06SMiquel Raynal 	}
2896*a430fa06SMiquel Raynal 	instr->state = MTD_ERASE_DONE;
2897*a430fa06SMiquel Raynal 
2898*a430fa06SMiquel Raynal erase_exit:
2899*a430fa06SMiquel Raynal 
2900*a430fa06SMiquel Raynal 	ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2901*a430fa06SMiquel Raynal 
2902*a430fa06SMiquel Raynal 	/* Deselect and wake up anyone waiting on the device */
2903*a430fa06SMiquel Raynal 	chip->select_chip(mtd, -1);
2904*a430fa06SMiquel Raynal 	nand_release_device(mtd);
2905*a430fa06SMiquel Raynal 
2906*a430fa06SMiquel Raynal 	/* Do call back function */
2907*a430fa06SMiquel Raynal 	if (!ret)
2908*a430fa06SMiquel Raynal 		mtd_erase_callback(instr);
2909*a430fa06SMiquel Raynal 
2910*a430fa06SMiquel Raynal 	/* Return more or less happy */
2911*a430fa06SMiquel Raynal 	return ret;
2912*a430fa06SMiquel Raynal }
2913*a430fa06SMiquel Raynal 
2914*a430fa06SMiquel Raynal /**
2915*a430fa06SMiquel Raynal  * nand_sync - [MTD Interface] sync
2916*a430fa06SMiquel Raynal  * @mtd: MTD device structure
2917*a430fa06SMiquel Raynal  *
2918*a430fa06SMiquel Raynal  * Sync is actually a wait for chip ready function.
2919*a430fa06SMiquel Raynal  */
2920*a430fa06SMiquel Raynal static void nand_sync(struct mtd_info *mtd)
2921*a430fa06SMiquel Raynal {
2922*a430fa06SMiquel Raynal 	pr_debug("%s: called\n", __func__);
2923*a430fa06SMiquel Raynal 
2924*a430fa06SMiquel Raynal 	/* Grab the lock and see if the device is available */
2925*a430fa06SMiquel Raynal 	nand_get_device(mtd, FL_SYNCING);
2926*a430fa06SMiquel Raynal 	/* Release it and go back */
2927*a430fa06SMiquel Raynal 	nand_release_device(mtd);
2928*a430fa06SMiquel Raynal }
2929*a430fa06SMiquel Raynal 
2930*a430fa06SMiquel Raynal /**
2931*a430fa06SMiquel Raynal  * nand_block_isbad - [MTD Interface] Check if block at offset is bad
2932*a430fa06SMiquel Raynal  * @mtd: MTD device structure
2933*a430fa06SMiquel Raynal  * @offs: offset relative to mtd start
2934*a430fa06SMiquel Raynal  */
2935*a430fa06SMiquel Raynal static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
2936*a430fa06SMiquel Raynal {
2937*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
2938*a430fa06SMiquel Raynal 	int chipnr = (int)(offs >> chip->chip_shift);
2939*a430fa06SMiquel Raynal 	int ret;
2940*a430fa06SMiquel Raynal 
2941*a430fa06SMiquel Raynal 	/* Select the NAND device */
2942*a430fa06SMiquel Raynal 	nand_get_device(mtd, FL_READING);
2943*a430fa06SMiquel Raynal 	chip->select_chip(mtd, chipnr);
2944*a430fa06SMiquel Raynal 
2945*a430fa06SMiquel Raynal 	ret = nand_block_checkbad(mtd, offs, 0);
2946*a430fa06SMiquel Raynal 
2947*a430fa06SMiquel Raynal 	chip->select_chip(mtd, -1);
2948*a430fa06SMiquel Raynal 	nand_release_device(mtd);
2949*a430fa06SMiquel Raynal 
2950*a430fa06SMiquel Raynal 	return ret;
2951*a430fa06SMiquel Raynal }
2952*a430fa06SMiquel Raynal 
2953*a430fa06SMiquel Raynal /**
2954*a430fa06SMiquel Raynal  * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
2955*a430fa06SMiquel Raynal  * @mtd: MTD device structure
2956*a430fa06SMiquel Raynal  * @ofs: offset relative to mtd start
2957*a430fa06SMiquel Raynal  */
2958*a430fa06SMiquel Raynal static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2959*a430fa06SMiquel Raynal {
2960*a430fa06SMiquel Raynal 	int ret;
2961*a430fa06SMiquel Raynal 
2962*a430fa06SMiquel Raynal 	ret = nand_block_isbad(mtd, ofs);
2963*a430fa06SMiquel Raynal 	if (ret) {
2964*a430fa06SMiquel Raynal 		/* If it was bad already, return success and do nothing */
2965*a430fa06SMiquel Raynal 		if (ret > 0)
2966*a430fa06SMiquel Raynal 			return 0;
2967*a430fa06SMiquel Raynal 		return ret;
2968*a430fa06SMiquel Raynal 	}
2969*a430fa06SMiquel Raynal 
2970*a430fa06SMiquel Raynal 	return nand_block_markbad_lowlevel(mtd, ofs);
2971*a430fa06SMiquel Raynal }
2972*a430fa06SMiquel Raynal 
2973*a430fa06SMiquel Raynal /**
2974*a430fa06SMiquel Raynal  * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2975*a430fa06SMiquel Raynal  * @mtd: MTD device structure
2976*a430fa06SMiquel Raynal  * @chip: nand chip info structure
2977*a430fa06SMiquel Raynal  * @addr: feature address.
2978*a430fa06SMiquel Raynal  * @subfeature_param: the subfeature parameters, a four bytes array.
2979*a430fa06SMiquel Raynal  */
2980*a430fa06SMiquel Raynal static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2981*a430fa06SMiquel Raynal 			int addr, uint8_t *subfeature_param)
2982*a430fa06SMiquel Raynal {
2983*a430fa06SMiquel Raynal 	int status;
2984*a430fa06SMiquel Raynal 	int i;
2985*a430fa06SMiquel Raynal 
2986*a430fa06SMiquel Raynal #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2987*a430fa06SMiquel Raynal 	if (!chip->onfi_version ||
2988*a430fa06SMiquel Raynal 	    !(le16_to_cpu(chip->onfi_params.opt_cmd)
2989*a430fa06SMiquel Raynal 	      & ONFI_OPT_CMD_SET_GET_FEATURES))
2990*a430fa06SMiquel Raynal 		return -ENOTSUPP;
2991*a430fa06SMiquel Raynal #endif
2992*a430fa06SMiquel Raynal 
2993*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2994*a430fa06SMiquel Raynal 	for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2995*a430fa06SMiquel Raynal 		chip->write_byte(mtd, subfeature_param[i]);
2996*a430fa06SMiquel Raynal 
2997*a430fa06SMiquel Raynal 	status = chip->waitfunc(mtd, chip);
2998*a430fa06SMiquel Raynal 	if (status & NAND_STATUS_FAIL)
2999*a430fa06SMiquel Raynal 		return -EIO;
3000*a430fa06SMiquel Raynal 	return 0;
3001*a430fa06SMiquel Raynal }
3002*a430fa06SMiquel Raynal 
3003*a430fa06SMiquel Raynal /**
3004*a430fa06SMiquel Raynal  * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3005*a430fa06SMiquel Raynal  * @mtd: MTD device structure
3006*a430fa06SMiquel Raynal  * @chip: nand chip info structure
3007*a430fa06SMiquel Raynal  * @addr: feature address.
3008*a430fa06SMiquel Raynal  * @subfeature_param: the subfeature parameters, a four bytes array.
3009*a430fa06SMiquel Raynal  */
3010*a430fa06SMiquel Raynal static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3011*a430fa06SMiquel Raynal 			int addr, uint8_t *subfeature_param)
3012*a430fa06SMiquel Raynal {
3013*a430fa06SMiquel Raynal 	int i;
3014*a430fa06SMiquel Raynal 
3015*a430fa06SMiquel Raynal #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3016*a430fa06SMiquel Raynal 	if (!chip->onfi_version ||
3017*a430fa06SMiquel Raynal 	    !(le16_to_cpu(chip->onfi_params.opt_cmd)
3018*a430fa06SMiquel Raynal 	      & ONFI_OPT_CMD_SET_GET_FEATURES))
3019*a430fa06SMiquel Raynal 		return -ENOTSUPP;
3020*a430fa06SMiquel Raynal #endif
3021*a430fa06SMiquel Raynal 
3022*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
3023*a430fa06SMiquel Raynal 	for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3024*a430fa06SMiquel Raynal 		*subfeature_param++ = chip->read_byte(mtd);
3025*a430fa06SMiquel Raynal 	return 0;
3026*a430fa06SMiquel Raynal }
3027*a430fa06SMiquel Raynal 
3028*a430fa06SMiquel Raynal /* Set default functions */
3029*a430fa06SMiquel Raynal static void nand_set_defaults(struct nand_chip *chip, int busw)
3030*a430fa06SMiquel Raynal {
3031*a430fa06SMiquel Raynal 	/* check for proper chip_delay setup, set 20us if not */
3032*a430fa06SMiquel Raynal 	if (!chip->chip_delay)
3033*a430fa06SMiquel Raynal 		chip->chip_delay = 20;
3034*a430fa06SMiquel Raynal 
3035*a430fa06SMiquel Raynal 	/* check, if a user supplied command function given */
3036*a430fa06SMiquel Raynal 	if (chip->cmdfunc == NULL)
3037*a430fa06SMiquel Raynal 		chip->cmdfunc = nand_command;
3038*a430fa06SMiquel Raynal 
3039*a430fa06SMiquel Raynal 	/* check, if a user supplied wait function given */
3040*a430fa06SMiquel Raynal 	if (chip->waitfunc == NULL)
3041*a430fa06SMiquel Raynal 		chip->waitfunc = nand_wait;
3042*a430fa06SMiquel Raynal 
3043*a430fa06SMiquel Raynal 	if (!chip->select_chip)
3044*a430fa06SMiquel Raynal 		chip->select_chip = nand_select_chip;
3045*a430fa06SMiquel Raynal 
3046*a430fa06SMiquel Raynal 	/* set for ONFI nand */
3047*a430fa06SMiquel Raynal 	if (!chip->onfi_set_features)
3048*a430fa06SMiquel Raynal 		chip->onfi_set_features = nand_onfi_set_features;
3049*a430fa06SMiquel Raynal 	if (!chip->onfi_get_features)
3050*a430fa06SMiquel Raynal 		chip->onfi_get_features = nand_onfi_get_features;
3051*a430fa06SMiquel Raynal 
3052*a430fa06SMiquel Raynal 	/* If called twice, pointers that depend on busw may need to be reset */
3053*a430fa06SMiquel Raynal 	if (!chip->read_byte || chip->read_byte == nand_read_byte)
3054*a430fa06SMiquel Raynal 		chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3055*a430fa06SMiquel Raynal 	if (!chip->read_word)
3056*a430fa06SMiquel Raynal 		chip->read_word = nand_read_word;
3057*a430fa06SMiquel Raynal 	if (!chip->block_bad)
3058*a430fa06SMiquel Raynal 		chip->block_bad = nand_block_bad;
3059*a430fa06SMiquel Raynal 	if (!chip->block_markbad)
3060*a430fa06SMiquel Raynal 		chip->block_markbad = nand_default_block_markbad;
3061*a430fa06SMiquel Raynal 	if (!chip->write_buf || chip->write_buf == nand_write_buf)
3062*a430fa06SMiquel Raynal 		chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
3063*a430fa06SMiquel Raynal 	if (!chip->write_byte || chip->write_byte == nand_write_byte)
3064*a430fa06SMiquel Raynal 		chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3065*a430fa06SMiquel Raynal 	if (!chip->read_buf || chip->read_buf == nand_read_buf)
3066*a430fa06SMiquel Raynal 		chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
3067*a430fa06SMiquel Raynal 	if (!chip->scan_bbt)
3068*a430fa06SMiquel Raynal 		chip->scan_bbt = nand_default_bbt;
3069*a430fa06SMiquel Raynal 
3070*a430fa06SMiquel Raynal 	if (!chip->controller) {
3071*a430fa06SMiquel Raynal 		chip->controller = &chip->hwcontrol;
3072*a430fa06SMiquel Raynal 		spin_lock_init(&chip->controller->lock);
3073*a430fa06SMiquel Raynal 		init_waitqueue_head(&chip->controller->wq);
3074*a430fa06SMiquel Raynal 	}
3075*a430fa06SMiquel Raynal 
3076*a430fa06SMiquel Raynal 	if (!chip->buf_align)
3077*a430fa06SMiquel Raynal 		chip->buf_align = 1;
3078*a430fa06SMiquel Raynal }
3079*a430fa06SMiquel Raynal 
3080*a430fa06SMiquel Raynal /* Sanitize ONFI strings so we can safely print them */
3081*a430fa06SMiquel Raynal static void sanitize_string(char *s, size_t len)
3082*a430fa06SMiquel Raynal {
3083*a430fa06SMiquel Raynal 	ssize_t i;
3084*a430fa06SMiquel Raynal 
3085*a430fa06SMiquel Raynal 	/* Null terminate */
3086*a430fa06SMiquel Raynal 	s[len - 1] = 0;
3087*a430fa06SMiquel Raynal 
3088*a430fa06SMiquel Raynal 	/* Remove non printable chars */
3089*a430fa06SMiquel Raynal 	for (i = 0; i < len - 1; i++) {
3090*a430fa06SMiquel Raynal 		if (s[i] < ' ' || s[i] > 127)
3091*a430fa06SMiquel Raynal 			s[i] = '?';
3092*a430fa06SMiquel Raynal 	}
3093*a430fa06SMiquel Raynal 
3094*a430fa06SMiquel Raynal 	/* Remove trailing spaces */
3095*a430fa06SMiquel Raynal 	strim(s);
3096*a430fa06SMiquel Raynal }
3097*a430fa06SMiquel Raynal 
3098*a430fa06SMiquel Raynal static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3099*a430fa06SMiquel Raynal {
3100*a430fa06SMiquel Raynal 	int i;
3101*a430fa06SMiquel Raynal 	while (len--) {
3102*a430fa06SMiquel Raynal 		crc ^= *p++ << 8;
3103*a430fa06SMiquel Raynal 		for (i = 0; i < 8; i++)
3104*a430fa06SMiquel Raynal 			crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3105*a430fa06SMiquel Raynal 	}
3106*a430fa06SMiquel Raynal 
3107*a430fa06SMiquel Raynal 	return crc;
3108*a430fa06SMiquel Raynal }
3109*a430fa06SMiquel Raynal 
3110*a430fa06SMiquel Raynal #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3111*a430fa06SMiquel Raynal /* Parse the Extended Parameter Page. */
3112*a430fa06SMiquel Raynal static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3113*a430fa06SMiquel Raynal 		struct nand_chip *chip, struct nand_onfi_params *p)
3114*a430fa06SMiquel Raynal {
3115*a430fa06SMiquel Raynal 	struct onfi_ext_param_page *ep;
3116*a430fa06SMiquel Raynal 	struct onfi_ext_section *s;
3117*a430fa06SMiquel Raynal 	struct onfi_ext_ecc_info *ecc;
3118*a430fa06SMiquel Raynal 	uint8_t *cursor;
3119*a430fa06SMiquel Raynal 	int ret = -EINVAL;
3120*a430fa06SMiquel Raynal 	int len;
3121*a430fa06SMiquel Raynal 	int i;
3122*a430fa06SMiquel Raynal 
3123*a430fa06SMiquel Raynal 	len = le16_to_cpu(p->ext_param_page_length) * 16;
3124*a430fa06SMiquel Raynal 	ep = kmalloc(len, GFP_KERNEL);
3125*a430fa06SMiquel Raynal 	if (!ep)
3126*a430fa06SMiquel Raynal 		return -ENOMEM;
3127*a430fa06SMiquel Raynal 
3128*a430fa06SMiquel Raynal 	/* Send our own NAND_CMD_PARAM. */
3129*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3130*a430fa06SMiquel Raynal 
3131*a430fa06SMiquel Raynal 	/* Use the Change Read Column command to skip the ONFI param pages. */
3132*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3133*a430fa06SMiquel Raynal 			sizeof(*p) * p->num_of_param_pages , -1);
3134*a430fa06SMiquel Raynal 
3135*a430fa06SMiquel Raynal 	/* Read out the Extended Parameter Page. */
3136*a430fa06SMiquel Raynal 	chip->read_buf(mtd, (uint8_t *)ep, len);
3137*a430fa06SMiquel Raynal 	if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3138*a430fa06SMiquel Raynal 		!= le16_to_cpu(ep->crc))) {
3139*a430fa06SMiquel Raynal 		pr_debug("fail in the CRC.\n");
3140*a430fa06SMiquel Raynal 		goto ext_out;
3141*a430fa06SMiquel Raynal 	}
3142*a430fa06SMiquel Raynal 
3143*a430fa06SMiquel Raynal 	/*
3144*a430fa06SMiquel Raynal 	 * Check the signature.
3145*a430fa06SMiquel Raynal 	 * Do not strictly follow the ONFI spec, maybe changed in future.
3146*a430fa06SMiquel Raynal 	 */
3147*a430fa06SMiquel Raynal 	if (strncmp((char *)ep->sig, "EPPS", 4)) {
3148*a430fa06SMiquel Raynal 		pr_debug("The signature is invalid.\n");
3149*a430fa06SMiquel Raynal 		goto ext_out;
3150*a430fa06SMiquel Raynal 	}
3151*a430fa06SMiquel Raynal 
3152*a430fa06SMiquel Raynal 	/* find the ECC section. */
3153*a430fa06SMiquel Raynal 	cursor = (uint8_t *)(ep + 1);
3154*a430fa06SMiquel Raynal 	for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3155*a430fa06SMiquel Raynal 		s = ep->sections + i;
3156*a430fa06SMiquel Raynal 		if (s->type == ONFI_SECTION_TYPE_2)
3157*a430fa06SMiquel Raynal 			break;
3158*a430fa06SMiquel Raynal 		cursor += s->length * 16;
3159*a430fa06SMiquel Raynal 	}
3160*a430fa06SMiquel Raynal 	if (i == ONFI_EXT_SECTION_MAX) {
3161*a430fa06SMiquel Raynal 		pr_debug("We can not find the ECC section.\n");
3162*a430fa06SMiquel Raynal 		goto ext_out;
3163*a430fa06SMiquel Raynal 	}
3164*a430fa06SMiquel Raynal 
3165*a430fa06SMiquel Raynal 	/* get the info we want. */
3166*a430fa06SMiquel Raynal 	ecc = (struct onfi_ext_ecc_info *)cursor;
3167*a430fa06SMiquel Raynal 
3168*a430fa06SMiquel Raynal 	if (!ecc->codeword_size) {
3169*a430fa06SMiquel Raynal 		pr_debug("Invalid codeword size\n");
3170*a430fa06SMiquel Raynal 		goto ext_out;
3171*a430fa06SMiquel Raynal 	}
3172*a430fa06SMiquel Raynal 
3173*a430fa06SMiquel Raynal 	chip->ecc_strength_ds = ecc->ecc_bits;
3174*a430fa06SMiquel Raynal 	chip->ecc_step_ds = 1 << ecc->codeword_size;
3175*a430fa06SMiquel Raynal 	ret = 0;
3176*a430fa06SMiquel Raynal 
3177*a430fa06SMiquel Raynal ext_out:
3178*a430fa06SMiquel Raynal 	kfree(ep);
3179*a430fa06SMiquel Raynal 	return ret;
3180*a430fa06SMiquel Raynal }
3181*a430fa06SMiquel Raynal 
3182*a430fa06SMiquel Raynal static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3183*a430fa06SMiquel Raynal {
3184*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
3185*a430fa06SMiquel Raynal 	uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3186*a430fa06SMiquel Raynal 
3187*a430fa06SMiquel Raynal 	return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3188*a430fa06SMiquel Raynal 			feature);
3189*a430fa06SMiquel Raynal }
3190*a430fa06SMiquel Raynal 
3191*a430fa06SMiquel Raynal /*
3192*a430fa06SMiquel Raynal  * Configure chip properties from Micron vendor-specific ONFI table
3193*a430fa06SMiquel Raynal  */
3194*a430fa06SMiquel Raynal static void nand_onfi_detect_micron(struct nand_chip *chip,
3195*a430fa06SMiquel Raynal 		struct nand_onfi_params *p)
3196*a430fa06SMiquel Raynal {
3197*a430fa06SMiquel Raynal 	struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3198*a430fa06SMiquel Raynal 
3199*a430fa06SMiquel Raynal 	if (le16_to_cpu(p->vendor_revision) < 1)
3200*a430fa06SMiquel Raynal 		return;
3201*a430fa06SMiquel Raynal 
3202*a430fa06SMiquel Raynal 	chip->read_retries = micron->read_retry_options;
3203*a430fa06SMiquel Raynal 	chip->setup_read_retry = nand_setup_read_retry_micron;
3204*a430fa06SMiquel Raynal }
3205*a430fa06SMiquel Raynal 
3206*a430fa06SMiquel Raynal /*
3207*a430fa06SMiquel Raynal  * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
3208*a430fa06SMiquel Raynal  */
3209*a430fa06SMiquel Raynal static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
3210*a430fa06SMiquel Raynal 					int *busw)
3211*a430fa06SMiquel Raynal {
3212*a430fa06SMiquel Raynal 	struct nand_onfi_params *p = &chip->onfi_params;
3213*a430fa06SMiquel Raynal 	int i, j;
3214*a430fa06SMiquel Raynal 	int val;
3215*a430fa06SMiquel Raynal 
3216*a430fa06SMiquel Raynal 	/* Try ONFI for unknown chip or LP */
3217*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3218*a430fa06SMiquel Raynal 	if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3219*a430fa06SMiquel Raynal 		chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3220*a430fa06SMiquel Raynal 		return 0;
3221*a430fa06SMiquel Raynal 
3222*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3223*a430fa06SMiquel Raynal 	for (i = 0; i < 3; i++) {
3224*a430fa06SMiquel Raynal 		for (j = 0; j < sizeof(*p); j++)
3225*a430fa06SMiquel Raynal 			((uint8_t *)p)[j] = chip->read_byte(mtd);
3226*a430fa06SMiquel Raynal 		if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3227*a430fa06SMiquel Raynal 				le16_to_cpu(p->crc)) {
3228*a430fa06SMiquel Raynal 			break;
3229*a430fa06SMiquel Raynal 		}
3230*a430fa06SMiquel Raynal 	}
3231*a430fa06SMiquel Raynal 
3232*a430fa06SMiquel Raynal 	if (i == 3) {
3233*a430fa06SMiquel Raynal 		pr_err("Could not find valid ONFI parameter page; aborting\n");
3234*a430fa06SMiquel Raynal 		return 0;
3235*a430fa06SMiquel Raynal 	}
3236*a430fa06SMiquel Raynal 
3237*a430fa06SMiquel Raynal 	/* Check version */
3238*a430fa06SMiquel Raynal 	val = le16_to_cpu(p->revision);
3239*a430fa06SMiquel Raynal 	if (val & (1 << 5))
3240*a430fa06SMiquel Raynal 		chip->onfi_version = 23;
3241*a430fa06SMiquel Raynal 	else if (val & (1 << 4))
3242*a430fa06SMiquel Raynal 		chip->onfi_version = 22;
3243*a430fa06SMiquel Raynal 	else if (val & (1 << 3))
3244*a430fa06SMiquel Raynal 		chip->onfi_version = 21;
3245*a430fa06SMiquel Raynal 	else if (val & (1 << 2))
3246*a430fa06SMiquel Raynal 		chip->onfi_version = 20;
3247*a430fa06SMiquel Raynal 	else if (val & (1 << 1))
3248*a430fa06SMiquel Raynal 		chip->onfi_version = 10;
3249*a430fa06SMiquel Raynal 
3250*a430fa06SMiquel Raynal 	if (!chip->onfi_version) {
3251*a430fa06SMiquel Raynal 		pr_info("unsupported ONFI version: %d\n", val);
3252*a430fa06SMiquel Raynal 		return 0;
3253*a430fa06SMiquel Raynal 	}
3254*a430fa06SMiquel Raynal 
3255*a430fa06SMiquel Raynal 	sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3256*a430fa06SMiquel Raynal 	sanitize_string(p->model, sizeof(p->model));
3257*a430fa06SMiquel Raynal 	if (!mtd->name)
3258*a430fa06SMiquel Raynal 		mtd->name = p->model;
3259*a430fa06SMiquel Raynal 
3260*a430fa06SMiquel Raynal 	mtd->writesize = le32_to_cpu(p->byte_per_page);
3261*a430fa06SMiquel Raynal 
3262*a430fa06SMiquel Raynal 	/*
3263*a430fa06SMiquel Raynal 	 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3264*a430fa06SMiquel Raynal 	 * (don't ask me who thought of this...). MTD assumes that these
3265*a430fa06SMiquel Raynal 	 * dimensions will be power-of-2, so just truncate the remaining area.
3266*a430fa06SMiquel Raynal 	 */
3267*a430fa06SMiquel Raynal 	mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3268*a430fa06SMiquel Raynal 	mtd->erasesize *= mtd->writesize;
3269*a430fa06SMiquel Raynal 
3270*a430fa06SMiquel Raynal 	mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3271*a430fa06SMiquel Raynal 
3272*a430fa06SMiquel Raynal 	/* See erasesize comment */
3273*a430fa06SMiquel Raynal 	chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3274*a430fa06SMiquel Raynal 	chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3275*a430fa06SMiquel Raynal 	chip->bits_per_cell = p->bits_per_cell;
3276*a430fa06SMiquel Raynal 
3277*a430fa06SMiquel Raynal 	if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
3278*a430fa06SMiquel Raynal 		*busw = NAND_BUSWIDTH_16;
3279*a430fa06SMiquel Raynal 	else
3280*a430fa06SMiquel Raynal 		*busw = 0;
3281*a430fa06SMiquel Raynal 
3282*a430fa06SMiquel Raynal 	if (p->ecc_bits != 0xff) {
3283*a430fa06SMiquel Raynal 		chip->ecc_strength_ds = p->ecc_bits;
3284*a430fa06SMiquel Raynal 		chip->ecc_step_ds = 512;
3285*a430fa06SMiquel Raynal 	} else if (chip->onfi_version >= 21 &&
3286*a430fa06SMiquel Raynal 		(onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3287*a430fa06SMiquel Raynal 
3288*a430fa06SMiquel Raynal 		/*
3289*a430fa06SMiquel Raynal 		 * The nand_flash_detect_ext_param_page() uses the
3290*a430fa06SMiquel Raynal 		 * Change Read Column command which maybe not supported
3291*a430fa06SMiquel Raynal 		 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3292*a430fa06SMiquel Raynal 		 * now. We do not replace user supplied command function.
3293*a430fa06SMiquel Raynal 		 */
3294*a430fa06SMiquel Raynal 		if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3295*a430fa06SMiquel Raynal 			chip->cmdfunc = nand_command_lp;
3296*a430fa06SMiquel Raynal 
3297*a430fa06SMiquel Raynal 		/* The Extended Parameter Page is supported since ONFI 2.1. */
3298*a430fa06SMiquel Raynal 		if (nand_flash_detect_ext_param_page(mtd, chip, p))
3299*a430fa06SMiquel Raynal 			pr_warn("Failed to detect ONFI extended param page\n");
3300*a430fa06SMiquel Raynal 	} else {
3301*a430fa06SMiquel Raynal 		pr_warn("Could not retrieve ONFI ECC requirements\n");
3302*a430fa06SMiquel Raynal 	}
3303*a430fa06SMiquel Raynal 
3304*a430fa06SMiquel Raynal 	if (p->jedec_id == NAND_MFR_MICRON)
3305*a430fa06SMiquel Raynal 		nand_onfi_detect_micron(chip, p);
3306*a430fa06SMiquel Raynal 
3307*a430fa06SMiquel Raynal 	return 1;
3308*a430fa06SMiquel Raynal }
3309*a430fa06SMiquel Raynal #else
3310*a430fa06SMiquel Raynal static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
3311*a430fa06SMiquel Raynal 					int *busw)
3312*a430fa06SMiquel Raynal {
3313*a430fa06SMiquel Raynal 	return 0;
3314*a430fa06SMiquel Raynal }
3315*a430fa06SMiquel Raynal #endif
3316*a430fa06SMiquel Raynal 
3317*a430fa06SMiquel Raynal /*
3318*a430fa06SMiquel Raynal  * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3319*a430fa06SMiquel Raynal  */
3320*a430fa06SMiquel Raynal static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3321*a430fa06SMiquel Raynal 					int *busw)
3322*a430fa06SMiquel Raynal {
3323*a430fa06SMiquel Raynal 	struct nand_jedec_params *p = &chip->jedec_params;
3324*a430fa06SMiquel Raynal 	struct jedec_ecc_info *ecc;
3325*a430fa06SMiquel Raynal 	int val;
3326*a430fa06SMiquel Raynal 	int i, j;
3327*a430fa06SMiquel Raynal 
3328*a430fa06SMiquel Raynal 	/* Try JEDEC for unknown chip or LP */
3329*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3330*a430fa06SMiquel Raynal 	if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3331*a430fa06SMiquel Raynal 		chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3332*a430fa06SMiquel Raynal 		chip->read_byte(mtd) != 'C')
3333*a430fa06SMiquel Raynal 		return 0;
3334*a430fa06SMiquel Raynal 
3335*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3336*a430fa06SMiquel Raynal 	for (i = 0; i < 3; i++) {
3337*a430fa06SMiquel Raynal 		for (j = 0; j < sizeof(*p); j++)
3338*a430fa06SMiquel Raynal 			((uint8_t *)p)[j] = chip->read_byte(mtd);
3339*a430fa06SMiquel Raynal 
3340*a430fa06SMiquel Raynal 		if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3341*a430fa06SMiquel Raynal 				le16_to_cpu(p->crc))
3342*a430fa06SMiquel Raynal 			break;
3343*a430fa06SMiquel Raynal 	}
3344*a430fa06SMiquel Raynal 
3345*a430fa06SMiquel Raynal 	if (i == 3) {
3346*a430fa06SMiquel Raynal 		pr_err("Could not find valid JEDEC parameter page; aborting\n");
3347*a430fa06SMiquel Raynal 		return 0;
3348*a430fa06SMiquel Raynal 	}
3349*a430fa06SMiquel Raynal 
3350*a430fa06SMiquel Raynal 	/* Check version */
3351*a430fa06SMiquel Raynal 	val = le16_to_cpu(p->revision);
3352*a430fa06SMiquel Raynal 	if (val & (1 << 2))
3353*a430fa06SMiquel Raynal 		chip->jedec_version = 10;
3354*a430fa06SMiquel Raynal 	else if (val & (1 << 1))
3355*a430fa06SMiquel Raynal 		chip->jedec_version = 1; /* vendor specific version */
3356*a430fa06SMiquel Raynal 
3357*a430fa06SMiquel Raynal 	if (!chip->jedec_version) {
3358*a430fa06SMiquel Raynal 		pr_info("unsupported JEDEC version: %d\n", val);
3359*a430fa06SMiquel Raynal 		return 0;
3360*a430fa06SMiquel Raynal 	}
3361*a430fa06SMiquel Raynal 
3362*a430fa06SMiquel Raynal 	sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3363*a430fa06SMiquel Raynal 	sanitize_string(p->model, sizeof(p->model));
3364*a430fa06SMiquel Raynal 	if (!mtd->name)
3365*a430fa06SMiquel Raynal 		mtd->name = p->model;
3366*a430fa06SMiquel Raynal 
3367*a430fa06SMiquel Raynal 	mtd->writesize = le32_to_cpu(p->byte_per_page);
3368*a430fa06SMiquel Raynal 
3369*a430fa06SMiquel Raynal 	/* Please reference to the comment for nand_flash_detect_onfi. */
3370*a430fa06SMiquel Raynal 	mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3371*a430fa06SMiquel Raynal 	mtd->erasesize *= mtd->writesize;
3372*a430fa06SMiquel Raynal 
3373*a430fa06SMiquel Raynal 	mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3374*a430fa06SMiquel Raynal 
3375*a430fa06SMiquel Raynal 	/* Please reference to the comment for nand_flash_detect_onfi. */
3376*a430fa06SMiquel Raynal 	chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3377*a430fa06SMiquel Raynal 	chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3378*a430fa06SMiquel Raynal 	chip->bits_per_cell = p->bits_per_cell;
3379*a430fa06SMiquel Raynal 
3380*a430fa06SMiquel Raynal 	if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3381*a430fa06SMiquel Raynal 		*busw = NAND_BUSWIDTH_16;
3382*a430fa06SMiquel Raynal 	else
3383*a430fa06SMiquel Raynal 		*busw = 0;
3384*a430fa06SMiquel Raynal 
3385*a430fa06SMiquel Raynal 	/* ECC info */
3386*a430fa06SMiquel Raynal 	ecc = &p->ecc_info[0];
3387*a430fa06SMiquel Raynal 
3388*a430fa06SMiquel Raynal 	if (ecc->codeword_size >= 9) {
3389*a430fa06SMiquel Raynal 		chip->ecc_strength_ds = ecc->ecc_bits;
3390*a430fa06SMiquel Raynal 		chip->ecc_step_ds = 1 << ecc->codeword_size;
3391*a430fa06SMiquel Raynal 	} else {
3392*a430fa06SMiquel Raynal 		pr_warn("Invalid codeword size\n");
3393*a430fa06SMiquel Raynal 	}
3394*a430fa06SMiquel Raynal 
3395*a430fa06SMiquel Raynal 	return 1;
3396*a430fa06SMiquel Raynal }
3397*a430fa06SMiquel Raynal 
3398*a430fa06SMiquel Raynal /*
3399*a430fa06SMiquel Raynal  * nand_id_has_period - Check if an ID string has a given wraparound period
3400*a430fa06SMiquel Raynal  * @id_data: the ID string
3401*a430fa06SMiquel Raynal  * @arrlen: the length of the @id_data array
3402*a430fa06SMiquel Raynal  * @period: the period of repitition
3403*a430fa06SMiquel Raynal  *
3404*a430fa06SMiquel Raynal  * Check if an ID string is repeated within a given sequence of bytes at
3405*a430fa06SMiquel Raynal  * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
3406*a430fa06SMiquel Raynal  * period of 3). This is a helper function for nand_id_len(). Returns non-zero
3407*a430fa06SMiquel Raynal  * if the repetition has a period of @period; otherwise, returns zero.
3408*a430fa06SMiquel Raynal  */
3409*a430fa06SMiquel Raynal static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3410*a430fa06SMiquel Raynal {
3411*a430fa06SMiquel Raynal 	int i, j;
3412*a430fa06SMiquel Raynal 	for (i = 0; i < period; i++)
3413*a430fa06SMiquel Raynal 		for (j = i + period; j < arrlen; j += period)
3414*a430fa06SMiquel Raynal 			if (id_data[i] != id_data[j])
3415*a430fa06SMiquel Raynal 				return 0;
3416*a430fa06SMiquel Raynal 	return 1;
3417*a430fa06SMiquel Raynal }
3418*a430fa06SMiquel Raynal 
3419*a430fa06SMiquel Raynal /*
3420*a430fa06SMiquel Raynal  * nand_id_len - Get the length of an ID string returned by CMD_READID
3421*a430fa06SMiquel Raynal  * @id_data: the ID string
3422*a430fa06SMiquel Raynal  * @arrlen: the length of the @id_data array
3423*a430fa06SMiquel Raynal 
3424*a430fa06SMiquel Raynal  * Returns the length of the ID string, according to known wraparound/trailing
3425*a430fa06SMiquel Raynal  * zero patterns. If no pattern exists, returns the length of the array.
3426*a430fa06SMiquel Raynal  */
3427*a430fa06SMiquel Raynal static int nand_id_len(u8 *id_data, int arrlen)
3428*a430fa06SMiquel Raynal {
3429*a430fa06SMiquel Raynal 	int last_nonzero, period;
3430*a430fa06SMiquel Raynal 
3431*a430fa06SMiquel Raynal 	/* Find last non-zero byte */
3432*a430fa06SMiquel Raynal 	for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3433*a430fa06SMiquel Raynal 		if (id_data[last_nonzero])
3434*a430fa06SMiquel Raynal 			break;
3435*a430fa06SMiquel Raynal 
3436*a430fa06SMiquel Raynal 	/* All zeros */
3437*a430fa06SMiquel Raynal 	if (last_nonzero < 0)
3438*a430fa06SMiquel Raynal 		return 0;
3439*a430fa06SMiquel Raynal 
3440*a430fa06SMiquel Raynal 	/* Calculate wraparound period */
3441*a430fa06SMiquel Raynal 	for (period = 1; period < arrlen; period++)
3442*a430fa06SMiquel Raynal 		if (nand_id_has_period(id_data, arrlen, period))
3443*a430fa06SMiquel Raynal 			break;
3444*a430fa06SMiquel Raynal 
3445*a430fa06SMiquel Raynal 	/* There's a repeated pattern */
3446*a430fa06SMiquel Raynal 	if (period < arrlen)
3447*a430fa06SMiquel Raynal 		return period;
3448*a430fa06SMiquel Raynal 
3449*a430fa06SMiquel Raynal 	/* There are trailing zeros */
3450*a430fa06SMiquel Raynal 	if (last_nonzero < arrlen - 1)
3451*a430fa06SMiquel Raynal 		return last_nonzero + 1;
3452*a430fa06SMiquel Raynal 
3453*a430fa06SMiquel Raynal 	/* No pattern detected */
3454*a430fa06SMiquel Raynal 	return arrlen;
3455*a430fa06SMiquel Raynal }
3456*a430fa06SMiquel Raynal 
3457*a430fa06SMiquel Raynal /* Extract the bits of per cell from the 3rd byte of the extended ID */
3458*a430fa06SMiquel Raynal static int nand_get_bits_per_cell(u8 cellinfo)
3459*a430fa06SMiquel Raynal {
3460*a430fa06SMiquel Raynal 	int bits;
3461*a430fa06SMiquel Raynal 
3462*a430fa06SMiquel Raynal 	bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3463*a430fa06SMiquel Raynal 	bits >>= NAND_CI_CELLTYPE_SHIFT;
3464*a430fa06SMiquel Raynal 	return bits + 1;
3465*a430fa06SMiquel Raynal }
3466*a430fa06SMiquel Raynal 
3467*a430fa06SMiquel Raynal /*
3468*a430fa06SMiquel Raynal  * Many new NAND share similar device ID codes, which represent the size of the
3469*a430fa06SMiquel Raynal  * chip. The rest of the parameters must be decoded according to generic or
3470*a430fa06SMiquel Raynal  * manufacturer-specific "extended ID" decoding patterns.
3471*a430fa06SMiquel Raynal  */
3472*a430fa06SMiquel Raynal static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3473*a430fa06SMiquel Raynal 				u8 id_data[8], int *busw)
3474*a430fa06SMiquel Raynal {
3475*a430fa06SMiquel Raynal 	int extid, id_len;
3476*a430fa06SMiquel Raynal 	/* The 3rd id byte holds MLC / multichip data */
3477*a430fa06SMiquel Raynal 	chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3478*a430fa06SMiquel Raynal 	/* The 4th id byte is the important one */
3479*a430fa06SMiquel Raynal 	extid = id_data[3];
3480*a430fa06SMiquel Raynal 
3481*a430fa06SMiquel Raynal 	id_len = nand_id_len(id_data, 8);
3482*a430fa06SMiquel Raynal 
3483*a430fa06SMiquel Raynal 	/*
3484*a430fa06SMiquel Raynal 	 * Field definitions are in the following datasheets:
3485*a430fa06SMiquel Raynal 	 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
3486*a430fa06SMiquel Raynal 	 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
3487*a430fa06SMiquel Raynal 	 * Hynix MLC   (6 byte ID): Hynix H27UBG8T2B (p.22)
3488*a430fa06SMiquel Raynal 	 *
3489*a430fa06SMiquel Raynal 	 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3490*a430fa06SMiquel Raynal 	 * ID to decide what to do.
3491*a430fa06SMiquel Raynal 	 */
3492*a430fa06SMiquel Raynal 	if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
3493*a430fa06SMiquel Raynal 			!nand_is_slc(chip) && id_data[5] != 0x00) {
3494*a430fa06SMiquel Raynal 		/* Calc pagesize */
3495*a430fa06SMiquel Raynal 		mtd->writesize = 2048 << (extid & 0x03);
3496*a430fa06SMiquel Raynal 		extid >>= 2;
3497*a430fa06SMiquel Raynal 		/* Calc oobsize */
3498*a430fa06SMiquel Raynal 		switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3499*a430fa06SMiquel Raynal 		case 1:
3500*a430fa06SMiquel Raynal 			mtd->oobsize = 128;
3501*a430fa06SMiquel Raynal 			break;
3502*a430fa06SMiquel Raynal 		case 2:
3503*a430fa06SMiquel Raynal 			mtd->oobsize = 218;
3504*a430fa06SMiquel Raynal 			break;
3505*a430fa06SMiquel Raynal 		case 3:
3506*a430fa06SMiquel Raynal 			mtd->oobsize = 400;
3507*a430fa06SMiquel Raynal 			break;
3508*a430fa06SMiquel Raynal 		case 4:
3509*a430fa06SMiquel Raynal 			mtd->oobsize = 436;
3510*a430fa06SMiquel Raynal 			break;
3511*a430fa06SMiquel Raynal 		case 5:
3512*a430fa06SMiquel Raynal 			mtd->oobsize = 512;
3513*a430fa06SMiquel Raynal 			break;
3514*a430fa06SMiquel Raynal 		case 6:
3515*a430fa06SMiquel Raynal 			mtd->oobsize = 640;
3516*a430fa06SMiquel Raynal 			break;
3517*a430fa06SMiquel Raynal 		case 7:
3518*a430fa06SMiquel Raynal 		default: /* Other cases are "reserved" (unknown) */
3519*a430fa06SMiquel Raynal 			mtd->oobsize = 1024;
3520*a430fa06SMiquel Raynal 			break;
3521*a430fa06SMiquel Raynal 		}
3522*a430fa06SMiquel Raynal 		extid >>= 2;
3523*a430fa06SMiquel Raynal 		/* Calc blocksize */
3524*a430fa06SMiquel Raynal 		mtd->erasesize = (128 * 1024) <<
3525*a430fa06SMiquel Raynal 			(((extid >> 1) & 0x04) | (extid & 0x03));
3526*a430fa06SMiquel Raynal 		*busw = 0;
3527*a430fa06SMiquel Raynal 	} else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
3528*a430fa06SMiquel Raynal 			!nand_is_slc(chip)) {
3529*a430fa06SMiquel Raynal 		unsigned int tmp;
3530*a430fa06SMiquel Raynal 
3531*a430fa06SMiquel Raynal 		/* Calc pagesize */
3532*a430fa06SMiquel Raynal 		mtd->writesize = 2048 << (extid & 0x03);
3533*a430fa06SMiquel Raynal 		extid >>= 2;
3534*a430fa06SMiquel Raynal 		/* Calc oobsize */
3535*a430fa06SMiquel Raynal 		switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3536*a430fa06SMiquel Raynal 		case 0:
3537*a430fa06SMiquel Raynal 			mtd->oobsize = 128;
3538*a430fa06SMiquel Raynal 			break;
3539*a430fa06SMiquel Raynal 		case 1:
3540*a430fa06SMiquel Raynal 			mtd->oobsize = 224;
3541*a430fa06SMiquel Raynal 			break;
3542*a430fa06SMiquel Raynal 		case 2:
3543*a430fa06SMiquel Raynal 			mtd->oobsize = 448;
3544*a430fa06SMiquel Raynal 			break;
3545*a430fa06SMiquel Raynal 		case 3:
3546*a430fa06SMiquel Raynal 			mtd->oobsize = 64;
3547*a430fa06SMiquel Raynal 			break;
3548*a430fa06SMiquel Raynal 		case 4:
3549*a430fa06SMiquel Raynal 			mtd->oobsize = 32;
3550*a430fa06SMiquel Raynal 			break;
3551*a430fa06SMiquel Raynal 		case 5:
3552*a430fa06SMiquel Raynal 			mtd->oobsize = 16;
3553*a430fa06SMiquel Raynal 			break;
3554*a430fa06SMiquel Raynal 		default:
3555*a430fa06SMiquel Raynal 			mtd->oobsize = 640;
3556*a430fa06SMiquel Raynal 			break;
3557*a430fa06SMiquel Raynal 		}
3558*a430fa06SMiquel Raynal 		extid >>= 2;
3559*a430fa06SMiquel Raynal 		/* Calc blocksize */
3560*a430fa06SMiquel Raynal 		tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3561*a430fa06SMiquel Raynal 		if (tmp < 0x03)
3562*a430fa06SMiquel Raynal 			mtd->erasesize = (128 * 1024) << tmp;
3563*a430fa06SMiquel Raynal 		else if (tmp == 0x03)
3564*a430fa06SMiquel Raynal 			mtd->erasesize = 768 * 1024;
3565*a430fa06SMiquel Raynal 		else
3566*a430fa06SMiquel Raynal 			mtd->erasesize = (64 * 1024) << tmp;
3567*a430fa06SMiquel Raynal 		*busw = 0;
3568*a430fa06SMiquel Raynal 	} else {
3569*a430fa06SMiquel Raynal 		/* Calc pagesize */
3570*a430fa06SMiquel Raynal 		mtd->writesize = 1024 << (extid & 0x03);
3571*a430fa06SMiquel Raynal 		extid >>= 2;
3572*a430fa06SMiquel Raynal 		/* Calc oobsize */
3573*a430fa06SMiquel Raynal 		mtd->oobsize = (8 << (extid & 0x01)) *
3574*a430fa06SMiquel Raynal 			(mtd->writesize >> 9);
3575*a430fa06SMiquel Raynal 		extid >>= 2;
3576*a430fa06SMiquel Raynal 		/* Calc blocksize. Blocksize is multiples of 64KiB */
3577*a430fa06SMiquel Raynal 		mtd->erasesize = (64 * 1024) << (extid & 0x03);
3578*a430fa06SMiquel Raynal 		extid >>= 2;
3579*a430fa06SMiquel Raynal 		/* Get buswidth information */
3580*a430fa06SMiquel Raynal 		*busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
3581*a430fa06SMiquel Raynal 
3582*a430fa06SMiquel Raynal 		/*
3583*a430fa06SMiquel Raynal 		 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3584*a430fa06SMiquel Raynal 		 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3585*a430fa06SMiquel Raynal 		 * follows:
3586*a430fa06SMiquel Raynal 		 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3587*a430fa06SMiquel Raynal 		 *                         110b -> 24nm
3588*a430fa06SMiquel Raynal 		 * - ID byte 5, bit[7]:    1 -> BENAND, 0 -> raw SLC
3589*a430fa06SMiquel Raynal 		 */
3590*a430fa06SMiquel Raynal 		if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
3591*a430fa06SMiquel Raynal 				nand_is_slc(chip) &&
3592*a430fa06SMiquel Raynal 				(id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3593*a430fa06SMiquel Raynal 				!(id_data[4] & 0x80) /* !BENAND */) {
3594*a430fa06SMiquel Raynal 			mtd->oobsize = 32 * mtd->writesize >> 9;
3595*a430fa06SMiquel Raynal 		}
3596*a430fa06SMiquel Raynal 
3597*a430fa06SMiquel Raynal 	}
3598*a430fa06SMiquel Raynal }
3599*a430fa06SMiquel Raynal 
3600*a430fa06SMiquel Raynal /*
3601*a430fa06SMiquel Raynal  * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3602*a430fa06SMiquel Raynal  * decodes a matching ID table entry and assigns the MTD size parameters for
3603*a430fa06SMiquel Raynal  * the chip.
3604*a430fa06SMiquel Raynal  */
3605*a430fa06SMiquel Raynal static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3606*a430fa06SMiquel Raynal 				struct nand_flash_dev *type, u8 id_data[8],
3607*a430fa06SMiquel Raynal 				int *busw)
3608*a430fa06SMiquel Raynal {
3609*a430fa06SMiquel Raynal 	int maf_id = id_data[0];
3610*a430fa06SMiquel Raynal 
3611*a430fa06SMiquel Raynal 	mtd->erasesize = type->erasesize;
3612*a430fa06SMiquel Raynal 	mtd->writesize = type->pagesize;
3613*a430fa06SMiquel Raynal 	mtd->oobsize = mtd->writesize / 32;
3614*a430fa06SMiquel Raynal 	*busw = type->options & NAND_BUSWIDTH_16;
3615*a430fa06SMiquel Raynal 
3616*a430fa06SMiquel Raynal 	/* All legacy ID NAND are small-page, SLC */
3617*a430fa06SMiquel Raynal 	chip->bits_per_cell = 1;
3618*a430fa06SMiquel Raynal 
3619*a430fa06SMiquel Raynal 	/*
3620*a430fa06SMiquel Raynal 	 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3621*a430fa06SMiquel Raynal 	 * some Spansion chips have erasesize that conflicts with size
3622*a430fa06SMiquel Raynal 	 * listed in nand_ids table.
3623*a430fa06SMiquel Raynal 	 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3624*a430fa06SMiquel Raynal 	 */
3625*a430fa06SMiquel Raynal 	if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3626*a430fa06SMiquel Raynal 			&& id_data[6] == 0x00 && id_data[7] == 0x00
3627*a430fa06SMiquel Raynal 			&& mtd->writesize == 512) {
3628*a430fa06SMiquel Raynal 		mtd->erasesize = 128 * 1024;
3629*a430fa06SMiquel Raynal 		mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3630*a430fa06SMiquel Raynal 	}
3631*a430fa06SMiquel Raynal }
3632*a430fa06SMiquel Raynal 
3633*a430fa06SMiquel Raynal /*
3634*a430fa06SMiquel Raynal  * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3635*a430fa06SMiquel Raynal  * heuristic patterns using various detected parameters (e.g., manufacturer,
3636*a430fa06SMiquel Raynal  * page size, cell-type information).
3637*a430fa06SMiquel Raynal  */
3638*a430fa06SMiquel Raynal static void nand_decode_bbm_options(struct mtd_info *mtd,
3639*a430fa06SMiquel Raynal 				    struct nand_chip *chip, u8 id_data[8])
3640*a430fa06SMiquel Raynal {
3641*a430fa06SMiquel Raynal 	int maf_id = id_data[0];
3642*a430fa06SMiquel Raynal 
3643*a430fa06SMiquel Raynal 	/* Set the bad block position */
3644*a430fa06SMiquel Raynal 	if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3645*a430fa06SMiquel Raynal 		chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3646*a430fa06SMiquel Raynal 	else
3647*a430fa06SMiquel Raynal 		chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3648*a430fa06SMiquel Raynal 
3649*a430fa06SMiquel Raynal 	/*
3650*a430fa06SMiquel Raynal 	 * Bad block marker is stored in the last page of each block on Samsung
3651*a430fa06SMiquel Raynal 	 * and Hynix MLC devices; stored in first two pages of each block on
3652*a430fa06SMiquel Raynal 	 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3653*a430fa06SMiquel Raynal 	 * AMD/Spansion, and Macronix.  All others scan only the first page.
3654*a430fa06SMiquel Raynal 	 */
3655*a430fa06SMiquel Raynal 	if (!nand_is_slc(chip) &&
3656*a430fa06SMiquel Raynal 			(maf_id == NAND_MFR_SAMSUNG ||
3657*a430fa06SMiquel Raynal 			 maf_id == NAND_MFR_HYNIX))
3658*a430fa06SMiquel Raynal 		chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
3659*a430fa06SMiquel Raynal 	else if ((nand_is_slc(chip) &&
3660*a430fa06SMiquel Raynal 				(maf_id == NAND_MFR_SAMSUNG ||
3661*a430fa06SMiquel Raynal 				 maf_id == NAND_MFR_HYNIX ||
3662*a430fa06SMiquel Raynal 				 maf_id == NAND_MFR_TOSHIBA ||
3663*a430fa06SMiquel Raynal 				 maf_id == NAND_MFR_AMD ||
3664*a430fa06SMiquel Raynal 				 maf_id == NAND_MFR_MACRONIX)) ||
3665*a430fa06SMiquel Raynal 			(mtd->writesize == 2048 &&
3666*a430fa06SMiquel Raynal 			 maf_id == NAND_MFR_MICRON))
3667*a430fa06SMiquel Raynal 		chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3668*a430fa06SMiquel Raynal }
3669*a430fa06SMiquel Raynal 
3670*a430fa06SMiquel Raynal static inline bool is_full_id_nand(struct nand_flash_dev *type)
3671*a430fa06SMiquel Raynal {
3672*a430fa06SMiquel Raynal 	return type->id_len;
3673*a430fa06SMiquel Raynal }
3674*a430fa06SMiquel Raynal 
3675*a430fa06SMiquel Raynal static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3676*a430fa06SMiquel Raynal 		   struct nand_flash_dev *type, u8 *id_data, int *busw)
3677*a430fa06SMiquel Raynal {
3678*a430fa06SMiquel Raynal 	if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
3679*a430fa06SMiquel Raynal 		mtd->writesize = type->pagesize;
3680*a430fa06SMiquel Raynal 		mtd->erasesize = type->erasesize;
3681*a430fa06SMiquel Raynal 		mtd->oobsize = type->oobsize;
3682*a430fa06SMiquel Raynal 
3683*a430fa06SMiquel Raynal 		chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3684*a430fa06SMiquel Raynal 		chip->chipsize = (uint64_t)type->chipsize << 20;
3685*a430fa06SMiquel Raynal 		chip->options |= type->options;
3686*a430fa06SMiquel Raynal 		chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3687*a430fa06SMiquel Raynal 		chip->ecc_step_ds = NAND_ECC_STEP(type);
3688*a430fa06SMiquel Raynal 		chip->onfi_timing_mode_default =
3689*a430fa06SMiquel Raynal 					type->onfi_timing_mode_default;
3690*a430fa06SMiquel Raynal 
3691*a430fa06SMiquel Raynal 		*busw = type->options & NAND_BUSWIDTH_16;
3692*a430fa06SMiquel Raynal 
3693*a430fa06SMiquel Raynal 		if (!mtd->name)
3694*a430fa06SMiquel Raynal 			mtd->name = type->name;
3695*a430fa06SMiquel Raynal 
3696*a430fa06SMiquel Raynal 		return true;
3697*a430fa06SMiquel Raynal 	}
3698*a430fa06SMiquel Raynal 	return false;
3699*a430fa06SMiquel Raynal }
3700*a430fa06SMiquel Raynal 
3701*a430fa06SMiquel Raynal /*
3702*a430fa06SMiquel Raynal  * Get the flash and manufacturer id and lookup if the type is supported.
3703*a430fa06SMiquel Raynal  */
3704*a430fa06SMiquel Raynal struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
3705*a430fa06SMiquel Raynal 						  struct nand_chip *chip,
3706*a430fa06SMiquel Raynal 						  int *maf_id, int *dev_id,
3707*a430fa06SMiquel Raynal 						  struct nand_flash_dev *type)
3708*a430fa06SMiquel Raynal {
3709*a430fa06SMiquel Raynal 	int busw;
3710*a430fa06SMiquel Raynal 	int i, maf_idx;
3711*a430fa06SMiquel Raynal 	u8 id_data[8];
3712*a430fa06SMiquel Raynal 
3713*a430fa06SMiquel Raynal 	/*
3714*a430fa06SMiquel Raynal 	 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
3715*a430fa06SMiquel Raynal 	 * after power-up.
3716*a430fa06SMiquel Raynal 	 */
3717*a430fa06SMiquel Raynal 	nand_reset(chip, 0);
3718*a430fa06SMiquel Raynal 
3719*a430fa06SMiquel Raynal 	/* Select the device */
3720*a430fa06SMiquel Raynal 	chip->select_chip(mtd, 0);
3721*a430fa06SMiquel Raynal 
3722*a430fa06SMiquel Raynal 	/* Send the command for reading device ID */
3723*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3724*a430fa06SMiquel Raynal 
3725*a430fa06SMiquel Raynal 	/* Read manufacturer and device IDs */
3726*a430fa06SMiquel Raynal 	*maf_id = chip->read_byte(mtd);
3727*a430fa06SMiquel Raynal 	*dev_id = chip->read_byte(mtd);
3728*a430fa06SMiquel Raynal 
3729*a430fa06SMiquel Raynal 	/*
3730*a430fa06SMiquel Raynal 	 * Try again to make sure, as some systems the bus-hold or other
3731*a430fa06SMiquel Raynal 	 * interface concerns can cause random data which looks like a
3732*a430fa06SMiquel Raynal 	 * possibly credible NAND flash to appear. If the two results do
3733*a430fa06SMiquel Raynal 	 * not match, ignore the device completely.
3734*a430fa06SMiquel Raynal 	 */
3735*a430fa06SMiquel Raynal 
3736*a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3737*a430fa06SMiquel Raynal 
3738*a430fa06SMiquel Raynal 	/* Read entire ID string */
3739*a430fa06SMiquel Raynal 	for (i = 0; i < 8; i++)
3740*a430fa06SMiquel Raynal 		id_data[i] = chip->read_byte(mtd);
3741*a430fa06SMiquel Raynal 
3742*a430fa06SMiquel Raynal 	if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
3743*a430fa06SMiquel Raynal 		pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
3744*a430fa06SMiquel Raynal 			*maf_id, *dev_id, id_data[0], id_data[1]);
3745*a430fa06SMiquel Raynal 		return ERR_PTR(-ENODEV);
3746*a430fa06SMiquel Raynal 	}
3747*a430fa06SMiquel Raynal 
3748*a430fa06SMiquel Raynal 	if (!type)
3749*a430fa06SMiquel Raynal 		type = nand_flash_ids;
3750*a430fa06SMiquel Raynal 
3751*a430fa06SMiquel Raynal 	for (; type->name != NULL; type++) {
3752*a430fa06SMiquel Raynal 		if (is_full_id_nand(type)) {
3753*a430fa06SMiquel Raynal 			if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3754*a430fa06SMiquel Raynal 				goto ident_done;
3755*a430fa06SMiquel Raynal 		} else if (*dev_id == type->dev_id) {
3756*a430fa06SMiquel Raynal 			break;
3757*a430fa06SMiquel Raynal 		}
3758*a430fa06SMiquel Raynal 	}
3759*a430fa06SMiquel Raynal 
3760*a430fa06SMiquel Raynal 	chip->onfi_version = 0;
3761*a430fa06SMiquel Raynal 	if (!type->name || !type->pagesize) {
3762*a430fa06SMiquel Raynal 		/* Check if the chip is ONFI compliant */
3763*a430fa06SMiquel Raynal 		if (nand_flash_detect_onfi(mtd, chip, &busw))
3764*a430fa06SMiquel Raynal 			goto ident_done;
3765*a430fa06SMiquel Raynal 
3766*a430fa06SMiquel Raynal 		/* Check if the chip is JEDEC compliant */
3767*a430fa06SMiquel Raynal 		if (nand_flash_detect_jedec(mtd, chip, &busw))
3768*a430fa06SMiquel Raynal 			goto ident_done;
3769*a430fa06SMiquel Raynal 	}
3770*a430fa06SMiquel Raynal 
3771*a430fa06SMiquel Raynal 	if (!type->name)
3772*a430fa06SMiquel Raynal 		return ERR_PTR(-ENODEV);
3773*a430fa06SMiquel Raynal 
3774*a430fa06SMiquel Raynal 	if (!mtd->name)
3775*a430fa06SMiquel Raynal 		mtd->name = type->name;
3776*a430fa06SMiquel Raynal 
3777*a430fa06SMiquel Raynal 	chip->chipsize = (uint64_t)type->chipsize << 20;
3778*a430fa06SMiquel Raynal 
3779*a430fa06SMiquel Raynal 	if (!type->pagesize) {
3780*a430fa06SMiquel Raynal 		/* Decode parameters from extended ID */
3781*a430fa06SMiquel Raynal 		nand_decode_ext_id(mtd, chip, id_data, &busw);
3782*a430fa06SMiquel Raynal 	} else {
3783*a430fa06SMiquel Raynal 		nand_decode_id(mtd, chip, type, id_data, &busw);
3784*a430fa06SMiquel Raynal 	}
3785*a430fa06SMiquel Raynal 	/* Get chip options */
3786*a430fa06SMiquel Raynal 	chip->options |= type->options;
3787*a430fa06SMiquel Raynal 
3788*a430fa06SMiquel Raynal 	/*
3789*a430fa06SMiquel Raynal 	 * Check if chip is not a Samsung device. Do not clear the
3790*a430fa06SMiquel Raynal 	 * options for chips which do not have an extended id.
3791*a430fa06SMiquel Raynal 	 */
3792*a430fa06SMiquel Raynal 	if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3793*a430fa06SMiquel Raynal 		chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3794*a430fa06SMiquel Raynal ident_done:
3795*a430fa06SMiquel Raynal 
3796*a430fa06SMiquel Raynal 	/* Try to identify manufacturer */
3797*a430fa06SMiquel Raynal 	for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
3798*a430fa06SMiquel Raynal 		if (nand_manuf_ids[maf_idx].id == *maf_id)
3799*a430fa06SMiquel Raynal 			break;
3800*a430fa06SMiquel Raynal 	}
3801*a430fa06SMiquel Raynal 
3802*a430fa06SMiquel Raynal 	if (chip->options & NAND_BUSWIDTH_AUTO) {
3803*a430fa06SMiquel Raynal 		WARN_ON(chip->options & NAND_BUSWIDTH_16);
3804*a430fa06SMiquel Raynal 		chip->options |= busw;
3805*a430fa06SMiquel Raynal 		nand_set_defaults(chip, busw);
3806*a430fa06SMiquel Raynal 	} else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3807*a430fa06SMiquel Raynal 		/*
3808*a430fa06SMiquel Raynal 		 * Check, if buswidth is correct. Hardware drivers should set
3809*a430fa06SMiquel Raynal 		 * chip correct!
3810*a430fa06SMiquel Raynal 		 */
3811*a430fa06SMiquel Raynal 		pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3812*a430fa06SMiquel Raynal 			*maf_id, *dev_id);
3813*a430fa06SMiquel Raynal 		pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3814*a430fa06SMiquel Raynal 		pr_warn("bus width %d instead %d bit\n",
3815*a430fa06SMiquel Raynal 			   (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3816*a430fa06SMiquel Raynal 			   busw ? 16 : 8);
3817*a430fa06SMiquel Raynal 		return ERR_PTR(-EINVAL);
3818*a430fa06SMiquel Raynal 	}
3819*a430fa06SMiquel Raynal 
3820*a430fa06SMiquel Raynal 	nand_decode_bbm_options(mtd, chip, id_data);
3821*a430fa06SMiquel Raynal 
3822*a430fa06SMiquel Raynal 	/* Calculate the address shift from the page size */
3823*a430fa06SMiquel Raynal 	chip->page_shift = ffs(mtd->writesize) - 1;
3824*a430fa06SMiquel Raynal 	/* Convert chipsize to number of pages per chip -1 */
3825*a430fa06SMiquel Raynal 	chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
3826*a430fa06SMiquel Raynal 
3827*a430fa06SMiquel Raynal 	chip->bbt_erase_shift = chip->phys_erase_shift =
3828*a430fa06SMiquel Raynal 		ffs(mtd->erasesize) - 1;
3829*a430fa06SMiquel Raynal 	if (chip->chipsize & 0xffffffff)
3830*a430fa06SMiquel Raynal 		chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
3831*a430fa06SMiquel Raynal 	else {
3832*a430fa06SMiquel Raynal 		chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3833*a430fa06SMiquel Raynal 		chip->chip_shift += 32 - 1;
3834*a430fa06SMiquel Raynal 	}
3835*a430fa06SMiquel Raynal 
3836*a430fa06SMiquel Raynal 	if (chip->chip_shift - chip->page_shift > 16)
3837*a430fa06SMiquel Raynal 		chip->options |= NAND_ROW_ADDR_3;
3838*a430fa06SMiquel Raynal 
3839*a430fa06SMiquel Raynal 	chip->badblockbits = 8;
3840*a430fa06SMiquel Raynal 	chip->erase = single_erase;
3841*a430fa06SMiquel Raynal 
3842*a430fa06SMiquel Raynal 	/* Do not replace user supplied command function! */
3843*a430fa06SMiquel Raynal 	if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3844*a430fa06SMiquel Raynal 		chip->cmdfunc = nand_command_lp;
3845*a430fa06SMiquel Raynal 
3846*a430fa06SMiquel Raynal 	pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3847*a430fa06SMiquel Raynal 		*maf_id, *dev_id);
3848*a430fa06SMiquel Raynal 
3849*a430fa06SMiquel Raynal #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3850*a430fa06SMiquel Raynal 	if (chip->onfi_version)
3851*a430fa06SMiquel Raynal 		pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3852*a430fa06SMiquel Raynal 				chip->onfi_params.model);
3853*a430fa06SMiquel Raynal 	else if (chip->jedec_version)
3854*a430fa06SMiquel Raynal 		pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3855*a430fa06SMiquel Raynal 				chip->jedec_params.model);
3856*a430fa06SMiquel Raynal 	else
3857*a430fa06SMiquel Raynal 		pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3858*a430fa06SMiquel Raynal 				type->name);
3859*a430fa06SMiquel Raynal #else
3860*a430fa06SMiquel Raynal 	if (chip->jedec_version)
3861*a430fa06SMiquel Raynal 		pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3862*a430fa06SMiquel Raynal 				chip->jedec_params.model);
3863*a430fa06SMiquel Raynal 	else
3864*a430fa06SMiquel Raynal 		pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3865*a430fa06SMiquel Raynal 				type->name);
3866*a430fa06SMiquel Raynal 
3867*a430fa06SMiquel Raynal 	pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3868*a430fa06SMiquel Raynal 		type->name);
3869*a430fa06SMiquel Raynal #endif
3870*a430fa06SMiquel Raynal 
3871*a430fa06SMiquel Raynal 	pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
3872*a430fa06SMiquel Raynal 		(int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
3873*a430fa06SMiquel Raynal 		mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
3874*a430fa06SMiquel Raynal 	return type;
3875*a430fa06SMiquel Raynal }
3876*a430fa06SMiquel Raynal EXPORT_SYMBOL(nand_get_flash_type);
3877*a430fa06SMiquel Raynal 
3878*a430fa06SMiquel Raynal #if CONFIG_IS_ENABLED(OF_CONTROL)
3879*a430fa06SMiquel Raynal DECLARE_GLOBAL_DATA_PTR;
3880*a430fa06SMiquel Raynal 
3881*a430fa06SMiquel Raynal static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
3882*a430fa06SMiquel Raynal {
3883*a430fa06SMiquel Raynal 	int ret, ecc_mode = -1, ecc_strength, ecc_step;
3884*a430fa06SMiquel Raynal 	const void *blob = gd->fdt_blob;
3885*a430fa06SMiquel Raynal 	const char *str;
3886*a430fa06SMiquel Raynal 
3887*a430fa06SMiquel Raynal 	ret = fdtdec_get_int(blob, node, "nand-bus-width", -1);
3888*a430fa06SMiquel Raynal 	if (ret == 16)
3889*a430fa06SMiquel Raynal 		chip->options |= NAND_BUSWIDTH_16;
3890*a430fa06SMiquel Raynal 
3891*a430fa06SMiquel Raynal 	if (fdtdec_get_bool(blob, node, "nand-on-flash-bbt"))
3892*a430fa06SMiquel Raynal 		chip->bbt_options |= NAND_BBT_USE_FLASH;
3893*a430fa06SMiquel Raynal 
3894*a430fa06SMiquel Raynal 	str = fdt_getprop(blob, node, "nand-ecc-mode", NULL);
3895*a430fa06SMiquel Raynal 	if (str) {
3896*a430fa06SMiquel Raynal 		if (!strcmp(str, "none"))
3897*a430fa06SMiquel Raynal 			ecc_mode = NAND_ECC_NONE;
3898*a430fa06SMiquel Raynal 		else if (!strcmp(str, "soft"))
3899*a430fa06SMiquel Raynal 			ecc_mode = NAND_ECC_SOFT;
3900*a430fa06SMiquel Raynal 		else if (!strcmp(str, "hw"))
3901*a430fa06SMiquel Raynal 			ecc_mode = NAND_ECC_HW;
3902*a430fa06SMiquel Raynal 		else if (!strcmp(str, "hw_syndrome"))
3903*a430fa06SMiquel Raynal 			ecc_mode = NAND_ECC_HW_SYNDROME;
3904*a430fa06SMiquel Raynal 		else if (!strcmp(str, "hw_oob_first"))
3905*a430fa06SMiquel Raynal 			ecc_mode = NAND_ECC_HW_OOB_FIRST;
3906*a430fa06SMiquel Raynal 		else if (!strcmp(str, "soft_bch"))
3907*a430fa06SMiquel Raynal 			ecc_mode = NAND_ECC_SOFT_BCH;
3908*a430fa06SMiquel Raynal 	}
3909*a430fa06SMiquel Raynal 
3910*a430fa06SMiquel Raynal 
3911*a430fa06SMiquel Raynal 	ecc_strength = fdtdec_get_int(blob, node, "nand-ecc-strength", -1);
3912*a430fa06SMiquel Raynal 	ecc_step = fdtdec_get_int(blob, node, "nand-ecc-step-size", -1);
3913*a430fa06SMiquel Raynal 
3914*a430fa06SMiquel Raynal 	if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
3915*a430fa06SMiquel Raynal 	    (!(ecc_step >= 0) && ecc_strength >= 0)) {
3916*a430fa06SMiquel Raynal 		pr_err("must set both strength and step size in DT\n");
3917*a430fa06SMiquel Raynal 		return -EINVAL;
3918*a430fa06SMiquel Raynal 	}
3919*a430fa06SMiquel Raynal 
3920*a430fa06SMiquel Raynal 	if (ecc_mode >= 0)
3921*a430fa06SMiquel Raynal 		chip->ecc.mode = ecc_mode;
3922*a430fa06SMiquel Raynal 
3923*a430fa06SMiquel Raynal 	if (ecc_strength >= 0)
3924*a430fa06SMiquel Raynal 		chip->ecc.strength = ecc_strength;
3925*a430fa06SMiquel Raynal 
3926*a430fa06SMiquel Raynal 	if (ecc_step > 0)
3927*a430fa06SMiquel Raynal 		chip->ecc.size = ecc_step;
3928*a430fa06SMiquel Raynal 
3929*a430fa06SMiquel Raynal 	if (fdt_getprop(blob, node, "nand-ecc-maximize", NULL))
3930*a430fa06SMiquel Raynal 		chip->ecc.options |= NAND_ECC_MAXIMIZE;
3931*a430fa06SMiquel Raynal 
3932*a430fa06SMiquel Raynal 	return 0;
3933*a430fa06SMiquel Raynal }
3934*a430fa06SMiquel Raynal #else
3935*a430fa06SMiquel Raynal static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
3936*a430fa06SMiquel Raynal {
3937*a430fa06SMiquel Raynal 	return 0;
3938*a430fa06SMiquel Raynal }
3939*a430fa06SMiquel Raynal #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
3940*a430fa06SMiquel Raynal 
3941*a430fa06SMiquel Raynal /**
3942*a430fa06SMiquel Raynal  * nand_scan_ident - [NAND Interface] Scan for the NAND device
3943*a430fa06SMiquel Raynal  * @mtd: MTD device structure
3944*a430fa06SMiquel Raynal  * @maxchips: number of chips to scan for
3945*a430fa06SMiquel Raynal  * @table: alternative NAND ID table
3946*a430fa06SMiquel Raynal  *
3947*a430fa06SMiquel Raynal  * This is the first phase of the normal nand_scan() function. It reads the
3948*a430fa06SMiquel Raynal  * flash ID and sets up MTD fields accordingly.
3949*a430fa06SMiquel Raynal  *
3950*a430fa06SMiquel Raynal  */
3951*a430fa06SMiquel Raynal int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3952*a430fa06SMiquel Raynal 		    struct nand_flash_dev *table)
3953*a430fa06SMiquel Raynal {
3954*a430fa06SMiquel Raynal 	int i, nand_maf_id, nand_dev_id;
3955*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
3956*a430fa06SMiquel Raynal 	struct nand_flash_dev *type;
3957*a430fa06SMiquel Raynal 	int ret;
3958*a430fa06SMiquel Raynal 
3959*a430fa06SMiquel Raynal 	if (chip->flash_node) {
3960*a430fa06SMiquel Raynal 		ret = nand_dt_init(mtd, chip, chip->flash_node);
3961*a430fa06SMiquel Raynal 		if (ret)
3962*a430fa06SMiquel Raynal 			return ret;
3963*a430fa06SMiquel Raynal 	}
3964*a430fa06SMiquel Raynal 
3965*a430fa06SMiquel Raynal 	/* Set the default functions */
3966*a430fa06SMiquel Raynal 	nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
3967*a430fa06SMiquel Raynal 
3968*a430fa06SMiquel Raynal 	/* Read the flash type */
3969*a430fa06SMiquel Raynal 	type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3970*a430fa06SMiquel Raynal 				   &nand_dev_id, table);
3971*a430fa06SMiquel Raynal 
3972*a430fa06SMiquel Raynal 	if (IS_ERR(type)) {
3973*a430fa06SMiquel Raynal 		if (!(chip->options & NAND_SCAN_SILENT_NODEV))
3974*a430fa06SMiquel Raynal 			pr_warn("No NAND device found\n");
3975*a430fa06SMiquel Raynal 		chip->select_chip(mtd, -1);
3976*a430fa06SMiquel Raynal 		return PTR_ERR(type);
3977*a430fa06SMiquel Raynal 	}
3978*a430fa06SMiquel Raynal 
3979*a430fa06SMiquel Raynal 	/* Initialize the ->data_interface field. */
3980*a430fa06SMiquel Raynal 	ret = nand_init_data_interface(chip);
3981*a430fa06SMiquel Raynal 	if (ret)
3982*a430fa06SMiquel Raynal 		return ret;
3983*a430fa06SMiquel Raynal 
3984*a430fa06SMiquel Raynal 	/*
3985*a430fa06SMiquel Raynal 	 * Setup the data interface correctly on the chip and controller side.
3986*a430fa06SMiquel Raynal 	 * This explicit call to nand_setup_data_interface() is only required
3987*a430fa06SMiquel Raynal 	 * for the first die, because nand_reset() has been called before
3988*a430fa06SMiquel Raynal 	 * ->data_interface and ->default_onfi_timing_mode were set.
3989*a430fa06SMiquel Raynal 	 * For the other dies, nand_reset() will automatically switch to the
3990*a430fa06SMiquel Raynal 	 * best mode for us.
3991*a430fa06SMiquel Raynal 	 */
3992*a430fa06SMiquel Raynal 	ret = nand_setup_data_interface(chip, 0);
3993*a430fa06SMiquel Raynal 	if (ret)
3994*a430fa06SMiquel Raynal 		return ret;
3995*a430fa06SMiquel Raynal 
3996*a430fa06SMiquel Raynal 	chip->select_chip(mtd, -1);
3997*a430fa06SMiquel Raynal 
3998*a430fa06SMiquel Raynal 	/* Check for a chip array */
3999*a430fa06SMiquel Raynal 	for (i = 1; i < maxchips; i++) {
4000*a430fa06SMiquel Raynal 		/* See comment in nand_get_flash_type for reset */
4001*a430fa06SMiquel Raynal 		nand_reset(chip, i);
4002*a430fa06SMiquel Raynal 
4003*a430fa06SMiquel Raynal 		chip->select_chip(mtd, i);
4004*a430fa06SMiquel Raynal 		/* Send the command for reading device ID */
4005*a430fa06SMiquel Raynal 		chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4006*a430fa06SMiquel Raynal 		/* Read manufacturer and device IDs */
4007*a430fa06SMiquel Raynal 		if (nand_maf_id != chip->read_byte(mtd) ||
4008*a430fa06SMiquel Raynal 		    nand_dev_id != chip->read_byte(mtd)) {
4009*a430fa06SMiquel Raynal 			chip->select_chip(mtd, -1);
4010*a430fa06SMiquel Raynal 			break;
4011*a430fa06SMiquel Raynal 		}
4012*a430fa06SMiquel Raynal 		chip->select_chip(mtd, -1);
4013*a430fa06SMiquel Raynal 	}
4014*a430fa06SMiquel Raynal 
4015*a430fa06SMiquel Raynal #ifdef DEBUG
4016*a430fa06SMiquel Raynal 	if (i > 1)
4017*a430fa06SMiquel Raynal 		pr_info("%d chips detected\n", i);
4018*a430fa06SMiquel Raynal #endif
4019*a430fa06SMiquel Raynal 
4020*a430fa06SMiquel Raynal 	/* Store the number of chips and calc total size for mtd */
4021*a430fa06SMiquel Raynal 	chip->numchips = i;
4022*a430fa06SMiquel Raynal 	mtd->size = i * chip->chipsize;
4023*a430fa06SMiquel Raynal 
4024*a430fa06SMiquel Raynal 	return 0;
4025*a430fa06SMiquel Raynal }
4026*a430fa06SMiquel Raynal EXPORT_SYMBOL(nand_scan_ident);
4027*a430fa06SMiquel Raynal 
4028*a430fa06SMiquel Raynal /**
4029*a430fa06SMiquel Raynal  * nand_check_ecc_caps - check the sanity of preset ECC settings
4030*a430fa06SMiquel Raynal  * @chip: nand chip info structure
4031*a430fa06SMiquel Raynal  * @caps: ECC caps info structure
4032*a430fa06SMiquel Raynal  * @oobavail: OOB size that the ECC engine can use
4033*a430fa06SMiquel Raynal  *
4034*a430fa06SMiquel Raynal  * When ECC step size and strength are already set, check if they are supported
4035*a430fa06SMiquel Raynal  * by the controller and the calculated ECC bytes fit within the chip's OOB.
4036*a430fa06SMiquel Raynal  * On success, the calculated ECC bytes is set.
4037*a430fa06SMiquel Raynal  */
4038*a430fa06SMiquel Raynal int nand_check_ecc_caps(struct nand_chip *chip,
4039*a430fa06SMiquel Raynal 			const struct nand_ecc_caps *caps, int oobavail)
4040*a430fa06SMiquel Raynal {
4041*a430fa06SMiquel Raynal 	struct mtd_info *mtd = nand_to_mtd(chip);
4042*a430fa06SMiquel Raynal 	const struct nand_ecc_step_info *stepinfo;
4043*a430fa06SMiquel Raynal 	int preset_step = chip->ecc.size;
4044*a430fa06SMiquel Raynal 	int preset_strength = chip->ecc.strength;
4045*a430fa06SMiquel Raynal 	int nsteps, ecc_bytes;
4046*a430fa06SMiquel Raynal 	int i, j;
4047*a430fa06SMiquel Raynal 
4048*a430fa06SMiquel Raynal 	if (WARN_ON(oobavail < 0))
4049*a430fa06SMiquel Raynal 		return -EINVAL;
4050*a430fa06SMiquel Raynal 
4051*a430fa06SMiquel Raynal 	if (!preset_step || !preset_strength)
4052*a430fa06SMiquel Raynal 		return -ENODATA;
4053*a430fa06SMiquel Raynal 
4054*a430fa06SMiquel Raynal 	nsteps = mtd->writesize / preset_step;
4055*a430fa06SMiquel Raynal 
4056*a430fa06SMiquel Raynal 	for (i = 0; i < caps->nstepinfos; i++) {
4057*a430fa06SMiquel Raynal 		stepinfo = &caps->stepinfos[i];
4058*a430fa06SMiquel Raynal 
4059*a430fa06SMiquel Raynal 		if (stepinfo->stepsize != preset_step)
4060*a430fa06SMiquel Raynal 			continue;
4061*a430fa06SMiquel Raynal 
4062*a430fa06SMiquel Raynal 		for (j = 0; j < stepinfo->nstrengths; j++) {
4063*a430fa06SMiquel Raynal 			if (stepinfo->strengths[j] != preset_strength)
4064*a430fa06SMiquel Raynal 				continue;
4065*a430fa06SMiquel Raynal 
4066*a430fa06SMiquel Raynal 			ecc_bytes = caps->calc_ecc_bytes(preset_step,
4067*a430fa06SMiquel Raynal 							 preset_strength);
4068*a430fa06SMiquel Raynal 			if (WARN_ON_ONCE(ecc_bytes < 0))
4069*a430fa06SMiquel Raynal 				return ecc_bytes;
4070*a430fa06SMiquel Raynal 
4071*a430fa06SMiquel Raynal 			if (ecc_bytes * nsteps > oobavail) {
4072*a430fa06SMiquel Raynal 				pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4073*a430fa06SMiquel Raynal 				       preset_step, preset_strength);
4074*a430fa06SMiquel Raynal 				return -ENOSPC;
4075*a430fa06SMiquel Raynal 			}
4076*a430fa06SMiquel Raynal 
4077*a430fa06SMiquel Raynal 			chip->ecc.bytes = ecc_bytes;
4078*a430fa06SMiquel Raynal 
4079*a430fa06SMiquel Raynal 			return 0;
4080*a430fa06SMiquel Raynal 		}
4081*a430fa06SMiquel Raynal 	}
4082*a430fa06SMiquel Raynal 
4083*a430fa06SMiquel Raynal 	pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4084*a430fa06SMiquel Raynal 	       preset_step, preset_strength);
4085*a430fa06SMiquel Raynal 
4086*a430fa06SMiquel Raynal 	return -ENOTSUPP;
4087*a430fa06SMiquel Raynal }
4088*a430fa06SMiquel Raynal EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4089*a430fa06SMiquel Raynal 
4090*a430fa06SMiquel Raynal /**
4091*a430fa06SMiquel Raynal  * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4092*a430fa06SMiquel Raynal  * @chip: nand chip info structure
4093*a430fa06SMiquel Raynal  * @caps: ECC engine caps info structure
4094*a430fa06SMiquel Raynal  * @oobavail: OOB size that the ECC engine can use
4095*a430fa06SMiquel Raynal  *
4096*a430fa06SMiquel Raynal  * If a chip's ECC requirement is provided, try to meet it with the least
4097*a430fa06SMiquel Raynal  * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4098*a430fa06SMiquel Raynal  * On success, the chosen ECC settings are set.
4099*a430fa06SMiquel Raynal  */
4100*a430fa06SMiquel Raynal int nand_match_ecc_req(struct nand_chip *chip,
4101*a430fa06SMiquel Raynal 		       const struct nand_ecc_caps *caps, int oobavail)
4102*a430fa06SMiquel Raynal {
4103*a430fa06SMiquel Raynal 	struct mtd_info *mtd = nand_to_mtd(chip);
4104*a430fa06SMiquel Raynal 	const struct nand_ecc_step_info *stepinfo;
4105*a430fa06SMiquel Raynal 	int req_step = chip->ecc_step_ds;
4106*a430fa06SMiquel Raynal 	int req_strength = chip->ecc_strength_ds;
4107*a430fa06SMiquel Raynal 	int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4108*a430fa06SMiquel Raynal 	int best_step, best_strength, best_ecc_bytes;
4109*a430fa06SMiquel Raynal 	int best_ecc_bytes_total = INT_MAX;
4110*a430fa06SMiquel Raynal 	int i, j;
4111*a430fa06SMiquel Raynal 
4112*a430fa06SMiquel Raynal 	if (WARN_ON(oobavail < 0))
4113*a430fa06SMiquel Raynal 		return -EINVAL;
4114*a430fa06SMiquel Raynal 
4115*a430fa06SMiquel Raynal 	/* No information provided by the NAND chip */
4116*a430fa06SMiquel Raynal 	if (!req_step || !req_strength)
4117*a430fa06SMiquel Raynal 		return -ENOTSUPP;
4118*a430fa06SMiquel Raynal 
4119*a430fa06SMiquel Raynal 	/* number of correctable bits the chip requires in a page */
4120*a430fa06SMiquel Raynal 	req_corr = mtd->writesize / req_step * req_strength;
4121*a430fa06SMiquel Raynal 
4122*a430fa06SMiquel Raynal 	for (i = 0; i < caps->nstepinfos; i++) {
4123*a430fa06SMiquel Raynal 		stepinfo = &caps->stepinfos[i];
4124*a430fa06SMiquel Raynal 		step_size = stepinfo->stepsize;
4125*a430fa06SMiquel Raynal 
4126*a430fa06SMiquel Raynal 		for (j = 0; j < stepinfo->nstrengths; j++) {
4127*a430fa06SMiquel Raynal 			strength = stepinfo->strengths[j];
4128*a430fa06SMiquel Raynal 
4129*a430fa06SMiquel Raynal 			/*
4130*a430fa06SMiquel Raynal 			 * If both step size and strength are smaller than the
4131*a430fa06SMiquel Raynal 			 * chip's requirement, it is not easy to compare the
4132*a430fa06SMiquel Raynal 			 * resulted reliability.
4133*a430fa06SMiquel Raynal 			 */
4134*a430fa06SMiquel Raynal 			if (step_size < req_step && strength < req_strength)
4135*a430fa06SMiquel Raynal 				continue;
4136*a430fa06SMiquel Raynal 
4137*a430fa06SMiquel Raynal 			if (mtd->writesize % step_size)
4138*a430fa06SMiquel Raynal 				continue;
4139*a430fa06SMiquel Raynal 
4140*a430fa06SMiquel Raynal 			nsteps = mtd->writesize / step_size;
4141*a430fa06SMiquel Raynal 
4142*a430fa06SMiquel Raynal 			ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4143*a430fa06SMiquel Raynal 			if (WARN_ON_ONCE(ecc_bytes < 0))
4144*a430fa06SMiquel Raynal 				continue;
4145*a430fa06SMiquel Raynal 			ecc_bytes_total = ecc_bytes * nsteps;
4146*a430fa06SMiquel Raynal 
4147*a430fa06SMiquel Raynal 			if (ecc_bytes_total > oobavail ||
4148*a430fa06SMiquel Raynal 			    strength * nsteps < req_corr)
4149*a430fa06SMiquel Raynal 				continue;
4150*a430fa06SMiquel Raynal 
4151*a430fa06SMiquel Raynal 			/*
4152*a430fa06SMiquel Raynal 			 * We assume the best is to meet the chip's requrement
4153*a430fa06SMiquel Raynal 			 * with the least number of ECC bytes.
4154*a430fa06SMiquel Raynal 			 */
4155*a430fa06SMiquel Raynal 			if (ecc_bytes_total < best_ecc_bytes_total) {
4156*a430fa06SMiquel Raynal 				best_ecc_bytes_total = ecc_bytes_total;
4157*a430fa06SMiquel Raynal 				best_step = step_size;
4158*a430fa06SMiquel Raynal 				best_strength = strength;
4159*a430fa06SMiquel Raynal 				best_ecc_bytes = ecc_bytes;
4160*a430fa06SMiquel Raynal 			}
4161*a430fa06SMiquel Raynal 		}
4162*a430fa06SMiquel Raynal 	}
4163*a430fa06SMiquel Raynal 
4164*a430fa06SMiquel Raynal 	if (best_ecc_bytes_total == INT_MAX)
4165*a430fa06SMiquel Raynal 		return -ENOTSUPP;
4166*a430fa06SMiquel Raynal 
4167*a430fa06SMiquel Raynal 	chip->ecc.size = best_step;
4168*a430fa06SMiquel Raynal 	chip->ecc.strength = best_strength;
4169*a430fa06SMiquel Raynal 	chip->ecc.bytes = best_ecc_bytes;
4170*a430fa06SMiquel Raynal 
4171*a430fa06SMiquel Raynal 	return 0;
4172*a430fa06SMiquel Raynal }
4173*a430fa06SMiquel Raynal EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4174*a430fa06SMiquel Raynal 
4175*a430fa06SMiquel Raynal /**
4176*a430fa06SMiquel Raynal  * nand_maximize_ecc - choose the max ECC strength available
4177*a430fa06SMiquel Raynal  * @chip: nand chip info structure
4178*a430fa06SMiquel Raynal  * @caps: ECC engine caps info structure
4179*a430fa06SMiquel Raynal  * @oobavail: OOB size that the ECC engine can use
4180*a430fa06SMiquel Raynal  *
4181*a430fa06SMiquel Raynal  * Choose the max ECC strength that is supported on the controller, and can fit
4182*a430fa06SMiquel Raynal  * within the chip's OOB.  On success, the chosen ECC settings are set.
4183*a430fa06SMiquel Raynal  */
4184*a430fa06SMiquel Raynal int nand_maximize_ecc(struct nand_chip *chip,
4185*a430fa06SMiquel Raynal 		      const struct nand_ecc_caps *caps, int oobavail)
4186*a430fa06SMiquel Raynal {
4187*a430fa06SMiquel Raynal 	struct mtd_info *mtd = nand_to_mtd(chip);
4188*a430fa06SMiquel Raynal 	const struct nand_ecc_step_info *stepinfo;
4189*a430fa06SMiquel Raynal 	int step_size, strength, nsteps, ecc_bytes, corr;
4190*a430fa06SMiquel Raynal 	int best_corr = 0;
4191*a430fa06SMiquel Raynal 	int best_step = 0;
4192*a430fa06SMiquel Raynal 	int best_strength, best_ecc_bytes;
4193*a430fa06SMiquel Raynal 	int i, j;
4194*a430fa06SMiquel Raynal 
4195*a430fa06SMiquel Raynal 	if (WARN_ON(oobavail < 0))
4196*a430fa06SMiquel Raynal 		return -EINVAL;
4197*a430fa06SMiquel Raynal 
4198*a430fa06SMiquel Raynal 	for (i = 0; i < caps->nstepinfos; i++) {
4199*a430fa06SMiquel Raynal 		stepinfo = &caps->stepinfos[i];
4200*a430fa06SMiquel Raynal 		step_size = stepinfo->stepsize;
4201*a430fa06SMiquel Raynal 
4202*a430fa06SMiquel Raynal 		/* If chip->ecc.size is already set, respect it */
4203*a430fa06SMiquel Raynal 		if (chip->ecc.size && step_size != chip->ecc.size)
4204*a430fa06SMiquel Raynal 			continue;
4205*a430fa06SMiquel Raynal 
4206*a430fa06SMiquel Raynal 		for (j = 0; j < stepinfo->nstrengths; j++) {
4207*a430fa06SMiquel Raynal 			strength = stepinfo->strengths[j];
4208*a430fa06SMiquel Raynal 
4209*a430fa06SMiquel Raynal 			if (mtd->writesize % step_size)
4210*a430fa06SMiquel Raynal 				continue;
4211*a430fa06SMiquel Raynal 
4212*a430fa06SMiquel Raynal 			nsteps = mtd->writesize / step_size;
4213*a430fa06SMiquel Raynal 
4214*a430fa06SMiquel Raynal 			ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4215*a430fa06SMiquel Raynal 			if (WARN_ON_ONCE(ecc_bytes < 0))
4216*a430fa06SMiquel Raynal 				continue;
4217*a430fa06SMiquel Raynal 
4218*a430fa06SMiquel Raynal 			if (ecc_bytes * nsteps > oobavail)
4219*a430fa06SMiquel Raynal 				continue;
4220*a430fa06SMiquel Raynal 
4221*a430fa06SMiquel Raynal 			corr = strength * nsteps;
4222*a430fa06SMiquel Raynal 
4223*a430fa06SMiquel Raynal 			/*
4224*a430fa06SMiquel Raynal 			 * If the number of correctable bits is the same,
4225*a430fa06SMiquel Raynal 			 * bigger step_size has more reliability.
4226*a430fa06SMiquel Raynal 			 */
4227*a430fa06SMiquel Raynal 			if (corr > best_corr ||
4228*a430fa06SMiquel Raynal 			    (corr == best_corr && step_size > best_step)) {
4229*a430fa06SMiquel Raynal 				best_corr = corr;
4230*a430fa06SMiquel Raynal 				best_step = step_size;
4231*a430fa06SMiquel Raynal 				best_strength = strength;
4232*a430fa06SMiquel Raynal 				best_ecc_bytes = ecc_bytes;
4233*a430fa06SMiquel Raynal 			}
4234*a430fa06SMiquel Raynal 		}
4235*a430fa06SMiquel Raynal 	}
4236*a430fa06SMiquel Raynal 
4237*a430fa06SMiquel Raynal 	if (!best_corr)
4238*a430fa06SMiquel Raynal 		return -ENOTSUPP;
4239*a430fa06SMiquel Raynal 
4240*a430fa06SMiquel Raynal 	chip->ecc.size = best_step;
4241*a430fa06SMiquel Raynal 	chip->ecc.strength = best_strength;
4242*a430fa06SMiquel Raynal 	chip->ecc.bytes = best_ecc_bytes;
4243*a430fa06SMiquel Raynal 
4244*a430fa06SMiquel Raynal 	return 0;
4245*a430fa06SMiquel Raynal }
4246*a430fa06SMiquel Raynal EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4247*a430fa06SMiquel Raynal 
4248*a430fa06SMiquel Raynal /*
4249*a430fa06SMiquel Raynal  * Check if the chip configuration meet the datasheet requirements.
4250*a430fa06SMiquel Raynal 
4251*a430fa06SMiquel Raynal  * If our configuration corrects A bits per B bytes and the minimum
4252*a430fa06SMiquel Raynal  * required correction level is X bits per Y bytes, then we must ensure
4253*a430fa06SMiquel Raynal  * both of the following are true:
4254*a430fa06SMiquel Raynal  *
4255*a430fa06SMiquel Raynal  * (1) A / B >= X / Y
4256*a430fa06SMiquel Raynal  * (2) A >= X
4257*a430fa06SMiquel Raynal  *
4258*a430fa06SMiquel Raynal  * Requirement (1) ensures we can correct for the required bitflip density.
4259*a430fa06SMiquel Raynal  * Requirement (2) ensures we can correct even when all bitflips are clumped
4260*a430fa06SMiquel Raynal  * in the same sector.
4261*a430fa06SMiquel Raynal  */
4262*a430fa06SMiquel Raynal static bool nand_ecc_strength_good(struct mtd_info *mtd)
4263*a430fa06SMiquel Raynal {
4264*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
4265*a430fa06SMiquel Raynal 	struct nand_ecc_ctrl *ecc = &chip->ecc;
4266*a430fa06SMiquel Raynal 	int corr, ds_corr;
4267*a430fa06SMiquel Raynal 
4268*a430fa06SMiquel Raynal 	if (ecc->size == 0 || chip->ecc_step_ds == 0)
4269*a430fa06SMiquel Raynal 		/* Not enough information */
4270*a430fa06SMiquel Raynal 		return true;
4271*a430fa06SMiquel Raynal 
4272*a430fa06SMiquel Raynal 	/*
4273*a430fa06SMiquel Raynal 	 * We get the number of corrected bits per page to compare
4274*a430fa06SMiquel Raynal 	 * the correction density.
4275*a430fa06SMiquel Raynal 	 */
4276*a430fa06SMiquel Raynal 	corr = (mtd->writesize * ecc->strength) / ecc->size;
4277*a430fa06SMiquel Raynal 	ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4278*a430fa06SMiquel Raynal 
4279*a430fa06SMiquel Raynal 	return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4280*a430fa06SMiquel Raynal }
4281*a430fa06SMiquel Raynal 
4282*a430fa06SMiquel Raynal static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4283*a430fa06SMiquel Raynal {
4284*a430fa06SMiquel Raynal 	struct nand_ecc_ctrl *ecc = &chip->ecc;
4285*a430fa06SMiquel Raynal 
4286*a430fa06SMiquel Raynal 	if (nand_standard_page_accessors(ecc))
4287*a430fa06SMiquel Raynal 		return false;
4288*a430fa06SMiquel Raynal 
4289*a430fa06SMiquel Raynal 	/*
4290*a430fa06SMiquel Raynal 	 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4291*a430fa06SMiquel Raynal 	 * controller driver implements all the page accessors because
4292*a430fa06SMiquel Raynal 	 * default helpers are not suitable when the core does not
4293*a430fa06SMiquel Raynal 	 * send the READ0/PAGEPROG commands.
4294*a430fa06SMiquel Raynal 	 */
4295*a430fa06SMiquel Raynal 	return (!ecc->read_page || !ecc->write_page ||
4296*a430fa06SMiquel Raynal 		!ecc->read_page_raw || !ecc->write_page_raw ||
4297*a430fa06SMiquel Raynal 		(NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4298*a430fa06SMiquel Raynal 		(NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4299*a430fa06SMiquel Raynal 		 ecc->hwctl && ecc->calculate));
4300*a430fa06SMiquel Raynal }
4301*a430fa06SMiquel Raynal 
4302*a430fa06SMiquel Raynal /**
4303*a430fa06SMiquel Raynal  * nand_scan_tail - [NAND Interface] Scan for the NAND device
4304*a430fa06SMiquel Raynal  * @mtd: MTD device structure
4305*a430fa06SMiquel Raynal  *
4306*a430fa06SMiquel Raynal  * This is the second phase of the normal nand_scan() function. It fills out
4307*a430fa06SMiquel Raynal  * all the uninitialized function pointers with the defaults and scans for a
4308*a430fa06SMiquel Raynal  * bad block table if appropriate.
4309*a430fa06SMiquel Raynal  */
4310*a430fa06SMiquel Raynal int nand_scan_tail(struct mtd_info *mtd)
4311*a430fa06SMiquel Raynal {
4312*a430fa06SMiquel Raynal 	int i;
4313*a430fa06SMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
4314*a430fa06SMiquel Raynal 	struct nand_ecc_ctrl *ecc = &chip->ecc;
4315*a430fa06SMiquel Raynal 	struct nand_buffers *nbuf;
4316*a430fa06SMiquel Raynal 
4317*a430fa06SMiquel Raynal 	/* New bad blocks should be marked in OOB, flash-based BBT, or both */
4318*a430fa06SMiquel Raynal 	BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4319*a430fa06SMiquel Raynal 			!(chip->bbt_options & NAND_BBT_USE_FLASH));
4320*a430fa06SMiquel Raynal 
4321*a430fa06SMiquel Raynal 	if (invalid_ecc_page_accessors(chip)) {
4322*a430fa06SMiquel Raynal 		pr_err("Invalid ECC page accessors setup\n");
4323*a430fa06SMiquel Raynal 		return -EINVAL;
4324*a430fa06SMiquel Raynal 	}
4325*a430fa06SMiquel Raynal 
4326*a430fa06SMiquel Raynal 	if (!(chip->options & NAND_OWN_BUFFERS)) {
4327*a430fa06SMiquel Raynal 		nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
4328*a430fa06SMiquel Raynal 		chip->buffers = nbuf;
4329*a430fa06SMiquel Raynal 	} else {
4330*a430fa06SMiquel Raynal 		if (!chip->buffers)
4331*a430fa06SMiquel Raynal 			return -ENOMEM;
4332*a430fa06SMiquel Raynal 	}
4333*a430fa06SMiquel Raynal 
4334*a430fa06SMiquel Raynal 	/* Set the internal oob buffer location, just after the page data */
4335*a430fa06SMiquel Raynal 	chip->oob_poi = chip->buffers->databuf + mtd->writesize;
4336*a430fa06SMiquel Raynal 
4337*a430fa06SMiquel Raynal 	/*
4338*a430fa06SMiquel Raynal 	 * If no default placement scheme is given, select an appropriate one.
4339*a430fa06SMiquel Raynal 	 */
4340*a430fa06SMiquel Raynal 	if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
4341*a430fa06SMiquel Raynal 		switch (mtd->oobsize) {
4342*a430fa06SMiquel Raynal 		case 8:
4343*a430fa06SMiquel Raynal 			ecc->layout = &nand_oob_8;
4344*a430fa06SMiquel Raynal 			break;
4345*a430fa06SMiquel Raynal 		case 16:
4346*a430fa06SMiquel Raynal 			ecc->layout = &nand_oob_16;
4347*a430fa06SMiquel Raynal 			break;
4348*a430fa06SMiquel Raynal 		case 64:
4349*a430fa06SMiquel Raynal 			ecc->layout = &nand_oob_64;
4350*a430fa06SMiquel Raynal 			break;
4351*a430fa06SMiquel Raynal 		case 128:
4352*a430fa06SMiquel Raynal 			ecc->layout = &nand_oob_128;
4353*a430fa06SMiquel Raynal 			break;
4354*a430fa06SMiquel Raynal 		default:
4355*a430fa06SMiquel Raynal 			pr_warn("No oob scheme defined for oobsize %d\n",
4356*a430fa06SMiquel Raynal 				   mtd->oobsize);
4357*a430fa06SMiquel Raynal 			BUG();
4358*a430fa06SMiquel Raynal 		}
4359*a430fa06SMiquel Raynal 	}
4360*a430fa06SMiquel Raynal 
4361*a430fa06SMiquel Raynal 	if (!chip->write_page)
4362*a430fa06SMiquel Raynal 		chip->write_page = nand_write_page;
4363*a430fa06SMiquel Raynal 
4364*a430fa06SMiquel Raynal 	/*
4365*a430fa06SMiquel Raynal 	 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
4366*a430fa06SMiquel Raynal 	 * selected and we have 256 byte pagesize fallback to software ECC
4367*a430fa06SMiquel Raynal 	 */
4368*a430fa06SMiquel Raynal 
4369*a430fa06SMiquel Raynal 	switch (ecc->mode) {
4370*a430fa06SMiquel Raynal 	case NAND_ECC_HW_OOB_FIRST:
4371*a430fa06SMiquel Raynal 		/* Similar to NAND_ECC_HW, but a separate read_page handle */
4372*a430fa06SMiquel Raynal 		if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
4373*a430fa06SMiquel Raynal 			pr_warn("No ECC functions supplied; hardware ECC not possible\n");
4374*a430fa06SMiquel Raynal 			BUG();
4375*a430fa06SMiquel Raynal 		}
4376*a430fa06SMiquel Raynal 		if (!ecc->read_page)
4377*a430fa06SMiquel Raynal 			ecc->read_page = nand_read_page_hwecc_oob_first;
4378*a430fa06SMiquel Raynal 
4379*a430fa06SMiquel Raynal 	case NAND_ECC_HW:
4380*a430fa06SMiquel Raynal 		/* Use standard hwecc read page function? */
4381*a430fa06SMiquel Raynal 		if (!ecc->read_page)
4382*a430fa06SMiquel Raynal 			ecc->read_page = nand_read_page_hwecc;
4383*a430fa06SMiquel Raynal 		if (!ecc->write_page)
4384*a430fa06SMiquel Raynal 			ecc->write_page = nand_write_page_hwecc;
4385*a430fa06SMiquel Raynal 		if (!ecc->read_page_raw)
4386*a430fa06SMiquel Raynal 			ecc->read_page_raw = nand_read_page_raw;
4387*a430fa06SMiquel Raynal 		if (!ecc->write_page_raw)
4388*a430fa06SMiquel Raynal 			ecc->write_page_raw = nand_write_page_raw;
4389*a430fa06SMiquel Raynal 		if (!ecc->read_oob)
4390*a430fa06SMiquel Raynal 			ecc->read_oob = nand_read_oob_std;
4391*a430fa06SMiquel Raynal 		if (!ecc->write_oob)
4392*a430fa06SMiquel Raynal 			ecc->write_oob = nand_write_oob_std;
4393*a430fa06SMiquel Raynal 		if (!ecc->read_subpage)
4394*a430fa06SMiquel Raynal 			ecc->read_subpage = nand_read_subpage;
4395*a430fa06SMiquel Raynal 		if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
4396*a430fa06SMiquel Raynal 			ecc->write_subpage = nand_write_subpage_hwecc;
4397*a430fa06SMiquel Raynal 
4398*a430fa06SMiquel Raynal 	case NAND_ECC_HW_SYNDROME:
4399*a430fa06SMiquel Raynal 		if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4400*a430fa06SMiquel Raynal 		    (!ecc->read_page ||
4401*a430fa06SMiquel Raynal 		     ecc->read_page == nand_read_page_hwecc ||
4402*a430fa06SMiquel Raynal 		     !ecc->write_page ||
4403*a430fa06SMiquel Raynal 		     ecc->write_page == nand_write_page_hwecc)) {
4404*a430fa06SMiquel Raynal 			pr_warn("No ECC functions supplied; hardware ECC not possible\n");
4405*a430fa06SMiquel Raynal 			BUG();
4406*a430fa06SMiquel Raynal 		}
4407*a430fa06SMiquel Raynal 		/* Use standard syndrome read/write page function? */
4408*a430fa06SMiquel Raynal 		if (!ecc->read_page)
4409*a430fa06SMiquel Raynal 			ecc->read_page = nand_read_page_syndrome;
4410*a430fa06SMiquel Raynal 		if (!ecc->write_page)
4411*a430fa06SMiquel Raynal 			ecc->write_page = nand_write_page_syndrome;
4412*a430fa06SMiquel Raynal 		if (!ecc->read_page_raw)
4413*a430fa06SMiquel Raynal 			ecc->read_page_raw = nand_read_page_raw_syndrome;
4414*a430fa06SMiquel Raynal 		if (!ecc->write_page_raw)
4415*a430fa06SMiquel Raynal 			ecc->write_page_raw = nand_write_page_raw_syndrome;
4416*a430fa06SMiquel Raynal 		if (!ecc->read_oob)
4417*a430fa06SMiquel Raynal 			ecc->read_oob = nand_read_oob_syndrome;
4418*a430fa06SMiquel Raynal 		if (!ecc->write_oob)
4419*a430fa06SMiquel Raynal 			ecc->write_oob = nand_write_oob_syndrome;
4420*a430fa06SMiquel Raynal 
4421*a430fa06SMiquel Raynal 		if (mtd->writesize >= ecc->size) {
4422*a430fa06SMiquel Raynal 			if (!ecc->strength) {
4423*a430fa06SMiquel Raynal 				pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4424*a430fa06SMiquel Raynal 				BUG();
4425*a430fa06SMiquel Raynal 			}
4426*a430fa06SMiquel Raynal 			break;
4427*a430fa06SMiquel Raynal 		}
4428*a430fa06SMiquel Raynal 		pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4429*a430fa06SMiquel Raynal 			ecc->size, mtd->writesize);
4430*a430fa06SMiquel Raynal 		ecc->mode = NAND_ECC_SOFT;
4431*a430fa06SMiquel Raynal 
4432*a430fa06SMiquel Raynal 	case NAND_ECC_SOFT:
4433*a430fa06SMiquel Raynal 		ecc->calculate = nand_calculate_ecc;
4434*a430fa06SMiquel Raynal 		ecc->correct = nand_correct_data;
4435*a430fa06SMiquel Raynal 		ecc->read_page = nand_read_page_swecc;
4436*a430fa06SMiquel Raynal 		ecc->read_subpage = nand_read_subpage;
4437*a430fa06SMiquel Raynal 		ecc->write_page = nand_write_page_swecc;
4438*a430fa06SMiquel Raynal 		ecc->read_page_raw = nand_read_page_raw;
4439*a430fa06SMiquel Raynal 		ecc->write_page_raw = nand_write_page_raw;
4440*a430fa06SMiquel Raynal 		ecc->read_oob = nand_read_oob_std;
4441*a430fa06SMiquel Raynal 		ecc->write_oob = nand_write_oob_std;
4442*a430fa06SMiquel Raynal 		if (!ecc->size)
4443*a430fa06SMiquel Raynal 			ecc->size = 256;
4444*a430fa06SMiquel Raynal 		ecc->bytes = 3;
4445*a430fa06SMiquel Raynal 		ecc->strength = 1;
4446*a430fa06SMiquel Raynal 		break;
4447*a430fa06SMiquel Raynal 
4448*a430fa06SMiquel Raynal 	case NAND_ECC_SOFT_BCH:
4449*a430fa06SMiquel Raynal 		if (!mtd_nand_has_bch()) {
4450*a430fa06SMiquel Raynal 			pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4451*a430fa06SMiquel Raynal 			BUG();
4452*a430fa06SMiquel Raynal 		}
4453*a430fa06SMiquel Raynal 		ecc->calculate = nand_bch_calculate_ecc;
4454*a430fa06SMiquel Raynal 		ecc->correct = nand_bch_correct_data;
4455*a430fa06SMiquel Raynal 		ecc->read_page = nand_read_page_swecc;
4456*a430fa06SMiquel Raynal 		ecc->read_subpage = nand_read_subpage;
4457*a430fa06SMiquel Raynal 		ecc->write_page = nand_write_page_swecc;
4458*a430fa06SMiquel Raynal 		ecc->read_page_raw = nand_read_page_raw;
4459*a430fa06SMiquel Raynal 		ecc->write_page_raw = nand_write_page_raw;
4460*a430fa06SMiquel Raynal 		ecc->read_oob = nand_read_oob_std;
4461*a430fa06SMiquel Raynal 		ecc->write_oob = nand_write_oob_std;
4462*a430fa06SMiquel Raynal 		/*
4463*a430fa06SMiquel Raynal 		 * Board driver should supply ecc.size and ecc.strength values
4464*a430fa06SMiquel Raynal 		 * to select how many bits are correctable. Otherwise, default
4465*a430fa06SMiquel Raynal 		 * to 4 bits for large page devices.
4466*a430fa06SMiquel Raynal 		 */
4467*a430fa06SMiquel Raynal 		if (!ecc->size && (mtd->oobsize >= 64)) {
4468*a430fa06SMiquel Raynal 			ecc->size = 512;
4469*a430fa06SMiquel Raynal 			ecc->strength = 4;
4470*a430fa06SMiquel Raynal 		}
4471*a430fa06SMiquel Raynal 
4472*a430fa06SMiquel Raynal 		/* See nand_bch_init() for details. */
4473*a430fa06SMiquel Raynal 		ecc->bytes = 0;
4474*a430fa06SMiquel Raynal 		ecc->priv = nand_bch_init(mtd);
4475*a430fa06SMiquel Raynal 		if (!ecc->priv) {
4476*a430fa06SMiquel Raynal 			pr_warn("BCH ECC initialization failed!\n");
4477*a430fa06SMiquel Raynal 			BUG();
4478*a430fa06SMiquel Raynal 		}
4479*a430fa06SMiquel Raynal 		break;
4480*a430fa06SMiquel Raynal 
4481*a430fa06SMiquel Raynal 	case NAND_ECC_NONE:
4482*a430fa06SMiquel Raynal 		pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
4483*a430fa06SMiquel Raynal 		ecc->read_page = nand_read_page_raw;
4484*a430fa06SMiquel Raynal 		ecc->write_page = nand_write_page_raw;
4485*a430fa06SMiquel Raynal 		ecc->read_oob = nand_read_oob_std;
4486*a430fa06SMiquel Raynal 		ecc->read_page_raw = nand_read_page_raw;
4487*a430fa06SMiquel Raynal 		ecc->write_page_raw = nand_write_page_raw;
4488*a430fa06SMiquel Raynal 		ecc->write_oob = nand_write_oob_std;
4489*a430fa06SMiquel Raynal 		ecc->size = mtd->writesize;
4490*a430fa06SMiquel Raynal 		ecc->bytes = 0;
4491*a430fa06SMiquel Raynal 		ecc->strength = 0;
4492*a430fa06SMiquel Raynal 		break;
4493*a430fa06SMiquel Raynal 
4494*a430fa06SMiquel Raynal 	default:
4495*a430fa06SMiquel Raynal 		pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
4496*a430fa06SMiquel Raynal 		BUG();
4497*a430fa06SMiquel Raynal 	}
4498*a430fa06SMiquel Raynal 
4499*a430fa06SMiquel Raynal 	/* For many systems, the standard OOB write also works for raw */
4500*a430fa06SMiquel Raynal 	if (!ecc->read_oob_raw)
4501*a430fa06SMiquel Raynal 		ecc->read_oob_raw = ecc->read_oob;
4502*a430fa06SMiquel Raynal 	if (!ecc->write_oob_raw)
4503*a430fa06SMiquel Raynal 		ecc->write_oob_raw = ecc->write_oob;
4504*a430fa06SMiquel Raynal 
4505*a430fa06SMiquel Raynal 	/*
4506*a430fa06SMiquel Raynal 	 * The number of bytes available for a client to place data into
4507*a430fa06SMiquel Raynal 	 * the out of band area.
4508*a430fa06SMiquel Raynal 	 */
4509*a430fa06SMiquel Raynal 	mtd->oobavail = 0;
4510*a430fa06SMiquel Raynal 	if (ecc->layout) {
4511*a430fa06SMiquel Raynal 		for (i = 0; ecc->layout->oobfree[i].length; i++)
4512*a430fa06SMiquel Raynal 			mtd->oobavail += ecc->layout->oobfree[i].length;
4513*a430fa06SMiquel Raynal 	}
4514*a430fa06SMiquel Raynal 
4515*a430fa06SMiquel Raynal 	/* ECC sanity check: warn if it's too weak */
4516*a430fa06SMiquel Raynal 	if (!nand_ecc_strength_good(mtd))
4517*a430fa06SMiquel Raynal 		pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4518*a430fa06SMiquel Raynal 			mtd->name);
4519*a430fa06SMiquel Raynal 
4520*a430fa06SMiquel Raynal 	/*
4521*a430fa06SMiquel Raynal 	 * Set the number of read / write steps for one page depending on ECC
4522*a430fa06SMiquel Raynal 	 * mode.
4523*a430fa06SMiquel Raynal 	 */
4524*a430fa06SMiquel Raynal 	ecc->steps = mtd->writesize / ecc->size;
4525*a430fa06SMiquel Raynal 	if (ecc->steps * ecc->size != mtd->writesize) {
4526*a430fa06SMiquel Raynal 		pr_warn("Invalid ECC parameters\n");
4527*a430fa06SMiquel Raynal 		BUG();
4528*a430fa06SMiquel Raynal 	}
4529*a430fa06SMiquel Raynal 	ecc->total = ecc->steps * ecc->bytes;
4530*a430fa06SMiquel Raynal 
4531*a430fa06SMiquel Raynal 	/* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
4532*a430fa06SMiquel Raynal 	if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
4533*a430fa06SMiquel Raynal 		switch (ecc->steps) {
4534*a430fa06SMiquel Raynal 		case 2:
4535*a430fa06SMiquel Raynal 			mtd->subpage_sft = 1;
4536*a430fa06SMiquel Raynal 			break;
4537*a430fa06SMiquel Raynal 		case 4:
4538*a430fa06SMiquel Raynal 		case 8:
4539*a430fa06SMiquel Raynal 		case 16:
4540*a430fa06SMiquel Raynal 			mtd->subpage_sft = 2;
4541*a430fa06SMiquel Raynal 			break;
4542*a430fa06SMiquel Raynal 		}
4543*a430fa06SMiquel Raynal 	}
4544*a430fa06SMiquel Raynal 	chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4545*a430fa06SMiquel Raynal 
4546*a430fa06SMiquel Raynal 	/* Initialize state */
4547*a430fa06SMiquel Raynal 	chip->state = FL_READY;
4548*a430fa06SMiquel Raynal 
4549*a430fa06SMiquel Raynal 	/* Invalidate the pagebuffer reference */
4550*a430fa06SMiquel Raynal 	chip->pagebuf = -1;
4551*a430fa06SMiquel Raynal 
4552*a430fa06SMiquel Raynal 	/* Large page NAND with SOFT_ECC should support subpage reads */
4553*a430fa06SMiquel Raynal 	switch (ecc->mode) {
4554*a430fa06SMiquel Raynal 	case NAND_ECC_SOFT:
4555*a430fa06SMiquel Raynal 	case NAND_ECC_SOFT_BCH:
4556*a430fa06SMiquel Raynal 		if (chip->page_shift > 9)
4557*a430fa06SMiquel Raynal 			chip->options |= NAND_SUBPAGE_READ;
4558*a430fa06SMiquel Raynal 		break;
4559*a430fa06SMiquel Raynal 
4560*a430fa06SMiquel Raynal 	default:
4561*a430fa06SMiquel Raynal 		break;
4562*a430fa06SMiquel Raynal 	}
4563*a430fa06SMiquel Raynal 
4564*a430fa06SMiquel Raynal 	/* Fill in remaining MTD driver data */
4565*a430fa06SMiquel Raynal 	mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
4566*a430fa06SMiquel Raynal 	mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4567*a430fa06SMiquel Raynal 						MTD_CAP_NANDFLASH;
4568*a430fa06SMiquel Raynal 	mtd->_erase = nand_erase;
4569*a430fa06SMiquel Raynal 	mtd->_panic_write = panic_nand_write;
4570*a430fa06SMiquel Raynal 	mtd->_read_oob = nand_read_oob;
4571*a430fa06SMiquel Raynal 	mtd->_write_oob = nand_write_oob;
4572*a430fa06SMiquel Raynal 	mtd->_sync = nand_sync;
4573*a430fa06SMiquel Raynal 	mtd->_lock = NULL;
4574*a430fa06SMiquel Raynal 	mtd->_unlock = NULL;
4575*a430fa06SMiquel Raynal 	mtd->_block_isreserved = nand_block_isreserved;
4576*a430fa06SMiquel Raynal 	mtd->_block_isbad = nand_block_isbad;
4577*a430fa06SMiquel Raynal 	mtd->_block_markbad = nand_block_markbad;
4578*a430fa06SMiquel Raynal 	mtd->writebufsize = mtd->writesize;
4579*a430fa06SMiquel Raynal 
4580*a430fa06SMiquel Raynal 	/* propagate ecc info to mtd_info */
4581*a430fa06SMiquel Raynal 	mtd->ecclayout = ecc->layout;
4582*a430fa06SMiquel Raynal 	mtd->ecc_strength = ecc->strength;
4583*a430fa06SMiquel Raynal 	mtd->ecc_step_size = ecc->size;
4584*a430fa06SMiquel Raynal 	/*
4585*a430fa06SMiquel Raynal 	 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4586*a430fa06SMiquel Raynal 	 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4587*a430fa06SMiquel Raynal 	 * properly set.
4588*a430fa06SMiquel Raynal 	 */
4589*a430fa06SMiquel Raynal 	if (!mtd->bitflip_threshold)
4590*a430fa06SMiquel Raynal 		mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
4591*a430fa06SMiquel Raynal 
4592*a430fa06SMiquel Raynal 	return 0;
4593*a430fa06SMiquel Raynal }
4594*a430fa06SMiquel Raynal EXPORT_SYMBOL(nand_scan_tail);
4595*a430fa06SMiquel Raynal 
4596*a430fa06SMiquel Raynal /**
4597*a430fa06SMiquel Raynal  * nand_scan - [NAND Interface] Scan for the NAND device
4598*a430fa06SMiquel Raynal  * @mtd: MTD device structure
4599*a430fa06SMiquel Raynal  * @maxchips: number of chips to scan for
4600*a430fa06SMiquel Raynal  *
4601*a430fa06SMiquel Raynal  * This fills out all the uninitialized function pointers with the defaults.
4602*a430fa06SMiquel Raynal  * The flash ID is read and the mtd/chip structures are filled with the
4603*a430fa06SMiquel Raynal  * appropriate values.
4604*a430fa06SMiquel Raynal  */
4605*a430fa06SMiquel Raynal int nand_scan(struct mtd_info *mtd, int maxchips)
4606*a430fa06SMiquel Raynal {
4607*a430fa06SMiquel Raynal 	int ret;
4608*a430fa06SMiquel Raynal 
4609*a430fa06SMiquel Raynal 	ret = nand_scan_ident(mtd, maxchips, NULL);
4610*a430fa06SMiquel Raynal 	if (!ret)
4611*a430fa06SMiquel Raynal 		ret = nand_scan_tail(mtd);
4612*a430fa06SMiquel Raynal 	return ret;
4613*a430fa06SMiquel Raynal }
4614*a430fa06SMiquel Raynal EXPORT_SYMBOL(nand_scan);
4615*a430fa06SMiquel Raynal 
4616*a430fa06SMiquel Raynal MODULE_LICENSE("GPL");
4617*a430fa06SMiquel Raynal MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4618*a430fa06SMiquel Raynal MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
4619*a430fa06SMiquel Raynal MODULE_DESCRIPTION("Generic NAND flash driver code");
4620