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.
34 #include <linux/module.h>
35 #include <linux/delay.h>
36 #include <linux/errno.h>
37 #include <linux/err.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/types.h>
41 #include <linux/mtd/mtd.h>
42 #include <linux/mtd/nand.h>
43 #include <linux/mtd/nand_ecc.h>
44 #include <linux/mtd/compatmac.h>
45 #include <linux/interrupt.h>
46 #include <linux/bitops.h>
47 #include <linux/leds.h>
50 #ifdef CONFIG_MTD_PARTITIONS
51 #include <linux/mtd/partitions.h>
54 /* Define default oob placement schemes for large and small page devices */
55 static struct nand_oobinfo nand_oob_8 = {
56 .useecc = MTD_NANDECC_AUTOPLACE,
59 .oobfree = {{3, 2}, {6, 2}}
62 static struct nand_oobinfo nand_oob_16 = {
63 .useecc = MTD_NANDECC_AUTOPLACE,
65 .eccpos = {0, 1, 2, 3, 6, 7},
69 static struct nand_oobinfo nand_oob_64 = {
70 .useecc = MTD_NANDECC_AUTOPLACE,
73 40, 41, 42, 43, 44, 45, 46, 47,
74 48, 49, 50, 51, 52, 53, 54, 55,
75 56, 57, 58, 59, 60, 61, 62, 63},
79 /* This is used for padding purposes in nand_write_oob */
80 static uint8_t ffchars[] = {
81 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
82 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
83 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
84 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
85 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
86 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
87 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
88 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
92 * NAND low-level MTD interface functions
94 static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len);
95 static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len);
96 static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len);
98 static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
99 size_t *retlen, uint8_t *buf);
100 static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
101 size_t *retlen, uint8_t *buf);
102 static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
103 size_t *retlen, const uint8_t *buf);
104 static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
105 size_t *retlen, const uint8_t *buf);
106 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr);
107 static void nand_sync(struct mtd_info *mtd);
109 /* Some internal functions */
110 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
111 int page, uint8_t * oob_buf,
112 struct nand_oobinfo *oobsel, int mode);
113 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
114 static int nand_verify_pages(struct mtd_info *mtd, struct nand_chip *chip,
115 int page, int numpages, uint8_t *oob_buf,
116 struct nand_oobinfo *oobsel, int chipnr,
119 #define nand_verify_pages(...) (0)
122 static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
126 * For devices which display every fart in the system on a seperate LED. Is
127 * compiled away when LED support is disabled.
129 DEFINE_LED_TRIGGER(nand_led_trigger);
132 * nand_release_device - [GENERIC] release chip
133 * @mtd: MTD device structure
135 * Deselect, release chip lock and wake up anyone waiting on the device
137 static void nand_release_device(struct mtd_info *mtd)
139 struct nand_chip *chip = mtd->priv;
141 /* De-select the NAND device */
142 chip->select_chip(mtd, -1);
144 /* Release the controller and the chip */
145 spin_lock(&chip->controller->lock);
146 chip->controller->active = NULL;
147 chip->state = FL_READY;
148 wake_up(&chip->controller->wq);
149 spin_unlock(&chip->controller->lock);
153 * nand_read_byte - [DEFAULT] read one byte from the chip
154 * @mtd: MTD device structure
156 * Default read function for 8bit buswith
158 static uint8_t nand_read_byte(struct mtd_info *mtd)
160 struct nand_chip *chip = mtd->priv;
161 return readb(chip->IO_ADDR_R);
165 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
166 * @mtd: MTD device structure
168 * Default read function for 16bit buswith with
169 * endianess conversion
171 static uint8_t nand_read_byte16(struct mtd_info *mtd)
173 struct nand_chip *chip = mtd->priv;
174 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
178 * nand_read_word - [DEFAULT] read one word from the chip
179 * @mtd: MTD device structure
181 * Default read function for 16bit buswith without
182 * endianess conversion
184 static u16 nand_read_word(struct mtd_info *mtd)
186 struct nand_chip *chip = mtd->priv;
187 return readw(chip->IO_ADDR_R);
191 * nand_select_chip - [DEFAULT] control CE line
192 * @mtd: MTD device structure
193 * @chip: chipnumber to select, -1 for deselect
195 * Default select function for 1 chip devices.
197 static void nand_select_chip(struct mtd_info *mtd, int chipnr)
199 struct nand_chip *chip = mtd->priv;
203 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
206 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
207 NAND_NCE | NAND_CTRL_CHANGE);
216 * nand_write_buf - [DEFAULT] write buffer to chip
217 * @mtd: MTD device structure
219 * @len: number of bytes to write
221 * Default write function for 8bit buswith
223 static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
226 struct nand_chip *chip = mtd->priv;
228 for (i = 0; i < len; i++)
229 writeb(buf[i], chip->IO_ADDR_W);
233 * nand_read_buf - [DEFAULT] read chip data into buffer
234 * @mtd: MTD device structure
235 * @buf: buffer to store date
236 * @len: number of bytes to read
238 * Default read function for 8bit buswith
240 static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
243 struct nand_chip *chip = mtd->priv;
245 for (i = 0; i < len; i++)
246 buf[i] = readb(chip->IO_ADDR_R);
250 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
251 * @mtd: MTD device structure
252 * @buf: buffer containing the data to compare
253 * @len: number of bytes to compare
255 * Default verify function for 8bit buswith
257 static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
260 struct nand_chip *chip = mtd->priv;
262 for (i = 0; i < len; i++)
263 if (buf[i] != readb(chip->IO_ADDR_R))
270 * nand_write_buf16 - [DEFAULT] write buffer to chip
271 * @mtd: MTD device structure
273 * @len: number of bytes to write
275 * Default write function for 16bit buswith
277 static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
280 struct nand_chip *chip = mtd->priv;
281 u16 *p = (u16 *) buf;
284 for (i = 0; i < len; i++)
285 writew(p[i], chip->IO_ADDR_W);
290 * nand_read_buf16 - [DEFAULT] read chip data into buffer
291 * @mtd: MTD device structure
292 * @buf: buffer to store date
293 * @len: number of bytes to read
295 * Default read function for 16bit buswith
297 static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
300 struct nand_chip *chip = mtd->priv;
301 u16 *p = (u16 *) buf;
304 for (i = 0; i < len; i++)
305 p[i] = readw(chip->IO_ADDR_R);
309 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
310 * @mtd: MTD device structure
311 * @buf: buffer containing the data to compare
312 * @len: number of bytes to compare
314 * Default verify function for 16bit buswith
316 static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
319 struct nand_chip *chip = mtd->priv;
320 u16 *p = (u16 *) buf;
323 for (i = 0; i < len; i++)
324 if (p[i] != readw(chip->IO_ADDR_R))
331 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
332 * @mtd: MTD device structure
333 * @ofs: offset from device start
334 * @getchip: 0, if the chip is already selected
336 * Check, if the block is bad.
338 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
340 int page, chipnr, res = 0;
341 struct nand_chip *chip = mtd->priv;
345 page = (int)(ofs >> chip->page_shift);
346 chipnr = (int)(ofs >> chip->chip_shift);
348 nand_get_device(chip, mtd, FL_READING);
350 /* Select the NAND device */
351 chip->select_chip(mtd, chipnr);
355 if (chip->options & NAND_BUSWIDTH_16) {
356 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
357 page & chip->pagemask);
358 bad = cpu_to_le16(chip->read_word(mtd));
359 if (chip->badblockpos & 0x1)
361 if ((bad & 0xFF) != 0xff)
364 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
365 page & chip->pagemask);
366 if (chip->read_byte(mtd) != 0xff)
371 nand_release_device(mtd);
377 * nand_default_block_markbad - [DEFAULT] mark a block bad
378 * @mtd: MTD device structure
379 * @ofs: offset from device start
381 * This is the default implementation, which can be overridden by
382 * a hardware specific driver.
384 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
386 struct nand_chip *chip = mtd->priv;
387 uint8_t buf[2] = { 0, 0 };
391 /* Get block number */
392 block = ((int)ofs) >> chip->bbt_erase_shift;
394 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
396 /* Do we have a flash based bad block table ? */
397 if (chip->options & NAND_USE_FLASH_BBT)
398 return nand_update_bbt(mtd, ofs);
400 /* We write two bytes, so we dont have to mess with 16 bit access */
401 ofs += mtd->oobsize + (chip->badblockpos & ~0x01);
402 return nand_write_oob(mtd, ofs, 2, &retlen, buf);
406 * nand_check_wp - [GENERIC] check if the chip is write protected
407 * @mtd: MTD device structure
408 * Check, if the device is write protected
410 * The function expects, that the device is already selected
412 static int nand_check_wp(struct mtd_info *mtd)
414 struct nand_chip *chip = mtd->priv;
415 /* Check the WP bit */
416 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
417 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
421 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
422 * @mtd: MTD device structure
423 * @ofs: offset from device start
424 * @getchip: 0, if the chip is already selected
425 * @allowbbt: 1, if its allowed to access the bbt area
427 * Check, if the block is bad. Either by reading the bad block table or
428 * calling of the scan function.
430 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
433 struct nand_chip *chip = mtd->priv;
436 return chip->block_bad(mtd, ofs, getchip);
438 /* Return info from the table */
439 return nand_isbad_bbt(mtd, ofs, allowbbt);
443 * Wait for the ready pin, after a command
444 * The timeout is catched later.
446 static void nand_wait_ready(struct mtd_info *mtd)
448 struct nand_chip *chip = mtd->priv;
449 unsigned long timeo = jiffies + 2;
451 led_trigger_event(nand_led_trigger, LED_FULL);
452 /* wait until command is processed or timeout occures */
454 if (chip->dev_ready(mtd))
456 touch_softlockup_watchdog();
457 } while (time_before(jiffies, timeo));
458 led_trigger_event(nand_led_trigger, LED_OFF);
462 * nand_command - [DEFAULT] Send command to NAND device
463 * @mtd: MTD device structure
464 * @command: the command to be sent
465 * @column: the column address for this command, -1 if none
466 * @page_addr: the page address for this command, -1 if none
468 * Send command to NAND device. This function is used for small page
469 * devices (256/512 Bytes per page)
471 static void nand_command(struct mtd_info *mtd, unsigned int command,
472 int column, int page_addr)
474 register struct nand_chip *chip = mtd->priv;
475 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
478 * Write out the command to the device.
480 if (command == NAND_CMD_SEQIN) {
483 if (column >= mtd->writesize) {
485 column -= mtd->writesize;
486 readcmd = NAND_CMD_READOOB;
487 } else if (column < 256) {
488 /* First 256 bytes --> READ0 */
489 readcmd = NAND_CMD_READ0;
492 readcmd = NAND_CMD_READ1;
494 chip->cmd_ctrl(mtd, readcmd, ctrl);
495 ctrl &= ~NAND_CTRL_CHANGE;
497 chip->cmd_ctrl(mtd, command, ctrl);
500 * Address cycle, when necessary
502 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
503 /* Serially input address */
505 /* Adjust columns for 16 bit buswidth */
506 if (chip->options & NAND_BUSWIDTH_16)
508 chip->cmd_ctrl(mtd, column, ctrl);
509 ctrl &= ~NAND_CTRL_CHANGE;
511 if (page_addr != -1) {
512 chip->cmd_ctrl(mtd, page_addr, ctrl);
513 ctrl &= ~NAND_CTRL_CHANGE;
514 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
515 /* One more address cycle for devices > 32MiB */
516 if (chip->chipsize > (32 << 20))
517 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
519 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
522 * program and erase have their own busy handlers
523 * status and sequential in needs no delay
527 case NAND_CMD_PAGEPROG:
528 case NAND_CMD_ERASE1:
529 case NAND_CMD_ERASE2:
531 case NAND_CMD_STATUS:
532 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE);
538 udelay(chip->chip_delay);
539 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
540 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
542 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
543 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
546 /* This applies to read commands */
549 * If we don't have access to the busy pin, we apply the given
552 if (!chip->dev_ready) {
553 udelay(chip->chip_delay);
557 /* Apply this short delay always to ensure that we do wait tWB in
558 * any case on any machine. */
561 nand_wait_ready(mtd);
565 * nand_command_lp - [DEFAULT] Send command to NAND large page device
566 * @mtd: MTD device structure
567 * @command: the command to be sent
568 * @column: the column address for this command, -1 if none
569 * @page_addr: the page address for this command, -1 if none
571 * Send command to NAND device. This is the version for the new large page
572 * devices We dont have the separate regions as we have in the small page
573 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
576 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
577 int column, int page_addr)
579 register struct nand_chip *chip = mtd->priv;
581 /* Emulate NAND_CMD_READOOB */
582 if (command == NAND_CMD_READOOB) {
583 column += mtd->writesize;
584 command = NAND_CMD_READ0;
587 /* Command latch cycle */
588 chip->cmd_ctrl(mtd, command & 0xff,
589 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
591 if (column != -1 || page_addr != -1) {
592 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
594 /* Serially input address */
596 /* Adjust columns for 16 bit buswidth */
597 if (chip->options & NAND_BUSWIDTH_16)
599 chip->cmd_ctrl(mtd, column, ctrl);
600 ctrl &= ~NAND_CTRL_CHANGE;
601 chip->cmd_ctrl(mtd, column >> 8, ctrl);
603 if (page_addr != -1) {
604 chip->cmd_ctrl(mtd, page_addr, ctrl);
605 chip->cmd_ctrl(mtd, page_addr >> 8,
606 NAND_NCE | NAND_ALE);
607 /* One more address cycle for devices > 128MiB */
608 if (chip->chipsize > (128 << 20))
609 chip->cmd_ctrl(mtd, page_addr >> 16,
610 NAND_NCE | NAND_ALE);
613 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
616 * program and erase have their own busy handlers
617 * status, sequential in, and deplete1 need no delay
621 case NAND_CMD_CACHEDPROG:
622 case NAND_CMD_PAGEPROG:
623 case NAND_CMD_ERASE1:
624 case NAND_CMD_ERASE2:
626 case NAND_CMD_STATUS:
627 case NAND_CMD_DEPLETE1:
631 * read error status commands require only a short delay
633 case NAND_CMD_STATUS_ERROR:
634 case NAND_CMD_STATUS_ERROR0:
635 case NAND_CMD_STATUS_ERROR1:
636 case NAND_CMD_STATUS_ERROR2:
637 case NAND_CMD_STATUS_ERROR3:
638 udelay(chip->chip_delay);
644 udelay(chip->chip_delay);
645 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
646 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
647 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
648 NAND_NCE | NAND_CTRL_CHANGE);
649 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
653 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
654 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
655 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
656 NAND_NCE | NAND_CTRL_CHANGE);
658 /* This applies to read commands */
661 * If we don't have access to the busy pin, we apply the given
664 if (!chip->dev_ready) {
665 udelay(chip->chip_delay);
670 /* Apply this short delay always to ensure that we do wait tWB in
671 * any case on any machine. */
674 nand_wait_ready(mtd);
678 * nand_get_device - [GENERIC] Get chip for selected access
679 * @this: the nand chip descriptor
680 * @mtd: MTD device structure
681 * @new_state: the state which is requested
683 * Get the device and lock it for exclusive access
686 nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
688 spinlock_t *lock = &chip->controller->lock;
689 wait_queue_head_t *wq = &chip->controller->wq;
690 DECLARE_WAITQUEUE(wait, current);
694 /* Hardware controller shared among independend devices */
695 /* Hardware controller shared among independend devices */
696 if (!chip->controller->active)
697 chip->controller->active = chip;
699 if (chip->controller->active == chip && chip->state == FL_READY) {
700 chip->state = new_state;
704 if (new_state == FL_PM_SUSPENDED) {
706 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
708 set_current_state(TASK_UNINTERRUPTIBLE);
709 add_wait_queue(wq, &wait);
712 remove_wait_queue(wq, &wait);
717 * nand_wait - [DEFAULT] wait until the command is done
718 * @mtd: MTD device structure
719 * @this: NAND chip structure
720 * @state: state to select the max. timeout value
722 * Wait for command done. This applies to erase and program only
723 * Erase can take up to 400ms and program up to 20ms according to
724 * general NAND and SmartMedia specs
727 static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip, int state)
730 unsigned long timeo = jiffies;
733 if (state == FL_ERASING)
734 timeo += (HZ * 400) / 1000;
736 timeo += (HZ * 20) / 1000;
738 led_trigger_event(nand_led_trigger, LED_FULL);
740 /* Apply this short delay always to ensure that we do wait tWB in
741 * any case on any machine. */
744 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
745 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
747 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
749 while (time_before(jiffies, timeo)) {
750 /* Check, if we were interrupted */
751 if (chip->state != state)
754 if (chip->dev_ready) {
755 if (chip->dev_ready(mtd))
758 if (chip->read_byte(mtd) & NAND_STATUS_READY)
763 led_trigger_event(nand_led_trigger, LED_OFF);
765 status = (int)chip->read_byte(mtd);
770 * nand_write_page - [GENERIC] write one page
771 * @mtd: MTD device structure
772 * @this: NAND chip structure
773 * @page: startpage inside the chip, must be called with (page & chip->pagemask)
774 * @oob_buf: out of band data buffer
775 * @oobsel: out of band selecttion structre
776 * @cached: 1 = enable cached programming if supported by chip
778 * Nand_page_program function is used for write and writev !
779 * This function will always program a full page of data
780 * If you call it with a non page aligned buffer, you're lost :)
782 * Cached programming is not supported yet.
784 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, int page,
785 uint8_t *oob_buf, struct nand_oobinfo *oobsel, int cached)
788 uint8_t ecc_code[32];
789 int eccmode = oobsel->useecc ? chip->ecc.mode : NAND_ECC_NONE;
790 int *oob_config = oobsel->eccpos;
791 int datidx = 0, eccidx = 0, eccsteps = chip->ecc.steps;
794 /* FIXME: Enable cached programming */
797 /* Send command to begin auto page programming */
798 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
800 /* Write out complete page of data, take care of eccmode */
802 /* No ecc, write all */
804 printk(KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
805 chip->write_buf(mtd, chip->data_poi, mtd->writesize);
808 /* Software ecc 3/256, write all */
810 for (; eccsteps; eccsteps--) {
811 chip->ecc.calculate(mtd, &chip->data_poi[datidx], ecc_code);
812 for (i = 0; i < 3; i++, eccidx++)
813 oob_buf[oob_config[eccidx]] = ecc_code[i];
814 datidx += chip->ecc.size;
816 chip->write_buf(mtd, chip->data_poi, mtd->writesize);
819 eccbytes = chip->ecc.bytes;
820 for (; eccsteps; eccsteps--) {
821 /* enable hardware ecc logic for write */
822 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
823 chip->write_buf(mtd, &chip->data_poi[datidx], chip->ecc.size);
824 chip->ecc.calculate(mtd, &chip->data_poi[datidx], ecc_code);
825 for (i = 0; i < eccbytes; i++, eccidx++)
826 oob_buf[oob_config[eccidx]] = ecc_code[i];
827 /* If the hardware ecc provides syndromes then
828 * the ecc code must be written immidiately after
829 * the data bytes (words) */
830 if (chip->options & NAND_HWECC_SYNDROME)
831 chip->write_buf(mtd, ecc_code, eccbytes);
832 datidx += chip->ecc.size;
837 /* Write out OOB data */
838 if (chip->options & NAND_HWECC_SYNDROME)
839 chip->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
841 chip->write_buf(mtd, oob_buf, mtd->oobsize);
843 /* Send command to actually program the data */
844 chip->cmdfunc(mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
847 /* call wait ready function */
848 status = chip->waitfunc(mtd, chip, FL_WRITING);
850 /* See if operation failed and additional status checks are available */
851 if ((status & NAND_STATUS_FAIL) && (chip->errstat)) {
852 status = chip->errstat(mtd, chip, FL_WRITING, status, page);
855 /* See if device thinks it succeeded */
856 if (status & NAND_STATUS_FAIL) {
857 DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
861 /* FIXME: Implement cached programming ! */
862 /* wait until cache is ready */
863 // status = chip->waitfunc (mtd, this, FL_CACHEDRPG);
868 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
870 * nand_verify_pages - [GENERIC] verify the chip contents after a write
871 * @mtd: MTD device structure
872 * @this: NAND chip structure
873 * @page: startpage inside the chip, must be called with (page & chip->pagemask)
874 * @numpages: number of pages to verify
875 * @oob_buf: out of band data buffer
876 * @oobsel: out of band selecttion structre
877 * @chipnr: number of the current chip
878 * @oobmode: 1 = full buffer verify, 0 = ecc only
880 * The NAND device assumes that it is always writing to a cleanly erased page.
881 * Hence, it performs its internal write verification only on bits that
882 * transitioned from 1 to 0. The device does NOT verify the whole page on a
883 * byte by byte basis. It is possible that the page was not completely erased
884 * or the page is becoming unusable due to wear. The read with ECC would catch
885 * the error later when the ECC page check fails, but we would rather catch
886 * it early in the page write stage. Better to write no data than invalid data.
888 static int nand_verify_pages(struct mtd_info *mtd, struct nand_chip *chip, int page, int numpages,
889 uint8_t *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
891 int i, j, datidx = 0, oobofs = 0, res = -EIO;
892 int eccsteps = chip->eccsteps;
896 hweccbytes = (chip->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
898 /* Send command to read back the first page */
899 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
902 for (j = 0; j < eccsteps; j++) {
903 /* Loop through and verify the data */
904 if (chip->verify_buf(mtd, &chip->data_poi[datidx], mtd->eccsize)) {
905 DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
908 datidx += mtd->eccsize;
909 /* Have we a hw generator layout ? */
912 if (chip->verify_buf(mtd, &chip->oob_buf[oobofs], hweccbytes)) {
913 DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
916 oobofs += hweccbytes;
919 /* check, if we must compare all data or if we just have to
920 * compare the ecc bytes
923 if (chip->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
924 DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
928 /* Read always, else autoincrement fails */
929 chip->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
931 if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
932 int ecccnt = oobsel->eccbytes;
934 for (i = 0; i < ecccnt; i++) {
935 int idx = oobsel->eccpos[i];
936 if (oobdata[idx] != oob_buf[oobofs + idx]) {
937 DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed ECC write verify, page 0x%08x, %6i bytes were succesful\n",
938 __FUNCTION__, page, i);
944 oobofs += mtd->oobsize - hweccbytes * eccsteps;
948 /* Apply delay or wait for ready/busy pin
949 * Do this before the AUTOINCR check, so no problems
950 * arise if a chip which does auto increment
951 * is marked as NOAUTOINCR by the board driver.
952 * Do this also before returning, so the chip is
953 * ready for the next command.
955 if (!chip->dev_ready)
956 udelay(chip->chip_delay);
958 nand_wait_ready(mtd);
960 /* All done, return happy */
964 /* Check, if the chip supports auto page increment */
965 if (!NAND_CANAUTOINCR(this))
966 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
969 * Terminate the read command. We come here in case of an error
970 * So we must issue a reset command.
973 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
979 * nand_read_page_swecc - {REPLACABLE] software ecc based page read function
980 * @mtd: mtd info structure
981 * @chip: nand chip info structure
982 * @buf: buffer to store read data
984 static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
987 int i, eccsize = chip->ecc.size;
988 int eccbytes = chip->ecc.bytes;
989 int eccsteps = chip->ecc.steps;
991 uint8_t *ecc_calc = chip->oob_buf + mtd->oobsize;
992 uint8_t *ecc_code = ecc_calc + mtd->oobsize;
993 int *eccpos = chip->autooob->eccpos;
995 chip->read_buf(mtd, buf, mtd->writesize);
996 chip->read_buf(mtd, chip->oob_buf, mtd->oobsize);
998 if (chip->ecc.mode == NAND_ECC_NONE)
1001 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1002 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1004 for (i = 0; i < chip->ecc.total; i++)
1005 ecc_code[i] = chip->oob_buf[eccpos[i]];
1007 eccsteps = chip->ecc.steps;
1010 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1013 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1015 mtd->ecc_stats.failed++;
1017 mtd->ecc_stats.corrected += stat;
1023 * nand_read_page_hwecc - {REPLACABLE] hardware ecc based page read function
1024 * @mtd: mtd info structure
1025 * @chip: nand chip info structure
1026 * @buf: buffer to store read data
1028 * Not for syndrome calculating ecc controllers which need a special oob layout
1030 static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1033 int i, eccsize = chip->ecc.size;
1034 int eccbytes = chip->ecc.bytes;
1035 int eccsteps = chip->ecc.steps;
1037 uint8_t *ecc_calc = chip->oob_buf + mtd->oobsize;
1038 uint8_t *ecc_code = ecc_calc + mtd->oobsize;
1039 int *eccpos = chip->autooob->eccpos;
1041 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1042 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1043 chip->read_buf(mtd, p, eccsize);
1044 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1046 chip->read_buf(mtd, chip->oob_buf, mtd->oobsize);
1048 for (i = 0; i < chip->ecc.total; i++)
1049 ecc_code[i] = chip->oob_buf[eccpos[i]];
1051 eccsteps = chip->ecc.steps;
1054 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1057 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1059 mtd->ecc_stats.failed++;
1061 mtd->ecc_stats.corrected += stat;
1067 * nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
1068 * @mtd: mtd info structure
1069 * @chip: nand chip info structure
1070 * @buf: buffer to store read data
1072 * The hw generator calculates the error syndrome automatically. Therefor
1073 * we need a special oob layout and .
1075 static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1078 int i, eccsize = chip->ecc.size;
1079 int eccbytes = chip->ecc.bytes;
1080 int eccsteps = chip->ecc.steps;
1082 uint8_t *oob = chip->oob_buf;
1084 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1087 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1088 chip->read_buf(mtd, p, eccsize);
1090 if (chip->ecc.prepad) {
1091 chip->read_buf(mtd, oob, chip->ecc.prepad);
1092 oob += chip->ecc.prepad;
1095 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1096 chip->read_buf(mtd, oob, eccbytes);
1097 stat = chip->ecc.correct(mtd, p, oob, NULL);
1100 mtd->ecc_stats.failed++;
1102 mtd->ecc_stats.corrected += stat;
1106 if (chip->ecc.postpad) {
1107 chip->read_buf(mtd, oob, chip->ecc.postpad);
1108 oob += chip->ecc.postpad;
1112 /* Calculate remaining oob bytes */
1113 i = oob - chip->oob_buf;
1115 chip->read_buf(mtd, oob, i);
1121 * nand_do_read - [Internal] Read data with ECC
1123 * @mtd: MTD device structure
1124 * @from: offset to read from
1125 * @len: number of bytes to read
1126 * @retlen: pointer to variable to store the number of read bytes
1127 * @buf: the databuffer to put data
1129 * Internal function. Called with chip held.
1131 int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
1132 size_t *retlen, uint8_t *buf)
1134 int chipnr, page, realpage, col, bytes, aligned;
1135 struct nand_chip *chip = mtd->priv;
1136 struct mtd_ecc_stats stats;
1137 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1140 uint32_t readlen = len;
1143 stats = mtd->ecc_stats;
1145 chipnr = (int)(from >> chip->chip_shift);
1146 chip->select_chip(mtd, chipnr);
1148 realpage = (int)(from >> chip->page_shift);
1149 page = realpage & chip->pagemask;
1151 col = (int)(from & (mtd->writesize - 1));
1154 bytes = min(mtd->writesize - col, readlen);
1155 aligned = (bytes == mtd->writesize);
1157 /* Is the current page in the buffer ? */
1158 if (realpage != chip->pagebuf) {
1159 bufpoi = aligned ? buf : chip->data_buf;
1161 if (likely(sndcmd)) {
1162 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1166 /* Now read the page into the buffer */
1167 ret = chip->ecc.read_page(mtd, chip, bufpoi);
1171 /* Transfer not aligned data */
1173 chip->pagebuf = realpage;
1174 memcpy(buf, chip->data_buf + col, bytes);
1177 if (!(chip->options & NAND_NO_READRDY)) {
1179 * Apply delay or wait for ready/busy pin. Do
1180 * this before the AUTOINCR check, so no
1181 * problems arise if a chip which does auto
1182 * increment is marked as NOAUTOINCR by the
1185 if (!chip->dev_ready)
1186 udelay(chip->chip_delay);
1188 nand_wait_ready(mtd);
1191 memcpy(buf, chip->data_buf + col, bytes);
1199 /* For subsequent reads align to page boundary. */
1201 /* Increment page address */
1204 page = realpage & chip->pagemask;
1205 /* Check, if we cross a chip boundary */
1208 chip->select_chip(mtd, -1);
1209 chip->select_chip(mtd, chipnr);
1212 /* Check, if the chip supports auto page increment
1213 * or if we have hit a block boundary.
1215 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1219 *retlen = len - (size_t) readlen;
1224 return mtd->ecc_stats.failed - stats.failed ? -EBADMSG : 0;
1228 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1229 * @mtd: MTD device structure
1230 * @from: offset to read from
1231 * @len: number of bytes to read
1232 * @retlen: pointer to variable to store the number of read bytes
1233 * @buf: the databuffer to put data
1235 * Get hold of the chip and call nand_do_read
1237 static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1238 size_t *retlen, uint8_t *buf)
1243 /* Do not allow reads past end of device */
1244 if ((from + len) > mtd->size)
1249 nand_get_device(mtd->priv, mtd, FL_READING);
1251 ret = nand_do_read(mtd, from, len, retlen, buf);
1253 nand_release_device(mtd);
1259 * nand_read_oob - [MTD Interface] NAND read out-of-band
1260 * @mtd: MTD device structure
1261 * @from: offset to read from
1262 * @len: number of bytes to read
1263 * @retlen: pointer to variable to store the number of read bytes
1264 * @buf: the databuffer to put data
1266 * NAND read out-of-band data from the spare area
1268 static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
1269 size_t *retlen, uint8_t *buf)
1271 int col, page, realpage, chipnr, sndcmd = 1;
1272 struct nand_chip *chip = mtd->priv;
1273 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1276 DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n",
1277 (unsigned int)from, (int)len);
1279 /* Initialize return length value */
1282 /* Do not allow reads past end of device */
1283 if ((from + len) > mtd->size) {
1284 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1285 "Attempt read beyond end of device\n");
1289 nand_get_device(chip, mtd, FL_READING);
1291 chipnr = (int)(from >> chip->chip_shift);
1292 chip->select_chip(mtd, chipnr);
1294 /* Shift to get page */
1295 realpage = (int)(from >> chip->page_shift);
1296 page = realpage & chip->pagemask;
1298 /* Mask to get column */
1299 col = from & (mtd->oobsize - 1);
1302 int bytes = min((int)(mtd->oobsize - col), readlen);
1304 if (likely(sndcmd)) {
1305 chip->cmdfunc(mtd, NAND_CMD_READOOB, col, page);
1309 chip->read_buf(mtd, buf, bytes);
1315 if (!(chip->options & NAND_NO_READRDY)) {
1317 * Apply delay or wait for ready/busy pin. Do this
1318 * before the AUTOINCR check, so no problems arise if a
1319 * chip which does auto increment is marked as
1320 * NOAUTOINCR by the board driver.
1322 if (!chip->dev_ready)
1323 udelay(chip->chip_delay);
1325 nand_wait_ready(mtd);
1329 bytes = mtd->oobsize;
1332 /* Increment page address */
1335 page = realpage & chip->pagemask;
1336 /* Check, if we cross a chip boundary */
1339 chip->select_chip(mtd, -1);
1340 chip->select_chip(mtd, chipnr);
1343 /* Check, if the chip supports auto page increment
1344 * or if we have hit a block boundary.
1346 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1350 /* Deselect and wake up anyone waiting on the device */
1351 nand_release_device(mtd);
1358 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
1359 * @mtd: MTD device structure
1360 * @buf: temporary buffer
1361 * @from: offset to read from
1362 * @len: number of bytes to read
1363 * @ooblen: number of oob data bytes to read
1365 * Read raw data including oob into buffer
1367 int nand_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len,
1370 struct nand_chip *chip = mtd->priv;
1371 int page = (int)(from >> chip->page_shift);
1372 int chipnr = (int)(from >> chip->chip_shift);
1375 int pagesize = mtd->writesize + mtd->oobsize;
1378 /* Do not allow reads past end of device */
1379 if ((from + len) > mtd->size) {
1380 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_raw: "
1381 "Attempt read beyond end of device\n");
1385 /* Grab the lock and see if the device is available */
1386 nand_get_device(chip, mtd, FL_READING);
1388 chip->select_chip(mtd, chipnr);
1390 /* Add requested oob length */
1392 blockcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1396 chip->cmdfunc(mtd, NAND_CMD_READ0, 0,
1397 page & chip->pagemask);
1400 chip->read_buf(mtd, &buf[cnt], pagesize);
1406 if (!chip->dev_ready)
1407 udelay(chip->chip_delay);
1409 nand_wait_ready(mtd);
1412 * Check, if the chip supports auto page increment or if we
1413 * cross a block boundary.
1415 if (!NAND_CANAUTOINCR(chip) || !(page & blockcheck))
1419 /* Deselect and wake up anyone waiting on the device */
1420 nand_release_device(mtd);
1425 * nand_write_raw - [GENERIC] Write raw data including oob
1426 * @mtd: MTD device structure
1427 * @buf: source buffer
1428 * @to: offset to write to
1429 * @len: number of bytes to write
1430 * @buf: source buffer
1433 * Write raw data including oob
1435 int nand_write_raw(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1436 uint8_t *buf, uint8_t *oob)
1438 struct nand_chip *chip = mtd->priv;
1439 int page = (int)(to >> chip->page_shift);
1440 int chipnr = (int)(to >> chip->chip_shift);
1445 /* Do not allow writes past end of device */
1446 if ((to + len) > mtd->size) {
1447 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt write "
1448 "beyond end of device\n");
1452 /* Grab the lock and see if the device is available */
1453 nand_get_device(chip, mtd, FL_WRITING);
1455 chip->select_chip(mtd, chipnr);
1456 chip->data_poi = buf;
1458 while (len != *retlen) {
1459 ret = nand_write_page(mtd, chip, page, oob, &mtd->oobinfo, 0);
1463 *retlen += mtd->writesize;
1464 chip->data_poi += mtd->writesize;
1465 oob += mtd->oobsize;
1468 /* Deselect and wake up anyone waiting on the device */
1469 nand_release_device(mtd);
1472 EXPORT_SYMBOL_GPL(nand_write_raw);
1475 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
1476 * @mtd: MTD device structure
1477 * @fsbuf: buffer given by fs driver
1478 * @oobsel: out of band selection structre
1479 * @autoplace: 1 = place given buffer into the oob bytes
1480 * @numpages: number of pages to prepare
1483 * 1. Filesystem buffer available and autoplacement is off,
1484 * return filesystem buffer
1485 * 2. No filesystem buffer or autoplace is off, return internal
1487 * 3. Filesystem buffer is given and autoplace selected
1488 * put data from fs buffer into internal buffer and
1489 * retrun internal buffer
1491 * Note: The internal buffer is filled with 0xff. This must
1492 * be done only once, when no autoplacement happens
1493 * Autoplacement sets the buffer dirty flag, which
1494 * forces the 0xff fill before using the buffer again.
1497 static uint8_t *nand_prepare_oobbuf(struct mtd_info *mtd, uint8_t *fsbuf, struct nand_oobinfo *oobsel,
1498 int autoplace, int numpages)
1500 struct nand_chip *chip = mtd->priv;
1503 /* Zero copy fs supplied buffer */
1504 if (fsbuf && !autoplace)
1507 /* Check, if the buffer must be filled with ff again */
1508 if (chip->oobdirty) {
1509 memset(chip->oob_buf, 0xff, mtd->oobsize << (chip->phys_erase_shift - chip->page_shift));
1513 /* If we have no autoplacement or no fs buffer use the internal one */
1514 if (!autoplace || !fsbuf)
1515 return chip->oob_buf;
1517 /* Walk through the pages and place the data */
1520 while (numpages--) {
1521 for (i = 0, len = 0; len < mtd->oobavail; i++) {
1522 int to = ofs + oobsel->oobfree[i][0];
1523 int num = oobsel->oobfree[i][1];
1524 memcpy(&chip->oob_buf[to], fsbuf, num);
1528 ofs += mtd->oobavail;
1530 return chip->oob_buf;
1533 #define NOTALIGNED(x) (x & (mtd->writesize-1)) != 0
1536 * nand_write - [MTD Interface] NAND write with ECC
1537 * @mtd: MTD device structure
1538 * @to: offset to write to
1539 * @len: number of bytes to write
1540 * @retlen: pointer to variable to store the number of written bytes
1541 * @buf: the data to write
1543 * NAND write with ECC
1545 static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
1546 size_t *retlen, const uint8_t *buf)
1548 int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
1549 int autoplace = 0, numpages, totalpages;
1550 struct nand_chip *chip = mtd->priv;
1551 uint8_t *oobbuf, *bufstart, *eccbuf = NULL;
1552 int ppblock = (1 << (chip->phys_erase_shift - chip->page_shift));
1553 struct nand_oobinfo *oobsel = &mtd->oobinfo;
1555 DEBUG(MTD_DEBUG_LEVEL3, "nand_write: to = 0x%08x, len = %i\n", (unsigned int)to, (int)len);
1557 /* Initialize retlen, in case of early exit */
1560 /* Do not allow write past end of device */
1561 if ((to + len) > mtd->size) {
1562 DEBUG(MTD_DEBUG_LEVEL0, "nand_write: Attempt to write past end of page\n");
1566 /* reject writes, which are not page aligned */
1567 if (NOTALIGNED(to) || NOTALIGNED(len)) {
1568 printk(KERN_NOTICE "nand_write: Attempt to write not page aligned data\n");
1572 /* Grab the lock and see if the device is available */
1573 nand_get_device(chip, mtd, FL_WRITING);
1575 /* Calculate chipnr */
1576 chipnr = (int)(to >> chip->chip_shift);
1577 /* Select the NAND device */
1578 chip->select_chip(mtd, chipnr);
1580 /* Check, if it is write protected */
1581 if (nand_check_wp(mtd))
1584 /* Autoplace of oob data ? Use the default placement scheme */
1585 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1586 oobsel = chip->autooob;
1589 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1592 /* Setup variables and oob buffer */
1593 totalpages = len >> chip->page_shift;
1594 page = (int)(to >> chip->page_shift);
1595 /* Invalidate the page cache, if we write to the cached page */
1596 if (page <= chip->pagebuf && chip->pagebuf < (page + totalpages))
1599 /* Set it relative to chip */
1600 page &= chip->pagemask;
1602 /* Calc number of pages we can write in one go */
1603 numpages = min(ppblock - (startpage & (ppblock - 1)), totalpages);
1604 oobbuf = nand_prepare_oobbuf(mtd, eccbuf, oobsel, autoplace, numpages);
1605 bufstart = (uint8_t *) buf;
1607 /* Loop until all data is written */
1608 while (written < len) {
1610 chip->data_poi = (uint8_t *) &buf[written];
1611 /* Write one page. If this is the last page to write
1612 * or the last page in this block, then use the
1613 * real pageprogram command, else select cached programming
1614 * if supported by the chip.
1616 ret = nand_write_page(mtd, chip, page, &oobbuf[oob], oobsel, (--numpages > 0));
1618 DEBUG(MTD_DEBUG_LEVEL0, "nand_write: write_page failed %d\n", ret);
1622 oob += mtd->oobsize;
1623 /* Update written bytes count */
1624 written += mtd->writesize;
1628 /* Increment page address */
1631 /* Have we hit a block boundary ? Then we have to verify and
1632 * if verify is ok, we have to setup the oob buffer for
1635 if (!(page & (ppblock - 1))) {
1637 chip->data_poi = bufstart;
1638 ret = nand_verify_pages(mtd, this, startpage, page - startpage,
1639 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1641 DEBUG(MTD_DEBUG_LEVEL0, "nand_write: verify_pages failed %d\n", ret);
1646 ofs = autoplace ? mtd->oobavail : mtd->oobsize;
1648 eccbuf += (page - startpage) * ofs;
1649 totalpages -= page - startpage;
1650 numpages = min(totalpages, ppblock);
1651 page &= chip->pagemask;
1653 oobbuf = nand_prepare_oobbuf(mtd, eccbuf, oobsel, autoplace, numpages);
1655 /* Check, if we cross a chip boundary */
1658 chip->select_chip(mtd, -1);
1659 chip->select_chip(mtd, chipnr);
1663 /* Verify the remaining pages */
1665 chip->data_poi = bufstart;
1666 ret = nand_verify_pages(mtd, this, startpage, totalpages, oobbuf, oobsel, chipnr, (eccbuf != NULL));
1670 DEBUG(MTD_DEBUG_LEVEL0, "nand_write: verify_pages failed %d\n", ret);
1673 /* Deselect and wake up anyone waiting on the device */
1674 nand_release_device(mtd);
1681 * nand_write_oob - [MTD Interface] NAND write out-of-band
1682 * @mtd: MTD device structure
1683 * @to: offset to write to
1684 * @len: number of bytes to write
1685 * @retlen: pointer to variable to store the number of written bytes
1686 * @buf: the data to write
1688 * NAND write out-of-band
1690 static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
1691 size_t *retlen, const uint8_t *buf)
1693 int column, page, status, ret = -EIO, chipnr;
1694 struct nand_chip *chip = mtd->priv;
1696 DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
1697 (unsigned int)to, (int)len);
1699 /* Initialize return length value */
1702 /* Do not allow write past end of page */
1703 column = to & (mtd->oobsize - 1);
1704 if ((column + len) > mtd->oobsize) {
1705 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
1706 "Attempt to write past end of page\n");
1710 nand_get_device(chip, mtd, FL_WRITING);
1712 chipnr = (int)(to >> chip->chip_shift);
1713 chip->select_chip(mtd, chipnr);
1715 /* Shift to get page */
1716 page = (int)(to >> chip->page_shift);
1719 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
1720 * of my DiskOnChip 2000 test units) will clear the whole data page too
1721 * if we don't do this. I have no clue why, but I seem to have 'fixed'
1722 * it in the doc2000 driver in August 1999. dwmw2.
1724 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1726 /* Check, if it is write protected */
1727 if (nand_check_wp(mtd))
1730 /* Invalidate the page cache, if we write to the cached page */
1731 if (page == chip->pagebuf)
1734 if (NAND_MUST_PAD(chip)) {
1735 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize,
1736 page & chip->pagemask);
1737 /* prepad 0xff for partial programming */
1738 chip->write_buf(mtd, ffchars, column);
1740 chip->write_buf(mtd, buf, len);
1741 /* postpad 0xff for partial programming */
1742 chip->write_buf(mtd, ffchars, mtd->oobsize - (len + column));
1744 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + column,
1745 page & chip->pagemask);
1746 chip->write_buf(mtd, buf, len);
1748 /* Send command to program the OOB data */
1749 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1751 status = chip->waitfunc(mtd, chip, FL_WRITING);
1753 /* See if device thinks it succeeded */
1754 if (status & NAND_STATUS_FAIL) {
1755 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
1756 "Failed write, page 0x%08x\n", page);
1762 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1763 /* Send command to read back the data */
1764 chip->cmdfunc(mtd, NAND_CMD_READOOB, column, page & chip->pagemask);
1766 if (chip->verify_buf(mtd, buf, len)) {
1767 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
1768 "Failed write verify, page 0x%08x\n", page);
1775 /* Deselect and wake up anyone waiting on the device */
1776 nand_release_device(mtd);
1782 * single_erease_cmd - [GENERIC] NAND standard block erase command function
1783 * @mtd: MTD device structure
1784 * @page: the page address of the block which will be erased
1786 * Standard erase command for NAND chips
1788 static void single_erase_cmd(struct mtd_info *mtd, int page)
1790 struct nand_chip *chip = mtd->priv;
1791 /* Send commands to erase a block */
1792 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1793 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1797 * multi_erease_cmd - [GENERIC] AND specific block erase command function
1798 * @mtd: MTD device structure
1799 * @page: the page address of the block which will be erased
1801 * AND multi block erase command function
1802 * Erase 4 consecutive blocks
1804 static void multi_erase_cmd(struct mtd_info *mtd, int page)
1806 struct nand_chip *chip = mtd->priv;
1807 /* Send commands to erase a block */
1808 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1809 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1810 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1811 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1812 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1816 * nand_erase - [MTD Interface] erase block(s)
1817 * @mtd: MTD device structure
1818 * @instr: erase instruction
1820 * Erase one ore more blocks
1822 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
1824 return nand_erase_nand(mtd, instr, 0);
1827 #define BBT_PAGE_MASK 0xffffff3f
1829 * nand_erase_nand - [Internal] erase block(s)
1830 * @mtd: MTD device structure
1831 * @instr: erase instruction
1832 * @allowbbt: allow erasing the bbt area
1834 * Erase one ore more blocks
1836 int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
1839 int page, len, status, pages_per_block, ret, chipnr;
1840 struct nand_chip *chip = mtd->priv;
1841 int rewrite_bbt[NAND_MAX_CHIPS]={0};
1842 unsigned int bbt_masked_page = 0xffffffff;
1844 DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
1845 (unsigned int)instr->addr, (unsigned int)instr->len);
1847 /* Start address must align on block boundary */
1848 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
1849 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
1853 /* Length must align on block boundary */
1854 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
1855 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1856 "Length not block aligned\n");
1860 /* Do not allow erase past end of device */
1861 if ((instr->len + instr->addr) > mtd->size) {
1862 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1863 "Erase past end of device\n");
1867 instr->fail_addr = 0xffffffff;
1869 /* Grab the lock and see if the device is available */
1870 nand_get_device(chip, mtd, FL_ERASING);
1872 /* Shift to get first page */
1873 page = (int)(instr->addr >> chip->page_shift);
1874 chipnr = (int)(instr->addr >> chip->chip_shift);
1876 /* Calculate pages in each block */
1877 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
1879 /* Select the NAND device */
1880 chip->select_chip(mtd, chipnr);
1882 /* Check, if it is write protected */
1883 if (nand_check_wp(mtd)) {
1884 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1885 "Device is write protected!!!\n");
1886 instr->state = MTD_ERASE_FAILED;
1891 * If BBT requires refresh, set the BBT page mask to see if the BBT
1892 * should be rewritten. Otherwise the mask is set to 0xffffffff which
1893 * can not be matched. This is also done when the bbt is actually
1894 * erased to avoid recusrsive updates
1896 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
1897 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
1899 /* Loop through the pages */
1902 instr->state = MTD_ERASING;
1906 * heck if we have a bad block, we do not erase bad blocks !
1908 if (nand_block_checkbad(mtd, ((loff_t) page) <<
1909 chip->page_shift, 0, allowbbt)) {
1910 printk(KERN_WARNING "nand_erase: attempt to erase a "
1911 "bad block at page 0x%08x\n", page);
1912 instr->state = MTD_ERASE_FAILED;
1917 * Invalidate the page cache, if we erase the block which
1918 * contains the current cached page
1920 if (page <= chip->pagebuf && chip->pagebuf <
1921 (page + pages_per_block))
1924 chip->erase_cmd(mtd, page & chip->pagemask);
1926 status = chip->waitfunc(mtd, chip, FL_ERASING);
1929 * See if operation failed and additional status checks are
1932 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1933 status = chip->errstat(mtd, chip, FL_ERASING,
1936 /* See if block erase succeeded */
1937 if (status & NAND_STATUS_FAIL) {
1938 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
1939 "Failed erase, page 0x%08x\n", page);
1940 instr->state = MTD_ERASE_FAILED;
1941 instr->fail_addr = (page << chip->page_shift);
1946 * If BBT requires refresh, set the BBT rewrite flag to the
1949 if (bbt_masked_page != 0xffffffff &&
1950 (page & BBT_PAGE_MASK) == bbt_masked_page)
1951 rewrite_bbt[chipnr] = (page << chip->page_shift);
1953 /* Increment page address and decrement length */
1954 len -= (1 << chip->phys_erase_shift);
1955 page += pages_per_block;
1957 /* Check, if we cross a chip boundary */
1958 if (len && !(page & chip->pagemask)) {
1960 chip->select_chip(mtd, -1);
1961 chip->select_chip(mtd, chipnr);
1964 * If BBT requires refresh and BBT-PERCHIP, set the BBT
1965 * page mask to see if this BBT should be rewritten
1967 if (bbt_masked_page != 0xffffffff &&
1968 (chip->bbt_td->options & NAND_BBT_PERCHIP))
1969 bbt_masked_page = chip->bbt_td->pages[chipnr] &
1973 instr->state = MTD_ERASE_DONE;
1977 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
1978 /* Do call back function */
1980 mtd_erase_callback(instr);
1982 /* Deselect and wake up anyone waiting on the device */
1983 nand_release_device(mtd);
1986 * If BBT requires refresh and erase was successful, rewrite any
1987 * selected bad block tables
1989 if (bbt_masked_page == 0xffffffff || ret)
1992 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
1993 if (!rewrite_bbt[chipnr])
1995 /* update the BBT for chip */
1996 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
1997 "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
1998 chip->bbt_td->pages[chipnr]);
1999 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2002 /* Return more or less happy */
2007 * nand_sync - [MTD Interface] sync
2008 * @mtd: MTD device structure
2010 * Sync is actually a wait for chip ready function
2012 static void nand_sync(struct mtd_info *mtd)
2014 struct nand_chip *chip = mtd->priv;
2016 DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2018 /* Grab the lock and see if the device is available */
2019 nand_get_device(chip, mtd, FL_SYNCING);
2020 /* Release it and go back */
2021 nand_release_device(mtd);
2025 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
2026 * @mtd: MTD device structure
2027 * @ofs: offset relative to mtd start
2029 static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
2031 /* Check for invalid offset */
2032 if (offs > mtd->size)
2035 return nand_block_checkbad(mtd, offs, 1, 0);
2039 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
2040 * @mtd: MTD device structure
2041 * @ofs: offset relative to mtd start
2043 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2045 struct nand_chip *chip = mtd->priv;
2048 if ((ret = nand_block_isbad(mtd, ofs))) {
2049 /* If it was bad already, return success and do nothing. */
2055 return chip->block_markbad(mtd, ofs);
2059 * nand_suspend - [MTD Interface] Suspend the NAND flash
2060 * @mtd: MTD device structure
2062 static int nand_suspend(struct mtd_info *mtd)
2064 struct nand_chip *chip = mtd->priv;
2066 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
2070 * nand_resume - [MTD Interface] Resume the NAND flash
2071 * @mtd: MTD device structure
2073 static void nand_resume(struct mtd_info *mtd)
2075 struct nand_chip *chip = mtd->priv;
2077 if (chip->state == FL_PM_SUSPENDED)
2078 nand_release_device(mtd);
2080 printk(KERN_ERR "nand_resume() called for a chip which is not "
2081 "in suspended state\n");
2085 * Free allocated data structures
2087 static void nand_free_kmem(struct nand_chip *chip)
2089 /* Buffer allocated by nand_scan ? */
2090 if (chip->options & NAND_OOBBUF_ALLOC)
2091 kfree(chip->oob_buf);
2092 /* Buffer allocated by nand_scan ? */
2093 if (chip->options & NAND_DATABUF_ALLOC)
2094 kfree(chip->data_buf);
2095 /* Controller allocated by nand_scan ? */
2096 if (chip->options & NAND_CONTROLLER_ALLOC)
2097 kfree(chip->controller);
2101 * Allocate buffers and data structures
2103 static int nand_allocate_kmem(struct mtd_info *mtd, struct nand_chip *chip)
2107 if (!chip->oob_buf) {
2108 len = mtd->oobsize <<
2109 (chip->phys_erase_shift - chip->page_shift);
2110 chip->oob_buf = kmalloc(len, GFP_KERNEL);
2113 chip->options |= NAND_OOBBUF_ALLOC;
2116 if (!chip->data_buf) {
2117 len = mtd->writesize + mtd->oobsize;
2118 chip->data_buf = kmalloc(len, GFP_KERNEL);
2119 if (!chip->data_buf)
2121 chip->options |= NAND_DATABUF_ALLOC;
2124 if (!chip->controller) {
2125 chip->controller = kzalloc(sizeof(struct nand_hw_control),
2127 if (!chip->controller)
2130 spin_lock_init(&chip->controller->lock);
2131 init_waitqueue_head(&chip->controller->wq);
2132 chip->options |= NAND_CONTROLLER_ALLOC;
2137 printk(KERN_ERR "nand_scan(): Cannot allocate buffers\n");
2138 nand_free_kmem(chip);
2143 * Set default functions
2145 static void nand_set_defaults(struct nand_chip *chip, int busw)
2147 /* check for proper chip_delay setup, set 20us if not */
2148 if (!chip->chip_delay)
2149 chip->chip_delay = 20;
2151 /* check, if a user supplied command function given */
2152 if (chip->cmdfunc == NULL)
2153 chip->cmdfunc = nand_command;
2155 /* check, if a user supplied wait function given */
2156 if (chip->waitfunc == NULL)
2157 chip->waitfunc = nand_wait;
2159 if (!chip->select_chip)
2160 chip->select_chip = nand_select_chip;
2161 if (!chip->read_byte)
2162 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2163 if (!chip->read_word)
2164 chip->read_word = nand_read_word;
2165 if (!chip->block_bad)
2166 chip->block_bad = nand_block_bad;
2167 if (!chip->block_markbad)
2168 chip->block_markbad = nand_default_block_markbad;
2169 if (!chip->write_buf)
2170 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2171 if (!chip->read_buf)
2172 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2173 if (!chip->verify_buf)
2174 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2175 if (!chip->scan_bbt)
2176 chip->scan_bbt = nand_default_bbt;
2180 * Get the flash and manufacturer id and lookup if the type is supported
2182 static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2183 struct nand_chip *chip,
2184 int busw, int *maf_id)
2186 struct nand_flash_dev *type = NULL;
2187 int i, dev_id, maf_idx;
2189 /* Select the device */
2190 chip->select_chip(mtd, 0);
2192 /* Send the command for reading device ID */
2193 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2195 /* Read manufacturer and device IDs */
2196 *maf_id = chip->read_byte(mtd);
2197 dev_id = chip->read_byte(mtd);
2199 /* Lookup the flash id */
2200 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2201 if (dev_id == nand_flash_ids[i].id) {
2202 type = &nand_flash_ids[i];
2208 return ERR_PTR(-ENODEV);
2210 chip->chipsize = nand_flash_ids[i].chipsize << 20;
2212 /* Newer devices have all the information in additional id bytes */
2213 if (!nand_flash_ids[i].pagesize) {
2215 /* The 3rd id byte contains non relevant data ATM */
2216 extid = chip->read_byte(mtd);
2217 /* The 4th id byte is the important one */
2218 extid = chip->read_byte(mtd);
2220 mtd->writesize = 1024 << (extid & 0x3);
2223 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
2225 /* Calc blocksize. Blocksize is multiples of 64KiB */
2226 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2228 /* Get buswidth information */
2229 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2233 * Old devices have chip data hardcoded in the device id table
2235 mtd->erasesize = nand_flash_ids[i].erasesize;
2236 mtd->writesize = nand_flash_ids[i].pagesize;
2237 mtd->oobsize = mtd->writesize / 32;
2238 busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
2241 /* Try to identify manufacturer */
2242 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_id++) {
2243 if (nand_manuf_ids[maf_idx].id == *maf_id)
2248 * Check, if buswidth is correct. Hardware drivers should set
2251 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2252 printk(KERN_INFO "NAND device: Manufacturer ID:"
2253 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2254 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2255 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
2256 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2258 return ERR_PTR(-EINVAL);
2261 /* Calculate the address shift from the page size */
2262 chip->page_shift = ffs(mtd->writesize) - 1;
2263 /* Convert chipsize to number of pages per chip -1. */
2264 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
2266 chip->bbt_erase_shift = chip->phys_erase_shift =
2267 ffs(mtd->erasesize) - 1;
2268 chip->chip_shift = ffs(chip->chipsize) - 1;
2270 /* Set the bad block position */
2271 chip->badblockpos = mtd->writesize > 512 ?
2272 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2274 /* Get chip options, preserve non chip based options */
2275 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2276 chip->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
2279 * Set chip as a default. Board drivers can override it, if necessary
2281 chip->options |= NAND_NO_AUTOINCR;
2283 /* Check if chip is a not a samsung device. Do not clear the
2284 * options for chips which are not having an extended id.
2286 if (*maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
2287 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2289 /* Check for AND chips with 4 page planes */
2290 if (chip->options & NAND_4PAGE_ARRAY)
2291 chip->erase_cmd = multi_erase_cmd;
2293 chip->erase_cmd = single_erase_cmd;
2295 /* Do not replace user supplied command function ! */
2296 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2297 chip->cmdfunc = nand_command_lp;
2299 printk(KERN_INFO "NAND device: Manufacturer ID:"
2300 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2301 nand_manuf_ids[maf_idx].name, type->name);
2306 /* module_text_address() isn't exported, and it's mostly a pointless
2307 test if this is a module _anyway_ -- they'd have to try _really_ hard
2308 to call us from in-kernel code if the core NAND support is modular. */
2310 #define caller_is_module() (1)
2312 #define caller_is_module() \
2313 module_text_address((unsigned long)__builtin_return_address(0))
2317 * nand_scan - [NAND Interface] Scan for the NAND device
2318 * @mtd: MTD device structure
2319 * @maxchips: Number of chips to scan for
2321 * This fills out all the uninitialized function pointers
2322 * with the defaults.
2323 * The flash ID is read and the mtd/chip structures are
2324 * filled with the appropriate values. Buffers are allocated if
2325 * they are not provided by the board driver
2326 * The mtd->owner field must be set to the module of the caller
2329 int nand_scan(struct mtd_info *mtd, int maxchips)
2331 int i, busw, nand_maf_id;
2332 struct nand_chip *chip = mtd->priv;
2333 struct nand_flash_dev *type;
2335 /* Many callers got this wrong, so check for it for a while... */
2336 if (!mtd->owner && caller_is_module()) {
2337 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2341 /* Get buswidth to select the correct functions */
2342 busw = chip->options & NAND_BUSWIDTH_16;
2343 /* Set the default functions */
2344 nand_set_defaults(chip, busw);
2346 /* Read the flash type */
2347 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
2350 printk(KERN_WARNING "No NAND device found!!!\n");
2351 chip->select_chip(mtd, -1);
2352 return PTR_ERR(type);
2355 /* Check for a chip array */
2356 for (i = 1; i < maxchips; i++) {
2357 chip->select_chip(mtd, i);
2358 /* Send the command for reading device ID */
2359 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2360 /* Read manufacturer and device IDs */
2361 if (nand_maf_id != chip->read_byte(mtd) ||
2362 type->id != chip->read_byte(mtd))
2366 printk(KERN_INFO "%d NAND chips detected\n", i);
2368 /* Store the number of chips and calc total size for mtd */
2370 mtd->size = i * chip->chipsize;
2372 /* Allocate buffers and data structures */
2373 if (nand_allocate_kmem(mtd, chip))
2376 /* Preset the internal oob buffer */
2377 memset(chip->oob_buf, 0xff,
2378 mtd->oobsize << (chip->phys_erase_shift - chip->page_shift));
2381 * If no default placement scheme is given, select an appropriate one
2383 if (!chip->autooob) {
2384 switch (mtd->oobsize) {
2386 chip->autooob = &nand_oob_8;
2389 chip->autooob = &nand_oob_16;
2392 chip->autooob = &nand_oob_64;
2395 printk(KERN_WARNING "No oob scheme defined for "
2396 "oobsize %d\n", mtd->oobsize);
2402 * The number of bytes available for the filesystem to place fs
2403 * dependend oob data
2406 for (i = 0; chip->autooob->oobfree[i][1]; i++)
2407 mtd->oobavail += chip->autooob->oobfree[i][1];
2410 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2411 * selected and we have 256 byte pagesize fallback to software ECC
2413 switch (chip->ecc.mode) {
2415 /* Use standard hwecc read page function ? */
2416 if (!chip->ecc.read_page)
2417 chip->ecc.read_page = nand_read_page_hwecc;
2419 case NAND_ECC_HW_SYNDROME:
2420 if (!chip->ecc.calculate || !chip->ecc.correct ||
2422 printk(KERN_WARNING "No ECC functions supplied, "
2423 "Hardware ECC not possible\n");
2426 /* Use standard syndrome read page function ? */
2427 if (!chip->ecc.read_page)
2428 chip->ecc.read_page = nand_read_page_syndrome;
2430 if (mtd->writesize >= chip->ecc.size)
2432 printk(KERN_WARNING "%d byte HW ECC not possible on "
2433 "%d byte page size, fallback to SW ECC\n",
2434 chip->ecc.size, mtd->writesize);
2435 chip->ecc.mode = NAND_ECC_SOFT;
2438 chip->ecc.calculate = nand_calculate_ecc;
2439 chip->ecc.correct = nand_correct_data;
2440 chip->ecc.read_page = nand_read_page_swecc;
2441 chip->ecc.size = 256;
2442 chip->ecc.bytes = 3;
2446 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2447 "This is not recommended !!\n");
2448 chip->ecc.read_page = nand_read_page_swecc;
2449 chip->ecc.size = mtd->writesize;
2450 chip->ecc.bytes = 0;
2453 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
2459 * Set the number of read / write steps for one page depending on ECC
2462 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2463 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
2464 printk(KERN_WARNING "Invalid ecc parameters\n");
2467 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
2469 /* Initialize state */
2470 chip->state = FL_READY;
2472 /* De-select the device */
2473 chip->select_chip(mtd, -1);
2475 /* Invalidate the pagebuffer reference */
2478 /* Fill in remaining MTD driver data */
2479 mtd->type = MTD_NANDFLASH;
2480 mtd->flags = MTD_CAP_NANDFLASH;
2481 mtd->ecctype = MTD_ECC_SW;
2482 mtd->erase = nand_erase;
2484 mtd->unpoint = NULL;
2485 mtd->read = nand_read;
2486 mtd->write = nand_write;
2487 mtd->read_oob = nand_read_oob;
2488 mtd->write_oob = nand_write_oob;
2489 mtd->sync = nand_sync;
2492 mtd->suspend = nand_suspend;
2493 mtd->resume = nand_resume;
2494 mtd->block_isbad = nand_block_isbad;
2495 mtd->block_markbad = nand_block_markbad;
2497 /* and make the autooob the default one */
2498 memcpy(&mtd->oobinfo, chip->autooob, sizeof(mtd->oobinfo));
2500 /* Check, if we should skip the bad block table scan */
2501 if (chip->options & NAND_SKIP_BBTSCAN)
2504 /* Build bad block table */
2505 return chip->scan_bbt(mtd);
2509 * nand_release - [NAND Interface] Free resources held by the NAND device
2510 * @mtd: MTD device structure
2512 void nand_release(struct mtd_info *mtd)
2514 struct nand_chip *chip = mtd->priv;
2516 #ifdef CONFIG_MTD_PARTITIONS
2517 /* Deregister partitions */
2518 del_mtd_partitions(mtd);
2520 /* Deregister the device */
2521 del_mtd_device(mtd);
2523 /* Free bad block table memory */
2526 nand_free_kmem(chip);
2529 EXPORT_SYMBOL_GPL(nand_scan);
2530 EXPORT_SYMBOL_GPL(nand_release);
2532 static int __init nand_base_init(void)
2534 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2538 static void __exit nand_base_exit(void)
2540 led_trigger_unregister_simple(nand_led_trigger);
2543 module_init(nand_base_init);
2544 module_exit(nand_base_exit);
2546 MODULE_LICENSE("GPL");
2547 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2548 MODULE_DESCRIPTION("Generic NAND flash driver code");