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/tech/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.
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License version 2 as
30 * published by the Free Software Foundation.
36 #include <linux/module.h>
37 #include <linux/delay.h>
38 #include <linux/errno.h>
39 #include <linux/err.h>
40 #include <linux/sched.h>
41 #include <linux/slab.h>
42 #include <linux/types.h>
43 #include <linux/mtd/mtd.h>
44 #include <linux/mtd/nand.h>
45 #include <linux/mtd/nand_ecc.h>
46 #include <linux/mtd/compatmac.h>
47 #include <linux/interrupt.h>
48 #include <linux/bitops.h>
49 #include <linux/leds.h>
52 #ifdef CONFIG_MTD_PARTITIONS
53 #include <linux/mtd/partitions.h>
60 #define ENOTSUPP 524 /* Operation is not supported */
64 #include <linux/err.h>
65 #include <linux/mtd/compat.h>
66 #include <linux/mtd/mtd.h>
67 #include <linux/mtd/nand.h>
68 #include <linux/mtd/nand_ecc.h>
71 #include <asm/errno.h>
73 #ifdef CONFIG_JFFS2_NAND
74 #include <jffs2/jffs2.h>
77 /* Define default oob placement schemes for large and small page devices */
78 static struct nand_ecclayout nand_oob_8 = {
88 static struct nand_ecclayout nand_oob_16 = {
90 .eccpos = {0, 1, 2, 3, 6, 7},
96 static struct nand_ecclayout nand_oob_64 = {
99 40, 41, 42, 43, 44, 45, 46, 47,
100 48, 49, 50, 51, 52, 53, 54, 55,
101 56, 57, 58, 59, 60, 61, 62, 63},
107 static struct nand_ecclayout nand_oob_128 = {
110 80, 81, 82, 83, 84, 85, 86, 87,
111 88, 89, 90, 91, 92, 93, 94, 95,
112 96, 97, 98, 99, 100, 101, 102, 103,
113 104, 105, 106, 107, 108, 109, 110, 111,
114 112, 113, 114, 115, 116, 117, 118, 119,
115 120, 121, 122, 123, 124, 125, 126, 127},
122 static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
125 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
126 struct mtd_oob_ops *ops);
128 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this);
131 * For devices which display every fart in the system on a seperate LED. Is
132 * compiled away when LED support is disabled.
136 DEFINE_LED_TRIGGER(nand_led_trigger);
140 * nand_release_device - [GENERIC] release chip
141 * @mtd: MTD device structure
143 * Deselect, release chip lock and wake up anyone waiting on the device
147 static void nand_release_device(struct mtd_info *mtd)
149 struct nand_chip *chip = mtd->priv;
151 /* De-select the NAND device */
152 chip->select_chip(mtd, -1);
154 /* Release the controller and the chip */
155 spin_lock(&chip->controller->lock);
156 chip->controller->active = NULL;
157 chip->state = FL_READY;
158 wake_up(&chip->controller->wq);
159 spin_unlock(&chip->controller->lock);
162 static void nand_release_device (struct mtd_info *mtd)
164 struct nand_chip *this = mtd->priv;
165 this->select_chip(mtd, -1); /* De-select the NAND device */
170 * nand_read_byte - [DEFAULT] read one byte from the chip
171 * @mtd: MTD device structure
173 * Default read function for 8bit buswith
175 static uint8_t nand_read_byte(struct mtd_info *mtd)
177 struct nand_chip *chip = mtd->priv;
178 return readb(chip->IO_ADDR_R);
182 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
183 * @mtd: MTD device structure
185 * Default read function for 16bit buswith with
186 * endianess conversion
188 static uint8_t nand_read_byte16(struct mtd_info *mtd)
190 struct nand_chip *chip = mtd->priv;
191 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
195 * nand_read_word - [DEFAULT] read one word from the chip
196 * @mtd: MTD device structure
198 * Default read function for 16bit buswith without
199 * endianess conversion
201 static u16 nand_read_word(struct mtd_info *mtd)
203 struct nand_chip *chip = mtd->priv;
204 return readw(chip->IO_ADDR_R);
208 * nand_select_chip - [DEFAULT] control CE line
209 * @mtd: MTD device structure
210 * @chipnr: chipnumber to select, -1 for deselect
212 * Default select function for 1 chip devices.
214 static void nand_select_chip(struct mtd_info *mtd, int chipnr)
216 struct nand_chip *chip = mtd->priv;
220 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
231 * nand_write_buf - [DEFAULT] write buffer to chip
232 * @mtd: MTD device structure
234 * @len: number of bytes to write
236 * Default write function for 8bit buswith
238 static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
241 struct nand_chip *chip = mtd->priv;
243 for (i = 0; i < len; i++)
244 writeb(buf[i], chip->IO_ADDR_W);
248 * nand_read_buf - [DEFAULT] read chip data into buffer
249 * @mtd: MTD device structure
250 * @buf: buffer to store date
251 * @len: number of bytes to read
253 * Default read function for 8bit buswith
255 static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
258 struct nand_chip *chip = mtd->priv;
260 for (i = 0; i < len; i++)
261 buf[i] = readb(chip->IO_ADDR_R);
265 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
266 * @mtd: MTD device structure
267 * @buf: buffer containing the data to compare
268 * @len: number of bytes to compare
270 * Default verify function for 8bit buswith
272 static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
275 struct nand_chip *chip = mtd->priv;
277 for (i = 0; i < len; i++)
278 if (buf[i] != readb(chip->IO_ADDR_R))
284 * nand_write_buf16 - [DEFAULT] write buffer to chip
285 * @mtd: MTD device structure
287 * @len: number of bytes to write
289 * Default write function for 16bit buswith
291 static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
294 struct nand_chip *chip = mtd->priv;
295 u16 *p = (u16 *) buf;
298 for (i = 0; i < len; i++)
299 writew(p[i], chip->IO_ADDR_W);
304 * nand_read_buf16 - [DEFAULT] read chip data into buffer
305 * @mtd: MTD device structure
306 * @buf: buffer to store date
307 * @len: number of bytes to read
309 * Default read function for 16bit buswith
311 static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
314 struct nand_chip *chip = mtd->priv;
315 u16 *p = (u16 *) buf;
318 for (i = 0; i < len; i++)
319 p[i] = readw(chip->IO_ADDR_R);
323 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
324 * @mtd: MTD device structure
325 * @buf: buffer containing the data to compare
326 * @len: number of bytes to compare
328 * Default verify function for 16bit buswith
330 static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
333 struct nand_chip *chip = mtd->priv;
334 u16 *p = (u16 *) buf;
337 for (i = 0; i < len; i++)
338 if (p[i] != readw(chip->IO_ADDR_R))
345 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
346 * @mtd: MTD device structure
347 * @ofs: offset from device start
348 * @getchip: 0, if the chip is already selected
350 * Check, if the block is bad.
352 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
354 int page, chipnr, res = 0;
355 struct nand_chip *chip = mtd->priv;
358 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
361 chipnr = (int)(ofs >> chip->chip_shift);
363 nand_get_device(chip, mtd, FL_READING);
365 /* Select the NAND device */
366 chip->select_chip(mtd, chipnr);
369 if (chip->options & NAND_BUSWIDTH_16) {
370 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
372 bad = cpu_to_le16(chip->read_word(mtd));
373 if (chip->badblockpos & 0x1)
375 if ((bad & 0xFF) != 0xff)
378 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
379 if (chip->read_byte(mtd) != 0xff)
384 nand_release_device(mtd);
390 * nand_default_block_markbad - [DEFAULT] mark a block bad
391 * @mtd: MTD device structure
392 * @ofs: offset from device start
394 * This is the default implementation, which can be overridden by
395 * a hardware specific driver.
397 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
399 struct nand_chip *chip = mtd->priv;
400 uint8_t buf[2] = { 0, 0 };
403 /* Get block number */
404 block = (int)(ofs >> chip->bbt_erase_shift);
406 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
408 /* Do we have a flash based bad block table ? */
409 if (chip->options & NAND_USE_FLASH_BBT)
410 ret = nand_update_bbt(mtd, ofs);
412 /* We write two bytes, so we dont have to mess with 16 bit
416 chip->ops.len = chip->ops.ooblen = 2;
417 chip->ops.datbuf = NULL;
418 chip->ops.oobbuf = buf;
419 chip->ops.ooboffs = chip->badblockpos & ~0x01;
421 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
424 mtd->ecc_stats.badblocks++;
429 * nand_check_wp - [GENERIC] check if the chip is write protected
430 * @mtd: MTD device structure
431 * Check, if the device is write protected
433 * The function expects, that the device is already selected
435 static int nand_check_wp(struct mtd_info *mtd)
437 struct nand_chip *chip = mtd->priv;
438 /* Check the WP bit */
439 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
440 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
444 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
445 * @mtd: MTD device structure
446 * @ofs: offset from device start
447 * @getchip: 0, if the chip is already selected
448 * @allowbbt: 1, if its allowed to access the bbt area
450 * Check, if the block is bad. Either by reading the bad block table or
451 * calling of the scan function.
453 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
456 struct nand_chip *chip = mtd->priv;
458 if (!(chip->options & NAND_BBT_SCANNED)) {
460 chip->options |= NAND_BBT_SCANNED;
464 return chip->block_bad(mtd, ofs, getchip);
466 /* Return info from the table */
467 return nand_isbad_bbt(mtd, ofs, allowbbt);
471 * Wait for the ready pin, after a command
472 * The timeout is catched later.
476 void nand_wait_ready(struct mtd_info *mtd)
478 struct nand_chip *chip = mtd->priv;
479 unsigned long timeo = jiffies + 2;
481 led_trigger_event(nand_led_trigger, LED_FULL);
482 /* wait until command is processed or timeout occures */
484 if (chip->dev_ready(mtd))
486 touch_softlockup_watchdog();
487 } while (time_before(jiffies, timeo));
488 led_trigger_event(nand_led_trigger, LED_OFF);
490 EXPORT_SYMBOL_GPL(nand_wait_ready);
492 void nand_wait_ready(struct mtd_info *mtd)
494 struct nand_chip *chip = mtd->priv;
495 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
499 /* wait until command is processed or timeout occures */
500 while (get_timer(0) < timeo) {
502 if (chip->dev_ready(mtd))
509 * nand_command - [DEFAULT] Send command to NAND device
510 * @mtd: MTD device structure
511 * @command: the command to be sent
512 * @column: the column address for this command, -1 if none
513 * @page_addr: the page address for this command, -1 if none
515 * Send command to NAND device. This function is used for small page
516 * devices (256/512 Bytes per page)
518 static void nand_command(struct mtd_info *mtd, unsigned int command,
519 int column, int page_addr)
521 register struct nand_chip *chip = mtd->priv;
522 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
525 * Write out the command to the device.
527 if (command == NAND_CMD_SEQIN) {
530 if (column >= mtd->writesize) {
532 column -= mtd->writesize;
533 readcmd = NAND_CMD_READOOB;
534 } else if (column < 256) {
535 /* First 256 bytes --> READ0 */
536 readcmd = NAND_CMD_READ0;
539 readcmd = NAND_CMD_READ1;
541 chip->cmd_ctrl(mtd, readcmd, ctrl);
542 ctrl &= ~NAND_CTRL_CHANGE;
544 chip->cmd_ctrl(mtd, command, ctrl);
547 * Address cycle, when necessary
549 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
550 /* Serially input address */
552 /* Adjust columns for 16 bit buswidth */
553 if (chip->options & NAND_BUSWIDTH_16)
555 chip->cmd_ctrl(mtd, column, ctrl);
556 ctrl &= ~NAND_CTRL_CHANGE;
558 if (page_addr != -1) {
559 chip->cmd_ctrl(mtd, page_addr, ctrl);
560 ctrl &= ~NAND_CTRL_CHANGE;
561 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
562 /* One more address cycle for devices > 32MiB */
563 if (chip->chipsize > (32 << 20))
564 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
566 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
569 * program and erase have their own busy handlers
570 * status and sequential in needs no delay
574 case NAND_CMD_PAGEPROG:
575 case NAND_CMD_ERASE1:
576 case NAND_CMD_ERASE2:
578 case NAND_CMD_STATUS:
584 udelay(chip->chip_delay);
585 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
586 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
588 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
589 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
592 /* This applies to read commands */
595 * If we don't have access to the busy pin, we apply the given
598 if (!chip->dev_ready) {
599 udelay(chip->chip_delay);
603 /* Apply this short delay always to ensure that we do wait tWB in
604 * any case on any machine. */
607 nand_wait_ready(mtd);
611 * nand_command_lp - [DEFAULT] Send command to NAND large page device
612 * @mtd: MTD device structure
613 * @command: the command to be sent
614 * @column: the column address for this command, -1 if none
615 * @page_addr: the page address for this command, -1 if none
617 * Send command to NAND device. This is the version for the new large page
618 * devices We dont have the separate regions as we have in the small page
619 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
621 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
622 int column, int page_addr)
624 register struct nand_chip *chip = mtd->priv;
626 /* Emulate NAND_CMD_READOOB */
627 if (command == NAND_CMD_READOOB) {
628 column += mtd->writesize;
629 command = NAND_CMD_READ0;
632 /* Command latch cycle */
633 chip->cmd_ctrl(mtd, command & 0xff,
634 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
636 if (column != -1 || page_addr != -1) {
637 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
639 /* Serially input address */
641 /* Adjust columns for 16 bit buswidth */
642 if (chip->options & NAND_BUSWIDTH_16)
644 chip->cmd_ctrl(mtd, column, ctrl);
645 ctrl &= ~NAND_CTRL_CHANGE;
646 chip->cmd_ctrl(mtd, column >> 8, ctrl);
648 if (page_addr != -1) {
649 chip->cmd_ctrl(mtd, page_addr, ctrl);
650 chip->cmd_ctrl(mtd, page_addr >> 8,
651 NAND_NCE | NAND_ALE);
652 /* One more address cycle for devices > 128MiB */
653 if (chip->chipsize > (128 << 20))
654 chip->cmd_ctrl(mtd, page_addr >> 16,
655 NAND_NCE | NAND_ALE);
658 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
661 * program and erase have their own busy handlers
662 * status, sequential in, and deplete1 need no delay
666 case NAND_CMD_CACHEDPROG:
667 case NAND_CMD_PAGEPROG:
668 case NAND_CMD_ERASE1:
669 case NAND_CMD_ERASE2:
672 case NAND_CMD_STATUS:
673 case NAND_CMD_DEPLETE1:
677 * read error status commands require only a short delay
679 case NAND_CMD_STATUS_ERROR:
680 case NAND_CMD_STATUS_ERROR0:
681 case NAND_CMD_STATUS_ERROR1:
682 case NAND_CMD_STATUS_ERROR2:
683 case NAND_CMD_STATUS_ERROR3:
684 udelay(chip->chip_delay);
690 udelay(chip->chip_delay);
691 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
692 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
693 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
694 NAND_NCE | NAND_CTRL_CHANGE);
695 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
698 case NAND_CMD_RNDOUT:
699 /* No ready / busy check necessary */
700 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
701 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
702 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
703 NAND_NCE | NAND_CTRL_CHANGE);
707 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
708 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
709 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
710 NAND_NCE | NAND_CTRL_CHANGE);
712 /* This applies to read commands */
715 * If we don't have access to the busy pin, we apply the given
718 if (!chip->dev_ready) {
719 udelay(chip->chip_delay);
724 /* Apply this short delay always to ensure that we do wait tWB in
725 * any case on any machine. */
728 nand_wait_ready(mtd);
732 * nand_get_device - [GENERIC] Get chip for selected access
733 * @chip: the nand chip descriptor
734 * @mtd: MTD device structure
735 * @new_state: the state which is requested
737 * Get the device and lock it for exclusive access
742 nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
744 spinlock_t *lock = &chip->controller->lock;
745 wait_queue_head_t *wq = &chip->controller->wq;
746 DECLARE_WAITQUEUE(wait, current);
750 /* Hardware controller shared among independend devices */
751 /* Hardware controller shared among independend devices */
752 if (!chip->controller->active)
753 chip->controller->active = chip;
755 if (chip->controller->active == chip && chip->state == FL_READY) {
756 chip->state = new_state;
760 if (new_state == FL_PM_SUSPENDED) {
762 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
764 set_current_state(TASK_UNINTERRUPTIBLE);
765 add_wait_queue(wq, &wait);
768 remove_wait_queue(wq, &wait);
772 static int nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
774 this->state = new_state;
780 * nand_wait - [DEFAULT] wait until the command is done
781 * @mtd: MTD device structure
782 * @chip: NAND chip structure
784 * Wait for command done. This applies to erase and program only
785 * Erase can take up to 400ms and program up to 20ms according to
786 * general NAND and SmartMedia specs
790 static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
793 unsigned long timeo = jiffies;
794 int status, state = chip->state;
796 if (state == FL_ERASING)
797 timeo += (HZ * 400) / 1000;
799 timeo += (HZ * 20) / 1000;
801 led_trigger_event(nand_led_trigger, LED_FULL);
803 /* Apply this short delay always to ensure that we do wait tWB in
804 * any case on any machine. */
807 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
808 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
810 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
812 while (time_before(jiffies, timeo)) {
813 if (chip->dev_ready) {
814 if (chip->dev_ready(mtd))
817 if (chip->read_byte(mtd) & NAND_STATUS_READY)
822 led_trigger_event(nand_led_trigger, LED_OFF);
824 status = (int)chip->read_byte(mtd);
828 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this)
831 int state = this->state;
833 if (state == FL_ERASING)
834 timeo = (CONFIG_SYS_HZ * 400) / 1000;
836 timeo = (CONFIG_SYS_HZ * 20) / 1000;
838 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
839 this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
841 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
846 if (get_timer(0) > timeo) {
851 if (this->dev_ready) {
852 if (this->dev_ready(mtd))
855 if (this->read_byte(mtd) & NAND_STATUS_READY)
859 #ifdef PPCHAMELON_NAND_TIMER_HACK
861 while (get_timer(0) < 10);
862 #endif /* PPCHAMELON_NAND_TIMER_HACK */
864 return this->read_byte(mtd);
869 * nand_read_page_raw - [Intern] read raw page data without ecc
870 * @mtd: mtd info structure
871 * @chip: nand chip info structure
872 * @buf: buffer to store read data
874 static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
877 chip->read_buf(mtd, buf, mtd->writesize);
878 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
883 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
884 * @mtd: mtd info structure
885 * @chip: nand chip info structure
886 * @buf: buffer to store read data
888 static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
891 int i, eccsize = chip->ecc.size;
892 int eccbytes = chip->ecc.bytes;
893 int eccsteps = chip->ecc.steps;
895 uint8_t *ecc_calc = chip->buffers->ecccalc;
896 uint8_t *ecc_code = chip->buffers->ecccode;
897 uint32_t *eccpos = chip->ecc.layout->eccpos;
899 chip->ecc.read_page_raw(mtd, chip, buf);
901 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
902 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
904 for (i = 0; i < chip->ecc.total; i++)
905 ecc_code[i] = chip->oob_poi[eccpos[i]];
907 eccsteps = chip->ecc.steps;
910 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
913 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
915 mtd->ecc_stats.failed++;
917 mtd->ecc_stats.corrected += stat;
923 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
924 * @mtd: mtd info structure
925 * @chip: nand chip info structure
926 * @buf: buffer to store read data
928 * Not for syndrome calculating ecc controllers which need a special oob layout
930 static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
933 int i, eccsize = chip->ecc.size;
934 int eccbytes = chip->ecc.bytes;
935 int eccsteps = chip->ecc.steps;
937 uint8_t *ecc_calc = chip->buffers->ecccalc;
938 uint8_t *ecc_code = chip->buffers->ecccode;
939 uint32_t *eccpos = chip->ecc.layout->eccpos;
941 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
942 chip->ecc.hwctl(mtd, NAND_ECC_READ);
943 chip->read_buf(mtd, p, eccsize);
944 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
946 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
948 for (i = 0; i < chip->ecc.total; i++)
949 ecc_code[i] = chip->oob_poi[eccpos[i]];
951 eccsteps = chip->ecc.steps;
954 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
957 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
959 mtd->ecc_stats.failed++;
961 mtd->ecc_stats.corrected += stat;
967 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
968 * @mtd: mtd info structure
969 * @chip: nand chip info structure
970 * @buf: buffer to store read data
972 * The hw generator calculates the error syndrome automatically. Therefor
973 * we need a special oob layout and handling.
975 static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
978 int i, eccsize = chip->ecc.size;
979 int eccbytes = chip->ecc.bytes;
980 int eccsteps = chip->ecc.steps;
982 uint8_t *oob = chip->oob_poi;
984 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
987 chip->ecc.hwctl(mtd, NAND_ECC_READ);
988 chip->read_buf(mtd, p, eccsize);
990 if (chip->ecc.prepad) {
991 chip->read_buf(mtd, oob, chip->ecc.prepad);
992 oob += chip->ecc.prepad;
995 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
996 chip->read_buf(mtd, oob, eccbytes);
997 stat = chip->ecc.correct(mtd, p, oob, NULL);
1000 mtd->ecc_stats.failed++;
1002 mtd->ecc_stats.corrected += stat;
1006 if (chip->ecc.postpad) {
1007 chip->read_buf(mtd, oob, chip->ecc.postpad);
1008 oob += chip->ecc.postpad;
1012 /* Calculate remaining oob bytes */
1013 i = mtd->oobsize - (oob - chip->oob_poi);
1015 chip->read_buf(mtd, oob, i);
1021 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1022 * @chip: nand chip structure
1023 * @oob: oob destination address
1024 * @ops: oob ops structure
1025 * @len: size of oob to transfer
1027 static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1028 struct mtd_oob_ops *ops, size_t len)
1034 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1037 case MTD_OOB_AUTO: {
1038 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1039 uint32_t boffs = 0, roffs = ops->ooboffs;
1042 for(; free->length && len; free++, len -= bytes) {
1043 /* Read request not from offset 0 ? */
1044 if (unlikely(roffs)) {
1045 if (roffs >= free->length) {
1046 roffs -= free->length;
1049 boffs = free->offset + roffs;
1050 bytes = min_t(size_t, len,
1051 (free->length - roffs));
1054 bytes = min_t(size_t, len, free->length);
1055 boffs = free->offset;
1057 memcpy(oob, chip->oob_poi + boffs, bytes);
1069 * nand_do_read_ops - [Internal] Read data with ECC
1071 * @mtd: MTD device structure
1072 * @from: offset to read from
1073 * @ops: oob ops structure
1075 * Internal function. Called with chip held.
1077 static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1078 struct mtd_oob_ops *ops)
1080 int chipnr, page, realpage, col, bytes, aligned;
1081 struct nand_chip *chip = mtd->priv;
1082 struct mtd_ecc_stats stats;
1083 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1086 uint32_t readlen = ops->len;
1087 uint32_t oobreadlen = ops->ooblen;
1088 uint8_t *bufpoi, *oob, *buf;
1090 stats = mtd->ecc_stats;
1092 chipnr = (int)(from >> chip->chip_shift);
1093 chip->select_chip(mtd, chipnr);
1095 realpage = (int)(from >> chip->page_shift);
1096 page = realpage & chip->pagemask;
1098 col = (int)(from & (mtd->writesize - 1));
1104 bytes = min(mtd->writesize - col, readlen);
1105 aligned = (bytes == mtd->writesize);
1107 /* Is the current page in the buffer ? */
1108 if (realpage != chip->pagebuf || oob) {
1109 bufpoi = aligned ? buf : chip->buffers->databuf;
1111 if (likely(sndcmd)) {
1112 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1116 /* Now read the page into the buffer */
1117 if (unlikely(ops->mode == MTD_OOB_RAW))
1118 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi);
1120 ret = chip->ecc.read_page(mtd, chip, bufpoi);
1124 /* Transfer not aligned data */
1126 chip->pagebuf = realpage;
1127 memcpy(buf, chip->buffers->databuf + col, bytes);
1132 if (unlikely(oob)) {
1133 /* Raw mode does data:oob:data:oob */
1134 if (ops->mode != MTD_OOB_RAW) {
1135 int toread = min(oobreadlen,
1136 chip->ecc.layout->oobavail);
1138 oob = nand_transfer_oob(chip,
1140 oobreadlen -= toread;
1143 buf = nand_transfer_oob(chip,
1144 buf, ops, mtd->oobsize);
1147 if (!(chip->options & NAND_NO_READRDY)) {
1149 * Apply delay or wait for ready/busy pin. Do
1150 * this before the AUTOINCR check, so no
1151 * problems arise if a chip which does auto
1152 * increment is marked as NOAUTOINCR by the
1155 if (!chip->dev_ready)
1156 udelay(chip->chip_delay);
1158 nand_wait_ready(mtd);
1161 memcpy(buf, chip->buffers->databuf + col, bytes);
1170 /* For subsequent reads align to page boundary. */
1172 /* Increment page address */
1175 page = realpage & chip->pagemask;
1176 /* Check, if we cross a chip boundary */
1179 chip->select_chip(mtd, -1);
1180 chip->select_chip(mtd, chipnr);
1183 /* Check, if the chip supports auto page increment
1184 * or if we have hit a block boundary.
1186 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1190 ops->retlen = ops->len - (size_t) readlen;
1192 ops->oobretlen = ops->ooblen - oobreadlen;
1197 if (mtd->ecc_stats.failed - stats.failed)
1200 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1204 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1205 * @mtd: MTD device structure
1206 * @from: offset to read from
1207 * @len: number of bytes to read
1208 * @retlen: pointer to variable to store the number of read bytes
1209 * @buf: the databuffer to put data
1211 * Get hold of the chip and call nand_do_read
1213 static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1214 size_t *retlen, uint8_t *buf)
1216 struct nand_chip *chip = mtd->priv;
1219 /* Do not allow reads past end of device */
1220 if ((from + len) > mtd->size)
1225 nand_get_device(chip, mtd, FL_READING);
1227 chip->ops.len = len;
1228 chip->ops.datbuf = buf;
1229 chip->ops.oobbuf = NULL;
1231 ret = nand_do_read_ops(mtd, from, &chip->ops);
1233 *retlen = chip->ops.retlen;
1235 nand_release_device(mtd);
1241 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1242 * @mtd: mtd info structure
1243 * @chip: nand chip info structure
1244 * @page: page number to read
1245 * @sndcmd: flag whether to issue read command or not
1247 static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1248 int page, int sndcmd)
1251 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1254 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1259 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1261 * @mtd: mtd info structure
1262 * @chip: nand chip info structure
1263 * @page: page number to read
1264 * @sndcmd: flag whether to issue read command or not
1266 static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1267 int page, int sndcmd)
1269 uint8_t *buf = chip->oob_poi;
1270 int length = mtd->oobsize;
1271 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1272 int eccsize = chip->ecc.size;
1273 uint8_t *bufpoi = buf;
1274 int i, toread, sndrnd = 0, pos;
1276 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1277 for (i = 0; i < chip->ecc.steps; i++) {
1279 pos = eccsize + i * (eccsize + chunk);
1280 if (mtd->writesize > 512)
1281 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1283 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1286 toread = min_t(int, length, chunk);
1287 chip->read_buf(mtd, bufpoi, toread);
1292 chip->read_buf(mtd, bufpoi, length);
1298 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1299 * @mtd: mtd info structure
1300 * @chip: nand chip info structure
1301 * @page: page number to write
1303 static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1307 const uint8_t *buf = chip->oob_poi;
1308 int length = mtd->oobsize;
1310 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1311 chip->write_buf(mtd, buf, length);
1312 /* Send command to program the OOB data */
1313 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1315 status = chip->waitfunc(mtd, chip);
1317 return status & NAND_STATUS_FAIL ? -EIO : 0;
1321 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1322 * with syndrome - only for large page flash !
1323 * @mtd: mtd info structure
1324 * @chip: nand chip info structure
1325 * @page: page number to write
1327 static int nand_write_oob_syndrome(struct mtd_info *mtd,
1328 struct nand_chip *chip, int page)
1330 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1331 int eccsize = chip->ecc.size, length = mtd->oobsize;
1332 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1333 const uint8_t *bufpoi = chip->oob_poi;
1336 * data-ecc-data-ecc ... ecc-oob
1338 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1340 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1341 pos = steps * (eccsize + chunk);
1346 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1347 for (i = 0; i < steps; i++) {
1349 if (mtd->writesize <= 512) {
1350 uint32_t fill = 0xFFFFFFFF;
1354 int num = min_t(int, len, 4);
1355 chip->write_buf(mtd, (uint8_t *)&fill,
1360 pos = eccsize + i * (eccsize + chunk);
1361 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1365 len = min_t(int, length, chunk);
1366 chip->write_buf(mtd, bufpoi, len);
1371 chip->write_buf(mtd, bufpoi, length);
1373 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1374 status = chip->waitfunc(mtd, chip);
1376 return status & NAND_STATUS_FAIL ? -EIO : 0;
1380 * nand_do_read_oob - [Intern] NAND read out-of-band
1381 * @mtd: MTD device structure
1382 * @from: offset to read from
1383 * @ops: oob operations description structure
1385 * NAND read out-of-band data from the spare area
1387 static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1388 struct mtd_oob_ops *ops)
1390 int page, realpage, chipnr, sndcmd = 1;
1391 struct nand_chip *chip = mtd->priv;
1392 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1393 int readlen = ops->ooblen;
1395 uint8_t *buf = ops->oobbuf;
1397 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1398 (unsigned long long)from, readlen);
1400 if (ops->mode == MTD_OOB_AUTO)
1401 len = chip->ecc.layout->oobavail;
1405 if (unlikely(ops->ooboffs >= len)) {
1406 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1407 "Attempt to start read outside oob\n");
1411 /* Do not allow reads past end of device */
1412 if (unlikely(from >= mtd->size ||
1413 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1414 (from >> chip->page_shift)) * len)) {
1415 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1416 "Attempt read beyond end of device\n");
1420 chipnr = (int)(from >> chip->chip_shift);
1421 chip->select_chip(mtd, chipnr);
1423 /* Shift to get page */
1424 realpage = (int)(from >> chip->page_shift);
1425 page = realpage & chip->pagemask;
1428 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
1430 len = min(len, readlen);
1431 buf = nand_transfer_oob(chip, buf, ops, len);
1433 if (!(chip->options & NAND_NO_READRDY)) {
1435 * Apply delay or wait for ready/busy pin. Do this
1436 * before the AUTOINCR check, so no problems arise if a
1437 * chip which does auto increment is marked as
1438 * NOAUTOINCR by the board driver.
1440 if (!chip->dev_ready)
1441 udelay(chip->chip_delay);
1443 nand_wait_ready(mtd);
1450 /* Increment page address */
1453 page = realpage & chip->pagemask;
1454 /* Check, if we cross a chip boundary */
1457 chip->select_chip(mtd, -1);
1458 chip->select_chip(mtd, chipnr);
1461 /* Check, if the chip supports auto page increment
1462 * or if we have hit a block boundary.
1464 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1468 ops->oobretlen = ops->ooblen;
1473 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
1474 * @mtd: MTD device structure
1475 * @from: offset to read from
1476 * @ops: oob operation description structure
1478 * NAND read data and/or out-of-band data
1480 static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1481 struct mtd_oob_ops *ops)
1483 struct nand_chip *chip = mtd->priv;
1484 int ret = -ENOTSUPP;
1488 /* Do not allow reads past end of device */
1489 if (ops->datbuf && (from + ops->len) > mtd->size) {
1490 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1491 "Attempt read beyond end of device\n");
1495 nand_get_device(chip, mtd, FL_READING);
1508 ret = nand_do_read_oob(mtd, from, ops);
1510 ret = nand_do_read_ops(mtd, from, ops);
1513 nand_release_device(mtd);
1519 * nand_write_page_raw - [Intern] raw page write function
1520 * @mtd: mtd info structure
1521 * @chip: nand chip info structure
1524 static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1527 chip->write_buf(mtd, buf, mtd->writesize);
1528 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1532 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
1533 * @mtd: mtd info structure
1534 * @chip: nand chip info structure
1537 static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1540 int i, eccsize = chip->ecc.size;
1541 int eccbytes = chip->ecc.bytes;
1542 int eccsteps = chip->ecc.steps;
1543 uint8_t *ecc_calc = chip->buffers->ecccalc;
1544 const uint8_t *p = buf;
1545 uint32_t *eccpos = chip->ecc.layout->eccpos;
1547 /* Software ecc calculation */
1548 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1549 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1551 for (i = 0; i < chip->ecc.total; i++)
1552 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1554 chip->ecc.write_page_raw(mtd, chip, buf);
1558 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
1559 * @mtd: mtd info structure
1560 * @chip: nand chip info structure
1563 static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1566 int i, eccsize = chip->ecc.size;
1567 int eccbytes = chip->ecc.bytes;
1568 int eccsteps = chip->ecc.steps;
1569 uint8_t *ecc_calc = chip->buffers->ecccalc;
1570 const uint8_t *p = buf;
1571 uint32_t *eccpos = chip->ecc.layout->eccpos;
1573 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1574 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1575 chip->write_buf(mtd, p, eccsize);
1576 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1579 for (i = 0; i < chip->ecc.total; i++)
1580 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1582 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1586 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
1587 * @mtd: mtd info structure
1588 * @chip: nand chip info structure
1591 * The hw generator calculates the error syndrome automatically. Therefor
1592 * we need a special oob layout and handling.
1594 static void nand_write_page_syndrome(struct mtd_info *mtd,
1595 struct nand_chip *chip, const uint8_t *buf)
1597 int i, eccsize = chip->ecc.size;
1598 int eccbytes = chip->ecc.bytes;
1599 int eccsteps = chip->ecc.steps;
1600 const uint8_t *p = buf;
1601 uint8_t *oob = chip->oob_poi;
1603 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1605 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1606 chip->write_buf(mtd, p, eccsize);
1608 if (chip->ecc.prepad) {
1609 chip->write_buf(mtd, oob, chip->ecc.prepad);
1610 oob += chip->ecc.prepad;
1613 chip->ecc.calculate(mtd, p, oob);
1614 chip->write_buf(mtd, oob, eccbytes);
1617 if (chip->ecc.postpad) {
1618 chip->write_buf(mtd, oob, chip->ecc.postpad);
1619 oob += chip->ecc.postpad;
1623 /* Calculate remaining oob bytes */
1624 i = mtd->oobsize - (oob - chip->oob_poi);
1626 chip->write_buf(mtd, oob, i);
1630 * nand_write_page - [REPLACEABLE] write one page
1631 * @mtd: MTD device structure
1632 * @chip: NAND chip descriptor
1633 * @buf: the data to write
1634 * @page: page number to write
1635 * @cached: cached programming
1636 * @raw: use _raw version of write_page
1638 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1639 const uint8_t *buf, int page, int cached, int raw)
1643 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1646 chip->ecc.write_page_raw(mtd, chip, buf);
1648 chip->ecc.write_page(mtd, chip, buf);
1651 * Cached progamming disabled for now, Not sure if its worth the
1652 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1656 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1658 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1659 status = chip->waitfunc(mtd, chip);
1661 * See if operation failed and additional status checks are
1664 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1665 status = chip->errstat(mtd, chip, FL_WRITING, status,
1668 if (status & NAND_STATUS_FAIL)
1671 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1672 status = chip->waitfunc(mtd, chip);
1675 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1676 /* Send command to read back the data */
1677 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1679 if (chip->verify_buf(mtd, buf, mtd->writesize))
1686 * nand_fill_oob - [Internal] Transfer client buffer to oob
1687 * @chip: nand chip structure
1688 * @oob: oob data buffer
1689 * @ops: oob ops structure
1691 static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1692 struct mtd_oob_ops *ops)
1694 size_t len = ops->ooblen;
1700 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1703 case MTD_OOB_AUTO: {
1704 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1705 uint32_t boffs = 0, woffs = ops->ooboffs;
1708 for(; free->length && len; free++, len -= bytes) {
1709 /* Write request not from offset 0 ? */
1710 if (unlikely(woffs)) {
1711 if (woffs >= free->length) {
1712 woffs -= free->length;
1715 boffs = free->offset + woffs;
1716 bytes = min_t(size_t, len,
1717 (free->length - woffs));
1720 bytes = min_t(size_t, len, free->length);
1721 boffs = free->offset;
1723 memcpy(chip->oob_poi + boffs, oob, bytes);
1734 #define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
1737 * nand_do_write_ops - [Internal] NAND write with ECC
1738 * @mtd: MTD device structure
1739 * @to: offset to write to
1740 * @ops: oob operations description structure
1742 * NAND write with ECC
1744 static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1745 struct mtd_oob_ops *ops)
1747 int chipnr, realpage, page, blockmask, column;
1748 struct nand_chip *chip = mtd->priv;
1749 uint32_t writelen = ops->len;
1750 uint8_t *oob = ops->oobbuf;
1751 uint8_t *buf = ops->datbuf;
1758 /* reject writes, which are not page aligned */
1759 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
1760 printk(KERN_NOTICE "nand_write: "
1761 "Attempt to write not page aligned data\n");
1765 column = to & (mtd->writesize - 1);
1766 subpage = column || (writelen & (mtd->writesize - 1));
1771 chipnr = (int)(to >> chip->chip_shift);
1772 chip->select_chip(mtd, chipnr);
1774 /* Check, if it is write protected */
1775 if (nand_check_wp(mtd)) {
1776 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1780 realpage = (int)(to >> chip->page_shift);
1781 page = realpage & chip->pagemask;
1782 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1784 /* Invalidate the page cache, when we write to the cached page */
1785 if (to <= (chip->pagebuf << chip->page_shift) &&
1786 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1789 /* If we're not given explicit OOB data, let it be 0xFF */
1791 memset(chip->oob_poi, 0xff, mtd->oobsize);
1794 int bytes = mtd->writesize;
1795 int cached = writelen > bytes && page != blockmask;
1796 uint8_t *wbuf = buf;
1798 /* Partial page write ? */
1799 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1801 bytes = min_t(int, bytes - column, (int) writelen);
1803 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1804 memcpy(&chip->buffers->databuf[column], buf, bytes);
1805 wbuf = chip->buffers->databuf;
1809 oob = nand_fill_oob(chip, oob, ops);
1811 ret = chip->write_page(mtd, chip, wbuf, page, cached,
1812 (ops->mode == MTD_OOB_RAW));
1824 page = realpage & chip->pagemask;
1825 /* Check, if we cross a chip boundary */
1828 chip->select_chip(mtd, -1);
1829 chip->select_chip(mtd, chipnr);
1833 ops->retlen = ops->len - writelen;
1835 ops->oobretlen = ops->ooblen;
1840 * nand_write - [MTD Interface] NAND write with ECC
1841 * @mtd: MTD device structure
1842 * @to: offset to write to
1843 * @len: number of bytes to write
1844 * @retlen: pointer to variable to store the number of written bytes
1845 * @buf: the data to write
1847 * NAND write with ECC
1849 static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
1850 size_t *retlen, const uint8_t *buf)
1852 struct nand_chip *chip = mtd->priv;
1855 /* Do not allow reads past end of device */
1856 if ((to + len) > mtd->size)
1861 nand_get_device(chip, mtd, FL_WRITING);
1863 chip->ops.len = len;
1864 chip->ops.datbuf = (uint8_t *)buf;
1865 chip->ops.oobbuf = NULL;
1867 ret = nand_do_write_ops(mtd, to, &chip->ops);
1869 *retlen = chip->ops.retlen;
1871 nand_release_device(mtd);
1877 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1878 * @mtd: MTD device structure
1879 * @to: offset to write to
1880 * @ops: oob operation description structure
1882 * NAND write out-of-band
1884 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1885 struct mtd_oob_ops *ops)
1887 int chipnr, page, status, len;
1888 struct nand_chip *chip = mtd->priv;
1890 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
1891 (unsigned int)to, (int)ops->ooblen);
1893 if (ops->mode == MTD_OOB_AUTO)
1894 len = chip->ecc.layout->oobavail;
1898 /* Do not allow write past end of page */
1899 if ((ops->ooboffs + ops->ooblen) > len) {
1900 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
1901 "Attempt to write past end of page\n");
1905 if (unlikely(ops->ooboffs >= len)) {
1906 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1907 "Attempt to start write outside oob\n");
1911 /* Do not allow reads past end of device */
1912 if (unlikely(to >= mtd->size ||
1913 ops->ooboffs + ops->ooblen >
1914 ((mtd->size >> chip->page_shift) -
1915 (to >> chip->page_shift)) * len)) {
1916 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1917 "Attempt write beyond end of device\n");
1921 chipnr = (int)(to >> chip->chip_shift);
1922 chip->select_chip(mtd, chipnr);
1924 /* Shift to get page */
1925 page = (int)(to >> chip->page_shift);
1928 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
1929 * of my DiskOnChip 2000 test units) will clear the whole data page too
1930 * if we don't do this. I have no clue why, but I seem to have 'fixed'
1931 * it in the doc2000 driver in August 1999. dwmw2.
1933 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1935 /* Check, if it is write protected */
1936 if (nand_check_wp(mtd))
1939 /* Invalidate the page cache, if we write to the cached page */
1940 if (page == chip->pagebuf)
1943 memset(chip->oob_poi, 0xff, mtd->oobsize);
1944 nand_fill_oob(chip, ops->oobbuf, ops);
1945 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
1946 memset(chip->oob_poi, 0xff, mtd->oobsize);
1951 ops->oobretlen = ops->ooblen;
1957 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1958 * @mtd: MTD device structure
1959 * @to: offset to write to
1960 * @ops: oob operation description structure
1962 static int nand_write_oob(struct mtd_info *mtd, loff_t to,
1963 struct mtd_oob_ops *ops)
1965 struct nand_chip *chip = mtd->priv;
1966 int ret = -ENOTSUPP;
1970 /* Do not allow writes past end of device */
1971 if (ops->datbuf && (to + ops->len) > mtd->size) {
1972 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1973 "Attempt read beyond end of device\n");
1977 nand_get_device(chip, mtd, FL_WRITING);
1990 ret = nand_do_write_oob(mtd, to, ops);
1992 ret = nand_do_write_ops(mtd, to, ops);
1995 nand_release_device(mtd);
2000 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2001 * @mtd: MTD device structure
2002 * @page: the page address of the block which will be erased
2004 * Standard erase command for NAND chips
2006 static void single_erase_cmd(struct mtd_info *mtd, int page)
2008 struct nand_chip *chip = mtd->priv;
2009 /* Send commands to erase a block */
2010 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2011 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2015 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2016 * @mtd: MTD device structure
2017 * @page: the page address of the block which will be erased
2019 * AND multi block erase command function
2020 * Erase 4 consecutive blocks
2022 static void multi_erase_cmd(struct mtd_info *mtd, int page)
2024 struct nand_chip *chip = mtd->priv;
2025 /* Send commands to erase a block */
2026 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2027 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2028 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2029 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2030 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2034 * nand_erase - [MTD Interface] erase block(s)
2035 * @mtd: MTD device structure
2036 * @instr: erase instruction
2038 * Erase one ore more blocks
2040 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
2042 return nand_erase_nand(mtd, instr, 0);
2045 #define BBT_PAGE_MASK 0xffffff3f
2047 * nand_erase_nand - [Internal] erase block(s)
2048 * @mtd: MTD device structure
2049 * @instr: erase instruction
2050 * @allowbbt: allow erasing the bbt area
2052 * Erase one ore more blocks
2054 int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2057 int page, len, status, pages_per_block, ret, chipnr;
2058 struct nand_chip *chip = mtd->priv;
2059 int rewrite_bbt[NAND_MAX_CHIPS]={0};
2060 unsigned int bbt_masked_page = 0xffffffff;
2062 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
2063 (unsigned int) instr->addr, (unsigned int) instr->len);
2065 /* Start address must align on block boundary */
2066 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
2067 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2071 /* Length must align on block boundary */
2072 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
2073 MTDDEBUG (MTD_DEBUG_LEVEL0,
2074 "nand_erase: Length not block aligned\n");
2078 /* Do not allow erase past end of device */
2079 if ((instr->len + instr->addr) > mtd->size) {
2080 MTDDEBUG (MTD_DEBUG_LEVEL0,
2081 "nand_erase: Erase past end of device\n");
2085 instr->fail_addr = 0xffffffff;
2087 /* Grab the lock and see if the device is available */
2088 nand_get_device(chip, mtd, FL_ERASING);
2090 /* Shift to get first page */
2091 page = (int)(instr->addr >> chip->page_shift);
2092 chipnr = (int)(instr->addr >> chip->chip_shift);
2094 /* Calculate pages in each block */
2095 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
2097 /* Select the NAND device */
2098 chip->select_chip(mtd, chipnr);
2100 /* Check, if it is write protected */
2101 if (nand_check_wp(mtd)) {
2102 MTDDEBUG (MTD_DEBUG_LEVEL0,
2103 "nand_erase: Device is write protected!!!\n");
2104 instr->state = MTD_ERASE_FAILED;
2109 * If BBT requires refresh, set the BBT page mask to see if the BBT
2110 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2111 * can not be matched. This is also done when the bbt is actually
2112 * erased to avoid recusrsive updates
2114 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2115 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2117 /* Loop through the pages */
2120 instr->state = MTD_ERASING;
2124 * heck if we have a bad block, we do not erase bad blocks !
2126 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2127 chip->page_shift, 0, allowbbt)) {
2128 printk(KERN_WARNING "nand_erase: attempt to erase a "
2129 "bad block at page 0x%08x\n", page);
2130 instr->state = MTD_ERASE_FAILED;
2135 * Invalidate the page cache, if we erase the block which
2136 * contains the current cached page
2138 if (page <= chip->pagebuf && chip->pagebuf <
2139 (page + pages_per_block))
2142 chip->erase_cmd(mtd, page & chip->pagemask);
2144 status = chip->waitfunc(mtd, chip);
2147 * See if operation failed and additional status checks are
2150 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2151 status = chip->errstat(mtd, chip, FL_ERASING,
2154 /* See if block erase succeeded */
2155 if (status & NAND_STATUS_FAIL) {
2156 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: "
2157 "Failed erase, page 0x%08x\n", page);
2158 instr->state = MTD_ERASE_FAILED;
2159 instr->fail_addr = (page << chip->page_shift);
2164 * If BBT requires refresh, set the BBT rewrite flag to the
2167 if (bbt_masked_page != 0xffffffff &&
2168 (page & BBT_PAGE_MASK) == bbt_masked_page)
2169 rewrite_bbt[chipnr] = (page << chip->page_shift);
2171 /* Increment page address and decrement length */
2172 len -= (1 << chip->phys_erase_shift);
2173 page += pages_per_block;
2175 /* Check, if we cross a chip boundary */
2176 if (len && !(page & chip->pagemask)) {
2178 chip->select_chip(mtd, -1);
2179 chip->select_chip(mtd, chipnr);
2182 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2183 * page mask to see if this BBT should be rewritten
2185 if (bbt_masked_page != 0xffffffff &&
2186 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2187 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2191 instr->state = MTD_ERASE_DONE;
2195 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2196 /* Do call back function */
2198 mtd_erase_callback(instr);
2200 /* Deselect and wake up anyone waiting on the device */
2201 nand_release_device(mtd);
2204 * If BBT requires refresh and erase was successful, rewrite any
2205 * selected bad block tables
2207 if (bbt_masked_page == 0xffffffff || ret)
2210 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2211 if (!rewrite_bbt[chipnr])
2213 /* update the BBT for chip */
2214 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
2215 "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2216 chip->bbt_td->pages[chipnr]);
2217 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2220 /* Return more or less happy */
2225 * nand_sync - [MTD Interface] sync
2226 * @mtd: MTD device structure
2228 * Sync is actually a wait for chip ready function
2230 static void nand_sync(struct mtd_info *mtd)
2232 struct nand_chip *chip = mtd->priv;
2234 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2236 /* Grab the lock and see if the device is available */
2237 nand_get_device(chip, mtd, FL_SYNCING);
2238 /* Release it and go back */
2239 nand_release_device(mtd);
2243 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
2244 * @mtd: MTD device structure
2245 * @offs: offset relative to mtd start
2247 static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
2249 /* Check for invalid offset */
2250 if (offs > mtd->size)
2253 return nand_block_checkbad(mtd, offs, 1, 0);
2257 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
2258 * @mtd: MTD device structure
2259 * @ofs: offset relative to mtd start
2261 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2263 struct nand_chip *chip = mtd->priv;
2266 if ((ret = nand_block_isbad(mtd, ofs))) {
2267 /* If it was bad already, return success and do nothing. */
2273 return chip->block_markbad(mtd, ofs);
2277 * nand_suspend - [MTD Interface] Suspend the NAND flash
2278 * @mtd: MTD device structure
2280 static int nand_suspend(struct mtd_info *mtd)
2282 struct nand_chip *chip = mtd->priv;
2284 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
2288 * nand_resume - [MTD Interface] Resume the NAND flash
2289 * @mtd: MTD device structure
2291 static void nand_resume(struct mtd_info *mtd)
2293 struct nand_chip *chip = mtd->priv;
2295 if (chip->state == FL_PM_SUSPENDED)
2296 nand_release_device(mtd);
2298 printk(KERN_ERR "nand_resume() called for a chip which is not "
2299 "in suspended state\n");
2303 * Set default functions
2305 static void nand_set_defaults(struct nand_chip *chip, int busw)
2307 /* check for proper chip_delay setup, set 20us if not */
2308 if (!chip->chip_delay)
2309 chip->chip_delay = 20;
2311 /* check, if a user supplied command function given */
2312 if (chip->cmdfunc == NULL)
2313 chip->cmdfunc = nand_command;
2315 /* check, if a user supplied wait function given */
2316 if (chip->waitfunc == NULL)
2317 chip->waitfunc = nand_wait;
2319 if (!chip->select_chip)
2320 chip->select_chip = nand_select_chip;
2321 if (!chip->read_byte)
2322 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2323 if (!chip->read_word)
2324 chip->read_word = nand_read_word;
2325 if (!chip->block_bad)
2326 chip->block_bad = nand_block_bad;
2327 if (!chip->block_markbad)
2328 chip->block_markbad = nand_default_block_markbad;
2329 if (!chip->write_buf)
2330 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2331 if (!chip->read_buf)
2332 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2333 if (!chip->verify_buf)
2334 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2335 if (!chip->scan_bbt)
2336 chip->scan_bbt = nand_default_bbt;
2338 if (!chip->controller) {
2339 chip->controller = &chip->hwcontrol;
2341 /* XXX U-BOOT XXX */
2343 spin_lock_init(&chip->controller->lock);
2344 init_waitqueue_head(&chip->controller->wq);
2351 * Get the flash and manufacturer id and lookup if the type is supported
2353 static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2354 struct nand_chip *chip,
2355 int busw, int *maf_id)
2357 struct nand_flash_dev *type = NULL;
2358 int i, dev_id, maf_idx;
2360 /* Select the device */
2361 chip->select_chip(mtd, 0);
2363 /* Send the command for reading device ID */
2364 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2366 /* Read manufacturer and device IDs */
2367 *maf_id = chip->read_byte(mtd);
2368 dev_id = chip->read_byte(mtd);
2370 /* Lookup the flash id */
2371 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2372 if (dev_id == nand_flash_ids[i].id) {
2373 type = &nand_flash_ids[i];
2379 return ERR_PTR(-ENODEV);
2382 mtd->name = type->name;
2384 chip->chipsize = type->chipsize << 20;
2386 /* Newer devices have all the information in additional id bytes */
2387 if (!type->pagesize) {
2389 /* The 3rd id byte holds MLC / multichip data */
2390 chip->cellinfo = chip->read_byte(mtd);
2391 /* The 4th id byte is the important one */
2392 extid = chip->read_byte(mtd);
2394 mtd->writesize = 1024 << (extid & 0x3);
2397 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
2399 /* Calc blocksize. Blocksize is multiples of 64KiB */
2400 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2402 /* Get buswidth information */
2403 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2407 * Old devices have chip data hardcoded in the device id table
2409 mtd->erasesize = type->erasesize;
2410 mtd->writesize = type->pagesize;
2411 mtd->oobsize = mtd->writesize / 32;
2412 busw = type->options & NAND_BUSWIDTH_16;
2415 /* Try to identify manufacturer */
2416 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2417 if (nand_manuf_ids[maf_idx].id == *maf_id)
2422 * Check, if buswidth is correct. Hardware drivers should set
2425 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2426 printk(KERN_INFO "NAND device: Manufacturer ID:"
2427 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2428 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2429 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
2430 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2432 return ERR_PTR(-EINVAL);
2435 /* Calculate the address shift from the page size */
2436 chip->page_shift = ffs(mtd->writesize) - 1;
2437 /* Convert chipsize to number of pages per chip -1. */
2438 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
2440 chip->bbt_erase_shift = chip->phys_erase_shift =
2441 ffs(mtd->erasesize) - 1;
2442 chip->chip_shift = ffs(chip->chipsize) - 1;
2444 /* Set the bad block position */
2445 chip->badblockpos = mtd->writesize > 512 ?
2446 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2448 /* Get chip options, preserve non chip based options */
2449 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2450 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
2453 * Set chip as a default. Board drivers can override it, if necessary
2455 chip->options |= NAND_NO_AUTOINCR;
2457 /* Check if chip is a not a samsung device. Do not clear the
2458 * options for chips which are not having an extended id.
2460 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2461 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2463 /* Check for AND chips with 4 page planes */
2464 if (chip->options & NAND_4PAGE_ARRAY)
2465 chip->erase_cmd = multi_erase_cmd;
2467 chip->erase_cmd = single_erase_cmd;
2469 /* Do not replace user supplied command function ! */
2470 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2471 chip->cmdfunc = nand_command_lp;
2473 MTDDEBUG (MTD_DEBUG_LEVEL0, "NAND device: Manufacturer ID:"
2474 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2475 nand_manuf_ids[maf_idx].name, type->name);
2481 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2482 * @mtd: MTD device structure
2483 * @maxchips: Number of chips to scan for
2485 * This is the first phase of the normal nand_scan() function. It
2486 * reads the flash ID and sets up MTD fields accordingly.
2488 * The mtd->owner field must be set to the module of the caller.
2490 int nand_scan_ident(struct mtd_info *mtd, int maxchips)
2492 int i, busw, nand_maf_id;
2493 struct nand_chip *chip = mtd->priv;
2494 struct nand_flash_dev *type;
2496 /* Get buswidth to select the correct functions */
2497 busw = chip->options & NAND_BUSWIDTH_16;
2498 /* Set the default functions */
2499 nand_set_defaults(chip, busw);
2501 /* Read the flash type */
2502 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
2505 printk(KERN_WARNING "No NAND device found!!!\n");
2506 chip->select_chip(mtd, -1);
2507 return PTR_ERR(type);
2510 /* Check for a chip array */
2511 for (i = 1; i < maxchips; i++) {
2512 chip->select_chip(mtd, i);
2513 /* Send the command for reading device ID */
2514 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2515 /* Read manufacturer and device IDs */
2516 if (nand_maf_id != chip->read_byte(mtd) ||
2517 type->id != chip->read_byte(mtd))
2521 printk(KERN_INFO "%d NAND chips detected\n", i);
2523 /* Store the number of chips and calc total size for mtd */
2525 mtd->size = i * chip->chipsize;
2532 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2533 * @mtd: MTD device structure
2534 * @maxchips: Number of chips to scan for
2536 * This is the second phase of the normal nand_scan() function. It
2537 * fills out all the uninitialized function pointers with the defaults
2538 * and scans for a bad block table if appropriate.
2540 int nand_scan_tail(struct mtd_info *mtd)
2543 struct nand_chip *chip = mtd->priv;
2545 if (!(chip->options & NAND_OWN_BUFFERS))
2546 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2550 /* Set the internal oob buffer location, just after the page data */
2551 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
2554 * If no default placement scheme is given, select an appropriate one
2556 if (!chip->ecc.layout) {
2557 switch (mtd->oobsize) {
2559 chip->ecc.layout = &nand_oob_8;
2562 chip->ecc.layout = &nand_oob_16;
2565 chip->ecc.layout = &nand_oob_64;
2568 chip->ecc.layout = &nand_oob_128;
2571 printk(KERN_WARNING "No oob scheme defined for "
2572 "oobsize %d\n", mtd->oobsize);
2577 if (!chip->write_page)
2578 chip->write_page = nand_write_page;
2581 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2582 * selected and we have 256 byte pagesize fallback to software ECC
2584 if (!chip->ecc.read_page_raw)
2585 chip->ecc.read_page_raw = nand_read_page_raw;
2586 if (!chip->ecc.write_page_raw)
2587 chip->ecc.write_page_raw = nand_write_page_raw;
2589 switch (chip->ecc.mode) {
2591 /* Use standard hwecc read page function ? */
2592 if (!chip->ecc.read_page)
2593 chip->ecc.read_page = nand_read_page_hwecc;
2594 if (!chip->ecc.write_page)
2595 chip->ecc.write_page = nand_write_page_hwecc;
2596 if (!chip->ecc.read_oob)
2597 chip->ecc.read_oob = nand_read_oob_std;
2598 if (!chip->ecc.write_oob)
2599 chip->ecc.write_oob = nand_write_oob_std;
2601 case NAND_ECC_HW_SYNDROME:
2602 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2603 !chip->ecc.hwctl) &&
2604 (!chip->ecc.read_page ||
2605 chip->ecc.read_page == nand_read_page_hwecc ||
2606 !chip->ecc.write_page ||
2607 chip->ecc.write_page == nand_write_page_hwecc)) {
2608 printk(KERN_WARNING "No ECC functions supplied, "
2609 "Hardware ECC not possible\n");
2612 /* Use standard syndrome read/write page function ? */
2613 if (!chip->ecc.read_page)
2614 chip->ecc.read_page = nand_read_page_syndrome;
2615 if (!chip->ecc.write_page)
2616 chip->ecc.write_page = nand_write_page_syndrome;
2617 if (!chip->ecc.read_oob)
2618 chip->ecc.read_oob = nand_read_oob_syndrome;
2619 if (!chip->ecc.write_oob)
2620 chip->ecc.write_oob = nand_write_oob_syndrome;
2622 if (mtd->writesize >= chip->ecc.size)
2624 printk(KERN_WARNING "%d byte HW ECC not possible on "
2625 "%d byte page size, fallback to SW ECC\n",
2626 chip->ecc.size, mtd->writesize);
2627 chip->ecc.mode = NAND_ECC_SOFT;
2630 chip->ecc.calculate = nand_calculate_ecc;
2631 chip->ecc.correct = nand_correct_data;
2632 chip->ecc.read_page = nand_read_page_swecc;
2633 chip->ecc.write_page = nand_write_page_swecc;
2634 chip->ecc.read_oob = nand_read_oob_std;
2635 chip->ecc.write_oob = nand_write_oob_std;
2636 chip->ecc.size = 256;
2637 chip->ecc.bytes = 3;
2641 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2642 "This is not recommended !!\n");
2643 chip->ecc.read_page = nand_read_page_raw;
2644 chip->ecc.write_page = nand_write_page_raw;
2645 chip->ecc.read_oob = nand_read_oob_std;
2646 chip->ecc.write_oob = nand_write_oob_std;
2647 chip->ecc.size = mtd->writesize;
2648 chip->ecc.bytes = 0;
2652 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
2658 * The number of bytes available for a client to place data into
2659 * the out of band area
2661 chip->ecc.layout->oobavail = 0;
2662 for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
2663 chip->ecc.layout->oobavail +=
2664 chip->ecc.layout->oobfree[i].length;
2665 mtd->oobavail = chip->ecc.layout->oobavail;
2668 * Set the number of read / write steps for one page depending on ECC
2671 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2672 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
2673 printk(KERN_WARNING "Invalid ecc parameters\n");
2676 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
2679 * Allow subpage writes up to ecc.steps. Not possible for MLC
2682 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2683 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2684 switch(chip->ecc.steps) {
2686 mtd->subpage_sft = 1;
2690 mtd->subpage_sft = 2;
2694 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
2696 /* Initialize state */
2697 chip->state = FL_READY;
2699 /* De-select the device */
2700 chip->select_chip(mtd, -1);
2702 /* Invalidate the pagebuffer reference */
2705 /* Fill in remaining MTD driver data */
2706 mtd->type = MTD_NANDFLASH;
2707 mtd->flags = MTD_CAP_NANDFLASH;
2708 mtd->erase = nand_erase;
2710 mtd->unpoint = NULL;
2711 mtd->read = nand_read;
2712 mtd->write = nand_write;
2713 mtd->read_oob = nand_read_oob;
2714 mtd->write_oob = nand_write_oob;
2715 mtd->sync = nand_sync;
2718 mtd->suspend = nand_suspend;
2719 mtd->resume = nand_resume;
2720 mtd->block_isbad = nand_block_isbad;
2721 mtd->block_markbad = nand_block_markbad;
2723 /* propagate ecc.layout to mtd_info */
2724 mtd->ecclayout = chip->ecc.layout;
2726 /* Check, if we should skip the bad block table scan */
2727 if (chip->options & NAND_SKIP_BBTSCAN)
2728 chip->options |= NAND_BBT_SCANNED;
2733 /* module_text_address() isn't exported, and it's mostly a pointless
2734 test if this is a module _anyway_ -- they'd have to try _really_ hard
2735 to call us from in-kernel code if the core NAND support is modular. */
2737 #define caller_is_module() (1)
2739 #define caller_is_module() \
2740 module_text_address((unsigned long)__builtin_return_address(0))
2744 * nand_scan - [NAND Interface] Scan for the NAND device
2745 * @mtd: MTD device structure
2746 * @maxchips: Number of chips to scan for
2748 * This fills out all the uninitialized function pointers
2749 * with the defaults.
2750 * The flash ID is read and the mtd/chip structures are
2751 * filled with the appropriate values.
2752 * The mtd->owner field must be set to the module of the caller
2755 int nand_scan(struct mtd_info *mtd, int maxchips)
2759 /* Many callers got this wrong, so check for it for a while... */
2760 /* XXX U-BOOT XXX */
2762 if (!mtd->owner && caller_is_module()) {
2763 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2768 ret = nand_scan_ident(mtd, maxchips);
2770 ret = nand_scan_tail(mtd);
2775 * nand_release - [NAND Interface] Free resources held by the NAND device
2776 * @mtd: MTD device structure
2778 void nand_release(struct mtd_info *mtd)
2780 struct nand_chip *chip = mtd->priv;
2782 #ifdef CONFIG_MTD_PARTITIONS
2783 /* Deregister partitions */
2784 del_mtd_partitions(mtd);
2786 /* Deregister the device */
2787 /* XXX U-BOOT XXX */
2789 del_mtd_device(mtd);
2792 /* Free bad block table memory */
2794 if (!(chip->options & NAND_OWN_BUFFERS))
2795 kfree(chip->buffers);
2798 /* XXX U-BOOT XXX */
2800 EXPORT_SYMBOL_GPL(nand_scan);
2801 EXPORT_SYMBOL_GPL(nand_scan_ident);
2802 EXPORT_SYMBOL_GPL(nand_scan_tail);
2803 EXPORT_SYMBOL_GPL(nand_release);
2805 static int __init nand_base_init(void)
2807 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2811 static void __exit nand_base_exit(void)
2813 led_trigger_unregister_simple(nand_led_trigger);
2816 module_init(nand_base_init);
2817 module_exit(nand_base_exit);
2819 MODULE_LICENSE("GPL");
2820 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2821 MODULE_DESCRIPTION("Generic NAND flash driver code");