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