5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
9 * Additional technical information is available on
10 * http://www.linux-mtd.infradead.org/doc/nand.html
12 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
13 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
16 * David Woodhouse for adding multichip support
18 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
22 * Enable cached programming for 2k page size chips
23 * Check, if mtd->ecctype should be set to MTD_ECC_HW
24 * if we have HW ECC support.
25 * The AG-AND chips have nice features for speed improvement,
26 * which are not supported yet. Read / program 4 pages in one go.
27 * BBT table is not serialized, has to be fixed
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License version 2 as
31 * published by the Free Software Foundation.
37 #define ENOTSUPP 524 /* Operation is not supported */
41 #include <linux/err.h>
42 #include <linux/compat.h>
43 #include <linux/mtd/mtd.h>
44 #include <linux/mtd/nand.h>
45 #include <linux/mtd/nand_ecc.h>
46 #include <linux/mtd/nand_bch.h>
48 #ifdef CONFIG_MTD_PARTITIONS
49 #include <linux/mtd/partitions.h>
53 #include <asm/errno.h>
56 * CONFIG_SYS_NAND_RESET_CNT is used as a timeout mechanism when resetting
57 * a flash. NAND flash is initialized prior to interrupts so standard timers
58 * can't be used. CONFIG_SYS_NAND_RESET_CNT should be set to a value
59 * which is greater than (max NAND reset time / NAND status read time).
60 * A conservative default of 200000 (500 us / 25 ns) is used as a default.
62 #ifndef CONFIG_SYS_NAND_RESET_CNT
63 #define CONFIG_SYS_NAND_RESET_CNT 200000
66 /* Define default oob placement schemes for large and small page devices */
67 static struct nand_ecclayout nand_oob_8 = {
77 static struct nand_ecclayout nand_oob_16 = {
79 .eccpos = {0, 1, 2, 3, 6, 7},
85 static struct nand_ecclayout nand_oob_64 = {
88 40, 41, 42, 43, 44, 45, 46, 47,
89 48, 49, 50, 51, 52, 53, 54, 55,
90 56, 57, 58, 59, 60, 61, 62, 63},
96 static struct nand_ecclayout nand_oob_128 = {
99 80, 81, 82, 83, 84, 85, 86, 87,
100 88, 89, 90, 91, 92, 93, 94, 95,
101 96, 97, 98, 99, 100, 101, 102, 103,
102 104, 105, 106, 107, 108, 109, 110, 111,
103 112, 113, 114, 115, 116, 117, 118, 119,
104 120, 121, 122, 123, 124, 125, 126, 127},
110 static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
113 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
114 struct mtd_oob_ops *ops);
116 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this);
118 static int check_offs_len(struct mtd_info *mtd,
119 loff_t ofs, uint64_t len)
121 struct nand_chip *chip = mtd->priv;
124 /* Start address must align on block boundary */
125 if (ofs & ((1 << chip->phys_erase_shift) - 1)) {
126 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__);
130 /* Length must align on block boundary */
131 if (len & ((1 << chip->phys_erase_shift) - 1)) {
132 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
141 * nand_release_device - [GENERIC] release chip
142 * @mtd: MTD device structure
144 * Deselect, release chip lock and wake up anyone waiting on the device.
146 static void nand_release_device(struct mtd_info *mtd)
148 struct nand_chip *chip = mtd->priv;
150 /* De-select the NAND device */
151 chip->select_chip(mtd, -1);
155 * nand_read_byte - [DEFAULT] read one byte from the chip
156 * @mtd: MTD device structure
158 * Default read function for 8bit buswidth.
160 uint8_t nand_read_byte(struct mtd_info *mtd)
162 struct nand_chip *chip = mtd->priv;
163 return readb(chip->IO_ADDR_R);
167 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
168 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
169 * @mtd: MTD device structure
171 * Default read function for 16bit buswidth with endianness conversion.
174 static uint8_t nand_read_byte16(struct mtd_info *mtd)
176 struct nand_chip *chip = mtd->priv;
177 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
181 * nand_read_word - [DEFAULT] read one word from the chip
182 * @mtd: MTD device structure
184 * Default read function for 16bit buswidth without endianness conversion.
186 static u16 nand_read_word(struct mtd_info *mtd)
188 struct nand_chip *chip = mtd->priv;
189 return readw(chip->IO_ADDR_R);
193 * nand_select_chip - [DEFAULT] control CE line
194 * @mtd: MTD device structure
195 * @chipnr: chipnumber to select, -1 for deselect
197 * Default select function for 1 chip devices.
199 static void nand_select_chip(struct mtd_info *mtd, int chipnr)
201 struct nand_chip *chip = mtd->priv;
205 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
216 * nand_write_buf - [DEFAULT] write buffer to chip
217 * @mtd: MTD device structure
219 * @len: number of bytes to write
221 * Default write function for 8bit buswidth.
223 void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
226 struct nand_chip *chip = mtd->priv;
228 for (i = 0; i < len; i++)
229 writeb(buf[i], chip->IO_ADDR_W);
233 * nand_read_buf - [DEFAULT] read chip data into buffer
234 * @mtd: MTD device structure
235 * @buf: buffer to store date
236 * @len: number of bytes to read
238 * Default read function for 8bit buswidth.
240 void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
243 struct nand_chip *chip = mtd->priv;
245 for (i = 0; i < len; i++)
246 buf[i] = readb(chip->IO_ADDR_R);
250 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
251 * @mtd: MTD device structure
252 * @buf: buffer containing the data to compare
253 * @len: number of bytes to compare
255 * Default verify function for 8bit buswidth.
257 static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
260 struct nand_chip *chip = mtd->priv;
262 for (i = 0; i < len; i++)
263 if (buf[i] != readb(chip->IO_ADDR_R))
269 * nand_write_buf16 - [DEFAULT] write buffer to chip
270 * @mtd: MTD device structure
272 * @len: number of bytes to write
274 * Default write function for 16bit buswidth.
276 void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
279 struct nand_chip *chip = mtd->priv;
280 u16 *p = (u16 *) buf;
283 for (i = 0; i < len; i++)
284 writew(p[i], chip->IO_ADDR_W);
289 * nand_read_buf16 - [DEFAULT] read chip data into buffer
290 * @mtd: MTD device structure
291 * @buf: buffer to store date
292 * @len: number of bytes to read
294 * Default read function for 16bit buswidth.
296 void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
299 struct nand_chip *chip = mtd->priv;
300 u16 *p = (u16 *) buf;
303 for (i = 0; i < len; i++)
304 p[i] = readw(chip->IO_ADDR_R);
308 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
309 * @mtd: MTD device structure
310 * @buf: buffer containing the data to compare
311 * @len: number of bytes to compare
313 * Default verify function for 16bit buswidth.
315 static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
318 struct nand_chip *chip = mtd->priv;
319 u16 *p = (u16 *) buf;
322 for (i = 0; i < len; i++)
323 if (p[i] != readw(chip->IO_ADDR_R))
330 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
331 * @mtd: MTD device structure
332 * @ofs: offset from device start
333 * @getchip: 0, if the chip is already selected
335 * Check, if the block is bad.
337 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
339 int page, chipnr, res = 0, i = 0;
340 struct nand_chip *chip = mtd->priv;
343 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
344 ofs += mtd->erasesize - mtd->writesize;
346 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
349 chipnr = (int)(ofs >> chip->chip_shift);
351 nand_get_device(chip, mtd, FL_READING);
353 /* Select the NAND device */
354 chip->select_chip(mtd, chipnr);
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));
382 nand_release_device(mtd);
388 * nand_default_block_markbad - [DEFAULT] mark a block bad
389 * @mtd: MTD device structure
390 * @ofs: offset from device start
392 * This is the default implementation, which can be overridden by a hardware
393 * specific driver. We try operations in the following order, according to our
394 * bbt_options (NAND_BBT_NO_OOB_BBM and NAND_BBT_USE_FLASH):
395 * (1) erase the affected block, to allow OOB marker to be written cleanly
396 * (2) update in-memory BBT
397 * (3) write bad block marker to OOB area of affected block
398 * (4) update flash-based BBT
399 * Note that we retain the first error encountered in (3) or (4), finish the
400 * procedures, and dump the error in the end.
402 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
404 struct nand_chip *chip = mtd->priv;
405 uint8_t buf[2] = { 0, 0 };
406 int block, res, ret = 0, i = 0;
407 int write_oob = !(chip->bbt_options & NAND_BBT_NO_OOB_BBM);
410 struct erase_info einfo;
412 /* Attempt erase before marking OOB */
413 memset(&einfo, 0, sizeof(einfo));
416 einfo.len = 1 << chip->phys_erase_shift;
417 nand_erase_nand(mtd, &einfo, 0);
420 /* Get block number */
421 block = (int)(ofs >> chip->bbt_erase_shift);
422 /* Mark block bad in memory-based BBT */
424 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
426 /* Write bad block marker to OOB */
428 struct mtd_oob_ops ops;
431 nand_get_device(chip, mtd, FL_WRITING);
435 ops.ooboffs = chip->badblockpos;
436 if (chip->options & NAND_BUSWIDTH_16) {
437 ops.ooboffs &= ~0x01;
438 ops.len = ops.ooblen = 2;
440 ops.len = ops.ooblen = 1;
442 ops.mode = MTD_OPS_PLACE_OOB;
444 /* Write to first/last page(s) if necessary */
445 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
446 wr_ofs += mtd->erasesize - mtd->writesize;
448 res = nand_do_write_oob(mtd, wr_ofs, &ops);
453 wr_ofs += mtd->writesize;
454 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
456 nand_release_device(mtd);
459 /* Update flash-based bad block table */
460 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
461 res = nand_update_bbt(mtd, ofs);
467 mtd->ecc_stats.badblocks++;
473 * nand_check_wp - [GENERIC] check if the chip is write protected
474 * @mtd: MTD device structure
476 * Check, if the device is write protected. The function expects, that the
477 * device is already selected.
479 static int nand_check_wp(struct mtd_info *mtd)
481 struct nand_chip *chip = mtd->priv;
483 /* Broken xD cards report WP despite being writable */
484 if (chip->options & NAND_BROKEN_XD)
487 /* Check the WP bit */
488 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
489 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
493 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
494 * @mtd: MTD device structure
495 * @ofs: offset from device start
496 * @getchip: 0, if the chip is already selected
497 * @allowbbt: 1, if its allowed to access the bbt area
499 * Check, if the block is bad. Either by reading the bad block table or
500 * calling of the scan function.
502 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
505 struct nand_chip *chip = mtd->priv;
507 if (!(chip->options & NAND_BBT_SCANNED)) {
508 chip->options |= NAND_BBT_SCANNED;
513 return chip->block_bad(mtd, ofs, getchip);
515 /* Return info from the table */
516 return nand_isbad_bbt(mtd, ofs, allowbbt);
519 /* Wait for the ready pin, after a command. The timeout is caught later. */
520 void nand_wait_ready(struct mtd_info *mtd)
522 struct nand_chip *chip = mtd->priv;
523 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
526 time_start = get_timer(0);
528 /* Wait until command is processed or timeout occurs */
529 while (get_timer(time_start) < timeo) {
531 if (chip->dev_ready(mtd))
537 * nand_command - [DEFAULT] Send command to NAND device
538 * @mtd: MTD device structure
539 * @command: the command to be sent
540 * @column: the column address for this command, -1 if none
541 * @page_addr: the page address for this command, -1 if none
543 * Send command to NAND device. This function is used for small page devices
544 * (256/512 Bytes per page).
546 static void nand_command(struct mtd_info *mtd, unsigned int command,
547 int column, int page_addr)
549 register struct nand_chip *chip = mtd->priv;
550 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
551 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
553 /* Write out the command to the device */
554 if (command == NAND_CMD_SEQIN) {
557 if (column >= mtd->writesize) {
559 column -= mtd->writesize;
560 readcmd = NAND_CMD_READOOB;
561 } else if (column < 256) {
562 /* First 256 bytes --> READ0 */
563 readcmd = NAND_CMD_READ0;
566 readcmd = NAND_CMD_READ1;
568 chip->cmd_ctrl(mtd, readcmd, ctrl);
569 ctrl &= ~NAND_CTRL_CHANGE;
571 chip->cmd_ctrl(mtd, command, ctrl);
573 /* Address cycle, when necessary */
574 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
575 /* Serially input address */
577 /* Adjust columns for 16 bit buswidth */
578 if ((chip->options & NAND_BUSWIDTH_16) &&
579 !nand_opcode_8bits(command))
581 chip->cmd_ctrl(mtd, column, ctrl);
582 ctrl &= ~NAND_CTRL_CHANGE;
584 if (page_addr != -1) {
585 chip->cmd_ctrl(mtd, page_addr, ctrl);
586 ctrl &= ~NAND_CTRL_CHANGE;
587 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
588 /* One more address cycle for devices > 32MiB */
589 if (chip->chipsize > (32 << 20))
590 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
592 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
595 * Program and erase have their own busy handlers status and sequential
600 case NAND_CMD_PAGEPROG:
601 case NAND_CMD_ERASE1:
602 case NAND_CMD_ERASE2:
604 case NAND_CMD_STATUS:
610 udelay(chip->chip_delay);
611 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
612 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
614 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
615 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
619 /* This applies to read commands */
622 * If we don't have access to the busy pin, we apply the given
625 if (!chip->dev_ready) {
626 udelay(chip->chip_delay);
631 * Apply this short delay always to ensure that we do wait tWB in
632 * any case on any machine.
636 nand_wait_ready(mtd);
640 * nand_command_lp - [DEFAULT] Send command to NAND large page device
641 * @mtd: MTD device structure
642 * @command: the command to be sent
643 * @column: the column address for this command, -1 if none
644 * @page_addr: the page address for this command, -1 if none
646 * Send command to NAND device. This is the version for the new large page
647 * devices. We don't have the separate regions as we have in the small page
648 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
650 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
651 int column, int page_addr)
653 register struct nand_chip *chip = mtd->priv;
654 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
656 /* Emulate NAND_CMD_READOOB */
657 if (command == NAND_CMD_READOOB) {
658 column += mtd->writesize;
659 command = NAND_CMD_READ0;
662 /* Command latch cycle */
663 chip->cmd_ctrl(mtd, command & 0xff,
664 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
666 if (column != -1 || page_addr != -1) {
667 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
669 /* Serially input address */
671 /* Adjust columns for 16 bit buswidth */
672 if ((chip->options & NAND_BUSWIDTH_16) &&
673 !nand_opcode_8bits(command))
675 chip->cmd_ctrl(mtd, column, ctrl);
676 ctrl &= ~NAND_CTRL_CHANGE;
677 chip->cmd_ctrl(mtd, column >> 8, ctrl);
679 if (page_addr != -1) {
680 chip->cmd_ctrl(mtd, page_addr, ctrl);
681 chip->cmd_ctrl(mtd, page_addr >> 8,
682 NAND_NCE | NAND_ALE);
683 /* One more address cycle for devices > 128MiB */
684 if (chip->chipsize > (128 << 20))
685 chip->cmd_ctrl(mtd, page_addr >> 16,
686 NAND_NCE | NAND_ALE);
689 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
692 * Program and erase have their own busy handlers status, sequential
693 * in, and deplete1 need no delay.
697 case NAND_CMD_CACHEDPROG:
698 case NAND_CMD_PAGEPROG:
699 case NAND_CMD_ERASE1:
700 case NAND_CMD_ERASE2:
703 case NAND_CMD_STATUS:
704 case NAND_CMD_DEPLETE1:
707 case NAND_CMD_STATUS_ERROR:
708 case NAND_CMD_STATUS_ERROR0:
709 case NAND_CMD_STATUS_ERROR1:
710 case NAND_CMD_STATUS_ERROR2:
711 case NAND_CMD_STATUS_ERROR3:
712 /* Read error status commands require only a short delay */
713 udelay(chip->chip_delay);
719 udelay(chip->chip_delay);
720 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
721 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
722 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
723 NAND_NCE | NAND_CTRL_CHANGE);
724 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
728 case NAND_CMD_RNDOUT:
729 /* No ready / busy check necessary */
730 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
731 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
732 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
733 NAND_NCE | NAND_CTRL_CHANGE);
737 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
738 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
739 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
740 NAND_NCE | NAND_CTRL_CHANGE);
742 /* This applies to read commands */
745 * If we don't have access to the busy pin, we apply the given
748 if (!chip->dev_ready) {
749 udelay(chip->chip_delay);
755 * Apply this short delay always to ensure that we do wait tWB in
756 * any case on any machine.
760 nand_wait_ready(mtd);
764 * nand_get_device - [GENERIC] Get chip for selected access
765 * @chip: the nand chip descriptor
766 * @mtd: MTD device structure
767 * @new_state: the state which is requested
769 * Get the device and lock it for exclusive access
772 nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
774 chip->state = new_state;
779 * nand_wait - [DEFAULT] wait until the command is done
780 * @mtd: MTD device structure
781 * @chip: NAND chip structure
783 * Wait for command done. This applies to erase and program only. Erase can
784 * take up to 400ms and program up to 20ms according to general NAND and
787 static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
790 int state = chip->state;
793 if (state == FL_ERASING)
794 timeo = (CONFIG_SYS_HZ * 400) / 1000;
796 timeo = (CONFIG_SYS_HZ * 20) / 1000;
798 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
799 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
801 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
803 time_start = get_timer(0);
806 if (get_timer(time_start) > timeo) {
811 if (chip->dev_ready) {
812 if (chip->dev_ready(mtd))
815 if (chip->read_byte(mtd) & NAND_STATUS_READY)
819 #ifdef PPCHAMELON_NAND_TIMER_HACK
820 time_start = get_timer(0);
821 while (get_timer(time_start) < 10)
823 #endif /* PPCHAMELON_NAND_TIMER_HACK */
825 return (int)chip->read_byte(mtd);
829 * nand_read_page_raw - [INTERN] read raw page data without ecc
830 * @mtd: mtd info structure
831 * @chip: nand chip info structure
832 * @buf: buffer to store read data
833 * @oob_required: caller requires OOB data read to chip->oob_poi
834 * @page: page number to read
836 * Not for syndrome calculating ECC controllers, which use a special oob layout.
838 static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
839 uint8_t *buf, int oob_required, int page)
841 chip->read_buf(mtd, buf, mtd->writesize);
843 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
848 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
849 * @mtd: mtd info structure
850 * @chip: nand chip info structure
851 * @buf: buffer to store read data
852 * @oob_required: caller requires OOB data read to chip->oob_poi
853 * @page: page number to read
855 * We need a special oob layout and handling even when OOB isn't used.
857 static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
858 struct nand_chip *chip, uint8_t *buf,
859 int oob_required, int page)
861 int eccsize = chip->ecc.size;
862 int eccbytes = chip->ecc.bytes;
863 uint8_t *oob = chip->oob_poi;
866 for (steps = chip->ecc.steps; steps > 0; steps--) {
867 chip->read_buf(mtd, buf, eccsize);
870 if (chip->ecc.prepad) {
871 chip->read_buf(mtd, oob, chip->ecc.prepad);
872 oob += chip->ecc.prepad;
875 chip->read_buf(mtd, oob, eccbytes);
878 if (chip->ecc.postpad) {
879 chip->read_buf(mtd, oob, chip->ecc.postpad);
880 oob += chip->ecc.postpad;
884 size = mtd->oobsize - (oob - chip->oob_poi);
886 chip->read_buf(mtd, oob, size);
892 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
893 * @mtd: mtd info structure
894 * @chip: nand chip info structure
895 * @buf: buffer to store read data
896 * @oob_required: caller requires OOB data read to chip->oob_poi
897 * @page: page number to read
899 static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
900 uint8_t *buf, int oob_required, int page)
902 int i, eccsize = chip->ecc.size;
903 int eccbytes = chip->ecc.bytes;
904 int eccsteps = chip->ecc.steps;
906 uint8_t *ecc_calc = chip->buffers->ecccalc;
907 uint8_t *ecc_code = chip->buffers->ecccode;
908 uint32_t *eccpos = chip->ecc.layout->eccpos;
910 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
912 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
913 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
915 for (i = 0; i < chip->ecc.total; i++)
916 ecc_code[i] = chip->oob_poi[eccpos[i]];
918 eccsteps = chip->ecc.steps;
921 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
924 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
926 mtd->ecc_stats.failed++;
928 mtd->ecc_stats.corrected += stat;
934 * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function
935 * @mtd: mtd info structure
936 * @chip: nand chip info structure
937 * @data_offs: offset of requested data within the page
938 * @readlen: data length
939 * @bufpoi: buffer to store read data
941 static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
942 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
944 int start_step, end_step, num_steps;
945 uint32_t *eccpos = chip->ecc.layout->eccpos;
947 int data_col_addr, i, gaps = 0;
948 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
949 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
952 /* Column address within the page aligned to ECC size (256bytes) */
953 start_step = data_offs / chip->ecc.size;
954 end_step = (data_offs + readlen - 1) / chip->ecc.size;
955 num_steps = end_step - start_step + 1;
957 /* Data size aligned to ECC ecc.size */
958 datafrag_len = num_steps * chip->ecc.size;
959 eccfrag_len = num_steps * chip->ecc.bytes;
961 data_col_addr = start_step * chip->ecc.size;
962 /* If we read not a page aligned data */
963 if (data_col_addr != 0)
964 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
966 p = bufpoi + data_col_addr;
967 chip->read_buf(mtd, p, datafrag_len);
970 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
971 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
974 * The performance is faster if we position offsets according to
975 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
977 for (i = 0; i < eccfrag_len - 1; i++) {
978 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
979 eccpos[i + start_step * chip->ecc.bytes + 1]) {
985 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
986 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
989 * Send the command to read the particular ECC bytes take care
990 * about buswidth alignment in read_buf.
992 index = start_step * chip->ecc.bytes;
994 aligned_pos = eccpos[index] & ~(busw - 1);
995 aligned_len = eccfrag_len;
996 if (eccpos[index] & (busw - 1))
998 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
1001 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1002 mtd->writesize + aligned_pos, -1);
1003 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1006 for (i = 0; i < eccfrag_len; i++)
1007 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
1009 p = bufpoi + data_col_addr;
1010 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1013 stat = chip->ecc.correct(mtd, p,
1014 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1016 mtd->ecc_stats.failed++;
1018 mtd->ecc_stats.corrected += stat;
1024 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1025 * @mtd: mtd info structure
1026 * @chip: nand chip info structure
1027 * @buf: buffer to store read data
1028 * @oob_required: caller requires OOB data read to chip->oob_poi
1029 * @page: page number to read
1031 * Not for syndrome calculating ECC controllers which need a special oob layout.
1033 static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1034 uint8_t *buf, int oob_required, int page)
1036 int i, eccsize = chip->ecc.size;
1037 int eccbytes = chip->ecc.bytes;
1038 int eccsteps = chip->ecc.steps;
1040 uint8_t *ecc_calc = chip->buffers->ecccalc;
1041 uint8_t *ecc_code = chip->buffers->ecccode;
1042 uint32_t *eccpos = chip->ecc.layout->eccpos;
1044 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1045 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1046 chip->read_buf(mtd, p, eccsize);
1047 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1049 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1051 for (i = 0; i < chip->ecc.total; i++)
1052 ecc_code[i] = chip->oob_poi[eccpos[i]];
1054 eccsteps = chip->ecc.steps;
1057 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1060 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1062 mtd->ecc_stats.failed++;
1064 mtd->ecc_stats.corrected += stat;
1070 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1071 * @mtd: mtd info structure
1072 * @chip: nand chip info structure
1073 * @buf: buffer to store read data
1074 * @oob_required: caller requires OOB data read to chip->oob_poi
1075 * @page: page number to read
1077 * Hardware ECC for large page chips, require OOB to be read first. For this
1078 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1079 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1080 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1081 * the data area, by overwriting the NAND manufacturer bad block markings.
1083 static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1084 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
1086 int i, eccsize = chip->ecc.size;
1087 int eccbytes = chip->ecc.bytes;
1088 int eccsteps = chip->ecc.steps;
1090 uint8_t *ecc_code = chip->buffers->ecccode;
1091 uint32_t *eccpos = chip->ecc.layout->eccpos;
1092 uint8_t *ecc_calc = chip->buffers->ecccalc;
1094 /* Read the OOB area first */
1095 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1096 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1097 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1099 for (i = 0; i < chip->ecc.total; i++)
1100 ecc_code[i] = chip->oob_poi[eccpos[i]];
1102 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1105 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1106 chip->read_buf(mtd, p, eccsize);
1107 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1109 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1111 mtd->ecc_stats.failed++;
1113 mtd->ecc_stats.corrected += stat;
1119 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1120 * @mtd: mtd info structure
1121 * @chip: nand chip info structure
1122 * @buf: buffer to store read data
1123 * @oob_required: caller requires OOB data read to chip->oob_poi
1124 * @page: page number to read
1126 * The hw generator calculates the error syndrome automatically. Therefore we
1127 * need a special oob layout and handling.
1129 static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1130 uint8_t *buf, int oob_required, int page)
1132 int i, eccsize = chip->ecc.size;
1133 int eccbytes = chip->ecc.bytes;
1134 int eccsteps = chip->ecc.steps;
1136 uint8_t *oob = chip->oob_poi;
1138 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1141 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1142 chip->read_buf(mtd, p, eccsize);
1144 if (chip->ecc.prepad) {
1145 chip->read_buf(mtd, oob, chip->ecc.prepad);
1146 oob += chip->ecc.prepad;
1149 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1150 chip->read_buf(mtd, oob, eccbytes);
1151 stat = chip->ecc.correct(mtd, p, oob, NULL);
1154 mtd->ecc_stats.failed++;
1156 mtd->ecc_stats.corrected += stat;
1160 if (chip->ecc.postpad) {
1161 chip->read_buf(mtd, oob, chip->ecc.postpad);
1162 oob += chip->ecc.postpad;
1166 /* Calculate remaining oob bytes */
1167 i = mtd->oobsize - (oob - chip->oob_poi);
1169 chip->read_buf(mtd, oob, i);
1175 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1176 * @chip: nand chip structure
1177 * @oob: oob destination address
1178 * @ops: oob ops structure
1179 * @len: size of oob to transfer
1181 static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1182 struct mtd_oob_ops *ops, size_t len)
1184 switch (ops->mode) {
1186 case MTD_OPS_PLACE_OOB:
1188 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1191 case MTD_OPS_AUTO_OOB: {
1192 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1193 uint32_t boffs = 0, roffs = ops->ooboffs;
1196 for (; free->length && len; free++, len -= bytes) {
1197 /* Read request not from offset 0? */
1198 if (unlikely(roffs)) {
1199 if (roffs >= free->length) {
1200 roffs -= free->length;
1203 boffs = free->offset + roffs;
1204 bytes = min_t(size_t, len,
1205 (free->length - roffs));
1208 bytes = min_t(size_t, len, free->length);
1209 boffs = free->offset;
1211 memcpy(oob, chip->oob_poi + boffs, bytes);
1223 * nand_do_read_ops - [INTERN] Read data with ECC
1224 * @mtd: MTD device structure
1225 * @from: offset to read from
1226 * @ops: oob ops structure
1228 * Internal function. Called with chip held.
1230 static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1231 struct mtd_oob_ops *ops)
1233 int chipnr, page, realpage, col, bytes, aligned, oob_required;
1234 struct nand_chip *chip = mtd->priv;
1235 struct mtd_ecc_stats stats;
1237 uint32_t readlen = ops->len;
1238 uint32_t oobreadlen = ops->ooblen;
1239 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
1240 mtd->oobavail : mtd->oobsize;
1242 uint8_t *bufpoi, *oob, *buf;
1243 unsigned int max_bitflips = 0;
1245 stats = mtd->ecc_stats;
1247 chipnr = (int)(from >> chip->chip_shift);
1248 chip->select_chip(mtd, chipnr);
1250 realpage = (int)(from >> chip->page_shift);
1251 page = realpage & chip->pagemask;
1253 col = (int)(from & (mtd->writesize - 1));
1257 oob_required = oob ? 1 : 0;
1262 bytes = min(mtd->writesize - col, readlen);
1263 aligned = (bytes == mtd->writesize);
1265 /* Is the current page in the buffer? */
1266 if (realpage != chip->pagebuf || oob) {
1267 bufpoi = aligned ? buf : chip->buffers->databuf;
1269 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1272 * Now read the page into the buffer. Absent an error,
1273 * the read methods return max bitflips per ecc step.
1275 if (unlikely(ops->mode == MTD_OPS_RAW))
1276 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1279 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1281 ret = chip->ecc.read_subpage(mtd, chip,
1282 col, bytes, bufpoi);
1284 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1285 oob_required, page);
1288 /* Invalidate page cache */
1293 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1295 /* Transfer not aligned data */
1297 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
1298 !(mtd->ecc_stats.failed - stats.failed) &&
1299 (ops->mode != MTD_OPS_RAW)) {
1300 chip->pagebuf = realpage;
1301 chip->pagebuf_bitflips = ret;
1303 /* Invalidate page cache */
1306 memcpy(buf, chip->buffers->databuf + col, bytes);
1311 if (unlikely(oob)) {
1312 int toread = min(oobreadlen, max_oobsize);
1315 oob = nand_transfer_oob(chip,
1317 oobreadlen -= toread;
1321 memcpy(buf, chip->buffers->databuf + col, bytes);
1323 max_bitflips = max_t(unsigned int, max_bitflips,
1324 chip->pagebuf_bitflips);
1332 /* For subsequent reads align to page boundary */
1334 /* Increment page address */
1337 page = realpage & chip->pagemask;
1338 /* Check, if we cross a chip boundary */
1341 chip->select_chip(mtd, -1);
1342 chip->select_chip(mtd, chipnr);
1346 ops->retlen = ops->len - (size_t) readlen;
1348 ops->oobretlen = ops->ooblen - oobreadlen;
1353 if (mtd->ecc_stats.failed - stats.failed)
1356 return max_bitflips;
1360 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
1361 * @mtd: MTD device structure
1362 * @from: offset to read from
1363 * @len: number of bytes to read
1364 * @retlen: pointer to variable to store the number of read bytes
1365 * @buf: the databuffer to put data
1367 * Get hold of the chip and call nand_do_read.
1369 static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1370 size_t *retlen, uint8_t *buf)
1372 struct nand_chip *chip = mtd->priv;
1373 struct mtd_oob_ops ops;
1376 nand_get_device(chip, mtd, FL_READING);
1380 ops.mode = MTD_OPS_PLACE_OOB;
1381 ret = nand_do_read_ops(mtd, from, &ops);
1382 *retlen = ops.retlen;
1383 nand_release_device(mtd);
1388 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1389 * @mtd: mtd info structure
1390 * @chip: nand chip info structure
1391 * @page: page number to read
1393 static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1396 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1397 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1402 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
1404 * @mtd: mtd info structure
1405 * @chip: nand chip info structure
1406 * @page: page number to read
1408 static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1411 uint8_t *buf = chip->oob_poi;
1412 int length = mtd->oobsize;
1413 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1414 int eccsize = chip->ecc.size;
1415 uint8_t *bufpoi = buf;
1416 int i, toread, sndrnd = 0, pos;
1418 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1419 for (i = 0; i < chip->ecc.steps; i++) {
1421 pos = eccsize + i * (eccsize + chunk);
1422 if (mtd->writesize > 512)
1423 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1425 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1428 toread = min_t(int, length, chunk);
1429 chip->read_buf(mtd, bufpoi, toread);
1434 chip->read_buf(mtd, bufpoi, length);
1440 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1441 * @mtd: mtd info structure
1442 * @chip: nand chip info structure
1443 * @page: page number to write
1445 static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1449 const uint8_t *buf = chip->oob_poi;
1450 int length = mtd->oobsize;
1452 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1453 chip->write_buf(mtd, buf, length);
1454 /* Send command to program the OOB data */
1455 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1457 status = chip->waitfunc(mtd, chip);
1459 return status & NAND_STATUS_FAIL ? -EIO : 0;
1463 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1464 * with syndrome - only for large page flash
1465 * @mtd: mtd info structure
1466 * @chip: nand chip info structure
1467 * @page: page number to write
1469 static int nand_write_oob_syndrome(struct mtd_info *mtd,
1470 struct nand_chip *chip, int page)
1472 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1473 int eccsize = chip->ecc.size, length = mtd->oobsize;
1474 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1475 const uint8_t *bufpoi = chip->oob_poi;
1478 * data-ecc-data-ecc ... ecc-oob
1480 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1482 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1483 pos = steps * (eccsize + chunk);
1488 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1489 for (i = 0; i < steps; i++) {
1491 if (mtd->writesize <= 512) {
1492 uint32_t fill = 0xFFFFFFFF;
1496 int num = min_t(int, len, 4);
1497 chip->write_buf(mtd, (uint8_t *)&fill,
1502 pos = eccsize + i * (eccsize + chunk);
1503 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1507 len = min_t(int, length, chunk);
1508 chip->write_buf(mtd, bufpoi, len);
1513 chip->write_buf(mtd, bufpoi, length);
1515 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1516 status = chip->waitfunc(mtd, chip);
1518 return status & NAND_STATUS_FAIL ? -EIO : 0;
1522 * nand_do_read_oob - [INTERN] NAND read out-of-band
1523 * @mtd: MTD device structure
1524 * @from: offset to read from
1525 * @ops: oob operations description structure
1527 * NAND read out-of-band data from the spare area.
1529 static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1530 struct mtd_oob_ops *ops)
1532 int page, realpage, chipnr;
1533 struct nand_chip *chip = mtd->priv;
1534 struct mtd_ecc_stats stats;
1535 int readlen = ops->ooblen;
1537 uint8_t *buf = ops->oobbuf;
1540 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
1541 __func__, (unsigned long long)from, readlen);
1543 stats = mtd->ecc_stats;
1545 if (ops->mode == MTD_OPS_AUTO_OOB)
1546 len = chip->ecc.layout->oobavail;
1550 if (unlikely(ops->ooboffs >= len)) {
1551 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
1552 "outside oob\n", __func__);
1556 /* Do not allow reads past end of device */
1557 if (unlikely(from >= mtd->size ||
1558 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1559 (from >> chip->page_shift)) * len)) {
1560 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
1561 "of device\n", __func__);
1565 chipnr = (int)(from >> chip->chip_shift);
1566 chip->select_chip(mtd, chipnr);
1568 /* Shift to get page */
1569 realpage = (int)(from >> chip->page_shift);
1570 page = realpage & chip->pagemask;
1574 if (ops->mode == MTD_OPS_RAW)
1575 ret = chip->ecc.read_oob_raw(mtd, chip, page);
1577 ret = chip->ecc.read_oob(mtd, chip, page);
1582 len = min(len, readlen);
1583 buf = nand_transfer_oob(chip, buf, ops, len);
1589 /* Increment page address */
1592 page = realpage & chip->pagemask;
1593 /* Check, if we cross a chip boundary */
1596 chip->select_chip(mtd, -1);
1597 chip->select_chip(mtd, chipnr);
1601 ops->oobretlen = ops->ooblen - readlen;
1606 if (mtd->ecc_stats.failed - stats.failed)
1609 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1613 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
1614 * @mtd: MTD device structure
1615 * @from: offset to read from
1616 * @ops: oob operation description structure
1618 * NAND read data and/or out-of-band data.
1620 static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1621 struct mtd_oob_ops *ops)
1623 struct nand_chip *chip = mtd->priv;
1624 int ret = -ENOTSUPP;
1628 /* Do not allow reads past end of device */
1629 if (ops->datbuf && (from + ops->len) > mtd->size) {
1630 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
1631 "beyond end of device\n", __func__);
1635 nand_get_device(chip, mtd, FL_READING);
1637 switch (ops->mode) {
1638 case MTD_OPS_PLACE_OOB:
1639 case MTD_OPS_AUTO_OOB:
1648 ret = nand_do_read_oob(mtd, from, ops);
1650 ret = nand_do_read_ops(mtd, from, ops);
1653 nand_release_device(mtd);
1659 * nand_write_page_raw - [INTERN] raw page write function
1660 * @mtd: mtd info structure
1661 * @chip: nand chip info structure
1663 * @oob_required: must write chip->oob_poi to OOB
1665 * Not for syndrome calculating ECC controllers, which use a special oob layout.
1667 static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1668 const uint8_t *buf, int oob_required)
1670 chip->write_buf(mtd, buf, mtd->writesize);
1672 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1678 * nand_write_page_raw_syndrome - [INTERN] raw page write function
1679 * @mtd: mtd info structure
1680 * @chip: nand chip info structure
1682 * @oob_required: must write chip->oob_poi to OOB
1684 * We need a special oob layout and handling even when ECC isn't checked.
1686 static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
1687 struct nand_chip *chip,
1688 const uint8_t *buf, int oob_required)
1690 int eccsize = chip->ecc.size;
1691 int eccbytes = chip->ecc.bytes;
1692 uint8_t *oob = chip->oob_poi;
1695 for (steps = chip->ecc.steps; steps > 0; steps--) {
1696 chip->write_buf(mtd, buf, eccsize);
1699 if (chip->ecc.prepad) {
1700 chip->write_buf(mtd, oob, chip->ecc.prepad);
1701 oob += chip->ecc.prepad;
1704 chip->read_buf(mtd, oob, eccbytes);
1707 if (chip->ecc.postpad) {
1708 chip->write_buf(mtd, oob, chip->ecc.postpad);
1709 oob += chip->ecc.postpad;
1713 size = mtd->oobsize - (oob - chip->oob_poi);
1715 chip->write_buf(mtd, oob, size);
1720 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
1721 * @mtd: mtd info structure
1722 * @chip: nand chip info structure
1724 * @oob_required: must write chip->oob_poi to OOB
1726 static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1727 const uint8_t *buf, int oob_required)
1729 int i, eccsize = chip->ecc.size;
1730 int eccbytes = chip->ecc.bytes;
1731 int eccsteps = chip->ecc.steps;
1732 uint8_t *ecc_calc = chip->buffers->ecccalc;
1733 const uint8_t *p = buf;
1734 uint32_t *eccpos = chip->ecc.layout->eccpos;
1736 /* Software ECC calculation */
1737 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1738 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1740 for (i = 0; i < chip->ecc.total; i++)
1741 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1743 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
1747 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
1748 * @mtd: mtd info structure
1749 * @chip: nand chip info structure
1751 * @oob_required: must write chip->oob_poi to OOB
1753 static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1754 const uint8_t *buf, int oob_required)
1756 int i, eccsize = chip->ecc.size;
1757 int eccbytes = chip->ecc.bytes;
1758 int eccsteps = chip->ecc.steps;
1759 uint8_t *ecc_calc = chip->buffers->ecccalc;
1760 const uint8_t *p = buf;
1761 uint32_t *eccpos = chip->ecc.layout->eccpos;
1763 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1764 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1765 chip->write_buf(mtd, p, eccsize);
1766 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1769 for (i = 0; i < chip->ecc.total; i++)
1770 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1772 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1778 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
1779 * @mtd: mtd info structure
1780 * @chip: nand chip info structure
1782 * @oob_required: must write chip->oob_poi to OOB
1784 * The hw generator calculates the error syndrome automatically. Therefore we
1785 * need a special oob layout and handling.
1787 static int nand_write_page_syndrome(struct mtd_info *mtd,
1788 struct nand_chip *chip,
1789 const uint8_t *buf, int oob_required)
1791 int i, eccsize = chip->ecc.size;
1792 int eccbytes = chip->ecc.bytes;
1793 int eccsteps = chip->ecc.steps;
1794 const uint8_t *p = buf;
1795 uint8_t *oob = chip->oob_poi;
1797 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1799 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1800 chip->write_buf(mtd, p, eccsize);
1802 if (chip->ecc.prepad) {
1803 chip->write_buf(mtd, oob, chip->ecc.prepad);
1804 oob += chip->ecc.prepad;
1807 chip->ecc.calculate(mtd, p, oob);
1808 chip->write_buf(mtd, oob, eccbytes);
1811 if (chip->ecc.postpad) {
1812 chip->write_buf(mtd, oob, chip->ecc.postpad);
1813 oob += chip->ecc.postpad;
1817 /* Calculate remaining oob bytes */
1818 i = mtd->oobsize - (oob - chip->oob_poi);
1820 chip->write_buf(mtd, oob, i);
1826 * nand_write_page - [REPLACEABLE] write one page
1827 * @mtd: MTD device structure
1828 * @chip: NAND chip descriptor
1829 * @buf: the data to write
1830 * @oob_required: must write chip->oob_poi to OOB
1831 * @page: page number to write
1832 * @cached: cached programming
1833 * @raw: use _raw version of write_page
1835 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1836 const uint8_t *buf, int oob_required, int page,
1837 int cached, int raw)
1841 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1844 status = chip->ecc.write_page_raw(mtd, chip, buf, oob_required);
1846 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
1852 * Cached progamming disabled for now. Not sure if it's worth the
1853 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
1857 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1859 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1860 status = chip->waitfunc(mtd, chip);
1862 * See if operation failed and additional status checks are
1865 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1866 status = chip->errstat(mtd, chip, FL_WRITING, status,
1869 if (status & NAND_STATUS_FAIL)
1872 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1873 status = chip->waitfunc(mtd, chip);
1876 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1877 /* Send command to read back the data */
1878 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1880 if (chip->verify_buf(mtd, buf, mtd->writesize))
1883 /* Make sure the next page prog is preceded by a status read */
1884 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1890 * nand_fill_oob - [INTERN] Transfer client buffer to oob
1891 * @mtd: MTD device structure
1892 * @oob: oob data buffer
1893 * @len: oob data write length
1894 * @ops: oob ops structure
1896 static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
1897 struct mtd_oob_ops *ops)
1899 struct nand_chip *chip = mtd->priv;
1902 * Initialise to all 0xFF, to avoid the possibility of left over OOB
1903 * data from a previous OOB read.
1905 memset(chip->oob_poi, 0xff, mtd->oobsize);
1907 switch (ops->mode) {
1909 case MTD_OPS_PLACE_OOB:
1911 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1914 case MTD_OPS_AUTO_OOB: {
1915 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1916 uint32_t boffs = 0, woffs = ops->ooboffs;
1919 for (; free->length && len; free++, len -= bytes) {
1920 /* Write request not from offset 0? */
1921 if (unlikely(woffs)) {
1922 if (woffs >= free->length) {
1923 woffs -= free->length;
1926 boffs = free->offset + woffs;
1927 bytes = min_t(size_t, len,
1928 (free->length - woffs));
1931 bytes = min_t(size_t, len, free->length);
1932 boffs = free->offset;
1934 memcpy(chip->oob_poi + boffs, oob, bytes);
1945 #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
1948 * nand_do_write_ops - [INTERN] NAND write with ECC
1949 * @mtd: MTD device structure
1950 * @to: offset to write to
1951 * @ops: oob operations description structure
1953 * NAND write with ECC.
1955 static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1956 struct mtd_oob_ops *ops)
1958 int chipnr, realpage, page, blockmask, column;
1959 struct nand_chip *chip = mtd->priv;
1960 uint32_t writelen = ops->len;
1962 uint32_t oobwritelen = ops->ooblen;
1963 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
1964 mtd->oobavail : mtd->oobsize;
1966 uint8_t *oob = ops->oobbuf;
1967 uint8_t *buf = ops->datbuf;
1969 int oob_required = oob ? 1 : 0;
1975 column = to & (mtd->writesize - 1);
1976 subpage = column || (writelen & (mtd->writesize - 1));
1981 chipnr = (int)(to >> chip->chip_shift);
1982 chip->select_chip(mtd, chipnr);
1984 /* Check, if it is write protected */
1985 if (nand_check_wp(mtd)) {
1986 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1990 realpage = (int)(to >> chip->page_shift);
1991 page = realpage & chip->pagemask;
1992 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1994 /* Invalidate the page cache, when we write to the cached page */
1995 if (to <= (chip->pagebuf << chip->page_shift) &&
1996 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1999 /* Don't allow multipage oob writes with offset */
2000 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
2006 int bytes = mtd->writesize;
2007 int cached = writelen > bytes && page != blockmask;
2008 uint8_t *wbuf = buf;
2010 /* Partial page write? */
2011 if (unlikely(column || writelen < mtd->writesize)) {
2013 bytes = min_t(int, bytes - column, (int) writelen);
2015 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2016 memcpy(&chip->buffers->databuf[column], buf, bytes);
2017 wbuf = chip->buffers->databuf;
2020 if (unlikely(oob)) {
2021 size_t len = min(oobwritelen, oobmaxlen);
2022 oob = nand_fill_oob(mtd, oob, len, ops);
2025 /* We still need to erase leftover OOB data */
2026 memset(chip->oob_poi, 0xff, mtd->oobsize);
2029 ret = chip->write_page(mtd, chip, wbuf, oob_required, page,
2030 cached, (ops->mode == MTD_OPS_RAW));
2042 page = realpage & chip->pagemask;
2043 /* Check, if we cross a chip boundary */
2046 chip->select_chip(mtd, -1);
2047 chip->select_chip(mtd, chipnr);
2051 ops->retlen = ops->len - writelen;
2053 ops->oobretlen = ops->ooblen;
2058 * nand_write - [MTD Interface] NAND write with ECC
2059 * @mtd: MTD device structure
2060 * @to: offset to write to
2061 * @len: number of bytes to write
2062 * @retlen: pointer to variable to store the number of written bytes
2063 * @buf: the data to write
2065 * NAND write with ECC.
2067 static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2068 size_t *retlen, const uint8_t *buf)
2070 struct nand_chip *chip = mtd->priv;
2071 struct mtd_oob_ops ops;
2074 nand_get_device(chip, mtd, FL_WRITING);
2076 ops.datbuf = (uint8_t *)buf;
2078 ops.mode = MTD_OPS_PLACE_OOB;
2079 ret = nand_do_write_ops(mtd, to, &ops);
2080 *retlen = ops.retlen;
2081 nand_release_device(mtd);
2086 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
2087 * @mtd: MTD device structure
2088 * @to: offset to write to
2089 * @ops: oob operation description structure
2091 * NAND write out-of-band.
2093 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2094 struct mtd_oob_ops *ops)
2096 int chipnr, page, status, len;
2097 struct nand_chip *chip = mtd->priv;
2099 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2100 __func__, (unsigned int)to, (int)ops->ooblen);
2102 if (ops->mode == MTD_OPS_AUTO_OOB)
2103 len = chip->ecc.layout->oobavail;
2107 /* Do not allow write past end of page */
2108 if ((ops->ooboffs + ops->ooblen) > len) {
2109 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
2110 "past end of page\n", __func__);
2114 if (unlikely(ops->ooboffs >= len)) {
2115 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
2116 "write outside oob\n", __func__);
2120 /* Do not allow write past end of device */
2121 if (unlikely(to >= mtd->size ||
2122 ops->ooboffs + ops->ooblen >
2123 ((mtd->size >> chip->page_shift) -
2124 (to >> chip->page_shift)) * len)) {
2125 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2126 "end of device\n", __func__);
2130 chipnr = (int)(to >> chip->chip_shift);
2131 chip->select_chip(mtd, chipnr);
2133 /* Shift to get page */
2134 page = (int)(to >> chip->page_shift);
2137 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2138 * of my DiskOnChip 2000 test units) will clear the whole data page too
2139 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2140 * it in the doc2000 driver in August 1999. dwmw2.
2142 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2144 /* Check, if it is write protected */
2145 if (nand_check_wp(mtd))
2148 /* Invalidate the page cache, if we write to the cached page */
2149 if (page == chip->pagebuf)
2152 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2154 if (ops->mode == MTD_OPS_RAW)
2155 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2157 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2162 ops->oobretlen = ops->ooblen;
2168 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2169 * @mtd: MTD device structure
2170 * @to: offset to write to
2171 * @ops: oob operation description structure
2173 static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2174 struct mtd_oob_ops *ops)
2176 struct nand_chip *chip = mtd->priv;
2177 int ret = -ENOTSUPP;
2181 /* Do not allow writes past end of device */
2182 if (ops->datbuf && (to + ops->len) > mtd->size) {
2183 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2184 "end of device\n", __func__);
2188 nand_get_device(chip, mtd, FL_WRITING);
2190 switch (ops->mode) {
2191 case MTD_OPS_PLACE_OOB:
2192 case MTD_OPS_AUTO_OOB:
2201 ret = nand_do_write_oob(mtd, to, ops);
2203 ret = nand_do_write_ops(mtd, to, ops);
2206 nand_release_device(mtd);
2211 * single_erase_cmd - [GENERIC] NAND standard block erase command function
2212 * @mtd: MTD device structure
2213 * @page: the page address of the block which will be erased
2215 * Standard erase command for NAND chips.
2217 static void single_erase_cmd(struct mtd_info *mtd, int page)
2219 struct nand_chip *chip = mtd->priv;
2220 /* Send commands to erase a block */
2221 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2222 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2226 * multi_erase_cmd - [GENERIC] AND specific block erase command function
2227 * @mtd: MTD device structure
2228 * @page: the page address of the block which will be erased
2230 * AND multi block erase command function. Erase 4 consecutive blocks.
2232 static void multi_erase_cmd(struct mtd_info *mtd, int page)
2234 struct nand_chip *chip = mtd->priv;
2235 /* Send commands to erase a block */
2236 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2237 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2238 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2239 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2240 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2244 * nand_erase - [MTD Interface] erase block(s)
2245 * @mtd: MTD device structure
2246 * @instr: erase instruction
2248 * Erase one ore more blocks.
2250 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
2252 return nand_erase_nand(mtd, instr, 0);
2255 #define BBT_PAGE_MASK 0xffffff3f
2257 * nand_erase_nand - [INTERN] erase block(s)
2258 * @mtd: MTD device structure
2259 * @instr: erase instruction
2260 * @allowbbt: allow erasing the bbt area
2262 * Erase one ore more blocks.
2264 int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2267 int page, status, pages_per_block, ret, chipnr;
2268 struct nand_chip *chip = mtd->priv;
2269 loff_t rewrite_bbt[CONFIG_SYS_NAND_MAX_CHIPS] = {0};
2270 unsigned int bbt_masked_page = 0xffffffff;
2273 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
2274 __func__, (unsigned long long)instr->addr,
2275 (unsigned long long)instr->len);
2277 if (check_offs_len(mtd, instr->addr, instr->len))
2280 /* Grab the lock and see if the device is available */
2281 nand_get_device(chip, mtd, FL_ERASING);
2283 /* Shift to get first page */
2284 page = (int)(instr->addr >> chip->page_shift);
2285 chipnr = (int)(instr->addr >> chip->chip_shift);
2287 /* Calculate pages in each block */
2288 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
2290 /* Select the NAND device */
2291 chip->select_chip(mtd, chipnr);
2293 /* Check, if it is write protected */
2294 if (nand_check_wp(mtd)) {
2295 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
2297 instr->state = MTD_ERASE_FAILED;
2302 * If BBT requires refresh, set the BBT page mask to see if the BBT
2303 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2304 * can not be matched. This is also done when the bbt is actually
2305 * erased to avoid recursive updates.
2307 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2308 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2310 /* Loop through the pages */
2313 instr->state = MTD_ERASING;
2317 /* Check if we have a bad block, we do not erase bad blocks! */
2318 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
2319 chip->page_shift, 0, allowbbt)) {
2320 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2322 instr->state = MTD_ERASE_FAILED;
2327 * Invalidate the page cache, if we erase the block which
2328 * contains the current cached page.
2330 if (page <= chip->pagebuf && chip->pagebuf <
2331 (page + pages_per_block))
2334 chip->erase_cmd(mtd, page & chip->pagemask);
2336 status = chip->waitfunc(mtd, chip);
2339 * See if operation failed and additional status checks are
2342 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2343 status = chip->errstat(mtd, chip, FL_ERASING,
2346 /* See if block erase succeeded */
2347 if (status & NAND_STATUS_FAIL) {
2348 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
2349 "page 0x%08x\n", __func__, page);
2350 instr->state = MTD_ERASE_FAILED;
2352 ((loff_t)page << chip->page_shift);
2357 * If BBT requires refresh, set the BBT rewrite flag to the
2358 * page being erased.
2360 if (bbt_masked_page != 0xffffffff &&
2361 (page & BBT_PAGE_MASK) == bbt_masked_page)
2362 rewrite_bbt[chipnr] =
2363 ((loff_t)page << chip->page_shift);
2365 /* Increment page address and decrement length */
2366 len -= (1 << chip->phys_erase_shift);
2367 page += pages_per_block;
2369 /* Check, if we cross a chip boundary */
2370 if (len && !(page & chip->pagemask)) {
2372 chip->select_chip(mtd, -1);
2373 chip->select_chip(mtd, chipnr);
2376 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2377 * page mask to see if this BBT should be rewritten.
2379 if (bbt_masked_page != 0xffffffff &&
2380 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2381 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2385 instr->state = MTD_ERASE_DONE;
2389 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2391 /* Deselect and wake up anyone waiting on the device */
2392 nand_release_device(mtd);
2394 /* Do call back function */
2396 mtd_erase_callback(instr);
2399 * If BBT requires refresh and erase was successful, rewrite any
2400 * selected bad block tables.
2402 if (bbt_masked_page == 0xffffffff || ret)
2405 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2406 if (!rewrite_bbt[chipnr])
2408 /* Update the BBT for chip */
2409 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
2410 "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
2411 rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
2412 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2415 /* Return more or less happy */
2420 * nand_sync - [MTD Interface] sync
2421 * @mtd: MTD device structure
2423 * Sync is actually a wait for chip ready function.
2425 static void nand_sync(struct mtd_info *mtd)
2427 struct nand_chip *chip = mtd->priv;
2429 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
2431 /* Grab the lock and see if the device is available */
2432 nand_get_device(chip, mtd, FL_SYNCING);
2433 /* Release it and go back */
2434 nand_release_device(mtd);
2438 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
2439 * @mtd: MTD device structure
2440 * @offs: offset relative to mtd start
2442 static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
2444 return nand_block_checkbad(mtd, offs, 1, 0);
2448 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
2449 * @mtd: MTD device structure
2450 * @ofs: offset relative to mtd start
2452 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2454 struct nand_chip *chip = mtd->priv;
2457 ret = nand_block_isbad(mtd, ofs);
2459 /* If it was bad already, return success and do nothing */
2465 return chip->block_markbad(mtd, ofs);
2469 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2470 * @mtd: MTD device structure
2471 * @chip: nand chip info structure
2472 * @addr: feature address.
2473 * @subfeature_param: the subfeature parameters, a four bytes array.
2475 static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2476 int addr, uint8_t *subfeature_param)
2480 if (!chip->onfi_version)
2483 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2484 chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2485 status = chip->waitfunc(mtd, chip);
2486 if (status & NAND_STATUS_FAIL)
2492 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2493 * @mtd: MTD device structure
2494 * @chip: nand chip info structure
2495 * @addr: feature address.
2496 * @subfeature_param: the subfeature parameters, a four bytes array.
2498 static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2499 int addr, uint8_t *subfeature_param)
2501 if (!chip->onfi_version)
2504 /* clear the sub feature parameters */
2505 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2507 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2508 chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2512 /* Set default functions */
2513 static void nand_set_defaults(struct nand_chip *chip, int busw)
2515 /* check for proper chip_delay setup, set 20us if not */
2516 if (!chip->chip_delay)
2517 chip->chip_delay = 20;
2519 /* check, if a user supplied command function given */
2520 if (chip->cmdfunc == NULL)
2521 chip->cmdfunc = nand_command;
2523 /* check, if a user supplied wait function given */
2524 if (chip->waitfunc == NULL)
2525 chip->waitfunc = nand_wait;
2527 if (!chip->select_chip)
2528 chip->select_chip = nand_select_chip;
2529 if (!chip->read_byte)
2530 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2531 if (!chip->read_word)
2532 chip->read_word = nand_read_word;
2533 if (!chip->block_bad)
2534 chip->block_bad = nand_block_bad;
2535 if (!chip->block_markbad)
2536 chip->block_markbad = nand_default_block_markbad;
2537 if (!chip->write_buf)
2538 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2539 if (!chip->read_buf)
2540 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2541 if (!chip->verify_buf)
2542 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2543 if (!chip->scan_bbt)
2544 chip->scan_bbt = nand_default_bbt;
2545 if (!chip->controller)
2546 chip->controller = &chip->hwcontrol;
2549 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2550 /* Sanitize ONFI strings so we can safely print them */
2551 static void sanitize_string(char *s, size_t len)
2555 /* Null terminate */
2558 /* Remove non printable chars */
2559 for (i = 0; i < len - 1; i++) {
2560 if (s[i] < ' ' || s[i] > 127)
2564 /* Remove trailing spaces */
2568 static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2573 for (i = 0; i < 8; i++)
2574 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2581 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
2583 static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
2586 struct nand_onfi_params *p = &chip->onfi_params;
2590 /* Try ONFI for unknown chip or LP */
2591 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2592 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2593 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2596 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2597 for (i = 0; i < 3; i++) {
2598 for (j = 0; j < sizeof(*p); j++)
2599 ((uint8_t *)p)[j] = chip->read_byte(mtd);
2600 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
2601 le16_to_cpu(p->crc)) {
2602 pr_info("ONFI param page %d valid\n", i);
2611 val = le16_to_cpu(p->revision);
2613 chip->onfi_version = 23;
2614 else if (val & (1 << 4))
2615 chip->onfi_version = 22;
2616 else if (val & (1 << 3))
2617 chip->onfi_version = 21;
2618 else if (val & (1 << 2))
2619 chip->onfi_version = 20;
2620 else if (val & (1 << 1))
2621 chip->onfi_version = 10;
2623 chip->onfi_version = 0;
2625 if (!chip->onfi_version) {
2626 pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
2630 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2631 sanitize_string(p->model, sizeof(p->model));
2633 mtd->name = p->model;
2634 mtd->writesize = le32_to_cpu(p->byte_per_page);
2635 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2636 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
2637 chip->chipsize = le32_to_cpu(p->blocks_per_lun);
2638 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
2640 if (le16_to_cpu(p->features) & 1)
2641 *busw = NAND_BUSWIDTH_16;
2643 pr_info("ONFI flash detected\n");
2647 static inline int nand_flash_detect_onfi(struct mtd_info *mtd,
2648 struct nand_chip *chip,
2656 * nand_id_has_period - Check if an ID string has a given wraparound period
2657 * @id_data: the ID string
2658 * @arrlen: the length of the @id_data array
2659 * @period: the period of repitition
2661 * Check if an ID string is repeated within a given sequence of bytes at
2662 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
2663 * period of 2). This is a helper function for nand_id_len(). Returns non-zero
2664 * if the repetition has a period of @period; otherwise, returns zero.
2666 static int nand_id_has_period(u8 *id_data, int arrlen, int period)
2669 for (i = 0; i < period; i++)
2670 for (j = i + period; j < arrlen; j += period)
2671 if (id_data[i] != id_data[j])
2677 * nand_id_len - Get the length of an ID string returned by CMD_READID
2678 * @id_data: the ID string
2679 * @arrlen: the length of the @id_data array
2681 * Returns the length of the ID string, according to known wraparound/trailing
2682 * zero patterns. If no pattern exists, returns the length of the array.
2684 static int nand_id_len(u8 *id_data, int arrlen)
2686 int last_nonzero, period;
2688 /* Find last non-zero byte */
2689 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
2690 if (id_data[last_nonzero])
2694 if (last_nonzero < 0)
2697 /* Calculate wraparound period */
2698 for (period = 1; period < arrlen; period++)
2699 if (nand_id_has_period(id_data, arrlen, period))
2702 /* There's a repeated pattern */
2703 if (period < arrlen)
2706 /* There are trailing zeros */
2707 if (last_nonzero < arrlen - 1)
2708 return last_nonzero + 1;
2710 /* No pattern detected */
2715 * Many new NAND share similar device ID codes, which represent the size of the
2716 * chip. The rest of the parameters must be decoded according to generic or
2717 * manufacturer-specific "extended ID" decoding patterns.
2719 static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
2720 u8 id_data[8], int *busw)
2723 /* The 3rd id byte holds MLC / multichip data */
2724 chip->cellinfo = id_data[2];
2725 /* The 4th id byte is the important one */
2728 id_len = nand_id_len(id_data, 8);
2731 * Field definitions are in the following datasheets:
2732 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
2733 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
2734 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
2736 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
2737 * ID to decide what to do.
2739 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
2740 (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2741 id_data[5] != 0x00) {
2743 mtd->writesize = 2048 << (extid & 0x03);
2746 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
2763 default: /* Other cases are "reserved" (unknown) */
2768 /* Calc blocksize */
2769 mtd->erasesize = (128 * 1024) <<
2770 (((extid >> 1) & 0x04) | (extid & 0x03));
2772 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
2773 (chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2777 mtd->writesize = 2048 << (extid & 0x03);
2780 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
2804 /* Calc blocksize */
2805 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
2807 mtd->erasesize = (128 * 1024) << tmp;
2808 else if (tmp == 0x03)
2809 mtd->erasesize = 768 * 1024;
2811 mtd->erasesize = (64 * 1024) << tmp;
2815 mtd->writesize = 1024 << (extid & 0x03);
2818 mtd->oobsize = (8 << (extid & 0x01)) *
2819 (mtd->writesize >> 9);
2821 /* Calc blocksize. Blocksize is multiples of 64KiB */
2822 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2824 /* Get buswidth information */
2825 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2830 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
2831 * decodes a matching ID table entry and assigns the MTD size parameters for
2834 static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
2835 const struct nand_flash_dev *type, u8 id_data[8],
2838 int maf_id = id_data[0];
2840 mtd->erasesize = type->erasesize;
2841 mtd->writesize = type->pagesize;
2842 mtd->oobsize = mtd->writesize / 32;
2843 *busw = type->options & NAND_BUSWIDTH_16;
2846 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
2847 * some Spansion chips have erasesize that conflicts with size
2848 * listed in nand_ids table.
2849 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
2851 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
2852 && id_data[6] == 0x00 && id_data[7] == 0x00
2853 && mtd->writesize == 512) {
2854 mtd->erasesize = 128 * 1024;
2855 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
2860 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
2861 * heuristic patterns using various detected parameters (e.g., manufacturer,
2862 * page size, cell-type information).
2864 static void nand_decode_bbm_options(struct mtd_info *mtd,
2865 struct nand_chip *chip, u8 id_data[8])
2867 int maf_id = id_data[0];
2869 /* Set the bad block position */
2870 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
2871 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
2873 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
2876 * Bad block marker is stored in the last page of each block on Samsung
2877 * and Hynix MLC devices; stored in first two pages of each block on
2878 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
2879 * AMD/Spansion, and Macronix. All others scan only the first page.
2881 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2882 (maf_id == NAND_MFR_SAMSUNG ||
2883 maf_id == NAND_MFR_HYNIX))
2884 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
2885 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2886 (maf_id == NAND_MFR_SAMSUNG ||
2887 maf_id == NAND_MFR_HYNIX ||
2888 maf_id == NAND_MFR_TOSHIBA ||
2889 maf_id == NAND_MFR_AMD ||
2890 maf_id == NAND_MFR_MACRONIX)) ||
2891 (mtd->writesize == 2048 &&
2892 maf_id == NAND_MFR_MICRON))
2893 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
2897 * Get the flash and manufacturer id and lookup if the type is supported.
2899 static const struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2900 struct nand_chip *chip,
2902 int *maf_id, int *dev_id,
2903 const struct nand_flash_dev *type)
2909 /* Select the device */
2910 chip->select_chip(mtd, 0);
2913 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2916 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2918 /* Send the command for reading device ID */
2919 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2921 /* Read manufacturer and device IDs */
2922 *maf_id = chip->read_byte(mtd);
2923 *dev_id = chip->read_byte(mtd);
2926 * Try again to make sure, as some systems the bus-hold or other
2927 * interface concerns can cause random data which looks like a
2928 * possibly credible NAND flash to appear. If the two results do
2929 * not match, ignore the device completely.
2932 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2934 /* Read entire ID string */
2935 for (i = 0; i < 8; i++)
2936 id_data[i] = chip->read_byte(mtd);
2938 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
2939 pr_info("%s: second ID read did not match "
2940 "%02x,%02x against %02x,%02x\n", __func__,
2941 *maf_id, *dev_id, id_data[0], id_data[1]);
2942 return ERR_PTR(-ENODEV);
2946 type = nand_flash_ids;
2948 for (; type->name != NULL; type++)
2949 if (*dev_id == type->id)
2952 chip->onfi_version = 0;
2953 if (!type->name || !type->pagesize) {
2954 /* Check is chip is ONFI compliant */
2955 if (nand_flash_detect_onfi(mtd, chip, &busw))
2960 return ERR_PTR(-ENODEV);
2963 mtd->name = type->name;
2965 chip->chipsize = (uint64_t)type->chipsize << 20;
2967 if (!type->pagesize && chip->init_size) {
2968 /* Set the pagesize, oobsize, erasesize by the driver */
2969 busw = chip->init_size(mtd, chip, id_data);
2970 } else if (!type->pagesize) {
2971 /* Decode parameters from extended ID */
2972 nand_decode_ext_id(mtd, chip, id_data, &busw);
2974 nand_decode_id(mtd, chip, type, id_data, &busw);
2976 /* Get chip options, preserve non chip based options */
2977 chip->options |= type->options;
2980 * Check if chip is not a Samsung device. Do not clear the
2981 * options for chips which do not have an extended id.
2983 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2984 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2987 /* Try to identify manufacturer */
2988 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2989 if (nand_manuf_ids[maf_idx].id == *maf_id)
2994 * Check, if buswidth is correct. Hardware drivers should set
2997 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2998 pr_info("NAND device: Manufacturer ID:"
2999 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
3000 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
3001 pr_warn("NAND bus width %d instead %d bit\n",
3002 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3004 return ERR_PTR(-EINVAL);
3007 nand_decode_bbm_options(mtd, chip, id_data);
3009 /* Calculate the address shift from the page size */
3010 chip->page_shift = ffs(mtd->writesize) - 1;
3011 /* Convert chipsize to number of pages per chip -1 */
3012 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
3014 chip->bbt_erase_shift = chip->phys_erase_shift =
3015 ffs(mtd->erasesize) - 1;
3016 if (chip->chipsize & 0xffffffff)
3017 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
3019 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3020 chip->chip_shift += 32 - 1;
3023 chip->badblockbits = 8;
3025 /* Check for AND chips with 4 page planes */
3026 if (chip->options & NAND_4PAGE_ARRAY)
3027 chip->erase_cmd = multi_erase_cmd;
3029 chip->erase_cmd = single_erase_cmd;
3031 /* Do not replace user supplied command function! */
3032 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3033 chip->cmdfunc = nand_command_lp;
3036 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3037 if (chip->onfi_version)
3038 name = chip->onfi_params.model;
3040 pr_info("NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s),"
3041 " page size: %d, OOB size: %d\n",
3042 *maf_id, *dev_id, nand_manuf_ids[maf_idx].name,
3044 mtd->writesize, mtd->oobsize);
3050 * nand_scan_ident - [NAND Interface] Scan for the NAND device
3051 * @mtd: MTD device structure
3052 * @maxchips: number of chips to scan for
3053 * @table: alternative NAND ID table
3055 * This is the first phase of the normal nand_scan() function. It reads the
3056 * flash ID and sets up MTD fields accordingly.
3058 * The mtd->owner field must be set to the module of the caller.
3060 int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3061 const struct nand_flash_dev *table)
3063 int i, busw, nand_maf_id, nand_dev_id;
3064 struct nand_chip *chip = mtd->priv;
3065 const struct nand_flash_dev *type;
3067 /* Get buswidth to select the correct functions */
3068 busw = chip->options & NAND_BUSWIDTH_16;
3069 /* Set the default functions */
3070 nand_set_defaults(chip, busw);
3072 /* Read the flash type */
3073 type = nand_get_flash_type(mtd, chip, busw,
3074 &nand_maf_id, &nand_dev_id, table);
3077 #ifndef CONFIG_SYS_NAND_QUIET_TEST
3078 pr_warn("No NAND device found\n");
3080 chip->select_chip(mtd, -1);
3081 return PTR_ERR(type);
3084 /* Check for a chip array */
3085 for (i = 1; i < maxchips; i++) {
3086 chip->select_chip(mtd, i);
3087 /* See comment in nand_get_flash_type for reset */
3088 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3089 /* Send the command for reading device ID */
3090 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3091 /* Read manufacturer and device IDs */
3092 if (nand_maf_id != chip->read_byte(mtd) ||
3093 nand_dev_id != chip->read_byte(mtd))
3098 pr_info("%d NAND chips detected\n", i);
3101 /* Store the number of chips and calc total size for mtd */
3103 mtd->size = i * chip->chipsize;
3110 * nand_scan_tail - [NAND Interface] Scan for the NAND device
3111 * @mtd: MTD device structure
3113 * This is the second phase of the normal nand_scan() function. It fills out
3114 * all the uninitialized function pointers with the defaults and scans for a
3115 * bad block table if appropriate.
3117 int nand_scan_tail(struct mtd_info *mtd)
3120 struct nand_chip *chip = mtd->priv;
3122 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3123 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3124 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3126 if (!(chip->options & NAND_OWN_BUFFERS))
3127 chip->buffers = memalign(ARCH_DMA_MINALIGN,
3128 sizeof(*chip->buffers));
3132 /* Set the internal oob buffer location, just after the page data */
3133 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
3136 * If no default placement scheme is given, select an appropriate one.
3138 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
3139 switch (mtd->oobsize) {
3141 chip->ecc.layout = &nand_oob_8;
3144 chip->ecc.layout = &nand_oob_16;
3147 chip->ecc.layout = &nand_oob_64;
3150 chip->ecc.layout = &nand_oob_128;
3153 pr_warn("No oob scheme defined for oobsize %d\n",
3158 if (!chip->write_page)
3159 chip->write_page = nand_write_page;
3161 /* set for ONFI nand */
3162 if (!chip->onfi_set_features)
3163 chip->onfi_set_features = nand_onfi_set_features;
3164 if (!chip->onfi_get_features)
3165 chip->onfi_get_features = nand_onfi_get_features;
3168 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
3169 * selected and we have 256 byte pagesize fallback to software ECC
3172 switch (chip->ecc.mode) {
3173 case NAND_ECC_HW_OOB_FIRST:
3174 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3175 if (!chip->ecc.calculate || !chip->ecc.correct ||
3177 pr_warn("No ECC functions supplied; "
3178 "hardware ECC not possible\n");
3181 if (!chip->ecc.read_page)
3182 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3185 /* Use standard hwecc read page function? */
3186 if (!chip->ecc.read_page)
3187 chip->ecc.read_page = nand_read_page_hwecc;
3188 if (!chip->ecc.write_page)
3189 chip->ecc.write_page = nand_write_page_hwecc;
3190 if (!chip->ecc.read_page_raw)
3191 chip->ecc.read_page_raw = nand_read_page_raw;
3192 if (!chip->ecc.write_page_raw)
3193 chip->ecc.write_page_raw = nand_write_page_raw;
3194 if (!chip->ecc.read_oob)
3195 chip->ecc.read_oob = nand_read_oob_std;
3196 if (!chip->ecc.write_oob)
3197 chip->ecc.write_oob = nand_write_oob_std;
3199 case NAND_ECC_HW_SYNDROME:
3200 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3201 !chip->ecc.hwctl) &&
3202 (!chip->ecc.read_page ||
3203 chip->ecc.read_page == nand_read_page_hwecc ||
3204 !chip->ecc.write_page ||
3205 chip->ecc.write_page == nand_write_page_hwecc)) {
3206 pr_warn("No ECC functions supplied; "
3207 "hardware ECC not possible\n");
3210 /* Use standard syndrome read/write page function? */
3211 if (!chip->ecc.read_page)
3212 chip->ecc.read_page = nand_read_page_syndrome;
3213 if (!chip->ecc.write_page)
3214 chip->ecc.write_page = nand_write_page_syndrome;
3215 if (!chip->ecc.read_page_raw)
3216 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3217 if (!chip->ecc.write_page_raw)
3218 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
3219 if (!chip->ecc.read_oob)
3220 chip->ecc.read_oob = nand_read_oob_syndrome;
3221 if (!chip->ecc.write_oob)
3222 chip->ecc.write_oob = nand_write_oob_syndrome;
3224 if (mtd->writesize >= chip->ecc.size) {
3225 if (!chip->ecc.strength) {
3226 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3231 pr_warn("%d byte HW ECC not possible on "
3232 "%d byte page size, fallback to SW ECC\n",
3233 chip->ecc.size, mtd->writesize);
3234 chip->ecc.mode = NAND_ECC_SOFT;
3237 chip->ecc.calculate = nand_calculate_ecc;
3238 chip->ecc.correct = nand_correct_data;
3239 chip->ecc.read_page = nand_read_page_swecc;
3240 chip->ecc.read_subpage = nand_read_subpage;
3241 chip->ecc.write_page = nand_write_page_swecc;
3242 chip->ecc.read_page_raw = nand_read_page_raw;
3243 chip->ecc.write_page_raw = nand_write_page_raw;
3244 chip->ecc.read_oob = nand_read_oob_std;
3245 chip->ecc.write_oob = nand_write_oob_std;
3246 if (!chip->ecc.size)
3247 chip->ecc.size = 256;
3248 chip->ecc.bytes = 3;
3249 chip->ecc.strength = 1;
3252 case NAND_ECC_SOFT_BCH:
3253 if (!mtd_nand_has_bch()) {
3254 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
3257 chip->ecc.calculate = nand_bch_calculate_ecc;
3258 chip->ecc.correct = nand_bch_correct_data;
3259 chip->ecc.read_page = nand_read_page_swecc;
3260 chip->ecc.read_subpage = nand_read_subpage;
3261 chip->ecc.write_page = nand_write_page_swecc;
3262 chip->ecc.read_page_raw = nand_read_page_raw;
3263 chip->ecc.write_page_raw = nand_write_page_raw;
3264 chip->ecc.read_oob = nand_read_oob_std;
3265 chip->ecc.write_oob = nand_write_oob_std;
3267 * Board driver should supply ecc.size and ecc.bytes values to
3268 * select how many bits are correctable; see nand_bch_init()
3269 * for details. Otherwise, default to 4 bits for large page
3272 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
3273 chip->ecc.size = 512;
3274 chip->ecc.bytes = 7;
3276 chip->ecc.priv = nand_bch_init(mtd,
3280 if (!chip->ecc.priv)
3281 pr_warn("BCH ECC initialization failed!\n");
3282 chip->ecc.strength =
3283 chip->ecc.bytes * 8 / fls(8 * chip->ecc.size);
3287 pr_warn("NAND_ECC_NONE selected by board driver. "
3288 "This is not recommended !!\n");
3289 chip->ecc.read_page = nand_read_page_raw;
3290 chip->ecc.write_page = nand_write_page_raw;
3291 chip->ecc.read_oob = nand_read_oob_std;
3292 chip->ecc.read_page_raw = nand_read_page_raw;
3293 chip->ecc.write_page_raw = nand_write_page_raw;
3294 chip->ecc.write_oob = nand_write_oob_std;
3295 chip->ecc.size = mtd->writesize;
3296 chip->ecc.bytes = 0;
3300 pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
3304 /* For many systems, the standard OOB write also works for raw */
3305 if (!chip->ecc.read_oob_raw)
3306 chip->ecc.read_oob_raw = chip->ecc.read_oob;
3307 if (!chip->ecc.write_oob_raw)
3308 chip->ecc.write_oob_raw = chip->ecc.write_oob;
3311 * The number of bytes available for a client to place data into
3312 * the out of band area.
3314 chip->ecc.layout->oobavail = 0;
3315 for (i = 0; chip->ecc.layout->oobfree[i].length
3316 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
3317 chip->ecc.layout->oobavail +=
3318 chip->ecc.layout->oobfree[i].length;
3319 mtd->oobavail = chip->ecc.layout->oobavail;
3322 * Set the number of read / write steps for one page depending on ECC
3325 chip->ecc.steps = mtd->writesize / chip->ecc.size;
3326 if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
3327 pr_warn("Invalid ECC parameters\n");
3330 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
3332 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
3333 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3334 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
3335 switch (chip->ecc.steps) {
3337 mtd->subpage_sft = 1;
3342 mtd->subpage_sft = 2;
3346 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3348 /* Initialize state */
3349 chip->state = FL_READY;
3351 /* De-select the device */
3352 chip->select_chip(mtd, -1);
3354 /* Invalidate the pagebuffer reference */
3357 /* Large page NAND with SOFT_ECC should support subpage reads */
3358 if ((chip->ecc.mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
3359 chip->options |= NAND_SUBPAGE_READ;
3361 /* Fill in remaining MTD driver data */
3362 mtd->type = MTD_NANDFLASH;
3363 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3365 mtd->_erase = nand_erase;
3367 mtd->_unpoint = NULL;
3368 mtd->_read = nand_read;
3369 mtd->_write = nand_write;
3370 mtd->_read_oob = nand_read_oob;
3371 mtd->_write_oob = nand_write_oob;
3372 mtd->_sync = nand_sync;
3374 mtd->_unlock = NULL;
3375 mtd->_block_isbad = nand_block_isbad;
3376 mtd->_block_markbad = nand_block_markbad;
3378 /* propagate ecc info to mtd_info */
3379 mtd->ecclayout = chip->ecc.layout;
3380 mtd->ecc_strength = chip->ecc.strength;
3382 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3383 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3386 if (!mtd->bitflip_threshold)
3387 mtd->bitflip_threshold = mtd->ecc_strength;
3389 /* Check, if we should skip the bad block table scan */
3390 if (chip->options & NAND_SKIP_BBTSCAN)
3391 chip->options |= NAND_BBT_SCANNED;
3397 * nand_scan - [NAND Interface] Scan for the NAND device
3398 * @mtd: MTD device structure
3399 * @maxchips: number of chips to scan for
3401 * This fills out all the uninitialized function pointers with the defaults.
3402 * The flash ID is read and the mtd/chip structures are filled with the
3403 * appropriate values. The mtd->owner field must be set to the module of the
3406 int nand_scan(struct mtd_info *mtd, int maxchips)
3410 ret = nand_scan_ident(mtd, maxchips, NULL);
3412 ret = nand_scan_tail(mtd);
3417 * nand_release - [NAND Interface] Free resources held by the NAND device
3418 * @mtd: MTD device structure
3420 void nand_release(struct mtd_info *mtd)
3422 struct nand_chip *chip = mtd->priv;
3424 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3425 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3427 #ifdef CONFIG_MTD_PARTITIONS
3428 /* Deregister partitions */
3429 del_mtd_partitions(mtd);
3432 /* Free bad block table memory */
3434 if (!(chip->options & NAND_OWN_BUFFERS))
3435 kfree(chip->buffers);
3437 /* Free bad block descriptor memory */
3438 if (chip->badblock_pattern && chip->badblock_pattern->options
3439 & NAND_BBT_DYNAMICSTRUCT)
3440 kfree(chip->badblock_pattern);