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 Thomas Gleixner (tglx@linutronix.de)
15 * 02-08-2004 tglx: support for strange chips, which cannot auto increment
16 * pages on read / read_oob
18 * 03-17-2004 tglx: Check ready before auto increment check. Simon Bayes
19 * pointed this out, as he marked an auto increment capable chip
20 * as NOAUTOINCR in the board driver.
21 * Make reads over block boundaries work too
23 * 04-14-2004 tglx: first working version for 2k page size chips
25 * 05-19-2004 tglx: Basic support for Renesas AG-AND chips
27 * 09-24-2004 tglx: add support for hardware controllers (e.g. ECC) shared
28 * among multiple independend devices. Suggestions and initial patch
29 * from Ben Dooks <ben-mtd@fluff.org>
32 * David Woodhouse for adding multichip support
34 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
35 * rework for 2K page size chips
38 * Enable cached programming for 2k page size chips
39 * Check, if mtd->ecctype should be set to MTD_ECC_HW
40 * if we have HW ecc support.
41 * The AG-AND chips have nice features for speed improvement,
42 * which are not supported yet. Read / program 4 pages in one go.
44 * $Id: nand_base.c,v 1.126 2004/12/13 11:22:25 lavinen Exp $
46 * This program is free software; you can redistribute it and/or modify
47 * it under the terms of the GNU General Public License version 2 as
48 * published by the Free Software Foundation.
54 #include <linux/delay.h>
55 #include <linux/errno.h>
56 #include <linux/sched.h>
57 #include <linux/slab.h>
58 #include <linux/types.h>
59 #include <linux/mtd/mtd.h>
60 #include <linux/mtd/nand.h>
61 #include <linux/mtd/nand_ecc.h>
62 #include <linux/mtd/compatmac.h>
63 #include <linux/interrupt.h>
64 #include <linux/bitops.h>
67 #ifdef CONFIG_MTD_PARTITIONS
68 #include <linux/mtd/partitions.h>
75 #if defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY)
79 #include <linux/mtd/compat.h>
80 #include <linux/mtd/mtd.h>
81 #include <linux/mtd/nand.h>
82 #include <linux/mtd/nand_ecc.h>
85 #include <asm/errno.h>
87 #ifdef CONFIG_JFFS2_NAND
88 #include <jffs2/jffs2.h>
91 /* Define default oob placement schemes for large and small page devices */
92 static struct nand_oobinfo nand_oob_8 = {
93 .useecc = MTD_NANDECC_AUTOPLACE,
96 .oobfree = { {3, 2}, {6, 2} }
99 static struct nand_oobinfo nand_oob_16 = {
100 .useecc = MTD_NANDECC_AUTOPLACE,
102 .eccpos = {0, 1, 2, 3, 6, 7},
103 .oobfree = { {8, 8} }
106 static struct nand_oobinfo nand_oob_64 = {
107 .useecc = MTD_NANDECC_AUTOPLACE,
110 40, 41, 42, 43, 44, 45, 46, 47,
111 48, 49, 50, 51, 52, 53, 54, 55,
112 56, 57, 58, 59, 60, 61, 62, 63},
113 .oobfree = { {2, 38} }
116 static struct nand_oobinfo nand_oob_128 = {
117 .useecc = MTD_NANDECC_AUTOPLACE,
120 80, 81, 82, 83, 84, 85, 86, 87,
121 88, 89, 90, 91, 92, 93, 94, 95,
122 96, 97, 98, 99, 100, 101, 102, 103,
123 104, 105, 106, 107, 108, 109, 110, 111,
124 112, 113, 114, 115, 116, 117, 118, 119,
125 120, 121, 122, 123, 124, 125, 126, 127},
126 .oobfree = { {2, 78} }
129 /* This is used for padding purposes in nand_write_oob */
130 static u_char *ffchars;
133 * NAND low-level MTD interface functions
135 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
136 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
137 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
139 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
140 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
141 size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
142 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
143 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf);
144 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
145 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
146 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char *buf);
149 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
150 unsigned long count, loff_t to, size_t * retlen);
151 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs,
152 unsigned long count, loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
154 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
155 static void nand_sync (struct mtd_info *mtd);
157 /* Some internal functions */
158 static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf,
159 struct nand_oobinfo *oobsel, int mode);
160 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
161 static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
162 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode);
164 #define nand_verify_pages(...) (0)
167 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state);
170 * nand_release_device - [GENERIC] release chip
171 * @mtd: MTD device structure
173 * Deselect, release chip lock and wake up anyone waiting on the device
177 static void nand_release_device (struct mtd_info *mtd)
179 struct nand_chip *this = mtd->priv;
181 /* De-select the NAND device */
182 this->select_chip(mtd, -1);
183 /* Do we have a hardware controller ? */
184 if (this->controller) {
185 spin_lock(&this->controller->lock);
186 this->controller->active = NULL;
187 spin_unlock(&this->controller->lock);
189 /* Release the chip */
190 spin_lock (&this->chip_lock);
191 this->state = FL_READY;
193 spin_unlock (&this->chip_lock);
196 static void nand_release_device (struct mtd_info *mtd)
198 struct nand_chip *this = mtd->priv;
199 this->select_chip(mtd, -1); /* De-select the NAND device */
208 * nand_read_byte - [DEFAULT] read one byte from the chip
209 * @mtd: MTD device structure
211 * Default read function for 8bit buswith
213 static u_char nand_read_byte(struct mtd_info *mtd)
215 struct nand_chip *this = mtd->priv;
216 return readb(this->IO_ADDR_R);
220 * nand_write_byte - [DEFAULT] write one byte to the chip
221 * @mtd: MTD device structure
222 * @byte: pointer to data byte to write
224 * Default write function for 8it buswith
226 static void nand_write_byte(struct mtd_info *mtd, u_char byte)
228 struct nand_chip *this = mtd->priv;
229 writeb(byte, this->IO_ADDR_W);
233 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
234 * @mtd: MTD device structure
236 * Default read function for 16bit buswith with
237 * endianess conversion
239 static u_char nand_read_byte16(struct mtd_info *mtd)
241 struct nand_chip *this = mtd->priv;
242 return (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
246 * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
247 * @mtd: MTD device structure
248 * @byte: pointer to data byte to write
250 * Default write function for 16bit buswith with
251 * endianess conversion
253 static void nand_write_byte16(struct mtd_info *mtd, u_char byte)
255 struct nand_chip *this = mtd->priv;
256 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
260 * nand_read_word - [DEFAULT] read one word from the chip
261 * @mtd: MTD device structure
263 * Default read function for 16bit buswith without
264 * endianess conversion
266 static u16 nand_read_word(struct mtd_info *mtd)
268 struct nand_chip *this = mtd->priv;
269 return readw(this->IO_ADDR_R);
273 * nand_write_word - [DEFAULT] write one word to the chip
274 * @mtd: MTD device structure
275 * @word: data word to write
277 * Default write function for 16bit buswith without
278 * endianess conversion
280 static void nand_write_word(struct mtd_info *mtd, u16 word)
282 struct nand_chip *this = mtd->priv;
283 writew(word, this->IO_ADDR_W);
287 * nand_select_chip - [DEFAULT] control CE line
288 * @mtd: MTD device structure
289 * @chip: chipnumber to select, -1 for deselect
291 * Default select function for 1 chip devices.
293 static void nand_select_chip(struct mtd_info *mtd, int chip)
295 struct nand_chip *this = mtd->priv;
298 this->hwcontrol(mtd, NAND_CTL_CLRNCE);
301 this->hwcontrol(mtd, NAND_CTL_SETNCE);
310 * nand_write_buf - [DEFAULT] write buffer to chip
311 * @mtd: MTD device structure
313 * @len: number of bytes to write
315 * Default write function for 8bit buswith
317 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
320 struct nand_chip *this = mtd->priv;
322 for (i=0; i<len; i++)
323 writeb(buf[i], this->IO_ADDR_W);
327 * nand_read_buf - [DEFAULT] read chip data into buffer
328 * @mtd: MTD device structure
329 * @buf: buffer to store date
330 * @len: number of bytes to read
332 * Default read function for 8bit buswith
334 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
337 struct nand_chip *this = mtd->priv;
339 for (i=0; i<len; i++)
340 buf[i] = readb(this->IO_ADDR_R);
344 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
345 * @mtd: MTD device structure
346 * @buf: buffer containing the data to compare
347 * @len: number of bytes to compare
349 * Default verify function for 8bit buswith
351 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
354 struct nand_chip *this = mtd->priv;
356 for (i=0; i<len; i++)
357 if (buf[i] != readb(this->IO_ADDR_R))
364 * nand_write_buf16 - [DEFAULT] write buffer to chip
365 * @mtd: MTD device structure
367 * @len: number of bytes to write
369 * Default write function for 16bit buswith
371 static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
374 struct nand_chip *this = mtd->priv;
375 u16 *p = (u16 *) buf;
378 for (i=0; i<len; i++)
379 writew(p[i], this->IO_ADDR_W);
384 * nand_read_buf16 - [DEFAULT] read chip data into buffer
385 * @mtd: MTD device structure
386 * @buf: buffer to store date
387 * @len: number of bytes to read
389 * Default read function for 16bit buswith
391 static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
394 struct nand_chip *this = mtd->priv;
395 u16 *p = (u16 *) buf;
398 for (i=0; i<len; i++)
399 p[i] = readw(this->IO_ADDR_R);
403 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
404 * @mtd: MTD device structure
405 * @buf: buffer containing the data to compare
406 * @len: number of bytes to compare
408 * Default verify function for 16bit buswith
410 static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
413 struct nand_chip *this = mtd->priv;
414 u16 *p = (u16 *) buf;
417 for (i=0; i<len; i++)
418 if (p[i] != readw(this->IO_ADDR_R))
425 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
426 * @mtd: MTD device structure
427 * @ofs: offset from device start
428 * @getchip: 0, if the chip is already selected
430 * Check, if the block is bad.
432 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
434 int page, chipnr, res = 0;
435 struct nand_chip *this = mtd->priv;
438 page = (int)(ofs >> this->page_shift) & this->pagemask;
441 chipnr = (int)(ofs >> this->chip_shift);
443 /* Grab the lock and see if the device is available */
444 nand_get_device (this, mtd, FL_READING);
446 /* Select the NAND device */
447 this->select_chip(mtd, chipnr);
450 if (this->options & NAND_BUSWIDTH_16) {
451 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page);
452 bad = cpu_to_le16(this->read_word(mtd));
453 if (this->badblockpos & 0x1)
455 if ((bad & 0xFF) != 0xff)
458 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page);
459 if (this->read_byte(mtd) != 0xff)
464 /* Deselect and wake up anyone waiting on the device */
465 nand_release_device(mtd);
472 * nand_default_block_markbad - [DEFAULT] mark a block bad
473 * @mtd: MTD device structure
474 * @ofs: offset from device start
476 * This is the default implementation, which can be overridden by
477 * a hardware specific driver.
479 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
481 struct nand_chip *this = mtd->priv;
482 u_char buf[2] = {0, 0};
486 /* Get block number */
487 block = ((int) ofs) >> this->bbt_erase_shift;
488 this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
490 /* Do we have a flash based bad block table ? */
491 if (this->options & NAND_USE_FLASH_BBT)
492 return nand_update_bbt (mtd, ofs);
494 /* We write two bytes, so we dont have to mess with 16 bit access */
495 ofs += mtd->oobsize + (this->badblockpos & ~0x01);
496 return nand_write_oob (mtd, ofs , 2, &retlen, buf);
500 * nand_check_wp - [GENERIC] check if the chip is write protected
501 * @mtd: MTD device structure
502 * Check, if the device is write protected
504 * The function expects, that the device is already selected
506 static int nand_check_wp (struct mtd_info *mtd)
508 struct nand_chip *this = mtd->priv;
509 /* Check the WP bit */
510 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
511 return (this->read_byte(mtd) & 0x80) ? 0 : 1;
515 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
516 * @mtd: MTD device structure
517 * @ofs: offset from device start
518 * @getchip: 0, if the chip is already selected
519 * @allowbbt: 1, if its allowed to access the bbt area
521 * Check, if the block is bad. Either by reading the bad block table or
522 * calling of the scan function.
524 static int nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
526 struct nand_chip *this = mtd->priv;
529 return this->block_bad(mtd, ofs, getchip);
531 /* Return info from the table */
532 return nand_isbad_bbt (mtd, ofs, allowbbt);
536 * nand_command - [DEFAULT] Send command to NAND device
537 * @mtd: MTD device structure
538 * @command: the command to be sent
539 * @column: the column address for this command, -1 if none
540 * @page_addr: the page address for this command, -1 if none
542 * Send command to NAND device. This function is used for small page
543 * devices (256/512 Bytes per page)
545 static void nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
547 register struct nand_chip *this = mtd->priv;
549 /* Begin command latch cycle */
550 this->hwcontrol(mtd, NAND_CTL_SETCLE);
552 * Write out the command to the device.
554 if (command == NAND_CMD_SEQIN) {
557 if (column >= mtd->oobblock) {
559 column -= mtd->oobblock;
560 readcmd = NAND_CMD_READOOB;
561 } else if (column < 256) {
562 /* First 256 bytes --> READ0 */
563 readcmd = NAND_CMD_READ0;
566 readcmd = NAND_CMD_READ1;
568 this->write_byte(mtd, readcmd);
570 this->write_byte(mtd, command);
572 /* Set ALE and clear CLE to start address cycle */
573 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
575 if (column != -1 || page_addr != -1) {
576 this->hwcontrol(mtd, NAND_CTL_SETALE);
578 /* Serially input address */
580 /* Adjust columns for 16 bit buswidth */
581 if (this->options & NAND_BUSWIDTH_16)
583 this->write_byte(mtd, column);
585 if (page_addr != -1) {
586 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
587 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
588 /* One more address cycle for devices > 32MiB */
589 if (this->chipsize > (32 << 20))
590 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));
592 /* Latch in address */
593 this->hwcontrol(mtd, NAND_CTL_CLRALE);
597 * program and erase have their own busy handlers
598 * status and sequential in needs no delay
602 case NAND_CMD_PAGEPROG:
603 case NAND_CMD_ERASE1:
604 case NAND_CMD_ERASE2:
606 case NAND_CMD_STATUS:
612 udelay(this->chip_delay);
613 this->hwcontrol(mtd, NAND_CTL_SETCLE);
614 this->write_byte(mtd, NAND_CMD_STATUS);
615 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
616 while ( !(this->read_byte(mtd) & 0x40));
619 /* This applies to read commands */
622 * If we don't have access to the busy pin, we apply the given
625 if (!this->dev_ready) {
626 udelay (this->chip_delay);
631 /* Apply this short delay always to ensure that we do wait tWB in
632 * any case on any machine. */
634 /* wait until command is processed */
635 while (!this->dev_ready(mtd));
639 * nand_command_lp - [DEFAULT] Send command to NAND large page device
640 * @mtd: MTD device structure
641 * @command: the command to be sent
642 * @column: the column address for this command, -1 if none
643 * @page_addr: the page address for this command, -1 if none
645 * Send command to NAND device. This is the version for the new large page devices
646 * We dont have the seperate regions as we have in the small page devices.
647 * We must emulate NAND_CMD_READOOB to keep the code compatible.
650 static void nand_command_lp (struct mtd_info *mtd, unsigned command, int column, int page_addr)
652 register struct nand_chip *this = mtd->priv;
654 /* Emulate NAND_CMD_READOOB */
655 if (command == NAND_CMD_READOOB) {
656 column += mtd->oobblock;
657 command = NAND_CMD_READ0;
661 /* Begin command latch cycle */
662 this->hwcontrol(mtd, NAND_CTL_SETCLE);
663 /* Write out the command to the device. */
664 this->write_byte(mtd, command);
665 /* End command latch cycle */
666 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
668 if (column != -1 || page_addr != -1) {
669 this->hwcontrol(mtd, NAND_CTL_SETALE);
671 /* Serially input address */
673 /* Adjust columns for 16 bit buswidth */
674 if (this->options & NAND_BUSWIDTH_16)
676 this->write_byte(mtd, column & 0xff);
677 this->write_byte(mtd, column >> 8);
679 if (page_addr != -1) {
680 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
681 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
682 /* One more address cycle for devices > 128MiB */
683 if (this->chipsize > (128 << 20))
684 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0xff));
686 /* Latch in address */
687 this->hwcontrol(mtd, NAND_CTL_CLRALE);
691 * program and erase have their own busy handlers
692 * status and sequential in needs no delay
696 case NAND_CMD_CACHEDPROG:
697 case NAND_CMD_PAGEPROG:
698 case NAND_CMD_ERASE1:
699 case NAND_CMD_ERASE2:
701 case NAND_CMD_STATUS:
708 udelay(this->chip_delay);
709 this->hwcontrol(mtd, NAND_CTL_SETCLE);
710 this->write_byte(mtd, NAND_CMD_STATUS);
711 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
712 while ( !(this->read_byte(mtd) & 0x40));
716 /* Begin command latch cycle */
717 this->hwcontrol(mtd, NAND_CTL_SETCLE);
718 /* Write out the start read command */
719 this->write_byte(mtd, NAND_CMD_READSTART);
720 /* End command latch cycle */
721 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
722 /* Fall through into ready check */
724 /* This applies to read commands */
727 * If we don't have access to the busy pin, we apply the given
730 if (!this->dev_ready) {
731 udelay (this->chip_delay);
736 /* Apply this short delay always to ensure that we do wait tWB in
737 * any case on any machine. */
739 /* wait until command is processed */
740 while (!this->dev_ready(mtd));
744 * nand_get_device - [GENERIC] Get chip for selected access
745 * @this: the nand chip descriptor
746 * @mtd: MTD device structure
747 * @new_state: the state which is requested
749 * Get the device and lock it for exclusive access
753 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
755 struct nand_chip *active = this;
757 DECLARE_WAITQUEUE (wait, current);
760 * Grab the lock and see if the device is available
763 /* Hardware controller shared among independend devices */
764 if (this->controller) {
765 spin_lock (&this->controller->lock);
766 if (this->controller->active)
767 active = this->controller->active;
769 this->controller->active = this;
770 spin_unlock (&this->controller->lock);
773 if (active == this) {
774 spin_lock (&this->chip_lock);
775 if (this->state == FL_READY) {
776 this->state = new_state;
777 spin_unlock (&this->chip_lock);
781 set_current_state (TASK_UNINTERRUPTIBLE);
782 add_wait_queue (&active->wq, &wait);
783 spin_unlock (&active->chip_lock);
785 remove_wait_queue (&active->wq, &wait);
789 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state) {}
793 * nand_wait - [DEFAULT] wait until the command is done
794 * @mtd: MTD device structure
795 * @this: NAND chip structure
796 * @state: state to select the max. timeout value
798 * Wait for command done. This applies to erase and program only
799 * Erase can take up to 400ms and program up to 20ms according to
800 * general NAND and SmartMedia specs
805 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
807 unsigned long timeo = jiffies;
810 if (state == FL_ERASING)
811 timeo += (HZ * 400) / 1000;
813 timeo += (HZ * 20) / 1000;
815 /* Apply this short delay always to ensure that we do wait tWB in
816 * any case on any machine. */
819 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
820 this->cmdfunc (mtd, NAND_CMD_STATUS_MULTI, -1, -1);
822 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
824 while (time_before(jiffies, timeo)) {
825 /* Check, if we were interrupted */
826 if (this->state != state)
829 if (this->dev_ready) {
830 if (this->dev_ready(mtd))
833 if (this->read_byte(mtd) & NAND_STATUS_READY)
838 status = (int) this->read_byte(mtd);
844 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
848 if (state == FL_ERASING)
849 timeo = (CFG_HZ * 400) / 1000;
851 timeo = (CFG_HZ * 20) / 1000;
853 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
854 this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
856 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
861 if (get_timer(0) > timeo) {
866 if (this->dev_ready) {
867 if (this->dev_ready(mtd))
870 if (this->read_byte(mtd) & NAND_STATUS_READY)
874 #ifdef PPCHAMELON_NAND_TIMER_HACK
876 while (get_timer(0) < 10);
877 #endif /* PPCHAMELON_NAND_TIMER_HACK */
879 return this->read_byte(mtd);
884 * nand_write_page - [GENERIC] write one page
885 * @mtd: MTD device structure
886 * @this: NAND chip structure
887 * @page: startpage inside the chip, must be called with (page & this->pagemask)
888 * @oob_buf: out of band data buffer
889 * @oobsel: out of band selecttion structre
890 * @cached: 1 = enable cached programming if supported by chip
892 * Nand_page_program function is used for write and writev !
893 * This function will always program a full page of data
894 * If you call it with a non page aligned buffer, you're lost :)
896 * Cached programming is not supported yet.
898 static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page,
899 u_char *oob_buf, struct nand_oobinfo *oobsel, int cached)
902 u_char ecc_code[NAND_MAX_OOBSIZE];
903 int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
904 uint *oob_config = oobsel->eccpos;
905 int datidx = 0, eccidx = 0, eccsteps = this->eccsteps;
908 /* FIXME: Enable cached programming */
911 /* Send command to begin auto page programming */
912 this->cmdfunc (mtd, NAND_CMD_SEQIN, 0x00, page);
914 /* Write out complete page of data, take care of eccmode */
916 /* No ecc, write all */
918 printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
919 this->write_buf(mtd, this->data_poi, mtd->oobblock);
922 /* Software ecc 3/256, write all */
924 for (; eccsteps; eccsteps--) {
925 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
926 for (i = 0; i < 3; i++, eccidx++)
927 oob_buf[oob_config[eccidx]] = ecc_code[i];
928 datidx += this->eccsize;
930 this->write_buf(mtd, this->data_poi, mtd->oobblock);
933 eccbytes = this->eccbytes;
934 for (; eccsteps; eccsteps--) {
935 /* enable hardware ecc logic for write */
936 this->enable_hwecc(mtd, NAND_ECC_WRITE);
937 this->write_buf(mtd, &this->data_poi[datidx], this->eccsize);
938 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
939 for (i = 0; i < eccbytes; i++, eccidx++)
940 oob_buf[oob_config[eccidx]] = ecc_code[i];
941 /* If the hardware ecc provides syndromes then
942 * the ecc code must be written immediately after
943 * the data bytes (words) */
944 if (this->options & NAND_HWECC_SYNDROME)
945 this->write_buf(mtd, ecc_code, eccbytes);
946 datidx += this->eccsize;
951 /* Write out OOB data */
952 if (this->options & NAND_HWECC_SYNDROME)
953 this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
955 this->write_buf(mtd, oob_buf, mtd->oobsize);
957 /* Send command to actually program the data */
958 this->cmdfunc (mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
961 /* call wait ready function */
962 status = this->waitfunc (mtd, this, FL_WRITING);
963 /* See if device thinks it succeeded */
965 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
969 /* FIXME: Implement cached programming ! */
970 /* wait until cache is ready*/
971 /* status = this->waitfunc (mtd, this, FL_CACHEDRPG); */
976 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
978 * nand_verify_pages - [GENERIC] verify the chip contents after a write
979 * @mtd: MTD device structure
980 * @this: NAND chip structure
981 * @page: startpage inside the chip, must be called with (page & this->pagemask)
982 * @numpages: number of pages to verify
983 * @oob_buf: out of band data buffer
984 * @oobsel: out of band selecttion structre
985 * @chipnr: number of the current chip
986 * @oobmode: 1 = full buffer verify, 0 = ecc only
988 * The NAND device assumes that it is always writing to a cleanly erased page.
989 * Hence, it performs its internal write verification only on bits that
990 * transitioned from 1 to 0. The device does NOT verify the whole page on a
991 * byte by byte basis. It is possible that the page was not completely erased
992 * or the page is becoming unusable due to wear. The read with ECC would catch
993 * the error later when the ECC page check fails, but we would rather catch
994 * it early in the page write stage. Better to write no data than invalid data.
996 static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
997 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
999 int i, j, datidx = 0, oobofs = 0, res = -EIO;
1000 int eccsteps = this->eccsteps;
1004 hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
1006 /* Send command to read back the first page */
1007 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page);
1010 for (j = 0; j < eccsteps; j++) {
1011 /* Loop through and verify the data */
1012 if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
1013 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1016 datidx += mtd->eccsize;
1017 /* Have we a hw generator layout ? */
1020 if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
1021 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1024 oobofs += hweccbytes;
1027 /* check, if we must compare all data or if we just have to
1028 * compare the ecc bytes
1031 if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
1032 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1036 /* Read always, else autoincrement fails */
1037 this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
1039 if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
1040 int ecccnt = oobsel->eccbytes;
1042 for (i = 0; i < ecccnt; i++) {
1043 int idx = oobsel->eccpos[i];
1044 if (oobdata[idx] != oob_buf[oobofs + idx] ) {
1045 DEBUG (MTD_DEBUG_LEVEL0,
1046 "%s: Failed ECC write "
1047 "verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__, page, i);
1053 oobofs += mtd->oobsize - hweccbytes * eccsteps;
1057 /* Apply delay or wait for ready/busy pin
1058 * Do this before the AUTOINCR check, so no problems
1059 * arise if a chip which does auto increment
1060 * is marked as NOAUTOINCR by the board driver.
1061 * Do this also before returning, so the chip is
1062 * ready for the next command.
1064 if (!this->dev_ready)
1065 udelay (this->chip_delay);
1067 while (!this->dev_ready(mtd));
1069 /* All done, return happy */
1074 /* Check, if the chip supports auto page increment */
1075 if (!NAND_CANAUTOINCR(this))
1076 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1079 * Terminate the read command. We come here in case of an error
1080 * So we must issue a reset command.
1083 this->cmdfunc (mtd, NAND_CMD_RESET, -1, -1);
1089 * nand_read - [MTD Interface] MTD compability function for nand_read_ecc
1090 * @mtd: MTD device structure
1091 * @from: offset to read from
1092 * @len: number of bytes to read
1093 * @retlen: pointer to variable to store the number of read bytes
1094 * @buf: the databuffer to put data
1096 * This function simply calls nand_read_ecc with oob buffer and oobsel = NULL
1098 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1100 return nand_read_ecc (mtd, from, len, retlen, buf, NULL, NULL);
1105 * nand_read_ecc - [MTD Interface] Read data with ECC
1106 * @mtd: MTD device structure
1107 * @from: offset to read from
1108 * @len: number of bytes to read
1109 * @retlen: pointer to variable to store the number of read bytes
1110 * @buf: the databuffer to put data
1111 * @oob_buf: filesystem supplied oob data buffer
1112 * @oobsel: oob selection structure
1114 * NAND read with ECC
1116 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
1117 size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel)
1119 int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
1120 int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
1121 struct nand_chip *this = mtd->priv;
1122 u_char *data_poi, *oob_data = oob_buf;
1123 u_char ecc_calc[NAND_MAX_OOBSIZE];
1124 u_char ecc_code[NAND_MAX_OOBSIZE];
1125 int eccmode, eccsteps;
1126 unsigned *oob_config;
1128 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1134 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1136 /* Do not allow reads past end of device */
1137 if ((from + len) > mtd->size) {
1138 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
1143 /* Grab the lock and see if the device is available */
1144 nand_get_device (this, mtd ,FL_READING);
1146 /* use userspace supplied oobinfo, if zero */
1148 oobsel = &mtd->oobinfo;
1150 /* Autoplace of oob data ? Use the default placement scheme */
1151 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1152 oobsel = this->autooob;
1154 eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
1155 oob_config = oobsel->eccpos;
1157 /* Select the NAND device */
1158 chipnr = (int)(from >> this->chip_shift);
1159 this->select_chip(mtd, chipnr);
1161 /* First we calculate the starting page */
1162 realpage = (int) (from >> this->page_shift);
1163 page = realpage & this->pagemask;
1165 /* Get raw starting column */
1166 col = from & (mtd->oobblock - 1);
1168 end = mtd->oobblock;
1169 ecc = this->eccsize;
1170 eccbytes = this->eccbytes;
1172 if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
1175 oobreadlen = mtd->oobsize;
1176 if (this->options & NAND_HWECC_SYNDROME)
1177 oobreadlen -= oobsel->eccbytes;
1179 /* Loop until all data read */
1180 while (read < len) {
1182 int aligned = (!col && (len - read) >= end);
1184 * If the read is not page aligned, we have to read into data buffer
1185 * due to ecc, else we read into return buffer direct
1188 data_poi = &buf[read];
1190 data_poi = this->data_buf;
1192 /* Check, if we have this page in the buffer
1194 * FIXME: Make it work when we must provide oob data too,
1195 * check the usage of data_buf oob field
1197 if (realpage == this->pagebuf && !oob_buf) {
1198 /* aligned read ? */
1200 memcpy (data_poi, this->data_buf, end);
1204 /* Check, if we must send the read command */
1206 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1210 /* get oob area, if we have no oob buffer from fs-driver */
1211 if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE ||
1212 oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1213 oob_data = &this->data_buf[end];
1215 eccsteps = this->eccsteps;
1218 case NAND_ECC_NONE: { /* No ECC, Read in a page */
1219 /* XXX U-BOOT XXX */
1221 static unsigned long lastwhinge = 0;
1222 if ((lastwhinge / HZ) != (jiffies / HZ)) {
1223 printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended\n");
1224 lastwhinge = jiffies;
1227 puts("Reading data from NAND FLASH without ECC is not recommended\n");
1229 this->read_buf(mtd, data_poi, end);
1233 case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
1234 this->read_buf(mtd, data_poi, end);
1235 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc)
1236 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1240 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) {
1241 this->enable_hwecc(mtd, NAND_ECC_READ);
1242 this->read_buf(mtd, &data_poi[datidx], ecc);
1244 /* HW ecc with syndrome calculation must read the
1245 * syndrome from flash immidiately after the data */
1247 /* Some hw ecc generators need to know when the
1248 * syndrome is read from flash */
1249 this->enable_hwecc(mtd, NAND_ECC_READSYN);
1250 this->read_buf(mtd, &oob_data[i], eccbytes);
1251 /* We calc error correction directly, it checks the hw
1252 * generator for an error, reads back the syndrome and
1253 * does the error correction on the fly */
1254 if (this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]) == -1) {
1255 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: "
1256 "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
1260 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1267 this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
1269 /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
1273 /* Pick the ECC bytes out of the oob data */
1274 for (j = 0; j < oobsel->eccbytes; j++)
1275 ecc_code[j] = oob_data[oob_config[j]];
1277 /* correct data, if neccecary */
1278 for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {
1279 ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
1281 /* Get next chunk of ecc bytes */
1284 /* Check, if we have a fs supplied oob-buffer,
1285 * This is the legacy mode. Used by YAFFS1
1286 * Should go away some day
1288 if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
1289 int *p = (int *)(&oob_data[mtd->oobsize]);
1293 if (ecc_status == -1) {
1294 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
1300 /* check, if we have a fs supplied oob-buffer */
1302 /* without autoplace. Legacy mode used by YAFFS1 */
1303 switch(oobsel->useecc) {
1304 case MTD_NANDECC_AUTOPLACE:
1305 case MTD_NANDECC_AUTOPL_USR:
1306 /* Walk through the autoplace chunks */
1307 for (i = 0, j = 0; j < mtd->oobavail; i++) {
1308 int from = oobsel->oobfree[i][0];
1309 int num = oobsel->oobfree[i][1];
1310 memcpy(&oob_buf[oob+j], &oob_data[from], num);
1313 oob += mtd->oobavail;
1315 case MTD_NANDECC_PLACE:
1316 /* YAFFS1 legacy mode */
1317 oob_data += this->eccsteps * sizeof (int);
1319 oob_data += mtd->oobsize;
1323 /* Partial page read, transfer data into fs buffer */
1325 for (j = col; j < end && read < len; j++)
1326 buf[read++] = data_poi[j];
1327 this->pagebuf = realpage;
1329 read += mtd->oobblock;
1331 /* Apply delay or wait for ready/busy pin
1332 * Do this before the AUTOINCR check, so no problems
1333 * arise if a chip which does auto increment
1334 * is marked as NOAUTOINCR by the board driver.
1336 if (!this->dev_ready)
1337 udelay (this->chip_delay);
1339 while (!this->dev_ready(mtd));
1344 /* For subsequent reads align to page boundary. */
1346 /* Increment page address */
1349 page = realpage & this->pagemask;
1350 /* Check, if we cross a chip boundary */
1353 this->select_chip(mtd, -1);
1354 this->select_chip(mtd, chipnr);
1356 /* Check, if the chip supports auto page increment
1357 * or if we have hit a block boundary.
1359 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1363 /* Deselect and wake up anyone waiting on the device */
1364 nand_release_device(mtd);
1367 * Return success, if no ECC failures, else -EBADMSG
1368 * fs driver will take care of that, because
1369 * retlen == desired len and result == -EBADMSG
1372 return ecc_failed ? -EBADMSG : 0;
1376 * nand_read_oob - [MTD Interface] NAND read out-of-band
1377 * @mtd: MTD device structure
1378 * @from: offset to read from
1379 * @len: number of bytes to read
1380 * @retlen: pointer to variable to store the number of read bytes
1381 * @buf: the databuffer to put data
1383 * NAND read out-of-band data from the spare area
1385 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1387 int i, col, page, chipnr;
1388 struct nand_chip *this = mtd->priv;
1389 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1391 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1393 /* Shift to get page */
1394 page = (int)(from >> this->page_shift);
1395 chipnr = (int)(from >> this->chip_shift);
1397 /* Mask to get column */
1398 col = from & (mtd->oobsize - 1);
1400 /* Initialize return length value */
1403 /* Do not allow reads past end of device */
1404 if ((from + len) > mtd->size) {
1405 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n");
1410 /* Grab the lock and see if the device is available */
1411 nand_get_device (this, mtd , FL_READING);
1413 /* Select the NAND device */
1414 this->select_chip(mtd, chipnr);
1416 /* Send the read command */
1417 this->cmdfunc (mtd, NAND_CMD_READOOB, col, page & this->pagemask);
1419 * Read the data, if we read more than one page
1420 * oob data, let the device transfer the data !
1424 int thislen = mtd->oobsize - col;
1425 thislen = min_t(int, thislen, len);
1426 this->read_buf(mtd, &buf[i], thislen);
1429 /* Apply delay or wait for ready/busy pin
1430 * Do this before the AUTOINCR check, so no problems
1431 * arise if a chip which does auto increment
1432 * is marked as NOAUTOINCR by the board driver.
1434 if (!this->dev_ready)
1435 udelay (this->chip_delay);
1437 while (!this->dev_ready(mtd));
1444 /* Check, if we cross a chip boundary */
1445 if (!(page & this->pagemask)) {
1447 this->select_chip(mtd, -1);
1448 this->select_chip(mtd, chipnr);
1451 /* Check, if the chip supports auto page increment
1452 * or if we have hit a block boundary.
1454 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
1455 /* For subsequent page reads set offset to 0 */
1456 this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
1461 /* Deselect and wake up anyone waiting on the device */
1462 nand_release_device(mtd);
1470 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
1471 * @mtd: MTD device structure
1472 * @buf: temporary buffer
1473 * @from: offset to read from
1474 * @len: number of bytes to read
1475 * @ooblen: number of oob data bytes to read
1477 * Read raw data including oob into buffer
1479 int nand_read_raw (struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
1481 struct nand_chip *this = mtd->priv;
1482 int page = (int) (from >> this->page_shift);
1483 int chip = (int) (from >> this->chip_shift);
1486 int pagesize = mtd->oobblock + mtd->oobsize;
1487 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1489 /* Do not allow reads past end of device */
1490 if ((from + len) > mtd->size) {
1491 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n");
1495 /* Grab the lock and see if the device is available */
1496 nand_get_device (this, mtd , FL_READING);
1498 this->select_chip (mtd, chip);
1500 /* Add requested oob length */
1505 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page & this->pagemask);
1508 this->read_buf (mtd, &buf[cnt], pagesize);
1514 if (!this->dev_ready)
1515 udelay (this->chip_delay);
1517 while (!this->dev_ready(mtd));
1519 /* Check, if the chip supports auto page increment */
1520 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1524 /* Deselect and wake up anyone waiting on the device */
1525 nand_release_device(mtd);
1531 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
1532 * @mtd: MTD device structure
1533 * @fsbuf: buffer given by fs driver
1534 * @oobsel: out of band selection structre
1535 * @autoplace: 1 = place given buffer into the oob bytes
1536 * @numpages: number of pages to prepare
1539 * 1. Filesystem buffer available and autoplacement is off,
1540 * return filesystem buffer
1541 * 2. No filesystem buffer or autoplace is off, return internal
1543 * 3. Filesystem buffer is given and autoplace selected
1544 * put data from fs buffer into internal buffer and
1545 * retrun internal buffer
1547 * Note: The internal buffer is filled with 0xff. This must
1548 * be done only once, when no autoplacement happens
1549 * Autoplacement sets the buffer dirty flag, which
1550 * forces the 0xff fill before using the buffer again.
1553 static u_char * nand_prepare_oobbuf (struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel,
1554 int autoplace, int numpages)
1556 struct nand_chip *this = mtd->priv;
1559 /* Zero copy fs supplied buffer */
1560 if (fsbuf && !autoplace)
1563 /* Check, if the buffer must be filled with ff again */
1564 if (this->oobdirty) {
1565 memset (this->oob_buf, 0xff,
1566 mtd->oobsize << (this->phys_erase_shift - this->page_shift));
1570 /* If we have no autoplacement or no fs buffer use the internal one */
1571 if (!autoplace || !fsbuf)
1572 return this->oob_buf;
1574 /* Walk through the pages and place the data */
1577 while (numpages--) {
1578 for (i = 0, len = 0; len < mtd->oobavail; i++) {
1579 int to = ofs + oobsel->oobfree[i][0];
1580 int num = oobsel->oobfree[i][1];
1581 memcpy (&this->oob_buf[to], fsbuf, num);
1585 ofs += mtd->oobavail;
1587 return this->oob_buf;
1590 #define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
1593 * nand_write - [MTD Interface] compability function for nand_write_ecc
1594 * @mtd: MTD device structure
1595 * @to: offset to write to
1596 * @len: number of bytes to write
1597 * @retlen: pointer to variable to store the number of written bytes
1598 * @buf: the data to write
1600 * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
1603 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1605 return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));
1609 * nand_write_ecc - [MTD Interface] NAND write with ECC
1610 * @mtd: MTD device structure
1611 * @to: offset to write to
1612 * @len: number of bytes to write
1613 * @retlen: pointer to variable to store the number of written bytes
1614 * @buf: the data to write
1615 * @eccbuf: filesystem supplied oob data buffer
1616 * @oobsel: oob selection structure
1618 * NAND write with ECC
1620 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
1621 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
1623 int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
1624 int autoplace = 0, numpages, totalpages;
1625 struct nand_chip *this = mtd->priv;
1626 u_char *oobbuf, *bufstart;
1627 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1629 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1631 /* Initialize retlen, in case of early exit */
1634 /* Do not allow write past end of device */
1635 if ((to + len) > mtd->size) {
1636 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n");
1640 /* reject writes, which are not page aligned */
1641 if (NOTALIGNED (to) || NOTALIGNED(len)) {
1642 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1646 /* Grab the lock and see if the device is available */
1647 nand_get_device (this, mtd, FL_WRITING);
1649 /* Calculate chipnr */
1650 chipnr = (int)(to >> this->chip_shift);
1651 /* Select the NAND device */
1652 this->select_chip(mtd, chipnr);
1654 /* Check, if it is write protected */
1655 if (nand_check_wp(mtd)) {
1656 printk (KERN_NOTICE "nand_write_ecc: Device is write protected\n");
1660 /* if oobsel is NULL, use chip defaults */
1662 oobsel = &mtd->oobinfo;
1664 /* Autoplace of oob data ? Use the default placement scheme */
1665 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1666 oobsel = this->autooob;
1669 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1672 /* Setup variables and oob buffer */
1673 totalpages = len >> this->page_shift;
1674 page = (int) (to >> this->page_shift);
1675 /* Invalidate the page cache, if we write to the cached page */
1676 if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
1679 /* Set it relative to chip */
1680 page &= this->pagemask;
1682 /* Calc number of pages we can write in one go */
1683 numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages);
1684 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages);
1685 bufstart = (u_char *)buf;
1687 /* Loop until all data is written */
1688 while (written < len) {
1690 this->data_poi = (u_char*) &buf[written];
1691 /* Write one page. If this is the last page to write
1692 * or the last page in this block, then use the
1693 * real pageprogram command, else select cached programming
1694 * if supported by the chip.
1696 ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
1698 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret);
1702 oob += mtd->oobsize;
1703 /* Update written bytes count */
1704 written += mtd->oobblock;
1708 /* Increment page address */
1711 /* Have we hit a block boundary ? Then we have to verify and
1712 * if verify is ok, we have to setup the oob buffer for
1715 if (!(page & (ppblock - 1))){
1717 this->data_poi = bufstart;
1718 ret = nand_verify_pages (mtd, this, startpage,
1720 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1722 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1726 bufstart = (u_char*) &buf[written];
1728 ofs = autoplace ? mtd->oobavail : mtd->oobsize;
1730 eccbuf += (page - startpage) * ofs;
1731 totalpages -= page - startpage;
1732 numpages = min (totalpages, ppblock);
1733 page &= this->pagemask;
1737 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel,
1738 autoplace, numpages);
1739 /* Check, if we cross a chip boundary */
1742 this->select_chip(mtd, -1);
1743 this->select_chip(mtd, chipnr);
1747 /* Verify the remaining pages */
1749 this->data_poi = bufstart;
1750 ret = nand_verify_pages (mtd, this, startpage, totalpages,
1751 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1755 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1758 /* Deselect and wake up anyone waiting on the device */
1759 nand_release_device(mtd);
1766 * nand_write_oob - [MTD Interface] NAND write out-of-band
1767 * @mtd: MTD device structure
1768 * @to: offset to write to
1769 * @len: number of bytes to write
1770 * @retlen: pointer to variable to store the number of written bytes
1771 * @buf: the data to write
1773 * NAND write out-of-band
1775 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1777 int column, page, status, ret = -EIO, chipnr;
1778 struct nand_chip *this = mtd->priv;
1780 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1782 /* Shift to get page */
1783 page = (int) (to >> this->page_shift);
1784 chipnr = (int) (to >> this->chip_shift);
1786 /* Mask to get column */
1787 column = to & (mtd->oobsize - 1);
1789 /* Initialize return length value */
1792 /* Do not allow write past end of page */
1793 if ((column + len) > mtd->oobsize) {
1794 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
1798 /* Grab the lock and see if the device is available */
1799 nand_get_device (this, mtd, FL_WRITING);
1801 /* Select the NAND device */
1802 this->select_chip(mtd, chipnr);
1804 /* Reset the chip. Some chips (like the Toshiba TC5832DC found
1805 in one of my DiskOnChip 2000 test units) will clear the whole
1806 data page too if we don't do this. I have no clue why, but
1807 I seem to have 'fixed' it in the doc2000 driver in
1808 August 1999. dwmw2. */
1809 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1811 /* Check, if it is write protected */
1812 if (nand_check_wp(mtd))
1815 /* Invalidate the page cache, if we write to the cached page */
1816 if (page == this->pagebuf)
1819 if (NAND_MUST_PAD(this)) {
1820 /* Write out desired data */
1821 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
1823 if (!(ffchars = kmalloc (mtd->oobsize, GFP_KERNEL))) {
1824 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
1825 "No memory for padding array, need %d bytes", mtd->oobsize);
1829 memset(ffchars, 0xff, mtd->oobsize);
1831 /* prepad 0xff for partial programming */
1832 this->write_buf(mtd, ffchars, column);
1834 this->write_buf(mtd, buf, len);
1835 /* postpad 0xff for partial programming */
1836 this->write_buf(mtd, ffchars, mtd->oobsize - (len+column));
1838 /* Write out desired data */
1839 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
1841 this->write_buf(mtd, buf, len);
1843 /* Send command to program the OOB data */
1844 this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1);
1846 status = this->waitfunc (mtd, this, FL_WRITING);
1848 /* See if device thinks it succeeded */
1849 if (status & 0x01) {
1850 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
1857 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1858 /* Send command to read back the data */
1859 this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask);
1861 if (this->verify_buf(mtd, buf, len)) {
1862 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
1869 /* Deselect and wake up anyone waiting on the device */
1870 nand_release_device(mtd);
1875 /* XXX U-BOOT XXX */
1878 * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
1879 * @mtd: MTD device structure
1880 * @vecs: the iovectors to write
1881 * @count: number of vectors
1882 * @to: offset to write to
1883 * @retlen: pointer to variable to store the number of written bytes
1885 * NAND write with kvec. This just calls the ecc function
1887 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1888 loff_t to, size_t * retlen)
1890 return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL));
1894 * nand_writev_ecc - [MTD Interface] write with iovec with ecc
1895 * @mtd: MTD device structure
1896 * @vecs: the iovectors to write
1897 * @count: number of vectors
1898 * @to: offset to write to
1899 * @retlen: pointer to variable to store the number of written bytes
1900 * @eccbuf: filesystem supplied oob data buffer
1901 * @oobsel: oob selection structure
1903 * NAND write with iovec with ecc
1905 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1906 loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
1908 int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
1909 int oob, numpages, autoplace = 0, startpage;
1910 struct nand_chip *this = mtd->priv;
1911 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1912 u_char *oobbuf, *bufstart;
1914 /* Preset written len for early exit */
1917 /* Calculate total length of data */
1919 for (i = 0; i < count; i++)
1920 total_len += (int) vecs[i].iov_len;
1922 DEBUG (MTD_DEBUG_LEVEL3,
1923 "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
1925 /* Do not allow write past end of page */
1926 if ((to + total_len) > mtd->size) {
1927 DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
1931 /* reject writes, which are not page aligned */
1932 if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
1933 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1937 /* Grab the lock and see if the device is available */
1938 nand_get_device (this, mtd, FL_WRITING);
1940 /* Get the current chip-nr */
1941 chipnr = (int) (to >> this->chip_shift);
1942 /* Select the NAND device */
1943 this->select_chip(mtd, chipnr);
1945 /* Check, if it is write protected */
1946 if (nand_check_wp(mtd))
1949 /* if oobsel is NULL, use chip defaults */
1951 oobsel = &mtd->oobinfo;
1953 /* Autoplace of oob data ? Use the default placement scheme */
1954 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1955 oobsel = this->autooob;
1958 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1961 /* Setup start page */
1962 page = (int) (to >> this->page_shift);
1963 /* Invalidate the page cache, if we write to the cached page */
1964 if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))
1967 startpage = page & this->pagemask;
1969 /* Loop until all kvec' data has been written */
1972 /* If the given tuple is >= pagesize then
1973 * write it out from the iov
1975 if ((vecs->iov_len - len) >= mtd->oobblock) {
1976 /* Calc number of pages we can write
1977 * out of this iov in one go */
1978 numpages = (vecs->iov_len - len) >> this->page_shift;
1979 /* Do not cross block boundaries */
1980 numpages = min (ppblock - (startpage & (ppblock - 1)), numpages);
1981 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1982 bufstart = (u_char *)vecs->iov_base;
1984 this->data_poi = bufstart;
1986 for (i = 1; i <= numpages; i++) {
1987 /* Write one page. If this is the last page to write
1988 * then use the real pageprogram command, else select
1989 * cached programming if supported by the chip.
1991 ret = nand_write_page (mtd, this, page & this->pagemask,
1992 &oobbuf[oob], oobsel, i != numpages);
1995 this->data_poi += mtd->oobblock;
1996 len += mtd->oobblock;
1997 oob += mtd->oobsize;
2000 /* Check, if we have to switch to the next tuple */
2001 if (len >= (int) vecs->iov_len) {
2007 /* We must use the internal buffer, read data out of each
2008 * tuple until we have a full page to write
2011 while (cnt < mtd->oobblock) {
2012 if (vecs->iov_base != NULL && vecs->iov_len)
2013 this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
2014 /* Check, if we have to switch to the next tuple */
2015 if (len >= (int) vecs->iov_len) {
2021 this->pagebuf = page;
2022 this->data_poi = this->data_buf;
2023 bufstart = this->data_poi;
2025 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
2026 ret = nand_write_page (mtd, this, page & this->pagemask,
2033 this->data_poi = bufstart;
2034 ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
2038 written += mtd->oobblock * numpages;
2043 startpage = page & this->pagemask;
2044 /* Check, if we cross a chip boundary */
2047 this->select_chip(mtd, -1);
2048 this->select_chip(mtd, chipnr);
2053 /* Deselect and wake up anyone waiting on the device */
2054 nand_release_device(mtd);
2062 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2063 * @mtd: MTD device structure
2064 * @page: the page address of the block which will be erased
2066 * Standard erase command for NAND chips
2068 static void single_erase_cmd (struct mtd_info *mtd, int page)
2070 struct nand_chip *this = mtd->priv;
2071 /* Send commands to erase a block */
2072 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2073 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2077 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2078 * @mtd: MTD device structure
2079 * @page: the page address of the block which will be erased
2081 * AND multi block erase command function
2082 * Erase 4 consecutive blocks
2084 static void multi_erase_cmd (struct mtd_info *mtd, int page)
2086 struct nand_chip *this = mtd->priv;
2087 /* Send commands to erase a block */
2088 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2089 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2090 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2091 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2092 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2096 * nand_erase - [MTD Interface] erase block(s)
2097 * @mtd: MTD device structure
2098 * @instr: erase instruction
2100 * Erase one ore more blocks
2102 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
2104 return nand_erase_nand (mtd, instr, 0);
2108 * nand_erase_intern - [NAND Interface] erase block(s)
2109 * @mtd: MTD device structure
2110 * @instr: erase instruction
2111 * @allowbbt: allow erasing the bbt area
2113 * Erase one ore more blocks
2115 int nand_erase_nand (struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
2117 int page, len, status, pages_per_block, ret, chipnr;
2118 struct nand_chip *this = mtd->priv;
2120 DEBUG (MTD_DEBUG_LEVEL3,
2121 "nand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
2123 /* Start address must align on block boundary */
2124 if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
2125 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2129 /* Length must align on block boundary */
2130 if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
2131 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
2135 /* Do not allow erase past end of device */
2136 if ((instr->len + instr->addr) > mtd->size) {
2137 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
2141 instr->fail_addr = 0xffffffff;
2143 /* Grab the lock and see if the device is available */
2144 nand_get_device (this, mtd, FL_ERASING);
2146 /* Shift to get first page */
2147 page = (int) (instr->addr >> this->page_shift);
2148 chipnr = (int) (instr->addr >> this->chip_shift);
2150 /* Calculate pages in each block */
2151 pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
2153 /* Select the NAND device */
2154 this->select_chip(mtd, chipnr);
2156 /* Check the WP bit */
2157 /* Check, if it is write protected */
2158 if (nand_check_wp(mtd)) {
2159 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
2160 instr->state = MTD_ERASE_FAILED;
2164 /* Loop through the pages */
2167 instr->state = MTD_ERASING;
2170 #ifndef NAND_ALLOW_ERASE_ALL
2171 /* Check if we have a bad block, we do not erase bad blocks ! */
2172 if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
2173 printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
2174 instr->state = MTD_ERASE_FAILED;
2178 /* Invalidate the page cache, if we erase the block which contains
2179 the current cached page */
2180 if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
2183 this->erase_cmd (mtd, page & this->pagemask);
2185 status = this->waitfunc (mtd, this, FL_ERASING);
2187 /* See if block erase succeeded */
2188 if (status & 0x01) {
2189 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
2190 instr->state = MTD_ERASE_FAILED;
2191 instr->fail_addr = (page << this->page_shift);
2195 /* Increment page address and decrement length */
2196 len -= (1 << this->phys_erase_shift);
2197 page += pages_per_block;
2199 /* Check, if we cross a chip boundary */
2200 if (len && !(page & this->pagemask)) {
2202 this->select_chip(mtd, -1);
2203 this->select_chip(mtd, chipnr);
2206 instr->state = MTD_ERASE_DONE;
2210 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2211 /* Do call back function */
2213 mtd_erase_callback(instr);
2215 /* Deselect and wake up anyone waiting on the device */
2216 nand_release_device(mtd);
2218 /* Return more or less happy */
2223 * nand_sync - [MTD Interface] sync
2224 * @mtd: MTD device structure
2226 * Sync is actually a wait for chip ready function
2228 static void nand_sync (struct mtd_info *mtd)
2230 struct nand_chip *this = mtd->priv;
2232 DEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2234 /* Grab the lock and see if the device is available */
2235 nand_get_device (this, mtd, FL_SYNCING);
2236 /* Release it and go back */
2237 nand_release_device (mtd);
2242 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2243 * @mtd: MTD device structure
2244 * @ofs: offset relative to mtd start
2246 static int nand_block_isbad (struct mtd_info *mtd, loff_t ofs)
2248 /* Check for invalid offset */
2249 if (ofs > mtd->size)
2252 return nand_block_checkbad (mtd, ofs, 1, 0);
2256 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2257 * @mtd: MTD device structure
2258 * @ofs: offset relative to mtd start
2260 static int nand_block_markbad (struct mtd_info *mtd, loff_t ofs)
2262 struct nand_chip *this = mtd->priv;
2265 if ((ret = nand_block_isbad(mtd, ofs))) {
2266 /* If it was bad already, return success and do nothing. */
2272 return this->block_markbad(mtd, ofs);
2276 * nand_scan - [NAND Interface] Scan for the NAND device
2277 * @mtd: MTD device structure
2278 * @maxchips: Number of chips to scan for
2280 * This fills out all the not initialized function pointers
2281 * with the defaults.
2282 * The flash ID is read and the mtd/chip structures are
2283 * filled with the appropriate values. Buffers are allocated if
2284 * they are not provided by the board driver
2287 int nand_scan (struct mtd_info *mtd, int maxchips)
2289 int i, j, nand_maf_id, nand_dev_id, busw;
2290 struct nand_chip *this = mtd->priv;
2292 /* Get buswidth to select the correct functions*/
2293 busw = this->options & NAND_BUSWIDTH_16;
2295 /* check for proper chip_delay setup, set 20us if not */
2296 if (!this->chip_delay)
2297 this->chip_delay = 20;
2299 /* check, if a user supplied command function given */
2300 if (this->cmdfunc == NULL)
2301 this->cmdfunc = nand_command;
2303 /* check, if a user supplied wait function given */
2304 if (this->waitfunc == NULL)
2305 this->waitfunc = nand_wait;
2307 if (!this->select_chip)
2308 this->select_chip = nand_select_chip;
2309 if (!this->write_byte)
2310 this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2311 if (!this->read_byte)
2312 this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2313 if (!this->write_word)
2314 this->write_word = nand_write_word;
2315 if (!this->read_word)
2316 this->read_word = nand_read_word;
2317 if (!this->block_bad)
2318 this->block_bad = nand_block_bad;
2319 if (!this->block_markbad)
2320 this->block_markbad = nand_default_block_markbad;
2321 if (!this->write_buf)
2322 this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2323 if (!this->read_buf)
2324 this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2325 if (!this->verify_buf)
2326 this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2327 if (!this->scan_bbt)
2328 this->scan_bbt = nand_default_bbt;
2330 /* Select the device */
2331 this->select_chip(mtd, 0);
2333 /* Send the command for reading device ID */
2334 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2336 /* Read manufacturer and device IDs */
2337 nand_maf_id = this->read_byte(mtd);
2338 nand_dev_id = this->read_byte(mtd);
2340 /* Print and store flash device information */
2341 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2343 if (nand_dev_id != nand_flash_ids[i].id)
2346 if (!mtd->name) mtd->name = nand_flash_ids[i].name;
2347 this->chipsize = nand_flash_ids[i].chipsize << 20;
2349 /* New devices have all the information in additional id bytes */
2350 if (!nand_flash_ids[i].pagesize) {
2352 /* The 3rd id byte contains non relevant data ATM */
2353 extid = this->read_byte(mtd);
2354 /* The 4th id byte is the important one */
2355 extid = this->read_byte(mtd);
2357 mtd->oobblock = 1024 << (extid & 0x3);
2360 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->oobblock / 512);
2362 /* Calc blocksize. Blocksize is multiples of 64KiB */
2363 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2365 /* Get buswidth information */
2366 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2369 /* Old devices have this data hardcoded in the
2370 * device id table */
2371 mtd->erasesize = nand_flash_ids[i].erasesize;
2372 mtd->oobblock = nand_flash_ids[i].pagesize;
2373 mtd->oobsize = mtd->oobblock / 32;
2374 busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
2377 /* Check, if buswidth is correct. Hardware drivers should set
2379 if (busw != (this->options & NAND_BUSWIDTH_16)) {
2380 printk (KERN_INFO "NAND device: Manufacturer ID:"
2381 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2382 nand_manuf_ids[i].name , mtd->name);
2383 printk (KERN_WARNING
2384 "NAND bus width %d instead %d bit\n",
2385 (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
2387 this->select_chip(mtd, -1);
2391 /* Calculate the address shift from the page size */
2392 this->page_shift = ffs(mtd->oobblock) - 1;
2393 this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
2394 this->chip_shift = ffs(this->chipsize) - 1;
2396 /* Set the bad block position */
2397 this->badblockpos = mtd->oobblock > 512 ?
2398 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2400 /* Get chip options, preserve non chip based options */
2401 this->options &= ~NAND_CHIPOPTIONS_MSK;
2402 this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
2403 /* Set this as a default. Board drivers can override it, if neccecary */
2404 this->options |= NAND_NO_AUTOINCR;
2405 /* Check if this is a not a samsung device. Do not clear the options
2406 * for chips which are not having an extended id.
2408 if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
2409 this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2411 /* Check for AND chips with 4 page planes */
2412 if (this->options & NAND_4PAGE_ARRAY)
2413 this->erase_cmd = multi_erase_cmd;
2415 this->erase_cmd = single_erase_cmd;
2417 /* Do not replace user supplied command function ! */
2418 if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
2419 this->cmdfunc = nand_command_lp;
2421 /* Try to identify manufacturer */
2422 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
2423 if (nand_manuf_ids[j].id == nand_maf_id)
2429 if (!nand_flash_ids[i].name) {
2430 #ifndef CFG_NAND_QUIET_TEST
2431 printk (KERN_WARNING "No NAND device found!!!\n");
2433 this->select_chip(mtd, -1);
2437 for (i=1; i < maxchips; i++) {
2438 this->select_chip(mtd, i);
2440 /* Send the command for reading device ID */
2441 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2443 /* Read manufacturer and device IDs */
2444 if (nand_maf_id != this->read_byte(mtd) ||
2445 nand_dev_id != this->read_byte(mtd))
2449 printk(KERN_INFO "%d NAND chips detected\n", i);
2451 /* Allocate buffers, if neccecary */
2452 if (!this->oob_buf) {
2454 len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
2455 this->oob_buf = kmalloc (len, GFP_KERNEL);
2456 if (!this->oob_buf) {
2457 printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
2460 this->options |= NAND_OOBBUF_ALLOC;
2463 if (!this->data_buf) {
2465 len = mtd->oobblock + mtd->oobsize;
2466 this->data_buf = kmalloc (len, GFP_KERNEL);
2467 if (!this->data_buf) {
2468 if (this->options & NAND_OOBBUF_ALLOC)
2469 kfree (this->oob_buf);
2470 printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
2473 this->options |= NAND_DATABUF_ALLOC;
2476 /* Store the number of chips and calc total size for mtd */
2478 mtd->size = i * this->chipsize;
2479 /* Convert chipsize to number of pages per chip -1. */
2480 this->pagemask = (this->chipsize >> this->page_shift) - 1;
2481 /* Preset the internal oob buffer */
2482 memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
2484 /* If no default placement scheme is given, select an
2485 * appropriate one */
2486 if (!this->autooob) {
2487 /* Select the appropriate default oob placement scheme for
2488 * placement agnostic filesystems */
2489 switch (mtd->oobsize) {
2491 this->autooob = &nand_oob_8;
2494 this->autooob = &nand_oob_16;
2497 this->autooob = &nand_oob_64;
2500 this->autooob = &nand_oob_128;
2503 printk (KERN_WARNING "No oob scheme defined for oobsize %d\n",
2509 /* The number of bytes available for the filesystem to place fs dependend
2512 for (i=0; this->autooob->oobfree[i][1]; i++)
2513 mtd->oobavail += this->autooob->oobfree[i][1];
2516 * check ECC mode, default to software
2517 * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
2518 * fallback to software ECC
2520 this->eccsize = 256; /* set default eccsize */
2523 switch (this->eccmode) {
2524 case NAND_ECC_HW12_2048:
2525 if (mtd->oobblock < 2048) {
2526 printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
2528 this->eccmode = NAND_ECC_SOFT;
2529 this->calculate_ecc = nand_calculate_ecc;
2530 this->correct_data = nand_correct_data;
2532 this->eccsize = 2048;
2535 case NAND_ECC_HW3_512:
2536 case NAND_ECC_HW6_512:
2537 case NAND_ECC_HW8_512:
2538 if (mtd->oobblock == 256) {
2539 printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
2540 this->eccmode = NAND_ECC_SOFT;
2541 this->calculate_ecc = nand_calculate_ecc;
2542 this->correct_data = nand_correct_data;
2544 this->eccsize = 512; /* set eccsize to 512 */
2547 case NAND_ECC_HW3_256:
2551 printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
2552 this->eccmode = NAND_ECC_NONE;
2556 this->calculate_ecc = nand_calculate_ecc;
2557 this->correct_data = nand_correct_data;
2561 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
2565 /* Check hardware ecc function availability and adjust number of ecc bytes per
2568 switch (this->eccmode) {
2569 case NAND_ECC_HW12_2048:
2570 this->eccbytes += 4;
2571 case NAND_ECC_HW8_512:
2572 this->eccbytes += 2;
2573 case NAND_ECC_HW6_512:
2574 this->eccbytes += 3;
2575 case NAND_ECC_HW3_512:
2576 case NAND_ECC_HW3_256:
2577 if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
2579 printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
2583 mtd->eccsize = this->eccsize;
2585 /* Set the number of read / write steps for one page to ensure ECC generation */
2586 switch (this->eccmode) {
2587 case NAND_ECC_HW12_2048:
2588 this->eccsteps = mtd->oobblock / 2048;
2590 case NAND_ECC_HW3_512:
2591 case NAND_ECC_HW6_512:
2592 case NAND_ECC_HW8_512:
2593 this->eccsteps = mtd->oobblock / 512;
2595 case NAND_ECC_HW3_256:
2597 this->eccsteps = mtd->oobblock / 256;
2605 /* XXX U-BOOT XXX */
2607 /* Initialize state, waitqueue and spinlock */
2608 this->state = FL_READY;
2609 init_waitqueue_head (&this->wq);
2610 spin_lock_init (&this->chip_lock);
2613 /* De-select the device */
2614 this->select_chip(mtd, -1);
2616 /* Invalidate the pagebuffer reference */
2619 /* Fill in remaining MTD driver data */
2620 mtd->type = MTD_NANDFLASH;
2621 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
2622 mtd->ecctype = MTD_ECC_SW;
2623 mtd->erase = nand_erase;
2625 mtd->unpoint = NULL;
2626 mtd->read = nand_read;
2627 mtd->write = nand_write;
2628 mtd->read_ecc = nand_read_ecc;
2629 mtd->write_ecc = nand_write_ecc;
2630 mtd->read_oob = nand_read_oob;
2631 mtd->write_oob = nand_write_oob;
2632 /* XXX U-BOOT XXX */
2635 mtd->writev = nand_writev;
2636 mtd->writev_ecc = nand_writev_ecc;
2638 mtd->sync = nand_sync;
2639 /* XXX U-BOOT XXX */
2643 mtd->suspend = NULL;
2646 mtd->block_isbad = nand_block_isbad;
2647 mtd->block_markbad = nand_block_markbad;
2649 /* and make the autooob the default one */
2650 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
2651 /* XXX U-BOOT XXX */
2653 mtd->owner = THIS_MODULE;
2655 /* Build bad block table */
2656 return this->scan_bbt (mtd);
2660 * nand_release - [NAND Interface] Free resources held by the NAND device
2661 * @mtd: MTD device structure
2663 void nand_release (struct mtd_info *mtd)
2665 struct nand_chip *this = mtd->priv;
2667 #ifdef CONFIG_MTD_PARTITIONS
2668 /* Deregister partitions */
2669 del_mtd_partitions (mtd);
2671 /* Deregister the device */
2672 /* XXX U-BOOT XXX */
2674 del_mtd_device (mtd);
2676 /* Free bad block table memory, if allocated */
2679 /* Buffer allocated by nand_scan ? */
2680 if (this->options & NAND_OOBBUF_ALLOC)
2681 kfree (this->oob_buf);
2682 /* Buffer allocated by nand_scan ? */
2683 if (this->options & NAND_DATABUF_ALLOC)
2684 kfree (this->data_buf);