5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
9 * Additional technical information is available on
10 * http://www.linux-mtd.infradead.org/doc/nand.html
12 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
13 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
16 * David Woodhouse for adding multichip support
18 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
22 * Enable cached programming for 2k page size chips
23 * Check, if mtd->ecctype should be set to MTD_ECC_HW
24 * if we have HW ecc support.
25 * The AG-AND chips have nice features for speed improvement,
26 * which are not supported yet. Read / program 4 pages in one go.
27 * BBT table is not serialized, has to be fixed
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License version 2 as
31 * published by the Free Software Foundation.
35 #include <linux/module.h>
36 #include <linux/delay.h>
37 #include <linux/errno.h>
38 #include <linux/err.h>
39 #include <linux/sched.h>
40 #include <linux/slab.h>
41 #include <linux/types.h>
42 #include <linux/mtd/mtd.h>
43 #include <linux/mtd/nand.h>
44 #include <linux/mtd/nand_ecc.h>
45 #include <linux/mtd/compatmac.h>
46 #include <linux/interrupt.h>
47 #include <linux/bitops.h>
48 #include <linux/leds.h>
51 #ifdef CONFIG_MTD_PARTITIONS
52 #include <linux/mtd/partitions.h>
55 /* Define default oob placement schemes for large and small page devices */
56 static struct nand_ecclayout nand_oob_8 = {
66 static struct nand_ecclayout nand_oob_16 = {
68 .eccpos = {0, 1, 2, 3, 6, 7},
74 static struct nand_ecclayout nand_oob_64 = {
77 40, 41, 42, 43, 44, 45, 46, 47,
78 48, 49, 50, 51, 52, 53, 54, 55,
79 56, 57, 58, 59, 60, 61, 62, 63},
85 static struct nand_ecclayout nand_oob_128 = {
88 80, 81, 82, 83, 84, 85, 86, 87,
89 88, 89, 90, 91, 92, 93, 94, 95,
90 96, 97, 98, 99, 100, 101, 102, 103,
91 104, 105, 106, 107, 108, 109, 110, 111,
92 112, 113, 114, 115, 116, 117, 118, 119,
93 120, 121, 122, 123, 124, 125, 126, 127},
99 static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
102 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
103 struct mtd_oob_ops *ops);
106 * For devices which display every fart in the system on a separate LED. Is
107 * compiled away when LED support is disabled.
109 DEFINE_LED_TRIGGER(nand_led_trigger);
111 static int check_offs_len(struct mtd_info *mtd,
112 loff_t ofs, uint64_t len)
114 struct nand_chip *chip = mtd->priv;
117 /* Start address must align on block boundary */
118 if (ofs & ((1 << chip->phys_erase_shift) - 1)) {
119 DEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__);
123 /* Length must align on block boundary */
124 if (len & ((1 << chip->phys_erase_shift) - 1)) {
125 DEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
130 /* Do not allow past end of device */
131 if (ofs + len > mtd->size) {
132 DEBUG(MTD_DEBUG_LEVEL0, "%s: Past end of device\n",
141 * nand_release_device - [GENERIC] release chip
142 * @mtd: MTD device structure
144 * Deselect, release chip lock and wake up anyone waiting on the device
146 static void nand_release_device(struct mtd_info *mtd)
148 struct nand_chip *chip = mtd->priv;
150 /* De-select the NAND device */
151 chip->select_chip(mtd, -1);
153 /* Release the controller and the chip */
154 spin_lock(&chip->controller->lock);
155 chip->controller->active = NULL;
156 chip->state = FL_READY;
157 wake_up(&chip->controller->wq);
158 spin_unlock(&chip->controller->lock);
162 * nand_read_byte - [DEFAULT] read one byte from the chip
163 * @mtd: MTD device structure
165 * Default read function for 8bit buswith
167 static uint8_t nand_read_byte(struct mtd_info *mtd)
169 struct nand_chip *chip = mtd->priv;
170 return readb(chip->IO_ADDR_R);
174 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
175 * @mtd: MTD device structure
177 * Default read function for 16bit buswith with
178 * endianess conversion
180 static uint8_t nand_read_byte16(struct mtd_info *mtd)
182 struct nand_chip *chip = mtd->priv;
183 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
187 * nand_read_word - [DEFAULT] read one word from the chip
188 * @mtd: MTD device structure
190 * Default read function for 16bit buswith without
191 * endianess conversion
193 static u16 nand_read_word(struct mtd_info *mtd)
195 struct nand_chip *chip = mtd->priv;
196 return readw(chip->IO_ADDR_R);
200 * nand_select_chip - [DEFAULT] control CE line
201 * @mtd: MTD device structure
202 * @chipnr: chipnumber to select, -1 for deselect
204 * Default select function for 1 chip devices.
206 static void nand_select_chip(struct mtd_info *mtd, int chipnr)
208 struct nand_chip *chip = mtd->priv;
212 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
223 * nand_write_buf - [DEFAULT] write buffer to chip
224 * @mtd: MTD device structure
226 * @len: number of bytes to write
228 * Default write function for 8bit buswith
230 static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
233 struct nand_chip *chip = mtd->priv;
235 for (i = 0; i < len; i++)
236 writeb(buf[i], chip->IO_ADDR_W);
240 * nand_read_buf - [DEFAULT] read chip data into buffer
241 * @mtd: MTD device structure
242 * @buf: buffer to store date
243 * @len: number of bytes to read
245 * Default read function for 8bit buswith
247 static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
250 struct nand_chip *chip = mtd->priv;
252 for (i = 0; i < len; i++)
253 buf[i] = readb(chip->IO_ADDR_R);
257 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
258 * @mtd: MTD device structure
259 * @buf: buffer containing the data to compare
260 * @len: number of bytes to compare
262 * Default verify function for 8bit buswith
264 static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
267 struct nand_chip *chip = mtd->priv;
269 for (i = 0; i < len; i++)
270 if (buf[i] != readb(chip->IO_ADDR_R))
276 * nand_write_buf16 - [DEFAULT] write buffer to chip
277 * @mtd: MTD device structure
279 * @len: number of bytes to write
281 * Default write function for 16bit buswith
283 static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
286 struct nand_chip *chip = mtd->priv;
287 u16 *p = (u16 *) buf;
290 for (i = 0; i < len; i++)
291 writew(p[i], chip->IO_ADDR_W);
296 * nand_read_buf16 - [DEFAULT] read chip data into buffer
297 * @mtd: MTD device structure
298 * @buf: buffer to store date
299 * @len: number of bytes to read
301 * Default read function for 16bit buswith
303 static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
306 struct nand_chip *chip = mtd->priv;
307 u16 *p = (u16 *) buf;
310 for (i = 0; i < len; i++)
311 p[i] = readw(chip->IO_ADDR_R);
315 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
316 * @mtd: MTD device structure
317 * @buf: buffer containing the data to compare
318 * @len: number of bytes to compare
320 * Default verify function for 16bit buswith
322 static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
325 struct nand_chip *chip = mtd->priv;
326 u16 *p = (u16 *) buf;
329 for (i = 0; i < len; i++)
330 if (p[i] != readw(chip->IO_ADDR_R))
337 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
338 * @mtd: MTD device structure
339 * @ofs: offset from device start
340 * @getchip: 0, if the chip is already selected
342 * Check, if the block is bad.
344 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
346 int page, chipnr, res = 0;
347 struct nand_chip *chip = mtd->priv;
350 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
353 chipnr = (int)(ofs >> chip->chip_shift);
355 nand_get_device(chip, mtd, FL_READING);
357 /* Select the NAND device */
358 chip->select_chip(mtd, chipnr);
361 if (chip->options & NAND_BUSWIDTH_16) {
362 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
364 bad = cpu_to_le16(chip->read_word(mtd));
365 if (chip->badblockpos & 0x1)
370 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
371 bad = chip->read_byte(mtd);
374 if (likely(chip->badblockbits == 8))
377 res = hweight8(bad) < chip->badblockbits;
380 nand_release_device(mtd);
386 * nand_default_block_markbad - [DEFAULT] mark a block bad
387 * @mtd: MTD device structure
388 * @ofs: offset from device start
390 * This is the default implementation, which can be overridden by
391 * a hardware specific driver.
393 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
395 struct nand_chip *chip = mtd->priv;
396 uint8_t buf[2] = { 0, 0 };
399 /* Get block number */
400 block = (int)(ofs >> chip->bbt_erase_shift);
402 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
404 /* Do we have a flash based bad block table ? */
405 if (chip->options & NAND_USE_FLASH_BBT)
406 ret = nand_update_bbt(mtd, ofs);
408 /* We write two bytes, so we dont have to mess with 16 bit
411 nand_get_device(chip, mtd, FL_WRITING);
413 chip->ops.len = chip->ops.ooblen = 2;
414 chip->ops.datbuf = NULL;
415 chip->ops.oobbuf = buf;
416 chip->ops.ooboffs = chip->badblockpos & ~0x01;
418 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
419 nand_release_device(mtd);
422 mtd->ecc_stats.badblocks++;
428 * nand_check_wp - [GENERIC] check if the chip is write protected
429 * @mtd: MTD device structure
430 * Check, if the device is write protected
432 * The function expects, that the device is already selected
434 static int nand_check_wp(struct mtd_info *mtd)
436 struct nand_chip *chip = mtd->priv;
437 /* Check the WP bit */
438 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
439 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
443 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
444 * @mtd: MTD device structure
445 * @ofs: offset from device start
446 * @getchip: 0, if the chip is already selected
447 * @allowbbt: 1, if its allowed to access the bbt area
449 * Check, if the block is bad. Either by reading the bad block table or
450 * calling of the scan function.
452 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
455 struct nand_chip *chip = mtd->priv;
458 return chip->block_bad(mtd, ofs, getchip);
460 /* Return info from the table */
461 return nand_isbad_bbt(mtd, ofs, allowbbt);
465 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
466 * @mtd: MTD device structure
469 * Helper function for nand_wait_ready used when needing to wait in interrupt
472 static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
474 struct nand_chip *chip = mtd->priv;
477 /* Wait for the device to get ready */
478 for (i = 0; i < timeo; i++) {
479 if (chip->dev_ready(mtd))
481 touch_softlockup_watchdog();
487 * Wait for the ready pin, after a command
488 * The timeout is catched later.
490 void nand_wait_ready(struct mtd_info *mtd)
492 struct nand_chip *chip = mtd->priv;
493 unsigned long timeo = jiffies + 2;
496 if (in_interrupt() || oops_in_progress)
497 return panic_nand_wait_ready(mtd, 400);
499 led_trigger_event(nand_led_trigger, LED_FULL);
500 /* wait until command is processed or timeout occures */
502 if (chip->dev_ready(mtd))
504 touch_softlockup_watchdog();
505 } while (time_before(jiffies, timeo));
506 led_trigger_event(nand_led_trigger, LED_OFF);
508 EXPORT_SYMBOL_GPL(nand_wait_ready);
511 * nand_command - [DEFAULT] Send command to NAND device
512 * @mtd: MTD device structure
513 * @command: the command to be sent
514 * @column: the column address for this command, -1 if none
515 * @page_addr: the page address for this command, -1 if none
517 * Send command to NAND device. This function is used for small page
518 * devices (256/512 Bytes per page)
520 static void nand_command(struct mtd_info *mtd, unsigned int command,
521 int column, int page_addr)
523 register struct nand_chip *chip = mtd->priv;
524 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
527 * Write out the command to the device.
529 if (command == NAND_CMD_SEQIN) {
532 if (column >= mtd->writesize) {
534 column -= mtd->writesize;
535 readcmd = NAND_CMD_READOOB;
536 } else if (column < 256) {
537 /* First 256 bytes --> READ0 */
538 readcmd = NAND_CMD_READ0;
541 readcmd = NAND_CMD_READ1;
543 chip->cmd_ctrl(mtd, readcmd, ctrl);
544 ctrl &= ~NAND_CTRL_CHANGE;
546 chip->cmd_ctrl(mtd, command, ctrl);
549 * Address cycle, when necessary
551 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
552 /* Serially input address */
554 /* Adjust columns for 16 bit buswidth */
555 if (chip->options & NAND_BUSWIDTH_16)
557 chip->cmd_ctrl(mtd, column, ctrl);
558 ctrl &= ~NAND_CTRL_CHANGE;
560 if (page_addr != -1) {
561 chip->cmd_ctrl(mtd, page_addr, ctrl);
562 ctrl &= ~NAND_CTRL_CHANGE;
563 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
564 /* One more address cycle for devices > 32MiB */
565 if (chip->chipsize > (32 << 20))
566 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
568 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
571 * program and erase have their own busy handlers
572 * status and sequential in needs no delay
576 case NAND_CMD_PAGEPROG:
577 case NAND_CMD_ERASE1:
578 case NAND_CMD_ERASE2:
580 case NAND_CMD_STATUS:
586 udelay(chip->chip_delay);
587 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
588 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
590 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
591 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
594 /* This applies to read commands */
597 * If we don't have access to the busy pin, we apply the given
600 if (!chip->dev_ready) {
601 udelay(chip->chip_delay);
605 /* Apply this short delay always to ensure that we do wait tWB in
606 * any case on any machine. */
609 nand_wait_ready(mtd);
613 * nand_command_lp - [DEFAULT] Send command to NAND large page device
614 * @mtd: MTD device structure
615 * @command: the command to be sent
616 * @column: the column address for this command, -1 if none
617 * @page_addr: the page address for this command, -1 if none
619 * Send command to NAND device. This is the version for the new large page
620 * devices We dont have the separate regions as we have in the small page
621 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
623 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
624 int column, int page_addr)
626 register struct nand_chip *chip = mtd->priv;
628 /* Emulate NAND_CMD_READOOB */
629 if (command == NAND_CMD_READOOB) {
630 column += mtd->writesize;
631 command = NAND_CMD_READ0;
634 /* Command latch cycle */
635 chip->cmd_ctrl(mtd, command & 0xff,
636 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
638 if (column != -1 || page_addr != -1) {
639 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
641 /* Serially input address */
643 /* Adjust columns for 16 bit buswidth */
644 if (chip->options & NAND_BUSWIDTH_16)
646 chip->cmd_ctrl(mtd, column, ctrl);
647 ctrl &= ~NAND_CTRL_CHANGE;
648 chip->cmd_ctrl(mtd, column >> 8, ctrl);
650 if (page_addr != -1) {
651 chip->cmd_ctrl(mtd, page_addr, ctrl);
652 chip->cmd_ctrl(mtd, page_addr >> 8,
653 NAND_NCE | NAND_ALE);
654 /* One more address cycle for devices > 128MiB */
655 if (chip->chipsize > (128 << 20))
656 chip->cmd_ctrl(mtd, page_addr >> 16,
657 NAND_NCE | NAND_ALE);
660 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
663 * program and erase have their own busy handlers
664 * status, sequential in, and deplete1 need no delay
668 case NAND_CMD_CACHEDPROG:
669 case NAND_CMD_PAGEPROG:
670 case NAND_CMD_ERASE1:
671 case NAND_CMD_ERASE2:
674 case NAND_CMD_STATUS:
675 case NAND_CMD_DEPLETE1:
679 * read error status commands require only a short delay
681 case NAND_CMD_STATUS_ERROR:
682 case NAND_CMD_STATUS_ERROR0:
683 case NAND_CMD_STATUS_ERROR1:
684 case NAND_CMD_STATUS_ERROR2:
685 case NAND_CMD_STATUS_ERROR3:
686 udelay(chip->chip_delay);
692 udelay(chip->chip_delay);
693 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
694 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
695 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
696 NAND_NCE | NAND_CTRL_CHANGE);
697 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
700 case NAND_CMD_RNDOUT:
701 /* No ready / busy check necessary */
702 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
703 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
704 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
705 NAND_NCE | NAND_CTRL_CHANGE);
709 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
710 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
711 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
712 NAND_NCE | NAND_CTRL_CHANGE);
714 /* This applies to read commands */
717 * If we don't have access to the busy pin, we apply the given
720 if (!chip->dev_ready) {
721 udelay(chip->chip_delay);
726 /* Apply this short delay always to ensure that we do wait tWB in
727 * any case on any machine. */
730 nand_wait_ready(mtd);
734 * panic_nand_get_device - [GENERIC] Get chip for selected access
735 * @chip: the nand chip descriptor
736 * @mtd: MTD device structure
737 * @new_state: the state which is requested
739 * Used when in panic, no locks are taken.
741 static void panic_nand_get_device(struct nand_chip *chip,
742 struct mtd_info *mtd, int new_state)
744 /* Hardware controller shared among independend devices */
745 chip->controller->active = chip;
746 chip->state = new_state;
750 * nand_get_device - [GENERIC] Get chip for selected access
751 * @chip: the nand chip descriptor
752 * @mtd: MTD device structure
753 * @new_state: the state which is requested
755 * Get the device and lock it for exclusive access
758 nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
760 spinlock_t *lock = &chip->controller->lock;
761 wait_queue_head_t *wq = &chip->controller->wq;
762 DECLARE_WAITQUEUE(wait, current);
766 /* Hardware controller shared among independent devices */
767 if (!chip->controller->active)
768 chip->controller->active = chip;
770 if (chip->controller->active == chip && chip->state == FL_READY) {
771 chip->state = new_state;
775 if (new_state == FL_PM_SUSPENDED) {
776 if (chip->controller->active->state == FL_PM_SUSPENDED) {
777 chip->state = FL_PM_SUSPENDED;
782 set_current_state(TASK_UNINTERRUPTIBLE);
783 add_wait_queue(wq, &wait);
786 remove_wait_queue(wq, &wait);
791 * panic_nand_wait - [GENERIC] wait until the command is done
792 * @mtd: MTD device structure
793 * @chip: NAND chip structure
796 * Wait for command done. This is a helper function for nand_wait used when
797 * we are in interrupt context. May happen when in panic and trying to write
798 * an oops trough mtdoops.
800 static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
804 for (i = 0; i < timeo; i++) {
805 if (chip->dev_ready) {
806 if (chip->dev_ready(mtd))
809 if (chip->read_byte(mtd) & NAND_STATUS_READY)
817 * nand_wait - [DEFAULT] wait until the command is done
818 * @mtd: MTD device structure
819 * @chip: NAND chip structure
821 * Wait for command done. This applies to erase and program only
822 * Erase can take up to 400ms and program up to 20ms according to
823 * general NAND and SmartMedia specs
825 static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
828 unsigned long timeo = jiffies;
829 int status, state = chip->state;
831 if (state == FL_ERASING)
832 timeo += (HZ * 400) / 1000;
834 timeo += (HZ * 20) / 1000;
836 led_trigger_event(nand_led_trigger, LED_FULL);
838 /* Apply this short delay always to ensure that we do wait tWB in
839 * any case on any machine. */
842 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
843 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
845 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
847 if (in_interrupt() || oops_in_progress)
848 panic_nand_wait(mtd, chip, timeo);
850 while (time_before(jiffies, timeo)) {
851 if (chip->dev_ready) {
852 if (chip->dev_ready(mtd))
855 if (chip->read_byte(mtd) & NAND_STATUS_READY)
861 led_trigger_event(nand_led_trigger, LED_OFF);
863 status = (int)chip->read_byte(mtd);
868 * __nand_unlock - [REPLACABLE] unlocks specified locked blockes
870 * @param mtd - mtd info
871 * @param ofs - offset to start unlock from
872 * @param len - length to unlock
873 * @invert - when = 0, unlock the range of blocks within the lower and
874 * upper boundary address
875 * whne = 1, unlock the range of blocks outside the boundaries
876 * of the lower and upper boundary address
878 * @return - unlock status
880 static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
881 uint64_t len, int invert)
885 struct nand_chip *chip = mtd->priv;
887 /* Submit address of first page to unlock */
888 page = ofs >> chip->page_shift;
889 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
891 /* Submit address of last page to unlock */
892 page = (ofs + len) >> chip->page_shift;
893 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
894 (page | invert) & chip->pagemask);
896 /* Call wait ready function */
897 status = chip->waitfunc(mtd, chip);
899 /* See if device thinks it succeeded */
901 DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n",
910 * nand_unlock - [REPLACABLE] unlocks specified locked blockes
912 * @param mtd - mtd info
913 * @param ofs - offset to start unlock from
914 * @param len - length to unlock
916 * @return - unlock status
918 int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
922 struct nand_chip *chip = mtd->priv;
924 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
925 __func__, (unsigned long long)ofs, len);
927 if (check_offs_len(mtd, ofs, len))
930 /* Align to last block address if size addresses end of the device */
931 if (ofs + len == mtd->size)
932 len -= mtd->erasesize;
934 nand_get_device(chip, mtd, FL_UNLOCKING);
936 /* Shift to get chip number */
937 chipnr = ofs >> chip->chip_shift;
939 chip->select_chip(mtd, chipnr);
941 /* Check, if it is write protected */
942 if (nand_check_wp(mtd)) {
943 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
949 ret = __nand_unlock(mtd, ofs, len, 0);
952 /* de-select the NAND device */
953 chip->select_chip(mtd, -1);
955 nand_release_device(mtd);
961 * nand_lock - [REPLACABLE] locks all blockes present in the device
963 * @param mtd - mtd info
964 * @param ofs - offset to start unlock from
965 * @param len - length to unlock
967 * @return - lock status
969 * This feature is not support in many NAND parts. 'Micron' NAND parts
970 * do have this feature, but it allows only to lock all blocks not for
971 * specified range for block.
973 * Implementing 'lock' feature by making use of 'unlock', for now.
975 int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
978 int chipnr, status, page;
979 struct nand_chip *chip = mtd->priv;
981 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
982 __func__, (unsigned long long)ofs, len);
984 if (check_offs_len(mtd, ofs, len))
987 nand_get_device(chip, mtd, FL_LOCKING);
989 /* Shift to get chip number */
990 chipnr = ofs >> chip->chip_shift;
992 chip->select_chip(mtd, chipnr);
994 /* Check, if it is write protected */
995 if (nand_check_wp(mtd)) {
996 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
998 status = MTD_ERASE_FAILED;
1003 /* Submit address of first page to lock */
1004 page = ofs >> chip->page_shift;
1005 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1007 /* Call wait ready function */
1008 status = chip->waitfunc(mtd, chip);
1010 /* See if device thinks it succeeded */
1011 if (status & 0x01) {
1012 DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n",
1018 ret = __nand_unlock(mtd, ofs, len, 0x1);
1021 /* de-select the NAND device */
1022 chip->select_chip(mtd, -1);
1024 nand_release_device(mtd);
1030 * nand_read_page_raw - [Intern] read raw page data without ecc
1031 * @mtd: mtd info structure
1032 * @chip: nand chip info structure
1033 * @buf: buffer to store read data
1034 * @page: page number to read
1036 * Not for syndrome calculating ecc controllers, which use a special oob layout
1038 static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1039 uint8_t *buf, int page)
1041 chip->read_buf(mtd, buf, mtd->writesize);
1042 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1047 * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
1048 * @mtd: mtd info structure
1049 * @chip: nand chip info structure
1050 * @buf: buffer to store read data
1051 * @page: page number to read
1053 * We need a special oob layout and handling even when OOB isn't used.
1055 static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1056 uint8_t *buf, int page)
1058 int eccsize = chip->ecc.size;
1059 int eccbytes = chip->ecc.bytes;
1060 uint8_t *oob = chip->oob_poi;
1063 for (steps = chip->ecc.steps; steps > 0; steps--) {
1064 chip->read_buf(mtd, buf, eccsize);
1067 if (chip->ecc.prepad) {
1068 chip->read_buf(mtd, oob, chip->ecc.prepad);
1069 oob += chip->ecc.prepad;
1072 chip->read_buf(mtd, oob, eccbytes);
1075 if (chip->ecc.postpad) {
1076 chip->read_buf(mtd, oob, chip->ecc.postpad);
1077 oob += chip->ecc.postpad;
1081 size = mtd->oobsize - (oob - chip->oob_poi);
1083 chip->read_buf(mtd, oob, size);
1089 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
1090 * @mtd: mtd info structure
1091 * @chip: nand chip info structure
1092 * @buf: buffer to store read data
1093 * @page: page number to read
1095 static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1096 uint8_t *buf, int page)
1098 int i, eccsize = chip->ecc.size;
1099 int eccbytes = chip->ecc.bytes;
1100 int eccsteps = chip->ecc.steps;
1102 uint8_t *ecc_calc = chip->buffers->ecccalc;
1103 uint8_t *ecc_code = chip->buffers->ecccode;
1104 uint32_t *eccpos = chip->ecc.layout->eccpos;
1106 chip->ecc.read_page_raw(mtd, chip, buf, page);
1108 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1109 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1111 for (i = 0; i < chip->ecc.total; i++)
1112 ecc_code[i] = chip->oob_poi[eccpos[i]];
1114 eccsteps = chip->ecc.steps;
1117 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1120 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1122 mtd->ecc_stats.failed++;
1124 mtd->ecc_stats.corrected += stat;
1130 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
1131 * @mtd: mtd info structure
1132 * @chip: nand chip info structure
1133 * @data_offs: offset of requested data within the page
1134 * @readlen: data length
1135 * @bufpoi: buffer to store read data
1137 static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
1139 int start_step, end_step, num_steps;
1140 uint32_t *eccpos = chip->ecc.layout->eccpos;
1142 int data_col_addr, i, gaps = 0;
1143 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1144 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
1146 /* Column address wihin the page aligned to ECC size (256bytes). */
1147 start_step = data_offs / chip->ecc.size;
1148 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1149 num_steps = end_step - start_step + 1;
1151 /* Data size aligned to ECC ecc.size*/
1152 datafrag_len = num_steps * chip->ecc.size;
1153 eccfrag_len = num_steps * chip->ecc.bytes;
1155 data_col_addr = start_step * chip->ecc.size;
1156 /* If we read not a page aligned data */
1157 if (data_col_addr != 0)
1158 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1160 p = bufpoi + data_col_addr;
1161 chip->read_buf(mtd, p, datafrag_len);
1164 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1165 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1167 /* The performance is faster if to position offsets
1168 according to ecc.pos. Let make sure here that
1169 there are no gaps in ecc positions */
1170 for (i = 0; i < eccfrag_len - 1; i++) {
1171 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1172 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1178 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1179 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1181 /* send the command to read the particular ecc bytes */
1182 /* take care about buswidth alignment in read_buf */
1183 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
1184 aligned_len = eccfrag_len;
1185 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
1187 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
1190 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
1191 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1194 for (i = 0; i < eccfrag_len; i++)
1195 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
1197 p = bufpoi + data_col_addr;
1198 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1201 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1203 mtd->ecc_stats.failed++;
1205 mtd->ecc_stats.corrected += stat;
1211 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
1212 * @mtd: mtd info structure
1213 * @chip: nand chip info structure
1214 * @buf: buffer to store read data
1215 * @page: page number to read
1217 * Not for syndrome calculating ecc controllers which need a special oob layout
1219 static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1220 uint8_t *buf, int page)
1222 int i, eccsize = chip->ecc.size;
1223 int eccbytes = chip->ecc.bytes;
1224 int eccsteps = chip->ecc.steps;
1226 uint8_t *ecc_calc = chip->buffers->ecccalc;
1227 uint8_t *ecc_code = chip->buffers->ecccode;
1228 uint32_t *eccpos = chip->ecc.layout->eccpos;
1230 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1231 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1232 chip->read_buf(mtd, p, eccsize);
1233 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1235 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1237 for (i = 0; i < chip->ecc.total; i++)
1238 ecc_code[i] = chip->oob_poi[eccpos[i]];
1240 eccsteps = chip->ecc.steps;
1243 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1246 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1248 mtd->ecc_stats.failed++;
1250 mtd->ecc_stats.corrected += stat;
1256 * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
1257 * @mtd: mtd info structure
1258 * @chip: nand chip info structure
1259 * @buf: buffer to store read data
1260 * @page: page number to read
1262 * Hardware ECC for large page chips, require OOB to be read first.
1263 * For this ECC mode, the write_page method is re-used from ECC_HW.
1264 * These methods read/write ECC from the OOB area, unlike the
1265 * ECC_HW_SYNDROME support with multiple ECC steps, follows the
1266 * "infix ECC" scheme and reads/writes ECC from the data area, by
1267 * overwriting the NAND manufacturer bad block markings.
1269 static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1270 struct nand_chip *chip, uint8_t *buf, int page)
1272 int i, eccsize = chip->ecc.size;
1273 int eccbytes = chip->ecc.bytes;
1274 int eccsteps = chip->ecc.steps;
1276 uint8_t *ecc_code = chip->buffers->ecccode;
1277 uint32_t *eccpos = chip->ecc.layout->eccpos;
1278 uint8_t *ecc_calc = chip->buffers->ecccalc;
1280 /* Read the OOB area first */
1281 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1282 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1283 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1285 for (i = 0; i < chip->ecc.total; i++)
1286 ecc_code[i] = chip->oob_poi[eccpos[i]];
1288 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1291 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1292 chip->read_buf(mtd, p, eccsize);
1293 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1295 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1297 mtd->ecc_stats.failed++;
1299 mtd->ecc_stats.corrected += stat;
1305 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
1306 * @mtd: mtd info structure
1307 * @chip: nand chip info structure
1308 * @buf: buffer to store read data
1309 * @page: page number to read
1311 * The hw generator calculates the error syndrome automatically. Therefor
1312 * we need a special oob layout and handling.
1314 static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1315 uint8_t *buf, int page)
1317 int i, eccsize = chip->ecc.size;
1318 int eccbytes = chip->ecc.bytes;
1319 int eccsteps = chip->ecc.steps;
1321 uint8_t *oob = chip->oob_poi;
1323 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1326 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1327 chip->read_buf(mtd, p, eccsize);
1329 if (chip->ecc.prepad) {
1330 chip->read_buf(mtd, oob, chip->ecc.prepad);
1331 oob += chip->ecc.prepad;
1334 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1335 chip->read_buf(mtd, oob, eccbytes);
1336 stat = chip->ecc.correct(mtd, p, oob, NULL);
1339 mtd->ecc_stats.failed++;
1341 mtd->ecc_stats.corrected += stat;
1345 if (chip->ecc.postpad) {
1346 chip->read_buf(mtd, oob, chip->ecc.postpad);
1347 oob += chip->ecc.postpad;
1351 /* Calculate remaining oob bytes */
1352 i = mtd->oobsize - (oob - chip->oob_poi);
1354 chip->read_buf(mtd, oob, i);
1360 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1361 * @chip: nand chip structure
1362 * @oob: oob destination address
1363 * @ops: oob ops structure
1364 * @len: size of oob to transfer
1366 static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1367 struct mtd_oob_ops *ops, size_t len)
1373 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1376 case MTD_OOB_AUTO: {
1377 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1378 uint32_t boffs = 0, roffs = ops->ooboffs;
1381 for(; free->length && len; free++, len -= bytes) {
1382 /* Read request not from offset 0 ? */
1383 if (unlikely(roffs)) {
1384 if (roffs >= free->length) {
1385 roffs -= free->length;
1388 boffs = free->offset + roffs;
1389 bytes = min_t(size_t, len,
1390 (free->length - roffs));
1393 bytes = min_t(size_t, len, free->length);
1394 boffs = free->offset;
1396 memcpy(oob, chip->oob_poi + boffs, bytes);
1408 * nand_do_read_ops - [Internal] Read data with ECC
1410 * @mtd: MTD device structure
1411 * @from: offset to read from
1412 * @ops: oob ops structure
1414 * Internal function. Called with chip held.
1416 static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1417 struct mtd_oob_ops *ops)
1419 int chipnr, page, realpage, col, bytes, aligned;
1420 struct nand_chip *chip = mtd->priv;
1421 struct mtd_ecc_stats stats;
1422 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1425 uint32_t readlen = ops->len;
1426 uint32_t oobreadlen = ops->ooblen;
1427 uint32_t max_oobsize = ops->mode == MTD_OOB_AUTO ?
1428 mtd->oobavail : mtd->oobsize;
1430 uint8_t *bufpoi, *oob, *buf;
1432 stats = mtd->ecc_stats;
1434 chipnr = (int)(from >> chip->chip_shift);
1435 chip->select_chip(mtd, chipnr);
1437 realpage = (int)(from >> chip->page_shift);
1438 page = realpage & chip->pagemask;
1440 col = (int)(from & (mtd->writesize - 1));
1446 bytes = min(mtd->writesize - col, readlen);
1447 aligned = (bytes == mtd->writesize);
1449 /* Is the current page in the buffer ? */
1450 if (realpage != chip->pagebuf || oob) {
1451 bufpoi = aligned ? buf : chip->buffers->databuf;
1453 if (likely(sndcmd)) {
1454 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1458 /* Now read the page into the buffer */
1459 if (unlikely(ops->mode == MTD_OOB_RAW))
1460 ret = chip->ecc.read_page_raw(mtd, chip,
1462 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1463 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
1465 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1470 /* Transfer not aligned data */
1472 if (!NAND_SUBPAGE_READ(chip) && !oob)
1473 chip->pagebuf = realpage;
1474 memcpy(buf, chip->buffers->databuf + col, bytes);
1479 if (unlikely(oob)) {
1481 int toread = min(oobreadlen, max_oobsize);
1484 oob = nand_transfer_oob(chip,
1486 oobreadlen -= toread;
1490 if (!(chip->options & NAND_NO_READRDY)) {
1492 * Apply delay or wait for ready/busy pin. Do
1493 * this before the AUTOINCR check, so no
1494 * problems arise if a chip which does auto
1495 * increment is marked as NOAUTOINCR by the
1498 if (!chip->dev_ready)
1499 udelay(chip->chip_delay);
1501 nand_wait_ready(mtd);
1504 memcpy(buf, chip->buffers->databuf + col, bytes);
1513 /* For subsequent reads align to page boundary. */
1515 /* Increment page address */
1518 page = realpage & chip->pagemask;
1519 /* Check, if we cross a chip boundary */
1522 chip->select_chip(mtd, -1);
1523 chip->select_chip(mtd, chipnr);
1526 /* Check, if the chip supports auto page increment
1527 * or if we have hit a block boundary.
1529 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1533 ops->retlen = ops->len - (size_t) readlen;
1535 ops->oobretlen = ops->ooblen - oobreadlen;
1540 if (mtd->ecc_stats.failed - stats.failed)
1543 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1547 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1548 * @mtd: MTD device structure
1549 * @from: offset to read from
1550 * @len: number of bytes to read
1551 * @retlen: pointer to variable to store the number of read bytes
1552 * @buf: the databuffer to put data
1554 * Get hold of the chip and call nand_do_read
1556 static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1557 size_t *retlen, uint8_t *buf)
1559 struct nand_chip *chip = mtd->priv;
1562 /* Do not allow reads past end of device */
1563 if ((from + len) > mtd->size)
1568 nand_get_device(chip, mtd, FL_READING);
1570 chip->ops.len = len;
1571 chip->ops.datbuf = buf;
1572 chip->ops.oobbuf = NULL;
1574 ret = nand_do_read_ops(mtd, from, &chip->ops);
1576 *retlen = chip->ops.retlen;
1578 nand_release_device(mtd);
1584 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1585 * @mtd: mtd info structure
1586 * @chip: nand chip info structure
1587 * @page: page number to read
1588 * @sndcmd: flag whether to issue read command or not
1590 static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1591 int page, int sndcmd)
1594 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1597 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1602 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1604 * @mtd: mtd info structure
1605 * @chip: nand chip info structure
1606 * @page: page number to read
1607 * @sndcmd: flag whether to issue read command or not
1609 static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1610 int page, int sndcmd)
1612 uint8_t *buf = chip->oob_poi;
1613 int length = mtd->oobsize;
1614 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1615 int eccsize = chip->ecc.size;
1616 uint8_t *bufpoi = buf;
1617 int i, toread, sndrnd = 0, pos;
1619 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1620 for (i = 0; i < chip->ecc.steps; i++) {
1622 pos = eccsize + i * (eccsize + chunk);
1623 if (mtd->writesize > 512)
1624 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1626 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1629 toread = min_t(int, length, chunk);
1630 chip->read_buf(mtd, bufpoi, toread);
1635 chip->read_buf(mtd, bufpoi, length);
1641 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1642 * @mtd: mtd info structure
1643 * @chip: nand chip info structure
1644 * @page: page number to write
1646 static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1650 const uint8_t *buf = chip->oob_poi;
1651 int length = mtd->oobsize;
1653 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1654 chip->write_buf(mtd, buf, length);
1655 /* Send command to program the OOB data */
1656 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1658 status = chip->waitfunc(mtd, chip);
1660 return status & NAND_STATUS_FAIL ? -EIO : 0;
1664 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1665 * with syndrome - only for large page flash !
1666 * @mtd: mtd info structure
1667 * @chip: nand chip info structure
1668 * @page: page number to write
1670 static int nand_write_oob_syndrome(struct mtd_info *mtd,
1671 struct nand_chip *chip, int page)
1673 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1674 int eccsize = chip->ecc.size, length = mtd->oobsize;
1675 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1676 const uint8_t *bufpoi = chip->oob_poi;
1679 * data-ecc-data-ecc ... ecc-oob
1681 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1683 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1684 pos = steps * (eccsize + chunk);
1689 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1690 for (i = 0; i < steps; i++) {
1692 if (mtd->writesize <= 512) {
1693 uint32_t fill = 0xFFFFFFFF;
1697 int num = min_t(int, len, 4);
1698 chip->write_buf(mtd, (uint8_t *)&fill,
1703 pos = eccsize + i * (eccsize + chunk);
1704 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1708 len = min_t(int, length, chunk);
1709 chip->write_buf(mtd, bufpoi, len);
1714 chip->write_buf(mtd, bufpoi, length);
1716 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1717 status = chip->waitfunc(mtd, chip);
1719 return status & NAND_STATUS_FAIL ? -EIO : 0;
1723 * nand_do_read_oob - [Intern] NAND read out-of-band
1724 * @mtd: MTD device structure
1725 * @from: offset to read from
1726 * @ops: oob operations description structure
1728 * NAND read out-of-band data from the spare area
1730 static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1731 struct mtd_oob_ops *ops)
1733 int page, realpage, chipnr, sndcmd = 1;
1734 struct nand_chip *chip = mtd->priv;
1735 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1736 int readlen = ops->ooblen;
1738 uint8_t *buf = ops->oobbuf;
1740 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
1741 __func__, (unsigned long long)from, readlen);
1743 if (ops->mode == MTD_OOB_AUTO)
1744 len = chip->ecc.layout->oobavail;
1748 if (unlikely(ops->ooboffs >= len)) {
1749 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
1750 "outside oob\n", __func__);
1754 /* Do not allow reads past end of device */
1755 if (unlikely(from >= mtd->size ||
1756 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1757 (from >> chip->page_shift)) * len)) {
1758 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
1759 "of device\n", __func__);
1763 chipnr = (int)(from >> chip->chip_shift);
1764 chip->select_chip(mtd, chipnr);
1766 /* Shift to get page */
1767 realpage = (int)(from >> chip->page_shift);
1768 page = realpage & chip->pagemask;
1771 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
1773 len = min(len, readlen);
1774 buf = nand_transfer_oob(chip, buf, ops, len);
1776 if (!(chip->options & NAND_NO_READRDY)) {
1778 * Apply delay or wait for ready/busy pin. Do this
1779 * before the AUTOINCR check, so no problems arise if a
1780 * chip which does auto increment is marked as
1781 * NOAUTOINCR by the board driver.
1783 if (!chip->dev_ready)
1784 udelay(chip->chip_delay);
1786 nand_wait_ready(mtd);
1793 /* Increment page address */
1796 page = realpage & chip->pagemask;
1797 /* Check, if we cross a chip boundary */
1800 chip->select_chip(mtd, -1);
1801 chip->select_chip(mtd, chipnr);
1804 /* Check, if the chip supports auto page increment
1805 * or if we have hit a block boundary.
1807 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1811 ops->oobretlen = ops->ooblen;
1816 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
1817 * @mtd: MTD device structure
1818 * @from: offset to read from
1819 * @ops: oob operation description structure
1821 * NAND read data and/or out-of-band data
1823 static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1824 struct mtd_oob_ops *ops)
1826 struct nand_chip *chip = mtd->priv;
1827 int ret = -ENOTSUPP;
1831 /* Do not allow reads past end of device */
1832 if (ops->datbuf && (from + ops->len) > mtd->size) {
1833 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
1834 "beyond end of device\n", __func__);
1838 nand_get_device(chip, mtd, FL_READING);
1851 ret = nand_do_read_oob(mtd, from, ops);
1853 ret = nand_do_read_ops(mtd, from, ops);
1856 nand_release_device(mtd);
1862 * nand_write_page_raw - [Intern] raw page write function
1863 * @mtd: mtd info structure
1864 * @chip: nand chip info structure
1867 * Not for syndrome calculating ecc controllers, which use a special oob layout
1869 static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1872 chip->write_buf(mtd, buf, mtd->writesize);
1873 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1877 * nand_write_page_raw_syndrome - [Intern] raw page write function
1878 * @mtd: mtd info structure
1879 * @chip: nand chip info structure
1882 * We need a special oob layout and handling even when ECC isn't checked.
1884 static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1887 int eccsize = chip->ecc.size;
1888 int eccbytes = chip->ecc.bytes;
1889 uint8_t *oob = chip->oob_poi;
1892 for (steps = chip->ecc.steps; steps > 0; steps--) {
1893 chip->write_buf(mtd, buf, eccsize);
1896 if (chip->ecc.prepad) {
1897 chip->write_buf(mtd, oob, chip->ecc.prepad);
1898 oob += chip->ecc.prepad;
1901 chip->read_buf(mtd, oob, eccbytes);
1904 if (chip->ecc.postpad) {
1905 chip->write_buf(mtd, oob, chip->ecc.postpad);
1906 oob += chip->ecc.postpad;
1910 size = mtd->oobsize - (oob - chip->oob_poi);
1912 chip->write_buf(mtd, oob, size);
1915 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
1916 * @mtd: mtd info structure
1917 * @chip: nand chip info structure
1920 static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1923 int i, eccsize = chip->ecc.size;
1924 int eccbytes = chip->ecc.bytes;
1925 int eccsteps = chip->ecc.steps;
1926 uint8_t *ecc_calc = chip->buffers->ecccalc;
1927 const uint8_t *p = buf;
1928 uint32_t *eccpos = chip->ecc.layout->eccpos;
1930 /* Software ecc calculation */
1931 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1932 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1934 for (i = 0; i < chip->ecc.total; i++)
1935 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1937 chip->ecc.write_page_raw(mtd, chip, buf);
1941 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
1942 * @mtd: mtd info structure
1943 * @chip: nand chip info structure
1946 static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1949 int i, eccsize = chip->ecc.size;
1950 int eccbytes = chip->ecc.bytes;
1951 int eccsteps = chip->ecc.steps;
1952 uint8_t *ecc_calc = chip->buffers->ecccalc;
1953 const uint8_t *p = buf;
1954 uint32_t *eccpos = chip->ecc.layout->eccpos;
1956 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1957 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1958 chip->write_buf(mtd, p, eccsize);
1959 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1962 for (i = 0; i < chip->ecc.total; i++)
1963 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1965 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1969 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
1970 * @mtd: mtd info structure
1971 * @chip: nand chip info structure
1974 * The hw generator calculates the error syndrome automatically. Therefor
1975 * we need a special oob layout and handling.
1977 static void nand_write_page_syndrome(struct mtd_info *mtd,
1978 struct nand_chip *chip, const uint8_t *buf)
1980 int i, eccsize = chip->ecc.size;
1981 int eccbytes = chip->ecc.bytes;
1982 int eccsteps = chip->ecc.steps;
1983 const uint8_t *p = buf;
1984 uint8_t *oob = chip->oob_poi;
1986 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1988 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1989 chip->write_buf(mtd, p, eccsize);
1991 if (chip->ecc.prepad) {
1992 chip->write_buf(mtd, oob, chip->ecc.prepad);
1993 oob += chip->ecc.prepad;
1996 chip->ecc.calculate(mtd, p, oob);
1997 chip->write_buf(mtd, oob, eccbytes);
2000 if (chip->ecc.postpad) {
2001 chip->write_buf(mtd, oob, chip->ecc.postpad);
2002 oob += chip->ecc.postpad;
2006 /* Calculate remaining oob bytes */
2007 i = mtd->oobsize - (oob - chip->oob_poi);
2009 chip->write_buf(mtd, oob, i);
2013 * nand_write_page - [REPLACEABLE] write one page
2014 * @mtd: MTD device structure
2015 * @chip: NAND chip descriptor
2016 * @buf: the data to write
2017 * @page: page number to write
2018 * @cached: cached programming
2019 * @raw: use _raw version of write_page
2021 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
2022 const uint8_t *buf, int page, int cached, int raw)
2026 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2029 chip->ecc.write_page_raw(mtd, chip, buf);
2031 chip->ecc.write_page(mtd, chip, buf);
2034 * Cached progamming disabled for now, Not sure if its worth the
2035 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
2039 if (!cached || !(chip->options & NAND_CACHEPRG)) {
2041 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2042 status = chip->waitfunc(mtd, chip);
2044 * See if operation failed and additional status checks are
2047 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2048 status = chip->errstat(mtd, chip, FL_WRITING, status,
2051 if (status & NAND_STATUS_FAIL)
2054 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
2055 status = chip->waitfunc(mtd, chip);
2058 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
2059 /* Send command to read back the data */
2060 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
2062 if (chip->verify_buf(mtd, buf, mtd->writesize))
2069 * nand_fill_oob - [Internal] Transfer client buffer to oob
2070 * @chip: nand chip structure
2071 * @oob: oob data buffer
2072 * @ops: oob ops structure
2074 static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len,
2075 struct mtd_oob_ops *ops)
2081 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2084 case MTD_OOB_AUTO: {
2085 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2086 uint32_t boffs = 0, woffs = ops->ooboffs;
2089 for(; free->length && len; free++, len -= bytes) {
2090 /* Write request not from offset 0 ? */
2091 if (unlikely(woffs)) {
2092 if (woffs >= free->length) {
2093 woffs -= free->length;
2096 boffs = free->offset + woffs;
2097 bytes = min_t(size_t, len,
2098 (free->length - woffs));
2101 bytes = min_t(size_t, len, free->length);
2102 boffs = free->offset;
2104 memcpy(chip->oob_poi + boffs, oob, bytes);
2115 #define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
2118 * nand_do_write_ops - [Internal] NAND write with ECC
2119 * @mtd: MTD device structure
2120 * @to: offset to write to
2121 * @ops: oob operations description structure
2123 * NAND write with ECC
2125 static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2126 struct mtd_oob_ops *ops)
2128 int chipnr, realpage, page, blockmask, column;
2129 struct nand_chip *chip = mtd->priv;
2130 uint32_t writelen = ops->len;
2132 uint32_t oobwritelen = ops->ooblen;
2133 uint32_t oobmaxlen = ops->mode == MTD_OOB_AUTO ?
2134 mtd->oobavail : mtd->oobsize;
2136 uint8_t *oob = ops->oobbuf;
2137 uint8_t *buf = ops->datbuf;
2144 /* reject writes, which are not page aligned */
2145 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
2146 printk(KERN_NOTICE "%s: Attempt to write not "
2147 "page aligned data\n", __func__);
2151 column = to & (mtd->writesize - 1);
2152 subpage = column || (writelen & (mtd->writesize - 1));
2157 chipnr = (int)(to >> chip->chip_shift);
2158 chip->select_chip(mtd, chipnr);
2160 /* Check, if it is write protected */
2161 if (nand_check_wp(mtd))
2164 realpage = (int)(to >> chip->page_shift);
2165 page = realpage & chip->pagemask;
2166 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2168 /* Invalidate the page cache, when we write to the cached page */
2169 if (to <= (chip->pagebuf << chip->page_shift) &&
2170 (chip->pagebuf << chip->page_shift) < (to + ops->len))
2173 /* If we're not given explicit OOB data, let it be 0xFF */
2175 memset(chip->oob_poi, 0xff, mtd->oobsize);
2177 /* Don't allow multipage oob writes with offset */
2178 if (ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
2182 int bytes = mtd->writesize;
2183 int cached = writelen > bytes && page != blockmask;
2184 uint8_t *wbuf = buf;
2186 /* Partial page write ? */
2187 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2189 bytes = min_t(int, bytes - column, (int) writelen);
2191 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2192 memcpy(&chip->buffers->databuf[column], buf, bytes);
2193 wbuf = chip->buffers->databuf;
2196 if (unlikely(oob)) {
2197 size_t len = min(oobwritelen, oobmaxlen);
2198 oob = nand_fill_oob(chip, oob, len, ops);
2202 ret = chip->write_page(mtd, chip, wbuf, page, cached,
2203 (ops->mode == MTD_OOB_RAW));
2215 page = realpage & chip->pagemask;
2216 /* Check, if we cross a chip boundary */
2219 chip->select_chip(mtd, -1);
2220 chip->select_chip(mtd, chipnr);
2224 ops->retlen = ops->len - writelen;
2226 ops->oobretlen = ops->ooblen;
2231 * panic_nand_write - [MTD Interface] NAND write with ECC
2232 * @mtd: MTD device structure
2233 * @to: offset to write to
2234 * @len: number of bytes to write
2235 * @retlen: pointer to variable to store the number of written bytes
2236 * @buf: the data to write
2238 * NAND write with ECC. Used when performing writes in interrupt context, this
2239 * may for example be called by mtdoops when writing an oops while in panic.
2241 static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2242 size_t *retlen, const uint8_t *buf)
2244 struct nand_chip *chip = mtd->priv;
2247 /* Do not allow reads past end of device */
2248 if ((to + len) > mtd->size)
2253 /* Wait for the device to get ready. */
2254 panic_nand_wait(mtd, chip, 400);
2256 /* Grab the device. */
2257 panic_nand_get_device(chip, mtd, FL_WRITING);
2259 chip->ops.len = len;
2260 chip->ops.datbuf = (uint8_t *)buf;
2261 chip->ops.oobbuf = NULL;
2263 ret = nand_do_write_ops(mtd, to, &chip->ops);
2265 *retlen = chip->ops.retlen;
2270 * nand_write - [MTD Interface] NAND write with ECC
2271 * @mtd: MTD device structure
2272 * @to: offset to write to
2273 * @len: number of bytes to write
2274 * @retlen: pointer to variable to store the number of written bytes
2275 * @buf: the data to write
2277 * NAND write with ECC
2279 static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2280 size_t *retlen, const uint8_t *buf)
2282 struct nand_chip *chip = mtd->priv;
2285 /* Do not allow reads past end of device */
2286 if ((to + len) > mtd->size)
2291 nand_get_device(chip, mtd, FL_WRITING);
2293 chip->ops.len = len;
2294 chip->ops.datbuf = (uint8_t *)buf;
2295 chip->ops.oobbuf = NULL;
2297 ret = nand_do_write_ops(mtd, to, &chip->ops);
2299 *retlen = chip->ops.retlen;
2301 nand_release_device(mtd);
2307 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
2308 * @mtd: MTD device structure
2309 * @to: offset to write to
2310 * @ops: oob operation description structure
2312 * NAND write out-of-band
2314 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2315 struct mtd_oob_ops *ops)
2317 int chipnr, page, status, len;
2318 struct nand_chip *chip = mtd->priv;
2320 DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2321 __func__, (unsigned int)to, (int)ops->ooblen);
2323 if (ops->mode == MTD_OOB_AUTO)
2324 len = chip->ecc.layout->oobavail;
2328 /* Do not allow write past end of page */
2329 if ((ops->ooboffs + ops->ooblen) > len) {
2330 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
2331 "past end of page\n", __func__);
2335 if (unlikely(ops->ooboffs >= len)) {
2336 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
2337 "write outside oob\n", __func__);
2341 /* Do not allow reads past end of device */
2342 if (unlikely(to >= mtd->size ||
2343 ops->ooboffs + ops->ooblen >
2344 ((mtd->size >> chip->page_shift) -
2345 (to >> chip->page_shift)) * len)) {
2346 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2347 "end of device\n", __func__);
2351 chipnr = (int)(to >> chip->chip_shift);
2352 chip->select_chip(mtd, chipnr);
2354 /* Shift to get page */
2355 page = (int)(to >> chip->page_shift);
2358 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2359 * of my DiskOnChip 2000 test units) will clear the whole data page too
2360 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2361 * it in the doc2000 driver in August 1999. dwmw2.
2363 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2365 /* Check, if it is write protected */
2366 if (nand_check_wp(mtd))
2369 /* Invalidate the page cache, if we write to the cached page */
2370 if (page == chip->pagebuf)
2373 memset(chip->oob_poi, 0xff, mtd->oobsize);
2374 nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
2375 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2376 memset(chip->oob_poi, 0xff, mtd->oobsize);
2381 ops->oobretlen = ops->ooblen;
2387 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2388 * @mtd: MTD device structure
2389 * @to: offset to write to
2390 * @ops: oob operation description structure
2392 static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2393 struct mtd_oob_ops *ops)
2395 struct nand_chip *chip = mtd->priv;
2396 int ret = -ENOTSUPP;
2400 /* Do not allow writes past end of device */
2401 if (ops->datbuf && (to + ops->len) > mtd->size) {
2402 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2403 "end of device\n", __func__);
2407 nand_get_device(chip, mtd, FL_WRITING);
2420 ret = nand_do_write_oob(mtd, to, ops);
2422 ret = nand_do_write_ops(mtd, to, ops);
2425 nand_release_device(mtd);
2430 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2431 * @mtd: MTD device structure
2432 * @page: the page address of the block which will be erased
2434 * Standard erase command for NAND chips
2436 static void single_erase_cmd(struct mtd_info *mtd, int page)
2438 struct nand_chip *chip = mtd->priv;
2439 /* Send commands to erase a block */
2440 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2441 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2445 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2446 * @mtd: MTD device structure
2447 * @page: the page address of the block which will be erased
2449 * AND multi block erase command function
2450 * Erase 4 consecutive blocks
2452 static void multi_erase_cmd(struct mtd_info *mtd, int page)
2454 struct nand_chip *chip = mtd->priv;
2455 /* Send commands to erase a block */
2456 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2457 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2458 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2459 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2460 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2464 * nand_erase - [MTD Interface] erase block(s)
2465 * @mtd: MTD device structure
2466 * @instr: erase instruction
2468 * Erase one ore more blocks
2470 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
2472 return nand_erase_nand(mtd, instr, 0);
2475 #define BBT_PAGE_MASK 0xffffff3f
2477 * nand_erase_nand - [Internal] erase block(s)
2478 * @mtd: MTD device structure
2479 * @instr: erase instruction
2480 * @allowbbt: allow erasing the bbt area
2482 * Erase one ore more blocks
2484 int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2487 int page, status, pages_per_block, ret, chipnr;
2488 struct nand_chip *chip = mtd->priv;
2489 loff_t rewrite_bbt[NAND_MAX_CHIPS]={0};
2490 unsigned int bbt_masked_page = 0xffffffff;
2493 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
2494 __func__, (unsigned long long)instr->addr,
2495 (unsigned long long)instr->len);
2497 if (check_offs_len(mtd, instr->addr, instr->len))
2500 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2502 /* Grab the lock and see if the device is available */
2503 nand_get_device(chip, mtd, FL_ERASING);
2505 /* Shift to get first page */
2506 page = (int)(instr->addr >> chip->page_shift);
2507 chipnr = (int)(instr->addr >> chip->chip_shift);
2509 /* Calculate pages in each block */
2510 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
2512 /* Select the NAND device */
2513 chip->select_chip(mtd, chipnr);
2515 /* Check, if it is write protected */
2516 if (nand_check_wp(mtd)) {
2517 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
2519 instr->state = MTD_ERASE_FAILED;
2524 * If BBT requires refresh, set the BBT page mask to see if the BBT
2525 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2526 * can not be matched. This is also done when the bbt is actually
2527 * erased to avoid recusrsive updates
2529 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2530 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2532 /* Loop through the pages */
2535 instr->state = MTD_ERASING;
2539 * heck if we have a bad block, we do not erase bad blocks !
2541 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2542 chip->page_shift, 0, allowbbt)) {
2543 printk(KERN_WARNING "%s: attempt to erase a bad block "
2544 "at page 0x%08x\n", __func__, page);
2545 instr->state = MTD_ERASE_FAILED;
2550 * Invalidate the page cache, if we erase the block which
2551 * contains the current cached page
2553 if (page <= chip->pagebuf && chip->pagebuf <
2554 (page + pages_per_block))
2557 chip->erase_cmd(mtd, page & chip->pagemask);
2559 status = chip->waitfunc(mtd, chip);
2562 * See if operation failed and additional status checks are
2565 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2566 status = chip->errstat(mtd, chip, FL_ERASING,
2569 /* See if block erase succeeded */
2570 if (status & NAND_STATUS_FAIL) {
2571 DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
2572 "page 0x%08x\n", __func__, page);
2573 instr->state = MTD_ERASE_FAILED;
2575 ((loff_t)page << chip->page_shift);
2580 * If BBT requires refresh, set the BBT rewrite flag to the
2583 if (bbt_masked_page != 0xffffffff &&
2584 (page & BBT_PAGE_MASK) == bbt_masked_page)
2585 rewrite_bbt[chipnr] =
2586 ((loff_t)page << chip->page_shift);
2588 /* Increment page address and decrement length */
2589 len -= (1 << chip->phys_erase_shift);
2590 page += pages_per_block;
2592 /* Check, if we cross a chip boundary */
2593 if (len && !(page & chip->pagemask)) {
2595 chip->select_chip(mtd, -1);
2596 chip->select_chip(mtd, chipnr);
2599 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2600 * page mask to see if this BBT should be rewritten
2602 if (bbt_masked_page != 0xffffffff &&
2603 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2604 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2608 instr->state = MTD_ERASE_DONE;
2612 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2614 /* Deselect and wake up anyone waiting on the device */
2615 nand_release_device(mtd);
2617 /* Do call back function */
2619 mtd_erase_callback(instr);
2622 * If BBT requires refresh and erase was successful, rewrite any
2623 * selected bad block tables
2625 if (bbt_masked_page == 0xffffffff || ret)
2628 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2629 if (!rewrite_bbt[chipnr])
2631 /* update the BBT for chip */
2632 DEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
2633 "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
2634 rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
2635 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2638 /* Return more or less happy */
2643 * nand_sync - [MTD Interface] sync
2644 * @mtd: MTD device structure
2646 * Sync is actually a wait for chip ready function
2648 static void nand_sync(struct mtd_info *mtd)
2650 struct nand_chip *chip = mtd->priv;
2652 DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
2654 /* Grab the lock and see if the device is available */
2655 nand_get_device(chip, mtd, FL_SYNCING);
2656 /* Release it and go back */
2657 nand_release_device(mtd);
2661 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
2662 * @mtd: MTD device structure
2663 * @offs: offset relative to mtd start
2665 static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
2667 /* Check for invalid offset */
2668 if (offs > mtd->size)
2671 return nand_block_checkbad(mtd, offs, 1, 0);
2675 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
2676 * @mtd: MTD device structure
2677 * @ofs: offset relative to mtd start
2679 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2681 struct nand_chip *chip = mtd->priv;
2684 if ((ret = nand_block_isbad(mtd, ofs))) {
2685 /* If it was bad already, return success and do nothing. */
2691 return chip->block_markbad(mtd, ofs);
2695 * nand_suspend - [MTD Interface] Suspend the NAND flash
2696 * @mtd: MTD device structure
2698 static int nand_suspend(struct mtd_info *mtd)
2700 struct nand_chip *chip = mtd->priv;
2702 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
2706 * nand_resume - [MTD Interface] Resume the NAND flash
2707 * @mtd: MTD device structure
2709 static void nand_resume(struct mtd_info *mtd)
2711 struct nand_chip *chip = mtd->priv;
2713 if (chip->state == FL_PM_SUSPENDED)
2714 nand_release_device(mtd);
2716 printk(KERN_ERR "%s called for a chip which is not "
2717 "in suspended state\n", __func__);
2721 * Set default functions
2723 static void nand_set_defaults(struct nand_chip *chip, int busw)
2725 /* check for proper chip_delay setup, set 20us if not */
2726 if (!chip->chip_delay)
2727 chip->chip_delay = 20;
2729 /* check, if a user supplied command function given */
2730 if (chip->cmdfunc == NULL)
2731 chip->cmdfunc = nand_command;
2733 /* check, if a user supplied wait function given */
2734 if (chip->waitfunc == NULL)
2735 chip->waitfunc = nand_wait;
2737 if (!chip->select_chip)
2738 chip->select_chip = nand_select_chip;
2739 if (!chip->read_byte)
2740 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2741 if (!chip->read_word)
2742 chip->read_word = nand_read_word;
2743 if (!chip->block_bad)
2744 chip->block_bad = nand_block_bad;
2745 if (!chip->block_markbad)
2746 chip->block_markbad = nand_default_block_markbad;
2747 if (!chip->write_buf)
2748 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2749 if (!chip->read_buf)
2750 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2751 if (!chip->verify_buf)
2752 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2753 if (!chip->scan_bbt)
2754 chip->scan_bbt = nand_default_bbt;
2756 if (!chip->controller) {
2757 chip->controller = &chip->hwcontrol;
2758 spin_lock_init(&chip->controller->lock);
2759 init_waitqueue_head(&chip->controller->wq);
2765 * Get the flash and manufacturer id and lookup if the type is supported
2767 static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2768 struct nand_chip *chip,
2769 int busw, int *maf_id,
2770 struct nand_flash_dev *type)
2772 int dev_id, maf_idx;
2773 int tmp_id, tmp_manf;
2775 /* Select the device */
2776 chip->select_chip(mtd, 0);
2779 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2782 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2784 /* Send the command for reading device ID */
2785 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2787 /* Read manufacturer and device IDs */
2788 *maf_id = chip->read_byte(mtd);
2789 dev_id = chip->read_byte(mtd);
2791 /* Try again to make sure, as some systems the bus-hold or other
2792 * interface concerns can cause random data which looks like a
2793 * possibly credible NAND flash to appear. If the two results do
2794 * not match, ignore the device completely.
2797 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2799 /* Read manufacturer and device IDs */
2801 tmp_manf = chip->read_byte(mtd);
2802 tmp_id = chip->read_byte(mtd);
2804 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2805 printk(KERN_INFO "%s: second ID read did not match "
2806 "%02x,%02x against %02x,%02x\n", __func__,
2807 *maf_id, dev_id, tmp_manf, tmp_id);
2808 return ERR_PTR(-ENODEV);
2812 type = nand_flash_ids;
2814 for (; type->name != NULL; type++)
2815 if (dev_id == type->id)
2819 return ERR_PTR(-ENODEV);
2822 mtd->name = type->name;
2824 chip->chipsize = (uint64_t)type->chipsize << 20;
2826 /* Newer devices have all the information in additional id bytes */
2827 if (!type->pagesize) {
2829 /* The 3rd id byte holds MLC / multichip data */
2830 chip->cellinfo = chip->read_byte(mtd);
2831 /* The 4th id byte is the important one */
2832 extid = chip->read_byte(mtd);
2834 mtd->writesize = 1024 << (extid & 0x3);
2837 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
2839 /* Calc blocksize. Blocksize is multiples of 64KiB */
2840 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2842 /* Get buswidth information */
2843 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2847 * Old devices have chip data hardcoded in the device id table
2849 mtd->erasesize = type->erasesize;
2850 mtd->writesize = type->pagesize;
2851 mtd->oobsize = mtd->writesize / 32;
2852 busw = type->options & NAND_BUSWIDTH_16;
2855 /* Try to identify manufacturer */
2856 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2857 if (nand_manuf_ids[maf_idx].id == *maf_id)
2862 * Check, if buswidth is correct. Hardware drivers should set
2865 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2866 printk(KERN_INFO "NAND device: Manufacturer ID:"
2867 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2868 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2869 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
2870 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2872 return ERR_PTR(-EINVAL);
2875 /* Calculate the address shift from the page size */
2876 chip->page_shift = ffs(mtd->writesize) - 1;
2877 /* Convert chipsize to number of pages per chip -1. */
2878 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
2880 chip->bbt_erase_shift = chip->phys_erase_shift =
2881 ffs(mtd->erasesize) - 1;
2882 if (chip->chipsize & 0xffffffff)
2883 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
2885 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1;
2887 /* Set the bad block position */
2888 chip->badblockpos = mtd->writesize > 512 ?
2889 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2890 chip->badblockbits = 8;
2892 /* Get chip options, preserve non chip based options */
2893 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2894 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
2897 * Set chip as a default. Board drivers can override it, if necessary
2899 chip->options |= NAND_NO_AUTOINCR;
2901 /* Check if chip is a not a samsung device. Do not clear the
2902 * options for chips which are not having an extended id.
2904 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2905 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2907 /* Check for AND chips with 4 page planes */
2908 if (chip->options & NAND_4PAGE_ARRAY)
2909 chip->erase_cmd = multi_erase_cmd;
2911 chip->erase_cmd = single_erase_cmd;
2913 /* Do not replace user supplied command function ! */
2914 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2915 chip->cmdfunc = nand_command_lp;
2917 printk(KERN_INFO "NAND device: Manufacturer ID:"
2918 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2919 nand_manuf_ids[maf_idx].name, type->name);
2925 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2926 * @mtd: MTD device structure
2927 * @maxchips: Number of chips to scan for
2928 * @table: Alternative NAND ID table
2930 * This is the first phase of the normal nand_scan() function. It
2931 * reads the flash ID and sets up MTD fields accordingly.
2933 * The mtd->owner field must be set to the module of the caller.
2935 int nand_scan_ident(struct mtd_info *mtd, int maxchips,
2936 struct nand_flash_dev *table)
2938 int i, busw, nand_maf_id;
2939 struct nand_chip *chip = mtd->priv;
2940 struct nand_flash_dev *type;
2942 /* Get buswidth to select the correct functions */
2943 busw = chip->options & NAND_BUSWIDTH_16;
2944 /* Set the default functions */
2945 nand_set_defaults(chip, busw);
2947 /* Read the flash type */
2948 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id, table);
2951 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
2952 printk(KERN_WARNING "No NAND device found.\n");
2953 chip->select_chip(mtd, -1);
2954 return PTR_ERR(type);
2957 /* Check for a chip array */
2958 for (i = 1; i < maxchips; i++) {
2959 chip->select_chip(mtd, i);
2960 /* See comment in nand_get_flash_type for reset */
2961 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2962 /* Send the command for reading device ID */
2963 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2964 /* Read manufacturer and device IDs */
2965 if (nand_maf_id != chip->read_byte(mtd) ||
2966 type->id != chip->read_byte(mtd))
2970 printk(KERN_INFO "%d NAND chips detected\n", i);
2972 /* Store the number of chips and calc total size for mtd */
2974 mtd->size = i * chip->chipsize;
2981 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2982 * @mtd: MTD device structure
2984 * This is the second phase of the normal nand_scan() function. It
2985 * fills out all the uninitialized function pointers with the defaults
2986 * and scans for a bad block table if appropriate.
2988 int nand_scan_tail(struct mtd_info *mtd)
2991 struct nand_chip *chip = mtd->priv;
2993 if (!(chip->options & NAND_OWN_BUFFERS))
2994 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2998 /* Set the internal oob buffer location, just after the page data */
2999 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
3002 * If no default placement scheme is given, select an appropriate one
3004 if (!chip->ecc.layout) {
3005 switch (mtd->oobsize) {
3007 chip->ecc.layout = &nand_oob_8;
3010 chip->ecc.layout = &nand_oob_16;
3013 chip->ecc.layout = &nand_oob_64;
3016 chip->ecc.layout = &nand_oob_128;
3019 printk(KERN_WARNING "No oob scheme defined for "
3020 "oobsize %d\n", mtd->oobsize);
3025 if (!chip->write_page)
3026 chip->write_page = nand_write_page;
3029 * check ECC mode, default to software if 3byte/512byte hardware ECC is
3030 * selected and we have 256 byte pagesize fallback to software ECC
3033 switch (chip->ecc.mode) {
3034 case NAND_ECC_HW_OOB_FIRST:
3035 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3036 if (!chip->ecc.calculate || !chip->ecc.correct ||
3038 printk(KERN_WARNING "No ECC functions supplied; "
3039 "Hardware ECC not possible\n");
3042 if (!chip->ecc.read_page)
3043 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3046 /* Use standard hwecc read page function ? */
3047 if (!chip->ecc.read_page)
3048 chip->ecc.read_page = nand_read_page_hwecc;
3049 if (!chip->ecc.write_page)
3050 chip->ecc.write_page = nand_write_page_hwecc;
3051 if (!chip->ecc.read_page_raw)
3052 chip->ecc.read_page_raw = nand_read_page_raw;
3053 if (!chip->ecc.write_page_raw)
3054 chip->ecc.write_page_raw = nand_write_page_raw;
3055 if (!chip->ecc.read_oob)
3056 chip->ecc.read_oob = nand_read_oob_std;
3057 if (!chip->ecc.write_oob)
3058 chip->ecc.write_oob = nand_write_oob_std;
3060 case NAND_ECC_HW_SYNDROME:
3061 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3062 !chip->ecc.hwctl) &&
3063 (!chip->ecc.read_page ||
3064 chip->ecc.read_page == nand_read_page_hwecc ||
3065 !chip->ecc.write_page ||
3066 chip->ecc.write_page == nand_write_page_hwecc)) {
3067 printk(KERN_WARNING "No ECC functions supplied; "
3068 "Hardware ECC not possible\n");
3071 /* Use standard syndrome read/write page function ? */
3072 if (!chip->ecc.read_page)
3073 chip->ecc.read_page = nand_read_page_syndrome;
3074 if (!chip->ecc.write_page)
3075 chip->ecc.write_page = nand_write_page_syndrome;
3076 if (!chip->ecc.read_page_raw)
3077 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3078 if (!chip->ecc.write_page_raw)
3079 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
3080 if (!chip->ecc.read_oob)
3081 chip->ecc.read_oob = nand_read_oob_syndrome;
3082 if (!chip->ecc.write_oob)
3083 chip->ecc.write_oob = nand_write_oob_syndrome;
3085 if (mtd->writesize >= chip->ecc.size)
3087 printk(KERN_WARNING "%d byte HW ECC not possible on "
3088 "%d byte page size, fallback to SW ECC\n",
3089 chip->ecc.size, mtd->writesize);
3090 chip->ecc.mode = NAND_ECC_SOFT;
3093 chip->ecc.calculate = nand_calculate_ecc;
3094 chip->ecc.correct = nand_correct_data;
3095 chip->ecc.read_page = nand_read_page_swecc;
3096 chip->ecc.read_subpage = nand_read_subpage;
3097 chip->ecc.write_page = nand_write_page_swecc;
3098 chip->ecc.read_page_raw = nand_read_page_raw;
3099 chip->ecc.write_page_raw = nand_write_page_raw;
3100 chip->ecc.read_oob = nand_read_oob_std;
3101 chip->ecc.write_oob = nand_write_oob_std;
3102 if (!chip->ecc.size)
3103 chip->ecc.size = 256;
3104 chip->ecc.bytes = 3;
3108 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
3109 "This is not recommended !!\n");
3110 chip->ecc.read_page = nand_read_page_raw;
3111 chip->ecc.write_page = nand_write_page_raw;
3112 chip->ecc.read_oob = nand_read_oob_std;
3113 chip->ecc.read_page_raw = nand_read_page_raw;
3114 chip->ecc.write_page_raw = nand_write_page_raw;
3115 chip->ecc.write_oob = nand_write_oob_std;
3116 chip->ecc.size = mtd->writesize;
3117 chip->ecc.bytes = 0;
3121 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
3127 * The number of bytes available for a client to place data into
3128 * the out of band area
3130 chip->ecc.layout->oobavail = 0;
3131 for (i = 0; chip->ecc.layout->oobfree[i].length
3132 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
3133 chip->ecc.layout->oobavail +=
3134 chip->ecc.layout->oobfree[i].length;
3135 mtd->oobavail = chip->ecc.layout->oobavail;
3138 * Set the number of read / write steps for one page depending on ECC
3141 chip->ecc.steps = mtd->writesize / chip->ecc.size;
3142 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
3143 printk(KERN_WARNING "Invalid ecc parameters\n");
3146 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
3149 * Allow subpage writes up to ecc.steps. Not possible for MLC
3152 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3153 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
3154 switch(chip->ecc.steps) {
3156 mtd->subpage_sft = 1;
3161 mtd->subpage_sft = 2;
3165 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3167 /* Initialize state */
3168 chip->state = FL_READY;
3170 /* De-select the device */
3171 chip->select_chip(mtd, -1);
3173 /* Invalidate the pagebuffer reference */
3176 /* Fill in remaining MTD driver data */
3177 mtd->type = MTD_NANDFLASH;
3178 mtd->flags = MTD_CAP_NANDFLASH;
3179 mtd->erase = nand_erase;
3181 mtd->unpoint = NULL;
3182 mtd->read = nand_read;
3183 mtd->write = nand_write;
3184 mtd->panic_write = panic_nand_write;
3185 mtd->read_oob = nand_read_oob;
3186 mtd->write_oob = nand_write_oob;
3187 mtd->sync = nand_sync;
3190 mtd->suspend = nand_suspend;
3191 mtd->resume = nand_resume;
3192 mtd->block_isbad = nand_block_isbad;
3193 mtd->block_markbad = nand_block_markbad;
3195 /* propagate ecc.layout to mtd_info */
3196 mtd->ecclayout = chip->ecc.layout;
3198 /* Check, if we should skip the bad block table scan */
3199 if (chip->options & NAND_SKIP_BBTSCAN)
3202 /* Build bad block table */
3203 return chip->scan_bbt(mtd);
3206 /* is_module_text_address() isn't exported, and it's mostly a pointless
3207 test if this is a module _anyway_ -- they'd have to try _really_ hard
3208 to call us from in-kernel code if the core NAND support is modular. */
3210 #define caller_is_module() (1)
3212 #define caller_is_module() \
3213 is_module_text_address((unsigned long)__builtin_return_address(0))
3217 * nand_scan - [NAND Interface] Scan for the NAND device
3218 * @mtd: MTD device structure
3219 * @maxchips: Number of chips to scan for
3221 * This fills out all the uninitialized function pointers
3222 * with the defaults.
3223 * The flash ID is read and the mtd/chip structures are
3224 * filled with the appropriate values.
3225 * The mtd->owner field must be set to the module of the caller
3228 int nand_scan(struct mtd_info *mtd, int maxchips)
3232 /* Many callers got this wrong, so check for it for a while... */
3233 if (!mtd->owner && caller_is_module()) {
3234 printk(KERN_CRIT "%s called with NULL mtd->owner!\n",
3239 ret = nand_scan_ident(mtd, maxchips, NULL);
3241 ret = nand_scan_tail(mtd);
3246 * nand_release - [NAND Interface] Free resources held by the NAND device
3247 * @mtd: MTD device structure
3249 void nand_release(struct mtd_info *mtd)
3251 struct nand_chip *chip = mtd->priv;
3253 #ifdef CONFIG_MTD_PARTITIONS
3254 /* Deregister partitions */
3255 del_mtd_partitions(mtd);
3257 /* Deregister the device */
3258 del_mtd_device(mtd);
3260 /* Free bad block table memory */
3262 if (!(chip->options & NAND_OWN_BUFFERS))
3263 kfree(chip->buffers);
3266 EXPORT_SYMBOL_GPL(nand_lock);
3267 EXPORT_SYMBOL_GPL(nand_unlock);
3268 EXPORT_SYMBOL_GPL(nand_scan);
3269 EXPORT_SYMBOL_GPL(nand_scan_ident);
3270 EXPORT_SYMBOL_GPL(nand_scan_tail);
3271 EXPORT_SYMBOL_GPL(nand_release);
3273 static int __init nand_base_init(void)
3275 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3279 static void __exit nand_base_exit(void)
3281 led_trigger_unregister_simple(nand_led_trigger);
3284 module_init(nand_base_init);
3285 module_exit(nand_base_exit);
3287 MODULE_LICENSE("GPL");
3288 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
3289 MODULE_DESCRIPTION("Generic NAND flash driver code");