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));
532 einfo.len = 1ULL << chip->phys_erase_shift;
533 nand_erase_nand(mtd, &einfo, 0);
535 /* Write bad block marker to OOB */
536 nand_get_device(mtd, FL_WRITING);
537 ret = chip->block_markbad(mtd, ofs);
538 nand_release_device(mtd);
541 /* Mark block bad in BBT */
543 res = nand_markbad_bbt(mtd, ofs);
549 mtd->ecc_stats.badblocks++;
555 * nand_check_wp - [GENERIC] check if the chip is write protected
556 * @mtd: MTD device structure
558 * Check, if the device is write protected. The function expects, that the
559 * device is already selected.
561 static int nand_check_wp(struct mtd_info *mtd)
563 struct nand_chip *chip = mtd_to_nand(mtd);
567 /* Broken xD cards report WP despite being writable */
568 if (chip->options & NAND_BROKEN_XD)
571 /* Check the WP bit */
572 ret = nand_status_op(chip, &status);
576 return status & NAND_STATUS_WP ? 0 : 1;
580 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
581 * @mtd: MTD device structure
582 * @ofs: offset from device start
584 * Check if the block is marked as reserved.
586 static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
588 struct nand_chip *chip = mtd_to_nand(mtd);
592 /* Return info from the table */
593 return nand_isreserved_bbt(mtd, ofs);
597 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
598 * @mtd: MTD device structure
599 * @ofs: offset from device start
600 * @allowbbt: 1, if its allowed to access the bbt area
602 * Check, if the block is bad. Either by reading the bad block table or
603 * calling of the scan function.
605 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
607 struct nand_chip *chip = mtd_to_nand(mtd);
610 return chip->block_bad(mtd, ofs);
612 /* Return info from the table */
613 return nand_isbad_bbt(mtd, ofs, allowbbt);
617 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
618 * @mtd: MTD device structure
621 * Helper function for nand_wait_ready used when needing to wait in interrupt
624 static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
626 struct nand_chip *chip = mtd_to_nand(mtd);
629 /* Wait for the device to get ready */
630 for (i = 0; i < timeo; i++) {
631 if (chip->dev_ready(mtd))
633 touch_softlockup_watchdog();
639 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
640 * @mtd: MTD device structure
642 * Wait for the ready pin after a command, and warn if a timeout occurs.
644 void nand_wait_ready(struct mtd_info *mtd)
646 struct nand_chip *chip = mtd_to_nand(mtd);
647 unsigned long timeo = 400;
649 if (in_interrupt() || oops_in_progress)
650 return panic_nand_wait_ready(mtd, timeo);
652 /* Wait until command is processed or timeout occurs */
653 timeo = jiffies + msecs_to_jiffies(timeo);
655 if (chip->dev_ready(mtd))
658 } while (time_before(jiffies, timeo));
660 if (!chip->dev_ready(mtd))
661 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
663 EXPORT_SYMBOL_GPL(nand_wait_ready);
666 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
667 * @mtd: MTD device structure
668 * @timeo: Timeout in ms
670 * Wait for status ready (i.e. command done) or timeout.
672 static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
674 register struct nand_chip *chip = mtd_to_nand(mtd);
677 timeo = jiffies + msecs_to_jiffies(timeo);
681 ret = nand_read_data_op(chip, &status, sizeof(status), true);
685 if (status & NAND_STATUS_READY)
687 touch_softlockup_watchdog();
688 } while (time_before(jiffies, timeo));
692 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
693 * @chip: NAND chip structure
694 * @timeout_ms: Timeout in ms
696 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
697 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
700 * This helper is intended to be used when the controller does not have access
701 * to the NAND R/B pin.
703 * Be aware that calling this helper from an ->exec_op() implementation means
704 * ->exec_op() must be re-entrant.
706 * Return 0 if the NAND chip is ready, a negative error otherwise.
708 int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
716 ret = nand_status_op(chip, NULL);
720 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
722 ret = nand_read_data_op(chip, &status, sizeof(status), true);
726 if (status & NAND_STATUS_READY)
730 * Typical lowest execution time for a tR on most NANDs is 10us,
731 * use this as polling delay before doing something smarter (ie.
732 * deriving a delay from the timeout value, timeout_ms/ratio).
735 } while (time_before(jiffies, timeout_ms));
738 * We have to exit READ_STATUS mode in order to read real data on the
739 * bus in case the WAITRDY instruction is preceding a DATA_IN
742 nand_exit_status_op(chip);
747 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
749 EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
752 * nand_command - [DEFAULT] Send command to NAND device
753 * @mtd: MTD device structure
754 * @command: the command to be sent
755 * @column: the column address for this command, -1 if none
756 * @page_addr: the page address for this command, -1 if none
758 * Send command to NAND device. This function is used for small page devices
759 * (512 Bytes per page).
761 static void nand_command(struct mtd_info *mtd, unsigned int command,
762 int column, int page_addr)
764 register struct nand_chip *chip = mtd_to_nand(mtd);
765 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
767 /* Write out the command to the device */
768 if (command == NAND_CMD_SEQIN) {
771 if (column >= mtd->writesize) {
773 column -= mtd->writesize;
774 readcmd = NAND_CMD_READOOB;
775 } else if (column < 256) {
776 /* First 256 bytes --> READ0 */
777 readcmd = NAND_CMD_READ0;
780 readcmd = NAND_CMD_READ1;
782 chip->cmd_ctrl(mtd, readcmd, ctrl);
783 ctrl &= ~NAND_CTRL_CHANGE;
785 if (command != NAND_CMD_NONE)
786 chip->cmd_ctrl(mtd, command, ctrl);
788 /* Address cycle, when necessary */
789 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
790 /* Serially input address */
792 /* Adjust columns for 16 bit buswidth */
793 if (chip->options & NAND_BUSWIDTH_16 &&
794 !nand_opcode_8bits(command))
796 chip->cmd_ctrl(mtd, column, ctrl);
797 ctrl &= ~NAND_CTRL_CHANGE;
799 if (page_addr != -1) {
800 chip->cmd_ctrl(mtd, page_addr, ctrl);
801 ctrl &= ~NAND_CTRL_CHANGE;
802 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
803 if (chip->options & NAND_ROW_ADDR_3)
804 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
806 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
809 * Program and erase have their own busy handlers status and sequential
815 case NAND_CMD_PAGEPROG:
816 case NAND_CMD_ERASE1:
817 case NAND_CMD_ERASE2:
819 case NAND_CMD_STATUS:
820 case NAND_CMD_READID:
821 case NAND_CMD_SET_FEATURES:
827 udelay(chip->chip_delay);
828 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
829 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
831 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
832 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
833 nand_wait_status_ready(mtd, 250);
836 /* This applies to read commands */
839 * READ0 is sometimes used to exit GET STATUS mode. When this
840 * is the case no address cycles are requested, and we can use
841 * this information to detect that we should not wait for the
842 * device to be ready.
844 if (column == -1 && page_addr == -1)
849 * If we don't have access to the busy pin, we apply the given
852 if (!chip->dev_ready) {
853 udelay(chip->chip_delay);
858 * Apply this short delay always to ensure that we do wait tWB in
859 * any case on any machine.
863 nand_wait_ready(mtd);
866 static void nand_ccs_delay(struct nand_chip *chip)
869 * The controller already takes care of waiting for tCCS when the RNDIN
870 * or RNDOUT command is sent, return directly.
872 if (!(chip->options & NAND_WAIT_TCCS))
876 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
877 * (which should be safe for all NANDs).
879 if (chip->setup_data_interface)
880 ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
886 * nand_command_lp - [DEFAULT] Send command to NAND large page device
887 * @mtd: MTD device structure
888 * @command: the command to be sent
889 * @column: the column address for this command, -1 if none
890 * @page_addr: the page address for this command, -1 if none
892 * Send command to NAND device. This is the version for the new large page
893 * devices. We don't have the separate regions as we have in the small page
894 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
896 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
897 int column, int page_addr)
899 register struct nand_chip *chip = mtd_to_nand(mtd);
901 /* Emulate NAND_CMD_READOOB */
902 if (command == NAND_CMD_READOOB) {
903 column += mtd->writesize;
904 command = NAND_CMD_READ0;
907 /* Command latch cycle */
908 if (command != NAND_CMD_NONE)
909 chip->cmd_ctrl(mtd, command,
910 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
912 if (column != -1 || page_addr != -1) {
913 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
915 /* Serially input address */
917 /* Adjust columns for 16 bit buswidth */
918 if (chip->options & NAND_BUSWIDTH_16 &&
919 !nand_opcode_8bits(command))
921 chip->cmd_ctrl(mtd, column, ctrl);
922 ctrl &= ~NAND_CTRL_CHANGE;
924 /* Only output a single addr cycle for 8bits opcodes. */
925 if (!nand_opcode_8bits(command))
926 chip->cmd_ctrl(mtd, column >> 8, ctrl);
928 if (page_addr != -1) {
929 chip->cmd_ctrl(mtd, page_addr, ctrl);
930 chip->cmd_ctrl(mtd, page_addr >> 8,
931 NAND_NCE | NAND_ALE);
932 if (chip->options & NAND_ROW_ADDR_3)
933 chip->cmd_ctrl(mtd, page_addr >> 16,
934 NAND_NCE | NAND_ALE);
937 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
940 * Program and erase have their own busy handlers status, sequential
941 * in and status need no delay.
946 case NAND_CMD_CACHEDPROG:
947 case NAND_CMD_PAGEPROG:
948 case NAND_CMD_ERASE1:
949 case NAND_CMD_ERASE2:
951 case NAND_CMD_STATUS:
952 case NAND_CMD_READID:
953 case NAND_CMD_SET_FEATURES:
957 nand_ccs_delay(chip);
963 udelay(chip->chip_delay);
964 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
965 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
966 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
967 NAND_NCE | NAND_CTRL_CHANGE);
968 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
969 nand_wait_status_ready(mtd, 250);
972 case NAND_CMD_RNDOUT:
973 /* No ready / busy check necessary */
974 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
975 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
976 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
977 NAND_NCE | NAND_CTRL_CHANGE);
979 nand_ccs_delay(chip);
984 * READ0 is sometimes used to exit GET STATUS mode. When this
985 * is the case no address cycles are requested, and we can use
986 * this information to detect that READSTART should not be
989 if (column == -1 && page_addr == -1)
992 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
993 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
994 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
995 NAND_NCE | NAND_CTRL_CHANGE);
997 /* This applies to read commands */
1000 * If we don't have access to the busy pin, we apply the given
1003 if (!chip->dev_ready) {
1004 udelay(chip->chip_delay);
1010 * Apply this short delay always to ensure that we do wait tWB in
1011 * any case on any machine.
1015 nand_wait_ready(mtd);
1019 * panic_nand_get_device - [GENERIC] Get chip for selected access
1020 * @chip: the nand chip descriptor
1021 * @mtd: MTD device structure
1022 * @new_state: the state which is requested
1024 * Used when in panic, no locks are taken.
1026 static void panic_nand_get_device(struct nand_chip *chip,
1027 struct mtd_info *mtd, int new_state)
1029 /* Hardware controller shared among independent devices */
1030 chip->controller->active = chip;
1031 chip->state = new_state;
1035 * nand_get_device - [GENERIC] Get chip for selected access
1036 * @mtd: MTD device structure
1037 * @new_state: the state which is requested
1039 * Get the device and lock it for exclusive access
1042 nand_get_device(struct mtd_info *mtd, int new_state)
1044 struct nand_chip *chip = mtd_to_nand(mtd);
1045 spinlock_t *lock = &chip->controller->lock;
1046 wait_queue_head_t *wq = &chip->controller->wq;
1047 DECLARE_WAITQUEUE(wait, current);
1051 /* Hardware controller shared among independent devices */
1052 if (!chip->controller->active)
1053 chip->controller->active = chip;
1055 if (chip->controller->active == chip && chip->state == FL_READY) {
1056 chip->state = new_state;
1060 if (new_state == FL_PM_SUSPENDED) {
1061 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1062 chip->state = FL_PM_SUSPENDED;
1067 set_current_state(TASK_UNINTERRUPTIBLE);
1068 add_wait_queue(wq, &wait);
1071 remove_wait_queue(wq, &wait);
1076 * panic_nand_wait - [GENERIC] wait until the command is done
1077 * @mtd: MTD device structure
1078 * @chip: NAND chip structure
1081 * Wait for command done. This is a helper function for nand_wait used when
1082 * we are in interrupt context. May happen when in panic and trying to write
1083 * an oops through mtdoops.
1085 static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1086 unsigned long timeo)
1089 for (i = 0; i < timeo; i++) {
1090 if (chip->dev_ready) {
1091 if (chip->dev_ready(mtd))
1097 ret = nand_read_data_op(chip, &status, sizeof(status),
1102 if (status & NAND_STATUS_READY)
1110 * nand_wait - [DEFAULT] wait until the command is done
1111 * @mtd: MTD device structure
1112 * @chip: NAND chip structure
1114 * Wait for command done. This applies to erase and program only.
1116 static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
1119 unsigned long timeo = 400;
1124 * Apply this short delay always to ensure that we do wait tWB in any
1125 * case on any machine.
1129 ret = nand_status_op(chip, NULL);
1133 if (in_interrupt() || oops_in_progress)
1134 panic_nand_wait(mtd, chip, timeo);
1136 timeo = jiffies + msecs_to_jiffies(timeo);
1138 if (chip->dev_ready) {
1139 if (chip->dev_ready(mtd))
1142 ret = nand_read_data_op(chip, &status,
1143 sizeof(status), true);
1147 if (status & NAND_STATUS_READY)
1151 } while (time_before(jiffies, timeo));
1154 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1158 /* This can happen if in case of timeout or buggy dev_ready */
1159 WARN_ON(!(status & NAND_STATUS_READY));
1163 static bool nand_supports_get_features(struct nand_chip *chip, int addr)
1165 return (chip->parameters.supports_set_get_features &&
1166 test_bit(addr, chip->parameters.get_feature_list));
1169 static bool nand_supports_set_features(struct nand_chip *chip, int addr)
1171 return (chip->parameters.supports_set_get_features &&
1172 test_bit(addr, chip->parameters.set_feature_list));
1176 * nand_get_features - wrapper to perform a GET_FEATURE
1177 * @chip: NAND chip info structure
1178 * @addr: feature address
1179 * @subfeature_param: the subfeature parameters, a four bytes array
1181 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1182 * operation cannot be handled.
1184 int nand_get_features(struct nand_chip *chip, int addr,
1185 u8 *subfeature_param)
1187 struct mtd_info *mtd = nand_to_mtd(chip);
1189 if (!nand_supports_get_features(chip, addr))
1192 return chip->get_features(mtd, chip, addr, subfeature_param);
1194 EXPORT_SYMBOL_GPL(nand_get_features);
1197 * nand_set_features - wrapper to perform a SET_FEATURE
1198 * @chip: NAND chip info structure
1199 * @addr: feature address
1200 * @subfeature_param: the subfeature parameters, a four bytes array
1202 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1203 * operation cannot be handled.
1205 int nand_set_features(struct nand_chip *chip, int addr,
1206 u8 *subfeature_param)
1208 struct mtd_info *mtd = nand_to_mtd(chip);
1210 if (!nand_supports_set_features(chip, addr))
1213 return chip->set_features(mtd, chip, addr, subfeature_param);
1215 EXPORT_SYMBOL_GPL(nand_set_features);
1218 * nand_reset_data_interface - Reset data interface and timings
1219 * @chip: The NAND chip
1220 * @chipnr: Internal die id
1222 * Reset the Data interface and timings to ONFI mode 0.
1224 * Returns 0 for success or negative error code otherwise.
1226 static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
1228 struct mtd_info *mtd = nand_to_mtd(chip);
1231 if (!chip->setup_data_interface)
1235 * The ONFI specification says:
1237 * To transition from NV-DDR or NV-DDR2 to the SDR data
1238 * interface, the host shall use the Reset (FFh) command
1239 * using SDR timing mode 0. A device in any timing mode is
1240 * required to recognize Reset (FFh) command issued in SDR
1244 * Configure the data interface in SDR mode and set the
1245 * timings to timing mode 0.
1248 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
1249 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
1251 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1257 * nand_setup_data_interface - Setup the best data interface and timings
1258 * @chip: The NAND chip
1259 * @chipnr: Internal die id
1261 * Find and configure the best data interface and NAND timings supported by
1262 * the chip and the driver.
1263 * First tries to retrieve supported timing modes from ONFI information,
1264 * and if the NAND chip does not support ONFI, relies on the
1265 * ->onfi_timing_mode_default specified in the nand_ids table.
1267 * Returns 0 for success or negative error code otherwise.
1269 static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
1271 struct mtd_info *mtd = nand_to_mtd(chip);
1272 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1273 chip->onfi_timing_mode_default,
1277 if (!chip->setup_data_interface)
1280 /* Change the mode on the chip side (if supported by the NAND chip) */
1281 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
1282 chip->select_chip(mtd, chipnr);
1283 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1285 chip->select_chip(mtd, -1);
1290 /* Change the mode on the controller side */
1291 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
1295 /* Check the mode has been accepted by the chip, if supported */
1296 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
1299 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
1300 chip->select_chip(mtd, chipnr);
1301 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1303 chip->select_chip(mtd, -1);
1305 goto err_reset_chip;
1307 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1308 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1309 chip->onfi_timing_mode_default);
1310 goto err_reset_chip;
1317 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1320 nand_reset_data_interface(chip, chipnr);
1321 chip->select_chip(mtd, chipnr);
1322 nand_reset_op(chip);
1323 chip->select_chip(mtd, -1);
1329 * nand_init_data_interface - find the best data interface and timings
1330 * @chip: The NAND chip
1332 * Find the best data interface and NAND timings supported by the chip
1334 * First tries to retrieve supported timing modes from ONFI information,
1335 * and if the NAND chip does not support ONFI, relies on the
1336 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1337 * function nand_chip->data_interface is initialized with the best timing mode
1340 * Returns 0 for success or negative error code otherwise.
1342 static int nand_init_data_interface(struct nand_chip *chip)
1344 struct mtd_info *mtd = nand_to_mtd(chip);
1345 int modes, mode, ret;
1347 if (!chip->setup_data_interface)
1351 * First try to identify the best timings from ONFI parameters and
1352 * if the NAND does not support ONFI, fallback to the default ONFI
1355 modes = onfi_get_async_timing_mode(chip);
1356 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1357 if (!chip->onfi_timing_mode_default)
1360 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1364 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1365 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
1370 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1371 * controller supports the requested timings.
1373 ret = chip->setup_data_interface(mtd,
1374 NAND_DATA_IFACE_CHECK_ONLY,
1375 &chip->data_interface);
1377 chip->onfi_timing_mode_default = mode;
1386 * nand_fill_column_cycles - fill the column cycles of an address
1387 * @chip: The NAND chip
1388 * @addrs: Array of address cycles to fill
1389 * @offset_in_page: The offset in the page
1391 * Fills the first or the first two bytes of the @addrs field depending
1392 * on the NAND bus width and the page size.
1394 * Returns the number of cycles needed to encode the column, or a negative
1395 * error code in case one of the arguments is invalid.
1397 static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1398 unsigned int offset_in_page)
1400 struct mtd_info *mtd = nand_to_mtd(chip);
1402 /* Make sure the offset is less than the actual page size. */
1403 if (offset_in_page > mtd->writesize + mtd->oobsize)
1407 * On small page NANDs, there's a dedicated command to access the OOB
1408 * area, and the column address is relative to the start of the OOB
1409 * area, not the start of the page. Asjust the address accordingly.
1411 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1412 offset_in_page -= mtd->writesize;
1415 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1416 * wide, then it must be divided by 2.
1418 if (chip->options & NAND_BUSWIDTH_16) {
1419 if (WARN_ON(offset_in_page % 2))
1422 offset_in_page /= 2;
1425 addrs[0] = offset_in_page;
1428 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1431 if (mtd->writesize <= 512)
1434 addrs[1] = offset_in_page >> 8;
1439 static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1440 unsigned int offset_in_page, void *buf,
1443 struct mtd_info *mtd = nand_to_mtd(chip);
1444 const struct nand_sdr_timings *sdr =
1445 nand_get_sdr_timings(&chip->data_interface);
1447 struct nand_op_instr instrs[] = {
1448 NAND_OP_CMD(NAND_CMD_READ0, 0),
1449 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1450 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1451 PSEC_TO_NSEC(sdr->tRR_min)),
1452 NAND_OP_DATA_IN(len, buf, 0),
1454 struct nand_operation op = NAND_OPERATION(instrs);
1457 /* Drop the DATA_IN instruction if len is set to 0. */
1461 if (offset_in_page >= mtd->writesize)
1462 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1463 else if (offset_in_page >= 256 &&
1464 !(chip->options & NAND_BUSWIDTH_16))
1465 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1467 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1472 addrs[2] = page >> 8;
1474 if (chip->options & NAND_ROW_ADDR_3) {
1475 addrs[3] = page >> 16;
1476 instrs[1].ctx.addr.naddrs++;
1479 return nand_exec_op(chip, &op);
1482 static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1483 unsigned int offset_in_page, void *buf,
1486 const struct nand_sdr_timings *sdr =
1487 nand_get_sdr_timings(&chip->data_interface);
1489 struct nand_op_instr instrs[] = {
1490 NAND_OP_CMD(NAND_CMD_READ0, 0),
1491 NAND_OP_ADDR(4, addrs, 0),
1492 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1493 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1494 PSEC_TO_NSEC(sdr->tRR_min)),
1495 NAND_OP_DATA_IN(len, buf, 0),
1497 struct nand_operation op = NAND_OPERATION(instrs);
1500 /* Drop the DATA_IN instruction if len is set to 0. */
1504 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1509 addrs[3] = page >> 8;
1511 if (chip->options & NAND_ROW_ADDR_3) {
1512 addrs[4] = page >> 16;
1513 instrs[1].ctx.addr.naddrs++;
1516 return nand_exec_op(chip, &op);
1520 * nand_read_page_op - Do a READ PAGE operation
1521 * @chip: The NAND chip
1522 * @page: page to read
1523 * @offset_in_page: offset within the page
1524 * @buf: buffer used to store the data
1525 * @len: length of the buffer
1527 * This function issues a READ PAGE operation.
1528 * This function does not select/unselect the CS line.
1530 * Returns 0 on success, a negative error code otherwise.
1532 int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1533 unsigned int offset_in_page, void *buf, unsigned int len)
1535 struct mtd_info *mtd = nand_to_mtd(chip);
1540 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1543 if (chip->exec_op) {
1544 if (mtd->writesize > 512)
1545 return nand_lp_exec_read_page_op(chip, page,
1546 offset_in_page, buf,
1549 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1553 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1555 chip->read_buf(mtd, buf, len);
1559 EXPORT_SYMBOL_GPL(nand_read_page_op);
1562 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1563 * @chip: The NAND chip
1564 * @page: parameter page to read
1565 * @buf: buffer used to store the data
1566 * @len: length of the buffer
1568 * This function issues a READ PARAMETER PAGE operation.
1569 * This function does not select/unselect the CS line.
1571 * Returns 0 on success, a negative error code otherwise.
1573 static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1576 struct mtd_info *mtd = nand_to_mtd(chip);
1583 if (chip->exec_op) {
1584 const struct nand_sdr_timings *sdr =
1585 nand_get_sdr_timings(&chip->data_interface);
1586 struct nand_op_instr instrs[] = {
1587 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1588 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1589 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1590 PSEC_TO_NSEC(sdr->tRR_min)),
1591 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1593 struct nand_operation op = NAND_OPERATION(instrs);
1595 /* Drop the DATA_IN instruction if len is set to 0. */
1599 return nand_exec_op(chip, &op);
1602 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1603 for (i = 0; i < len; i++)
1604 p[i] = chip->read_byte(mtd);
1610 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1611 * @chip: The NAND chip
1612 * @offset_in_page: offset within the page
1613 * @buf: buffer used to store the data
1614 * @len: length of the buffer
1615 * @force_8bit: force 8-bit bus access
1617 * This function issues a CHANGE READ COLUMN operation.
1618 * This function does not select/unselect the CS line.
1620 * Returns 0 on success, a negative error code otherwise.
1622 int nand_change_read_column_op(struct nand_chip *chip,
1623 unsigned int offset_in_page, void *buf,
1624 unsigned int len, bool force_8bit)
1626 struct mtd_info *mtd = nand_to_mtd(chip);
1631 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1634 /* Small page NANDs do not support column change. */
1635 if (mtd->writesize <= 512)
1638 if (chip->exec_op) {
1639 const struct nand_sdr_timings *sdr =
1640 nand_get_sdr_timings(&chip->data_interface);
1642 struct nand_op_instr instrs[] = {
1643 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1644 NAND_OP_ADDR(2, addrs, 0),
1645 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1646 PSEC_TO_NSEC(sdr->tCCS_min)),
1647 NAND_OP_DATA_IN(len, buf, 0),
1649 struct nand_operation op = NAND_OPERATION(instrs);
1652 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1656 /* Drop the DATA_IN instruction if len is set to 0. */
1660 instrs[3].ctx.data.force_8bit = force_8bit;
1662 return nand_exec_op(chip, &op);
1665 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1667 chip->read_buf(mtd, buf, len);
1671 EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1674 * nand_read_oob_op - Do a READ OOB operation
1675 * @chip: The NAND chip
1676 * @page: page to read
1677 * @offset_in_oob: offset within the OOB area
1678 * @buf: buffer used to store the data
1679 * @len: length of the buffer
1681 * This function issues a READ OOB operation.
1682 * This function does not select/unselect the CS line.
1684 * Returns 0 on success, a negative error code otherwise.
1686 int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1687 unsigned int offset_in_oob, void *buf, unsigned int len)
1689 struct mtd_info *mtd = nand_to_mtd(chip);
1694 if (offset_in_oob + len > mtd->oobsize)
1698 return nand_read_page_op(chip, page,
1699 mtd->writesize + offset_in_oob,
1702 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1704 chip->read_buf(mtd, buf, len);
1708 EXPORT_SYMBOL_GPL(nand_read_oob_op);
1710 static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1711 unsigned int offset_in_page, const void *buf,
1712 unsigned int len, bool prog)
1714 struct mtd_info *mtd = nand_to_mtd(chip);
1715 const struct nand_sdr_timings *sdr =
1716 nand_get_sdr_timings(&chip->data_interface);
1718 struct nand_op_instr instrs[] = {
1720 * The first instruction will be dropped if we're dealing
1721 * with a large page NAND and adjusted if we're dealing
1722 * with a small page NAND and the page offset is > 255.
1724 NAND_OP_CMD(NAND_CMD_READ0, 0),
1725 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1726 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1727 NAND_OP_DATA_OUT(len, buf, 0),
1728 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1729 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1731 struct nand_operation op = NAND_OPERATION(instrs);
1732 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1739 addrs[naddrs++] = page;
1740 addrs[naddrs++] = page >> 8;
1741 if (chip->options & NAND_ROW_ADDR_3)
1742 addrs[naddrs++] = page >> 16;
1744 instrs[2].ctx.addr.naddrs = naddrs;
1746 /* Drop the last two instructions if we're not programming the page. */
1749 /* Also drop the DATA_OUT instruction if empty. */
1754 if (mtd->writesize <= 512) {
1756 * Small pages need some more tweaking: we have to adjust the
1757 * first instruction depending on the page offset we're trying
1760 if (offset_in_page >= mtd->writesize)
1761 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1762 else if (offset_in_page >= 256 &&
1763 !(chip->options & NAND_BUSWIDTH_16))
1764 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1767 * Drop the first command if we're dealing with a large page
1774 ret = nand_exec_op(chip, &op);
1778 ret = nand_status_op(chip, &status);
1786 * nand_prog_page_begin_op - starts a PROG PAGE operation
1787 * @chip: The NAND chip
1788 * @page: page to write
1789 * @offset_in_page: offset within the page
1790 * @buf: buffer containing the data to write to the page
1791 * @len: length of the buffer
1793 * This function issues the first half of a PROG PAGE operation.
1794 * This function does not select/unselect the CS line.
1796 * Returns 0 on success, a negative error code otherwise.
1798 int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1799 unsigned int offset_in_page, const void *buf,
1802 struct mtd_info *mtd = nand_to_mtd(chip);
1807 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1811 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1814 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1817 chip->write_buf(mtd, buf, len);
1821 EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1824 * nand_prog_page_end_op - ends a PROG PAGE operation
1825 * @chip: The NAND chip
1827 * This function issues the second half of a PROG PAGE operation.
1828 * This function does not select/unselect the CS line.
1830 * Returns 0 on success, a negative error code otherwise.
1832 int nand_prog_page_end_op(struct nand_chip *chip)
1834 struct mtd_info *mtd = nand_to_mtd(chip);
1838 if (chip->exec_op) {
1839 const struct nand_sdr_timings *sdr =
1840 nand_get_sdr_timings(&chip->data_interface);
1841 struct nand_op_instr instrs[] = {
1842 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1843 PSEC_TO_NSEC(sdr->tWB_max)),
1844 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1846 struct nand_operation op = NAND_OPERATION(instrs);
1848 ret = nand_exec_op(chip, &op);
1852 ret = nand_status_op(chip, &status);
1856 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1857 ret = chip->waitfunc(mtd, chip);
1864 if (status & NAND_STATUS_FAIL)
1869 EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1872 * nand_prog_page_op - Do a full PROG PAGE operation
1873 * @chip: The NAND chip
1874 * @page: page to write
1875 * @offset_in_page: offset within the page
1876 * @buf: buffer containing the data to write to the page
1877 * @len: length of the buffer
1879 * This function issues a full PROG PAGE operation.
1880 * This function does not select/unselect the CS line.
1882 * Returns 0 on success, a negative error code otherwise.
1884 int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1885 unsigned int offset_in_page, const void *buf,
1888 struct mtd_info *mtd = nand_to_mtd(chip);
1894 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1897 if (chip->exec_op) {
1898 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1901 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1902 chip->write_buf(mtd, buf, len);
1903 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1904 status = chip->waitfunc(mtd, chip);
1907 if (status & NAND_STATUS_FAIL)
1912 EXPORT_SYMBOL_GPL(nand_prog_page_op);
1915 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1916 * @chip: The NAND chip
1917 * @offset_in_page: offset within the page
1918 * @buf: buffer containing the data to send to the NAND
1919 * @len: length of the buffer
1920 * @force_8bit: force 8-bit bus access
1922 * This function issues a CHANGE WRITE COLUMN operation.
1923 * This function does not select/unselect the CS line.
1925 * Returns 0 on success, a negative error code otherwise.
1927 int nand_change_write_column_op(struct nand_chip *chip,
1928 unsigned int offset_in_page,
1929 const void *buf, unsigned int len,
1932 struct mtd_info *mtd = nand_to_mtd(chip);
1937 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1940 /* Small page NANDs do not support column change. */
1941 if (mtd->writesize <= 512)
1944 if (chip->exec_op) {
1945 const struct nand_sdr_timings *sdr =
1946 nand_get_sdr_timings(&chip->data_interface);
1948 struct nand_op_instr instrs[] = {
1949 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1950 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1951 NAND_OP_DATA_OUT(len, buf, 0),
1953 struct nand_operation op = NAND_OPERATION(instrs);
1956 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1960 instrs[2].ctx.data.force_8bit = force_8bit;
1962 /* Drop the DATA_OUT instruction if len is set to 0. */
1966 return nand_exec_op(chip, &op);
1969 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1971 chip->write_buf(mtd, buf, len);
1975 EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1978 * nand_readid_op - Do a READID operation
1979 * @chip: The NAND chip
1980 * @addr: address cycle to pass after the READID command
1981 * @buf: buffer used to store the ID
1982 * @len: length of the buffer
1984 * This function sends a READID command and reads back the ID returned by the
1986 * This function does not select/unselect the CS line.
1988 * Returns 0 on success, a negative error code otherwise.
1990 int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1993 struct mtd_info *mtd = nand_to_mtd(chip);
2000 if (chip->exec_op) {
2001 const struct nand_sdr_timings *sdr =
2002 nand_get_sdr_timings(&chip->data_interface);
2003 struct nand_op_instr instrs[] = {
2004 NAND_OP_CMD(NAND_CMD_READID, 0),
2005 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
2006 NAND_OP_8BIT_DATA_IN(len, buf, 0),
2008 struct nand_operation op = NAND_OPERATION(instrs);
2010 /* Drop the DATA_IN instruction if len is set to 0. */
2014 return nand_exec_op(chip, &op);
2017 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
2019 for (i = 0; i < len; i++)
2020 id[i] = chip->read_byte(mtd);
2024 EXPORT_SYMBOL_GPL(nand_readid_op);
2027 * nand_status_op - Do a STATUS operation
2028 * @chip: The NAND chip
2029 * @status: out variable to store the NAND status
2031 * This function sends a STATUS command and reads back the status returned by
2033 * This function does not select/unselect the CS line.
2035 * Returns 0 on success, a negative error code otherwise.
2037 int nand_status_op(struct nand_chip *chip, u8 *status)
2039 struct mtd_info *mtd = nand_to_mtd(chip);
2041 if (chip->exec_op) {
2042 const struct nand_sdr_timings *sdr =
2043 nand_get_sdr_timings(&chip->data_interface);
2044 struct nand_op_instr instrs[] = {
2045 NAND_OP_CMD(NAND_CMD_STATUS,
2046 PSEC_TO_NSEC(sdr->tADL_min)),
2047 NAND_OP_8BIT_DATA_IN(1, status, 0),
2049 struct nand_operation op = NAND_OPERATION(instrs);
2054 return nand_exec_op(chip, &op);
2057 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
2059 *status = chip->read_byte(mtd);
2063 EXPORT_SYMBOL_GPL(nand_status_op);
2066 * nand_exit_status_op - Exit a STATUS operation
2067 * @chip: The NAND chip
2069 * This function sends a READ0 command to cancel the effect of the STATUS
2070 * command to avoid reading only the status until a new read command is sent.
2072 * This function does not select/unselect the CS line.
2074 * Returns 0 on success, a negative error code otherwise.
2076 int nand_exit_status_op(struct nand_chip *chip)
2078 struct mtd_info *mtd = nand_to_mtd(chip);
2080 if (chip->exec_op) {
2081 struct nand_op_instr instrs[] = {
2082 NAND_OP_CMD(NAND_CMD_READ0, 0),
2084 struct nand_operation op = NAND_OPERATION(instrs);
2086 return nand_exec_op(chip, &op);
2089 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
2093 EXPORT_SYMBOL_GPL(nand_exit_status_op);
2096 * nand_erase_op - Do an erase operation
2097 * @chip: The NAND chip
2098 * @eraseblock: block to erase
2100 * This function sends an ERASE command and waits for the NAND to be ready
2102 * This function does not select/unselect the CS line.
2104 * Returns 0 on success, a negative error code otherwise.
2106 int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2108 struct mtd_info *mtd = nand_to_mtd(chip);
2109 unsigned int page = eraseblock <<
2110 (chip->phys_erase_shift - chip->page_shift);
2114 if (chip->exec_op) {
2115 const struct nand_sdr_timings *sdr =
2116 nand_get_sdr_timings(&chip->data_interface);
2117 u8 addrs[3] = { page, page >> 8, page >> 16 };
2118 struct nand_op_instr instrs[] = {
2119 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2120 NAND_OP_ADDR(2, addrs, 0),
2121 NAND_OP_CMD(NAND_CMD_ERASE2,
2122 PSEC_TO_MSEC(sdr->tWB_max)),
2123 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2125 struct nand_operation op = NAND_OPERATION(instrs);
2127 if (chip->options & NAND_ROW_ADDR_3)
2128 instrs[1].ctx.addr.naddrs++;
2130 ret = nand_exec_op(chip, &op);
2134 ret = nand_status_op(chip, &status);
2138 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2139 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2141 ret = chip->waitfunc(mtd, chip);
2148 if (status & NAND_STATUS_FAIL)
2153 EXPORT_SYMBOL_GPL(nand_erase_op);
2156 * nand_set_features_op - Do a SET FEATURES operation
2157 * @chip: The NAND chip
2158 * @feature: feature id
2159 * @data: 4 bytes of data
2161 * This function sends a SET FEATURES command and waits for the NAND to be
2162 * ready before returning.
2163 * This function does not select/unselect the CS line.
2165 * Returns 0 on success, a negative error code otherwise.
2167 static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2170 struct mtd_info *mtd = nand_to_mtd(chip);
2171 const u8 *params = data;
2175 if (chip->exec_op) {
2176 const struct nand_sdr_timings *sdr =
2177 nand_get_sdr_timings(&chip->data_interface);
2178 struct nand_op_instr instrs[] = {
2179 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2180 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2181 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2182 PSEC_TO_NSEC(sdr->tWB_max)),
2183 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2185 struct nand_operation op = NAND_OPERATION(instrs);
2187 ret = nand_exec_op(chip, &op);
2191 ret = nand_status_op(chip, &status);
2195 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
2196 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2197 chip->write_byte(mtd, params[i]);
2199 ret = chip->waitfunc(mtd, chip);
2206 if (status & NAND_STATUS_FAIL)
2213 * nand_get_features_op - Do a GET FEATURES operation
2214 * @chip: The NAND chip
2215 * @feature: feature id
2216 * @data: 4 bytes of data
2218 * This function sends a GET FEATURES command and waits for the NAND to be
2219 * ready before returning.
2220 * This function does not select/unselect the CS line.
2222 * Returns 0 on success, a negative error code otherwise.
2224 static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2227 struct mtd_info *mtd = nand_to_mtd(chip);
2231 if (chip->exec_op) {
2232 const struct nand_sdr_timings *sdr =
2233 nand_get_sdr_timings(&chip->data_interface);
2234 struct nand_op_instr instrs[] = {
2235 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2236 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2237 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2238 PSEC_TO_NSEC(sdr->tRR_min)),
2239 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2242 struct nand_operation op = NAND_OPERATION(instrs);
2244 return nand_exec_op(chip, &op);
2247 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
2248 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2249 params[i] = chip->read_byte(mtd);
2255 * nand_reset_op - Do a reset operation
2256 * @chip: The NAND chip
2258 * This function sends a RESET command and waits for the NAND to be ready
2260 * This function does not select/unselect the CS line.
2262 * Returns 0 on success, a negative error code otherwise.
2264 int nand_reset_op(struct nand_chip *chip)
2266 struct mtd_info *mtd = nand_to_mtd(chip);
2268 if (chip->exec_op) {
2269 const struct nand_sdr_timings *sdr =
2270 nand_get_sdr_timings(&chip->data_interface);
2271 struct nand_op_instr instrs[] = {
2272 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2273 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2275 struct nand_operation op = NAND_OPERATION(instrs);
2277 return nand_exec_op(chip, &op);
2280 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2284 EXPORT_SYMBOL_GPL(nand_reset_op);
2287 * nand_read_data_op - Read data from the NAND
2288 * @chip: The NAND chip
2289 * @buf: buffer used to store the data
2290 * @len: length of the buffer
2291 * @force_8bit: force 8-bit bus access
2293 * This function does a raw data read on the bus. Usually used after launching
2294 * another NAND operation like nand_read_page_op().
2295 * This function does not select/unselect the CS line.
2297 * Returns 0 on success, a negative error code otherwise.
2299 int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2302 struct mtd_info *mtd = nand_to_mtd(chip);
2307 if (chip->exec_op) {
2308 struct nand_op_instr instrs[] = {
2309 NAND_OP_DATA_IN(len, buf, 0),
2311 struct nand_operation op = NAND_OPERATION(instrs);
2313 instrs[0].ctx.data.force_8bit = force_8bit;
2315 return nand_exec_op(chip, &op);
2322 for (i = 0; i < len; i++)
2323 p[i] = chip->read_byte(mtd);
2325 chip->read_buf(mtd, buf, len);
2330 EXPORT_SYMBOL_GPL(nand_read_data_op);
2333 * nand_write_data_op - Write data from the NAND
2334 * @chip: The NAND chip
2335 * @buf: buffer containing the data to send on the bus
2336 * @len: length of the buffer
2337 * @force_8bit: force 8-bit bus access
2339 * This function does a raw data write on the bus. Usually used after launching
2340 * another NAND operation like nand_write_page_begin_op().
2341 * This function does not select/unselect the CS line.
2343 * Returns 0 on success, a negative error code otherwise.
2345 int nand_write_data_op(struct nand_chip *chip, const void *buf,
2346 unsigned int len, bool force_8bit)
2348 struct mtd_info *mtd = nand_to_mtd(chip);
2353 if (chip->exec_op) {
2354 struct nand_op_instr instrs[] = {
2355 NAND_OP_DATA_OUT(len, buf, 0),
2357 struct nand_operation op = NAND_OPERATION(instrs);
2359 instrs[0].ctx.data.force_8bit = force_8bit;
2361 return nand_exec_op(chip, &op);
2368 for (i = 0; i < len; i++)
2369 chip->write_byte(mtd, p[i]);
2371 chip->write_buf(mtd, buf, len);
2376 EXPORT_SYMBOL_GPL(nand_write_data_op);
2379 * struct nand_op_parser_ctx - Context used by the parser
2380 * @instrs: array of all the instructions that must be addressed
2381 * @ninstrs: length of the @instrs array
2382 * @subop: Sub-operation to be passed to the NAND controller
2384 * This structure is used by the core to split NAND operations into
2385 * sub-operations that can be handled by the NAND controller.
2387 struct nand_op_parser_ctx {
2388 const struct nand_op_instr *instrs;
2389 unsigned int ninstrs;
2390 struct nand_subop subop;
2394 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2395 * @pat: the parser pattern element that matches @instr
2396 * @instr: pointer to the instruction to check
2397 * @start_offset: this is an in/out parameter. If @instr has already been
2398 * split, then @start_offset is the offset from which to start
2399 * (either an address cycle or an offset in the data buffer).
2400 * Conversely, if the function returns true (ie. instr must be
2401 * split), this parameter is updated to point to the first
2402 * data/address cycle that has not been taken care of.
2404 * Some NAND controllers are limited and cannot send X address cycles with a
2405 * unique operation, or cannot read/write more than Y bytes at the same time.
2406 * In this case, split the instruction that does not fit in a single
2407 * controller-operation into two or more chunks.
2409 * Returns true if the instruction must be split, false otherwise.
2410 * The @start_offset parameter is also updated to the offset at which the next
2411 * bundle of instruction must start (if an address or a data instruction).
2414 nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2415 const struct nand_op_instr *instr,
2416 unsigned int *start_offset)
2418 switch (pat->type) {
2419 case NAND_OP_ADDR_INSTR:
2420 if (!pat->ctx.addr.maxcycles)
2423 if (instr->ctx.addr.naddrs - *start_offset >
2424 pat->ctx.addr.maxcycles) {
2425 *start_offset += pat->ctx.addr.maxcycles;
2430 case NAND_OP_DATA_IN_INSTR:
2431 case NAND_OP_DATA_OUT_INSTR:
2432 if (!pat->ctx.data.maxlen)
2435 if (instr->ctx.data.len - *start_offset >
2436 pat->ctx.data.maxlen) {
2437 *start_offset += pat->ctx.data.maxlen;
2450 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2451 * remaining in the parser context
2452 * @pat: the pattern to test
2453 * @ctx: the parser context structure to match with the pattern @pat
2455 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2456 * Returns true if this is the case, false ortherwise. When true is returned,
2457 * @ctx->subop is updated with the set of instructions to be passed to the
2458 * controller driver.
2461 nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2462 struct nand_op_parser_ctx *ctx)
2464 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2465 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2466 const struct nand_op_instr *instr = ctx->subop.instrs;
2467 unsigned int i, ninstrs;
2469 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2471 * The pattern instruction does not match the operation
2472 * instruction. If the instruction is marked optional in the
2473 * pattern definition, we skip the pattern element and continue
2474 * to the next one. If the element is mandatory, there's no
2475 * match and we can return false directly.
2477 if (instr->type != pat->elems[i].type) {
2478 if (!pat->elems[i].optional)
2485 * Now check the pattern element constraints. If the pattern is
2486 * not able to handle the whole instruction in a single step,
2487 * we have to split it.
2488 * The last_instr_end_off value comes back updated to point to
2489 * the position where we have to split the instruction (the
2490 * start of the next subop chunk).
2492 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2505 * This can happen if all instructions of a pattern are optional.
2506 * Still, if there's not at least one instruction handled by this
2507 * pattern, this is not a match, and we should try the next one (if
2514 * We had a match on the pattern head, but the pattern may be longer
2515 * than the instructions we're asked to execute. We need to make sure
2516 * there's no mandatory elements in the pattern tail.
2518 for (; i < pat->nelems; i++) {
2519 if (!pat->elems[i].optional)
2524 * We have a match: update the subop structure accordingly and return
2527 ctx->subop.ninstrs = ninstrs;
2528 ctx->subop.last_instr_end_off = instr_offset;
2533 #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2534 static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2536 const struct nand_op_instr *instr;
2540 pr_debug("executing subop:\n");
2542 for (i = 0; i < ctx->ninstrs; i++) {
2543 instr = &ctx->instrs[i];
2545 if (instr == &ctx->subop.instrs[0])
2548 switch (instr->type) {
2549 case NAND_OP_CMD_INSTR:
2550 pr_debug("%sCMD [0x%02x]\n", prefix,
2551 instr->ctx.cmd.opcode);
2553 case NAND_OP_ADDR_INSTR:
2554 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2555 instr->ctx.addr.naddrs,
2556 instr->ctx.addr.naddrs < 64 ?
2557 instr->ctx.addr.naddrs : 64,
2558 instr->ctx.addr.addrs);
2560 case NAND_OP_DATA_IN_INSTR:
2561 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2562 instr->ctx.data.len,
2563 instr->ctx.data.force_8bit ?
2564 ", force 8-bit" : "");
2566 case NAND_OP_DATA_OUT_INSTR:
2567 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2568 instr->ctx.data.len,
2569 instr->ctx.data.force_8bit ?
2570 ", force 8-bit" : "");
2572 case NAND_OP_WAITRDY_INSTR:
2573 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2574 instr->ctx.waitrdy.timeout_ms);
2578 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2583 static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2590 * nand_op_parser_exec_op - exec_op parser
2591 * @chip: the NAND chip
2592 * @parser: patterns description provided by the controller driver
2593 * @op: the NAND operation to address
2594 * @check_only: when true, the function only checks if @op can be handled but
2595 * does not execute the operation
2597 * Helper function designed to ease integration of NAND controller drivers that
2598 * only support a limited set of instruction sequences. The supported sequences
2599 * are described in @parser, and the framework takes care of splitting @op into
2600 * multiple sub-operations (if required) and pass them back to the ->exec()
2601 * callback of the matching pattern if @check_only is set to false.
2603 * NAND controller drivers should call this function from their own ->exec_op()
2606 * Returns 0 on success, a negative error code otherwise. A failure can be
2607 * caused by an unsupported operation (none of the supported patterns is able
2608 * to handle the requested operation), or an error returned by one of the
2609 * matching pattern->exec() hook.
2611 int nand_op_parser_exec_op(struct nand_chip *chip,
2612 const struct nand_op_parser *parser,
2613 const struct nand_operation *op, bool check_only)
2615 struct nand_op_parser_ctx ctx = {
2616 .subop.instrs = op->instrs,
2617 .instrs = op->instrs,
2618 .ninstrs = op->ninstrs,
2622 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2625 for (i = 0; i < parser->npatterns; i++) {
2626 const struct nand_op_parser_pattern *pattern;
2628 pattern = &parser->patterns[i];
2629 if (!nand_op_parser_match_pat(pattern, &ctx))
2632 nand_op_parser_trace(&ctx);
2637 ret = pattern->exec(chip, &ctx.subop);
2644 if (i == parser->npatterns) {
2645 pr_debug("->exec_op() parser: pattern not found!\n");
2650 * Update the context structure by pointing to the start of the
2653 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2654 if (ctx.subop.last_instr_end_off)
2655 ctx.subop.instrs -= 1;
2657 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2662 EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2664 static bool nand_instr_is_data(const struct nand_op_instr *instr)
2666 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2667 instr->type == NAND_OP_DATA_OUT_INSTR);
2670 static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2671 unsigned int instr_idx)
2673 return subop && instr_idx < subop->ninstrs;
2676 static int nand_subop_get_start_off(const struct nand_subop *subop,
2677 unsigned int instr_idx)
2682 return subop->first_instr_start_off;
2686 * nand_subop_get_addr_start_off - Get the start offset in an address array
2687 * @subop: The entire sub-operation
2688 * @instr_idx: Index of the instruction inside the sub-operation
2690 * During driver development, one could be tempted to directly use the
2691 * ->addr.addrs field of address instructions. This is wrong as address
2692 * instructions might be split.
2694 * Given an address instruction, returns the offset of the first cycle to issue.
2696 int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2697 unsigned int instr_idx)
2699 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2700 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2703 return nand_subop_get_start_off(subop, instr_idx);
2705 EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2708 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2709 * @subop: The entire sub-operation
2710 * @instr_idx: Index of the instruction inside the sub-operation
2712 * During driver development, one could be tempted to directly use the
2713 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2716 * Given an address instruction, returns the number of address cycle to issue.
2718 int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2719 unsigned int instr_idx)
2721 int start_off, end_off;
2723 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2724 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2727 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2729 if (instr_idx == subop->ninstrs - 1 &&
2730 subop->last_instr_end_off)
2731 end_off = subop->last_instr_end_off;
2733 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2735 return end_off - start_off;
2737 EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2740 * nand_subop_get_data_start_off - Get the start offset in a data array
2741 * @subop: The entire sub-operation
2742 * @instr_idx: Index of the instruction inside the sub-operation
2744 * During driver development, one could be tempted to directly use the
2745 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2746 * instructions might be split.
2748 * Given a data instruction, returns the offset to start from.
2750 int nand_subop_get_data_start_off(const struct nand_subop *subop,
2751 unsigned int instr_idx)
2753 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2754 !nand_instr_is_data(&subop->instrs[instr_idx]))
2757 return nand_subop_get_start_off(subop, instr_idx);
2759 EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2762 * nand_subop_get_data_len - Get the number of bytes to retrieve
2763 * @subop: The entire sub-operation
2764 * @instr_idx: Index of the instruction inside the sub-operation
2766 * During driver development, one could be tempted to directly use the
2767 * ->data->len field of a data instruction. This is wrong as data instructions
2770 * Returns the length of the chunk of data to send/receive.
2772 int nand_subop_get_data_len(const struct nand_subop *subop,
2773 unsigned int instr_idx)
2775 int start_off = 0, end_off;
2777 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2778 !nand_instr_is_data(&subop->instrs[instr_idx]))
2781 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2783 if (instr_idx == subop->ninstrs - 1 &&
2784 subop->last_instr_end_off)
2785 end_off = subop->last_instr_end_off;
2787 end_off = subop->instrs[instr_idx].ctx.data.len;
2789 return end_off - start_off;
2791 EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2794 * nand_reset - Reset and initialize a NAND device
2795 * @chip: The NAND chip
2796 * @chipnr: Internal die id
2798 * Save the timings data structure, then apply SDR timings mode 0 (see
2799 * nand_reset_data_interface for details), do the reset operation, and
2800 * apply back the previous timings.
2802 * Returns 0 on success, a negative error code otherwise.
2804 int nand_reset(struct nand_chip *chip, int chipnr)
2806 struct mtd_info *mtd = nand_to_mtd(chip);
2807 struct nand_data_interface saved_data_intf = chip->data_interface;
2810 ret = nand_reset_data_interface(chip, chipnr);
2815 * The CS line has to be released before we can apply the new NAND
2816 * interface settings, hence this weird ->select_chip() dance.
2818 chip->select_chip(mtd, chipnr);
2819 ret = nand_reset_op(chip);
2820 chip->select_chip(mtd, -1);
2825 * A nand_reset_data_interface() put both the NAND chip and the NAND
2826 * controller in timings mode 0. If the default mode for this chip is
2827 * also 0, no need to proceed to the change again. Plus, at probe time,
2828 * nand_setup_data_interface() uses ->set/get_features() which would
2829 * fail anyway as the parameter page is not available yet.
2831 if (!chip->onfi_timing_mode_default)
2834 chip->data_interface = saved_data_intf;
2835 ret = nand_setup_data_interface(chip, chipnr);
2841 EXPORT_SYMBOL_GPL(nand_reset);
2844 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2845 * @buf: buffer to test
2846 * @len: buffer length
2847 * @bitflips_threshold: maximum number of bitflips
2849 * Check if a buffer contains only 0xff, which means the underlying region
2850 * has been erased and is ready to be programmed.
2851 * The bitflips_threshold specify the maximum number of bitflips before
2852 * considering the region is not erased.
2853 * Note: The logic of this function has been extracted from the memweight
2854 * implementation, except that nand_check_erased_buf function exit before
2855 * testing the whole buffer if the number of bitflips exceed the
2856 * bitflips_threshold value.
2858 * Returns a positive number of bitflips less than or equal to
2859 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2862 static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2864 const unsigned char *bitmap = buf;
2868 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2870 weight = hweight8(*bitmap);
2871 bitflips += BITS_PER_BYTE - weight;
2872 if (unlikely(bitflips > bitflips_threshold))
2876 for (; len >= sizeof(long);
2877 len -= sizeof(long), bitmap += sizeof(long)) {
2878 unsigned long d = *((unsigned long *)bitmap);
2881 weight = hweight_long(d);
2882 bitflips += BITS_PER_LONG - weight;
2883 if (unlikely(bitflips > bitflips_threshold))
2887 for (; len > 0; len--, bitmap++) {
2888 weight = hweight8(*bitmap);
2889 bitflips += BITS_PER_BYTE - weight;
2890 if (unlikely(bitflips > bitflips_threshold))
2898 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2900 * @data: data buffer to test
2901 * @datalen: data length
2903 * @ecclen: ECC length
2904 * @extraoob: extra OOB buffer
2905 * @extraooblen: extra OOB length
2906 * @bitflips_threshold: maximum number of bitflips
2908 * Check if a data buffer and its associated ECC and OOB data contains only
2909 * 0xff pattern, which means the underlying region has been erased and is
2910 * ready to be programmed.
2911 * The bitflips_threshold specify the maximum number of bitflips before
2912 * considering the region as not erased.
2915 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2916 * different from the NAND page size. When fixing bitflips, ECC engines will
2917 * report the number of errors per chunk, and the NAND core infrastructure
2918 * expect you to return the maximum number of bitflips for the whole page.
2919 * This is why you should always use this function on a single chunk and
2920 * not on the whole page. After checking each chunk you should update your
2921 * max_bitflips value accordingly.
2922 * 2/ When checking for bitflips in erased pages you should not only check
2923 * the payload data but also their associated ECC data, because a user might
2924 * have programmed almost all bits to 1 but a few. In this case, we
2925 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2927 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2928 * data are protected by the ECC engine.
2929 * It could also be used if you support subpages and want to attach some
2930 * extra OOB data to an ECC chunk.
2932 * Returns a positive number of bitflips less than or equal to
2933 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2934 * threshold. In case of success, the passed buffers are filled with 0xff.
2936 int nand_check_erased_ecc_chunk(void *data, int datalen,
2937 void *ecc, int ecclen,
2938 void *extraoob, int extraooblen,
2939 int bitflips_threshold)
2941 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2943 data_bitflips = nand_check_erased_buf(data, datalen,
2944 bitflips_threshold);
2945 if (data_bitflips < 0)
2946 return data_bitflips;
2948 bitflips_threshold -= data_bitflips;
2950 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2951 if (ecc_bitflips < 0)
2952 return ecc_bitflips;
2954 bitflips_threshold -= ecc_bitflips;
2956 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2957 bitflips_threshold);
2958 if (extraoob_bitflips < 0)
2959 return extraoob_bitflips;
2962 memset(data, 0xff, datalen);
2965 memset(ecc, 0xff, ecclen);
2967 if (extraoob_bitflips)
2968 memset(extraoob, 0xff, extraooblen);
2970 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2972 EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2975 * nand_read_page_raw - [INTERN] read raw page data without ecc
2976 * @mtd: mtd info structure
2977 * @chip: nand chip info structure
2978 * @buf: buffer to store read data
2979 * @oob_required: caller requires OOB data read to chip->oob_poi
2980 * @page: page number to read
2982 * Not for syndrome calculating ECC controllers, which use a special oob layout.
2984 int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2985 uint8_t *buf, int oob_required, int page)
2989 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
2994 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
3002 EXPORT_SYMBOL(nand_read_page_raw);
3005 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
3006 * @mtd: mtd info structure
3007 * @chip: nand chip info structure
3008 * @buf: buffer to store read data
3009 * @oob_required: caller requires OOB data read to chip->oob_poi
3010 * @page: page number to read
3012 * We need a special oob layout and handling even when OOB isn't used.
3014 static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
3015 struct nand_chip *chip, uint8_t *buf,
3016 int oob_required, int page)
3018 int eccsize = chip->ecc.size;
3019 int eccbytes = chip->ecc.bytes;
3020 uint8_t *oob = chip->oob_poi;
3021 int steps, size, ret;
3023 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3027 for (steps = chip->ecc.steps; steps > 0; steps--) {
3028 ret = nand_read_data_op(chip, buf, eccsize, false);
3034 if (chip->ecc.prepad) {
3035 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3040 oob += chip->ecc.prepad;
3043 ret = nand_read_data_op(chip, oob, eccbytes, false);
3049 if (chip->ecc.postpad) {
3050 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3055 oob += chip->ecc.postpad;
3059 size = mtd->oobsize - (oob - chip->oob_poi);
3061 ret = nand_read_data_op(chip, oob, size, false);
3070 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
3071 * @mtd: mtd info structure
3072 * @chip: nand chip info structure
3073 * @buf: buffer to store read data
3074 * @oob_required: caller requires OOB data read to chip->oob_poi
3075 * @page: page number to read
3077 static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
3078 uint8_t *buf, int oob_required, int page)
3080 int i, eccsize = chip->ecc.size, ret;
3081 int eccbytes = chip->ecc.bytes;
3082 int eccsteps = chip->ecc.steps;
3084 uint8_t *ecc_calc = chip->ecc.calc_buf;
3085 uint8_t *ecc_code = chip->ecc.code_buf;
3086 unsigned int max_bitflips = 0;
3088 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
3090 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3091 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3093 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3098 eccsteps = chip->ecc.steps;
3101 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3104 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
3106 mtd->ecc_stats.failed++;
3108 mtd->ecc_stats.corrected += stat;
3109 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3112 return max_bitflips;
3116 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
3117 * @mtd: mtd info structure
3118 * @chip: nand chip info structure
3119 * @data_offs: offset of requested data within the page
3120 * @readlen: data length
3121 * @bufpoi: buffer to store read data
3122 * @page: page number to read
3124 static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
3125 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
3128 int start_step, end_step, num_steps, ret;
3130 int data_col_addr, i, gaps = 0;
3131 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3132 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
3133 int index, section = 0;
3134 unsigned int max_bitflips = 0;
3135 struct mtd_oob_region oobregion = { };
3137 /* Column address within the page aligned to ECC size (256bytes) */
3138 start_step = data_offs / chip->ecc.size;
3139 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3140 num_steps = end_step - start_step + 1;
3141 index = start_step * chip->ecc.bytes;
3143 /* Data size aligned to ECC ecc.size */
3144 datafrag_len = num_steps * chip->ecc.size;
3145 eccfrag_len = num_steps * chip->ecc.bytes;
3147 data_col_addr = start_step * chip->ecc.size;
3148 /* If we read not a page aligned data */
3149 p = bufpoi + data_col_addr;
3150 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
3155 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
3156 chip->ecc.calculate(mtd, p, &chip->ecc.calc_buf[i]);
3159 * The performance is faster if we position offsets according to
3160 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
3162 ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion);
3166 if (oobregion.length < eccfrag_len)
3170 ret = nand_change_read_column_op(chip, mtd->writesize,
3171 chip->oob_poi, mtd->oobsize,
3177 * Send the command to read the particular ECC bytes take care
3178 * about buswidth alignment in read_buf.
3180 aligned_pos = oobregion.offset & ~(busw - 1);
3181 aligned_len = eccfrag_len;
3182 if (oobregion.offset & (busw - 1))
3184 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3188 ret = nand_change_read_column_op(chip,
3189 mtd->writesize + aligned_pos,
3190 &chip->oob_poi[aligned_pos],
3191 aligned_len, false);
3196 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
3197 chip->oob_poi, index, eccfrag_len);
3201 p = bufpoi + data_col_addr;
3202 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3205 stat = chip->ecc.correct(mtd, p, &chip->ecc.code_buf[i],
3206 &chip->ecc.calc_buf[i]);
3207 if (stat == -EBADMSG &&
3208 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3209 /* check for empty pages with bitflips */
3210 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3211 &chip->ecc.code_buf[i],
3214 chip->ecc.strength);
3218 mtd->ecc_stats.failed++;
3220 mtd->ecc_stats.corrected += stat;
3221 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3224 return max_bitflips;
3228 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
3229 * @mtd: mtd info structure
3230 * @chip: nand chip info structure
3231 * @buf: buffer to store read data
3232 * @oob_required: caller requires OOB data read to chip->oob_poi
3233 * @page: page number to read
3235 * Not for syndrome calculating ECC controllers which need a special oob layout.
3237 static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
3238 uint8_t *buf, int oob_required, int page)
3240 int i, eccsize = chip->ecc.size, ret;
3241 int eccbytes = chip->ecc.bytes;
3242 int eccsteps = chip->ecc.steps;
3244 uint8_t *ecc_calc = chip->ecc.calc_buf;
3245 uint8_t *ecc_code = chip->ecc.code_buf;
3246 unsigned int max_bitflips = 0;
3248 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3252 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3253 chip->ecc.hwctl(mtd, NAND_ECC_READ);
3255 ret = nand_read_data_op(chip, p, eccsize, false);
3259 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3262 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3266 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3271 eccsteps = chip->ecc.steps;
3274 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3277 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
3278 if (stat == -EBADMSG &&
3279 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3280 /* check for empty pages with bitflips */
3281 stat = nand_check_erased_ecc_chunk(p, eccsize,
3282 &ecc_code[i], eccbytes,
3284 chip->ecc.strength);
3288 mtd->ecc_stats.failed++;
3290 mtd->ecc_stats.corrected += stat;
3291 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3294 return max_bitflips;
3298 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
3299 * @mtd: mtd info structure
3300 * @chip: nand chip info structure
3301 * @buf: buffer to store read data
3302 * @oob_required: caller requires OOB data read to chip->oob_poi
3303 * @page: page number to read
3305 * Hardware ECC for large page chips, require OOB to be read first. For this
3306 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3307 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3308 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3309 * the data area, by overwriting the NAND manufacturer bad block markings.
3311 static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
3312 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
3314 int i, eccsize = chip->ecc.size, ret;
3315 int eccbytes = chip->ecc.bytes;
3316 int eccsteps = chip->ecc.steps;
3318 uint8_t *ecc_code = chip->ecc.code_buf;
3319 uint8_t *ecc_calc = chip->ecc.calc_buf;
3320 unsigned int max_bitflips = 0;
3322 /* Read the OOB area first */
3323 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3327 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3331 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3336 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3339 chip->ecc.hwctl(mtd, NAND_ECC_READ);
3341 ret = nand_read_data_op(chip, p, eccsize, false);
3345 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3347 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
3348 if (stat == -EBADMSG &&
3349 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3350 /* check for empty pages with bitflips */
3351 stat = nand_check_erased_ecc_chunk(p, eccsize,
3352 &ecc_code[i], eccbytes,