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 #ifdef CFG_NAND_LEGACY
76 #error CFG_NAND_LEGACY defined in a file not using the legacy NAND support!
79 #if (CONFIG_COMMANDS & CFG_CMD_NAND)
83 #include <linux/mtd/compat.h>
84 #include <linux/mtd/mtd.h>
85 #include <linux/mtd/nand.h>
86 #include <linux/mtd/nand_ecc.h>
89 #include <asm/errno.h>
91 #ifdef CONFIG_JFFS2_NAND
92 #include <jffs2/jffs2.h>
95 /* Define default oob placement schemes for large and small page devices */
96 static struct nand_oobinfo nand_oob_8 = {
97 .useecc = MTD_NANDECC_AUTOPLACE,
100 .oobfree = { {3, 2}, {6, 2} }
103 static struct nand_oobinfo nand_oob_16 = {
104 .useecc = MTD_NANDECC_AUTOPLACE,
106 .eccpos = {0, 1, 2, 3, 6, 7},
107 .oobfree = { {8, 8} }
110 static struct nand_oobinfo nand_oob_64 = {
111 .useecc = MTD_NANDECC_AUTOPLACE,
114 40, 41, 42, 43, 44, 45, 46, 47,
115 48, 49, 50, 51, 52, 53, 54, 55,
116 56, 57, 58, 59, 60, 61, 62, 63},
117 .oobfree = { {2, 38} }
120 /* This is used for padding purposes in nand_write_oob */
121 static u_char ffchars[] = {
122 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
123 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
124 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
125 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
126 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
127 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
128 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
129 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
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 */
204 * nand_read_byte - [DEFAULT] read one byte from the chip
205 * @mtd: MTD device structure
207 * Default read function for 8bit buswith
209 static u_char nand_read_byte(struct mtd_info *mtd)
211 struct nand_chip *this = mtd->priv;
212 return readb(this->IO_ADDR_R);
216 * nand_write_byte - [DEFAULT] write one byte to the chip
217 * @mtd: MTD device structure
218 * @byte: pointer to data byte to write
220 * Default write function for 8it buswith
222 static void nand_write_byte(struct mtd_info *mtd, u_char byte)
224 struct nand_chip *this = mtd->priv;
225 writeb(byte, this->IO_ADDR_W);
229 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
230 * @mtd: MTD device structure
232 * Default read function for 16bit buswith with
233 * endianess conversion
235 static u_char nand_read_byte16(struct mtd_info *mtd)
237 struct nand_chip *this = mtd->priv;
238 return (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
242 * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
243 * @mtd: MTD device structure
244 * @byte: pointer to data byte to write
246 * Default write function for 16bit buswith with
247 * endianess conversion
249 static void nand_write_byte16(struct mtd_info *mtd, u_char byte)
251 struct nand_chip *this = mtd->priv;
252 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
256 * nand_read_word - [DEFAULT] read one word from the chip
257 * @mtd: MTD device structure
259 * Default read function for 16bit buswith without
260 * endianess conversion
262 static u16 nand_read_word(struct mtd_info *mtd)
264 struct nand_chip *this = mtd->priv;
265 return readw(this->IO_ADDR_R);
269 * nand_write_word - [DEFAULT] write one word to the chip
270 * @mtd: MTD device structure
271 * @word: data word to write
273 * Default write function for 16bit buswith without
274 * endianess conversion
276 static void nand_write_word(struct mtd_info *mtd, u16 word)
278 struct nand_chip *this = mtd->priv;
279 writew(word, this->IO_ADDR_W);
283 * nand_select_chip - [DEFAULT] control CE line
284 * @mtd: MTD device structure
285 * @chip: chipnumber to select, -1 for deselect
287 * Default select function for 1 chip devices.
289 static void nand_select_chip(struct mtd_info *mtd, int chip)
291 struct nand_chip *this = mtd->priv;
294 this->hwcontrol(mtd, NAND_CTL_CLRNCE);
297 this->hwcontrol(mtd, NAND_CTL_SETNCE);
306 * nand_write_buf - [DEFAULT] write buffer to chip
307 * @mtd: MTD device structure
309 * @len: number of bytes to write
311 * Default write function for 8bit buswith
313 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
316 struct nand_chip *this = mtd->priv;
318 for (i=0; i<len; i++)
319 writeb(buf[i], this->IO_ADDR_W);
323 * nand_read_buf - [DEFAULT] read chip data into buffer
324 * @mtd: MTD device structure
325 * @buf: buffer to store date
326 * @len: number of bytes to read
328 * Default read function for 8bit buswith
330 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
333 struct nand_chip *this = mtd->priv;
335 for (i=0; i<len; i++)
336 buf[i] = readb(this->IO_ADDR_R);
340 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
341 * @mtd: MTD device structure
342 * @buf: buffer containing the data to compare
343 * @len: number of bytes to compare
345 * Default verify function for 8bit buswith
347 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
350 struct nand_chip *this = mtd->priv;
352 for (i=0; i<len; i++)
353 if (buf[i] != readb(this->IO_ADDR_R))
360 * nand_write_buf16 - [DEFAULT] write buffer to chip
361 * @mtd: MTD device structure
363 * @len: number of bytes to write
365 * Default write function for 16bit buswith
367 static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
370 struct nand_chip *this = mtd->priv;
371 u16 *p = (u16 *) buf;
374 for (i=0; i<len; i++)
375 writew(p[i], this->IO_ADDR_W);
380 * nand_read_buf16 - [DEFAULT] read chip data into buffer
381 * @mtd: MTD device structure
382 * @buf: buffer to store date
383 * @len: number of bytes to read
385 * Default read function for 16bit buswith
387 static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
390 struct nand_chip *this = mtd->priv;
391 u16 *p = (u16 *) buf;
394 for (i=0; i<len; i++)
395 p[i] = readw(this->IO_ADDR_R);
399 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
400 * @mtd: MTD device structure
401 * @buf: buffer containing the data to compare
402 * @len: number of bytes to compare
404 * Default verify function for 16bit buswith
406 static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
409 struct nand_chip *this = mtd->priv;
410 u16 *p = (u16 *) buf;
413 for (i=0; i<len; i++)
414 if (p[i] != readw(this->IO_ADDR_R))
421 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
422 * @mtd: MTD device structure
423 * @ofs: offset from device start
424 * @getchip: 0, if the chip is already selected
426 * Check, if the block is bad.
428 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
430 int page, chipnr, res = 0;
431 struct nand_chip *this = mtd->priv;
435 page = (int)(ofs >> this->page_shift);
436 chipnr = (int)(ofs >> this->chip_shift);
438 /* Grab the lock and see if the device is available */
439 nand_get_device (this, mtd, FL_READING);
441 /* Select the NAND device */
442 this->select_chip(mtd, chipnr);
446 if (this->options & NAND_BUSWIDTH_16) {
447 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page & this->pagemask);
448 bad = cpu_to_le16(this->read_word(mtd));
449 if (this->badblockpos & 0x1)
451 if ((bad & 0xFF) != 0xff)
454 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page & this->pagemask);
455 if (this->read_byte(mtd) != 0xff)
460 /* Deselect and wake up anyone waiting on the device */
461 nand_release_device(mtd);
468 * nand_default_block_markbad - [DEFAULT] mark a block bad
469 * @mtd: MTD device structure
470 * @ofs: offset from device start
472 * This is the default implementation, which can be overridden by
473 * a hardware specific driver.
475 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
477 struct nand_chip *this = mtd->priv;
478 u_char buf[2] = {0, 0};
482 /* Get block number */
483 block = ((int) ofs) >> this->bbt_erase_shift;
484 this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
486 /* Do we have a flash based bad block table ? */
487 if (this->options & NAND_USE_FLASH_BBT)
488 return nand_update_bbt (mtd, ofs);
490 /* We write two bytes, so we dont have to mess with 16 bit access */
491 ofs += mtd->oobsize + (this->badblockpos & ~0x01);
492 return nand_write_oob (mtd, ofs , 2, &retlen, buf);
496 * nand_check_wp - [GENERIC] check if the chip is write protected
497 * @mtd: MTD device structure
498 * Check, if the device is write protected
500 * The function expects, that the device is already selected
502 static int nand_check_wp (struct mtd_info *mtd)
504 struct nand_chip *this = mtd->priv;
505 /* Check the WP bit */
506 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
507 return (this->read_byte(mtd) & 0x80) ? 0 : 1;
511 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
512 * @mtd: MTD device structure
513 * @ofs: offset from device start
514 * @getchip: 0, if the chip is already selected
515 * @allowbbt: 1, if its allowed to access the bbt area
517 * Check, if the block is bad. Either by reading the bad block table or
518 * calling of the scan function.
520 static int nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
522 struct nand_chip *this = mtd->priv;
525 return this->block_bad(mtd, ofs, getchip);
527 /* Return info from the table */
528 return nand_isbad_bbt (mtd, ofs, allowbbt);
532 * nand_command - [DEFAULT] Send command to NAND device
533 * @mtd: MTD device structure
534 * @command: the command to be sent
535 * @column: the column address for this command, -1 if none
536 * @page_addr: the page address for this command, -1 if none
538 * Send command to NAND device. This function is used for small page
539 * devices (256/512 Bytes per page)
541 static void nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
543 register struct nand_chip *this = mtd->priv;
545 /* Begin command latch cycle */
546 this->hwcontrol(mtd, NAND_CTL_SETCLE);
548 * Write out the command to the device.
550 if (command == NAND_CMD_SEQIN) {
553 if (column >= mtd->oobblock) {
555 column -= mtd->oobblock;
556 readcmd = NAND_CMD_READOOB;
557 } else if (column < 256) {
558 /* First 256 bytes --> READ0 */
559 readcmd = NAND_CMD_READ0;
562 readcmd = NAND_CMD_READ1;
564 this->write_byte(mtd, readcmd);
566 this->write_byte(mtd, command);
568 /* Set ALE and clear CLE to start address cycle */
569 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
571 if (column != -1 || page_addr != -1) {
572 this->hwcontrol(mtd, NAND_CTL_SETALE);
574 /* Serially input address */
576 /* Adjust columns for 16 bit buswidth */
577 if (this->options & NAND_BUSWIDTH_16)
579 this->write_byte(mtd, column);
581 if (page_addr != -1) {
582 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
583 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
584 /* One more address cycle for devices > 32MiB */
585 if (this->chipsize > (32 << 20))
586 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));
588 /* Latch in address */
589 this->hwcontrol(mtd, NAND_CTL_CLRALE);
593 * program and erase have their own busy handlers
594 * status and sequential in needs no delay
598 case NAND_CMD_PAGEPROG:
599 case NAND_CMD_ERASE1:
600 case NAND_CMD_ERASE2:
602 case NAND_CMD_STATUS:
608 udelay(this->chip_delay);
609 this->hwcontrol(mtd, NAND_CTL_SETCLE);
610 this->write_byte(mtd, NAND_CMD_STATUS);
611 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
612 while ( !(this->read_byte(mtd) & 0x40));
615 /* This applies to read commands */
618 * If we don't have access to the busy pin, we apply the given
621 if (!this->dev_ready) {
622 udelay (this->chip_delay);
627 /* Apply this short delay always to ensure that we do wait tWB in
628 * any case on any machine. */
630 /* wait until command is processed */
631 while (!this->dev_ready(mtd));
635 * nand_command_lp - [DEFAULT] Send command to NAND large page device
636 * @mtd: MTD device structure
637 * @command: the command to be sent
638 * @column: the column address for this command, -1 if none
639 * @page_addr: the page address for this command, -1 if none
641 * Send command to NAND device. This is the version for the new large page devices
642 * We dont have the seperate regions as we have in the small page devices.
643 * We must emulate NAND_CMD_READOOB to keep the code compatible.
646 static void nand_command_lp (struct mtd_info *mtd, unsigned command, int column, int page_addr)
648 register struct nand_chip *this = mtd->priv;
650 /* Emulate NAND_CMD_READOOB */
651 if (command == NAND_CMD_READOOB) {
652 column += mtd->oobblock;
653 command = NAND_CMD_READ0;
657 /* Begin command latch cycle */
658 this->hwcontrol(mtd, NAND_CTL_SETCLE);
659 /* Write out the command to the device. */
660 this->write_byte(mtd, command);
661 /* End command latch cycle */
662 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
664 if (column != -1 || page_addr != -1) {
665 this->hwcontrol(mtd, NAND_CTL_SETALE);
667 /* Serially input address */
669 /* Adjust columns for 16 bit buswidth */
670 if (this->options & NAND_BUSWIDTH_16)
672 this->write_byte(mtd, column & 0xff);
673 this->write_byte(mtd, column >> 8);
675 if (page_addr != -1) {
676 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
677 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
678 /* One more address cycle for devices > 128MiB */
679 if (this->chipsize > (128 << 20))
680 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0xff));
682 /* Latch in address */
683 this->hwcontrol(mtd, NAND_CTL_CLRALE);
687 * program and erase have their own busy handlers
688 * status and sequential in needs no delay
692 case NAND_CMD_CACHEDPROG:
693 case NAND_CMD_PAGEPROG:
694 case NAND_CMD_ERASE1:
695 case NAND_CMD_ERASE2:
697 case NAND_CMD_STATUS:
704 udelay(this->chip_delay);
705 this->hwcontrol(mtd, NAND_CTL_SETCLE);
706 this->write_byte(mtd, NAND_CMD_STATUS);
707 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
708 while ( !(this->read_byte(mtd) & 0x40));
712 /* Begin command latch cycle */
713 this->hwcontrol(mtd, NAND_CTL_SETCLE);
714 /* Write out the start read command */
715 this->write_byte(mtd, NAND_CMD_READSTART);
716 /* End command latch cycle */
717 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
718 /* Fall through into ready check */
720 /* This applies to read commands */
723 * If we don't have access to the busy pin, we apply the given
726 if (!this->dev_ready) {
727 udelay (this->chip_delay);
732 /* Apply this short delay always to ensure that we do wait tWB in
733 * any case on any machine. */
735 /* wait until command is processed */
736 while (!this->dev_ready(mtd));
740 * nand_get_device - [GENERIC] Get chip for selected access
741 * @this: the nand chip descriptor
742 * @mtd: MTD device structure
743 * @new_state: the state which is requested
745 * Get the device and lock it for exclusive access
749 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
751 struct nand_chip *active = this;
753 DECLARE_WAITQUEUE (wait, current);
756 * Grab the lock and see if the device is available
759 /* Hardware controller shared among independend devices */
760 if (this->controller) {
761 spin_lock (&this->controller->lock);
762 if (this->controller->active)
763 active = this->controller->active;
765 this->controller->active = this;
766 spin_unlock (&this->controller->lock);
769 if (active == this) {
770 spin_lock (&this->chip_lock);
771 if (this->state == FL_READY) {
772 this->state = new_state;
773 spin_unlock (&this->chip_lock);
777 set_current_state (TASK_UNINTERRUPTIBLE);
778 add_wait_queue (&active->wq, &wait);
779 spin_unlock (&active->chip_lock);
781 remove_wait_queue (&active->wq, &wait);
785 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state) {}
789 * nand_wait - [DEFAULT] wait until the command is done
790 * @mtd: MTD device structure
791 * @this: NAND chip structure
792 * @state: state to select the max. timeout value
794 * Wait for command done. This applies to erase and program only
795 * Erase can take up to 400ms and program up to 20ms according to
796 * general NAND and SmartMedia specs
801 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
803 unsigned long timeo = jiffies;
806 if (state == FL_ERASING)
807 timeo += (HZ * 400) / 1000;
809 timeo += (HZ * 20) / 1000;
811 /* Apply this short delay always to ensure that we do wait tWB in
812 * any case on any machine. */
815 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
816 this->cmdfunc (mtd, NAND_CMD_STATUS_MULTI, -1, -1);
818 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
820 while (time_before(jiffies, timeo)) {
821 /* Check, if we were interrupted */
822 if (this->state != state)
825 if (this->dev_ready) {
826 if (this->dev_ready(mtd))
829 if (this->read_byte(mtd) & NAND_STATUS_READY)
834 status = (int) this->read_byte(mtd);
840 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
844 if (state == FL_ERASING)
845 timeo = CFG_HZ * 400;
849 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
850 this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
852 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
857 if (get_timer(0) > timeo) {
862 if (this->dev_ready) {
863 if (this->dev_ready(mtd))
866 if (this->read_byte(mtd) & NAND_STATUS_READY)
870 #ifdef PPCHAMELON_NAND_TIMER_HACK
872 while (get_timer(0) < 10);
873 #endif /* PPCHAMELON_NAND_TIMER_HACK */
875 return this->read_byte(mtd);
880 * nand_write_page - [GENERIC] write one page
881 * @mtd: MTD device structure
882 * @this: NAND chip structure
883 * @page: startpage inside the chip, must be called with (page & this->pagemask)
884 * @oob_buf: out of band data buffer
885 * @oobsel: out of band selecttion structre
886 * @cached: 1 = enable cached programming if supported by chip
888 * Nand_page_program function is used for write and writev !
889 * This function will always program a full page of data
890 * If you call it with a non page aligned buffer, you're lost :)
892 * Cached programming is not supported yet.
894 static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page,
895 u_char *oob_buf, struct nand_oobinfo *oobsel, int cached)
899 int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
900 int *oob_config = oobsel->eccpos;
901 int datidx = 0, eccidx = 0, eccsteps = this->eccsteps;
904 /* FIXME: Enable cached programming */
907 /* Send command to begin auto page programming */
908 this->cmdfunc (mtd, NAND_CMD_SEQIN, 0x00, page);
910 /* Write out complete page of data, take care of eccmode */
912 /* No ecc, write all */
914 printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
915 this->write_buf(mtd, this->data_poi, mtd->oobblock);
918 /* Software ecc 3/256, write all */
920 for (; eccsteps; eccsteps--) {
921 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
922 for (i = 0; i < 3; i++, eccidx++)
923 oob_buf[oob_config[eccidx]] = ecc_code[i];
924 datidx += this->eccsize;
926 this->write_buf(mtd, this->data_poi, mtd->oobblock);
929 eccbytes = this->eccbytes;
930 for (; eccsteps; eccsteps--) {
931 /* enable hardware ecc logic for write */
932 this->enable_hwecc(mtd, NAND_ECC_WRITE);
933 this->write_buf(mtd, &this->data_poi[datidx], this->eccsize);
934 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
935 for (i = 0; i < eccbytes; i++, eccidx++)
936 oob_buf[oob_config[eccidx]] = ecc_code[i];
937 /* If the hardware ecc provides syndromes then
938 * the ecc code must be written immidiately after
939 * the data bytes (words) */
940 if (this->options & NAND_HWECC_SYNDROME)
941 this->write_buf(mtd, ecc_code, eccbytes);
942 datidx += this->eccsize;
947 /* Write out OOB data */
948 if (this->options & NAND_HWECC_SYNDROME)
949 this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
951 this->write_buf(mtd, oob_buf, mtd->oobsize);
953 /* Send command to actually program the data */
954 this->cmdfunc (mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
957 /* call wait ready function */
958 status = this->waitfunc (mtd, this, FL_WRITING);
959 /* See if device thinks it succeeded */
961 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
965 /* FIXME: Implement cached programming ! */
966 /* wait until cache is ready*/
967 /* status = this->waitfunc (mtd, this, FL_CACHEDRPG); */
972 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
974 * nand_verify_pages - [GENERIC] verify the chip contents after a write
975 * @mtd: MTD device structure
976 * @this: NAND chip structure
977 * @page: startpage inside the chip, must be called with (page & this->pagemask)
978 * @numpages: number of pages to verify
979 * @oob_buf: out of band data buffer
980 * @oobsel: out of band selecttion structre
981 * @chipnr: number of the current chip
982 * @oobmode: 1 = full buffer verify, 0 = ecc only
984 * The NAND device assumes that it is always writing to a cleanly erased page.
985 * Hence, it performs its internal write verification only on bits that
986 * transitioned from 1 to 0. The device does NOT verify the whole page on a
987 * byte by byte basis. It is possible that the page was not completely erased
988 * or the page is becoming unusable due to wear. The read with ECC would catch
989 * the error later when the ECC page check fails, but we would rather catch
990 * it early in the page write stage. Better to write no data than invalid data.
992 static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
993 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
995 int i, j, datidx = 0, oobofs = 0, res = -EIO;
996 int eccsteps = this->eccsteps;
1000 hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
1002 /* Send command to read back the first page */
1003 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page);
1006 for (j = 0; j < eccsteps; j++) {
1007 /* Loop through and verify the data */
1008 if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
1009 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1012 datidx += mtd->eccsize;
1013 /* Have we a hw generator layout ? */
1016 if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
1017 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1020 oobofs += hweccbytes;
1023 /* check, if we must compare all data or if we just have to
1024 * compare the ecc bytes
1027 if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
1028 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1032 /* Read always, else autoincrement fails */
1033 this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
1035 if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
1036 int ecccnt = oobsel->eccbytes;
1038 for (i = 0; i < ecccnt; i++) {
1039 int idx = oobsel->eccpos[i];
1040 if (oobdata[idx] != oob_buf[oobofs + idx] ) {
1041 DEBUG (MTD_DEBUG_LEVEL0,
1042 "%s: Failed ECC write "
1043 "verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__, page, i);
1049 oobofs += mtd->oobsize - hweccbytes * eccsteps;
1053 /* Apply delay or wait for ready/busy pin
1054 * Do this before the AUTOINCR check, so no problems
1055 * arise if a chip which does auto increment
1056 * is marked as NOAUTOINCR by the board driver.
1057 * Do this also before returning, so the chip is
1058 * ready for the next command.
1060 if (!this->dev_ready)
1061 udelay (this->chip_delay);
1063 while (!this->dev_ready(mtd));
1065 /* All done, return happy */
1070 /* Check, if the chip supports auto page increment */
1071 if (!NAND_CANAUTOINCR(this))
1072 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1075 * Terminate the read command. We come here in case of an error
1076 * So we must issue a reset command.
1079 this->cmdfunc (mtd, NAND_CMD_RESET, -1, -1);
1085 * nand_read - [MTD Interface] MTD compability function for nand_read_ecc
1086 * @mtd: MTD device structure
1087 * @from: offset to read from
1088 * @len: number of bytes to read
1089 * @retlen: pointer to variable to store the number of read bytes
1090 * @buf: the databuffer to put data
1092 * This function simply calls nand_read_ecc with oob buffer and oobsel = NULL
1094 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1096 return nand_read_ecc (mtd, from, len, retlen, buf, NULL, NULL);
1101 * nand_read_ecc - [MTD Interface] Read data with ECC
1102 * @mtd: MTD device structure
1103 * @from: offset to read from
1104 * @len: number of bytes to read
1105 * @retlen: pointer to variable to store the number of read bytes
1106 * @buf: the databuffer to put data
1107 * @oob_buf: filesystem supplied oob data buffer
1108 * @oobsel: oob selection structure
1110 * NAND read with ECC
1112 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
1113 size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel)
1115 int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
1116 int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
1117 struct nand_chip *this = mtd->priv;
1118 u_char *data_poi, *oob_data = oob_buf;
1119 u_char ecc_calc[32];
1120 u_char ecc_code[32];
1121 int eccmode, eccsteps;
1122 int *oob_config, datidx;
1123 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1129 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1131 /* Do not allow reads past end of device */
1132 if ((from + len) > mtd->size) {
1133 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
1138 /* Grab the lock and see if the device is available */
1139 nand_get_device (this, mtd ,FL_READING);
1141 /* use userspace supplied oobinfo, if zero */
1143 oobsel = &mtd->oobinfo;
1145 /* Autoplace of oob data ? Use the default placement scheme */
1146 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1147 oobsel = this->autooob;
1149 eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
1150 oob_config = oobsel->eccpos;
1152 /* Select the NAND device */
1153 chipnr = (int)(from >> this->chip_shift);
1154 this->select_chip(mtd, chipnr);
1156 /* First we calculate the starting page */
1157 realpage = (int) (from >> this->page_shift);
1158 page = realpage & this->pagemask;
1160 /* Get raw starting column */
1161 col = from & (mtd->oobblock - 1);
1163 end = mtd->oobblock;
1164 ecc = this->eccsize;
1165 eccbytes = this->eccbytes;
1167 if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
1170 oobreadlen = mtd->oobsize;
1171 if (this->options & NAND_HWECC_SYNDROME)
1172 oobreadlen -= oobsel->eccbytes;
1174 /* Loop until all data read */
1175 while (read < len) {
1177 int aligned = (!col && (len - read) >= end);
1179 * If the read is not page aligned, we have to read into data buffer
1180 * due to ecc, else we read into return buffer direct
1183 data_poi = &buf[read];
1185 data_poi = this->data_buf;
1187 /* Check, if we have this page in the buffer
1189 * FIXME: Make it work when we must provide oob data too,
1190 * check the usage of data_buf oob field
1192 if (realpage == this->pagebuf && !oob_buf) {
1193 /* aligned read ? */
1195 memcpy (data_poi, this->data_buf, end);
1199 /* Check, if we must send the read command */
1201 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1205 /* get oob area, if we have no oob buffer from fs-driver */
1206 if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE ||
1207 oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1208 oob_data = &this->data_buf[end];
1210 eccsteps = this->eccsteps;
1213 case NAND_ECC_NONE: { /* No ECC, Read in a page */
1214 /* XXX U-BOOT XXX */
1216 static unsigned long lastwhinge = 0;
1217 if ((lastwhinge / HZ) != (jiffies / HZ)) {
1218 printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended\n");
1219 lastwhinge = jiffies;
1222 puts("Reading data from NAND FLASH without ECC is not recommended\n");
1224 this->read_buf(mtd, data_poi, end);
1228 case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
1229 this->read_buf(mtd, data_poi, end);
1230 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc)
1231 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1235 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) {
1236 this->enable_hwecc(mtd, NAND_ECC_READ);
1237 this->read_buf(mtd, &data_poi[datidx], ecc);
1239 /* HW ecc with syndrome calculation must read the
1240 * syndrome from flash immidiately after the data */
1242 /* Some hw ecc generators need to know when the
1243 * syndrome is read from flash */
1244 this->enable_hwecc(mtd, NAND_ECC_READSYN);
1245 this->read_buf(mtd, &oob_data[i], eccbytes);
1246 /* We calc error correction directly, it checks the hw
1247 * generator for an error, reads back the syndrome and
1248 * does the error correction on the fly */
1249 if (this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]) == -1) {
1250 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: "
1251 "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
1255 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1262 this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
1264 /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
1268 /* Pick the ECC bytes out of the oob data */
1269 for (j = 0; j < oobsel->eccbytes; j++)
1270 ecc_code[j] = oob_data[oob_config[j]];
1272 /* correct data, if neccecary */
1273 for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {
1274 ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
1276 /* Get next chunk of ecc bytes */
1279 /* Check, if we have a fs supplied oob-buffer,
1280 * This is the legacy mode. Used by YAFFS1
1281 * Should go away some day
1283 if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
1284 int *p = (int *)(&oob_data[mtd->oobsize]);
1288 if (ecc_status == -1) {
1289 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
1295 /* check, if we have a fs supplied oob-buffer */
1297 /* without autoplace. Legacy mode used by YAFFS1 */
1298 switch(oobsel->useecc) {
1299 case MTD_NANDECC_AUTOPLACE:
1300 case MTD_NANDECC_AUTOPL_USR:
1301 /* Walk through the autoplace chunks */
1302 for (i = 0, j = 0; j < mtd->oobavail; i++) {
1303 int from = oobsel->oobfree[i][0];
1304 int num = oobsel->oobfree[i][1];
1305 memcpy(&oob_buf[oob], &oob_data[from], num);
1308 oob += mtd->oobavail;
1310 case MTD_NANDECC_PLACE:
1311 /* YAFFS1 legacy mode */
1312 oob_data += this->eccsteps * sizeof (int);
1314 oob_data += mtd->oobsize;
1318 /* Partial page read, transfer data into fs buffer */
1320 for (j = col; j < end && read < len; j++)
1321 buf[read++] = data_poi[j];
1322 this->pagebuf = realpage;
1324 read += mtd->oobblock;
1326 /* Apply delay or wait for ready/busy pin
1327 * Do this before the AUTOINCR check, so no problems
1328 * arise if a chip which does auto increment
1329 * is marked as NOAUTOINCR by the board driver.
1331 if (!this->dev_ready)
1332 udelay (this->chip_delay);
1334 while (!this->dev_ready(mtd));
1339 /* For subsequent reads align to page boundary. */
1341 /* Increment page address */
1344 page = realpage & this->pagemask;
1345 /* Check, if we cross a chip boundary */
1348 this->select_chip(mtd, -1);
1349 this->select_chip(mtd, chipnr);
1351 /* Check, if the chip supports auto page increment
1352 * or if we have hit a block boundary.
1354 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1358 /* Deselect and wake up anyone waiting on the device */
1359 nand_release_device(mtd);
1362 * Return success, if no ECC failures, else -EBADMSG
1363 * fs driver will take care of that, because
1364 * retlen == desired len and result == -EBADMSG
1367 return ecc_failed ? -EBADMSG : 0;
1371 * nand_read_oob - [MTD Interface] NAND read out-of-band
1372 * @mtd: MTD device structure
1373 * @from: offset to read from
1374 * @len: number of bytes to read
1375 * @retlen: pointer to variable to store the number of read bytes
1376 * @buf: the databuffer to put data
1378 * NAND read out-of-band data from the spare area
1380 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1382 int i, col, page, chipnr;
1383 struct nand_chip *this = mtd->priv;
1384 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1386 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1388 /* Shift to get page */
1389 page = (int)(from >> this->page_shift);
1390 chipnr = (int)(from >> this->chip_shift);
1392 /* Mask to get column */
1393 col = from & (mtd->oobsize - 1);
1395 /* Initialize return length value */
1398 /* Do not allow reads past end of device */
1399 if ((from + len) > mtd->size) {
1400 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n");
1405 /* Grab the lock and see if the device is available */
1406 nand_get_device (this, mtd , FL_READING);
1408 /* Select the NAND device */
1409 this->select_chip(mtd, chipnr);
1411 /* Send the read command */
1412 this->cmdfunc (mtd, NAND_CMD_READOOB, col, page & this->pagemask);
1414 * Read the data, if we read more than one page
1415 * oob data, let the device transfer the data !
1419 int thislen = mtd->oobsize - col;
1420 thislen = min_t(int, thislen, len);
1421 this->read_buf(mtd, &buf[i], thislen);
1424 /* Apply delay or wait for ready/busy pin
1425 * Do this before the AUTOINCR check, so no problems
1426 * arise if a chip which does auto increment
1427 * is marked as NOAUTOINCR by the board driver.
1429 if (!this->dev_ready)
1430 udelay (this->chip_delay);
1432 while (!this->dev_ready(mtd));
1439 /* Check, if we cross a chip boundary */
1440 if (!(page & this->pagemask)) {
1442 this->select_chip(mtd, -1);
1443 this->select_chip(mtd, chipnr);
1446 /* Check, if the chip supports auto page increment
1447 * or if we have hit a block boundary.
1449 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
1450 /* For subsequent page reads set offset to 0 */
1451 this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
1456 /* Deselect and wake up anyone waiting on the device */
1457 nand_release_device(mtd);
1465 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
1466 * @mtd: MTD device structure
1467 * @buf: temporary buffer
1468 * @from: offset to read from
1469 * @len: number of bytes to read
1470 * @ooblen: number of oob data bytes to read
1472 * Read raw data including oob into buffer
1474 int nand_read_raw (struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
1476 struct nand_chip *this = mtd->priv;
1477 int page = (int) (from >> this->page_shift);
1478 int chip = (int) (from >> this->chip_shift);
1481 int pagesize = mtd->oobblock + mtd->oobsize;
1482 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1484 /* Do not allow reads past end of device */
1485 if ((from + len) > mtd->size) {
1486 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n");
1490 /* Grab the lock and see if the device is available */
1491 nand_get_device (this, mtd , FL_READING);
1493 this->select_chip (mtd, chip);
1495 /* Add requested oob length */
1500 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page & this->pagemask);
1503 this->read_buf (mtd, &buf[cnt], pagesize);
1509 if (!this->dev_ready)
1510 udelay (this->chip_delay);
1512 while (!this->dev_ready(mtd));
1514 /* Check, if the chip supports auto page increment */
1515 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1519 /* Deselect and wake up anyone waiting on the device */
1520 nand_release_device(mtd);
1526 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
1527 * @mtd: MTD device structure
1528 * @fsbuf: buffer given by fs driver
1529 * @oobsel: out of band selection structre
1530 * @autoplace: 1 = place given buffer into the oob bytes
1531 * @numpages: number of pages to prepare
1534 * 1. Filesystem buffer available and autoplacement is off,
1535 * return filesystem buffer
1536 * 2. No filesystem buffer or autoplace is off, return internal
1538 * 3. Filesystem buffer is given and autoplace selected
1539 * put data from fs buffer into internal buffer and
1540 * retrun internal buffer
1542 * Note: The internal buffer is filled with 0xff. This must
1543 * be done only once, when no autoplacement happens
1544 * Autoplacement sets the buffer dirty flag, which
1545 * forces the 0xff fill before using the buffer again.
1548 static u_char * nand_prepare_oobbuf (struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel,
1549 int autoplace, int numpages)
1551 struct nand_chip *this = mtd->priv;
1554 /* Zero copy fs supplied buffer */
1555 if (fsbuf && !autoplace)
1558 /* Check, if the buffer must be filled with ff again */
1559 if (this->oobdirty) {
1560 memset (this->oob_buf, 0xff,
1561 mtd->oobsize << (this->phys_erase_shift - this->page_shift));
1565 /* If we have no autoplacement or no fs buffer use the internal one */
1566 if (!autoplace || !fsbuf)
1567 return this->oob_buf;
1569 /* Walk through the pages and place the data */
1572 while (numpages--) {
1573 for (i = 0, len = 0; len < mtd->oobavail; i++) {
1574 int to = ofs + oobsel->oobfree[i][0];
1575 int num = oobsel->oobfree[i][1];
1576 memcpy (&this->oob_buf[to], fsbuf, num);
1580 ofs += mtd->oobavail;
1582 return this->oob_buf;
1585 #define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
1588 * nand_write - [MTD Interface] compability function for nand_write_ecc
1589 * @mtd: MTD device structure
1590 * @to: offset to write to
1591 * @len: number of bytes to write
1592 * @retlen: pointer to variable to store the number of written bytes
1593 * @buf: the data to write
1595 * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
1598 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1600 return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));
1604 * nand_write_ecc - [MTD Interface] NAND write with ECC
1605 * @mtd: MTD device structure
1606 * @to: offset to write to
1607 * @len: number of bytes to write
1608 * @retlen: pointer to variable to store the number of written bytes
1609 * @buf: the data to write
1610 * @eccbuf: filesystem supplied oob data buffer
1611 * @oobsel: oob selection structure
1613 * NAND write with ECC
1615 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
1616 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
1618 int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
1619 int autoplace = 0, numpages, totalpages;
1620 struct nand_chip *this = mtd->priv;
1621 u_char *oobbuf, *bufstart;
1622 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1624 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1626 /* Initialize retlen, in case of early exit */
1629 /* Do not allow write past end of device */
1630 if ((to + len) > mtd->size) {
1631 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n");
1635 /* reject writes, which are not page aligned */
1636 if (NOTALIGNED (to) || NOTALIGNED(len)) {
1637 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1641 /* Grab the lock and see if the device is available */
1642 nand_get_device (this, mtd, FL_WRITING);
1644 /* Calculate chipnr */
1645 chipnr = (int)(to >> this->chip_shift);
1646 /* Select the NAND device */
1647 this->select_chip(mtd, chipnr);
1649 /* Check, if it is write protected */
1650 if (nand_check_wp(mtd))
1653 /* if oobsel is NULL, use chip defaults */
1655 oobsel = &mtd->oobinfo;
1657 /* Autoplace of oob data ? Use the default placement scheme */
1658 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1659 oobsel = this->autooob;
1662 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1665 /* Setup variables and oob buffer */
1666 totalpages = len >> this->page_shift;
1667 page = (int) (to >> this->page_shift);
1668 /* Invalidate the page cache, if we write to the cached page */
1669 if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
1672 /* Set it relative to chip */
1673 page &= this->pagemask;
1675 /* Calc number of pages we can write in one go */
1676 numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages);
1677 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages);
1678 bufstart = (u_char *)buf;
1680 /* Loop until all data is written */
1681 while (written < len) {
1683 this->data_poi = (u_char*) &buf[written];
1684 /* Write one page. If this is the last page to write
1685 * or the last page in this block, then use the
1686 * real pageprogram command, else select cached programming
1687 * if supported by the chip.
1689 ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
1691 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret);
1695 oob += mtd->oobsize;
1696 /* Update written bytes count */
1697 written += mtd->oobblock;
1701 /* Increment page address */
1704 /* Have we hit a block boundary ? Then we have to verify and
1705 * if verify is ok, we have to setup the oob buffer for
1708 if (!(page & (ppblock - 1))){
1710 this->data_poi = bufstart;
1711 ret = nand_verify_pages (mtd, this, startpage,
1713 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1715 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1720 ofs = autoplace ? mtd->oobavail : mtd->oobsize;
1722 eccbuf += (page - startpage) * ofs;
1723 totalpages -= page - startpage;
1724 numpages = min (totalpages, ppblock);
1725 page &= this->pagemask;
1729 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel,
1730 autoplace, numpages);
1731 /* Check, if we cross a chip boundary */
1734 this->select_chip(mtd, -1);
1735 this->select_chip(mtd, chipnr);
1739 /* Verify the remaining pages */
1741 this->data_poi = bufstart;
1742 ret = nand_verify_pages (mtd, this, startpage, totalpages,
1743 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1747 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1750 /* Deselect and wake up anyone waiting on the device */
1751 nand_release_device(mtd);
1758 * nand_write_oob - [MTD Interface] NAND write out-of-band
1759 * @mtd: MTD device structure
1760 * @to: offset to write to
1761 * @len: number of bytes to write
1762 * @retlen: pointer to variable to store the number of written bytes
1763 * @buf: the data to write
1765 * NAND write out-of-band
1767 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1769 int column, page, status, ret = -EIO, chipnr;
1770 struct nand_chip *this = mtd->priv;
1772 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1774 /* Shift to get page */
1775 page = (int) (to >> this->page_shift);
1776 chipnr = (int) (to >> this->chip_shift);
1778 /* Mask to get column */
1779 column = to & (mtd->oobsize - 1);
1781 /* Initialize return length value */
1784 /* Do not allow write past end of page */
1785 if ((column + len) > mtd->oobsize) {
1786 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
1790 /* Grab the lock and see if the device is available */
1791 nand_get_device (this, mtd, FL_WRITING);
1793 /* Select the NAND device */
1794 this->select_chip(mtd, chipnr);
1796 /* Reset the chip. Some chips (like the Toshiba TC5832DC found
1797 in one of my DiskOnChip 2000 test units) will clear the whole
1798 data page too if we don't do this. I have no clue why, but
1799 I seem to have 'fixed' it in the doc2000 driver in
1800 August 1999. dwmw2. */
1801 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1803 /* Check, if it is write protected */
1804 if (nand_check_wp(mtd))
1807 /* Invalidate the page cache, if we write to the cached page */
1808 if (page == this->pagebuf)
1811 if (NAND_MUST_PAD(this)) {
1812 /* Write out desired data */
1813 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
1814 /* prepad 0xff for partial programming */
1815 this->write_buf(mtd, ffchars, column);
1817 this->write_buf(mtd, buf, len);
1818 /* postpad 0xff for partial programming */
1819 this->write_buf(mtd, ffchars, mtd->oobsize - (len+column));
1821 /* Write out desired data */
1822 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
1824 this->write_buf(mtd, buf, len);
1826 /* Send command to program the OOB data */
1827 this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1);
1829 status = this->waitfunc (mtd, this, FL_WRITING);
1831 /* See if device thinks it succeeded */
1832 if (status & 0x01) {
1833 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
1840 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1841 /* Send command to read back the data */
1842 this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask);
1844 if (this->verify_buf(mtd, buf, len)) {
1845 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
1852 /* Deselect and wake up anyone waiting on the device */
1853 nand_release_device(mtd);
1858 /* XXX U-BOOT XXX */
1861 * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
1862 * @mtd: MTD device structure
1863 * @vecs: the iovectors to write
1864 * @count: number of vectors
1865 * @to: offset to write to
1866 * @retlen: pointer to variable to store the number of written bytes
1868 * NAND write with kvec. This just calls the ecc function
1870 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1871 loff_t to, size_t * retlen)
1873 return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL));
1877 * nand_writev_ecc - [MTD Interface] write with iovec with ecc
1878 * @mtd: MTD device structure
1879 * @vecs: the iovectors to write
1880 * @count: number of vectors
1881 * @to: offset to write to
1882 * @retlen: pointer to variable to store the number of written bytes
1883 * @eccbuf: filesystem supplied oob data buffer
1884 * @oobsel: oob selection structure
1886 * NAND write with iovec with ecc
1888 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1889 loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
1891 int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
1892 int oob, numpages, autoplace = 0, startpage;
1893 struct nand_chip *this = mtd->priv;
1894 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1895 u_char *oobbuf, *bufstart;
1897 /* Preset written len for early exit */
1900 /* Calculate total length of data */
1902 for (i = 0; i < count; i++)
1903 total_len += (int) vecs[i].iov_len;
1905 DEBUG (MTD_DEBUG_LEVEL3,
1906 "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
1908 /* Do not allow write past end of page */
1909 if ((to + total_len) > mtd->size) {
1910 DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
1914 /* reject writes, which are not page aligned */
1915 if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
1916 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1920 /* Grab the lock and see if the device is available */
1921 nand_get_device (this, mtd, FL_WRITING);
1923 /* Get the current chip-nr */
1924 chipnr = (int) (to >> this->chip_shift);
1925 /* Select the NAND device */
1926 this->select_chip(mtd, chipnr);
1928 /* Check, if it is write protected */
1929 if (nand_check_wp(mtd))
1932 /* if oobsel is NULL, use chip defaults */
1934 oobsel = &mtd->oobinfo;
1936 /* Autoplace of oob data ? Use the default placement scheme */
1937 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1938 oobsel = this->autooob;
1941 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1944 /* Setup start page */
1945 page = (int) (to >> this->page_shift);
1946 /* Invalidate the page cache, if we write to the cached page */
1947 if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))
1950 startpage = page & this->pagemask;
1952 /* Loop until all kvec' data has been written */
1955 /* If the given tuple is >= pagesize then
1956 * write it out from the iov
1958 if ((vecs->iov_len - len) >= mtd->oobblock) {
1959 /* Calc number of pages we can write
1960 * out of this iov in one go */
1961 numpages = (vecs->iov_len - len) >> this->page_shift;
1962 /* Do not cross block boundaries */
1963 numpages = min (ppblock - (startpage & (ppblock - 1)), numpages);
1964 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1965 bufstart = (u_char *)vecs->iov_base;
1967 this->data_poi = bufstart;
1969 for (i = 1; i <= numpages; i++) {
1970 /* Write one page. If this is the last page to write
1971 * then use the real pageprogram command, else select
1972 * cached programming if supported by the chip.
1974 ret = nand_write_page (mtd, this, page & this->pagemask,
1975 &oobbuf[oob], oobsel, i != numpages);
1978 this->data_poi += mtd->oobblock;
1979 len += mtd->oobblock;
1980 oob += mtd->oobsize;
1983 /* Check, if we have to switch to the next tuple */
1984 if (len >= (int) vecs->iov_len) {
1990 /* We must use the internal buffer, read data out of each
1991 * tuple until we have a full page to write
1994 while (cnt < mtd->oobblock) {
1995 if (vecs->iov_base != NULL && vecs->iov_len)
1996 this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
1997 /* Check, if we have to switch to the next tuple */
1998 if (len >= (int) vecs->iov_len) {
2004 this->pagebuf = page;
2005 this->data_poi = this->data_buf;
2006 bufstart = this->data_poi;
2008 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
2009 ret = nand_write_page (mtd, this, page & this->pagemask,
2016 this->data_poi = bufstart;
2017 ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
2021 written += mtd->oobblock * numpages;
2026 startpage = page & this->pagemask;
2027 /* Check, if we cross a chip boundary */
2030 this->select_chip(mtd, -1);
2031 this->select_chip(mtd, chipnr);
2036 /* Deselect and wake up anyone waiting on the device */
2037 nand_release_device(mtd);
2045 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2046 * @mtd: MTD device structure
2047 * @page: the page address of the block which will be erased
2049 * Standard erase command for NAND chips
2051 static void single_erase_cmd (struct mtd_info *mtd, int page)
2053 struct nand_chip *this = mtd->priv;
2054 /* Send commands to erase a block */
2055 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2056 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2060 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2061 * @mtd: MTD device structure
2062 * @page: the page address of the block which will be erased
2064 * AND multi block erase command function
2065 * Erase 4 consecutive blocks
2067 static void multi_erase_cmd (struct mtd_info *mtd, int page)
2069 struct nand_chip *this = mtd->priv;
2070 /* Send commands to erase a block */
2071 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2072 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2073 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2074 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2075 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2079 * nand_erase - [MTD Interface] erase block(s)
2080 * @mtd: MTD device structure
2081 * @instr: erase instruction
2083 * Erase one ore more blocks
2085 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
2087 return nand_erase_nand (mtd, instr, 0);
2091 * nand_erase_intern - [NAND Interface] erase block(s)
2092 * @mtd: MTD device structure
2093 * @instr: erase instruction
2094 * @allowbbt: allow erasing the bbt area
2096 * Erase one ore more blocks
2098 int nand_erase_nand (struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
2100 int page, len, status, pages_per_block, ret, chipnr;
2101 struct nand_chip *this = mtd->priv;
2103 DEBUG (MTD_DEBUG_LEVEL3,
2104 "nand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
2106 /* Start address must align on block boundary */
2107 if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
2108 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2112 /* Length must align on block boundary */
2113 if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
2114 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
2118 /* Do not allow erase past end of device */
2119 if ((instr->len + instr->addr) > mtd->size) {
2120 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
2124 instr->fail_addr = 0xffffffff;
2126 /* Grab the lock and see if the device is available */
2127 nand_get_device (this, mtd, FL_ERASING);
2129 /* Shift to get first page */
2130 page = (int) (instr->addr >> this->page_shift);
2131 chipnr = (int) (instr->addr >> this->chip_shift);
2133 /* Calculate pages in each block */
2134 pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
2136 /* Select the NAND device */
2137 this->select_chip(mtd, chipnr);
2139 /* Check the WP bit */
2140 /* Check, if it is write protected */
2141 if (nand_check_wp(mtd)) {
2142 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
2143 instr->state = MTD_ERASE_FAILED;
2147 /* Loop through the pages */
2150 instr->state = MTD_ERASING;
2153 #ifndef NAND_ALLOW_ERASE_ALL
2154 /* Check if we have a bad block, we do not erase bad blocks ! */
2155 if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
2156 printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
2157 instr->state = MTD_ERASE_FAILED;
2161 /* Invalidate the page cache, if we erase the block which contains
2162 the current cached page */
2163 if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
2166 this->erase_cmd (mtd, page & this->pagemask);
2168 status = this->waitfunc (mtd, this, FL_ERASING);
2170 /* See if block erase succeeded */
2171 if (status & 0x01) {
2172 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
2173 instr->state = MTD_ERASE_FAILED;
2174 instr->fail_addr = (page << this->page_shift);
2178 /* Increment page address and decrement length */
2179 len -= (1 << this->phys_erase_shift);
2180 page += pages_per_block;
2182 /* Check, if we cross a chip boundary */
2183 if (len && !(page & this->pagemask)) {
2185 this->select_chip(mtd, -1);
2186 this->select_chip(mtd, chipnr);
2189 instr->state = MTD_ERASE_DONE;
2193 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2194 /* Do call back function */
2196 mtd_erase_callback(instr);
2198 /* Deselect and wake up anyone waiting on the device */
2199 nand_release_device(mtd);
2201 /* Return more or less happy */
2206 * nand_sync - [MTD Interface] sync
2207 * @mtd: MTD device structure
2209 * Sync is actually a wait for chip ready function
2211 static void nand_sync (struct mtd_info *mtd)
2213 struct nand_chip *this = mtd->priv;
2215 DEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2217 /* Grab the lock and see if the device is available */
2218 nand_get_device (this, mtd, FL_SYNCING);
2219 /* Release it and go back */
2220 nand_release_device (mtd);
2225 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2226 * @mtd: MTD device structure
2227 * @ofs: offset relative to mtd start
2229 static int nand_block_isbad (struct mtd_info *mtd, loff_t ofs)
2231 /* Check for invalid offset */
2232 if (ofs > mtd->size)
2235 return nand_block_checkbad (mtd, ofs, 1, 0);
2239 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2240 * @mtd: MTD device structure
2241 * @ofs: offset relative to mtd start
2243 static int nand_block_markbad (struct mtd_info *mtd, loff_t ofs)
2245 struct nand_chip *this = mtd->priv;
2248 if ((ret = nand_block_isbad(mtd, ofs))) {
2249 /* If it was bad already, return success and do nothing. */
2255 return this->block_markbad(mtd, ofs);
2259 * nand_scan - [NAND Interface] Scan for the NAND device
2260 * @mtd: MTD device structure
2261 * @maxchips: Number of chips to scan for
2263 * This fills out all the not initialized function pointers
2264 * with the defaults.
2265 * The flash ID is read and the mtd/chip structures are
2266 * filled with the appropriate values. Buffers are allocated if
2267 * they are not provided by the board driver
2270 int nand_scan (struct mtd_info *mtd, int maxchips)
2272 int i, j, nand_maf_id, nand_dev_id, busw;
2273 struct nand_chip *this = mtd->priv;
2275 /* Get buswidth to select the correct functions*/
2276 busw = this->options & NAND_BUSWIDTH_16;
2278 /* check for proper chip_delay setup, set 20us if not */
2279 if (!this->chip_delay)
2280 this->chip_delay = 20;
2282 /* check, if a user supplied command function given */
2283 if (this->cmdfunc == NULL)
2284 this->cmdfunc = nand_command;
2286 /* check, if a user supplied wait function given */
2287 if (this->waitfunc == NULL)
2288 this->waitfunc = nand_wait;
2290 if (!this->select_chip)
2291 this->select_chip = nand_select_chip;
2292 if (!this->write_byte)
2293 this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2294 if (!this->read_byte)
2295 this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2296 if (!this->write_word)
2297 this->write_word = nand_write_word;
2298 if (!this->read_word)
2299 this->read_word = nand_read_word;
2300 if (!this->block_bad)
2301 this->block_bad = nand_block_bad;
2302 if (!this->block_markbad)
2303 this->block_markbad = nand_default_block_markbad;
2304 if (!this->write_buf)
2305 this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2306 if (!this->read_buf)
2307 this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2308 if (!this->verify_buf)
2309 this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2310 if (!this->scan_bbt)
2311 this->scan_bbt = nand_default_bbt;
2313 /* Select the device */
2314 this->select_chip(mtd, 0);
2316 /* Send the command for reading device ID */
2317 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2319 /* Read manufacturer and device IDs */
2320 nand_maf_id = this->read_byte(mtd);
2321 nand_dev_id = this->read_byte(mtd);
2323 /* Print and store flash device information */
2324 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2326 if (nand_dev_id != nand_flash_ids[i].id)
2329 if (!mtd->name) mtd->name = nand_flash_ids[i].name;
2330 this->chipsize = nand_flash_ids[i].chipsize << 20;
2332 /* New devices have all the information in additional id bytes */
2333 if (!nand_flash_ids[i].pagesize) {
2335 /* The 3rd id byte contains non relevant data ATM */
2336 extid = this->read_byte(mtd);
2337 /* The 4th id byte is the important one */
2338 extid = this->read_byte(mtd);
2340 mtd->oobblock = 1024 << (extid & 0x3);
2343 mtd->oobsize = (8 << (extid & 0x03)) * (mtd->oobblock / 512);
2345 /* Calc blocksize. Blocksize is multiples of 64KiB */
2346 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2348 /* Get buswidth information */
2349 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2352 /* Old devices have this data hardcoded in the
2353 * device id table */
2354 mtd->erasesize = nand_flash_ids[i].erasesize;
2355 mtd->oobblock = nand_flash_ids[i].pagesize;
2356 mtd->oobsize = mtd->oobblock / 32;
2357 busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
2360 /* Check, if buswidth is correct. Hardware drivers should set
2362 if (busw != (this->options & NAND_BUSWIDTH_16)) {
2363 printk (KERN_INFO "NAND device: Manufacturer ID:"
2364 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2365 nand_manuf_ids[i].name , mtd->name);
2366 printk (KERN_WARNING
2367 "NAND bus width %d instead %d bit\n",
2368 (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
2370 this->select_chip(mtd, -1);
2374 /* Calculate the address shift from the page size */
2375 this->page_shift = ffs(mtd->oobblock) - 1;
2376 this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
2377 this->chip_shift = ffs(this->chipsize) - 1;
2379 /* Set the bad block position */
2380 this->badblockpos = mtd->oobblock > 512 ?
2381 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2383 /* Get chip options, preserve non chip based options */
2384 this->options &= ~NAND_CHIPOPTIONS_MSK;
2385 this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
2386 /* Set this as a default. Board drivers can override it, if neccecary */
2387 this->options |= NAND_NO_AUTOINCR;
2388 /* Check if this is a not a samsung device. Do not clear the options
2389 * for chips which are not having an extended id.
2391 if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
2392 this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2394 /* Check for AND chips with 4 page planes */
2395 if (this->options & NAND_4PAGE_ARRAY)
2396 this->erase_cmd = multi_erase_cmd;
2398 this->erase_cmd = single_erase_cmd;
2400 /* Do not replace user supplied command function ! */
2401 if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
2402 this->cmdfunc = nand_command_lp;
2404 /* Try to identify manufacturer */
2405 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
2406 if (nand_manuf_ids[j].id == nand_maf_id)
2412 if (!nand_flash_ids[i].name) {
2413 printk (KERN_WARNING "No NAND device found!!!\n");
2414 this->select_chip(mtd, -1);
2418 for (i=1; i < maxchips; i++) {
2419 this->select_chip(mtd, i);
2421 /* Send the command for reading device ID */
2422 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2424 /* Read manufacturer and device IDs */
2425 if (nand_maf_id != this->read_byte(mtd) ||
2426 nand_dev_id != this->read_byte(mtd))
2430 printk(KERN_INFO "%d NAND chips detected\n", i);
2432 /* Allocate buffers, if neccecary */
2433 if (!this->oob_buf) {
2435 len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
2436 this->oob_buf = kmalloc (len, GFP_KERNEL);
2437 if (!this->oob_buf) {
2438 printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
2441 this->options |= NAND_OOBBUF_ALLOC;
2444 if (!this->data_buf) {
2446 len = mtd->oobblock + mtd->oobsize;
2447 this->data_buf = kmalloc (len, GFP_KERNEL);
2448 if (!this->data_buf) {
2449 if (this->options & NAND_OOBBUF_ALLOC)
2450 kfree (this->oob_buf);
2451 printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
2454 this->options |= NAND_DATABUF_ALLOC;
2457 /* Store the number of chips and calc total size for mtd */
2459 mtd->size = i * this->chipsize;
2460 /* Convert chipsize to number of pages per chip -1. */
2461 this->pagemask = (this->chipsize >> this->page_shift) - 1;
2462 /* Preset the internal oob buffer */
2463 memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
2465 /* If no default placement scheme is given, select an
2466 * appropriate one */
2467 if (!this->autooob) {
2468 /* Select the appropriate default oob placement scheme for
2469 * placement agnostic filesystems */
2470 switch (mtd->oobsize) {
2472 this->autooob = &nand_oob_8;
2475 this->autooob = &nand_oob_16;
2478 this->autooob = &nand_oob_64;
2481 printk (KERN_WARNING "No oob scheme defined for oobsize %d\n",
2487 /* The number of bytes available for the filesystem to place fs dependend
2489 if (this->options & NAND_BUSWIDTH_16) {
2490 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 2);
2491 if (this->autooob->eccbytes & 0x01)
2494 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 1);
2497 * check ECC mode, default to software
2498 * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
2499 * fallback to software ECC
2501 this->eccsize = 256; /* set default eccsize */
2504 switch (this->eccmode) {
2505 case NAND_ECC_HW12_2048:
2506 if (mtd->oobblock < 2048) {
2507 printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
2509 this->eccmode = NAND_ECC_SOFT;
2510 this->calculate_ecc = nand_calculate_ecc;
2511 this->correct_data = nand_correct_data;
2513 this->eccsize = 2048;
2516 case NAND_ECC_HW3_512:
2517 case NAND_ECC_HW6_512:
2518 case NAND_ECC_HW8_512:
2519 if (mtd->oobblock == 256) {
2520 printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
2521 this->eccmode = NAND_ECC_SOFT;
2522 this->calculate_ecc = nand_calculate_ecc;
2523 this->correct_data = nand_correct_data;
2525 this->eccsize = 512; /* set eccsize to 512 */
2528 case NAND_ECC_HW3_256:
2532 printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
2533 this->eccmode = NAND_ECC_NONE;
2537 this->calculate_ecc = nand_calculate_ecc;
2538 this->correct_data = nand_correct_data;
2542 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
2546 /* Check hardware ecc function availability and adjust number of ecc bytes per
2549 switch (this->eccmode) {
2550 case NAND_ECC_HW12_2048:
2551 this->eccbytes += 4;
2552 case NAND_ECC_HW8_512:
2553 this->eccbytes += 2;
2554 case NAND_ECC_HW6_512:
2555 this->eccbytes += 3;
2556 case NAND_ECC_HW3_512:
2557 case NAND_ECC_HW3_256:
2558 if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
2560 printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
2564 mtd->eccsize = this->eccsize;
2566 /* Set the number of read / write steps for one page to ensure ECC generation */
2567 switch (this->eccmode) {
2568 case NAND_ECC_HW12_2048:
2569 this->eccsteps = mtd->oobblock / 2048;
2571 case NAND_ECC_HW3_512:
2572 case NAND_ECC_HW6_512:
2573 case NAND_ECC_HW8_512:
2574 this->eccsteps = mtd->oobblock / 512;
2576 case NAND_ECC_HW3_256:
2578 this->eccsteps = mtd->oobblock / 256;
2586 /* XXX U-BOOT XXX */
2588 /* Initialize state, waitqueue and spinlock */
2589 this->state = FL_READY;
2590 init_waitqueue_head (&this->wq);
2591 spin_lock_init (&this->chip_lock);
2594 /* De-select the device */
2595 this->select_chip(mtd, -1);
2597 /* Invalidate the pagebuffer reference */
2600 /* Fill in remaining MTD driver data */
2601 mtd->type = MTD_NANDFLASH;
2602 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
2603 mtd->ecctype = MTD_ECC_SW;
2604 mtd->erase = nand_erase;
2606 mtd->unpoint = NULL;
2607 mtd->read = nand_read;
2608 mtd->write = nand_write;
2609 mtd->read_ecc = nand_read_ecc;
2610 mtd->write_ecc = nand_write_ecc;
2611 mtd->read_oob = nand_read_oob;
2612 mtd->write_oob = nand_write_oob;
2613 /* XXX U-BOOT XXX */
2616 mtd->writev = nand_writev;
2617 mtd->writev_ecc = nand_writev_ecc;
2619 mtd->sync = nand_sync;
2620 /* XXX U-BOOT XXX */
2624 mtd->suspend = NULL;
2627 mtd->block_isbad = nand_block_isbad;
2628 mtd->block_markbad = nand_block_markbad;
2630 /* and make the autooob the default one */
2631 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
2632 /* XXX U-BOOT XXX */
2634 mtd->owner = THIS_MODULE;
2636 /* Build bad block table */
2637 return this->scan_bbt (mtd);
2641 * nand_release - [NAND Interface] Free resources held by the NAND device
2642 * @mtd: MTD device structure
2644 void nand_release (struct mtd_info *mtd)
2646 struct nand_chip *this = mtd->priv;
2648 #ifdef CONFIG_MTD_PARTITIONS
2649 /* Deregister partitions */
2650 del_mtd_partitions (mtd);
2652 /* Deregister the device */
2653 /* XXX U-BOOT XXX */
2655 del_mtd_device (mtd);
2657 /* Free bad block table memory, if allocated */
2660 /* Buffer allocated by nand_scan ? */
2661 if (this->options & NAND_OOBBUF_ALLOC)
2662 kfree (this->oob_buf);
2663 /* Buffer allocated by nand_scan ? */
2664 if (this->options & NAND_DATABUF_ALLOC)
2665 kfree (this->data_buf);