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 MTDDEBUG (MTD_DEBUG_LEVEL0,
966 "%s: Failed write, page 0x%08x, ",
971 /* FIXME: Implement cached programming ! */
972 /* wait until cache is ready*/
973 /* status = this->waitfunc (mtd, this, FL_CACHEDRPG); */
978 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
980 * nand_verify_pages - [GENERIC] verify the chip contents after a write
981 * @mtd: MTD device structure
982 * @this: NAND chip structure
983 * @page: startpage inside the chip, must be called with (page & this->pagemask)
984 * @numpages: number of pages to verify
985 * @oob_buf: out of band data buffer
986 * @oobsel: out of band selecttion structre
987 * @chipnr: number of the current chip
988 * @oobmode: 1 = full buffer verify, 0 = ecc only
990 * The NAND device assumes that it is always writing to a cleanly erased page.
991 * Hence, it performs its internal write verification only on bits that
992 * transitioned from 1 to 0. The device does NOT verify the whole page on a
993 * byte by byte basis. It is possible that the page was not completely erased
994 * or the page is becoming unusable due to wear. The read with ECC would catch
995 * the error later when the ECC page check fails, but we would rather catch
996 * it early in the page write stage. Better to write no data than invalid data.
998 static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
999 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
1001 int i, j, datidx = 0, oobofs = 0, res = -EIO;
1002 int eccsteps = this->eccsteps;
1006 hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
1008 /* Send command to read back the first page */
1009 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page);
1012 for (j = 0; j < eccsteps; j++) {
1013 /* Loop through and verify the data */
1014 if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
1015 MTDDEBUG (MTD_DEBUG_LEVEL0, "%s: "
1016 "Failed write verify, page 0x%08x ",
1017 __FUNCTION__, page);
1020 datidx += mtd->eccsize;
1021 /* Have we a hw generator layout ? */
1024 if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
1025 MTDDEBUG (MTD_DEBUG_LEVEL0, "%s: "
1026 "Failed write verify, page 0x%08x ",
1027 __FUNCTION__, page);
1030 oobofs += hweccbytes;
1033 /* check, if we must compare all data or if we just have to
1034 * compare the ecc bytes
1037 if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
1038 MTDDEBUG (MTD_DEBUG_LEVEL0, "%s: "
1039 "Failed write verify, page 0x%08x ",
1040 __FUNCTION__, page);
1044 /* Read always, else autoincrement fails */
1045 this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
1047 if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
1048 int ecccnt = oobsel->eccbytes;
1050 for (i = 0; i < ecccnt; i++) {
1051 int idx = oobsel->eccpos[i];
1052 if (oobdata[idx] != oob_buf[oobofs + idx] ) {
1053 MTDDEBUG (MTD_DEBUG_LEVEL0,
1054 "%s: Failed ECC write "
1055 "verify, page 0x%08x, "
1056 "%6i bytes were succesful\n",
1057 __FUNCTION__, page, i);
1063 oobofs += mtd->oobsize - hweccbytes * eccsteps;
1067 /* Apply delay or wait for ready/busy pin
1068 * Do this before the AUTOINCR check, so no problems
1069 * arise if a chip which does auto increment
1070 * is marked as NOAUTOINCR by the board driver.
1071 * Do this also before returning, so the chip is
1072 * ready for the next command.
1074 if (!this->dev_ready)
1075 udelay (this->chip_delay);
1077 while (!this->dev_ready(mtd));
1079 /* All done, return happy */
1084 /* Check, if the chip supports auto page increment */
1085 if (!NAND_CANAUTOINCR(this))
1086 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1089 * Terminate the read command. We come here in case of an error
1090 * So we must issue a reset command.
1093 this->cmdfunc (mtd, NAND_CMD_RESET, -1, -1);
1099 * nand_read - [MTD Interface] MTD compability function for nand_read_ecc
1100 * @mtd: MTD device structure
1101 * @from: offset to read from
1102 * @len: number of bytes to read
1103 * @retlen: pointer to variable to store the number of read bytes
1104 * @buf: the databuffer to put data
1106 * This function simply calls nand_read_ecc with oob buffer and oobsel = NULL
1108 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1110 return nand_read_ecc (mtd, from, len, retlen, buf, NULL, NULL);
1115 * nand_read_ecc - [MTD Interface] Read data with ECC
1116 * @mtd: MTD device structure
1117 * @from: offset to read from
1118 * @len: number of bytes to read
1119 * @retlen: pointer to variable to store the number of read bytes
1120 * @buf: the databuffer to put data
1121 * @oob_buf: filesystem supplied oob data buffer
1122 * @oobsel: oob selection structure
1124 * NAND read with ECC
1126 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
1127 size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel)
1129 int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
1130 int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
1131 struct nand_chip *this = mtd->priv;
1132 u_char *data_poi, *oob_data = oob_buf;
1133 u_char ecc_calc[NAND_MAX_OOBSIZE];
1134 u_char ecc_code[NAND_MAX_OOBSIZE];
1135 int eccmode, eccsteps;
1136 unsigned *oob_config;
1138 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1144 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n",
1145 (unsigned int) from, (int) len);
1147 /* Do not allow reads past end of device */
1148 if ((from + len) > mtd->size) {
1149 MTDDEBUG (MTD_DEBUG_LEVEL0,
1150 "nand_read_ecc: Attempt read beyond end of device\n");
1155 /* Grab the lock and see if the device is available */
1156 nand_get_device (this, mtd ,FL_READING);
1158 /* use userspace supplied oobinfo, if zero */
1160 oobsel = &mtd->oobinfo;
1162 /* Autoplace of oob data ? Use the default placement scheme */
1163 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1164 oobsel = this->autooob;
1166 eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
1167 oob_config = oobsel->eccpos;
1169 /* Select the NAND device */
1170 chipnr = (int)(from >> this->chip_shift);
1171 this->select_chip(mtd, chipnr);
1173 /* First we calculate the starting page */
1174 realpage = (int) (from >> this->page_shift);
1175 page = realpage & this->pagemask;
1177 /* Get raw starting column */
1178 col = from & (mtd->oobblock - 1);
1180 end = mtd->oobblock;
1181 ecc = this->eccsize;
1182 eccbytes = this->eccbytes;
1184 if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
1187 oobreadlen = mtd->oobsize;
1188 if (this->options & NAND_HWECC_SYNDROME)
1189 oobreadlen -= oobsel->eccbytes;
1191 /* Loop until all data read */
1192 while (read < len) {
1194 int aligned = (!col && (len - read) >= end);
1196 * If the read is not page aligned, we have to read into data buffer
1197 * due to ecc, else we read into return buffer direct
1200 data_poi = &buf[read];
1202 data_poi = this->data_buf;
1204 /* Check, if we have this page in the buffer
1206 * FIXME: Make it work when we must provide oob data too,
1207 * check the usage of data_buf oob field
1209 if (realpage == this->pagebuf && !oob_buf) {
1210 /* aligned read ? */
1212 memcpy (data_poi, this->data_buf, end);
1216 /* Check, if we must send the read command */
1218 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1222 /* get oob area, if we have no oob buffer from fs-driver */
1223 if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE ||
1224 oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1225 oob_data = &this->data_buf[end];
1227 eccsteps = this->eccsteps;
1230 case NAND_ECC_NONE: { /* No ECC, Read in a page */
1231 /* XXX U-BOOT XXX */
1233 static unsigned long lastwhinge = 0;
1234 if ((lastwhinge / HZ) != (jiffies / HZ)) {
1235 printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended\n");
1236 lastwhinge = jiffies;
1239 puts("Reading data from NAND FLASH without ECC is not recommended\n");
1241 this->read_buf(mtd, data_poi, end);
1245 case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
1246 this->read_buf(mtd, data_poi, end);
1247 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc)
1248 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1252 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) {
1253 this->enable_hwecc(mtd, NAND_ECC_READ);
1254 this->read_buf(mtd, &data_poi[datidx], ecc);
1256 /* HW ecc with syndrome calculation must read the
1257 * syndrome from flash immidiately after the data */
1259 /* Some hw ecc generators need to know when the
1260 * syndrome is read from flash */
1261 this->enable_hwecc(mtd, NAND_ECC_READSYN);
1262 this->read_buf(mtd, &oob_data[i], eccbytes);
1263 /* We calc error correction directly, it checks the hw
1264 * generator for an error, reads back the syndrome and
1265 * does the error correction on the fly */
1266 if (this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]) == -1) {
1267 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: "
1268 "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
1272 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1279 this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
1281 /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
1285 /* Pick the ECC bytes out of the oob data */
1286 for (j = 0; j < oobsel->eccbytes; j++)
1287 ecc_code[j] = oob_data[oob_config[j]];
1289 /* correct data, if neccecary */
1290 for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {
1291 ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
1293 /* Get next chunk of ecc bytes */
1296 /* Check, if we have a fs supplied oob-buffer,
1297 * This is the legacy mode. Used by YAFFS1
1298 * Should go away some day
1300 if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
1301 int *p = (int *)(&oob_data[mtd->oobsize]);
1305 if (ecc_status == -1) {
1306 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: "
1307 "Failed ECC read, page 0x%08x\n",
1314 /* check, if we have a fs supplied oob-buffer */
1316 /* without autoplace. Legacy mode used by YAFFS1 */
1317 switch(oobsel->useecc) {
1318 case MTD_NANDECC_AUTOPLACE:
1319 case MTD_NANDECC_AUTOPL_USR:
1320 /* Walk through the autoplace chunks */
1321 for (i = 0, j = 0; j < mtd->oobavail; i++) {
1322 int from = oobsel->oobfree[i][0];
1323 int num = oobsel->oobfree[i][1];
1324 memcpy(&oob_buf[oob+j], &oob_data[from], num);
1327 oob += mtd->oobavail;
1329 case MTD_NANDECC_PLACE:
1330 /* YAFFS1 legacy mode */
1331 oob_data += this->eccsteps * sizeof (int);
1333 oob_data += mtd->oobsize;
1337 /* Partial page read, transfer data into fs buffer */
1339 for (j = col; j < end && read < len; j++)
1340 buf[read++] = data_poi[j];
1341 this->pagebuf = realpage;
1343 read += mtd->oobblock;
1345 /* Apply delay or wait for ready/busy pin
1346 * Do this before the AUTOINCR check, so no problems
1347 * arise if a chip which does auto increment
1348 * is marked as NOAUTOINCR by the board driver.
1350 if (!this->dev_ready)
1351 udelay (this->chip_delay);
1353 while (!this->dev_ready(mtd));
1358 /* For subsequent reads align to page boundary. */
1360 /* Increment page address */
1363 page = realpage & this->pagemask;
1364 /* Check, if we cross a chip boundary */
1367 this->select_chip(mtd, -1);
1368 this->select_chip(mtd, chipnr);
1370 /* Check, if the chip supports auto page increment
1371 * or if we have hit a block boundary.
1373 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1377 /* Deselect and wake up anyone waiting on the device */
1378 nand_release_device(mtd);
1381 * Return success, if no ECC failures, else -EBADMSG
1382 * fs driver will take care of that, because
1383 * retlen == desired len and result == -EBADMSG
1386 return ecc_failed ? -EBADMSG : 0;
1390 * nand_read_oob - [MTD Interface] NAND read out-of-band
1391 * @mtd: MTD device structure
1392 * @from: offset to read from
1393 * @len: number of bytes to read
1394 * @retlen: pointer to variable to store the number of read bytes
1395 * @buf: the databuffer to put data
1397 * NAND read out-of-band data from the spare area
1399 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1401 int i, col, page, chipnr;
1402 struct nand_chip *this = mtd->priv;
1403 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1405 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n",
1406 (unsigned int) from, (int) len);
1408 /* Shift to get page */
1409 page = (int)(from >> this->page_shift);
1410 chipnr = (int)(from >> this->chip_shift);
1412 /* Mask to get column */
1413 col = from & (mtd->oobsize - 1);
1415 /* Initialize return length value */
1418 /* Do not allow reads past end of device */
1419 if ((from + len) > mtd->size) {
1420 MTDDEBUG (MTD_DEBUG_LEVEL0,
1421 "nand_read_oob: Attempt read beyond end of device\n");
1426 /* Grab the lock and see if the device is available */
1427 nand_get_device (this, mtd , FL_READING);
1429 /* Select the NAND device */
1430 this->select_chip(mtd, chipnr);
1432 /* Send the read command */
1433 this->cmdfunc (mtd, NAND_CMD_READOOB, col, page & this->pagemask);
1435 * Read the data, if we read more than one page
1436 * oob data, let the device transfer the data !
1440 int thislen = mtd->oobsize - col;
1441 thislen = min_t(int, thislen, len);
1442 this->read_buf(mtd, &buf[i], thislen);
1445 /* Apply delay or wait for ready/busy pin
1446 * Do this before the AUTOINCR check, so no problems
1447 * arise if a chip which does auto increment
1448 * is marked as NOAUTOINCR by the board driver.
1450 if (!this->dev_ready)
1451 udelay (this->chip_delay);
1453 while (!this->dev_ready(mtd));
1460 /* Check, if we cross a chip boundary */
1461 if (!(page & this->pagemask)) {
1463 this->select_chip(mtd, -1);
1464 this->select_chip(mtd, chipnr);
1467 /* Check, if the chip supports auto page increment
1468 * or if we have hit a block boundary.
1470 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
1471 /* For subsequent page reads set offset to 0 */
1472 this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
1477 /* Deselect and wake up anyone waiting on the device */
1478 nand_release_device(mtd);
1486 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
1487 * @mtd: MTD device structure
1488 * @buf: temporary buffer
1489 * @from: offset to read from
1490 * @len: number of bytes to read
1491 * @ooblen: number of oob data bytes to read
1493 * Read raw data including oob into buffer
1495 int nand_read_raw (struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
1497 struct nand_chip *this = mtd->priv;
1498 int page = (int) (from >> this->page_shift);
1499 int chip = (int) (from >> this->chip_shift);
1502 int pagesize = mtd->oobblock + mtd->oobsize;
1503 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1505 /* Do not allow reads past end of device */
1506 if ((from + len) > mtd->size) {
1507 MTDDEBUG (MTD_DEBUG_LEVEL0,
1508 "nand_read_raw: Attempt read beyond end of device\n");
1512 /* Grab the lock and see if the device is available */
1513 nand_get_device (this, mtd , FL_READING);
1515 this->select_chip (mtd, chip);
1517 /* Add requested oob length */
1522 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page & this->pagemask);
1525 this->read_buf (mtd, &buf[cnt], pagesize);
1531 if (!this->dev_ready)
1532 udelay (this->chip_delay);
1534 while (!this->dev_ready(mtd));
1536 /* Check, if the chip supports auto page increment */
1537 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1541 /* Deselect and wake up anyone waiting on the device */
1542 nand_release_device(mtd);
1548 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
1549 * @mtd: MTD device structure
1550 * @fsbuf: buffer given by fs driver
1551 * @oobsel: out of band selection structre
1552 * @autoplace: 1 = place given buffer into the oob bytes
1553 * @numpages: number of pages to prepare
1556 * 1. Filesystem buffer available and autoplacement is off,
1557 * return filesystem buffer
1558 * 2. No filesystem buffer or autoplace is off, return internal
1560 * 3. Filesystem buffer is given and autoplace selected
1561 * put data from fs buffer into internal buffer and
1562 * retrun internal buffer
1564 * Note: The internal buffer is filled with 0xff. This must
1565 * be done only once, when no autoplacement happens
1566 * Autoplacement sets the buffer dirty flag, which
1567 * forces the 0xff fill before using the buffer again.
1570 static u_char * nand_prepare_oobbuf (struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel,
1571 int autoplace, int numpages)
1573 struct nand_chip *this = mtd->priv;
1576 /* Zero copy fs supplied buffer */
1577 if (fsbuf && !autoplace)
1580 /* Check, if the buffer must be filled with ff again */
1581 if (this->oobdirty) {
1582 memset (this->oob_buf, 0xff,
1583 mtd->oobsize << (this->phys_erase_shift - this->page_shift));
1587 /* If we have no autoplacement or no fs buffer use the internal one */
1588 if (!autoplace || !fsbuf)
1589 return this->oob_buf;
1591 /* Walk through the pages and place the data */
1594 while (numpages--) {
1595 for (i = 0, len = 0; len < mtd->oobavail; i++) {
1596 int to = ofs + oobsel->oobfree[i][0];
1597 int num = oobsel->oobfree[i][1];
1598 memcpy (&this->oob_buf[to], fsbuf, num);
1602 ofs += mtd->oobavail;
1604 return this->oob_buf;
1607 #define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
1610 * nand_write - [MTD Interface] compability function for nand_write_ecc
1611 * @mtd: MTD device structure
1612 * @to: offset to write to
1613 * @len: number of bytes to write
1614 * @retlen: pointer to variable to store the number of written bytes
1615 * @buf: the data to write
1617 * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
1620 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1622 return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));
1626 * nand_write_ecc - [MTD Interface] NAND write with ECC
1627 * @mtd: MTD device structure
1628 * @to: offset to write to
1629 * @len: number of bytes to write
1630 * @retlen: pointer to variable to store the number of written bytes
1631 * @buf: the data to write
1632 * @eccbuf: filesystem supplied oob data buffer
1633 * @oobsel: oob selection structure
1635 * NAND write with ECC
1637 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
1638 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
1640 int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
1641 int autoplace = 0, numpages, totalpages;
1642 struct nand_chip *this = mtd->priv;
1643 u_char *oobbuf, *bufstart;
1644 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1646 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n",
1647 (unsigned int) to, (int) len);
1649 /* Initialize retlen, in case of early exit */
1652 /* Do not allow write past end of device */
1653 if ((to + len) > mtd->size) {
1654 MTDDEBUG (MTD_DEBUG_LEVEL0,
1655 "nand_write_ecc: Attempt to write past end of page\n");
1659 /* reject writes, which are not page aligned */
1660 if (NOTALIGNED (to) || NOTALIGNED(len)) {
1661 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1665 /* Grab the lock and see if the device is available */
1666 nand_get_device (this, mtd, FL_WRITING);
1668 /* Calculate chipnr */
1669 chipnr = (int)(to >> this->chip_shift);
1670 /* Select the NAND device */
1671 this->select_chip(mtd, chipnr);
1673 /* Check, if it is write protected */
1674 if (nand_check_wp(mtd)) {
1675 printk (KERN_NOTICE "nand_write_ecc: Device is write protected\n");
1679 /* if oobsel is NULL, use chip defaults */
1681 oobsel = &mtd->oobinfo;
1683 /* Autoplace of oob data ? Use the default placement scheme */
1684 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1685 oobsel = this->autooob;
1688 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1691 /* Setup variables and oob buffer */
1692 totalpages = len >> this->page_shift;
1693 page = (int) (to >> this->page_shift);
1694 /* Invalidate the page cache, if we write to the cached page */
1695 if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
1698 /* Set it relative to chip */
1699 page &= this->pagemask;
1701 /* Calc number of pages we can write in one go */
1702 numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages);
1703 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages);
1704 bufstart = (u_char *)buf;
1706 /* Loop until all data is written */
1707 while (written < len) {
1709 this->data_poi = (u_char*) &buf[written];
1710 /* Write one page. If this is the last page to write
1711 * or the last page in this block, then use the
1712 * real pageprogram command, else select cached programming
1713 * if supported by the chip.
1715 ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
1717 MTDDEBUG (MTD_DEBUG_LEVEL0,
1718 "nand_write_ecc: write_page failed %d\n", ret);
1722 oob += mtd->oobsize;
1723 /* Update written bytes count */
1724 written += mtd->oobblock;
1728 /* Increment page address */
1731 /* Have we hit a block boundary ? Then we have to verify and
1732 * if verify is ok, we have to setup the oob buffer for
1735 if (!(page & (ppblock - 1))){
1737 this->data_poi = bufstart;
1738 ret = nand_verify_pages (mtd, this, startpage,
1740 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1742 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: "
1743 "verify_pages failed %d\n", ret);
1747 bufstart = (u_char*) &buf[written];
1749 ofs = autoplace ? mtd->oobavail : mtd->oobsize;
1751 eccbuf += (page - startpage) * ofs;
1752 totalpages -= page - startpage;
1753 numpages = min (totalpages, ppblock);
1754 page &= this->pagemask;
1758 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel,
1759 autoplace, numpages);
1760 /* Check, if we cross a chip boundary */
1763 this->select_chip(mtd, -1);
1764 this->select_chip(mtd, chipnr);
1768 /* Verify the remaining pages */
1770 this->data_poi = bufstart;
1771 ret = nand_verify_pages (mtd, this, startpage, totalpages,
1772 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1776 MTDDEBUG (MTD_DEBUG_LEVEL0,
1777 "nand_write_ecc: verify_pages failed %d\n", ret);
1780 /* Deselect and wake up anyone waiting on the device */
1781 nand_release_device(mtd);
1788 * nand_write_oob - [MTD Interface] NAND write out-of-band
1789 * @mtd: MTD device structure
1790 * @to: offset to write to
1791 * @len: number of bytes to write
1792 * @retlen: pointer to variable to store the number of written bytes
1793 * @buf: the data to write
1795 * NAND write out-of-band
1797 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1799 int column, page, status, ret = -EIO, chipnr;
1800 struct nand_chip *this = mtd->priv;
1802 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
1803 (unsigned int) to, (int) len);
1805 /* Shift to get page */
1806 page = (int) (to >> this->page_shift);
1807 chipnr = (int) (to >> this->chip_shift);
1809 /* Mask to get column */
1810 column = to & (mtd->oobsize - 1);
1812 /* Initialize return length value */
1815 /* Do not allow write past end of page */
1816 if ((column + len) > mtd->oobsize) {
1817 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
1818 "Attempt to write past end of page\n");
1822 /* Grab the lock and see if the device is available */
1823 nand_get_device (this, mtd, FL_WRITING);
1825 /* Select the NAND device */
1826 this->select_chip(mtd, chipnr);
1828 /* Reset the chip. Some chips (like the Toshiba TC5832DC found
1829 in one of my DiskOnChip 2000 test units) will clear the whole
1830 data page too if we don't do this. I have no clue why, but
1831 I seem to have 'fixed' it in the doc2000 driver in
1832 August 1999. dwmw2. */
1833 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1835 /* Check, if it is write protected */
1836 if (nand_check_wp(mtd))
1839 /* Invalidate the page cache, if we write to the cached page */
1840 if (page == this->pagebuf)
1843 if (NAND_MUST_PAD(this)) {
1844 /* Write out desired data */
1845 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
1847 if (!(ffchars = kmalloc (mtd->oobsize, GFP_KERNEL))) {
1848 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
1849 "No memory for padding array, "
1850 "need %d bytes", mtd->oobsize);
1854 memset(ffchars, 0xff, mtd->oobsize);
1856 /* prepad 0xff for partial programming */
1857 this->write_buf(mtd, ffchars, column);
1859 this->write_buf(mtd, buf, len);
1860 /* postpad 0xff for partial programming */
1861 this->write_buf(mtd, ffchars, mtd->oobsize - (len+column));
1863 /* Write out desired data */
1864 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
1866 this->write_buf(mtd, buf, len);
1868 /* Send command to program the OOB data */
1869 this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1);
1871 status = this->waitfunc (mtd, this, FL_WRITING);
1873 /* See if device thinks it succeeded */
1874 if (status & 0x01) {
1875 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
1876 "Failed write, page 0x%08x\n", page);
1883 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1884 /* Send command to read back the data */
1885 this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask);
1887 if (this->verify_buf(mtd, buf, len)) {
1888 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
1889 "Failed write verify, page 0x%08x\n", page);
1896 /* Deselect and wake up anyone waiting on the device */
1897 nand_release_device(mtd);
1902 /* XXX U-BOOT XXX */
1905 * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
1906 * @mtd: MTD device structure
1907 * @vecs: the iovectors to write
1908 * @count: number of vectors
1909 * @to: offset to write to
1910 * @retlen: pointer to variable to store the number of written bytes
1912 * NAND write with kvec. This just calls the ecc function
1914 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1915 loff_t to, size_t * retlen)
1917 return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL));
1921 * nand_writev_ecc - [MTD Interface] write with iovec with ecc
1922 * @mtd: MTD device structure
1923 * @vecs: the iovectors to write
1924 * @count: number of vectors
1925 * @to: offset to write to
1926 * @retlen: pointer to variable to store the number of written bytes
1927 * @eccbuf: filesystem supplied oob data buffer
1928 * @oobsel: oob selection structure
1930 * NAND write with iovec with ecc
1932 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1933 loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
1935 int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
1936 int oob, numpages, autoplace = 0, startpage;
1937 struct nand_chip *this = mtd->priv;
1938 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1939 u_char *oobbuf, *bufstart;
1941 /* Preset written len for early exit */
1944 /* Calculate total length of data */
1946 for (i = 0; i < count; i++)
1947 total_len += (int) vecs[i].iov_len;
1949 MTDDEBUG (MTD_DEBUG_LEVEL3,
1950 "nand_writev: to = 0x%08x, len = %i, count = %ld\n",
1951 (unsigned int) to, (unsigned int) total_len, count);
1953 /* Do not allow write past end of page */
1954 if ((to + total_len) > mtd->size) {
1955 MTDDEBUG (MTD_DEBUG_LEVEL0,
1956 "nand_writev: Attempted write past end of device\n");
1960 /* reject writes, which are not page aligned */
1961 if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
1962 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1966 /* Grab the lock and see if the device is available */
1967 nand_get_device (this, mtd, FL_WRITING);
1969 /* Get the current chip-nr */
1970 chipnr = (int) (to >> this->chip_shift);
1971 /* Select the NAND device */
1972 this->select_chip(mtd, chipnr);
1974 /* Check, if it is write protected */
1975 if (nand_check_wp(mtd))
1978 /* if oobsel is NULL, use chip defaults */
1980 oobsel = &mtd->oobinfo;
1982 /* Autoplace of oob data ? Use the default placement scheme */
1983 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1984 oobsel = this->autooob;
1987 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1990 /* Setup start page */
1991 page = (int) (to >> this->page_shift);
1992 /* Invalidate the page cache, if we write to the cached page */
1993 if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))
1996 startpage = page & this->pagemask;
1998 /* Loop until all kvec' data has been written */
2001 /* If the given tuple is >= pagesize then
2002 * write it out from the iov
2004 if ((vecs->iov_len - len) >= mtd->oobblock) {
2005 /* Calc number of pages we can write
2006 * out of this iov in one go */
2007 numpages = (vecs->iov_len - len) >> this->page_shift;
2008 /* Do not cross block boundaries */
2009 numpages = min (ppblock - (startpage & (ppblock - 1)), numpages);
2010 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
2011 bufstart = (u_char *)vecs->iov_base;
2013 this->data_poi = bufstart;
2015 for (i = 1; i <= numpages; i++) {
2016 /* Write one page. If this is the last page to write
2017 * then use the real pageprogram command, else select
2018 * cached programming if supported by the chip.
2020 ret = nand_write_page (mtd, this, page & this->pagemask,
2021 &oobbuf[oob], oobsel, i != numpages);
2024 this->data_poi += mtd->oobblock;
2025 len += mtd->oobblock;
2026 oob += mtd->oobsize;
2029 /* Check, if we have to switch to the next tuple */
2030 if (len >= (int) vecs->iov_len) {
2036 /* We must use the internal buffer, read data out of each
2037 * tuple until we have a full page to write
2040 while (cnt < mtd->oobblock) {
2041 if (vecs->iov_base != NULL && vecs->iov_len)
2042 this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
2043 /* Check, if we have to switch to the next tuple */
2044 if (len >= (int) vecs->iov_len) {
2050 this->pagebuf = page;
2051 this->data_poi = this->data_buf;
2052 bufstart = this->data_poi;
2054 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
2055 ret = nand_write_page (mtd, this, page & this->pagemask,
2062 this->data_poi = bufstart;
2063 ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
2067 written += mtd->oobblock * numpages;
2072 startpage = page & this->pagemask;
2073 /* Check, if we cross a chip boundary */
2076 this->select_chip(mtd, -1);
2077 this->select_chip(mtd, chipnr);
2082 /* Deselect and wake up anyone waiting on the device */
2083 nand_release_device(mtd);
2091 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2092 * @mtd: MTD device structure
2093 * @page: the page address of the block which will be erased
2095 * Standard erase command for NAND chips
2097 static void single_erase_cmd (struct mtd_info *mtd, int page)
2099 struct nand_chip *this = mtd->priv;
2100 /* Send commands to erase a block */
2101 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2102 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2106 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2107 * @mtd: MTD device structure
2108 * @page: the page address of the block which will be erased
2110 * AND multi block erase command function
2111 * Erase 4 consecutive blocks
2113 static void multi_erase_cmd (struct mtd_info *mtd, int page)
2115 struct nand_chip *this = mtd->priv;
2116 /* Send commands to erase a block */
2117 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2118 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2119 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2120 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2121 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2125 * nand_erase - [MTD Interface] erase block(s)
2126 * @mtd: MTD device structure
2127 * @instr: erase instruction
2129 * Erase one ore more blocks
2131 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
2133 return nand_erase_nand (mtd, instr, 0);
2137 * nand_erase_intern - [NAND Interface] erase block(s)
2138 * @mtd: MTD device structure
2139 * @instr: erase instruction
2140 * @allowbbt: allow erasing the bbt area
2142 * Erase one ore more blocks
2144 int nand_erase_nand (struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
2146 int page, len, status, pages_per_block, ret, chipnr;
2147 struct nand_chip *this = mtd->priv;
2149 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
2150 (unsigned int) instr->addr, (unsigned int) instr->len);
2152 /* Start address must align on block boundary */
2153 if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
2154 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2158 /* Length must align on block boundary */
2159 if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
2160 MTDDEBUG (MTD_DEBUG_LEVEL0,
2161 "nand_erase: Length not block aligned\n");
2165 /* Do not allow erase past end of device */
2166 if ((instr->len + instr->addr) > mtd->size) {
2167 MTDDEBUG (MTD_DEBUG_LEVEL0,
2168 "nand_erase: Erase past end of device\n");
2172 instr->fail_addr = 0xffffffff;
2174 /* Grab the lock and see if the device is available */
2175 nand_get_device (this, mtd, FL_ERASING);
2177 /* Shift to get first page */
2178 page = (int) (instr->addr >> this->page_shift);
2179 chipnr = (int) (instr->addr >> this->chip_shift);
2181 /* Calculate pages in each block */
2182 pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
2184 /* Select the NAND device */
2185 this->select_chip(mtd, chipnr);
2187 /* Check the WP bit */
2188 /* Check, if it is write protected */
2189 if (nand_check_wp(mtd)) {
2190 MTDDEBUG (MTD_DEBUG_LEVEL0,
2191 "nand_erase: Device is write protected!!!\n");
2192 instr->state = MTD_ERASE_FAILED;
2196 /* Loop through the pages */
2199 instr->state = MTD_ERASING;
2202 #ifndef NAND_ALLOW_ERASE_ALL
2203 /* Check if we have a bad block, we do not erase bad blocks ! */
2204 if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
2205 printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
2206 instr->state = MTD_ERASE_FAILED;
2210 /* Invalidate the page cache, if we erase the block which contains
2211 the current cached page */
2212 if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
2215 this->erase_cmd (mtd, page & this->pagemask);
2217 status = this->waitfunc (mtd, this, FL_ERASING);
2219 /* See if block erase succeeded */
2220 if (status & 0x01) {
2221 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: "
2222 "Failed erase, page 0x%08x\n", page);
2223 instr->state = MTD_ERASE_FAILED;
2224 instr->fail_addr = (page << this->page_shift);
2228 /* Increment page address and decrement length */
2229 len -= (1 << this->phys_erase_shift);
2230 page += pages_per_block;
2232 /* Check, if we cross a chip boundary */
2233 if (len && !(page & this->pagemask)) {
2235 this->select_chip(mtd, -1);
2236 this->select_chip(mtd, chipnr);
2239 instr->state = MTD_ERASE_DONE;
2243 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2244 /* Do call back function */
2246 mtd_erase_callback(instr);
2248 /* Deselect and wake up anyone waiting on the device */
2249 nand_release_device(mtd);
2251 /* Return more or less happy */
2256 * nand_sync - [MTD Interface] sync
2257 * @mtd: MTD device structure
2259 * Sync is actually a wait for chip ready function
2261 static void nand_sync (struct mtd_info *mtd)
2263 struct nand_chip *this = mtd->priv;
2265 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2267 /* Grab the lock and see if the device is available */
2268 nand_get_device (this, mtd, FL_SYNCING);
2269 /* Release it and go back */
2270 nand_release_device (mtd);
2275 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2276 * @mtd: MTD device structure
2277 * @ofs: offset relative to mtd start
2279 static int nand_block_isbad (struct mtd_info *mtd, loff_t ofs)
2281 /* Check for invalid offset */
2282 if (ofs > mtd->size)
2285 return nand_block_checkbad (mtd, ofs, 1, 0);
2289 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2290 * @mtd: MTD device structure
2291 * @ofs: offset relative to mtd start
2293 static int nand_block_markbad (struct mtd_info *mtd, loff_t ofs)
2295 struct nand_chip *this = mtd->priv;
2298 if ((ret = nand_block_isbad(mtd, ofs))) {
2299 /* If it was bad already, return success and do nothing. */
2305 return this->block_markbad(mtd, ofs);
2309 * nand_scan - [NAND Interface] Scan for the NAND device
2310 * @mtd: MTD device structure
2311 * @maxchips: Number of chips to scan for
2313 * This fills out all the not initialized function pointers
2314 * with the defaults.
2315 * The flash ID is read and the mtd/chip structures are
2316 * filled with the appropriate values. Buffers are allocated if
2317 * they are not provided by the board driver
2320 int nand_scan (struct mtd_info *mtd, int maxchips)
2322 int i, j, nand_maf_id, nand_dev_id, busw;
2323 struct nand_chip *this = mtd->priv;
2325 /* Get buswidth to select the correct functions*/
2326 busw = this->options & NAND_BUSWIDTH_16;
2328 /* check for proper chip_delay setup, set 20us if not */
2329 if (!this->chip_delay)
2330 this->chip_delay = 20;
2332 /* check, if a user supplied command function given */
2333 if (this->cmdfunc == NULL)
2334 this->cmdfunc = nand_command;
2336 /* check, if a user supplied wait function given */
2337 if (this->waitfunc == NULL)
2338 this->waitfunc = nand_wait;
2340 if (!this->select_chip)
2341 this->select_chip = nand_select_chip;
2342 if (!this->write_byte)
2343 this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2344 if (!this->read_byte)
2345 this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2346 if (!this->write_word)
2347 this->write_word = nand_write_word;
2348 if (!this->read_word)
2349 this->read_word = nand_read_word;
2350 if (!this->block_bad)
2351 this->block_bad = nand_block_bad;
2352 if (!this->block_markbad)
2353 this->block_markbad = nand_default_block_markbad;
2354 if (!this->write_buf)
2355 this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2356 if (!this->read_buf)
2357 this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2358 if (!this->verify_buf)
2359 this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2360 if (!this->scan_bbt)
2361 this->scan_bbt = nand_default_bbt;
2363 /* Select the device */
2364 this->select_chip(mtd, 0);
2366 /* Send the command for reading device ID */
2367 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2369 /* Read manufacturer and device IDs */
2370 nand_maf_id = this->read_byte(mtd);
2371 nand_dev_id = this->read_byte(mtd);
2373 /* Print and store flash device information */
2374 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2376 if (nand_dev_id != nand_flash_ids[i].id)
2379 if (!mtd->name) mtd->name = nand_flash_ids[i].name;
2380 this->chipsize = nand_flash_ids[i].chipsize << 20;
2382 /* New devices have all the information in additional id bytes */
2383 if (!nand_flash_ids[i].pagesize) {
2385 /* The 3rd id byte contains non relevant data ATM */
2386 extid = this->read_byte(mtd);
2387 /* The 4th id byte is the important one */
2388 extid = this->read_byte(mtd);
2390 mtd->oobblock = 1024 << (extid & 0x3);
2393 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->oobblock / 512);
2395 /* Calc blocksize. Blocksize is multiples of 64KiB */
2396 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2398 /* Get buswidth information */
2399 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2402 /* Old devices have this data hardcoded in the
2403 * device id table */
2404 mtd->erasesize = nand_flash_ids[i].erasesize;
2405 mtd->oobblock = nand_flash_ids[i].pagesize;
2406 mtd->oobsize = mtd->oobblock / 32;
2407 busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
2410 /* Check, if buswidth is correct. Hardware drivers should set
2412 if (busw != (this->options & NAND_BUSWIDTH_16)) {
2413 printk (KERN_INFO "NAND device: Manufacturer ID:"
2414 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2415 nand_manuf_ids[i].name , mtd->name);
2416 printk (KERN_WARNING
2417 "NAND bus width %d instead %d bit\n",
2418 (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
2420 this->select_chip(mtd, -1);
2424 /* Calculate the address shift from the page size */
2425 this->page_shift = ffs(mtd->oobblock) - 1;
2426 this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
2427 this->chip_shift = ffs(this->chipsize) - 1;
2429 /* Set the bad block position */
2430 this->badblockpos = mtd->oobblock > 512 ?
2431 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2433 /* Get chip options, preserve non chip based options */
2434 this->options &= ~NAND_CHIPOPTIONS_MSK;
2435 this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
2436 /* Set this as a default. Board drivers can override it, if neccecary */
2437 this->options |= NAND_NO_AUTOINCR;
2438 /* Check if this is a not a samsung device. Do not clear the options
2439 * for chips which are not having an extended id.
2441 if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
2442 this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2444 /* Check for AND chips with 4 page planes */
2445 if (this->options & NAND_4PAGE_ARRAY)
2446 this->erase_cmd = multi_erase_cmd;
2448 this->erase_cmd = single_erase_cmd;
2450 /* Do not replace user supplied command function ! */
2451 if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
2452 this->cmdfunc = nand_command_lp;
2454 /* Try to identify manufacturer */
2455 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
2456 if (nand_manuf_ids[j].id == nand_maf_id)
2462 if (!nand_flash_ids[i].name) {
2463 #ifndef CFG_NAND_QUIET_TEST
2464 printk (KERN_WARNING "No NAND device found!!!\n");
2466 this->select_chip(mtd, -1);
2470 for (i=1; i < maxchips; i++) {
2471 this->select_chip(mtd, i);
2473 /* Send the command for reading device ID */
2474 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2476 /* Read manufacturer and device IDs */
2477 if (nand_maf_id != this->read_byte(mtd) ||
2478 nand_dev_id != this->read_byte(mtd))
2482 printk(KERN_INFO "%d NAND chips detected\n", i);
2484 /* Allocate buffers, if neccecary */
2485 if (!this->oob_buf) {
2487 len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
2488 this->oob_buf = kmalloc (len, GFP_KERNEL);
2489 if (!this->oob_buf) {
2490 printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
2493 this->options |= NAND_OOBBUF_ALLOC;
2496 if (!this->data_buf) {
2498 len = mtd->oobblock + mtd->oobsize;
2499 this->data_buf = kmalloc (len, GFP_KERNEL);
2500 if (!this->data_buf) {
2501 if (this->options & NAND_OOBBUF_ALLOC)
2502 kfree (this->oob_buf);
2503 printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
2506 this->options |= NAND_DATABUF_ALLOC;
2509 /* Store the number of chips and calc total size for mtd */
2511 mtd->size = i * this->chipsize;
2512 /* Convert chipsize to number of pages per chip -1. */
2513 this->pagemask = (this->chipsize >> this->page_shift) - 1;
2514 /* Preset the internal oob buffer */
2515 memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
2517 /* If no default placement scheme is given, select an
2518 * appropriate one */
2519 if (!this->autooob) {
2520 /* Select the appropriate default oob placement scheme for
2521 * placement agnostic filesystems */
2522 switch (mtd->oobsize) {
2524 this->autooob = &nand_oob_8;
2527 this->autooob = &nand_oob_16;
2530 this->autooob = &nand_oob_64;
2533 this->autooob = &nand_oob_128;
2536 printk (KERN_WARNING "No oob scheme defined for oobsize %d\n",
2542 /* The number of bytes available for the filesystem to place fs dependend
2545 for (i=0; this->autooob->oobfree[i][1]; i++)
2546 mtd->oobavail += this->autooob->oobfree[i][1];
2549 * check ECC mode, default to software
2550 * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
2551 * fallback to software ECC
2553 this->eccsize = 256; /* set default eccsize */
2556 switch (this->eccmode) {
2557 case NAND_ECC_HW12_2048:
2558 if (mtd->oobblock < 2048) {
2559 printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
2561 this->eccmode = NAND_ECC_SOFT;
2562 this->calculate_ecc = nand_calculate_ecc;
2563 this->correct_data = nand_correct_data;
2565 this->eccsize = 2048;
2568 case NAND_ECC_HW3_512:
2569 case NAND_ECC_HW6_512:
2570 case NAND_ECC_HW8_512:
2571 if (mtd->oobblock == 256) {
2572 printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
2573 this->eccmode = NAND_ECC_SOFT;
2574 this->calculate_ecc = nand_calculate_ecc;
2575 this->correct_data = nand_correct_data;
2577 this->eccsize = 512; /* set eccsize to 512 */
2580 case NAND_ECC_HW3_256:
2584 printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
2585 this->eccmode = NAND_ECC_NONE;
2589 this->calculate_ecc = nand_calculate_ecc;
2590 this->correct_data = nand_correct_data;
2594 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
2598 /* Check hardware ecc function availability and adjust number of ecc bytes per
2601 switch (this->eccmode) {
2602 case NAND_ECC_HW12_2048:
2603 this->eccbytes += 4;
2604 case NAND_ECC_HW8_512:
2605 this->eccbytes += 2;
2606 case NAND_ECC_HW6_512:
2607 this->eccbytes += 3;
2608 case NAND_ECC_HW3_512:
2609 case NAND_ECC_HW3_256:
2610 if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
2612 printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
2616 mtd->eccsize = this->eccsize;
2618 /* Set the number of read / write steps for one page to ensure ECC generation */
2619 switch (this->eccmode) {
2620 case NAND_ECC_HW12_2048:
2621 this->eccsteps = mtd->oobblock / 2048;
2623 case NAND_ECC_HW3_512:
2624 case NAND_ECC_HW6_512:
2625 case NAND_ECC_HW8_512:
2626 this->eccsteps = mtd->oobblock / 512;
2628 case NAND_ECC_HW3_256:
2630 this->eccsteps = mtd->oobblock / 256;
2638 /* XXX U-BOOT XXX */
2640 /* Initialize state, waitqueue and spinlock */
2641 this->state = FL_READY;
2642 init_waitqueue_head (&this->wq);
2643 spin_lock_init (&this->chip_lock);
2646 /* De-select the device */
2647 this->select_chip(mtd, -1);
2649 /* Invalidate the pagebuffer reference */
2652 /* Fill in remaining MTD driver data */
2653 mtd->type = MTD_NANDFLASH;
2654 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
2655 mtd->ecctype = MTD_ECC_SW;
2656 mtd->erase = nand_erase;
2658 mtd->unpoint = NULL;
2659 mtd->read = nand_read;
2660 mtd->write = nand_write;
2661 mtd->read_ecc = nand_read_ecc;
2662 mtd->write_ecc = nand_write_ecc;
2663 mtd->read_oob = nand_read_oob;
2664 mtd->write_oob = nand_write_oob;
2665 /* XXX U-BOOT XXX */
2668 mtd->writev = nand_writev;
2669 mtd->writev_ecc = nand_writev_ecc;
2671 mtd->sync = nand_sync;
2672 /* XXX U-BOOT XXX */
2676 mtd->suspend = NULL;
2679 mtd->block_isbad = nand_block_isbad;
2680 mtd->block_markbad = nand_block_markbad;
2682 /* and make the autooob the default one */
2683 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
2684 /* XXX U-BOOT XXX */
2686 mtd->owner = THIS_MODULE;
2688 /* Build bad block table */
2689 return this->scan_bbt (mtd);
2693 * nand_release - [NAND Interface] Free resources held by the NAND device
2694 * @mtd: MTD device structure
2696 void nand_release (struct mtd_info *mtd)
2698 struct nand_chip *this = mtd->priv;
2700 #ifdef CONFIG_MTD_PARTITIONS
2701 /* Deregister partitions */
2702 del_mtd_partitions (mtd);
2704 /* Deregister the device */
2705 /* XXX U-BOOT XXX */
2707 del_mtd_device (mtd);
2709 /* Free bad block table memory, if allocated */
2712 /* Buffer allocated by nand_scan ? */
2713 if (this->options & NAND_OOBBUF_ALLOC)
2714 kfree (this->oob_buf);
2715 /* Buffer allocated by nand_scan ? */
2716 if (this->options & NAND_DATABUF_ALLOC)
2717 kfree (this->data_buf);