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.
8 * Additional technical information is available on
9 * http://www.linux-mtd.infradead.org/doc/nand.html
11 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
12 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
15 * David Woodhouse for adding multichip support
17 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
18 * rework for 2K page size chips
21 * Enable cached programming for 2k page size chips
22 * Check, if mtd->ecctype should be set to MTD_ECC_HW
23 * if we have HW ECC support.
24 * BBT table is not serialized, has to be fixed
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License version 2 as
28 * published by the Free Software Foundation.
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 #include <linux/err.h>
37 #include <linux/compat.h>
38 #include <linux/mtd/mtd.h>
39 #include <linux/mtd/nand.h>
40 #include <linux/mtd/nand_ecc.h>
41 #include <linux/mtd/nand_bch.h>
42 #ifdef CONFIG_MTD_PARTITIONS
43 #include <linux/mtd/partitions.h>
46 #include <asm/errno.h>
49 * CONFIG_SYS_NAND_RESET_CNT is used as a timeout mechanism when resetting
50 * a flash. NAND flash is initialized prior to interrupts so standard timers
51 * can't be used. CONFIG_SYS_NAND_RESET_CNT should be set to a value
52 * which is greater than (max NAND reset time / NAND status read time).
53 * A conservative default of 200000 (500 us / 25 ns) is used as a default.
55 #ifndef CONFIG_SYS_NAND_RESET_CNT
56 #define CONFIG_SYS_NAND_RESET_CNT 200000
59 static bool is_module_text_address(unsigned long addr) {return 0;}
61 /* Define default oob placement schemes for large and small page devices */
62 static struct nand_ecclayout nand_oob_8 = {
72 static struct nand_ecclayout nand_oob_16 = {
74 .eccpos = {0, 1, 2, 3, 6, 7},
80 static struct nand_ecclayout nand_oob_64 = {
83 40, 41, 42, 43, 44, 45, 46, 47,
84 48, 49, 50, 51, 52, 53, 54, 55,
85 56, 57, 58, 59, 60, 61, 62, 63},
91 static struct nand_ecclayout nand_oob_128 = {
94 80, 81, 82, 83, 84, 85, 86, 87,
95 88, 89, 90, 91, 92, 93, 94, 95,
96 96, 97, 98, 99, 100, 101, 102, 103,
97 104, 105, 106, 107, 108, 109, 110, 111,
98 112, 113, 114, 115, 116, 117, 118, 119,
99 120, 121, 122, 123, 124, 125, 126, 127},
105 static int nand_get_device(struct mtd_info *mtd, int new_state);
107 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
108 struct mtd_oob_ops *ops);
111 * For devices which display every fart in the system on a separate LED. Is
112 * compiled away when LED support is disabled.
114 DEFINE_LED_TRIGGER(nand_led_trigger);
116 static int check_offs_len(struct mtd_info *mtd,
117 loff_t ofs, uint64_t len)
119 struct nand_chip *chip = mtd->priv;
122 /* Start address must align on block boundary */
123 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
124 pr_debug("%s: unaligned address\n", __func__);
128 /* Length must align on block boundary */
129 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
130 pr_debug("%s: length not block aligned\n", __func__);
138 * nand_release_device - [GENERIC] release chip
139 * @mtd: MTD device structure
141 * Release chip lock and wake up anyone waiting on the device.
143 static void nand_release_device(struct mtd_info *mtd)
145 struct nand_chip *chip = mtd->priv;
147 /* De-select the NAND device */
148 chip->select_chip(mtd, -1);
152 * nand_read_byte - [DEFAULT] read one byte from the chip
153 * @mtd: MTD device structure
155 * Default read function for 8bit buswidth
157 uint8_t nand_read_byte(struct mtd_info *mtd)
159 struct nand_chip *chip = mtd->priv;
160 return readb(chip->IO_ADDR_R);
164 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
165 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
166 * @mtd: MTD device structure
168 * Default read function for 16bit buswidth with endianness conversion.
171 static uint8_t nand_read_byte16(struct mtd_info *mtd)
173 struct nand_chip *chip = mtd->priv;
174 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
178 * nand_read_word - [DEFAULT] read one word from the chip
179 * @mtd: MTD device structure
181 * Default read function for 16bit buswidth without endianness conversion.
183 static u16 nand_read_word(struct mtd_info *mtd)
185 struct nand_chip *chip = mtd->priv;
186 return readw(chip->IO_ADDR_R);
190 * nand_select_chip - [DEFAULT] control CE line
191 * @mtd: MTD device structure
192 * @chipnr: chipnumber to select, -1 for deselect
194 * Default select function for 1 chip devices.
196 static void nand_select_chip(struct mtd_info *mtd, int chipnr)
198 struct nand_chip *chip = mtd->priv;
202 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
213 * nand_write_byte - [DEFAULT] write single byte to chip
214 * @mtd: MTD device structure
215 * @byte: value to write
217 * Default function to write a byte to I/O[7:0]
219 static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
221 struct nand_chip *chip = mtd->priv;
223 chip->write_buf(mtd, &byte, 1);
227 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
228 * @mtd: MTD device structure
229 * @byte: value to write
231 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
233 static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
235 struct nand_chip *chip = mtd->priv;
236 uint16_t word = byte;
239 * It's not entirely clear what should happen to I/O[15:8] when writing
240 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
242 * When the host supports a 16-bit bus width, only data is
243 * transferred at the 16-bit width. All address and command line
244 * transfers shall use only the lower 8-bits of the data bus. During
245 * command transfers, the host may place any value on the upper
246 * 8-bits of the data bus. During address transfers, the host shall
247 * set the upper 8-bits of the data bus to 00h.
249 * One user of the write_byte callback is nand_onfi_set_features. The
250 * four parameters are specified to be written to I/O[7:0], but this is
251 * neither an address nor a command transfer. Let's assume a 0 on the
252 * upper I/O lines is OK.
254 chip->write_buf(mtd, (uint8_t *)&word, 2);
257 #if !defined(CONFIG_BLACKFIN)
258 static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
262 for (i = 0; i < len; i++)
263 writeb(buf[i], addr);
265 static void ioread8_rep(void *addr, uint8_t *buf, int len)
269 for (i = 0; i < len; i++)
270 buf[i] = readb(addr);
273 static void ioread16_rep(void *addr, void *buf, int len)
276 u16 *p = (u16 *) buf;
278 for (i = 0; i < len; i++)
282 static void iowrite16_rep(void *addr, void *buf, int len)
285 u16 *p = (u16 *) buf;
287 for (i = 0; i < len; i++)
293 * nand_write_buf - [DEFAULT] write buffer to chip
294 * @mtd: MTD device structure
296 * @len: number of bytes to write
298 * Default write function for 8bit buswidth.
300 void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
302 struct nand_chip *chip = mtd->priv;
304 iowrite8_rep(chip->IO_ADDR_W, buf, len);
308 * nand_read_buf - [DEFAULT] read chip data into buffer
309 * @mtd: MTD device structure
310 * @buf: buffer to store date
311 * @len: number of bytes to read
313 * Default read function for 8bit buswidth.
315 void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
317 struct nand_chip *chip = mtd->priv;
319 ioread8_rep(chip->IO_ADDR_R, buf, len);
323 * nand_write_buf16 - [DEFAULT] write buffer to chip
324 * @mtd: MTD device structure
326 * @len: number of bytes to write
328 * Default write function for 16bit buswidth.
330 void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
332 struct nand_chip *chip = mtd->priv;
333 u16 *p = (u16 *) buf;
335 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
339 * nand_read_buf16 - [DEFAULT] read chip data into buffer
340 * @mtd: MTD device structure
341 * @buf: buffer to store date
342 * @len: number of bytes to read
344 * Default read function for 16bit buswidth.
346 void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
348 struct nand_chip *chip = mtd->priv;
349 u16 *p = (u16 *) buf;
351 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
355 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
356 * @mtd: MTD device structure
357 * @ofs: offset from device start
358 * @getchip: 0, if the chip is already selected
360 * Check, if the block is bad.
362 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
364 int page, chipnr, res = 0, i = 0;
365 struct nand_chip *chip = mtd->priv;
368 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
369 ofs += mtd->erasesize - mtd->writesize;
371 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
374 chipnr = (int)(ofs >> chip->chip_shift);
376 nand_get_device(mtd, FL_READING);
378 /* Select the NAND device */
379 chip->select_chip(mtd, chipnr);
383 if (chip->options & NAND_BUSWIDTH_16) {
384 chip->cmdfunc(mtd, NAND_CMD_READOOB,
385 chip->badblockpos & 0xFE, page);
386 bad = cpu_to_le16(chip->read_word(mtd));
387 if (chip->badblockpos & 0x1)
392 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
394 bad = chip->read_byte(mtd);
397 if (likely(chip->badblockbits == 8))
400 res = hweight8(bad) < chip->badblockbits;
401 ofs += mtd->writesize;
402 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
404 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
407 chip->select_chip(mtd, -1);
408 nand_release_device(mtd);
415 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
416 * @mtd: MTD device structure
417 * @ofs: offset from device start
419 * This is the default implementation, which can be overridden by a hardware
420 * specific driver. It provides the details for writing a bad block marker to a
423 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
425 struct nand_chip *chip = mtd->priv;
426 struct mtd_oob_ops ops;
427 uint8_t buf[2] = { 0, 0 };
428 int ret = 0, res, i = 0;
432 ops.ooboffs = chip->badblockpos;
433 if (chip->options & NAND_BUSWIDTH_16) {
434 ops.ooboffs &= ~0x01;
435 ops.len = ops.ooblen = 2;
437 ops.len = ops.ooblen = 1;
439 ops.mode = MTD_OPS_PLACE_OOB;
441 /* Write to first/last page(s) if necessary */
442 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
443 ofs += mtd->erasesize - mtd->writesize;
445 res = nand_do_write_oob(mtd, ofs, &ops);
450 ofs += mtd->writesize;
451 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
457 * nand_block_markbad_lowlevel - mark a block bad
458 * @mtd: MTD device structure
459 * @ofs: offset from device start
461 * This function performs the generic NAND bad block marking steps (i.e., bad
462 * block table(s) and/or marker(s)). We only allow the hardware driver to
463 * specify how to write bad block markers to OOB (chip->block_markbad).
465 * We try operations in the following order:
466 * (1) erase the affected block, to allow OOB marker to be written cleanly
467 * (2) write bad block marker to OOB area of affected block (unless flag
468 * NAND_BBT_NO_OOB_BBM is present)
470 * Note that we retain the first error encountered in (2) or (3), finish the
471 * procedures, and dump the error in the end.
473 static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
475 struct nand_chip *chip = mtd->priv;
478 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
479 struct erase_info einfo;
481 /* Attempt erase before marking OOB */
482 memset(&einfo, 0, sizeof(einfo));
485 einfo.len = 1ULL << chip->phys_erase_shift;
486 nand_erase_nand(mtd, &einfo, 0);
488 /* Write bad block marker to OOB */
489 nand_get_device(mtd, FL_WRITING);
490 ret = chip->block_markbad(mtd, ofs);
491 nand_release_device(mtd);
494 /* Mark block bad in BBT */
496 res = nand_markbad_bbt(mtd, ofs);
502 mtd->ecc_stats.badblocks++;
508 * nand_check_wp - [GENERIC] check if the chip is write protected
509 * @mtd: MTD device structure
511 * Check, if the device is write protected. The function expects, that the
512 * device is already selected.
514 static int nand_check_wp(struct mtd_info *mtd)
516 struct nand_chip *chip = mtd->priv;
518 /* Broken xD cards report WP despite being writable */
519 if (chip->options & NAND_BROKEN_XD)
522 /* Check the WP bit */
523 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
524 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
528 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
529 * @mtd: MTD device structure
530 * @ofs: offset from device start
532 * Check if the block is mark as reserved.
534 static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
536 struct nand_chip *chip = mtd->priv;
540 /* Return info from the table */
541 return nand_isreserved_bbt(mtd, ofs);
545 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
546 * @mtd: MTD device structure
547 * @ofs: offset from device start
548 * @getchip: 0, if the chip is already selected
549 * @allowbbt: 1, if its allowed to access the bbt area
551 * Check, if the block is bad. Either by reading the bad block table or
552 * calling of the scan function.
554 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
557 struct nand_chip *chip = mtd->priv;
559 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
560 !(chip->options & NAND_BBT_SCANNED)) {
561 chip->options |= NAND_BBT_SCANNED;
566 return chip->block_bad(mtd, ofs, getchip);
568 /* Return info from the table */
569 return nand_isbad_bbt(mtd, ofs, allowbbt);
572 /* Wait for the ready pin, after a command. The timeout is caught later. */
573 void nand_wait_ready(struct mtd_info *mtd)
575 struct nand_chip *chip = mtd->priv;
576 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
579 time_start = get_timer(0);
580 /* Wait until command is processed or timeout occurs */
581 while (get_timer(time_start) < timeo) {
583 if (chip->dev_ready(mtd))
587 EXPORT_SYMBOL_GPL(nand_wait_ready);
590 * nand_command - [DEFAULT] Send command to NAND device
591 * @mtd: MTD device structure
592 * @command: the command to be sent
593 * @column: the column address for this command, -1 if none
594 * @page_addr: the page address for this command, -1 if none
596 * Send command to NAND device. This function is used for small page devices
597 * (512 Bytes per page).
599 static void nand_command(struct mtd_info *mtd, unsigned int command,
600 int column, int page_addr)
602 register struct nand_chip *chip = mtd->priv;
603 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
604 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
606 /* Write out the command to the device */
607 if (command == NAND_CMD_SEQIN) {
610 if (column >= mtd->writesize) {
612 column -= mtd->writesize;
613 readcmd = NAND_CMD_READOOB;
614 } else if (column < 256) {
615 /* First 256 bytes --> READ0 */
616 readcmd = NAND_CMD_READ0;
619 readcmd = NAND_CMD_READ1;
621 chip->cmd_ctrl(mtd, readcmd, ctrl);
622 ctrl &= ~NAND_CTRL_CHANGE;
624 chip->cmd_ctrl(mtd, command, ctrl);
626 /* Address cycle, when necessary */
627 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
628 /* Serially input address */
630 /* Adjust columns for 16 bit buswidth */
631 if (chip->options & NAND_BUSWIDTH_16 &&
632 !nand_opcode_8bits(command))
634 chip->cmd_ctrl(mtd, column, ctrl);
635 ctrl &= ~NAND_CTRL_CHANGE;
637 if (page_addr != -1) {
638 chip->cmd_ctrl(mtd, page_addr, ctrl);
639 ctrl &= ~NAND_CTRL_CHANGE;
640 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
641 /* One more address cycle for devices > 32MiB */
642 if (chip->chipsize > (32 << 20))
643 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
645 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
648 * Program and erase have their own busy handlers status and sequential
653 case NAND_CMD_PAGEPROG:
654 case NAND_CMD_ERASE1:
655 case NAND_CMD_ERASE2:
657 case NAND_CMD_STATUS:
663 udelay(chip->chip_delay);
664 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
665 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
667 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
668 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
672 /* This applies to read commands */
675 * If we don't have access to the busy pin, we apply the given
678 if (!chip->dev_ready) {
679 udelay(chip->chip_delay);
684 * Apply this short delay always to ensure that we do wait tWB in
685 * any case on any machine.
689 nand_wait_ready(mtd);
693 * nand_command_lp - [DEFAULT] Send command to NAND large page device
694 * @mtd: MTD device structure
695 * @command: the command to be sent
696 * @column: the column address for this command, -1 if none
697 * @page_addr: the page address for this command, -1 if none
699 * Send command to NAND device. This is the version for the new large page
700 * devices. We don't have the separate regions as we have in the small page
701 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
703 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
704 int column, int page_addr)
706 register struct nand_chip *chip = mtd->priv;
707 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
709 /* Emulate NAND_CMD_READOOB */
710 if (command == NAND_CMD_READOOB) {
711 column += mtd->writesize;
712 command = NAND_CMD_READ0;
715 /* Command latch cycle */
716 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
718 if (column != -1 || page_addr != -1) {
719 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
721 /* Serially input address */
723 /* Adjust columns for 16 bit buswidth */
724 if (chip->options & NAND_BUSWIDTH_16 &&
725 !nand_opcode_8bits(command))
727 chip->cmd_ctrl(mtd, column, ctrl);
728 ctrl &= ~NAND_CTRL_CHANGE;
729 chip->cmd_ctrl(mtd, column >> 8, ctrl);
731 if (page_addr != -1) {
732 chip->cmd_ctrl(mtd, page_addr, ctrl);
733 chip->cmd_ctrl(mtd, page_addr >> 8,
734 NAND_NCE | NAND_ALE);
735 /* One more address cycle for devices > 128MiB */
736 if (chip->chipsize > (128 << 20))
737 chip->cmd_ctrl(mtd, page_addr >> 16,
738 NAND_NCE | NAND_ALE);
741 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
744 * Program and erase have their own busy handlers status, sequential
745 * in, and deplete1 need no delay.
749 case NAND_CMD_CACHEDPROG:
750 case NAND_CMD_PAGEPROG:
751 case NAND_CMD_ERASE1:
752 case NAND_CMD_ERASE2:
755 case NAND_CMD_STATUS:
761 udelay(chip->chip_delay);
762 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
763 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
764 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
765 NAND_NCE | NAND_CTRL_CHANGE);
766 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
770 case NAND_CMD_RNDOUT:
771 /* No ready / busy check necessary */
772 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
773 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
774 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
775 NAND_NCE | NAND_CTRL_CHANGE);
779 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
780 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
781 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
782 NAND_NCE | NAND_CTRL_CHANGE);
784 /* This applies to read commands */
787 * If we don't have access to the busy pin, we apply the given
790 if (!chip->dev_ready) {
791 udelay(chip->chip_delay);
797 * Apply this short delay always to ensure that we do wait tWB in
798 * any case on any machine.
802 nand_wait_ready(mtd);
806 * panic_nand_get_device - [GENERIC] Get chip for selected access
807 * @chip: the nand chip descriptor
808 * @mtd: MTD device structure
809 * @new_state: the state which is requested
811 * Used when in panic, no locks are taken.
813 static void panic_nand_get_device(struct nand_chip *chip,
814 struct mtd_info *mtd, int new_state)
816 /* Hardware controller shared among independent devices */
817 chip->controller->active = chip;
818 chip->state = new_state;
822 * nand_get_device - [GENERIC] Get chip for selected access
823 * @mtd: MTD device structure
824 * @new_state: the state which is requested
826 * Get the device and lock it for exclusive access
829 nand_get_device(struct mtd_info *mtd, int new_state)
831 struct nand_chip *chip = mtd->priv;
832 chip->state = new_state;
837 * panic_nand_wait - [GENERIC] wait until the command is done
838 * @mtd: MTD device structure
839 * @chip: NAND chip structure
842 * Wait for command done. This is a helper function for nand_wait used when
843 * we are in interrupt context. May happen when in panic and trying to write
844 * an oops through mtdoops.
846 static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
850 for (i = 0; i < timeo; i++) {
851 if (chip->dev_ready) {
852 if (chip->dev_ready(mtd))
855 if (chip->read_byte(mtd) & NAND_STATUS_READY)
863 * nand_wait - [DEFAULT] wait until the command is done
864 * @mtd: MTD device structure
865 * @chip: NAND chip structure
867 * Wait for command done. This applies to erase and program only. Erase can
868 * take up to 400ms and program up to 20ms according to general NAND and
871 static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
874 int status, state = chip->state;
875 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
877 led_trigger_event(nand_led_trigger, LED_FULL);
880 * Apply this short delay always to ensure that we do wait tWB in any
881 * case on any machine.
885 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
887 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
890 time_start = get_timer(0);
891 while (get_timer(time_start) < timer) {
892 if (chip->dev_ready) {
893 if (chip->dev_ready(mtd))
896 if (chip->read_byte(mtd) & NAND_STATUS_READY)
900 led_trigger_event(nand_led_trigger, LED_OFF);
902 status = (int)chip->read_byte(mtd);
903 /* This can happen if in case of timeout or buggy dev_ready */
904 WARN_ON(!(status & NAND_STATUS_READY));
909 * nand_read_page_raw - [INTERN] read raw page data without ecc
910 * @mtd: mtd info structure
911 * @chip: nand chip info structure
912 * @buf: buffer to store read data
913 * @oob_required: caller requires OOB data read to chip->oob_poi
914 * @page: page number to read
916 * Not for syndrome calculating ECC controllers, which use a special oob layout.
918 static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
919 uint8_t *buf, int oob_required, int page)
921 chip->read_buf(mtd, buf, mtd->writesize);
923 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
928 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
929 * @mtd: mtd info structure
930 * @chip: nand chip info structure
931 * @buf: buffer to store read data
932 * @oob_required: caller requires OOB data read to chip->oob_poi
933 * @page: page number to read
935 * We need a special oob layout and handling even when OOB isn't used.
937 static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
938 struct nand_chip *chip, uint8_t *buf,
939 int oob_required, int page)
941 int eccsize = chip->ecc.size;
942 int eccbytes = chip->ecc.bytes;
943 uint8_t *oob = chip->oob_poi;
946 for (steps = chip->ecc.steps; steps > 0; steps--) {
947 chip->read_buf(mtd, buf, eccsize);
950 if (chip->ecc.prepad) {
951 chip->read_buf(mtd, oob, chip->ecc.prepad);
952 oob += chip->ecc.prepad;
955 chip->read_buf(mtd, oob, eccbytes);
958 if (chip->ecc.postpad) {
959 chip->read_buf(mtd, oob, chip->ecc.postpad);
960 oob += chip->ecc.postpad;
964 size = mtd->oobsize - (oob - chip->oob_poi);
966 chip->read_buf(mtd, oob, size);
972 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
973 * @mtd: mtd info structure
974 * @chip: nand chip info structure
975 * @buf: buffer to store read data
976 * @oob_required: caller requires OOB data read to chip->oob_poi
977 * @page: page number to read
979 static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
980 uint8_t *buf, int oob_required, int page)
982 int i, eccsize = chip->ecc.size;
983 int eccbytes = chip->ecc.bytes;
984 int eccsteps = chip->ecc.steps;
986 uint8_t *ecc_calc = chip->buffers->ecccalc;
987 uint8_t *ecc_code = chip->buffers->ecccode;
988 uint32_t *eccpos = chip->ecc.layout->eccpos;
989 unsigned int max_bitflips = 0;
991 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
993 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
994 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
996 for (i = 0; i < chip->ecc.total; i++)
997 ecc_code[i] = chip->oob_poi[eccpos[i]];
999 eccsteps = chip->ecc.steps;
1002 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1005 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1007 mtd->ecc_stats.failed++;
1009 mtd->ecc_stats.corrected += stat;
1010 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1013 return max_bitflips;
1017 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
1018 * @mtd: mtd info structure
1019 * @chip: nand chip info structure
1020 * @data_offs: offset of requested data within the page
1021 * @readlen: data length
1022 * @bufpoi: buffer to store read data
1023 * @page: page number to read
1025 static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1026 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1029 int start_step, end_step, num_steps;
1030 uint32_t *eccpos = chip->ecc.layout->eccpos;
1032 int data_col_addr, i, gaps = 0;
1033 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1034 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
1036 unsigned int max_bitflips = 0;
1038 /* Column address within the page aligned to ECC size (256bytes) */
1039 start_step = data_offs / chip->ecc.size;
1040 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1041 num_steps = end_step - start_step + 1;
1042 index = start_step * chip->ecc.bytes;
1044 /* Data size aligned to ECC ecc.size */
1045 datafrag_len = num_steps * chip->ecc.size;
1046 eccfrag_len = num_steps * chip->ecc.bytes;
1048 data_col_addr = start_step * chip->ecc.size;
1049 /* If we read not a page aligned data */
1050 if (data_col_addr != 0)
1051 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1053 p = bufpoi + data_col_addr;
1054 chip->read_buf(mtd, p, datafrag_len);
1057 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1058 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1061 * The performance is faster if we position offsets according to
1062 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1064 for (i = 0; i < eccfrag_len - 1; i++) {
1065 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1066 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1072 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1073 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1076 * Send the command to read the particular ECC bytes take care
1077 * about buswidth alignment in read_buf.
1079 aligned_pos = eccpos[index] & ~(busw - 1);
1080 aligned_len = eccfrag_len;
1081 if (eccpos[index] & (busw - 1))
1083 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
1086 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1087 mtd->writesize + aligned_pos, -1);
1088 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1091 for (i = 0; i < eccfrag_len; i++)
1092 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
1094 p = bufpoi + data_col_addr;
1095 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1098 stat = chip->ecc.correct(mtd, p,
1099 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1101 mtd->ecc_stats.failed++;
1103 mtd->ecc_stats.corrected += stat;
1104 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1107 return max_bitflips;
1111 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1112 * @mtd: mtd info structure
1113 * @chip: nand chip info structure
1114 * @buf: buffer to store read data
1115 * @oob_required: caller requires OOB data read to chip->oob_poi
1116 * @page: page number to read
1118 * Not for syndrome calculating ECC controllers which need a special oob layout.
1120 static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1121 uint8_t *buf, int oob_required, int page)
1123 int i, eccsize = chip->ecc.size;
1124 int eccbytes = chip->ecc.bytes;
1125 int eccsteps = chip->ecc.steps;
1127 uint8_t *ecc_calc = chip->buffers->ecccalc;
1128 uint8_t *ecc_code = chip->buffers->ecccode;
1129 uint32_t *eccpos = chip->ecc.layout->eccpos;
1130 unsigned int max_bitflips = 0;
1132 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1133 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1134 chip->read_buf(mtd, p, eccsize);
1135 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1137 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1139 for (i = 0; i < chip->ecc.total; i++)
1140 ecc_code[i] = chip->oob_poi[eccpos[i]];
1142 eccsteps = chip->ecc.steps;
1145 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1148 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1150 mtd->ecc_stats.failed++;
1152 mtd->ecc_stats.corrected += stat;
1153 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1156 return max_bitflips;
1160 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1161 * @mtd: mtd info structure
1162 * @chip: nand chip info structure
1163 * @buf: buffer to store read data
1164 * @oob_required: caller requires OOB data read to chip->oob_poi
1165 * @page: page number to read
1167 * Hardware ECC for large page chips, require OOB to be read first. For this
1168 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1169 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1170 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1171 * the data area, by overwriting the NAND manufacturer bad block markings.
1173 static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1174 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
1176 int i, eccsize = chip->ecc.size;
1177 int eccbytes = chip->ecc.bytes;
1178 int eccsteps = chip->ecc.steps;
1180 uint8_t *ecc_code = chip->buffers->ecccode;
1181 uint32_t *eccpos = chip->ecc.layout->eccpos;
1182 uint8_t *ecc_calc = chip->buffers->ecccalc;
1183 unsigned int max_bitflips = 0;
1185 /* Read the OOB area first */
1186 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1187 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1188 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1190 for (i = 0; i < chip->ecc.total; i++)
1191 ecc_code[i] = chip->oob_poi[eccpos[i]];
1193 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1196 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1197 chip->read_buf(mtd, p, eccsize);
1198 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1200 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1202 mtd->ecc_stats.failed++;
1204 mtd->ecc_stats.corrected += stat;
1205 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1208 return max_bitflips;
1212 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1213 * @mtd: mtd info structure
1214 * @chip: nand chip info structure
1215 * @buf: buffer to store read data
1216 * @oob_required: caller requires OOB data read to chip->oob_poi
1217 * @page: page number to read
1219 * The hw generator calculates the error syndrome automatically. Therefore we
1220 * need a special oob layout and handling.
1222 static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1223 uint8_t *buf, int oob_required, int page)
1225 int i, eccsize = chip->ecc.size;
1226 int eccbytes = chip->ecc.bytes;
1227 int eccsteps = chip->ecc.steps;
1229 uint8_t *oob = chip->oob_poi;
1230 unsigned int max_bitflips = 0;
1232 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1235 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1236 chip->read_buf(mtd, p, eccsize);
1238 if (chip->ecc.prepad) {
1239 chip->read_buf(mtd, oob, chip->ecc.prepad);
1240 oob += chip->ecc.prepad;
1243 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1244 chip->read_buf(mtd, oob, eccbytes);
1245 stat = chip->ecc.correct(mtd, p, oob, NULL);
1248 mtd->ecc_stats.failed++;
1250 mtd->ecc_stats.corrected += stat;
1251 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1256 if (chip->ecc.postpad) {
1257 chip->read_buf(mtd, oob, chip->ecc.postpad);
1258 oob += chip->ecc.postpad;
1262 /* Calculate remaining oob bytes */
1263 i = mtd->oobsize - (oob - chip->oob_poi);
1265 chip->read_buf(mtd, oob, i);
1267 return max_bitflips;
1271 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1272 * @chip: nand chip structure
1273 * @oob: oob destination address
1274 * @ops: oob ops structure
1275 * @len: size of oob to transfer
1277 static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1278 struct mtd_oob_ops *ops, size_t len)
1280 switch (ops->mode) {
1282 case MTD_OPS_PLACE_OOB:
1284 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1287 case MTD_OPS_AUTO_OOB: {
1288 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1289 uint32_t boffs = 0, roffs = ops->ooboffs;
1292 for (; free->length && len; free++, len -= bytes) {
1293 /* Read request not from offset 0? */
1294 if (unlikely(roffs)) {
1295 if (roffs >= free->length) {
1296 roffs -= free->length;
1299 boffs = free->offset + roffs;
1300 bytes = min_t(size_t, len,
1301 (free->length - roffs));
1304 bytes = min_t(size_t, len, free->length);
1305 boffs = free->offset;
1307 memcpy(oob, chip->oob_poi + boffs, bytes);
1319 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1320 * @mtd: MTD device structure
1321 * @retry_mode: the retry mode to use
1323 * Some vendors supply a special command to shift the Vt threshold, to be used
1324 * when there are too many bitflips in a page (i.e., ECC error). After setting
1325 * a new threshold, the host should retry reading the page.
1327 static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1329 struct nand_chip *chip = mtd->priv;
1331 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1333 if (retry_mode >= chip->read_retries)
1336 if (!chip->setup_read_retry)
1339 return chip->setup_read_retry(mtd, retry_mode);
1343 * nand_do_read_ops - [INTERN] Read data with ECC
1344 * @mtd: MTD device structure
1345 * @from: offset to read from
1346 * @ops: oob ops structure
1348 * Internal function. Called with chip held.
1350 static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1351 struct mtd_oob_ops *ops)
1353 int chipnr, page, realpage, col, bytes, aligned, oob_required;
1354 struct nand_chip *chip = mtd->priv;
1356 uint32_t readlen = ops->len;
1357 uint32_t oobreadlen = ops->ooblen;
1358 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
1359 mtd->oobavail : mtd->oobsize;
1361 uint8_t *bufpoi, *oob, *buf;
1362 unsigned int max_bitflips = 0;
1364 bool ecc_fail = false;
1366 chipnr = (int)(from >> chip->chip_shift);
1367 chip->select_chip(mtd, chipnr);
1369 realpage = (int)(from >> chip->page_shift);
1370 page = realpage & chip->pagemask;
1372 col = (int)(from & (mtd->writesize - 1));
1376 oob_required = oob ? 1 : 0;
1379 unsigned int ecc_failures = mtd->ecc_stats.failed;
1382 bytes = min(mtd->writesize - col, readlen);
1383 aligned = (bytes == mtd->writesize);
1385 /* Is the current page in the buffer? */
1386 if (realpage != chip->pagebuf || oob) {
1387 bufpoi = aligned ? buf : chip->buffers->databuf;
1390 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1393 * Now read the page into the buffer. Absent an error,
1394 * the read methods return max bitflips per ecc step.
1396 if (unlikely(ops->mode == MTD_OPS_RAW))
1397 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1400 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1402 ret = chip->ecc.read_subpage(mtd, chip,
1406 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1407 oob_required, page);
1410 /* Invalidate page cache */
1415 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1417 /* Transfer not aligned data */
1419 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
1420 !(mtd->ecc_stats.failed - ecc_failures) &&
1421 (ops->mode != MTD_OPS_RAW)) {
1422 chip->pagebuf = realpage;
1423 chip->pagebuf_bitflips = ret;
1425 /* Invalidate page cache */
1428 memcpy(buf, chip->buffers->databuf + col, bytes);
1431 if (unlikely(oob)) {
1432 int toread = min(oobreadlen, max_oobsize);
1435 oob = nand_transfer_oob(chip,
1437 oobreadlen -= toread;
1441 if (chip->options & NAND_NEED_READRDY) {
1442 /* Apply delay or wait for ready/busy pin */
1443 if (!chip->dev_ready)
1444 udelay(chip->chip_delay);
1446 nand_wait_ready(mtd);
1449 if (mtd->ecc_stats.failed - ecc_failures) {
1450 if (retry_mode + 1 < chip->read_retries) {
1452 ret = nand_setup_read_retry(mtd,
1457 /* Reset failures; retry */
1458 mtd->ecc_stats.failed = ecc_failures;
1461 /* No more retry modes; real failure */
1468 memcpy(buf, chip->buffers->databuf + col, bytes);
1470 max_bitflips = max_t(unsigned int, max_bitflips,
1471 chip->pagebuf_bitflips);
1476 /* Reset to retry mode 0 */
1478 ret = nand_setup_read_retry(mtd, 0);
1487 /* For subsequent reads align to page boundary */
1489 /* Increment page address */
1492 page = realpage & chip->pagemask;
1493 /* Check, if we cross a chip boundary */
1496 chip->select_chip(mtd, -1);
1497 chip->select_chip(mtd, chipnr);
1500 chip->select_chip(mtd, -1);
1502 ops->retlen = ops->len - (size_t) readlen;
1504 ops->oobretlen = ops->ooblen - oobreadlen;
1512 return max_bitflips;
1516 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
1517 * @mtd: MTD device structure
1518 * @from: offset to read from
1519 * @len: number of bytes to read
1520 * @retlen: pointer to variable to store the number of read bytes
1521 * @buf: the databuffer to put data
1523 * Get hold of the chip and call nand_do_read.
1525 static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1526 size_t *retlen, uint8_t *buf)
1528 struct mtd_oob_ops ops;
1531 nand_get_device(mtd, FL_READING);
1535 ops.mode = MTD_OPS_PLACE_OOB;
1536 ret = nand_do_read_ops(mtd, from, &ops);
1537 *retlen = ops.retlen;
1538 nand_release_device(mtd);
1543 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1544 * @mtd: mtd info structure
1545 * @chip: nand chip info structure
1546 * @page: page number to read
1548 static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1551 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1552 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1557 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
1559 * @mtd: mtd info structure
1560 * @chip: nand chip info structure
1561 * @page: page number to read
1563 static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1566 uint8_t *buf = chip->oob_poi;
1567 int length = mtd->oobsize;
1568 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1569 int eccsize = chip->ecc.size;
1570 uint8_t *bufpoi = buf;
1571 int i, toread, sndrnd = 0, pos;
1573 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1574 for (i = 0; i < chip->ecc.steps; i++) {
1576 pos = eccsize + i * (eccsize + chunk);
1577 if (mtd->writesize > 512)
1578 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1580 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1583 toread = min_t(int, length, chunk);
1584 chip->read_buf(mtd, bufpoi, toread);
1589 chip->read_buf(mtd, bufpoi, length);
1595 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1596 * @mtd: mtd info structure
1597 * @chip: nand chip info structure
1598 * @page: page number to write
1600 static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1604 const uint8_t *buf = chip->oob_poi;
1605 int length = mtd->oobsize;
1607 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1608 chip->write_buf(mtd, buf, length);
1609 /* Send command to program the OOB data */
1610 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1612 status = chip->waitfunc(mtd, chip);
1614 return status & NAND_STATUS_FAIL ? -EIO : 0;
1618 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1619 * with syndrome - only for large page flash
1620 * @mtd: mtd info structure
1621 * @chip: nand chip info structure
1622 * @page: page number to write
1624 static int nand_write_oob_syndrome(struct mtd_info *mtd,
1625 struct nand_chip *chip, int page)
1627 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1628 int eccsize = chip->ecc.size, length = mtd->oobsize;
1629 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1630 const uint8_t *bufpoi = chip->oob_poi;
1633 * data-ecc-data-ecc ... ecc-oob
1635 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1637 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1638 pos = steps * (eccsize + chunk);
1643 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1644 for (i = 0; i < steps; i++) {
1646 if (mtd->writesize <= 512) {
1647 uint32_t fill = 0xFFFFFFFF;
1651 int num = min_t(int, len, 4);
1652 chip->write_buf(mtd, (uint8_t *)&fill,
1657 pos = eccsize + i * (eccsize + chunk);
1658 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1662 len = min_t(int, length, chunk);
1663 chip->write_buf(mtd, bufpoi, len);
1668 chip->write_buf(mtd, bufpoi, length);
1670 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1671 status = chip->waitfunc(mtd, chip);
1673 return status & NAND_STATUS_FAIL ? -EIO : 0;
1677 * nand_do_read_oob - [INTERN] NAND read out-of-band
1678 * @mtd: MTD device structure
1679 * @from: offset to read from
1680 * @ops: oob operations description structure
1682 * NAND read out-of-band data from the spare area.
1684 static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1685 struct mtd_oob_ops *ops)
1687 int page, realpage, chipnr;
1688 struct nand_chip *chip = mtd->priv;
1689 struct mtd_ecc_stats stats;
1690 int readlen = ops->ooblen;
1692 uint8_t *buf = ops->oobbuf;
1695 pr_debug("%s: from = 0x%08Lx, len = %i\n",
1696 __func__, (unsigned long long)from, readlen);
1698 stats = mtd->ecc_stats;
1700 if (ops->mode == MTD_OPS_AUTO_OOB)
1701 len = chip->ecc.layout->oobavail;
1705 if (unlikely(ops->ooboffs >= len)) {
1706 pr_debug("%s: attempt to start read outside oob\n",
1711 /* Do not allow reads past end of device */
1712 if (unlikely(from >= mtd->size ||
1713 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1714 (from >> chip->page_shift)) * len)) {
1715 pr_debug("%s: attempt to read beyond end of device\n",
1720 chipnr = (int)(from >> chip->chip_shift);
1721 chip->select_chip(mtd, chipnr);
1723 /* Shift to get page */
1724 realpage = (int)(from >> chip->page_shift);
1725 page = realpage & chip->pagemask;
1730 if (ops->mode == MTD_OPS_RAW)
1731 ret = chip->ecc.read_oob_raw(mtd, chip, page);
1733 ret = chip->ecc.read_oob(mtd, chip, page);
1738 len = min(len, readlen);
1739 buf = nand_transfer_oob(chip, buf, ops, len);
1741 if (chip->options & NAND_NEED_READRDY) {
1742 /* Apply delay or wait for ready/busy pin */
1743 if (!chip->dev_ready)
1744 udelay(chip->chip_delay);
1746 nand_wait_ready(mtd);
1753 /* Increment page address */
1756 page = realpage & chip->pagemask;
1757 /* Check, if we cross a chip boundary */
1760 chip->select_chip(mtd, -1);
1761 chip->select_chip(mtd, chipnr);
1764 chip->select_chip(mtd, -1);
1766 ops->oobretlen = ops->ooblen - readlen;
1771 if (mtd->ecc_stats.failed - stats.failed)
1774 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1778 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
1779 * @mtd: MTD device structure
1780 * @from: offset to read from
1781 * @ops: oob operation description structure
1783 * NAND read data and/or out-of-band data.
1785 static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1786 struct mtd_oob_ops *ops)
1788 int ret = -ENOTSUPP;
1792 /* Do not allow reads past end of device */
1793 if (ops->datbuf && (from + ops->len) > mtd->size) {
1794 pr_debug("%s: attempt to read beyond end of device\n",
1799 nand_get_device(mtd, FL_READING);
1801 switch (ops->mode) {
1802 case MTD_OPS_PLACE_OOB:
1803 case MTD_OPS_AUTO_OOB:
1812 ret = nand_do_read_oob(mtd, from, ops);
1814 ret = nand_do_read_ops(mtd, from, ops);
1817 nand_release_device(mtd);
1823 * nand_write_page_raw - [INTERN] raw page write function
1824 * @mtd: mtd info structure
1825 * @chip: nand chip info structure
1827 * @oob_required: must write chip->oob_poi to OOB
1829 * Not for syndrome calculating ECC controllers, which use a special oob layout.
1831 static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1832 const uint8_t *buf, int oob_required)
1834 chip->write_buf(mtd, buf, mtd->writesize);
1836 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1842 * nand_write_page_raw_syndrome - [INTERN] raw page write function
1843 * @mtd: mtd info structure
1844 * @chip: nand chip info structure
1846 * @oob_required: must write chip->oob_poi to OOB
1848 * We need a special oob layout and handling even when ECC isn't checked.
1850 static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
1851 struct nand_chip *chip,
1852 const uint8_t *buf, int oob_required)
1854 int eccsize = chip->ecc.size;
1855 int eccbytes = chip->ecc.bytes;
1856 uint8_t *oob = chip->oob_poi;
1859 for (steps = chip->ecc.steps; steps > 0; steps--) {
1860 chip->write_buf(mtd, buf, eccsize);
1863 if (chip->ecc.prepad) {
1864 chip->write_buf(mtd, oob, chip->ecc.prepad);
1865 oob += chip->ecc.prepad;
1868 chip->write_buf(mtd, oob, eccbytes);
1871 if (chip->ecc.postpad) {
1872 chip->write_buf(mtd, oob, chip->ecc.postpad);
1873 oob += chip->ecc.postpad;
1877 size = mtd->oobsize - (oob - chip->oob_poi);
1879 chip->write_buf(mtd, oob, size);
1884 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
1885 * @mtd: mtd info structure
1886 * @chip: nand chip info structure
1888 * @oob_required: must write chip->oob_poi to OOB
1890 static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1891 const uint8_t *buf, int oob_required)
1893 int i, eccsize = chip->ecc.size;
1894 int eccbytes = chip->ecc.bytes;
1895 int eccsteps = chip->ecc.steps;
1896 uint8_t *ecc_calc = chip->buffers->ecccalc;
1897 const uint8_t *p = buf;
1898 uint32_t *eccpos = chip->ecc.layout->eccpos;
1900 /* Software ECC calculation */
1901 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1902 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1904 for (i = 0; i < chip->ecc.total; i++)
1905 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1907 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
1911 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
1912 * @mtd: mtd info structure
1913 * @chip: nand chip info structure
1915 * @oob_required: must write chip->oob_poi to OOB
1917 static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1918 const uint8_t *buf, int oob_required)
1920 int i, eccsize = chip->ecc.size;
1921 int eccbytes = chip->ecc.bytes;
1922 int eccsteps = chip->ecc.steps;
1923 uint8_t *ecc_calc = chip->buffers->ecccalc;
1924 const uint8_t *p = buf;
1925 uint32_t *eccpos = chip->ecc.layout->eccpos;
1927 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1928 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1929 chip->write_buf(mtd, p, eccsize);
1930 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1933 for (i = 0; i < chip->ecc.total; i++)
1934 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1936 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1943 * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
1944 * @mtd: mtd info structure
1945 * @chip: nand chip info structure
1946 * @offset: column address of subpage within the page
1947 * @data_len: data length
1949 * @oob_required: must write chip->oob_poi to OOB
1951 static int nand_write_subpage_hwecc(struct mtd_info *mtd,
1952 struct nand_chip *chip, uint32_t offset,
1953 uint32_t data_len, const uint8_t *buf,
1956 uint8_t *oob_buf = chip->oob_poi;
1957 uint8_t *ecc_calc = chip->buffers->ecccalc;
1958 int ecc_size = chip->ecc.size;
1959 int ecc_bytes = chip->ecc.bytes;
1960 int ecc_steps = chip->ecc.steps;
1961 uint32_t *eccpos = chip->ecc.layout->eccpos;
1962 uint32_t start_step = offset / ecc_size;
1963 uint32_t end_step = (offset + data_len - 1) / ecc_size;
1964 int oob_bytes = mtd->oobsize / ecc_steps;
1967 for (step = 0; step < ecc_steps; step++) {
1968 /* configure controller for WRITE access */
1969 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1971 /* write data (untouched subpages already masked by 0xFF) */
1972 chip->write_buf(mtd, buf, ecc_size);
1974 /* mask ECC of un-touched subpages by padding 0xFF */
1975 if ((step < start_step) || (step > end_step))
1976 memset(ecc_calc, 0xff, ecc_bytes);
1978 chip->ecc.calculate(mtd, buf, ecc_calc);
1980 /* mask OOB of un-touched subpages by padding 0xFF */
1981 /* if oob_required, preserve OOB metadata of written subpage */
1982 if (!oob_required || (step < start_step) || (step > end_step))
1983 memset(oob_buf, 0xff, oob_bytes);
1986 ecc_calc += ecc_bytes;
1987 oob_buf += oob_bytes;
1990 /* copy calculated ECC for whole page to chip->buffer->oob */
1991 /* this include masked-value(0xFF) for unwritten subpages */
1992 ecc_calc = chip->buffers->ecccalc;
1993 for (i = 0; i < chip->ecc.total; i++)
1994 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1996 /* write OOB buffer to NAND device */
1997 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2004 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2005 * @mtd: mtd info structure
2006 * @chip: nand chip info structure
2008 * @oob_required: must write chip->oob_poi to OOB
2010 * The hw generator calculates the error syndrome automatically. Therefore we
2011 * need a special oob layout and handling.
2013 static int nand_write_page_syndrome(struct mtd_info *mtd,
2014 struct nand_chip *chip,
2015 const uint8_t *buf, int oob_required)
2017 int i, eccsize = chip->ecc.size;
2018 int eccbytes = chip->ecc.bytes;
2019 int eccsteps = chip->ecc.steps;
2020 const uint8_t *p = buf;
2021 uint8_t *oob = chip->oob_poi;
2023 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2025 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2026 chip->write_buf(mtd, p, eccsize);
2028 if (chip->ecc.prepad) {
2029 chip->write_buf(mtd, oob, chip->ecc.prepad);
2030 oob += chip->ecc.prepad;
2033 chip->ecc.calculate(mtd, p, oob);
2034 chip->write_buf(mtd, oob, eccbytes);
2037 if (chip->ecc.postpad) {
2038 chip->write_buf(mtd, oob, chip->ecc.postpad);
2039 oob += chip->ecc.postpad;
2043 /* Calculate remaining oob bytes */
2044 i = mtd->oobsize - (oob - chip->oob_poi);
2046 chip->write_buf(mtd, oob, i);
2052 * nand_write_page - [REPLACEABLE] write one page
2053 * @mtd: MTD device structure
2054 * @chip: NAND chip descriptor
2055 * @offset: address offset within the page
2056 * @data_len: length of actual data to be written
2057 * @buf: the data to write
2058 * @oob_required: must write chip->oob_poi to OOB
2059 * @page: page number to write
2060 * @cached: cached programming
2061 * @raw: use _raw version of write_page
2063 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
2064 uint32_t offset, int data_len, const uint8_t *buf,
2065 int oob_required, int page, int cached, int raw)
2067 int status, subpage;
2069 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2070 chip->ecc.write_subpage)
2071 subpage = offset || (data_len < mtd->writesize);
2075 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2078 status = chip->ecc.write_page_raw(mtd, chip, buf,
2081 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2084 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2090 * Cached progamming disabled for now. Not sure if it's worth the
2091 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
2095 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
2097 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2098 status = chip->waitfunc(mtd, chip);
2100 * See if operation failed and additional status checks are
2103 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2104 status = chip->errstat(mtd, chip, FL_WRITING, status,
2107 if (status & NAND_STATUS_FAIL)
2110 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
2111 status = chip->waitfunc(mtd, chip);
2118 * nand_fill_oob - [INTERN] Transfer client buffer to oob
2119 * @mtd: MTD device structure
2120 * @oob: oob data buffer
2121 * @len: oob data write length
2122 * @ops: oob ops structure
2124 static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2125 struct mtd_oob_ops *ops)
2127 struct nand_chip *chip = mtd->priv;
2130 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2131 * data from a previous OOB read.
2133 memset(chip->oob_poi, 0xff, mtd->oobsize);
2135 switch (ops->mode) {
2137 case MTD_OPS_PLACE_OOB:
2139 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2142 case MTD_OPS_AUTO_OOB: {
2143 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2144 uint32_t boffs = 0, woffs = ops->ooboffs;
2147 for (; free->length && len; free++, len -= bytes) {
2148 /* Write request not from offset 0? */
2149 if (unlikely(woffs)) {
2150 if (woffs >= free->length) {
2151 woffs -= free->length;
2154 boffs = free->offset + woffs;
2155 bytes = min_t(size_t, len,
2156 (free->length - woffs));
2159 bytes = min_t(size_t, len, free->length);
2160 boffs = free->offset;
2162 memcpy(chip->oob_poi + boffs, oob, bytes);
2173 #define NOTALIGNED(x) (((x) & (chip->subpagesize - 1)) != 0)
2176 * nand_do_write_ops - [INTERN] NAND write with ECC
2177 * @mtd: MTD device structure
2178 * @to: offset to write to
2179 * @ops: oob operations description structure
2181 * NAND write with ECC.
2183 static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2184 struct mtd_oob_ops *ops)
2186 int chipnr, realpage, page, blockmask, column;
2187 struct nand_chip *chip = mtd->priv;
2188 uint32_t writelen = ops->len;
2190 uint32_t oobwritelen = ops->ooblen;
2191 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
2192 mtd->oobavail : mtd->oobsize;
2194 uint8_t *oob = ops->oobbuf;
2195 uint8_t *buf = ops->datbuf;
2197 int oob_required = oob ? 1 : 0;
2203 /* Reject writes, which are not page aligned */
2204 if (NOTALIGNED(to)) {
2205 pr_notice("%s: attempt to write non page aligned data\n",
2210 column = to & (mtd->writesize - 1);
2212 chipnr = (int)(to >> chip->chip_shift);
2213 chip->select_chip(mtd, chipnr);
2215 /* Check, if it is write protected */
2216 if (nand_check_wp(mtd)) {
2221 realpage = (int)(to >> chip->page_shift);
2222 page = realpage & chip->pagemask;
2223 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2225 /* Invalidate the page cache, when we write to the cached page */
2226 if (to <= (chip->pagebuf << chip->page_shift) &&
2227 (chip->pagebuf << chip->page_shift) < (to + ops->len))
2230 /* Don't allow multipage oob writes with offset */
2231 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2237 int bytes = mtd->writesize;
2238 int cached = writelen > bytes && page != blockmask;
2239 uint8_t *wbuf = buf;
2242 /* Partial page write? */
2243 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2245 bytes = min_t(int, bytes - column, (int) writelen);
2247 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2248 memcpy(&chip->buffers->databuf[column], buf, bytes);
2249 wbuf = chip->buffers->databuf;
2252 if (unlikely(oob)) {
2253 size_t len = min(oobwritelen, oobmaxlen);
2254 oob = nand_fill_oob(mtd, oob, len, ops);
2257 /* We still need to erase leftover OOB data */
2258 memset(chip->oob_poi, 0xff, mtd->oobsize);
2260 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2261 oob_required, page, cached,
2262 (ops->mode == MTD_OPS_RAW));
2274 page = realpage & chip->pagemask;
2275 /* Check, if we cross a chip boundary */
2278 chip->select_chip(mtd, -1);
2279 chip->select_chip(mtd, chipnr);
2283 ops->retlen = ops->len - writelen;
2285 ops->oobretlen = ops->ooblen;
2288 chip->select_chip(mtd, -1);
2293 * panic_nand_write - [MTD Interface] NAND write with ECC
2294 * @mtd: MTD device structure
2295 * @to: offset to write to
2296 * @len: number of bytes to write
2297 * @retlen: pointer to variable to store the number of written bytes
2298 * @buf: the data to write
2300 * NAND write with ECC. Used when performing writes in interrupt context, this
2301 * may for example be called by mtdoops when writing an oops while in panic.
2303 static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2304 size_t *retlen, const uint8_t *buf)
2306 struct nand_chip *chip = mtd->priv;
2307 struct mtd_oob_ops ops;
2310 /* Wait for the device to get ready */
2311 panic_nand_wait(mtd, chip, 400);
2313 /* Grab the device */
2314 panic_nand_get_device(chip, mtd, FL_WRITING);
2317 ops.datbuf = (uint8_t *)buf;
2319 ops.mode = MTD_OPS_PLACE_OOB;
2321 ret = nand_do_write_ops(mtd, to, &ops);
2323 *retlen = ops.retlen;
2328 * nand_write - [MTD Interface] NAND write with ECC
2329 * @mtd: MTD device structure
2330 * @to: offset to write to
2331 * @len: number of bytes to write
2332 * @retlen: pointer to variable to store the number of written bytes
2333 * @buf: the data to write
2335 * NAND write with ECC.
2337 static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2338 size_t *retlen, const uint8_t *buf)
2340 struct mtd_oob_ops ops;
2343 nand_get_device(mtd, FL_WRITING);
2345 ops.datbuf = (uint8_t *)buf;
2347 ops.mode = MTD_OPS_PLACE_OOB;
2348 ret = nand_do_write_ops(mtd, to, &ops);
2349 *retlen = ops.retlen;
2350 nand_release_device(mtd);
2355 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
2356 * @mtd: MTD device structure
2357 * @to: offset to write to
2358 * @ops: oob operation description structure
2360 * NAND write out-of-band.
2362 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2363 struct mtd_oob_ops *ops)
2365 int chipnr, page, status, len;
2366 struct nand_chip *chip = mtd->priv;
2368 pr_debug("%s: to = 0x%08x, len = %i\n",
2369 __func__, (unsigned int)to, (int)ops->ooblen);
2371 if (ops->mode == MTD_OPS_AUTO_OOB)
2372 len = chip->ecc.layout->oobavail;
2376 /* Do not allow write past end of page */
2377 if ((ops->ooboffs + ops->ooblen) > len) {
2378 pr_debug("%s: attempt to write past end of page\n",
2383 if (unlikely(ops->ooboffs >= len)) {
2384 pr_debug("%s: attempt to start write outside oob\n",
2389 /* Do not allow write past end of device */
2390 if (unlikely(to >= mtd->size ||
2391 ops->ooboffs + ops->ooblen >
2392 ((mtd->size >> chip->page_shift) -
2393 (to >> chip->page_shift)) * len)) {
2394 pr_debug("%s: attempt to write beyond end of device\n",
2399 chipnr = (int)(to >> chip->chip_shift);
2400 chip->select_chip(mtd, chipnr);
2402 /* Shift to get page */
2403 page = (int)(to >> chip->page_shift);
2406 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2407 * of my DiskOnChip 2000 test units) will clear the whole data page too
2408 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2409 * it in the doc2000 driver in August 1999. dwmw2.
2411 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2413 /* Check, if it is write protected */
2414 if (nand_check_wp(mtd)) {
2415 chip->select_chip(mtd, -1);
2419 /* Invalidate the page cache, if we write to the cached page */
2420 if (page == chip->pagebuf)
2423 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2425 if (ops->mode == MTD_OPS_RAW)
2426 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2428 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2430 chip->select_chip(mtd, -1);
2435 ops->oobretlen = ops->ooblen;
2441 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2442 * @mtd: MTD device structure
2443 * @to: offset to write to
2444 * @ops: oob operation description structure
2446 static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2447 struct mtd_oob_ops *ops)
2449 int ret = -ENOTSUPP;
2453 /* Do not allow writes past end of device */
2454 if (ops->datbuf && (to + ops->len) > mtd->size) {
2455 pr_debug("%s: attempt to write beyond end of device\n",
2460 nand_get_device(mtd, FL_WRITING);
2462 switch (ops->mode) {
2463 case MTD_OPS_PLACE_OOB:
2464 case MTD_OPS_AUTO_OOB:
2473 ret = nand_do_write_oob(mtd, to, ops);
2475 ret = nand_do_write_ops(mtd, to, ops);
2478 nand_release_device(mtd);
2483 * single_erase_cmd - [GENERIC] NAND standard block erase command function
2484 * @mtd: MTD device structure
2485 * @page: the page address of the block which will be erased
2487 * Standard erase command for NAND chips.
2489 static void single_erase_cmd(struct mtd_info *mtd, int page)
2491 struct nand_chip *chip = mtd->priv;
2492 /* Send commands to erase a block */
2493 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2494 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2498 * nand_erase - [MTD Interface] erase block(s)
2499 * @mtd: MTD device structure
2500 * @instr: erase instruction
2502 * Erase one ore more blocks.
2504 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
2506 return nand_erase_nand(mtd, instr, 0);
2510 * nand_erase_nand - [INTERN] erase block(s)
2511 * @mtd: MTD device structure
2512 * @instr: erase instruction
2513 * @allowbbt: allow erasing the bbt area
2515 * Erase one ore more blocks.
2517 int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2520 int page, status, pages_per_block, ret, chipnr;
2521 struct nand_chip *chip = mtd->priv;
2524 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2525 __func__, (unsigned long long)instr->addr,
2526 (unsigned long long)instr->len);
2528 if (check_offs_len(mtd, instr->addr, instr->len))
2531 /* Grab the lock and see if the device is available */
2532 nand_get_device(mtd, FL_ERASING);
2534 /* Shift to get first page */
2535 page = (int)(instr->addr >> chip->page_shift);
2536 chipnr = (int)(instr->addr >> chip->chip_shift);
2538 /* Calculate pages in each block */
2539 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
2541 /* Select the NAND device */
2542 chip->select_chip(mtd, chipnr);
2544 /* Check, if it is write protected */
2545 if (nand_check_wp(mtd)) {
2546 pr_debug("%s: device is write protected!\n",
2548 instr->state = MTD_ERASE_FAILED;
2552 /* Loop through the pages */
2555 instr->state = MTD_ERASING;
2560 /* Check if we have a bad block, we do not erase bad blocks! */
2561 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
2562 chip->page_shift, 0, allowbbt)) {
2563 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2565 instr->state = MTD_ERASE_FAILED;
2570 * Invalidate the page cache, if we erase the block which
2571 * contains the current cached page.
2573 if (page <= chip->pagebuf && chip->pagebuf <
2574 (page + pages_per_block))
2577 chip->erase_cmd(mtd, page & chip->pagemask);
2579 status = chip->waitfunc(mtd, chip);
2582 * See if operation failed and additional status checks are
2585 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2586 status = chip->errstat(mtd, chip, FL_ERASING,
2589 /* See if block erase succeeded */
2590 if (status & NAND_STATUS_FAIL) {
2591 pr_debug("%s: failed erase, page 0x%08x\n",
2593 instr->state = MTD_ERASE_FAILED;
2595 ((loff_t)page << chip->page_shift);
2599 /* Increment page address and decrement length */
2600 len -= (1ULL << chip->phys_erase_shift);
2601 page += pages_per_block;
2603 /* Check, if we cross a chip boundary */
2604 if (len && !(page & chip->pagemask)) {
2606 chip->select_chip(mtd, -1);
2607 chip->select_chip(mtd, chipnr);
2610 instr->state = MTD_ERASE_DONE;
2614 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2616 /* Deselect and wake up anyone waiting on the device */
2617 chip->select_chip(mtd, -1);
2618 nand_release_device(mtd);
2620 /* Do call back function */
2622 mtd_erase_callback(instr);
2624 /* Return more or less happy */
2629 * nand_sync - [MTD Interface] sync
2630 * @mtd: MTD device structure
2632 * Sync is actually a wait for chip ready function.
2634 static void nand_sync(struct mtd_info *mtd)
2636 pr_debug("%s: called\n", __func__);
2638 /* Grab the lock and see if the device is available */
2639 nand_get_device(mtd, FL_SYNCING);
2640 /* Release it and go back */
2641 nand_release_device(mtd);
2645 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
2646 * @mtd: MTD device structure
2647 * @offs: offset relative to mtd start
2649 static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
2651 return nand_block_checkbad(mtd, offs, 1, 0);
2655 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
2656 * @mtd: MTD device structure
2657 * @ofs: offset relative to mtd start
2659 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2663 ret = nand_block_isbad(mtd, ofs);
2665 /* If it was bad already, return success and do nothing */
2671 return nand_block_markbad_lowlevel(mtd, ofs);
2675 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2676 * @mtd: MTD device structure
2677 * @chip: nand chip info structure
2678 * @addr: feature address.
2679 * @subfeature_param: the subfeature parameters, a four bytes array.
2681 static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2682 int addr, uint8_t *subfeature_param)
2687 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2688 if (!chip->onfi_version ||
2689 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2690 & ONFI_OPT_CMD_SET_GET_FEATURES))
2694 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2695 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2696 chip->write_byte(mtd, subfeature_param[i]);
2698 status = chip->waitfunc(mtd, chip);
2699 if (status & NAND_STATUS_FAIL)
2705 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2706 * @mtd: MTD device structure
2707 * @chip: nand chip info structure
2708 * @addr: feature address.
2709 * @subfeature_param: the subfeature parameters, a four bytes array.
2711 static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2712 int addr, uint8_t *subfeature_param)
2716 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2717 if (!chip->onfi_version ||
2718 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2719 & ONFI_OPT_CMD_SET_GET_FEATURES))
2723 /* clear the sub feature parameters */
2724 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2726 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2727 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2728 *subfeature_param++ = chip->read_byte(mtd);
2733 /* Set default functions */
2734 static void nand_set_defaults(struct nand_chip *chip, int busw)
2736 /* check for proper chip_delay setup, set 20us if not */
2737 if (!chip->chip_delay)
2738 chip->chip_delay = 20;
2740 /* check, if a user supplied command function given */
2741 if (chip->cmdfunc == NULL)
2742 chip->cmdfunc = nand_command;
2744 /* check, if a user supplied wait function given */
2745 if (chip->waitfunc == NULL)
2746 chip->waitfunc = nand_wait;
2748 if (!chip->select_chip)
2749 chip->select_chip = nand_select_chip;
2751 /* set for ONFI nand */
2752 if (!chip->onfi_set_features)
2753 chip->onfi_set_features = nand_onfi_set_features;
2754 if (!chip->onfi_get_features)
2755 chip->onfi_get_features = nand_onfi_get_features;
2757 /* If called twice, pointers that depend on busw may need to be reset */
2758 if (!chip->read_byte || chip->read_byte == nand_read_byte)
2759 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2760 if (!chip->read_word)
2761 chip->read_word = nand_read_word;
2762 if (!chip->block_bad)
2763 chip->block_bad = nand_block_bad;
2764 if (!chip->block_markbad)
2765 chip->block_markbad = nand_default_block_markbad;
2766 if (!chip->write_buf || chip->write_buf == nand_write_buf)
2767 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2768 if (!chip->write_byte || chip->write_byte == nand_write_byte)
2769 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2770 if (!chip->read_buf || chip->read_buf == nand_read_buf)
2771 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2772 if (!chip->scan_bbt)
2773 chip->scan_bbt = nand_default_bbt;
2775 if (!chip->controller) {
2776 chip->controller = &chip->hwcontrol;
2777 spin_lock_init(&chip->controller->lock);
2778 init_waitqueue_head(&chip->controller->wq);
2783 /* Sanitize ONFI strings so we can safely print them */
2784 static void sanitize_string(char *s, size_t len)
2788 /* Null terminate */
2791 /* Remove non printable chars */
2792 for (i = 0; i < len - 1; i++) {
2793 if (s[i] < ' ' || s[i] > 127)
2797 /* Remove trailing spaces */
2801 static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2806 for (i = 0; i < 8; i++)
2807 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2813 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2814 /* Parse the Extended Parameter Page. */
2815 static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
2816 struct nand_chip *chip, struct nand_onfi_params *p)
2818 struct onfi_ext_param_page *ep;
2819 struct onfi_ext_section *s;
2820 struct onfi_ext_ecc_info *ecc;
2826 len = le16_to_cpu(p->ext_param_page_length) * 16;
2827 ep = kmalloc(len, GFP_KERNEL);
2831 /* Send our own NAND_CMD_PARAM. */
2832 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2834 /* Use the Change Read Column command to skip the ONFI param pages. */
2835 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
2836 sizeof(*p) * p->num_of_param_pages , -1);
2838 /* Read out the Extended Parameter Page. */
2839 chip->read_buf(mtd, (uint8_t *)ep, len);
2840 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
2841 != le16_to_cpu(ep->crc))) {
2842 pr_debug("fail in the CRC.\n");
2847 * Check the signature.
2848 * Do not strictly follow the ONFI spec, maybe changed in future.
2850 if (strncmp((char *)ep->sig, "EPPS", 4)) {
2851 pr_debug("The signature is invalid.\n");
2855 /* find the ECC section. */
2856 cursor = (uint8_t *)(ep + 1);
2857 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
2858 s = ep->sections + i;
2859 if (s->type == ONFI_SECTION_TYPE_2)
2861 cursor += s->length * 16;
2863 if (i == ONFI_EXT_SECTION_MAX) {
2864 pr_debug("We can not find the ECC section.\n");
2868 /* get the info we want. */
2869 ecc = (struct onfi_ext_ecc_info *)cursor;
2871 if (!ecc->codeword_size) {
2872 pr_debug("Invalid codeword size\n");
2876 chip->ecc_strength_ds = ecc->ecc_bits;
2877 chip->ecc_step_ds = 1 << ecc->codeword_size;
2885 static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
2887 struct nand_chip *chip = mtd->priv;
2888 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
2890 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
2895 * Configure chip properties from Micron vendor-specific ONFI table
2897 static void nand_onfi_detect_micron(struct nand_chip *chip,
2898 struct nand_onfi_params *p)
2900 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
2902 if (le16_to_cpu(p->vendor_revision) < 1)
2905 chip->read_retries = micron->read_retry_options;
2906 chip->setup_read_retry = nand_setup_read_retry_micron;
2910 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
2912 static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
2915 struct nand_onfi_params *p = &chip->onfi_params;
2919 /* Try ONFI for unknown chip or LP */
2920 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2921 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2922 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2925 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2926 for (i = 0; i < 3; i++) {
2927 for (j = 0; j < sizeof(*p); j++)
2928 ((uint8_t *)p)[j] = chip->read_byte(mtd);
2929 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
2930 le16_to_cpu(p->crc)) {
2936 pr_err("Could not find valid ONFI parameter page; aborting\n");
2941 val = le16_to_cpu(p->revision);
2943 chip->onfi_version = 23;
2944 else if (val & (1 << 4))
2945 chip->onfi_version = 22;
2946 else if (val & (1 << 3))
2947 chip->onfi_version = 21;
2948 else if (val & (1 << 2))
2949 chip->onfi_version = 20;
2950 else if (val & (1 << 1))
2951 chip->onfi_version = 10;
2953 if (!chip->onfi_version) {
2954 pr_info("unsupported ONFI version: %d\n", val);
2958 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2959 sanitize_string(p->model, sizeof(p->model));
2961 mtd->name = p->model;
2963 mtd->writesize = le32_to_cpu(p->byte_per_page);
2966 * pages_per_block and blocks_per_lun may not be a power-of-2 size
2967 * (don't ask me who thought of this...). MTD assumes that these
2968 * dimensions will be power-of-2, so just truncate the remaining area.
2970 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
2971 mtd->erasesize *= mtd->writesize;
2973 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
2975 /* See erasesize comment */
2976 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
2977 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
2978 chip->bits_per_cell = p->bits_per_cell;
2980 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
2981 *busw = NAND_BUSWIDTH_16;
2985 if (p->ecc_bits != 0xff) {
2986 chip->ecc_strength_ds = p->ecc_bits;
2987 chip->ecc_step_ds = 512;
2988 } else if (chip->onfi_version >= 21 &&
2989 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
2992 * The nand_flash_detect_ext_param_page() uses the
2993 * Change Read Column command which maybe not supported
2994 * by the chip->cmdfunc. So try to update the chip->cmdfunc
2995 * now. We do not replace user supplied command function.
2997 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2998 chip->cmdfunc = nand_command_lp;
3000 /* The Extended Parameter Page is supported since ONFI 2.1. */
3001 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3002 pr_warn("Failed to detect ONFI extended param page\n");
3004 pr_warn("Could not retrieve ONFI ECC requirements\n");
3007 if (p->jedec_id == NAND_MFR_MICRON)
3008 nand_onfi_detect_micron(chip, p);
3013 static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
3021 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3023 static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3026 struct nand_jedec_params *p = &chip->jedec_params;
3027 struct jedec_ecc_info *ecc;
3031 /* Try JEDEC for unknown chip or LP */
3032 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3033 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3034 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3035 chip->read_byte(mtd) != 'C')
3038 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3039 for (i = 0; i < 3; i++) {
3040 for (j = 0; j < sizeof(*p); j++)
3041 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3043 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3044 le16_to_cpu(p->crc))
3049 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3054 val = le16_to_cpu(p->revision);
3056 chip->jedec_version = 10;
3057 else if (val & (1 << 1))
3058 chip->jedec_version = 1; /* vendor specific version */
3060 if (!chip->jedec_version) {
3061 pr_info("unsupported JEDEC version: %d\n", val);
3065 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3066 sanitize_string(p->model, sizeof(p->model));
3068 mtd->name = p->model;
3070 mtd->writesize = le32_to_cpu(p->byte_per_page);
3072 /* Please reference to the comment for nand_flash_detect_onfi. */
3073 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3074 mtd->erasesize *= mtd->writesize;
3076 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3078 /* Please reference to the comment for nand_flash_detect_onfi. */
3079 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3080 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3081 chip->bits_per_cell = p->bits_per_cell;
3083 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3084 *busw = NAND_BUSWIDTH_16;
3089 ecc = &p->ecc_info[0];
3091 if (ecc->codeword_size >= 9) {
3092 chip->ecc_strength_ds = ecc->ecc_bits;
3093 chip->ecc_step_ds = 1 << ecc->codeword_size;
3095 pr_warn("Invalid codeword size\n");
3102 * nand_id_has_period - Check if an ID string has a given wraparound period
3103 * @id_data: the ID string
3104 * @arrlen: the length of the @id_data array
3105 * @period: the period of repitition
3107 * Check if an ID string is repeated within a given sequence of bytes at
3108 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
3109 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
3110 * if the repetition has a period of @period; otherwise, returns zero.
3112 static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3115 for (i = 0; i < period; i++)
3116 for (j = i + period; j < arrlen; j += period)
3117 if (id_data[i] != id_data[j])
3123 * nand_id_len - Get the length of an ID string returned by CMD_READID
3124 * @id_data: the ID string
3125 * @arrlen: the length of the @id_data array
3127 * Returns the length of the ID string, according to known wraparound/trailing
3128 * zero patterns. If no pattern exists, returns the length of the array.
3130 static int nand_id_len(u8 *id_data, int arrlen)
3132 int last_nonzero, period;
3134 /* Find last non-zero byte */
3135 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3136 if (id_data[last_nonzero])
3140 if (last_nonzero < 0)
3143 /* Calculate wraparound period */
3144 for (period = 1; period < arrlen; period++)
3145 if (nand_id_has_period(id_data, arrlen, period))
3148 /* There's a repeated pattern */
3149 if (period < arrlen)
3152 /* There are trailing zeros */
3153 if (last_nonzero < arrlen - 1)
3154 return last_nonzero + 1;
3156 /* No pattern detected */
3160 /* Extract the bits of per cell from the 3rd byte of the extended ID */
3161 static int nand_get_bits_per_cell(u8 cellinfo)
3165 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3166 bits >>= NAND_CI_CELLTYPE_SHIFT;
3171 * Many new NAND share similar device ID codes, which represent the size of the
3172 * chip. The rest of the parameters must be decoded according to generic or
3173 * manufacturer-specific "extended ID" decoding patterns.
3175 static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3176 u8 id_data[8], int *busw)
3179 /* The 3rd id byte holds MLC / multichip data */
3180 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3181 /* The 4th id byte is the important one */
3184 id_len = nand_id_len(id_data, 8);
3187 * Field definitions are in the following datasheets:
3188 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
3189 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
3190 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
3192 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3193 * ID to decide what to do.
3195 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
3196 !nand_is_slc(chip) && id_data[5] != 0x00) {
3198 mtd->writesize = 2048 << (extid & 0x03);
3201 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3221 default: /* Other cases are "reserved" (unknown) */
3222 mtd->oobsize = 1024;
3226 /* Calc blocksize */
3227 mtd->erasesize = (128 * 1024) <<
3228 (((extid >> 1) & 0x04) | (extid & 0x03));
3230 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
3231 !nand_is_slc(chip)) {
3235 mtd->writesize = 2048 << (extid & 0x03);
3238 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3262 /* Calc blocksize */
3263 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3265 mtd->erasesize = (128 * 1024) << tmp;
3266 else if (tmp == 0x03)
3267 mtd->erasesize = 768 * 1024;
3269 mtd->erasesize = (64 * 1024) << tmp;
3273 mtd->writesize = 1024 << (extid & 0x03);
3276 mtd->oobsize = (8 << (extid & 0x01)) *
3277 (mtd->writesize >> 9);
3279 /* Calc blocksize. Blocksize is multiples of 64KiB */
3280 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3282 /* Get buswidth information */
3283 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
3286 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3287 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3289 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3291 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3293 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
3294 nand_is_slc(chip) &&
3295 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3296 !(id_data[4] & 0x80) /* !BENAND */) {
3297 mtd->oobsize = 32 * mtd->writesize >> 9;
3304 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3305 * decodes a matching ID table entry and assigns the MTD size parameters for
3308 static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3309 struct nand_flash_dev *type, u8 id_data[8],
3312 int maf_id = id_data[0];
3314 mtd->erasesize = type->erasesize;
3315 mtd->writesize = type->pagesize;
3316 mtd->oobsize = mtd->writesize / 32;
3317 *busw = type->options & NAND_BUSWIDTH_16;
3319 /* All legacy ID NAND are small-page, SLC */
3320 chip->bits_per_cell = 1;
3323 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3324 * some Spansion chips have erasesize that conflicts with size
3325 * listed in nand_ids table.
3326 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3328 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3329 && id_data[6] == 0x00 && id_data[7] == 0x00
3330 && mtd->writesize == 512) {
3331 mtd->erasesize = 128 * 1024;
3332 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3337 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3338 * heuristic patterns using various detected parameters (e.g., manufacturer,
3339 * page size, cell-type information).
3341 static void nand_decode_bbm_options(struct mtd_info *mtd,
3342 struct nand_chip *chip, u8 id_data[8])
3344 int maf_id = id_data[0];
3346 /* Set the bad block position */
3347 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3348 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3350 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3353 * Bad block marker is stored in the last page of each block on Samsung
3354 * and Hynix MLC devices; stored in first two pages of each block on
3355 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3356 * AMD/Spansion, and Macronix. All others scan only the first page.
3358 if (!nand_is_slc(chip) &&
3359 (maf_id == NAND_MFR_SAMSUNG ||
3360 maf_id == NAND_MFR_HYNIX))
3361 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
3362 else if ((nand_is_slc(chip) &&
3363 (maf_id == NAND_MFR_SAMSUNG ||
3364 maf_id == NAND_MFR_HYNIX ||
3365 maf_id == NAND_MFR_TOSHIBA ||
3366 maf_id == NAND_MFR_AMD ||
3367 maf_id == NAND_MFR_MACRONIX)) ||
3368 (mtd->writesize == 2048 &&
3369 maf_id == NAND_MFR_MICRON))
3370 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3373 static inline bool is_full_id_nand(struct nand_flash_dev *type)
3375 return type->id_len;
3378 static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3379 struct nand_flash_dev *type, u8 *id_data, int *busw)
3381 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
3382 mtd->writesize = type->pagesize;
3383 mtd->erasesize = type->erasesize;
3384 mtd->oobsize = type->oobsize;
3386 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3387 chip->chipsize = (uint64_t)type->chipsize << 20;
3388 chip->options |= type->options;
3389 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3390 chip->ecc_step_ds = NAND_ECC_STEP(type);
3392 *busw = type->options & NAND_BUSWIDTH_16;
3395 mtd->name = type->name;
3403 * Get the flash and manufacturer id and lookup if the type is supported.
3405 static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
3406 struct nand_chip *chip,
3407 int *maf_id, int *dev_id,
3408 struct nand_flash_dev *type)
3414 /* Select the device */
3415 chip->select_chip(mtd, 0);
3418 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
3421 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3423 /* Send the command for reading device ID */
3424 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3426 /* Read manufacturer and device IDs */
3427 *maf_id = chip->read_byte(mtd);
3428 *dev_id = chip->read_byte(mtd);
3431 * Try again to make sure, as some systems the bus-hold or other
3432 * interface concerns can cause random data which looks like a
3433 * possibly credible NAND flash to appear. If the two results do
3434 * not match, ignore the device completely.
3437 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3439 /* Read entire ID string */
3440 for (i = 0; i < 8; i++)
3441 id_data[i] = chip->read_byte(mtd);
3443 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
3444 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
3445 *maf_id, *dev_id, id_data[0], id_data[1]);
3446 return ERR_PTR(-ENODEV);
3450 type = nand_flash_ids;
3452 for (; type->name != NULL; type++) {
3453 if (is_full_id_nand(type)) {
3454 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3456 } else if (*dev_id == type->dev_id) {
3461 chip->onfi_version = 0;
3462 if (!type->name || !type->pagesize) {
3463 /* Check is chip is ONFI compliant */
3464 if (nand_flash_detect_onfi(mtd, chip, &busw))
3467 /* Check if the chip is JEDEC compliant */
3468 if (nand_flash_detect_jedec(mtd, chip, &busw))
3473 return ERR_PTR(-ENODEV);
3476 mtd->name = type->name;
3478 chip->chipsize = (uint64_t)type->chipsize << 20;
3480 if (!type->pagesize && chip->init_size) {
3481 /* Set the pagesize, oobsize, erasesize by the driver */
3482 busw = chip->init_size(mtd, chip, id_data);
3483 } else if (!type->pagesize) {
3484 /* Decode parameters from extended ID */
3485 nand_decode_ext_id(mtd, chip, id_data, &busw);
3487 nand_decode_id(mtd, chip, type, id_data, &busw);
3489 /* Get chip options */
3490 chip->options |= type->options;
3493 * Check if chip is not a Samsung device. Do not clear the
3494 * options for chips which do not have an extended id.
3496 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3497 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3500 /* Try to identify manufacturer */
3501 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
3502 if (nand_manuf_ids[maf_idx].id == *maf_id)
3506 if (chip->options & NAND_BUSWIDTH_AUTO) {
3507 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3508 chip->options |= busw;
3509 nand_set_defaults(chip, busw);
3510 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3512 * Check, if buswidth is correct. Hardware drivers should set
3515 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3517 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3518 pr_warn("bus width %d instead %d bit\n",
3519 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3521 return ERR_PTR(-EINVAL);
3524 nand_decode_bbm_options(mtd, chip, id_data);
3526 /* Calculate the address shift from the page size */
3527 chip->page_shift = ffs(mtd->writesize) - 1;
3528 /* Convert chipsize to number of pages per chip -1 */
3529 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
3531 chip->bbt_erase_shift = chip->phys_erase_shift =
3532 ffs(mtd->erasesize) - 1;
3533 if (chip->chipsize & 0xffffffff)
3534 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
3536 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3537 chip->chip_shift += 32 - 1;
3540 chip->badblockbits = 8;
3541 chip->erase_cmd = single_erase_cmd;
3543 /* Do not replace user supplied command function! */
3544 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3545 chip->cmdfunc = nand_command_lp;
3547 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3550 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3551 if (chip->onfi_version)
3552 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3553 chip->onfi_params.model);
3554 else if (chip->jedec_version)
3555 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3556 chip->jedec_params.model);
3558 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3561 if (chip->jedec_version)
3562 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3563 chip->jedec_params.model);
3565 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3568 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3572 pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
3573 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
3574 mtd->writesize, mtd->oobsize);
3579 * nand_scan_ident - [NAND Interface] Scan for the NAND device
3580 * @mtd: MTD device structure
3581 * @maxchips: number of chips to scan for
3582 * @table: alternative NAND ID table
3584 * This is the first phase of the normal nand_scan() function. It reads the
3585 * flash ID and sets up MTD fields accordingly.
3587 * The mtd->owner field must be set to the module of the caller.
3589 int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3590 struct nand_flash_dev *table)
3592 int i, nand_maf_id, nand_dev_id;
3593 struct nand_chip *chip = mtd->priv;
3594 struct nand_flash_dev *type;
3596 /* Set the default functions */
3597 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
3599 /* Read the flash type */
3600 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3601 &nand_dev_id, table);
3604 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
3605 pr_warn("No NAND device found\n");
3606 chip->select_chip(mtd, -1);
3607 return PTR_ERR(type);
3610 chip->select_chip(mtd, -1);
3612 /* Check for a chip array */
3613 for (i = 1; i < maxchips; i++) {
3614 chip->select_chip(mtd, i);
3615 /* See comment in nand_get_flash_type for reset */
3616 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3617 /* Send the command for reading device ID */
3618 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3619 /* Read manufacturer and device IDs */
3620 if (nand_maf_id != chip->read_byte(mtd) ||
3621 nand_dev_id != chip->read_byte(mtd)) {
3622 chip->select_chip(mtd, -1);
3625 chip->select_chip(mtd, -1);
3630 pr_info("%d chips detected\n", i);
3633 /* Store the number of chips and calc total size for mtd */
3635 mtd->size = i * chip->chipsize;
3639 EXPORT_SYMBOL(nand_scan_ident);
3643 * nand_scan_tail - [NAND Interface] Scan for the NAND device
3644 * @mtd: MTD device structure
3646 * This is the second phase of the normal nand_scan() function. It fills out
3647 * all the uninitialized function pointers with the defaults and scans for a
3648 * bad block table if appropriate.
3650 int nand_scan_tail(struct mtd_info *mtd)
3653 struct nand_chip *chip = mtd->priv;
3654 struct nand_ecc_ctrl *ecc = &chip->ecc;
3655 struct nand_buffers *nbuf;
3657 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3658 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3659 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3661 if (!(chip->options & NAND_OWN_BUFFERS)) {
3662 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
3663 chip->buffers = nbuf;
3669 /* Set the internal oob buffer location, just after the page data */
3670 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
3673 * If no default placement scheme is given, select an appropriate one.
3675 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
3676 switch (mtd->oobsize) {
3678 ecc->layout = &nand_oob_8;
3681 ecc->layout = &nand_oob_16;
3684 ecc->layout = &nand_oob_64;
3687 ecc->layout = &nand_oob_128;
3690 pr_warn("No oob scheme defined for oobsize %d\n",
3696 if (!chip->write_page)
3697 chip->write_page = nand_write_page;
3700 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
3701 * selected and we have 256 byte pagesize fallback to software ECC
3704 switch (ecc->mode) {
3705 case NAND_ECC_HW_OOB_FIRST:
3706 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3707 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
3708 pr_warn("No ECC functions supplied; "
3709 "hardware ECC not possible\n");
3712 if (!ecc->read_page)
3713 ecc->read_page = nand_read_page_hwecc_oob_first;
3716 /* Use standard hwecc read page function? */
3717 if (!ecc->read_page)
3718 ecc->read_page = nand_read_page_hwecc;
3719 if (!ecc->write_page)
3720 ecc->write_page = nand_write_page_hwecc;
3721 if (!ecc->read_page_raw)
3722 ecc->read_page_raw = nand_read_page_raw;
3723 if (!ecc->write_page_raw)
3724 ecc->write_page_raw = nand_write_page_raw;
3726 ecc->read_oob = nand_read_oob_std;
3727 if (!ecc->write_oob)
3728 ecc->write_oob = nand_write_oob_std;
3729 if (!ecc->read_subpage)
3730 ecc->read_subpage = nand_read_subpage;
3731 if (!ecc->write_subpage)
3732 ecc->write_subpage = nand_write_subpage_hwecc;
3734 case NAND_ECC_HW_SYNDROME:
3735 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
3737 ecc->read_page == nand_read_page_hwecc ||
3739 ecc->write_page == nand_write_page_hwecc)) {
3740 pr_warn("No ECC functions supplied; "
3741 "hardware ECC not possible\n");
3744 /* Use standard syndrome read/write page function? */
3745 if (!ecc->read_page)
3746 ecc->read_page = nand_read_page_syndrome;
3747 if (!ecc->write_page)
3748 ecc->write_page = nand_write_page_syndrome;
3749 if (!ecc->read_page_raw)
3750 ecc->read_page_raw = nand_read_page_raw_syndrome;
3751 if (!ecc->write_page_raw)
3752 ecc->write_page_raw = nand_write_page_raw_syndrome;
3754 ecc->read_oob = nand_read_oob_syndrome;
3755 if (!ecc->write_oob)
3756 ecc->write_oob = nand_write_oob_syndrome;
3758 if (mtd->writesize >= ecc->size) {
3759 if (!ecc->strength) {
3760 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3765 pr_warn("%d byte HW ECC not possible on "
3766 "%d byte page size, fallback to SW ECC\n",
3767 ecc->size, mtd->writesize);
3768 ecc->mode = NAND_ECC_SOFT;
3771 ecc->calculate = nand_calculate_ecc;
3772 ecc->correct = nand_correct_data;
3773 ecc->read_page = nand_read_page_swecc;
3774 ecc->read_subpage = nand_read_subpage;
3775 ecc->write_page = nand_write_page_swecc;
3776 ecc->read_page_raw = nand_read_page_raw;
3777 ecc->write_page_raw = nand_write_page_raw;
3778 ecc->read_oob = nand_read_oob_std;
3779 ecc->write_oob = nand_write_oob_std;
3786 case NAND_ECC_SOFT_BCH:
3787 if (!mtd_nand_has_bch()) {
3788 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
3791 ecc->calculate = nand_bch_calculate_ecc;
3792 ecc->correct = nand_bch_correct_data;
3793 ecc->read_page = nand_read_page_swecc;
3794 ecc->read_subpage = nand_read_subpage;
3795 ecc->write_page = nand_write_page_swecc;
3796 ecc->read_page_raw = nand_read_page_raw;
3797 ecc->write_page_raw = nand_write_page_raw;
3798 ecc->read_oob = nand_read_oob_std;
3799 ecc->write_oob = nand_write_oob_std;
3801 * Board driver should supply ecc.size and ecc.bytes values to
3802 * select how many bits are correctable; see nand_bch_init()
3803 * for details. Otherwise, default to 4 bits for large page
3806 if (!ecc->size && (mtd->oobsize >= 64)) {
3810 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
3813 pr_warn("BCH ECC initialization failed!\n");
3816 ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
3820 pr_warn("NAND_ECC_NONE selected by board driver. "
3821 "This is not recommended!\n");
3822 ecc->read_page = nand_read_page_raw;
3823 ecc->write_page = nand_write_page_raw;
3824 ecc->read_oob = nand_read_oob_std;
3825 ecc->read_page_raw = nand_read_page_raw;
3826 ecc->write_page_raw = nand_write_page_raw;
3827 ecc->write_oob = nand_write_oob_std;
3828 ecc->size = mtd->writesize;
3834 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
3838 /* For many systems, the standard OOB write also works for raw */
3839 if (!ecc->read_oob_raw)
3840 ecc->read_oob_raw = ecc->read_oob;
3841 if (!ecc->write_oob_raw)
3842 ecc->write_oob_raw = ecc->write_oob;
3845 * The number of bytes available for a client to place data into
3846 * the out of band area.
3848 ecc->layout->oobavail = 0;
3849 for (i = 0; ecc->layout->oobfree[i].length
3850 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
3851 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
3852 mtd->oobavail = ecc->layout->oobavail;
3855 * Set the number of read / write steps for one page depending on ECC
3858 ecc->steps = mtd->writesize / ecc->size;
3859 if (ecc->steps * ecc->size != mtd->writesize) {
3860 pr_warn("Invalid ECC parameters\n");
3861 pr_warn("steps=%d size=%d writesize=%d\n",
3862 chip->ecc.steps, chip->ecc.size, mtd->writesize);
3865 ecc->total = ecc->steps * ecc->bytes;
3867 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
3868 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
3869 switch (ecc->steps) {
3871 mtd->subpage_sft = 1;
3876 mtd->subpage_sft = 2;
3880 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3882 /* Initialize state */
3883 chip->state = FL_READY;
3885 /* Invalidate the pagebuffer reference */
3888 /* Large page NAND with SOFT_ECC should support subpage reads */
3889 if ((ecc->mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
3890 chip->options |= NAND_SUBPAGE_READ;
3892 /* Fill in remaining MTD driver data */
3893 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
3894 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3896 mtd->_erase = nand_erase;
3897 mtd->_read = nand_read;
3898 mtd->_write = nand_write;
3899 mtd->_panic_write = panic_nand_write;
3900 mtd->_read_oob = nand_read_oob;
3901 mtd->_write_oob = nand_write_oob;
3902 mtd->_sync = nand_sync;
3904 mtd->_unlock = NULL;
3905 mtd->_block_isreserved = nand_block_isreserved;
3906 mtd->_block_isbad = nand_block_isbad;
3907 mtd->_block_markbad = nand_block_markbad;
3908 mtd->writebufsize = mtd->writesize;
3910 /* propagate ecc info to mtd_info */
3911 mtd->ecclayout = ecc->layout;
3912 mtd->ecc_strength = ecc->strength;
3913 mtd->ecc_step_size = ecc->size;
3915 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3916 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3919 if (!mtd->bitflip_threshold)
3920 mtd->bitflip_threshold = mtd->ecc_strength;
3924 EXPORT_SYMBOL(nand_scan_tail);
3927 * is_module_text_address() isn't exported, and it's mostly a pointless
3928 * test if this is a module _anyway_ -- they'd have to try _really_ hard
3929 * to call us from in-kernel code if the core NAND support is modular.
3932 #define caller_is_module() (1)
3934 #define caller_is_module() \
3935 is_module_text_address((unsigned long)__builtin_return_address(0))
3939 * nand_scan - [NAND Interface] Scan for the NAND device
3940 * @mtd: MTD device structure
3941 * @maxchips: number of chips to scan for
3943 * This fills out all the uninitialized function pointers with the defaults.
3944 * The flash ID is read and the mtd/chip structures are filled with the
3945 * appropriate values. The mtd->owner field must be set to the module of the
3948 int nand_scan(struct mtd_info *mtd, int maxchips)
3952 /* Many callers got this wrong, so check for it for a while... */
3953 if (!mtd->owner && caller_is_module()) {
3954 pr_crit("%s called with NULL mtd->owner!\n", __func__);
3958 ret = nand_scan_ident(mtd, maxchips, NULL);
3960 ret = nand_scan_tail(mtd);
3963 EXPORT_SYMBOL(nand_scan);
3965 module_init(nand_base_init);
3966 module_exit(nand_base_exit);
3968 MODULE_LICENSE("GPL");
3969 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
3970 MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
3971 MODULE_DESCRIPTION("Generic NAND flash driver code");