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
29 * patch from Ben Dooks <ben-mtd@fluff.org>
31 * 12-05-2004 dmarlin: add workaround for Renesas AG-AND chips "disturb"
32 * issue. Basically, any block not rewritten may lose data when
33 * surrounding blocks are rewritten many times. JFFS2 ensures
34 * this doesn't happen for blocks it uses, but the Bad Block
35 * Table(s) may not be rewritten. To ensure they do not lose
36 * data, force them to be rewritten when some of the surrounding
37 * blocks are erased. Rather than tracking a specific nearby
38 * block (which could itself go bad), use a page address 'mask' to
39 * select several blocks in the same area, and rewrite the BBT
40 * when any of them are erased.
42 * 01-03-2005 dmarlin: added support for the device recovery command sequence
43 * for Renesas AG-AND chips. If there was a sudden loss of power
44 * during an erase operation, a "device recovery" operation must
45 * be performed when power is restored to ensure correct
48 * 01-20-2005 dmarlin: added support for optional hardware specific callback
49 * routine to perform extra error status checks on erase and write
50 * failures. This required adding a wrapper function for
53 * 08-20-2005 vwool: suspend/resume added
56 * David Woodhouse for adding multichip support
58 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
59 * rework for 2K page size chips
62 * Enable cached programming for 2k page size chips
63 * Check, if mtd->ecctype should be set to MTD_ECC_HW
64 * if we have HW ecc support.
65 * The AG-AND chips have nice features for speed improvement,
66 * which are not supported yet. Read / program 4 pages in one go.
68 * $Id: nand_base.c,v 1.150 2005/09/15 13:58:48 vwool Exp $
70 * This program is free software; you can redistribute it and/or modify
71 * it under the terms of the GNU General Public License version 2 as
72 * published by the Free Software Foundation.
76 #include <linux/module.h>
77 #include <linux/delay.h>
78 #include <linux/errno.h>
79 #include <linux/sched.h>
80 #include <linux/slab.h>
81 #include <linux/types.h>
82 #include <linux/mtd/mtd.h>
83 #include <linux/mtd/nand.h>
84 #include <linux/mtd/nand_ecc.h>
85 #include <linux/mtd/compatmac.h>
86 #include <linux/interrupt.h>
87 #include <linux/bitops.h>
88 #include <linux/leds.h>
91 #ifdef CONFIG_MTD_PARTITIONS
92 #include <linux/mtd/partitions.h>
95 /* Define default oob placement schemes for large and small page devices */
96 static struct nand_oobinfo nand_oob_8 = {
97 .useecc = MTD_NANDECC_AUTOPLACE,
100 .oobfree = {{3, 2}, {6, 2}}
103 static struct nand_oobinfo nand_oob_16 = {
104 .useecc = MTD_NANDECC_AUTOPLACE,
106 .eccpos = {0, 1, 2, 3, 6, 7},
110 static struct nand_oobinfo nand_oob_64 = {
111 .useecc = MTD_NANDECC_AUTOPLACE,
114 40, 41, 42, 43, 44, 45, 46, 47,
115 48, 49, 50, 51, 52, 53, 54, 55,
116 56, 57, 58, 59, 60, 61, 62, 63},
120 /* This is used for padding purposes in nand_write_oob */
121 static uint8_t ffchars[] = {
122 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
123 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
124 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
125 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
126 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
127 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
128 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
129 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
133 * NAND low-level MTD interface functions
135 static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len);
136 static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len);
137 static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len);
139 static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
140 size_t *retlen, uint8_t *buf);
141 static int nand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
142 size_t *retlen, uint8_t *buf, uint8_t *eccbuf,
143 struct nand_oobinfo *oobsel);
144 static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
145 size_t *retlen, uint8_t *buf);
146 static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
147 size_t *retlen, const uint8_t *buf);
148 static int nand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
149 size_t *retlen, const uint8_t *buf, uint8_t *eccbuf,
150 struct nand_oobinfo *oobsel);
151 static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
152 size_t *retlen, const uint8_t *buf);
153 static int nand_writev(struct mtd_info *mtd, const struct kvec *vecs,
154 unsigned long count, loff_t to, size_t *retlen);
155 static int nand_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
156 unsigned long count, loff_t to, size_t *retlen,
157 uint8_t *eccbuf, struct nand_oobinfo *oobsel);
158 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr);
159 static void nand_sync(struct mtd_info *mtd);
161 /* Some internal functions */
162 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *this,
163 int page, uint8_t * oob_buf,
164 struct nand_oobinfo *oobsel, int mode);
165 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
166 static int nand_verify_pages(struct mtd_info *mtd, struct nand_chip *this,
167 int page, int numpages, uint8_t *oob_buf,
168 struct nand_oobinfo *oobsel, int chipnr,
171 #define nand_verify_pages(...) (0)
174 static int nand_get_device(struct nand_chip *this, struct mtd_info *mtd,
178 * nand_release_device - [GENERIC] release chip
179 * @mtd: MTD device structure
181 * Deselect, release chip lock and wake up anyone waiting on the device
183 static void nand_release_device(struct mtd_info *mtd)
185 struct nand_chip *this = mtd->priv;
187 /* De-select the NAND device */
188 this->select_chip(mtd, -1);
190 /* Release the controller and the chip */
191 spin_lock(&this->controller->lock);
192 this->controller->active = NULL;
193 this->state = FL_READY;
194 wake_up(&this->controller->wq);
195 spin_unlock(&this->controller->lock);
199 * nand_read_byte - [DEFAULT] read one byte from the chip
200 * @mtd: MTD device structure
202 * Default read function for 8bit buswith
204 static uint8_t nand_read_byte(struct mtd_info *mtd)
206 struct nand_chip *this = mtd->priv;
207 return readb(this->IO_ADDR_R);
211 * nand_write_byte - [DEFAULT] write one byte to the chip
212 * @mtd: MTD device structure
213 * @byte: pointer to data byte to write
215 * Default write function for 8it buswith
217 static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
219 struct nand_chip *this = mtd->priv;
220 writeb(byte, this->IO_ADDR_W);
224 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
225 * @mtd: MTD device structure
227 * Default read function for 16bit buswith with
228 * endianess conversion
230 static uint8_t nand_read_byte16(struct mtd_info *mtd)
232 struct nand_chip *this = mtd->priv;
233 return (uint8_t) cpu_to_le16(readw(this->IO_ADDR_R));
237 * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
238 * @mtd: MTD device structure
239 * @byte: pointer to data byte to write
241 * Default write function for 16bit buswith with
242 * endianess conversion
244 static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
246 struct nand_chip *this = mtd->priv;
247 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
251 * nand_read_word - [DEFAULT] read one word from the chip
252 * @mtd: MTD device structure
254 * Default read function for 16bit buswith without
255 * endianess conversion
257 static u16 nand_read_word(struct mtd_info *mtd)
259 struct nand_chip *this = mtd->priv;
260 return readw(this->IO_ADDR_R);
264 * nand_write_word - [DEFAULT] write one word to the chip
265 * @mtd: MTD device structure
266 * @word: data word to write
268 * Default write function for 16bit buswith without
269 * endianess conversion
271 static void nand_write_word(struct mtd_info *mtd, u16 word)
273 struct nand_chip *this = mtd->priv;
274 writew(word, this->IO_ADDR_W);
278 * nand_select_chip - [DEFAULT] control CE line
279 * @mtd: MTD device structure
280 * @chip: chipnumber to select, -1 for deselect
282 * Default select function for 1 chip devices.
284 static void nand_select_chip(struct mtd_info *mtd, int chip)
286 struct nand_chip *this = mtd->priv;
289 this->hwcontrol(mtd, NAND_CTL_CLRNCE);
292 this->hwcontrol(mtd, NAND_CTL_SETNCE);
301 * nand_write_buf - [DEFAULT] write buffer to chip
302 * @mtd: MTD device structure
304 * @len: number of bytes to write
306 * Default write function for 8bit buswith
308 static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
311 struct nand_chip *this = mtd->priv;
313 for (i = 0; i < len; i++)
314 writeb(buf[i], this->IO_ADDR_W);
318 * nand_read_buf - [DEFAULT] read chip data into buffer
319 * @mtd: MTD device structure
320 * @buf: buffer to store date
321 * @len: number of bytes to read
323 * Default read function for 8bit buswith
325 static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
328 struct nand_chip *this = mtd->priv;
330 for (i = 0; i < len; i++)
331 buf[i] = readb(this->IO_ADDR_R);
335 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
336 * @mtd: MTD device structure
337 * @buf: buffer containing the data to compare
338 * @len: number of bytes to compare
340 * Default verify function for 8bit buswith
342 static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
345 struct nand_chip *this = mtd->priv;
347 for (i = 0; i < len; i++)
348 if (buf[i] != readb(this->IO_ADDR_R))
355 * nand_write_buf16 - [DEFAULT] write buffer to chip
356 * @mtd: MTD device structure
358 * @len: number of bytes to write
360 * Default write function for 16bit buswith
362 static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
365 struct nand_chip *this = mtd->priv;
366 u16 *p = (u16 *) buf;
369 for (i = 0; i < len; i++)
370 writew(p[i], this->IO_ADDR_W);
375 * nand_read_buf16 - [DEFAULT] read chip data into buffer
376 * @mtd: MTD device structure
377 * @buf: buffer to store date
378 * @len: number of bytes to read
380 * Default read function for 16bit buswith
382 static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
385 struct nand_chip *this = mtd->priv;
386 u16 *p = (u16 *) buf;
389 for (i = 0; i < len; i++)
390 p[i] = readw(this->IO_ADDR_R);
394 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
395 * @mtd: MTD device structure
396 * @buf: buffer containing the data to compare
397 * @len: number of bytes to compare
399 * Default verify function for 16bit buswith
401 static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
404 struct nand_chip *this = mtd->priv;
405 u16 *p = (u16 *) buf;
408 for (i = 0; i < len; i++)
409 if (p[i] != readw(this->IO_ADDR_R))
416 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
417 * @mtd: MTD device structure
418 * @ofs: offset from device start
419 * @getchip: 0, if the chip is already selected
421 * Check, if the block is bad.
423 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
425 int page, chipnr, res = 0;
426 struct nand_chip *this = mtd->priv;
430 page = (int)(ofs >> this->page_shift);
431 chipnr = (int)(ofs >> this->chip_shift);
433 /* Grab the lock and see if the device is available */
434 nand_get_device(this, mtd, FL_READING);
436 /* Select the NAND device */
437 this->select_chip(mtd, chipnr);
441 if (this->options & NAND_BUSWIDTH_16) {
442 this->cmdfunc(mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE,
443 page & this->pagemask);
444 bad = cpu_to_le16(this->read_word(mtd));
445 if (this->badblockpos & 0x1)
447 if ((bad & 0xFF) != 0xff)
450 this->cmdfunc(mtd, NAND_CMD_READOOB, this->badblockpos,
451 page & this->pagemask);
452 if (this->read_byte(mtd) != 0xff)
457 /* Deselect and wake up anyone waiting on the device */
458 nand_release_device(mtd);
465 * nand_default_block_markbad - [DEFAULT] mark a block bad
466 * @mtd: MTD device structure
467 * @ofs: offset from device start
469 * This is the default implementation, which can be overridden by
470 * a hardware specific driver.
472 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
474 struct nand_chip *this = mtd->priv;
475 uint8_t buf[2] = { 0, 0 };
479 /* Get block number */
480 block = ((int)ofs) >> this->bbt_erase_shift;
482 this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
484 /* Do we have a flash based bad block table ? */
485 if (this->options & NAND_USE_FLASH_BBT)
486 return nand_update_bbt(mtd, ofs);
488 /* We write two bytes, so we dont have to mess with 16 bit access */
489 ofs += mtd->oobsize + (this->badblockpos & ~0x01);
490 return nand_write_oob(mtd, ofs, 2, &retlen, buf);
494 * nand_check_wp - [GENERIC] check if the chip is write protected
495 * @mtd: MTD device structure
496 * Check, if the device is write protected
498 * The function expects, that the device is already selected
500 static int nand_check_wp(struct mtd_info *mtd)
502 struct nand_chip *this = mtd->priv;
503 /* Check the WP bit */
504 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
505 return (this->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
509 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
510 * @mtd: MTD device structure
511 * @ofs: offset from device start
512 * @getchip: 0, if the chip is already selected
513 * @allowbbt: 1, if its allowed to access the bbt area
515 * Check, if the block is bad. Either by reading the bad block table or
516 * calling of the scan function.
518 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
521 struct nand_chip *this = mtd->priv;
524 return this->block_bad(mtd, ofs, getchip);
526 /* Return info from the table */
527 return nand_isbad_bbt(mtd, ofs, allowbbt);
530 DEFINE_LED_TRIGGER(nand_led_trigger);
533 * Wait for the ready pin, after a command
534 * The timeout is catched later.
536 static void nand_wait_ready(struct mtd_info *mtd)
538 struct nand_chip *this = mtd->priv;
539 unsigned long timeo = jiffies + 2;
541 led_trigger_event(nand_led_trigger, LED_FULL);
542 /* wait until command is processed or timeout occures */
544 if (this->dev_ready(mtd))
546 touch_softlockup_watchdog();
547 } while (time_before(jiffies, timeo));
548 led_trigger_event(nand_led_trigger, LED_OFF);
552 * nand_command - [DEFAULT] Send command to NAND device
553 * @mtd: MTD device structure
554 * @command: the command to be sent
555 * @column: the column address for this command, -1 if none
556 * @page_addr: the page address for this command, -1 if none
558 * Send command to NAND device. This function is used for small page
559 * devices (256/512 Bytes per page)
561 static void nand_command(struct mtd_info *mtd, unsigned command, int column,
564 register struct nand_chip *this = mtd->priv;
566 /* Begin command latch cycle */
567 this->hwcontrol(mtd, NAND_CTL_SETCLE);
569 * Write out the command to the device.
571 if (command == NAND_CMD_SEQIN) {
574 if (column >= mtd->oobblock) {
576 column -= mtd->oobblock;
577 readcmd = NAND_CMD_READOOB;
578 } else if (column < 256) {
579 /* First 256 bytes --> READ0 */
580 readcmd = NAND_CMD_READ0;
583 readcmd = NAND_CMD_READ1;
585 this->write_byte(mtd, readcmd);
587 this->write_byte(mtd, command);
589 /* Set ALE and clear CLE to start address cycle */
590 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
592 if (column != -1 || page_addr != -1) {
593 this->hwcontrol(mtd, NAND_CTL_SETALE);
595 /* Serially input address */
597 /* Adjust columns for 16 bit buswidth */
598 if (this->options & NAND_BUSWIDTH_16)
600 this->write_byte(mtd, column);
602 if (page_addr != -1) {
603 this->write_byte(mtd, (uint8_t)(page_addr & 0xff));
604 this->write_byte(mtd, (uint8_t)((page_addr >> 8) & 0xff));
605 /* One more address cycle for devices > 32MiB */
606 if (this->chipsize > (32 << 20))
607 this->write_byte(mtd, (uint8_t)((page_addr >> 16) & 0x0f));
609 /* Latch in address */
610 this->hwcontrol(mtd, NAND_CTL_CLRALE);
614 * program and erase have their own busy handlers
615 * status and sequential in needs no delay
619 case NAND_CMD_PAGEPROG:
620 case NAND_CMD_ERASE1:
621 case NAND_CMD_ERASE2:
623 case NAND_CMD_STATUS:
629 udelay(this->chip_delay);
630 this->hwcontrol(mtd, NAND_CTL_SETCLE);
631 this->write_byte(mtd, NAND_CMD_STATUS);
632 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
633 while (!(this->read_byte(mtd) & NAND_STATUS_READY)) ;
636 /* This applies to read commands */
639 * If we don't have access to the busy pin, we apply the given
642 if (!this->dev_ready) {
643 udelay(this->chip_delay);
647 /* Apply this short delay always to ensure that we do wait tWB in
648 * any case on any machine. */
651 nand_wait_ready(mtd);
655 * nand_command_lp - [DEFAULT] Send command to NAND large page device
656 * @mtd: MTD device structure
657 * @command: the command to be sent
658 * @column: the column address for this command, -1 if none
659 * @page_addr: the page address for this command, -1 if none
661 * Send command to NAND device. This is the version for the new large page devices
662 * We dont have the separate regions as we have in the small page devices.
663 * We must emulate NAND_CMD_READOOB to keep the code compatible.
666 static void nand_command_lp(struct mtd_info *mtd, unsigned command, int column, int page_addr)
668 register struct nand_chip *this = mtd->priv;
670 /* Emulate NAND_CMD_READOOB */
671 if (command == NAND_CMD_READOOB) {
672 column += mtd->oobblock;
673 command = NAND_CMD_READ0;
676 /* Begin command latch cycle */
677 this->hwcontrol(mtd, NAND_CTL_SETCLE);
678 /* Write out the command to the device. */
679 this->write_byte(mtd, (command & 0xff));
680 /* End command latch cycle */
681 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
683 if (column != -1 || page_addr != -1) {
684 this->hwcontrol(mtd, NAND_CTL_SETALE);
686 /* Serially input address */
688 /* Adjust columns for 16 bit buswidth */
689 if (this->options & NAND_BUSWIDTH_16)
691 this->write_byte(mtd, column & 0xff);
692 this->write_byte(mtd, column >> 8);
694 if (page_addr != -1) {
695 this->write_byte(mtd, (uint8_t)(page_addr & 0xff));
696 this->write_byte(mtd, (uint8_t)((page_addr >> 8) & 0xff));
697 /* One more address cycle for devices > 128MiB */
698 if (this->chipsize > (128 << 20))
699 this->write_byte(mtd, (uint8_t)((page_addr >> 16) & 0xff));
701 /* Latch in address */
702 this->hwcontrol(mtd, NAND_CTL_CLRALE);
706 * program and erase have their own busy handlers
707 * status, sequential in, and deplete1 need no delay
711 case NAND_CMD_CACHEDPROG:
712 case NAND_CMD_PAGEPROG:
713 case NAND_CMD_ERASE1:
714 case NAND_CMD_ERASE2:
716 case NAND_CMD_STATUS:
717 case NAND_CMD_DEPLETE1:
721 * read error status commands require only a short delay
723 case NAND_CMD_STATUS_ERROR:
724 case NAND_CMD_STATUS_ERROR0:
725 case NAND_CMD_STATUS_ERROR1:
726 case NAND_CMD_STATUS_ERROR2:
727 case NAND_CMD_STATUS_ERROR3:
728 udelay(this->chip_delay);
734 udelay(this->chip_delay);
735 this->hwcontrol(mtd, NAND_CTL_SETCLE);
736 this->write_byte(mtd, NAND_CMD_STATUS);
737 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
738 while (!(this->read_byte(mtd) & NAND_STATUS_READY)) ;
742 /* Begin command latch cycle */
743 this->hwcontrol(mtd, NAND_CTL_SETCLE);
744 /* Write out the start read command */
745 this->write_byte(mtd, NAND_CMD_READSTART);
746 /* End command latch cycle */
747 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
748 /* Fall through into ready check */
750 /* This applies to read commands */
753 * If we don't have access to the busy pin, we apply the given
756 if (!this->dev_ready) {
757 udelay(this->chip_delay);
762 /* Apply this short delay always to ensure that we do wait tWB in
763 * any case on any machine. */
766 nand_wait_ready(mtd);
770 * nand_get_device - [GENERIC] Get chip for selected access
771 * @this: the nand chip descriptor
772 * @mtd: MTD device structure
773 * @new_state: the state which is requested
775 * Get the device and lock it for exclusive access
778 nand_get_device(struct nand_chip *this, struct mtd_info *mtd, int new_state)
780 spinlock_t *lock = &this->controller->lock;
781 wait_queue_head_t *wq = &this->controller->wq;
782 DECLARE_WAITQUEUE(wait, current);
786 /* Hardware controller shared among independend devices */
787 /* Hardware controller shared among independend devices */
788 if (!this->controller->active)
789 this->controller->active = this;
791 if (this->controller->active == this && this->state == FL_READY) {
792 this->state = new_state;
796 if (new_state == FL_PM_SUSPENDED) {
798 return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
800 set_current_state(TASK_UNINTERRUPTIBLE);
801 add_wait_queue(wq, &wait);
804 remove_wait_queue(wq, &wait);
809 * nand_wait - [DEFAULT] wait until the command is done
810 * @mtd: MTD device structure
811 * @this: NAND chip structure
812 * @state: state to select the max. timeout value
814 * Wait for command done. This applies to erase and program only
815 * Erase can take up to 400ms and program up to 20ms according to
816 * general NAND and SmartMedia specs
819 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
822 unsigned long timeo = jiffies;
825 if (state == FL_ERASING)
826 timeo += (HZ * 400) / 1000;
828 timeo += (HZ * 20) / 1000;
830 led_trigger_event(nand_led_trigger, LED_FULL);
832 /* Apply this short delay always to ensure that we do wait tWB in
833 * any case on any machine. */
836 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
837 this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
839 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
841 while (time_before(jiffies, timeo)) {
842 /* Check, if we were interrupted */
843 if (this->state != state)
846 if (this->dev_ready) {
847 if (this->dev_ready(mtd))
850 if (this->read_byte(mtd) & NAND_STATUS_READY)
855 led_trigger_event(nand_led_trigger, LED_OFF);
857 status = (int)this->read_byte(mtd);
862 * nand_write_page - [GENERIC] write one page
863 * @mtd: MTD device structure
864 * @this: NAND chip structure
865 * @page: startpage inside the chip, must be called with (page & this->pagemask)
866 * @oob_buf: out of band data buffer
867 * @oobsel: out of band selecttion structre
868 * @cached: 1 = enable cached programming if supported by chip
870 * Nand_page_program function is used for write and writev !
871 * This function will always program a full page of data
872 * If you call it with a non page aligned buffer, you're lost :)
874 * Cached programming is not supported yet.
876 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *this, int page,
877 uint8_t *oob_buf, struct nand_oobinfo *oobsel, int cached)
880 uint8_t ecc_code[32];
881 int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
882 int *oob_config = oobsel->eccpos;
883 int datidx = 0, eccidx = 0, eccsteps = this->eccsteps;
886 /* FIXME: Enable cached programming */
889 /* Send command to begin auto page programming */
890 this->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
892 /* Write out complete page of data, take care of eccmode */
894 /* No ecc, write all */
896 printk(KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
897 this->write_buf(mtd, this->data_poi, mtd->oobblock);
900 /* Software ecc 3/256, write all */
902 for (; eccsteps; eccsteps--) {
903 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
904 for (i = 0; i < 3; i++, eccidx++)
905 oob_buf[oob_config[eccidx]] = ecc_code[i];
906 datidx += this->eccsize;
908 this->write_buf(mtd, this->data_poi, mtd->oobblock);
911 eccbytes = this->eccbytes;
912 for (; eccsteps; eccsteps--) {
913 /* enable hardware ecc logic for write */
914 this->enable_hwecc(mtd, NAND_ECC_WRITE);
915 this->write_buf(mtd, &this->data_poi[datidx], this->eccsize);
916 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
917 for (i = 0; i < eccbytes; i++, eccidx++)
918 oob_buf[oob_config[eccidx]] = ecc_code[i];
919 /* If the hardware ecc provides syndromes then
920 * the ecc code must be written immidiately after
921 * the data bytes (words) */
922 if (this->options & NAND_HWECC_SYNDROME)
923 this->write_buf(mtd, ecc_code, eccbytes);
924 datidx += this->eccsize;
929 /* Write out OOB data */
930 if (this->options & NAND_HWECC_SYNDROME)
931 this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
933 this->write_buf(mtd, oob_buf, mtd->oobsize);
935 /* Send command to actually program the data */
936 this->cmdfunc(mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
939 /* call wait ready function */
940 status = this->waitfunc(mtd, this, FL_WRITING);
942 /* See if operation failed and additional status checks are available */
943 if ((status & NAND_STATUS_FAIL) && (this->errstat)) {
944 status = this->errstat(mtd, this, FL_WRITING, status, page);
947 /* See if device thinks it succeeded */
948 if (status & NAND_STATUS_FAIL) {
949 DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
953 /* FIXME: Implement cached programming ! */
954 /* wait until cache is ready */
955 // status = this->waitfunc (mtd, this, FL_CACHEDRPG);
960 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
962 * nand_verify_pages - [GENERIC] verify the chip contents after a write
963 * @mtd: MTD device structure
964 * @this: NAND chip structure
965 * @page: startpage inside the chip, must be called with (page & this->pagemask)
966 * @numpages: number of pages to verify
967 * @oob_buf: out of band data buffer
968 * @oobsel: out of band selecttion structre
969 * @chipnr: number of the current chip
970 * @oobmode: 1 = full buffer verify, 0 = ecc only
972 * The NAND device assumes that it is always writing to a cleanly erased page.
973 * Hence, it performs its internal write verification only on bits that
974 * transitioned from 1 to 0. The device does NOT verify the whole page on a
975 * byte by byte basis. It is possible that the page was not completely erased
976 * or the page is becoming unusable due to wear. The read with ECC would catch
977 * the error later when the ECC page check fails, but we would rather catch
978 * it early in the page write stage. Better to write no data than invalid data.
980 static int nand_verify_pages(struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
981 uint8_t *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
983 int i, j, datidx = 0, oobofs = 0, res = -EIO;
984 int eccsteps = this->eccsteps;
988 hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
990 /* Send command to read back the first page */
991 this->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
994 for (j = 0; j < eccsteps; j++) {
995 /* Loop through and verify the data */
996 if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
997 DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1000 datidx += mtd->eccsize;
1001 /* Have we a hw generator layout ? */
1004 if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
1005 DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1008 oobofs += hweccbytes;
1011 /* check, if we must compare all data or if we just have to
1012 * compare the ecc bytes
1015 if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
1016 DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
1020 /* Read always, else autoincrement fails */
1021 this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
1023 if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
1024 int ecccnt = oobsel->eccbytes;
1026 for (i = 0; i < ecccnt; i++) {
1027 int idx = oobsel->eccpos[i];
1028 if (oobdata[idx] != oob_buf[oobofs + idx]) {
1029 DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed ECC write verify, page 0x%08x, %6i bytes were succesful\n",
1030 __FUNCTION__, page, i);
1036 oobofs += mtd->oobsize - hweccbytes * eccsteps;
1040 /* Apply delay or wait for ready/busy pin
1041 * Do this before the AUTOINCR check, so no problems
1042 * arise if a chip which does auto increment
1043 * is marked as NOAUTOINCR by the board driver.
1044 * Do this also before returning, so the chip is
1045 * ready for the next command.
1047 if (!this->dev_ready)
1048 udelay(this->chip_delay);
1050 nand_wait_ready(mtd);
1052 /* All done, return happy */
1056 /* Check, if the chip supports auto page increment */
1057 if (!NAND_CANAUTOINCR(this))
1058 this->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1061 * Terminate the read command. We come here in case of an error
1062 * So we must issue a reset command.
1065 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1071 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1072 * @mtd: MTD device structure
1073 * @from: offset to read from
1074 * @len: number of bytes to read
1075 * @retlen: pointer to variable to store the number of read bytes
1076 * @buf: the databuffer to put data
1078 * This function simply calls nand_do_read_ecc with oob buffer and oobsel = NULL
1081 static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, uint8_t *buf)
1083 return nand_do_read_ecc(mtd, from, len, retlen, buf, NULL, &mtd->oobinfo, 0xff);
1087 * nand_read_ecc - [MTD Interface] MTD compability function for nand_do_read_ecc
1088 * @mtd: MTD device structure
1089 * @from: offset to read from
1090 * @len: number of bytes to read
1091 * @retlen: pointer to variable to store the number of read bytes
1092 * @buf: the databuffer to put data
1093 * @oob_buf: filesystem supplied oob data buffer
1094 * @oobsel: oob selection structure
1096 * This function simply calls nand_do_read_ecc with flags = 0xff
1098 static int nand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
1099 size_t *retlen, uint8_t *buf, uint8_t *oob_buf, struct nand_oobinfo *oobsel)
1101 /* use userspace supplied oobinfo, if zero */
1103 oobsel = &mtd->oobinfo;
1104 return nand_do_read_ecc(mtd, from, len, retlen, buf, oob_buf, oobsel, 0xff);
1108 * nand_do_read_ecc - [MTD Interface] Read data with ECC
1109 * @mtd: MTD device structure
1110 * @from: offset to read from
1111 * @len: number of bytes to read
1112 * @retlen: pointer to variable to store the number of read bytes
1113 * @buf: the databuffer to put data
1114 * @oob_buf: filesystem supplied oob data buffer (can be NULL)
1115 * @oobsel: oob selection structure
1116 * @flags: flag to indicate if nand_get_device/nand_release_device should be preformed
1117 * and how many corrected error bits are acceptable:
1118 * bits 0..7 - number of tolerable errors
1119 * bit 8 - 0 == do not get/release chip, 1 == get/release chip
1121 * NAND read with ECC
1123 int nand_do_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
1124 size_t *retlen, uint8_t *buf, uint8_t *oob_buf, struct nand_oobinfo *oobsel, int flags)
1127 int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
1128 int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
1129 struct nand_chip *this = mtd->priv;
1130 uint8_t *data_poi, *oob_data = oob_buf;
1131 uint8_t ecc_calc[32];
1132 uint8_t ecc_code[32];
1133 int eccmode, eccsteps;
1134 int *oob_config, datidx;
1135 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1140 DEBUG(MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int)from, (int)len);
1142 /* Do not allow reads past end of device */
1143 if ((from + len) > mtd->size) {
1144 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
1149 /* Grab the lock and see if the device is available */
1150 if (flags & NAND_GET_DEVICE)
1151 nand_get_device(this, mtd, FL_READING);
1153 /* Autoplace of oob data ? Use the default placement scheme */
1154 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1155 oobsel = this->autooob;
1157 eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
1158 oob_config = oobsel->eccpos;
1160 /* Select the NAND device */
1161 chipnr = (int)(from >> this->chip_shift);
1162 this->select_chip(mtd, chipnr);
1164 /* First we calculate the starting page */
1165 realpage = (int)(from >> this->page_shift);
1166 page = realpage & this->pagemask;
1168 /* Get raw starting column */
1169 col = from & (mtd->oobblock - 1);
1171 end = mtd->oobblock;
1172 ecc = this->eccsize;
1173 eccbytes = this->eccbytes;
1175 if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
1178 oobreadlen = mtd->oobsize;
1179 if (this->options & NAND_HWECC_SYNDROME)
1180 oobreadlen -= oobsel->eccbytes;
1182 /* Loop until all data read */
1183 while (read < len) {
1185 int aligned = (!col && (len - read) >= end);
1187 * If the read is not page aligned, we have to read into data buffer
1188 * due to ecc, else we read into return buffer direct
1191 data_poi = &buf[read];
1193 data_poi = this->data_buf;
1195 /* Check, if we have this page in the buffer
1197 * FIXME: Make it work when we must provide oob data too,
1198 * check the usage of data_buf oob field
1200 if (realpage == this->pagebuf && !oob_buf) {
1201 /* aligned read ? */
1203 memcpy(data_poi, this->data_buf, end);
1207 /* Check, if we must send the read command */
1209 this->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1213 /* get oob area, if we have no oob buffer from fs-driver */
1214 if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE ||
1215 oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1216 oob_data = &this->data_buf[end];
1218 eccsteps = this->eccsteps;
1221 case NAND_ECC_NONE:{
1222 /* No ECC, Read in a page */
1223 static unsigned long lastwhinge = 0;
1224 if ((lastwhinge / HZ) != (jiffies / HZ)) {
1226 "Reading data from NAND FLASH without ECC is not recommended\n");
1227 lastwhinge = jiffies;
1229 this->read_buf(mtd, data_poi, end);
1233 case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
1234 this->read_buf(mtd, data_poi, end);
1235 for (i = 0, datidx = 0; eccsteps; eccsteps--, i += 3, datidx += ecc)
1236 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1240 for (i = 0, datidx = 0; eccsteps; eccsteps--, i += eccbytes, datidx += ecc) {
1241 this->enable_hwecc(mtd, NAND_ECC_READ);
1242 this->read_buf(mtd, &data_poi[datidx], ecc);
1244 /* HW ecc with syndrome calculation must read the
1245 * syndrome from flash immidiately after the data */
1247 /* Some hw ecc generators need to know when the
1248 * syndrome is read from flash */
1249 this->enable_hwecc(mtd, NAND_ECC_READSYN);
1250 this->read_buf(mtd, &oob_data[i], eccbytes);
1251 /* We calc error correction directly, it checks the hw
1252 * generator for an error, reads back the syndrome and
1253 * does the error correction on the fly */
1254 ecc_status = this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]);
1255 if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) {
1256 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_ecc: "
1257 "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
1261 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1268 this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
1270 /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
1274 /* Pick the ECC bytes out of the oob data */
1275 for (j = 0; j < oobsel->eccbytes; j++)
1276 ecc_code[j] = oob_data[oob_config[j]];
1278 /* correct data, if necessary */
1279 for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {
1280 ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
1282 /* Get next chunk of ecc bytes */
1285 /* Check, if we have a fs supplied oob-buffer,
1286 * This is the legacy mode. Used by YAFFS1
1287 * Should go away some day
1289 if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
1290 int *p = (int *)(&oob_data[mtd->oobsize]);
1294 if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) {
1295 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
1301 /* check, if we have a fs supplied oob-buffer */
1303 /* without autoplace. Legacy mode used by YAFFS1 */
1304 switch (oobsel->useecc) {
1305 case MTD_NANDECC_AUTOPLACE:
1306 case MTD_NANDECC_AUTOPL_USR:
1307 /* Walk through the autoplace chunks */
1308 for (i = 0; oobsel->oobfree[i][1]; i++) {
1309 int from = oobsel->oobfree[i][0];
1310 int num = oobsel->oobfree[i][1];
1311 memcpy(&oob_buf[oob], &oob_data[from], num);
1315 case MTD_NANDECC_PLACE:
1316 /* YAFFS1 legacy mode */
1317 oob_data += this->eccsteps * sizeof(int);
1319 oob_data += mtd->oobsize;
1323 /* Partial page read, transfer data into fs buffer */
1325 for (j = col; j < end && read < len; j++)
1326 buf[read++] = data_poi[j];
1327 this->pagebuf = realpage;
1329 read += mtd->oobblock;
1331 /* Apply delay or wait for ready/busy pin
1332 * Do this before the AUTOINCR check, so no problems
1333 * arise if a chip which does auto increment
1334 * is marked as NOAUTOINCR by the board driver.
1336 if (!this->dev_ready)
1337 udelay(this->chip_delay);
1339 nand_wait_ready(mtd);
1344 /* For subsequent reads align to page boundary. */
1346 /* Increment page address */
1349 page = realpage & this->pagemask;
1350 /* Check, if we cross a chip boundary */
1353 this->select_chip(mtd, -1);
1354 this->select_chip(mtd, chipnr);
1356 /* Check, if the chip supports auto page increment
1357 * or if we have hit a block boundary.
1359 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1363 /* Deselect and wake up anyone waiting on the device */
1364 if (flags & NAND_GET_DEVICE)
1365 nand_release_device(mtd);
1368 * Return success, if no ECC failures, else -EBADMSG
1369 * fs driver will take care of that, because
1370 * retlen == desired len and result == -EBADMSG
1373 return ecc_failed ? -EBADMSG : 0;
1377 * nand_read_oob - [MTD Interface] NAND read out-of-band
1378 * @mtd: MTD device structure
1379 * @from: offset to read from
1380 * @len: number of bytes to read
1381 * @retlen: pointer to variable to store the number of read bytes
1382 * @buf: the databuffer to put data
1384 * NAND read out-of-band data from the spare area
1386 static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, uint8_t *buf)
1388 int i, col, page, chipnr;
1389 struct nand_chip *this = mtd->priv;
1390 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1392 DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int)from, (int)len);
1394 /* Shift to get page */
1395 page = (int)(from >> this->page_shift);
1396 chipnr = (int)(from >> this->chip_shift);
1398 /* Mask to get column */
1399 col = from & (mtd->oobsize - 1);
1401 /* Initialize return length value */
1404 /* Do not allow reads past end of device */
1405 if ((from + len) > mtd->size) {
1406 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n");
1411 /* Grab the lock and see if the device is available */
1412 nand_get_device(this, mtd, FL_READING);
1414 /* Select the NAND device */
1415 this->select_chip(mtd, chipnr);
1417 /* Send the read command */
1418 this->cmdfunc(mtd, NAND_CMD_READOOB, col, page & this->pagemask);
1420 * Read the data, if we read more than one page
1421 * oob data, let the device transfer the data !
1425 int thislen = mtd->oobsize - col;
1426 thislen = min_t(int, thislen, len);
1427 this->read_buf(mtd, &buf[i], thislen);
1435 /* Check, if we cross a chip boundary */
1436 if (!(page & this->pagemask)) {
1438 this->select_chip(mtd, -1);
1439 this->select_chip(mtd, chipnr);
1442 /* Apply delay or wait for ready/busy pin
1443 * Do this before the AUTOINCR check, so no problems
1444 * arise if a chip which does auto increment
1445 * is marked as NOAUTOINCR by the board driver.
1447 if (!this->dev_ready)
1448 udelay(this->chip_delay);
1450 nand_wait_ready(mtd);
1452 /* Check, if the chip supports auto page increment
1453 * or if we have hit a block boundary.
1455 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
1456 /* For subsequent page reads set offset to 0 */
1457 this->cmdfunc(mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
1462 /* Deselect and wake up anyone waiting on the device */
1463 nand_release_device(mtd);
1471 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
1472 * @mtd: MTD device structure
1473 * @buf: temporary buffer
1474 * @from: offset to read from
1475 * @len: number of bytes to read
1476 * @ooblen: number of oob data bytes to read
1478 * Read raw data including oob into buffer
1480 int nand_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
1482 struct nand_chip *this = mtd->priv;
1483 int page = (int)(from >> this->page_shift);
1484 int chip = (int)(from >> this->chip_shift);
1487 int pagesize = mtd->oobblock + mtd->oobsize;
1488 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1490 /* Do not allow reads past end of device */
1491 if ((from + len) > mtd->size) {
1492 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n");
1496 /* Grab the lock and see if the device is available */
1497 nand_get_device(this, mtd, FL_READING);
1499 this->select_chip(mtd, chip);
1501 /* Add requested oob length */
1506 this->cmdfunc(mtd, NAND_CMD_READ0, 0, page & this->pagemask);
1509 this->read_buf(mtd, &buf[cnt], pagesize);
1515 if (!this->dev_ready)
1516 udelay(this->chip_delay);
1518 nand_wait_ready(mtd);
1520 /* Check, if the chip supports auto page increment */
1521 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1525 /* Deselect and wake up anyone waiting on the device */
1526 nand_release_device(mtd);
1531 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
1532 * @mtd: MTD device structure
1533 * @fsbuf: buffer given by fs driver
1534 * @oobsel: out of band selection structre
1535 * @autoplace: 1 = place given buffer into the oob bytes
1536 * @numpages: number of pages to prepare
1539 * 1. Filesystem buffer available and autoplacement is off,
1540 * return filesystem buffer
1541 * 2. No filesystem buffer or autoplace is off, return internal
1543 * 3. Filesystem buffer is given and autoplace selected
1544 * put data from fs buffer into internal buffer and
1545 * retrun internal buffer
1547 * Note: The internal buffer is filled with 0xff. This must
1548 * be done only once, when no autoplacement happens
1549 * Autoplacement sets the buffer dirty flag, which
1550 * forces the 0xff fill before using the buffer again.
1553 static uint8_t *nand_prepare_oobbuf(struct mtd_info *mtd, uint8_t *fsbuf, struct nand_oobinfo *oobsel,
1554 int autoplace, int numpages)
1556 struct nand_chip *this = mtd->priv;
1559 /* Zero copy fs supplied buffer */
1560 if (fsbuf && !autoplace)
1563 /* Check, if the buffer must be filled with ff again */
1564 if (this->oobdirty) {
1565 memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
1569 /* If we have no autoplacement or no fs buffer use the internal one */
1570 if (!autoplace || !fsbuf)
1571 return this->oob_buf;
1573 /* Walk through the pages and place the data */
1576 while (numpages--) {
1577 for (i = 0, len = 0; len < mtd->oobavail; i++) {
1578 int to = ofs + oobsel->oobfree[i][0];
1579 int num = oobsel->oobfree[i][1];
1580 memcpy(&this->oob_buf[to], fsbuf, num);
1584 ofs += mtd->oobavail;
1586 return this->oob_buf;
1589 #define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
1592 * nand_write - [MTD Interface] compability function for nand_write_ecc
1593 * @mtd: MTD device structure
1594 * @to: offset to write to
1595 * @len: number of bytes to write
1596 * @retlen: pointer to variable to store the number of written bytes
1597 * @buf: the data to write
1599 * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
1602 static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const uint8_t *buf)
1604 return (nand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL));
1608 * nand_write_ecc - [MTD Interface] NAND write with ECC
1609 * @mtd: MTD device structure
1610 * @to: offset to write to
1611 * @len: number of bytes to write
1612 * @retlen: pointer to variable to store the number of written bytes
1613 * @buf: the data to write
1614 * @eccbuf: filesystem supplied oob data buffer
1615 * @oobsel: oob selection structure
1617 * NAND write with ECC
1619 static int nand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
1620 size_t *retlen, const uint8_t *buf, uint8_t *eccbuf,
1621 struct nand_oobinfo *oobsel)
1623 int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
1624 int autoplace = 0, numpages, totalpages;
1625 struct nand_chip *this = mtd->priv;
1626 uint8_t *oobbuf, *bufstart;
1627 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1629 DEBUG(MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int)to, (int)len);
1631 /* Initialize retlen, in case of early exit */
1634 /* Do not allow write past end of device */
1635 if ((to + len) > mtd->size) {
1636 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n");
1640 /* reject writes, which are not page aligned */
1641 if (NOTALIGNED(to) || NOTALIGNED(len)) {
1642 printk(KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1646 /* Grab the lock and see if the device is available */
1647 nand_get_device(this, mtd, FL_WRITING);
1649 /* Calculate chipnr */
1650 chipnr = (int)(to >> this->chip_shift);
1651 /* Select the NAND device */
1652 this->select_chip(mtd, chipnr);
1654 /* Check, if it is write protected */
1655 if (nand_check_wp(mtd))
1658 /* if oobsel is NULL, use chip defaults */
1660 oobsel = &mtd->oobinfo;
1662 /* Autoplace of oob data ? Use the default placement scheme */
1663 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1664 oobsel = this->autooob;
1667 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1670 /* Setup variables and oob buffer */
1671 totalpages = len >> this->page_shift;
1672 page = (int)(to >> this->page_shift);
1673 /* Invalidate the page cache, if we write to the cached page */
1674 if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
1677 /* Set it relative to chip */
1678 page &= this->pagemask;
1680 /* Calc number of pages we can write in one go */
1681 numpages = min(ppblock - (startpage & (ppblock - 1)), totalpages);
1682 oobbuf = nand_prepare_oobbuf(mtd, eccbuf, oobsel, autoplace, numpages);
1683 bufstart = (uint8_t *) buf;
1685 /* Loop until all data is written */
1686 while (written < len) {
1688 this->data_poi = (uint8_t *) &buf[written];
1689 /* Write one page. If this is the last page to write
1690 * or the last page in this block, then use the
1691 * real pageprogram command, else select cached programming
1692 * if supported by the chip.
1694 ret = nand_write_page(mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
1696 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret);
1700 oob += mtd->oobsize;
1701 /* Update written bytes count */
1702 written += mtd->oobblock;
1706 /* Increment page address */
1709 /* Have we hit a block boundary ? Then we have to verify and
1710 * if verify is ok, we have to setup the oob buffer for
1713 if (!(page & (ppblock - 1))) {
1715 this->data_poi = bufstart;
1716 ret = nand_verify_pages(mtd, this, startpage, page - startpage,
1717 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1719 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1724 ofs = autoplace ? mtd->oobavail : mtd->oobsize;
1726 eccbuf += (page - startpage) * ofs;
1727 totalpages -= page - startpage;
1728 numpages = min(totalpages, ppblock);
1729 page &= this->pagemask;
1731 oobbuf = nand_prepare_oobbuf(mtd, eccbuf, oobsel, autoplace, numpages);
1733 /* Check, if we cross a chip boundary */
1736 this->select_chip(mtd, -1);
1737 this->select_chip(mtd, chipnr);
1741 /* Verify the remaining pages */
1743 this->data_poi = bufstart;
1744 ret = nand_verify_pages(mtd, this, startpage, totalpages, oobbuf, oobsel, chipnr, (eccbuf != NULL));
1748 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1751 /* Deselect and wake up anyone waiting on the device */
1752 nand_release_device(mtd);
1758 * nand_write_oob - [MTD Interface] NAND write out-of-band
1759 * @mtd: MTD device structure
1760 * @to: offset to write to
1761 * @len: number of bytes to write
1762 * @retlen: pointer to variable to store the number of written bytes
1763 * @buf: the data to write
1765 * NAND write out-of-band
1767 static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const uint8_t *buf)
1769 int column, page, status, ret = -EIO, chipnr;
1770 struct nand_chip *this = mtd->priv;
1772 DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int)to, (int)len);
1774 /* Shift to get page */
1775 page = (int)(to >> this->page_shift);
1776 chipnr = (int)(to >> this->chip_shift);
1778 /* Mask to get column */
1779 column = to & (mtd->oobsize - 1);
1781 /* Initialize return length value */
1784 /* Do not allow write past end of page */
1785 if ((column + len) > mtd->oobsize) {
1786 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
1790 /* Grab the lock and see if the device is available */
1791 nand_get_device(this, mtd, FL_WRITING);
1793 /* Select the NAND device */
1794 this->select_chip(mtd, chipnr);
1796 /* Reset the chip. Some chips (like the Toshiba TC5832DC found
1797 in one of my DiskOnChip 2000 test units) will clear the whole
1798 data page too if we don't do this. I have no clue why, but
1799 I seem to have 'fixed' it in the doc2000 driver in
1800 August 1999. dwmw2. */
1801 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1803 /* Check, if it is write protected */
1804 if (nand_check_wp(mtd))
1807 /* Invalidate the page cache, if we write to the cached page */
1808 if (page == this->pagebuf)
1811 if (NAND_MUST_PAD(this)) {
1812 /* Write out desired data */
1813 this->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
1814 /* prepad 0xff for partial programming */
1815 this->write_buf(mtd, ffchars, column);
1817 this->write_buf(mtd, buf, len);
1818 /* postpad 0xff for partial programming */
1819 this->write_buf(mtd, ffchars, mtd->oobsize - (len + column));
1821 /* Write out desired data */
1822 this->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
1824 this->write_buf(mtd, buf, len);
1826 /* Send command to program the OOB data */
1827 this->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1829 status = this->waitfunc(mtd, this, FL_WRITING);
1831 /* See if device thinks it succeeded */
1832 if (status & NAND_STATUS_FAIL) {
1833 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
1840 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1841 /* Send command to read back the data */
1842 this->cmdfunc(mtd, NAND_CMD_READOOB, column, page & this->pagemask);
1844 if (this->verify_buf(mtd, buf, len)) {
1845 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
1852 /* Deselect and wake up anyone waiting on the device */
1853 nand_release_device(mtd);
1859 * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
1860 * @mtd: MTD device structure
1861 * @vecs: the iovectors to write
1862 * @count: number of vectors
1863 * @to: offset to write to
1864 * @retlen: pointer to variable to store the number of written bytes
1866 * NAND write with kvec. This just calls the ecc function
1868 static int nand_writev(struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1869 loff_t to, size_t *retlen)
1871 return (nand_writev_ecc(mtd, vecs, count, to, retlen, NULL, NULL));
1875 * nand_writev_ecc - [MTD Interface] write with iovec with ecc
1876 * @mtd: MTD device structure
1877 * @vecs: the iovectors to write
1878 * @count: number of vectors
1879 * @to: offset to write to
1880 * @retlen: pointer to variable to store the number of written bytes
1881 * @eccbuf: filesystem supplied oob data buffer
1882 * @oobsel: oob selection structure
1884 * NAND write with iovec with ecc
1886 static int nand_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1887 loff_t to, size_t *retlen, uint8_t *eccbuf, struct nand_oobinfo *oobsel)
1889 int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
1890 int oob, numpages, autoplace = 0, startpage;
1891 struct nand_chip *this = mtd->priv;
1892 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1893 uint8_t *oobbuf, *bufstart;
1895 /* Preset written len for early exit */
1898 /* Calculate total length of data */
1900 for (i = 0; i < count; i++)
1901 total_len += (int)vecs[i].iov_len;
1903 DEBUG(MTD_DEBUG_LEVEL3, "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int)to, (unsigned int)total_len, count);
1905 /* Do not allow write past end of page */
1906 if ((to + total_len) > mtd->size) {
1907 DEBUG(MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
1911 /* reject writes, which are not page aligned */
1912 if (NOTALIGNED(to) || NOTALIGNED(total_len)) {
1913 printk(KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1917 /* Grab the lock and see if the device is available */
1918 nand_get_device(this, mtd, FL_WRITING);
1920 /* Get the current chip-nr */
1921 chipnr = (int)(to >> this->chip_shift);
1922 /* Select the NAND device */
1923 this->select_chip(mtd, chipnr);
1925 /* Check, if it is write protected */
1926 if (nand_check_wp(mtd))
1929 /* if oobsel is NULL, use chip defaults */
1931 oobsel = &mtd->oobinfo;
1933 /* Autoplace of oob data ? Use the default placement scheme */
1934 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1935 oobsel = this->autooob;
1938 if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
1941 /* Setup start page */
1942 page = (int)(to >> this->page_shift);
1943 /* Invalidate the page cache, if we write to the cached page */
1944 if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))
1947 startpage = page & this->pagemask;
1949 /* Loop until all kvec' data has been written */
1952 /* If the given tuple is >= pagesize then
1953 * write it out from the iov
1955 if ((vecs->iov_len - len) >= mtd->oobblock) {
1956 /* Calc number of pages we can write
1957 * out of this iov in one go */
1958 numpages = (vecs->iov_len - len) >> this->page_shift;
1959 /* Do not cross block boundaries */
1960 numpages = min(ppblock - (startpage & (ppblock - 1)), numpages);
1961 oobbuf = nand_prepare_oobbuf(mtd, NULL, oobsel, autoplace, numpages);
1962 bufstart = (uint8_t *) vecs->iov_base;
1964 this->data_poi = bufstart;
1966 for (i = 1; i <= numpages; i++) {
1967 /* Write one page. If this is the last page to write
1968 * then use the real pageprogram command, else select
1969 * cached programming if supported by the chip.
1971 ret = nand_write_page(mtd, this, page & this->pagemask,
1972 &oobbuf[oob], oobsel, i != numpages);
1975 this->data_poi += mtd->oobblock;
1976 len += mtd->oobblock;
1977 oob += mtd->oobsize;
1980 /* Check, if we have to switch to the next tuple */
1981 if (len >= (int)vecs->iov_len) {
1987 /* We must use the internal buffer, read data out of each
1988 * tuple until we have a full page to write
1991 while (cnt < mtd->oobblock) {
1992 if (vecs->iov_base != NULL && vecs->iov_len)
1993 this->data_buf[cnt++] = ((uint8_t *) vecs->iov_base)[len++];
1994 /* Check, if we have to switch to the next tuple */
1995 if (len >= (int)vecs->iov_len) {
2001 this->pagebuf = page;
2002 this->data_poi = this->data_buf;
2003 bufstart = this->data_poi;
2005 oobbuf = nand_prepare_oobbuf(mtd, NULL, oobsel, autoplace, numpages);
2006 ret = nand_write_page(mtd, this, page & this->pagemask, oobbuf, oobsel, 0);
2012 this->data_poi = bufstart;
2013 ret = nand_verify_pages(mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
2017 written += mtd->oobblock * numpages;
2022 startpage = page & this->pagemask;
2023 /* Check, if we cross a chip boundary */
2026 this->select_chip(mtd, -1);
2027 this->select_chip(mtd, chipnr);
2032 /* Deselect and wake up anyone waiting on the device */
2033 nand_release_device(mtd);
2040 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2041 * @mtd: MTD device structure
2042 * @page: the page address of the block which will be erased
2044 * Standard erase command for NAND chips
2046 static void single_erase_cmd(struct mtd_info *mtd, int page)
2048 struct nand_chip *this = mtd->priv;
2049 /* Send commands to erase a block */
2050 this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2051 this->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2055 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2056 * @mtd: MTD device structure
2057 * @page: the page address of the block which will be erased
2059 * AND multi block erase command function
2060 * Erase 4 consecutive blocks
2062 static void multi_erase_cmd(struct mtd_info *mtd, int page)
2064 struct nand_chip *this = mtd->priv;
2065 /* Send commands to erase a block */
2066 this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2067 this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2068 this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2069 this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2070 this->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2074 * nand_erase - [MTD Interface] erase block(s)
2075 * @mtd: MTD device structure
2076 * @instr: erase instruction
2078 * Erase one ore more blocks
2080 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
2082 return nand_erase_nand(mtd, instr, 0);
2085 #define BBT_PAGE_MASK 0xffffff3f
2087 * nand_erase_intern - [NAND Interface] erase block(s)
2088 * @mtd: MTD device structure
2089 * @instr: erase instruction
2090 * @allowbbt: allow erasing the bbt area
2092 * Erase one ore more blocks
2094 int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
2096 int page, len, status, pages_per_block, ret, chipnr;
2097 struct nand_chip *this = mtd->priv;
2098 int rewrite_bbt[NAND_MAX_CHIPS]={0}; /* flags to indicate the page, if bbt needs to be rewritten. */
2099 unsigned int bbt_masked_page; /* bbt mask to compare to page being erased. */
2100 /* It is used to see if the current page is in the same */
2101 /* 256 block group and the same bank as the bbt. */
2103 DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n", (unsigned int)instr->addr, (unsigned int)instr->len);
2105 /* Start address must align on block boundary */
2106 if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
2107 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2111 /* Length must align on block boundary */
2112 if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
2113 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
2117 /* Do not allow erase past end of device */
2118 if ((instr->len + instr->addr) > mtd->size) {
2119 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
2123 instr->fail_addr = 0xffffffff;
2125 /* Grab the lock and see if the device is available */
2126 nand_get_device(this, mtd, FL_ERASING);
2128 /* Shift to get first page */
2129 page = (int)(instr->addr >> this->page_shift);
2130 chipnr = (int)(instr->addr >> this->chip_shift);
2132 /* Calculate pages in each block */
2133 pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
2135 /* Select the NAND device */
2136 this->select_chip(mtd, chipnr);
2138 /* Check the WP bit */
2139 /* Check, if it is write protected */
2140 if (nand_check_wp(mtd)) {
2141 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
2142 instr->state = MTD_ERASE_FAILED;
2146 /* if BBT requires refresh, set the BBT page mask to see if the BBT should be rewritten */
2147 if (this->options & BBT_AUTO_REFRESH) {
2148 bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2150 bbt_masked_page = 0xffffffff; /* should not match anything */
2153 /* Loop through the pages */
2156 instr->state = MTD_ERASING;
2159 /* Check if we have a bad block, we do not erase bad blocks ! */
2160 if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
2161 printk(KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
2162 instr->state = MTD_ERASE_FAILED;
2166 /* Invalidate the page cache, if we erase the block which contains
2167 the current cached page */
2168 if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
2171 this->erase_cmd(mtd, page & this->pagemask);
2173 status = this->waitfunc(mtd, this, FL_ERASING);
2175 /* See if operation failed and additional status checks are available */
2176 if ((status & NAND_STATUS_FAIL) && (this->errstat)) {
2177 status = this->errstat(mtd, this, FL_ERASING, status, page);
2180 /* See if block erase succeeded */
2181 if (status & NAND_STATUS_FAIL) {
2182 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
2183 instr->state = MTD_ERASE_FAILED;
2184 instr->fail_addr = (page << this->page_shift);
2188 /* if BBT requires refresh, set the BBT rewrite flag to the page being erased */
2189 if (this->options & BBT_AUTO_REFRESH) {
2190 if (((page & BBT_PAGE_MASK) == bbt_masked_page) &&
2191 (page != this->bbt_td->pages[chipnr])) {
2192 rewrite_bbt[chipnr] = (page << this->page_shift);
2196 /* Increment page address and decrement length */
2197 len -= (1 << this->phys_erase_shift);
2198 page += pages_per_block;
2200 /* Check, if we cross a chip boundary */
2201 if (len && !(page & this->pagemask)) {
2203 this->select_chip(mtd, -1);
2204 this->select_chip(mtd, chipnr);
2206 /* if BBT requires refresh and BBT-PERCHIP,
2207 * set the BBT page mask to see if this BBT should be rewritten */
2208 if ((this->options & BBT_AUTO_REFRESH) && (this->bbt_td->options & NAND_BBT_PERCHIP)) {
2209 bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2214 instr->state = MTD_ERASE_DONE;
2218 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2219 /* Do call back function */
2221 mtd_erase_callback(instr);
2223 /* Deselect and wake up anyone waiting on the device */
2224 nand_release_device(mtd);
2226 /* if BBT requires refresh and erase was successful, rewrite any selected bad block tables */
2227 if ((this->options & BBT_AUTO_REFRESH) && (!ret)) {
2228 for (chipnr = 0; chipnr < this->numchips; chipnr++) {
2229 if (rewrite_bbt[chipnr]) {
2230 /* update the BBT for chip */
2231 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt (%d:0x%0x 0x%0x)\n",
2232 chipnr, rewrite_bbt[chipnr], this->bbt_td->pages[chipnr]);
2233 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2238 /* Return more or less happy */
2243 * nand_sync - [MTD Interface] sync
2244 * @mtd: MTD device structure
2246 * Sync is actually a wait for chip ready function
2248 static void nand_sync(struct mtd_info *mtd)
2250 struct nand_chip *this = mtd->priv;
2252 DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2254 /* Grab the lock and see if the device is available */
2255 nand_get_device(this, mtd, FL_SYNCING);
2256 /* Release it and go back */
2257 nand_release_device(mtd);
2261 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2262 * @mtd: MTD device structure
2263 * @ofs: offset relative to mtd start
2265 static int nand_block_isbad(struct mtd_info *mtd, loff_t ofs)
2267 /* Check for invalid offset */
2268 if (ofs > mtd->size)
2271 return nand_block_checkbad(mtd, ofs, 1, 0);
2275 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2276 * @mtd: MTD device structure
2277 * @ofs: offset relative to mtd start
2279 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2281 struct nand_chip *this = mtd->priv;
2284 if ((ret = nand_block_isbad(mtd, ofs))) {
2285 /* If it was bad already, return success and do nothing. */
2291 return this->block_markbad(mtd, ofs);
2295 * nand_suspend - [MTD Interface] Suspend the NAND flash
2296 * @mtd: MTD device structure
2298 static int nand_suspend(struct mtd_info *mtd)
2300 struct nand_chip *this = mtd->priv;
2302 return nand_get_device(this, mtd, FL_PM_SUSPENDED);
2306 * nand_resume - [MTD Interface] Resume the NAND flash
2307 * @mtd: MTD device structure
2309 static void nand_resume(struct mtd_info *mtd)
2311 struct nand_chip *this = mtd->priv;
2313 if (this->state == FL_PM_SUSPENDED)
2314 nand_release_device(mtd);
2316 printk(KERN_ERR "nand_resume() called for a chip which is not "
2317 "in suspended state\n");
2321 * Free allocated data structures
2323 static void nand_free_kmem(struct nand_chip *this)
2325 /* Buffer allocated by nand_scan ? */
2326 if (this->options & NAND_OOBBUF_ALLOC)
2327 kfree(this->oob_buf);
2328 /* Buffer allocated by nand_scan ? */
2329 if (this->options & NAND_DATABUF_ALLOC)
2330 kfree(this->data_buf);
2331 /* Controller allocated by nand_scan ? */
2332 if (this->options & NAND_CONTROLLER_ALLOC)
2333 kfree(this->controller);
2336 /* module_text_address() isn't exported, and it's mostly a pointless
2337 test if this is a module _anyway_ -- they'd have to try _really_ hard
2338 to call us from in-kernel code if the core NAND support is modular. */
2340 #define caller_is_module() (1)
2342 #define caller_is_module() module_text_address((unsigned long)__builtin_return_address(0))
2346 * nand_scan - [NAND Interface] Scan for the NAND device
2347 * @mtd: MTD device structure
2348 * @maxchips: Number of chips to scan for
2350 * This fills out all the uninitialized function pointers
2351 * with the defaults.
2352 * The flash ID is read and the mtd/chip structures are
2353 * filled with the appropriate values. Buffers are allocated if
2354 * they are not provided by the board driver
2355 * The mtd->owner field must be set to the module of the caller
2358 int nand_scan(struct mtd_info *mtd, int maxchips)
2360 int i, nand_maf_id, nand_dev_id, busw, maf_id;
2361 struct nand_chip *this = mtd->priv;
2363 /* Many callers got this wrong, so check for it for a while... */
2364 if (!mtd->owner && caller_is_module()) {
2365 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2369 /* Get buswidth to select the correct functions */
2370 busw = this->options & NAND_BUSWIDTH_16;
2372 /* check for proper chip_delay setup, set 20us if not */
2373 if (!this->chip_delay)
2374 this->chip_delay = 20;
2376 /* check, if a user supplied command function given */
2377 if (this->cmdfunc == NULL)
2378 this->cmdfunc = nand_command;
2380 /* check, if a user supplied wait function given */
2381 if (this->waitfunc == NULL)
2382 this->waitfunc = nand_wait;
2384 if (!this->select_chip)
2385 this->select_chip = nand_select_chip;
2386 if (!this->write_byte)
2387 this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2388 if (!this->read_byte)
2389 this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2390 if (!this->write_word)
2391 this->write_word = nand_write_word;
2392 if (!this->read_word)
2393 this->read_word = nand_read_word;
2394 if (!this->block_bad)
2395 this->block_bad = nand_block_bad;
2396 if (!this->block_markbad)
2397 this->block_markbad = nand_default_block_markbad;
2398 if (!this->write_buf)
2399 this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2400 if (!this->read_buf)
2401 this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2402 if (!this->verify_buf)
2403 this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2404 if (!this->scan_bbt)
2405 this->scan_bbt = nand_default_bbt;
2407 /* Select the device */
2408 this->select_chip(mtd, 0);
2410 /* Send the command for reading device ID */
2411 this->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2413 /* Read manufacturer and device IDs */
2414 nand_maf_id = this->read_byte(mtd);
2415 nand_dev_id = this->read_byte(mtd);
2417 /* Print and store flash device information */
2418 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2420 if (nand_dev_id != nand_flash_ids[i].id)
2424 mtd->name = nand_flash_ids[i].name;
2425 this->chipsize = nand_flash_ids[i].chipsize << 20;
2427 /* New devices have all the information in additional id bytes */
2428 if (!nand_flash_ids[i].pagesize) {
2430 /* The 3rd id byte contains non relevant data ATM */
2431 extid = this->read_byte(mtd);
2432 /* The 4th id byte is the important one */
2433 extid = this->read_byte(mtd);
2435 mtd->oobblock = 1024 << (extid & 0x3);
2438 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->oobblock >> 9);
2440 /* Calc blocksize. Blocksize is multiples of 64KiB */
2441 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2443 /* Get buswidth information */
2444 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2447 /* Old devices have this data hardcoded in the
2448 * device id table */
2449 mtd->erasesize = nand_flash_ids[i].erasesize;
2450 mtd->oobblock = nand_flash_ids[i].pagesize;
2451 mtd->oobsize = mtd->oobblock / 32;
2452 busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
2455 /* Try to identify manufacturer */
2456 for (maf_id = 0; nand_manuf_ids[maf_id].id != 0x0; maf_id++) {
2457 if (nand_manuf_ids[maf_id].id == nand_maf_id)
2461 /* Check, if buswidth is correct. Hardware drivers should set
2463 if (busw != (this->options & NAND_BUSWIDTH_16)) {
2464 printk(KERN_INFO "NAND device: Manufacturer ID:"
2465 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2466 nand_manuf_ids[maf_id].name, mtd->name);
2468 "NAND bus width %d instead %d bit\n",
2469 (this->options & NAND_BUSWIDTH_16) ? 16 : 8, busw ? 16 : 8);
2470 this->select_chip(mtd, -1);
2474 /* Calculate the address shift from the page size */
2475 this->page_shift = ffs(mtd->oobblock) - 1;
2476 this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
2477 this->chip_shift = ffs(this->chipsize) - 1;
2479 /* Set the bad block position */
2480 this->badblockpos = mtd->oobblock > 512 ? NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2482 /* Get chip options, preserve non chip based options */
2483 this->options &= ~NAND_CHIPOPTIONS_MSK;
2484 this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
2485 /* Set this as a default. Board drivers can override it, if necessary */
2486 this->options |= NAND_NO_AUTOINCR;
2487 /* Check if this is a not a samsung device. Do not clear the options
2488 * for chips which are not having an extended id.
2490 if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
2491 this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2493 /* Check for AND chips with 4 page planes */
2494 if (this->options & NAND_4PAGE_ARRAY)
2495 this->erase_cmd = multi_erase_cmd;
2497 this->erase_cmd = single_erase_cmd;
2499 /* Do not replace user supplied command function ! */
2500 if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
2501 this->cmdfunc = nand_command_lp;
2503 printk(KERN_INFO "NAND device: Manufacturer ID:"
2504 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2505 nand_manuf_ids[maf_id].name, nand_flash_ids[i].name);
2509 if (!nand_flash_ids[i].name) {
2510 printk(KERN_WARNING "No NAND device found!!!\n");
2511 this->select_chip(mtd, -1);
2515 for (i = 1; i < maxchips; i++) {
2516 this->select_chip(mtd, i);
2518 /* Send the command for reading device ID */
2519 this->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2521 /* Read manufacturer and device IDs */
2522 if (nand_maf_id != this->read_byte(mtd) ||
2523 nand_dev_id != this->read_byte(mtd))
2527 printk(KERN_INFO "%d NAND chips detected\n", i);
2529 /* Allocate buffers, if necessary */
2530 if (!this->oob_buf) {
2532 len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
2533 this->oob_buf = kmalloc(len, GFP_KERNEL);
2534 if (!this->oob_buf) {
2535 printk(KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
2538 this->options |= NAND_OOBBUF_ALLOC;
2541 if (!this->data_buf) {
2543 len = mtd->oobblock + mtd->oobsize;
2544 this->data_buf = kmalloc(len, GFP_KERNEL);
2545 if (!this->data_buf) {
2546 printk(KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
2547 nand_free_kmem(this);
2550 this->options |= NAND_DATABUF_ALLOC;
2553 /* Store the number of chips and calc total size for mtd */
2555 mtd->size = i * this->chipsize;
2556 /* Convert chipsize to number of pages per chip -1. */
2557 this->pagemask = (this->chipsize >> this->page_shift) - 1;
2558 /* Preset the internal oob buffer */
2559 memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
2561 /* If no default placement scheme is given, select an
2562 * appropriate one */
2563 if (!this->autooob) {
2564 /* Select the appropriate default oob placement scheme for
2565 * placement agnostic filesystems */
2566 switch (mtd->oobsize) {
2568 this->autooob = &nand_oob_8;
2571 this->autooob = &nand_oob_16;
2574 this->autooob = &nand_oob_64;
2577 printk(KERN_WARNING "No oob scheme defined for oobsize %d\n", mtd->oobsize);
2582 /* The number of bytes available for the filesystem to place fs dependend
2585 for (i = 0; this->autooob->oobfree[i][1]; i++)
2586 mtd->oobavail += this->autooob->oobfree[i][1];
2589 * check ECC mode, default to software
2590 * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
2591 * fallback to software ECC
2593 this->eccsize = 256; /* set default eccsize */
2596 switch (this->eccmode) {
2597 case NAND_ECC_HW12_2048:
2598 if (mtd->oobblock < 2048) {
2599 printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
2601 this->eccmode = NAND_ECC_SOFT;
2602 this->calculate_ecc = nand_calculate_ecc;
2603 this->correct_data = nand_correct_data;
2605 this->eccsize = 2048;
2608 case NAND_ECC_HW3_512:
2609 case NAND_ECC_HW6_512:
2610 case NAND_ECC_HW8_512:
2611 if (mtd->oobblock == 256) {
2612 printk(KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
2613 this->eccmode = NAND_ECC_SOFT;
2614 this->calculate_ecc = nand_calculate_ecc;
2615 this->correct_data = nand_correct_data;
2617 this->eccsize = 512; /* set eccsize to 512 */
2620 case NAND_ECC_HW3_256:
2624 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
2625 this->eccmode = NAND_ECC_NONE;
2629 this->calculate_ecc = nand_calculate_ecc;
2630 this->correct_data = nand_correct_data;
2634 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
2638 /* Check hardware ecc function availability and adjust number of ecc bytes per
2641 switch (this->eccmode) {
2642 case NAND_ECC_HW12_2048:
2643 this->eccbytes += 4;
2644 case NAND_ECC_HW8_512:
2645 this->eccbytes += 2;
2646 case NAND_ECC_HW6_512:
2647 this->eccbytes += 3;
2648 case NAND_ECC_HW3_512:
2649 case NAND_ECC_HW3_256:
2650 if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
2652 printk(KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
2656 mtd->eccsize = this->eccsize;
2658 /* Set the number of read / write steps for one page to ensure ECC generation */
2659 switch (this->eccmode) {
2660 case NAND_ECC_HW12_2048:
2661 this->eccsteps = mtd->oobblock / 2048;
2663 case NAND_ECC_HW3_512:
2664 case NAND_ECC_HW6_512:
2665 case NAND_ECC_HW8_512:
2666 this->eccsteps = mtd->oobblock / 512;
2668 case NAND_ECC_HW3_256:
2670 this->eccsteps = mtd->oobblock / 256;
2678 /* Initialize state, waitqueue and spinlock */
2679 this->state = FL_READY;
2680 if (!this->controller) {
2681 this->controller = kzalloc(sizeof(struct nand_hw_control),
2683 if (!this->controller) {
2684 nand_free_kmem(this);
2687 this->options |= NAND_CONTROLLER_ALLOC;
2689 init_waitqueue_head(&this->controller->wq);
2690 spin_lock_init(&this->controller->lock);
2692 /* De-select the device */
2693 this->select_chip(mtd, -1);
2695 /* Invalidate the pagebuffer reference */
2698 /* Fill in remaining MTD driver data */
2699 mtd->type = MTD_NANDFLASH;
2700 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
2701 mtd->ecctype = MTD_ECC_SW;
2702 mtd->erase = nand_erase;
2704 mtd->unpoint = NULL;
2705 mtd->read = nand_read;
2706 mtd->write = nand_write;
2707 mtd->read_ecc = nand_read_ecc;
2708 mtd->write_ecc = nand_write_ecc;
2709 mtd->read_oob = nand_read_oob;
2710 mtd->write_oob = nand_write_oob;
2712 mtd->writev = nand_writev;
2713 mtd->writev_ecc = nand_writev_ecc;
2714 mtd->sync = nand_sync;
2717 mtd->suspend = nand_suspend;
2718 mtd->resume = nand_resume;
2719 mtd->block_isbad = nand_block_isbad;
2720 mtd->block_markbad = nand_block_markbad;
2722 /* and make the autooob the default one */
2723 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
2725 /* Check, if we should skip the bad block table scan */
2726 if (this->options & NAND_SKIP_BBTSCAN)
2729 /* Build bad block table */
2730 return this->scan_bbt(mtd);
2734 * nand_release - [NAND Interface] Free resources held by the NAND device
2735 * @mtd: MTD device structure
2737 void nand_release(struct mtd_info *mtd)
2739 struct nand_chip *this = mtd->priv;
2741 #ifdef CONFIG_MTD_PARTITIONS
2742 /* Deregister partitions */
2743 del_mtd_partitions(mtd);
2745 /* Deregister the device */
2746 del_mtd_device(mtd);
2748 /* Free bad block table memory */
2751 nand_free_kmem(this);
2754 EXPORT_SYMBOL_GPL(nand_scan);
2755 EXPORT_SYMBOL_GPL(nand_release);
2757 static int __init nand_base_init(void)
2759 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2763 static void __exit nand_base_exit(void)
2765 led_trigger_unregister_simple(nand_led_trigger);
2768 module_init(nand_base_init);
2769 module_exit(nand_base_exit);
2771 MODULE_LICENSE("GPL");
2772 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2773 MODULE_DESCRIPTION("Generic NAND flash driver code");