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)
37 #include <linux/err.h>
38 #include <linux/compat.h>
39 #include <linux/mtd/mtd.h>
40 #include <linux/mtd/nand.h>
41 #include <linux/mtd/nand_ecc.h>
42 #include <linux/mtd/nand_bch.h>
43 #ifdef CONFIG_MTD_PARTITIONS
44 #include <linux/mtd/partitions.h>
47 #include <linux/errno.h>
49 /* Define default oob placement schemes for large and small page devices */
50 static struct nand_ecclayout nand_oob_8 = {
60 static struct nand_ecclayout nand_oob_16 = {
62 .eccpos = {0, 1, 2, 3, 6, 7},
68 static struct nand_ecclayout nand_oob_64 = {
71 40, 41, 42, 43, 44, 45, 46, 47,
72 48, 49, 50, 51, 52, 53, 54, 55,
73 56, 57, 58, 59, 60, 61, 62, 63},
79 static struct nand_ecclayout nand_oob_128 = {
82 80, 81, 82, 83, 84, 85, 86, 87,
83 88, 89, 90, 91, 92, 93, 94, 95,
84 96, 97, 98, 99, 100, 101, 102, 103,
85 104, 105, 106, 107, 108, 109, 110, 111,
86 112, 113, 114, 115, 116, 117, 118, 119,
87 120, 121, 122, 123, 124, 125, 126, 127},
93 static int nand_get_device(struct mtd_info *mtd, int new_state);
95 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
96 struct mtd_oob_ops *ops);
99 * For devices which display every fart in the system on a separate LED. Is
100 * compiled away when LED support is disabled.
102 DEFINE_LED_TRIGGER(nand_led_trigger);
104 static int check_offs_len(struct mtd_info *mtd,
105 loff_t ofs, uint64_t len)
107 struct nand_chip *chip = mtd_to_nand(mtd);
110 /* Start address must align on block boundary */
111 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
112 pr_debug("%s: unaligned address\n", __func__);
116 /* Length must align on block boundary */
117 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
118 pr_debug("%s: length not block aligned\n", __func__);
126 * nand_release_device - [GENERIC] release chip
127 * @mtd: MTD device structure
129 * Release chip lock and wake up anyone waiting on the device.
131 static void nand_release_device(struct mtd_info *mtd)
133 struct nand_chip *chip = mtd_to_nand(mtd);
135 /* De-select the NAND device */
136 chip->select_chip(mtd, -1);
140 * nand_read_byte - [DEFAULT] read one byte from the chip
141 * @mtd: MTD device structure
143 * Default read function for 8bit buswidth
145 uint8_t nand_read_byte(struct mtd_info *mtd)
147 struct nand_chip *chip = mtd_to_nand(mtd);
148 return readb(chip->IO_ADDR_R);
152 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
153 * @mtd: MTD device structure
155 * Default read function for 16bit buswidth with endianness conversion.
158 static uint8_t nand_read_byte16(struct mtd_info *mtd)
160 struct nand_chip *chip = mtd_to_nand(mtd);
161 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
165 * nand_read_word - [DEFAULT] read one word from the chip
166 * @mtd: MTD device structure
168 * Default read function for 16bit buswidth without endianness conversion.
170 static u16 nand_read_word(struct mtd_info *mtd)
172 struct nand_chip *chip = mtd_to_nand(mtd);
173 return readw(chip->IO_ADDR_R);
177 * nand_select_chip - [DEFAULT] control CE line
178 * @mtd: MTD device structure
179 * @chipnr: chipnumber to select, -1 for deselect
181 * Default select function for 1 chip devices.
183 static void nand_select_chip(struct mtd_info *mtd, int chipnr)
185 struct nand_chip *chip = mtd_to_nand(mtd);
189 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
200 * nand_write_byte - [DEFAULT] write single byte to chip
201 * @mtd: MTD device structure
202 * @byte: value to write
204 * Default function to write a byte to I/O[7:0]
206 static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
208 struct nand_chip *chip = mtd_to_nand(mtd);
210 chip->write_buf(mtd, &byte, 1);
214 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
215 * @mtd: MTD device structure
216 * @byte: value to write
218 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
220 static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
222 struct nand_chip *chip = mtd_to_nand(mtd);
223 uint16_t word = byte;
226 * It's not entirely clear what should happen to I/O[15:8] when writing
227 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
229 * When the host supports a 16-bit bus width, only data is
230 * transferred at the 16-bit width. All address and command line
231 * transfers shall use only the lower 8-bits of the data bus. During
232 * command transfers, the host may place any value on the upper
233 * 8-bits of the data bus. During address transfers, the host shall
234 * set the upper 8-bits of the data bus to 00h.
236 * One user of the write_byte callback is nand_onfi_set_features. The
237 * four parameters are specified to be written to I/O[7:0], but this is
238 * neither an address nor a command transfer. Let's assume a 0 on the
239 * upper I/O lines is OK.
241 chip->write_buf(mtd, (uint8_t *)&word, 2);
244 static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
248 for (i = 0; i < len; i++)
249 writeb(buf[i], addr);
251 static void ioread8_rep(void *addr, uint8_t *buf, int len)
255 for (i = 0; i < len; i++)
256 buf[i] = readb(addr);
259 static void ioread16_rep(void *addr, void *buf, int len)
262 u16 *p = (u16 *) buf;
264 for (i = 0; i < len; i++)
268 static void iowrite16_rep(void *addr, void *buf, int len)
271 u16 *p = (u16 *) buf;
273 for (i = 0; i < len; i++)
278 * nand_write_buf - [DEFAULT] write buffer to chip
279 * @mtd: MTD device structure
281 * @len: number of bytes to write
283 * Default write function for 8bit buswidth.
285 void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
287 struct nand_chip *chip = mtd_to_nand(mtd);
289 iowrite8_rep(chip->IO_ADDR_W, buf, len);
293 * nand_read_buf - [DEFAULT] read chip data into buffer
294 * @mtd: MTD device structure
295 * @buf: buffer to store date
296 * @len: number of bytes to read
298 * Default read function for 8bit buswidth.
300 void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
302 struct nand_chip *chip = mtd_to_nand(mtd);
304 ioread8_rep(chip->IO_ADDR_R, buf, len);
308 * nand_write_buf16 - [DEFAULT] write buffer to chip
309 * @mtd: MTD device structure
311 * @len: number of bytes to write
313 * Default write function for 16bit buswidth.
315 void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
317 struct nand_chip *chip = mtd_to_nand(mtd);
318 u16 *p = (u16 *) buf;
320 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
324 * nand_read_buf16 - [DEFAULT] read chip data into buffer
325 * @mtd: MTD device structure
326 * @buf: buffer to store date
327 * @len: number of bytes to read
329 * Default read function for 16bit buswidth.
331 void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
333 struct nand_chip *chip = mtd_to_nand(mtd);
334 u16 *p = (u16 *) buf;
336 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
340 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
341 * @mtd: MTD device structure
342 * @ofs: offset from device start
344 * Check, if the block is bad.
346 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
348 int page, res = 0, i = 0;
349 struct nand_chip *chip = mtd_to_nand(mtd);
352 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
353 ofs += mtd->erasesize - mtd->writesize;
355 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
358 if (chip->options & NAND_BUSWIDTH_16) {
359 chip->cmdfunc(mtd, NAND_CMD_READOOB,
360 chip->badblockpos & 0xFE, page);
361 bad = cpu_to_le16(chip->read_word(mtd));
362 if (chip->badblockpos & 0x1)
367 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
369 bad = chip->read_byte(mtd);
372 if (likely(chip->badblockbits == 8))
375 res = hweight8(bad) < chip->badblockbits;
376 ofs += mtd->writesize;
377 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
379 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
385 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
386 * @mtd: MTD device structure
387 * @ofs: offset from device start
389 * This is the default implementation, which can be overridden by a hardware
390 * specific driver. It provides the details for writing a bad block marker to a
393 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
395 struct nand_chip *chip = mtd_to_nand(mtd);
396 struct mtd_oob_ops ops;
397 uint8_t buf[2] = { 0, 0 };
398 int ret = 0, res, i = 0;
400 memset(&ops, 0, sizeof(ops));
402 ops.ooboffs = chip->badblockpos;
403 if (chip->options & NAND_BUSWIDTH_16) {
404 ops.ooboffs &= ~0x01;
405 ops.len = ops.ooblen = 2;
407 ops.len = ops.ooblen = 1;
409 ops.mode = MTD_OPS_PLACE_OOB;
411 /* Write to first/last page(s) if necessary */
412 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
413 ofs += mtd->erasesize - mtd->writesize;
415 res = nand_do_write_oob(mtd, ofs, &ops);
420 ofs += mtd->writesize;
421 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
427 * nand_block_markbad_lowlevel - mark a block bad
428 * @mtd: MTD device structure
429 * @ofs: offset from device start
431 * This function performs the generic NAND bad block marking steps (i.e., bad
432 * block table(s) and/or marker(s)). We only allow the hardware driver to
433 * specify how to write bad block markers to OOB (chip->block_markbad).
435 * We try operations in the following order:
436 * (1) erase the affected block, to allow OOB marker to be written cleanly
437 * (2) write bad block marker to OOB area of affected block (unless flag
438 * NAND_BBT_NO_OOB_BBM is present)
440 * Note that we retain the first error encountered in (2) or (3), finish the
441 * procedures, and dump the error in the end.
443 static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
445 struct nand_chip *chip = mtd_to_nand(mtd);
448 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
449 struct erase_info einfo;
451 /* Attempt erase before marking OOB */
452 memset(&einfo, 0, sizeof(einfo));
455 einfo.len = 1ULL << chip->phys_erase_shift;
456 nand_erase_nand(mtd, &einfo, 0);
458 /* Write bad block marker to OOB */
459 nand_get_device(mtd, FL_WRITING);
460 ret = chip->block_markbad(mtd, ofs);
461 nand_release_device(mtd);
464 /* Mark block bad in BBT */
466 res = nand_markbad_bbt(mtd, ofs);
472 mtd->ecc_stats.badblocks++;
478 * nand_check_wp - [GENERIC] check if the chip is write protected
479 * @mtd: MTD device structure
481 * Check, if the device is write protected. The function expects, that the
482 * device is already selected.
484 static int nand_check_wp(struct mtd_info *mtd)
486 struct nand_chip *chip = mtd_to_nand(mtd);
488 /* Broken xD cards report WP despite being writable */
489 if (chip->options & NAND_BROKEN_XD)
492 /* Check the WP bit */
493 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
494 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
498 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
499 * @mtd: MTD device structure
500 * @ofs: offset from device start
502 * Check if the block is marked as reserved.
504 static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
506 struct nand_chip *chip = mtd_to_nand(mtd);
510 /* Return info from the table */
511 return nand_isreserved_bbt(mtd, ofs);
515 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
516 * @mtd: MTD device structure
517 * @ofs: offset from device start
518 * @allowbbt: 1, if its allowed to access the bbt area
520 * Check, if the block is bad. Either by reading the bad block table or
521 * calling of the scan function.
523 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
525 struct nand_chip *chip = mtd_to_nand(mtd);
527 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
528 !(chip->options & NAND_BBT_SCANNED)) {
529 chip->options |= NAND_BBT_SCANNED;
534 return chip->block_bad(mtd, ofs);
536 /* Return info from the table */
537 return nand_isbad_bbt(mtd, ofs, allowbbt);
541 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
542 * @mtd: MTD device structure
544 * Wait for the ready pin after a command, and warn if a timeout occurs.
546 void nand_wait_ready(struct mtd_info *mtd)
548 struct nand_chip *chip = mtd_to_nand(mtd);
549 u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
552 time_start = get_timer(0);
553 /* Wait until command is processed or timeout occurs */
554 while (get_timer(time_start) < timeo) {
556 if (chip->dev_ready(mtd))
560 if (!chip->dev_ready(mtd))
561 pr_warn("timeout while waiting for chip to become ready\n");
563 EXPORT_SYMBOL_GPL(nand_wait_ready);
566 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
567 * @mtd: MTD device structure
568 * @timeo: Timeout in ms
570 * Wait for status ready (i.e. command done) or timeout.
572 static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
574 register struct nand_chip *chip = mtd_to_nand(mtd);
577 timeo = (CONFIG_SYS_HZ * timeo) / 1000;
578 time_start = get_timer(0);
579 while (get_timer(time_start) < timeo) {
580 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
587 * nand_command - [DEFAULT] Send command to NAND device
588 * @mtd: MTD device structure
589 * @command: the command to be sent
590 * @column: the column address for this command, -1 if none
591 * @page_addr: the page address for this command, -1 if none
593 * Send command to NAND device. This function is used for small page devices
594 * (512 Bytes per page).
596 static void nand_command(struct mtd_info *mtd, unsigned int command,
597 int column, int page_addr)
599 register struct nand_chip *chip = mtd_to_nand(mtd);
600 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
602 /* Write out the command to the device */
603 if (command == NAND_CMD_SEQIN) {
606 if (column >= mtd->writesize) {
608 column -= mtd->writesize;
609 readcmd = NAND_CMD_READOOB;
610 } else if (column < 256) {
611 /* First 256 bytes --> READ0 */
612 readcmd = NAND_CMD_READ0;
615 readcmd = NAND_CMD_READ1;
617 chip->cmd_ctrl(mtd, readcmd, ctrl);
618 ctrl &= ~NAND_CTRL_CHANGE;
620 chip->cmd_ctrl(mtd, command, ctrl);
622 /* Address cycle, when necessary */
623 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
624 /* Serially input address */
626 /* Adjust columns for 16 bit buswidth */
627 if (chip->options & NAND_BUSWIDTH_16 &&
628 !nand_opcode_8bits(command))
630 chip->cmd_ctrl(mtd, column, ctrl);
631 ctrl &= ~NAND_CTRL_CHANGE;
633 if (page_addr != -1) {
634 chip->cmd_ctrl(mtd, page_addr, ctrl);
635 ctrl &= ~NAND_CTRL_CHANGE;
636 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
637 /* One more address cycle for devices > 32MiB */
638 if (chip->chipsize > (32 << 20))
639 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
641 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
644 * Program and erase have their own busy handlers status and sequential
649 case NAND_CMD_PAGEPROG:
650 case NAND_CMD_ERASE1:
651 case NAND_CMD_ERASE2:
653 case NAND_CMD_STATUS:
659 udelay(chip->chip_delay);
660 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
661 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
663 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
664 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
665 nand_wait_status_ready(mtd, 250);
668 /* This applies to read commands */
671 * If we don't have access to the busy pin, we apply the given
674 if (!chip->dev_ready) {
675 udelay(chip->chip_delay);
680 * Apply this short delay always to ensure that we do wait tWB in
681 * any case on any machine.
685 nand_wait_ready(mtd);
689 * nand_command_lp - [DEFAULT] Send command to NAND large page device
690 * @mtd: MTD device structure
691 * @command: the command to be sent
692 * @column: the column address for this command, -1 if none
693 * @page_addr: the page address for this command, -1 if none
695 * Send command to NAND device. This is the version for the new large page
696 * devices. We don't have the separate regions as we have in the small page
697 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
699 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
700 int column, int page_addr)
702 register struct nand_chip *chip = mtd_to_nand(mtd);
704 /* Emulate NAND_CMD_READOOB */
705 if (command == NAND_CMD_READOOB) {
706 column += mtd->writesize;
707 command = NAND_CMD_READ0;
710 /* Command latch cycle */
711 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
713 if (column != -1 || page_addr != -1) {
714 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
716 /* Serially input address */
718 /* Adjust columns for 16 bit buswidth */
719 if (chip->options & NAND_BUSWIDTH_16 &&
720 !nand_opcode_8bits(command))
722 chip->cmd_ctrl(mtd, column, ctrl);
723 ctrl &= ~NAND_CTRL_CHANGE;
724 chip->cmd_ctrl(mtd, column >> 8, ctrl);
726 if (page_addr != -1) {
727 chip->cmd_ctrl(mtd, page_addr, ctrl);
728 chip->cmd_ctrl(mtd, page_addr >> 8,
729 NAND_NCE | NAND_ALE);
730 /* One more address cycle for devices > 128MiB */
731 if (chip->chipsize > (128 << 20))
732 chip->cmd_ctrl(mtd, page_addr >> 16,
733 NAND_NCE | NAND_ALE);
736 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
739 * Program and erase have their own busy handlers status, sequential
740 * in and status need no delay.
744 case NAND_CMD_CACHEDPROG:
745 case NAND_CMD_PAGEPROG:
746 case NAND_CMD_ERASE1:
747 case NAND_CMD_ERASE2:
750 case NAND_CMD_STATUS:
756 udelay(chip->chip_delay);
757 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
758 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
759 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
760 NAND_NCE | NAND_CTRL_CHANGE);
761 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
762 nand_wait_status_ready(mtd, 250);
765 case NAND_CMD_RNDOUT:
766 /* No ready / busy check necessary */
767 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
768 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
769 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
770 NAND_NCE | NAND_CTRL_CHANGE);
774 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
775 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
776 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
777 NAND_NCE | NAND_CTRL_CHANGE);
779 /* This applies to read commands */
782 * If we don't have access to the busy pin, we apply the given
785 if (!chip->dev_ready) {
786 udelay(chip->chip_delay);
792 * Apply this short delay always to ensure that we do wait tWB in
793 * any case on any machine.
797 nand_wait_ready(mtd);
801 * panic_nand_get_device - [GENERIC] Get chip for selected access
802 * @chip: the nand chip descriptor
803 * @mtd: MTD device structure
804 * @new_state: the state which is requested
806 * Used when in panic, no locks are taken.
808 static void panic_nand_get_device(struct nand_chip *chip,
809 struct mtd_info *mtd, int new_state)
811 /* Hardware controller shared among independent devices */
812 chip->controller->active = chip;
813 chip->state = new_state;
817 * nand_get_device - [GENERIC] Get chip for selected access
818 * @mtd: MTD device structure
819 * @new_state: the state which is requested
821 * Get the device and lock it for exclusive access
824 nand_get_device(struct mtd_info *mtd, int new_state)
826 struct nand_chip *chip = mtd_to_nand(mtd);
827 chip->state = new_state;
832 * panic_nand_wait - [GENERIC] wait until the command is done
833 * @mtd: MTD device structure
834 * @chip: NAND chip structure
837 * Wait for command done. This is a helper function for nand_wait used when
838 * we are in interrupt context. May happen when in panic and trying to write
839 * an oops through mtdoops.
841 static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
845 for (i = 0; i < timeo; i++) {
846 if (chip->dev_ready) {
847 if (chip->dev_ready(mtd))
850 if (chip->read_byte(mtd) & NAND_STATUS_READY)
858 * nand_wait - [DEFAULT] wait until the command is done
859 * @mtd: MTD device structure
860 * @chip: NAND chip structure
862 * Wait for command done. This applies to erase and program only.
864 static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
867 unsigned long timeo = 400;
869 led_trigger_event(nand_led_trigger, LED_FULL);
872 * Apply this short delay always to ensure that we do wait tWB in any
873 * case on any machine.
877 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
879 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
882 time_start = get_timer(0);
883 while (get_timer(time_start) < timer) {
884 if (chip->dev_ready) {
885 if (chip->dev_ready(mtd))
888 if (chip->read_byte(mtd) & NAND_STATUS_READY)
892 led_trigger_event(nand_led_trigger, LED_OFF);
894 status = (int)chip->read_byte(mtd);
895 /* This can happen if in case of timeout or buggy dev_ready */
896 WARN_ON(!(status & NAND_STATUS_READY));
900 #define BITS_PER_BYTE 8
903 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
904 * @buf: buffer to test
905 * @len: buffer length
906 * @bitflips_threshold: maximum number of bitflips
908 * Check if a buffer contains only 0xff, which means the underlying region
909 * has been erased and is ready to be programmed.
910 * The bitflips_threshold specify the maximum number of bitflips before
911 * considering the region is not erased.
912 * Note: The logic of this function has been extracted from the memweight
913 * implementation, except that nand_check_erased_buf function exit before
914 * testing the whole buffer if the number of bitflips exceed the
915 * bitflips_threshold value.
917 * Returns a positive number of bitflips less than or equal to
918 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
921 static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
923 const unsigned char *bitmap = buf;
927 for (; len && ((uintptr_t)bitmap) % sizeof(long);
929 weight = hweight8(*bitmap);
930 bitflips += BITS_PER_BYTE - weight;
931 if (unlikely(bitflips > bitflips_threshold))
935 for (; len >= 4; len -= 4, bitmap += 4) {
936 weight = hweight32(*((u32 *)bitmap));
937 bitflips += 32 - weight;
938 if (unlikely(bitflips > bitflips_threshold))
942 for (; len > 0; len--, bitmap++) {
943 weight = hweight8(*bitmap);
944 bitflips += BITS_PER_BYTE - weight;
945 if (unlikely(bitflips > bitflips_threshold))
953 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
955 * @data: data buffer to test
956 * @datalen: data length
958 * @ecclen: ECC length
959 * @extraoob: extra OOB buffer
960 * @extraooblen: extra OOB length
961 * @bitflips_threshold: maximum number of bitflips
963 * Check if a data buffer and its associated ECC and OOB data contains only
964 * 0xff pattern, which means the underlying region has been erased and is
965 * ready to be programmed.
966 * The bitflips_threshold specify the maximum number of bitflips before
967 * considering the region as not erased.
970 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
971 * different from the NAND page size. When fixing bitflips, ECC engines will
972 * report the number of errors per chunk, and the NAND core infrastructure
973 * expect you to return the maximum number of bitflips for the whole page.
974 * This is why you should always use this function on a single chunk and
975 * not on the whole page. After checking each chunk you should update your
976 * max_bitflips value accordingly.
977 * 2/ When checking for bitflips in erased pages you should not only check
978 * the payload data but also their associated ECC data, because a user might
979 * have programmed almost all bits to 1 but a few. In this case, we
980 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
982 * 3/ The extraoob argument is optional, and should be used if some of your OOB
983 * data are protected by the ECC engine.
984 * It could also be used if you support subpages and want to attach some
985 * extra OOB data to an ECC chunk.
987 * Returns a positive number of bitflips less than or equal to
988 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
989 * threshold. In case of success, the passed buffers are filled with 0xff.
991 int nand_check_erased_ecc_chunk(void *data, int datalen,
992 void *ecc, int ecclen,
993 void *extraoob, int extraooblen,
994 int bitflips_threshold)
996 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
998 data_bitflips = nand_check_erased_buf(data, datalen,
1000 if (data_bitflips < 0)
1001 return data_bitflips;
1003 bitflips_threshold -= data_bitflips;
1005 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1006 if (ecc_bitflips < 0)
1007 return ecc_bitflips;
1009 bitflips_threshold -= ecc_bitflips;
1011 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1012 bitflips_threshold);
1013 if (extraoob_bitflips < 0)
1014 return extraoob_bitflips;
1017 memset(data, 0xff, datalen);
1020 memset(ecc, 0xff, ecclen);
1022 if (extraoob_bitflips)
1023 memset(extraoob, 0xff, extraooblen);
1025 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1027 EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1030 * nand_read_page_raw - [INTERN] read raw page data without ecc
1031 * @mtd: mtd info structure
1032 * @chip: nand chip info structure
1033 * @buf: buffer to store read data
1034 * @oob_required: caller requires OOB data read to chip->oob_poi
1035 * @page: page number to read
1037 * Not for syndrome calculating ECC controllers, which use a special oob layout.
1039 static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1040 uint8_t *buf, int oob_required, int page)
1042 chip->read_buf(mtd, buf, mtd->writesize);
1044 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1049 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1050 * @mtd: mtd info structure
1051 * @chip: nand chip info structure
1052 * @buf: buffer to store read data
1053 * @oob_required: caller requires OOB data read to chip->oob_poi
1054 * @page: page number to read
1056 * We need a special oob layout and handling even when OOB isn't used.
1058 static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
1059 struct nand_chip *chip, uint8_t *buf,
1060 int oob_required, int page)
1062 int eccsize = chip->ecc.size;
1063 int eccbytes = chip->ecc.bytes;
1064 uint8_t *oob = chip->oob_poi;
1067 for (steps = chip->ecc.steps; steps > 0; steps--) {
1068 chip->read_buf(mtd, buf, eccsize);
1071 if (chip->ecc.prepad) {
1072 chip->read_buf(mtd, oob, chip->ecc.prepad);
1073 oob += chip->ecc.prepad;
1076 chip->read_buf(mtd, oob, eccbytes);
1079 if (chip->ecc.postpad) {
1080 chip->read_buf(mtd, oob, chip->ecc.postpad);
1081 oob += chip->ecc.postpad;
1085 size = mtd->oobsize - (oob - chip->oob_poi);
1087 chip->read_buf(mtd, oob, size);
1093 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1094 * @mtd: mtd info structure
1095 * @chip: nand chip info structure
1096 * @buf: buffer to store read data
1097 * @oob_required: caller requires OOB data read to chip->oob_poi
1098 * @page: page number to read
1100 static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1101 uint8_t *buf, int oob_required, int page)
1103 int i, eccsize = chip->ecc.size;
1104 int eccbytes = chip->ecc.bytes;
1105 int eccsteps = chip->ecc.steps;
1107 uint8_t *ecc_calc = chip->buffers->ecccalc;
1108 uint8_t *ecc_code = chip->buffers->ecccode;
1109 uint32_t *eccpos = chip->ecc.layout->eccpos;
1110 unsigned int max_bitflips = 0;
1112 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
1114 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1115 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1117 for (i = 0; i < chip->ecc.total; i++)
1118 ecc_code[i] = chip->oob_poi[eccpos[i]];
1120 eccsteps = chip->ecc.steps;
1123 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1126 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1128 mtd->ecc_stats.failed++;
1130 mtd->ecc_stats.corrected += stat;
1131 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1134 return max_bitflips;
1138 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
1139 * @mtd: mtd info structure
1140 * @chip: nand chip info structure
1141 * @data_offs: offset of requested data within the page
1142 * @readlen: data length
1143 * @bufpoi: buffer to store read data
1144 * @page: page number to read
1146 static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1147 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1150 int start_step, end_step, num_steps;
1151 uint32_t *eccpos = chip->ecc.layout->eccpos;
1153 int data_col_addr, i, gaps = 0;
1154 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1155 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
1157 unsigned int max_bitflips = 0;
1159 /* Column address within the page aligned to ECC size (256bytes) */
1160 start_step = data_offs / chip->ecc.size;
1161 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1162 num_steps = end_step - start_step + 1;
1163 index = start_step * chip->ecc.bytes;
1165 /* Data size aligned to ECC ecc.size */
1166 datafrag_len = num_steps * chip->ecc.size;
1167 eccfrag_len = num_steps * chip->ecc.bytes;
1169 data_col_addr = start_step * chip->ecc.size;
1170 /* If we read not a page aligned data */
1171 if (data_col_addr != 0)
1172 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1174 p = bufpoi + data_col_addr;
1175 chip->read_buf(mtd, p, datafrag_len);
1178 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1179 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1182 * The performance is faster if we position offsets according to
1183 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1185 for (i = 0; i < eccfrag_len - 1; i++) {
1186 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
1192 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1193 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1196 * Send the command to read the particular ECC bytes take care
1197 * about buswidth alignment in read_buf.
1199 aligned_pos = eccpos[index] & ~(busw - 1);
1200 aligned_len = eccfrag_len;
1201 if (eccpos[index] & (busw - 1))
1203 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
1206 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1207 mtd->writesize + aligned_pos, -1);
1208 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1211 for (i = 0; i < eccfrag_len; i++)
1212 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
1214 p = bufpoi + data_col_addr;
1215 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1218 stat = chip->ecc.correct(mtd, p,
1219 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1220 if (stat == -EBADMSG &&
1221 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1222 /* check for empty pages with bitflips */
1223 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1224 &chip->buffers->ecccode[i],
1227 chip->ecc.strength);
1231 mtd->ecc_stats.failed++;
1233 mtd->ecc_stats.corrected += stat;
1234 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1237 return max_bitflips;
1241 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1242 * @mtd: mtd info structure
1243 * @chip: nand chip info structure
1244 * @buf: buffer to store read data
1245 * @oob_required: caller requires OOB data read to chip->oob_poi
1246 * @page: page number to read
1248 * Not for syndrome calculating ECC controllers which need a special oob layout.
1250 static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1251 uint8_t *buf, int oob_required, int page)
1253 int i, eccsize = chip->ecc.size;
1254 int eccbytes = chip->ecc.bytes;
1255 int eccsteps = chip->ecc.steps;
1257 uint8_t *ecc_calc = chip->buffers->ecccalc;
1258 uint8_t *ecc_code = chip->buffers->ecccode;
1259 uint32_t *eccpos = chip->ecc.layout->eccpos;
1260 unsigned int max_bitflips = 0;
1262 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1263 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1264 chip->read_buf(mtd, p, eccsize);
1265 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1267 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1269 for (i = 0; i < chip->ecc.total; i++)
1270 ecc_code[i] = chip->oob_poi[eccpos[i]];
1272 eccsteps = chip->ecc.steps;
1275 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1278 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1279 if (stat == -EBADMSG &&
1280 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1281 /* check for empty pages with bitflips */
1282 stat = nand_check_erased_ecc_chunk(p, eccsize,
1283 &ecc_code[i], eccbytes,
1285 chip->ecc.strength);
1289 mtd->ecc_stats.failed++;
1291 mtd->ecc_stats.corrected += stat;
1292 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1295 return max_bitflips;
1299 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1300 * @mtd: mtd info structure
1301 * @chip: nand chip info structure
1302 * @buf: buffer to store read data
1303 * @oob_required: caller requires OOB data read to chip->oob_poi
1304 * @page: page number to read
1306 * Hardware ECC for large page chips, require OOB to be read first. For this
1307 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1308 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1309 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1310 * the data area, by overwriting the NAND manufacturer bad block markings.
1312 static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1313 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
1315 int i, eccsize = chip->ecc.size;
1316 int eccbytes = chip->ecc.bytes;
1317 int eccsteps = chip->ecc.steps;
1319 uint8_t *ecc_code = chip->buffers->ecccode;
1320 uint32_t *eccpos = chip->ecc.layout->eccpos;
1321 uint8_t *ecc_calc = chip->buffers->ecccalc;
1322 unsigned int max_bitflips = 0;
1324 /* Read the OOB area first */
1325 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1326 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1327 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1329 for (i = 0; i < chip->ecc.total; i++)
1330 ecc_code[i] = chip->oob_poi[eccpos[i]];
1332 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1335 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1336 chip->read_buf(mtd, p, eccsize);
1337 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1339 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1340 if (stat == -EBADMSG &&
1341 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1342 /* check for empty pages with bitflips */
1343 stat = nand_check_erased_ecc_chunk(p, eccsize,
1344 &ecc_code[i], eccbytes,
1346 chip->ecc.strength);
1350 mtd->ecc_stats.failed++;
1352 mtd->ecc_stats.corrected += stat;
1353 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1356 return max_bitflips;
1360 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1361 * @mtd: mtd info structure
1362 * @chip: nand chip info structure
1363 * @buf: buffer to store read data
1364 * @oob_required: caller requires OOB data read to chip->oob_poi
1365 * @page: page number to read
1367 * The hw generator calculates the error syndrome automatically. Therefore we
1368 * need a special oob layout and handling.
1370 static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1371 uint8_t *buf, int oob_required, int page)
1373 int i, eccsize = chip->ecc.size;
1374 int eccbytes = chip->ecc.bytes;
1375 int eccsteps = chip->ecc.steps;
1376 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
1378 uint8_t *oob = chip->oob_poi;
1379 unsigned int max_bitflips = 0;
1381 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1384 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1385 chip->read_buf(mtd, p, eccsize);
1387 if (chip->ecc.prepad) {
1388 chip->read_buf(mtd, oob, chip->ecc.prepad);
1389 oob += chip->ecc.prepad;
1392 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1393 chip->read_buf(mtd, oob, eccbytes);
1394 stat = chip->ecc.correct(mtd, p, oob, NULL);
1398 if (chip->ecc.postpad) {
1399 chip->read_buf(mtd, oob, chip->ecc.postpad);
1400 oob += chip->ecc.postpad;
1403 if (stat == -EBADMSG &&
1404 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1405 /* check for empty pages with bitflips */
1406 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1410 chip->ecc.strength);
1414 mtd->ecc_stats.failed++;
1416 mtd->ecc_stats.corrected += stat;
1417 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1421 /* Calculate remaining oob bytes */
1422 i = mtd->oobsize - (oob - chip->oob_poi);
1424 chip->read_buf(mtd, oob, i);
1426 return max_bitflips;
1430 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1431 * @chip: nand chip structure
1432 * @oob: oob destination address
1433 * @ops: oob ops structure
1434 * @len: size of oob to transfer
1436 static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1437 struct mtd_oob_ops *ops, size_t len)
1439 switch (ops->mode) {
1441 case MTD_OPS_PLACE_OOB:
1443 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1446 case MTD_OPS_AUTO_OOB: {
1447 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1448 uint32_t boffs = 0, roffs = ops->ooboffs;
1451 for (; free->length && len; free++, len -= bytes) {
1452 /* Read request not from offset 0? */
1453 if (unlikely(roffs)) {
1454 if (roffs >= free->length) {
1455 roffs -= free->length;
1458 boffs = free->offset + roffs;
1459 bytes = min_t(size_t, len,
1460 (free->length - roffs));
1463 bytes = min_t(size_t, len, free->length);
1464 boffs = free->offset;
1466 memcpy(oob, chip->oob_poi + boffs, bytes);
1478 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1479 * @mtd: MTD device structure
1480 * @retry_mode: the retry mode to use
1482 * Some vendors supply a special command to shift the Vt threshold, to be used
1483 * when there are too many bitflips in a page (i.e., ECC error). After setting
1484 * a new threshold, the host should retry reading the page.
1486 static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1488 struct nand_chip *chip = mtd_to_nand(mtd);
1490 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1492 if (retry_mode >= chip->read_retries)
1495 if (!chip->setup_read_retry)
1498 return chip->setup_read_retry(mtd, retry_mode);
1502 * nand_do_read_ops - [INTERN] Read data with ECC
1503 * @mtd: MTD device structure
1504 * @from: offset to read from
1505 * @ops: oob ops structure
1507 * Internal function. Called with chip held.
1509 static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1510 struct mtd_oob_ops *ops)
1512 int chipnr, page, realpage, col, bytes, aligned, oob_required;
1513 struct nand_chip *chip = mtd_to_nand(mtd);
1515 uint32_t readlen = ops->len;
1516 uint32_t oobreadlen = ops->ooblen;
1517 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
1519 uint8_t *bufpoi, *oob, *buf;
1521 unsigned int max_bitflips = 0;
1523 bool ecc_fail = false;
1525 chipnr = (int)(from >> chip->chip_shift);
1526 chip->select_chip(mtd, chipnr);
1528 realpage = (int)(from >> chip->page_shift);
1529 page = realpage & chip->pagemask;
1531 col = (int)(from & (mtd->writesize - 1));
1535 oob_required = oob ? 1 : 0;
1538 unsigned int ecc_failures = mtd->ecc_stats.failed;
1541 bytes = min(mtd->writesize - col, readlen);
1542 aligned = (bytes == mtd->writesize);
1549 /* Is the current page in the buffer? */
1550 if (realpage != chip->pagebuf || oob) {
1551 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1553 if (use_bufpoi && aligned)
1554 pr_debug("%s: using read bounce buffer for buf@%p\n",
1558 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1561 * Now read the page into the buffer. Absent an error,
1562 * the read methods return max bitflips per ecc step.
1564 if (unlikely(ops->mode == MTD_OPS_RAW))
1565 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1568 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1570 ret = chip->ecc.read_subpage(mtd, chip,
1574 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1575 oob_required, page);
1578 /* Invalidate page cache */
1583 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1585 /* Transfer not aligned data */
1587 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
1588 !(mtd->ecc_stats.failed - ecc_failures) &&
1589 (ops->mode != MTD_OPS_RAW)) {
1590 chip->pagebuf = realpage;
1591 chip->pagebuf_bitflips = ret;
1593 /* Invalidate page cache */
1596 memcpy(buf, chip->buffers->databuf + col, bytes);
1599 if (unlikely(oob)) {
1600 int toread = min(oobreadlen, max_oobsize);
1603 oob = nand_transfer_oob(chip,
1605 oobreadlen -= toread;
1609 if (chip->options & NAND_NEED_READRDY) {
1610 /* Apply delay or wait for ready/busy pin */
1611 if (!chip->dev_ready)
1612 udelay(chip->chip_delay);
1614 nand_wait_ready(mtd);
1617 if (mtd->ecc_stats.failed - ecc_failures) {
1618 if (retry_mode + 1 < chip->read_retries) {
1620 ret = nand_setup_read_retry(mtd,
1625 /* Reset failures; retry */
1626 mtd->ecc_stats.failed = ecc_failures;
1629 /* No more retry modes; real failure */
1636 memcpy(buf, chip->buffers->databuf + col, bytes);
1638 max_bitflips = max_t(unsigned int, max_bitflips,
1639 chip->pagebuf_bitflips);
1644 /* Reset to retry mode 0 */
1646 ret = nand_setup_read_retry(mtd, 0);
1655 /* For subsequent reads align to page boundary */
1657 /* Increment page address */
1660 page = realpage & chip->pagemask;
1661 /* Check, if we cross a chip boundary */
1664 chip->select_chip(mtd, -1);
1665 chip->select_chip(mtd, chipnr);
1668 chip->select_chip(mtd, -1);
1670 ops->retlen = ops->len - (size_t) readlen;
1672 ops->oobretlen = ops->ooblen - oobreadlen;
1680 return max_bitflips;
1684 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
1685 * @mtd: MTD device structure
1686 * @from: offset to read from
1687 * @len: number of bytes to read
1688 * @retlen: pointer to variable to store the number of read bytes
1689 * @buf: the databuffer to put data
1691 * Get hold of the chip and call nand_do_read.
1693 static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1694 size_t *retlen, uint8_t *buf)
1696 struct mtd_oob_ops ops;
1699 nand_get_device(mtd, FL_READING);
1700 memset(&ops, 0, sizeof(ops));
1703 ops.mode = MTD_OPS_PLACE_OOB;
1704 ret = nand_do_read_ops(mtd, from, &ops);
1705 *retlen = ops.retlen;
1706 nand_release_device(mtd);
1711 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1712 * @mtd: mtd info structure
1713 * @chip: nand chip info structure
1714 * @page: page number to read
1716 static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1719 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1720 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1725 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
1727 * @mtd: mtd info structure
1728 * @chip: nand chip info structure
1729 * @page: page number to read
1731 static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1734 int length = mtd->oobsize;
1735 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1736 int eccsize = chip->ecc.size;
1737 uint8_t *bufpoi = chip->oob_poi;
1738 int i, toread, sndrnd = 0, pos;
1740 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1741 for (i = 0; i < chip->ecc.steps; i++) {
1743 pos = eccsize + i * (eccsize + chunk);
1744 if (mtd->writesize > 512)
1745 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1747 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1750 toread = min_t(int, length, chunk);
1751 chip->read_buf(mtd, bufpoi, toread);
1756 chip->read_buf(mtd, bufpoi, length);
1762 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1763 * @mtd: mtd info structure
1764 * @chip: nand chip info structure
1765 * @page: page number to write
1767 static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1771 const uint8_t *buf = chip->oob_poi;
1772 int length = mtd->oobsize;
1774 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1775 chip->write_buf(mtd, buf, length);
1776 /* Send command to program the OOB data */
1777 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1779 status = chip->waitfunc(mtd, chip);
1781 return status & NAND_STATUS_FAIL ? -EIO : 0;
1785 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1786 * with syndrome - only for large page flash
1787 * @mtd: mtd info structure
1788 * @chip: nand chip info structure
1789 * @page: page number to write
1791 static int nand_write_oob_syndrome(struct mtd_info *mtd,
1792 struct nand_chip *chip, int page)
1794 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1795 int eccsize = chip->ecc.size, length = mtd->oobsize;
1796 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1797 const uint8_t *bufpoi = chip->oob_poi;
1800 * data-ecc-data-ecc ... ecc-oob
1802 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1804 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1805 pos = steps * (eccsize + chunk);
1810 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1811 for (i = 0; i < steps; i++) {
1813 if (mtd->writesize <= 512) {
1814 uint32_t fill = 0xFFFFFFFF;
1818 int num = min_t(int, len, 4);
1819 chip->write_buf(mtd, (uint8_t *)&fill,
1824 pos = eccsize + i * (eccsize + chunk);
1825 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1829 len = min_t(int, length, chunk);
1830 chip->write_buf(mtd, bufpoi, len);
1835 chip->write_buf(mtd, bufpoi, length);
1837 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1838 status = chip->waitfunc(mtd, chip);
1840 return status & NAND_STATUS_FAIL ? -EIO : 0;
1844 * nand_do_read_oob - [INTERN] NAND read out-of-band
1845 * @mtd: MTD device structure
1846 * @from: offset to read from
1847 * @ops: oob operations description structure
1849 * NAND read out-of-band data from the spare area.
1851 static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1852 struct mtd_oob_ops *ops)
1854 int page, realpage, chipnr;
1855 struct nand_chip *chip = mtd_to_nand(mtd);
1856 struct mtd_ecc_stats stats;
1857 int readlen = ops->ooblen;
1859 uint8_t *buf = ops->oobbuf;
1862 pr_debug("%s: from = 0x%08Lx, len = %i\n",
1863 __func__, (unsigned long long)from, readlen);
1865 stats = mtd->ecc_stats;
1867 len = mtd_oobavail(mtd, ops);
1869 if (unlikely(ops->ooboffs >= len)) {
1870 pr_debug("%s: attempt to start read outside oob\n",
1875 /* Do not allow reads past end of device */
1876 if (unlikely(from >= mtd->size ||
1877 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1878 (from >> chip->page_shift)) * len)) {
1879 pr_debug("%s: attempt to read beyond end of device\n",
1884 chipnr = (int)(from >> chip->chip_shift);
1885 chip->select_chip(mtd, chipnr);
1887 /* Shift to get page */
1888 realpage = (int)(from >> chip->page_shift);
1889 page = realpage & chip->pagemask;
1894 if (ops->mode == MTD_OPS_RAW)
1895 ret = chip->ecc.read_oob_raw(mtd, chip, page);
1897 ret = chip->ecc.read_oob(mtd, chip, page);
1902 len = min(len, readlen);
1903 buf = nand_transfer_oob(chip, buf, ops, len);
1905 if (chip->options & NAND_NEED_READRDY) {
1906 /* Apply delay or wait for ready/busy pin */
1907 if (!chip->dev_ready)
1908 udelay(chip->chip_delay);
1910 nand_wait_ready(mtd);
1917 /* Increment page address */
1920 page = realpage & chip->pagemask;
1921 /* Check, if we cross a chip boundary */
1924 chip->select_chip(mtd, -1);
1925 chip->select_chip(mtd, chipnr);
1928 chip->select_chip(mtd, -1);
1930 ops->oobretlen = ops->ooblen - readlen;
1935 if (mtd->ecc_stats.failed - stats.failed)
1938 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1942 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
1943 * @mtd: MTD device structure
1944 * @from: offset to read from
1945 * @ops: oob operation description structure
1947 * NAND read data and/or out-of-band data.
1949 static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1950 struct mtd_oob_ops *ops)
1952 int ret = -ENOTSUPP;
1956 /* Do not allow reads past end of device */
1957 if (ops->datbuf && (from + ops->len) > mtd->size) {
1958 pr_debug("%s: attempt to read beyond end of device\n",
1963 nand_get_device(mtd, FL_READING);
1965 switch (ops->mode) {
1966 case MTD_OPS_PLACE_OOB:
1967 case MTD_OPS_AUTO_OOB:
1976 ret = nand_do_read_oob(mtd, from, ops);
1978 ret = nand_do_read_ops(mtd, from, ops);
1981 nand_release_device(mtd);
1987 * nand_write_page_raw - [INTERN] raw page write function
1988 * @mtd: mtd info structure
1989 * @chip: nand chip info structure
1991 * @oob_required: must write chip->oob_poi to OOB
1992 * @page: page number to write
1994 * Not for syndrome calculating ECC controllers, which use a special oob layout.
1996 static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1997 const uint8_t *buf, int oob_required, int page)
1999 chip->write_buf(mtd, buf, mtd->writesize);
2001 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2007 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2008 * @mtd: mtd info structure
2009 * @chip: nand chip info structure
2011 * @oob_required: must write chip->oob_poi to OOB
2012 * @page: page number to write
2014 * We need a special oob layout and handling even when ECC isn't checked.
2016 static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
2017 struct nand_chip *chip,
2018 const uint8_t *buf, int oob_required,
2021 int eccsize = chip->ecc.size;
2022 int eccbytes = chip->ecc.bytes;
2023 uint8_t *oob = chip->oob_poi;
2026 for (steps = chip->ecc.steps; steps > 0; steps--) {
2027 chip->write_buf(mtd, buf, eccsize);
2030 if (chip->ecc.prepad) {
2031 chip->write_buf(mtd, oob, chip->ecc.prepad);
2032 oob += chip->ecc.prepad;
2035 chip->write_buf(mtd, oob, eccbytes);
2038 if (chip->ecc.postpad) {
2039 chip->write_buf(mtd, oob, chip->ecc.postpad);
2040 oob += chip->ecc.postpad;
2044 size = mtd->oobsize - (oob - chip->oob_poi);
2046 chip->write_buf(mtd, oob, size);
2051 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2052 * @mtd: mtd info structure
2053 * @chip: nand chip info structure
2055 * @oob_required: must write chip->oob_poi to OOB
2056 * @page: page number to write
2058 static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
2059 const uint8_t *buf, int oob_required,
2062 int i, eccsize = chip->ecc.size;
2063 int eccbytes = chip->ecc.bytes;
2064 int eccsteps = chip->ecc.steps;
2065 uint8_t *ecc_calc = chip->buffers->ecccalc;
2066 const uint8_t *p = buf;
2067 uint32_t *eccpos = chip->ecc.layout->eccpos;
2069 /* Software ECC calculation */
2070 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2071 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2073 for (i = 0; i < chip->ecc.total; i++)
2074 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2076 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
2080 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2081 * @mtd: mtd info structure
2082 * @chip: nand chip info structure
2084 * @oob_required: must write chip->oob_poi to OOB
2085 * @page: page number to write
2087 static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
2088 const uint8_t *buf, int oob_required,
2091 int i, eccsize = chip->ecc.size;
2092 int eccbytes = chip->ecc.bytes;
2093 int eccsteps = chip->ecc.steps;
2094 uint8_t *ecc_calc = chip->buffers->ecccalc;
2095 const uint8_t *p = buf;
2096 uint32_t *eccpos = chip->ecc.layout->eccpos;
2098 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2099 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2100 chip->write_buf(mtd, p, eccsize);
2101 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2104 for (i = 0; i < chip->ecc.total; i++)
2105 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2107 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2114 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
2115 * @mtd: mtd info structure
2116 * @chip: nand chip info structure
2117 * @offset: column address of subpage within the page
2118 * @data_len: data length
2120 * @oob_required: must write chip->oob_poi to OOB
2121 * @page: page number to write
2123 static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2124 struct nand_chip *chip, uint32_t offset,
2125 uint32_t data_len, const uint8_t *buf,
2126 int oob_required, int page)
2128 uint8_t *oob_buf = chip->oob_poi;
2129 uint8_t *ecc_calc = chip->buffers->ecccalc;
2130 int ecc_size = chip->ecc.size;
2131 int ecc_bytes = chip->ecc.bytes;
2132 int ecc_steps = chip->ecc.steps;
2133 uint32_t *eccpos = chip->ecc.layout->eccpos;
2134 uint32_t start_step = offset / ecc_size;
2135 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2136 int oob_bytes = mtd->oobsize / ecc_steps;
2139 for (step = 0; step < ecc_steps; step++) {
2140 /* configure controller for WRITE access */
2141 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2143 /* write data (untouched subpages already masked by 0xFF) */
2144 chip->write_buf(mtd, buf, ecc_size);
2146 /* mask ECC of un-touched subpages by padding 0xFF */
2147 if ((step < start_step) || (step > end_step))
2148 memset(ecc_calc, 0xff, ecc_bytes);
2150 chip->ecc.calculate(mtd, buf, ecc_calc);
2152 /* mask OOB of un-touched subpages by padding 0xFF */
2153 /* if oob_required, preserve OOB metadata of written subpage */
2154 if (!oob_required || (step < start_step) || (step > end_step))
2155 memset(oob_buf, 0xff, oob_bytes);
2158 ecc_calc += ecc_bytes;
2159 oob_buf += oob_bytes;
2162 /* copy calculated ECC for whole page to chip->buffer->oob */
2163 /* this include masked-value(0xFF) for unwritten subpages */
2164 ecc_calc = chip->buffers->ecccalc;
2165 for (i = 0; i < chip->ecc.total; i++)
2166 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2168 /* write OOB buffer to NAND device */
2169 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2176 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2177 * @mtd: mtd info structure
2178 * @chip: nand chip info structure
2180 * @oob_required: must write chip->oob_poi to OOB
2181 * @page: page number to write
2183 * The hw generator calculates the error syndrome automatically. Therefore we
2184 * need a special oob layout and handling.
2186 static int nand_write_page_syndrome(struct mtd_info *mtd,
2187 struct nand_chip *chip,
2188 const uint8_t *buf, int oob_required,
2191 int i, eccsize = chip->ecc.size;
2192 int eccbytes = chip->ecc.bytes;
2193 int eccsteps = chip->ecc.steps;
2194 const uint8_t *p = buf;
2195 uint8_t *oob = chip->oob_poi;
2197 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2199 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2200 chip->write_buf(mtd, p, eccsize);
2202 if (chip->ecc.prepad) {
2203 chip->write_buf(mtd, oob, chip->ecc.prepad);
2204 oob += chip->ecc.prepad;
2207 chip->ecc.calculate(mtd, p, oob);
2208 chip->write_buf(mtd, oob, eccbytes);
2211 if (chip->ecc.postpad) {
2212 chip->write_buf(mtd, oob, chip->ecc.postpad);
2213 oob += chip->ecc.postpad;
2217 /* Calculate remaining oob bytes */
2218 i = mtd->oobsize - (oob - chip->oob_poi);
2220 chip->write_buf(mtd, oob, i);
2226 * nand_write_page - [REPLACEABLE] write one page
2227 * @mtd: MTD device structure
2228 * @chip: NAND chip descriptor
2229 * @offset: address offset within the page
2230 * @data_len: length of actual data to be written
2231 * @buf: the data to write
2232 * @oob_required: must write chip->oob_poi to OOB
2233 * @page: page number to write
2234 * @cached: cached programming
2235 * @raw: use _raw version of write_page
2237 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
2238 uint32_t offset, int data_len, const uint8_t *buf,
2239 int oob_required, int page, int cached, int raw)
2241 int status, subpage;
2243 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2244 chip->ecc.write_subpage)
2245 subpage = offset || (data_len < mtd->writesize);
2249 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2252 status = chip->ecc.write_page_raw(mtd, chip, buf,
2253 oob_required, page);
2255 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2256 buf, oob_required, page);
2258 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2265 * Cached progamming disabled for now. Not sure if it's worth the
2266 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
2270 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
2272 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2273 status = chip->waitfunc(mtd, chip);
2275 * See if operation failed and additional status checks are
2278 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2279 status = chip->errstat(mtd, chip, FL_WRITING, status,
2282 if (status & NAND_STATUS_FAIL)
2285 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
2286 status = chip->waitfunc(mtd, chip);
2293 * nand_fill_oob - [INTERN] Transfer client buffer to oob
2294 * @mtd: MTD device structure
2295 * @oob: oob data buffer
2296 * @len: oob data write length
2297 * @ops: oob ops structure
2299 static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2300 struct mtd_oob_ops *ops)
2302 struct nand_chip *chip = mtd_to_nand(mtd);
2305 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2306 * data from a previous OOB read.
2308 memset(chip->oob_poi, 0xff, mtd->oobsize);
2310 switch (ops->mode) {
2312 case MTD_OPS_PLACE_OOB:
2314 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2317 case MTD_OPS_AUTO_OOB: {
2318 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2319 uint32_t boffs = 0, woffs = ops->ooboffs;
2322 for (; free->length && len; free++, len -= bytes) {
2323 /* Write request not from offset 0? */
2324 if (unlikely(woffs)) {
2325 if (woffs >= free->length) {
2326 woffs -= free->length;
2329 boffs = free->offset + woffs;
2330 bytes = min_t(size_t, len,
2331 (free->length - woffs));
2334 bytes = min_t(size_t, len, free->length);
2335 boffs = free->offset;
2337 memcpy(chip->oob_poi + boffs, oob, bytes);
2348 #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
2351 * nand_do_write_ops - [INTERN] NAND write with ECC
2352 * @mtd: MTD device structure
2353 * @to: offset to write to
2354 * @ops: oob operations description structure
2356 * NAND write with ECC.
2358 static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2359 struct mtd_oob_ops *ops)
2361 int chipnr, realpage, page, blockmask, column;
2362 struct nand_chip *chip = mtd_to_nand(mtd);
2363 uint32_t writelen = ops->len;
2365 uint32_t oobwritelen = ops->ooblen;
2366 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
2368 uint8_t *oob = ops->oobbuf;
2369 uint8_t *buf = ops->datbuf;
2371 int oob_required = oob ? 1 : 0;
2377 /* Reject writes, which are not page aligned */
2378 if (NOTALIGNED(to)) {
2379 pr_notice("%s: attempt to write non page aligned data\n",
2384 column = to & (mtd->writesize - 1);
2386 chipnr = (int)(to >> chip->chip_shift);
2387 chip->select_chip(mtd, chipnr);
2389 /* Check, if it is write protected */
2390 if (nand_check_wp(mtd)) {
2395 realpage = (int)(to >> chip->page_shift);
2396 page = realpage & chip->pagemask;
2397 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2399 /* Invalidate the page cache, when we write to the cached page */
2400 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2401 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
2404 /* Don't allow multipage oob writes with offset */
2405 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2411 int bytes = mtd->writesize;
2412 int cached = writelen > bytes && page != blockmask;
2413 uint8_t *wbuf = buf;
2415 int part_pagewr = (column || writelen < mtd->writesize);
2423 /* Partial page write?, or need to use bounce buffer */
2425 pr_debug("%s: using write bounce buffer for buf@%p\n",
2429 bytes = min_t(int, bytes - column, writelen);
2431 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2432 memcpy(&chip->buffers->databuf[column], buf, bytes);
2433 wbuf = chip->buffers->databuf;
2436 if (unlikely(oob)) {
2437 size_t len = min(oobwritelen, oobmaxlen);
2438 oob = nand_fill_oob(mtd, oob, len, ops);
2441 /* We still need to erase leftover OOB data */
2442 memset(chip->oob_poi, 0xff, mtd->oobsize);
2444 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2445 oob_required, page, cached,
2446 (ops->mode == MTD_OPS_RAW));
2458 page = realpage & chip->pagemask;
2459 /* Check, if we cross a chip boundary */
2462 chip->select_chip(mtd, -1);
2463 chip->select_chip(mtd, chipnr);
2467 ops->retlen = ops->len - writelen;
2469 ops->oobretlen = ops->ooblen;
2472 chip->select_chip(mtd, -1);
2477 * panic_nand_write - [MTD Interface] NAND write with ECC
2478 * @mtd: MTD device structure
2479 * @to: offset to write to
2480 * @len: number of bytes to write
2481 * @retlen: pointer to variable to store the number of written bytes
2482 * @buf: the data to write
2484 * NAND write with ECC. Used when performing writes in interrupt context, this
2485 * may for example be called by mtdoops when writing an oops while in panic.
2487 static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2488 size_t *retlen, const uint8_t *buf)
2490 struct nand_chip *chip = mtd_to_nand(mtd);
2491 struct mtd_oob_ops ops;
2494 /* Wait for the device to get ready */
2495 panic_nand_wait(mtd, chip, 400);
2497 /* Grab the device */
2498 panic_nand_get_device(chip, mtd, FL_WRITING);
2500 memset(&ops, 0, sizeof(ops));
2502 ops.datbuf = (uint8_t *)buf;
2503 ops.mode = MTD_OPS_PLACE_OOB;
2505 ret = nand_do_write_ops(mtd, to, &ops);
2507 *retlen = ops.retlen;
2512 * nand_write - [MTD Interface] NAND write with ECC
2513 * @mtd: MTD device structure
2514 * @to: offset to write to
2515 * @len: number of bytes to write
2516 * @retlen: pointer to variable to store the number of written bytes
2517 * @buf: the data to write
2519 * NAND write with ECC.
2521 static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2522 size_t *retlen, const uint8_t *buf)
2524 struct mtd_oob_ops ops;
2527 nand_get_device(mtd, FL_WRITING);
2528 memset(&ops, 0, sizeof(ops));
2530 ops.datbuf = (uint8_t *)buf;
2531 ops.mode = MTD_OPS_PLACE_OOB;
2532 ret = nand_do_write_ops(mtd, to, &ops);
2533 *retlen = ops.retlen;
2534 nand_release_device(mtd);
2539 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
2540 * @mtd: MTD device structure
2541 * @to: offset to write to
2542 * @ops: oob operation description structure
2544 * NAND write out-of-band.
2546 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2547 struct mtd_oob_ops *ops)
2549 int chipnr, page, status, len;
2550 struct nand_chip *chip = mtd_to_nand(mtd);
2552 pr_debug("%s: to = 0x%08x, len = %i\n",
2553 __func__, (unsigned int)to, (int)ops->ooblen);
2555 len = mtd_oobavail(mtd, ops);
2557 /* Do not allow write past end of page */
2558 if ((ops->ooboffs + ops->ooblen) > len) {
2559 pr_debug("%s: attempt to write past end of page\n",
2564 if (unlikely(ops->ooboffs >= len)) {
2565 pr_debug("%s: attempt to start write outside oob\n",
2570 /* Do not allow write past end of device */
2571 if (unlikely(to >= mtd->size ||
2572 ops->ooboffs + ops->ooblen >
2573 ((mtd->size >> chip->page_shift) -
2574 (to >> chip->page_shift)) * len)) {
2575 pr_debug("%s: attempt to write beyond end of device\n",
2580 chipnr = (int)(to >> chip->chip_shift);
2581 chip->select_chip(mtd, chipnr);
2583 /* Shift to get page */
2584 page = (int)(to >> chip->page_shift);
2587 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2588 * of my DiskOnChip 2000 test units) will clear the whole data page too
2589 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2590 * it in the doc2000 driver in August 1999. dwmw2.
2592 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2594 /* Check, if it is write protected */
2595 if (nand_check_wp(mtd)) {
2596 chip->select_chip(mtd, -1);
2600 /* Invalidate the page cache, if we write to the cached page */
2601 if (page == chip->pagebuf)
2604 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2606 if (ops->mode == MTD_OPS_RAW)
2607 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2609 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2611 chip->select_chip(mtd, -1);
2616 ops->oobretlen = ops->ooblen;
2622 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2623 * @mtd: MTD device structure
2624 * @to: offset to write to
2625 * @ops: oob operation description structure
2627 static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2628 struct mtd_oob_ops *ops)
2630 int ret = -ENOTSUPP;
2634 /* Do not allow writes past end of device */
2635 if (ops->datbuf && (to + ops->len) > mtd->size) {
2636 pr_debug("%s: attempt to write beyond end of device\n",
2641 nand_get_device(mtd, FL_WRITING);
2643 switch (ops->mode) {
2644 case MTD_OPS_PLACE_OOB:
2645 case MTD_OPS_AUTO_OOB:
2654 ret = nand_do_write_oob(mtd, to, ops);
2656 ret = nand_do_write_ops(mtd, to, ops);
2659 nand_release_device(mtd);
2664 * single_erase - [GENERIC] NAND standard block erase command function
2665 * @mtd: MTD device structure
2666 * @page: the page address of the block which will be erased
2668 * Standard erase command for NAND chips. Returns NAND status.
2670 static int single_erase(struct mtd_info *mtd, int page)
2672 struct nand_chip *chip = mtd_to_nand(mtd);
2673 /* Send commands to erase a block */
2674 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2675 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2677 return chip->waitfunc(mtd, chip);
2681 * nand_erase - [MTD Interface] erase block(s)
2682 * @mtd: MTD device structure
2683 * @instr: erase instruction
2685 * Erase one ore more blocks.
2687 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
2689 return nand_erase_nand(mtd, instr, 0);
2693 * nand_erase_nand - [INTERN] erase block(s)
2694 * @mtd: MTD device structure
2695 * @instr: erase instruction
2696 * @allowbbt: allow erasing the bbt area
2698 * Erase one ore more blocks.
2700 int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2703 int page, status, pages_per_block, ret, chipnr;
2704 struct nand_chip *chip = mtd_to_nand(mtd);
2707 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2708 __func__, (unsigned long long)instr->addr,
2709 (unsigned long long)instr->len);
2711 if (check_offs_len(mtd, instr->addr, instr->len))
2714 /* Grab the lock and see if the device is available */
2715 nand_get_device(mtd, FL_ERASING);
2717 /* Shift to get first page */
2718 page = (int)(instr->addr >> chip->page_shift);
2719 chipnr = (int)(instr->addr >> chip->chip_shift);
2721 /* Calculate pages in each block */
2722 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
2724 /* Select the NAND device */
2725 chip->select_chip(mtd, chipnr);
2727 /* Check, if it is write protected */
2728 if (nand_check_wp(mtd)) {
2729 pr_debug("%s: device is write protected!\n",
2731 instr->state = MTD_ERASE_FAILED;
2735 /* Loop through the pages */
2738 instr->state = MTD_ERASING;
2743 /* Check if we have a bad block, we do not erase bad blocks! */
2744 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
2745 chip->page_shift, allowbbt)) {
2746 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2748 instr->state = MTD_ERASE_FAILED;
2753 * Invalidate the page cache, if we erase the block which
2754 * contains the current cached page.
2756 if (page <= chip->pagebuf && chip->pagebuf <
2757 (page + pages_per_block))
2760 status = chip->erase(mtd, page & chip->pagemask);
2763 * See if operation failed and additional status checks are
2766 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2767 status = chip->errstat(mtd, chip, FL_ERASING,
2770 /* See if block erase succeeded */
2771 if (status & NAND_STATUS_FAIL) {
2772 pr_debug("%s: failed erase, page 0x%08x\n",
2774 instr->state = MTD_ERASE_FAILED;
2776 ((loff_t)page << chip->page_shift);
2780 /* Increment page address and decrement length */
2781 len -= (1ULL << chip->phys_erase_shift);
2782 page += pages_per_block;
2784 /* Check, if we cross a chip boundary */
2785 if (len && !(page & chip->pagemask)) {
2787 chip->select_chip(mtd, -1);
2788 chip->select_chip(mtd, chipnr);
2791 instr->state = MTD_ERASE_DONE;
2795 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2797 /* Deselect and wake up anyone waiting on the device */
2798 chip->select_chip(mtd, -1);
2799 nand_release_device(mtd);
2801 /* Do call back function */
2803 mtd_erase_callback(instr);
2805 /* Return more or less happy */
2810 * nand_sync - [MTD Interface] sync
2811 * @mtd: MTD device structure
2813 * Sync is actually a wait for chip ready function.
2815 static void nand_sync(struct mtd_info *mtd)
2817 pr_debug("%s: called\n", __func__);
2819 /* Grab the lock and see if the device is available */
2820 nand_get_device(mtd, FL_SYNCING);
2821 /* Release it and go back */
2822 nand_release_device(mtd);
2826 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
2827 * @mtd: MTD device structure
2828 * @offs: offset relative to mtd start
2830 static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
2832 struct nand_chip *chip = mtd_to_nand(mtd);
2833 int chipnr = (int)(offs >> chip->chip_shift);
2836 /* Select the NAND device */
2837 nand_get_device(mtd, FL_READING);
2838 chip->select_chip(mtd, chipnr);
2840 ret = nand_block_checkbad(mtd, offs, 0);
2842 chip->select_chip(mtd, -1);
2843 nand_release_device(mtd);
2849 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
2850 * @mtd: MTD device structure
2851 * @ofs: offset relative to mtd start
2853 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2857 ret = nand_block_isbad(mtd, ofs);
2859 /* If it was bad already, return success and do nothing */
2865 return nand_block_markbad_lowlevel(mtd, ofs);
2869 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2870 * @mtd: MTD device structure
2871 * @chip: nand chip info structure
2872 * @addr: feature address.
2873 * @subfeature_param: the subfeature parameters, a four bytes array.
2875 static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2876 int addr, uint8_t *subfeature_param)
2881 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2882 if (!chip->onfi_version ||
2883 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2884 & ONFI_OPT_CMD_SET_GET_FEATURES))
2888 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2889 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2890 chip->write_byte(mtd, subfeature_param[i]);
2892 status = chip->waitfunc(mtd, chip);
2893 if (status & NAND_STATUS_FAIL)
2899 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2900 * @mtd: MTD device structure
2901 * @chip: nand chip info structure
2902 * @addr: feature address.
2903 * @subfeature_param: the subfeature parameters, a four bytes array.
2905 static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2906 int addr, uint8_t *subfeature_param)
2910 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2911 if (!chip->onfi_version ||
2912 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2913 & ONFI_OPT_CMD_SET_GET_FEATURES))
2917 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2918 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2919 *subfeature_param++ = chip->read_byte(mtd);
2923 /* Set default functions */
2924 static void nand_set_defaults(struct nand_chip *chip, int busw)
2926 /* check for proper chip_delay setup, set 20us if not */
2927 if (!chip->chip_delay)
2928 chip->chip_delay = 20;
2930 /* check, if a user supplied command function given */
2931 if (chip->cmdfunc == NULL)
2932 chip->cmdfunc = nand_command;
2934 /* check, if a user supplied wait function given */
2935 if (chip->waitfunc == NULL)
2936 chip->waitfunc = nand_wait;
2938 if (!chip->select_chip)
2939 chip->select_chip = nand_select_chip;
2941 /* set for ONFI nand */
2942 if (!chip->onfi_set_features)
2943 chip->onfi_set_features = nand_onfi_set_features;
2944 if (!chip->onfi_get_features)
2945 chip->onfi_get_features = nand_onfi_get_features;
2947 /* If called twice, pointers that depend on busw may need to be reset */
2948 if (!chip->read_byte || chip->read_byte == nand_read_byte)
2949 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2950 if (!chip->read_word)
2951 chip->read_word = nand_read_word;
2952 if (!chip->block_bad)
2953 chip->block_bad = nand_block_bad;
2954 if (!chip->block_markbad)
2955 chip->block_markbad = nand_default_block_markbad;
2956 if (!chip->write_buf || chip->write_buf == nand_write_buf)
2957 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2958 if (!chip->write_byte || chip->write_byte == nand_write_byte)
2959 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2960 if (!chip->read_buf || chip->read_buf == nand_read_buf)
2961 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2962 if (!chip->scan_bbt)
2963 chip->scan_bbt = nand_default_bbt;
2965 if (!chip->controller) {
2966 chip->controller = &chip->hwcontrol;
2967 spin_lock_init(&chip->controller->lock);
2968 init_waitqueue_head(&chip->controller->wq);
2973 /* Sanitize ONFI strings so we can safely print them */
2974 static void sanitize_string(char *s, size_t len)
2978 /* Null terminate */
2981 /* Remove non printable chars */
2982 for (i = 0; i < len - 1; i++) {
2983 if (s[i] < ' ' || s[i] > 127)
2987 /* Remove trailing spaces */
2991 static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2996 for (i = 0; i < 8; i++)
2997 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3003 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3004 /* Parse the Extended Parameter Page. */
3005 static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3006 struct nand_chip *chip, struct nand_onfi_params *p)
3008 struct onfi_ext_param_page *ep;
3009 struct onfi_ext_section *s;
3010 struct onfi_ext_ecc_info *ecc;
3016 len = le16_to_cpu(p->ext_param_page_length) * 16;
3017 ep = kmalloc(len, GFP_KERNEL);
3021 /* Send our own NAND_CMD_PARAM. */
3022 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3024 /* Use the Change Read Column command to skip the ONFI param pages. */
3025 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3026 sizeof(*p) * p->num_of_param_pages , -1);
3028 /* Read out the Extended Parameter Page. */
3029 chip->read_buf(mtd, (uint8_t *)ep, len);
3030 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3031 != le16_to_cpu(ep->crc))) {
3032 pr_debug("fail in the CRC.\n");
3037 * Check the signature.
3038 * Do not strictly follow the ONFI spec, maybe changed in future.
3040 if (strncmp((char *)ep->sig, "EPPS", 4)) {
3041 pr_debug("The signature is invalid.\n");
3045 /* find the ECC section. */
3046 cursor = (uint8_t *)(ep + 1);
3047 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3048 s = ep->sections + i;
3049 if (s->type == ONFI_SECTION_TYPE_2)
3051 cursor += s->length * 16;
3053 if (i == ONFI_EXT_SECTION_MAX) {
3054 pr_debug("We can not find the ECC section.\n");
3058 /* get the info we want. */
3059 ecc = (struct onfi_ext_ecc_info *)cursor;
3061 if (!ecc->codeword_size) {
3062 pr_debug("Invalid codeword size\n");
3066 chip->ecc_strength_ds = ecc->ecc_bits;
3067 chip->ecc_step_ds = 1 << ecc->codeword_size;
3075 static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3077 struct nand_chip *chip = mtd_to_nand(mtd);
3078 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3080 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3085 * Configure chip properties from Micron vendor-specific ONFI table
3087 static void nand_onfi_detect_micron(struct nand_chip *chip,
3088 struct nand_onfi_params *p)
3090 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3092 if (le16_to_cpu(p->vendor_revision) < 1)
3095 chip->read_retries = micron->read_retry_options;
3096 chip->setup_read_retry = nand_setup_read_retry_micron;
3100 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
3102 static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
3105 struct nand_onfi_params *p = &chip->onfi_params;
3109 /* Try ONFI for unknown chip or LP */
3110 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3111 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3112 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3115 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3116 for (i = 0; i < 3; i++) {
3117 for (j = 0; j < sizeof(*p); j++)
3118 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3119 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3120 le16_to_cpu(p->crc)) {
3126 pr_err("Could not find valid ONFI parameter page; aborting\n");
3131 val = le16_to_cpu(p->revision);
3133 chip->onfi_version = 23;
3134 else if (val & (1 << 4))
3135 chip->onfi_version = 22;
3136 else if (val & (1 << 3))
3137 chip->onfi_version = 21;
3138 else if (val & (1 << 2))
3139 chip->onfi_version = 20;
3140 else if (val & (1 << 1))
3141 chip->onfi_version = 10;
3143 if (!chip->onfi_version) {
3144 pr_info("unsupported ONFI version: %d\n", val);
3148 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3149 sanitize_string(p->model, sizeof(p->model));
3151 mtd->name = p->model;
3153 mtd->writesize = le32_to_cpu(p->byte_per_page);
3156 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3157 * (don't ask me who thought of this...). MTD assumes that these
3158 * dimensions will be power-of-2, so just truncate the remaining area.
3160 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3161 mtd->erasesize *= mtd->writesize;
3163 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3165 /* See erasesize comment */
3166 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3167 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3168 chip->bits_per_cell = p->bits_per_cell;
3170 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
3171 *busw = NAND_BUSWIDTH_16;
3175 if (p->ecc_bits != 0xff) {
3176 chip->ecc_strength_ds = p->ecc_bits;
3177 chip->ecc_step_ds = 512;
3178 } else if (chip->onfi_version >= 21 &&
3179 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3182 * The nand_flash_detect_ext_param_page() uses the
3183 * Change Read Column command which maybe not supported
3184 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3185 * now. We do not replace user supplied command function.
3187 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3188 chip->cmdfunc = nand_command_lp;
3190 /* The Extended Parameter Page is supported since ONFI 2.1. */
3191 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3192 pr_warn("Failed to detect ONFI extended param page\n");
3194 pr_warn("Could not retrieve ONFI ECC requirements\n");
3197 if (p->jedec_id == NAND_MFR_MICRON)
3198 nand_onfi_detect_micron(chip, p);
3203 static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
3211 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3213 static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3216 struct nand_jedec_params *p = &chip->jedec_params;
3217 struct jedec_ecc_info *ecc;
3221 /* Try JEDEC for unknown chip or LP */
3222 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3223 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3224 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3225 chip->read_byte(mtd) != 'C')
3228 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3229 for (i = 0; i < 3; i++) {
3230 for (j = 0; j < sizeof(*p); j++)
3231 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3233 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3234 le16_to_cpu(p->crc))
3239 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3244 val = le16_to_cpu(p->revision);
3246 chip->jedec_version = 10;
3247 else if (val & (1 << 1))
3248 chip->jedec_version = 1; /* vendor specific version */
3250 if (!chip->jedec_version) {
3251 pr_info("unsupported JEDEC version: %d\n", val);
3255 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3256 sanitize_string(p->model, sizeof(p->model));
3258 mtd->name = p->model;
3260 mtd->writesize = le32_to_cpu(p->byte_per_page);
3262 /* Please reference to the comment for nand_flash_detect_onfi. */
3263 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3264 mtd->erasesize *= mtd->writesize;
3266 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3268 /* Please reference to the comment for nand_flash_detect_onfi. */
3269 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3270 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3271 chip->bits_per_cell = p->bits_per_cell;
3273 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3274 *busw = NAND_BUSWIDTH_16;
3279 ecc = &p->ecc_info[0];
3281 if (ecc->codeword_size >= 9) {
3282 chip->ecc_strength_ds = ecc->ecc_bits;
3283 chip->ecc_step_ds = 1 << ecc->codeword_size;
3285 pr_warn("Invalid codeword size\n");
3292 * nand_id_has_period - Check if an ID string has a given wraparound period
3293 * @id_data: the ID string
3294 * @arrlen: the length of the @id_data array
3295 * @period: the period of repitition
3297 * Check if an ID string is repeated within a given sequence of bytes at
3298 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
3299 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
3300 * if the repetition has a period of @period; otherwise, returns zero.
3302 static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3305 for (i = 0; i < period; i++)
3306 for (j = i + period; j < arrlen; j += period)
3307 if (id_data[i] != id_data[j])
3313 * nand_id_len - Get the length of an ID string returned by CMD_READID
3314 * @id_data: the ID string
3315 * @arrlen: the length of the @id_data array
3317 * Returns the length of the ID string, according to known wraparound/trailing
3318 * zero patterns. If no pattern exists, returns the length of the array.
3320 static int nand_id_len(u8 *id_data, int arrlen)
3322 int last_nonzero, period;
3324 /* Find last non-zero byte */
3325 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3326 if (id_data[last_nonzero])
3330 if (last_nonzero < 0)
3333 /* Calculate wraparound period */
3334 for (period = 1; period < arrlen; period++)
3335 if (nand_id_has_period(id_data, arrlen, period))
3338 /* There's a repeated pattern */
3339 if (period < arrlen)
3342 /* There are trailing zeros */
3343 if (last_nonzero < arrlen - 1)
3344 return last_nonzero + 1;
3346 /* No pattern detected */
3350 /* Extract the bits of per cell from the 3rd byte of the extended ID */
3351 static int nand_get_bits_per_cell(u8 cellinfo)
3355 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3356 bits >>= NAND_CI_CELLTYPE_SHIFT;
3361 * Many new NAND share similar device ID codes, which represent the size of the
3362 * chip. The rest of the parameters must be decoded according to generic or
3363 * manufacturer-specific "extended ID" decoding patterns.
3365 static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3366 u8 id_data[8], int *busw)
3369 /* The 3rd id byte holds MLC / multichip data */
3370 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3371 /* The 4th id byte is the important one */
3374 id_len = nand_id_len(id_data, 8);
3377 * Field definitions are in the following datasheets:
3378 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
3379 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
3380 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
3382 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3383 * ID to decide what to do.
3385 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
3386 !nand_is_slc(chip) && id_data[5] != 0x00) {
3388 mtd->writesize = 2048 << (extid & 0x03);
3391 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3411 default: /* Other cases are "reserved" (unknown) */
3412 mtd->oobsize = 1024;
3416 /* Calc blocksize */
3417 mtd->erasesize = (128 * 1024) <<
3418 (((extid >> 1) & 0x04) | (extid & 0x03));
3420 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
3421 !nand_is_slc(chip)) {
3425 mtd->writesize = 2048 << (extid & 0x03);
3428 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3452 /* Calc blocksize */
3453 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3455 mtd->erasesize = (128 * 1024) << tmp;
3456 else if (tmp == 0x03)
3457 mtd->erasesize = 768 * 1024;
3459 mtd->erasesize = (64 * 1024) << tmp;
3463 mtd->writesize = 1024 << (extid & 0x03);
3466 mtd->oobsize = (8 << (extid & 0x01)) *
3467 (mtd->writesize >> 9);
3469 /* Calc blocksize. Blocksize is multiples of 64KiB */
3470 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3472 /* Get buswidth information */
3473 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
3476 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3477 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3479 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3481 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3483 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
3484 nand_is_slc(chip) &&
3485 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3486 !(id_data[4] & 0x80) /* !BENAND */) {
3487 mtd->oobsize = 32 * mtd->writesize >> 9;
3494 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3495 * decodes a matching ID table entry and assigns the MTD size parameters for
3498 static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3499 struct nand_flash_dev *type, u8 id_data[8],
3502 int maf_id = id_data[0];
3504 mtd->erasesize = type->erasesize;
3505 mtd->writesize = type->pagesize;
3506 mtd->oobsize = mtd->writesize / 32;
3507 *busw = type->options & NAND_BUSWIDTH_16;
3509 /* All legacy ID NAND are small-page, SLC */
3510 chip->bits_per_cell = 1;
3513 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3514 * some Spansion chips have erasesize that conflicts with size
3515 * listed in nand_ids table.
3516 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3518 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3519 && id_data[6] == 0x00 && id_data[7] == 0x00
3520 && mtd->writesize == 512) {
3521 mtd->erasesize = 128 * 1024;
3522 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3527 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3528 * heuristic patterns using various detected parameters (e.g., manufacturer,
3529 * page size, cell-type information).
3531 static void nand_decode_bbm_options(struct mtd_info *mtd,
3532 struct nand_chip *chip, u8 id_data[8])
3534 int maf_id = id_data[0];
3536 /* Set the bad block position */
3537 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3538 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3540 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3543 * Bad block marker is stored in the last page of each block on Samsung
3544 * and Hynix MLC devices; stored in first two pages of each block on
3545 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3546 * AMD/Spansion, and Macronix. All others scan only the first page.
3548 if (!nand_is_slc(chip) &&
3549 (maf_id == NAND_MFR_SAMSUNG ||
3550 maf_id == NAND_MFR_HYNIX))
3551 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
3552 else if ((nand_is_slc(chip) &&
3553 (maf_id == NAND_MFR_SAMSUNG ||
3554 maf_id == NAND_MFR_HYNIX ||
3555 maf_id == NAND_MFR_TOSHIBA ||
3556 maf_id == NAND_MFR_AMD ||
3557 maf_id == NAND_MFR_MACRONIX)) ||
3558 (mtd->writesize == 2048 &&
3559 maf_id == NAND_MFR_MICRON))
3560 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3563 static inline bool is_full_id_nand(struct nand_flash_dev *type)
3565 return type->id_len;
3568 static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3569 struct nand_flash_dev *type, u8 *id_data, int *busw)
3571 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
3572 mtd->writesize = type->pagesize;
3573 mtd->erasesize = type->erasesize;
3574 mtd->oobsize = type->oobsize;
3576 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3577 chip->chipsize = (uint64_t)type->chipsize << 20;
3578 chip->options |= type->options;
3579 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3580 chip->ecc_step_ds = NAND_ECC_STEP(type);
3581 chip->onfi_timing_mode_default =
3582 type->onfi_timing_mode_default;
3584 *busw = type->options & NAND_BUSWIDTH_16;
3587 mtd->name = type->name;
3595 * Get the flash and manufacturer id and lookup if the type is supported.
3597 static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
3598 struct nand_chip *chip,
3599 int *maf_id, int *dev_id,
3600 struct nand_flash_dev *type)
3606 /* Select the device */
3607 chip->select_chip(mtd, 0);
3610 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
3613 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3615 /* Send the command for reading device ID */
3616 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3618 /* Read manufacturer and device IDs */
3619 *maf_id = chip->read_byte(mtd);
3620 *dev_id = chip->read_byte(mtd);
3623 * Try again to make sure, as some systems the bus-hold or other
3624 * interface concerns can cause random data which looks like a
3625 * possibly credible NAND flash to appear. If the two results do
3626 * not match, ignore the device completely.
3629 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3631 /* Read entire ID string */
3632 for (i = 0; i < 8; i++)
3633 id_data[i] = chip->read_byte(mtd);
3635 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
3636 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
3637 *maf_id, *dev_id, id_data[0], id_data[1]);
3638 return ERR_PTR(-ENODEV);
3642 type = nand_flash_ids;
3644 for (; type->name != NULL; type++) {
3645 if (is_full_id_nand(type)) {
3646 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3648 } else if (*dev_id == type->dev_id) {
3653 chip->onfi_version = 0;
3654 if (!type->name || !type->pagesize) {
3655 /* Check if the chip is ONFI compliant */
3656 if (nand_flash_detect_onfi(mtd, chip, &busw))
3659 /* Check if the chip is JEDEC compliant */
3660 if (nand_flash_detect_jedec(mtd, chip, &busw))
3665 return ERR_PTR(-ENODEV);
3668 mtd->name = type->name;
3670 chip->chipsize = (uint64_t)type->chipsize << 20;
3672 if (!type->pagesize) {
3673 /* Decode parameters from extended ID */
3674 nand_decode_ext_id(mtd, chip, id_data, &busw);
3676 nand_decode_id(mtd, chip, type, id_data, &busw);
3678 /* Get chip options */
3679 chip->options |= type->options;
3682 * Check if chip is not a Samsung device. Do not clear the
3683 * options for chips which do not have an extended id.
3685 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3686 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3689 /* Try to identify manufacturer */
3690 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
3691 if (nand_manuf_ids[maf_idx].id == *maf_id)
3695 if (chip->options & NAND_BUSWIDTH_AUTO) {
3696 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3697 chip->options |= busw;
3698 nand_set_defaults(chip, busw);
3699 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3701 * Check, if buswidth is correct. Hardware drivers should set
3704 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3706 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3707 pr_warn("bus width %d instead %d bit\n",
3708 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3710 return ERR_PTR(-EINVAL);
3713 nand_decode_bbm_options(mtd, chip, id_data);
3715 /* Calculate the address shift from the page size */
3716 chip->page_shift = ffs(mtd->writesize) - 1;
3717 /* Convert chipsize to number of pages per chip -1 */
3718 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
3720 chip->bbt_erase_shift = chip->phys_erase_shift =
3721 ffs(mtd->erasesize) - 1;
3722 if (chip->chipsize & 0xffffffff)
3723 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
3725 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3726 chip->chip_shift += 32 - 1;
3729 chip->badblockbits = 8;
3730 chip->erase = single_erase;
3732 /* Do not replace user supplied command function! */
3733 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3734 chip->cmdfunc = nand_command_lp;
3736 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3739 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3740 if (chip->onfi_version)
3741 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3742 chip->onfi_params.model);
3743 else if (chip->jedec_version)
3744 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3745 chip->jedec_params.model);
3747 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3750 if (chip->jedec_version)
3751 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3752 chip->jedec_params.model);
3754 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3757 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3761 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
3762 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
3763 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
3767 #if CONFIG_IS_ENABLED(OF_CONTROL)
3768 DECLARE_GLOBAL_DATA_PTR;
3770 static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
3772 int ret, ecc_mode = -1, ecc_strength, ecc_step;
3773 const void *blob = gd->fdt_blob;
3776 ret = fdtdec_get_int(blob, node, "nand-bus-width", -1);
3778 chip->options |= NAND_BUSWIDTH_16;
3780 if (fdtdec_get_bool(blob, node, "nand-on-flash-bbt"))
3781 chip->bbt_options |= NAND_BBT_USE_FLASH;
3783 str = fdt_getprop(blob, node, "nand-ecc-mode", NULL);
3785 if (!strcmp(str, "none"))
3786 ecc_mode = NAND_ECC_NONE;
3787 else if (!strcmp(str, "soft"))
3788 ecc_mode = NAND_ECC_SOFT;
3789 else if (!strcmp(str, "hw"))
3790 ecc_mode = NAND_ECC_HW;
3791 else if (!strcmp(str, "hw_syndrome"))
3792 ecc_mode = NAND_ECC_HW_SYNDROME;
3793 else if (!strcmp(str, "hw_oob_first"))
3794 ecc_mode = NAND_ECC_HW_OOB_FIRST;
3795 else if (!strcmp(str, "soft_bch"))
3796 ecc_mode = NAND_ECC_SOFT_BCH;
3800 ecc_strength = fdtdec_get_int(blob, node, "nand-ecc-strength", -1);
3801 ecc_step = fdtdec_get_int(blob, node, "nand-ecc-step-size", -1);
3803 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
3804 (!(ecc_step >= 0) && ecc_strength >= 0)) {
3805 pr_err("must set both strength and step size in DT\n");
3810 chip->ecc.mode = ecc_mode;
3812 if (ecc_strength >= 0)
3813 chip->ecc.strength = ecc_strength;
3816 chip->ecc.size = ecc_step;
3821 static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
3825 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
3828 * nand_scan_ident - [NAND Interface] Scan for the NAND device
3829 * @mtd: MTD device structure
3830 * @maxchips: number of chips to scan for
3831 * @table: alternative NAND ID table
3833 * This is the first phase of the normal nand_scan() function. It reads the
3834 * flash ID and sets up MTD fields accordingly.
3837 int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3838 struct nand_flash_dev *table)
3840 int i, nand_maf_id, nand_dev_id;
3841 struct nand_chip *chip = mtd_to_nand(mtd);
3842 struct nand_flash_dev *type;
3845 if (chip->flash_node) {
3846 ret = nand_dt_init(mtd, chip, chip->flash_node);
3851 /* Set the default functions */
3852 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
3854 /* Read the flash type */
3855 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3856 &nand_dev_id, table);
3859 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
3860 pr_warn("No NAND device found\n");
3861 chip->select_chip(mtd, -1);
3862 return PTR_ERR(type);
3865 chip->select_chip(mtd, -1);
3867 /* Check for a chip array */
3868 for (i = 1; i < maxchips; i++) {
3869 chip->select_chip(mtd, i);
3870 /* See comment in nand_get_flash_type for reset */
3871 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3872 /* Send the command for reading device ID */
3873 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3874 /* Read manufacturer and device IDs */
3875 if (nand_maf_id != chip->read_byte(mtd) ||
3876 nand_dev_id != chip->read_byte(mtd)) {
3877 chip->select_chip(mtd, -1);
3880 chip->select_chip(mtd, -1);
3885 pr_info("%d chips detected\n", i);
3888 /* Store the number of chips and calc total size for mtd */
3890 mtd->size = i * chip->chipsize;
3894 EXPORT_SYMBOL(nand_scan_ident);
3897 * Check if the chip configuration meet the datasheet requirements.
3899 * If our configuration corrects A bits per B bytes and the minimum
3900 * required correction level is X bits per Y bytes, then we must ensure
3901 * both of the following are true:
3903 * (1) A / B >= X / Y
3906 * Requirement (1) ensures we can correct for the required bitflip density.
3907 * Requirement (2) ensures we can correct even when all bitflips are clumped
3908 * in the same sector.
3910 static bool nand_ecc_strength_good(struct mtd_info *mtd)
3912 struct nand_chip *chip = mtd_to_nand(mtd);
3913 struct nand_ecc_ctrl *ecc = &chip->ecc;
3916 if (ecc->size == 0 || chip->ecc_step_ds == 0)
3917 /* Not enough information */
3921 * We get the number of corrected bits per page to compare
3922 * the correction density.
3924 corr = (mtd->writesize * ecc->strength) / ecc->size;
3925 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
3927 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
3931 * nand_scan_tail - [NAND Interface] Scan for the NAND device
3932 * @mtd: MTD device structure
3934 * This is the second phase of the normal nand_scan() function. It fills out
3935 * all the uninitialized function pointers with the defaults and scans for a
3936 * bad block table if appropriate.
3938 int nand_scan_tail(struct mtd_info *mtd)
3941 struct nand_chip *chip = mtd_to_nand(mtd);
3942 struct nand_ecc_ctrl *ecc = &chip->ecc;
3943 struct nand_buffers *nbuf;
3945 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3946 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3947 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3949 if (!(chip->options & NAND_OWN_BUFFERS)) {
3950 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
3951 chip->buffers = nbuf;
3957 /* Set the internal oob buffer location, just after the page data */
3958 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
3961 * If no default placement scheme is given, select an appropriate one.
3963 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
3964 switch (mtd->oobsize) {
3966 ecc->layout = &nand_oob_8;
3969 ecc->layout = &nand_oob_16;
3972 ecc->layout = &nand_oob_64;
3975 ecc->layout = &nand_oob_128;
3978 pr_warn("No oob scheme defined for oobsize %d\n",
3984 if (!chip->write_page)
3985 chip->write_page = nand_write_page;
3988 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
3989 * selected and we have 256 byte pagesize fallback to software ECC
3992 switch (ecc->mode) {
3993 case NAND_ECC_HW_OOB_FIRST:
3994 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3995 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
3996 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
3999 if (!ecc->read_page)
4000 ecc->read_page = nand_read_page_hwecc_oob_first;
4003 /* Use standard hwecc read page function? */
4004 if (!ecc->read_page)
4005 ecc->read_page = nand_read_page_hwecc;
4006 if (!ecc->write_page)
4007 ecc->write_page = nand_write_page_hwecc;
4008 if (!ecc->read_page_raw)
4009 ecc->read_page_raw = nand_read_page_raw;
4010 if (!ecc->write_page_raw)
4011 ecc->write_page_raw = nand_write_page_raw;
4013 ecc->read_oob = nand_read_oob_std;
4014 if (!ecc->write_oob)
4015 ecc->write_oob = nand_write_oob_std;
4016 if (!ecc->read_subpage)
4017 ecc->read_subpage = nand_read_subpage;
4018 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
4019 ecc->write_subpage = nand_write_subpage_hwecc;
4021 case NAND_ECC_HW_SYNDROME:
4022 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4024 ecc->read_page == nand_read_page_hwecc ||
4026 ecc->write_page == nand_write_page_hwecc)) {
4027 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
4030 /* Use standard syndrome read/write page function? */
4031 if (!ecc->read_page)
4032 ecc->read_page = nand_read_page_syndrome;
4033 if (!ecc->write_page)
4034 ecc->write_page = nand_write_page_syndrome;
4035 if (!ecc->read_page_raw)
4036 ecc->read_page_raw = nand_read_page_raw_syndrome;
4037 if (!ecc->write_page_raw)
4038 ecc->write_page_raw = nand_write_page_raw_syndrome;
4040 ecc->read_oob = nand_read_oob_syndrome;
4041 if (!ecc->write_oob)
4042 ecc->write_oob = nand_write_oob_syndrome;
4044 if (mtd->writesize >= ecc->size) {
4045 if (!ecc->strength) {
4046 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4051 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4052 ecc->size, mtd->writesize);
4053 ecc->mode = NAND_ECC_SOFT;
4056 ecc->calculate = nand_calculate_ecc;
4057 ecc->correct = nand_correct_data;
4058 ecc->read_page = nand_read_page_swecc;
4059 ecc->read_subpage = nand_read_subpage;
4060 ecc->write_page = nand_write_page_swecc;
4061 ecc->read_page_raw = nand_read_page_raw;
4062 ecc->write_page_raw = nand_write_page_raw;
4063 ecc->read_oob = nand_read_oob_std;
4064 ecc->write_oob = nand_write_oob_std;
4071 case NAND_ECC_SOFT_BCH:
4072 if (!mtd_nand_has_bch()) {
4073 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4076 ecc->calculate = nand_bch_calculate_ecc;
4077 ecc->correct = nand_bch_correct_data;
4078 ecc->read_page = nand_read_page_swecc;
4079 ecc->read_subpage = nand_read_subpage;
4080 ecc->write_page = nand_write_page_swecc;
4081 ecc->read_page_raw = nand_read_page_raw;
4082 ecc->write_page_raw = nand_write_page_raw;
4083 ecc->read_oob = nand_read_oob_std;
4084 ecc->write_oob = nand_write_oob_std;
4086 * Board driver should supply ecc.size and ecc.strength values
4087 * to select how many bits are correctable. Otherwise, default
4088 * to 4 bits for large page devices.
4090 if (!ecc->size && (mtd->oobsize >= 64)) {
4095 /* See nand_bch_init() for details. */
4097 ecc->priv = nand_bch_init(mtd);
4099 pr_warn("BCH ECC initialization failed!\n");
4105 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
4106 ecc->read_page = nand_read_page_raw;
4107 ecc->write_page = nand_write_page_raw;
4108 ecc->read_oob = nand_read_oob_std;
4109 ecc->read_page_raw = nand_read_page_raw;
4110 ecc->write_page_raw = nand_write_page_raw;
4111 ecc->write_oob = nand_write_oob_std;
4112 ecc->size = mtd->writesize;
4118 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
4122 /* For many systems, the standard OOB write also works for raw */
4123 if (!ecc->read_oob_raw)
4124 ecc->read_oob_raw = ecc->read_oob;
4125 if (!ecc->write_oob_raw)
4126 ecc->write_oob_raw = ecc->write_oob;
4129 * The number of bytes available for a client to place data into
4130 * the out of band area.
4134 for (i = 0; ecc->layout->oobfree[i].length; i++)
4135 mtd->oobavail += ecc->layout->oobfree[i].length;
4138 /* ECC sanity check: warn if it's too weak */
4139 if (!nand_ecc_strength_good(mtd))
4140 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4144 * Set the number of read / write steps for one page depending on ECC
4147 ecc->steps = mtd->writesize / ecc->size;
4148 if (ecc->steps * ecc->size != mtd->writesize) {
4149 pr_warn("Invalid ECC parameters\n");
4152 ecc->total = ecc->steps * ecc->bytes;
4154 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
4155 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
4156 switch (ecc->steps) {
4158 mtd->subpage_sft = 1;
4163 mtd->subpage_sft = 2;
4167 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4169 /* Initialize state */
4170 chip->state = FL_READY;
4172 /* Invalidate the pagebuffer reference */
4175 /* Large page NAND with SOFT_ECC should support subpage reads */
4176 switch (ecc->mode) {
4178 case NAND_ECC_SOFT_BCH:
4179 if (chip->page_shift > 9)
4180 chip->options |= NAND_SUBPAGE_READ;
4187 /* Fill in remaining MTD driver data */
4188 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
4189 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4191 mtd->_erase = nand_erase;
4192 mtd->_read = nand_read;
4193 mtd->_write = nand_write;
4194 mtd->_panic_write = panic_nand_write;
4195 mtd->_read_oob = nand_read_oob;
4196 mtd->_write_oob = nand_write_oob;
4197 mtd->_sync = nand_sync;
4199 mtd->_unlock = NULL;
4200 mtd->_block_isreserved = nand_block_isreserved;
4201 mtd->_block_isbad = nand_block_isbad;
4202 mtd->_block_markbad = nand_block_markbad;
4203 mtd->writebufsize = mtd->writesize;
4205 /* propagate ecc info to mtd_info */
4206 mtd->ecclayout = ecc->layout;
4207 mtd->ecc_strength = ecc->strength;
4208 mtd->ecc_step_size = ecc->size;
4210 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4211 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4214 if (!mtd->bitflip_threshold)
4215 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
4219 EXPORT_SYMBOL(nand_scan_tail);
4222 * nand_scan - [NAND Interface] Scan for the NAND device
4223 * @mtd: MTD device structure
4224 * @maxchips: number of chips to scan for
4226 * This fills out all the uninitialized function pointers with the defaults.
4227 * The flash ID is read and the mtd/chip structures are filled with the
4228 * appropriate values.
4230 int nand_scan(struct mtd_info *mtd, int maxchips)
4234 ret = nand_scan_ident(mtd, maxchips, NULL);
4236 ret = nand_scan_tail(mtd);
4239 EXPORT_SYMBOL(nand_scan);
4241 MODULE_LICENSE("GPL");
4242 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4243 MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
4244 MODULE_DESCRIPTION("Generic NAND flash driver code");