3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
6 * Additional technical information is available on
7 * http://www.linux-mtd.infradead.org/doc/nand.html
9 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
10 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
13 * David Woodhouse for adding multichip support
15 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
19 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
21 * if we have HW ECC support.
22 * BBT table is not serialized, has to be fixed
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/module.h>
33 #include <linux/delay.h>
34 #include <linux/errno.h>
35 #include <linux/err.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
39 #include <linux/nmi.h>
40 #include <linux/types.h>
41 #include <linux/mtd/mtd.h>
42 #include <linux/mtd/nand.h>
43 #include <linux/mtd/nand_ecc.h>
44 #include <linux/mtd/nand_bch.h>
45 #include <linux/interrupt.h>
46 #include <linux/bitops.h>
48 #include <linux/mtd/partitions.h>
51 static int nand_get_device(struct mtd_info *mtd, int new_state);
53 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
54 struct mtd_oob_ops *ops);
56 /* Define default oob placement schemes for large and small page devices */
57 static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
58 struct mtd_oob_region *oobregion)
60 struct nand_chip *chip = mtd_to_nand(mtd);
61 struct nand_ecc_ctrl *ecc = &chip->ecc;
67 oobregion->offset = 0;
68 oobregion->length = 4;
70 oobregion->offset = 6;
71 oobregion->length = ecc->total - 4;
77 static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
78 struct mtd_oob_region *oobregion)
83 if (mtd->oobsize == 16) {
87 oobregion->length = 8;
88 oobregion->offset = 8;
90 oobregion->length = 2;
92 oobregion->offset = 3;
94 oobregion->offset = 6;
100 const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
101 .ecc = nand_ooblayout_ecc_sp,
102 .free = nand_ooblayout_free_sp,
104 EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
106 static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
107 struct mtd_oob_region *oobregion)
109 struct nand_chip *chip = mtd_to_nand(mtd);
110 struct nand_ecc_ctrl *ecc = &chip->ecc;
115 oobregion->length = ecc->total;
116 oobregion->offset = mtd->oobsize - oobregion->length;
121 static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
122 struct mtd_oob_region *oobregion)
124 struct nand_chip *chip = mtd_to_nand(mtd);
125 struct nand_ecc_ctrl *ecc = &chip->ecc;
130 oobregion->length = mtd->oobsize - ecc->total - 2;
131 oobregion->offset = 2;
136 const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
137 .ecc = nand_ooblayout_ecc_lp,
138 .free = nand_ooblayout_free_lp,
140 EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
143 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
144 * are placed at a fixed offset.
146 static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
147 struct mtd_oob_region *oobregion)
149 struct nand_chip *chip = mtd_to_nand(mtd);
150 struct nand_ecc_ctrl *ecc = &chip->ecc;
155 switch (mtd->oobsize) {
157 oobregion->offset = 40;
160 oobregion->offset = 80;
166 oobregion->length = ecc->total;
167 if (oobregion->offset + oobregion->length > mtd->oobsize)
173 static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
174 struct mtd_oob_region *oobregion)
176 struct nand_chip *chip = mtd_to_nand(mtd);
177 struct nand_ecc_ctrl *ecc = &chip->ecc;
180 if (section < 0 || section > 1)
183 switch (mtd->oobsize) {
195 oobregion->offset = 2;
196 oobregion->length = ecc_offset - 2;
198 oobregion->offset = ecc_offset + ecc->total;
199 oobregion->length = mtd->oobsize - oobregion->offset;
205 const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
206 .ecc = nand_ooblayout_ecc_lp_hamming,
207 .free = nand_ooblayout_free_lp_hamming,
210 static int check_offs_len(struct mtd_info *mtd,
211 loff_t ofs, uint64_t len)
213 struct nand_chip *chip = mtd_to_nand(mtd);
216 /* Start address must align on block boundary */
217 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
218 pr_debug("%s: unaligned address\n", __func__);
222 /* Length must align on block boundary */
223 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
224 pr_debug("%s: length not block aligned\n", __func__);
232 * nand_release_device - [GENERIC] release chip
233 * @mtd: MTD device structure
235 * Release chip lock and wake up anyone waiting on the device.
237 static void nand_release_device(struct mtd_info *mtd)
239 struct nand_chip *chip = mtd_to_nand(mtd);
241 /* Release the controller and the chip */
242 spin_lock(&chip->controller->lock);
243 chip->controller->active = NULL;
244 chip->state = FL_READY;
245 wake_up(&chip->controller->wq);
246 spin_unlock(&chip->controller->lock);
250 * nand_read_byte - [DEFAULT] read one byte from the chip
251 * @mtd: MTD device structure
253 * Default read function for 8bit buswidth
255 static uint8_t nand_read_byte(struct mtd_info *mtd)
257 struct nand_chip *chip = mtd_to_nand(mtd);
258 return readb(chip->IO_ADDR_R);
262 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
263 * @mtd: MTD device structure
265 * Default read function for 16bit buswidth with endianness conversion.
268 static uint8_t nand_read_byte16(struct mtd_info *mtd)
270 struct nand_chip *chip = mtd_to_nand(mtd);
271 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
275 * nand_read_word - [DEFAULT] read one word from the chip
276 * @mtd: MTD device structure
278 * Default read function for 16bit buswidth without endianness conversion.
280 static u16 nand_read_word(struct mtd_info *mtd)
282 struct nand_chip *chip = mtd_to_nand(mtd);
283 return readw(chip->IO_ADDR_R);
287 * nand_select_chip - [DEFAULT] control CE line
288 * @mtd: MTD device structure
289 * @chipnr: chipnumber to select, -1 for deselect
291 * Default select function for 1 chip devices.
293 static void nand_select_chip(struct mtd_info *mtd, int chipnr)
295 struct nand_chip *chip = mtd_to_nand(mtd);
299 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
310 * nand_write_byte - [DEFAULT] write single byte to chip
311 * @mtd: MTD device structure
312 * @byte: value to write
314 * Default function to write a byte to I/O[7:0]
316 static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
318 struct nand_chip *chip = mtd_to_nand(mtd);
320 chip->write_buf(mtd, &byte, 1);
324 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
325 * @mtd: MTD device structure
326 * @byte: value to write
328 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
330 static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
332 struct nand_chip *chip = mtd_to_nand(mtd);
333 uint16_t word = byte;
336 * It's not entirely clear what should happen to I/O[15:8] when writing
337 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
339 * When the host supports a 16-bit bus width, only data is
340 * transferred at the 16-bit width. All address and command line
341 * transfers shall use only the lower 8-bits of the data bus. During
342 * command transfers, the host may place any value on the upper
343 * 8-bits of the data bus. During address transfers, the host shall
344 * set the upper 8-bits of the data bus to 00h.
346 * One user of the write_byte callback is nand_onfi_set_features. The
347 * four parameters are specified to be written to I/O[7:0], but this is
348 * neither an address nor a command transfer. Let's assume a 0 on the
349 * upper I/O lines is OK.
351 chip->write_buf(mtd, (uint8_t *)&word, 2);
355 * nand_write_buf - [DEFAULT] write buffer to chip
356 * @mtd: MTD device structure
358 * @len: number of bytes to write
360 * Default write function for 8bit buswidth.
362 static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
364 struct nand_chip *chip = mtd_to_nand(mtd);
366 iowrite8_rep(chip->IO_ADDR_W, buf, len);
370 * nand_read_buf - [DEFAULT] read chip data into buffer
371 * @mtd: MTD device structure
372 * @buf: buffer to store date
373 * @len: number of bytes to read
375 * Default read function for 8bit buswidth.
377 static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
379 struct nand_chip *chip = mtd_to_nand(mtd);
381 ioread8_rep(chip->IO_ADDR_R, buf, len);
385 * nand_write_buf16 - [DEFAULT] write buffer to chip
386 * @mtd: MTD device structure
388 * @len: number of bytes to write
390 * Default write function for 16bit buswidth.
392 static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
394 struct nand_chip *chip = mtd_to_nand(mtd);
395 u16 *p = (u16 *) buf;
397 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
401 * nand_read_buf16 - [DEFAULT] read chip data into buffer
402 * @mtd: MTD device structure
403 * @buf: buffer to store date
404 * @len: number of bytes to read
406 * Default read function for 16bit buswidth.
408 static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
410 struct nand_chip *chip = mtd_to_nand(mtd);
411 u16 *p = (u16 *) buf;
413 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
417 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
418 * @mtd: MTD device structure
419 * @ofs: offset from device start
421 * Check, if the block is bad.
423 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
425 int page, page_end, res;
426 struct nand_chip *chip = mtd_to_nand(mtd);
429 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
430 ofs += mtd->erasesize - mtd->writesize;
432 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
433 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
435 for (; page < page_end; page++) {
436 res = chip->ecc.read_oob(mtd, chip, page);
440 bad = chip->oob_poi[chip->badblockpos];
442 if (likely(chip->badblockbits == 8))
445 res = hweight8(bad) < chip->badblockbits;
454 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
455 * @mtd: MTD device structure
456 * @ofs: offset from device start
458 * This is the default implementation, which can be overridden by a hardware
459 * specific driver. It provides the details for writing a bad block marker to a
462 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
464 struct nand_chip *chip = mtd_to_nand(mtd);
465 struct mtd_oob_ops ops;
466 uint8_t buf[2] = { 0, 0 };
467 int ret = 0, res, i = 0;
469 memset(&ops, 0, sizeof(ops));
471 ops.ooboffs = chip->badblockpos;
472 if (chip->options & NAND_BUSWIDTH_16) {
473 ops.ooboffs &= ~0x01;
474 ops.len = ops.ooblen = 2;
476 ops.len = ops.ooblen = 1;
478 ops.mode = MTD_OPS_PLACE_OOB;
480 /* Write to first/last page(s) if necessary */
481 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
482 ofs += mtd->erasesize - mtd->writesize;
484 res = nand_do_write_oob(mtd, ofs, &ops);
489 ofs += mtd->writesize;
490 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
496 * nand_block_markbad_lowlevel - mark a block bad
497 * @mtd: MTD device structure
498 * @ofs: offset from device start
500 * This function performs the generic NAND bad block marking steps (i.e., bad
501 * block table(s) and/or marker(s)). We only allow the hardware driver to
502 * specify how to write bad block markers to OOB (chip->block_markbad).
504 * We try operations in the following order:
505 * (1) erase the affected block, to allow OOB marker to be written cleanly
506 * (2) write bad block marker to OOB area of affected block (unless flag
507 * NAND_BBT_NO_OOB_BBM is present)
509 * Note that we retain the first error encountered in (2) or (3), finish the
510 * procedures, and dump the error in the end.
512 static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
514 struct nand_chip *chip = mtd_to_nand(mtd);
517 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
518 struct erase_info einfo;
520 /* Attempt erase before marking OOB */
521 memset(&einfo, 0, sizeof(einfo));
524 einfo.len = 1ULL << chip->phys_erase_shift;
525 nand_erase_nand(mtd, &einfo, 0);
527 /* Write bad block marker to OOB */
528 nand_get_device(mtd, FL_WRITING);
529 ret = chip->block_markbad(mtd, ofs);
530 nand_release_device(mtd);
533 /* Mark block bad in BBT */
535 res = nand_markbad_bbt(mtd, ofs);
541 mtd->ecc_stats.badblocks++;
547 * nand_check_wp - [GENERIC] check if the chip is write protected
548 * @mtd: MTD device structure
550 * Check, if the device is write protected. The function expects, that the
551 * device is already selected.
553 static int nand_check_wp(struct mtd_info *mtd)
555 struct nand_chip *chip = mtd_to_nand(mtd);
557 /* Broken xD cards report WP despite being writable */
558 if (chip->options & NAND_BROKEN_XD)
561 /* Check the WP bit */
562 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
563 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
567 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
568 * @mtd: MTD device structure
569 * @ofs: offset from device start
571 * Check if the block is marked as reserved.
573 static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
575 struct nand_chip *chip = mtd_to_nand(mtd);
579 /* Return info from the table */
580 return nand_isreserved_bbt(mtd, ofs);
584 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
585 * @mtd: MTD device structure
586 * @ofs: offset from device start
587 * @allowbbt: 1, if its allowed to access the bbt area
589 * Check, if the block is bad. Either by reading the bad block table or
590 * calling of the scan function.
592 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
594 struct nand_chip *chip = mtd_to_nand(mtd);
597 return chip->block_bad(mtd, ofs);
599 /* Return info from the table */
600 return nand_isbad_bbt(mtd, ofs, allowbbt);
604 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
605 * @mtd: MTD device structure
608 * Helper function for nand_wait_ready used when needing to wait in interrupt
611 static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
613 struct nand_chip *chip = mtd_to_nand(mtd);
616 /* Wait for the device to get ready */
617 for (i = 0; i < timeo; i++) {
618 if (chip->dev_ready(mtd))
620 touch_softlockup_watchdog();
626 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
627 * @mtd: MTD device structure
629 * Wait for the ready pin after a command, and warn if a timeout occurs.
631 void nand_wait_ready(struct mtd_info *mtd)
633 struct nand_chip *chip = mtd_to_nand(mtd);
634 unsigned long timeo = 400;
636 if (in_interrupt() || oops_in_progress)
637 return panic_nand_wait_ready(mtd, timeo);
639 /* Wait until command is processed or timeout occurs */
640 timeo = jiffies + msecs_to_jiffies(timeo);
642 if (chip->dev_ready(mtd))
645 } while (time_before(jiffies, timeo));
647 if (!chip->dev_ready(mtd))
648 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
650 EXPORT_SYMBOL_GPL(nand_wait_ready);
653 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
654 * @mtd: MTD device structure
655 * @timeo: Timeout in ms
657 * Wait for status ready (i.e. command done) or timeout.
659 static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
661 register struct nand_chip *chip = mtd_to_nand(mtd);
663 timeo = jiffies + msecs_to_jiffies(timeo);
665 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
667 touch_softlockup_watchdog();
668 } while (time_before(jiffies, timeo));
672 * nand_command - [DEFAULT] Send command to NAND device
673 * @mtd: MTD device structure
674 * @command: the command to be sent
675 * @column: the column address for this command, -1 if none
676 * @page_addr: the page address for this command, -1 if none
678 * Send command to NAND device. This function is used for small page devices
679 * (512 Bytes per page).
681 static void nand_command(struct mtd_info *mtd, unsigned int command,
682 int column, int page_addr)
684 register struct nand_chip *chip = mtd_to_nand(mtd);
685 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
687 /* Write out the command to the device */
688 if (command == NAND_CMD_SEQIN) {
691 if (column >= mtd->writesize) {
693 column -= mtd->writesize;
694 readcmd = NAND_CMD_READOOB;
695 } else if (column < 256) {
696 /* First 256 bytes --> READ0 */
697 readcmd = NAND_CMD_READ0;
700 readcmd = NAND_CMD_READ1;
702 chip->cmd_ctrl(mtd, readcmd, ctrl);
703 ctrl &= ~NAND_CTRL_CHANGE;
705 chip->cmd_ctrl(mtd, command, ctrl);
707 /* Address cycle, when necessary */
708 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
709 /* Serially input address */
711 /* Adjust columns for 16 bit buswidth */
712 if (chip->options & NAND_BUSWIDTH_16 &&
713 !nand_opcode_8bits(command))
715 chip->cmd_ctrl(mtd, column, ctrl);
716 ctrl &= ~NAND_CTRL_CHANGE;
718 if (page_addr != -1) {
719 chip->cmd_ctrl(mtd, page_addr, ctrl);
720 ctrl &= ~NAND_CTRL_CHANGE;
721 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
722 /* One more address cycle for devices > 32MiB */
723 if (chip->chipsize > (32 << 20))
724 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
726 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
729 * Program and erase have their own busy handlers status and sequential
734 case NAND_CMD_PAGEPROG:
735 case NAND_CMD_ERASE1:
736 case NAND_CMD_ERASE2:
738 case NAND_CMD_STATUS:
739 case NAND_CMD_READID:
740 case NAND_CMD_SET_FEATURES:
746 udelay(chip->chip_delay);
747 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
748 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
750 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
751 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
752 nand_wait_status_ready(mtd, 250);
755 /* This applies to read commands */
758 * If we don't have access to the busy pin, we apply the given
761 if (!chip->dev_ready) {
762 udelay(chip->chip_delay);
767 * Apply this short delay always to ensure that we do wait tWB in
768 * any case on any machine.
772 nand_wait_ready(mtd);
775 static void nand_ccs_delay(struct nand_chip *chip)
778 * The controller already takes care of waiting for tCCS when the RNDIN
779 * or RNDOUT command is sent, return directly.
781 if (!(chip->options & NAND_WAIT_TCCS))
785 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
786 * (which should be safe for all NANDs).
788 if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min)
789 ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000);
795 * nand_command_lp - [DEFAULT] Send command to NAND large page device
796 * @mtd: MTD device structure
797 * @command: the command to be sent
798 * @column: the column address for this command, -1 if none
799 * @page_addr: the page address for this command, -1 if none
801 * Send command to NAND device. This is the version for the new large page
802 * devices. We don't have the separate regions as we have in the small page
803 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
805 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
806 int column, int page_addr)
808 register struct nand_chip *chip = mtd_to_nand(mtd);
810 /* Emulate NAND_CMD_READOOB */
811 if (command == NAND_CMD_READOOB) {
812 column += mtd->writesize;
813 command = NAND_CMD_READ0;
816 /* Command latch cycle */
817 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
819 if (column != -1 || page_addr != -1) {
820 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
822 /* Serially input address */
824 /* Adjust columns for 16 bit buswidth */
825 if (chip->options & NAND_BUSWIDTH_16 &&
826 !nand_opcode_8bits(command))
828 chip->cmd_ctrl(mtd, column, ctrl);
829 ctrl &= ~NAND_CTRL_CHANGE;
831 /* Only output a single addr cycle for 8bits opcodes. */
832 if (!nand_opcode_8bits(command))
833 chip->cmd_ctrl(mtd, column >> 8, ctrl);
835 if (page_addr != -1) {
836 chip->cmd_ctrl(mtd, page_addr, ctrl);
837 chip->cmd_ctrl(mtd, page_addr >> 8,
838 NAND_NCE | NAND_ALE);
839 /* One more address cycle for devices > 128MiB */
840 if (chip->chipsize > (128 << 20))
841 chip->cmd_ctrl(mtd, page_addr >> 16,
842 NAND_NCE | NAND_ALE);
845 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
848 * Program and erase have their own busy handlers status, sequential
849 * in and status need no delay.
853 case NAND_CMD_CACHEDPROG:
854 case NAND_CMD_PAGEPROG:
855 case NAND_CMD_ERASE1:
856 case NAND_CMD_ERASE2:
858 case NAND_CMD_STATUS:
859 case NAND_CMD_READID:
860 case NAND_CMD_SET_FEATURES:
864 nand_ccs_delay(chip);
870 udelay(chip->chip_delay);
871 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
872 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
873 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
874 NAND_NCE | NAND_CTRL_CHANGE);
875 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
876 nand_wait_status_ready(mtd, 250);
879 case NAND_CMD_RNDOUT:
880 /* No ready / busy check necessary */
881 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
882 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
883 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
884 NAND_NCE | NAND_CTRL_CHANGE);
886 nand_ccs_delay(chip);
890 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
891 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
892 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
893 NAND_NCE | NAND_CTRL_CHANGE);
895 /* This applies to read commands */
898 * If we don't have access to the busy pin, we apply the given
901 if (!chip->dev_ready) {
902 udelay(chip->chip_delay);
908 * Apply this short delay always to ensure that we do wait tWB in
909 * any case on any machine.
913 nand_wait_ready(mtd);
917 * panic_nand_get_device - [GENERIC] Get chip for selected access
918 * @chip: the nand chip descriptor
919 * @mtd: MTD device structure
920 * @new_state: the state which is requested
922 * Used when in panic, no locks are taken.
924 static void panic_nand_get_device(struct nand_chip *chip,
925 struct mtd_info *mtd, int new_state)
927 /* Hardware controller shared among independent devices */
928 chip->controller->active = chip;
929 chip->state = new_state;
933 * nand_get_device - [GENERIC] Get chip for selected access
934 * @mtd: MTD device structure
935 * @new_state: the state which is requested
937 * Get the device and lock it for exclusive access
940 nand_get_device(struct mtd_info *mtd, int new_state)
942 struct nand_chip *chip = mtd_to_nand(mtd);
943 spinlock_t *lock = &chip->controller->lock;
944 wait_queue_head_t *wq = &chip->controller->wq;
945 DECLARE_WAITQUEUE(wait, current);
949 /* Hardware controller shared among independent devices */
950 if (!chip->controller->active)
951 chip->controller->active = chip;
953 if (chip->controller->active == chip && chip->state == FL_READY) {
954 chip->state = new_state;
958 if (new_state == FL_PM_SUSPENDED) {
959 if (chip->controller->active->state == FL_PM_SUSPENDED) {
960 chip->state = FL_PM_SUSPENDED;
965 set_current_state(TASK_UNINTERRUPTIBLE);
966 add_wait_queue(wq, &wait);
969 remove_wait_queue(wq, &wait);
974 * panic_nand_wait - [GENERIC] wait until the command is done
975 * @mtd: MTD device structure
976 * @chip: NAND chip structure
979 * Wait for command done. This is a helper function for nand_wait used when
980 * we are in interrupt context. May happen when in panic and trying to write
981 * an oops through mtdoops.
983 static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
987 for (i = 0; i < timeo; i++) {
988 if (chip->dev_ready) {
989 if (chip->dev_ready(mtd))
992 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1000 * nand_wait - [DEFAULT] wait until the command is done
1001 * @mtd: MTD device structure
1002 * @chip: NAND chip structure
1004 * Wait for command done. This applies to erase and program only.
1006 static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
1010 unsigned long timeo = 400;
1013 * Apply this short delay always to ensure that we do wait tWB in any
1014 * case on any machine.
1018 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1020 if (in_interrupt() || oops_in_progress)
1021 panic_nand_wait(mtd, chip, timeo);
1023 timeo = jiffies + msecs_to_jiffies(timeo);
1025 if (chip->dev_ready) {
1026 if (chip->dev_ready(mtd))
1029 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1033 } while (time_before(jiffies, timeo));
1036 status = (int)chip->read_byte(mtd);
1037 /* This can happen if in case of timeout or buggy dev_ready */
1038 WARN_ON(!(status & NAND_STATUS_READY));
1043 * nand_reset_data_interface - Reset data interface and timings
1044 * @chip: The NAND chip
1045 * @chipnr: Internal die id
1047 * Reset the Data interface and timings to ONFI mode 0.
1049 * Returns 0 for success or negative error code otherwise.
1051 static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
1053 struct mtd_info *mtd = nand_to_mtd(chip);
1054 const struct nand_data_interface *conf;
1057 if (!chip->setup_data_interface)
1061 * The ONFI specification says:
1063 * To transition from NV-DDR or NV-DDR2 to the SDR data
1064 * interface, the host shall use the Reset (FFh) command
1065 * using SDR timing mode 0. A device in any timing mode is
1066 * required to recognize Reset (FFh) command issued in SDR
1070 * Configure the data interface in SDR mode and set the
1071 * timings to timing mode 0.
1074 conf = nand_get_default_data_interface();
1075 ret = chip->setup_data_interface(mtd, chipnr, conf);
1077 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1083 * nand_setup_data_interface - Setup the best data interface and timings
1084 * @chip: The NAND chip
1085 * @chipnr: Internal die id
1087 * Find and configure the best data interface and NAND timings supported by
1088 * the chip and the driver.
1089 * First tries to retrieve supported timing modes from ONFI information,
1090 * and if the NAND chip does not support ONFI, relies on the
1091 * ->onfi_timing_mode_default specified in the nand_ids table.
1093 * Returns 0 for success or negative error code otherwise.
1095 static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
1097 struct mtd_info *mtd = nand_to_mtd(chip);
1100 if (!chip->setup_data_interface || !chip->data_interface)
1104 * Ensure the timing mode has been changed on the chip side
1105 * before changing timings on the controller side.
1107 if (chip->onfi_version) {
1108 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1109 chip->onfi_timing_mode_default,
1112 ret = chip->onfi_set_features(mtd, chip,
1113 ONFI_FEATURE_ADDR_TIMING_MODE,
1119 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
1125 * nand_init_data_interface - find the best data interface and timings
1126 * @chip: The NAND chip
1128 * Find the best data interface and NAND timings supported by the chip
1130 * First tries to retrieve supported timing modes from ONFI information,
1131 * and if the NAND chip does not support ONFI, relies on the
1132 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1133 * function nand_chip->data_interface is initialized with the best timing mode
1136 * Returns 0 for success or negative error code otherwise.
1138 static int nand_init_data_interface(struct nand_chip *chip)
1140 struct mtd_info *mtd = nand_to_mtd(chip);
1141 int modes, mode, ret;
1143 if (!chip->setup_data_interface)
1147 * First try to identify the best timings from ONFI parameters and
1148 * if the NAND does not support ONFI, fallback to the default ONFI
1151 modes = onfi_get_async_timing_mode(chip);
1152 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1153 if (!chip->onfi_timing_mode_default)
1156 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1159 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1161 if (!chip->data_interface)
1164 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1165 ret = onfi_init_data_interface(chip, chip->data_interface,
1166 NAND_SDR_IFACE, mode);
1170 /* Pass -1 to only */
1171 ret = chip->setup_data_interface(mtd,
1172 NAND_DATA_IFACE_CHECK_ONLY,
1173 chip->data_interface);
1175 chip->onfi_timing_mode_default = mode;
1183 static void nand_release_data_interface(struct nand_chip *chip)
1185 kfree(chip->data_interface);
1189 * nand_reset - Reset and initialize a NAND device
1190 * @chip: The NAND chip
1191 * @chipnr: Internal die id
1193 * Returns 0 for success or negative error code otherwise
1195 int nand_reset(struct nand_chip *chip, int chipnr)
1197 struct mtd_info *mtd = nand_to_mtd(chip);
1200 ret = nand_reset_data_interface(chip, chipnr);
1205 * The CS line has to be released before we can apply the new NAND
1206 * interface settings, hence this weird ->select_chip() dance.
1208 chip->select_chip(mtd, chipnr);
1209 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1210 chip->select_chip(mtd, -1);
1212 chip->select_chip(mtd, chipnr);
1213 ret = nand_setup_data_interface(chip, chipnr);
1214 chip->select_chip(mtd, -1);
1222 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
1224 * @ofs: offset to start unlock from
1225 * @len: length to unlock
1226 * @invert: when = 0, unlock the range of blocks within the lower and
1227 * upper boundary address
1228 * when = 1, unlock the range of blocks outside the boundaries
1229 * of the lower and upper boundary address
1231 * Returs unlock status.
1233 static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1234 uint64_t len, int invert)
1238 struct nand_chip *chip = mtd_to_nand(mtd);
1240 /* Submit address of first page to unlock */
1241 page = ofs >> chip->page_shift;
1242 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1244 /* Submit address of last page to unlock */
1245 page = (ofs + len) >> chip->page_shift;
1246 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1247 (page | invert) & chip->pagemask);
1249 /* Call wait ready function */
1250 status = chip->waitfunc(mtd, chip);
1251 /* See if device thinks it succeeded */
1252 if (status & NAND_STATUS_FAIL) {
1253 pr_debug("%s: error status = 0x%08x\n",
1262 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
1264 * @ofs: offset to start unlock from
1265 * @len: length to unlock
1267 * Returns unlock status.
1269 int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1273 struct nand_chip *chip = mtd_to_nand(mtd);
1275 pr_debug("%s: start = 0x%012llx, len = %llu\n",
1276 __func__, (unsigned long long)ofs, len);
1278 if (check_offs_len(mtd, ofs, len))
1281 /* Align to last block address if size addresses end of the device */
1282 if (ofs + len == mtd->size)
1283 len -= mtd->erasesize;
1285 nand_get_device(mtd, FL_UNLOCKING);
1287 /* Shift to get chip number */
1288 chipnr = ofs >> chip->chip_shift;
1292 * If we want to check the WP through READ STATUS and check the bit 7
1293 * we must reset the chip
1294 * some operation can also clear the bit 7 of status register
1295 * eg. erase/program a locked block
1297 nand_reset(chip, chipnr);
1299 chip->select_chip(mtd, chipnr);
1301 /* Check, if it is write protected */
1302 if (nand_check_wp(mtd)) {
1303 pr_debug("%s: device is write protected!\n",
1309 ret = __nand_unlock(mtd, ofs, len, 0);
1312 chip->select_chip(mtd, -1);
1313 nand_release_device(mtd);
1317 EXPORT_SYMBOL(nand_unlock);
1320 * nand_lock - [REPLACEABLE] locks all blocks present in the device
1322 * @ofs: offset to start unlock from
1323 * @len: length to unlock
1325 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1326 * have this feature, but it allows only to lock all blocks, not for specified
1327 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1330 * Returns lock status.
1332 int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1335 int chipnr, status, page;
1336 struct nand_chip *chip = mtd_to_nand(mtd);
1338 pr_debug("%s: start = 0x%012llx, len = %llu\n",
1339 __func__, (unsigned long long)ofs, len);
1341 if (check_offs_len(mtd, ofs, len))
1344 nand_get_device(mtd, FL_LOCKING);
1346 /* Shift to get chip number */
1347 chipnr = ofs >> chip->chip_shift;
1351 * If we want to check the WP through READ STATUS and check the bit 7
1352 * we must reset the chip
1353 * some operation can also clear the bit 7 of status register
1354 * eg. erase/program a locked block
1356 nand_reset(chip, chipnr);
1358 chip->select_chip(mtd, chipnr);
1360 /* Check, if it is write protected */
1361 if (nand_check_wp(mtd)) {
1362 pr_debug("%s: device is write protected!\n",
1364 status = MTD_ERASE_FAILED;
1369 /* Submit address of first page to lock */
1370 page = ofs >> chip->page_shift;
1371 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1373 /* Call wait ready function */
1374 status = chip->waitfunc(mtd, chip);
1375 /* See if device thinks it succeeded */
1376 if (status & NAND_STATUS_FAIL) {
1377 pr_debug("%s: error status = 0x%08x\n",
1383 ret = __nand_unlock(mtd, ofs, len, 0x1);
1386 chip->select_chip(mtd, -1);
1387 nand_release_device(mtd);
1391 EXPORT_SYMBOL(nand_lock);
1394 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1395 * @buf: buffer to test
1396 * @len: buffer length
1397 * @bitflips_threshold: maximum number of bitflips
1399 * Check if a buffer contains only 0xff, which means the underlying region
1400 * has been erased and is ready to be programmed.
1401 * The bitflips_threshold specify the maximum number of bitflips before
1402 * considering the region is not erased.
1403 * Note: The logic of this function has been extracted from the memweight
1404 * implementation, except that nand_check_erased_buf function exit before
1405 * testing the whole buffer if the number of bitflips exceed the
1406 * bitflips_threshold value.
1408 * Returns a positive number of bitflips less than or equal to
1409 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1412 static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1414 const unsigned char *bitmap = buf;
1418 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1420 weight = hweight8(*bitmap);
1421 bitflips += BITS_PER_BYTE - weight;
1422 if (unlikely(bitflips > bitflips_threshold))
1426 for (; len >= sizeof(long);
1427 len -= sizeof(long), bitmap += sizeof(long)) {
1428 unsigned long d = *((unsigned long *)bitmap);
1431 weight = hweight_long(d);
1432 bitflips += BITS_PER_LONG - weight;
1433 if (unlikely(bitflips > bitflips_threshold))
1437 for (; len > 0; len--, bitmap++) {
1438 weight = hweight8(*bitmap);
1439 bitflips += BITS_PER_BYTE - weight;
1440 if (unlikely(bitflips > bitflips_threshold))
1448 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1450 * @data: data buffer to test
1451 * @datalen: data length
1453 * @ecclen: ECC length
1454 * @extraoob: extra OOB buffer
1455 * @extraooblen: extra OOB length
1456 * @bitflips_threshold: maximum number of bitflips
1458 * Check if a data buffer and its associated ECC and OOB data contains only
1459 * 0xff pattern, which means the underlying region has been erased and is
1460 * ready to be programmed.
1461 * The bitflips_threshold specify the maximum number of bitflips before
1462 * considering the region as not erased.
1465 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1466 * different from the NAND page size. When fixing bitflips, ECC engines will
1467 * report the number of errors per chunk, and the NAND core infrastructure
1468 * expect you to return the maximum number of bitflips for the whole page.
1469 * This is why you should always use this function on a single chunk and
1470 * not on the whole page. After checking each chunk you should update your
1471 * max_bitflips value accordingly.
1472 * 2/ When checking for bitflips in erased pages you should not only check
1473 * the payload data but also their associated ECC data, because a user might
1474 * have programmed almost all bits to 1 but a few. In this case, we
1475 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1477 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1478 * data are protected by the ECC engine.
1479 * It could also be used if you support subpages and want to attach some
1480 * extra OOB data to an ECC chunk.
1482 * Returns a positive number of bitflips less than or equal to
1483 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1484 * threshold. In case of success, the passed buffers are filled with 0xff.
1486 int nand_check_erased_ecc_chunk(void *data, int datalen,
1487 void *ecc, int ecclen,
1488 void *extraoob, int extraooblen,
1489 int bitflips_threshold)
1491 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1493 data_bitflips = nand_check_erased_buf(data, datalen,
1494 bitflips_threshold);
1495 if (data_bitflips < 0)
1496 return data_bitflips;
1498 bitflips_threshold -= data_bitflips;
1500 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1501 if (ecc_bitflips < 0)
1502 return ecc_bitflips;
1504 bitflips_threshold -= ecc_bitflips;
1506 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1507 bitflips_threshold);
1508 if (extraoob_bitflips < 0)
1509 return extraoob_bitflips;
1512 memset(data, 0xff, datalen);
1515 memset(ecc, 0xff, ecclen);
1517 if (extraoob_bitflips)
1518 memset(extraoob, 0xff, extraooblen);
1520 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1522 EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1525 * nand_read_page_raw - [INTERN] read raw page data without ecc
1526 * @mtd: mtd info structure
1527 * @chip: nand chip info structure
1528 * @buf: buffer to store read data
1529 * @oob_required: caller requires OOB data read to chip->oob_poi
1530 * @page: page number to read
1532 * Not for syndrome calculating ECC controllers, which use a special oob layout.
1534 int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1535 uint8_t *buf, int oob_required, int page)
1537 chip->read_buf(mtd, buf, mtd->writesize);
1539 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1542 EXPORT_SYMBOL(nand_read_page_raw);
1545 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1546 * @mtd: mtd info structure
1547 * @chip: nand chip info structure
1548 * @buf: buffer to store read data
1549 * @oob_required: caller requires OOB data read to chip->oob_poi
1550 * @page: page number to read
1552 * We need a special oob layout and handling even when OOB isn't used.
1554 static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
1555 struct nand_chip *chip, uint8_t *buf,
1556 int oob_required, int page)
1558 int eccsize = chip->ecc.size;
1559 int eccbytes = chip->ecc.bytes;
1560 uint8_t *oob = chip->oob_poi;
1563 for (steps = chip->ecc.steps; steps > 0; steps--) {
1564 chip->read_buf(mtd, buf, eccsize);
1567 if (chip->ecc.prepad) {
1568 chip->read_buf(mtd, oob, chip->ecc.prepad);
1569 oob += chip->ecc.prepad;
1572 chip->read_buf(mtd, oob, eccbytes);
1575 if (chip->ecc.postpad) {
1576 chip->read_buf(mtd, oob, chip->ecc.postpad);
1577 oob += chip->ecc.postpad;
1581 size = mtd->oobsize - (oob - chip->oob_poi);
1583 chip->read_buf(mtd, oob, size);
1589 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1590 * @mtd: mtd info structure
1591 * @chip: nand chip info structure
1592 * @buf: buffer to store read data
1593 * @oob_required: caller requires OOB data read to chip->oob_poi
1594 * @page: page number to read
1596 static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1597 uint8_t *buf, int oob_required, int page)
1599 int i, eccsize = chip->ecc.size, ret;
1600 int eccbytes = chip->ecc.bytes;
1601 int eccsteps = chip->ecc.steps;
1603 uint8_t *ecc_calc = chip->buffers->ecccalc;
1604 uint8_t *ecc_code = chip->buffers->ecccode;
1605 unsigned int max_bitflips = 0;
1607 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
1609 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1610 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1612 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1617 eccsteps = chip->ecc.steps;
1620 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1623 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1625 mtd->ecc_stats.failed++;
1627 mtd->ecc_stats.corrected += stat;
1628 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1631 return max_bitflips;
1635 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
1636 * @mtd: mtd info structure
1637 * @chip: nand chip info structure
1638 * @data_offs: offset of requested data within the page
1639 * @readlen: data length
1640 * @bufpoi: buffer to store read data
1641 * @page: page number to read
1643 static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1644 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1647 int start_step, end_step, num_steps, ret;
1649 int data_col_addr, i, gaps = 0;
1650 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1651 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
1652 int index, section = 0;
1653 unsigned int max_bitflips = 0;
1654 struct mtd_oob_region oobregion = { };
1656 /* Column address within the page aligned to ECC size (256bytes) */
1657 start_step = data_offs / chip->ecc.size;
1658 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1659 num_steps = end_step - start_step + 1;
1660 index = start_step * chip->ecc.bytes;
1662 /* Data size aligned to ECC ecc.size */
1663 datafrag_len = num_steps * chip->ecc.size;
1664 eccfrag_len = num_steps * chip->ecc.bytes;
1666 data_col_addr = start_step * chip->ecc.size;
1667 /* If we read not a page aligned data */
1668 if (data_col_addr != 0)
1669 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1671 p = bufpoi + data_col_addr;
1672 chip->read_buf(mtd, p, datafrag_len);
1675 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1676 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1679 * The performance is faster if we position offsets according to
1680 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1682 ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion);
1686 if (oobregion.length < eccfrag_len)
1690 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1691 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1694 * Send the command to read the particular ECC bytes take care
1695 * about buswidth alignment in read_buf.
1697 aligned_pos = oobregion.offset & ~(busw - 1);
1698 aligned_len = eccfrag_len;
1699 if (oobregion.offset & (busw - 1))
1701 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1705 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1706 mtd->writesize + aligned_pos, -1);
1707 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1710 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1711 chip->oob_poi, index, eccfrag_len);
1715 p = bufpoi + data_col_addr;
1716 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1719 stat = chip->ecc.correct(mtd, p,
1720 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1721 if (stat == -EBADMSG &&
1722 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1723 /* check for empty pages with bitflips */
1724 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1725 &chip->buffers->ecccode[i],
1728 chip->ecc.strength);
1732 mtd->ecc_stats.failed++;
1734 mtd->ecc_stats.corrected += stat;
1735 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1738 return max_bitflips;
1742 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1743 * @mtd: mtd info structure
1744 * @chip: nand chip info structure
1745 * @buf: buffer to store read data
1746 * @oob_required: caller requires OOB data read to chip->oob_poi
1747 * @page: page number to read
1749 * Not for syndrome calculating ECC controllers which need a special oob layout.
1751 static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1752 uint8_t *buf, int oob_required, int page)
1754 int i, eccsize = chip->ecc.size, ret;
1755 int eccbytes = chip->ecc.bytes;
1756 int eccsteps = chip->ecc.steps;
1758 uint8_t *ecc_calc = chip->buffers->ecccalc;
1759 uint8_t *ecc_code = chip->buffers->ecccode;
1760 unsigned int max_bitflips = 0;
1762 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1763 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1764 chip->read_buf(mtd, p, eccsize);
1765 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1767 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1769 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1774 eccsteps = chip->ecc.steps;
1777 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1780 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1781 if (stat == -EBADMSG &&
1782 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1783 /* check for empty pages with bitflips */
1784 stat = nand_check_erased_ecc_chunk(p, eccsize,
1785 &ecc_code[i], eccbytes,
1787 chip->ecc.strength);
1791 mtd->ecc_stats.failed++;
1793 mtd->ecc_stats.corrected += stat;
1794 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1797 return max_bitflips;
1801 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1802 * @mtd: mtd info structure
1803 * @chip: nand chip info structure
1804 * @buf: buffer to store read data
1805 * @oob_required: caller requires OOB data read to chip->oob_poi
1806 * @page: page number to read
1808 * Hardware ECC for large page chips, require OOB to be read first. For this
1809 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1810 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1811 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1812 * the data area, by overwriting the NAND manufacturer bad block markings.
1814 static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1815 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
1817 int i, eccsize = chip->ecc.size, ret;
1818 int eccbytes = chip->ecc.bytes;
1819 int eccsteps = chip->ecc.steps;
1821 uint8_t *ecc_code = chip->buffers->ecccode;
1822 uint8_t *ecc_calc = chip->buffers->ecccalc;
1823 unsigned int max_bitflips = 0;
1825 /* Read the OOB area first */
1826 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1827 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1828 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1830 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1835 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1838 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1839 chip->read_buf(mtd, p, eccsize);
1840 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1842 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1843 if (stat == -EBADMSG &&
1844 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1845 /* check for empty pages with bitflips */
1846 stat = nand_check_erased_ecc_chunk(p, eccsize,
1847 &ecc_code[i], eccbytes,
1849 chip->ecc.strength);
1853 mtd->ecc_stats.failed++;
1855 mtd->ecc_stats.corrected += stat;
1856 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1859 return max_bitflips;
1863 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1864 * @mtd: mtd info structure
1865 * @chip: nand chip info structure
1866 * @buf: buffer to store read data
1867 * @oob_required: caller requires OOB data read to chip->oob_poi
1868 * @page: page number to read
1870 * The hw generator calculates the error syndrome automatically. Therefore we
1871 * need a special oob layout and handling.
1873 static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1874 uint8_t *buf, int oob_required, int page)
1876 int i, eccsize = chip->ecc.size;
1877 int eccbytes = chip->ecc.bytes;
1878 int eccsteps = chip->ecc.steps;
1879 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
1881 uint8_t *oob = chip->oob_poi;
1882 unsigned int max_bitflips = 0;
1884 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1887 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1888 chip->read_buf(mtd, p, eccsize);
1890 if (chip->ecc.prepad) {
1891 chip->read_buf(mtd, oob, chip->ecc.prepad);
1892 oob += chip->ecc.prepad;
1895 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1896 chip->read_buf(mtd, oob, eccbytes);
1897 stat = chip->ecc.correct(mtd, p, oob, NULL);
1901 if (chip->ecc.postpad) {
1902 chip->read_buf(mtd, oob, chip->ecc.postpad);
1903 oob += chip->ecc.postpad;
1906 if (stat == -EBADMSG &&
1907 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1908 /* check for empty pages with bitflips */
1909 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1913 chip->ecc.strength);
1917 mtd->ecc_stats.failed++;
1919 mtd->ecc_stats.corrected += stat;
1920 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1924 /* Calculate remaining oob bytes */
1925 i = mtd->oobsize - (oob - chip->oob_poi);
1927 chip->read_buf(mtd, oob, i);
1929 return max_bitflips;
1933 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1934 * @mtd: mtd info structure
1935 * @oob: oob destination address
1936 * @ops: oob ops structure
1937 * @len: size of oob to transfer
1939 static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
1940 struct mtd_oob_ops *ops, size_t len)
1942 struct nand_chip *chip = mtd_to_nand(mtd);
1945 switch (ops->mode) {
1947 case MTD_OPS_PLACE_OOB:
1949 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1952 case MTD_OPS_AUTO_OOB:
1953 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1965 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1966 * @mtd: MTD device structure
1967 * @retry_mode: the retry mode to use
1969 * Some vendors supply a special command to shift the Vt threshold, to be used
1970 * when there are too many bitflips in a page (i.e., ECC error). After setting
1971 * a new threshold, the host should retry reading the page.
1973 static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1975 struct nand_chip *chip = mtd_to_nand(mtd);
1977 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1979 if (retry_mode >= chip->read_retries)
1982 if (!chip->setup_read_retry)
1985 return chip->setup_read_retry(mtd, retry_mode);
1989 * nand_do_read_ops - [INTERN] Read data with ECC
1990 * @mtd: MTD device structure
1991 * @from: offset to read from
1992 * @ops: oob ops structure
1994 * Internal function. Called with chip held.
1996 static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1997 struct mtd_oob_ops *ops)
1999 int chipnr, page, realpage, col, bytes, aligned, oob_required;
2000 struct nand_chip *chip = mtd_to_nand(mtd);
2002 uint32_t readlen = ops->len;
2003 uint32_t oobreadlen = ops->ooblen;
2004 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
2006 uint8_t *bufpoi, *oob, *buf;
2008 unsigned int max_bitflips = 0;
2010 bool ecc_fail = false;
2012 chipnr = (int)(from >> chip->chip_shift);
2013 chip->select_chip(mtd, chipnr);
2015 realpage = (int)(from >> chip->page_shift);
2016 page = realpage & chip->pagemask;
2018 col = (int)(from & (mtd->writesize - 1));
2022 oob_required = oob ? 1 : 0;
2025 unsigned int ecc_failures = mtd->ecc_stats.failed;
2027 bytes = min(mtd->writesize - col, readlen);
2028 aligned = (bytes == mtd->writesize);
2032 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2033 use_bufpoi = !virt_addr_valid(buf) ||
2034 !IS_ALIGNED((unsigned long)buf,
2039 /* Is the current page in the buffer? */
2040 if (realpage != chip->pagebuf || oob) {
2041 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2043 if (use_bufpoi && aligned)
2044 pr_debug("%s: using read bounce buffer for buf@%p\n",
2048 if (nand_standard_page_accessors(&chip->ecc))
2049 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
2052 * Now read the page into the buffer. Absent an error,
2053 * the read methods return max bitflips per ecc step.
2055 if (unlikely(ops->mode == MTD_OPS_RAW))
2056 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
2059 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
2061 ret = chip->ecc.read_subpage(mtd, chip,
2065 ret = chip->ecc.read_page(mtd, chip, bufpoi,
2066 oob_required, page);
2069 /* Invalidate page cache */
2074 /* Transfer not aligned data */
2076 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
2077 !(mtd->ecc_stats.failed - ecc_failures) &&
2078 (ops->mode != MTD_OPS_RAW)) {
2079 chip->pagebuf = realpage;
2080 chip->pagebuf_bitflips = ret;
2082 /* Invalidate page cache */
2085 memcpy(buf, chip->buffers->databuf + col, bytes);
2088 if (unlikely(oob)) {
2089 int toread = min(oobreadlen, max_oobsize);
2092 oob = nand_transfer_oob(mtd,
2094 oobreadlen -= toread;
2098 if (chip->options & NAND_NEED_READRDY) {
2099 /* Apply delay or wait for ready/busy pin */
2100 if (!chip->dev_ready)
2101 udelay(chip->chip_delay);
2103 nand_wait_ready(mtd);
2106 if (mtd->ecc_stats.failed - ecc_failures) {
2107 if (retry_mode + 1 < chip->read_retries) {
2109 ret = nand_setup_read_retry(mtd,
2114 /* Reset failures; retry */
2115 mtd->ecc_stats.failed = ecc_failures;
2118 /* No more retry modes; real failure */
2124 max_bitflips = max_t(unsigned int, max_bitflips, ret);
2126 memcpy(buf, chip->buffers->databuf + col, bytes);
2128 max_bitflips = max_t(unsigned int, max_bitflips,
2129 chip->pagebuf_bitflips);
2134 /* Reset to retry mode 0 */
2136 ret = nand_setup_read_retry(mtd, 0);
2145 /* For subsequent reads align to page boundary */
2147 /* Increment page address */
2150 page = realpage & chip->pagemask;
2151 /* Check, if we cross a chip boundary */
2154 chip->select_chip(mtd, -1);
2155 chip->select_chip(mtd, chipnr);
2158 chip->select_chip(mtd, -1);
2160 ops->retlen = ops->len - (size_t) readlen;
2162 ops->oobretlen = ops->ooblen - oobreadlen;
2170 return max_bitflips;
2174 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
2175 * @mtd: MTD device structure
2176 * @from: offset to read from
2177 * @len: number of bytes to read
2178 * @retlen: pointer to variable to store the number of read bytes
2179 * @buf: the databuffer to put data
2181 * Get hold of the chip and call nand_do_read.
2183 static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2184 size_t *retlen, uint8_t *buf)
2186 struct mtd_oob_ops ops;
2189 nand_get_device(mtd, FL_READING);
2190 memset(&ops, 0, sizeof(ops));
2193 ops.mode = MTD_OPS_PLACE_OOB;
2194 ret = nand_do_read_ops(mtd, from, &ops);
2195 *retlen = ops.retlen;
2196 nand_release_device(mtd);
2201 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
2202 * @mtd: mtd info structure
2203 * @chip: nand chip info structure
2204 * @page: page number to read
2206 int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
2208 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
2209 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
2212 EXPORT_SYMBOL(nand_read_oob_std);
2215 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
2217 * @mtd: mtd info structure
2218 * @chip: nand chip info structure
2219 * @page: page number to read
2221 int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2224 int length = mtd->oobsize;
2225 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2226 int eccsize = chip->ecc.size;
2227 uint8_t *bufpoi = chip->oob_poi;
2228 int i, toread, sndrnd = 0, pos;
2230 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2231 for (i = 0; i < chip->ecc.steps; i++) {
2233 pos = eccsize + i * (eccsize + chunk);
2234 if (mtd->writesize > 512)
2235 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2237 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2240 toread = min_t(int, length, chunk);
2241 chip->read_buf(mtd, bufpoi, toread);
2246 chip->read_buf(mtd, bufpoi, length);
2250 EXPORT_SYMBOL(nand_read_oob_syndrome);
2253 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
2254 * @mtd: mtd info structure
2255 * @chip: nand chip info structure
2256 * @page: page number to write
2258 int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
2261 const uint8_t *buf = chip->oob_poi;
2262 int length = mtd->oobsize;
2264 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2265 chip->write_buf(mtd, buf, length);
2266 /* Send command to program the OOB data */
2267 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2269 status = chip->waitfunc(mtd, chip);
2271 return status & NAND_STATUS_FAIL ? -EIO : 0;
2273 EXPORT_SYMBOL(nand_write_oob_std);
2276 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
2277 * with syndrome - only for large page flash
2278 * @mtd: mtd info structure
2279 * @chip: nand chip info structure
2280 * @page: page number to write
2282 int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2285 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2286 int eccsize = chip->ecc.size, length = mtd->oobsize;
2287 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2288 const uint8_t *bufpoi = chip->oob_poi;
2291 * data-ecc-data-ecc ... ecc-oob
2293 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2295 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2296 pos = steps * (eccsize + chunk);
2301 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2302 for (i = 0; i < steps; i++) {
2304 if (mtd->writesize <= 512) {
2305 uint32_t fill = 0xFFFFFFFF;
2309 int num = min_t(int, len, 4);
2310 chip->write_buf(mtd, (uint8_t *)&fill,
2315 pos = eccsize + i * (eccsize + chunk);
2316 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2320 len = min_t(int, length, chunk);
2321 chip->write_buf(mtd, bufpoi, len);
2326 chip->write_buf(mtd, bufpoi, length);
2328 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2329 status = chip->waitfunc(mtd, chip);
2331 return status & NAND_STATUS_FAIL ? -EIO : 0;
2333 EXPORT_SYMBOL(nand_write_oob_syndrome);
2336 * nand_do_read_oob - [INTERN] NAND read out-of-band
2337 * @mtd: MTD device structure
2338 * @from: offset to read from
2339 * @ops: oob operations description structure
2341 * NAND read out-of-band data from the spare area.
2343 static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2344 struct mtd_oob_ops *ops)
2346 int page, realpage, chipnr;
2347 struct nand_chip *chip = mtd_to_nand(mtd);
2348 struct mtd_ecc_stats stats;
2349 int readlen = ops->ooblen;
2351 uint8_t *buf = ops->oobbuf;
2354 pr_debug("%s: from = 0x%08Lx, len = %i\n",
2355 __func__, (unsigned long long)from, readlen);
2357 stats = mtd->ecc_stats;
2359 len = mtd_oobavail(mtd, ops);
2361 if (unlikely(ops->ooboffs >= len)) {
2362 pr_debug("%s: attempt to start read outside oob\n",
2367 /* Do not allow reads past end of device */
2368 if (unlikely(from >= mtd->size ||
2369 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2370 (from >> chip->page_shift)) * len)) {
2371 pr_debug("%s: attempt to read beyond end of device\n",
2376 chipnr = (int)(from >> chip->chip_shift);
2377 chip->select_chip(mtd, chipnr);
2379 /* Shift to get page */
2380 realpage = (int)(from >> chip->page_shift);
2381 page = realpage & chip->pagemask;
2384 if (ops->mode == MTD_OPS_RAW)
2385 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2387 ret = chip->ecc.read_oob(mtd, chip, page);
2392 len = min(len, readlen);
2393 buf = nand_transfer_oob(mtd, buf, ops, len);
2395 if (chip->options & NAND_NEED_READRDY) {
2396 /* Apply delay or wait for ready/busy pin */
2397 if (!chip->dev_ready)
2398 udelay(chip->chip_delay);
2400 nand_wait_ready(mtd);
2407 /* Increment page address */
2410 page = realpage & chip->pagemask;
2411 /* Check, if we cross a chip boundary */
2414 chip->select_chip(mtd, -1);
2415 chip->select_chip(mtd, chipnr);
2418 chip->select_chip(mtd, -1);
2420 ops->oobretlen = ops->ooblen - readlen;
2425 if (mtd->ecc_stats.failed - stats.failed)
2428 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
2432 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
2433 * @mtd: MTD device structure
2434 * @from: offset to read from
2435 * @ops: oob operation description structure
2437 * NAND read data and/or out-of-band data.
2439 static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2440 struct mtd_oob_ops *ops)
2446 /* Do not allow reads past end of device */
2447 if (ops->datbuf && (from + ops->len) > mtd->size) {
2448 pr_debug("%s: attempt to read beyond end of device\n",
2453 if (ops->mode != MTD_OPS_PLACE_OOB &&
2454 ops->mode != MTD_OPS_AUTO_OOB &&
2455 ops->mode != MTD_OPS_RAW)
2458 nand_get_device(mtd, FL_READING);
2461 ret = nand_do_read_oob(mtd, from, ops);
2463 ret = nand_do_read_ops(mtd, from, ops);
2465 nand_release_device(mtd);
2471 * nand_write_page_raw - [INTERN] raw page write function
2472 * @mtd: mtd info structure
2473 * @chip: nand chip info structure
2475 * @oob_required: must write chip->oob_poi to OOB
2476 * @page: page number to write
2478 * Not for syndrome calculating ECC controllers, which use a special oob layout.
2480 int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2481 const uint8_t *buf, int oob_required, int page)
2483 chip->write_buf(mtd, buf, mtd->writesize);
2485 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2489 EXPORT_SYMBOL(nand_write_page_raw);
2492 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2493 * @mtd: mtd info structure
2494 * @chip: nand chip info structure
2496 * @oob_required: must write chip->oob_poi to OOB
2497 * @page: page number to write
2499 * We need a special oob layout and handling even when ECC isn't checked.
2501 static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
2502 struct nand_chip *chip,
2503 const uint8_t *buf, int oob_required,
2506 int eccsize = chip->ecc.size;
2507 int eccbytes = chip->ecc.bytes;
2508 uint8_t *oob = chip->oob_poi;
2511 for (steps = chip->ecc.steps; steps > 0; steps--) {
2512 chip->write_buf(mtd, buf, eccsize);
2515 if (chip->ecc.prepad) {
2516 chip->write_buf(mtd, oob, chip->ecc.prepad);
2517 oob += chip->ecc.prepad;
2520 chip->write_buf(mtd, oob, eccbytes);
2523 if (chip->ecc.postpad) {
2524 chip->write_buf(mtd, oob, chip->ecc.postpad);
2525 oob += chip->ecc.postpad;
2529 size = mtd->oobsize - (oob - chip->oob_poi);
2531 chip->write_buf(mtd, oob, size);
2536 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2537 * @mtd: mtd info structure
2538 * @chip: nand chip info structure
2540 * @oob_required: must write chip->oob_poi to OOB
2541 * @page: page number to write
2543 static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
2544 const uint8_t *buf, int oob_required,
2547 int i, eccsize = chip->ecc.size, ret;
2548 int eccbytes = chip->ecc.bytes;
2549 int eccsteps = chip->ecc.steps;
2550 uint8_t *ecc_calc = chip->buffers->ecccalc;
2551 const uint8_t *p = buf;
2553 /* Software ECC calculation */
2554 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2555 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2557 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2562 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
2566 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2567 * @mtd: mtd info structure
2568 * @chip: nand chip info structure
2570 * @oob_required: must write chip->oob_poi to OOB
2571 * @page: page number to write
2573 static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
2574 const uint8_t *buf, int oob_required,
2577 int i, eccsize = chip->ecc.size, ret;
2578 int eccbytes = chip->ecc.bytes;
2579 int eccsteps = chip->ecc.steps;
2580 uint8_t *ecc_calc = chip->buffers->ecccalc;
2581 const uint8_t *p = buf;
2583 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2584 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2585 chip->write_buf(mtd, p, eccsize);
2586 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2589 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2594 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2601 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
2602 * @mtd: mtd info structure
2603 * @chip: nand chip info structure
2604 * @offset: column address of subpage within the page
2605 * @data_len: data length
2607 * @oob_required: must write chip->oob_poi to OOB
2608 * @page: page number to write
2610 static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2611 struct nand_chip *chip, uint32_t offset,
2612 uint32_t data_len, const uint8_t *buf,
2613 int oob_required, int page)
2615 uint8_t *oob_buf = chip->oob_poi;
2616 uint8_t *ecc_calc = chip->buffers->ecccalc;
2617 int ecc_size = chip->ecc.size;
2618 int ecc_bytes = chip->ecc.bytes;
2619 int ecc_steps = chip->ecc.steps;
2620 uint32_t start_step = offset / ecc_size;
2621 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2622 int oob_bytes = mtd->oobsize / ecc_steps;
2625 for (step = 0; step < ecc_steps; step++) {
2626 /* configure controller for WRITE access */
2627 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2629 /* write data (untouched subpages already masked by 0xFF) */
2630 chip->write_buf(mtd, buf, ecc_size);
2632 /* mask ECC of un-touched subpages by padding 0xFF */
2633 if ((step < start_step) || (step > end_step))
2634 memset(ecc_calc, 0xff, ecc_bytes);
2636 chip->ecc.calculate(mtd, buf, ecc_calc);
2638 /* mask OOB of un-touched subpages by padding 0xFF */
2639 /* if oob_required, preserve OOB metadata of written subpage */
2640 if (!oob_required || (step < start_step) || (step > end_step))
2641 memset(oob_buf, 0xff, oob_bytes);
2644 ecc_calc += ecc_bytes;
2645 oob_buf += oob_bytes;
2648 /* copy calculated ECC for whole page to chip->buffer->oob */
2649 /* this include masked-value(0xFF) for unwritten subpages */
2650 ecc_calc = chip->buffers->ecccalc;
2651 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2656 /* write OOB buffer to NAND device */
2657 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2664 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2665 * @mtd: mtd info structure
2666 * @chip: nand chip info structure
2668 * @oob_required: must write chip->oob_poi to OOB
2669 * @page: page number to write
2671 * The hw generator calculates the error syndrome automatically. Therefore we
2672 * need a special oob layout and handling.
2674 static int nand_write_page_syndrome(struct mtd_info *mtd,
2675 struct nand_chip *chip,
2676 const uint8_t *buf, int oob_required,
2679 int i, eccsize = chip->ecc.size;
2680 int eccbytes = chip->ecc.bytes;
2681 int eccsteps = chip->ecc.steps;
2682 const uint8_t *p = buf;
2683 uint8_t *oob = chip->oob_poi;
2685 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2687 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2688 chip->write_buf(mtd, p, eccsize);
2690 if (chip->ecc.prepad) {
2691 chip->write_buf(mtd, oob, chip->ecc.prepad);
2692 oob += chip->ecc.prepad;
2695 chip->ecc.calculate(mtd, p, oob);
2696 chip->write_buf(mtd, oob, eccbytes);
2699 if (chip->ecc.postpad) {
2700 chip->write_buf(mtd, oob, chip->ecc.postpad);
2701 oob += chip->ecc.postpad;
2705 /* Calculate remaining oob bytes */
2706 i = mtd->oobsize - (oob - chip->oob_poi);
2708 chip->write_buf(mtd, oob, i);
2714 * nand_write_page - write one page
2715 * @mtd: MTD device structure
2716 * @chip: NAND chip descriptor
2717 * @offset: address offset within the page
2718 * @data_len: length of actual data to be written
2719 * @buf: the data to write
2720 * @oob_required: must write chip->oob_poi to OOB
2721 * @page: page number to write
2722 * @cached: cached programming
2723 * @raw: use _raw version of write_page
2725 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
2726 uint32_t offset, int data_len, const uint8_t *buf,
2727 int oob_required, int page, int raw)
2729 int status, subpage;
2731 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2732 chip->ecc.write_subpage)
2733 subpage = offset || (data_len < mtd->writesize);
2737 if (nand_standard_page_accessors(&chip->ecc))
2738 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2741 status = chip->ecc.write_page_raw(mtd, chip, buf,
2742 oob_required, page);
2744 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2745 buf, oob_required, page);
2747 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2753 if (nand_standard_page_accessors(&chip->ecc))
2754 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2755 status = chip->waitfunc(mtd, chip);
2756 if (status & NAND_STATUS_FAIL)
2763 * nand_fill_oob - [INTERN] Transfer client buffer to oob
2764 * @mtd: MTD device structure
2765 * @oob: oob data buffer
2766 * @len: oob data write length
2767 * @ops: oob ops structure
2769 static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2770 struct mtd_oob_ops *ops)
2772 struct nand_chip *chip = mtd_to_nand(mtd);
2776 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2777 * data from a previous OOB read.
2779 memset(chip->oob_poi, 0xff, mtd->oobsize);
2781 switch (ops->mode) {
2783 case MTD_OPS_PLACE_OOB:
2785 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2788 case MTD_OPS_AUTO_OOB:
2789 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2800 #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
2803 * nand_do_write_ops - [INTERN] NAND write with ECC
2804 * @mtd: MTD device structure
2805 * @to: offset to write to
2806 * @ops: oob operations description structure
2808 * NAND write with ECC.
2810 static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2811 struct mtd_oob_ops *ops)
2813 int chipnr, realpage, page, blockmask, column;
2814 struct nand_chip *chip = mtd_to_nand(mtd);
2815 uint32_t writelen = ops->len;
2817 uint32_t oobwritelen = ops->ooblen;
2818 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
2820 uint8_t *oob = ops->oobbuf;
2821 uint8_t *buf = ops->datbuf;
2823 int oob_required = oob ? 1 : 0;
2829 /* Reject writes, which are not page aligned */
2830 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
2831 pr_notice("%s: attempt to write non page aligned data\n",
2836 column = to & (mtd->writesize - 1);
2838 chipnr = (int)(to >> chip->chip_shift);
2839 chip->select_chip(mtd, chipnr);
2841 /* Check, if it is write protected */
2842 if (nand_check_wp(mtd)) {
2847 realpage = (int)(to >> chip->page_shift);
2848 page = realpage & chip->pagemask;
2849 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2851 /* Invalidate the page cache, when we write to the cached page */
2852 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2853 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
2856 /* Don't allow multipage oob writes with offset */
2857 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2863 int bytes = mtd->writesize;
2864 uint8_t *wbuf = buf;
2866 int part_pagewr = (column || writelen < mtd->writesize);
2870 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2871 use_bufpoi = !virt_addr_valid(buf) ||
2872 !IS_ALIGNED((unsigned long)buf,
2877 /* Partial page write?, or need to use bounce buffer */
2879 pr_debug("%s: using write bounce buffer for buf@%p\n",
2882 bytes = min_t(int, bytes - column, writelen);
2884 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2885 memcpy(&chip->buffers->databuf[column], buf, bytes);
2886 wbuf = chip->buffers->databuf;
2889 if (unlikely(oob)) {
2890 size_t len = min(oobwritelen, oobmaxlen);
2891 oob = nand_fill_oob(mtd, oob, len, ops);
2894 /* We still need to erase leftover OOB data */
2895 memset(chip->oob_poi, 0xff, mtd->oobsize);
2898 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
2900 (ops->mode == MTD_OPS_RAW));
2912 page = realpage & chip->pagemask;
2913 /* Check, if we cross a chip boundary */
2916 chip->select_chip(mtd, -1);
2917 chip->select_chip(mtd, chipnr);
2921 ops->retlen = ops->len - writelen;
2923 ops->oobretlen = ops->ooblen;
2926 chip->select_chip(mtd, -1);
2931 * panic_nand_write - [MTD Interface] NAND write with ECC
2932 * @mtd: MTD device structure
2933 * @to: offset to write to
2934 * @len: number of bytes to write
2935 * @retlen: pointer to variable to store the number of written bytes
2936 * @buf: the data to write
2938 * NAND write with ECC. Used when performing writes in interrupt context, this
2939 * may for example be called by mtdoops when writing an oops while in panic.
2941 static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2942 size_t *retlen, const uint8_t *buf)
2944 struct nand_chip *chip = mtd_to_nand(mtd);
2945 struct mtd_oob_ops ops;
2948 /* Wait for the device to get ready */
2949 panic_nand_wait(mtd, chip, 400);
2951 /* Grab the device */
2952 panic_nand_get_device(chip, mtd, FL_WRITING);
2954 memset(&ops, 0, sizeof(ops));
2956 ops.datbuf = (uint8_t *)buf;
2957 ops.mode = MTD_OPS_PLACE_OOB;
2959 ret = nand_do_write_ops(mtd, to, &ops);
2961 *retlen = ops.retlen;
2966 * nand_write - [MTD Interface] NAND write with ECC
2967 * @mtd: MTD device structure
2968 * @to: offset to write to
2969 * @len: number of bytes to write
2970 * @retlen: pointer to variable to store the number of written bytes
2971 * @buf: the data to write
2973 * NAND write with ECC.
2975 static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2976 size_t *retlen, const uint8_t *buf)
2978 struct mtd_oob_ops ops;
2981 nand_get_device(mtd, FL_WRITING);
2982 memset(&ops, 0, sizeof(ops));
2984 ops.datbuf = (uint8_t *)buf;
2985 ops.mode = MTD_OPS_PLACE_OOB;
2986 ret = nand_do_write_ops(mtd, to, &ops);
2987 *retlen = ops.retlen;
2988 nand_release_device(mtd);
2993 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
2994 * @mtd: MTD device structure
2995 * @to: offset to write to
2996 * @ops: oob operation description structure
2998 * NAND write out-of-band.
3000 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3001 struct mtd_oob_ops *ops)
3003 int chipnr, page, status, len;
3004 struct nand_chip *chip = mtd_to_nand(mtd);
3006 pr_debug("%s: to = 0x%08x, len = %i\n",
3007 __func__, (unsigned int)to, (int)ops->ooblen);
3009 len = mtd_oobavail(mtd, ops);
3011 /* Do not allow write past end of page */
3012 if ((ops->ooboffs + ops->ooblen) > len) {
3013 pr_debug("%s: attempt to write past end of page\n",
3018 if (unlikely(ops->ooboffs >= len)) {
3019 pr_debug("%s: attempt to start write outside oob\n",
3024 /* Do not allow write past end of device */
3025 if (unlikely(to >= mtd->size ||
3026 ops->ooboffs + ops->ooblen >
3027 ((mtd->size >> chip->page_shift) -
3028 (to >> chip->page_shift)) * len)) {
3029 pr_debug("%s: attempt to write beyond end of device\n",
3034 chipnr = (int)(to >> chip->chip_shift);
3037 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3038 * of my DiskOnChip 2000 test units) will clear the whole data page too
3039 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3040 * it in the doc2000 driver in August 1999. dwmw2.
3042 nand_reset(chip, chipnr);
3044 chip->select_chip(mtd, chipnr);
3046 /* Shift to get page */
3047 page = (int)(to >> chip->page_shift);
3049 /* Check, if it is write protected */
3050 if (nand_check_wp(mtd)) {
3051 chip->select_chip(mtd, -1);
3055 /* Invalidate the page cache, if we write to the cached page */
3056 if (page == chip->pagebuf)
3059 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
3061 if (ops->mode == MTD_OPS_RAW)
3062 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3064 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
3066 chip->select_chip(mtd, -1);
3071 ops->oobretlen = ops->ooblen;
3077 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
3078 * @mtd: MTD device structure
3079 * @to: offset to write to
3080 * @ops: oob operation description structure
3082 static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3083 struct mtd_oob_ops *ops)
3085 int ret = -ENOTSUPP;
3089 /* Do not allow writes past end of device */
3090 if (ops->datbuf && (to + ops->len) > mtd->size) {
3091 pr_debug("%s: attempt to write beyond end of device\n",
3096 nand_get_device(mtd, FL_WRITING);
3098 switch (ops->mode) {
3099 case MTD_OPS_PLACE_OOB:
3100 case MTD_OPS_AUTO_OOB:
3109 ret = nand_do_write_oob(mtd, to, ops);
3111 ret = nand_do_write_ops(mtd, to, ops);
3114 nand_release_device(mtd);
3119 * single_erase - [GENERIC] NAND standard block erase command function
3120 * @mtd: MTD device structure
3121 * @page: the page address of the block which will be erased
3123 * Standard erase command for NAND chips. Returns NAND status.
3125 static int single_erase(struct mtd_info *mtd, int page)
3127 struct nand_chip *chip = mtd_to_nand(mtd);
3128 /* Send commands to erase a block */
3129 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
3130 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
3132 return chip->waitfunc(mtd, chip);
3136 * nand_erase - [MTD Interface] erase block(s)
3137 * @mtd: MTD device structure
3138 * @instr: erase instruction
3140 * Erase one ore more blocks.
3142 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
3144 return nand_erase_nand(mtd, instr, 0);
3148 * nand_erase_nand - [INTERN] erase block(s)
3149 * @mtd: MTD device structure
3150 * @instr: erase instruction
3151 * @allowbbt: allow erasing the bbt area
3153 * Erase one ore more blocks.
3155 int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3158 int page, status, pages_per_block, ret, chipnr;
3159 struct nand_chip *chip = mtd_to_nand(mtd);
3162 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3163 __func__, (unsigned long long)instr->addr,
3164 (unsigned long long)instr->len);
3166 if (check_offs_len(mtd, instr->addr, instr->len))
3169 /* Grab the lock and see if the device is available */
3170 nand_get_device(mtd, FL_ERASING);
3172 /* Shift to get first page */
3173 page = (int)(instr->addr >> chip->page_shift);
3174 chipnr = (int)(instr->addr >> chip->chip_shift);
3176 /* Calculate pages in each block */
3177 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
3179 /* Select the NAND device */
3180 chip->select_chip(mtd, chipnr);
3182 /* Check, if it is write protected */
3183 if (nand_check_wp(mtd)) {
3184 pr_debug("%s: device is write protected!\n",
3186 instr->state = MTD_ERASE_FAILED;
3190 /* Loop through the pages */
3193 instr->state = MTD_ERASING;
3196 /* Check if we have a bad block, we do not erase bad blocks! */
3197 if (nand_block_checkbad(mtd, ((loff_t) page) <<
3198 chip->page_shift, allowbbt)) {
3199 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3201 instr->state = MTD_ERASE_FAILED;
3206 * Invalidate the page cache, if we erase the block which
3207 * contains the current cached page.
3209 if (page <= chip->pagebuf && chip->pagebuf <
3210 (page + pages_per_block))
3213 status = chip->erase(mtd, page & chip->pagemask);
3215 /* See if block erase succeeded */
3216 if (status & NAND_STATUS_FAIL) {
3217 pr_debug("%s: failed erase, page 0x%08x\n",
3219 instr->state = MTD_ERASE_FAILED;
3221 ((loff_t)page << chip->page_shift);
3225 /* Increment page address and decrement length */
3226 len -= (1ULL << chip->phys_erase_shift);
3227 page += pages_per_block;
3229 /* Check, if we cross a chip boundary */
3230 if (len && !(page & chip->pagemask)) {
3232 chip->select_chip(mtd, -1);
3233 chip->select_chip(mtd, chipnr);
3236 instr->state = MTD_ERASE_DONE;
3240 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
3242 /* Deselect and wake up anyone waiting on the device */
3243 chip->select_chip(mtd, -1);
3244 nand_release_device(mtd);
3246 /* Do call back function */
3248 mtd_erase_callback(instr);
3250 /* Return more or less happy */
3255 * nand_sync - [MTD Interface] sync
3256 * @mtd: MTD device structure
3258 * Sync is actually a wait for chip ready function.
3260 static void nand_sync(struct mtd_info *mtd)
3262 pr_debug("%s: called\n", __func__);
3264 /* Grab the lock and see if the device is available */
3265 nand_get_device(mtd, FL_SYNCING);
3266 /* Release it and go back */
3267 nand_release_device(mtd);
3271 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
3272 * @mtd: MTD device structure
3273 * @offs: offset relative to mtd start
3275 static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
3277 struct nand_chip *chip = mtd_to_nand(mtd);
3278 int chipnr = (int)(offs >> chip->chip_shift);
3281 /* Select the NAND device */
3282 nand_get_device(mtd, FL_READING);
3283 chip->select_chip(mtd, chipnr);
3285 ret = nand_block_checkbad(mtd, offs, 0);
3287 chip->select_chip(mtd, -1);
3288 nand_release_device(mtd);
3294 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
3295 * @mtd: MTD device structure
3296 * @ofs: offset relative to mtd start
3298 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
3302 ret = nand_block_isbad(mtd, ofs);
3304 /* If it was bad already, return success and do nothing */
3310 return nand_block_markbad_lowlevel(mtd, ofs);
3314 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3315 * @mtd: MTD device structure
3316 * @ofs: offset relative to mtd start
3317 * @len: length of mtd
3319 static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3321 struct nand_chip *chip = mtd_to_nand(mtd);
3322 u32 part_start_block;
3328 * max_bb_per_die and blocks_per_die used to determine
3329 * the maximum bad block count.
3331 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3334 /* Get the start and end of the partition in erase blocks. */
3335 part_start_block = mtd_div_by_eb(ofs, mtd);
3336 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3338 /* Get the start and end LUNs of the partition. */
3339 part_start_die = part_start_block / chip->blocks_per_die;
3340 part_end_die = part_end_block / chip->blocks_per_die;
3343 * Look up the bad blocks per unit and multiply by the number of units
3344 * that the partition spans.
3346 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3350 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3351 * @mtd: MTD device structure
3352 * @chip: nand chip info structure
3353 * @addr: feature address.
3354 * @subfeature_param: the subfeature parameters, a four bytes array.
3356 static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3357 int addr, uint8_t *subfeature_param)
3362 if (!chip->onfi_version ||
3363 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3364 & ONFI_OPT_CMD_SET_GET_FEATURES))
3367 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
3368 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3369 chip->write_byte(mtd, subfeature_param[i]);
3371 status = chip->waitfunc(mtd, chip);
3372 if (status & NAND_STATUS_FAIL)
3378 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3379 * @mtd: MTD device structure
3380 * @chip: nand chip info structure
3381 * @addr: feature address.
3382 * @subfeature_param: the subfeature parameters, a four bytes array.
3384 static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3385 int addr, uint8_t *subfeature_param)
3389 if (!chip->onfi_version ||
3390 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3391 & ONFI_OPT_CMD_SET_GET_FEATURES))
3394 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
3395 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3396 *subfeature_param++ = chip->read_byte(mtd);
3401 * nand_onfi_get_set_features_notsupp - set/get features stub returning
3403 * @mtd: MTD device structure
3404 * @chip: nand chip info structure
3405 * @addr: feature address.
3406 * @subfeature_param: the subfeature parameters, a four bytes array.
3408 * Should be used by NAND controller drivers that do not support the SET/GET
3409 * FEATURES operations.
3411 int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd,
3412 struct nand_chip *chip, int addr,
3413 u8 *subfeature_param)
3417 EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp);
3420 * nand_suspend - [MTD Interface] Suspend the NAND flash
3421 * @mtd: MTD device structure
3423 static int nand_suspend(struct mtd_info *mtd)
3425 return nand_get_device(mtd, FL_PM_SUSPENDED);
3429 * nand_resume - [MTD Interface] Resume the NAND flash
3430 * @mtd: MTD device structure
3432 static void nand_resume(struct mtd_info *mtd)
3434 struct nand_chip *chip = mtd_to_nand(mtd);
3436 if (chip->state == FL_PM_SUSPENDED)
3437 nand_release_device(mtd);
3439 pr_err("%s called for a chip which is not in suspended state\n",
3444 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3445 * prevent further operations
3446 * @mtd: MTD device structure
3448 static void nand_shutdown(struct mtd_info *mtd)
3450 nand_get_device(mtd, FL_PM_SUSPENDED);
3453 /* Set default functions */
3454 static void nand_set_defaults(struct nand_chip *chip)
3456 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
3458 /* check for proper chip_delay setup, set 20us if not */
3459 if (!chip->chip_delay)
3460 chip->chip_delay = 20;
3462 /* check, if a user supplied command function given */
3463 if (chip->cmdfunc == NULL)
3464 chip->cmdfunc = nand_command;
3466 /* check, if a user supplied wait function given */
3467 if (chip->waitfunc == NULL)
3468 chip->waitfunc = nand_wait;
3470 if (!chip->select_chip)
3471 chip->select_chip = nand_select_chip;
3473 /* set for ONFI nand */
3474 if (!chip->onfi_set_features)
3475 chip->onfi_set_features = nand_onfi_set_features;
3476 if (!chip->onfi_get_features)
3477 chip->onfi_get_features = nand_onfi_get_features;
3479 /* If called twice, pointers that depend on busw may need to be reset */
3480 if (!chip->read_byte || chip->read_byte == nand_read_byte)
3481 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3482 if (!chip->read_word)
3483 chip->read_word = nand_read_word;
3484 if (!chip->block_bad)
3485 chip->block_bad = nand_block_bad;
3486 if (!chip->block_markbad)
3487 chip->block_markbad = nand_default_block_markbad;
3488 if (!chip->write_buf || chip->write_buf == nand_write_buf)
3489 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
3490 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3491 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3492 if (!chip->read_buf || chip->read_buf == nand_read_buf)
3493 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
3494 if (!chip->scan_bbt)
3495 chip->scan_bbt = nand_default_bbt;
3497 if (!chip->controller) {
3498 chip->controller = &chip->hwcontrol;
3499 nand_hw_control_init(chip->controller);
3502 if (!chip->buf_align)
3503 chip->buf_align = 1;
3506 /* Sanitize ONFI strings so we can safely print them */
3507 static void sanitize_string(uint8_t *s, size_t len)
3511 /* Null terminate */
3514 /* Remove non printable chars */
3515 for (i = 0; i < len - 1; i++) {
3516 if (s[i] < ' ' || s[i] > 127)
3520 /* Remove trailing spaces */
3524 static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3529 for (i = 0; i < 8; i++)
3530 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3536 /* Parse the Extended Parameter Page. */
3537 static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
3538 struct nand_onfi_params *p)
3540 struct mtd_info *mtd = nand_to_mtd(chip);
3541 struct onfi_ext_param_page *ep;
3542 struct onfi_ext_section *s;
3543 struct onfi_ext_ecc_info *ecc;
3549 len = le16_to_cpu(p->ext_param_page_length) * 16;
3550 ep = kmalloc(len, GFP_KERNEL);
3554 /* Send our own NAND_CMD_PARAM. */
3555 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3557 /* Use the Change Read Column command to skip the ONFI param pages. */
3558 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3559 sizeof(*p) * p->num_of_param_pages , -1);
3561 /* Read out the Extended Parameter Page. */
3562 chip->read_buf(mtd, (uint8_t *)ep, len);
3563 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3564 != le16_to_cpu(ep->crc))) {
3565 pr_debug("fail in the CRC.\n");
3570 * Check the signature.
3571 * Do not strictly follow the ONFI spec, maybe changed in future.
3573 if (strncmp(ep->sig, "EPPS", 4)) {
3574 pr_debug("The signature is invalid.\n");
3578 /* find the ECC section. */
3579 cursor = (uint8_t *)(ep + 1);
3580 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3581 s = ep->sections + i;
3582 if (s->type == ONFI_SECTION_TYPE_2)
3584 cursor += s->length * 16;
3586 if (i == ONFI_EXT_SECTION_MAX) {
3587 pr_debug("We can not find the ECC section.\n");
3591 /* get the info we want. */
3592 ecc = (struct onfi_ext_ecc_info *)cursor;
3594 if (!ecc->codeword_size) {
3595 pr_debug("Invalid codeword size\n");
3599 chip->ecc_strength_ds = ecc->ecc_bits;
3600 chip->ecc_step_ds = 1 << ecc->codeword_size;
3609 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
3611 static int nand_flash_detect_onfi(struct nand_chip *chip)
3613 struct mtd_info *mtd = nand_to_mtd(chip);
3614 struct nand_onfi_params *p = &chip->onfi_params;
3618 /* Try ONFI for unknown chip or LP */
3619 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3620 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3621 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3624 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3625 for (i = 0; i < 3; i++) {
3626 for (j = 0; j < sizeof(*p); j++)
3627 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3628 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3629 le16_to_cpu(p->crc)) {
3635 pr_err("Could not find valid ONFI parameter page; aborting\n");
3640 val = le16_to_cpu(p->revision);
3642 chip->onfi_version = 23;
3643 else if (val & (1 << 4))
3644 chip->onfi_version = 22;
3645 else if (val & (1 << 3))
3646 chip->onfi_version = 21;
3647 else if (val & (1 << 2))
3648 chip->onfi_version = 20;
3649 else if (val & (1 << 1))
3650 chip->onfi_version = 10;
3652 if (!chip->onfi_version) {
3653 pr_info("unsupported ONFI version: %d\n", val);
3657 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3658 sanitize_string(p->model, sizeof(p->model));
3660 mtd->name = p->model;
3662 mtd->writesize = le32_to_cpu(p->byte_per_page);
3665 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3666 * (don't ask me who thought of this...). MTD assumes that these
3667 * dimensions will be power-of-2, so just truncate the remaining area.
3669 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3670 mtd->erasesize *= mtd->writesize;
3672 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3674 /* See erasesize comment */
3675 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3676 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3677 chip->bits_per_cell = p->bits_per_cell;
3679 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3680 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3682 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
3683 chip->options |= NAND_BUSWIDTH_16;
3685 if (p->ecc_bits != 0xff) {
3686 chip->ecc_strength_ds = p->ecc_bits;
3687 chip->ecc_step_ds = 512;
3688 } else if (chip->onfi_version >= 21 &&
3689 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3692 * The nand_flash_detect_ext_param_page() uses the
3693 * Change Read Column command which maybe not supported
3694 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3695 * now. We do not replace user supplied command function.
3697 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3698 chip->cmdfunc = nand_command_lp;
3700 /* The Extended Parameter Page is supported since ONFI 2.1. */
3701 if (nand_flash_detect_ext_param_page(chip, p))
3702 pr_warn("Failed to detect ONFI extended param page\n");
3704 pr_warn("Could not retrieve ONFI ECC requirements\n");
3711 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3713 static int nand_flash_detect_jedec(struct nand_chip *chip)
3715 struct mtd_info *mtd = nand_to_mtd(chip);
3716 struct nand_jedec_params *p = &chip->jedec_params;
3717 struct jedec_ecc_info *ecc;
3721 /* Try JEDEC for unknown chip or LP */
3722 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3723 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3724 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3725 chip->read_byte(mtd) != 'C')
3728 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3729 for (i = 0; i < 3; i++) {
3730 for (j = 0; j < sizeof(*p); j++)
3731 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3733 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3734 le16_to_cpu(p->crc))
3739 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3744 val = le16_to_cpu(p->revision);
3746 chip->jedec_version = 10;
3747 else if (val & (1 << 1))
3748 chip->jedec_version = 1; /* vendor specific version */
3750 if (!chip->jedec_version) {
3751 pr_info("unsupported JEDEC version: %d\n", val);
3755 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3756 sanitize_string(p->model, sizeof(p->model));
3758 mtd->name = p->model;
3760 mtd->writesize = le32_to_cpu(p->byte_per_page);
3762 /* Please reference to the comment for nand_flash_detect_onfi. */
3763 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3764 mtd->erasesize *= mtd->writesize;
3766 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3768 /* Please reference to the comment for nand_flash_detect_onfi. */
3769 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3770 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3771 chip->bits_per_cell = p->bits_per_cell;
3773 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3774 chip->options |= NAND_BUSWIDTH_16;
3777 ecc = &p->ecc_info[0];
3779 if (ecc->codeword_size >= 9) {
3780 chip->ecc_strength_ds = ecc->ecc_bits;
3781 chip->ecc_step_ds = 1 << ecc->codeword_size;
3783 pr_warn("Invalid codeword size\n");
3790 * nand_id_has_period - Check if an ID string has a given wraparound period
3791 * @id_data: the ID string
3792 * @arrlen: the length of the @id_data array
3793 * @period: the period of repitition
3795 * Check if an ID string is repeated within a given sequence of bytes at
3796 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
3797 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
3798 * if the repetition has a period of @period; otherwise, returns zero.
3800 static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3803 for (i = 0; i < period; i++)
3804 for (j = i + period; j < arrlen; j += period)
3805 if (id_data[i] != id_data[j])
3811 * nand_id_len - Get the length of an ID string returned by CMD_READID
3812 * @id_data: the ID string
3813 * @arrlen: the length of the @id_data array
3815 * Returns the length of the ID string, according to known wraparound/trailing
3816 * zero patterns. If no pattern exists, returns the length of the array.
3818 static int nand_id_len(u8 *id_data, int arrlen)
3820 int last_nonzero, period;
3822 /* Find last non-zero byte */
3823 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3824 if (id_data[last_nonzero])
3828 if (last_nonzero < 0)
3831 /* Calculate wraparound period */
3832 for (period = 1; period < arrlen; period++)
3833 if (nand_id_has_period(id_data, arrlen, period))
3836 /* There's a repeated pattern */
3837 if (period < arrlen)
3840 /* There are trailing zeros */
3841 if (last_nonzero < arrlen - 1)
3842 return last_nonzero + 1;
3844 /* No pattern detected */
3848 /* Extract the bits of per cell from the 3rd byte of the extended ID */
3849 static int nand_get_bits_per_cell(u8 cellinfo)
3853 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3854 bits >>= NAND_CI_CELLTYPE_SHIFT;
3859 * Many new NAND share similar device ID codes, which represent the size of the
3860 * chip. The rest of the parameters must be decoded according to generic or
3861 * manufacturer-specific "extended ID" decoding patterns.
3863 void nand_decode_ext_id(struct nand_chip *chip)
3865 struct mtd_info *mtd = nand_to_mtd(chip);
3867 u8 *id_data = chip->id.data;
3868 /* The 3rd id byte holds MLC / multichip data */
3869 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3870 /* The 4th id byte is the important one */
3874 mtd->writesize = 1024 << (extid & 0x03);
3877 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
3879 /* Calc blocksize. Blocksize is multiples of 64KiB */
3880 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3882 /* Get buswidth information */
3884 chip->options |= NAND_BUSWIDTH_16;
3886 EXPORT_SYMBOL_GPL(nand_decode_ext_id);
3889 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3890 * decodes a matching ID table entry and assigns the MTD size parameters for
3893 static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
3895 struct mtd_info *mtd = nand_to_mtd(chip);
3897 mtd->erasesize = type->erasesize;
3898 mtd->writesize = type->pagesize;
3899 mtd->oobsize = mtd->writesize / 32;
3901 /* All legacy ID NAND are small-page, SLC */
3902 chip->bits_per_cell = 1;
3906 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3907 * heuristic patterns using various detected parameters (e.g., manufacturer,
3908 * page size, cell-type information).
3910 static void nand_decode_bbm_options(struct nand_chip *chip)
3912 struct mtd_info *mtd = nand_to_mtd(chip);
3914 /* Set the bad block position */
3915 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3916 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3918 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3921 static inline bool is_full_id_nand(struct nand_flash_dev *type)
3923 return type->id_len;
3926 static bool find_full_id_nand(struct nand_chip *chip,
3927 struct nand_flash_dev *type)
3929 struct mtd_info *mtd = nand_to_mtd(chip);
3930 u8 *id_data = chip->id.data;
3932 if (!strncmp(type->id, id_data, type->id_len)) {
3933 mtd->writesize = type->pagesize;
3934 mtd->erasesize = type->erasesize;
3935 mtd->oobsize = type->oobsize;
3937 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3938 chip->chipsize = (uint64_t)type->chipsize << 20;
3939 chip->options |= type->options;
3940 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3941 chip->ecc_step_ds = NAND_ECC_STEP(type);
3942 chip->onfi_timing_mode_default =
3943 type->onfi_timing_mode_default;
3946 mtd->name = type->name;
3954 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
3955 * compliant and does not have a full-id or legacy-id entry in the nand_ids
3958 static void nand_manufacturer_detect(struct nand_chip *chip)
3961 * Try manufacturer detection if available and use
3962 * nand_decode_ext_id() otherwise.
3964 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3965 chip->manufacturer.desc->ops->detect)
3966 chip->manufacturer.desc->ops->detect(chip);
3968 nand_decode_ext_id(chip);
3972 * Manufacturer initialization. This function is called for all NANDs including
3973 * ONFI and JEDEC compliant ones.
3974 * Manufacturer drivers should put all their specific initialization code in
3975 * their ->init() hook.
3977 static int nand_manufacturer_init(struct nand_chip *chip)
3979 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
3980 !chip->manufacturer.desc->ops->init)
3983 return chip->manufacturer.desc->ops->init(chip);
3987 * Manufacturer cleanup. This function is called for all NANDs including
3988 * ONFI and JEDEC compliant ones.
3989 * Manufacturer drivers should put all their specific cleanup code in their
3992 static void nand_manufacturer_cleanup(struct nand_chip *chip)
3994 /* Release manufacturer private data */
3995 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3996 chip->manufacturer.desc->ops->cleanup)
3997 chip->manufacturer.desc->ops->cleanup(chip);
4001 * Get the flash and manufacturer id and lookup if the type is supported.
4003 static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
4005 const struct nand_manufacturer *manufacturer;
4006 struct mtd_info *mtd = nand_to_mtd(chip);
4009 u8 *id_data = chip->id.data;
4013 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
4016 nand_reset(chip, 0);
4018 /* Select the device */
4019 chip->select_chip(mtd, 0);
4021 /* Send the command for reading device ID */
4022 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4024 /* Read manufacturer and device IDs */
4025 maf_id = chip->read_byte(mtd);
4026 dev_id = chip->read_byte(mtd);
4029 * Try again to make sure, as some systems the bus-hold or other
4030 * interface concerns can cause random data which looks like a
4031 * possibly credible NAND flash to appear. If the two results do
4032 * not match, ignore the device completely.
4035 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4037 /* Read entire ID string */
4038 for (i = 0; i < 8; i++)
4039 id_data[i] = chip->read_byte(mtd);
4041 if (id_data[0] != maf_id || id_data[1] != dev_id) {
4042 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
4043 maf_id, dev_id, id_data[0], id_data[1]);
4047 chip->id.len = nand_id_len(id_data, 8);
4049 /* Try to identify manufacturer */
4050 manufacturer = nand_get_manufacturer(maf_id);
4051 chip->manufacturer.desc = manufacturer;
4054 type = nand_flash_ids;
4057 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4059 * This is required to make sure initial NAND bus width set by the
4060 * NAND controller driver is coherent with the real NAND bus width
4061 * (extracted by auto-detection code).
4063 busw = chip->options & NAND_BUSWIDTH_16;
4066 * The flag is only set (never cleared), reset it to its default value
4067 * before starting auto-detection.
4069 chip->options &= ~NAND_BUSWIDTH_16;
4071 for (; type->name != NULL; type++) {
4072 if (is_full_id_nand(type)) {
4073 if (find_full_id_nand(chip, type))
4075 } else if (dev_id == type->dev_id) {
4080 chip->onfi_version = 0;
4081 if (!type->name || !type->pagesize) {
4082 /* Check if the chip is ONFI compliant */
4083 if (nand_flash_detect_onfi(chip))
4086 /* Check if the chip is JEDEC compliant */
4087 if (nand_flash_detect_jedec(chip))
4095 mtd->name = type->name;
4097 chip->chipsize = (uint64_t)type->chipsize << 20;
4099 if (!type->pagesize)
4100 nand_manufacturer_detect(chip);
4102 nand_decode_id(chip, type);
4104 /* Get chip options */
4105 chip->options |= type->options;
4109 if (chip->options & NAND_BUSWIDTH_AUTO) {
4110 WARN_ON(busw & NAND_BUSWIDTH_16);
4111 nand_set_defaults(chip);
4112 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4114 * Check, if buswidth is correct. Hardware drivers should set
4117 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4119 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4121 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4122 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
4126 nand_decode_bbm_options(chip);
4128 /* Calculate the address shift from the page size */
4129 chip->page_shift = ffs(mtd->writesize) - 1;
4130 /* Convert chipsize to number of pages per chip -1 */
4131 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
4133 chip->bbt_erase_shift = chip->phys_erase_shift =
4134 ffs(mtd->erasesize) - 1;
4135 if (chip->chipsize & 0xffffffff)
4136 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
4138 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4139 chip->chip_shift += 32 - 1;
4142 chip->badblockbits = 8;
4143 chip->erase = single_erase;
4145 /* Do not replace user supplied command function! */
4146 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4147 chip->cmdfunc = nand_command_lp;
4149 ret = nand_manufacturer_init(chip);
4153 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4156 if (chip->onfi_version)
4157 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4158 chip->onfi_params.model);
4159 else if (chip->jedec_version)
4160 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4161 chip->jedec_params.model);
4163 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4166 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
4167 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
4168 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
4172 static const char * const nand_ecc_modes[] = {
4173 [NAND_ECC_NONE] = "none",
4174 [NAND_ECC_SOFT] = "soft",
4175 [NAND_ECC_HW] = "hw",
4176 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4177 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
4178 [NAND_ECC_ON_DIE] = "on-die",
4181 static int of_get_nand_ecc_mode(struct device_node *np)
4186 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4190 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4191 if (!strcasecmp(pm, nand_ecc_modes[i]))
4195 * For backward compatibility we support few obsoleted values that don't
4196 * have their mappings into nand_ecc_modes_t anymore (they were merged
4197 * with other enums).
4199 if (!strcasecmp(pm, "soft_bch"))
4200 return NAND_ECC_SOFT;
4205 static const char * const nand_ecc_algos[] = {
4206 [NAND_ECC_HAMMING] = "hamming",
4207 [NAND_ECC_BCH] = "bch",
4210 static int of_get_nand_ecc_algo(struct device_node *np)
4215 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4217 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4218 if (!strcasecmp(pm, nand_ecc_algos[i]))
4224 * For backward compatibility we also read "nand-ecc-mode" checking
4225 * for some obsoleted values that were specifying ECC algorithm.
4227 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4231 if (!strcasecmp(pm, "soft"))
4232 return NAND_ECC_HAMMING;
4233 else if (!strcasecmp(pm, "soft_bch"))
4234 return NAND_ECC_BCH;
4239 static int of_get_nand_ecc_step_size(struct device_node *np)
4244 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4245 return ret ? ret : val;
4248 static int of_get_nand_ecc_strength(struct device_node *np)
4253 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4254 return ret ? ret : val;
4257 static int of_get_nand_bus_width(struct device_node *np)
4261 if (of_property_read_u32(np, "nand-bus-width", &val))
4273 static bool of_get_nand_on_flash_bbt(struct device_node *np)
4275 return of_property_read_bool(np, "nand-on-flash-bbt");
4278 static int nand_dt_init(struct nand_chip *chip)
4280 struct device_node *dn = nand_get_flash_node(chip);
4281 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
4286 if (of_get_nand_bus_width(dn) == 16)
4287 chip->options |= NAND_BUSWIDTH_16;
4289 if (of_get_nand_on_flash_bbt(dn))
4290 chip->bbt_options |= NAND_BBT_USE_FLASH;
4292 ecc_mode = of_get_nand_ecc_mode(dn);
4293 ecc_algo = of_get_nand_ecc_algo(dn);
4294 ecc_strength = of_get_nand_ecc_strength(dn);
4295 ecc_step = of_get_nand_ecc_step_size(dn);
4298 chip->ecc.mode = ecc_mode;
4301 chip->ecc.algo = ecc_algo;
4303 if (ecc_strength >= 0)
4304 chip->ecc.strength = ecc_strength;
4307 chip->ecc.size = ecc_step;
4309 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4310 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4316 * nand_scan_ident - [NAND Interface] Scan for the NAND device
4317 * @mtd: MTD device structure
4318 * @maxchips: number of chips to scan for
4319 * @table: alternative NAND ID table
4321 * This is the first phase of the normal nand_scan() function. It reads the
4322 * flash ID and sets up MTD fields accordingly.
4325 int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4326 struct nand_flash_dev *table)
4328 int i, nand_maf_id, nand_dev_id;
4329 struct nand_chip *chip = mtd_to_nand(mtd);
4332 ret = nand_dt_init(chip);
4336 if (!mtd->name && mtd->dev.parent)
4337 mtd->name = dev_name(mtd->dev.parent);
4339 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4341 * Default functions assigned for chip_select() and
4342 * cmdfunc() both expect cmd_ctrl() to be populated,
4343 * so we need to check that that's the case
4345 pr_err("chip.cmd_ctrl() callback is not provided");
4348 /* Set the default functions */
4349 nand_set_defaults(chip);
4351 /* Read the flash type */
4352 ret = nand_detect(chip, table);
4354 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
4355 pr_warn("No NAND device found\n");
4356 chip->select_chip(mtd, -1);
4360 /* Initialize the ->data_interface field. */
4361 ret = nand_init_data_interface(chip);
4366 * Setup the data interface correctly on the chip and controller side.
4367 * This explicit call to nand_setup_data_interface() is only required
4368 * for the first die, because nand_reset() has been called before
4369 * ->data_interface and ->default_onfi_timing_mode were set.
4370 * For the other dies, nand_reset() will automatically switch to the
4373 ret = nand_setup_data_interface(chip, 0);
4377 nand_maf_id = chip->id.data[0];
4378 nand_dev_id = chip->id.data[1];
4380 chip->select_chip(mtd, -1);
4382 /* Check for a chip array */
4383 for (i = 1; i < maxchips; i++) {
4384 /* See comment in nand_get_flash_type for reset */
4385 nand_reset(chip, i);
4387 chip->select_chip(mtd, i);
4388 /* Send the command for reading device ID */
4389 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4390 /* Read manufacturer and device IDs */
4391 if (nand_maf_id != chip->read_byte(mtd) ||
4392 nand_dev_id != chip->read_byte(mtd)) {
4393 chip->select_chip(mtd, -1);
4396 chip->select_chip(mtd, -1);
4399 pr_info("%d chips detected\n", i);
4401 /* Store the number of chips and calc total size for mtd */
4403 mtd->size = i * chip->chipsize;
4407 EXPORT_SYMBOL(nand_scan_ident);
4409 static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4411 struct nand_chip *chip = mtd_to_nand(mtd);
4412 struct nand_ecc_ctrl *ecc = &chip->ecc;
4414 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
4417 switch (ecc->algo) {
4418 case NAND_ECC_HAMMING:
4419 ecc->calculate = nand_calculate_ecc;
4420 ecc->correct = nand_correct_data;
4421 ecc->read_page = nand_read_page_swecc;
4422 ecc->read_subpage = nand_read_subpage;
4423 ecc->write_page = nand_write_page_swecc;
4424 ecc->read_page_raw = nand_read_page_raw;
4425 ecc->write_page_raw = nand_write_page_raw;
4426 ecc->read_oob = nand_read_oob_std;
4427 ecc->write_oob = nand_write_oob_std;
4434 if (!mtd_nand_has_bch()) {
4435 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4438 ecc->calculate = nand_bch_calculate_ecc;
4439 ecc->correct = nand_bch_correct_data;
4440 ecc->read_page = nand_read_page_swecc;
4441 ecc->read_subpage = nand_read_subpage;
4442 ecc->write_page = nand_write_page_swecc;
4443 ecc->read_page_raw = nand_read_page_raw;
4444 ecc->write_page_raw = nand_write_page_raw;
4445 ecc->read_oob = nand_read_oob_std;
4446 ecc->write_oob = nand_write_oob_std;
4449 * Board driver should supply ecc.size and ecc.strength
4450 * values to select how many bits are correctable.
4451 * Otherwise, default to 4 bits for large page devices.
4453 if (!ecc->size && (mtd->oobsize >= 64)) {
4459 * if no ecc placement scheme was provided pickup the default
4462 if (!mtd->ooblayout) {
4463 /* handle large page devices only */
4464 if (mtd->oobsize < 64) {
4465 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4469 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
4474 * We can only maximize ECC config when the default layout is
4475 * used, otherwise we don't know how many bytes can really be
4478 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4479 ecc->options & NAND_ECC_MAXIMIZE) {
4482 /* Always prefer 1k blocks over 512bytes ones */
4484 steps = mtd->writesize / ecc->size;
4486 /* Reserve 2 bytes for the BBM */
4487 bytes = (mtd->oobsize - 2) / steps;
4488 ecc->strength = bytes * 8 / fls(8 * ecc->size);
4491 /* See nand_bch_init() for details. */
4493 ecc->priv = nand_bch_init(mtd);
4495 WARN(1, "BCH ECC initialization failed!\n");
4500 WARN(1, "Unsupported ECC algorithm!\n");
4506 * Check if the chip configuration meet the datasheet requirements.
4508 * If our configuration corrects A bits per B bytes and the minimum
4509 * required correction level is X bits per Y bytes, then we must ensure
4510 * both of the following are true:
4512 * (1) A / B >= X / Y
4515 * Requirement (1) ensures we can correct for the required bitflip density.
4516 * Requirement (2) ensures we can correct even when all bitflips are clumped
4517 * in the same sector.
4519 static bool nand_ecc_strength_good(struct mtd_info *mtd)
4521 struct nand_chip *chip = mtd_to_nand(mtd);
4522 struct nand_ecc_ctrl *ecc = &chip->ecc;
4525 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4526 /* Not enough information */
4530 * We get the number of corrected bits per page to compare
4531 * the correction density.
4533 corr = (mtd->writesize * ecc->strength) / ecc->size;
4534 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4536 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4539 static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4541 struct nand_ecc_ctrl *ecc = &chip->ecc;
4543 if (nand_standard_page_accessors(ecc))
4547 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4548 * controller driver implements all the page accessors because
4549 * default helpers are not suitable when the core does not
4550 * send the READ0/PAGEPROG commands.
4552 return (!ecc->read_page || !ecc->write_page ||
4553 !ecc->read_page_raw || !ecc->write_page_raw ||
4554 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4555 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4556 ecc->hwctl && ecc->calculate));
4560 * nand_scan_tail - [NAND Interface] Scan for the NAND device
4561 * @mtd: MTD device structure
4563 * This is the second phase of the normal nand_scan() function. It fills out
4564 * all the uninitialized function pointers with the defaults and scans for a
4565 * bad block table if appropriate.
4567 int nand_scan_tail(struct mtd_info *mtd)
4569 struct nand_chip *chip = mtd_to_nand(mtd);
4570 struct nand_ecc_ctrl *ecc = &chip->ecc;
4571 struct nand_buffers *nbuf = NULL;
4574 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
4575 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4576 !(chip->bbt_options & NAND_BBT_USE_FLASH)))
4579 if (invalid_ecc_page_accessors(chip)) {
4580 pr_err("Invalid ECC page accessors setup\n");
4584 if (!(chip->options & NAND_OWN_BUFFERS)) {
4585 nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL);
4589 nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL);
4590 if (!nbuf->ecccalc) {
4595 nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL);
4596 if (!nbuf->ecccode) {
4601 nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize,
4603 if (!nbuf->databuf) {
4608 chip->buffers = nbuf;
4614 /* Set the internal oob buffer location, just after the page data */
4615 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
4618 * If no default placement scheme is given, select an appropriate one.
4620 if (!mtd->ooblayout &&
4621 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
4622 switch (mtd->oobsize) {
4625 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
4629 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
4632 WARN(1, "No oob scheme defined for oobsize %d\n",
4640 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
4641 * selected and we have 256 byte pagesize fallback to software ECC
4644 switch (ecc->mode) {
4645 case NAND_ECC_HW_OOB_FIRST:
4646 /* Similar to NAND_ECC_HW, but a separate read_page handle */
4647 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
4648 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4652 if (!ecc->read_page)
4653 ecc->read_page = nand_read_page_hwecc_oob_first;
4656 /* Use standard hwecc read page function? */
4657 if (!ecc->read_page)
4658 ecc->read_page = nand_read_page_hwecc;
4659 if (!ecc->write_page)
4660 ecc->write_page = nand_write_page_hwecc;
4661 if (!ecc->read_page_raw)
4662 ecc->read_page_raw = nand_read_page_raw;
4663 if (!ecc->write_page_raw)
4664 ecc->write_page_raw = nand_write_page_raw;
4666 ecc->read_oob = nand_read_oob_std;
4667 if (!ecc->write_oob)
4668 ecc->write_oob = nand_write_oob_std;
4669 if (!ecc->read_subpage)
4670 ecc->read_subpage = nand_read_subpage;
4671 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
4672 ecc->write_subpage = nand_write_subpage_hwecc;
4674 case NAND_ECC_HW_SYNDROME:
4675 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4677 ecc->read_page == nand_read_page_hwecc ||
4679 ecc->write_page == nand_write_page_hwecc)) {
4680 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4684 /* Use standard syndrome read/write page function? */
4685 if (!ecc->read_page)
4686 ecc->read_page = nand_read_page_syndrome;
4687 if (!ecc->write_page)
4688 ecc->write_page = nand_write_page_syndrome;
4689 if (!ecc->read_page_raw)
4690 ecc->read_page_raw = nand_read_page_raw_syndrome;
4691 if (!ecc->write_page_raw)
4692 ecc->write_page_raw = nand_write_page_raw_syndrome;
4694 ecc->read_oob = nand_read_oob_syndrome;
4695 if (!ecc->write_oob)
4696 ecc->write_oob = nand_write_oob_syndrome;
4698 if (mtd->writesize >= ecc->size) {
4699 if (!ecc->strength) {
4700 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4706 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4707 ecc->size, mtd->writesize);
4708 ecc->mode = NAND_ECC_SOFT;
4709 ecc->algo = NAND_ECC_HAMMING;
4712 ret = nand_set_ecc_soft_ops(mtd);
4719 case NAND_ECC_ON_DIE:
4720 if (!ecc->read_page || !ecc->write_page) {
4721 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
4726 ecc->read_oob = nand_read_oob_std;
4727 if (!ecc->write_oob)
4728 ecc->write_oob = nand_write_oob_std;
4732 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
4733 ecc->read_page = nand_read_page_raw;
4734 ecc->write_page = nand_write_page_raw;
4735 ecc->read_oob = nand_read_oob_std;
4736 ecc->read_page_raw = nand_read_page_raw;
4737 ecc->write_page_raw = nand_write_page_raw;
4738 ecc->write_oob = nand_write_oob_std;
4739 ecc->size = mtd->writesize;
4745 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4750 /* For many systems, the standard OOB write also works for raw */
4751 if (!ecc->read_oob_raw)
4752 ecc->read_oob_raw = ecc->read_oob;
4753 if (!ecc->write_oob_raw)
4754 ecc->write_oob_raw = ecc->write_oob;
4756 /* propagate ecc info to mtd_info */
4757 mtd->ecc_strength = ecc->strength;
4758 mtd->ecc_step_size = ecc->size;
4761 * Set the number of read / write steps for one page depending on ECC
4764 ecc->steps = mtd->writesize / ecc->size;
4765 if (ecc->steps * ecc->size != mtd->writesize) {
4766 WARN(1, "Invalid ECC parameters\n");
4770 ecc->total = ecc->steps * ecc->bytes;
4773 * The number of bytes available for a client to place data into
4774 * the out of band area.
4776 ret = mtd_ooblayout_count_freebytes(mtd);
4780 mtd->oobavail = ret;
4782 /* ECC sanity check: warn if it's too weak */
4783 if (!nand_ecc_strength_good(mtd))
4784 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4787 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
4788 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
4789 switch (ecc->steps) {
4791 mtd->subpage_sft = 1;
4796 mtd->subpage_sft = 2;
4800 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4802 /* Initialize state */
4803 chip->state = FL_READY;
4805 /* Invalidate the pagebuffer reference */
4808 /* Large page NAND with SOFT_ECC should support subpage reads */
4809 switch (ecc->mode) {
4811 if (chip->page_shift > 9)
4812 chip->options |= NAND_SUBPAGE_READ;
4819 /* Fill in remaining MTD driver data */
4820 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
4821 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4823 mtd->_erase = nand_erase;
4825 mtd->_unpoint = NULL;
4826 mtd->_read = nand_read;
4827 mtd->_write = nand_write;
4828 mtd->_panic_write = panic_nand_write;
4829 mtd->_read_oob = nand_read_oob;
4830 mtd->_write_oob = nand_write_oob;
4831 mtd->_sync = nand_sync;
4833 mtd->_unlock = NULL;
4834 mtd->_suspend = nand_suspend;
4835 mtd->_resume = nand_resume;
4836 mtd->_reboot = nand_shutdown;
4837 mtd->_block_isreserved = nand_block_isreserved;
4838 mtd->_block_isbad = nand_block_isbad;
4839 mtd->_block_markbad = nand_block_markbad;
4840 mtd->_max_bad_blocks = nand_max_bad_blocks;
4841 mtd->writebufsize = mtd->writesize;
4844 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4845 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4848 if (!mtd->bitflip_threshold)
4849 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
4851 /* Check, if we should skip the bad block table scan */
4852 if (chip->options & NAND_SKIP_BBTSCAN)
4855 /* Build bad block table */
4856 return chip->scan_bbt(mtd);
4859 kfree(nbuf->databuf);
4860 kfree(nbuf->ecccode);
4861 kfree(nbuf->ecccalc);
4866 EXPORT_SYMBOL(nand_scan_tail);
4869 * is_module_text_address() isn't exported, and it's mostly a pointless
4870 * test if this is a module _anyway_ -- they'd have to try _really_ hard
4871 * to call us from in-kernel code if the core NAND support is modular.
4874 #define caller_is_module() (1)
4876 #define caller_is_module() \
4877 is_module_text_address((unsigned long)__builtin_return_address(0))
4881 * nand_scan - [NAND Interface] Scan for the NAND device
4882 * @mtd: MTD device structure
4883 * @maxchips: number of chips to scan for
4885 * This fills out all the uninitialized function pointers with the defaults.
4886 * The flash ID is read and the mtd/chip structures are filled with the
4887 * appropriate values.
4889 int nand_scan(struct mtd_info *mtd, int maxchips)
4893 ret = nand_scan_ident(mtd, maxchips, NULL);
4895 ret = nand_scan_tail(mtd);
4898 EXPORT_SYMBOL(nand_scan);
4901 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
4902 * @chip: NAND chip object
4904 void nand_cleanup(struct nand_chip *chip)
4906 if (chip->ecc.mode == NAND_ECC_SOFT &&
4907 chip->ecc.algo == NAND_ECC_BCH)
4908 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4910 nand_release_data_interface(chip);
4912 /* Free bad block table memory */
4914 if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) {
4915 kfree(chip->buffers->databuf);
4916 kfree(chip->buffers->ecccode);
4917 kfree(chip->buffers->ecccalc);
4918 kfree(chip->buffers);
4921 /* Free bad block descriptor memory */
4922 if (chip->badblock_pattern && chip->badblock_pattern->options
4923 & NAND_BBT_DYNAMICSTRUCT)
4924 kfree(chip->badblock_pattern);
4926 /* Free manufacturer priv data. */
4927 nand_manufacturer_cleanup(chip);
4929 EXPORT_SYMBOL_GPL(nand_cleanup);
4932 * nand_release - [NAND Interface] Unregister the MTD device and free resources
4933 * held by the NAND device
4934 * @mtd: MTD device structure
4936 void nand_release(struct mtd_info *mtd)
4938 mtd_device_unregister(mtd);
4939 nand_cleanup(mtd_to_nand(mtd));
4941 EXPORT_SYMBOL_GPL(nand_release);
4943 MODULE_LICENSE("GPL");
4944 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4945 MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
4946 MODULE_DESCRIPTION("Generic NAND flash driver code");