3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
6 * Additional technical information is available on
7 * http://www.linux-mtd.infradead.org/doc/nand.html
9 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
10 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
13 * David Woodhouse for adding multichip support
15 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
19 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
21 * if we have HW ECC support.
22 * BBT table is not serialized, has to be fixed
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/module.h>
33 #include <linux/delay.h>
34 #include <linux/errno.h>
35 #include <linux/err.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
39 #include <linux/nmi.h>
40 #include <linux/types.h>
41 #include <linux/mtd/mtd.h>
42 #include <linux/mtd/rawnand.h>
43 #include <linux/mtd/nand_ecc.h>
44 #include <linux/mtd/nand_bch.h>
45 #include <linux/interrupt.h>
46 #include <linux/bitops.h>
48 #include <linux/mtd/partitions.h>
51 static int nand_get_device(struct mtd_info *mtd, int new_state);
53 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
54 struct mtd_oob_ops *ops);
56 /* Define default oob placement schemes for large and small page devices */
57 static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
58 struct mtd_oob_region *oobregion)
60 struct nand_chip *chip = mtd_to_nand(mtd);
61 struct nand_ecc_ctrl *ecc = &chip->ecc;
67 oobregion->offset = 0;
68 if (mtd->oobsize == 16)
69 oobregion->length = 4;
71 oobregion->length = 3;
73 if (mtd->oobsize == 8)
76 oobregion->offset = 6;
77 oobregion->length = ecc->total - 4;
83 static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
84 struct mtd_oob_region *oobregion)
89 if (mtd->oobsize == 16) {
93 oobregion->length = 8;
94 oobregion->offset = 8;
96 oobregion->length = 2;
98 oobregion->offset = 3;
100 oobregion->offset = 6;
106 const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
107 .ecc = nand_ooblayout_ecc_sp,
108 .free = nand_ooblayout_free_sp,
110 EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
112 static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
113 struct mtd_oob_region *oobregion)
115 struct nand_chip *chip = mtd_to_nand(mtd);
116 struct nand_ecc_ctrl *ecc = &chip->ecc;
118 if (section || !ecc->total)
121 oobregion->length = ecc->total;
122 oobregion->offset = mtd->oobsize - oobregion->length;
127 static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
128 struct mtd_oob_region *oobregion)
130 struct nand_chip *chip = mtd_to_nand(mtd);
131 struct nand_ecc_ctrl *ecc = &chip->ecc;
136 oobregion->length = mtd->oobsize - ecc->total - 2;
137 oobregion->offset = 2;
142 const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
143 .ecc = nand_ooblayout_ecc_lp,
144 .free = nand_ooblayout_free_lp,
146 EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
149 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
150 * are placed at a fixed offset.
152 static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
153 struct mtd_oob_region *oobregion)
155 struct nand_chip *chip = mtd_to_nand(mtd);
156 struct nand_ecc_ctrl *ecc = &chip->ecc;
161 switch (mtd->oobsize) {
163 oobregion->offset = 40;
166 oobregion->offset = 80;
172 oobregion->length = ecc->total;
173 if (oobregion->offset + oobregion->length > mtd->oobsize)
179 static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
180 struct mtd_oob_region *oobregion)
182 struct nand_chip *chip = mtd_to_nand(mtd);
183 struct nand_ecc_ctrl *ecc = &chip->ecc;
186 if (section < 0 || section > 1)
189 switch (mtd->oobsize) {
201 oobregion->offset = 2;
202 oobregion->length = ecc_offset - 2;
204 oobregion->offset = ecc_offset + ecc->total;
205 oobregion->length = mtd->oobsize - oobregion->offset;
211 static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
212 .ecc = nand_ooblayout_ecc_lp_hamming,
213 .free = nand_ooblayout_free_lp_hamming,
216 static int check_offs_len(struct mtd_info *mtd,
217 loff_t ofs, uint64_t len)
219 struct nand_chip *chip = mtd_to_nand(mtd);
222 /* Start address must align on block boundary */
223 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
224 pr_debug("%s: unaligned address\n", __func__);
228 /* Length must align on block boundary */
229 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
230 pr_debug("%s: length not block aligned\n", __func__);
238 * nand_release_device - [GENERIC] release chip
239 * @mtd: MTD device structure
241 * Release chip lock and wake up anyone waiting on the device.
243 static void nand_release_device(struct mtd_info *mtd)
245 struct nand_chip *chip = mtd_to_nand(mtd);
247 /* Release the controller and the chip */
248 spin_lock(&chip->controller->lock);
249 chip->controller->active = NULL;
250 chip->state = FL_READY;
251 wake_up(&chip->controller->wq);
252 spin_unlock(&chip->controller->lock);
256 * nand_read_byte - [DEFAULT] read one byte from the chip
257 * @mtd: MTD device structure
259 * Default read function for 8bit buswidth
261 static uint8_t nand_read_byte(struct mtd_info *mtd)
263 struct nand_chip *chip = mtd_to_nand(mtd);
264 return readb(chip->IO_ADDR_R);
268 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
269 * @mtd: MTD device structure
271 * Default read function for 16bit buswidth with endianness conversion.
274 static uint8_t nand_read_byte16(struct mtd_info *mtd)
276 struct nand_chip *chip = mtd_to_nand(mtd);
277 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
281 * nand_read_word - [DEFAULT] read one word from the chip
282 * @mtd: MTD device structure
284 * Default read function for 16bit buswidth without endianness conversion.
286 static u16 nand_read_word(struct mtd_info *mtd)
288 struct nand_chip *chip = mtd_to_nand(mtd);
289 return readw(chip->IO_ADDR_R);
293 * nand_select_chip - [DEFAULT] control CE line
294 * @mtd: MTD device structure
295 * @chipnr: chipnumber to select, -1 for deselect
297 * Default select function for 1 chip devices.
299 static void nand_select_chip(struct mtd_info *mtd, int chipnr)
301 struct nand_chip *chip = mtd_to_nand(mtd);
305 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
316 * nand_write_byte - [DEFAULT] write single byte to chip
317 * @mtd: MTD device structure
318 * @byte: value to write
320 * Default function to write a byte to I/O[7:0]
322 static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
324 struct nand_chip *chip = mtd_to_nand(mtd);
326 chip->write_buf(mtd, &byte, 1);
330 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
331 * @mtd: MTD device structure
332 * @byte: value to write
334 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
336 static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
338 struct nand_chip *chip = mtd_to_nand(mtd);
339 uint16_t word = byte;
342 * It's not entirely clear what should happen to I/O[15:8] when writing
343 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
345 * When the host supports a 16-bit bus width, only data is
346 * transferred at the 16-bit width. All address and command line
347 * transfers shall use only the lower 8-bits of the data bus. During
348 * command transfers, the host may place any value on the upper
349 * 8-bits of the data bus. During address transfers, the host shall
350 * set the upper 8-bits of the data bus to 00h.
352 * One user of the write_byte callback is nand_set_features. The
353 * four parameters are specified to be written to I/O[7:0], but this is
354 * neither an address nor a command transfer. Let's assume a 0 on the
355 * upper I/O lines is OK.
357 chip->write_buf(mtd, (uint8_t *)&word, 2);
361 * nand_write_buf - [DEFAULT] write buffer to chip
362 * @mtd: MTD device structure
364 * @len: number of bytes to write
366 * Default write function for 8bit buswidth.
368 static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
370 struct nand_chip *chip = mtd_to_nand(mtd);
372 iowrite8_rep(chip->IO_ADDR_W, buf, len);
376 * nand_read_buf - [DEFAULT] read chip data into buffer
377 * @mtd: MTD device structure
378 * @buf: buffer to store date
379 * @len: number of bytes to read
381 * Default read function for 8bit buswidth.
383 static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
385 struct nand_chip *chip = mtd_to_nand(mtd);
387 ioread8_rep(chip->IO_ADDR_R, buf, len);
391 * nand_write_buf16 - [DEFAULT] write buffer to chip
392 * @mtd: MTD device structure
394 * @len: number of bytes to write
396 * Default write function for 16bit buswidth.
398 static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
400 struct nand_chip *chip = mtd_to_nand(mtd);
401 u16 *p = (u16 *) buf;
403 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
407 * nand_read_buf16 - [DEFAULT] read chip data into buffer
408 * @mtd: MTD device structure
409 * @buf: buffer to store date
410 * @len: number of bytes to read
412 * Default read function for 16bit buswidth.
414 static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
416 struct nand_chip *chip = mtd_to_nand(mtd);
417 u16 *p = (u16 *) buf;
419 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
423 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
424 * @mtd: MTD device structure
425 * @ofs: offset from device start
427 * Check, if the block is bad.
429 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
431 int page, page_end, res;
432 struct nand_chip *chip = mtd_to_nand(mtd);
435 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
436 ofs += mtd->erasesize - mtd->writesize;
438 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
439 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
441 for (; page < page_end; page++) {
442 res = chip->ecc.read_oob(mtd, chip, page);
446 bad = chip->oob_poi[chip->badblockpos];
448 if (likely(chip->badblockbits == 8))
451 res = hweight8(bad) < chip->badblockbits;
460 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
461 * @mtd: MTD device structure
462 * @ofs: offset from device start
464 * This is the default implementation, which can be overridden by a hardware
465 * specific driver. It provides the details for writing a bad block marker to a
468 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
470 struct nand_chip *chip = mtd_to_nand(mtd);
471 struct mtd_oob_ops ops;
472 uint8_t buf[2] = { 0, 0 };
473 int ret = 0, res, i = 0;
475 memset(&ops, 0, sizeof(ops));
477 ops.ooboffs = chip->badblockpos;
478 if (chip->options & NAND_BUSWIDTH_16) {
479 ops.ooboffs &= ~0x01;
480 ops.len = ops.ooblen = 2;
482 ops.len = ops.ooblen = 1;
484 ops.mode = MTD_OPS_PLACE_OOB;
486 /* Write to first/last page(s) if necessary */
487 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
488 ofs += mtd->erasesize - mtd->writesize;
490 res = nand_do_write_oob(mtd, ofs, &ops);
495 ofs += mtd->writesize;
496 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
502 * nand_block_markbad_lowlevel - mark a block bad
503 * @mtd: MTD device structure
504 * @ofs: offset from device start
506 * This function performs the generic NAND bad block marking steps (i.e., bad
507 * block table(s) and/or marker(s)). We only allow the hardware driver to
508 * specify how to write bad block markers to OOB (chip->block_markbad).
510 * We try operations in the following order:
512 * (1) erase the affected block, to allow OOB marker to be written cleanly
513 * (2) write bad block marker to OOB area of affected block (unless flag
514 * NAND_BBT_NO_OOB_BBM is present)
517 * Note that we retain the first error encountered in (2) or (3), finish the
518 * procedures, and dump the error in the end.
520 static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
522 struct nand_chip *chip = mtd_to_nand(mtd);
525 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
526 struct erase_info einfo;
528 /* Attempt erase before marking OOB */
529 memset(&einfo, 0, sizeof(einfo));
531 einfo.len = 1ULL << chip->phys_erase_shift;
532 nand_erase_nand(mtd, &einfo, 0);
534 /* Write bad block marker to OOB */
535 nand_get_device(mtd, FL_WRITING);
536 ret = chip->block_markbad(mtd, ofs);
537 nand_release_device(mtd);
540 /* Mark block bad in BBT */
542 res = nand_markbad_bbt(mtd, ofs);
548 mtd->ecc_stats.badblocks++;
554 * nand_check_wp - [GENERIC] check if the chip is write protected
555 * @mtd: MTD device structure
557 * Check, if the device is write protected. The function expects, that the
558 * device is already selected.
560 static int nand_check_wp(struct mtd_info *mtd)
562 struct nand_chip *chip = mtd_to_nand(mtd);
566 /* Broken xD cards report WP despite being writable */
567 if (chip->options & NAND_BROKEN_XD)
570 /* Check the WP bit */
571 ret = nand_status_op(chip, &status);
575 return status & NAND_STATUS_WP ? 0 : 1;
579 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
580 * @mtd: MTD device structure
581 * @ofs: offset from device start
583 * Check if the block is marked as reserved.
585 static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
587 struct nand_chip *chip = mtd_to_nand(mtd);
591 /* Return info from the table */
592 return nand_isreserved_bbt(mtd, ofs);
596 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
597 * @mtd: MTD device structure
598 * @ofs: offset from device start
599 * @allowbbt: 1, if its allowed to access the bbt area
601 * Check, if the block is bad. Either by reading the bad block table or
602 * calling of the scan function.
604 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
606 struct nand_chip *chip = mtd_to_nand(mtd);
609 return chip->block_bad(mtd, ofs);
611 /* Return info from the table */
612 return nand_isbad_bbt(mtd, ofs, allowbbt);
616 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
617 * @mtd: MTD device structure
620 * Helper function for nand_wait_ready used when needing to wait in interrupt
623 static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
625 struct nand_chip *chip = mtd_to_nand(mtd);
628 /* Wait for the device to get ready */
629 for (i = 0; i < timeo; i++) {
630 if (chip->dev_ready(mtd))
632 touch_softlockup_watchdog();
638 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
639 * @mtd: MTD device structure
641 * Wait for the ready pin after a command, and warn if a timeout occurs.
643 void nand_wait_ready(struct mtd_info *mtd)
645 struct nand_chip *chip = mtd_to_nand(mtd);
646 unsigned long timeo = 400;
648 if (in_interrupt() || oops_in_progress)
649 return panic_nand_wait_ready(mtd, timeo);
651 /* Wait until command is processed or timeout occurs */
652 timeo = jiffies + msecs_to_jiffies(timeo);
654 if (chip->dev_ready(mtd))
657 } while (time_before(jiffies, timeo));
659 if (!chip->dev_ready(mtd))
660 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
662 EXPORT_SYMBOL_GPL(nand_wait_ready);
665 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
666 * @mtd: MTD device structure
667 * @timeo: Timeout in ms
669 * Wait for status ready (i.e. command done) or timeout.
671 static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
673 register struct nand_chip *chip = mtd_to_nand(mtd);
676 timeo = jiffies + msecs_to_jiffies(timeo);
680 ret = nand_read_data_op(chip, &status, sizeof(status), true);
684 if (status & NAND_STATUS_READY)
686 touch_softlockup_watchdog();
687 } while (time_before(jiffies, timeo));
691 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
692 * @chip: NAND chip structure
693 * @timeout_ms: Timeout in ms
695 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
696 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
699 * This helper is intended to be used when the controller does not have access
700 * to the NAND R/B pin.
702 * Be aware that calling this helper from an ->exec_op() implementation means
703 * ->exec_op() must be re-entrant.
705 * Return 0 if the NAND chip is ready, a negative error otherwise.
707 int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
715 ret = nand_status_op(chip, NULL);
719 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
721 ret = nand_read_data_op(chip, &status, sizeof(status), true);
725 if (status & NAND_STATUS_READY)
729 * Typical lowest execution time for a tR on most NANDs is 10us,
730 * use this as polling delay before doing something smarter (ie.
731 * deriving a delay from the timeout value, timeout_ms/ratio).
734 } while (time_before(jiffies, timeout_ms));
737 * We have to exit READ_STATUS mode in order to read real data on the
738 * bus in case the WAITRDY instruction is preceding a DATA_IN
741 nand_exit_status_op(chip);
746 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
748 EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
751 * nand_command - [DEFAULT] Send command to NAND device
752 * @mtd: MTD device structure
753 * @command: the command to be sent
754 * @column: the column address for this command, -1 if none
755 * @page_addr: the page address for this command, -1 if none
757 * Send command to NAND device. This function is used for small page devices
758 * (512 Bytes per page).
760 static void nand_command(struct mtd_info *mtd, unsigned int command,
761 int column, int page_addr)
763 register struct nand_chip *chip = mtd_to_nand(mtd);
764 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
766 /* Write out the command to the device */
767 if (command == NAND_CMD_SEQIN) {
770 if (column >= mtd->writesize) {
772 column -= mtd->writesize;
773 readcmd = NAND_CMD_READOOB;
774 } else if (column < 256) {
775 /* First 256 bytes --> READ0 */
776 readcmd = NAND_CMD_READ0;
779 readcmd = NAND_CMD_READ1;
781 chip->cmd_ctrl(mtd, readcmd, ctrl);
782 ctrl &= ~NAND_CTRL_CHANGE;
784 if (command != NAND_CMD_NONE)
785 chip->cmd_ctrl(mtd, command, ctrl);
787 /* Address cycle, when necessary */
788 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
789 /* Serially input address */
791 /* Adjust columns for 16 bit buswidth */
792 if (chip->options & NAND_BUSWIDTH_16 &&
793 !nand_opcode_8bits(command))
795 chip->cmd_ctrl(mtd, column, ctrl);
796 ctrl &= ~NAND_CTRL_CHANGE;
798 if (page_addr != -1) {
799 chip->cmd_ctrl(mtd, page_addr, ctrl);
800 ctrl &= ~NAND_CTRL_CHANGE;
801 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
802 if (chip->options & NAND_ROW_ADDR_3)
803 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
805 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
808 * Program and erase have their own busy handlers status and sequential
814 case NAND_CMD_PAGEPROG:
815 case NAND_CMD_ERASE1:
816 case NAND_CMD_ERASE2:
818 case NAND_CMD_STATUS:
819 case NAND_CMD_READID:
820 case NAND_CMD_SET_FEATURES:
826 udelay(chip->chip_delay);
827 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
828 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
830 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
831 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
832 nand_wait_status_ready(mtd, 250);
835 /* This applies to read commands */
838 * READ0 is sometimes used to exit GET STATUS mode. When this
839 * is the case no address cycles are requested, and we can use
840 * this information to detect that we should not wait for the
841 * device to be ready.
843 if (column == -1 && page_addr == -1)
848 * If we don't have access to the busy pin, we apply the given
851 if (!chip->dev_ready) {
852 udelay(chip->chip_delay);
857 * Apply this short delay always to ensure that we do wait tWB in
858 * any case on any machine.
862 nand_wait_ready(mtd);
865 static void nand_ccs_delay(struct nand_chip *chip)
868 * The controller already takes care of waiting for tCCS when the RNDIN
869 * or RNDOUT command is sent, return directly.
871 if (!(chip->options & NAND_WAIT_TCCS))
875 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
876 * (which should be safe for all NANDs).
878 if (chip->setup_data_interface)
879 ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
885 * nand_command_lp - [DEFAULT] Send command to NAND large page device
886 * @mtd: MTD device structure
887 * @command: the command to be sent
888 * @column: the column address for this command, -1 if none
889 * @page_addr: the page address for this command, -1 if none
891 * Send command to NAND device. This is the version for the new large page
892 * devices. We don't have the separate regions as we have in the small page
893 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
895 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
896 int column, int page_addr)
898 register struct nand_chip *chip = mtd_to_nand(mtd);
900 /* Emulate NAND_CMD_READOOB */
901 if (command == NAND_CMD_READOOB) {
902 column += mtd->writesize;
903 command = NAND_CMD_READ0;
906 /* Command latch cycle */
907 if (command != NAND_CMD_NONE)
908 chip->cmd_ctrl(mtd, command,
909 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
911 if (column != -1 || page_addr != -1) {
912 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
914 /* Serially input address */
916 /* Adjust columns for 16 bit buswidth */
917 if (chip->options & NAND_BUSWIDTH_16 &&
918 !nand_opcode_8bits(command))
920 chip->cmd_ctrl(mtd, column, ctrl);
921 ctrl &= ~NAND_CTRL_CHANGE;
923 /* Only output a single addr cycle for 8bits opcodes. */
924 if (!nand_opcode_8bits(command))
925 chip->cmd_ctrl(mtd, column >> 8, ctrl);
927 if (page_addr != -1) {
928 chip->cmd_ctrl(mtd, page_addr, ctrl);
929 chip->cmd_ctrl(mtd, page_addr >> 8,
930 NAND_NCE | NAND_ALE);
931 if (chip->options & NAND_ROW_ADDR_3)
932 chip->cmd_ctrl(mtd, page_addr >> 16,
933 NAND_NCE | NAND_ALE);
936 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
939 * Program and erase have their own busy handlers status, sequential
940 * in and status need no delay.
945 case NAND_CMD_CACHEDPROG:
946 case NAND_CMD_PAGEPROG:
947 case NAND_CMD_ERASE1:
948 case NAND_CMD_ERASE2:
950 case NAND_CMD_STATUS:
951 case NAND_CMD_READID:
952 case NAND_CMD_SET_FEATURES:
956 nand_ccs_delay(chip);
962 udelay(chip->chip_delay);
963 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
964 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
965 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
966 NAND_NCE | NAND_CTRL_CHANGE);
967 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
968 nand_wait_status_ready(mtd, 250);
971 case NAND_CMD_RNDOUT:
972 /* No ready / busy check necessary */
973 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
974 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
975 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
976 NAND_NCE | NAND_CTRL_CHANGE);
978 nand_ccs_delay(chip);
983 * READ0 is sometimes used to exit GET STATUS mode. When this
984 * is the case no address cycles are requested, and we can use
985 * this information to detect that READSTART should not be
988 if (column == -1 && page_addr == -1)
991 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
992 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
993 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
994 NAND_NCE | NAND_CTRL_CHANGE);
996 /* This applies to read commands */
999 * If we don't have access to the busy pin, we apply the given
1002 if (!chip->dev_ready) {
1003 udelay(chip->chip_delay);
1009 * Apply this short delay always to ensure that we do wait tWB in
1010 * any case on any machine.
1014 nand_wait_ready(mtd);
1018 * panic_nand_get_device - [GENERIC] Get chip for selected access
1019 * @chip: the nand chip descriptor
1020 * @mtd: MTD device structure
1021 * @new_state: the state which is requested
1023 * Used when in panic, no locks are taken.
1025 static void panic_nand_get_device(struct nand_chip *chip,
1026 struct mtd_info *mtd, int new_state)
1028 /* Hardware controller shared among independent devices */
1029 chip->controller->active = chip;
1030 chip->state = new_state;
1034 * nand_get_device - [GENERIC] Get chip for selected access
1035 * @mtd: MTD device structure
1036 * @new_state: the state which is requested
1038 * Get the device and lock it for exclusive access
1041 nand_get_device(struct mtd_info *mtd, int new_state)
1043 struct nand_chip *chip = mtd_to_nand(mtd);
1044 spinlock_t *lock = &chip->controller->lock;
1045 wait_queue_head_t *wq = &chip->controller->wq;
1046 DECLARE_WAITQUEUE(wait, current);
1050 /* Hardware controller shared among independent devices */
1051 if (!chip->controller->active)
1052 chip->controller->active = chip;
1054 if (chip->controller->active == chip && chip->state == FL_READY) {
1055 chip->state = new_state;
1059 if (new_state == FL_PM_SUSPENDED) {
1060 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1061 chip->state = FL_PM_SUSPENDED;
1066 set_current_state(TASK_UNINTERRUPTIBLE);
1067 add_wait_queue(wq, &wait);
1070 remove_wait_queue(wq, &wait);
1075 * panic_nand_wait - [GENERIC] wait until the command is done
1076 * @mtd: MTD device structure
1077 * @chip: NAND chip structure
1080 * Wait for command done. This is a helper function for nand_wait used when
1081 * we are in interrupt context. May happen when in panic and trying to write
1082 * an oops through mtdoops.
1084 static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1085 unsigned long timeo)
1088 for (i = 0; i < timeo; i++) {
1089 if (chip->dev_ready) {
1090 if (chip->dev_ready(mtd))
1096 ret = nand_read_data_op(chip, &status, sizeof(status),
1101 if (status & NAND_STATUS_READY)
1109 * nand_wait - [DEFAULT] wait until the command is done
1110 * @mtd: MTD device structure
1111 * @chip: NAND chip structure
1113 * Wait for command done. This applies to erase and program only.
1115 static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
1118 unsigned long timeo = 400;
1123 * Apply this short delay always to ensure that we do wait tWB in any
1124 * case on any machine.
1128 ret = nand_status_op(chip, NULL);
1132 if (in_interrupt() || oops_in_progress)
1133 panic_nand_wait(mtd, chip, timeo);
1135 timeo = jiffies + msecs_to_jiffies(timeo);
1137 if (chip->dev_ready) {
1138 if (chip->dev_ready(mtd))
1141 ret = nand_read_data_op(chip, &status,
1142 sizeof(status), true);
1146 if (status & NAND_STATUS_READY)
1150 } while (time_before(jiffies, timeo));
1153 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1157 /* This can happen if in case of timeout or buggy dev_ready */
1158 WARN_ON(!(status & NAND_STATUS_READY));
1162 static bool nand_supports_get_features(struct nand_chip *chip, int addr)
1164 return (chip->parameters.supports_set_get_features &&
1165 test_bit(addr, chip->parameters.get_feature_list));
1168 static bool nand_supports_set_features(struct nand_chip *chip, int addr)
1170 return (chip->parameters.supports_set_get_features &&
1171 test_bit(addr, chip->parameters.set_feature_list));
1175 * nand_get_features - wrapper to perform a GET_FEATURE
1176 * @chip: NAND chip info structure
1177 * @addr: feature address
1178 * @subfeature_param: the subfeature parameters, a four bytes array
1180 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1181 * operation cannot be handled.
1183 int nand_get_features(struct nand_chip *chip, int addr,
1184 u8 *subfeature_param)
1186 struct mtd_info *mtd = nand_to_mtd(chip);
1188 if (!nand_supports_get_features(chip, addr))
1191 return chip->get_features(mtd, chip, addr, subfeature_param);
1193 EXPORT_SYMBOL_GPL(nand_get_features);
1196 * nand_set_features - wrapper to perform a SET_FEATURE
1197 * @chip: NAND chip info structure
1198 * @addr: feature address
1199 * @subfeature_param: the subfeature parameters, a four bytes array
1201 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1202 * operation cannot be handled.
1204 int nand_set_features(struct nand_chip *chip, int addr,
1205 u8 *subfeature_param)
1207 struct mtd_info *mtd = nand_to_mtd(chip);
1209 if (!nand_supports_set_features(chip, addr))
1212 return chip->set_features(mtd, chip, addr, subfeature_param);
1214 EXPORT_SYMBOL_GPL(nand_set_features);
1217 * nand_reset_data_interface - Reset data interface and timings
1218 * @chip: The NAND chip
1219 * @chipnr: Internal die id
1221 * Reset the Data interface and timings to ONFI mode 0.
1223 * Returns 0 for success or negative error code otherwise.
1225 static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
1227 struct mtd_info *mtd = nand_to_mtd(chip);
1230 if (!chip->setup_data_interface)
1234 * The ONFI specification says:
1236 * To transition from NV-DDR or NV-DDR2 to the SDR data
1237 * interface, the host shall use the Reset (FFh) command
1238 * using SDR timing mode 0. A device in any timing mode is
1239 * required to recognize Reset (FFh) command issued in SDR
1243 * Configure the data interface in SDR mode and set the
1244 * timings to timing mode 0.
1247 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
1248 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
1250 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1256 * nand_setup_data_interface - Setup the best data interface and timings
1257 * @chip: The NAND chip
1258 * @chipnr: Internal die id
1260 * Find and configure the best data interface and NAND timings supported by
1261 * the chip and the driver.
1262 * First tries to retrieve supported timing modes from ONFI information,
1263 * and if the NAND chip does not support ONFI, relies on the
1264 * ->onfi_timing_mode_default specified in the nand_ids table.
1266 * Returns 0 for success or negative error code otherwise.
1268 static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
1270 struct mtd_info *mtd = nand_to_mtd(chip);
1271 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1272 chip->onfi_timing_mode_default,
1276 if (!chip->setup_data_interface)
1279 /* Change the mode on the chip side (if supported by the NAND chip) */
1280 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
1281 chip->select_chip(mtd, chipnr);
1282 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1284 chip->select_chip(mtd, -1);
1289 /* Change the mode on the controller side */
1290 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
1294 /* Check the mode has been accepted by the chip, if supported */
1295 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
1298 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
1299 chip->select_chip(mtd, chipnr);
1300 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1302 chip->select_chip(mtd, -1);
1304 goto err_reset_chip;
1306 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1307 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1308 chip->onfi_timing_mode_default);
1309 goto err_reset_chip;
1316 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1319 nand_reset_data_interface(chip, chipnr);
1320 chip->select_chip(mtd, chipnr);
1321 nand_reset_op(chip);
1322 chip->select_chip(mtd, -1);
1328 * nand_init_data_interface - find the best data interface and timings
1329 * @chip: The NAND chip
1331 * Find the best data interface and NAND timings supported by the chip
1333 * First tries to retrieve supported timing modes from ONFI information,
1334 * and if the NAND chip does not support ONFI, relies on the
1335 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1336 * function nand_chip->data_interface is initialized with the best timing mode
1339 * Returns 0 for success or negative error code otherwise.
1341 static int nand_init_data_interface(struct nand_chip *chip)
1343 struct mtd_info *mtd = nand_to_mtd(chip);
1344 int modes, mode, ret;
1346 if (!chip->setup_data_interface)
1350 * First try to identify the best timings from ONFI parameters and
1351 * if the NAND does not support ONFI, fallback to the default ONFI
1354 modes = onfi_get_async_timing_mode(chip);
1355 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1356 if (!chip->onfi_timing_mode_default)
1359 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1363 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1364 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
1369 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1370 * controller supports the requested timings.
1372 ret = chip->setup_data_interface(mtd,
1373 NAND_DATA_IFACE_CHECK_ONLY,
1374 &chip->data_interface);
1376 chip->onfi_timing_mode_default = mode;
1385 * nand_fill_column_cycles - fill the column cycles of an address
1386 * @chip: The NAND chip
1387 * @addrs: Array of address cycles to fill
1388 * @offset_in_page: The offset in the page
1390 * Fills the first or the first two bytes of the @addrs field depending
1391 * on the NAND bus width and the page size.
1393 * Returns the number of cycles needed to encode the column, or a negative
1394 * error code in case one of the arguments is invalid.
1396 static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1397 unsigned int offset_in_page)
1399 struct mtd_info *mtd = nand_to_mtd(chip);
1401 /* Make sure the offset is less than the actual page size. */
1402 if (offset_in_page > mtd->writesize + mtd->oobsize)
1406 * On small page NANDs, there's a dedicated command to access the OOB
1407 * area, and the column address is relative to the start of the OOB
1408 * area, not the start of the page. Asjust the address accordingly.
1410 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1411 offset_in_page -= mtd->writesize;
1414 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1415 * wide, then it must be divided by 2.
1417 if (chip->options & NAND_BUSWIDTH_16) {
1418 if (WARN_ON(offset_in_page % 2))
1421 offset_in_page /= 2;
1424 addrs[0] = offset_in_page;
1427 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1430 if (mtd->writesize <= 512)
1433 addrs[1] = offset_in_page >> 8;
1438 static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1439 unsigned int offset_in_page, void *buf,
1442 struct mtd_info *mtd = nand_to_mtd(chip);
1443 const struct nand_sdr_timings *sdr =
1444 nand_get_sdr_timings(&chip->data_interface);
1446 struct nand_op_instr instrs[] = {
1447 NAND_OP_CMD(NAND_CMD_READ0, 0),
1448 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1449 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1450 PSEC_TO_NSEC(sdr->tRR_min)),
1451 NAND_OP_DATA_IN(len, buf, 0),
1453 struct nand_operation op = NAND_OPERATION(instrs);
1456 /* Drop the DATA_IN instruction if len is set to 0. */
1460 if (offset_in_page >= mtd->writesize)
1461 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1462 else if (offset_in_page >= 256 &&
1463 !(chip->options & NAND_BUSWIDTH_16))
1464 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1466 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1471 addrs[2] = page >> 8;
1473 if (chip->options & NAND_ROW_ADDR_3) {
1474 addrs[3] = page >> 16;
1475 instrs[1].ctx.addr.naddrs++;
1478 return nand_exec_op(chip, &op);
1481 static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1482 unsigned int offset_in_page, void *buf,
1485 const struct nand_sdr_timings *sdr =
1486 nand_get_sdr_timings(&chip->data_interface);
1488 struct nand_op_instr instrs[] = {
1489 NAND_OP_CMD(NAND_CMD_READ0, 0),
1490 NAND_OP_ADDR(4, addrs, 0),
1491 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1492 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1493 PSEC_TO_NSEC(sdr->tRR_min)),
1494 NAND_OP_DATA_IN(len, buf, 0),
1496 struct nand_operation op = NAND_OPERATION(instrs);
1499 /* Drop the DATA_IN instruction if len is set to 0. */
1503 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1508 addrs[3] = page >> 8;
1510 if (chip->options & NAND_ROW_ADDR_3) {
1511 addrs[4] = page >> 16;
1512 instrs[1].ctx.addr.naddrs++;
1515 return nand_exec_op(chip, &op);
1519 * nand_read_page_op - Do a READ PAGE operation
1520 * @chip: The NAND chip
1521 * @page: page to read
1522 * @offset_in_page: offset within the page
1523 * @buf: buffer used to store the data
1524 * @len: length of the buffer
1526 * This function issues a READ PAGE operation.
1527 * This function does not select/unselect the CS line.
1529 * Returns 0 on success, a negative error code otherwise.
1531 int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1532 unsigned int offset_in_page, void *buf, unsigned int len)
1534 struct mtd_info *mtd = nand_to_mtd(chip);
1539 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1542 if (chip->exec_op) {
1543 if (mtd->writesize > 512)
1544 return nand_lp_exec_read_page_op(chip, page,
1545 offset_in_page, buf,
1548 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1552 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1554 chip->read_buf(mtd, buf, len);
1558 EXPORT_SYMBOL_GPL(nand_read_page_op);
1561 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1562 * @chip: The NAND chip
1563 * @page: parameter page to read
1564 * @buf: buffer used to store the data
1565 * @len: length of the buffer
1567 * This function issues a READ PARAMETER PAGE operation.
1568 * This function does not select/unselect the CS line.
1570 * Returns 0 on success, a negative error code otherwise.
1572 static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1575 struct mtd_info *mtd = nand_to_mtd(chip);
1582 if (chip->exec_op) {
1583 const struct nand_sdr_timings *sdr =
1584 nand_get_sdr_timings(&chip->data_interface);
1585 struct nand_op_instr instrs[] = {
1586 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1587 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1588 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1589 PSEC_TO_NSEC(sdr->tRR_min)),
1590 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1592 struct nand_operation op = NAND_OPERATION(instrs);
1594 /* Drop the DATA_IN instruction if len is set to 0. */
1598 return nand_exec_op(chip, &op);
1601 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1602 for (i = 0; i < len; i++)
1603 p[i] = chip->read_byte(mtd);
1609 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1610 * @chip: The NAND chip
1611 * @offset_in_page: offset within the page
1612 * @buf: buffer used to store the data
1613 * @len: length of the buffer
1614 * @force_8bit: force 8-bit bus access
1616 * This function issues a CHANGE READ COLUMN operation.
1617 * This function does not select/unselect the CS line.
1619 * Returns 0 on success, a negative error code otherwise.
1621 int nand_change_read_column_op(struct nand_chip *chip,
1622 unsigned int offset_in_page, void *buf,
1623 unsigned int len, bool force_8bit)
1625 struct mtd_info *mtd = nand_to_mtd(chip);
1630 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1633 /* Small page NANDs do not support column change. */
1634 if (mtd->writesize <= 512)
1637 if (chip->exec_op) {
1638 const struct nand_sdr_timings *sdr =
1639 nand_get_sdr_timings(&chip->data_interface);
1641 struct nand_op_instr instrs[] = {
1642 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1643 NAND_OP_ADDR(2, addrs, 0),
1644 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1645 PSEC_TO_NSEC(sdr->tCCS_min)),
1646 NAND_OP_DATA_IN(len, buf, 0),
1648 struct nand_operation op = NAND_OPERATION(instrs);
1651 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1655 /* Drop the DATA_IN instruction if len is set to 0. */
1659 instrs[3].ctx.data.force_8bit = force_8bit;
1661 return nand_exec_op(chip, &op);
1664 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1666 chip->read_buf(mtd, buf, len);
1670 EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1673 * nand_read_oob_op - Do a READ OOB operation
1674 * @chip: The NAND chip
1675 * @page: page to read
1676 * @offset_in_oob: offset within the OOB area
1677 * @buf: buffer used to store the data
1678 * @len: length of the buffer
1680 * This function issues a READ OOB operation.
1681 * This function does not select/unselect the CS line.
1683 * Returns 0 on success, a negative error code otherwise.
1685 int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1686 unsigned int offset_in_oob, void *buf, unsigned int len)
1688 struct mtd_info *mtd = nand_to_mtd(chip);
1693 if (offset_in_oob + len > mtd->oobsize)
1697 return nand_read_page_op(chip, page,
1698 mtd->writesize + offset_in_oob,
1701 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1703 chip->read_buf(mtd, buf, len);
1707 EXPORT_SYMBOL_GPL(nand_read_oob_op);
1709 static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1710 unsigned int offset_in_page, const void *buf,
1711 unsigned int len, bool prog)
1713 struct mtd_info *mtd = nand_to_mtd(chip);
1714 const struct nand_sdr_timings *sdr =
1715 nand_get_sdr_timings(&chip->data_interface);
1717 struct nand_op_instr instrs[] = {
1719 * The first instruction will be dropped if we're dealing
1720 * with a large page NAND and adjusted if we're dealing
1721 * with a small page NAND and the page offset is > 255.
1723 NAND_OP_CMD(NAND_CMD_READ0, 0),
1724 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1725 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1726 NAND_OP_DATA_OUT(len, buf, 0),
1727 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1728 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1730 struct nand_operation op = NAND_OPERATION(instrs);
1731 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1738 addrs[naddrs++] = page;
1739 addrs[naddrs++] = page >> 8;
1740 if (chip->options & NAND_ROW_ADDR_3)
1741 addrs[naddrs++] = page >> 16;
1743 instrs[2].ctx.addr.naddrs = naddrs;
1745 /* Drop the last two instructions if we're not programming the page. */
1748 /* Also drop the DATA_OUT instruction if empty. */
1753 if (mtd->writesize <= 512) {
1755 * Small pages need some more tweaking: we have to adjust the
1756 * first instruction depending on the page offset we're trying
1759 if (offset_in_page >= mtd->writesize)
1760 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1761 else if (offset_in_page >= 256 &&
1762 !(chip->options & NAND_BUSWIDTH_16))
1763 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1766 * Drop the first command if we're dealing with a large page
1773 ret = nand_exec_op(chip, &op);
1777 ret = nand_status_op(chip, &status);
1785 * nand_prog_page_begin_op - starts a PROG PAGE operation
1786 * @chip: The NAND chip
1787 * @page: page to write
1788 * @offset_in_page: offset within the page
1789 * @buf: buffer containing the data to write to the page
1790 * @len: length of the buffer
1792 * This function issues the first half of a PROG PAGE operation.
1793 * This function does not select/unselect the CS line.
1795 * Returns 0 on success, a negative error code otherwise.
1797 int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1798 unsigned int offset_in_page, const void *buf,
1801 struct mtd_info *mtd = nand_to_mtd(chip);
1806 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1810 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1813 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1816 chip->write_buf(mtd, buf, len);
1820 EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1823 * nand_prog_page_end_op - ends a PROG PAGE operation
1824 * @chip: The NAND chip
1826 * This function issues the second half of a PROG PAGE operation.
1827 * This function does not select/unselect the CS line.
1829 * Returns 0 on success, a negative error code otherwise.
1831 int nand_prog_page_end_op(struct nand_chip *chip)
1833 struct mtd_info *mtd = nand_to_mtd(chip);
1837 if (chip->exec_op) {
1838 const struct nand_sdr_timings *sdr =
1839 nand_get_sdr_timings(&chip->data_interface);
1840 struct nand_op_instr instrs[] = {
1841 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1842 PSEC_TO_NSEC(sdr->tWB_max)),
1843 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1845 struct nand_operation op = NAND_OPERATION(instrs);
1847 ret = nand_exec_op(chip, &op);
1851 ret = nand_status_op(chip, &status);
1855 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1856 ret = chip->waitfunc(mtd, chip);
1863 if (status & NAND_STATUS_FAIL)
1868 EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1871 * nand_prog_page_op - Do a full PROG PAGE operation
1872 * @chip: The NAND chip
1873 * @page: page to write
1874 * @offset_in_page: offset within the page
1875 * @buf: buffer containing the data to write to the page
1876 * @len: length of the buffer
1878 * This function issues a full PROG PAGE operation.
1879 * This function does not select/unselect the CS line.
1881 * Returns 0 on success, a negative error code otherwise.
1883 int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1884 unsigned int offset_in_page, const void *buf,
1887 struct mtd_info *mtd = nand_to_mtd(chip);
1893 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1896 if (chip->exec_op) {
1897 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1900 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1901 chip->write_buf(mtd, buf, len);
1902 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1903 status = chip->waitfunc(mtd, chip);
1906 if (status & NAND_STATUS_FAIL)
1911 EXPORT_SYMBOL_GPL(nand_prog_page_op);
1914 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1915 * @chip: The NAND chip
1916 * @offset_in_page: offset within the page
1917 * @buf: buffer containing the data to send to the NAND
1918 * @len: length of the buffer
1919 * @force_8bit: force 8-bit bus access
1921 * This function issues a CHANGE WRITE COLUMN operation.
1922 * This function does not select/unselect the CS line.
1924 * Returns 0 on success, a negative error code otherwise.
1926 int nand_change_write_column_op(struct nand_chip *chip,
1927 unsigned int offset_in_page,
1928 const void *buf, unsigned int len,
1931 struct mtd_info *mtd = nand_to_mtd(chip);
1936 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1939 /* Small page NANDs do not support column change. */
1940 if (mtd->writesize <= 512)
1943 if (chip->exec_op) {
1944 const struct nand_sdr_timings *sdr =
1945 nand_get_sdr_timings(&chip->data_interface);
1947 struct nand_op_instr instrs[] = {
1948 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1949 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1950 NAND_OP_DATA_OUT(len, buf, 0),
1952 struct nand_operation op = NAND_OPERATION(instrs);
1955 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1959 instrs[2].ctx.data.force_8bit = force_8bit;
1961 /* Drop the DATA_OUT instruction if len is set to 0. */
1965 return nand_exec_op(chip, &op);
1968 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1970 chip->write_buf(mtd, buf, len);
1974 EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1977 * nand_readid_op - Do a READID operation
1978 * @chip: The NAND chip
1979 * @addr: address cycle to pass after the READID command
1980 * @buf: buffer used to store the ID
1981 * @len: length of the buffer
1983 * This function sends a READID command and reads back the ID returned by the
1985 * This function does not select/unselect the CS line.
1987 * Returns 0 on success, a negative error code otherwise.
1989 int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1992 struct mtd_info *mtd = nand_to_mtd(chip);
1999 if (chip->exec_op) {
2000 const struct nand_sdr_timings *sdr =
2001 nand_get_sdr_timings(&chip->data_interface);
2002 struct nand_op_instr instrs[] = {
2003 NAND_OP_CMD(NAND_CMD_READID, 0),
2004 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
2005 NAND_OP_8BIT_DATA_IN(len, buf, 0),
2007 struct nand_operation op = NAND_OPERATION(instrs);
2009 /* Drop the DATA_IN instruction if len is set to 0. */
2013 return nand_exec_op(chip, &op);
2016 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
2018 for (i = 0; i < len; i++)
2019 id[i] = chip->read_byte(mtd);
2023 EXPORT_SYMBOL_GPL(nand_readid_op);
2026 * nand_status_op - Do a STATUS operation
2027 * @chip: The NAND chip
2028 * @status: out variable to store the NAND status
2030 * This function sends a STATUS command and reads back the status returned by
2032 * This function does not select/unselect the CS line.
2034 * Returns 0 on success, a negative error code otherwise.
2036 int nand_status_op(struct nand_chip *chip, u8 *status)
2038 struct mtd_info *mtd = nand_to_mtd(chip);
2040 if (chip->exec_op) {
2041 const struct nand_sdr_timings *sdr =
2042 nand_get_sdr_timings(&chip->data_interface);
2043 struct nand_op_instr instrs[] = {
2044 NAND_OP_CMD(NAND_CMD_STATUS,
2045 PSEC_TO_NSEC(sdr->tADL_min)),
2046 NAND_OP_8BIT_DATA_IN(1, status, 0),
2048 struct nand_operation op = NAND_OPERATION(instrs);
2053 return nand_exec_op(chip, &op);
2056 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
2058 *status = chip->read_byte(mtd);
2062 EXPORT_SYMBOL_GPL(nand_status_op);
2065 * nand_exit_status_op - Exit a STATUS operation
2066 * @chip: The NAND chip
2068 * This function sends a READ0 command to cancel the effect of the STATUS
2069 * command to avoid reading only the status until a new read command is sent.
2071 * This function does not select/unselect the CS line.
2073 * Returns 0 on success, a negative error code otherwise.
2075 int nand_exit_status_op(struct nand_chip *chip)
2077 struct mtd_info *mtd = nand_to_mtd(chip);
2079 if (chip->exec_op) {
2080 struct nand_op_instr instrs[] = {
2081 NAND_OP_CMD(NAND_CMD_READ0, 0),
2083 struct nand_operation op = NAND_OPERATION(instrs);
2085 return nand_exec_op(chip, &op);
2088 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
2092 EXPORT_SYMBOL_GPL(nand_exit_status_op);
2095 * nand_erase_op - Do an erase operation
2096 * @chip: The NAND chip
2097 * @eraseblock: block to erase
2099 * This function sends an ERASE command and waits for the NAND to be ready
2101 * This function does not select/unselect the CS line.
2103 * Returns 0 on success, a negative error code otherwise.
2105 int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2107 struct mtd_info *mtd = nand_to_mtd(chip);
2108 unsigned int page = eraseblock <<
2109 (chip->phys_erase_shift - chip->page_shift);
2113 if (chip->exec_op) {
2114 const struct nand_sdr_timings *sdr =
2115 nand_get_sdr_timings(&chip->data_interface);
2116 u8 addrs[3] = { page, page >> 8, page >> 16 };
2117 struct nand_op_instr instrs[] = {
2118 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2119 NAND_OP_ADDR(2, addrs, 0),
2120 NAND_OP_CMD(NAND_CMD_ERASE2,
2121 PSEC_TO_MSEC(sdr->tWB_max)),
2122 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2124 struct nand_operation op = NAND_OPERATION(instrs);
2126 if (chip->options & NAND_ROW_ADDR_3)
2127 instrs[1].ctx.addr.naddrs++;
2129 ret = nand_exec_op(chip, &op);
2133 ret = nand_status_op(chip, &status);
2137 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2138 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2140 ret = chip->waitfunc(mtd, chip);
2147 if (status & NAND_STATUS_FAIL)
2152 EXPORT_SYMBOL_GPL(nand_erase_op);
2155 * nand_set_features_op - Do a SET FEATURES operation
2156 * @chip: The NAND chip
2157 * @feature: feature id
2158 * @data: 4 bytes of data
2160 * This function sends a SET FEATURES command and waits for the NAND to be
2161 * ready before returning.
2162 * This function does not select/unselect the CS line.
2164 * Returns 0 on success, a negative error code otherwise.
2166 static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2169 struct mtd_info *mtd = nand_to_mtd(chip);
2170 const u8 *params = data;
2174 if (chip->exec_op) {
2175 const struct nand_sdr_timings *sdr =
2176 nand_get_sdr_timings(&chip->data_interface);
2177 struct nand_op_instr instrs[] = {
2178 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2179 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2180 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2181 PSEC_TO_NSEC(sdr->tWB_max)),
2182 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2184 struct nand_operation op = NAND_OPERATION(instrs);
2186 ret = nand_exec_op(chip, &op);
2190 ret = nand_status_op(chip, &status);
2194 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
2195 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2196 chip->write_byte(mtd, params[i]);
2198 ret = chip->waitfunc(mtd, chip);
2205 if (status & NAND_STATUS_FAIL)
2212 * nand_get_features_op - Do a GET FEATURES operation
2213 * @chip: The NAND chip
2214 * @feature: feature id
2215 * @data: 4 bytes of data
2217 * This function sends a GET FEATURES command and waits for the NAND to be
2218 * ready before returning.
2219 * This function does not select/unselect the CS line.
2221 * Returns 0 on success, a negative error code otherwise.
2223 static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2226 struct mtd_info *mtd = nand_to_mtd(chip);
2230 if (chip->exec_op) {
2231 const struct nand_sdr_timings *sdr =
2232 nand_get_sdr_timings(&chip->data_interface);
2233 struct nand_op_instr instrs[] = {
2234 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2235 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2236 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2237 PSEC_TO_NSEC(sdr->tRR_min)),
2238 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2241 struct nand_operation op = NAND_OPERATION(instrs);
2243 return nand_exec_op(chip, &op);
2246 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
2247 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2248 params[i] = chip->read_byte(mtd);
2254 * nand_reset_op - Do a reset operation
2255 * @chip: The NAND chip
2257 * This function sends a RESET command and waits for the NAND to be ready
2259 * This function does not select/unselect the CS line.
2261 * Returns 0 on success, a negative error code otherwise.
2263 int nand_reset_op(struct nand_chip *chip)
2265 struct mtd_info *mtd = nand_to_mtd(chip);
2267 if (chip->exec_op) {
2268 const struct nand_sdr_timings *sdr =
2269 nand_get_sdr_timings(&chip->data_interface);
2270 struct nand_op_instr instrs[] = {
2271 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2272 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2274 struct nand_operation op = NAND_OPERATION(instrs);
2276 return nand_exec_op(chip, &op);
2279 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2283 EXPORT_SYMBOL_GPL(nand_reset_op);
2286 * nand_read_data_op - Read data from the NAND
2287 * @chip: The NAND chip
2288 * @buf: buffer used to store the data
2289 * @len: length of the buffer
2290 * @force_8bit: force 8-bit bus access
2292 * This function does a raw data read on the bus. Usually used after launching
2293 * another NAND operation like nand_read_page_op().
2294 * This function does not select/unselect the CS line.
2296 * Returns 0 on success, a negative error code otherwise.
2298 int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2301 struct mtd_info *mtd = nand_to_mtd(chip);
2306 if (chip->exec_op) {
2307 struct nand_op_instr instrs[] = {
2308 NAND_OP_DATA_IN(len, buf, 0),
2310 struct nand_operation op = NAND_OPERATION(instrs);
2312 instrs[0].ctx.data.force_8bit = force_8bit;
2314 return nand_exec_op(chip, &op);
2321 for (i = 0; i < len; i++)
2322 p[i] = chip->read_byte(mtd);
2324 chip->read_buf(mtd, buf, len);
2329 EXPORT_SYMBOL_GPL(nand_read_data_op);
2332 * nand_write_data_op - Write data from the NAND
2333 * @chip: The NAND chip
2334 * @buf: buffer containing the data to send on the bus
2335 * @len: length of the buffer
2336 * @force_8bit: force 8-bit bus access
2338 * This function does a raw data write on the bus. Usually used after launching
2339 * another NAND operation like nand_write_page_begin_op().
2340 * This function does not select/unselect the CS line.
2342 * Returns 0 on success, a negative error code otherwise.
2344 int nand_write_data_op(struct nand_chip *chip, const void *buf,
2345 unsigned int len, bool force_8bit)
2347 struct mtd_info *mtd = nand_to_mtd(chip);
2352 if (chip->exec_op) {
2353 struct nand_op_instr instrs[] = {
2354 NAND_OP_DATA_OUT(len, buf, 0),
2356 struct nand_operation op = NAND_OPERATION(instrs);
2358 instrs[0].ctx.data.force_8bit = force_8bit;
2360 return nand_exec_op(chip, &op);
2367 for (i = 0; i < len; i++)
2368 chip->write_byte(mtd, p[i]);
2370 chip->write_buf(mtd, buf, len);
2375 EXPORT_SYMBOL_GPL(nand_write_data_op);
2378 * struct nand_op_parser_ctx - Context used by the parser
2379 * @instrs: array of all the instructions that must be addressed
2380 * @ninstrs: length of the @instrs array
2381 * @subop: Sub-operation to be passed to the NAND controller
2383 * This structure is used by the core to split NAND operations into
2384 * sub-operations that can be handled by the NAND controller.
2386 struct nand_op_parser_ctx {
2387 const struct nand_op_instr *instrs;
2388 unsigned int ninstrs;
2389 struct nand_subop subop;
2393 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2394 * @pat: the parser pattern element that matches @instr
2395 * @instr: pointer to the instruction to check
2396 * @start_offset: this is an in/out parameter. If @instr has already been
2397 * split, then @start_offset is the offset from which to start
2398 * (either an address cycle or an offset in the data buffer).
2399 * Conversely, if the function returns true (ie. instr must be
2400 * split), this parameter is updated to point to the first
2401 * data/address cycle that has not been taken care of.
2403 * Some NAND controllers are limited and cannot send X address cycles with a
2404 * unique operation, or cannot read/write more than Y bytes at the same time.
2405 * In this case, split the instruction that does not fit in a single
2406 * controller-operation into two or more chunks.
2408 * Returns true if the instruction must be split, false otherwise.
2409 * The @start_offset parameter is also updated to the offset at which the next
2410 * bundle of instruction must start (if an address or a data instruction).
2413 nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2414 const struct nand_op_instr *instr,
2415 unsigned int *start_offset)
2417 switch (pat->type) {
2418 case NAND_OP_ADDR_INSTR:
2419 if (!pat->ctx.addr.maxcycles)
2422 if (instr->ctx.addr.naddrs - *start_offset >
2423 pat->ctx.addr.maxcycles) {
2424 *start_offset += pat->ctx.addr.maxcycles;
2429 case NAND_OP_DATA_IN_INSTR:
2430 case NAND_OP_DATA_OUT_INSTR:
2431 if (!pat->ctx.data.maxlen)
2434 if (instr->ctx.data.len - *start_offset >
2435 pat->ctx.data.maxlen) {
2436 *start_offset += pat->ctx.data.maxlen;
2449 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2450 * remaining in the parser context
2451 * @pat: the pattern to test
2452 * @ctx: the parser context structure to match with the pattern @pat
2454 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2455 * Returns true if this is the case, false ortherwise. When true is returned,
2456 * @ctx->subop is updated with the set of instructions to be passed to the
2457 * controller driver.
2460 nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2461 struct nand_op_parser_ctx *ctx)
2463 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2464 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2465 const struct nand_op_instr *instr = ctx->subop.instrs;
2466 unsigned int i, ninstrs;
2468 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2470 * The pattern instruction does not match the operation
2471 * instruction. If the instruction is marked optional in the
2472 * pattern definition, we skip the pattern element and continue
2473 * to the next one. If the element is mandatory, there's no
2474 * match and we can return false directly.
2476 if (instr->type != pat->elems[i].type) {
2477 if (!pat->elems[i].optional)
2484 * Now check the pattern element constraints. If the pattern is
2485 * not able to handle the whole instruction in a single step,
2486 * we have to split it.
2487 * The last_instr_end_off value comes back updated to point to
2488 * the position where we have to split the instruction (the
2489 * start of the next subop chunk).
2491 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2504 * This can happen if all instructions of a pattern are optional.
2505 * Still, if there's not at least one instruction handled by this
2506 * pattern, this is not a match, and we should try the next one (if
2513 * We had a match on the pattern head, but the pattern may be longer
2514 * than the instructions we're asked to execute. We need to make sure
2515 * there's no mandatory elements in the pattern tail.
2517 for (; i < pat->nelems; i++) {
2518 if (!pat->elems[i].optional)
2523 * We have a match: update the subop structure accordingly and return
2526 ctx->subop.ninstrs = ninstrs;
2527 ctx->subop.last_instr_end_off = instr_offset;
2532 #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2533 static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2535 const struct nand_op_instr *instr;
2539 pr_debug("executing subop:\n");
2541 for (i = 0; i < ctx->ninstrs; i++) {
2542 instr = &ctx->instrs[i];
2544 if (instr == &ctx->subop.instrs[0])
2547 switch (instr->type) {
2548 case NAND_OP_CMD_INSTR:
2549 pr_debug("%sCMD [0x%02x]\n", prefix,
2550 instr->ctx.cmd.opcode);
2552 case NAND_OP_ADDR_INSTR:
2553 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2554 instr->ctx.addr.naddrs,
2555 instr->ctx.addr.naddrs < 64 ?
2556 instr->ctx.addr.naddrs : 64,
2557 instr->ctx.addr.addrs);
2559 case NAND_OP_DATA_IN_INSTR:
2560 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2561 instr->ctx.data.len,
2562 instr->ctx.data.force_8bit ?
2563 ", force 8-bit" : "");
2565 case NAND_OP_DATA_OUT_INSTR:
2566 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2567 instr->ctx.data.len,
2568 instr->ctx.data.force_8bit ?
2569 ", force 8-bit" : "");
2571 case NAND_OP_WAITRDY_INSTR:
2572 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2573 instr->ctx.waitrdy.timeout_ms);
2577 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2582 static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2589 * nand_op_parser_exec_op - exec_op parser
2590 * @chip: the NAND chip
2591 * @parser: patterns description provided by the controller driver
2592 * @op: the NAND operation to address
2593 * @check_only: when true, the function only checks if @op can be handled but
2594 * does not execute the operation
2596 * Helper function designed to ease integration of NAND controller drivers that
2597 * only support a limited set of instruction sequences. The supported sequences
2598 * are described in @parser, and the framework takes care of splitting @op into
2599 * multiple sub-operations (if required) and pass them back to the ->exec()
2600 * callback of the matching pattern if @check_only is set to false.
2602 * NAND controller drivers should call this function from their own ->exec_op()
2605 * Returns 0 on success, a negative error code otherwise. A failure can be
2606 * caused by an unsupported operation (none of the supported patterns is able
2607 * to handle the requested operation), or an error returned by one of the
2608 * matching pattern->exec() hook.
2610 int nand_op_parser_exec_op(struct nand_chip *chip,
2611 const struct nand_op_parser *parser,
2612 const struct nand_operation *op, bool check_only)
2614 struct nand_op_parser_ctx ctx = {
2615 .subop.instrs = op->instrs,
2616 .instrs = op->instrs,
2617 .ninstrs = op->ninstrs,
2621 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2624 for (i = 0; i < parser->npatterns; i++) {
2625 const struct nand_op_parser_pattern *pattern;
2627 pattern = &parser->patterns[i];
2628 if (!nand_op_parser_match_pat(pattern, &ctx))
2631 nand_op_parser_trace(&ctx);
2636 ret = pattern->exec(chip, &ctx.subop);
2643 if (i == parser->npatterns) {
2644 pr_debug("->exec_op() parser: pattern not found!\n");
2649 * Update the context structure by pointing to the start of the
2652 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2653 if (ctx.subop.last_instr_end_off)
2654 ctx.subop.instrs -= 1;
2656 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2661 EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2663 static bool nand_instr_is_data(const struct nand_op_instr *instr)
2665 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2666 instr->type == NAND_OP_DATA_OUT_INSTR);
2669 static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2670 unsigned int instr_idx)
2672 return subop && instr_idx < subop->ninstrs;
2675 static int nand_subop_get_start_off(const struct nand_subop *subop,
2676 unsigned int instr_idx)
2681 return subop->first_instr_start_off;
2685 * nand_subop_get_addr_start_off - Get the start offset in an address array
2686 * @subop: The entire sub-operation
2687 * @instr_idx: Index of the instruction inside the sub-operation
2689 * During driver development, one could be tempted to directly use the
2690 * ->addr.addrs field of address instructions. This is wrong as address
2691 * instructions might be split.
2693 * Given an address instruction, returns the offset of the first cycle to issue.
2695 int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2696 unsigned int instr_idx)
2698 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2699 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2702 return nand_subop_get_start_off(subop, instr_idx);
2704 EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2707 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2708 * @subop: The entire sub-operation
2709 * @instr_idx: Index of the instruction inside the sub-operation
2711 * During driver development, one could be tempted to directly use the
2712 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2715 * Given an address instruction, returns the number of address cycle to issue.
2717 int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2718 unsigned int instr_idx)
2720 int start_off, end_off;
2722 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2723 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2726 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2728 if (instr_idx == subop->ninstrs - 1 &&
2729 subop->last_instr_end_off)
2730 end_off = subop->last_instr_end_off;
2732 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2734 return end_off - start_off;
2736 EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2739 * nand_subop_get_data_start_off - Get the start offset in a data array
2740 * @subop: The entire sub-operation
2741 * @instr_idx: Index of the instruction inside the sub-operation
2743 * During driver development, one could be tempted to directly use the
2744 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2745 * instructions might be split.
2747 * Given a data instruction, returns the offset to start from.
2749 int nand_subop_get_data_start_off(const struct nand_subop *subop,
2750 unsigned int instr_idx)
2752 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2753 !nand_instr_is_data(&subop->instrs[instr_idx]))
2756 return nand_subop_get_start_off(subop, instr_idx);
2758 EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2761 * nand_subop_get_data_len - Get the number of bytes to retrieve
2762 * @subop: The entire sub-operation
2763 * @instr_idx: Index of the instruction inside the sub-operation
2765 * During driver development, one could be tempted to directly use the
2766 * ->data->len field of a data instruction. This is wrong as data instructions
2769 * Returns the length of the chunk of data to send/receive.
2771 int nand_subop_get_data_len(const struct nand_subop *subop,
2772 unsigned int instr_idx)
2774 int start_off = 0, end_off;
2776 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2777 !nand_instr_is_data(&subop->instrs[instr_idx]))
2780 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2782 if (instr_idx == subop->ninstrs - 1 &&
2783 subop->last_instr_end_off)
2784 end_off = subop->last_instr_end_off;
2786 end_off = subop->instrs[instr_idx].ctx.data.len;
2788 return end_off - start_off;
2790 EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2793 * nand_reset - Reset and initialize a NAND device
2794 * @chip: The NAND chip
2795 * @chipnr: Internal die id
2797 * Save the timings data structure, then apply SDR timings mode 0 (see
2798 * nand_reset_data_interface for details), do the reset operation, and
2799 * apply back the previous timings.
2801 * Returns 0 on success, a negative error code otherwise.
2803 int nand_reset(struct nand_chip *chip, int chipnr)
2805 struct mtd_info *mtd = nand_to_mtd(chip);
2806 struct nand_data_interface saved_data_intf = chip->data_interface;
2809 ret = nand_reset_data_interface(chip, chipnr);
2814 * The CS line has to be released before we can apply the new NAND
2815 * interface settings, hence this weird ->select_chip() dance.
2817 chip->select_chip(mtd, chipnr);
2818 ret = nand_reset_op(chip);
2819 chip->select_chip(mtd, -1);
2824 * A nand_reset_data_interface() put both the NAND chip and the NAND
2825 * controller in timings mode 0. If the default mode for this chip is
2826 * also 0, no need to proceed to the change again. Plus, at probe time,
2827 * nand_setup_data_interface() uses ->set/get_features() which would
2828 * fail anyway as the parameter page is not available yet.
2830 if (!chip->onfi_timing_mode_default)
2833 chip->data_interface = saved_data_intf;
2834 ret = nand_setup_data_interface(chip, chipnr);
2840 EXPORT_SYMBOL_GPL(nand_reset);
2843 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2844 * @buf: buffer to test
2845 * @len: buffer length
2846 * @bitflips_threshold: maximum number of bitflips
2848 * Check if a buffer contains only 0xff, which means the underlying region
2849 * has been erased and is ready to be programmed.
2850 * The bitflips_threshold specify the maximum number of bitflips before
2851 * considering the region is not erased.
2852 * Note: The logic of this function has been extracted from the memweight
2853 * implementation, except that nand_check_erased_buf function exit before
2854 * testing the whole buffer if the number of bitflips exceed the
2855 * bitflips_threshold value.
2857 * Returns a positive number of bitflips less than or equal to
2858 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2861 static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2863 const unsigned char *bitmap = buf;
2867 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2869 weight = hweight8(*bitmap);
2870 bitflips += BITS_PER_BYTE - weight;
2871 if (unlikely(bitflips > bitflips_threshold))
2875 for (; len >= sizeof(long);
2876 len -= sizeof(long), bitmap += sizeof(long)) {
2877 unsigned long d = *((unsigned long *)bitmap);
2880 weight = hweight_long(d);
2881 bitflips += BITS_PER_LONG - weight;
2882 if (unlikely(bitflips > bitflips_threshold))
2886 for (; len > 0; len--, bitmap++) {
2887 weight = hweight8(*bitmap);
2888 bitflips += BITS_PER_BYTE - weight;
2889 if (unlikely(bitflips > bitflips_threshold))
2897 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2899 * @data: data buffer to test
2900 * @datalen: data length
2902 * @ecclen: ECC length
2903 * @extraoob: extra OOB buffer
2904 * @extraooblen: extra OOB length
2905 * @bitflips_threshold: maximum number of bitflips
2907 * Check if a data buffer and its associated ECC and OOB data contains only
2908 * 0xff pattern, which means the underlying region has been erased and is
2909 * ready to be programmed.
2910 * The bitflips_threshold specify the maximum number of bitflips before
2911 * considering the region as not erased.
2914 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2915 * different from the NAND page size. When fixing bitflips, ECC engines will
2916 * report the number of errors per chunk, and the NAND core infrastructure
2917 * expect you to return the maximum number of bitflips for the whole page.
2918 * This is why you should always use this function on a single chunk and
2919 * not on the whole page. After checking each chunk you should update your
2920 * max_bitflips value accordingly.
2921 * 2/ When checking for bitflips in erased pages you should not only check
2922 * the payload data but also their associated ECC data, because a user might
2923 * have programmed almost all bits to 1 but a few. In this case, we
2924 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2926 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2927 * data are protected by the ECC engine.
2928 * It could also be used if you support subpages and want to attach some
2929 * extra OOB data to an ECC chunk.
2931 * Returns a positive number of bitflips less than or equal to
2932 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2933 * threshold. In case of success, the passed buffers are filled with 0xff.
2935 int nand_check_erased_ecc_chunk(void *data, int datalen,
2936 void *ecc, int ecclen,
2937 void *extraoob, int extraooblen,
2938 int bitflips_threshold)
2940 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2942 data_bitflips = nand_check_erased_buf(data, datalen,
2943 bitflips_threshold);
2944 if (data_bitflips < 0)
2945 return data_bitflips;
2947 bitflips_threshold -= data_bitflips;
2949 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2950 if (ecc_bitflips < 0)
2951 return ecc_bitflips;
2953 bitflips_threshold -= ecc_bitflips;
2955 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2956 bitflips_threshold);
2957 if (extraoob_bitflips < 0)
2958 return extraoob_bitflips;
2961 memset(data, 0xff, datalen);
2964 memset(ecc, 0xff, ecclen);
2966 if (extraoob_bitflips)
2967 memset(extraoob, 0xff, extraooblen);
2969 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2971 EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2974 * nand_read_page_raw - [INTERN] read raw page data without ecc
2975 * @mtd: mtd info structure
2976 * @chip: nand chip info structure
2977 * @buf: buffer to store read data
2978 * @oob_required: caller requires OOB data read to chip->oob_poi
2979 * @page: page number to read
2981 * Not for syndrome calculating ECC controllers, which use a special oob layout.
2983 int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2984 uint8_t *buf, int oob_required, int page)
2988 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
2993 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
3001 EXPORT_SYMBOL(nand_read_page_raw);
3004 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
3005 * @mtd: mtd info structure
3006 * @chip: nand chip info structure
3007 * @buf: buffer to store read data
3008 * @oob_required: caller requires OOB data read to chip->oob_poi
3009 * @page: page number to read
3011 * We need a special oob layout and handling even when OOB isn't used.
3013 static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
3014 struct nand_chip *chip, uint8_t *buf,
3015 int oob_required, int page)
3017 int eccsize = chip->ecc.size;
3018 int eccbytes = chip->ecc.bytes;
3019 uint8_t *oob = chip->oob_poi;
3020 int steps, size, ret;
3022 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3026 for (steps = chip->ecc.steps; steps > 0; steps--) {
3027 ret = nand_read_data_op(chip, buf, eccsize, false);
3033 if (chip->ecc.prepad) {
3034 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3039 oob += chip->ecc.prepad;
3042 ret = nand_read_data_op(chip, oob, eccbytes, false);
3048 if (chip->ecc.postpad) {
3049 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3054 oob += chip->ecc.postpad;
3058 size = mtd->oobsize - (oob - chip->oob_poi);
3060 ret = nand_read_data_op(chip, oob, size, false);
3069 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
3070 * @mtd: mtd info structure
3071 * @chip: nand chip info structure
3072 * @buf: buffer to store read data
3073 * @oob_required: caller requires OOB data read to chip->oob_poi
3074 * @page: page number to read
3076 static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
3077 uint8_t *buf, int oob_required, int page)
3079 int i, eccsize = chip->ecc.size, ret;
3080 int eccbytes = chip->ecc.bytes;
3081 int eccsteps = chip->ecc.steps;
3083 uint8_t *ecc_calc = chip->ecc.calc_buf;
3084 uint8_t *ecc_code = chip->ecc.code_buf;
3085 unsigned int max_bitflips = 0;
3087 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
3089 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3090 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3092 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3097 eccsteps = chip->ecc.steps;
3100 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3103 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
3105 mtd->ecc_stats.failed++;
3107 mtd->ecc_stats.corrected += stat;
3108 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3111 return max_bitflips;
3115 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
3116 * @mtd: mtd info structure
3117 * @chip: nand chip info structure
3118 * @data_offs: offset of requested data within the page
3119 * @readlen: data length
3120 * @bufpoi: buffer to store read data
3121 * @page: page number to read
3123 static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
3124 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
3127 int start_step, end_step, num_steps, ret;
3129 int data_col_addr, i, gaps = 0;
3130 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3131 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
3132 int index, section = 0;
3133 unsigned int max_bitflips = 0;
3134 struct mtd_oob_region oobregion = { };
3136 /* Column address within the page aligned to ECC size (256bytes) */
3137 start_step = data_offs / chip->ecc.size;
3138 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3139 num_steps = end_step - start_step + 1;
3140 index = start_step * chip->ecc.bytes;
3142 /* Data size aligned to ECC ecc.size */
3143 datafrag_len = num_steps * chip->ecc.size;
3144 eccfrag_len = num_steps * chip->ecc.bytes;
3146 data_col_addr = start_step * chip->ecc.size;
3147 /* If we read not a page aligned data */
3148 p = bufpoi + data_col_addr;
3149 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
3154 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
3155 chip->ecc.calculate(mtd, p, &chip->ecc.calc_buf[i]);
3158 * The performance is faster if we position offsets according to
3159 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
3161 ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion);
3165 if (oobregion.length < eccfrag_len)
3169 ret = nand_change_read_column_op(chip, mtd->writesize,
3170 chip->oob_poi, mtd->oobsize,
3176 * Send the command to read the particular ECC bytes take care
3177 * about buswidth alignment in read_buf.
3179 aligned_pos = oobregion.offset & ~(busw - 1);
3180 aligned_len = eccfrag_len;
3181 if (oobregion.offset & (busw - 1))
3183 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3187 ret = nand_change_read_column_op(chip,
3188 mtd->writesize + aligned_pos,
3189 &chip->oob_poi[aligned_pos],
3190 aligned_len, false);
3195 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
3196 chip->oob_poi, index, eccfrag_len);
3200 p = bufpoi + data_col_addr;
3201 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3204 stat = chip->ecc.correct(mtd, p, &chip->ecc.code_buf[i],
3205 &chip->ecc.calc_buf[i]);
3206 if (stat == -EBADMSG &&
3207 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3208 /* check for empty pages with bitflips */
3209 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3210 &chip->ecc.code_buf[i],
3213 chip->ecc.strength);
3217 mtd->ecc_stats.failed++;
3219 mtd->ecc_stats.corrected += stat;
3220 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3223 return max_bitflips;
3227 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
3228 * @mtd: mtd info structure
3229 * @chip: nand chip info structure
3230 * @buf: buffer to store read data
3231 * @oob_required: caller requires OOB data read to chip->oob_poi
3232 * @page: page number to read
3234 * Not for syndrome calculating ECC controllers which need a special oob layout.
3236 static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
3237 uint8_t *buf, int oob_required, int page)
3239 int i, eccsize = chip->ecc.size, ret;
3240 int eccbytes = chip->ecc.bytes;
3241 int eccsteps = chip->ecc.steps;
3243 uint8_t *ecc_calc = chip->ecc.calc_buf;
3244 uint8_t *ecc_code = chip->ecc.code_buf;
3245 unsigned int max_bitflips = 0;
3247 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3251 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3252 chip->ecc.hwctl(mtd, NAND_ECC_READ);
3254 ret = nand_read_data_op(chip, p, eccsize, false);
3258 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3261 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3265 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3270 eccsteps = chip->ecc.steps;
3273 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3276 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
3277 if (stat == -EBADMSG &&
3278 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3279 /* check for empty pages with bitflips */
3280 stat = nand_check_erased_ecc_chunk(p, eccsize,
3281 &ecc_code[i], eccbytes,
3283 chip->ecc.strength);
3287 mtd->ecc_stats.failed++;
3289 mtd->ecc_stats.corrected += stat;
3290 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3293 return max_bitflips;
3297 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
3298 * @mtd: mtd info structure
3299 * @chip: nand chip info structure
3300 * @buf: buffer to store read data
3301 * @oob_required: caller requires OOB data read to chip->oob_poi
3302 * @page: page number to read
3304 * Hardware ECC for large page chips, require OOB to be read first. For this
3305 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3306 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3307 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3308 * the data area, by overwriting the NAND manufacturer bad block markings.
3310 static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
3311 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
3313 int i, eccsize = chip->ecc.size, ret;
3314 int eccbytes = chip->ecc.bytes;
3315 int eccsteps = chip->ecc.steps;
3317 uint8_t *ecc_code = chip->ecc.code_buf;
3318 uint8_t *ecc_calc = chip->ecc.calc_buf;
3319 unsigned int max_bitflips = 0;
3321 /* Read the OOB area first */
3322 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3326 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3330 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3335 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3338 chip->ecc.hwctl(mtd, NAND_ECC_READ);
3340 ret = nand_read_data_op(chip, p, eccsize, false);
3344 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3346 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
3347 if (stat == -EBADMSG &&
3348 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3349 /* check for empty pages with bitflips */
3350 stat = nand_check_erased_ecc_chunk(p, eccsize,
3351 &ecc_code[i], eccbytes,