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>
74 #ifdef CONFIG_NEW_NAND_CODE
76 #if (CONFIG_COMMANDS & CFG_CMD_NAND)
80 #include <linux/mtd/compat.h>
81 #include <linux/mtd/mtd.h>
82 #include <linux/mtd/nand.h>
83 #include <linux/mtd/nand_ecc.h>
86 #include <asm/errno.h>
88 #ifdef CONFIG_JFFS2_NAND
89 #include <jffs2/jffs2.h>
92 /* Define default oob placement schemes for large and small page devices */
93 static struct nand_oobinfo nand_oob_8 = {
94 .useecc = MTD_NANDECC_AUTOPLACE,
97 .oobfree = { {3, 2}, {6, 2} }
100 static struct nand_oobinfo nand_oob_16 = {
101 .useecc = MTD_NANDECC_AUTOPLACE,
103 .eccpos = {0, 1, 2, 3, 6, 7},
104 .oobfree = { {8, 8} }
107 static struct nand_oobinfo nand_oob_64 = {
108 .useecc = MTD_NANDECC_AUTOPLACE,
111 40, 41, 42, 43, 44, 45, 46, 47,
112 48, 49, 50, 51, 52, 53, 54, 55,
113 56, 57, 58, 59, 60, 61, 62, 63},
114 .oobfree = { {2, 38} }
117 /* This is used for padding purposes in nand_write_oob */
118 static u_char ffchars[] = {
119 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
120 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
121 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
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,
130 * NAND low-level MTD interface functions
132 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
133 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
134 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
136 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
137 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
138 size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
139 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
140 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf);
141 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
142 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
143 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char *buf);
146 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
147 unsigned long count, loff_t to, size_t * retlen);
148 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs,
149 unsigned long count, loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
151 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
152 static void nand_sync (struct mtd_info *mtd);
154 /* Some internal functions */
155 static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf,
156 struct nand_oobinfo *oobsel, int mode);
157 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
158 static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
159 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode);
161 #define nand_verify_pages(...) (0)
164 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state);
167 * nand_release_device - [GENERIC] release chip
168 * @mtd: MTD device structure
170 * Deselect, release chip lock and wake up anyone waiting on the device
174 static void nand_release_device (struct mtd_info *mtd)
176 struct nand_chip *this = mtd->priv;
178 /* De-select the NAND device */
179 this->select_chip(mtd, -1);
180 /* Do we have a hardware controller ? */
181 if (this->controller) {
182 spin_lock(&this->controller->lock);
183 this->controller->active = NULL;
184 spin_unlock(&this->controller->lock);
186 /* Release the chip */
187 spin_lock (&this->chip_lock);
188 this->state = FL_READY;
190 spin_unlock (&this->chip_lock);
193 static void nand_release_device (struct mtd_info *mtd)
195 struct nand_chip *this = mtd->priv;
196 this->select_chip(mtd, -1); /* De-select the NAND device */
201 * nand_read_byte - [DEFAULT] read one byte from the chip
202 * @mtd: MTD device structure
204 * Default read function for 8bit buswith
206 static u_char nand_read_byte(struct mtd_info *mtd)
208 struct nand_chip *this = mtd->priv;
209 return readb(this->IO_ADDR_R);
213 * nand_write_byte - [DEFAULT] write one byte to the chip
214 * @mtd: MTD device structure
215 * @byte: pointer to data byte to write
217 * Default write function for 8it buswith
219 static void nand_write_byte(struct mtd_info *mtd, u_char byte)
221 struct nand_chip *this = mtd->priv;
222 writeb(byte, this->IO_ADDR_W);
226 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
227 * @mtd: MTD device structure
229 * Default read function for 16bit buswith with
230 * endianess conversion
232 static u_char nand_read_byte16(struct mtd_info *mtd)
234 struct nand_chip *this = mtd->priv;
235 return (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
239 * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
240 * @mtd: MTD device structure
241 * @byte: pointer to data byte to write
243 * Default write function for 16bit buswith with
244 * endianess conversion
246 static void nand_write_byte16(struct mtd_info *mtd, u_char byte)
248 struct nand_chip *this = mtd->priv;
249 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
253 * nand_read_word - [DEFAULT] read one word from the chip
254 * @mtd: MTD device structure
256 * Default read function for 16bit buswith without
257 * endianess conversion
259 static u16 nand_read_word(struct mtd_info *mtd)
261 struct nand_chip *this = mtd->priv;
262 return readw(this->IO_ADDR_R);
266 * nand_write_word - [DEFAULT] write one word to the chip
267 * @mtd: MTD device structure
268 * @word: data word to write
270 * Default write function for 16bit buswith without
271 * endianess conversion
273 static void nand_write_word(struct mtd_info *mtd, u16 word)
275 struct nand_chip *this = mtd->priv;
276 writew(word, this->IO_ADDR_W);
280 * nand_select_chip - [DEFAULT] control CE line
281 * @mtd: MTD device structure
282 * @chip: chipnumber to select, -1 for deselect
284 * Default select function for 1 chip devices.
286 static void nand_select_chip(struct mtd_info *mtd, int chip)
288 struct nand_chip *this = mtd->priv;
291 this->hwcontrol(mtd, NAND_CTL_CLRNCE);
294 this->hwcontrol(mtd, NAND_CTL_SETNCE);
303 * nand_write_buf - [DEFAULT] write buffer to chip
304 * @mtd: MTD device structure
306 * @len: number of bytes to write
308 * Default write function for 8bit buswith
310 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
313 struct nand_chip *this = mtd->priv;
315 for (i=0; i<len; i++)
316 writeb(buf[i], this->IO_ADDR_W);
320 * nand_read_buf - [DEFAULT] read chip data into buffer
321 * @mtd: MTD device structure
322 * @buf: buffer to store date
323 * @len: number of bytes to read
325 * Default read function for 8bit buswith
327 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
330 struct nand_chip *this = mtd->priv;
332 for (i=0; i<len; i++)
333 buf[i] = readb(this->IO_ADDR_R);
337 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
338 * @mtd: MTD device structure
339 * @buf: buffer containing the data to compare
340 * @len: number of bytes to compare
342 * Default verify function for 8bit buswith
344 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
347 struct nand_chip *this = mtd->priv;
349 for (i=0; i<len; i++)
350 if (buf[i] != readb(this->IO_ADDR_R))
357 * nand_write_buf16 - [DEFAULT] write buffer to chip
358 * @mtd: MTD device structure
360 * @len: number of bytes to write
362 * Default write function for 16bit buswith
364 static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
367 struct nand_chip *this = mtd->priv;
368 u16 *p = (u16 *) buf;
371 for (i=0; i<len; i++)
372 writew(p[i], this->IO_ADDR_W);
377 * nand_read_buf16 - [DEFAULT] read chip data into buffer
378 * @mtd: MTD device structure
379 * @buf: buffer to store date
380 * @len: number of bytes to read
382 * Default read function for 16bit buswith
384 static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
387 struct nand_chip *this = mtd->priv;
388 u16 *p = (u16 *) buf;
391 for (i=0; i<len; i++)
392 p[i] = readw(this->IO_ADDR_R);
396 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
397 * @mtd: MTD device structure
398 * @buf: buffer containing the data to compare
399 * @len: number of bytes to compare
401 * Default verify function for 16bit buswith
403 static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
406 struct nand_chip *this = mtd->priv;
407 u16 *p = (u16 *) buf;
410 for (i=0; i<len; i++)
411 if (p[i] != readw(this->IO_ADDR_R))
418 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
419 * @mtd: MTD device structure
420 * @ofs: offset from device start
421 * @getchip: 0, if the chip is already selected
423 * Check, if the block is bad.
425 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
427 int page, chipnr, res = 0;
428 struct nand_chip *this = mtd->priv;
432 page = (int)(ofs >> this->page_shift);
433 chipnr = (int)(ofs >> this->chip_shift);
435 /* Grab the lock and see if the device is available */
436 nand_get_device (this, mtd, FL_READING);
438 /* Select the NAND device */
439 this->select_chip(mtd, chipnr);
443 if (this->options & NAND_BUSWIDTH_16) {
444 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page & this->pagemask);
445 bad = cpu_to_le16(this->read_word(mtd));
446 if (this->badblockpos & 0x1)
448 if ((bad & 0xFF) != 0xff)
451 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page & this->pagemask);
452 if (this->read_byte(mtd) != 0xff)
457 /* Deselect and wake up anyone waiting on the device */
458 nand_release_device(mtd);
465 * nand_default_block_markbad - [DEFAULT] mark a block bad
466 * @mtd: MTD device structure
467 * @ofs: offset from device start
469 * This is the default implementation, which can be overridden by
470 * a hardware specific driver.
472 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
474 struct nand_chip *this = mtd->priv;
475 u_char buf[2] = {0, 0};
479 /* Get block number */
480 block = ((int) ofs) >> this->bbt_erase_shift;
481 this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
483 /* Do we have a flash based bad block table ? */
484 if (this->options & NAND_USE_FLASH_BBT)
485 return nand_update_bbt (mtd, ofs);
487 /* We write two bytes, so we dont have to mess with 16 bit access */
488 ofs += mtd->oobsize + (this->badblockpos & ~0x01);
489 return nand_write_oob (mtd, ofs , 2, &retlen, buf);
494 * nand_check_wp - [GENERIC] check if the chip is write protected
495 * @mtd: MTD device structure
496 * Check, if the device is write protected
498 * The function expects, that the device is already selected
500 static int nand_check_wp (struct mtd_info *mtd)
502 struct nand_chip *this = mtd->priv;
503 /* Check the WP bit */
504 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
505 return (this->read_byte(mtd) & 0x80) ? 0 : 1;
508 static int nand_check_wp (struct mtd_info *mtd)
514 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
515 * @mtd: MTD device structure
516 * @ofs: offset from device start
517 * @getchip: 0, if the chip is already selected
518 * @allowbbt: 1, if its allowed to access the bbt area
520 * Check, if the block is bad. Either by reading the bad block table or
521 * calling of the scan function.
523 static int nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
525 struct nand_chip *this = mtd->priv;
528 return this->block_bad(mtd, ofs, getchip);
530 /* Return info from the table */
531 return nand_isbad_bbt (mtd, ofs, allowbbt);
535 * nand_command - [DEFAULT] Send command to NAND device
536 * @mtd: MTD device structure
537 * @command: the command to be sent
538 * @column: the column address for this command, -1 if none
539 * @page_addr: the page address for this command, -1 if none
541 * Send command to NAND device. This function is used for small page
542 * devices (256/512 Bytes per page)
544 static void nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
546 register struct nand_chip *this = mtd->priv;
548 /* Begin command latch cycle */
549 this->hwcontrol(mtd, NAND_CTL_SETCLE);
551 * Write out the command to the device.
553 if (command == NAND_CMD_SEQIN) {
556 if (column >= mtd->oobblock) {
558 column -= mtd->oobblock;
559 readcmd = NAND_CMD_READOOB;
560 } else if (column < 256) {
561 /* First 256 bytes --> READ0 */
562 readcmd = NAND_CMD_READ0;
565 readcmd = NAND_CMD_READ1;
567 this->write_byte(mtd, readcmd);
569 this->write_byte(mtd, command);
571 /* Set ALE and clear CLE to start address cycle */
572 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
574 if (column != -1 || page_addr != -1) {
575 this->hwcontrol(mtd, NAND_CTL_SETALE);
577 /* Serially input address */
579 /* Adjust columns for 16 bit buswidth */
580 if (this->options & NAND_BUSWIDTH_16)
582 this->write_byte(mtd, column);
584 if (page_addr != -1) {
585 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
586 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
587 /* One more address cycle for devices > 32MiB */
588 if (this->chipsize > (32 << 20))
589 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));
591 /* Latch in address */
592 this->hwcontrol(mtd, NAND_CTL_CLRALE);
596 * program and erase have their own busy handlers
597 * status and sequential in needs no delay
601 case NAND_CMD_PAGEPROG:
602 case NAND_CMD_ERASE1:
603 case NAND_CMD_ERASE2:
605 case NAND_CMD_STATUS:
611 udelay(this->chip_delay);
612 this->hwcontrol(mtd, NAND_CTL_SETCLE);
613 this->write_byte(mtd, NAND_CMD_STATUS);
614 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
615 while ( !(this->read_byte(mtd) & 0x40));
618 /* This applies to read commands */
621 * If we don't have access to the busy pin, we apply the given
624 if (!this->dev_ready) {
625 udelay (this->chip_delay);
630 /* Apply this short delay always to ensure that we do wait tWB in
631 * any case on any machine. */
633 /* wait until command is processed */
634 while (!this->dev_ready(mtd));
638 * nand_command_lp - [DEFAULT] Send command to NAND large page device
639 * @mtd: MTD device structure
640 * @command: the command to be sent
641 * @column: the column address for this command, -1 if none
642 * @page_addr: the page address for this command, -1 if none
644 * Send command to NAND device. This is the version for the new large page devices
645 * We dont have the seperate regions as we have in the small page devices.
646 * We must emulate NAND_CMD_READOOB to keep the code compatible.
649 static void nand_command_lp (struct mtd_info *mtd, unsigned command, int column, int page_addr)
651 register struct nand_chip *this = mtd->priv;
653 /* Emulate NAND_CMD_READOOB */
654 if (command == NAND_CMD_READOOB) {
655 column += mtd->oobblock;
656 command = NAND_CMD_READ0;
660 /* Begin command latch cycle */
661 this->hwcontrol(mtd, NAND_CTL_SETCLE);
662 /* Write out the command to the device. */
663 this->write_byte(mtd, command);
664 /* End command latch cycle */
665 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
667 if (column != -1 || page_addr != -1) {
668 this->hwcontrol(mtd, NAND_CTL_SETALE);
670 /* Serially input address */
672 /* Adjust columns for 16 bit buswidth */
673 if (this->options & NAND_BUSWIDTH_16)
675 this->write_byte(mtd, column & 0xff);
676 this->write_byte(mtd, column >> 8);
678 if (page_addr != -1) {
679 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
680 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
681 /* One more address cycle for devices > 128MiB */
682 if (this->chipsize > (128 << 20))
683 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0xff));
685 /* Latch in address */
686 this->hwcontrol(mtd, NAND_CTL_CLRALE);
690 * program and erase have their own busy handlers
691 * status and sequential in needs no delay
695 case NAND_CMD_CACHEDPROG:
696 case NAND_CMD_PAGEPROG:
697 case NAND_CMD_ERASE1:
698 case NAND_CMD_ERASE2:
700 case NAND_CMD_STATUS:
707 udelay(this->chip_delay);
708 this->hwcontrol(mtd, NAND_CTL_SETCLE);
709 this->write_byte(mtd, NAND_CMD_STATUS);
710 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
711 while ( !(this->read_byte(mtd) & 0x40));
715 /* Begin command latch cycle */
716 this->hwcontrol(mtd, NAND_CTL_SETCLE);
717 /* Write out the start read command */
718 this->write_byte(mtd, NAND_CMD_READSTART);
719 /* End command latch cycle */
720 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
721 /* Fall through into ready check */
723 /* This applies to read commands */
726 * If we don't have access to the busy pin, we apply the given
729 if (!this->dev_ready) {
730 udelay (this->chip_delay);
735 /* Apply this short delay always to ensure that we do wait tWB in
736 * any case on any machine. */
738 /* wait until command is processed */
739 while (!this->dev_ready(mtd));
743 * nand_get_device - [GENERIC] Get chip for selected access
744 * @this: the nand chip descriptor
745 * @mtd: MTD device structure
746 * @new_state: the state which is requested
748 * Get the device and lock it for exclusive access
752 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
754 struct nand_chip *active = this;
756 DECLARE_WAITQUEUE (wait, current);
759 * Grab the lock and see if the device is available
762 /* Hardware controller shared among independend devices */
763 if (this->controller) {
764 spin_lock (&this->controller->lock);
765 if (this->controller->active)
766 active = this->controller->active;
768 this->controller->active = this;
769 spin_unlock (&this->controller->lock);
772 if (active == this) {
773 spin_lock (&this->chip_lock);
774 if (this->state == FL_READY) {
775 this->state = new_state;
776 spin_unlock (&this->chip_lock);
780 set_current_state (TASK_UNINTERRUPTIBLE);
781 add_wait_queue (&active->wq, &wait);
782 spin_unlock (&active->chip_lock);
784 remove_wait_queue (&active->wq, &wait);
788 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state) {}
792 * nand_wait - [DEFAULT] wait until the command is done
793 * @mtd: MTD device structure
794 * @this: NAND chip structure
795 * @state: state to select the max. timeout value
797 * Wait for command done. This applies to erase and program only
798 * Erase can take up to 400ms and program up to 20ms according to
799 * general NAND and SmartMedia specs
804 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
806 unsigned long timeo = jiffies;
809 if (state == FL_ERASING)
810 timeo += (HZ * 400) / 1000;
812 timeo += (HZ * 20) / 1000;
814 /* Apply this short delay always to ensure that we do wait tWB in
815 * any case on any machine. */
818 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
819 this->cmdfunc (mtd, NAND_CMD_STATUS_MULTI, -1, -1);
821 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
823 while (time_before(jiffies, timeo)) {
824 /* Check, if we were interrupted */
825 if (this->state != state)
828 if (this->dev_ready) {
829 if (this->dev_ready(mtd))
832 if (this->read_byte(mtd) & NAND_STATUS_READY)
837 status = (int) this->read_byte(mtd);
843 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
847 if (state == FL_ERASING)
848 timeo = CFG_HZ * 400;
852 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
853 this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
855 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
860 if (get_timer(0) > timeo) {
865 if (this->dev_ready) {
866 if (this->dev_ready(mtd))
869 if (this->read_byte(mtd) & NAND_STATUS_READY)
874 /* XXX nand device 1 on dave (PPChameleonEVB) needs more time */
876 while (get_timer(0) < 10);
878 return this->read_byte(mtd);
883 * nand_write_page - [GENERIC] write one page
884 * @mtd: MTD device structure
885 * @this: NAND chip structure
886 * @page: startpage inside the chip, must be called with (page & this->pagemask)
887 * @oob_buf: out of band data buffer
888 * @oobsel: out of band selecttion structre
889 * @cached: 1 = enable cached programming if supported by chip
891 * Nand_page_program function is used for write and writev !
892 * This function will always program a full page of data
893 * If you call it with a non page aligned buffer, you're lost :)
895 * Cached programming is not supported yet.
897 static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page,
898 u_char *oob_buf, struct nand_oobinfo *oobsel, int cached)
902 int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
903 int *oob_config = oobsel->eccpos;
904 int datidx = 0, eccidx = 0, eccsteps = this->eccsteps;
907 /* FIXME: Enable cached programming */
910 /* Send command to begin auto page programming */
911 this->cmdfunc (mtd, NAND_CMD_SEQIN, 0x00, page);
913 /* Write out complete page of data, take care of eccmode */
915 /* No ecc, write all */
917 printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
918 this->write_buf(mtd, this->data_poi, mtd->oobblock);
921 /* Software ecc 3/256, write all */
923 for (; eccsteps; eccsteps--) {
924 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
925 for (i = 0; i < 3; i++, eccidx++)
926 oob_buf[oob_config[eccidx]] = ecc_code[i];
927 datidx += this->eccsize;
929 this->write_buf(mtd, this->data_poi, mtd->oobblock);
932 eccbytes = this->eccbytes;
933 for (; eccsteps; eccsteps--) {
934 /* enable hardware ecc logic for write */
935 this->enable_hwecc(mtd, NAND_ECC_WRITE);
936 this->write_buf(mtd, &this->data_poi[datidx], this->eccsize);
937 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
938 for (i = 0; i < eccbytes; i++, eccidx++)
939 oob_buf[oob_config[eccidx]] = ecc_code[i];
940 /* If the hardware ecc provides syndromes then
941 * the ecc code must be written immidiately after
942 * the data bytes (words) */
943 if (this->options & NAND_HWECC_SYNDROME)
944 this->write_buf(mtd, ecc_code, eccbytes);
945 datidx += this->eccsize;
950 /* Write out OOB data */
951 if (this->options & NAND_HWECC_SYNDROME)
952 this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
954 this->write_buf(mtd, oob_buf, mtd->oobsize);
956 /* Send command to actually program the data */
957 this->cmdfunc (mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
960 /* call wait ready function */
961 status = this->waitfunc (mtd, this, FL_WRITING);
962 /* See if device thinks it succeeded */
964 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
968 /* FIXME: Implement cached programming ! */
969 /* wait until cache is ready*/
970 /* status = this->waitfunc (mtd, this, FL_CACHEDRPG); */
975 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
977 * nand_verify_pages - [GENERIC] verify the chip contents after a write
978 * @mtd: MTD device structure
979 * @this: NAND chip structure
980 * @page: startpage inside the chip, must be called with (page & this->pagemask)
981 * @numpages: number of pages to verify
982 * @oob_buf: out of band data buffer
983 * @oobsel: out of band selecttion structre
984 * @chipnr: number of the current chip
985 * @oobmode: 1 = full buffer verify, 0 = ecc only
987 * The NAND device assumes that it is always writing to a cleanly erased page.
988 * Hence, it performs its internal write verification only on bits that
989 * transitioned from 1 to 0. The device does NOT verify the whole page on a
990 * byte by byte basis. It is possible that the page was not completely erased
991 * or the page is becoming unusable due to wear. The read with ECC would catch
992 * the error later when the ECC page check fails, but we would rather catch
993 * it early in the page write stage. Better to write no data than invalid data.
995 static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
996 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
998 int i, j, datidx = 0, oobofs = 0, res = -EIO;
999 int eccsteps = this->eccsteps;
1003 hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
1005 /* Send command to read back the first page */
1006 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page);
1009 for (j = 0; j < eccsteps; j++) {
1010 /* Loop through and verify the data */
1011 if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
1012 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1015 datidx += mtd->eccsize;
1016 /* Have we a hw generator layout ? */
1019 if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
1020 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1023 oobofs += hweccbytes;
1026 /* check, if we must compare all data or if we just have to
1027 * compare the ecc bytes
1030 if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
1031 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1035 /* Read always, else autoincrement fails */
1036 this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
1038 if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
1039 int ecccnt = oobsel->eccbytes;
1041 for (i = 0; i < ecccnt; i++) {
1042 int idx = oobsel->eccpos[i];
1043 if (oobdata[idx] != oob_buf[oobofs + idx] ) {
1044 DEBUG (MTD_DEBUG_LEVEL0,
1045 "%s: Failed ECC write "
1046 "verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__, page, i);
1052 oobofs += mtd->oobsize - hweccbytes * eccsteps;
1056 /* Apply delay or wait for ready/busy pin
1057 * Do this before the AUTOINCR check, so no problems
1058 * arise if a chip which does auto increment
1059 * is marked as NOAUTOINCR by the board driver.
1060 * Do this also before returning, so the chip is
1061 * ready for the next command.
1063 if (!this->dev_ready)
1064 udelay (this->chip_delay);
1066 while (!this->dev_ready(mtd));
1068 /* All done, return happy */
1073 /* Check, if the chip supports auto page increment */
1074 if (!NAND_CANAUTOINCR(this))
1075 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1078 * Terminate the read command. We come here in case of an error
1079 * So we must issue a reset command.
1082 this->cmdfunc (mtd, NAND_CMD_RESET, -1, -1);
1088 * nand_read - [MTD Interface] MTD compability function for nand_read_ecc
1089 * @mtd: MTD device structure
1090 * @from: offset to read from
1091 * @len: number of bytes to read
1092 * @retlen: pointer to variable to store the number of read bytes
1093 * @buf: the databuffer to put data
1095 * This function simply calls nand_read_ecc with oob buffer and oobsel = NULL
1097 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1099 return nand_read_ecc (mtd, from, len, retlen, buf, NULL, NULL);
1104 * nand_read_ecc - [MTD Interface] Read data with ECC
1105 * @mtd: MTD device structure
1106 * @from: offset to read from
1107 * @len: number of bytes to read
1108 * @retlen: pointer to variable to store the number of read bytes
1109 * @buf: the databuffer to put data
1110 * @oob_buf: filesystem supplied oob data buffer
1111 * @oobsel: oob selection structure
1113 * NAND read with ECC
1115 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
1116 size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel)
1118 int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
1119 int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
1120 struct nand_chip *this = mtd->priv;
1121 u_char *data_poi, *oob_data = oob_buf;
1122 u_char ecc_calc[32];
1123 u_char ecc_code[32];
1124 int eccmode, eccsteps;
1125 int *oob_config, datidx;
1126 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1132 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1134 /* Do not allow reads past end of device */
1135 if ((from + len) > mtd->size) {
1136 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
1141 /* Grab the lock and see if the device is available */
1142 nand_get_device (this, mtd ,FL_READING);
1144 /* use userspace supplied oobinfo, if zero */
1146 oobsel = &mtd->oobinfo;
1148 /* Autoplace of oob data ? Use the default placement scheme */
1149 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1150 oobsel = this->autooob;
1152 eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
1153 oob_config = oobsel->eccpos;
1155 /* Select the NAND device */
1156 chipnr = (int)(from >> this->chip_shift);
1157 this->select_chip(mtd, chipnr);
1159 /* First we calculate the starting page */
1160 realpage = (int) (from >> this->page_shift);
1161 page = realpage & this->pagemask;
1163 /* Get raw starting column */
1164 col = from & (mtd->oobblock - 1);
1166 end = mtd->oobblock;
1167 ecc = this->eccsize;
1168 eccbytes = this->eccbytes;
1170 if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
1173 oobreadlen = mtd->oobsize;
1174 if (this->options & NAND_HWECC_SYNDROME)
1175 oobreadlen -= oobsel->eccbytes;
1177 /* Loop until all data read */
1178 while (read < len) {
1180 int aligned = (!col && (len - read) >= end);
1182 * If the read is not page aligned, we have to read into data buffer
1183 * due to ecc, else we read into return buffer direct
1186 data_poi = &buf[read];
1188 data_poi = this->data_buf;
1190 /* Check, if we have this page in the buffer
1192 * FIXME: Make it work when we must provide oob data too,
1193 * check the usage of data_buf oob field
1195 if (realpage == this->pagebuf && !oob_buf) {
1196 /* aligned read ? */
1198 memcpy (data_poi, this->data_buf, end);
1202 /* Check, if we must send the read command */
1204 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1208 /* get oob area, if we have no oob buffer from fs-driver */
1209 if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE ||
1210 oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1211 oob_data = &this->data_buf[end];
1213 eccsteps = this->eccsteps;
1216 case NAND_ECC_NONE: { /* No ECC, Read in a page */
1217 /* XXX U-BOOT XXX */
1219 static unsigned long lastwhinge = 0;
1220 if ((lastwhinge / HZ) != (jiffies / HZ)) {
1221 printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended\n");
1222 lastwhinge = jiffies;
1225 puts("Reading data from NAND FLASH without ECC is not recommended\n");
1227 this->read_buf(mtd, data_poi, end);
1231 case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
1232 this->read_buf(mtd, data_poi, end);
1233 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc)
1234 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1238 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) {
1239 this->enable_hwecc(mtd, NAND_ECC_READ);
1240 this->read_buf(mtd, &data_poi[datidx], ecc);
1242 /* HW ecc with syndrome calculation must read the
1243 * syndrome from flash immidiately after the data */
1245 /* Some hw ecc generators need to know when the
1246 * syndrome is read from flash */
1247 this->enable_hwecc(mtd, NAND_ECC_READSYN);
1248 this->read_buf(mtd, &oob_data[i], eccbytes);
1249 /* We calc error correction directly, it checks the hw
1250 * generator for an error, reads back the syndrome and
1251 * does the error correction on the fly */
1252 if (this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]) == -1) {
1253 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: "
1254 "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
1258 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1265 this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
1267 /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
1271 /* Pick the ECC bytes out of the oob data */
1272 for (j = 0; j < oobsel->eccbytes; j++)
1273 ecc_code[j] = oob_data[oob_config[j]];
1275 /* correct data, if neccecary */
1276 for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {
1277 ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
1279 /* Get next chunk of ecc bytes */
1282 /* Check, if we have a fs supplied oob-buffer,
1283 * This is the legacy mode. Used by YAFFS1
1284 * Should go away some day
1286 if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
1287 int *p = (int *)(&oob_data[mtd->oobsize]);
1291 if (ecc_status == -1) {
1292 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
1298 /* check, if we have a fs supplied oob-buffer */
1300 /* without autoplace. Legacy mode used by YAFFS1 */
1301 switch(oobsel->useecc) {
1302 case MTD_NANDECC_AUTOPLACE:
1303 case MTD_NANDECC_AUTOPL_USR:
1304 /* Walk through the autoplace chunks */
1305 for (i = 0, j = 0; j < mtd->oobavail; i++) {
1306 int from = oobsel->oobfree[i][0];
1307 int num = oobsel->oobfree[i][1];
1308 memcpy(&oob_buf[oob], &oob_data[from], num);
1311 oob += mtd->oobavail;
1313 case MTD_NANDECC_PLACE:
1314 /* YAFFS1 legacy mode */
1315 oob_data += this->eccsteps * sizeof (int);
1317 oob_data += mtd->oobsize;
1321 /* Partial page read, transfer data into fs buffer */
1323 for (j = col; j < end && read < len; j++)
1324 buf[read++] = data_poi[j];
1325 this->pagebuf = realpage;
1327 read += mtd->oobblock;
1329 /* Apply delay or wait for ready/busy pin
1330 * Do this before the AUTOINCR check, so no problems
1331 * arise if a chip which does auto increment
1332 * is marked as NOAUTOINCR by the board driver.
1334 if (!this->dev_ready)
1335 udelay (this->chip_delay);
1337 while (!this->dev_ready(mtd));
1342 /* For subsequent reads align to page boundary. */
1344 /* Increment page address */
1347 page = realpage & this->pagemask;
1348 /* Check, if we cross a chip boundary */
1351 this->select_chip(mtd, -1);
1352 this->select_chip(mtd, chipnr);
1354 /* Check, if the chip supports auto page increment
1355 * or if we have hit a block boundary.
1357 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1361 /* Deselect and wake up anyone waiting on the device */
1362 nand_release_device(mtd);
1365 * Return success, if no ECC failures, else -EBADMSG
1366 * fs driver will take care of that, because
1367 * retlen == desired len and result == -EBADMSG
1370 return ecc_failed ? -EBADMSG : 0;
1374 * nand_read_oob - [MTD Interface] NAND read out-of-band
1375 * @mtd: MTD device structure
1376 * @from: offset to read from
1377 * @len: number of bytes to read
1378 * @retlen: pointer to variable to store the number of read bytes
1379 * @buf: the databuffer to put data
1381 * NAND read out-of-band data from the spare area
1383 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1385 int i, col, page, chipnr;
1386 struct nand_chip *this = mtd->priv;
1387 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1389 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1391 /* Shift to get page */
1392 page = (int)(from >> this->page_shift);
1393 chipnr = (int)(from >> this->chip_shift);
1395 /* Mask to get column */
1396 col = from & (mtd->oobsize - 1);
1398 /* Initialize return length value */
1401 /* Do not allow reads past end of device */
1402 if ((from + len) > mtd->size) {
1403 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n");
1408 /* Grab the lock and see if the device is available */
1409 nand_get_device (this, mtd , FL_READING);
1411 /* Select the NAND device */
1412 this->select_chip(mtd, chipnr);
1414 /* Send the read command */
1415 this->cmdfunc (mtd, NAND_CMD_READOOB, col, page & this->pagemask);
1417 * Read the data, if we read more than one page
1418 * oob data, let the device transfer the data !
1422 int thislen = mtd->oobsize - col;
1423 thislen = min_t(int, thislen, len);
1424 this->read_buf(mtd, &buf[i], thislen);
1427 /* Apply delay or wait for ready/busy pin
1428 * Do this before the AUTOINCR check, so no problems
1429 * arise if a chip which does auto increment
1430 * is marked as NOAUTOINCR by the board driver.
1432 if (!this->dev_ready)
1433 udelay (this->chip_delay);
1435 while (!this->dev_ready(mtd));
1442 /* Check, if we cross a chip boundary */
1443 if (!(page & this->pagemask)) {
1445 this->select_chip(mtd, -1);
1446 this->select_chip(mtd, chipnr);
1449 /* Check, if the chip supports auto page increment
1450 * or if we have hit a block boundary.
1452 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
1453 /* For subsequent page reads set offset to 0 */
1454 this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
1459 /* Deselect and wake up anyone waiting on the device */
1460 nand_release_device(mtd);
1468 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
1469 * @mtd: MTD device structure
1470 * @buf: temporary buffer
1471 * @from: offset to read from
1472 * @len: number of bytes to read
1473 * @ooblen: number of oob data bytes to read
1475 * Read raw data including oob into buffer
1477 int nand_read_raw (struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
1479 struct nand_chip *this = mtd->priv;
1480 int page = (int) (from >> this->page_shift);
1481 int chip = (int) (from >> this->chip_shift);
1484 int pagesize = mtd->oobblock + mtd->oobsize;
1485 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1487 /* Do not allow reads past end of device */
1488 if ((from + len) > mtd->size) {
1489 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n");
1493 /* Grab the lock and see if the device is available */
1494 nand_get_device (this, mtd , FL_READING);
1496 this->select_chip (mtd, chip);
1498 /* Add requested oob length */
1503 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page & this->pagemask);
1506 this->read_buf (mtd, &buf[cnt], pagesize);
1512 if (!this->dev_ready)
1513 udelay (this->chip_delay);
1515 while (!this->dev_ready(mtd));
1517 /* Check, if the chip supports auto page increment */
1518 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1522 /* Deselect and wake up anyone waiting on the device */
1523 nand_release_device(mtd);
1529 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
1530 * @mtd: MTD device structure
1531 * @fsbuf: buffer given by fs driver
1532 * @oobsel: out of band selection structre
1533 * @autoplace: 1 = place given buffer into the oob bytes
1534 * @numpages: number of pages to prepare
1537 * 1. Filesystem buffer available and autoplacement is off,
1538 * return filesystem buffer
1539 * 2. No filesystem buffer or autoplace is off, return internal
1541 * 3. Filesystem buffer is given and autoplace selected
1542 * put data from fs buffer into internal buffer and
1543 * retrun internal buffer
1545 * Note: The internal buffer is filled with 0xff. This must
1546 * be done only once, when no autoplacement happens
1547 * Autoplacement sets the buffer dirty flag, which
1548 * forces the 0xff fill before using the buffer again.
1551 static u_char * nand_prepare_oobbuf (struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel,
1552 int autoplace, int numpages)
1554 struct nand_chip *this = mtd->priv;
1557 /* Zero copy fs supplied buffer */
1558 if (fsbuf && !autoplace)
1561 /* Check, if the buffer must be filled with ff again */
1562 if (this->oobdirty) {
1563 memset (this->oob_buf, 0xff,
1564 mtd->oobsize << (this->phys_erase_shift - this->page_shift));
1568 /* If we have no autoplacement or no fs buffer use the internal one */
1569 if (!autoplace || !fsbuf)
1570 return this->oob_buf;
1572 /* Walk through the pages and place the data */
1575 while (numpages--) {
1576 for (i = 0, len = 0; len < mtd->oobavail; i++) {
1577 int to = ofs + oobsel->oobfree[i][0];
1578 int num = oobsel->oobfree[i][1];
1579 memcpy (&this->oob_buf[to], fsbuf, num);
1583 ofs += mtd->oobavail;
1585 return this->oob_buf;
1588 #define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
1591 * nand_write - [MTD Interface] compability function for nand_write_ecc
1592 * @mtd: MTD device structure
1593 * @to: offset to write to
1594 * @len: number of bytes to write
1595 * @retlen: pointer to variable to store the number of written bytes
1596 * @buf: the data to write
1598 * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
1601 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1603 return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));
1607 * nand_write_ecc - [MTD Interface] NAND write with ECC
1608 * @mtd: MTD device structure
1609 * @to: offset to write to
1610 * @len: number of bytes to write
1611 * @retlen: pointer to variable to store the number of written bytes
1612 * @buf: the data to write
1613 * @eccbuf: filesystem supplied oob data buffer
1614 * @oobsel: oob selection structure
1616 * NAND write with ECC
1618 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
1619 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
1621 int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
1622 int autoplace = 0, numpages, totalpages;
1623 struct nand_chip *this = mtd->priv;
1624 u_char *oobbuf, *bufstart;
1625 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1627 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1629 /* Initialize retlen, in case of early exit */
1632 /* Do not allow write past end of device */
1633 if ((to + len) > mtd->size) {
1634 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n");
1638 /* reject writes, which are not page aligned */
1639 if (NOTALIGNED (to) || NOTALIGNED(len)) {
1640 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1644 /* Grab the lock and see if the device is available */
1645 nand_get_device (this, mtd, FL_WRITING);
1647 /* Calculate chipnr */
1648 chipnr = (int)(to >> this->chip_shift);
1649 /* Select the NAND device */
1650 this->select_chip(mtd, chipnr);
1652 /* Check, if it is write protected */
1653 if (nand_check_wp(mtd))
1656 /* if oobsel is NULL, use chip defaults */
1658 oobsel = &mtd->oobinfo;
1660 /* Autoplace of oob data ? Use the default placement scheme */
1661 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1662 oobsel = this->autooob;
1665 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1668 /* Setup variables and oob buffer */
1669 totalpages = len >> this->page_shift;
1670 page = (int) (to >> this->page_shift);
1671 /* Invalidate the page cache, if we write to the cached page */
1672 if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
1675 /* Set it relative to chip */
1676 page &= this->pagemask;
1678 /* Calc number of pages we can write in one go */
1679 numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages);
1680 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages);
1681 bufstart = (u_char *)buf;
1683 /* Loop until all data is written */
1684 while (written < len) {
1686 this->data_poi = (u_char*) &buf[written];
1687 /* Write one page. If this is the last page to write
1688 * or the last page in this block, then use the
1689 * real pageprogram command, else select cached programming
1690 * if supported by the chip.
1692 ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
1694 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret);
1698 oob += mtd->oobsize;
1699 /* Update written bytes count */
1700 written += mtd->oobblock;
1704 /* Increment page address */
1707 /* Have we hit a block boundary ? Then we have to verify and
1708 * if verify is ok, we have to setup the oob buffer for
1711 if (!(page & (ppblock - 1))){
1713 this->data_poi = bufstart;
1714 ret = nand_verify_pages (mtd, this, startpage,
1716 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1718 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1723 ofs = autoplace ? mtd->oobavail : mtd->oobsize;
1725 eccbuf += (page - startpage) * ofs;
1726 totalpages -= page - startpage;
1727 numpages = min (totalpages, ppblock);
1728 page &= this->pagemask;
1732 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel,
1733 autoplace, numpages);
1734 /* Check, if we cross a chip boundary */
1737 this->select_chip(mtd, -1);
1738 this->select_chip(mtd, chipnr);
1742 /* Verify the remaining pages */
1744 this->data_poi = bufstart;
1745 ret = nand_verify_pages (mtd, this, startpage, totalpages,
1746 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1750 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1753 /* Deselect and wake up anyone waiting on the device */
1754 nand_release_device(mtd);
1761 * nand_write_oob - [MTD Interface] NAND write out-of-band
1762 * @mtd: MTD device structure
1763 * @to: offset to write to
1764 * @len: number of bytes to write
1765 * @retlen: pointer to variable to store the number of written bytes
1766 * @buf: the data to write
1768 * NAND write out-of-band
1770 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1772 int column, page, status, ret = -EIO, chipnr;
1773 struct nand_chip *this = mtd->priv;
1775 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1777 /* Shift to get page */
1778 page = (int) (to >> this->page_shift);
1779 chipnr = (int) (to >> this->chip_shift);
1781 /* Mask to get column */
1782 column = to & (mtd->oobsize - 1);
1784 /* Initialize return length value */
1787 /* Do not allow write past end of page */
1788 if ((column + len) > mtd->oobsize) {
1789 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
1793 /* Grab the lock and see if the device is available */
1794 nand_get_device (this, mtd, FL_WRITING);
1796 /* Select the NAND device */
1797 this->select_chip(mtd, chipnr);
1799 /* Reset the chip. Some chips (like the Toshiba TC5832DC found
1800 in one of my DiskOnChip 2000 test units) will clear the whole
1801 data page too if we don't do this. I have no clue why, but
1802 I seem to have 'fixed' it in the doc2000 driver in
1803 August 1999. dwmw2. */
1804 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1806 /* Check, if it is write protected */
1807 if (nand_check_wp(mtd))
1810 /* Invalidate the page cache, if we write to the cached page */
1811 if (page == this->pagebuf)
1814 if (NAND_MUST_PAD(this)) {
1815 /* Write out desired data */
1816 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
1817 /* prepad 0xff for partial programming */
1818 this->write_buf(mtd, ffchars, column);
1820 this->write_buf(mtd, buf, len);
1821 /* postpad 0xff for partial programming */
1822 this->write_buf(mtd, ffchars, mtd->oobsize - (len+column));
1824 /* Write out desired data */
1825 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
1827 this->write_buf(mtd, buf, len);
1829 /* Send command to program the OOB data */
1830 this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1);
1832 status = this->waitfunc (mtd, this, FL_WRITING);
1834 /* See if device thinks it succeeded */
1835 if (status & 0x01) {
1836 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
1843 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1844 /* Send command to read back the data */
1845 this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask);
1847 if (this->verify_buf(mtd, buf, len)) {
1848 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
1855 /* Deselect and wake up anyone waiting on the device */
1856 nand_release_device(mtd);
1861 /* XXX U-BOOT XXX */
1864 * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
1865 * @mtd: MTD device structure
1866 * @vecs: the iovectors to write
1867 * @count: number of vectors
1868 * @to: offset to write to
1869 * @retlen: pointer to variable to store the number of written bytes
1871 * NAND write with kvec. This just calls the ecc function
1873 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1874 loff_t to, size_t * retlen)
1876 return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL));
1880 * nand_writev_ecc - [MTD Interface] write with iovec with ecc
1881 * @mtd: MTD device structure
1882 * @vecs: the iovectors to write
1883 * @count: number of vectors
1884 * @to: offset to write to
1885 * @retlen: pointer to variable to store the number of written bytes
1886 * @eccbuf: filesystem supplied oob data buffer
1887 * @oobsel: oob selection structure
1889 * NAND write with iovec with ecc
1891 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1892 loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
1894 int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
1895 int oob, numpages, autoplace = 0, startpage;
1896 struct nand_chip *this = mtd->priv;
1897 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1898 u_char *oobbuf, *bufstart;
1900 /* Preset written len for early exit */
1903 /* Calculate total length of data */
1905 for (i = 0; i < count; i++)
1906 total_len += (int) vecs[i].iov_len;
1908 DEBUG (MTD_DEBUG_LEVEL3,
1909 "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
1911 /* Do not allow write past end of page */
1912 if ((to + total_len) > mtd->size) {
1913 DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
1917 /* reject writes, which are not page aligned */
1918 if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
1919 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1923 /* Grab the lock and see if the device is available */
1924 nand_get_device (this, mtd, FL_WRITING);
1926 /* Get the current chip-nr */
1927 chipnr = (int) (to >> this->chip_shift);
1928 /* Select the NAND device */
1929 this->select_chip(mtd, chipnr);
1931 /* Check, if it is write protected */
1932 if (nand_check_wp(mtd))
1935 /* if oobsel is NULL, use chip defaults */
1937 oobsel = &mtd->oobinfo;
1939 /* Autoplace of oob data ? Use the default placement scheme */
1940 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1941 oobsel = this->autooob;
1944 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1947 /* Setup start page */
1948 page = (int) (to >> this->page_shift);
1949 /* Invalidate the page cache, if we write to the cached page */
1950 if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))
1953 startpage = page & this->pagemask;
1955 /* Loop until all kvec' data has been written */
1958 /* If the given tuple is >= pagesize then
1959 * write it out from the iov
1961 if ((vecs->iov_len - len) >= mtd->oobblock) {
1962 /* Calc number of pages we can write
1963 * out of this iov in one go */
1964 numpages = (vecs->iov_len - len) >> this->page_shift;
1965 /* Do not cross block boundaries */
1966 numpages = min (ppblock - (startpage & (ppblock - 1)), numpages);
1967 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1968 bufstart = (u_char *)vecs->iov_base;
1970 this->data_poi = bufstart;
1972 for (i = 1; i <= numpages; i++) {
1973 /* Write one page. If this is the last page to write
1974 * then use the real pageprogram command, else select
1975 * cached programming if supported by the chip.
1977 ret = nand_write_page (mtd, this, page & this->pagemask,
1978 &oobbuf[oob], oobsel, i != numpages);
1981 this->data_poi += mtd->oobblock;
1982 len += mtd->oobblock;
1983 oob += mtd->oobsize;
1986 /* Check, if we have to switch to the next tuple */
1987 if (len >= (int) vecs->iov_len) {
1993 /* We must use the internal buffer, read data out of each
1994 * tuple until we have a full page to write
1997 while (cnt < mtd->oobblock) {
1998 if (vecs->iov_base != NULL && vecs->iov_len)
1999 this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
2000 /* Check, if we have to switch to the next tuple */
2001 if (len >= (int) vecs->iov_len) {
2007 this->pagebuf = page;
2008 this->data_poi = this->data_buf;
2009 bufstart = this->data_poi;
2011 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
2012 ret = nand_write_page (mtd, this, page & this->pagemask,
2019 this->data_poi = bufstart;
2020 ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
2024 written += mtd->oobblock * numpages;
2029 startpage = page & this->pagemask;
2030 /* Check, if we cross a chip boundary */
2033 this->select_chip(mtd, -1);
2034 this->select_chip(mtd, chipnr);
2039 /* Deselect and wake up anyone waiting on the device */
2040 nand_release_device(mtd);
2048 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2049 * @mtd: MTD device structure
2050 * @page: the page address of the block which will be erased
2052 * Standard erase command for NAND chips
2054 static void single_erase_cmd (struct mtd_info *mtd, int page)
2056 struct nand_chip *this = mtd->priv;
2057 /* Send commands to erase a block */
2058 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2059 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2063 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2064 * @mtd: MTD device structure
2065 * @page: the page address of the block which will be erased
2067 * AND multi block erase command function
2068 * Erase 4 consecutive blocks
2070 static void multi_erase_cmd (struct mtd_info *mtd, int page)
2072 struct nand_chip *this = mtd->priv;
2073 /* Send commands to erase a block */
2074 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2075 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2076 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2077 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2078 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2082 * nand_erase - [MTD Interface] erase block(s)
2083 * @mtd: MTD device structure
2084 * @instr: erase instruction
2086 * Erase one ore more blocks
2088 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
2090 return nand_erase_nand (mtd, instr, 0);
2094 * nand_erase_intern - [NAND Interface] erase block(s)
2095 * @mtd: MTD device structure
2096 * @instr: erase instruction
2097 * @allowbbt: allow erasing the bbt area
2099 * Erase one ore more blocks
2101 int nand_erase_nand (struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
2103 int page, len, status, pages_per_block, ret, chipnr;
2104 struct nand_chip *this = mtd->priv;
2106 DEBUG (MTD_DEBUG_LEVEL3,
2107 "nand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
2109 /* Start address must align on block boundary */
2110 if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
2111 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2115 /* Length must align on block boundary */
2116 if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
2117 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
2121 /* Do not allow erase past end of device */
2122 if ((instr->len + instr->addr) > mtd->size) {
2123 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
2127 instr->fail_addr = 0xffffffff;
2129 /* Grab the lock and see if the device is available */
2130 nand_get_device (this, mtd, FL_ERASING);
2132 /* Shift to get first page */
2133 page = (int) (instr->addr >> this->page_shift);
2134 chipnr = (int) (instr->addr >> this->chip_shift);
2136 /* Calculate pages in each block */
2137 pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
2139 /* Select the NAND device */
2140 this->select_chip(mtd, chipnr);
2142 /* Check the WP bit */
2143 /* Check, if it is write protected */
2144 if (nand_check_wp(mtd)) {
2145 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
2146 instr->state = MTD_ERASE_FAILED;
2150 /* Loop through the pages */
2153 instr->state = MTD_ERASING;
2156 #ifndef NAND_ALLOW_ERASE_ALL
2157 /* Check if we have a bad block, we do not erase bad blocks ! */
2158 if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
2159 printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
2160 instr->state = MTD_ERASE_FAILED;
2164 /* Invalidate the page cache, if we erase the block which contains
2165 the current cached page */
2166 if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
2169 this->erase_cmd (mtd, page & this->pagemask);
2171 status = this->waitfunc (mtd, this, FL_ERASING);
2173 /* See if block erase succeeded */
2174 if (status & 0x01) {
2175 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
2176 instr->state = MTD_ERASE_FAILED;
2177 instr->fail_addr = (page << this->page_shift);
2181 /* Increment page address and decrement length */
2182 len -= (1 << this->phys_erase_shift);
2183 page += pages_per_block;
2185 /* Check, if we cross a chip boundary */
2186 if (len && !(page & this->pagemask)) {
2188 this->select_chip(mtd, -1);
2189 this->select_chip(mtd, chipnr);
2192 instr->state = MTD_ERASE_DONE;
2196 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2197 /* Do call back function */
2199 mtd_erase_callback(instr);
2201 /* Deselect and wake up anyone waiting on the device */
2202 nand_release_device(mtd);
2204 /* Return more or less happy */
2209 * nand_sync - [MTD Interface] sync
2210 * @mtd: MTD device structure
2212 * Sync is actually a wait for chip ready function
2214 static void nand_sync (struct mtd_info *mtd)
2216 struct nand_chip *this = mtd->priv;
2218 DEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2220 /* Grab the lock and see if the device is available */
2221 nand_get_device (this, mtd, FL_SYNCING);
2222 /* Release it and go back */
2223 nand_release_device (mtd);
2228 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2229 * @mtd: MTD device structure
2230 * @ofs: offset relative to mtd start
2232 static int nand_block_isbad (struct mtd_info *mtd, loff_t ofs)
2234 /* Check for invalid offset */
2235 if (ofs > mtd->size)
2238 return nand_block_checkbad (mtd, ofs, 1, 0);
2242 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2243 * @mtd: MTD device structure
2244 * @ofs: offset relative to mtd start
2246 static int nand_block_markbad (struct mtd_info *mtd, loff_t ofs)
2248 struct nand_chip *this = mtd->priv;
2251 if ((ret = nand_block_isbad(mtd, ofs))) {
2252 /* If it was bad already, return success and do nothing. */
2258 return this->block_markbad(mtd, ofs);
2262 * nand_scan - [NAND Interface] Scan for the NAND device
2263 * @mtd: MTD device structure
2264 * @maxchips: Number of chips to scan for
2266 * This fills out all the not initialized function pointers
2267 * with the defaults.
2268 * The flash ID is read and the mtd/chip structures are
2269 * filled with the appropriate values. Buffers are allocated if
2270 * they are not provided by the board driver
2273 int nand_scan (struct mtd_info *mtd, int maxchips)
2275 int i, j, nand_maf_id, nand_dev_id, busw;
2276 struct nand_chip *this = mtd->priv;
2278 /* Get buswidth to select the correct functions*/
2279 busw = this->options & NAND_BUSWIDTH_16;
2281 /* check for proper chip_delay setup, set 20us if not */
2282 if (!this->chip_delay)
2283 this->chip_delay = 20;
2285 /* check, if a user supplied command function given */
2286 if (this->cmdfunc == NULL)
2287 this->cmdfunc = nand_command;
2289 /* check, if a user supplied wait function given */
2290 if (this->waitfunc == NULL)
2291 this->waitfunc = nand_wait;
2293 if (!this->select_chip)
2294 this->select_chip = nand_select_chip;
2295 if (!this->write_byte)
2296 this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2297 if (!this->read_byte)
2298 this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2299 if (!this->write_word)
2300 this->write_word = nand_write_word;
2301 if (!this->read_word)
2302 this->read_word = nand_read_word;
2303 if (!this->block_bad)
2304 this->block_bad = nand_block_bad;
2305 if (!this->block_markbad)
2306 this->block_markbad = nand_default_block_markbad;
2307 if (!this->write_buf)
2308 this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2309 if (!this->read_buf)
2310 this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2311 if (!this->verify_buf)
2312 this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2313 if (!this->scan_bbt)
2314 this->scan_bbt = nand_default_bbt;
2316 /* Select the device */
2317 this->select_chip(mtd, 0);
2319 /* Send the command for reading device ID */
2320 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2322 /* Read manufacturer and device IDs */
2323 nand_maf_id = this->read_byte(mtd);
2324 nand_dev_id = this->read_byte(mtd);
2326 /* Print and store flash device information */
2327 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2329 if (nand_dev_id != nand_flash_ids[i].id)
2332 if (!mtd->name) mtd->name = nand_flash_ids[i].name;
2333 this->chipsize = nand_flash_ids[i].chipsize << 20;
2335 /* New devices have all the information in additional id bytes */
2336 if (!nand_flash_ids[i].pagesize) {
2338 /* The 3rd id byte contains non relevant data ATM */
2339 extid = this->read_byte(mtd);
2340 /* The 4th id byte is the important one */
2341 extid = this->read_byte(mtd);
2343 mtd->oobblock = 1024 << (extid & 0x3);
2346 mtd->oobsize = (8 << (extid & 0x03)) * (mtd->oobblock / 512);
2348 /* Calc blocksize. Blocksize is multiples of 64KiB */
2349 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2351 /* Get buswidth information */
2352 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2355 /* Old devices have this data hardcoded in the
2356 * device id table */
2357 mtd->erasesize = nand_flash_ids[i].erasesize;
2358 mtd->oobblock = nand_flash_ids[i].pagesize;
2359 mtd->oobsize = mtd->oobblock / 32;
2360 busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
2363 /* Check, if buswidth is correct. Hardware drivers should set
2365 if (busw != (this->options & NAND_BUSWIDTH_16)) {
2366 printk (KERN_INFO "NAND device: Manufacturer ID:"
2367 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2368 nand_manuf_ids[i].name , mtd->name);
2369 printk (KERN_WARNING
2370 "NAND bus width %d instead %d bit\n",
2371 (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
2373 this->select_chip(mtd, -1);
2377 /* Calculate the address shift from the page size */
2378 this->page_shift = ffs(mtd->oobblock) - 1;
2379 this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
2380 this->chip_shift = ffs(this->chipsize) - 1;
2382 /* Set the bad block position */
2383 this->badblockpos = mtd->oobblock > 512 ?
2384 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2386 /* Get chip options, preserve non chip based options */
2387 this->options &= ~NAND_CHIPOPTIONS_MSK;
2388 this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
2389 /* Set this as a default. Board drivers can override it, if neccecary */
2390 this->options |= NAND_NO_AUTOINCR;
2391 /* Check if this is a not a samsung device. Do not clear the options
2392 * for chips which are not having an extended id.
2394 if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
2395 this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2397 /* Check for AND chips with 4 page planes */
2398 if (this->options & NAND_4PAGE_ARRAY)
2399 this->erase_cmd = multi_erase_cmd;
2401 this->erase_cmd = single_erase_cmd;
2403 /* Do not replace user supplied command function ! */
2404 if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
2405 this->cmdfunc = nand_command_lp;
2407 /* Try to identify manufacturer */
2408 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
2409 if (nand_manuf_ids[j].id == nand_maf_id)
2415 if (!nand_flash_ids[i].name) {
2416 printk (KERN_WARNING "No NAND device found!!!\n");
2417 this->select_chip(mtd, -1);
2421 for (i=1; i < maxchips; i++) {
2422 this->select_chip(mtd, i);
2424 /* Send the command for reading device ID */
2425 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2427 /* Read manufacturer and device IDs */
2428 if (nand_maf_id != this->read_byte(mtd) ||
2429 nand_dev_id != this->read_byte(mtd))
2433 printk(KERN_INFO "%d NAND chips detected\n", i);
2435 /* Allocate buffers, if neccecary */
2436 if (!this->oob_buf) {
2438 len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
2439 this->oob_buf = kmalloc (len, GFP_KERNEL);
2440 if (!this->oob_buf) {
2441 printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
2444 this->options |= NAND_OOBBUF_ALLOC;
2447 if (!this->data_buf) {
2449 len = mtd->oobblock + mtd->oobsize;
2450 this->data_buf = kmalloc (len, GFP_KERNEL);
2451 if (!this->data_buf) {
2452 if (this->options & NAND_OOBBUF_ALLOC)
2453 kfree (this->oob_buf);
2454 printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
2457 this->options |= NAND_DATABUF_ALLOC;
2460 /* Store the number of chips and calc total size for mtd */
2462 mtd->size = i * this->chipsize;
2463 /* Convert chipsize to number of pages per chip -1. */
2464 this->pagemask = (this->chipsize >> this->page_shift) - 1;
2465 /* Preset the internal oob buffer */
2466 memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
2468 /* If no default placement scheme is given, select an
2469 * appropriate one */
2470 if (!this->autooob) {
2471 /* Select the appropriate default oob placement scheme for
2472 * placement agnostic filesystems */
2473 switch (mtd->oobsize) {
2475 this->autooob = &nand_oob_8;
2478 this->autooob = &nand_oob_16;
2481 this->autooob = &nand_oob_64;
2484 printk (KERN_WARNING "No oob scheme defined for oobsize %d\n",
2490 /* The number of bytes available for the filesystem to place fs dependend
2492 if (this->options & NAND_BUSWIDTH_16) {
2493 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 2);
2494 if (this->autooob->eccbytes & 0x01)
2497 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 1);
2500 * check ECC mode, default to software
2501 * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
2502 * fallback to software ECC
2504 this->eccsize = 256; /* set default eccsize */
2507 switch (this->eccmode) {
2508 case NAND_ECC_HW12_2048:
2509 if (mtd->oobblock < 2048) {
2510 printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
2512 this->eccmode = NAND_ECC_SOFT;
2513 this->calculate_ecc = nand_calculate_ecc;
2514 this->correct_data = nand_correct_data;
2516 this->eccsize = 2048;
2519 case NAND_ECC_HW3_512:
2520 case NAND_ECC_HW6_512:
2521 case NAND_ECC_HW8_512:
2522 if (mtd->oobblock == 256) {
2523 printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
2524 this->eccmode = NAND_ECC_SOFT;
2525 this->calculate_ecc = nand_calculate_ecc;
2526 this->correct_data = nand_correct_data;
2528 this->eccsize = 512; /* set eccsize to 512 */
2531 case NAND_ECC_HW3_256:
2535 printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
2536 this->eccmode = NAND_ECC_NONE;
2540 this->calculate_ecc = nand_calculate_ecc;
2541 this->correct_data = nand_correct_data;
2545 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
2549 /* Check hardware ecc function availability and adjust number of ecc bytes per
2552 switch (this->eccmode) {
2553 case NAND_ECC_HW12_2048:
2554 this->eccbytes += 4;
2555 case NAND_ECC_HW8_512:
2556 this->eccbytes += 2;
2557 case NAND_ECC_HW6_512:
2558 this->eccbytes += 3;
2559 case NAND_ECC_HW3_512:
2560 case NAND_ECC_HW3_256:
2561 if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
2563 printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
2567 mtd->eccsize = this->eccsize;
2569 /* Set the number of read / write steps for one page to ensure ECC generation */
2570 switch (this->eccmode) {
2571 case NAND_ECC_HW12_2048:
2572 this->eccsteps = mtd->oobblock / 2048;
2574 case NAND_ECC_HW3_512:
2575 case NAND_ECC_HW6_512:
2576 case NAND_ECC_HW8_512:
2577 this->eccsteps = mtd->oobblock / 512;
2579 case NAND_ECC_HW3_256:
2581 this->eccsteps = mtd->oobblock / 256;
2589 /* XXX U-BOOT XXX */
2591 /* Initialize state, waitqueue and spinlock */
2592 this->state = FL_READY;
2593 init_waitqueue_head (&this->wq);
2594 spin_lock_init (&this->chip_lock);
2597 /* De-select the device */
2598 this->select_chip(mtd, -1);
2600 /* Invalidate the pagebuffer reference */
2603 /* Fill in remaining MTD driver data */
2604 mtd->type = MTD_NANDFLASH;
2605 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
2606 mtd->ecctype = MTD_ECC_SW;
2607 mtd->erase = nand_erase;
2609 mtd->unpoint = NULL;
2610 mtd->read = nand_read;
2611 mtd->write = nand_write;
2612 mtd->read_ecc = nand_read_ecc;
2613 mtd->write_ecc = nand_write_ecc;
2614 mtd->read_oob = nand_read_oob;
2615 mtd->write_oob = nand_write_oob;
2616 /* XXX U-BOOT XXX */
2619 mtd->writev = nand_writev;
2620 mtd->writev_ecc = nand_writev_ecc;
2622 mtd->sync = nand_sync;
2623 /* XXX U-BOOT XXX */
2627 mtd->suspend = NULL;
2630 mtd->block_isbad = nand_block_isbad;
2631 mtd->block_markbad = nand_block_markbad;
2633 /* and make the autooob the default one */
2634 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
2635 /* XXX U-BOOT XXX */
2637 mtd->owner = THIS_MODULE;
2639 /* Build bad block table */
2640 return this->scan_bbt (mtd);
2644 * nand_release - [NAND Interface] Free resources held by the NAND device
2645 * @mtd: MTD device structure
2647 void nand_release (struct mtd_info *mtd)
2649 struct nand_chip *this = mtd->priv;
2651 #ifdef CONFIG_MTD_PARTITIONS
2652 /* Deregister partitions */
2653 del_mtd_partitions (mtd);
2655 /* Deregister the device */
2656 /* XXX U-BOOT XXX */
2658 del_mtd_device (mtd);
2660 /* Free bad block table memory, if allocated */
2663 /* Buffer allocated by nand_scan ? */
2664 if (this->options & NAND_OOBBUF_ALLOC)
2665 kfree (this->oob_buf);
2666 /* Buffer allocated by nand_scan ? */
2667 if (this->options & NAND_DATABUF_ALLOC)
2668 kfree (this->data_buf);
2672 #endif /* CONFIG_NEW_NAND_CODE */