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.121 2004/10/06 19:53:11 gleixner 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.
52 #include <linux/delay.h>
53 #include <linux/errno.h>
54 #include <linux/sched.h>
55 #include <linux/slab.h>
56 #include <linux/types.h>
57 #include <linux/mtd/mtd.h>
58 #include <linux/mtd/nand.h>
59 #include <linux/mtd/nand_ecc.h>
60 #include <linux/mtd/compatmac.h>
61 #include <linux/interrupt.h>
62 #include <linux/bitops.h>
65 #ifdef CONFIG_MTD_PARTITIONS
66 #include <linux/mtd/partitions.h>
69 /* Define default oob placement schemes for large and small page devices */
70 static struct nand_oobinfo nand_oob_8 = {
71 .useecc = MTD_NANDECC_AUTOPLACE,
74 .oobfree = { {3, 2}, {6, 2} }
77 static struct nand_oobinfo nand_oob_16 = {
78 .useecc = MTD_NANDECC_AUTOPLACE,
80 .eccpos = {0, 1, 2, 3, 6, 7},
84 static struct nand_oobinfo nand_oob_64 = {
85 .useecc = MTD_NANDECC_AUTOPLACE,
88 40, 41, 42, 43, 44, 45, 46, 47,
89 48, 49, 50, 51, 52, 53, 54, 55,
90 56, 57, 58, 59, 60, 61, 62, 63},
91 .oobfree = { {2, 38} }
94 /* This is used for padding purposes in nand_write_oob */
95 static u_char ffchars[] = {
96 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
97 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
98 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
99 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
100 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
101 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
107 * NAND low-level MTD interface functions
109 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
110 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
111 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
113 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
114 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
115 size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
116 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
117 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf);
118 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
119 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
120 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char *buf);
121 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
122 unsigned long count, loff_t to, size_t * retlen);
123 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs,
124 unsigned long count, loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
125 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
126 static void nand_sync (struct mtd_info *mtd);
128 /* Some internal functions */
129 static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf,
130 struct nand_oobinfo *oobsel, int mode);
131 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
132 static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
133 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode);
135 #define nand_verify_pages(...) (0)
138 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state);
141 * nand_release_device - [GENERIC] release chip
142 * @mtd: MTD device structure
144 * Deselect, release chip lock and wake up anyone waiting on the device
146 static void nand_release_device (struct mtd_info *mtd)
148 struct nand_chip *this = mtd->priv;
150 /* De-select the NAND device */
151 this->select_chip(mtd, -1);
152 /* Do we have a hardware controller ? */
153 if (this->controller) {
154 spin_lock(&this->controller->lock);
155 this->controller->active = NULL;
156 spin_unlock(&this->controller->lock);
158 /* Release the chip */
159 spin_lock (&this->chip_lock);
160 this->state = FL_READY;
162 spin_unlock (&this->chip_lock);
166 * nand_read_byte - [DEFAULT] read one byte from the chip
167 * @mtd: MTD device structure
169 * Default read function for 8bit buswith
171 static u_char nand_read_byte(struct mtd_info *mtd)
173 struct nand_chip *this = mtd->priv;
174 return readb(this->IO_ADDR_R);
178 * nand_write_byte - [DEFAULT] write one byte to the chip
179 * @mtd: MTD device structure
180 * @byte: pointer to data byte to write
182 * Default write function for 8it buswith
184 static void nand_write_byte(struct mtd_info *mtd, u_char byte)
186 struct nand_chip *this = mtd->priv;
187 writeb(byte, this->IO_ADDR_W);
191 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
192 * @mtd: MTD device structure
194 * Default read function for 16bit buswith with
195 * endianess conversion
197 static u_char nand_read_byte16(struct mtd_info *mtd)
199 struct nand_chip *this = mtd->priv;
200 return (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
204 * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
205 * @mtd: MTD device structure
206 * @byte: pointer to data byte to write
208 * Default write function for 16bit buswith with
209 * endianess conversion
211 static void nand_write_byte16(struct mtd_info *mtd, u_char byte)
213 struct nand_chip *this = mtd->priv;
214 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
218 * nand_read_word - [DEFAULT] read one word from the chip
219 * @mtd: MTD device structure
221 * Default read function for 16bit buswith without
222 * endianess conversion
224 static u16 nand_read_word(struct mtd_info *mtd)
226 struct nand_chip *this = mtd->priv;
227 return readw(this->IO_ADDR_R);
231 * nand_write_word - [DEFAULT] write one word to the chip
232 * @mtd: MTD device structure
233 * @word: data word to write
235 * Default write function for 16bit buswith without
236 * endianess conversion
238 static void nand_write_word(struct mtd_info *mtd, u16 word)
240 struct nand_chip *this = mtd->priv;
241 writew(word, this->IO_ADDR_W);
245 * nand_select_chip - [DEFAULT] control CE line
246 * @mtd: MTD device structure
247 * @chip: chipnumber to select, -1 for deselect
249 * Default select function for 1 chip devices.
251 static void nand_select_chip(struct mtd_info *mtd, int chip)
253 struct nand_chip *this = mtd->priv;
256 this->hwcontrol(mtd, NAND_CTL_CLRNCE);
259 this->hwcontrol(mtd, NAND_CTL_SETNCE);
268 * nand_write_buf - [DEFAULT] write buffer to chip
269 * @mtd: MTD device structure
271 * @len: number of bytes to write
273 * Default write function for 8bit buswith
275 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
278 struct nand_chip *this = mtd->priv;
280 for (i=0; i<len; i++)
281 writeb(buf[i], this->IO_ADDR_W);
285 * nand_read_buf - [DEFAULT] read chip data into buffer
286 * @mtd: MTD device structure
287 * @buf: buffer to store date
288 * @len: number of bytes to read
290 * Default read function for 8bit buswith
292 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
295 struct nand_chip *this = mtd->priv;
297 for (i=0; i<len; i++)
298 buf[i] = readb(this->IO_ADDR_R);
302 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
303 * @mtd: MTD device structure
304 * @buf: buffer containing the data to compare
305 * @len: number of bytes to compare
307 * Default verify function for 8bit buswith
309 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
312 struct nand_chip *this = mtd->priv;
314 for (i=0; i<len; i++)
315 if (buf[i] != readb(this->IO_ADDR_R))
322 * nand_write_buf16 - [DEFAULT] write buffer to chip
323 * @mtd: MTD device structure
325 * @len: number of bytes to write
327 * Default write function for 16bit buswith
329 static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
332 struct nand_chip *this = mtd->priv;
333 u16 *p = (u16 *) buf;
336 for (i=0; i<len; i++)
337 writew(p[i], this->IO_ADDR_W);
342 * nand_read_buf16 - [DEFAULT] read chip data into buffer
343 * @mtd: MTD device structure
344 * @buf: buffer to store date
345 * @len: number of bytes to read
347 * Default read function for 16bit buswith
349 static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
352 struct nand_chip *this = mtd->priv;
353 u16 *p = (u16 *) buf;
356 for (i=0; i<len; i++)
357 p[i] = readw(this->IO_ADDR_R);
361 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
362 * @mtd: MTD device structure
363 * @buf: buffer containing the data to compare
364 * @len: number of bytes to compare
366 * Default verify function for 16bit buswith
368 static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
371 struct nand_chip *this = mtd->priv;
372 u16 *p = (u16 *) buf;
375 for (i=0; i<len; i++)
376 if (p[i] != readw(this->IO_ADDR_R))
383 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
384 * @mtd: MTD device structure
385 * @ofs: offset from device start
386 * @getchip: 0, if the chip is already selected
388 * Check, if the block is bad.
390 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
392 int page, chipnr, res = 0;
393 struct nand_chip *this = mtd->priv;
397 page = (int)(ofs >> this->page_shift);
398 chipnr = (int)(ofs >> this->chip_shift);
400 /* Grab the lock and see if the device is available */
401 nand_get_device (this, mtd, FL_READING);
403 /* Select the NAND device */
404 this->select_chip(mtd, chipnr);
408 if (this->options & NAND_BUSWIDTH_16) {
409 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page & this->pagemask);
410 bad = cpu_to_le16(this->read_word(mtd));
411 if (this->badblockpos & 0x1)
413 if ((bad & 0xFF) != 0xff)
416 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page & this->pagemask);
417 if (this->read_byte(mtd) != 0xff)
422 /* Deselect and wake up anyone waiting on the device */
423 nand_release_device(mtd);
430 * nand_default_block_markbad - [DEFAULT] mark a block bad
431 * @mtd: MTD device structure
432 * @ofs: offset from device start
434 * This is the default implementation, which can be overridden by
435 * a hardware specific driver.
437 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
439 struct nand_chip *this = mtd->priv;
440 u_char buf[2] = {0, 0};
444 /* Get block number */
445 block = ((int) ofs) >> this->bbt_erase_shift;
446 this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
448 /* Do we have a flash based bad block table ? */
449 if (this->options & NAND_USE_FLASH_BBT)
450 return nand_update_bbt (mtd, ofs);
452 /* We write two bytes, so we dont have to mess with 16 bit access */
453 ofs += mtd->oobsize + (this->badblockpos & ~0x01);
454 return nand_write_oob (mtd, ofs , 2, &retlen, buf);
458 * nand_check_wp - [GENERIC] check if the chip is write protected
459 * @mtd: MTD device structure
460 * Check, if the device is write protected
462 * The function expects, that the device is already selected
464 static int nand_check_wp (struct mtd_info *mtd)
466 struct nand_chip *this = mtd->priv;
467 /* Check the WP bit */
468 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
469 return (this->read_byte(mtd) & 0x80) ? 0 : 1;
473 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
474 * @mtd: MTD device structure
475 * @ofs: offset from device start
476 * @getchip: 0, if the chip is already selected
477 * @allowbbt: 1, if its allowed to access the bbt area
479 * Check, if the block is bad. Either by reading the bad block table or
480 * calling of the scan function.
482 static int nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
484 struct nand_chip *this = mtd->priv;
487 return this->block_bad(mtd, ofs, getchip);
489 /* Return info from the table */
490 return nand_isbad_bbt (mtd, ofs, allowbbt);
494 * nand_command - [DEFAULT] Send command to NAND device
495 * @mtd: MTD device structure
496 * @command: the command to be sent
497 * @column: the column address for this command, -1 if none
498 * @page_addr: the page address for this command, -1 if none
500 * Send command to NAND device. This function is used for small page
501 * devices (256/512 Bytes per page)
503 static void nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
505 register struct nand_chip *this = mtd->priv;
507 /* Begin command latch cycle */
508 this->hwcontrol(mtd, NAND_CTL_SETCLE);
510 * Write out the command to the device.
512 if (command == NAND_CMD_SEQIN) {
515 if (column >= mtd->oobblock) {
517 column -= mtd->oobblock;
518 readcmd = NAND_CMD_READOOB;
519 } else if (column < 256) {
520 /* First 256 bytes --> READ0 */
521 readcmd = NAND_CMD_READ0;
524 readcmd = NAND_CMD_READ1;
526 this->write_byte(mtd, readcmd);
528 this->write_byte(mtd, command);
530 /* Set ALE and clear CLE to start address cycle */
531 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
533 if (column != -1 || page_addr != -1) {
534 this->hwcontrol(mtd, NAND_CTL_SETALE);
536 /* Serially input address */
538 /* Adjust columns for 16 bit buswidth */
539 if (this->options & NAND_BUSWIDTH_16)
541 this->write_byte(mtd, column);
543 if (page_addr != -1) {
544 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
545 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
546 /* One more address cycle for devices > 32MiB */
547 if (this->chipsize > (32 << 20))
548 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));
550 /* Latch in address */
551 this->hwcontrol(mtd, NAND_CTL_CLRALE);
555 * program and erase have their own busy handlers
556 * status and sequential in needs no delay
560 case NAND_CMD_PAGEPROG:
561 case NAND_CMD_ERASE1:
562 case NAND_CMD_ERASE2:
564 case NAND_CMD_STATUS:
570 udelay(this->chip_delay);
571 this->hwcontrol(mtd, NAND_CTL_SETCLE);
572 this->write_byte(mtd, NAND_CMD_STATUS);
573 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
574 while ( !(this->read_byte(mtd) & 0x40));
577 /* This applies to read commands */
580 * If we don't have access to the busy pin, we apply the given
583 if (!this->dev_ready) {
584 udelay (this->chip_delay);
589 /* Apply this short delay always to ensure that we do wait tWB in
590 * any case on any machine. */
592 /* wait until command is processed */
593 while (!this->dev_ready(mtd));
597 * nand_command_lp - [DEFAULT] Send command to NAND large page device
598 * @mtd: MTD device structure
599 * @command: the command to be sent
600 * @column: the column address for this command, -1 if none
601 * @page_addr: the page address for this command, -1 if none
603 * Send command to NAND device. This is the version for the new large page devices
604 * We dont have the seperate regions as we have in the small page devices.
605 * We must emulate NAND_CMD_READOOB to keep the code compatible.
608 static void nand_command_lp (struct mtd_info *mtd, unsigned command, int column, int page_addr)
610 register struct nand_chip *this = mtd->priv;
612 /* Emulate NAND_CMD_READOOB */
613 if (command == NAND_CMD_READOOB) {
614 column += mtd->oobblock;
615 command = NAND_CMD_READ0;
619 /* Begin command latch cycle */
620 this->hwcontrol(mtd, NAND_CTL_SETCLE);
621 /* Write out the command to the device. */
622 this->write_byte(mtd, command);
623 /* End command latch cycle */
624 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
626 if (column != -1 || page_addr != -1) {
627 this->hwcontrol(mtd, NAND_CTL_SETALE);
629 /* Serially input address */
631 /* Adjust columns for 16 bit buswidth */
632 if (this->options & NAND_BUSWIDTH_16)
634 this->write_byte(mtd, column & 0xff);
635 this->write_byte(mtd, column >> 8);
637 if (page_addr != -1) {
638 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
639 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
640 /* One more address cycle for devices > 128MiB */
641 if (this->chipsize > (128 << 20))
642 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0xff));
644 /* Latch in address */
645 this->hwcontrol(mtd, NAND_CTL_CLRALE);
649 * program and erase have their own busy handlers
650 * status and sequential in needs no delay
654 case NAND_CMD_CACHEDPROG:
655 case NAND_CMD_PAGEPROG:
656 case NAND_CMD_ERASE1:
657 case NAND_CMD_ERASE2:
659 case NAND_CMD_STATUS:
666 udelay(this->chip_delay);
667 this->hwcontrol(mtd, NAND_CTL_SETCLE);
668 this->write_byte(mtd, NAND_CMD_STATUS);
669 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
670 while ( !(this->read_byte(mtd) & 0x40));
674 /* Begin command latch cycle */
675 this->hwcontrol(mtd, NAND_CTL_SETCLE);
676 /* Write out the start read command */
677 this->write_byte(mtd, NAND_CMD_READSTART);
678 /* End command latch cycle */
679 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
680 /* Fall through into ready check */
682 /* This applies to read commands */
685 * If we don't have access to the busy pin, we apply the given
688 if (!this->dev_ready) {
689 udelay (this->chip_delay);
694 /* Apply this short delay always to ensure that we do wait tWB in
695 * any case on any machine. */
697 /* wait until command is processed */
698 while (!this->dev_ready(mtd));
702 * nand_get_device - [GENERIC] Get chip for selected access
703 * @this: the nand chip descriptor
704 * @mtd: MTD device structure
705 * @new_state: the state which is requested
707 * Get the device and lock it for exclusive access
709 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
711 struct nand_chip *active = this;
713 DECLARE_WAITQUEUE (wait, current);
716 * Grab the lock and see if the device is available
719 /* Hardware controller shared among independend devices */
720 if (this->controller) {
721 spin_lock (&this->controller->lock);
722 if (this->controller->active)
723 active = this->controller->active;
725 this->controller->active = this;
726 spin_unlock (&this->controller->lock);
729 if (active == this) {
730 spin_lock (&this->chip_lock);
731 if (this->state == FL_READY) {
732 this->state = new_state;
733 spin_unlock (&this->chip_lock);
737 set_current_state (TASK_UNINTERRUPTIBLE);
738 add_wait_queue (&active->wq, &wait);
739 spin_unlock (&active->chip_lock);
741 remove_wait_queue (&active->wq, &wait);
746 * nand_wait - [DEFAULT] wait until the command is done
747 * @mtd: MTD device structure
748 * @this: NAND chip structure
749 * @state: state to select the max. timeout value
751 * Wait for command done. This applies to erase and program only
752 * Erase can take up to 400ms and program up to 20ms according to
753 * general NAND and SmartMedia specs
756 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
759 unsigned long timeo = jiffies;
762 if (state == FL_ERASING)
763 timeo += (HZ * 400) / 1000;
765 timeo += (HZ * 20) / 1000;
767 /* Apply this short delay always to ensure that we do wait tWB in
768 * any case on any machine. */
771 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
772 this->cmdfunc (mtd, NAND_CMD_STATUS_MULTI, -1, -1);
774 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
776 while (time_before(jiffies, timeo)) {
777 /* Check, if we were interrupted */
778 if (this->state != state)
781 if (this->dev_ready) {
782 if (this->dev_ready(mtd))
785 if (this->read_byte(mtd) & NAND_STATUS_READY)
790 status = (int) this->read_byte(mtd);
795 * nand_write_page - [GENERIC] write one page
796 * @mtd: MTD device structure
797 * @this: NAND chip structure
798 * @page: startpage inside the chip, must be called with (page & this->pagemask)
799 * @oob_buf: out of band data buffer
800 * @oobsel: out of band selecttion structre
801 * @cached: 1 = enable cached programming if supported by chip
803 * Nand_page_program function is used for write and writev !
804 * This function will always program a full page of data
805 * If you call it with a non page aligned buffer, you're lost :)
807 * Cached programming is not supported yet.
809 static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page,
810 u_char *oob_buf, struct nand_oobinfo *oobsel, int cached)
814 int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
815 int *oob_config = oobsel->eccpos;
816 int datidx = 0, eccidx = 0, eccsteps = this->eccsteps;
819 /* FIXME: Enable cached programming */
822 /* Send command to begin auto page programming */
823 this->cmdfunc (mtd, NAND_CMD_SEQIN, 0x00, page);
825 /* Write out complete page of data, take care of eccmode */
827 /* No ecc, write all */
829 printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
830 this->write_buf(mtd, this->data_poi, mtd->oobblock);
833 /* Software ecc 3/256, write all */
835 for (; eccsteps; eccsteps--) {
836 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
837 for (i = 0; i < 3; i++, eccidx++)
838 oob_buf[oob_config[eccidx]] = ecc_code[i];
839 datidx += this->eccsize;
841 this->write_buf(mtd, this->data_poi, mtd->oobblock);
844 /* Hardware ecc 8 byte / 512 byte data */
845 case NAND_ECC_HW8_512:
847 /* Hardware ecc 6 byte / 512 byte data */
848 case NAND_ECC_HW6_512:
850 /* Hardware ecc 3 byte / 256 data */
851 /* Hardware ecc 3 byte / 512 byte data */
852 case NAND_ECC_HW3_256:
853 case NAND_ECC_HW3_512:
855 for (; eccsteps; eccsteps--) {
856 /* enable hardware ecc logic for write */
857 this->enable_hwecc(mtd, NAND_ECC_WRITE);
858 this->write_buf(mtd, &this->data_poi[datidx], this->eccsize);
859 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
860 for (i = 0; i < eccbytes; i++, eccidx++)
861 oob_buf[oob_config[eccidx]] = ecc_code[i];
862 /* If the hardware ecc provides syndromes then
863 * the ecc code must be written immidiately after
864 * the data bytes (words) */
865 if (this->options & NAND_HWECC_SYNDROME)
866 this->write_buf(mtd, ecc_code, eccbytes);
868 datidx += this->eccsize;
873 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
877 /* Write out OOB data */
878 if (this->options & NAND_HWECC_SYNDROME)
879 this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
881 this->write_buf(mtd, oob_buf, mtd->oobsize);
883 /* Send command to actually program the data */
884 this->cmdfunc (mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
887 /* call wait ready function */
888 status = this->waitfunc (mtd, this, FL_WRITING);
889 /* See if device thinks it succeeded */
891 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
895 /* FIXME: Implement cached programming ! */
896 /* wait until cache is ready*/
897 // status = this->waitfunc (mtd, this, FL_CACHEDRPG);
902 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
904 * nand_verify_pages - [GENERIC] verify the chip contents after a write
905 * @mtd: MTD device structure
906 * @this: NAND chip structure
907 * @page: startpage inside the chip, must be called with (page & this->pagemask)
908 * @numpages: number of pages to verify
909 * @oob_buf: out of band data buffer
910 * @oobsel: out of band selecttion structre
911 * @chipnr: number of the current chip
912 * @oobmode: 1 = full buffer verify, 0 = ecc only
914 * The NAND device assumes that it is always writing to a cleanly erased page.
915 * Hence, it performs its internal write verification only on bits that
916 * transitioned from 1 to 0. The device does NOT verify the whole page on a
917 * byte by byte basis. It is possible that the page was not completely erased
918 * or the page is becoming unusable due to wear. The read with ECC would catch
919 * the error later when the ECC page check fails, but we would rather catch
920 * it early in the page write stage. Better to write no data than invalid data.
922 static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
923 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
925 int i, j, datidx = 0, oobofs = 0, res = -EIO;
926 int eccsteps = this->eccsteps;
930 hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
932 /* Send command to read back the first page */
933 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page);
936 for (j = 0; j < eccsteps; j++) {
937 /* Loop through and verify the data */
938 if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
939 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
942 datidx += mtd->eccsize;
943 /* Have we a hw generator layout ? */
946 if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
947 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
950 oobofs += hweccbytes;
953 /* check, if we must compare all data or if we just have to
954 * compare the ecc bytes
957 if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
958 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
962 /* Read always, else autoincrement fails */
963 this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
965 if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
966 int ecccnt = oobsel->eccbytes;
968 for (i = 0; i < ecccnt; i++) {
969 int idx = oobsel->eccpos[i];
970 if (oobdata[idx] != oob_buf[oobofs + idx] ) {
971 DEBUG (MTD_DEBUG_LEVEL0,
972 "%s: Failed ECC write "
973 "verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__, page, i);
979 oobofs += mtd->oobsize - hweccbytes * eccsteps;
983 /* Apply delay or wait for ready/busy pin
984 * Do this before the AUTOINCR check, so no problems
985 * arise if a chip which does auto increment
986 * is marked as NOAUTOINCR by the board driver.
987 * Do this also before returning, so the chip is
988 * ready for the next command.
990 if (!this->dev_ready)
991 udelay (this->chip_delay);
993 while (!this->dev_ready(mtd));
995 /* All done, return happy */
1000 /* Check, if the chip supports auto page increment */
1001 if (!NAND_CANAUTOINCR(this))
1002 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1005 * Terminate the read command. We come here in case of an error
1006 * So we must issue a reset command.
1009 this->cmdfunc (mtd, NAND_CMD_RESET, -1, -1);
1015 * nand_read - [MTD Interface] MTD compability function for nand_read_ecc
1016 * @mtd: MTD device structure
1017 * @from: offset to read from
1018 * @len: number of bytes to read
1019 * @retlen: pointer to variable to store the number of read bytes
1020 * @buf: the databuffer to put data
1022 * This function simply calls nand_read_ecc with oob buffer and oobsel = NULL
1024 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1026 return nand_read_ecc (mtd, from, len, retlen, buf, NULL, NULL);
1031 * nand_read_ecc - [MTD Interface] Read data with ECC
1032 * @mtd: MTD device structure
1033 * @from: offset to read from
1034 * @len: number of bytes to read
1035 * @retlen: pointer to variable to store the number of read bytes
1036 * @buf: the databuffer to put data
1037 * @oob_buf: filesystem supplied oob data buffer
1038 * @oobsel: oob selection structure
1040 * NAND read with ECC
1042 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
1043 size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel)
1045 int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
1046 int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
1047 struct nand_chip *this = mtd->priv;
1048 u_char *data_poi, *oob_data = oob_buf;
1049 u_char ecc_calc[32];
1050 u_char ecc_code[32];
1051 int eccmode, eccsteps;
1052 int *oob_config, datidx;
1053 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1059 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1061 /* Do not allow reads past end of device */
1062 if ((from + len) > mtd->size) {
1063 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
1068 /* Grab the lock and see if the device is available */
1069 nand_get_device (this, mtd ,FL_READING);
1071 /* use userspace supplied oobinfo, if zero */
1073 oobsel = &mtd->oobinfo;
1075 /* Autoplace of oob data ? Use the default placement scheme */
1076 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1077 oobsel = this->autooob;
1079 eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
1080 oob_config = oobsel->eccpos;
1082 /* Select the NAND device */
1083 chipnr = (int)(from >> this->chip_shift);
1084 this->select_chip(mtd, chipnr);
1086 /* First we calculate the starting page */
1087 realpage = (int) (from >> this->page_shift);
1088 page = realpage & this->pagemask;
1090 /* Get raw starting column */
1091 col = from & (mtd->oobblock - 1);
1093 end = mtd->oobblock;
1094 ecc = this->eccsize;
1096 case NAND_ECC_HW6_512: /* Hardware ECC 6 byte / 512 byte data */
1099 case NAND_ECC_HW8_512: /* Hardware ECC 8 byte / 512 byte data */
1107 if (this->options & NAND_HWECC_SYNDROME)
1110 oobreadlen = mtd->oobsize;
1111 if (this->options & NAND_HWECC_SYNDROME)
1112 oobreadlen -= oobsel->eccbytes;
1114 /* Loop until all data read */
1115 while (read < len) {
1117 int aligned = (!col && (len - read) >= end);
1119 * If the read is not page aligned, we have to read into data buffer
1120 * due to ecc, else we read into return buffer direct
1123 data_poi = &buf[read];
1125 data_poi = this->data_buf;
1127 /* Check, if we have this page in the buffer
1129 * FIXME: Make it work when we must provide oob data too,
1130 * check the usage of data_buf oob field
1132 if (realpage == this->pagebuf && !oob_buf) {
1133 /* aligned read ? */
1135 memcpy (data_poi, this->data_buf, end);
1139 /* Check, if we must send the read command */
1141 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1145 /* get oob area, if we have no oob buffer from fs-driver */
1146 if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1147 oob_data = &this->data_buf[end];
1149 eccsteps = this->eccsteps;
1152 case NAND_ECC_NONE: { /* No ECC, Read in a page */
1153 static unsigned long lastwhinge = 0;
1154 if ((lastwhinge / HZ) != (jiffies / HZ)) {
1155 printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended\n");
1156 lastwhinge = jiffies;
1158 this->read_buf(mtd, data_poi, end);
1162 case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
1163 this->read_buf(mtd, data_poi, end);
1164 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc)
1165 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1168 case NAND_ECC_HW3_256: /* Hardware ECC 3 byte /256 byte data */
1169 case NAND_ECC_HW3_512: /* Hardware ECC 3 byte /512 byte data */
1170 case NAND_ECC_HW6_512: /* Hardware ECC 6 byte / 512 byte data */
1171 case NAND_ECC_HW8_512: /* Hardware ECC 8 byte / 512 byte data */
1172 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) {
1173 this->enable_hwecc(mtd, NAND_ECC_READ);
1174 this->read_buf(mtd, &data_poi[datidx], ecc);
1176 /* HW ecc with syndrome calculation must read the
1177 * syndrome from flash immidiately after the data */
1179 /* Some hw ecc generators need to know when the
1180 * syndrome is read from flash */
1181 this->enable_hwecc(mtd, NAND_ECC_READSYN);
1182 this->read_buf(mtd, &oob_data[i], eccbytes);
1183 /* We calc error correction directly, it checks the hw
1184 * generator for an error, reads back the syndrome and
1185 * does the error correction on the fly */
1186 if (this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]) == -1) {
1187 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: "
1188 "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
1192 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1198 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
1203 this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
1205 /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
1209 /* Pick the ECC bytes out of the oob data */
1210 for (j = 0; j < oobsel->eccbytes; j++)
1211 ecc_code[j] = oob_data[oob_config[j]];
1213 /* correct data, if neccecary */
1214 for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {
1215 ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
1217 /* Get next chunk of ecc bytes */
1220 /* Check, if we have a fs supplied oob-buffer,
1221 * This is the legacy mode. Used by YAFFS1
1222 * Should go away some day
1224 if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
1225 int *p = (int *)(&oob_data[mtd->oobsize]);
1229 if (ecc_status == -1) {
1230 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
1236 /* check, if we have a fs supplied oob-buffer */
1238 /* without autoplace. Legacy mode used by YAFFS1 */
1239 switch(oobsel->useecc) {
1240 case MTD_NANDECC_AUTOPLACE:
1241 /* Walk through the autoplace chunks */
1242 for (i = 0, j = 0; j < mtd->oobavail; i++) {
1243 int from = oobsel->oobfree[i][0];
1244 int num = oobsel->oobfree[i][1];
1245 memcpy(&oob_buf[oob], &oob_data[from], num);
1248 oob += mtd->oobavail;
1250 case MTD_NANDECC_PLACE:
1251 /* YAFFS1 legacy mode */
1252 oob_data += this->eccsteps * sizeof (int);
1254 oob_data += mtd->oobsize;
1258 /* Partial page read, transfer data into fs buffer */
1260 for (j = col; j < end && read < len; j++)
1261 buf[read++] = data_poi[j];
1262 this->pagebuf = realpage;
1264 read += mtd->oobblock;
1266 /* Apply delay or wait for ready/busy pin
1267 * Do this before the AUTOINCR check, so no problems
1268 * arise if a chip which does auto increment
1269 * is marked as NOAUTOINCR by the board driver.
1271 if (!this->dev_ready)
1272 udelay (this->chip_delay);
1274 while (!this->dev_ready(mtd));
1279 /* For subsequent reads align to page boundary. */
1281 /* Increment page address */
1284 page = realpage & this->pagemask;
1285 /* Check, if we cross a chip boundary */
1288 this->select_chip(mtd, -1);
1289 this->select_chip(mtd, chipnr);
1291 /* Check, if the chip supports auto page increment
1292 * or if we have hit a block boundary.
1294 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1298 /* Deselect and wake up anyone waiting on the device */
1299 nand_release_device(mtd);
1302 * Return success, if no ECC failures, else -EBADMSG
1303 * fs driver will take care of that, because
1304 * retlen == desired len and result == -EBADMSG
1307 return ecc_failed ? -EBADMSG : 0;
1311 * nand_read_oob - [MTD Interface] NAND read out-of-band
1312 * @mtd: MTD device structure
1313 * @from: offset to read from
1314 * @len: number of bytes to read
1315 * @retlen: pointer to variable to store the number of read bytes
1316 * @buf: the databuffer to put data
1318 * NAND read out-of-band data from the spare area
1320 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1322 int i, col, page, chipnr;
1323 struct nand_chip *this = mtd->priv;
1324 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1326 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1328 /* Shift to get page */
1329 page = (int)(from >> this->page_shift);
1330 chipnr = (int)(from >> this->chip_shift);
1332 /* Mask to get column */
1333 col = from & (mtd->oobsize - 1);
1335 /* Initialize return length value */
1338 /* Do not allow reads past end of device */
1339 if ((from + len) > mtd->size) {
1340 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n");
1345 /* Grab the lock and see if the device is available */
1346 nand_get_device (this, mtd , FL_READING);
1348 /* Select the NAND device */
1349 this->select_chip(mtd, chipnr);
1351 /* Send the read command */
1352 this->cmdfunc (mtd, NAND_CMD_READOOB, col, page & this->pagemask);
1354 * Read the data, if we read more than one page
1355 * oob data, let the device transfer the data !
1359 int thislen = mtd->oobsize - col;
1360 thislen = min_t(int, thislen, len);
1361 this->read_buf(mtd, &buf[i], thislen);
1364 /* Apply delay or wait for ready/busy pin
1365 * Do this before the AUTOINCR check, so no problems
1366 * arise if a chip which does auto increment
1367 * is marked as NOAUTOINCR by the board driver.
1369 if (!this->dev_ready)
1370 udelay (this->chip_delay);
1372 while (!this->dev_ready(mtd));
1379 /* Check, if we cross a chip boundary */
1380 if (!(page & this->pagemask)) {
1382 this->select_chip(mtd, -1);
1383 this->select_chip(mtd, chipnr);
1386 /* Check, if the chip supports auto page increment
1387 * or if we have hit a block boundary.
1389 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
1390 /* For subsequent page reads set offset to 0 */
1391 this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
1396 /* Deselect and wake up anyone waiting on the device */
1397 nand_release_device(mtd);
1405 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
1406 * @mtd: MTD device structure
1407 * @buf: temporary buffer
1408 * @from: offset to read from
1409 * @len: number of bytes to read
1410 * @ooblen: number of oob data bytes to read
1412 * Read raw data including oob into buffer
1414 int nand_read_raw (struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
1416 struct nand_chip *this = mtd->priv;
1417 int page = (int) (from >> this->page_shift);
1418 int chip = (int) (from >> this->chip_shift);
1421 int pagesize = mtd->oobblock + mtd->oobsize;
1422 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1424 /* Do not allow reads past end of device */
1425 if ((from + len) > mtd->size) {
1426 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n");
1430 /* Grab the lock and see if the device is available */
1431 nand_get_device (this, mtd , FL_READING);
1433 this->select_chip (mtd, chip);
1435 /* Add requested oob length */
1440 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page & this->pagemask);
1443 this->read_buf (mtd, &buf[cnt], pagesize);
1449 if (!this->dev_ready)
1450 udelay (this->chip_delay);
1452 while (!this->dev_ready(mtd));
1454 /* Check, if the chip supports auto page increment */
1455 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1459 /* Deselect and wake up anyone waiting on the device */
1460 nand_release_device(mtd);
1466 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
1467 * @mtd: MTD device structure
1468 * @fsbuf: buffer given by fs driver
1469 * @oobsel: out of band selection structre
1470 * @autoplace: 1 = place given buffer into the oob bytes
1471 * @numpages: number of pages to prepare
1474 * 1. Filesystem buffer available and autoplacement is off,
1475 * return filesystem buffer
1476 * 2. No filesystem buffer or autoplace is off, return internal
1478 * 3. Filesystem buffer is given and autoplace selected
1479 * put data from fs buffer into internal buffer and
1480 * retrun internal buffer
1482 * Note: The internal buffer is filled with 0xff. This must
1483 * be done only once, when no autoplacement happens
1484 * Autoplacement sets the buffer dirty flag, which
1485 * forces the 0xff fill before using the buffer again.
1488 static u_char * nand_prepare_oobbuf (struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel,
1489 int autoplace, int numpages)
1491 struct nand_chip *this = mtd->priv;
1494 /* Zero copy fs supplied buffer */
1495 if (fsbuf && !autoplace)
1498 /* Check, if the buffer must be filled with ff again */
1499 if (this->oobdirty) {
1500 memset (this->oob_buf, 0xff,
1501 mtd->oobsize << (this->phys_erase_shift - this->page_shift));
1505 /* If we have no autoplacement or no fs buffer use the internal one */
1506 if (!autoplace || !fsbuf)
1507 return this->oob_buf;
1509 /* Walk through the pages and place the data */
1512 while (numpages--) {
1513 for (i = 0, len = 0; len < mtd->oobavail; i++) {
1514 int to = ofs + oobsel->oobfree[i][0];
1515 int num = oobsel->oobfree[i][1];
1516 memcpy (&this->oob_buf[to], fsbuf, num);
1520 ofs += mtd->oobavail;
1522 return this->oob_buf;
1525 #define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
1528 * nand_write - [MTD Interface] compability function for nand_write_ecc
1529 * @mtd: MTD device structure
1530 * @to: offset to write to
1531 * @len: number of bytes to write
1532 * @retlen: pointer to variable to store the number of written bytes
1533 * @buf: the data to write
1535 * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
1538 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1540 return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));
1544 * nand_write_ecc - [MTD Interface] NAND write with ECC
1545 * @mtd: MTD device structure
1546 * @to: offset to write to
1547 * @len: number of bytes to write
1548 * @retlen: pointer to variable to store the number of written bytes
1549 * @buf: the data to write
1550 * @eccbuf: filesystem supplied oob data buffer
1551 * @oobsel: oob selection structure
1553 * NAND write with ECC
1555 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
1556 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
1558 int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
1559 int autoplace = 0, numpages, totalpages;
1560 struct nand_chip *this = mtd->priv;
1561 u_char *oobbuf, *bufstart;
1562 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1564 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1566 /* Initialize retlen, in case of early exit */
1569 /* Do not allow write past end of device */
1570 if ((to + len) > mtd->size) {
1571 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n");
1575 /* reject writes, which are not page aligned */
1576 if (NOTALIGNED (to) || NOTALIGNED(len)) {
1577 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1581 /* Grab the lock and see if the device is available */
1582 nand_get_device (this, mtd, FL_WRITING);
1584 /* Calculate chipnr */
1585 chipnr = (int)(to >> this->chip_shift);
1586 /* Select the NAND device */
1587 this->select_chip(mtd, chipnr);
1589 /* Check, if it is write protected */
1590 if (nand_check_wp(mtd))
1593 /* if oobsel is NULL, use chip defaults */
1595 oobsel = &mtd->oobinfo;
1597 /* Autoplace of oob data ? Use the default placement scheme */
1598 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1599 oobsel = this->autooob;
1603 /* Setup variables and oob buffer */
1604 totalpages = len >> this->page_shift;
1605 page = (int) (to >> this->page_shift);
1606 /* Invalidate the page cache, if we write to the cached page */
1607 if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
1610 /* Set it relative to chip */
1611 page &= this->pagemask;
1613 /* Calc number of pages we can write in one go */
1614 numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages);
1615 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages);
1616 bufstart = (u_char *)buf;
1618 /* Loop until all data is written */
1619 while (written < len) {
1621 this->data_poi = (u_char*) &buf[written];
1622 /* Write one page. If this is the last page to write
1623 * or the last page in this block, then use the
1624 * real pageprogram command, else select cached programming
1625 * if supported by the chip.
1627 ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
1629 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret);
1633 oob += mtd->oobsize;
1634 /* Update written bytes count */
1635 written += mtd->oobblock;
1639 /* Increment page address */
1642 /* Have we hit a block boundary ? Then we have to verify and
1643 * if verify is ok, we have to setup the oob buffer for
1646 if (!(page & (ppblock - 1))){
1648 this->data_poi = bufstart;
1649 ret = nand_verify_pages (mtd, this, startpage,
1651 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1653 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1658 ofs = autoplace ? mtd->oobavail : mtd->oobsize;
1660 eccbuf += (page - startpage) * ofs;
1661 totalpages -= page - startpage;
1662 numpages = min (totalpages, ppblock);
1663 page &= this->pagemask;
1665 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel,
1666 autoplace, numpages);
1667 /* Check, if we cross a chip boundary */
1670 this->select_chip(mtd, -1);
1671 this->select_chip(mtd, chipnr);
1675 /* Verify the remaining pages */
1677 this->data_poi = bufstart;
1678 ret = nand_verify_pages (mtd, this, startpage, totalpages,
1679 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1683 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1686 /* Deselect and wake up anyone waiting on the device */
1687 nand_release_device(mtd);
1694 * nand_write_oob - [MTD Interface] NAND write out-of-band
1695 * @mtd: MTD device structure
1696 * @to: offset to write to
1697 * @len: number of bytes to write
1698 * @retlen: pointer to variable to store the number of written bytes
1699 * @buf: the data to write
1701 * NAND write out-of-band
1703 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1705 int column, page, status, ret = -EIO, chipnr;
1706 struct nand_chip *this = mtd->priv;
1708 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1710 /* Shift to get page */
1711 page = (int) (to >> this->page_shift);
1712 chipnr = (int) (to >> this->chip_shift);
1714 /* Mask to get column */
1715 column = to & (mtd->oobsize - 1);
1717 /* Initialize return length value */
1720 /* Do not allow write past end of page */
1721 if ((column + len) > mtd->oobsize) {
1722 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
1726 /* Grab the lock and see if the device is available */
1727 nand_get_device (this, mtd, FL_WRITING);
1729 /* Select the NAND device */
1730 this->select_chip(mtd, chipnr);
1732 /* Reset the chip. Some chips (like the Toshiba TC5832DC found
1733 in one of my DiskOnChip 2000 test units) will clear the whole
1734 data page too if we don't do this. I have no clue why, but
1735 I seem to have 'fixed' it in the doc2000 driver in
1736 August 1999. dwmw2. */
1737 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1739 /* Check, if it is write protected */
1740 if (nand_check_wp(mtd))
1743 /* Invalidate the page cache, if we write to the cached page */
1744 if (page == this->pagebuf)
1747 if (NAND_MUST_PAD(this)) {
1748 /* Write out desired data */
1749 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
1750 /* prepad 0xff for partial programming */
1751 this->write_buf(mtd, ffchars, column);
1753 this->write_buf(mtd, buf, len);
1754 /* postpad 0xff for partial programming */
1755 this->write_buf(mtd, ffchars, mtd->oobsize - (len+column));
1757 /* Write out desired data */
1758 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
1760 this->write_buf(mtd, buf, len);
1762 /* Send command to program the OOB data */
1763 this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1);
1765 status = this->waitfunc (mtd, this, FL_WRITING);
1767 /* See if device thinks it succeeded */
1768 if (status & 0x01) {
1769 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
1776 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1777 /* Send command to read back the data */
1778 this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask);
1780 if (this->verify_buf(mtd, buf, len)) {
1781 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
1788 /* Deselect and wake up anyone waiting on the device */
1789 nand_release_device(mtd);
1796 * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
1797 * @mtd: MTD device structure
1798 * @vecs: the iovectors to write
1799 * @count: number of vectors
1800 * @to: offset to write to
1801 * @retlen: pointer to variable to store the number of written bytes
1803 * NAND write with kvec. This just calls the ecc function
1805 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1806 loff_t to, size_t * retlen)
1808 return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL));
1812 * nand_writev_ecc - [MTD Interface] write with iovec with ecc
1813 * @mtd: MTD device structure
1814 * @vecs: the iovectors to write
1815 * @count: number of vectors
1816 * @to: offset to write to
1817 * @retlen: pointer to variable to store the number of written bytes
1818 * @eccbuf: filesystem supplied oob data buffer
1819 * @oobsel: oob selection structure
1821 * NAND write with iovec with ecc
1823 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1824 loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
1826 int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
1827 int oob, numpages, autoplace = 0, startpage;
1828 struct nand_chip *this = mtd->priv;
1829 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1830 u_char *oobbuf, *bufstart;
1832 /* Preset written len for early exit */
1835 /* Calculate total length of data */
1837 for (i = 0; i < count; i++)
1838 total_len += (int) vecs[i].iov_len;
1840 DEBUG (MTD_DEBUG_LEVEL3,
1841 "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
1843 /* Do not allow write past end of page */
1844 if ((to + total_len) > mtd->size) {
1845 DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
1849 /* reject writes, which are not page aligned */
1850 if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
1851 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1855 /* Grab the lock and see if the device is available */
1856 nand_get_device (this, mtd, FL_WRITING);
1858 /* Get the current chip-nr */
1859 chipnr = (int) (to >> this->chip_shift);
1860 /* Select the NAND device */
1861 this->select_chip(mtd, chipnr);
1863 /* Check, if it is write protected */
1864 if (nand_check_wp(mtd))
1867 /* if oobsel is NULL, use chip defaults */
1869 oobsel = &mtd->oobinfo;
1871 /* Autoplace of oob data ? Use the default placement scheme */
1872 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1873 oobsel = this->autooob;
1877 /* Setup start page */
1878 page = (int) (to >> this->page_shift);
1879 /* Invalidate the page cache, if we write to the cached page */
1880 if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))
1883 startpage = page & this->pagemask;
1885 /* Loop until all kvec' data has been written */
1888 /* If the given tuple is >= pagesize then
1889 * write it out from the iov
1891 if ((vecs->iov_len - len) >= mtd->oobblock) {
1892 /* Calc number of pages we can write
1893 * out of this iov in one go */
1894 numpages = (vecs->iov_len - len) >> this->page_shift;
1895 /* Do not cross block boundaries */
1896 numpages = min (ppblock - (startpage & (ppblock - 1)), numpages);
1897 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1898 bufstart = (u_char *)vecs->iov_base;
1900 this->data_poi = bufstart;
1902 for (i = 1; i <= numpages; i++) {
1903 /* Write one page. If this is the last page to write
1904 * then use the real pageprogram command, else select
1905 * cached programming if supported by the chip.
1907 ret = nand_write_page (mtd, this, page & this->pagemask,
1908 &oobbuf[oob], oobsel, i != numpages);
1911 this->data_poi += mtd->oobblock;
1912 len += mtd->oobblock;
1913 oob += mtd->oobsize;
1916 /* Check, if we have to switch to the next tuple */
1917 if (len >= (int) vecs->iov_len) {
1923 /* We must use the internal buffer, read data out of each
1924 * tuple until we have a full page to write
1927 while (cnt < mtd->oobblock) {
1928 if (vecs->iov_base != NULL && vecs->iov_len)
1929 this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
1930 /* Check, if we have to switch to the next tuple */
1931 if (len >= (int) vecs->iov_len) {
1937 this->pagebuf = page;
1938 this->data_poi = this->data_buf;
1939 bufstart = this->data_poi;
1941 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1942 ret = nand_write_page (mtd, this, page & this->pagemask,
1949 this->data_poi = bufstart;
1950 ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
1954 written += mtd->oobblock * numpages;
1959 startpage = page & this->pagemask;
1960 /* Check, if we cross a chip boundary */
1963 this->select_chip(mtd, -1);
1964 this->select_chip(mtd, chipnr);
1969 /* Deselect and wake up anyone waiting on the device */
1970 nand_release_device(mtd);
1977 * single_erease_cmd - [GENERIC] NAND standard block erase command function
1978 * @mtd: MTD device structure
1979 * @page: the page address of the block which will be erased
1981 * Standard erase command for NAND chips
1983 static void single_erase_cmd (struct mtd_info *mtd, int page)
1985 struct nand_chip *this = mtd->priv;
1986 /* Send commands to erase a block */
1987 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
1988 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
1992 * multi_erease_cmd - [GENERIC] AND specific block erase command function
1993 * @mtd: MTD device structure
1994 * @page: the page address of the block which will be erased
1996 * AND multi block erase command function
1997 * Erase 4 consecutive blocks
1999 static void multi_erase_cmd (struct mtd_info *mtd, int page)
2001 struct nand_chip *this = mtd->priv;
2002 /* Send commands to erase a block */
2003 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2004 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2005 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2006 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2007 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2011 * nand_erase - [MTD Interface] erase block(s)
2012 * @mtd: MTD device structure
2013 * @instr: erase instruction
2015 * Erase one ore more blocks
2017 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
2019 return nand_erase_nand (mtd, instr, 0);
2023 * nand_erase_intern - [NAND Interface] erase block(s)
2024 * @mtd: MTD device structure
2025 * @instr: erase instruction
2026 * @allowbbt: allow erasing the bbt area
2028 * Erase one ore more blocks
2030 int nand_erase_nand (struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
2032 int page, len, status, pages_per_block, ret, chipnr;
2033 struct nand_chip *this = mtd->priv;
2035 DEBUG (MTD_DEBUG_LEVEL3,
2036 "nand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
2038 /* Start address must align on block boundary */
2039 if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
2040 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2044 /* Length must align on block boundary */
2045 if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
2046 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
2050 /* Do not allow erase past end of device */
2051 if ((instr->len + instr->addr) > mtd->size) {
2052 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
2056 instr->fail_addr = 0xffffffff;
2058 /* Grab the lock and see if the device is available */
2059 nand_get_device (this, mtd, FL_ERASING);
2061 /* Shift to get first page */
2062 page = (int) (instr->addr >> this->page_shift);
2063 chipnr = (int) (instr->addr >> this->chip_shift);
2065 /* Calculate pages in each block */
2066 pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
2068 /* Select the NAND device */
2069 this->select_chip(mtd, chipnr);
2071 /* Check the WP bit */
2072 /* Check, if it is write protected */
2073 if (nand_check_wp(mtd)) {
2074 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
2075 instr->state = MTD_ERASE_FAILED;
2079 /* Loop through the pages */
2082 instr->state = MTD_ERASING;
2085 /* Check if we have a bad block, we do not erase bad blocks ! */
2086 if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
2087 printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
2088 instr->state = MTD_ERASE_FAILED;
2092 /* Invalidate the page cache, if we erase the block which contains
2093 the current cached page */
2094 if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
2097 this->erase_cmd (mtd, page & this->pagemask);
2099 status = this->waitfunc (mtd, this, FL_ERASING);
2101 /* See if block erase succeeded */
2102 if (status & 0x01) {
2103 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
2104 instr->state = MTD_ERASE_FAILED;
2105 instr->fail_addr = (page << this->page_shift);
2109 /* Increment page address and decrement length */
2110 len -= (1 << this->phys_erase_shift);
2111 page += pages_per_block;
2113 /* Check, if we cross a chip boundary */
2114 if (len && !(page & this->pagemask)) {
2116 this->select_chip(mtd, -1);
2117 this->select_chip(mtd, chipnr);
2120 instr->state = MTD_ERASE_DONE;
2124 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2125 /* Do call back function */
2127 mtd_erase_callback(instr);
2129 /* Deselect and wake up anyone waiting on the device */
2130 nand_release_device(mtd);
2132 /* Return more or less happy */
2137 * nand_sync - [MTD Interface] sync
2138 * @mtd: MTD device structure
2140 * Sync is actually a wait for chip ready function
2142 static void nand_sync (struct mtd_info *mtd)
2144 struct nand_chip *this = mtd->priv;
2146 DEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2148 /* Grab the lock and see if the device is available */
2149 nand_get_device (this, mtd, FL_SYNCING);
2150 /* Release it and go back */
2151 nand_release_device (mtd);
2156 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2157 * @mtd: MTD device structure
2158 * @ofs: offset relative to mtd start
2160 static int nand_block_isbad (struct mtd_info *mtd, loff_t ofs)
2162 /* Check for invalid offset */
2163 if (ofs > mtd->size)
2166 return nand_block_checkbad (mtd, ofs, 1, 0);
2170 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2171 * @mtd: MTD device structure
2172 * @ofs: offset relative to mtd start
2174 static int nand_block_markbad (struct mtd_info *mtd, loff_t ofs)
2176 struct nand_chip *this = mtd->priv;
2179 if ((ret = nand_block_isbad(mtd, ofs))) {
2180 /* If it was bad already, return success and do nothing. */
2186 return this->block_markbad(mtd, ofs);
2190 * nand_scan - [NAND Interface] Scan for the NAND device
2191 * @mtd: MTD device structure
2192 * @maxchips: Number of chips to scan for
2194 * This fills out all the not initialized function pointers
2195 * with the defaults.
2196 * The flash ID is read and the mtd/chip structures are
2197 * filled with the appropriate values. Buffers are allocated if
2198 * they are not provided by the board driver
2201 int nand_scan (struct mtd_info *mtd, int maxchips)
2203 int i, j, nand_maf_id, nand_dev_id, busw;
2204 struct nand_chip *this = mtd->priv;
2206 /* Get buswidth to select the correct functions*/
2207 busw = this->options & NAND_BUSWIDTH_16;
2209 /* check for proper chip_delay setup, set 20us if not */
2210 if (!this->chip_delay)
2211 this->chip_delay = 20;
2213 /* check, if a user supplied command function given */
2214 if (this->cmdfunc == NULL)
2215 this->cmdfunc = nand_command;
2217 /* check, if a user supplied wait function given */
2218 if (this->waitfunc == NULL)
2219 this->waitfunc = nand_wait;
2221 if (!this->select_chip)
2222 this->select_chip = nand_select_chip;
2223 if (!this->write_byte)
2224 this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2225 if (!this->read_byte)
2226 this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2227 if (!this->write_word)
2228 this->write_word = nand_write_word;
2229 if (!this->read_word)
2230 this->read_word = nand_read_word;
2231 if (!this->block_bad)
2232 this->block_bad = nand_block_bad;
2233 if (!this->block_markbad)
2234 this->block_markbad = nand_default_block_markbad;
2235 if (!this->write_buf)
2236 this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2237 if (!this->read_buf)
2238 this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2239 if (!this->verify_buf)
2240 this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2241 if (!this->scan_bbt)
2242 this->scan_bbt = nand_default_bbt;
2244 /* Select the device */
2245 this->select_chip(mtd, 0);
2247 /* Send the command for reading device ID */
2248 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2250 /* Read manufacturer and device IDs */
2251 nand_maf_id = this->read_byte(mtd);
2252 nand_dev_id = this->read_byte(mtd);
2254 /* Print and store flash device information */
2255 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2257 if (nand_dev_id != nand_flash_ids[i].id)
2260 if (!mtd->name) mtd->name = nand_flash_ids[i].name;
2261 this->chipsize = nand_flash_ids[i].chipsize << 20;
2263 /* New devices have all the information in additional id bytes */
2264 if (!nand_flash_ids[i].pagesize) {
2266 /* The 3rd id byte contains non relevant data ATM */
2267 extid = this->read_byte(mtd);
2268 /* The 4th id byte is the important one */
2269 extid = this->read_byte(mtd);
2271 mtd->oobblock = 1024 << (extid & 0x3);
2274 mtd->oobsize = (8 << (extid & 0x03)) * (mtd->oobblock / 512);
2276 /* Calc blocksize. Blocksize is multiples of 64KiB */
2277 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2279 /* Get buswidth information */
2280 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2283 /* Old devices have this data hardcoded in the
2284 * device id table */
2285 mtd->erasesize = nand_flash_ids[i].erasesize;
2286 mtd->oobblock = nand_flash_ids[i].pagesize;
2287 mtd->oobsize = mtd->oobblock / 32;
2288 busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
2291 /* Check, if buswidth is correct. Hardware drivers should set
2293 if (busw != (this->options & NAND_BUSWIDTH_16)) {
2294 printk (KERN_INFO "NAND device: Manufacturer ID:"
2295 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2296 nand_manuf_ids[i].name , mtd->name);
2297 printk (KERN_WARNING
2298 "NAND bus width %d instead %d bit\n",
2299 (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
2301 this->select_chip(mtd, -1);
2305 /* Calculate the address shift from the page size */
2306 this->page_shift = ffs(mtd->oobblock) - 1;
2307 this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
2308 this->chip_shift = ffs(this->chipsize) - 1;
2310 /* Set the bad block position */
2311 this->badblockpos = mtd->oobblock > 512 ?
2312 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2314 /* Get chip options, preserve non chip based options */
2315 this->options &= ~NAND_CHIPOPTIONS_MSK;
2316 this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
2317 /* Set this as a default. Board drivers can override it, if neccecary */
2318 this->options |= NAND_NO_AUTOINCR;
2319 /* Check if this is a not a samsung device. Do not clear the options
2320 * for chips which are not having an extended id.
2322 if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
2323 this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2325 /* Check for AND chips with 4 page planes */
2326 if (this->options & NAND_4PAGE_ARRAY)
2327 this->erase_cmd = multi_erase_cmd;
2329 this->erase_cmd = single_erase_cmd;
2331 /* Do not replace user supplied command function ! */
2332 if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
2333 this->cmdfunc = nand_command_lp;
2335 /* Try to identify manufacturer */
2336 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
2337 if (nand_manuf_ids[j].id == nand_maf_id)
2340 printk (KERN_INFO "NAND device: Manufacturer ID:"
2341 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2342 nand_manuf_ids[j].name , nand_flash_ids[i].name);
2346 if (!nand_flash_ids[i].name) {
2347 printk (KERN_WARNING "No NAND device found!!!\n");
2348 this->select_chip(mtd, -1);
2352 for (i=1; i < maxchips; i++) {
2353 this->select_chip(mtd, i);
2355 /* Send the command for reading device ID */
2356 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2358 /* Read manufacturer and device IDs */
2359 if (nand_maf_id != this->read_byte(mtd) ||
2360 nand_dev_id != this->read_byte(mtd))
2364 printk(KERN_INFO "%d NAND chips detected\n", i);
2366 /* Allocate buffers, if neccecary */
2367 if (!this->oob_buf) {
2369 len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
2370 this->oob_buf = kmalloc (len, GFP_KERNEL);
2371 if (!this->oob_buf) {
2372 printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
2375 this->options |= NAND_OOBBUF_ALLOC;
2378 if (!this->data_buf) {
2380 len = mtd->oobblock + mtd->oobsize;
2381 this->data_buf = kmalloc (len, GFP_KERNEL);
2382 if (!this->data_buf) {
2383 if (this->options & NAND_OOBBUF_ALLOC)
2384 kfree (this->oob_buf);
2385 printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
2388 this->options |= NAND_DATABUF_ALLOC;
2391 /* Store the number of chips and calc total size for mtd */
2393 mtd->size = i * this->chipsize;
2394 /* Convert chipsize to number of pages per chip -1. */
2395 this->pagemask = (this->chipsize >> this->page_shift) - 1;
2396 /* Preset the internal oob buffer */
2397 memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
2399 /* If no default placement scheme is given, select an
2400 * appropriate one */
2401 if (!this->autooob) {
2402 /* Select the appropriate default oob placement scheme for
2403 * placement agnostic filesystems */
2404 switch (mtd->oobsize) {
2406 this->autooob = &nand_oob_8;
2409 this->autooob = &nand_oob_16;
2412 this->autooob = &nand_oob_64;
2415 printk (KERN_WARNING "No oob scheme defined for oobsize %d\n",
2421 /* The number of bytes available for the filesystem to place fs dependend
2423 if (this->options & NAND_BUSWIDTH_16) {
2424 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 2);
2425 if (this->autooob->eccbytes & 0x01)
2428 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 1);
2431 * check ECC mode, default to software
2432 * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
2433 * fallback to software ECC
2435 this->eccsize = 256; /* set default eccsize */
2437 switch (this->eccmode) {
2439 case NAND_ECC_HW3_512:
2440 case NAND_ECC_HW6_512:
2441 case NAND_ECC_HW8_512:
2442 if (mtd->oobblock == 256) {
2443 printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
2444 this->eccmode = NAND_ECC_SOFT;
2445 this->calculate_ecc = nand_calculate_ecc;
2446 this->correct_data = nand_correct_data;
2449 this->eccsize = 512; /* set eccsize to 512 and fall through for function check */
2451 case NAND_ECC_HW3_256:
2452 if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
2454 printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
2458 printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
2459 this->eccmode = NAND_ECC_NONE;
2463 this->calculate_ecc = nand_calculate_ecc;
2464 this->correct_data = nand_correct_data;
2468 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
2472 mtd->eccsize = this->eccsize;
2474 /* Set the number of read / write steps for one page to ensure ECC generation */
2475 switch (this->eccmode) {
2476 case NAND_ECC_HW3_512:
2477 case NAND_ECC_HW6_512:
2478 case NAND_ECC_HW8_512:
2479 this->eccsteps = mtd->oobblock / 512;
2481 case NAND_ECC_HW3_256:
2483 this->eccsteps = mtd->oobblock / 256;
2491 /* Initialize state, waitqueue and spinlock */
2492 this->state = FL_READY;
2493 init_waitqueue_head (&this->wq);
2494 spin_lock_init (&this->chip_lock);
2496 /* De-select the device */
2497 this->select_chip(mtd, -1);
2499 /* Invalidate the pagebuffer reference */
2502 /* Fill in remaining MTD driver data */
2503 mtd->type = MTD_NANDFLASH;
2504 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
2505 mtd->ecctype = MTD_ECC_SW;
2506 mtd->erase = nand_erase;
2508 mtd->unpoint = NULL;
2509 mtd->read = nand_read;
2510 mtd->write = nand_write;
2511 mtd->read_ecc = nand_read_ecc;
2512 mtd->write_ecc = nand_write_ecc;
2513 mtd->read_oob = nand_read_oob;
2514 mtd->write_oob = nand_write_oob;
2516 mtd->writev = nand_writev;
2517 mtd->writev_ecc = nand_writev_ecc;
2518 mtd->sync = nand_sync;
2521 mtd->suspend = NULL;
2523 mtd->block_isbad = nand_block_isbad;
2524 mtd->block_markbad = nand_block_markbad;
2526 /* and make the autooob the default one */
2527 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
2529 mtd->owner = THIS_MODULE;
2531 /* Build bad block table */
2532 return this->scan_bbt (mtd);
2536 * nand_release - [NAND Interface] Free resources held by the NAND device
2537 * @mtd: MTD device structure
2539 void nand_release (struct mtd_info *mtd)
2541 struct nand_chip *this = mtd->priv;
2543 #ifdef CONFIG_MTD_PARTITIONS
2544 /* Deregister partitions */
2545 del_mtd_partitions (mtd);
2547 /* Deregister the device */
2548 del_mtd_device (mtd);
2550 /* Free bad block table memory, if allocated */
2553 /* Buffer allocated by nand_scan ? */
2554 if (this->options & NAND_OOBBUF_ALLOC)
2555 kfree (this->oob_buf);
2556 /* Buffer allocated by nand_scan ? */
2557 if (this->options & NAND_DATABUF_ALLOC)
2558 kfree (this->data_buf);
2561 EXPORT_SYMBOL (nand_scan);
2562 EXPORT_SYMBOL (nand_release);
2564 MODULE_LICENSE ("GPL");
2565 MODULE_AUTHOR ("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2566 MODULE_DESCRIPTION ("Generic NAND flash driver code");