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 #if CONFIG_IS_ENABLED(OF_CONTROL)
38 #include <dm/devres.h>
39 #include <linux/bug.h>
40 #include <linux/delay.h>
41 #include <linux/err.h>
42 #include <linux/compat.h>
43 #include <linux/mtd/mtd.h>
44 #include <linux/mtd/rawnand.h>
45 #include <linux/mtd/nand_ecc.h>
46 #include <linux/mtd/nand_bch.h>
47 #ifdef CONFIG_MTD_PARTITIONS
48 #include <linux/mtd/partitions.h>
51 #include <linux/errno.h>
53 /* Define default oob placement schemes for large and small page devices */
54 #ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
55 static struct nand_ecclayout nand_oob_8 = {
65 static struct nand_ecclayout nand_oob_16 = {
67 .eccpos = {0, 1, 2, 3, 6, 7},
73 static struct nand_ecclayout nand_oob_64 = {
76 40, 41, 42, 43, 44, 45, 46, 47,
77 48, 49, 50, 51, 52, 53, 54, 55,
78 56, 57, 58, 59, 60, 61, 62, 63},
84 static struct nand_ecclayout nand_oob_128 = {
87 80, 81, 82, 83, 84, 85, 86, 87,
88 88, 89, 90, 91, 92, 93, 94, 95,
89 96, 97, 98, 99, 100, 101, 102, 103,
90 104, 105, 106, 107, 108, 109, 110, 111,
91 112, 113, 114, 115, 116, 117, 118, 119,
92 120, 121, 122, 123, 124, 125, 126, 127},
99 static int nand_get_device(struct mtd_info *mtd, int new_state);
101 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
102 struct mtd_oob_ops *ops);
105 * For devices which display every fart in the system on a separate LED. Is
106 * compiled away when LED support is disabled.
108 DEFINE_LED_TRIGGER(nand_led_trigger);
110 static int check_offs_len(struct mtd_info *mtd,
111 loff_t ofs, uint64_t len)
113 struct nand_chip *chip = mtd_to_nand(mtd);
116 /* Start address must align on block boundary */
117 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
118 pr_debug("%s: unaligned address\n", __func__);
122 /* Length must align on block boundary */
123 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
124 pr_debug("%s: length not block aligned\n", __func__);
132 * nand_release_device - [GENERIC] release chip
133 * @mtd: MTD device structure
135 * Release chip lock and wake up anyone waiting on the device.
137 static void nand_release_device(struct mtd_info *mtd)
139 struct nand_chip *chip = mtd_to_nand(mtd);
141 /* De-select the NAND device */
142 chip->select_chip(mtd, -1);
146 * nand_read_byte - [DEFAULT] read one byte from the chip
147 * @mtd: MTD device structure
149 * Default read function for 8bit buswidth
151 uint8_t nand_read_byte(struct mtd_info *mtd)
153 struct nand_chip *chip = mtd_to_nand(mtd);
154 return readb(chip->IO_ADDR_R);
158 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
159 * @mtd: MTD device structure
161 * Default read function for 16bit buswidth with endianness conversion.
164 static uint8_t nand_read_byte16(struct mtd_info *mtd)
166 struct nand_chip *chip = mtd_to_nand(mtd);
167 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
171 * nand_read_word - [DEFAULT] read one word from the chip
172 * @mtd: MTD device structure
174 * Default read function for 16bit buswidth without endianness conversion.
176 static u16 nand_read_word(struct mtd_info *mtd)
178 struct nand_chip *chip = mtd_to_nand(mtd);
179 return readw(chip->IO_ADDR_R);
183 * nand_select_chip - [DEFAULT] control CE line
184 * @mtd: MTD device structure
185 * @chipnr: chipnumber to select, -1 for deselect
187 * Default select function for 1 chip devices.
189 static void nand_select_chip(struct mtd_info *mtd, int chipnr)
191 struct nand_chip *chip = mtd_to_nand(mtd);
195 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
206 * nand_write_byte - [DEFAULT] write single byte to chip
207 * @mtd: MTD device structure
208 * @byte: value to write
210 * Default function to write a byte to I/O[7:0]
212 static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
214 struct nand_chip *chip = mtd_to_nand(mtd);
216 chip->write_buf(mtd, &byte, 1);
220 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
221 * @mtd: MTD device structure
222 * @byte: value to write
224 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
226 static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
228 struct nand_chip *chip = mtd_to_nand(mtd);
229 uint16_t word = byte;
232 * It's not entirely clear what should happen to I/O[15:8] when writing
233 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
235 * When the host supports a 16-bit bus width, only data is
236 * transferred at the 16-bit width. All address and command line
237 * transfers shall use only the lower 8-bits of the data bus. During
238 * command transfers, the host may place any value on the upper
239 * 8-bits of the data bus. During address transfers, the host shall
240 * set the upper 8-bits of the data bus to 00h.
242 * One user of the write_byte callback is nand_onfi_set_features. The
243 * four parameters are specified to be written to I/O[7:0], but this is
244 * neither an address nor a command transfer. Let's assume a 0 on the
245 * upper I/O lines is OK.
247 chip->write_buf(mtd, (uint8_t *)&word, 2);
250 static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
254 for (i = 0; i < len; i++)
255 writeb(buf[i], addr);
257 static void ioread8_rep(void *addr, uint8_t *buf, int len)
261 for (i = 0; i < len; i++)
262 buf[i] = readb(addr);
265 static void ioread16_rep(void *addr, void *buf, int len)
268 u16 *p = (u16 *) buf;
270 for (i = 0; i < len; i++)
274 static void iowrite16_rep(void *addr, void *buf, int len)
277 u16 *p = (u16 *) buf;
279 for (i = 0; i < len; i++)
284 * nand_write_buf - [DEFAULT] write buffer to chip
285 * @mtd: MTD device structure
287 * @len: number of bytes to write
289 * Default write function for 8bit buswidth.
291 void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
293 struct nand_chip *chip = mtd_to_nand(mtd);
295 iowrite8_rep(chip->IO_ADDR_W, buf, len);
299 * nand_read_buf - [DEFAULT] read chip data into buffer
300 * @mtd: MTD device structure
301 * @buf: buffer to store date
302 * @len: number of bytes to read
304 * Default read function for 8bit buswidth.
306 void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
308 struct nand_chip *chip = mtd_to_nand(mtd);
310 ioread8_rep(chip->IO_ADDR_R, buf, len);
314 * nand_write_buf16 - [DEFAULT] write buffer to chip
315 * @mtd: MTD device structure
317 * @len: number of bytes to write
319 * Default write function for 16bit buswidth.
321 void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
323 struct nand_chip *chip = mtd_to_nand(mtd);
324 u16 *p = (u16 *) buf;
326 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
330 * nand_read_buf16 - [DEFAULT] read chip data into buffer
331 * @mtd: MTD device structure
332 * @buf: buffer to store date
333 * @len: number of bytes to read
335 * Default read function for 16bit buswidth.
337 void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
339 struct nand_chip *chip = mtd_to_nand(mtd);
340 u16 *p = (u16 *) buf;
342 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
346 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
347 * @mtd: MTD device structure
348 * @ofs: offset from device start
350 * Check, if the block is bad.
352 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
354 int page, res = 0, i = 0;
355 struct nand_chip *chip = mtd_to_nand(mtd);
358 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
359 ofs += mtd->erasesize - mtd->writesize;
361 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
364 if (chip->options & NAND_BUSWIDTH_16) {
365 chip->cmdfunc(mtd, NAND_CMD_READOOB,
366 chip->badblockpos & 0xFE, page);
367 bad = cpu_to_le16(chip->read_word(mtd));
368 if (chip->badblockpos & 0x1)
373 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
375 bad = chip->read_byte(mtd);
378 if (likely(chip->badblockbits == 8))
381 res = hweight8(bad) < chip->badblockbits;
382 ofs += mtd->writesize;
383 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
385 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
391 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
392 * @mtd: MTD device structure
393 * @ofs: offset from device start
395 * This is the default implementation, which can be overridden by a hardware
396 * specific driver. It provides the details for writing a bad block marker to a
399 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
401 struct nand_chip *chip = mtd_to_nand(mtd);
402 struct mtd_oob_ops ops;
403 uint8_t buf[2] = { 0, 0 };
404 int ret = 0, res, i = 0;
406 memset(&ops, 0, sizeof(ops));
408 ops.ooboffs = chip->badblockpos;
409 if (chip->options & NAND_BUSWIDTH_16) {
410 ops.ooboffs &= ~0x01;
411 ops.len = ops.ooblen = 2;
413 ops.len = ops.ooblen = 1;
415 ops.mode = MTD_OPS_PLACE_OOB;
417 /* Write to first/last page(s) if necessary */
418 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
419 ofs += mtd->erasesize - mtd->writesize;
421 res = nand_do_write_oob(mtd, ofs, &ops);
426 ofs += mtd->writesize;
427 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
433 * nand_block_markbad_lowlevel - mark a block bad
434 * @mtd: MTD device structure
435 * @ofs: offset from device start
437 * This function performs the generic NAND bad block marking steps (i.e., bad
438 * block table(s) and/or marker(s)). We only allow the hardware driver to
439 * specify how to write bad block markers to OOB (chip->block_markbad).
441 * We try operations in the following order:
442 * (1) erase the affected block, to allow OOB marker to be written cleanly
443 * (2) write bad block marker to OOB area of affected block (unless flag
444 * NAND_BBT_NO_OOB_BBM is present)
446 * Note that we retain the first error encountered in (2) or (3), finish the
447 * procedures, and dump the error in the end.
449 static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
451 struct nand_chip *chip = mtd_to_nand(mtd);
454 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
455 struct erase_info einfo;
457 /* Attempt erase before marking OOB */
458 memset(&einfo, 0, sizeof(einfo));
461 einfo.len = 1ULL << chip->phys_erase_shift;
462 nand_erase_nand(mtd, &einfo, 0);
464 /* Write bad block marker to OOB */
465 nand_get_device(mtd, FL_WRITING);
466 ret = chip->block_markbad(mtd, ofs);
467 nand_release_device(mtd);
470 /* Mark block bad in BBT */
472 res = nand_markbad_bbt(mtd, ofs);
478 mtd->ecc_stats.badblocks++;
484 * nand_check_wp - [GENERIC] check if the chip is write protected
485 * @mtd: MTD device structure
487 * Check, if the device is write protected. The function expects, that the
488 * device is already selected.
490 static int nand_check_wp(struct mtd_info *mtd)
492 struct nand_chip *chip = mtd_to_nand(mtd);
496 /* Broken xD cards report WP despite being writable */
497 if (chip->options & NAND_BROKEN_XD)
500 /* Check the WP bit */
501 ret = nand_status_op(chip, &status);
505 return status & NAND_STATUS_WP ? 0 : 1;
509 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
510 * @mtd: MTD device structure
511 * @ofs: offset from device start
513 * Check if the block is marked as reserved.
515 static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
517 struct nand_chip *chip = mtd_to_nand(mtd);
521 /* Return info from the table */
522 return nand_isreserved_bbt(mtd, ofs);
526 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
527 * @mtd: MTD device structure
528 * @ofs: offset from device start
529 * @allowbbt: 1, if its allowed to access the bbt area
531 * Check, if the block is bad. Either by reading the bad block table or
532 * calling of the scan function.
534 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
536 struct nand_chip *chip = mtd_to_nand(mtd);
538 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
539 !(chip->options & NAND_BBT_SCANNED)) {
540 chip->options |= NAND_BBT_SCANNED;
545 return chip->block_bad(mtd, ofs);
547 /* Return info from the table */
548 return nand_isbad_bbt(mtd, ofs, allowbbt);
552 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
553 * @mtd: MTD device structure
555 * Wait for the ready pin after a command, and warn if a timeout occurs.
557 void nand_wait_ready(struct mtd_info *mtd)
559 struct nand_chip *chip = mtd_to_nand(mtd);
560 u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
563 time_start = get_timer(0);
564 /* Wait until command is processed or timeout occurs */
565 while (get_timer(time_start) < timeo) {
567 if (chip->dev_ready(mtd))
571 if (!chip->dev_ready(mtd))
572 pr_warn("timeout while waiting for chip to become ready\n");
574 EXPORT_SYMBOL_GPL(nand_wait_ready);
577 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
578 * @mtd: MTD device structure
579 * @timeo: Timeout in ms
581 * Wait for status ready (i.e. command done) or timeout.
583 static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
585 register struct nand_chip *chip = mtd_to_nand(mtd);
589 timeo = (CONFIG_SYS_HZ * timeo) / 1000;
590 time_start = get_timer(0);
591 while (get_timer(time_start) < timeo) {
594 ret = nand_read_data_op(chip, &status, sizeof(status), true);
598 if (status & NAND_STATUS_READY)
605 * nand_command - [DEFAULT] Send command to NAND device
606 * @mtd: MTD device structure
607 * @command: the command to be sent
608 * @column: the column address for this command, -1 if none
609 * @page_addr: the page address for this command, -1 if none
611 * Send command to NAND device. This function is used for small page devices
612 * (512 Bytes per page).
614 static void nand_command(struct mtd_info *mtd, unsigned int command,
615 int column, int page_addr)
617 register struct nand_chip *chip = mtd_to_nand(mtd);
618 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
620 /* Write out the command to the device */
621 if (command == NAND_CMD_SEQIN) {
624 if (column >= mtd->writesize) {
626 column -= mtd->writesize;
627 readcmd = NAND_CMD_READOOB;
628 } else if (column < 256) {
629 /* First 256 bytes --> READ0 */
630 readcmd = NAND_CMD_READ0;
633 readcmd = NAND_CMD_READ1;
635 chip->cmd_ctrl(mtd, readcmd, ctrl);
636 ctrl &= ~NAND_CTRL_CHANGE;
638 chip->cmd_ctrl(mtd, command, ctrl);
640 /* Address cycle, when necessary */
641 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
642 /* Serially input address */
644 /* Adjust columns for 16 bit buswidth */
645 if (chip->options & NAND_BUSWIDTH_16 &&
646 !nand_opcode_8bits(command))
648 chip->cmd_ctrl(mtd, column, ctrl);
649 ctrl &= ~NAND_CTRL_CHANGE;
651 if (page_addr != -1) {
652 chip->cmd_ctrl(mtd, page_addr, ctrl);
653 ctrl &= ~NAND_CTRL_CHANGE;
654 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
655 if (chip->options & NAND_ROW_ADDR_3)
656 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
658 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
661 * Program and erase have their own busy handlers status and sequential
666 case NAND_CMD_PAGEPROG:
667 case NAND_CMD_ERASE1:
668 case NAND_CMD_ERASE2:
670 case NAND_CMD_STATUS:
671 case NAND_CMD_READID:
672 case NAND_CMD_SET_FEATURES:
678 udelay(chip->chip_delay);
679 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
680 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
682 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
683 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
684 nand_wait_status_ready(mtd, 250);
687 /* This applies to read commands */
690 * If we don't have access to the busy pin, we apply the given
693 if (!chip->dev_ready) {
694 udelay(chip->chip_delay);
699 * Apply this short delay always to ensure that we do wait tWB in
700 * any case on any machine.
704 nand_wait_ready(mtd);
708 * nand_command_lp - [DEFAULT] Send command to NAND large page device
709 * @mtd: MTD device structure
710 * @command: the command to be sent
711 * @column: the column address for this command, -1 if none
712 * @page_addr: the page address for this command, -1 if none
714 * Send command to NAND device. This is the version for the new large page
715 * devices. We don't have the separate regions as we have in the small page
716 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
718 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
719 int column, int page_addr)
721 register struct nand_chip *chip = mtd_to_nand(mtd);
723 /* Emulate NAND_CMD_READOOB */
724 if (command == NAND_CMD_READOOB) {
725 column += mtd->writesize;
726 command = NAND_CMD_READ0;
729 /* Command latch cycle */
730 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
732 if (column != -1 || page_addr != -1) {
733 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
735 /* Serially input address */
737 /* Adjust columns for 16 bit buswidth */
738 if (chip->options & NAND_BUSWIDTH_16 &&
739 !nand_opcode_8bits(command))
741 chip->cmd_ctrl(mtd, column, ctrl);
742 ctrl &= ~NAND_CTRL_CHANGE;
743 chip->cmd_ctrl(mtd, column >> 8, ctrl);
745 if (page_addr != -1) {
746 chip->cmd_ctrl(mtd, page_addr, ctrl);
747 chip->cmd_ctrl(mtd, page_addr >> 8,
748 NAND_NCE | NAND_ALE);
749 if (chip->options & NAND_ROW_ADDR_3)
750 chip->cmd_ctrl(mtd, page_addr >> 16,
751 NAND_NCE | NAND_ALE);
754 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
757 * Program and erase have their own busy handlers status, sequential
758 * in and status need no delay.
762 case NAND_CMD_CACHEDPROG:
763 case NAND_CMD_PAGEPROG:
764 case NAND_CMD_ERASE1:
765 case NAND_CMD_ERASE2:
768 case NAND_CMD_STATUS:
769 case NAND_CMD_READID:
770 case NAND_CMD_SET_FEATURES:
776 udelay(chip->chip_delay);
777 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
778 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
779 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
780 NAND_NCE | NAND_CTRL_CHANGE);
781 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
782 nand_wait_status_ready(mtd, 250);
785 case NAND_CMD_RNDOUT:
786 /* No ready / busy check necessary */
787 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
788 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
789 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
790 NAND_NCE | NAND_CTRL_CHANGE);
794 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
795 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
796 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
797 NAND_NCE | NAND_CTRL_CHANGE);
799 /* This applies to read commands */
802 * If we don't have access to the busy pin, we apply the given
805 if (!chip->dev_ready) {
806 udelay(chip->chip_delay);
812 * Apply this short delay always to ensure that we do wait tWB in
813 * any case on any machine.
817 nand_wait_ready(mtd);
821 * panic_nand_get_device - [GENERIC] Get chip for selected access
822 * @chip: the nand chip descriptor
823 * @mtd: MTD device structure
824 * @new_state: the state which is requested
826 * Used when in panic, no locks are taken.
828 static void panic_nand_get_device(struct nand_chip *chip,
829 struct mtd_info *mtd, int new_state)
831 /* Hardware controller shared among independent devices */
832 chip->controller->active = chip;
833 chip->state = new_state;
837 * nand_get_device - [GENERIC] Get chip for selected access
838 * @mtd: MTD device structure
839 * @new_state: the state which is requested
841 * Get the device and lock it for exclusive access
844 nand_get_device(struct mtd_info *mtd, int new_state)
846 struct nand_chip *chip = mtd_to_nand(mtd);
847 chip->state = new_state;
852 * panic_nand_wait - [GENERIC] wait until the command is done
853 * @mtd: MTD device structure
854 * @chip: NAND chip structure
857 * Wait for command done. This is a helper function for nand_wait used when
858 * we are in interrupt context. May happen when in panic and trying to write
859 * an oops through mtdoops.
861 static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
865 for (i = 0; i < timeo; i++) {
866 if (chip->dev_ready) {
867 if (chip->dev_ready(mtd))
873 ret = nand_read_data_op(chip, &status, sizeof(status),
878 if (status & NAND_STATUS_READY)
886 * nand_wait - [DEFAULT] wait until the command is done
887 * @mtd: MTD device structure
888 * @chip: NAND chip structure
890 * Wait for command done. This applies to erase and program only.
892 static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
894 unsigned long timeo = 400;
898 led_trigger_event(nand_led_trigger, LED_FULL);
901 * Apply this short delay always to ensure that we do wait tWB in any
902 * case on any machine.
906 ret = nand_status_op(chip, NULL);
910 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
913 time_start = get_timer(0);
914 while (get_timer(time_start) < timer) {
915 if (chip->dev_ready) {
916 if (chip->dev_ready(mtd))
919 ret = nand_read_data_op(chip, &status,
920 sizeof(status), true);
924 if (status & NAND_STATUS_READY)
928 led_trigger_event(nand_led_trigger, LED_OFF);
930 ret = nand_read_data_op(chip, &status, sizeof(status), true);
934 /* This can happen if in case of timeout or buggy dev_ready */
935 WARN_ON(!(status & NAND_STATUS_READY));
940 * nand_reset_data_interface - Reset data interface and timings
941 * @chip: The NAND chip
942 * @chipnr: Internal die id
944 * Reset the Data interface and timings to ONFI mode 0.
946 * Returns 0 for success or negative error code otherwise.
948 static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
950 struct mtd_info *mtd = nand_to_mtd(chip);
951 const struct nand_data_interface *conf;
954 if (!chip->setup_data_interface)
958 * The ONFI specification says:
960 * To transition from NV-DDR or NV-DDR2 to the SDR data
961 * interface, the host shall use the Reset (FFh) command
962 * using SDR timing mode 0. A device in any timing mode is
963 * required to recognize Reset (FFh) command issued in SDR
967 * Configure the data interface in SDR mode and set the
968 * timings to timing mode 0.
971 conf = nand_get_default_data_interface();
972 ret = chip->setup_data_interface(mtd, chipnr, conf);
974 pr_err("Failed to configure data interface to SDR timing mode 0\n");
980 * nand_setup_data_interface - Setup the best data interface and timings
981 * @chip: The NAND chip
982 * @chipnr: Internal die id
984 * Find and configure the best data interface and NAND timings supported by
985 * the chip and the driver.
986 * First tries to retrieve supported timing modes from ONFI information,
987 * and if the NAND chip does not support ONFI, relies on the
988 * ->onfi_timing_mode_default specified in the nand_ids table.
990 * Returns 0 for success or negative error code otherwise.
992 static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
994 struct mtd_info *mtd = nand_to_mtd(chip);
997 if (!chip->setup_data_interface || !chip->data_interface)
1001 * Ensure the timing mode has been changed on the chip side
1002 * before changing timings on the controller side.
1004 if (chip->onfi_version) {
1005 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1006 chip->onfi_timing_mode_default,
1009 ret = chip->onfi_set_features(mtd, chip,
1010 ONFI_FEATURE_ADDR_TIMING_MODE,
1016 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
1022 * nand_init_data_interface - find the best data interface and timings
1023 * @chip: The NAND chip
1025 * Find the best data interface and NAND timings supported by the chip
1027 * First tries to retrieve supported timing modes from ONFI information,
1028 * and if the NAND chip does not support ONFI, relies on the
1029 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1030 * function nand_chip->data_interface is initialized with the best timing mode
1033 * Returns 0 for success or negative error code otherwise.
1035 static int nand_init_data_interface(struct nand_chip *chip)
1037 struct mtd_info *mtd = nand_to_mtd(chip);
1038 int modes, mode, ret;
1040 if (!chip->setup_data_interface)
1044 * First try to identify the best timings from ONFI parameters and
1045 * if the NAND does not support ONFI, fallback to the default ONFI
1048 modes = onfi_get_async_timing_mode(chip);
1049 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1050 if (!chip->onfi_timing_mode_default)
1053 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1056 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1058 if (!chip->data_interface)
1061 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1062 ret = onfi_init_data_interface(chip, chip->data_interface,
1063 NAND_SDR_IFACE, mode);
1067 /* Pass -1 to only */
1068 ret = chip->setup_data_interface(mtd,
1069 NAND_DATA_IFACE_CHECK_ONLY,
1070 chip->data_interface);
1072 chip->onfi_timing_mode_default = mode;
1080 static void __maybe_unused nand_release_data_interface(struct nand_chip *chip)
1082 kfree(chip->data_interface);
1086 * nand_read_page_op - Do a READ PAGE operation
1087 * @chip: The NAND chip
1088 * @page: page to read
1089 * @offset_in_page: offset within the page
1090 * @buf: buffer used to store the data
1091 * @len: length of the buffer
1093 * This function issues a READ PAGE operation.
1094 * This function does not select/unselect the CS line.
1096 * Returns 0 on success, a negative error code otherwise.
1098 int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1099 unsigned int offset_in_page, void *buf, unsigned int len)
1101 struct mtd_info *mtd = nand_to_mtd(chip);
1106 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1109 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1111 chip->read_buf(mtd, buf, len);
1115 EXPORT_SYMBOL_GPL(nand_read_page_op);
1118 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1119 * @chip: The NAND chip
1120 * @page: parameter page to read
1121 * @buf: buffer used to store the data
1122 * @len: length of the buffer
1124 * This function issues a READ PARAMETER PAGE operation.
1125 * This function does not select/unselect the CS line.
1127 * Returns 0 on success, a negative error code otherwise.
1129 static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1132 struct mtd_info *mtd = nand_to_mtd(chip);
1139 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1140 for (i = 0; i < len; i++)
1141 p[i] = chip->read_byte(mtd);
1147 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1148 * @chip: The NAND chip
1149 * @offset_in_page: offset within the page
1150 * @buf: buffer used to store the data
1151 * @len: length of the buffer
1152 * @force_8bit: force 8-bit bus access
1154 * This function issues a CHANGE READ COLUMN operation.
1155 * This function does not select/unselect the CS line.
1157 * Returns 0 on success, a negative error code otherwise.
1159 int nand_change_read_column_op(struct nand_chip *chip,
1160 unsigned int offset_in_page, void *buf,
1161 unsigned int len, bool force_8bit)
1163 struct mtd_info *mtd = nand_to_mtd(chip);
1168 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1171 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1173 chip->read_buf(mtd, buf, len);
1177 EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1180 * nand_read_oob_op - Do a READ OOB operation
1181 * @chip: The NAND chip
1182 * @page: page to read
1183 * @offset_in_oob: offset within the OOB area
1184 * @buf: buffer used to store the data
1185 * @len: length of the buffer
1187 * This function issues a READ OOB operation.
1188 * This function does not select/unselect the CS line.
1190 * Returns 0 on success, a negative error code otherwise.
1192 int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1193 unsigned int offset_in_oob, void *buf, unsigned int len)
1195 struct mtd_info *mtd = nand_to_mtd(chip);
1200 if (offset_in_oob + len > mtd->oobsize)
1203 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1205 chip->read_buf(mtd, buf, len);
1209 EXPORT_SYMBOL_GPL(nand_read_oob_op);
1212 * nand_prog_page_begin_op - starts a PROG PAGE operation
1213 * @chip: The NAND chip
1214 * @page: page to write
1215 * @offset_in_page: offset within the page
1216 * @buf: buffer containing the data to write to the page
1217 * @len: length of the buffer
1219 * This function issues the first half of a PROG PAGE operation.
1220 * This function does not select/unselect the CS line.
1222 * Returns 0 on success, a negative error code otherwise.
1224 int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1225 unsigned int offset_in_page, const void *buf,
1228 struct mtd_info *mtd = nand_to_mtd(chip);
1233 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1236 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1239 chip->write_buf(mtd, buf, len);
1243 EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1246 * nand_prog_page_end_op - ends a PROG PAGE operation
1247 * @chip: The NAND chip
1249 * This function issues the second half of a PROG PAGE operation.
1250 * This function does not select/unselect the CS line.
1252 * Returns 0 on success, a negative error code otherwise.
1254 int nand_prog_page_end_op(struct nand_chip *chip)
1256 struct mtd_info *mtd = nand_to_mtd(chip);
1259 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1261 status = chip->waitfunc(mtd, chip);
1262 if (status & NAND_STATUS_FAIL)
1267 EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1270 * nand_prog_page_op - Do a full PROG PAGE operation
1271 * @chip: The NAND chip
1272 * @page: page to write
1273 * @offset_in_page: offset within the page
1274 * @buf: buffer containing the data to write to the page
1275 * @len: length of the buffer
1277 * This function issues a full PROG PAGE operation.
1278 * This function does not select/unselect the CS line.
1280 * Returns 0 on success, a negative error code otherwise.
1282 int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1283 unsigned int offset_in_page, const void *buf,
1286 struct mtd_info *mtd = nand_to_mtd(chip);
1292 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1295 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1296 chip->write_buf(mtd, buf, len);
1297 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1299 status = chip->waitfunc(mtd, chip);
1300 if (status & NAND_STATUS_FAIL)
1305 EXPORT_SYMBOL_GPL(nand_prog_page_op);
1308 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1309 * @chip: The NAND chip
1310 * @offset_in_page: offset within the page
1311 * @buf: buffer containing the data to send to the NAND
1312 * @len: length of the buffer
1313 * @force_8bit: force 8-bit bus access
1315 * This function issues a CHANGE WRITE COLUMN operation.
1316 * This function does not select/unselect the CS line.
1318 * Returns 0 on success, a negative error code otherwise.
1320 int nand_change_write_column_op(struct nand_chip *chip,
1321 unsigned int offset_in_page,
1322 const void *buf, unsigned int len,
1325 struct mtd_info *mtd = nand_to_mtd(chip);
1330 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1333 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1335 chip->write_buf(mtd, buf, len);
1339 EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1342 * nand_readid_op - Do a READID operation
1343 * @chip: The NAND chip
1344 * @addr: address cycle to pass after the READID command
1345 * @buf: buffer used to store the ID
1346 * @len: length of the buffer
1348 * This function sends a READID command and reads back the ID returned by the
1350 * This function does not select/unselect the CS line.
1352 * Returns 0 on success, a negative error code otherwise.
1354 int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1357 struct mtd_info *mtd = nand_to_mtd(chip);
1364 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
1366 for (i = 0; i < len; i++)
1367 id[i] = chip->read_byte(mtd);
1371 EXPORT_SYMBOL_GPL(nand_readid_op);
1374 * nand_status_op - Do a STATUS operation
1375 * @chip: The NAND chip
1376 * @status: out variable to store the NAND status
1378 * This function sends a STATUS command and reads back the status returned by
1380 * This function does not select/unselect the CS line.
1382 * Returns 0 on success, a negative error code otherwise.
1384 int nand_status_op(struct nand_chip *chip, u8 *status)
1386 struct mtd_info *mtd = nand_to_mtd(chip);
1388 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1390 *status = chip->read_byte(mtd);
1394 EXPORT_SYMBOL_GPL(nand_status_op);
1397 * nand_exit_status_op - Exit a STATUS operation
1398 * @chip: The NAND chip
1400 * This function sends a READ0 command to cancel the effect of the STATUS
1401 * command to avoid reading only the status until a new read command is sent.
1403 * This function does not select/unselect the CS line.
1405 * Returns 0 on success, a negative error code otherwise.
1407 int nand_exit_status_op(struct nand_chip *chip)
1409 struct mtd_info *mtd = nand_to_mtd(chip);
1411 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
1415 EXPORT_SYMBOL_GPL(nand_exit_status_op);
1418 * nand_erase_op - Do an erase operation
1419 * @chip: The NAND chip
1420 * @eraseblock: block to erase
1422 * This function sends an ERASE command and waits for the NAND to be ready
1424 * This function does not select/unselect the CS line.
1426 * Returns 0 on success, a negative error code otherwise.
1428 int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1430 struct mtd_info *mtd = nand_to_mtd(chip);
1431 unsigned int page = eraseblock <<
1432 (chip->phys_erase_shift - chip->page_shift);
1435 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1436 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1438 status = chip->waitfunc(mtd, chip);
1442 if (status & NAND_STATUS_FAIL)
1447 EXPORT_SYMBOL_GPL(nand_erase_op);
1450 * nand_set_features_op - Do a SET FEATURES operation
1451 * @chip: The NAND chip
1452 * @feature: feature id
1453 * @data: 4 bytes of data
1455 * This function sends a SET FEATURES command and waits for the NAND to be
1456 * ready before returning.
1457 * This function does not select/unselect the CS line.
1459 * Returns 0 on success, a negative error code otherwise.
1461 static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1464 struct mtd_info *mtd = nand_to_mtd(chip);
1465 const u8 *params = data;
1468 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
1469 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1470 chip->write_byte(mtd, params[i]);
1472 status = chip->waitfunc(mtd, chip);
1473 if (status & NAND_STATUS_FAIL)
1480 * nand_get_features_op - Do a GET FEATURES operation
1481 * @chip: The NAND chip
1482 * @feature: feature id
1483 * @data: 4 bytes of data
1485 * This function sends a GET FEATURES command and waits for the NAND to be
1486 * ready before returning.
1487 * This function does not select/unselect the CS line.
1489 * Returns 0 on success, a negative error code otherwise.
1491 static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1494 struct mtd_info *mtd = nand_to_mtd(chip);
1498 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
1499 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1500 params[i] = chip->read_byte(mtd);
1506 * nand_reset_op - Do a reset operation
1507 * @chip: The NAND chip
1509 * This function sends a RESET command and waits for the NAND to be ready
1511 * This function does not select/unselect the CS line.
1513 * Returns 0 on success, a negative error code otherwise.
1515 int nand_reset_op(struct nand_chip *chip)
1517 struct mtd_info *mtd = nand_to_mtd(chip);
1519 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1523 EXPORT_SYMBOL_GPL(nand_reset_op);
1526 * nand_read_data_op - Read data from the NAND
1527 * @chip: The NAND chip
1528 * @buf: buffer used to store the data
1529 * @len: length of the buffer
1530 * @force_8bit: force 8-bit bus access
1532 * This function does a raw data read on the bus. Usually used after launching
1533 * another NAND operation like nand_read_page_op().
1534 * This function does not select/unselect the CS line.
1536 * Returns 0 on success, a negative error code otherwise.
1538 int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1541 struct mtd_info *mtd = nand_to_mtd(chip);
1550 for (i = 0; i < len; i++)
1551 p[i] = chip->read_byte(mtd);
1553 chip->read_buf(mtd, buf, len);
1558 EXPORT_SYMBOL_GPL(nand_read_data_op);
1561 * nand_write_data_op - Write data from the NAND
1562 * @chip: The NAND chip
1563 * @buf: buffer containing the data to send on the bus
1564 * @len: length of the buffer
1565 * @force_8bit: force 8-bit bus access
1567 * This function does a raw data write on the bus. Usually used after launching
1568 * another NAND operation like nand_write_page_begin_op().
1569 * This function does not select/unselect the CS line.
1571 * Returns 0 on success, a negative error code otherwise.
1573 int nand_write_data_op(struct nand_chip *chip, const void *buf,
1574 unsigned int len, bool force_8bit)
1576 struct mtd_info *mtd = nand_to_mtd(chip);
1585 for (i = 0; i < len; i++)
1586 chip->write_byte(mtd, p[i]);
1588 chip->write_buf(mtd, buf, len);
1593 EXPORT_SYMBOL_GPL(nand_write_data_op);
1596 * nand_reset - Reset and initialize a NAND device
1597 * @chip: The NAND chip
1598 * @chipnr: Internal die id
1600 * Returns 0 for success or negative error code otherwise
1602 int nand_reset(struct nand_chip *chip, int chipnr)
1604 struct mtd_info *mtd = nand_to_mtd(chip);
1607 ret = nand_reset_data_interface(chip, chipnr);
1612 * The CS line has to be released before we can apply the new NAND
1613 * interface settings, hence this weird ->select_chip() dance.
1615 chip->select_chip(mtd, chipnr);
1616 ret = nand_reset_op(chip);
1617 chip->select_chip(mtd, -1);
1621 chip->select_chip(mtd, chipnr);
1622 ret = nand_setup_data_interface(chip, chipnr);
1623 chip->select_chip(mtd, -1);
1631 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1632 * @buf: buffer to test
1633 * @len: buffer length
1634 * @bitflips_threshold: maximum number of bitflips
1636 * Check if a buffer contains only 0xff, which means the underlying region
1637 * has been erased and is ready to be programmed.
1638 * The bitflips_threshold specify the maximum number of bitflips before
1639 * considering the region is not erased.
1640 * Note: The logic of this function has been extracted from the memweight
1641 * implementation, except that nand_check_erased_buf function exit before
1642 * testing the whole buffer if the number of bitflips exceed the
1643 * bitflips_threshold value.
1645 * Returns a positive number of bitflips less than or equal to
1646 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1649 static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1651 const unsigned char *bitmap = buf;
1655 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1657 weight = hweight8(*bitmap);
1658 bitflips += BITS_PER_BYTE - weight;
1659 if (unlikely(bitflips > bitflips_threshold))
1663 for (; len >= 4; len -= 4, bitmap += 4) {
1664 weight = hweight32(*((u32 *)bitmap));
1665 bitflips += 32 - weight;
1666 if (unlikely(bitflips > bitflips_threshold))
1670 for (; len > 0; len--, bitmap++) {
1671 weight = hweight8(*bitmap);
1672 bitflips += BITS_PER_BYTE - weight;
1673 if (unlikely(bitflips > bitflips_threshold))
1681 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1683 * @data: data buffer to test
1684 * @datalen: data length
1686 * @ecclen: ECC length
1687 * @extraoob: extra OOB buffer
1688 * @extraooblen: extra OOB length
1689 * @bitflips_threshold: maximum number of bitflips
1691 * Check if a data buffer and its associated ECC and OOB data contains only
1692 * 0xff pattern, which means the underlying region has been erased and is
1693 * ready to be programmed.
1694 * The bitflips_threshold specify the maximum number of bitflips before
1695 * considering the region as not erased.
1698 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1699 * different from the NAND page size. When fixing bitflips, ECC engines will
1700 * report the number of errors per chunk, and the NAND core infrastructure
1701 * expect you to return the maximum number of bitflips for the whole page.
1702 * This is why you should always use this function on a single chunk and
1703 * not on the whole page. After checking each chunk you should update your
1704 * max_bitflips value accordingly.
1705 * 2/ When checking for bitflips in erased pages you should not only check
1706 * the payload data but also their associated ECC data, because a user might
1707 * have programmed almost all bits to 1 but a few. In this case, we
1708 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1710 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1711 * data are protected by the ECC engine.
1712 * It could also be used if you support subpages and want to attach some
1713 * extra OOB data to an ECC chunk.
1715 * Returns a positive number of bitflips less than or equal to
1716 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1717 * threshold. In case of success, the passed buffers are filled with 0xff.
1719 int nand_check_erased_ecc_chunk(void *data, int datalen,
1720 void *ecc, int ecclen,
1721 void *extraoob, int extraooblen,
1722 int bitflips_threshold)
1724 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1726 data_bitflips = nand_check_erased_buf(data, datalen,
1727 bitflips_threshold);
1728 if (data_bitflips < 0)
1729 return data_bitflips;
1731 bitflips_threshold -= data_bitflips;
1733 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1734 if (ecc_bitflips < 0)
1735 return ecc_bitflips;
1737 bitflips_threshold -= ecc_bitflips;
1739 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1740 bitflips_threshold);
1741 if (extraoob_bitflips < 0)
1742 return extraoob_bitflips;
1745 memset(data, 0xff, datalen);
1748 memset(ecc, 0xff, ecclen);
1750 if (extraoob_bitflips)
1751 memset(extraoob, 0xff, extraooblen);
1753 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1755 EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1758 * nand_read_page_raw - [INTERN] read raw page data without ecc
1759 * @mtd: mtd info structure
1760 * @chip: nand chip info structure
1761 * @buf: buffer to store read data
1762 * @oob_required: caller requires OOB data read to chip->oob_poi
1763 * @page: page number to read
1765 * Not for syndrome calculating ECC controllers, which use a special oob layout.
1767 static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1768 uint8_t *buf, int oob_required, int page)
1772 ret = nand_read_data_op(chip, buf, mtd->writesize, false);
1777 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
1787 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1788 * @mtd: mtd info structure
1789 * @chip: nand chip info structure
1790 * @buf: buffer to store read data
1791 * @oob_required: caller requires OOB data read to chip->oob_poi
1792 * @page: page number to read
1794 * We need a special oob layout and handling even when OOB isn't used.
1796 static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
1797 struct nand_chip *chip, uint8_t *buf,
1798 int oob_required, int page)
1800 int eccsize = chip->ecc.size;
1801 int eccbytes = chip->ecc.bytes;
1802 uint8_t *oob = chip->oob_poi;
1803 int steps, size, ret;
1805 for (steps = chip->ecc.steps; steps > 0; steps--) {
1806 ret = nand_read_data_op(chip, buf, eccsize, false);
1812 if (chip->ecc.prepad) {
1813 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
1818 oob += chip->ecc.prepad;
1821 ret = nand_read_data_op(chip, oob, eccbytes, false);
1827 if (chip->ecc.postpad) {
1828 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
1833 oob += chip->ecc.postpad;
1837 size = mtd->oobsize - (oob - chip->oob_poi);
1839 ret = nand_read_data_op(chip, oob, size, false);
1848 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1849 * @mtd: mtd info structure
1850 * @chip: nand chip info structure
1851 * @buf: buffer to store read data
1852 * @oob_required: caller requires OOB data read to chip->oob_poi
1853 * @page: page number to read
1855 static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1856 uint8_t *buf, int oob_required, int page)
1858 int i, eccsize = chip->ecc.size;
1859 int eccbytes = chip->ecc.bytes;
1860 int eccsteps = chip->ecc.steps;
1862 uint8_t *ecc_calc = chip->buffers->ecccalc;
1863 uint8_t *ecc_code = chip->buffers->ecccode;
1864 uint32_t *eccpos = chip->ecc.layout->eccpos;
1865 unsigned int max_bitflips = 0;
1867 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
1869 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1870 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1872 for (i = 0; i < chip->ecc.total; i++)
1873 ecc_code[i] = chip->oob_poi[eccpos[i]];
1875 eccsteps = chip->ecc.steps;
1878 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1881 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1883 mtd->ecc_stats.failed++;
1885 mtd->ecc_stats.corrected += stat;
1886 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1889 return max_bitflips;
1893 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
1894 * @mtd: mtd info structure
1895 * @chip: nand chip info structure
1896 * @data_offs: offset of requested data within the page
1897 * @readlen: data length
1898 * @bufpoi: buffer to store read data
1899 * @page: page number to read
1901 static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1902 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1905 int start_step, end_step, num_steps;
1906 uint32_t *eccpos = chip->ecc.layout->eccpos;
1908 int data_col_addr, i, gaps = 0;
1909 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1910 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
1912 unsigned int max_bitflips = 0;
1915 /* Column address within the page aligned to ECC size (256bytes) */
1916 start_step = data_offs / chip->ecc.size;
1917 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1918 num_steps = end_step - start_step + 1;
1919 index = start_step * chip->ecc.bytes;
1921 /* Data size aligned to ECC ecc.size */
1922 datafrag_len = num_steps * chip->ecc.size;
1923 eccfrag_len = num_steps * chip->ecc.bytes;
1925 data_col_addr = start_step * chip->ecc.size;
1926 /* If we read not a page aligned data */
1927 if (data_col_addr != 0)
1928 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1930 p = bufpoi + data_col_addr;
1931 ret = nand_read_data_op(chip, p, datafrag_len, false);
1936 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1937 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1940 * The performance is faster if we position offsets according to
1941 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1943 for (i = 0; i < eccfrag_len - 1; i++) {
1944 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
1950 ret = nand_change_read_column_op(chip, mtd->writesize,
1951 chip->oob_poi, mtd->oobsize,
1957 * Send the command to read the particular ECC bytes take care
1958 * about buswidth alignment in read_buf.
1960 aligned_pos = eccpos[index] & ~(busw - 1);
1961 aligned_len = eccfrag_len;
1962 if (eccpos[index] & (busw - 1))
1964 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
1967 ret = nand_change_read_column_op(chip,
1968 mtd->writesize + aligned_pos,
1969 &chip->oob_poi[aligned_pos],
1970 aligned_len, false);
1975 for (i = 0; i < eccfrag_len; i++)
1976 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
1978 p = bufpoi + data_col_addr;
1979 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1982 stat = chip->ecc.correct(mtd, p,
1983 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1984 if (stat == -EBADMSG &&
1985 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1986 /* check for empty pages with bitflips */
1987 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1988 &chip->buffers->ecccode[i],
1991 chip->ecc.strength);
1995 mtd->ecc_stats.failed++;
1997 mtd->ecc_stats.corrected += stat;
1998 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2001 return max_bitflips;
2005 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
2006 * @mtd: mtd info structure
2007 * @chip: nand chip info structure
2008 * @buf: buffer to store read data
2009 * @oob_required: caller requires OOB data read to chip->oob_poi
2010 * @page: page number to read
2012 * Not for syndrome calculating ECC controllers which need a special oob layout.
2014 static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
2015 uint8_t *buf, int oob_required, int page)
2017 int i, eccsize = chip->ecc.size;
2018 int eccbytes = chip->ecc.bytes;
2019 int eccsteps = chip->ecc.steps;
2021 uint8_t *ecc_calc = chip->buffers->ecccalc;
2022 uint8_t *ecc_code = chip->buffers->ecccode;
2023 uint32_t *eccpos = chip->ecc.layout->eccpos;
2024 unsigned int max_bitflips = 0;
2027 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2028 chip->ecc.hwctl(mtd, NAND_ECC_READ);
2030 ret = nand_read_data_op(chip, p, eccsize, false);
2034 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2037 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2041 for (i = 0; i < chip->ecc.total; i++)
2042 ecc_code[i] = chip->oob_poi[eccpos[i]];
2044 eccsteps = chip->ecc.steps;
2047 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2050 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
2051 if (stat == -EBADMSG &&
2052 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2053 /* check for empty pages with bitflips */
2054 stat = nand_check_erased_ecc_chunk(p, eccsize,
2055 &ecc_code[i], eccbytes,
2057 chip->ecc.strength);
2061 mtd->ecc_stats.failed++;
2063 mtd->ecc_stats.corrected += stat;
2064 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2067 return max_bitflips;
2071 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
2072 * @mtd: mtd info structure
2073 * @chip: nand chip info structure
2074 * @buf: buffer to store read data
2075 * @oob_required: caller requires OOB data read to chip->oob_poi
2076 * @page: page number to read
2078 * Hardware ECC for large page chips, require OOB to be read first. For this
2079 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2080 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2081 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2082 * the data area, by overwriting the NAND manufacturer bad block markings.
2084 static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
2085 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
2087 int i, eccsize = chip->ecc.size;
2088 int eccbytes = chip->ecc.bytes;
2089 int eccsteps = chip->ecc.steps;
2091 uint8_t *ecc_code = chip->buffers->ecccode;
2092 uint32_t *eccpos = chip->ecc.layout->eccpos;
2093 uint8_t *ecc_calc = chip->buffers->ecccalc;
2094 unsigned int max_bitflips = 0;
2097 /* Read the OOB area first */
2098 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2102 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2106 for (i = 0; i < chip->ecc.total; i++)
2107 ecc_code[i] = chip->oob_poi[eccpos[i]];
2109 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2112 chip->ecc.hwctl(mtd, NAND_ECC_READ);
2114 ret = nand_read_data_op(chip, p, eccsize, false);
2118 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2120 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
2121 if (stat == -EBADMSG &&
2122 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2123 /* check for empty pages with bitflips */
2124 stat = nand_check_erased_ecc_chunk(p, eccsize,
2125 &ecc_code[i], eccbytes,
2127 chip->ecc.strength);
2131 mtd->ecc_stats.failed++;
2133 mtd->ecc_stats.corrected += stat;
2134 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2137 return max_bitflips;
2141 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
2142 * @mtd: mtd info structure
2143 * @chip: nand chip info structure
2144 * @buf: buffer to store read data
2145 * @oob_required: caller requires OOB data read to chip->oob_poi
2146 * @page: page number to read
2148 * The hw generator calculates the error syndrome automatically. Therefore we
2149 * need a special oob layout and handling.
2151 static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2152 uint8_t *buf, int oob_required, int page)
2154 int ret, i, eccsize = chip->ecc.size;
2155 int eccbytes = chip->ecc.bytes;
2156 int eccsteps = chip->ecc.steps;
2157 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
2159 uint8_t *oob = chip->oob_poi;
2160 unsigned int max_bitflips = 0;
2162 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2165 chip->ecc.hwctl(mtd, NAND_ECC_READ);
2167 ret = nand_read_data_op(chip, p, eccsize, false);
2171 if (chip->ecc.prepad) {
2172 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2177 oob += chip->ecc.prepad;
2180 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
2182 ret = nand_read_data_op(chip, oob, eccbytes, false);
2186 stat = chip->ecc.correct(mtd, p, oob, NULL);
2190 if (chip->ecc.postpad) {
2191 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2196 oob += chip->ecc.postpad;
2199 if (stat == -EBADMSG &&
2200 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2201 /* check for empty pages with bitflips */
2202 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
2206 chip->ecc.strength);
2210 mtd->ecc_stats.failed++;
2212 mtd->ecc_stats.corrected += stat;
2213 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2217 /* Calculate remaining oob bytes */
2218 i = mtd->oobsize - (oob - chip->oob_poi);
2220 ret = nand_read_data_op(chip, oob, i, false);
2225 return max_bitflips;
2229 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
2230 * @chip: nand chip structure
2231 * @oob: oob destination address
2232 * @ops: oob ops structure
2233 * @len: size of oob to transfer
2235 static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
2236 struct mtd_oob_ops *ops, size_t len)
2238 switch (ops->mode) {
2240 case MTD_OPS_PLACE_OOB:
2242 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
2245 case MTD_OPS_AUTO_OOB: {
2246 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2247 uint32_t boffs = 0, roffs = ops->ooboffs;
2250 for (; free->length && len; free++, len -= bytes) {
2251 /* Read request not from offset 0? */
2252 if (unlikely(roffs)) {
2253 if (roffs >= free->length) {
2254 roffs -= free->length;
2257 boffs = free->offset + roffs;
2258 bytes = min_t(size_t, len,
2259 (free->length - roffs));
2262 bytes = min_t(size_t, len, free->length);
2263 boffs = free->offset;
2265 memcpy(oob, chip->oob_poi + boffs, bytes);
2277 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
2278 * @mtd: MTD device structure
2279 * @retry_mode: the retry mode to use
2281 * Some vendors supply a special command to shift the Vt threshold, to be used
2282 * when there are too many bitflips in a page (i.e., ECC error). After setting
2283 * a new threshold, the host should retry reading the page.
2285 static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
2287 struct nand_chip *chip = mtd_to_nand(mtd);
2289 pr_debug("setting READ RETRY mode %d\n", retry_mode);
2291 if (retry_mode >= chip->read_retries)
2294 if (!chip->setup_read_retry)
2297 return chip->setup_read_retry(mtd, retry_mode);
2301 * nand_do_read_ops - [INTERN] Read data with ECC
2302 * @mtd: MTD device structure
2303 * @from: offset to read from
2304 * @ops: oob ops structure
2306 * Internal function. Called with chip held.
2308 static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
2309 struct mtd_oob_ops *ops)
2311 int chipnr, page, realpage, col, bytes, aligned, oob_required;
2312 struct nand_chip *chip = mtd_to_nand(mtd);
2314 uint32_t readlen = ops->len;
2315 uint32_t oobreadlen = ops->ooblen;
2316 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
2318 uint8_t *bufpoi, *oob, *buf;
2320 unsigned int max_bitflips = 0;
2322 bool ecc_fail = false;
2324 chipnr = (int)(from >> chip->chip_shift);
2325 chip->select_chip(mtd, chipnr);
2327 realpage = (int)(from >> chip->page_shift);
2328 page = realpage & chip->pagemask;
2330 col = (int)(from & (mtd->writesize - 1));
2334 oob_required = oob ? 1 : 0;
2337 unsigned int ecc_failures = mtd->ecc_stats.failed;
2340 bytes = min(mtd->writesize - col, readlen);
2341 aligned = (bytes == mtd->writesize);
2345 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2346 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
2351 /* Is the current page in the buffer? */
2352 if (realpage != chip->pagebuf || oob) {
2353 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2355 if (use_bufpoi && aligned)
2356 pr_debug("%s: using read bounce buffer for buf@%p\n",
2360 if (nand_standard_page_accessors(&chip->ecc)) {
2361 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2367 * Now read the page into the buffer. Absent an error,
2368 * the read methods return max bitflips per ecc step.
2370 if (unlikely(ops->mode == MTD_OPS_RAW))
2371 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
2374 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
2376 ret = chip->ecc.read_subpage(mtd, chip,
2380 ret = chip->ecc.read_page(mtd, chip, bufpoi,
2381 oob_required, page);
2384 /* Invalidate page cache */
2389 max_bitflips = max_t(unsigned int, max_bitflips, ret);
2391 /* Transfer not aligned data */
2393 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
2394 !(mtd->ecc_stats.failed - ecc_failures) &&
2395 (ops->mode != MTD_OPS_RAW)) {
2396 chip->pagebuf = realpage;
2397 chip->pagebuf_bitflips = ret;
2399 /* Invalidate page cache */
2402 memcpy(buf, chip->buffers->databuf + col, bytes);
2405 if (unlikely(oob)) {
2406 int toread = min(oobreadlen, max_oobsize);
2409 oob = nand_transfer_oob(chip,
2411 oobreadlen -= toread;
2415 if (chip->options & NAND_NEED_READRDY) {
2416 /* Apply delay or wait for ready/busy pin */
2417 if (!chip->dev_ready)
2418 udelay(chip->chip_delay);
2420 nand_wait_ready(mtd);
2423 if (mtd->ecc_stats.failed - ecc_failures) {
2424 if (retry_mode + 1 < chip->read_retries) {
2426 ret = nand_setup_read_retry(mtd,
2431 /* Reset failures; retry */
2432 mtd->ecc_stats.failed = ecc_failures;
2435 /* No more retry modes; real failure */
2442 memcpy(buf, chip->buffers->databuf + col, bytes);
2444 max_bitflips = max_t(unsigned int, max_bitflips,
2445 chip->pagebuf_bitflips);
2450 /* Reset to retry mode 0 */
2452 ret = nand_setup_read_retry(mtd, 0);
2461 /* For subsequent reads align to page boundary */
2463 /* Increment page address */
2466 page = realpage & chip->pagemask;
2467 /* Check, if we cross a chip boundary */
2470 chip->select_chip(mtd, -1);
2471 chip->select_chip(mtd, chipnr);
2474 chip->select_chip(mtd, -1);
2476 ops->retlen = ops->len - (size_t) readlen;
2478 ops->oobretlen = ops->ooblen - oobreadlen;
2486 return max_bitflips;
2490 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
2491 * @mtd: mtd info structure
2492 * @chip: nand chip info structure
2493 * @page: page number to read
2495 static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
2498 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2502 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
2504 * @mtd: mtd info structure
2505 * @chip: nand chip info structure
2506 * @page: page number to read
2508 static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2511 int length = mtd->oobsize;
2512 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2513 int eccsize = chip->ecc.size;
2514 uint8_t *bufpoi = chip->oob_poi;
2515 int i, toread, sndrnd = 0, pos, ret;
2517 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
2521 for (i = 0; i < chip->ecc.steps; i++) {
2525 pos = eccsize + i * (eccsize + chunk);
2526 if (mtd->writesize > 512)
2527 ret = nand_change_read_column_op(chip, pos,
2531 ret = nand_read_page_op(chip, page, pos, NULL,
2538 toread = min_t(int, length, chunk);
2540 ret = nand_read_data_op(chip, bufpoi, toread, false);
2548 ret = nand_read_data_op(chip, bufpoi, length, false);
2557 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
2558 * @mtd: mtd info structure
2559 * @chip: nand chip info structure
2560 * @page: page number to write
2562 static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
2565 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
2570 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
2571 * with syndrome - only for large page flash
2572 * @mtd: mtd info structure
2573 * @chip: nand chip info structure
2574 * @page: page number to write
2576 static int nand_write_oob_syndrome(struct mtd_info *mtd,
2577 struct nand_chip *chip, int page)
2579 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2580 int eccsize = chip->ecc.size, length = mtd->oobsize;
2581 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
2582 const uint8_t *bufpoi = chip->oob_poi;
2585 * data-ecc-data-ecc ... ecc-oob
2587 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2589 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2590 pos = steps * (eccsize + chunk);
2595 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
2599 for (i = 0; i < steps; i++) {
2601 if (mtd->writesize <= 512) {
2602 uint32_t fill = 0xFFFFFFFF;
2606 int num = min_t(int, len, 4);
2608 ret = nand_write_data_op(chip, &fill,
2616 pos = eccsize + i * (eccsize + chunk);
2617 ret = nand_change_write_column_op(chip, pos,
2625 len = min_t(int, length, chunk);
2627 ret = nand_write_data_op(chip, bufpoi, len, false);
2635 ret = nand_write_data_op(chip, bufpoi, length, false);
2640 return nand_prog_page_end_op(chip);
2644 * nand_do_read_oob - [INTERN] NAND read out-of-band
2645 * @mtd: MTD device structure
2646 * @from: offset to read from
2647 * @ops: oob operations description structure
2649 * NAND read out-of-band data from the spare area.
2651 static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2652 struct mtd_oob_ops *ops)
2654 int page, realpage, chipnr;
2655 struct nand_chip *chip = mtd_to_nand(mtd);
2656 struct mtd_ecc_stats stats;
2657 int readlen = ops->ooblen;
2659 uint8_t *buf = ops->oobbuf;
2662 pr_debug("%s: from = 0x%08Lx, len = %i\n",
2663 __func__, (unsigned long long)from, readlen);
2665 stats = mtd->ecc_stats;
2667 len = mtd_oobavail(mtd, ops);
2669 if (unlikely(ops->ooboffs >= len)) {
2670 pr_debug("%s: attempt to start read outside oob\n",
2675 /* Do not allow reads past end of device */
2676 if (unlikely(from >= mtd->size ||
2677 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2678 (from >> chip->page_shift)) * len)) {
2679 pr_debug("%s: attempt to read beyond end of device\n",
2684 chipnr = (int)(from >> chip->chip_shift);
2685 chip->select_chip(mtd, chipnr);
2687 /* Shift to get page */
2688 realpage = (int)(from >> chip->page_shift);
2689 page = realpage & chip->pagemask;
2694 if (ops->mode == MTD_OPS_RAW)
2695 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2697 ret = chip->ecc.read_oob(mtd, chip, page);
2702 len = min(len, readlen);
2703 buf = nand_transfer_oob(chip, buf, ops, len);
2705 if (chip->options & NAND_NEED_READRDY) {
2706 /* Apply delay or wait for ready/busy pin */
2707 if (!chip->dev_ready)
2708 udelay(chip->chip_delay);
2710 nand_wait_ready(mtd);
2717 /* Increment page address */
2720 page = realpage & chip->pagemask;
2721 /* Check, if we cross a chip boundary */
2724 chip->select_chip(mtd, -1);
2725 chip->select_chip(mtd, chipnr);
2728 chip->select_chip(mtd, -1);
2730 ops->oobretlen = ops->ooblen - readlen;
2735 if (mtd->ecc_stats.failed - stats.failed)
2738 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
2742 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
2743 * @mtd: MTD device structure
2744 * @from: offset to read from
2745 * @ops: oob operation description structure
2747 * NAND read data and/or out-of-band data.
2749 static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2750 struct mtd_oob_ops *ops)
2752 int ret = -ENOTSUPP;
2756 /* Do not allow reads past end of device */
2757 if (ops->datbuf && (from + ops->len) > mtd->size) {
2758 pr_debug("%s: attempt to read beyond end of device\n",
2763 nand_get_device(mtd, FL_READING);
2765 switch (ops->mode) {
2766 case MTD_OPS_PLACE_OOB:
2767 case MTD_OPS_AUTO_OOB:
2776 ret = nand_do_read_oob(mtd, from, ops);
2778 ret = nand_do_read_ops(mtd, from, ops);
2781 nand_release_device(mtd);
2787 * nand_write_page_raw - [INTERN] raw page write function
2788 * @mtd: mtd info structure
2789 * @chip: nand chip info structure
2791 * @oob_required: must write chip->oob_poi to OOB
2792 * @page: page number to write
2794 * Not for syndrome calculating ECC controllers, which use a special oob layout.
2796 static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2797 const uint8_t *buf, int oob_required, int page)
2801 ret = nand_write_data_op(chip, buf, mtd->writesize, false);
2806 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
2816 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2817 * @mtd: mtd info structure
2818 * @chip: nand chip info structure
2820 * @oob_required: must write chip->oob_poi to OOB
2821 * @page: page number to write
2823 * We need a special oob layout and handling even when ECC isn't checked.
2825 static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
2826 struct nand_chip *chip,
2827 const uint8_t *buf, int oob_required,
2830 int eccsize = chip->ecc.size;
2831 int eccbytes = chip->ecc.bytes;
2832 uint8_t *oob = chip->oob_poi;
2833 int steps, size, ret;
2835 for (steps = chip->ecc.steps; steps > 0; steps--) {
2836 ret = nand_write_data_op(chip, buf, eccsize, false);
2842 if (chip->ecc.prepad) {
2843 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
2848 oob += chip->ecc.prepad;
2851 ret = nand_write_data_op(chip, oob, eccbytes, false);
2857 if (chip->ecc.postpad) {
2858 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
2863 oob += chip->ecc.postpad;
2867 size = mtd->oobsize - (oob - chip->oob_poi);
2869 ret = nand_write_data_op(chip, oob, size, false);
2877 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2878 * @mtd: mtd info structure
2879 * @chip: nand chip info structure
2881 * @oob_required: must write chip->oob_poi to OOB
2882 * @page: page number to write
2884 static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
2885 const uint8_t *buf, int oob_required,
2888 int i, eccsize = chip->ecc.size;
2889 int eccbytes = chip->ecc.bytes;
2890 int eccsteps = chip->ecc.steps;
2891 uint8_t *ecc_calc = chip->buffers->ecccalc;
2892 const uint8_t *p = buf;
2893 uint32_t *eccpos = chip->ecc.layout->eccpos;
2895 /* Software ECC calculation */
2896 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2897 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2899 for (i = 0; i < chip->ecc.total; i++)
2900 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2902 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
2906 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2907 * @mtd: mtd info structure
2908 * @chip: nand chip info structure
2910 * @oob_required: must write chip->oob_poi to OOB
2911 * @page: page number to write
2913 static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
2914 const uint8_t *buf, int oob_required,
2917 int i, eccsize = chip->ecc.size;
2918 int eccbytes = chip->ecc.bytes;
2919 int eccsteps = chip->ecc.steps;
2920 uint8_t *ecc_calc = chip->buffers->ecccalc;
2921 const uint8_t *p = buf;
2922 uint32_t *eccpos = chip->ecc.layout->eccpos;
2925 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2926 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2928 ret = nand_write_data_op(chip, p, eccsize, false);
2932 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2935 for (i = 0; i < chip->ecc.total; i++)
2936 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2938 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2947 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
2948 * @mtd: mtd info structure
2949 * @chip: nand chip info structure
2950 * @offset: column address of subpage within the page
2951 * @data_len: data length
2953 * @oob_required: must write chip->oob_poi to OOB
2954 * @page: page number to write
2956 static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2957 struct nand_chip *chip, uint32_t offset,
2958 uint32_t data_len, const uint8_t *buf,
2959 int oob_required, int page)
2961 uint8_t *oob_buf = chip->oob_poi;
2962 uint8_t *ecc_calc = chip->buffers->ecccalc;
2963 int ecc_size = chip->ecc.size;
2964 int ecc_bytes = chip->ecc.bytes;
2965 int ecc_steps = chip->ecc.steps;
2966 uint32_t *eccpos = chip->ecc.layout->eccpos;
2967 uint32_t start_step = offset / ecc_size;
2968 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2969 int oob_bytes = mtd->oobsize / ecc_steps;
2973 for (step = 0; step < ecc_steps; step++) {
2974 /* configure controller for WRITE access */
2975 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2977 /* write data (untouched subpages already masked by 0xFF) */
2978 ret = nand_write_data_op(chip, buf, ecc_size, false);
2982 /* mask ECC of un-touched subpages by padding 0xFF */
2983 if ((step < start_step) || (step > end_step))
2984 memset(ecc_calc, 0xff, ecc_bytes);
2986 chip->ecc.calculate(mtd, buf, ecc_calc);
2988 /* mask OOB of un-touched subpages by padding 0xFF */
2989 /* if oob_required, preserve OOB metadata of written subpage */
2990 if (!oob_required || (step < start_step) || (step > end_step))
2991 memset(oob_buf, 0xff, oob_bytes);
2994 ecc_calc += ecc_bytes;
2995 oob_buf += oob_bytes;
2998 /* copy calculated ECC for whole page to chip->buffer->oob */
2999 /* this include masked-value(0xFF) for unwritten subpages */
3000 ecc_calc = chip->buffers->ecccalc;
3001 for (i = 0; i < chip->ecc.total; i++)
3002 chip->oob_poi[eccpos[i]] = ecc_calc[i];
3004 /* write OOB buffer to NAND device */
3005 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3014 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
3015 * @mtd: mtd info structure
3016 * @chip: nand chip info structure
3018 * @oob_required: must write chip->oob_poi to OOB
3019 * @page: page number to write
3021 * The hw generator calculates the error syndrome automatically. Therefore we
3022 * need a special oob layout and handling.
3024 static int nand_write_page_syndrome(struct mtd_info *mtd,
3025 struct nand_chip *chip,
3026 const uint8_t *buf, int oob_required,
3029 int i, eccsize = chip->ecc.size;
3030 int eccbytes = chip->ecc.bytes;
3031 int eccsteps = chip->ecc.steps;
3032 const uint8_t *p = buf;
3033 uint8_t *oob = chip->oob_poi;
3036 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3037 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
3039 ret = nand_write_data_op(chip, p, eccsize, false);
3043 if (chip->ecc.prepad) {
3044 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3049 oob += chip->ecc.prepad;
3052 chip->ecc.calculate(mtd, p, oob);
3054 ret = nand_write_data_op(chip, oob, eccbytes, false);
3060 if (chip->ecc.postpad) {
3061 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3066 oob += chip->ecc.postpad;
3070 /* Calculate remaining oob bytes */
3071 i = mtd->oobsize - (oob - chip->oob_poi);
3073 ret = nand_write_data_op(chip, oob, i, false);
3082 * nand_write_page - [REPLACEABLE] write one page
3083 * @mtd: MTD device structure
3084 * @chip: NAND chip descriptor
3085 * @offset: address offset within the page
3086 * @data_len: length of actual data to be written
3087 * @buf: the data to write
3088 * @oob_required: must write chip->oob_poi to OOB
3089 * @page: page number to write
3090 * @raw: use _raw version of write_page
3092 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
3093 uint32_t offset, int data_len, const uint8_t *buf,
3094 int oob_required, int page, int raw)
3096 int status, subpage;
3098 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3099 chip->ecc.write_subpage)
3100 subpage = offset || (data_len < mtd->writesize);
3104 if (nand_standard_page_accessors(&chip->ecc)) {
3105 status = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3111 status = chip->ecc.write_page_raw(mtd, chip, buf,
3112 oob_required, page);
3114 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
3115 buf, oob_required, page);
3117 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
3123 if (nand_standard_page_accessors(&chip->ecc))
3124 return nand_prog_page_end_op(chip);
3130 * nand_fill_oob - [INTERN] Transfer client buffer to oob
3131 * @mtd: MTD device structure
3132 * @oob: oob data buffer
3133 * @len: oob data write length
3134 * @ops: oob ops structure
3136 static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
3137 struct mtd_oob_ops *ops)
3139 struct nand_chip *chip = mtd_to_nand(mtd);
3142 * Initialise to all 0xFF, to avoid the possibility of left over OOB
3143 * data from a previous OOB read.
3145 memset(chip->oob_poi, 0xff, mtd->oobsize);
3147 switch (ops->mode) {
3149 case MTD_OPS_PLACE_OOB:
3151 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
3154 case MTD_OPS_AUTO_OOB: {
3155 struct nand_oobfree *free = chip->ecc.layout->oobfree;
3156 uint32_t boffs = 0, woffs = ops->ooboffs;
3159 for (; free->length && len; free++, len -= bytes) {
3160 /* Write request not from offset 0? */
3161 if (unlikely(woffs)) {
3162 if (woffs >= free->length) {
3163 woffs -= free->length;
3166 boffs = free->offset + woffs;
3167 bytes = min_t(size_t, len,
3168 (free->length - woffs));
3171 bytes = min_t(size_t, len, free->length);
3172 boffs = free->offset;
3174 memcpy(chip->oob_poi + boffs, oob, bytes);
3185 #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
3188 * nand_do_write_ops - [INTERN] NAND write with ECC
3189 * @mtd: MTD device structure
3190 * @to: offset to write to
3191 * @ops: oob operations description structure
3193 * NAND write with ECC.
3195 static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
3196 struct mtd_oob_ops *ops)
3198 int chipnr, realpage, page, column;
3199 struct nand_chip *chip = mtd_to_nand(mtd);
3200 uint32_t writelen = ops->len;
3202 uint32_t oobwritelen = ops->ooblen;
3203 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
3205 uint8_t *oob = ops->oobbuf;
3206 uint8_t *buf = ops->datbuf;
3208 int oob_required = oob ? 1 : 0;
3214 /* Reject writes, which are not page aligned */
3215 if (NOTALIGNED(to)) {
3216 pr_notice("%s: attempt to write non page aligned data\n",
3221 column = to & (mtd->writesize - 1);
3223 chipnr = (int)(to >> chip->chip_shift);
3224 chip->select_chip(mtd, chipnr);
3226 /* Check, if it is write protected */
3227 if (nand_check_wp(mtd)) {
3232 realpage = (int)(to >> chip->page_shift);
3233 page = realpage & chip->pagemask;
3235 /* Invalidate the page cache, when we write to the cached page */
3236 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3237 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
3240 /* Don't allow multipage oob writes with offset */
3241 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3247 int bytes = mtd->writesize;
3248 uint8_t *wbuf = buf;
3250 int part_pagewr = (column || writelen < mtd->writesize);
3254 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
3255 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
3261 /* Partial page write?, or need to use bounce buffer */
3263 pr_debug("%s: using write bounce buffer for buf@%p\n",
3266 bytes = min_t(int, bytes - column, writelen);
3268 memset(chip->buffers->databuf, 0xff, mtd->writesize);
3269 memcpy(&chip->buffers->databuf[column], buf, bytes);
3270 wbuf = chip->buffers->databuf;
3273 if (unlikely(oob)) {
3274 size_t len = min(oobwritelen, oobmaxlen);
3275 oob = nand_fill_oob(mtd, oob, len, ops);
3278 /* We still need to erase leftover OOB data */
3279 memset(chip->oob_poi, 0xff, mtd->oobsize);
3281 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
3283 (ops->mode == MTD_OPS_RAW));
3295 page = realpage & chip->pagemask;
3296 /* Check, if we cross a chip boundary */
3299 chip->select_chip(mtd, -1);
3300 chip->select_chip(mtd, chipnr);
3304 ops->retlen = ops->len - writelen;
3306 ops->oobretlen = ops->ooblen;
3309 chip->select_chip(mtd, -1);
3314 * panic_nand_write - [MTD Interface] NAND write with ECC
3315 * @mtd: MTD device structure
3316 * @to: offset to write to
3317 * @len: number of bytes to write
3318 * @retlen: pointer to variable to store the number of written bytes
3319 * @buf: the data to write
3321 * NAND write with ECC. Used when performing writes in interrupt context, this
3322 * may for example be called by mtdoops when writing an oops while in panic.
3324 static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
3325 size_t *retlen, const uint8_t *buf)
3327 struct nand_chip *chip = mtd_to_nand(mtd);
3328 struct mtd_oob_ops ops;
3331 /* Wait for the device to get ready */
3332 panic_nand_wait(mtd, chip, 400);
3334 /* Grab the device */
3335 panic_nand_get_device(chip, mtd, FL_WRITING);
3337 memset(&ops, 0, sizeof(ops));
3339 ops.datbuf = (uint8_t *)buf;
3340 ops.mode = MTD_OPS_PLACE_OOB;
3342 ret = nand_do_write_ops(mtd, to, &ops);
3344 *retlen = ops.retlen;
3349 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
3350 * @mtd: MTD device structure
3351 * @to: offset to write to
3352 * @ops: oob operation description structure
3354 * NAND write out-of-band.
3356 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3357 struct mtd_oob_ops *ops)
3359 int chipnr, page, status, len;
3360 struct nand_chip *chip = mtd_to_nand(mtd);
3362 pr_debug("%s: to = 0x%08x, len = %i\n",
3363 __func__, (unsigned int)to, (int)ops->ooblen);
3365 len = mtd_oobavail(mtd, ops);
3367 /* Do not allow write past end of page */
3368 if ((ops->ooboffs + ops->ooblen) > len) {
3369 pr_debug("%s: attempt to write past end of page\n",
3374 if (unlikely(ops->ooboffs >= len)) {
3375 pr_debug("%s: attempt to start write outside oob\n",
3380 /* Do not allow write past end of device */
3381 if (unlikely(to >= mtd->size ||
3382 ops->ooboffs + ops->ooblen >
3383 ((mtd->size >> chip->page_shift) -
3384 (to >> chip->page_shift)) * len)) {
3385 pr_debug("%s: attempt to write beyond end of device\n",
3390 chipnr = (int)(to >> chip->chip_shift);
3393 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3394 * of my DiskOnChip 2000 test units) will clear the whole data page too
3395 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3396 * it in the doc2000 driver in August 1999. dwmw2.
3398 nand_reset(chip, chipnr);
3400 chip->select_chip(mtd, chipnr);
3402 /* Shift to get page */
3403 page = (int)(to >> chip->page_shift);
3405 /* Check, if it is write protected */
3406 if (nand_check_wp(mtd)) {
3407 chip->select_chip(mtd, -1);
3411 /* Invalidate the page cache, if we write to the cached page */
3412 if (page == chip->pagebuf)
3415 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
3417 if (ops->mode == MTD_OPS_RAW)
3418 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3420 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
3422 chip->select_chip(mtd, -1);
3427 ops->oobretlen = ops->ooblen;
3433 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
3434 * @mtd: MTD device structure
3435 * @to: offset to write to
3436 * @ops: oob operation description structure
3438 static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3439 struct mtd_oob_ops *ops)
3441 int ret = -ENOTSUPP;
3445 /* Do not allow writes past end of device */
3446 if (ops->datbuf && (to + ops->len) > mtd->size) {
3447 pr_debug("%s: attempt to write beyond end of device\n",
3452 nand_get_device(mtd, FL_WRITING);
3454 switch (ops->mode) {
3455 case MTD_OPS_PLACE_OOB:
3456 case MTD_OPS_AUTO_OOB:
3465 ret = nand_do_write_oob(mtd, to, ops);
3467 ret = nand_do_write_ops(mtd, to, ops);
3470 nand_release_device(mtd);
3475 * single_erase - [GENERIC] NAND standard block erase command function
3476 * @mtd: MTD device structure
3477 * @page: the page address of the block which will be erased
3479 * Standard erase command for NAND chips. Returns NAND status.
3481 static int single_erase(struct mtd_info *mtd, int page)
3483 struct nand_chip *chip = mtd_to_nand(mtd);
3484 unsigned int eraseblock;
3486 /* Send commands to erase a block */
3487 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
3489 return nand_erase_op(chip, eraseblock);
3493 * nand_erase - [MTD Interface] erase block(s)
3494 * @mtd: MTD device structure
3495 * @instr: erase instruction
3497 * Erase one ore more blocks.
3499 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
3501 return nand_erase_nand(mtd, instr, 0);
3505 * nand_erase_nand - [INTERN] erase block(s)
3506 * @mtd: MTD device structure
3507 * @instr: erase instruction
3508 * @allowbbt: allow erasing the bbt area
3510 * Erase one ore more blocks.
3512 int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3515 int page, status, pages_per_block, ret, chipnr;
3516 struct nand_chip *chip = mtd_to_nand(mtd);
3519 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3520 __func__, (unsigned long long)instr->addr,
3521 (unsigned long long)instr->len);
3523 if (check_offs_len(mtd, instr->addr, instr->len))
3526 /* Grab the lock and see if the device is available */
3527 nand_get_device(mtd, FL_ERASING);
3529 /* Shift to get first page */
3530 page = (int)(instr->addr >> chip->page_shift);
3531 chipnr = (int)(instr->addr >> chip->chip_shift);
3533 /* Calculate pages in each block */
3534 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
3536 /* Select the NAND device */
3537 chip->select_chip(mtd, chipnr);
3539 /* Check, if it is write protected */
3540 if (nand_check_wp(mtd)) {
3541 pr_debug("%s: device is write protected!\n",
3543 instr->state = MTD_ERASE_FAILED;
3547 /* Loop through the pages */
3550 instr->state = MTD_ERASING;
3555 /* Check if we have a bad block, we do not erase bad blocks! */
3556 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
3557 chip->page_shift, allowbbt)) {
3558 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3560 instr->state = MTD_ERASE_FAILED;
3565 * Invalidate the page cache, if we erase the block which
3566 * contains the current cached page.
3568 if (page <= chip->pagebuf && chip->pagebuf <
3569 (page + pages_per_block))
3572 status = chip->erase(mtd, page & chip->pagemask);
3574 /* See if block erase succeeded */
3575 if (status & NAND_STATUS_FAIL) {
3576 pr_debug("%s: failed erase, page 0x%08x\n",
3578 instr->state = MTD_ERASE_FAILED;
3580 ((loff_t)page << chip->page_shift);
3584 /* Increment page address and decrement length */
3585 len -= (1ULL << chip->phys_erase_shift);
3586 page += pages_per_block;
3588 /* Check, if we cross a chip boundary */
3589 if (len && !(page & chip->pagemask)) {
3591 chip->select_chip(mtd, -1);
3592 chip->select_chip(mtd, chipnr);
3595 instr->state = MTD_ERASE_DONE;
3599 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
3601 /* Deselect and wake up anyone waiting on the device */
3602 chip->select_chip(mtd, -1);
3603 nand_release_device(mtd);
3605 /* Do call back function */
3607 mtd_erase_callback(instr);
3609 /* Return more or less happy */
3614 * nand_sync - [MTD Interface] sync
3615 * @mtd: MTD device structure
3617 * Sync is actually a wait for chip ready function.
3619 static void nand_sync(struct mtd_info *mtd)
3621 pr_debug("%s: called\n", __func__);
3623 /* Grab the lock and see if the device is available */
3624 nand_get_device(mtd, FL_SYNCING);
3625 /* Release it and go back */
3626 nand_release_device(mtd);
3630 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
3631 * @mtd: MTD device structure
3632 * @offs: offset relative to mtd start
3634 static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
3636 struct nand_chip *chip = mtd_to_nand(mtd);
3637 int chipnr = (int)(offs >> chip->chip_shift);
3640 /* Select the NAND device */
3641 nand_get_device(mtd, FL_READING);
3642 chip->select_chip(mtd, chipnr);
3644 ret = nand_block_checkbad(mtd, offs, 0);
3646 chip->select_chip(mtd, -1);
3647 nand_release_device(mtd);
3653 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
3654 * @mtd: MTD device structure
3655 * @ofs: offset relative to mtd start
3657 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
3661 ret = nand_block_isbad(mtd, ofs);
3663 /* If it was bad already, return success and do nothing */
3669 return nand_block_markbad_lowlevel(mtd, ofs);
3673 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3674 * @mtd: MTD device structure
3675 * @chip: nand chip info structure
3676 * @addr: feature address.
3677 * @subfeature_param: the subfeature parameters, a four bytes array.
3679 static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3680 int addr, uint8_t *subfeature_param)
3682 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3683 if (!chip->onfi_version ||
3684 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3685 & ONFI_OPT_CMD_SET_GET_FEATURES))
3689 return nand_set_features_op(chip, addr, subfeature_param);
3693 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3694 * @mtd: MTD device structure
3695 * @chip: nand chip info structure
3696 * @addr: feature address.
3697 * @subfeature_param: the subfeature parameters, a four bytes array.
3699 static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3700 int addr, uint8_t *subfeature_param)
3702 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3703 if (!chip->onfi_version ||
3704 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3705 & ONFI_OPT_CMD_SET_GET_FEATURES))
3709 return nand_get_features_op(chip, addr, subfeature_param);
3712 /* Set default functions */
3713 static void nand_set_defaults(struct nand_chip *chip, int busw)
3715 /* check for proper chip_delay setup, set 20us if not */
3716 if (!chip->chip_delay)
3717 chip->chip_delay = 20;
3719 /* check, if a user supplied command function given */
3720 if (chip->cmdfunc == NULL)
3721 chip->cmdfunc = nand_command;
3723 /* check, if a user supplied wait function given */
3724 if (chip->waitfunc == NULL)
3725 chip->waitfunc = nand_wait;
3727 if (!chip->select_chip)
3728 chip->select_chip = nand_select_chip;
3730 /* set for ONFI nand */
3731 if (!chip->onfi_set_features)
3732 chip->onfi_set_features = nand_onfi_set_features;
3733 if (!chip->onfi_get_features)
3734 chip->onfi_get_features = nand_onfi_get_features;
3736 /* If called twice, pointers that depend on busw may need to be reset */
3737 if (!chip->read_byte || chip->read_byte == nand_read_byte)
3738 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3739 if (!chip->read_word)
3740 chip->read_word = nand_read_word;
3741 if (!chip->block_bad)
3742 chip->block_bad = nand_block_bad;
3743 if (!chip->block_markbad)
3744 chip->block_markbad = nand_default_block_markbad;
3745 if (!chip->write_buf || chip->write_buf == nand_write_buf)
3746 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
3747 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3748 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3749 if (!chip->read_buf || chip->read_buf == nand_read_buf)
3750 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
3751 if (!chip->scan_bbt)
3752 chip->scan_bbt = nand_default_bbt;
3754 if (!chip->controller) {
3755 chip->controller = &chip->hwcontrol;
3756 spin_lock_init(&chip->controller->lock);
3757 init_waitqueue_head(&chip->controller->wq);
3760 if (!chip->buf_align)
3761 chip->buf_align = 1;
3764 /* Sanitize ONFI strings so we can safely print them */
3765 static void sanitize_string(char *s, size_t len)
3769 /* Null terminate */
3772 /* Remove non printable chars */
3773 for (i = 0; i < len - 1; i++) {
3774 if (s[i] < ' ' || s[i] > 127)
3778 /* Remove trailing spaces */
3782 static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3787 for (i = 0; i < 8; i++)
3788 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3794 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3795 /* Parse the Extended Parameter Page. */
3796 static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3797 struct nand_chip *chip, struct nand_onfi_params *p)
3799 struct onfi_ext_param_page *ep;
3800 struct onfi_ext_section *s;
3801 struct onfi_ext_ecc_info *ecc;
3807 len = le16_to_cpu(p->ext_param_page_length) * 16;
3808 ep = kmalloc(len, GFP_KERNEL);
3812 /* Send our own NAND_CMD_PARAM. */
3813 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3817 /* Use the Change Read Column command to skip the ONFI param pages. */
3818 ret = nand_change_read_column_op(chip,
3819 sizeof(*p) * p->num_of_param_pages,
3825 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3826 != le16_to_cpu(ep->crc))) {
3827 pr_debug("fail in the CRC.\n");
3832 * Check the signature.
3833 * Do not strictly follow the ONFI spec, maybe changed in future.
3835 if (strncmp((char *)ep->sig, "EPPS", 4)) {
3836 pr_debug("The signature is invalid.\n");
3840 /* find the ECC section. */
3841 cursor = (uint8_t *)(ep + 1);
3842 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3843 s = ep->sections + i;
3844 if (s->type == ONFI_SECTION_TYPE_2)
3846 cursor += s->length * 16;
3848 if (i == ONFI_EXT_SECTION_MAX) {
3849 pr_debug("We can not find the ECC section.\n");
3853 /* get the info we want. */
3854 ecc = (struct onfi_ext_ecc_info *)cursor;
3856 if (!ecc->codeword_size) {
3857 pr_debug("Invalid codeword size\n");
3861 chip->ecc_strength_ds = ecc->ecc_bits;
3862 chip->ecc_step_ds = 1 << ecc->codeword_size;
3870 static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3872 struct nand_chip *chip = mtd_to_nand(mtd);
3873 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3875 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3880 * Configure chip properties from Micron vendor-specific ONFI table
3882 static void nand_onfi_detect_micron(struct nand_chip *chip,
3883 struct nand_onfi_params *p)
3885 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3887 if (le16_to_cpu(p->vendor_revision) < 1)
3890 chip->read_retries = micron->read_retry_options;
3891 chip->setup_read_retry = nand_setup_read_retry_micron;
3895 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
3897 static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
3900 struct nand_onfi_params *p = &chip->onfi_params;
3904 /* Try ONFI for unknown chip or LP */
3905 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
3906 if (ret || strncmp(id, "ONFI", 4))
3909 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3913 for (i = 0; i < 3; i++) {
3914 ret = nand_read_data_op(chip, p, sizeof(*p), true);
3918 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3919 le16_to_cpu(p->crc)) {
3925 pr_err("Could not find valid ONFI parameter page; aborting\n");
3930 val = le16_to_cpu(p->revision);
3932 chip->onfi_version = 23;
3933 else if (val & (1 << 4))
3934 chip->onfi_version = 22;
3935 else if (val & (1 << 3))
3936 chip->onfi_version = 21;
3937 else if (val & (1 << 2))
3938 chip->onfi_version = 20;
3939 else if (val & (1 << 1))
3940 chip->onfi_version = 10;
3942 if (!chip->onfi_version) {
3943 pr_info("unsupported ONFI version: %d\n", val);
3947 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3948 sanitize_string(p->model, sizeof(p->model));
3950 mtd->name = p->model;
3952 mtd->writesize = le32_to_cpu(p->byte_per_page);
3955 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3956 * (don't ask me who thought of this...). MTD assumes that these
3957 * dimensions will be power-of-2, so just truncate the remaining area.
3959 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3960 mtd->erasesize *= mtd->writesize;
3962 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3964 /* See erasesize comment */
3965 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3966 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3967 chip->bits_per_cell = p->bits_per_cell;
3969 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
3970 *busw = NAND_BUSWIDTH_16;
3974 if (p->ecc_bits != 0xff) {
3975 chip->ecc_strength_ds = p->ecc_bits;
3976 chip->ecc_step_ds = 512;
3977 } else if (chip->onfi_version >= 21 &&
3978 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3981 * The nand_flash_detect_ext_param_page() uses the
3982 * Change Read Column command which maybe not supported
3983 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3984 * now. We do not replace user supplied command function.
3986 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3987 chip->cmdfunc = nand_command_lp;
3989 /* The Extended Parameter Page is supported since ONFI 2.1. */
3990 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3991 pr_warn("Failed to detect ONFI extended param page\n");
3993 pr_warn("Could not retrieve ONFI ECC requirements\n");
3996 if (p->jedec_id == NAND_MFR_MICRON)
3997 nand_onfi_detect_micron(chip, p);
4002 static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
4010 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
4012 static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
4015 struct nand_jedec_params *p = &chip->jedec_params;
4016 struct jedec_ecc_info *ecc;
4020 /* Try JEDEC for unknown chip or LP */
4021 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
4022 if (ret || strncmp(id, "JEDEC", sizeof(id)))
4025 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
4029 for (i = 0; i < 3; i++) {
4030 ret = nand_read_data_op(chip, p, sizeof(*p), true);
4034 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
4035 le16_to_cpu(p->crc))
4040 pr_err("Could not find valid JEDEC parameter page; aborting\n");
4045 val = le16_to_cpu(p->revision);
4047 chip->jedec_version = 10;
4048 else if (val & (1 << 1))
4049 chip->jedec_version = 1; /* vendor specific version */
4051 if (!chip->jedec_version) {
4052 pr_info("unsupported JEDEC version: %d\n", val);
4056 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
4057 sanitize_string(p->model, sizeof(p->model));
4059 mtd->name = p->model;
4061 mtd->writesize = le32_to_cpu(p->byte_per_page);
4063 /* Please reference to the comment for nand_flash_detect_onfi. */
4064 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
4065 mtd->erasesize *= mtd->writesize;
4067 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
4069 /* Please reference to the comment for nand_flash_detect_onfi. */
4070 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
4071 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
4072 chip->bits_per_cell = p->bits_per_cell;
4074 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
4075 *busw = NAND_BUSWIDTH_16;
4080 ecc = &p->ecc_info[0];
4082 if (ecc->codeword_size >= 9) {
4083 chip->ecc_strength_ds = ecc->ecc_bits;
4084 chip->ecc_step_ds = 1 << ecc->codeword_size;
4086 pr_warn("Invalid codeword size\n");
4093 * nand_id_has_period - Check if an ID string has a given wraparound period
4094 * @id_data: the ID string
4095 * @arrlen: the length of the @id_data array
4096 * @period: the period of repitition
4098 * Check if an ID string is repeated within a given sequence of bytes at
4099 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
4100 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
4101 * if the repetition has a period of @period; otherwise, returns zero.
4103 static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4106 for (i = 0; i < period; i++)
4107 for (j = i + period; j < arrlen; j += period)
4108 if (id_data[i] != id_data[j])
4114 * nand_id_len - Get the length of an ID string returned by CMD_READID
4115 * @id_data: the ID string
4116 * @arrlen: the length of the @id_data array
4118 * Returns the length of the ID string, according to known wraparound/trailing
4119 * zero patterns. If no pattern exists, returns the length of the array.
4121 static int nand_id_len(u8 *id_data, int arrlen)
4123 int last_nonzero, period;
4125 /* Find last non-zero byte */
4126 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4127 if (id_data[last_nonzero])
4131 if (last_nonzero < 0)
4134 /* Calculate wraparound period */
4135 for (period = 1; period < arrlen; period++)
4136 if (nand_id_has_period(id_data, arrlen, period))
4139 /* There's a repeated pattern */
4140 if (period < arrlen)
4143 /* There are trailing zeros */
4144 if (last_nonzero < arrlen - 1)
4145 return last_nonzero + 1;
4147 /* No pattern detected */
4151 /* Extract the bits of per cell from the 3rd byte of the extended ID */
4152 static int nand_get_bits_per_cell(u8 cellinfo)
4156 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4157 bits >>= NAND_CI_CELLTYPE_SHIFT;
4162 * Many new NAND share similar device ID codes, which represent the size of the
4163 * chip. The rest of the parameters must be decoded according to generic or
4164 * manufacturer-specific "extended ID" decoding patterns.
4166 static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
4167 u8 id_data[8], int *busw)
4170 /* The 3rd id byte holds MLC / multichip data */
4171 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
4172 /* The 4th id byte is the important one */
4175 id_len = nand_id_len(id_data, 8);
4178 * Field definitions are in the following datasheets:
4179 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
4180 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
4181 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
4183 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
4184 * ID to decide what to do.
4186 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
4187 !nand_is_slc(chip) && id_data[5] != 0x00) {
4189 mtd->writesize = 2048 << (extid & 0x03);
4192 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
4212 default: /* Other cases are "reserved" (unknown) */
4213 mtd->oobsize = 1024;
4217 /* Calc blocksize */
4218 mtd->erasesize = (128 * 1024) <<
4219 (((extid >> 1) & 0x04) | (extid & 0x03));
4221 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
4222 !nand_is_slc(chip)) {
4226 mtd->writesize = 2048 << (extid & 0x03);
4229 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
4253 /* Calc blocksize */
4254 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
4256 mtd->erasesize = (128 * 1024) << tmp;
4257 else if (tmp == 0x03)
4258 mtd->erasesize = 768 * 1024;
4260 mtd->erasesize = (64 * 1024) << tmp;
4264 mtd->writesize = 1024 << (extid & 0x03);
4267 mtd->oobsize = (8 << (extid & 0x01)) *
4268 (mtd->writesize >> 9);
4270 /* Calc blocksize. Blocksize is multiples of 64KiB */
4271 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4273 /* Get buswidth information */
4274 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
4277 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
4278 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
4280 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
4282 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
4284 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
4285 nand_is_slc(chip) &&
4286 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
4287 !(id_data[4] & 0x80) /* !BENAND */) {
4288 mtd->oobsize = 32 * mtd->writesize >> 9;
4295 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4296 * decodes a matching ID table entry and assigns the MTD size parameters for
4299 static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
4300 struct nand_flash_dev *type, u8 id_data[8],
4303 int maf_id = id_data[0];
4305 mtd->erasesize = type->erasesize;
4306 mtd->writesize = type->pagesize;
4307 mtd->oobsize = mtd->writesize / 32;
4308 *busw = type->options & NAND_BUSWIDTH_16;
4310 /* All legacy ID NAND are small-page, SLC */
4311 chip->bits_per_cell = 1;
4314 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
4315 * some Spansion chips have erasesize that conflicts with size
4316 * listed in nand_ids table.
4317 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
4319 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
4320 && id_data[6] == 0x00 && id_data[7] == 0x00
4321 && mtd->writesize == 512) {
4322 mtd->erasesize = 128 * 1024;
4323 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
4328 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4329 * heuristic patterns using various detected parameters (e.g., manufacturer,
4330 * page size, cell-type information).
4332 static void nand_decode_bbm_options(struct mtd_info *mtd,
4333 struct nand_chip *chip, u8 id_data[8])
4335 int maf_id = id_data[0];
4337 /* Set the bad block position */
4338 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4339 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4341 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
4344 * Bad block marker is stored in the last page of each block on Samsung
4345 * and Hynix MLC devices; stored in first two pages of each block on
4346 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
4347 * AMD/Spansion, and Macronix. All others scan only the first page.
4349 if (!nand_is_slc(chip) &&
4350 (maf_id == NAND_MFR_SAMSUNG ||
4351 maf_id == NAND_MFR_HYNIX))
4352 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
4353 else if ((nand_is_slc(chip) &&
4354 (maf_id == NAND_MFR_SAMSUNG ||
4355 maf_id == NAND_MFR_HYNIX ||
4356 maf_id == NAND_MFR_TOSHIBA ||
4357 maf_id == NAND_MFR_AMD ||
4358 maf_id == NAND_MFR_MACRONIX)) ||
4359 (mtd->writesize == 2048 &&
4360 maf_id == NAND_MFR_MICRON))
4361 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
4364 static inline bool is_full_id_nand(struct nand_flash_dev *type)
4366 return type->id_len;
4369 static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
4370 struct nand_flash_dev *type, u8 *id_data, int *busw)
4372 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
4373 mtd->writesize = type->pagesize;
4374 mtd->erasesize = type->erasesize;
4375 mtd->oobsize = type->oobsize;
4377 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
4378 chip->chipsize = (uint64_t)type->chipsize << 20;
4379 chip->options |= type->options;
4380 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4381 chip->ecc_step_ds = NAND_ECC_STEP(type);
4382 chip->onfi_timing_mode_default =
4383 type->onfi_timing_mode_default;
4385 *busw = type->options & NAND_BUSWIDTH_16;
4388 mtd->name = type->name;
4396 * Get the flash and manufacturer id and lookup if the type is supported.
4398 struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
4399 struct nand_chip *chip,
4400 int *maf_id, int *dev_id,
4401 struct nand_flash_dev *type)
4408 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
4411 ret = nand_reset(chip, 0);
4413 return ERR_PTR(ret);
4415 /* Select the device */
4416 chip->select_chip(mtd, 0);
4418 /* Send the command for reading device ID */
4419 ret = nand_readid_op(chip, 0, id_data, 2);
4421 return ERR_PTR(ret);
4423 /* Read manufacturer and device IDs */
4424 *maf_id = id_data[0];
4425 *dev_id = id_data[1];
4428 * Try again to make sure, as some systems the bus-hold or other
4429 * interface concerns can cause random data which looks like a
4430 * possibly credible NAND flash to appear. If the two results do
4431 * not match, ignore the device completely.
4434 /* Read entire ID string */
4435 ret = nand_readid_op(chip, 0, id_data, 8);
4437 return ERR_PTR(ret);
4439 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
4440 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
4441 *maf_id, *dev_id, id_data[0], id_data[1]);
4442 return ERR_PTR(-ENODEV);
4446 type = nand_flash_ids;
4448 for (; type->name != NULL; type++) {
4449 if (is_full_id_nand(type)) {
4450 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
4452 } else if (*dev_id == type->dev_id) {
4457 chip->onfi_version = 0;
4458 if (!type->name || !type->pagesize) {
4459 /* Check if the chip is ONFI compliant */
4460 if (nand_flash_detect_onfi(mtd, chip, &busw))
4463 /* Check if the chip is JEDEC compliant */
4464 if (nand_flash_detect_jedec(mtd, chip, &busw))
4469 return ERR_PTR(-ENODEV);
4472 mtd->name = type->name;
4474 chip->chipsize = (uint64_t)type->chipsize << 20;
4476 if (!type->pagesize) {
4477 /* Decode parameters from extended ID */
4478 nand_decode_ext_id(mtd, chip, id_data, &busw);
4480 nand_decode_id(mtd, chip, type, id_data, &busw);
4482 /* Get chip options */
4483 chip->options |= type->options;
4486 * Check if chip is not a Samsung device. Do not clear the
4487 * options for chips which do not have an extended id.
4489 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
4490 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
4493 /* Try to identify manufacturer */
4494 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
4495 if (nand_manuf_ids[maf_idx].id == *maf_id)
4499 if (chip->options & NAND_BUSWIDTH_AUTO) {
4500 WARN_ON(chip->options & NAND_BUSWIDTH_16);
4501 chip->options |= busw;
4502 nand_set_defaults(chip, busw);
4503 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4505 * Check, if buswidth is correct. Hardware drivers should set
4508 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4510 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
4511 pr_warn("bus width %d instead %d bit\n",
4512 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
4514 return ERR_PTR(-EINVAL);
4517 nand_decode_bbm_options(mtd, chip, id_data);
4519 /* Calculate the address shift from the page size */
4520 chip->page_shift = ffs(mtd->writesize) - 1;
4521 /* Convert chipsize to number of pages per chip -1 */
4522 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
4524 chip->bbt_erase_shift = chip->phys_erase_shift =
4525 ffs(mtd->erasesize) - 1;
4526 if (chip->chipsize & 0xffffffff)
4527 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
4529 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4530 chip->chip_shift += 32 - 1;
4533 if (chip->chip_shift - chip->page_shift > 16)
4534 chip->options |= NAND_ROW_ADDR_3;
4536 chip->badblockbits = 8;
4537 chip->erase = single_erase;
4539 /* Do not replace user supplied command function! */
4540 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4541 chip->cmdfunc = nand_command_lp;
4543 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4546 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
4547 if (chip->onfi_version)
4548 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4549 chip->onfi_params.model);
4550 else if (chip->jedec_version)
4551 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4552 chip->jedec_params.model);
4554 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4557 if (chip->jedec_version)
4558 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4559 chip->jedec_params.model);
4561 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4564 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4568 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
4569 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
4570 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
4573 EXPORT_SYMBOL(nand_get_flash_type);
4575 #if CONFIG_IS_ENABLED(OF_CONTROL)
4576 DECLARE_GLOBAL_DATA_PTR;
4578 static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
4580 int ret, ecc_mode = -1, ecc_strength, ecc_step;
4581 const void *blob = gd->fdt_blob;
4584 ret = fdtdec_get_int(blob, node, "nand-bus-width", -1);
4586 chip->options |= NAND_BUSWIDTH_16;
4588 if (fdtdec_get_bool(blob, node, "nand-on-flash-bbt"))
4589 chip->bbt_options |= NAND_BBT_USE_FLASH;
4591 str = fdt_getprop(blob, node, "nand-ecc-mode", NULL);
4593 if (!strcmp(str, "none"))
4594 ecc_mode = NAND_ECC_NONE;
4595 else if (!strcmp(str, "soft"))
4596 ecc_mode = NAND_ECC_SOFT;
4597 else if (!strcmp(str, "hw"))
4598 ecc_mode = NAND_ECC_HW;
4599 else if (!strcmp(str, "hw_syndrome"))
4600 ecc_mode = NAND_ECC_HW_SYNDROME;
4601 else if (!strcmp(str, "hw_oob_first"))
4602 ecc_mode = NAND_ECC_HW_OOB_FIRST;
4603 else if (!strcmp(str, "soft_bch"))
4604 ecc_mode = NAND_ECC_SOFT_BCH;
4608 ecc_strength = fdtdec_get_int(blob, node, "nand-ecc-strength", -1);
4609 ecc_step = fdtdec_get_int(blob, node, "nand-ecc-step-size", -1);
4611 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4612 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4613 pr_err("must set both strength and step size in DT\n");
4618 chip->ecc.mode = ecc_mode;
4620 if (ecc_strength >= 0)
4621 chip->ecc.strength = ecc_strength;
4624 chip->ecc.size = ecc_step;
4626 if (fdt_getprop(blob, node, "nand-ecc-maximize", NULL))
4627 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4632 static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
4636 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
4639 * nand_scan_ident - [NAND Interface] Scan for the NAND device
4640 * @mtd: MTD device structure
4641 * @maxchips: number of chips to scan for
4642 * @table: alternative NAND ID table
4644 * This is the first phase of the normal nand_scan() function. It reads the
4645 * flash ID and sets up MTD fields accordingly.
4648 int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4649 struct nand_flash_dev *table)
4651 int i, nand_maf_id, nand_dev_id;
4652 struct nand_chip *chip = mtd_to_nand(mtd);
4653 struct nand_flash_dev *type;
4656 if (chip->flash_node) {
4657 ret = nand_dt_init(mtd, chip, chip->flash_node);
4662 /* Set the default functions */
4663 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
4665 /* Read the flash type */
4666 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
4667 &nand_dev_id, table);
4670 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
4671 pr_warn("No NAND device found\n");
4672 chip->select_chip(mtd, -1);
4673 return PTR_ERR(type);
4676 /* Initialize the ->data_interface field. */
4677 ret = nand_init_data_interface(chip);
4682 * Setup the data interface correctly on the chip and controller side.
4683 * This explicit call to nand_setup_data_interface() is only required
4684 * for the first die, because nand_reset() has been called before
4685 * ->data_interface and ->default_onfi_timing_mode were set.
4686 * For the other dies, nand_reset() will automatically switch to the
4689 ret = nand_setup_data_interface(chip, 0);
4693 chip->select_chip(mtd, -1);
4695 /* Check for a chip array */
4696 for (i = 1; i < maxchips; i++) {
4699 /* See comment in nand_get_flash_type for reset */
4700 nand_reset(chip, i);
4702 chip->select_chip(mtd, i);
4703 /* Send the command for reading device ID */
4704 nand_readid_op(chip, 0, id, sizeof(id));
4706 /* Read manufacturer and device IDs */
4707 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
4708 chip->select_chip(mtd, -1);
4711 chip->select_chip(mtd, -1);
4716 pr_info("%d chips detected\n", i);
4719 /* Store the number of chips and calc total size for mtd */
4721 mtd->size = i * chip->chipsize;
4725 EXPORT_SYMBOL(nand_scan_ident);
4728 * nand_check_ecc_caps - check the sanity of preset ECC settings
4729 * @chip: nand chip info structure
4730 * @caps: ECC caps info structure
4731 * @oobavail: OOB size that the ECC engine can use
4733 * When ECC step size and strength are already set, check if they are supported
4734 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4735 * On success, the calculated ECC bytes is set.
4737 int nand_check_ecc_caps(struct nand_chip *chip,
4738 const struct nand_ecc_caps *caps, int oobavail)
4740 struct mtd_info *mtd = nand_to_mtd(chip);
4741 const struct nand_ecc_step_info *stepinfo;
4742 int preset_step = chip->ecc.size;
4743 int preset_strength = chip->ecc.strength;
4744 int nsteps, ecc_bytes;
4747 if (WARN_ON(oobavail < 0))
4750 if (!preset_step || !preset_strength)
4753 nsteps = mtd->writesize / preset_step;
4755 for (i = 0; i < caps->nstepinfos; i++) {
4756 stepinfo = &caps->stepinfos[i];
4758 if (stepinfo->stepsize != preset_step)
4761 for (j = 0; j < stepinfo->nstrengths; j++) {
4762 if (stepinfo->strengths[j] != preset_strength)
4765 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4767 if (WARN_ON_ONCE(ecc_bytes < 0))
4770 if (ecc_bytes * nsteps > oobavail) {
4771 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4772 preset_step, preset_strength);
4776 chip->ecc.bytes = ecc_bytes;
4782 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4783 preset_step, preset_strength);
4787 EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4790 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4791 * @chip: nand chip info structure
4792 * @caps: ECC engine caps info structure
4793 * @oobavail: OOB size that the ECC engine can use
4795 * If a chip's ECC requirement is provided, try to meet it with the least
4796 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4797 * On success, the chosen ECC settings are set.
4799 int nand_match_ecc_req(struct nand_chip *chip,
4800 const struct nand_ecc_caps *caps, int oobavail)
4802 struct mtd_info *mtd = nand_to_mtd(chip);
4803 const struct nand_ecc_step_info *stepinfo;
4804 int req_step = chip->ecc_step_ds;
4805 int req_strength = chip->ecc_strength_ds;
4806 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4807 int best_step, best_strength, best_ecc_bytes;
4808 int best_ecc_bytes_total = INT_MAX;
4811 if (WARN_ON(oobavail < 0))
4814 /* No information provided by the NAND chip */
4815 if (!req_step || !req_strength)
4818 /* number of correctable bits the chip requires in a page */
4819 req_corr = mtd->writesize / req_step * req_strength;
4821 for (i = 0; i < caps->nstepinfos; i++) {
4822 stepinfo = &caps->stepinfos[i];
4823 step_size = stepinfo->stepsize;
4825 for (j = 0; j < stepinfo->nstrengths; j++) {
4826 strength = stepinfo->strengths[j];
4829 * If both step size and strength are smaller than the
4830 * chip's requirement, it is not easy to compare the
4831 * resulted reliability.
4833 if (step_size < req_step && strength < req_strength)
4836 if (mtd->writesize % step_size)
4839 nsteps = mtd->writesize / step_size;
4841 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4842 if (WARN_ON_ONCE(ecc_bytes < 0))
4844 ecc_bytes_total = ecc_bytes * nsteps;
4846 if (ecc_bytes_total > oobavail ||
4847 strength * nsteps < req_corr)
4851 * We assume the best is to meet the chip's requrement
4852 * with the least number of ECC bytes.
4854 if (ecc_bytes_total < best_ecc_bytes_total) {
4855 best_ecc_bytes_total = ecc_bytes_total;
4856 best_step = step_size;
4857 best_strength = strength;
4858 best_ecc_bytes = ecc_bytes;
4863 if (best_ecc_bytes_total == INT_MAX)
4866 chip->ecc.size = best_step;
4867 chip->ecc.strength = best_strength;
4868 chip->ecc.bytes = best_ecc_bytes;
4872 EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4875 * nand_maximize_ecc - choose the max ECC strength available
4876 * @chip: nand chip info structure
4877 * @caps: ECC engine caps info structure
4878 * @oobavail: OOB size that the ECC engine can use
4880 * Choose the max ECC strength that is supported on the controller, and can fit
4881 * within the chip's OOB. On success, the chosen ECC settings are set.
4883 int nand_maximize_ecc(struct nand_chip *chip,
4884 const struct nand_ecc_caps *caps, int oobavail)
4886 struct mtd_info *mtd = nand_to_mtd(chip);
4887 const struct nand_ecc_step_info *stepinfo;
4888 int step_size, strength, nsteps, ecc_bytes, corr;
4891 int best_strength, best_ecc_bytes;
4894 if (WARN_ON(oobavail < 0))
4897 for (i = 0; i < caps->nstepinfos; i++) {
4898 stepinfo = &caps->stepinfos[i];
4899 step_size = stepinfo->stepsize;
4901 /* If chip->ecc.size is already set, respect it */
4902 if (chip->ecc.size && step_size != chip->ecc.size)
4905 for (j = 0; j < stepinfo->nstrengths; j++) {
4906 strength = stepinfo->strengths[j];
4908 if (mtd->writesize % step_size)
4911 nsteps = mtd->writesize / step_size;
4913 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4914 if (WARN_ON_ONCE(ecc_bytes < 0))
4917 if (ecc_bytes * nsteps > oobavail)
4920 corr = strength * nsteps;
4923 * If the number of correctable bits is the same,
4924 * bigger step_size has more reliability.
4926 if (corr > best_corr ||
4927 (corr == best_corr && step_size > best_step)) {
4929 best_step = step_size;
4930 best_strength = strength;
4931 best_ecc_bytes = ecc_bytes;
4939 chip->ecc.size = best_step;
4940 chip->ecc.strength = best_strength;
4941 chip->ecc.bytes = best_ecc_bytes;
4945 EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4948 * Check if the chip configuration meet the datasheet requirements.
4950 * If our configuration corrects A bits per B bytes and the minimum
4951 * required correction level is X bits per Y bytes, then we must ensure
4952 * both of the following are true:
4954 * (1) A / B >= X / Y
4957 * Requirement (1) ensures we can correct for the required bitflip density.
4958 * Requirement (2) ensures we can correct even when all bitflips are clumped
4959 * in the same sector.
4961 static bool nand_ecc_strength_good(struct mtd_info *mtd)
4963 struct nand_chip *chip = mtd_to_nand(mtd);
4964 struct nand_ecc_ctrl *ecc = &chip->ecc;
4967 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4968 /* Not enough information */
4972 * We get the number of corrected bits per page to compare
4973 * the correction density.
4975 corr = (mtd->writesize * ecc->strength) / ecc->size;
4976 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4978 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4981 static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4983 struct nand_ecc_ctrl *ecc = &chip->ecc;
4985 if (nand_standard_page_accessors(ecc))
4989 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4990 * controller driver implements all the page accessors because
4991 * default helpers are not suitable when the core does not
4992 * send the READ0/PAGEPROG commands.
4994 return (!ecc->read_page || !ecc->write_page ||
4995 !ecc->read_page_raw || !ecc->write_page_raw ||
4996 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4997 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4998 ecc->hwctl && ecc->calculate));
5002 * nand_scan_tail - [NAND Interface] Scan for the NAND device
5003 * @mtd: MTD device structure
5005 * This is the second phase of the normal nand_scan() function. It fills out
5006 * all the uninitialized function pointers with the defaults and scans for a
5007 * bad block table if appropriate.
5009 int nand_scan_tail(struct mtd_info *mtd)
5012 struct nand_chip *chip = mtd_to_nand(mtd);
5013 struct nand_ecc_ctrl *ecc = &chip->ecc;
5014 struct nand_buffers *nbuf;
5016 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
5017 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
5018 !(chip->bbt_options & NAND_BBT_USE_FLASH));
5020 if (invalid_ecc_page_accessors(chip)) {
5021 pr_err("Invalid ECC page accessors setup\n");
5025 if (!(chip->options & NAND_OWN_BUFFERS)) {
5026 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
5027 chip->buffers = nbuf;
5033 /* Set the internal oob buffer location, just after the page data */
5034 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
5037 * If no default placement scheme is given, select an appropriate one.
5039 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
5040 switch (mtd->oobsize) {
5041 #ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
5043 ecc->layout = &nand_oob_8;
5046 ecc->layout = &nand_oob_16;
5049 ecc->layout = &nand_oob_64;
5052 ecc->layout = &nand_oob_128;
5056 pr_warn("No oob scheme defined for oobsize %d\n",
5062 if (!chip->write_page)
5063 chip->write_page = nand_write_page;
5066 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
5067 * selected and we have 256 byte pagesize fallback to software ECC
5070 switch (ecc->mode) {
5071 case NAND_ECC_HW_OOB_FIRST:
5072 /* Similar to NAND_ECC_HW, but a separate read_page handle */
5073 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
5074 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
5077 if (!ecc->read_page)
5078 ecc->read_page = nand_read_page_hwecc_oob_first;
5081 /* Use standard hwecc read page function? */
5082 if (!ecc->read_page)
5083 ecc->read_page = nand_read_page_hwecc;
5084 if (!ecc->write_page)
5085 ecc->write_page = nand_write_page_hwecc;
5086 if (!ecc->read_page_raw)
5087 ecc->read_page_raw = nand_read_page_raw;
5088 if (!ecc->write_page_raw)
5089 ecc->write_page_raw = nand_write_page_raw;
5091 ecc->read_oob = nand_read_oob_std;
5092 if (!ecc->write_oob)
5093 ecc->write_oob = nand_write_oob_std;
5094 if (!ecc->read_subpage)
5095 ecc->read_subpage = nand_read_subpage;
5096 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
5097 ecc->write_subpage = nand_write_subpage_hwecc;
5099 case NAND_ECC_HW_SYNDROME:
5100 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5102 ecc->read_page == nand_read_page_hwecc ||
5104 ecc->write_page == nand_write_page_hwecc)) {
5105 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
5108 /* Use standard syndrome read/write page function? */
5109 if (!ecc->read_page)
5110 ecc->read_page = nand_read_page_syndrome;
5111 if (!ecc->write_page)
5112 ecc->write_page = nand_write_page_syndrome;
5113 if (!ecc->read_page_raw)
5114 ecc->read_page_raw = nand_read_page_raw_syndrome;
5115 if (!ecc->write_page_raw)
5116 ecc->write_page_raw = nand_write_page_raw_syndrome;
5118 ecc->read_oob = nand_read_oob_syndrome;
5119 if (!ecc->write_oob)
5120 ecc->write_oob = nand_write_oob_syndrome;
5122 if (mtd->writesize >= ecc->size) {
5123 if (!ecc->strength) {
5124 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
5129 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5130 ecc->size, mtd->writesize);
5131 ecc->mode = NAND_ECC_SOFT;
5134 ecc->calculate = nand_calculate_ecc;
5135 ecc->correct = nand_correct_data;
5136 ecc->read_page = nand_read_page_swecc;
5137 ecc->read_subpage = nand_read_subpage;
5138 ecc->write_page = nand_write_page_swecc;
5139 ecc->read_page_raw = nand_read_page_raw;
5140 ecc->write_page_raw = nand_write_page_raw;
5141 ecc->read_oob = nand_read_oob_std;
5142 ecc->write_oob = nand_write_oob_std;
5149 case NAND_ECC_SOFT_BCH:
5150 if (!mtd_nand_has_bch()) {
5151 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5154 ecc->calculate = nand_bch_calculate_ecc;
5155 ecc->correct = nand_bch_correct_data;
5156 ecc->read_page = nand_read_page_swecc;
5157 ecc->read_subpage = nand_read_subpage;
5158 ecc->write_page = nand_write_page_swecc;
5159 ecc->read_page_raw = nand_read_page_raw;
5160 ecc->write_page_raw = nand_write_page_raw;
5161 ecc->read_oob = nand_read_oob_std;
5162 ecc->write_oob = nand_write_oob_std;
5164 * Board driver should supply ecc.size and ecc.strength values
5165 * to select how many bits are correctable. Otherwise, default
5166 * to 4 bits for large page devices.
5168 if (!ecc->size && (mtd->oobsize >= 64)) {
5173 /* See nand_bch_init() for details. */
5175 ecc->priv = nand_bch_init(mtd);
5177 pr_warn("BCH ECC initialization failed!\n");
5183 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
5184 ecc->read_page = nand_read_page_raw;
5185 ecc->write_page = nand_write_page_raw;
5186 ecc->read_oob = nand_read_oob_std;
5187 ecc->read_page_raw = nand_read_page_raw;
5188 ecc->write_page_raw = nand_write_page_raw;
5189 ecc->write_oob = nand_write_oob_std;
5190 ecc->size = mtd->writesize;
5196 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
5200 /* For many systems, the standard OOB write also works for raw */
5201 if (!ecc->read_oob_raw)
5202 ecc->read_oob_raw = ecc->read_oob;
5203 if (!ecc->write_oob_raw)
5204 ecc->write_oob_raw = ecc->write_oob;
5207 * The number of bytes available for a client to place data into
5208 * the out of band area.
5212 for (i = 0; ecc->layout->oobfree[i].length; i++)
5213 mtd->oobavail += ecc->layout->oobfree[i].length;
5216 /* ECC sanity check: warn if it's too weak */
5217 if (!nand_ecc_strength_good(mtd))
5218 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5222 * Set the number of read / write steps for one page depending on ECC
5225 ecc->steps = mtd->writesize / ecc->size;
5226 if (ecc->steps * ecc->size != mtd->writesize) {
5227 pr_warn("Invalid ECC parameters\n");
5230 ecc->total = ecc->steps * ecc->bytes;
5232 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
5233 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
5234 switch (ecc->steps) {
5236 mtd->subpage_sft = 1;
5241 mtd->subpage_sft = 2;
5245 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
5247 /* Initialize state */
5248 chip->state = FL_READY;
5250 /* Invalidate the pagebuffer reference */
5253 /* Large page NAND with SOFT_ECC should support subpage reads */
5254 switch (ecc->mode) {
5256 case NAND_ECC_SOFT_BCH:
5257 if (chip->page_shift > 9)
5258 chip->options |= NAND_SUBPAGE_READ;
5265 /* Fill in remaining MTD driver data */
5266 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
5267 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5269 mtd->_erase = nand_erase;
5270 mtd->_panic_write = panic_nand_write;
5271 mtd->_read_oob = nand_read_oob;
5272 mtd->_write_oob = nand_write_oob;
5273 mtd->_sync = nand_sync;
5275 mtd->_unlock = NULL;
5276 mtd->_block_isreserved = nand_block_isreserved;
5277 mtd->_block_isbad = nand_block_isbad;
5278 mtd->_block_markbad = nand_block_markbad;
5279 mtd->writebufsize = mtd->writesize;
5281 /* propagate ecc info to mtd_info */
5282 mtd->ecclayout = ecc->layout;
5283 mtd->ecc_strength = ecc->strength;
5284 mtd->ecc_step_size = ecc->size;
5286 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5287 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5290 if (!mtd->bitflip_threshold)
5291 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
5295 EXPORT_SYMBOL(nand_scan_tail);
5298 * nand_scan - [NAND Interface] Scan for the NAND device
5299 * @mtd: MTD device structure
5300 * @maxchips: number of chips to scan for
5302 * This fills out all the uninitialized function pointers with the defaults.
5303 * The flash ID is read and the mtd/chip structures are filled with the
5304 * appropriate values.
5306 int nand_scan(struct mtd_info *mtd, int maxchips)
5310 ret = nand_scan_ident(mtd, maxchips, NULL);
5312 ret = nand_scan_tail(mtd);
5315 EXPORT_SYMBOL(nand_scan);
5317 MODULE_LICENSE("GPL");
5318 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5319 MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
5320 MODULE_DESCRIPTION("Generic NAND flash driver code");