5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
9 * Additional technical information is available on
10 * http://www.linux-mtd.infradead.org/tech/nand.html
12 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
13 * 2002 Thomas Gleixner (tglx@linutronix.de)
15 * 02-08-2004 tglx: support for strange chips, which cannot auto increment
16 * pages on read / read_oob
18 * 03-17-2004 tglx: Check ready before auto increment check. Simon Bayes
19 * pointed this out, as he marked an auto increment capable chip
20 * as NOAUTOINCR in the board driver.
21 * Make reads over block boundaries work too
23 * 04-14-2004 tglx: first working version for 2k page size chips
25 * 05-19-2004 tglx: Basic support for Renesas AG-AND chips
27 * 09-24-2004 tglx: add support for hardware controllers (e.g. ECC) shared
28 * among multiple independend devices. Suggestions and initial patch
29 * from Ben Dooks <ben-mtd@fluff.org>
32 * David Woodhouse for adding multichip support
34 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
35 * rework for 2K page size chips
38 * Enable cached programming for 2k page size chips
39 * Check, if mtd->ecctype should be set to MTD_ECC_HW
40 * if we have HW ecc support.
41 * The AG-AND chips have nice features for speed improvement,
42 * which are not supported yet. Read / program 4 pages in one go.
44 * $Id: nand_base.c,v 1.126 2004/12/13 11:22:25 lavinen Exp $
46 * This program is free software; you can redistribute it and/or modify
47 * it under the terms of the GNU General Public License version 2 as
48 * published by the Free Software Foundation.
54 #include <linux/delay.h>
55 #include <linux/errno.h>
56 #include <linux/sched.h>
57 #include <linux/slab.h>
58 #include <linux/types.h>
59 #include <linux/mtd/mtd.h>
60 #include <linux/mtd/nand.h>
61 #include <linux/mtd/nand_ecc.h>
62 #include <linux/mtd/compatmac.h>
63 #include <linux/interrupt.h>
64 #include <linux/bitops.h>
67 #ifdef CONFIG_MTD_PARTITIONS
68 #include <linux/mtd/partitions.h>
75 #if (CONFIG_COMMANDS & CFG_CMD_NAND)
79 #include <linux/mtd/compat.h>
80 #include <linux/mtd/mtd.h>
81 #include <linux/mtd/nand.h>
82 #include <linux/mtd/nand_ecc.h>
85 #include <asm/errno.h>
87 #ifdef CONFIG_JFFS2_NAND
88 #include <jffs2/jffs2.h>
93 /* Define default oob placement schemes for large and small page devices */
94 static struct nand_oobinfo nand_oob_8 = {
95 .useecc = MTD_NANDECC_AUTOPLACE,
98 .oobfree = { {3, 2}, {6, 2} }
101 static struct nand_oobinfo nand_oob_16 = {
102 .useecc = MTD_NANDECC_AUTOPLACE,
104 .eccpos = {0, 1, 2, 3, 6, 7},
105 .oobfree = { {8, 8} }
108 static struct nand_oobinfo nand_oob_64 = {
109 .useecc = MTD_NANDECC_AUTOPLACE,
112 40, 41, 42, 43, 44, 45, 46, 47,
113 48, 49, 50, 51, 52, 53, 54, 55,
114 56, 57, 58, 59, 60, 61, 62, 63},
115 .oobfree = { {2, 38} }
118 /* This is used for padding purposes in nand_write_oob */
119 static u_char ffchars[] = {
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,
127 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
131 * NAND low-level MTD interface functions
133 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
134 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
135 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
137 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
138 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
139 size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
140 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
141 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf);
142 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
143 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
144 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char *buf);
147 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
148 unsigned long count, loff_t to, size_t * retlen);
149 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs,
150 unsigned long count, loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
152 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
153 static void nand_sync (struct mtd_info *mtd);
155 /* Some internal functions */
156 static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf,
157 struct nand_oobinfo *oobsel, int mode);
158 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
159 static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
160 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode);
162 #define nand_verify_pages(...) (0)
165 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state);
168 * nand_release_device - [GENERIC] release chip
169 * @mtd: MTD device structure
171 * Deselect, release chip lock and wake up anyone waiting on the device
175 static void nand_release_device (struct mtd_info *mtd)
177 struct nand_chip *this = mtd->priv;
179 /* De-select the NAND device */
180 this->select_chip(mtd, -1);
181 /* Do we have a hardware controller ? */
182 if (this->controller) {
183 spin_lock(&this->controller->lock);
184 this->controller->active = NULL;
185 spin_unlock(&this->controller->lock);
187 /* Release the chip */
188 spin_lock (&this->chip_lock);
189 this->state = FL_READY;
191 spin_unlock (&this->chip_lock);
194 #define nand_release_device(mtd) do {} while(0)
198 * nand_read_byte - [DEFAULT] read one byte from the chip
199 * @mtd: MTD device structure
201 * Default read function for 8bit buswith
203 static u_char nand_read_byte(struct mtd_info *mtd)
205 struct nand_chip *this = mtd->priv;
206 return readb(this->IO_ADDR_R);
210 * nand_write_byte - [DEFAULT] write one byte to the chip
211 * @mtd: MTD device structure
212 * @byte: pointer to data byte to write
214 * Default write function for 8it buswith
216 static void nand_write_byte(struct mtd_info *mtd, u_char byte)
218 struct nand_chip *this = mtd->priv;
219 writeb(byte, this->IO_ADDR_W);
223 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
224 * @mtd: MTD device structure
226 * Default read function for 16bit buswith with
227 * endianess conversion
229 static u_char nand_read_byte16(struct mtd_info *mtd)
231 struct nand_chip *this = mtd->priv;
232 return (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
236 * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
237 * @mtd: MTD device structure
238 * @byte: pointer to data byte to write
240 * Default write function for 16bit buswith with
241 * endianess conversion
243 static void nand_write_byte16(struct mtd_info *mtd, u_char byte)
245 struct nand_chip *this = mtd->priv;
246 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
250 * nand_read_word - [DEFAULT] read one word from the chip
251 * @mtd: MTD device structure
253 * Default read function for 16bit buswith without
254 * endianess conversion
256 static u16 nand_read_word(struct mtd_info *mtd)
258 struct nand_chip *this = mtd->priv;
259 return readw(this->IO_ADDR_R);
263 * nand_write_word - [DEFAULT] write one word to the chip
264 * @mtd: MTD device structure
265 * @word: data word to write
267 * Default write function for 16bit buswith without
268 * endianess conversion
270 static void nand_write_word(struct mtd_info *mtd, u16 word)
272 struct nand_chip *this = mtd->priv;
273 writew(word, this->IO_ADDR_W);
277 * nand_select_chip - [DEFAULT] control CE line
278 * @mtd: MTD device structure
279 * @chip: chipnumber to select, -1 for deselect
281 * Default select function for 1 chip devices.
283 static void nand_select_chip(struct mtd_info *mtd, int chip)
285 struct nand_chip *this = mtd->priv;
288 this->hwcontrol(mtd, NAND_CTL_CLRNCE);
291 this->hwcontrol(mtd, NAND_CTL_SETNCE);
300 * nand_write_buf - [DEFAULT] write buffer to chip
301 * @mtd: MTD device structure
303 * @len: number of bytes to write
305 * Default write function for 8bit buswith
307 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
310 struct nand_chip *this = mtd->priv;
312 for (i=0; i<len; i++)
313 writeb(buf[i], this->IO_ADDR_W);
317 * nand_read_buf - [DEFAULT] read chip data into buffer
318 * @mtd: MTD device structure
319 * @buf: buffer to store date
320 * @len: number of bytes to read
322 * Default read function for 8bit buswith
324 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
327 struct nand_chip *this = mtd->priv;
329 for (i=0; i<len; i++)
330 buf[i] = readb(this->IO_ADDR_R);
334 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
335 * @mtd: MTD device structure
336 * @buf: buffer containing the data to compare
337 * @len: number of bytes to compare
339 * Default verify function for 8bit buswith
341 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
344 struct nand_chip *this = mtd->priv;
346 for (i=0; i<len; i++)
347 if (buf[i] != readb(this->IO_ADDR_R))
354 * nand_write_buf16 - [DEFAULT] write buffer to chip
355 * @mtd: MTD device structure
357 * @len: number of bytes to write
359 * Default write function for 16bit buswith
361 static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
364 struct nand_chip *this = mtd->priv;
365 u16 *p = (u16 *) buf;
368 for (i=0; i<len; i++)
369 writew(p[i], this->IO_ADDR_W);
374 * nand_read_buf16 - [DEFAULT] read chip data into buffer
375 * @mtd: MTD device structure
376 * @buf: buffer to store date
377 * @len: number of bytes to read
379 * Default read function for 16bit buswith
381 static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
384 struct nand_chip *this = mtd->priv;
385 u16 *p = (u16 *) buf;
388 for (i=0; i<len; i++)
389 p[i] = readw(this->IO_ADDR_R);
393 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
394 * @mtd: MTD device structure
395 * @buf: buffer containing the data to compare
396 * @len: number of bytes to compare
398 * Default verify function for 16bit buswith
400 static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
403 struct nand_chip *this = mtd->priv;
404 u16 *p = (u16 *) buf;
407 for (i=0; i<len; i++)
408 if (p[i] != readw(this->IO_ADDR_R))
415 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
416 * @mtd: MTD device structure
417 * @ofs: offset from device start
418 * @getchip: 0, if the chip is already selected
420 * Check, if the block is bad.
422 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
424 int page, chipnr, res = 0;
425 struct nand_chip *this = mtd->priv;
429 page = (int)(ofs >> this->page_shift);
430 chipnr = (int)(ofs >> this->chip_shift);
432 /* Grab the lock and see if the device is available */
433 nand_get_device (this, mtd, FL_READING);
435 /* Select the NAND device */
436 this->select_chip(mtd, chipnr);
440 if (this->options & NAND_BUSWIDTH_16) {
441 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page & this->pagemask);
442 bad = cpu_to_le16(this->read_word(mtd));
443 if (this->badblockpos & 0x1)
445 if ((bad & 0xFF) != 0xff)
448 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page & this->pagemask);
449 if (this->read_byte(mtd) != 0xff)
454 /* Deselect and wake up anyone waiting on the device */
455 nand_release_device(mtd);
462 * nand_default_block_markbad - [DEFAULT] mark a block bad
463 * @mtd: MTD device structure
464 * @ofs: offset from device start
466 * This is the default implementation, which can be overridden by
467 * a hardware specific driver.
469 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
471 struct nand_chip *this = mtd->priv;
472 u_char buf[2] = {0, 0};
476 /* Get block number */
477 block = ((int) ofs) >> this->bbt_erase_shift;
478 this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
480 /* Do we have a flash based bad block table ? */
481 if (this->options & NAND_USE_FLASH_BBT)
482 return nand_update_bbt (mtd, ofs);
484 /* We write two bytes, so we dont have to mess with 16 bit access */
485 ofs += mtd->oobsize + (this->badblockpos & ~0x01);
486 return nand_write_oob (mtd, ofs , 2, &retlen, buf);
490 * nand_check_wp - [GENERIC] check if the chip is write protected
491 * @mtd: MTD device structure
492 * Check, if the device is write protected
494 * The function expects, that the device is already selected
496 static int nand_check_wp (struct mtd_info *mtd)
498 struct nand_chip *this = mtd->priv;
499 /* Check the WP bit */
500 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
501 return (this->read_byte(mtd) & 0x80) ? 0 : 1;
505 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
506 * @mtd: MTD device structure
507 * @ofs: offset from device start
508 * @getchip: 0, if the chip is already selected
509 * @allowbbt: 1, if its allowed to access the bbt area
511 * Check, if the block is bad. Either by reading the bad block table or
512 * calling of the scan function.
514 static int nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
516 struct nand_chip *this = mtd->priv;
519 return this->block_bad(mtd, ofs, getchip);
521 /* Return info from the table */
522 return nand_isbad_bbt (mtd, ofs, allowbbt);
526 * nand_command - [DEFAULT] Send command to NAND device
527 * @mtd: MTD device structure
528 * @command: the command to be sent
529 * @column: the column address for this command, -1 if none
530 * @page_addr: the page address for this command, -1 if none
532 * Send command to NAND device. This function is used for small page
533 * devices (256/512 Bytes per page)
535 static void nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
537 register struct nand_chip *this = mtd->priv;
539 /* Begin command latch cycle */
540 this->hwcontrol(mtd, NAND_CTL_SETCLE);
542 * Write out the command to the device.
544 if (command == NAND_CMD_SEQIN) {
547 if (column >= mtd->oobblock) {
549 column -= mtd->oobblock;
550 readcmd = NAND_CMD_READOOB;
551 } else if (column < 256) {
552 /* First 256 bytes --> READ0 */
553 readcmd = NAND_CMD_READ0;
556 readcmd = NAND_CMD_READ1;
558 this->write_byte(mtd, readcmd);
560 this->write_byte(mtd, command);
562 /* Set ALE and clear CLE to start address cycle */
563 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
565 if (column != -1 || page_addr != -1) {
566 this->hwcontrol(mtd, NAND_CTL_SETALE);
568 /* Serially input address */
570 /* Adjust columns for 16 bit buswidth */
571 if (this->options & NAND_BUSWIDTH_16)
573 this->write_byte(mtd, column);
575 if (page_addr != -1) {
576 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
577 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
578 /* One more address cycle for devices > 32MiB */
579 if (this->chipsize > (32 << 20))
580 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));
582 /* Latch in address */
583 this->hwcontrol(mtd, NAND_CTL_CLRALE);
587 * program and erase have their own busy handlers
588 * status and sequential in needs no delay
592 case NAND_CMD_PAGEPROG:
593 case NAND_CMD_ERASE1:
594 case NAND_CMD_ERASE2:
596 case NAND_CMD_STATUS:
602 udelay(this->chip_delay);
603 this->hwcontrol(mtd, NAND_CTL_SETCLE);
604 this->write_byte(mtd, NAND_CMD_STATUS);
605 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
606 while ( !(this->read_byte(mtd) & 0x40));
609 /* This applies to read commands */
612 * If we don't have access to the busy pin, we apply the given
615 if (!this->dev_ready) {
616 udelay (this->chip_delay);
621 /* Apply this short delay always to ensure that we do wait tWB in
622 * any case on any machine. */
624 /* wait until command is processed */
625 while (!this->dev_ready(mtd));
629 * nand_command_lp - [DEFAULT] Send command to NAND large page device
630 * @mtd: MTD device structure
631 * @command: the command to be sent
632 * @column: the column address for this command, -1 if none
633 * @page_addr: the page address for this command, -1 if none
635 * Send command to NAND device. This is the version for the new large page devices
636 * We dont have the seperate regions as we have in the small page devices.
637 * We must emulate NAND_CMD_READOOB to keep the code compatible.
640 static void nand_command_lp (struct mtd_info *mtd, unsigned command, int column, int page_addr)
642 register struct nand_chip *this = mtd->priv;
644 /* Emulate NAND_CMD_READOOB */
645 if (command == NAND_CMD_READOOB) {
646 column += mtd->oobblock;
647 command = NAND_CMD_READ0;
651 /* Begin command latch cycle */
652 this->hwcontrol(mtd, NAND_CTL_SETCLE);
653 /* Write out the command to the device. */
654 this->write_byte(mtd, command);
655 /* End command latch cycle */
656 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
658 if (column != -1 || page_addr != -1) {
659 this->hwcontrol(mtd, NAND_CTL_SETALE);
661 /* Serially input address */
663 /* Adjust columns for 16 bit buswidth */
664 if (this->options & NAND_BUSWIDTH_16)
666 this->write_byte(mtd, column & 0xff);
667 this->write_byte(mtd, column >> 8);
669 if (page_addr != -1) {
670 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
671 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
672 /* One more address cycle for devices > 128MiB */
673 if (this->chipsize > (128 << 20))
674 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0xff));
676 /* Latch in address */
677 this->hwcontrol(mtd, NAND_CTL_CLRALE);
681 * program and erase have their own busy handlers
682 * status and sequential in needs no delay
686 case NAND_CMD_CACHEDPROG:
687 case NAND_CMD_PAGEPROG:
688 case NAND_CMD_ERASE1:
689 case NAND_CMD_ERASE2:
691 case NAND_CMD_STATUS:
698 udelay(this->chip_delay);
699 this->hwcontrol(mtd, NAND_CTL_SETCLE);
700 this->write_byte(mtd, NAND_CMD_STATUS);
701 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
702 while ( !(this->read_byte(mtd) & 0x40));
706 /* Begin command latch cycle */
707 this->hwcontrol(mtd, NAND_CTL_SETCLE);
708 /* Write out the start read command */
709 this->write_byte(mtd, NAND_CMD_READSTART);
710 /* End command latch cycle */
711 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
712 /* Fall through into ready check */
714 /* This applies to read commands */
717 * If we don't have access to the busy pin, we apply the given
720 if (!this->dev_ready) {
721 udelay (this->chip_delay);
726 /* Apply this short delay always to ensure that we do wait tWB in
727 * any case on any machine. */
729 /* wait until command is processed */
730 while (!this->dev_ready(mtd));
734 * nand_get_device - [GENERIC] Get chip for selected access
735 * @this: the nand chip descriptor
736 * @mtd: MTD device structure
737 * @new_state: the state which is requested
739 * Get the device and lock it for exclusive access
743 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
745 struct nand_chip *active = this;
747 DECLARE_WAITQUEUE (wait, current);
750 * Grab the lock and see if the device is available
753 /* Hardware controller shared among independend devices */
754 if (this->controller) {
755 spin_lock (&this->controller->lock);
756 if (this->controller->active)
757 active = this->controller->active;
759 this->controller->active = this;
760 spin_unlock (&this->controller->lock);
763 if (active == this) {
764 spin_lock (&this->chip_lock);
765 if (this->state == FL_READY) {
766 this->state = new_state;
767 spin_unlock (&this->chip_lock);
771 set_current_state (TASK_UNINTERRUPTIBLE);
772 add_wait_queue (&active->wq, &wait);
773 spin_unlock (&active->chip_lock);
775 remove_wait_queue (&active->wq, &wait);
779 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state) {}
783 * nand_wait - [DEFAULT] wait until the command is done
784 * @mtd: MTD device structure
785 * @this: NAND chip structure
786 * @state: state to select the max. timeout value
788 * Wait for command done. This applies to erase and program only
789 * Erase can take up to 400ms and program up to 20ms according to
790 * general NAND and SmartMedia specs
795 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
797 unsigned long timeo = jiffies;
800 if (state == FL_ERASING)
801 timeo += (HZ * 400) / 1000;
803 timeo += (HZ * 20) / 1000;
805 /* Apply this short delay always to ensure that we do wait tWB in
806 * any case on any machine. */
809 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
810 this->cmdfunc (mtd, NAND_CMD_STATUS_MULTI, -1, -1);
812 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
814 while (time_before(jiffies, timeo)) {
815 /* Check, if we were interrupted */
816 if (this->state != state)
819 if (this->dev_ready) {
820 if (this->dev_ready(mtd))
823 if (this->read_byte(mtd) & NAND_STATUS_READY)
828 status = (int) this->read_byte(mtd);
834 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
842 * nand_write_page - [GENERIC] write one page
843 * @mtd: MTD device structure
844 * @this: NAND chip structure
845 * @page: startpage inside the chip, must be called with (page & this->pagemask)
846 * @oob_buf: out of band data buffer
847 * @oobsel: out of band selecttion structre
848 * @cached: 1 = enable cached programming if supported by chip
850 * Nand_page_program function is used for write and writev !
851 * This function will always program a full page of data
852 * If you call it with a non page aligned buffer, you're lost :)
854 * Cached programming is not supported yet.
856 static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page,
857 u_char *oob_buf, struct nand_oobinfo *oobsel, int cached)
861 int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
862 int *oob_config = oobsel->eccpos;
863 int datidx = 0, eccidx = 0, eccsteps = this->eccsteps;
866 /* FIXME: Enable cached programming */
869 /* Send command to begin auto page programming */
870 this->cmdfunc (mtd, NAND_CMD_SEQIN, 0x00, page);
872 /* Write out complete page of data, take care of eccmode */
874 /* No ecc, write all */
876 printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
877 this->write_buf(mtd, this->data_poi, mtd->oobblock);
880 /* Software ecc 3/256, write all */
882 for (; eccsteps; eccsteps--) {
883 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
884 for (i = 0; i < 3; i++, eccidx++)
885 oob_buf[oob_config[eccidx]] = ecc_code[i];
886 datidx += this->eccsize;
888 this->write_buf(mtd, this->data_poi, mtd->oobblock);
891 eccbytes = this->eccbytes;
892 for (; eccsteps; eccsteps--) {
893 /* enable hardware ecc logic for write */
894 this->enable_hwecc(mtd, NAND_ECC_WRITE);
895 this->write_buf(mtd, &this->data_poi[datidx], this->eccsize);
896 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
897 for (i = 0; i < eccbytes; i++, eccidx++)
898 oob_buf[oob_config[eccidx]] = ecc_code[i];
899 /* If the hardware ecc provides syndromes then
900 * the ecc code must be written immidiately after
901 * the data bytes (words) */
902 if (this->options & NAND_HWECC_SYNDROME)
903 this->write_buf(mtd, ecc_code, eccbytes);
904 datidx += this->eccsize;
909 /* Write out OOB data */
910 if (this->options & NAND_HWECC_SYNDROME)
911 this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
913 this->write_buf(mtd, oob_buf, mtd->oobsize);
915 /* Send command to actually program the data */
916 this->cmdfunc (mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
919 /* call wait ready function */
920 status = this->waitfunc (mtd, this, FL_WRITING);
921 /* See if device thinks it succeeded */
923 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
927 /* FIXME: Implement cached programming ! */
928 /* wait until cache is ready*/
929 // status = this->waitfunc (mtd, this, FL_CACHEDRPG);
934 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
936 * nand_verify_pages - [GENERIC] verify the chip contents after a write
937 * @mtd: MTD device structure
938 * @this: NAND chip structure
939 * @page: startpage inside the chip, must be called with (page & this->pagemask)
940 * @numpages: number of pages to verify
941 * @oob_buf: out of band data buffer
942 * @oobsel: out of band selecttion structre
943 * @chipnr: number of the current chip
944 * @oobmode: 1 = full buffer verify, 0 = ecc only
946 * The NAND device assumes that it is always writing to a cleanly erased page.
947 * Hence, it performs its internal write verification only on bits that
948 * transitioned from 1 to 0. The device does NOT verify the whole page on a
949 * byte by byte basis. It is possible that the page was not completely erased
950 * or the page is becoming unusable due to wear. The read with ECC would catch
951 * the error later when the ECC page check fails, but we would rather catch
952 * it early in the page write stage. Better to write no data than invalid data.
954 static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
955 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
957 int i, j, datidx = 0, oobofs = 0, res = -EIO;
958 int eccsteps = this->eccsteps;
962 hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
964 /* Send command to read back the first page */
965 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page);
968 for (j = 0; j < eccsteps; j++) {
969 /* Loop through and verify the data */
970 if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
971 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
974 datidx += mtd->eccsize;
975 /* Have we a hw generator layout ? */
978 if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
979 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
982 oobofs += hweccbytes;
985 /* check, if we must compare all data or if we just have to
986 * compare the ecc bytes
989 if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
990 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
994 /* Read always, else autoincrement fails */
995 this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
997 if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
998 int ecccnt = oobsel->eccbytes;
1000 for (i = 0; i < ecccnt; i++) {
1001 int idx = oobsel->eccpos[i];
1002 if (oobdata[idx] != oob_buf[oobofs + idx] ) {
1003 DEBUG (MTD_DEBUG_LEVEL0,
1004 "%s: Failed ECC write "
1005 "verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__, page, i);
1011 oobofs += mtd->oobsize - hweccbytes * eccsteps;
1015 /* Apply delay or wait for ready/busy pin
1016 * Do this before the AUTOINCR check, so no problems
1017 * arise if a chip which does auto increment
1018 * is marked as NOAUTOINCR by the board driver.
1019 * Do this also before returning, so the chip is
1020 * ready for the next command.
1022 if (!this->dev_ready)
1023 udelay (this->chip_delay);
1025 while (!this->dev_ready(mtd));
1027 /* All done, return happy */
1032 /* Check, if the chip supports auto page increment */
1033 if (!NAND_CANAUTOINCR(this))
1034 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1037 * Terminate the read command. We come here in case of an error
1038 * So we must issue a reset command.
1041 this->cmdfunc (mtd, NAND_CMD_RESET, -1, -1);
1047 * nand_read - [MTD Interface] MTD compability function for nand_read_ecc
1048 * @mtd: MTD device structure
1049 * @from: offset to read from
1050 * @len: number of bytes to read
1051 * @retlen: pointer to variable to store the number of read bytes
1052 * @buf: the databuffer to put data
1054 * This function simply calls nand_read_ecc with oob buffer and oobsel = NULL
1056 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1058 return nand_read_ecc (mtd, from, len, retlen, buf, NULL, NULL);
1063 * nand_read_ecc - [MTD Interface] Read data with ECC
1064 * @mtd: MTD device structure
1065 * @from: offset to read from
1066 * @len: number of bytes to read
1067 * @retlen: pointer to variable to store the number of read bytes
1068 * @buf: the databuffer to put data
1069 * @oob_buf: filesystem supplied oob data buffer
1070 * @oobsel: oob selection structure
1072 * NAND read with ECC
1074 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
1075 size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel)
1077 int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
1078 int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
1079 struct nand_chip *this = mtd->priv;
1080 u_char *data_poi, *oob_data = oob_buf;
1081 u_char ecc_calc[32];
1082 u_char ecc_code[32];
1083 int eccmode, eccsteps;
1084 int *oob_config, datidx;
1085 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1091 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1093 /* Do not allow reads past end of device */
1094 if ((from + len) > mtd->size) {
1095 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
1100 /* Grab the lock and see if the device is available */
1101 nand_get_device (this, mtd ,FL_READING);
1103 /* use userspace supplied oobinfo, if zero */
1105 oobsel = &mtd->oobinfo;
1107 /* Autoplace of oob data ? Use the default placement scheme */
1108 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1109 oobsel = this->autooob;
1111 eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
1112 oob_config = oobsel->eccpos;
1114 /* Select the NAND device */
1115 chipnr = (int)(from >> this->chip_shift);
1116 this->select_chip(mtd, chipnr);
1118 /* First we calculate the starting page */
1119 realpage = (int) (from >> this->page_shift);
1120 page = realpage & this->pagemask;
1122 /* Get raw starting column */
1123 col = from & (mtd->oobblock - 1);
1125 end = mtd->oobblock;
1126 ecc = this->eccsize;
1127 eccbytes = this->eccbytes;
1129 if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
1132 oobreadlen = mtd->oobsize;
1133 if (this->options & NAND_HWECC_SYNDROME)
1134 oobreadlen -= oobsel->eccbytes;
1136 /* Loop until all data read */
1137 while (read < len) {
1139 int aligned = (!col && (len - read) >= end);
1141 * If the read is not page aligned, we have to read into data buffer
1142 * due to ecc, else we read into return buffer direct
1145 data_poi = &buf[read];
1147 data_poi = this->data_buf;
1149 /* Check, if we have this page in the buffer
1151 * FIXME: Make it work when we must provide oob data too,
1152 * check the usage of data_buf oob field
1154 if (realpage == this->pagebuf && !oob_buf) {
1155 /* aligned read ? */
1157 memcpy (data_poi, this->data_buf, end);
1161 /* Check, if we must send the read command */
1163 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1167 /* get oob area, if we have no oob buffer from fs-driver */
1168 if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE ||
1169 oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1170 oob_data = &this->data_buf[end];
1172 eccsteps = this->eccsteps;
1175 case NAND_ECC_NONE: { /* No ECC, Read in a page */
1176 /* XXX U-BOOT XXX */
1178 static unsigned long lastwhinge = 0;
1179 if ((lastwhinge / HZ) != (jiffies / HZ)) {
1180 printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended\n");
1181 lastwhinge = jiffies;
1184 puts("Reading data from NAND FLASH without ECC is not recommended\n");
1186 this->read_buf(mtd, data_poi, end);
1190 case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
1191 this->read_buf(mtd, data_poi, end);
1192 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc)
1193 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1197 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) {
1198 this->enable_hwecc(mtd, NAND_ECC_READ);
1199 this->read_buf(mtd, &data_poi[datidx], ecc);
1201 /* HW ecc with syndrome calculation must read the
1202 * syndrome from flash immidiately after the data */
1204 /* Some hw ecc generators need to know when the
1205 * syndrome is read from flash */
1206 this->enable_hwecc(mtd, NAND_ECC_READSYN);
1207 this->read_buf(mtd, &oob_data[i], eccbytes);
1208 /* We calc error correction directly, it checks the hw
1209 * generator for an error, reads back the syndrome and
1210 * does the error correction on the fly */
1211 if (this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]) == -1) {
1212 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: "
1213 "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
1217 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1224 this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
1226 /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
1230 /* Pick the ECC bytes out of the oob data */
1231 for (j = 0; j < oobsel->eccbytes; j++)
1232 ecc_code[j] = oob_data[oob_config[j]];
1234 /* correct data, if neccecary */
1235 for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {
1236 ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
1238 /* Get next chunk of ecc bytes */
1241 /* Check, if we have a fs supplied oob-buffer,
1242 * This is the legacy mode. Used by YAFFS1
1243 * Should go away some day
1245 if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
1246 int *p = (int *)(&oob_data[mtd->oobsize]);
1250 if (ecc_status == -1) {
1251 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
1257 /* check, if we have a fs supplied oob-buffer */
1259 /* without autoplace. Legacy mode used by YAFFS1 */
1260 switch(oobsel->useecc) {
1261 case MTD_NANDECC_AUTOPLACE:
1262 case MTD_NANDECC_AUTOPL_USR:
1263 /* Walk through the autoplace chunks */
1264 for (i = 0, j = 0; j < mtd->oobavail; i++) {
1265 int from = oobsel->oobfree[i][0];
1266 int num = oobsel->oobfree[i][1];
1267 memcpy(&oob_buf[oob], &oob_data[from], num);
1270 oob += mtd->oobavail;
1272 case MTD_NANDECC_PLACE:
1273 /* YAFFS1 legacy mode */
1274 oob_data += this->eccsteps * sizeof (int);
1276 oob_data += mtd->oobsize;
1280 /* Partial page read, transfer data into fs buffer */
1282 for (j = col; j < end && read < len; j++)
1283 buf[read++] = data_poi[j];
1284 this->pagebuf = realpage;
1286 read += mtd->oobblock;
1288 /* Apply delay or wait for ready/busy pin
1289 * Do this before the AUTOINCR check, so no problems
1290 * arise if a chip which does auto increment
1291 * is marked as NOAUTOINCR by the board driver.
1293 if (!this->dev_ready)
1294 udelay (this->chip_delay);
1296 while (!this->dev_ready(mtd));
1301 /* For subsequent reads align to page boundary. */
1303 /* Increment page address */
1306 page = realpage & this->pagemask;
1307 /* Check, if we cross a chip boundary */
1310 this->select_chip(mtd, -1);
1311 this->select_chip(mtd, chipnr);
1313 /* Check, if the chip supports auto page increment
1314 * or if we have hit a block boundary.
1316 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1320 /* Deselect and wake up anyone waiting on the device */
1321 nand_release_device(mtd);
1324 * Return success, if no ECC failures, else -EBADMSG
1325 * fs driver will take care of that, because
1326 * retlen == desired len and result == -EBADMSG
1329 return ecc_failed ? -EBADMSG : 0;
1333 * nand_read_oob - [MTD Interface] NAND read out-of-band
1334 * @mtd: MTD device structure
1335 * @from: offset to read from
1336 * @len: number of bytes to read
1337 * @retlen: pointer to variable to store the number of read bytes
1338 * @buf: the databuffer to put data
1340 * NAND read out-of-band data from the spare area
1342 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1344 int i, col, page, chipnr;
1345 struct nand_chip *this = mtd->priv;
1346 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1348 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1350 /* Shift to get page */
1351 page = (int)(from >> this->page_shift);
1352 chipnr = (int)(from >> this->chip_shift);
1354 /* Mask to get column */
1355 col = from & (mtd->oobsize - 1);
1357 /* Initialize return length value */
1360 /* Do not allow reads past end of device */
1361 if ((from + len) > mtd->size) {
1362 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n");
1367 /* Grab the lock and see if the device is available */
1368 nand_get_device (this, mtd , FL_READING);
1370 /* Select the NAND device */
1371 this->select_chip(mtd, chipnr);
1373 /* Send the read command */
1374 this->cmdfunc (mtd, NAND_CMD_READOOB, col, page & this->pagemask);
1376 * Read the data, if we read more than one page
1377 * oob data, let the device transfer the data !
1381 int thislen = mtd->oobsize - col;
1382 thislen = min_t(int, thislen, len);
1383 this->read_buf(mtd, &buf[i], thislen);
1386 /* Apply delay or wait for ready/busy pin
1387 * Do this before the AUTOINCR check, so no problems
1388 * arise if a chip which does auto increment
1389 * is marked as NOAUTOINCR by the board driver.
1391 if (!this->dev_ready)
1392 udelay (this->chip_delay);
1394 while (!this->dev_ready(mtd));
1401 /* Check, if we cross a chip boundary */
1402 if (!(page & this->pagemask)) {
1404 this->select_chip(mtd, -1);
1405 this->select_chip(mtd, chipnr);
1408 /* Check, if the chip supports auto page increment
1409 * or if we have hit a block boundary.
1411 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
1412 /* For subsequent page reads set offset to 0 */
1413 this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
1418 /* Deselect and wake up anyone waiting on the device */
1419 nand_release_device(mtd);
1427 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
1428 * @mtd: MTD device structure
1429 * @buf: temporary buffer
1430 * @from: offset to read from
1431 * @len: number of bytes to read
1432 * @ooblen: number of oob data bytes to read
1434 * Read raw data including oob into buffer
1436 int nand_read_raw (struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
1438 struct nand_chip *this = mtd->priv;
1439 int page = (int) (from >> this->page_shift);
1440 int chip = (int) (from >> this->chip_shift);
1443 int pagesize = mtd->oobblock + mtd->oobsize;
1444 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1446 /* Do not allow reads past end of device */
1447 if ((from + len) > mtd->size) {
1448 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n");
1452 /* Grab the lock and see if the device is available */
1453 nand_get_device (this, mtd , FL_READING);
1455 this->select_chip (mtd, chip);
1457 /* Add requested oob length */
1462 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page & this->pagemask);
1465 this->read_buf (mtd, &buf[cnt], pagesize);
1471 if (!this->dev_ready)
1472 udelay (this->chip_delay);
1474 while (!this->dev_ready(mtd));
1476 /* Check, if the chip supports auto page increment */
1477 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1481 /* Deselect and wake up anyone waiting on the device */
1482 nand_release_device(mtd);
1488 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
1489 * @mtd: MTD device structure
1490 * @fsbuf: buffer given by fs driver
1491 * @oobsel: out of band selection structre
1492 * @autoplace: 1 = place given buffer into the oob bytes
1493 * @numpages: number of pages to prepare
1496 * 1. Filesystem buffer available and autoplacement is off,
1497 * return filesystem buffer
1498 * 2. No filesystem buffer or autoplace is off, return internal
1500 * 3. Filesystem buffer is given and autoplace selected
1501 * put data from fs buffer into internal buffer and
1502 * retrun internal buffer
1504 * Note: The internal buffer is filled with 0xff. This must
1505 * be done only once, when no autoplacement happens
1506 * Autoplacement sets the buffer dirty flag, which
1507 * forces the 0xff fill before using the buffer again.
1510 static u_char * nand_prepare_oobbuf (struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel,
1511 int autoplace, int numpages)
1513 struct nand_chip *this = mtd->priv;
1516 /* Zero copy fs supplied buffer */
1517 if (fsbuf && !autoplace)
1520 /* Check, if the buffer must be filled with ff again */
1521 if (this->oobdirty) {
1522 memset (this->oob_buf, 0xff,
1523 mtd->oobsize << (this->phys_erase_shift - this->page_shift));
1527 /* If we have no autoplacement or no fs buffer use the internal one */
1528 if (!autoplace || !fsbuf)
1529 return this->oob_buf;
1531 /* Walk through the pages and place the data */
1534 while (numpages--) {
1535 for (i = 0, len = 0; len < mtd->oobavail; i++) {
1536 int to = ofs + oobsel->oobfree[i][0];
1537 int num = oobsel->oobfree[i][1];
1538 memcpy (&this->oob_buf[to], fsbuf, num);
1542 ofs += mtd->oobavail;
1544 return this->oob_buf;
1547 #define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
1550 * nand_write - [MTD Interface] compability function for nand_write_ecc
1551 * @mtd: MTD device structure
1552 * @to: offset to write to
1553 * @len: number of bytes to write
1554 * @retlen: pointer to variable to store the number of written bytes
1555 * @buf: the data to write
1557 * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
1560 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1562 return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));
1566 * nand_write_ecc - [MTD Interface] NAND write with ECC
1567 * @mtd: MTD device structure
1568 * @to: offset to write to
1569 * @len: number of bytes to write
1570 * @retlen: pointer to variable to store the number of written bytes
1571 * @buf: the data to write
1572 * @eccbuf: filesystem supplied oob data buffer
1573 * @oobsel: oob selection structure
1575 * NAND write with ECC
1577 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
1578 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
1580 int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
1581 int autoplace = 0, numpages, totalpages;
1582 struct nand_chip *this = mtd->priv;
1583 u_char *oobbuf, *bufstart;
1584 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1586 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1588 /* Initialize retlen, in case of early exit */
1591 /* Do not allow write past end of device */
1592 if ((to + len) > mtd->size) {
1593 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n");
1597 /* reject writes, which are not page aligned */
1598 if (NOTALIGNED (to) || NOTALIGNED(len)) {
1599 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1603 /* Grab the lock and see if the device is available */
1604 nand_get_device (this, mtd, FL_WRITING);
1606 /* Calculate chipnr */
1607 chipnr = (int)(to >> this->chip_shift);
1608 /* Select the NAND device */
1609 this->select_chip(mtd, chipnr);
1611 /* Check, if it is write protected */
1612 if (nand_check_wp(mtd))
1615 /* if oobsel is NULL, use chip defaults */
1617 oobsel = &mtd->oobinfo;
1619 /* Autoplace of oob data ? Use the default placement scheme */
1620 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1621 oobsel = this->autooob;
1624 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1627 /* Setup variables and oob buffer */
1628 totalpages = len >> this->page_shift;
1629 page = (int) (to >> this->page_shift);
1630 /* Invalidate the page cache, if we write to the cached page */
1631 if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
1634 /* Set it relative to chip */
1635 page &= this->pagemask;
1637 /* Calc number of pages we can write in one go */
1638 numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages);
1639 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages);
1640 bufstart = (u_char *)buf;
1642 /* Loop until all data is written */
1643 while (written < len) {
1645 this->data_poi = (u_char*) &buf[written];
1646 /* Write one page. If this is the last page to write
1647 * or the last page in this block, then use the
1648 * real pageprogram command, else select cached programming
1649 * if supported by the chip.
1651 ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
1653 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret);
1657 oob += mtd->oobsize;
1658 /* Update written bytes count */
1659 written += mtd->oobblock;
1663 /* Increment page address */
1666 /* Have we hit a block boundary ? Then we have to verify and
1667 * if verify is ok, we have to setup the oob buffer for
1670 if (!(page & (ppblock - 1))){
1672 this->data_poi = bufstart;
1673 ret = nand_verify_pages (mtd, this, startpage,
1675 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1677 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1682 ofs = autoplace ? mtd->oobavail : mtd->oobsize;
1684 eccbuf += (page - startpage) * ofs;
1685 totalpages -= page - startpage;
1686 numpages = min (totalpages, ppblock);
1687 page &= this->pagemask;
1689 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel,
1690 autoplace, numpages);
1691 /* Check, if we cross a chip boundary */
1694 this->select_chip(mtd, -1);
1695 this->select_chip(mtd, chipnr);
1699 /* Verify the remaining pages */
1701 this->data_poi = bufstart;
1702 ret = nand_verify_pages (mtd, this, startpage, totalpages,
1703 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1707 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1710 /* Deselect and wake up anyone waiting on the device */
1711 nand_release_device(mtd);
1718 * nand_write_oob - [MTD Interface] NAND write out-of-band
1719 * @mtd: MTD device structure
1720 * @to: offset to write to
1721 * @len: number of bytes to write
1722 * @retlen: pointer to variable to store the number of written bytes
1723 * @buf: the data to write
1725 * NAND write out-of-band
1727 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1729 int column, page, status, ret = -EIO, chipnr;
1730 struct nand_chip *this = mtd->priv;
1732 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1734 /* Shift to get page */
1735 page = (int) (to >> this->page_shift);
1736 chipnr = (int) (to >> this->chip_shift);
1738 /* Mask to get column */
1739 column = to & (mtd->oobsize - 1);
1741 /* Initialize return length value */
1744 /* Do not allow write past end of page */
1745 if ((column + len) > mtd->oobsize) {
1746 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
1750 /* Grab the lock and see if the device is available */
1751 nand_get_device (this, mtd, FL_WRITING);
1753 /* Select the NAND device */
1754 this->select_chip(mtd, chipnr);
1756 /* Reset the chip. Some chips (like the Toshiba TC5832DC found
1757 in one of my DiskOnChip 2000 test units) will clear the whole
1758 data page too if we don't do this. I have no clue why, but
1759 I seem to have 'fixed' it in the doc2000 driver in
1760 August 1999. dwmw2. */
1761 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1763 /* Check, if it is write protected */
1764 if (nand_check_wp(mtd))
1767 /* Invalidate the page cache, if we write to the cached page */
1768 if (page == this->pagebuf)
1771 if (NAND_MUST_PAD(this)) {
1772 /* Write out desired data */
1773 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
1774 /* prepad 0xff for partial programming */
1775 this->write_buf(mtd, ffchars, column);
1777 this->write_buf(mtd, buf, len);
1778 /* postpad 0xff for partial programming */
1779 this->write_buf(mtd, ffchars, mtd->oobsize - (len+column));
1781 /* Write out desired data */
1782 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
1784 this->write_buf(mtd, buf, len);
1786 /* Send command to program the OOB data */
1787 this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1);
1789 status = this->waitfunc (mtd, this, FL_WRITING);
1791 /* See if device thinks it succeeded */
1792 if (status & 0x01) {
1793 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
1800 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1801 /* Send command to read back the data */
1802 this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask);
1804 if (this->verify_buf(mtd, buf, len)) {
1805 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
1812 /* Deselect and wake up anyone waiting on the device */
1813 nand_release_device(mtd);
1818 /* XXX U-BOOT XXX */
1821 * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
1822 * @mtd: MTD device structure
1823 * @vecs: the iovectors to write
1824 * @count: number of vectors
1825 * @to: offset to write to
1826 * @retlen: pointer to variable to store the number of written bytes
1828 * NAND write with kvec. This just calls the ecc function
1830 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1831 loff_t to, size_t * retlen)
1833 return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL));
1837 * nand_writev_ecc - [MTD Interface] write with iovec with ecc
1838 * @mtd: MTD device structure
1839 * @vecs: the iovectors to write
1840 * @count: number of vectors
1841 * @to: offset to write to
1842 * @retlen: pointer to variable to store the number of written bytes
1843 * @eccbuf: filesystem supplied oob data buffer
1844 * @oobsel: oob selection structure
1846 * NAND write with iovec with ecc
1848 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1849 loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
1851 int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
1852 int oob, numpages, autoplace = 0, startpage;
1853 struct nand_chip *this = mtd->priv;
1854 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1855 u_char *oobbuf, *bufstart;
1857 /* Preset written len for early exit */
1860 /* Calculate total length of data */
1862 for (i = 0; i < count; i++)
1863 total_len += (int) vecs[i].iov_len;
1865 DEBUG (MTD_DEBUG_LEVEL3,
1866 "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
1868 /* Do not allow write past end of page */
1869 if ((to + total_len) > mtd->size) {
1870 DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
1874 /* reject writes, which are not page aligned */
1875 if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
1876 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1880 /* Grab the lock and see if the device is available */
1881 nand_get_device (this, mtd, FL_WRITING);
1883 /* Get the current chip-nr */
1884 chipnr = (int) (to >> this->chip_shift);
1885 /* Select the NAND device */
1886 this->select_chip(mtd, chipnr);
1888 /* Check, if it is write protected */
1889 if (nand_check_wp(mtd))
1892 /* if oobsel is NULL, use chip defaults */
1894 oobsel = &mtd->oobinfo;
1896 /* Autoplace of oob data ? Use the default placement scheme */
1897 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1898 oobsel = this->autooob;
1901 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1904 /* Setup start page */
1905 page = (int) (to >> this->page_shift);
1906 /* Invalidate the page cache, if we write to the cached page */
1907 if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))
1910 startpage = page & this->pagemask;
1912 /* Loop until all kvec' data has been written */
1915 /* If the given tuple is >= pagesize then
1916 * write it out from the iov
1918 if ((vecs->iov_len - len) >= mtd->oobblock) {
1919 /* Calc number of pages we can write
1920 * out of this iov in one go */
1921 numpages = (vecs->iov_len - len) >> this->page_shift;
1922 /* Do not cross block boundaries */
1923 numpages = min (ppblock - (startpage & (ppblock - 1)), numpages);
1924 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1925 bufstart = (u_char *)vecs->iov_base;
1927 this->data_poi = bufstart;
1929 for (i = 1; i <= numpages; i++) {
1930 /* Write one page. If this is the last page to write
1931 * then use the real pageprogram command, else select
1932 * cached programming if supported by the chip.
1934 ret = nand_write_page (mtd, this, page & this->pagemask,
1935 &oobbuf[oob], oobsel, i != numpages);
1938 this->data_poi += mtd->oobblock;
1939 len += mtd->oobblock;
1940 oob += mtd->oobsize;
1943 /* Check, if we have to switch to the next tuple */
1944 if (len >= (int) vecs->iov_len) {
1950 /* We must use the internal buffer, read data out of each
1951 * tuple until we have a full page to write
1954 while (cnt < mtd->oobblock) {
1955 if (vecs->iov_base != NULL && vecs->iov_len)
1956 this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
1957 /* Check, if we have to switch to the next tuple */
1958 if (len >= (int) vecs->iov_len) {
1964 this->pagebuf = page;
1965 this->data_poi = this->data_buf;
1966 bufstart = this->data_poi;
1968 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1969 ret = nand_write_page (mtd, this, page & this->pagemask,
1976 this->data_poi = bufstart;
1977 ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
1981 written += mtd->oobblock * numpages;
1986 startpage = page & this->pagemask;
1987 /* Check, if we cross a chip boundary */
1990 this->select_chip(mtd, -1);
1991 this->select_chip(mtd, chipnr);
1996 /* Deselect and wake up anyone waiting on the device */
1997 nand_release_device(mtd);
2005 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2006 * @mtd: MTD device structure
2007 * @page: the page address of the block which will be erased
2009 * Standard erase command for NAND chips
2011 static void single_erase_cmd (struct mtd_info *mtd, int page)
2013 struct nand_chip *this = mtd->priv;
2014 /* Send commands to erase a block */
2015 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2016 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2020 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2021 * @mtd: MTD device structure
2022 * @page: the page address of the block which will be erased
2024 * AND multi block erase command function
2025 * Erase 4 consecutive blocks
2027 static void multi_erase_cmd (struct mtd_info *mtd, int page)
2029 struct nand_chip *this = mtd->priv;
2030 /* Send commands to erase a block */
2031 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2032 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2033 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2034 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2035 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2039 * nand_erase - [MTD Interface] erase block(s)
2040 * @mtd: MTD device structure
2041 * @instr: erase instruction
2043 * Erase one ore more blocks
2045 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
2047 return nand_erase_nand (mtd, instr, 0);
2051 * nand_erase_intern - [NAND Interface] erase block(s)
2052 * @mtd: MTD device structure
2053 * @instr: erase instruction
2054 * @allowbbt: allow erasing the bbt area
2056 * Erase one ore more blocks
2058 int nand_erase_nand (struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
2060 int page, len, status, pages_per_block, ret, chipnr;
2061 struct nand_chip *this = mtd->priv;
2063 DEBUG (MTD_DEBUG_LEVEL3,
2064 "nand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
2066 /* Start address must align on block boundary */
2067 if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
2068 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2072 /* Length must align on block boundary */
2073 if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
2074 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
2078 /* Do not allow erase past end of device */
2079 if ((instr->len + instr->addr) > mtd->size) {
2080 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
2084 instr->fail_addr = 0xffffffff;
2086 /* Grab the lock and see if the device is available */
2087 nand_get_device (this, mtd, FL_ERASING);
2089 /* Shift to get first page */
2090 page = (int) (instr->addr >> this->page_shift);
2091 chipnr = (int) (instr->addr >> this->chip_shift);
2093 /* Calculate pages in each block */
2094 pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
2096 /* Select the NAND device */
2097 this->select_chip(mtd, chipnr);
2099 /* Check the WP bit */
2100 /* Check, if it is write protected */
2101 if (nand_check_wp(mtd)) {
2102 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
2103 instr->state = MTD_ERASE_FAILED;
2107 /* Loop through the pages */
2110 instr->state = MTD_ERASING;
2113 /* Check if we have a bad block, we do not erase bad blocks ! */
2114 if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
2115 printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
2116 instr->state = MTD_ERASE_FAILED;
2120 /* Invalidate the page cache, if we erase the block which contains
2121 the current cached page */
2122 if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
2125 this->erase_cmd (mtd, page & this->pagemask);
2127 status = this->waitfunc (mtd, this, FL_ERASING);
2129 /* See if block erase succeeded */
2130 if (status & 0x01) {
2131 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
2132 instr->state = MTD_ERASE_FAILED;
2133 instr->fail_addr = (page << this->page_shift);
2137 /* Increment page address and decrement length */
2138 len -= (1 << this->phys_erase_shift);
2139 page += pages_per_block;
2141 /* Check, if we cross a chip boundary */
2142 if (len && !(page & this->pagemask)) {
2144 this->select_chip(mtd, -1);
2145 this->select_chip(mtd, chipnr);
2148 instr->state = MTD_ERASE_DONE;
2152 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2153 /* Do call back function */
2155 mtd_erase_callback(instr);
2157 /* Deselect and wake up anyone waiting on the device */
2158 nand_release_device(mtd);
2160 /* Return more or less happy */
2165 * nand_sync - [MTD Interface] sync
2166 * @mtd: MTD device structure
2168 * Sync is actually a wait for chip ready function
2170 static void nand_sync (struct mtd_info *mtd)
2172 struct nand_chip *this = mtd->priv;
2174 DEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2176 /* Grab the lock and see if the device is available */
2177 nand_get_device (this, mtd, FL_SYNCING);
2178 /* Release it and go back */
2179 nand_release_device (mtd);
2184 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2185 * @mtd: MTD device structure
2186 * @ofs: offset relative to mtd start
2188 static int nand_block_isbad (struct mtd_info *mtd, loff_t ofs)
2190 /* Check for invalid offset */
2191 if (ofs > mtd->size)
2194 return nand_block_checkbad (mtd, ofs, 1, 0);
2198 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2199 * @mtd: MTD device structure
2200 * @ofs: offset relative to mtd start
2202 static int nand_block_markbad (struct mtd_info *mtd, loff_t ofs)
2204 struct nand_chip *this = mtd->priv;
2207 if ((ret = nand_block_isbad(mtd, ofs))) {
2208 /* If it was bad already, return success and do nothing. */
2214 return this->block_markbad(mtd, ofs);
2218 * nand_scan - [NAND Interface] Scan for the NAND device
2219 * @mtd: MTD device structure
2220 * @maxchips: Number of chips to scan for
2222 * This fills out all the not initialized function pointers
2223 * with the defaults.
2224 * The flash ID is read and the mtd/chip structures are
2225 * filled with the appropriate values. Buffers are allocated if
2226 * they are not provided by the board driver
2229 int nand_scan (struct mtd_info *mtd, int maxchips)
2231 int i, j, nand_maf_id, nand_dev_id, busw;
2232 struct nand_chip *this = mtd->priv;
2234 /* Get buswidth to select the correct functions*/
2235 busw = this->options & NAND_BUSWIDTH_16;
2237 /* check for proper chip_delay setup, set 20us if not */
2238 if (!this->chip_delay)
2239 this->chip_delay = 20;
2241 /* check, if a user supplied command function given */
2242 if (this->cmdfunc == NULL)
2243 this->cmdfunc = nand_command;
2245 /* check, if a user supplied wait function given */
2246 if (this->waitfunc == NULL)
2247 this->waitfunc = nand_wait;
2249 if (!this->select_chip)
2250 this->select_chip = nand_select_chip;
2251 if (!this->write_byte)
2252 this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2253 if (!this->read_byte)
2254 this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2255 if (!this->write_word)
2256 this->write_word = nand_write_word;
2257 if (!this->read_word)
2258 this->read_word = nand_read_word;
2259 if (!this->block_bad)
2260 this->block_bad = nand_block_bad;
2261 if (!this->block_markbad)
2262 this->block_markbad = nand_default_block_markbad;
2263 if (!this->write_buf)
2264 this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2265 if (!this->read_buf)
2266 this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2267 if (!this->verify_buf)
2268 this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2269 if (!this->scan_bbt)
2270 this->scan_bbt = nand_default_bbt;
2272 /* Select the device */
2273 this->select_chip(mtd, 0);
2275 /* Send the command for reading device ID */
2276 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2278 /* Read manufacturer and device IDs */
2279 nand_maf_id = this->read_byte(mtd);
2280 nand_dev_id = this->read_byte(mtd);
2282 /* Print and store flash device information */
2283 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2285 if (nand_dev_id != nand_flash_ids[i].id)
2288 if (!mtd->name) mtd->name = nand_flash_ids[i].name;
2289 this->chipsize = nand_flash_ids[i].chipsize << 20;
2291 /* New devices have all the information in additional id bytes */
2292 if (!nand_flash_ids[i].pagesize) {
2294 /* The 3rd id byte contains non relevant data ATM */
2295 extid = this->read_byte(mtd);
2296 /* The 4th id byte is the important one */
2297 extid = this->read_byte(mtd);
2299 mtd->oobblock = 1024 << (extid & 0x3);
2302 mtd->oobsize = (8 << (extid & 0x03)) * (mtd->oobblock / 512);
2304 /* Calc blocksize. Blocksize is multiples of 64KiB */
2305 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2307 /* Get buswidth information */
2308 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2311 /* Old devices have this data hardcoded in the
2312 * device id table */
2313 mtd->erasesize = nand_flash_ids[i].erasesize;
2314 mtd->oobblock = nand_flash_ids[i].pagesize;
2315 mtd->oobsize = mtd->oobblock / 32;
2316 busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
2319 /* Check, if buswidth is correct. Hardware drivers should set
2321 if (busw != (this->options & NAND_BUSWIDTH_16)) {
2322 printk (KERN_INFO "NAND device: Manufacturer ID:"
2323 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2324 nand_manuf_ids[i].name , mtd->name);
2325 printk (KERN_WARNING
2326 "NAND bus width %d instead %d bit\n",
2327 (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
2329 this->select_chip(mtd, -1);
2333 /* Calculate the address shift from the page size */
2334 this->page_shift = ffs(mtd->oobblock) - 1;
2335 this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
2336 this->chip_shift = ffs(this->chipsize) - 1;
2338 /* Set the bad block position */
2339 this->badblockpos = mtd->oobblock > 512 ?
2340 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2342 /* Get chip options, preserve non chip based options */
2343 this->options &= ~NAND_CHIPOPTIONS_MSK;
2344 this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
2345 /* Set this as a default. Board drivers can override it, if neccecary */
2346 this->options |= NAND_NO_AUTOINCR;
2347 /* Check if this is a not a samsung device. Do not clear the options
2348 * for chips which are not having an extended id.
2350 if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
2351 this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2353 /* Check for AND chips with 4 page planes */
2354 if (this->options & NAND_4PAGE_ARRAY)
2355 this->erase_cmd = multi_erase_cmd;
2357 this->erase_cmd = single_erase_cmd;
2359 /* Do not replace user supplied command function ! */
2360 if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
2361 this->cmdfunc = nand_command_lp;
2363 /* Try to identify manufacturer */
2364 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
2365 if (nand_manuf_ids[j].id == nand_maf_id)
2368 printk (KERN_INFO "NAND device: Manufacturer ID:"
2369 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2370 nand_manuf_ids[j].name , nand_flash_ids[i].name);
2374 if (!nand_flash_ids[i].name) {
2375 printk (KERN_WARNING "No NAND device found!!!\n");
2376 this->select_chip(mtd, -1);
2380 for (i=1; i < maxchips; i++) {
2381 this->select_chip(mtd, i);
2383 /* Send the command for reading device ID */
2384 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2386 /* Read manufacturer and device IDs */
2387 if (nand_maf_id != this->read_byte(mtd) ||
2388 nand_dev_id != this->read_byte(mtd))
2392 printk(KERN_INFO "%d NAND chips detected\n", i);
2394 /* Allocate buffers, if neccecary */
2395 if (!this->oob_buf) {
2397 len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
2398 this->oob_buf = kmalloc (len, GFP_KERNEL);
2399 if (!this->oob_buf) {
2400 printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
2403 this->options |= NAND_OOBBUF_ALLOC;
2406 if (!this->data_buf) {
2408 len = mtd->oobblock + mtd->oobsize;
2409 this->data_buf = kmalloc (len, GFP_KERNEL);
2410 if (!this->data_buf) {
2411 if (this->options & NAND_OOBBUF_ALLOC)
2412 kfree (this->oob_buf);
2413 printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
2416 this->options |= NAND_DATABUF_ALLOC;
2419 /* Store the number of chips and calc total size for mtd */
2421 mtd->size = i * this->chipsize;
2422 /* Convert chipsize to number of pages per chip -1. */
2423 this->pagemask = (this->chipsize >> this->page_shift) - 1;
2424 /* Preset the internal oob buffer */
2425 memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
2427 /* If no default placement scheme is given, select an
2428 * appropriate one */
2429 if (!this->autooob) {
2430 /* Select the appropriate default oob placement scheme for
2431 * placement agnostic filesystems */
2432 switch (mtd->oobsize) {
2434 this->autooob = &nand_oob_8;
2437 this->autooob = &nand_oob_16;
2440 this->autooob = &nand_oob_64;
2443 printk (KERN_WARNING "No oob scheme defined for oobsize %d\n",
2449 /* The number of bytes available for the filesystem to place fs dependend
2451 if (this->options & NAND_BUSWIDTH_16) {
2452 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 2);
2453 if (this->autooob->eccbytes & 0x01)
2456 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 1);
2459 * check ECC mode, default to software
2460 * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
2461 * fallback to software ECC
2463 this->eccsize = 256; /* set default eccsize */
2466 switch (this->eccmode) {
2467 case NAND_ECC_HW12_2048:
2468 if (mtd->oobblock < 2048) {
2469 printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
2471 this->eccmode = NAND_ECC_SOFT;
2472 this->calculate_ecc = nand_calculate_ecc;
2473 this->correct_data = nand_correct_data;
2475 this->eccsize = 2048;
2478 case NAND_ECC_HW3_512:
2479 case NAND_ECC_HW6_512:
2480 case NAND_ECC_HW8_512:
2481 if (mtd->oobblock == 256) {
2482 printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
2483 this->eccmode = NAND_ECC_SOFT;
2484 this->calculate_ecc = nand_calculate_ecc;
2485 this->correct_data = nand_correct_data;
2487 this->eccsize = 512; /* set eccsize to 512 */
2490 case NAND_ECC_HW3_256:
2494 printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
2495 this->eccmode = NAND_ECC_NONE;
2499 this->calculate_ecc = nand_calculate_ecc;
2500 this->correct_data = nand_correct_data;
2504 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
2508 /* Check hardware ecc function availability and adjust number of ecc bytes per
2511 switch (this->eccmode) {
2512 case NAND_ECC_HW12_2048:
2513 this->eccbytes += 4;
2514 case NAND_ECC_HW8_512:
2515 this->eccbytes += 2;
2516 case NAND_ECC_HW6_512:
2517 this->eccbytes += 3;
2518 case NAND_ECC_HW3_512:
2519 case NAND_ECC_HW3_256:
2520 if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
2522 printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
2526 mtd->eccsize = this->eccsize;
2528 /* Set the number of read / write steps for one page to ensure ECC generation */
2529 switch (this->eccmode) {
2530 case NAND_ECC_HW12_2048:
2531 this->eccsteps = mtd->oobblock / 2048;
2533 case NAND_ECC_HW3_512:
2534 case NAND_ECC_HW6_512:
2535 case NAND_ECC_HW8_512:
2536 this->eccsteps = mtd->oobblock / 512;
2538 case NAND_ECC_HW3_256:
2540 this->eccsteps = mtd->oobblock / 256;
2548 /* XXX U-BOOT XXX */
2550 /* Initialize state, waitqueue and spinlock */
2551 this->state = FL_READY;
2552 init_waitqueue_head (&this->wq);
2553 spin_lock_init (&this->chip_lock);
2556 /* De-select the device */
2557 this->select_chip(mtd, -1);
2559 /* Invalidate the pagebuffer reference */
2562 /* Fill in remaining MTD driver data */
2563 mtd->type = MTD_NANDFLASH;
2564 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
2565 mtd->ecctype = MTD_ECC_SW;
2566 mtd->erase = nand_erase;
2568 mtd->unpoint = NULL;
2569 mtd->read = nand_read;
2570 mtd->write = nand_write;
2571 mtd->read_ecc = nand_read_ecc;
2572 mtd->write_ecc = nand_write_ecc;
2573 mtd->read_oob = nand_read_oob;
2574 mtd->write_oob = nand_write_oob;
2575 /* XXX U-BOOT XXX */
2578 mtd->writev = nand_writev;
2579 mtd->writev_ecc = nand_writev_ecc;
2581 mtd->sync = nand_sync;
2582 /* XXX U-BOOT XXX */
2586 mtd->suspend = NULL;
2589 mtd->block_isbad = nand_block_isbad;
2590 mtd->block_markbad = nand_block_markbad;
2592 /* and make the autooob the default one */
2593 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
2594 /* XXX U-BOOT XXX */
2596 mtd->owner = THIS_MODULE;
2598 /* Build bad block table */
2599 return this->scan_bbt (mtd);
2603 * nand_release - [NAND Interface] Free resources held by the NAND device
2604 * @mtd: MTD device structure
2606 void nand_release (struct mtd_info *mtd)
2608 struct nand_chip *this = mtd->priv;
2610 #ifdef CONFIG_MTD_PARTITIONS
2611 /* Deregister partitions */
2612 del_mtd_partitions (mtd);
2614 /* Deregister the device */
2615 /* XXX U-BOOT XXX */
2617 del_mtd_device (mtd);
2619 /* Free bad block table memory, if allocated */
2622 /* Buffer allocated by nand_scan ? */
2623 if (this->options & NAND_OOBBUF_ALLOC)
2624 kfree (this->oob_buf);
2625 /* Buffer allocated by nand_scan ? */
2626 if (this->options & NAND_DATABUF_ALLOC)
2627 kfree (this->data_buf);