5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
9 * Additional technical information is available on
10 * http://www.linux-mtd.infradead.org/doc/nand.html
12 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
13 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
16 * David Woodhouse for adding multichip support
18 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
22 * Enable cached programming for 2k page size chips
23 * Check, if mtd->ecctype should be set to MTD_ECC_HW
24 * if we have HW ecc support.
25 * The AG-AND chips have nice features for speed improvement,
26 * which are not supported yet. Read / program 4 pages in one go.
27 * BBT table is not serialized, has to be fixed
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License version 2 as
31 * published by the Free Software Foundation.
37 #include <linux/module.h>
38 #include <linux/delay.h>
39 #include <linux/errno.h>
40 #include <linux/err.h>
41 #include <linux/sched.h>
42 #include <linux/slab.h>
43 #include <linux/types.h>
44 #include <linux/mtd/mtd.h>
45 #include <linux/mtd/nand.h>
46 #include <linux/mtd/nand_ecc.h>
47 #include <linux/mtd/compatmac.h>
48 #include <linux/interrupt.h>
49 #include <linux/bitops.h>
50 #include <linux/leds.h>
53 #ifdef CONFIG_MTD_PARTITIONS
54 #include <linux/mtd/partitions.h>
61 #define ENOTSUPP 524 /* Operation is not supported */
65 #include <linux/err.h>
66 #include <linux/mtd/compat.h>
67 #include <linux/mtd/mtd.h>
68 #include <linux/mtd/nand.h>
69 #include <linux/mtd/nand_ecc.h>
72 #include <asm/errno.h>
74 #ifdef CONFIG_JFFS2_NAND
75 #include <jffs2/jffs2.h>
78 /* Define default oob placement schemes for large and small page devices */
79 static struct nand_ecclayout nand_oob_8 = {
89 static struct nand_ecclayout nand_oob_16 = {
91 .eccpos = {0, 1, 2, 3, 6, 7},
97 static struct nand_ecclayout nand_oob_64 = {
100 40, 41, 42, 43, 44, 45, 46, 47,
101 48, 49, 50, 51, 52, 53, 54, 55,
102 56, 57, 58, 59, 60, 61, 62, 63},
108 static struct nand_ecclayout nand_oob_128 = {
111 80, 81, 82, 83, 84, 85, 86, 87,
112 88, 89, 90, 91, 92, 93, 94, 95,
113 96, 97, 98, 99, 100, 101, 102, 103,
114 104, 105, 106, 107, 108, 109, 110, 111,
115 112, 113, 114, 115, 116, 117, 118, 119,
116 120, 121, 122, 123, 124, 125, 126, 127},
123 static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
126 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
127 struct mtd_oob_ops *ops);
129 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this);
132 * For devices which display every fart in the system on a separate LED. Is
133 * compiled away when LED support is disabled.
137 DEFINE_LED_TRIGGER(nand_led_trigger);
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
148 static void nand_release_device(struct mtd_info *mtd)
150 struct nand_chip *chip = mtd->priv;
152 /* De-select the NAND device */
153 chip->select_chip(mtd, -1);
155 /* Release the controller and the chip */
156 spin_lock(&chip->controller->lock);
157 chip->controller->active = NULL;
158 chip->state = FL_READY;
159 wake_up(&chip->controller->wq);
160 spin_unlock(&chip->controller->lock);
163 static void nand_release_device (struct mtd_info *mtd)
165 struct nand_chip *this = mtd->priv;
166 this->select_chip(mtd, -1); /* De-select the NAND device */
171 * nand_read_byte - [DEFAULT] read one byte from the chip
172 * @mtd: MTD device structure
174 * Default read function for 8bit buswith
176 static uint8_t nand_read_byte(struct mtd_info *mtd)
178 struct nand_chip *chip = mtd->priv;
179 return readb(chip->IO_ADDR_R);
183 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
184 * @mtd: MTD device structure
186 * Default read function for 16bit buswith with
187 * endianess conversion
189 static uint8_t nand_read_byte16(struct mtd_info *mtd)
191 struct nand_chip *chip = mtd->priv;
192 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
196 * nand_read_word - [DEFAULT] read one word from the chip
197 * @mtd: MTD device structure
199 * Default read function for 16bit buswith without
200 * endianess conversion
202 static u16 nand_read_word(struct mtd_info *mtd)
204 struct nand_chip *chip = mtd->priv;
205 return readw(chip->IO_ADDR_R);
209 * nand_select_chip - [DEFAULT] control CE line
210 * @mtd: MTD device structure
211 * @chipnr: chipnumber to select, -1 for deselect
213 * Default select function for 1 chip devices.
215 static void nand_select_chip(struct mtd_info *mtd, int chipnr)
217 struct nand_chip *chip = mtd->priv;
221 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
232 * nand_write_buf - [DEFAULT] write buffer to chip
233 * @mtd: MTD device structure
235 * @len: number of bytes to write
237 * Default write function for 8bit buswith
239 static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
242 struct nand_chip *chip = mtd->priv;
244 for (i = 0; i < len; i++)
245 writeb(buf[i], chip->IO_ADDR_W);
249 * nand_read_buf - [DEFAULT] read chip data into buffer
250 * @mtd: MTD device structure
251 * @buf: buffer to store date
252 * @len: number of bytes to read
254 * Default read function for 8bit buswith
256 static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
259 struct nand_chip *chip = mtd->priv;
261 for (i = 0; i < len; i++)
262 buf[i] = readb(chip->IO_ADDR_R);
266 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
267 * @mtd: MTD device structure
268 * @buf: buffer containing the data to compare
269 * @len: number of bytes to compare
271 * Default verify function for 8bit buswith
273 static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
276 struct nand_chip *chip = mtd->priv;
278 for (i = 0; i < len; i++)
279 if (buf[i] != readb(chip->IO_ADDR_R))
285 * nand_write_buf16 - [DEFAULT] write buffer to chip
286 * @mtd: MTD device structure
288 * @len: number of bytes to write
290 * Default write function for 16bit buswith
292 static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
295 struct nand_chip *chip = mtd->priv;
296 u16 *p = (u16 *) buf;
299 for (i = 0; i < len; i++)
300 writew(p[i], chip->IO_ADDR_W);
305 * nand_read_buf16 - [DEFAULT] read chip data into buffer
306 * @mtd: MTD device structure
307 * @buf: buffer to store date
308 * @len: number of bytes to read
310 * Default read function for 16bit buswith
312 static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
315 struct nand_chip *chip = mtd->priv;
316 u16 *p = (u16 *) buf;
319 for (i = 0; i < len; i++)
320 p[i] = readw(chip->IO_ADDR_R);
324 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
325 * @mtd: MTD device structure
326 * @buf: buffer containing the data to compare
327 * @len: number of bytes to compare
329 * Default verify function for 16bit buswith
331 static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
334 struct nand_chip *chip = mtd->priv;
335 u16 *p = (u16 *) buf;
338 for (i = 0; i < len; i++)
339 if (p[i] != readw(chip->IO_ADDR_R))
346 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
347 * @mtd: MTD device structure
348 * @ofs: offset from device start
349 * @getchip: 0, if the chip is already selected
351 * Check, if the block is bad.
353 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
355 int page, chipnr, res = 0;
356 struct nand_chip *chip = mtd->priv;
359 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
362 chipnr = (int)(ofs >> chip->chip_shift);
364 nand_get_device(chip, mtd, FL_READING);
366 /* Select the NAND device */
367 chip->select_chip(mtd, chipnr);
370 if (chip->options & NAND_BUSWIDTH_16) {
371 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
373 bad = cpu_to_le16(chip->read_word(mtd));
374 if (chip->badblockpos & 0x1)
376 if ((bad & 0xFF) != 0xff)
379 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
380 if (chip->read_byte(mtd) != 0xff)
385 nand_release_device(mtd);
391 * nand_default_block_markbad - [DEFAULT] mark a block bad
392 * @mtd: MTD device structure
393 * @ofs: offset from device start
395 * This is the default implementation, which can be overridden by
396 * a hardware specific driver.
398 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
400 struct nand_chip *chip = mtd->priv;
401 uint8_t buf[2] = { 0, 0 };
404 /* Get block number */
405 block = (int)(ofs >> chip->bbt_erase_shift);
407 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
409 /* Do we have a flash based bad block table ? */
410 if (chip->options & NAND_USE_FLASH_BBT)
411 ret = nand_update_bbt(mtd, ofs);
413 /* We write two bytes, so we dont have to mess with 16 bit
416 nand_get_device(chip, mtd, FL_WRITING);
418 chip->ops.len = chip->ops.ooblen = 2;
419 chip->ops.datbuf = NULL;
420 chip->ops.oobbuf = buf;
421 chip->ops.ooboffs = chip->badblockpos & ~0x01;
423 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
424 nand_release_device(mtd);
427 mtd->ecc_stats.badblocks++;
433 * nand_check_wp - [GENERIC] check if the chip is write protected
434 * @mtd: MTD device structure
435 * Check, if the device is write protected
437 * The function expects, that the device is already selected
439 static int nand_check_wp(struct mtd_info *mtd)
441 struct nand_chip *chip = mtd->priv;
442 /* Check the WP bit */
443 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
444 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
448 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
449 * @mtd: MTD device structure
450 * @ofs: offset from device start
451 * @getchip: 0, if the chip is already selected
452 * @allowbbt: 1, if its allowed to access the bbt area
454 * Check, if the block is bad. Either by reading the bad block table or
455 * calling of the scan function.
457 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
460 struct nand_chip *chip = mtd->priv;
462 if (!(chip->options & NAND_BBT_SCANNED)) {
463 chip->options |= NAND_BBT_SCANNED;
468 return chip->block_bad(mtd, ofs, getchip);
470 /* Return info from the table */
471 return nand_isbad_bbt(mtd, ofs, allowbbt);
475 * Wait for the ready pin, after a command
476 * The timeout is catched later.
480 void nand_wait_ready(struct mtd_info *mtd)
482 struct nand_chip *chip = mtd->priv;
483 unsigned long timeo = jiffies + 2;
485 led_trigger_event(nand_led_trigger, LED_FULL);
486 /* wait until command is processed or timeout occures */
488 if (chip->dev_ready(mtd))
490 touch_softlockup_watchdog();
491 } while (time_before(jiffies, timeo));
492 led_trigger_event(nand_led_trigger, LED_OFF);
494 EXPORT_SYMBOL_GPL(nand_wait_ready);
496 void nand_wait_ready(struct mtd_info *mtd)
498 struct nand_chip *chip = mtd->priv;
499 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
503 /* wait until command is processed or timeout occures */
504 while (get_timer(0) < timeo) {
506 if (chip->dev_ready(mtd))
513 * nand_command - [DEFAULT] Send command to NAND device
514 * @mtd: MTD device structure
515 * @command: the command to be sent
516 * @column: the column address for this command, -1 if none
517 * @page_addr: the page address for this command, -1 if none
519 * Send command to NAND device. This function is used for small page
520 * devices (256/512 Bytes per page)
522 static void nand_command(struct mtd_info *mtd, unsigned int command,
523 int column, int page_addr)
525 register struct nand_chip *chip = mtd->priv;
526 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
529 * Write out the command to the device.
531 if (command == NAND_CMD_SEQIN) {
534 if (column >= mtd->writesize) {
536 column -= mtd->writesize;
537 readcmd = NAND_CMD_READOOB;
538 } else if (column < 256) {
539 /* First 256 bytes --> READ0 */
540 readcmd = NAND_CMD_READ0;
543 readcmd = NAND_CMD_READ1;
545 chip->cmd_ctrl(mtd, readcmd, ctrl);
546 ctrl &= ~NAND_CTRL_CHANGE;
548 chip->cmd_ctrl(mtd, command, ctrl);
551 * Address cycle, when necessary
553 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
554 /* Serially input address */
556 /* Adjust columns for 16 bit buswidth */
557 if (chip->options & NAND_BUSWIDTH_16)
559 chip->cmd_ctrl(mtd, column, ctrl);
560 ctrl &= ~NAND_CTRL_CHANGE;
562 if (page_addr != -1) {
563 chip->cmd_ctrl(mtd, page_addr, ctrl);
564 ctrl &= ~NAND_CTRL_CHANGE;
565 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
566 /* One more address cycle for devices > 32MiB */
567 if (chip->chipsize > (32 << 20))
568 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
570 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
573 * program and erase have their own busy handlers
574 * status and sequential in needs no delay
578 case NAND_CMD_PAGEPROG:
579 case NAND_CMD_ERASE1:
580 case NAND_CMD_ERASE2:
582 case NAND_CMD_STATUS:
588 udelay(chip->chip_delay);
589 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
590 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
592 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
593 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
596 /* This applies to read commands */
599 * If we don't have access to the busy pin, we apply the given
602 if (!chip->dev_ready) {
603 udelay(chip->chip_delay);
607 /* Apply this short delay always to ensure that we do wait tWB in
608 * any case on any machine. */
611 nand_wait_ready(mtd);
615 * nand_command_lp - [DEFAULT] Send command to NAND large page device
616 * @mtd: MTD device structure
617 * @command: the command to be sent
618 * @column: the column address for this command, -1 if none
619 * @page_addr: the page address for this command, -1 if none
621 * Send command to NAND device. This is the version for the new large page
622 * devices We dont have the separate regions as we have in the small page
623 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
625 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
626 int column, int page_addr)
628 register struct nand_chip *chip = mtd->priv;
630 /* Emulate NAND_CMD_READOOB */
631 if (command == NAND_CMD_READOOB) {
632 column += mtd->writesize;
633 command = NAND_CMD_READ0;
636 /* Command latch cycle */
637 chip->cmd_ctrl(mtd, command & 0xff,
638 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
640 if (column != -1 || page_addr != -1) {
641 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
643 /* Serially input address */
645 /* Adjust columns for 16 bit buswidth */
646 if (chip->options & NAND_BUSWIDTH_16)
648 chip->cmd_ctrl(mtd, column, ctrl);
649 ctrl &= ~NAND_CTRL_CHANGE;
650 chip->cmd_ctrl(mtd, column >> 8, ctrl);
652 if (page_addr != -1) {
653 chip->cmd_ctrl(mtd, page_addr, ctrl);
654 chip->cmd_ctrl(mtd, page_addr >> 8,
655 NAND_NCE | NAND_ALE);
656 /* One more address cycle for devices > 128MiB */
657 if (chip->chipsize > (128 << 20))
658 chip->cmd_ctrl(mtd, page_addr >> 16,
659 NAND_NCE | NAND_ALE);
662 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
665 * program and erase have their own busy handlers
666 * status, sequential in, and deplete1 need no delay
670 case NAND_CMD_CACHEDPROG:
671 case NAND_CMD_PAGEPROG:
672 case NAND_CMD_ERASE1:
673 case NAND_CMD_ERASE2:
676 case NAND_CMD_STATUS:
677 case NAND_CMD_DEPLETE1:
681 * read error status commands require only a short delay
683 case NAND_CMD_STATUS_ERROR:
684 case NAND_CMD_STATUS_ERROR0:
685 case NAND_CMD_STATUS_ERROR1:
686 case NAND_CMD_STATUS_ERROR2:
687 case NAND_CMD_STATUS_ERROR3:
688 udelay(chip->chip_delay);
694 udelay(chip->chip_delay);
695 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
696 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
697 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
698 NAND_NCE | NAND_CTRL_CHANGE);
699 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
702 case NAND_CMD_RNDOUT:
703 /* No ready / busy check necessary */
704 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
705 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
706 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
707 NAND_NCE | NAND_CTRL_CHANGE);
711 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
712 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
713 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
714 NAND_NCE | NAND_CTRL_CHANGE);
716 /* This applies to read commands */
719 * If we don't have access to the busy pin, we apply the given
722 if (!chip->dev_ready) {
723 udelay(chip->chip_delay);
728 /* Apply this short delay always to ensure that we do wait tWB in
729 * any case on any machine. */
732 nand_wait_ready(mtd);
736 * nand_get_device - [GENERIC] Get chip for selected access
737 * @chip: the nand chip descriptor
738 * @mtd: MTD device structure
739 * @new_state: the state which is requested
741 * Get the device and lock it for exclusive access
746 nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
748 spinlock_t *lock = &chip->controller->lock;
749 wait_queue_head_t *wq = &chip->controller->wq;
750 DECLARE_WAITQUEUE(wait, current);
754 /* Hardware controller shared among independend devices */
755 /* Hardware controller shared among independend devices */
756 if (!chip->controller->active)
757 chip->controller->active = chip;
759 if (chip->controller->active == chip && chip->state == FL_READY) {
760 chip->state = new_state;
764 if (new_state == FL_PM_SUSPENDED) {
766 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
768 set_current_state(TASK_UNINTERRUPTIBLE);
769 add_wait_queue(wq, &wait);
772 remove_wait_queue(wq, &wait);
776 static int nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
778 this->state = new_state;
784 * nand_wait - [DEFAULT] wait until the command is done
785 * @mtd: MTD device structure
786 * @chip: NAND chip structure
788 * Wait for command done. This applies to erase and program only
789 * Erase can take up to 400ms and program up to 20ms according to
790 * general NAND and SmartMedia specs
794 static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
797 unsigned long timeo = jiffies;
798 int status, state = chip->state;
800 if (state == FL_ERASING)
801 timeo += (HZ * 400) / 1000;
803 timeo += (HZ * 20) / 1000;
805 led_trigger_event(nand_led_trigger, LED_FULL);
807 /* Apply this short delay always to ensure that we do wait tWB in
808 * any case on any machine. */
811 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
812 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
814 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
816 while (time_before(jiffies, timeo)) {
817 if (chip->dev_ready) {
818 if (chip->dev_ready(mtd))
821 if (chip->read_byte(mtd) & NAND_STATUS_READY)
826 led_trigger_event(nand_led_trigger, LED_OFF);
828 status = (int)chip->read_byte(mtd);
832 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this)
835 int state = this->state;
837 if (state == FL_ERASING)
838 timeo = (CONFIG_SYS_HZ * 400) / 1000;
840 timeo = (CONFIG_SYS_HZ * 20) / 1000;
842 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
843 this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
845 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
850 if (get_timer(0) > timeo) {
855 if (this->dev_ready) {
856 if (this->dev_ready(mtd))
859 if (this->read_byte(mtd) & NAND_STATUS_READY)
863 #ifdef PPCHAMELON_NAND_TIMER_HACK
865 while (get_timer(0) < 10);
866 #endif /* PPCHAMELON_NAND_TIMER_HACK */
868 return this->read_byte(mtd);
873 * nand_read_page_raw - [Intern] read raw page data without ecc
874 * @mtd: mtd info structure
875 * @chip: nand chip info structure
876 * @buf: buffer to store read data
878 static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
881 chip->read_buf(mtd, buf, mtd->writesize);
882 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
887 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
888 * @mtd: mtd info structure
889 * @chip: nand chip info structure
890 * @buf: buffer to store read data
892 static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
895 int i, eccsize = chip->ecc.size;
896 int eccbytes = chip->ecc.bytes;
897 int eccsteps = chip->ecc.steps;
899 uint8_t *ecc_calc = chip->buffers->ecccalc;
900 uint8_t *ecc_code = chip->buffers->ecccode;
901 uint32_t *eccpos = chip->ecc.layout->eccpos;
903 chip->ecc.read_page_raw(mtd, chip, buf);
905 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
906 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
908 for (i = 0; i < chip->ecc.total; i++)
909 ecc_code[i] = chip->oob_poi[eccpos[i]];
911 eccsteps = chip->ecc.steps;
914 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
917 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
919 mtd->ecc_stats.failed++;
921 mtd->ecc_stats.corrected += stat;
927 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
928 * @mtd: mtd info structure
929 * @chip: nand chip info structure
930 * @dataofs offset of requested data within the page
931 * @readlen data length
932 * @buf: buffer to store read data
934 static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
936 int start_step, end_step, num_steps;
937 uint32_t *eccpos = chip->ecc.layout->eccpos;
939 int data_col_addr, i, gaps = 0;
940 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
941 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
943 /* Column address wihin the page aligned to ECC size (256bytes). */
944 start_step = data_offs / chip->ecc.size;
945 end_step = (data_offs + readlen - 1) / chip->ecc.size;
946 num_steps = end_step - start_step + 1;
948 /* Data size aligned to ECC ecc.size*/
949 datafrag_len = num_steps * chip->ecc.size;
950 eccfrag_len = num_steps * chip->ecc.bytes;
952 data_col_addr = start_step * chip->ecc.size;
953 /* If we read not a page aligned data */
954 if (data_col_addr != 0)
955 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
957 p = bufpoi + data_col_addr;
958 chip->read_buf(mtd, p, datafrag_len);
961 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
962 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
964 /* The performance is faster if to position offsets
965 according to ecc.pos. Let make sure here that
966 there are no gaps in ecc positions */
967 for (i = 0; i < eccfrag_len - 1; i++) {
968 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
969 eccpos[i + start_step * chip->ecc.bytes + 1]) {
975 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
976 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
978 /* send the command to read the particular ecc bytes */
979 /* take care about buswidth alignment in read_buf */
980 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
981 aligned_len = eccfrag_len;
982 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
984 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
987 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
988 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
991 for (i = 0; i < eccfrag_len; i++)
992 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
994 p = bufpoi + data_col_addr;
995 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
998 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1000 mtd->ecc_stats.failed++;
1002 mtd->ecc_stats.corrected += stat;
1008 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
1009 * @mtd: mtd info structure
1010 * @chip: nand chip info structure
1011 * @buf: buffer to store read data
1013 * Not for syndrome calculating ecc controllers which need a special oob layout
1015 static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1018 int i, eccsize = chip->ecc.size;
1019 int eccbytes = chip->ecc.bytes;
1020 int eccsteps = chip->ecc.steps;
1022 uint8_t *ecc_calc = chip->buffers->ecccalc;
1023 uint8_t *ecc_code = chip->buffers->ecccode;
1024 uint32_t *eccpos = chip->ecc.layout->eccpos;
1026 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1027 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1028 chip->read_buf(mtd, p, eccsize);
1029 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1031 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1033 for (i = 0; i < chip->ecc.total; i++)
1034 ecc_code[i] = chip->oob_poi[eccpos[i]];
1036 eccsteps = chip->ecc.steps;
1039 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1042 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1044 mtd->ecc_stats.failed++;
1046 mtd->ecc_stats.corrected += stat;
1052 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
1053 * @mtd: mtd info structure
1054 * @chip: nand chip info structure
1055 * @buf: buffer to store read data
1057 * The hw generator calculates the error syndrome automatically. Therefor
1058 * we need a special oob layout and handling.
1060 static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1063 int i, eccsize = chip->ecc.size;
1064 int eccbytes = chip->ecc.bytes;
1065 int eccsteps = chip->ecc.steps;
1067 uint8_t *oob = chip->oob_poi;
1069 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1072 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1073 chip->read_buf(mtd, p, eccsize);
1075 if (chip->ecc.prepad) {
1076 chip->read_buf(mtd, oob, chip->ecc.prepad);
1077 oob += chip->ecc.prepad;
1080 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1081 chip->read_buf(mtd, oob, eccbytes);
1082 stat = chip->ecc.correct(mtd, p, oob, NULL);
1085 mtd->ecc_stats.failed++;
1087 mtd->ecc_stats.corrected += stat;
1091 if (chip->ecc.postpad) {
1092 chip->read_buf(mtd, oob, chip->ecc.postpad);
1093 oob += chip->ecc.postpad;
1097 /* Calculate remaining oob bytes */
1098 i = mtd->oobsize - (oob - chip->oob_poi);
1100 chip->read_buf(mtd, oob, i);
1106 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1107 * @chip: nand chip structure
1108 * @oob: oob destination address
1109 * @ops: oob ops structure
1110 * @len: size of oob to transfer
1112 static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1113 struct mtd_oob_ops *ops, size_t len)
1119 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1122 case MTD_OOB_AUTO: {
1123 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1124 uint32_t boffs = 0, roffs = ops->ooboffs;
1127 for(; free->length && len; free++, len -= bytes) {
1128 /* Read request not from offset 0 ? */
1129 if (unlikely(roffs)) {
1130 if (roffs >= free->length) {
1131 roffs -= free->length;
1134 boffs = free->offset + roffs;
1135 bytes = min_t(size_t, len,
1136 (free->length - roffs));
1139 bytes = min_t(size_t, len, free->length);
1140 boffs = free->offset;
1142 memcpy(oob, chip->oob_poi + boffs, bytes);
1154 * nand_do_read_ops - [Internal] Read data with ECC
1156 * @mtd: MTD device structure
1157 * @from: offset to read from
1158 * @ops: oob ops structure
1160 * Internal function. Called with chip held.
1162 static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1163 struct mtd_oob_ops *ops)
1165 int chipnr, page, realpage, col, bytes, aligned;
1166 struct nand_chip *chip = mtd->priv;
1167 struct mtd_ecc_stats stats;
1168 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1171 uint32_t readlen = ops->len;
1172 uint32_t oobreadlen = ops->ooblen;
1173 uint8_t *bufpoi, *oob, *buf;
1175 stats = mtd->ecc_stats;
1177 chipnr = (int)(from >> chip->chip_shift);
1178 chip->select_chip(mtd, chipnr);
1180 realpage = (int)(from >> chip->page_shift);
1181 page = realpage & chip->pagemask;
1183 col = (int)(from & (mtd->writesize - 1));
1189 bytes = min(mtd->writesize - col, readlen);
1190 aligned = (bytes == mtd->writesize);
1192 /* Is the current page in the buffer ? */
1193 if (realpage != chip->pagebuf || oob) {
1194 bufpoi = aligned ? buf : chip->buffers->databuf;
1196 if (likely(sndcmd)) {
1197 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1201 /* Now read the page into the buffer */
1202 if (unlikely(ops->mode == MTD_OOB_RAW))
1203 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi);
1204 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1205 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
1207 ret = chip->ecc.read_page(mtd, chip, bufpoi);
1211 /* Transfer not aligned data */
1213 if (!NAND_SUBPAGE_READ(chip) && !oob)
1214 chip->pagebuf = realpage;
1215 memcpy(buf, chip->buffers->databuf + col, bytes);
1220 if (unlikely(oob)) {
1221 /* Raw mode does data:oob:data:oob */
1222 if (ops->mode != MTD_OOB_RAW) {
1223 int toread = min(oobreadlen,
1224 chip->ecc.layout->oobavail);
1226 oob = nand_transfer_oob(chip,
1228 oobreadlen -= toread;
1231 buf = nand_transfer_oob(chip,
1232 buf, ops, mtd->oobsize);
1235 if (!(chip->options & NAND_NO_READRDY)) {
1237 * Apply delay or wait for ready/busy pin. Do
1238 * this before the AUTOINCR check, so no
1239 * problems arise if a chip which does auto
1240 * increment is marked as NOAUTOINCR by the
1243 if (!chip->dev_ready)
1244 udelay(chip->chip_delay);
1246 nand_wait_ready(mtd);
1249 memcpy(buf, chip->buffers->databuf + col, bytes);
1258 /* For subsequent reads align to page boundary. */
1260 /* Increment page address */
1263 page = realpage & chip->pagemask;
1264 /* Check, if we cross a chip boundary */
1267 chip->select_chip(mtd, -1);
1268 chip->select_chip(mtd, chipnr);
1271 /* Check, if the chip supports auto page increment
1272 * or if we have hit a block boundary.
1274 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1278 ops->retlen = ops->len - (size_t) readlen;
1280 ops->oobretlen = ops->ooblen - oobreadlen;
1285 if (mtd->ecc_stats.failed - stats.failed)
1288 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1292 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1293 * @mtd: MTD device structure
1294 * @from: offset to read from
1295 * @len: number of bytes to read
1296 * @retlen: pointer to variable to store the number of read bytes
1297 * @buf: the databuffer to put data
1299 * Get hold of the chip and call nand_do_read
1301 static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1302 size_t *retlen, uint8_t *buf)
1304 struct nand_chip *chip = mtd->priv;
1307 /* Do not allow reads past end of device */
1308 if ((from + len) > mtd->size)
1313 nand_get_device(chip, mtd, FL_READING);
1315 chip->ops.len = len;
1316 chip->ops.datbuf = buf;
1317 chip->ops.oobbuf = NULL;
1319 ret = nand_do_read_ops(mtd, from, &chip->ops);
1321 *retlen = chip->ops.retlen;
1323 nand_release_device(mtd);
1329 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1330 * @mtd: mtd info structure
1331 * @chip: nand chip info structure
1332 * @page: page number to read
1333 * @sndcmd: flag whether to issue read command or not
1335 static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1336 int page, int sndcmd)
1339 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1342 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1347 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1349 * @mtd: mtd info structure
1350 * @chip: nand chip info structure
1351 * @page: page number to read
1352 * @sndcmd: flag whether to issue read command or not
1354 static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1355 int page, int sndcmd)
1357 uint8_t *buf = chip->oob_poi;
1358 int length = mtd->oobsize;
1359 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1360 int eccsize = chip->ecc.size;
1361 uint8_t *bufpoi = buf;
1362 int i, toread, sndrnd = 0, pos;
1364 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1365 for (i = 0; i < chip->ecc.steps; i++) {
1367 pos = eccsize + i * (eccsize + chunk);
1368 if (mtd->writesize > 512)
1369 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1371 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1374 toread = min_t(int, length, chunk);
1375 chip->read_buf(mtd, bufpoi, toread);
1380 chip->read_buf(mtd, bufpoi, length);
1386 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1387 * @mtd: mtd info structure
1388 * @chip: nand chip info structure
1389 * @page: page number to write
1391 static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1395 const uint8_t *buf = chip->oob_poi;
1396 int length = mtd->oobsize;
1398 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1399 chip->write_buf(mtd, buf, length);
1400 /* Send command to program the OOB data */
1401 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1403 status = chip->waitfunc(mtd, chip);
1405 return status & NAND_STATUS_FAIL ? -EIO : 0;
1409 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1410 * with syndrome - only for large page flash !
1411 * @mtd: mtd info structure
1412 * @chip: nand chip info structure
1413 * @page: page number to write
1415 static int nand_write_oob_syndrome(struct mtd_info *mtd,
1416 struct nand_chip *chip, int page)
1418 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1419 int eccsize = chip->ecc.size, length = mtd->oobsize;
1420 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1421 const uint8_t *bufpoi = chip->oob_poi;
1424 * data-ecc-data-ecc ... ecc-oob
1426 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1428 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1429 pos = steps * (eccsize + chunk);
1434 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1435 for (i = 0; i < steps; i++) {
1437 if (mtd->writesize <= 512) {
1438 uint32_t fill = 0xFFFFFFFF;
1442 int num = min_t(int, len, 4);
1443 chip->write_buf(mtd, (uint8_t *)&fill,
1448 pos = eccsize + i * (eccsize + chunk);
1449 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1453 len = min_t(int, length, chunk);
1454 chip->write_buf(mtd, bufpoi, len);
1459 chip->write_buf(mtd, bufpoi, length);
1461 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1462 status = chip->waitfunc(mtd, chip);
1464 return status & NAND_STATUS_FAIL ? -EIO : 0;
1468 * nand_do_read_oob - [Intern] NAND read out-of-band
1469 * @mtd: MTD device structure
1470 * @from: offset to read from
1471 * @ops: oob operations description structure
1473 * NAND read out-of-band data from the spare area
1475 static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1476 struct mtd_oob_ops *ops)
1478 int page, realpage, chipnr, sndcmd = 1;
1479 struct nand_chip *chip = mtd->priv;
1480 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1481 int readlen = ops->ooblen;
1483 uint8_t *buf = ops->oobbuf;
1485 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1486 (unsigned long long)from, readlen);
1488 if (ops->mode == MTD_OOB_AUTO)
1489 len = chip->ecc.layout->oobavail;
1493 if (unlikely(ops->ooboffs >= len)) {
1494 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1495 "Attempt to start read outside oob\n");
1499 /* Do not allow reads past end of device */
1500 if (unlikely(from >= mtd->size ||
1501 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1502 (from >> chip->page_shift)) * len)) {
1503 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1504 "Attempt read beyond end of device\n");
1508 chipnr = (int)(from >> chip->chip_shift);
1509 chip->select_chip(mtd, chipnr);
1511 /* Shift to get page */
1512 realpage = (int)(from >> chip->page_shift);
1513 page = realpage & chip->pagemask;
1516 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
1518 len = min(len, readlen);
1519 buf = nand_transfer_oob(chip, buf, ops, len);
1521 if (!(chip->options & NAND_NO_READRDY)) {
1523 * Apply delay or wait for ready/busy pin. Do this
1524 * before the AUTOINCR check, so no problems arise if a
1525 * chip which does auto increment is marked as
1526 * NOAUTOINCR by the board driver.
1528 if (!chip->dev_ready)
1529 udelay(chip->chip_delay);
1531 nand_wait_ready(mtd);
1538 /* Increment page address */
1541 page = realpage & chip->pagemask;
1542 /* Check, if we cross a chip boundary */
1545 chip->select_chip(mtd, -1);
1546 chip->select_chip(mtd, chipnr);
1549 /* Check, if the chip supports auto page increment
1550 * or if we have hit a block boundary.
1552 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1556 ops->oobretlen = ops->ooblen;
1561 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
1562 * @mtd: MTD device structure
1563 * @from: offset to read from
1564 * @ops: oob operation description structure
1566 * NAND read data and/or out-of-band data
1568 static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1569 struct mtd_oob_ops *ops)
1571 struct nand_chip *chip = mtd->priv;
1572 int ret = -ENOTSUPP;
1576 /* Do not allow reads past end of device */
1577 if (ops->datbuf && (from + ops->len) > mtd->size) {
1578 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1579 "Attempt read beyond end of device\n");
1583 nand_get_device(chip, mtd, FL_READING);
1596 ret = nand_do_read_oob(mtd, from, ops);
1598 ret = nand_do_read_ops(mtd, from, ops);
1601 nand_release_device(mtd);
1607 * nand_write_page_raw - [Intern] raw page write function
1608 * @mtd: mtd info structure
1609 * @chip: nand chip info structure
1612 static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1615 chip->write_buf(mtd, buf, mtd->writesize);
1616 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1620 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
1621 * @mtd: mtd info structure
1622 * @chip: nand chip info structure
1625 static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1628 int i, eccsize = chip->ecc.size;
1629 int eccbytes = chip->ecc.bytes;
1630 int eccsteps = chip->ecc.steps;
1631 uint8_t *ecc_calc = chip->buffers->ecccalc;
1632 const uint8_t *p = buf;
1633 uint32_t *eccpos = chip->ecc.layout->eccpos;
1635 /* Software ecc calculation */
1636 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1637 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1639 for (i = 0; i < chip->ecc.total; i++)
1640 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1642 chip->ecc.write_page_raw(mtd, chip, buf);
1646 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
1647 * @mtd: mtd info structure
1648 * @chip: nand chip info structure
1651 static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1654 int i, eccsize = chip->ecc.size;
1655 int eccbytes = chip->ecc.bytes;
1656 int eccsteps = chip->ecc.steps;
1657 uint8_t *ecc_calc = chip->buffers->ecccalc;
1658 const uint8_t *p = buf;
1659 uint32_t *eccpos = chip->ecc.layout->eccpos;
1661 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1662 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1663 chip->write_buf(mtd, p, eccsize);
1664 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1667 for (i = 0; i < chip->ecc.total; i++)
1668 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1670 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1674 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
1675 * @mtd: mtd info structure
1676 * @chip: nand chip info structure
1679 * The hw generator calculates the error syndrome automatically. Therefor
1680 * we need a special oob layout and handling.
1682 static void nand_write_page_syndrome(struct mtd_info *mtd,
1683 struct nand_chip *chip, const uint8_t *buf)
1685 int i, eccsize = chip->ecc.size;
1686 int eccbytes = chip->ecc.bytes;
1687 int eccsteps = chip->ecc.steps;
1688 const uint8_t *p = buf;
1689 uint8_t *oob = chip->oob_poi;
1691 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1693 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1694 chip->write_buf(mtd, p, eccsize);
1696 if (chip->ecc.prepad) {
1697 chip->write_buf(mtd, oob, chip->ecc.prepad);
1698 oob += chip->ecc.prepad;
1701 chip->ecc.calculate(mtd, p, oob);
1702 chip->write_buf(mtd, oob, eccbytes);
1705 if (chip->ecc.postpad) {
1706 chip->write_buf(mtd, oob, chip->ecc.postpad);
1707 oob += chip->ecc.postpad;
1711 /* Calculate remaining oob bytes */
1712 i = mtd->oobsize - (oob - chip->oob_poi);
1714 chip->write_buf(mtd, oob, i);
1718 * nand_write_page - [REPLACEABLE] write one page
1719 * @mtd: MTD device structure
1720 * @chip: NAND chip descriptor
1721 * @buf: the data to write
1722 * @page: page number to write
1723 * @cached: cached programming
1724 * @raw: use _raw version of write_page
1726 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1727 const uint8_t *buf, int page, int cached, int raw)
1731 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1734 chip->ecc.write_page_raw(mtd, chip, buf);
1736 chip->ecc.write_page(mtd, chip, buf);
1739 * Cached progamming disabled for now, Not sure if its worth the
1740 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1744 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1746 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1747 status = chip->waitfunc(mtd, chip);
1749 * See if operation failed and additional status checks are
1752 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1753 status = chip->errstat(mtd, chip, FL_WRITING, status,
1756 if (status & NAND_STATUS_FAIL)
1759 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1760 status = chip->waitfunc(mtd, chip);
1763 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1764 /* Send command to read back the data */
1765 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1767 if (chip->verify_buf(mtd, buf, mtd->writesize))
1774 * nand_fill_oob - [Internal] Transfer client buffer to oob
1775 * @chip: nand chip structure
1776 * @oob: oob data buffer
1777 * @ops: oob ops structure
1779 static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1780 struct mtd_oob_ops *ops)
1782 size_t len = ops->ooblen;
1788 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1791 case MTD_OOB_AUTO: {
1792 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1793 uint32_t boffs = 0, woffs = ops->ooboffs;
1796 for(; free->length && len; free++, len -= bytes) {
1797 /* Write request not from offset 0 ? */
1798 if (unlikely(woffs)) {
1799 if (woffs >= free->length) {
1800 woffs -= free->length;
1803 boffs = free->offset + woffs;
1804 bytes = min_t(size_t, len,
1805 (free->length - woffs));
1808 bytes = min_t(size_t, len, free->length);
1809 boffs = free->offset;
1811 memcpy(chip->oob_poi + boffs, oob, bytes);
1822 #define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
1825 * nand_do_write_ops - [Internal] NAND write with ECC
1826 * @mtd: MTD device structure
1827 * @to: offset to write to
1828 * @ops: oob operations description structure
1830 * NAND write with ECC
1832 static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1833 struct mtd_oob_ops *ops)
1835 int chipnr, realpage, page, blockmask, column;
1836 struct nand_chip *chip = mtd->priv;
1837 uint32_t writelen = ops->len;
1838 uint8_t *oob = ops->oobbuf;
1839 uint8_t *buf = ops->datbuf;
1846 /* reject writes, which are not page aligned */
1847 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
1848 printk(KERN_NOTICE "nand_write: "
1849 "Attempt to write not page aligned data\n");
1853 column = to & (mtd->writesize - 1);
1854 subpage = column || (writelen & (mtd->writesize - 1));
1859 chipnr = (int)(to >> chip->chip_shift);
1860 chip->select_chip(mtd, chipnr);
1862 /* Check, if it is write protected */
1863 if (nand_check_wp(mtd)) {
1864 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1868 realpage = (int)(to >> chip->page_shift);
1869 page = realpage & chip->pagemask;
1870 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1872 /* Invalidate the page cache, when we write to the cached page */
1873 if (to <= (chip->pagebuf << chip->page_shift) &&
1874 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1877 /* If we're not given explicit OOB data, let it be 0xFF */
1879 memset(chip->oob_poi, 0xff, mtd->oobsize);
1882 int bytes = mtd->writesize;
1883 int cached = writelen > bytes && page != blockmask;
1884 uint8_t *wbuf = buf;
1886 /* Partial page write ? */
1887 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1889 bytes = min_t(int, bytes - column, (int) writelen);
1891 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1892 memcpy(&chip->buffers->databuf[column], buf, bytes);
1893 wbuf = chip->buffers->databuf;
1897 oob = nand_fill_oob(chip, oob, ops);
1899 ret = chip->write_page(mtd, chip, wbuf, page, cached,
1900 (ops->mode == MTD_OOB_RAW));
1912 page = realpage & chip->pagemask;
1913 /* Check, if we cross a chip boundary */
1916 chip->select_chip(mtd, -1);
1917 chip->select_chip(mtd, chipnr);
1921 ops->retlen = ops->len - writelen;
1923 ops->oobretlen = ops->ooblen;
1928 * nand_write - [MTD Interface] NAND write with ECC
1929 * @mtd: MTD device structure
1930 * @to: offset to write to
1931 * @len: number of bytes to write
1932 * @retlen: pointer to variable to store the number of written bytes
1933 * @buf: the data to write
1935 * NAND write with ECC
1937 static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
1938 size_t *retlen, const uint8_t *buf)
1940 struct nand_chip *chip = mtd->priv;
1943 /* Do not allow reads past end of device */
1944 if ((to + len) > mtd->size)
1949 nand_get_device(chip, mtd, FL_WRITING);
1951 chip->ops.len = len;
1952 chip->ops.datbuf = (uint8_t *)buf;
1953 chip->ops.oobbuf = NULL;
1955 ret = nand_do_write_ops(mtd, to, &chip->ops);
1957 *retlen = chip->ops.retlen;
1959 nand_release_device(mtd);
1965 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1966 * @mtd: MTD device structure
1967 * @to: offset to write to
1968 * @ops: oob operation description structure
1970 * NAND write out-of-band
1972 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1973 struct mtd_oob_ops *ops)
1975 int chipnr, page, status, len;
1976 struct nand_chip *chip = mtd->priv;
1978 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
1979 (unsigned int)to, (int)ops->ooblen);
1981 if (ops->mode == MTD_OOB_AUTO)
1982 len = chip->ecc.layout->oobavail;
1986 /* Do not allow write past end of page */
1987 if ((ops->ooboffs + ops->ooblen) > len) {
1988 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
1989 "Attempt to write past end of page\n");
1993 if (unlikely(ops->ooboffs >= len)) {
1994 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1995 "Attempt to start write outside oob\n");
1999 /* Do not allow reads past end of device */
2000 if (unlikely(to >= mtd->size ||
2001 ops->ooboffs + ops->ooblen >
2002 ((mtd->size >> chip->page_shift) -
2003 (to >> chip->page_shift)) * len)) {
2004 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2005 "Attempt write beyond end of device\n");
2009 chipnr = (int)(to >> chip->chip_shift);
2010 chip->select_chip(mtd, chipnr);
2012 /* Shift to get page */
2013 page = (int)(to >> chip->page_shift);
2016 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2017 * of my DiskOnChip 2000 test units) will clear the whole data page too
2018 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2019 * it in the doc2000 driver in August 1999. dwmw2.
2021 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2023 /* Check, if it is write protected */
2024 if (nand_check_wp(mtd))
2027 /* Invalidate the page cache, if we write to the cached page */
2028 if (page == chip->pagebuf)
2031 memset(chip->oob_poi, 0xff, mtd->oobsize);
2032 nand_fill_oob(chip, ops->oobbuf, ops);
2033 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2034 memset(chip->oob_poi, 0xff, mtd->oobsize);
2039 ops->oobretlen = ops->ooblen;
2045 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2046 * @mtd: MTD device structure
2047 * @to: offset to write to
2048 * @ops: oob operation description structure
2050 static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2051 struct mtd_oob_ops *ops)
2053 struct nand_chip *chip = mtd->priv;
2054 int ret = -ENOTSUPP;
2058 /* Do not allow writes past end of device */
2059 if (ops->datbuf && (to + ops->len) > mtd->size) {
2060 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2061 "Attempt read beyond end of device\n");
2065 nand_get_device(chip, mtd, FL_WRITING);
2078 ret = nand_do_write_oob(mtd, to, ops);
2080 ret = nand_do_write_ops(mtd, to, ops);
2083 nand_release_device(mtd);
2088 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2089 * @mtd: MTD device structure
2090 * @page: the page address of the block which will be erased
2092 * Standard erase command for NAND chips
2094 static void single_erase_cmd(struct mtd_info *mtd, int page)
2096 struct nand_chip *chip = mtd->priv;
2097 /* Send commands to erase a block */
2098 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2099 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2103 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2104 * @mtd: MTD device structure
2105 * @page: the page address of the block which will be erased
2107 * AND multi block erase command function
2108 * Erase 4 consecutive blocks
2110 static void multi_erase_cmd(struct mtd_info *mtd, int page)
2112 struct nand_chip *chip = mtd->priv;
2113 /* Send commands to erase a block */
2114 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2115 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2116 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2117 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2118 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2122 * nand_erase - [MTD Interface] erase block(s)
2123 * @mtd: MTD device structure
2124 * @instr: erase instruction
2126 * Erase one ore more blocks
2128 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
2130 return nand_erase_nand(mtd, instr, 0);
2133 #define BBT_PAGE_MASK 0xffffff3f
2135 * nand_erase_nand - [Internal] erase block(s)
2136 * @mtd: MTD device structure
2137 * @instr: erase instruction
2138 * @allowbbt: allow erasing the bbt area
2140 * Erase one ore more blocks
2142 int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2145 int page, len, status, pages_per_block, ret, chipnr;
2146 struct nand_chip *chip = mtd->priv;
2147 int rewrite_bbt[NAND_MAX_CHIPS]={0};
2148 unsigned int bbt_masked_page = 0xffffffff;
2150 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
2151 (unsigned int) instr->addr, (unsigned int) instr->len);
2153 /* Start address must align on block boundary */
2154 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
2155 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2159 /* Length must align on block boundary */
2160 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
2161 MTDDEBUG (MTD_DEBUG_LEVEL0,
2162 "nand_erase: Length not block aligned\n");
2166 /* Do not allow erase past end of device */
2167 if ((instr->len + instr->addr) > mtd->size) {
2168 MTDDEBUG (MTD_DEBUG_LEVEL0,
2169 "nand_erase: Erase past end of device\n");
2173 instr->fail_addr = 0xffffffff;
2175 /* Grab the lock and see if the device is available */
2176 nand_get_device(chip, mtd, FL_ERASING);
2178 /* Shift to get first page */
2179 page = (int)(instr->addr >> chip->page_shift);
2180 chipnr = (int)(instr->addr >> chip->chip_shift);
2182 /* Calculate pages in each block */
2183 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
2185 /* Select the NAND device */
2186 chip->select_chip(mtd, chipnr);
2188 /* Check, if it is write protected */
2189 if (nand_check_wp(mtd)) {
2190 MTDDEBUG (MTD_DEBUG_LEVEL0,
2191 "nand_erase: Device is write protected!!!\n");
2192 instr->state = MTD_ERASE_FAILED;
2197 * If BBT requires refresh, set the BBT page mask to see if the BBT
2198 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2199 * can not be matched. This is also done when the bbt is actually
2200 * erased to avoid recusrsive updates
2202 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2203 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2205 /* Loop through the pages */
2208 instr->state = MTD_ERASING;
2212 * heck if we have a bad block, we do not erase bad blocks !
2214 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2215 chip->page_shift, 0, allowbbt)) {
2216 printk(KERN_WARNING "nand_erase: attempt to erase a "
2217 "bad block at page 0x%08x\n", page);
2218 instr->state = MTD_ERASE_FAILED;
2223 * Invalidate the page cache, if we erase the block which
2224 * contains the current cached page
2226 if (page <= chip->pagebuf && chip->pagebuf <
2227 (page + pages_per_block))
2230 chip->erase_cmd(mtd, page & chip->pagemask);
2232 status = chip->waitfunc(mtd, chip);
2235 * See if operation failed and additional status checks are
2238 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2239 status = chip->errstat(mtd, chip, FL_ERASING,
2242 /* See if block erase succeeded */
2243 if (status & NAND_STATUS_FAIL) {
2244 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: "
2245 "Failed erase, page 0x%08x\n", page);
2246 instr->state = MTD_ERASE_FAILED;
2247 instr->fail_addr = (page << chip->page_shift);
2252 * If BBT requires refresh, set the BBT rewrite flag to the
2255 if (bbt_masked_page != 0xffffffff &&
2256 (page & BBT_PAGE_MASK) == bbt_masked_page)
2257 rewrite_bbt[chipnr] = (page << chip->page_shift);
2259 /* Increment page address and decrement length */
2260 len -= (1 << chip->phys_erase_shift);
2261 page += pages_per_block;
2263 /* Check, if we cross a chip boundary */
2264 if (len && !(page & chip->pagemask)) {
2266 chip->select_chip(mtd, -1);
2267 chip->select_chip(mtd, chipnr);
2270 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2271 * page mask to see if this BBT should be rewritten
2273 if (bbt_masked_page != 0xffffffff &&
2274 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2275 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2279 instr->state = MTD_ERASE_DONE;
2283 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2285 /* Deselect and wake up anyone waiting on the device */
2286 nand_release_device(mtd);
2288 /* Do call back function */
2290 mtd_erase_callback(instr);
2293 * If BBT requires refresh and erase was successful, rewrite any
2294 * selected bad block tables
2296 if (bbt_masked_page == 0xffffffff || ret)
2299 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2300 if (!rewrite_bbt[chipnr])
2302 /* update the BBT for chip */
2303 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
2304 "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2305 chip->bbt_td->pages[chipnr]);
2306 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2309 /* Return more or less happy */
2314 * nand_sync - [MTD Interface] sync
2315 * @mtd: MTD device structure
2317 * Sync is actually a wait for chip ready function
2319 static void nand_sync(struct mtd_info *mtd)
2321 struct nand_chip *chip = mtd->priv;
2323 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2325 /* Grab the lock and see if the device is available */
2326 nand_get_device(chip, mtd, FL_SYNCING);
2327 /* Release it and go back */
2328 nand_release_device(mtd);
2332 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
2333 * @mtd: MTD device structure
2334 * @offs: offset relative to mtd start
2336 static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
2338 /* Check for invalid offset */
2339 if (offs > mtd->size)
2342 return nand_block_checkbad(mtd, offs, 1, 0);
2346 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
2347 * @mtd: MTD device structure
2348 * @ofs: offset relative to mtd start
2350 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2352 struct nand_chip *chip = mtd->priv;
2355 if ((ret = nand_block_isbad(mtd, ofs))) {
2356 /* If it was bad already, return success and do nothing. */
2362 return chip->block_markbad(mtd, ofs);
2366 * nand_suspend - [MTD Interface] Suspend the NAND flash
2367 * @mtd: MTD device structure
2369 static int nand_suspend(struct mtd_info *mtd)
2371 struct nand_chip *chip = mtd->priv;
2373 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
2377 * nand_resume - [MTD Interface] Resume the NAND flash
2378 * @mtd: MTD device structure
2380 static void nand_resume(struct mtd_info *mtd)
2382 struct nand_chip *chip = mtd->priv;
2384 if (chip->state == FL_PM_SUSPENDED)
2385 nand_release_device(mtd);
2387 printk(KERN_ERR "nand_resume() called for a chip which is not "
2388 "in suspended state\n");
2392 * Set default functions
2394 static void nand_set_defaults(struct nand_chip *chip, int busw)
2396 /* check for proper chip_delay setup, set 20us if not */
2397 if (!chip->chip_delay)
2398 chip->chip_delay = 20;
2400 /* check, if a user supplied command function given */
2401 if (chip->cmdfunc == NULL)
2402 chip->cmdfunc = nand_command;
2404 /* check, if a user supplied wait function given */
2405 if (chip->waitfunc == NULL)
2406 chip->waitfunc = nand_wait;
2408 if (!chip->select_chip)
2409 chip->select_chip = nand_select_chip;
2410 if (!chip->read_byte)
2411 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2412 if (!chip->read_word)
2413 chip->read_word = nand_read_word;
2414 if (!chip->block_bad)
2415 chip->block_bad = nand_block_bad;
2416 if (!chip->block_markbad)
2417 chip->block_markbad = nand_default_block_markbad;
2418 if (!chip->write_buf)
2419 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2420 if (!chip->read_buf)
2421 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2422 if (!chip->verify_buf)
2423 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2424 if (!chip->scan_bbt)
2425 chip->scan_bbt = nand_default_bbt;
2427 if (!chip->controller) {
2428 chip->controller = &chip->hwcontrol;
2430 /* XXX U-BOOT XXX */
2432 spin_lock_init(&chip->controller->lock);
2433 init_waitqueue_head(&chip->controller->wq);
2440 * Get the flash and manufacturer id and lookup if the type is supported
2442 static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2443 struct nand_chip *chip,
2444 int busw, int *maf_id)
2446 struct nand_flash_dev *type = NULL;
2447 int i, dev_id, maf_idx;
2448 int tmp_id, tmp_manf;
2450 /* Select the device */
2451 chip->select_chip(mtd, 0);
2454 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2457 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2459 /* Send the command for reading device ID */
2460 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2462 /* Read manufacturer and device IDs */
2463 *maf_id = chip->read_byte(mtd);
2464 dev_id = chip->read_byte(mtd);
2466 /* Try again to make sure, as some systems the bus-hold or other
2467 * interface concerns can cause random data which looks like a
2468 * possibly credible NAND flash to appear. If the two results do
2469 * not match, ignore the device completely.
2472 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2474 /* Read manufacturer and device IDs */
2476 tmp_manf = chip->read_byte(mtd);
2477 tmp_id = chip->read_byte(mtd);
2479 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2480 printk(KERN_INFO "%s: second ID read did not match "
2481 "%02x,%02x against %02x,%02x\n", __func__,
2482 *maf_id, dev_id, tmp_manf, tmp_id);
2483 return ERR_PTR(-ENODEV);
2486 /* Lookup the flash id */
2487 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2488 if (dev_id == nand_flash_ids[i].id) {
2489 type = &nand_flash_ids[i];
2495 return ERR_PTR(-ENODEV);
2498 mtd->name = type->name;
2500 chip->chipsize = type->chipsize << 20;
2502 /* Newer devices have all the information in additional id bytes */
2503 if (!type->pagesize) {
2505 /* The 3rd id byte holds MLC / multichip data */
2506 chip->cellinfo = chip->read_byte(mtd);
2507 /* The 4th id byte is the important one */
2508 extid = chip->read_byte(mtd);
2510 mtd->writesize = 1024 << (extid & 0x3);
2513 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
2515 /* Calc blocksize. Blocksize is multiples of 64KiB */
2516 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2518 /* Get buswidth information */
2519 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2523 * Old devices have chip data hardcoded in the device id table
2525 mtd->erasesize = type->erasesize;
2526 mtd->writesize = type->pagesize;
2527 mtd->oobsize = mtd->writesize / 32;
2528 busw = type->options & NAND_BUSWIDTH_16;
2531 /* Try to identify manufacturer */
2532 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2533 if (nand_manuf_ids[maf_idx].id == *maf_id)
2538 * Check, if buswidth is correct. Hardware drivers should set
2541 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2542 printk(KERN_INFO "NAND device: Manufacturer ID:"
2543 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2544 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2545 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
2546 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2548 return ERR_PTR(-EINVAL);
2551 /* Calculate the address shift from the page size */
2552 chip->page_shift = ffs(mtd->writesize) - 1;
2553 /* Convert chipsize to number of pages per chip -1. */
2554 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
2556 chip->bbt_erase_shift = chip->phys_erase_shift =
2557 ffs(mtd->erasesize) - 1;
2558 chip->chip_shift = ffs(chip->chipsize) - 1;
2560 /* Set the bad block position */
2561 chip->badblockpos = mtd->writesize > 512 ?
2562 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2564 /* Get chip options, preserve non chip based options */
2565 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2566 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
2569 * Set chip as a default. Board drivers can override it, if necessary
2571 chip->options |= NAND_NO_AUTOINCR;
2573 /* Check if chip is a not a samsung device. Do not clear the
2574 * options for chips which are not having an extended id.
2576 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2577 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2579 /* Check for AND chips with 4 page planes */
2580 if (chip->options & NAND_4PAGE_ARRAY)
2581 chip->erase_cmd = multi_erase_cmd;
2583 chip->erase_cmd = single_erase_cmd;
2585 /* Do not replace user supplied command function ! */
2586 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2587 chip->cmdfunc = nand_command_lp;
2589 MTDDEBUG (MTD_DEBUG_LEVEL0, "NAND device: Manufacturer ID:"
2590 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2591 nand_manuf_ids[maf_idx].name, type->name);
2597 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2598 * @mtd: MTD device structure
2599 * @maxchips: Number of chips to scan for
2601 * This is the first phase of the normal nand_scan() function. It
2602 * reads the flash ID and sets up MTD fields accordingly.
2604 * The mtd->owner field must be set to the module of the caller.
2606 int nand_scan_ident(struct mtd_info *mtd, int maxchips)
2608 int i, busw, nand_maf_id;
2609 struct nand_chip *chip = mtd->priv;
2610 struct nand_flash_dev *type;
2612 /* Get buswidth to select the correct functions */
2613 busw = chip->options & NAND_BUSWIDTH_16;
2614 /* Set the default functions */
2615 nand_set_defaults(chip, busw);
2617 /* Read the flash type */
2618 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
2621 printk(KERN_WARNING "No NAND device found!!!\n");
2622 chip->select_chip(mtd, -1);
2623 return PTR_ERR(type);
2626 /* Check for a chip array */
2627 for (i = 1; i < maxchips; i++) {
2628 chip->select_chip(mtd, i);
2629 /* See comment in nand_get_flash_type for reset */
2630 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2631 /* Send the command for reading device ID */
2632 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2633 /* Read manufacturer and device IDs */
2634 if (nand_maf_id != chip->read_byte(mtd) ||
2635 type->id != chip->read_byte(mtd))
2639 printk(KERN_INFO "%d NAND chips detected\n", i);
2641 /* Store the number of chips and calc total size for mtd */
2643 mtd->size = i * chip->chipsize;
2650 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2651 * @mtd: MTD device structure
2652 * @maxchips: Number of chips to scan for
2654 * This is the second phase of the normal nand_scan() function. It
2655 * fills out all the uninitialized function pointers with the defaults
2656 * and scans for a bad block table if appropriate.
2658 int nand_scan_tail(struct mtd_info *mtd)
2661 struct nand_chip *chip = mtd->priv;
2663 if (!(chip->options & NAND_OWN_BUFFERS))
2664 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2668 /* Set the internal oob buffer location, just after the page data */
2669 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
2672 * If no default placement scheme is given, select an appropriate one
2674 if (!chip->ecc.layout) {
2675 switch (mtd->oobsize) {
2677 chip->ecc.layout = &nand_oob_8;
2680 chip->ecc.layout = &nand_oob_16;
2683 chip->ecc.layout = &nand_oob_64;
2686 chip->ecc.layout = &nand_oob_128;
2689 printk(KERN_WARNING "No oob scheme defined for "
2690 "oobsize %d\n", mtd->oobsize);
2695 if (!chip->write_page)
2696 chip->write_page = nand_write_page;
2699 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2700 * selected and we have 256 byte pagesize fallback to software ECC
2702 if (!chip->ecc.read_page_raw)
2703 chip->ecc.read_page_raw = nand_read_page_raw;
2704 if (!chip->ecc.write_page_raw)
2705 chip->ecc.write_page_raw = nand_write_page_raw;
2707 switch (chip->ecc.mode) {
2709 /* Use standard hwecc read page function ? */
2710 if (!chip->ecc.read_page)
2711 chip->ecc.read_page = nand_read_page_hwecc;
2712 if (!chip->ecc.write_page)
2713 chip->ecc.write_page = nand_write_page_hwecc;
2714 if (!chip->ecc.read_oob)
2715 chip->ecc.read_oob = nand_read_oob_std;
2716 if (!chip->ecc.write_oob)
2717 chip->ecc.write_oob = nand_write_oob_std;
2719 case NAND_ECC_HW_SYNDROME:
2720 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2721 !chip->ecc.hwctl) &&
2722 (!chip->ecc.read_page ||
2723 chip->ecc.read_page == nand_read_page_hwecc ||
2724 !chip->ecc.write_page ||
2725 chip->ecc.write_page == nand_write_page_hwecc)) {
2726 printk(KERN_WARNING "No ECC functions supplied, "
2727 "Hardware ECC not possible\n");
2730 /* Use standard syndrome read/write page function ? */
2731 if (!chip->ecc.read_page)
2732 chip->ecc.read_page = nand_read_page_syndrome;
2733 if (!chip->ecc.write_page)
2734 chip->ecc.write_page = nand_write_page_syndrome;
2735 if (!chip->ecc.read_oob)
2736 chip->ecc.read_oob = nand_read_oob_syndrome;
2737 if (!chip->ecc.write_oob)
2738 chip->ecc.write_oob = nand_write_oob_syndrome;
2740 if (mtd->writesize >= chip->ecc.size)
2742 printk(KERN_WARNING "%d byte HW ECC not possible on "
2743 "%d byte page size, fallback to SW ECC\n",
2744 chip->ecc.size, mtd->writesize);
2745 chip->ecc.mode = NAND_ECC_SOFT;
2748 chip->ecc.calculate = nand_calculate_ecc;
2749 chip->ecc.correct = nand_correct_data;
2750 chip->ecc.read_page = nand_read_page_swecc;
2751 chip->ecc.read_subpage = nand_read_subpage;
2752 chip->ecc.write_page = nand_write_page_swecc;
2753 chip->ecc.read_oob = nand_read_oob_std;
2754 chip->ecc.write_oob = nand_write_oob_std;
2755 chip->ecc.size = 256;
2756 chip->ecc.bytes = 3;
2760 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2761 "This is not recommended !!\n");
2762 chip->ecc.read_page = nand_read_page_raw;
2763 chip->ecc.write_page = nand_write_page_raw;
2764 chip->ecc.read_oob = nand_read_oob_std;
2765 chip->ecc.write_oob = nand_write_oob_std;
2766 chip->ecc.size = mtd->writesize;
2767 chip->ecc.bytes = 0;
2771 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
2777 * The number of bytes available for a client to place data into
2778 * the out of band area
2780 chip->ecc.layout->oobavail = 0;
2781 for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
2782 chip->ecc.layout->oobavail +=
2783 chip->ecc.layout->oobfree[i].length;
2784 mtd->oobavail = chip->ecc.layout->oobavail;
2787 * Set the number of read / write steps for one page depending on ECC
2790 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2791 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
2792 printk(KERN_WARNING "Invalid ecc parameters\n");
2795 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
2798 * Allow subpage writes up to ecc.steps. Not possible for MLC
2801 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2802 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2803 switch(chip->ecc.steps) {
2805 mtd->subpage_sft = 1;
2809 mtd->subpage_sft = 2;
2813 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
2815 /* Initialize state */
2816 chip->state = FL_READY;
2818 /* De-select the device */
2819 chip->select_chip(mtd, -1);
2821 /* Invalidate the pagebuffer reference */
2824 /* Fill in remaining MTD driver data */
2825 mtd->type = MTD_NANDFLASH;
2826 mtd->flags = MTD_CAP_NANDFLASH;
2827 mtd->erase = nand_erase;
2829 mtd->unpoint = NULL;
2830 mtd->read = nand_read;
2831 mtd->write = nand_write;
2832 mtd->read_oob = nand_read_oob;
2833 mtd->write_oob = nand_write_oob;
2834 mtd->sync = nand_sync;
2837 mtd->suspend = nand_suspend;
2838 mtd->resume = nand_resume;
2839 mtd->block_isbad = nand_block_isbad;
2840 mtd->block_markbad = nand_block_markbad;
2842 /* propagate ecc.layout to mtd_info */
2843 mtd->ecclayout = chip->ecc.layout;
2845 /* Check, if we should skip the bad block table scan */
2846 if (chip->options & NAND_SKIP_BBTSCAN)
2847 chip->options |= NAND_BBT_SCANNED;
2852 /* module_text_address() isn't exported, and it's mostly a pointless
2853 test if this is a module _anyway_ -- they'd have to try _really_ hard
2854 to call us from in-kernel code if the core NAND support is modular. */
2856 #define caller_is_module() (1)
2858 #define caller_is_module() \
2859 module_text_address((unsigned long)__builtin_return_address(0))
2863 * nand_scan - [NAND Interface] Scan for the NAND device
2864 * @mtd: MTD device structure
2865 * @maxchips: Number of chips to scan for
2867 * This fills out all the uninitialized function pointers
2868 * with the defaults.
2869 * The flash ID is read and the mtd/chip structures are
2870 * filled with the appropriate values.
2871 * The mtd->owner field must be set to the module of the caller
2874 int nand_scan(struct mtd_info *mtd, int maxchips)
2878 /* Many callers got this wrong, so check for it for a while... */
2879 /* XXX U-BOOT XXX */
2881 if (!mtd->owner && caller_is_module()) {
2882 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2887 ret = nand_scan_ident(mtd, maxchips);
2889 ret = nand_scan_tail(mtd);
2894 * nand_release - [NAND Interface] Free resources held by the NAND device
2895 * @mtd: MTD device structure
2897 void nand_release(struct mtd_info *mtd)
2899 struct nand_chip *chip = mtd->priv;
2901 #ifdef CONFIG_MTD_PARTITIONS
2902 /* Deregister partitions */
2903 del_mtd_partitions(mtd);
2905 /* Deregister the device */
2906 /* XXX U-BOOT XXX */
2908 del_mtd_device(mtd);
2911 /* Free bad block table memory */
2913 if (!(chip->options & NAND_OWN_BUFFERS))
2914 kfree(chip->buffers);
2917 /* XXX U-BOOT XXX */
2919 EXPORT_SYMBOL_GPL(nand_scan);
2920 EXPORT_SYMBOL_GPL(nand_scan_ident);
2921 EXPORT_SYMBOL_GPL(nand_scan_tail);
2922 EXPORT_SYMBOL_GPL(nand_release);
2924 static int __init nand_base_init(void)
2926 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2930 static void __exit nand_base_exit(void)
2932 led_trigger_unregister_simple(nand_led_trigger);
2935 module_init(nand_base_init);
2936 module_exit(nand_base_exit);
2938 MODULE_LICENSE("GPL");
2939 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2940 MODULE_DESCRIPTION("Generic NAND flash driver code");