[MTD] NAND: Allow operation without bad block table
[firewire-audio.git] / drivers / mtd / nand / nand_base.c
blob1806ffae24525053f6ee2f31ae7fa9560ceae7e2
1 /*
2 * drivers/mtd/nand.c
4 * Overview:
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.
8 *
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>
31 * 12-05-2004 dmarlin: add workaround for Renesas AG-AND chips "disturb" issue.
32 * Basically, any block not rewritten may lose data when surrounding blocks
33 * are rewritten many times. JFFS2 ensures this doesn't happen for blocks
34 * it uses, but the Bad Block Table(s) may not be rewritten. To ensure they
35 * do not lose data, force them to be rewritten when some of the surrounding
36 * blocks are erased. Rather than tracking a specific nearby block (which
37 * could itself go bad), use a page address 'mask' to select several blocks
38 * in the same area, and rewrite the BBT when any of them are erased.
40 * 01-03-2005 dmarlin: added support for the device recovery command sequence for Renesas
41 * AG-AND chips. If there was a sudden loss of power during an erase operation,
42 * a "device recovery" operation must be performed when power is restored
43 * to ensure correct operation.
45 * 01-20-2005 dmarlin: added support for optional hardware specific callback routine to
46 * perform extra error status checks on erase and write failures. This required
47 * adding a wrapper function for nand_read_ecc.
49 * Credits:
50 * David Woodhouse for adding multichip support
52 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
53 * rework for 2K page size chips
55 * TODO:
56 * Enable cached programming for 2k page size chips
57 * Check, if mtd->ecctype should be set to MTD_ECC_HW
58 * if we have HW ecc support.
59 * The AG-AND chips have nice features for speed improvement,
60 * which are not supported yet. Read / program 4 pages in one go.
62 * $Id: nand_base.c,v 1.132 2005/02/09 14:49:56 dedekind Exp $
64 * This program is free software; you can redistribute it and/or modify
65 * it under the terms of the GNU General Public License version 2 as
66 * published by the Free Software Foundation.
70 #include <linux/delay.h>
71 #include <linux/errno.h>
72 #include <linux/sched.h>
73 #include <linux/slab.h>
74 #include <linux/types.h>
75 #include <linux/mtd/mtd.h>
76 #include <linux/mtd/nand.h>
77 #include <linux/mtd/nand_ecc.h>
78 #include <linux/mtd/compatmac.h>
79 #include <linux/interrupt.h>
80 #include <linux/bitops.h>
81 #include <asm/io.h>
83 #ifdef CONFIG_MTD_PARTITIONS
84 #include <linux/mtd/partitions.h>
85 #endif
87 /* Define default oob placement schemes for large and small page devices */
88 static struct nand_oobinfo nand_oob_8 = {
89 .useecc = MTD_NANDECC_AUTOPLACE,
90 .eccbytes = 3,
91 .eccpos = {0, 1, 2},
92 .oobfree = { {3, 2}, {6, 2} }
95 static struct nand_oobinfo nand_oob_16 = {
96 .useecc = MTD_NANDECC_AUTOPLACE,
97 .eccbytes = 6,
98 .eccpos = {0, 1, 2, 3, 6, 7},
99 .oobfree = { {8, 8} }
102 static struct nand_oobinfo nand_oob_64 = {
103 .useecc = MTD_NANDECC_AUTOPLACE,
104 .eccbytes = 24,
105 .eccpos = {
106 40, 41, 42, 43, 44, 45, 46, 47,
107 48, 49, 50, 51, 52, 53, 54, 55,
108 56, 57, 58, 59, 60, 61, 62, 63},
109 .oobfree = { {2, 38} }
112 /* This is used for padding purposes in nand_write_oob */
113 static u_char ffchars[] = {
114 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
115 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
116 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
117 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
118 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
119 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
120 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
121 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
125 * NAND low-level MTD interface functions
127 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
128 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
129 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
131 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
132 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
133 size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
134 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
135 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf);
136 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
137 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
138 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char *buf);
139 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
140 unsigned long count, loff_t to, size_t * retlen);
141 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs,
142 unsigned long count, loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
143 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
144 static void nand_sync (struct mtd_info *mtd);
146 /* Some internal functions */
147 static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf,
148 struct nand_oobinfo *oobsel, int mode);
149 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
150 static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
151 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode);
152 #else
153 #define nand_verify_pages(...) (0)
154 #endif
156 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state);
159 * nand_release_device - [GENERIC] release chip
160 * @mtd: MTD device structure
162 * Deselect, release chip lock and wake up anyone waiting on the device
164 static void nand_release_device (struct mtd_info *mtd)
166 struct nand_chip *this = mtd->priv;
168 /* De-select the NAND device */
169 this->select_chip(mtd, -1);
170 /* Do we have a hardware controller ? */
171 if (this->controller) {
172 spin_lock(&this->controller->lock);
173 this->controller->active = NULL;
174 spin_unlock(&this->controller->lock);
176 /* Release the chip */
177 spin_lock (&this->chip_lock);
178 this->state = FL_READY;
179 wake_up (&this->wq);
180 spin_unlock (&this->chip_lock);
184 * nand_read_byte - [DEFAULT] read one byte from the chip
185 * @mtd: MTD device structure
187 * Default read function for 8bit buswith
189 static u_char nand_read_byte(struct mtd_info *mtd)
191 struct nand_chip *this = mtd->priv;
192 return readb(this->IO_ADDR_R);
196 * nand_write_byte - [DEFAULT] write one byte to the chip
197 * @mtd: MTD device structure
198 * @byte: pointer to data byte to write
200 * Default write function for 8it buswith
202 static void nand_write_byte(struct mtd_info *mtd, u_char byte)
204 struct nand_chip *this = mtd->priv;
205 writeb(byte, this->IO_ADDR_W);
209 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
210 * @mtd: MTD device structure
212 * Default read function for 16bit buswith with
213 * endianess conversion
215 static u_char nand_read_byte16(struct mtd_info *mtd)
217 struct nand_chip *this = mtd->priv;
218 return (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
222 * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
223 * @mtd: MTD device structure
224 * @byte: pointer to data byte to write
226 * Default write function for 16bit buswith with
227 * endianess conversion
229 static void nand_write_byte16(struct mtd_info *mtd, u_char byte)
231 struct nand_chip *this = mtd->priv;
232 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
236 * nand_read_word - [DEFAULT] read one word from the chip
237 * @mtd: MTD device structure
239 * Default read function for 16bit buswith without
240 * endianess conversion
242 static u16 nand_read_word(struct mtd_info *mtd)
244 struct nand_chip *this = mtd->priv;
245 return readw(this->IO_ADDR_R);
249 * nand_write_word - [DEFAULT] write one word to the chip
250 * @mtd: MTD device structure
251 * @word: data word to write
253 * Default write function for 16bit buswith without
254 * endianess conversion
256 static void nand_write_word(struct mtd_info *mtd, u16 word)
258 struct nand_chip *this = mtd->priv;
259 writew(word, this->IO_ADDR_W);
263 * nand_select_chip - [DEFAULT] control CE line
264 * @mtd: MTD device structure
265 * @chip: chipnumber to select, -1 for deselect
267 * Default select function for 1 chip devices.
269 static void nand_select_chip(struct mtd_info *mtd, int chip)
271 struct nand_chip *this = mtd->priv;
272 switch(chip) {
273 case -1:
274 this->hwcontrol(mtd, NAND_CTL_CLRNCE);
275 break;
276 case 0:
277 this->hwcontrol(mtd, NAND_CTL_SETNCE);
278 break;
280 default:
281 BUG();
286 * nand_write_buf - [DEFAULT] write buffer to chip
287 * @mtd: MTD device structure
288 * @buf: data buffer
289 * @len: number of bytes to write
291 * Default write function for 8bit buswith
293 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
295 int i;
296 struct nand_chip *this = mtd->priv;
298 for (i=0; i<len; i++)
299 writeb(buf[i], this->IO_ADDR_W);
303 * nand_read_buf - [DEFAULT] read chip data into buffer
304 * @mtd: MTD device structure
305 * @buf: buffer to store date
306 * @len: number of bytes to read
308 * Default read function for 8bit buswith
310 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
312 int i;
313 struct nand_chip *this = mtd->priv;
315 for (i=0; i<len; i++)
316 buf[i] = readb(this->IO_ADDR_R);
320 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
321 * @mtd: MTD device structure
322 * @buf: buffer containing the data to compare
323 * @len: number of bytes to compare
325 * Default verify function for 8bit buswith
327 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
329 int i;
330 struct nand_chip *this = mtd->priv;
332 for (i=0; i<len; i++)
333 if (buf[i] != readb(this->IO_ADDR_R))
334 return -EFAULT;
336 return 0;
340 * nand_write_buf16 - [DEFAULT] write buffer to chip
341 * @mtd: MTD device structure
342 * @buf: data buffer
343 * @len: number of bytes to write
345 * Default write function for 16bit buswith
347 static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
349 int i;
350 struct nand_chip *this = mtd->priv;
351 u16 *p = (u16 *) buf;
352 len >>= 1;
354 for (i=0; i<len; i++)
355 writew(p[i], this->IO_ADDR_W);
360 * nand_read_buf16 - [DEFAULT] read chip data into buffer
361 * @mtd: MTD device structure
362 * @buf: buffer to store date
363 * @len: number of bytes to read
365 * Default read function for 16bit buswith
367 static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
369 int i;
370 struct nand_chip *this = mtd->priv;
371 u16 *p = (u16 *) buf;
372 len >>= 1;
374 for (i=0; i<len; i++)
375 p[i] = readw(this->IO_ADDR_R);
379 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
380 * @mtd: MTD device structure
381 * @buf: buffer containing the data to compare
382 * @len: number of bytes to compare
384 * Default verify function for 16bit buswith
386 static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
388 int i;
389 struct nand_chip *this = mtd->priv;
390 u16 *p = (u16 *) buf;
391 len >>= 1;
393 for (i=0; i<len; i++)
394 if (p[i] != readw(this->IO_ADDR_R))
395 return -EFAULT;
397 return 0;
401 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
402 * @mtd: MTD device structure
403 * @ofs: offset from device start
404 * @getchip: 0, if the chip is already selected
406 * Check, if the block is bad.
408 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
410 int page, chipnr, res = 0;
411 struct nand_chip *this = mtd->priv;
412 u16 bad;
414 if (getchip) {
415 page = (int)(ofs >> this->page_shift);
416 chipnr = (int)(ofs >> this->chip_shift);
418 /* Grab the lock and see if the device is available */
419 nand_get_device (this, mtd, FL_READING);
421 /* Select the NAND device */
422 this->select_chip(mtd, chipnr);
423 } else
424 page = (int) ofs;
426 if (this->options & NAND_BUSWIDTH_16) {
427 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page & this->pagemask);
428 bad = cpu_to_le16(this->read_word(mtd));
429 if (this->badblockpos & 0x1)
430 bad >>= 1;
431 if ((bad & 0xFF) != 0xff)
432 res = 1;
433 } else {
434 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page & this->pagemask);
435 if (this->read_byte(mtd) != 0xff)
436 res = 1;
439 if (getchip) {
440 /* Deselect and wake up anyone waiting on the device */
441 nand_release_device(mtd);
444 return res;
448 * nand_default_block_markbad - [DEFAULT] mark a block bad
449 * @mtd: MTD device structure
450 * @ofs: offset from device start
452 * This is the default implementation, which can be overridden by
453 * a hardware specific driver.
455 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
457 struct nand_chip *this = mtd->priv;
458 u_char buf[2] = {0, 0};
459 size_t retlen;
460 int block;
462 /* Get block number */
463 block = ((int) ofs) >> this->bbt_erase_shift;
464 if (this->bbt)
465 this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
467 /* Do we have a flash based bad block table ? */
468 if (this->options & NAND_USE_FLASH_BBT)
469 return nand_update_bbt (mtd, ofs);
471 /* We write two bytes, so we dont have to mess with 16 bit access */
472 ofs += mtd->oobsize + (this->badblockpos & ~0x01);
473 return nand_write_oob (mtd, ofs , 2, &retlen, buf);
476 /**
477 * nand_check_wp - [GENERIC] check if the chip is write protected
478 * @mtd: MTD device structure
479 * Check, if the device is write protected
481 * The function expects, that the device is already selected
483 static int nand_check_wp (struct mtd_info *mtd)
485 struct nand_chip *this = mtd->priv;
486 /* Check the WP bit */
487 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
488 return (this->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
492 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
493 * @mtd: MTD device structure
494 * @ofs: offset from device start
495 * @getchip: 0, if the chip is already selected
496 * @allowbbt: 1, if its allowed to access the bbt area
498 * Check, if the block is bad. Either by reading the bad block table or
499 * calling of the scan function.
501 static int nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
503 struct nand_chip *this = mtd->priv;
505 if (!this->bbt)
506 return this->block_bad(mtd, ofs, getchip);
508 /* Return info from the table */
509 return nand_isbad_bbt (mtd, ofs, allowbbt);
513 * nand_command - [DEFAULT] Send command to NAND device
514 * @mtd: MTD device structure
515 * @command: the command to be sent
516 * @column: the column address for this command, -1 if none
517 * @page_addr: the page address for this command, -1 if none
519 * Send command to NAND device. This function is used for small page
520 * devices (256/512 Bytes per page)
522 static void nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
524 register struct nand_chip *this = mtd->priv;
526 /* Begin command latch cycle */
527 this->hwcontrol(mtd, NAND_CTL_SETCLE);
529 * Write out the command to the device.
531 if (command == NAND_CMD_SEQIN) {
532 int readcmd;
534 if (column >= mtd->oobblock) {
535 /* OOB area */
536 column -= mtd->oobblock;
537 readcmd = NAND_CMD_READOOB;
538 } else if (column < 256) {
539 /* First 256 bytes --> READ0 */
540 readcmd = NAND_CMD_READ0;
541 } else {
542 column -= 256;
543 readcmd = NAND_CMD_READ1;
545 this->write_byte(mtd, readcmd);
547 this->write_byte(mtd, command);
549 /* Set ALE and clear CLE to start address cycle */
550 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
552 if (column != -1 || page_addr != -1) {
553 this->hwcontrol(mtd, NAND_CTL_SETALE);
555 /* Serially input address */
556 if (column != -1) {
557 /* Adjust columns for 16 bit buswidth */
558 if (this->options & NAND_BUSWIDTH_16)
559 column >>= 1;
560 this->write_byte(mtd, column);
562 if (page_addr != -1) {
563 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
564 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
565 /* One more address cycle for devices > 32MiB */
566 if (this->chipsize > (32 << 20))
567 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));
569 /* Latch in address */
570 this->hwcontrol(mtd, NAND_CTL_CLRALE);
574 * program and erase have their own busy handlers
575 * status and sequential in needs no delay
577 switch (command) {
579 case NAND_CMD_PAGEPROG:
580 case NAND_CMD_ERASE1:
581 case NAND_CMD_ERASE2:
582 case NAND_CMD_SEQIN:
583 case NAND_CMD_STATUS:
584 return;
586 case NAND_CMD_RESET:
587 if (this->dev_ready)
588 break;
589 udelay(this->chip_delay);
590 this->hwcontrol(mtd, NAND_CTL_SETCLE);
591 this->write_byte(mtd, NAND_CMD_STATUS);
592 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
593 while ( !(this->read_byte(mtd) & NAND_STATUS_READY));
594 return;
596 /* This applies to read commands */
597 default:
599 * If we don't have access to the busy pin, we apply the given
600 * command delay
602 if (!this->dev_ready) {
603 udelay (this->chip_delay);
604 return;
608 /* Apply this short delay always to ensure that we do wait tWB in
609 * any case on any machine. */
610 ndelay (100);
611 /* wait until command is processed */
612 while (!this->dev_ready(mtd));
616 * nand_command_lp - [DEFAULT] Send command to NAND large page device
617 * @mtd: MTD device structure
618 * @command: the command to be sent
619 * @column: the column address for this command, -1 if none
620 * @page_addr: the page address for this command, -1 if none
622 * Send command to NAND device. This is the version for the new large page devices
623 * We dont have the seperate regions as we have in the small page devices.
624 * We must emulate NAND_CMD_READOOB to keep the code compatible.
627 static void nand_command_lp (struct mtd_info *mtd, unsigned command, int column, int page_addr)
629 register struct nand_chip *this = mtd->priv;
631 /* Emulate NAND_CMD_READOOB */
632 if (command == NAND_CMD_READOOB) {
633 column += mtd->oobblock;
634 command = NAND_CMD_READ0;
638 /* Begin command latch cycle */
639 this->hwcontrol(mtd, NAND_CTL_SETCLE);
640 /* Write out the command to the device. */
641 this->write_byte(mtd, (command & 0xff));
642 /* End command latch cycle */
643 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
645 if (column != -1 || page_addr != -1) {
646 this->hwcontrol(mtd, NAND_CTL_SETALE);
648 /* Serially input address */
649 if (column != -1) {
650 /* Adjust columns for 16 bit buswidth */
651 if (this->options & NAND_BUSWIDTH_16)
652 column >>= 1;
653 this->write_byte(mtd, column & 0xff);
654 this->write_byte(mtd, column >> 8);
656 if (page_addr != -1) {
657 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
658 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
659 /* One more address cycle for devices > 128MiB */
660 if (this->chipsize > (128 << 20))
661 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0xff));
663 /* Latch in address */
664 this->hwcontrol(mtd, NAND_CTL_CLRALE);
668 * program and erase have their own busy handlers
669 * status, sequential in, and deplete1 need no delay
671 switch (command) {
673 case NAND_CMD_CACHEDPROG:
674 case NAND_CMD_PAGEPROG:
675 case NAND_CMD_ERASE1:
676 case NAND_CMD_ERASE2:
677 case NAND_CMD_SEQIN:
678 case NAND_CMD_STATUS:
679 case NAND_CMD_DEPLETE1:
680 return;
683 * read error status commands require only a short delay
685 case NAND_CMD_STATUS_ERROR:
686 case NAND_CMD_STATUS_ERROR0:
687 case NAND_CMD_STATUS_ERROR1:
688 case NAND_CMD_STATUS_ERROR2:
689 case NAND_CMD_STATUS_ERROR3:
690 udelay(this->chip_delay);
691 return;
693 case NAND_CMD_RESET:
694 if (this->dev_ready)
695 break;
696 udelay(this->chip_delay);
697 this->hwcontrol(mtd, NAND_CTL_SETCLE);
698 this->write_byte(mtd, NAND_CMD_STATUS);
699 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
700 while ( !(this->read_byte(mtd) & NAND_STATUS_READY));
701 return;
703 case NAND_CMD_READ0:
704 /* Begin command latch cycle */
705 this->hwcontrol(mtd, NAND_CTL_SETCLE);
706 /* Write out the start read command */
707 this->write_byte(mtd, NAND_CMD_READSTART);
708 /* End command latch cycle */
709 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
710 /* Fall through into ready check */
712 /* This applies to read commands */
713 default:
715 * If we don't have access to the busy pin, we apply the given
716 * command delay
718 if (!this->dev_ready) {
719 udelay (this->chip_delay);
720 return;
724 /* Apply this short delay always to ensure that we do wait tWB in
725 * any case on any machine. */
726 ndelay (100);
727 /* wait until command is processed */
728 while (!this->dev_ready(mtd));
732 * nand_get_device - [GENERIC] Get chip for selected access
733 * @this: the nand chip descriptor
734 * @mtd: MTD device structure
735 * @new_state: the state which is requested
737 * Get the device and lock it for exclusive access
739 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
741 struct nand_chip *active = this;
743 DECLARE_WAITQUEUE (wait, current);
746 * Grab the lock and see if the device is available
748 retry:
749 /* Hardware controller shared among independend devices */
750 if (this->controller) {
751 spin_lock (&this->controller->lock);
752 if (this->controller->active)
753 active = this->controller->active;
754 else
755 this->controller->active = this;
756 spin_unlock (&this->controller->lock);
759 if (active == this) {
760 spin_lock (&this->chip_lock);
761 if (this->state == FL_READY) {
762 this->state = new_state;
763 spin_unlock (&this->chip_lock);
764 return;
767 set_current_state (TASK_UNINTERRUPTIBLE);
768 add_wait_queue (&active->wq, &wait);
769 spin_unlock (&active->chip_lock);
770 schedule ();
771 remove_wait_queue (&active->wq, &wait);
772 goto retry;
776 * nand_wait - [DEFAULT] wait until the command is done
777 * @mtd: MTD device structure
778 * @this: NAND chip structure
779 * @state: state to select the max. timeout value
781 * Wait for command done. This applies to erase and program only
782 * Erase can take up to 400ms and program up to 20ms according to
783 * general NAND and SmartMedia specs
786 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
789 unsigned long timeo = jiffies;
790 int status;
792 if (state == FL_ERASING)
793 timeo += (HZ * 400) / 1000;
794 else
795 timeo += (HZ * 20) / 1000;
797 /* Apply this short delay always to ensure that we do wait tWB in
798 * any case on any machine. */
799 ndelay (100);
801 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
802 this->cmdfunc (mtd, NAND_CMD_STATUS_MULTI, -1, -1);
803 else
804 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
806 while (time_before(jiffies, timeo)) {
807 /* Check, if we were interrupted */
808 if (this->state != state)
809 return 0;
811 if (this->dev_ready) {
812 if (this->dev_ready(mtd))
813 break;
814 } else {
815 if (this->read_byte(mtd) & NAND_STATUS_READY)
816 break;
818 msleep(1);
820 status = (int) this->read_byte(mtd);
821 return status;
825 * nand_write_page - [GENERIC] write one page
826 * @mtd: MTD device structure
827 * @this: NAND chip structure
828 * @page: startpage inside the chip, must be called with (page & this->pagemask)
829 * @oob_buf: out of band data buffer
830 * @oobsel: out of band selecttion structre
831 * @cached: 1 = enable cached programming if supported by chip
833 * Nand_page_program function is used for write and writev !
834 * This function will always program a full page of data
835 * If you call it with a non page aligned buffer, you're lost :)
837 * Cached programming is not supported yet.
839 static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page,
840 u_char *oob_buf, struct nand_oobinfo *oobsel, int cached)
842 int i, status;
843 u_char ecc_code[32];
844 int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
845 int *oob_config = oobsel->eccpos;
846 int datidx = 0, eccidx = 0, eccsteps = this->eccsteps;
847 int eccbytes = 0;
849 /* FIXME: Enable cached programming */
850 cached = 0;
852 /* Send command to begin auto page programming */
853 this->cmdfunc (mtd, NAND_CMD_SEQIN, 0x00, page);
855 /* Write out complete page of data, take care of eccmode */
856 switch (eccmode) {
857 /* No ecc, write all */
858 case NAND_ECC_NONE:
859 printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
860 this->write_buf(mtd, this->data_poi, mtd->oobblock);
861 break;
863 /* Software ecc 3/256, write all */
864 case NAND_ECC_SOFT:
865 for (; eccsteps; eccsteps--) {
866 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
867 for (i = 0; i < 3; i++, eccidx++)
868 oob_buf[oob_config[eccidx]] = ecc_code[i];
869 datidx += this->eccsize;
871 this->write_buf(mtd, this->data_poi, mtd->oobblock);
872 break;
873 default:
874 eccbytes = this->eccbytes;
875 for (; eccsteps; eccsteps--) {
876 /* enable hardware ecc logic for write */
877 this->enable_hwecc(mtd, NAND_ECC_WRITE);
878 this->write_buf(mtd, &this->data_poi[datidx], this->eccsize);
879 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
880 for (i = 0; i < eccbytes; i++, eccidx++)
881 oob_buf[oob_config[eccidx]] = ecc_code[i];
882 /* If the hardware ecc provides syndromes then
883 * the ecc code must be written immidiately after
884 * the data bytes (words) */
885 if (this->options & NAND_HWECC_SYNDROME)
886 this->write_buf(mtd, ecc_code, eccbytes);
887 datidx += this->eccsize;
889 break;
892 /* Write out OOB data */
893 if (this->options & NAND_HWECC_SYNDROME)
894 this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
895 else
896 this->write_buf(mtd, oob_buf, mtd->oobsize);
898 /* Send command to actually program the data */
899 this->cmdfunc (mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
901 if (!cached) {
902 /* call wait ready function */
903 status = this->waitfunc (mtd, this, FL_WRITING);
905 /* See if operation failed and additional status checks are available */
906 if ((status & NAND_STATUS_FAIL) && (this->errstat)) {
907 status = this->errstat(mtd, this, FL_WRITING, status, page);
910 /* See if device thinks it succeeded */
911 if (status & NAND_STATUS_FAIL) {
912 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
913 return -EIO;
915 } else {
916 /* FIXME: Implement cached programming ! */
917 /* wait until cache is ready*/
918 // status = this->waitfunc (mtd, this, FL_CACHEDRPG);
920 return 0;
923 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
925 * nand_verify_pages - [GENERIC] verify the chip contents after a write
926 * @mtd: MTD device structure
927 * @this: NAND chip structure
928 * @page: startpage inside the chip, must be called with (page & this->pagemask)
929 * @numpages: number of pages to verify
930 * @oob_buf: out of band data buffer
931 * @oobsel: out of band selecttion structre
932 * @chipnr: number of the current chip
933 * @oobmode: 1 = full buffer verify, 0 = ecc only
935 * The NAND device assumes that it is always writing to a cleanly erased page.
936 * Hence, it performs its internal write verification only on bits that
937 * transitioned from 1 to 0. The device does NOT verify the whole page on a
938 * byte by byte basis. It is possible that the page was not completely erased
939 * or the page is becoming unusable due to wear. The read with ECC would catch
940 * the error later when the ECC page check fails, but we would rather catch
941 * it early in the page write stage. Better to write no data than invalid data.
943 static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
944 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
946 int i, j, datidx = 0, oobofs = 0, res = -EIO;
947 int eccsteps = this->eccsteps;
948 int hweccbytes;
949 u_char oobdata[64];
951 hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
953 /* Send command to read back the first page */
954 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page);
956 for(;;) {
957 for (j = 0; j < eccsteps; j++) {
958 /* Loop through and verify the data */
959 if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
960 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
961 goto out;
963 datidx += mtd->eccsize;
964 /* Have we a hw generator layout ? */
965 if (!hweccbytes)
966 continue;
967 if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
968 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
969 goto out;
971 oobofs += hweccbytes;
974 /* check, if we must compare all data or if we just have to
975 * compare the ecc bytes
977 if (oobmode) {
978 if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
979 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
980 goto out;
982 } else {
983 /* Read always, else autoincrement fails */
984 this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
986 if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
987 int ecccnt = oobsel->eccbytes;
989 for (i = 0; i < ecccnt; i++) {
990 int idx = oobsel->eccpos[i];
991 if (oobdata[idx] != oob_buf[oobofs + idx] ) {
992 DEBUG (MTD_DEBUG_LEVEL0,
993 "%s: Failed ECC write "
994 "verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__, page, i);
995 goto out;
1000 oobofs += mtd->oobsize - hweccbytes * eccsteps;
1001 page++;
1002 numpages--;
1004 /* Apply delay or wait for ready/busy pin
1005 * Do this before the AUTOINCR check, so no problems
1006 * arise if a chip which does auto increment
1007 * is marked as NOAUTOINCR by the board driver.
1008 * Do this also before returning, so the chip is
1009 * ready for the next command.
1011 if (!this->dev_ready)
1012 udelay (this->chip_delay);
1013 else
1014 while (!this->dev_ready(mtd));
1016 /* All done, return happy */
1017 if (!numpages)
1018 return 0;
1021 /* Check, if the chip supports auto page increment */
1022 if (!NAND_CANAUTOINCR(this))
1023 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1026 * Terminate the read command. We come here in case of an error
1027 * So we must issue a reset command.
1029 out:
1030 this->cmdfunc (mtd, NAND_CMD_RESET, -1, -1);
1031 return res;
1033 #endif
1036 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1037 * @mtd: MTD device structure
1038 * @from: offset to read from
1039 * @len: number of bytes to read
1040 * @retlen: pointer to variable to store the number of read bytes
1041 * @buf: the databuffer to put data
1043 * This function simply calls nand_do_read_ecc with oob buffer and oobsel = NULL
1044 * and flags = 0xff
1046 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1048 return nand_do_read_ecc (mtd, from, len, retlen, buf, NULL, NULL, 0xff);
1053 * nand_read_ecc - [MTD Interface] MTD compability function for nand_do_read_ecc
1054 * @mtd: MTD device structure
1055 * @from: offset to read from
1056 * @len: number of bytes to read
1057 * @retlen: pointer to variable to store the number of read bytes
1058 * @buf: the databuffer to put data
1059 * @oob_buf: filesystem supplied oob data buffer
1060 * @oobsel: oob selection structure
1062 * This function simply calls nand_do_read_ecc with flags = 0xff
1064 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
1065 size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel)
1067 return nand_do_read_ecc(mtd, from, len, retlen, buf, oob_buf, oobsel, 0xff);
1072 * nand_do_read_ecc - [MTD Interface] Read data with ECC
1073 * @mtd: MTD device structure
1074 * @from: offset to read from
1075 * @len: number of bytes to read
1076 * @retlen: pointer to variable to store the number of read bytes
1077 * @buf: the databuffer to put data
1078 * @oob_buf: filesystem supplied oob data buffer
1079 * @oobsel: oob selection structure
1080 * @flags: flag to indicate if nand_get_device/nand_release_device should be preformed
1081 * and how many corrected error bits are acceptable:
1082 * bits 0..7 - number of tolerable errors
1083 * bit 8 - 0 == do not get/release chip, 1 == get/release chip
1085 * NAND read with ECC
1087 int nand_do_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
1088 size_t * retlen, u_char * buf, u_char * oob_buf,
1089 struct nand_oobinfo *oobsel, int flags)
1091 int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
1092 int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
1093 struct nand_chip *this = mtd->priv;
1094 u_char *data_poi, *oob_data = oob_buf;
1095 u_char ecc_calc[32];
1096 u_char ecc_code[32];
1097 int eccmode, eccsteps;
1098 int *oob_config, datidx;
1099 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1100 int eccbytes;
1101 int compareecc = 1;
1102 int oobreadlen;
1105 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1107 /* Do not allow reads past end of device */
1108 if ((from + len) > mtd->size) {
1109 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
1110 *retlen = 0;
1111 return -EINVAL;
1114 /* Grab the lock and see if the device is available */
1115 if (flags & NAND_GET_DEVICE)
1116 nand_get_device (this, mtd, FL_READING);
1118 /* use userspace supplied oobinfo, if zero */
1119 if (oobsel == NULL)
1120 oobsel = &mtd->oobinfo;
1122 /* Autoplace of oob data ? Use the default placement scheme */
1123 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1124 oobsel = this->autooob;
1126 eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
1127 oob_config = oobsel->eccpos;
1129 /* Select the NAND device */
1130 chipnr = (int)(from >> this->chip_shift);
1131 this->select_chip(mtd, chipnr);
1133 /* First we calculate the starting page */
1134 realpage = (int) (from >> this->page_shift);
1135 page = realpage & this->pagemask;
1137 /* Get raw starting column */
1138 col = from & (mtd->oobblock - 1);
1140 end = mtd->oobblock;
1141 ecc = this->eccsize;
1142 eccbytes = this->eccbytes;
1144 if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
1145 compareecc = 0;
1147 oobreadlen = mtd->oobsize;
1148 if (this->options & NAND_HWECC_SYNDROME)
1149 oobreadlen -= oobsel->eccbytes;
1151 /* Loop until all data read */
1152 while (read < len) {
1154 int aligned = (!col && (len - read) >= end);
1156 * If the read is not page aligned, we have to read into data buffer
1157 * due to ecc, else we read into return buffer direct
1159 if (aligned)
1160 data_poi = &buf[read];
1161 else
1162 data_poi = this->data_buf;
1164 /* Check, if we have this page in the buffer
1166 * FIXME: Make it work when we must provide oob data too,
1167 * check the usage of data_buf oob field
1169 if (realpage == this->pagebuf && !oob_buf) {
1170 /* aligned read ? */
1171 if (aligned)
1172 memcpy (data_poi, this->data_buf, end);
1173 goto readdata;
1176 /* Check, if we must send the read command */
1177 if (sndcmd) {
1178 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1179 sndcmd = 0;
1182 /* get oob area, if we have no oob buffer from fs-driver */
1183 if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1184 oob_data = &this->data_buf[end];
1186 eccsteps = this->eccsteps;
1188 switch (eccmode) {
1189 case NAND_ECC_NONE: { /* No ECC, Read in a page */
1190 static unsigned long lastwhinge = 0;
1191 if ((lastwhinge / HZ) != (jiffies / HZ)) {
1192 printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended\n");
1193 lastwhinge = jiffies;
1195 this->read_buf(mtd, data_poi, end);
1196 break;
1199 case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
1200 this->read_buf(mtd, data_poi, end);
1201 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc)
1202 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1203 break;
1205 default:
1206 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) {
1207 this->enable_hwecc(mtd, NAND_ECC_READ);
1208 this->read_buf(mtd, &data_poi[datidx], ecc);
1210 /* HW ecc with syndrome calculation must read the
1211 * syndrome from flash immidiately after the data */
1212 if (!compareecc) {
1213 /* Some hw ecc generators need to know when the
1214 * syndrome is read from flash */
1215 this->enable_hwecc(mtd, NAND_ECC_READSYN);
1216 this->read_buf(mtd, &oob_data[i], eccbytes);
1217 /* We calc error correction directly, it checks the hw
1218 * generator for an error, reads back the syndrome and
1219 * does the error correction on the fly */
1220 ecc_status = this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]);
1221 if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) {
1222 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: "
1223 "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
1224 ecc_failed++;
1226 } else {
1227 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1230 break;
1233 /* read oobdata */
1234 this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
1236 /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
1237 if (!compareecc)
1238 goto readoob;
1240 /* Pick the ECC bytes out of the oob data */
1241 for (j = 0; j < oobsel->eccbytes; j++)
1242 ecc_code[j] = oob_data[oob_config[j]];
1244 /* correct data, if neccecary */
1245 for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {
1246 ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
1248 /* Get next chunk of ecc bytes */
1249 j += eccbytes;
1251 /* Check, if we have a fs supplied oob-buffer,
1252 * This is the legacy mode. Used by YAFFS1
1253 * Should go away some day
1255 if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
1256 int *p = (int *)(&oob_data[mtd->oobsize]);
1257 p[i] = ecc_status;
1260 if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) {
1261 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
1262 ecc_failed++;
1266 readoob:
1267 /* check, if we have a fs supplied oob-buffer */
1268 if (oob_buf) {
1269 /* without autoplace. Legacy mode used by YAFFS1 */
1270 switch(oobsel->useecc) {
1271 case MTD_NANDECC_AUTOPLACE:
1272 /* Walk through the autoplace chunks */
1273 for (i = 0, j = 0; j < mtd->oobavail; i++) {
1274 int from = oobsel->oobfree[i][0];
1275 int num = oobsel->oobfree[i][1];
1276 memcpy(&oob_buf[oob], &oob_data[from], num);
1277 j+= num;
1279 oob += mtd->oobavail;
1280 break;
1281 case MTD_NANDECC_PLACE:
1282 /* YAFFS1 legacy mode */
1283 oob_data += this->eccsteps * sizeof (int);
1284 default:
1285 oob_data += mtd->oobsize;
1288 readdata:
1289 /* Partial page read, transfer data into fs buffer */
1290 if (!aligned) {
1291 for (j = col; j < end && read < len; j++)
1292 buf[read++] = data_poi[j];
1293 this->pagebuf = realpage;
1294 } else
1295 read += mtd->oobblock;
1297 /* Apply delay or wait for ready/busy pin
1298 * Do this before the AUTOINCR check, so no problems
1299 * arise if a chip which does auto increment
1300 * is marked as NOAUTOINCR by the board driver.
1302 if (!this->dev_ready)
1303 udelay (this->chip_delay);
1304 else
1305 while (!this->dev_ready(mtd));
1307 if (read == len)
1308 break;
1310 /* For subsequent reads align to page boundary. */
1311 col = 0;
1312 /* Increment page address */
1313 realpage++;
1315 page = realpage & this->pagemask;
1316 /* Check, if we cross a chip boundary */
1317 if (!page) {
1318 chipnr++;
1319 this->select_chip(mtd, -1);
1320 this->select_chip(mtd, chipnr);
1322 /* Check, if the chip supports auto page increment
1323 * or if we have hit a block boundary.
1325 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1326 sndcmd = 1;
1329 /* Deselect and wake up anyone waiting on the device */
1330 if (flags & NAND_GET_DEVICE)
1331 nand_release_device(mtd);
1334 * Return success, if no ECC failures, else -EBADMSG
1335 * fs driver will take care of that, because
1336 * retlen == desired len and result == -EBADMSG
1338 *retlen = read;
1339 return ecc_failed ? -EBADMSG : 0;
1343 * nand_read_oob - [MTD Interface] NAND read out-of-band
1344 * @mtd: MTD device structure
1345 * @from: offset to read from
1346 * @len: number of bytes to read
1347 * @retlen: pointer to variable to store the number of read bytes
1348 * @buf: the databuffer to put data
1350 * NAND read out-of-band data from the spare area
1352 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1354 int i, col, page, chipnr;
1355 struct nand_chip *this = mtd->priv;
1356 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1358 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1360 /* Shift to get page */
1361 page = (int)(from >> this->page_shift);
1362 chipnr = (int)(from >> this->chip_shift);
1364 /* Mask to get column */
1365 col = from & (mtd->oobsize - 1);
1367 /* Initialize return length value */
1368 *retlen = 0;
1370 /* Do not allow reads past end of device */
1371 if ((from + len) > mtd->size) {
1372 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n");
1373 *retlen = 0;
1374 return -EINVAL;
1377 /* Grab the lock and see if the device is available */
1378 nand_get_device (this, mtd , FL_READING);
1380 /* Select the NAND device */
1381 this->select_chip(mtd, chipnr);
1383 /* Send the read command */
1384 this->cmdfunc (mtd, NAND_CMD_READOOB, col, page & this->pagemask);
1386 * Read the data, if we read more than one page
1387 * oob data, let the device transfer the data !
1389 i = 0;
1390 while (i < len) {
1391 int thislen = mtd->oobsize - col;
1392 thislen = min_t(int, thislen, len);
1393 this->read_buf(mtd, &buf[i], thislen);
1394 i += thislen;
1396 /* Apply delay or wait for ready/busy pin
1397 * Do this before the AUTOINCR check, so no problems
1398 * arise if a chip which does auto increment
1399 * is marked as NOAUTOINCR by the board driver.
1401 if (!this->dev_ready)
1402 udelay (this->chip_delay);
1403 else
1404 while (!this->dev_ready(mtd));
1406 /* Read more ? */
1407 if (i < len) {
1408 page++;
1409 col = 0;
1411 /* Check, if we cross a chip boundary */
1412 if (!(page & this->pagemask)) {
1413 chipnr++;
1414 this->select_chip(mtd, -1);
1415 this->select_chip(mtd, chipnr);
1418 /* Check, if the chip supports auto page increment
1419 * or if we have hit a block boundary.
1421 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
1422 /* For subsequent page reads set offset to 0 */
1423 this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
1428 /* Deselect and wake up anyone waiting on the device */
1429 nand_release_device(mtd);
1431 /* Return happy */
1432 *retlen = len;
1433 return 0;
1437 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
1438 * @mtd: MTD device structure
1439 * @buf: temporary buffer
1440 * @from: offset to read from
1441 * @len: number of bytes to read
1442 * @ooblen: number of oob data bytes to read
1444 * Read raw data including oob into buffer
1446 int nand_read_raw (struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
1448 struct nand_chip *this = mtd->priv;
1449 int page = (int) (from >> this->page_shift);
1450 int chip = (int) (from >> this->chip_shift);
1451 int sndcmd = 1;
1452 int cnt = 0;
1453 int pagesize = mtd->oobblock + mtd->oobsize;
1454 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1456 /* Do not allow reads past end of device */
1457 if ((from + len) > mtd->size) {
1458 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n");
1459 return -EINVAL;
1462 /* Grab the lock and see if the device is available */
1463 nand_get_device (this, mtd , FL_READING);
1465 this->select_chip (mtd, chip);
1467 /* Add requested oob length */
1468 len += ooblen;
1470 while (len) {
1471 if (sndcmd)
1472 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page & this->pagemask);
1473 sndcmd = 0;
1475 this->read_buf (mtd, &buf[cnt], pagesize);
1477 len -= pagesize;
1478 cnt += pagesize;
1479 page++;
1481 if (!this->dev_ready)
1482 udelay (this->chip_delay);
1483 else
1484 while (!this->dev_ready(mtd));
1486 /* Check, if the chip supports auto page increment */
1487 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1488 sndcmd = 1;
1491 /* Deselect and wake up anyone waiting on the device */
1492 nand_release_device(mtd);
1493 return 0;
1497 /**
1498 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
1499 * @mtd: MTD device structure
1500 * @fsbuf: buffer given by fs driver
1501 * @oobsel: out of band selection structre
1502 * @autoplace: 1 = place given buffer into the oob bytes
1503 * @numpages: number of pages to prepare
1505 * Return:
1506 * 1. Filesystem buffer available and autoplacement is off,
1507 * return filesystem buffer
1508 * 2. No filesystem buffer or autoplace is off, return internal
1509 * buffer
1510 * 3. Filesystem buffer is given and autoplace selected
1511 * put data from fs buffer into internal buffer and
1512 * retrun internal buffer
1514 * Note: The internal buffer is filled with 0xff. This must
1515 * be done only once, when no autoplacement happens
1516 * Autoplacement sets the buffer dirty flag, which
1517 * forces the 0xff fill before using the buffer again.
1520 static u_char * nand_prepare_oobbuf (struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel,
1521 int autoplace, int numpages)
1523 struct nand_chip *this = mtd->priv;
1524 int i, len, ofs;
1526 /* Zero copy fs supplied buffer */
1527 if (fsbuf && !autoplace)
1528 return fsbuf;
1530 /* Check, if the buffer must be filled with ff again */
1531 if (this->oobdirty) {
1532 memset (this->oob_buf, 0xff,
1533 mtd->oobsize << (this->phys_erase_shift - this->page_shift));
1534 this->oobdirty = 0;
1537 /* If we have no autoplacement or no fs buffer use the internal one */
1538 if (!autoplace || !fsbuf)
1539 return this->oob_buf;
1541 /* Walk through the pages and place the data */
1542 this->oobdirty = 1;
1543 ofs = 0;
1544 while (numpages--) {
1545 for (i = 0, len = 0; len < mtd->oobavail; i++) {
1546 int to = ofs + oobsel->oobfree[i][0];
1547 int num = oobsel->oobfree[i][1];
1548 memcpy (&this->oob_buf[to], fsbuf, num);
1549 len += num;
1550 fsbuf += num;
1552 ofs += mtd->oobavail;
1554 return this->oob_buf;
1557 #define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
1560 * nand_write - [MTD Interface] compability function for nand_write_ecc
1561 * @mtd: MTD device structure
1562 * @to: offset to write to
1563 * @len: number of bytes to write
1564 * @retlen: pointer to variable to store the number of written bytes
1565 * @buf: the data to write
1567 * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
1570 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1572 return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));
1576 * nand_write_ecc - [MTD Interface] NAND write with ECC
1577 * @mtd: MTD device structure
1578 * @to: offset to write to
1579 * @len: number of bytes to write
1580 * @retlen: pointer to variable to store the number of written bytes
1581 * @buf: the data to write
1582 * @eccbuf: filesystem supplied oob data buffer
1583 * @oobsel: oob selection structure
1585 * NAND write with ECC
1587 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
1588 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
1590 int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
1591 int autoplace = 0, numpages, totalpages;
1592 struct nand_chip *this = mtd->priv;
1593 u_char *oobbuf, *bufstart;
1594 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1596 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1598 /* Initialize retlen, in case of early exit */
1599 *retlen = 0;
1601 /* Do not allow write past end of device */
1602 if ((to + len) > mtd->size) {
1603 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n");
1604 return -EINVAL;
1607 /* reject writes, which are not page aligned */
1608 if (NOTALIGNED (to) || NOTALIGNED(len)) {
1609 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1610 return -EINVAL;
1613 /* Grab the lock and see if the device is available */
1614 nand_get_device (this, mtd, FL_WRITING);
1616 /* Calculate chipnr */
1617 chipnr = (int)(to >> this->chip_shift);
1618 /* Select the NAND device */
1619 this->select_chip(mtd, chipnr);
1621 /* Check, if it is write protected */
1622 if (nand_check_wp(mtd))
1623 goto out;
1625 /* if oobsel is NULL, use chip defaults */
1626 if (oobsel == NULL)
1627 oobsel = &mtd->oobinfo;
1629 /* Autoplace of oob data ? Use the default placement scheme */
1630 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1631 oobsel = this->autooob;
1632 autoplace = 1;
1635 /* Setup variables and oob buffer */
1636 totalpages = len >> this->page_shift;
1637 page = (int) (to >> this->page_shift);
1638 /* Invalidate the page cache, if we write to the cached page */
1639 if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
1640 this->pagebuf = -1;
1642 /* Set it relative to chip */
1643 page &= this->pagemask;
1644 startpage = page;
1645 /* Calc number of pages we can write in one go */
1646 numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages);
1647 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages);
1648 bufstart = (u_char *)buf;
1650 /* Loop until all data is written */
1651 while (written < len) {
1653 this->data_poi = (u_char*) &buf[written];
1654 /* Write one page. If this is the last page to write
1655 * or the last page in this block, then use the
1656 * real pageprogram command, else select cached programming
1657 * if supported by the chip.
1659 ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
1660 if (ret) {
1661 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret);
1662 goto out;
1664 /* Next oob page */
1665 oob += mtd->oobsize;
1666 /* Update written bytes count */
1667 written += mtd->oobblock;
1668 if (written == len)
1669 goto cmp;
1671 /* Increment page address */
1672 page++;
1674 /* Have we hit a block boundary ? Then we have to verify and
1675 * if verify is ok, we have to setup the oob buffer for
1676 * the next pages.
1678 if (!(page & (ppblock - 1))){
1679 int ofs;
1680 this->data_poi = bufstart;
1681 ret = nand_verify_pages (mtd, this, startpage,
1682 page - startpage,
1683 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1684 if (ret) {
1685 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1686 goto out;
1688 *retlen = written;
1690 ofs = autoplace ? mtd->oobavail : mtd->oobsize;
1691 if (eccbuf)
1692 eccbuf += (page - startpage) * ofs;
1693 totalpages -= page - startpage;
1694 numpages = min (totalpages, ppblock);
1695 page &= this->pagemask;
1696 startpage = page;
1697 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel,
1698 autoplace, numpages);
1699 /* Check, if we cross a chip boundary */
1700 if (!page) {
1701 chipnr++;
1702 this->select_chip(mtd, -1);
1703 this->select_chip(mtd, chipnr);
1707 /* Verify the remaining pages */
1708 cmp:
1709 this->data_poi = bufstart;
1710 ret = nand_verify_pages (mtd, this, startpage, totalpages,
1711 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1712 if (!ret)
1713 *retlen = written;
1714 else
1715 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1717 out:
1718 /* Deselect and wake up anyone waiting on the device */
1719 nand_release_device(mtd);
1721 return ret;
1726 * nand_write_oob - [MTD Interface] NAND write out-of-band
1727 * @mtd: MTD device structure
1728 * @to: offset to write to
1729 * @len: number of bytes to write
1730 * @retlen: pointer to variable to store the number of written bytes
1731 * @buf: the data to write
1733 * NAND write out-of-band
1735 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1737 int column, page, status, ret = -EIO, chipnr;
1738 struct nand_chip *this = mtd->priv;
1740 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1742 /* Shift to get page */
1743 page = (int) (to >> this->page_shift);
1744 chipnr = (int) (to >> this->chip_shift);
1746 /* Mask to get column */
1747 column = to & (mtd->oobsize - 1);
1749 /* Initialize return length value */
1750 *retlen = 0;
1752 /* Do not allow write past end of page */
1753 if ((column + len) > mtd->oobsize) {
1754 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
1755 return -EINVAL;
1758 /* Grab the lock and see if the device is available */
1759 nand_get_device (this, mtd, FL_WRITING);
1761 /* Select the NAND device */
1762 this->select_chip(mtd, chipnr);
1764 /* Reset the chip. Some chips (like the Toshiba TC5832DC found
1765 in one of my DiskOnChip 2000 test units) will clear the whole
1766 data page too if we don't do this. I have no clue why, but
1767 I seem to have 'fixed' it in the doc2000 driver in
1768 August 1999. dwmw2. */
1769 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1771 /* Check, if it is write protected */
1772 if (nand_check_wp(mtd))
1773 goto out;
1775 /* Invalidate the page cache, if we write to the cached page */
1776 if (page == this->pagebuf)
1777 this->pagebuf = -1;
1779 if (NAND_MUST_PAD(this)) {
1780 /* Write out desired data */
1781 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
1782 /* prepad 0xff for partial programming */
1783 this->write_buf(mtd, ffchars, column);
1784 /* write data */
1785 this->write_buf(mtd, buf, len);
1786 /* postpad 0xff for partial programming */
1787 this->write_buf(mtd, ffchars, mtd->oobsize - (len+column));
1788 } else {
1789 /* Write out desired data */
1790 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
1791 /* write data */
1792 this->write_buf(mtd, buf, len);
1794 /* Send command to program the OOB data */
1795 this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1);
1797 status = this->waitfunc (mtd, this, FL_WRITING);
1799 /* See if device thinks it succeeded */
1800 if (status & NAND_STATUS_FAIL) {
1801 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
1802 ret = -EIO;
1803 goto out;
1805 /* Return happy */
1806 *retlen = len;
1808 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1809 /* Send command to read back the data */
1810 this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask);
1812 if (this->verify_buf(mtd, buf, len)) {
1813 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
1814 ret = -EIO;
1815 goto out;
1817 #endif
1818 ret = 0;
1819 out:
1820 /* Deselect and wake up anyone waiting on the device */
1821 nand_release_device(mtd);
1823 return ret;
1828 * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
1829 * @mtd: MTD device structure
1830 * @vecs: the iovectors to write
1831 * @count: number of vectors
1832 * @to: offset to write to
1833 * @retlen: pointer to variable to store the number of written bytes
1835 * NAND write with kvec. This just calls the ecc function
1837 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1838 loff_t to, size_t * retlen)
1840 return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL));
1844 * nand_writev_ecc - [MTD Interface] write with iovec with ecc
1845 * @mtd: MTD device structure
1846 * @vecs: the iovectors to write
1847 * @count: number of vectors
1848 * @to: offset to write to
1849 * @retlen: pointer to variable to store the number of written bytes
1850 * @eccbuf: filesystem supplied oob data buffer
1851 * @oobsel: oob selection structure
1853 * NAND write with iovec with ecc
1855 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1856 loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
1858 int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
1859 int oob, numpages, autoplace = 0, startpage;
1860 struct nand_chip *this = mtd->priv;
1861 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1862 u_char *oobbuf, *bufstart;
1864 /* Preset written len for early exit */
1865 *retlen = 0;
1867 /* Calculate total length of data */
1868 total_len = 0;
1869 for (i = 0; i < count; i++)
1870 total_len += (int) vecs[i].iov_len;
1872 DEBUG (MTD_DEBUG_LEVEL3,
1873 "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
1875 /* Do not allow write past end of page */
1876 if ((to + total_len) > mtd->size) {
1877 DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
1878 return -EINVAL;
1881 /* reject writes, which are not page aligned */
1882 if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
1883 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1884 return -EINVAL;
1887 /* Grab the lock and see if the device is available */
1888 nand_get_device (this, mtd, FL_WRITING);
1890 /* Get the current chip-nr */
1891 chipnr = (int) (to >> this->chip_shift);
1892 /* Select the NAND device */
1893 this->select_chip(mtd, chipnr);
1895 /* Check, if it is write protected */
1896 if (nand_check_wp(mtd))
1897 goto out;
1899 /* if oobsel is NULL, use chip defaults */
1900 if (oobsel == NULL)
1901 oobsel = &mtd->oobinfo;
1903 /* Autoplace of oob data ? Use the default placement scheme */
1904 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1905 oobsel = this->autooob;
1906 autoplace = 1;
1909 /* Setup start page */
1910 page = (int) (to >> this->page_shift);
1911 /* Invalidate the page cache, if we write to the cached page */
1912 if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))
1913 this->pagebuf = -1;
1915 startpage = page & this->pagemask;
1917 /* Loop until all kvec' data has been written */
1918 len = 0;
1919 while (count) {
1920 /* If the given tuple is >= pagesize then
1921 * write it out from the iov
1923 if ((vecs->iov_len - len) >= mtd->oobblock) {
1924 /* Calc number of pages we can write
1925 * out of this iov in one go */
1926 numpages = (vecs->iov_len - len) >> this->page_shift;
1927 /* Do not cross block boundaries */
1928 numpages = min (ppblock - (startpage & (ppblock - 1)), numpages);
1929 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1930 bufstart = (u_char *)vecs->iov_base;
1931 bufstart += len;
1932 this->data_poi = bufstart;
1933 oob = 0;
1934 for (i = 1; i <= numpages; i++) {
1935 /* Write one page. If this is the last page to write
1936 * then use the real pageprogram command, else select
1937 * cached programming if supported by the chip.
1939 ret = nand_write_page (mtd, this, page & this->pagemask,
1940 &oobbuf[oob], oobsel, i != numpages);
1941 if (ret)
1942 goto out;
1943 this->data_poi += mtd->oobblock;
1944 len += mtd->oobblock;
1945 oob += mtd->oobsize;
1946 page++;
1948 /* Check, if we have to switch to the next tuple */
1949 if (len >= (int) vecs->iov_len) {
1950 vecs++;
1951 len = 0;
1952 count--;
1954 } else {
1955 /* We must use the internal buffer, read data out of each
1956 * tuple until we have a full page to write
1958 int cnt = 0;
1959 while (cnt < mtd->oobblock) {
1960 if (vecs->iov_base != NULL && vecs->iov_len)
1961 this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
1962 /* Check, if we have to switch to the next tuple */
1963 if (len >= (int) vecs->iov_len) {
1964 vecs++;
1965 len = 0;
1966 count--;
1969 this->pagebuf = page;
1970 this->data_poi = this->data_buf;
1971 bufstart = this->data_poi;
1972 numpages = 1;
1973 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1974 ret = nand_write_page (mtd, this, page & this->pagemask,
1975 oobbuf, oobsel, 0);
1976 if (ret)
1977 goto out;
1978 page++;
1981 this->data_poi = bufstart;
1982 ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
1983 if (ret)
1984 goto out;
1986 written += mtd->oobblock * numpages;
1987 /* All done ? */
1988 if (!count)
1989 break;
1991 startpage = page & this->pagemask;
1992 /* Check, if we cross a chip boundary */
1993 if (!startpage) {
1994 chipnr++;
1995 this->select_chip(mtd, -1);
1996 this->select_chip(mtd, chipnr);
1999 ret = 0;
2000 out:
2001 /* Deselect and wake up anyone waiting on the device */
2002 nand_release_device(mtd);
2004 *retlen = written;
2005 return ret;
2009 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2010 * @mtd: MTD device structure
2011 * @page: the page address of the block which will be erased
2013 * Standard erase command for NAND chips
2015 static void single_erase_cmd (struct mtd_info *mtd, int page)
2017 struct nand_chip *this = mtd->priv;
2018 /* Send commands to erase a block */
2019 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2020 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2024 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2025 * @mtd: MTD device structure
2026 * @page: the page address of the block which will be erased
2028 * AND multi block erase command function
2029 * Erase 4 consecutive blocks
2031 static void multi_erase_cmd (struct mtd_info *mtd, int page)
2033 struct nand_chip *this = mtd->priv;
2034 /* Send commands to erase a block */
2035 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2036 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2037 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
2038 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
2039 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
2043 * nand_erase - [MTD Interface] erase block(s)
2044 * @mtd: MTD device structure
2045 * @instr: erase instruction
2047 * Erase one ore more blocks
2049 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
2051 return nand_erase_nand (mtd, instr, 0);
2054 #define BBT_PAGE_MASK 0xffffff3f
2056 * nand_erase_intern - [NAND Interface] erase block(s)
2057 * @mtd: MTD device structure
2058 * @instr: erase instruction
2059 * @allowbbt: allow erasing the bbt area
2061 * Erase one ore more blocks
2063 int nand_erase_nand (struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
2065 int page, len, status, pages_per_block, ret, chipnr;
2066 struct nand_chip *this = mtd->priv;
2067 int rewrite_bbt[NAND_MAX_CHIPS]={0}; /* flags to indicate the page, if bbt needs to be rewritten. */
2068 unsigned int bbt_masked_page; /* bbt mask to compare to page being erased. */
2069 /* It is used to see if the current page is in the same */
2070 /* 256 block group and the same bank as the bbt. */
2072 DEBUG (MTD_DEBUG_LEVEL3,
2073 "nand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
2075 /* Start address must align on block boundary */
2076 if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
2077 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2078 return -EINVAL;
2081 /* Length must align on block boundary */
2082 if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
2083 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
2084 return -EINVAL;
2087 /* Do not allow erase past end of device */
2088 if ((instr->len + instr->addr) > mtd->size) {
2089 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
2090 return -EINVAL;
2093 instr->fail_addr = 0xffffffff;
2095 /* Grab the lock and see if the device is available */
2096 nand_get_device (this, mtd, FL_ERASING);
2098 /* Shift to get first page */
2099 page = (int) (instr->addr >> this->page_shift);
2100 chipnr = (int) (instr->addr >> this->chip_shift);
2102 /* Calculate pages in each block */
2103 pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
2105 /* Select the NAND device */
2106 this->select_chip(mtd, chipnr);
2108 /* Check the WP bit */
2109 /* Check, if it is write protected */
2110 if (nand_check_wp(mtd)) {
2111 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
2112 instr->state = MTD_ERASE_FAILED;
2113 goto erase_exit;
2116 /* if BBT requires refresh, set the BBT page mask to see if the BBT should be rewritten */
2117 if (this->options & BBT_AUTO_REFRESH) {
2118 bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2119 } else {
2120 bbt_masked_page = 0xffffffff; /* should not match anything */
2123 /* Loop through the pages */
2124 len = instr->len;
2126 instr->state = MTD_ERASING;
2128 while (len) {
2129 /* Check if we have a bad block, we do not erase bad blocks ! */
2130 if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
2131 printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
2132 instr->state = MTD_ERASE_FAILED;
2133 goto erase_exit;
2136 /* Invalidate the page cache, if we erase the block which contains
2137 the current cached page */
2138 if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
2139 this->pagebuf = -1;
2141 this->erase_cmd (mtd, page & this->pagemask);
2143 status = this->waitfunc (mtd, this, FL_ERASING);
2145 /* See if operation failed and additional status checks are available */
2146 if ((status & NAND_STATUS_FAIL) && (this->errstat)) {
2147 status = this->errstat(mtd, this, FL_ERASING, status, page);
2150 /* See if block erase succeeded */
2151 if (status & NAND_STATUS_FAIL) {
2152 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
2153 instr->state = MTD_ERASE_FAILED;
2154 instr->fail_addr = (page << this->page_shift);
2155 goto erase_exit;
2158 /* if BBT requires refresh, set the BBT rewrite flag to the page being erased */
2159 if (this->options & BBT_AUTO_REFRESH) {
2160 if (((page & BBT_PAGE_MASK) == bbt_masked_page) &&
2161 (page != this->bbt_td->pages[chipnr])) {
2162 rewrite_bbt[chipnr] = (page << this->page_shift);
2166 /* Increment page address and decrement length */
2167 len -= (1 << this->phys_erase_shift);
2168 page += pages_per_block;
2170 /* Check, if we cross a chip boundary */
2171 if (len && !(page & this->pagemask)) {
2172 chipnr++;
2173 this->select_chip(mtd, -1);
2174 this->select_chip(mtd, chipnr);
2176 /* if BBT requires refresh and BBT-PERCHIP,
2177 * set the BBT page mask to see if this BBT should be rewritten */
2178 if ((this->options & BBT_AUTO_REFRESH) && (this->bbt_td->options & NAND_BBT_PERCHIP)) {
2179 bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2184 instr->state = MTD_ERASE_DONE;
2186 erase_exit:
2188 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2189 /* Do call back function */
2190 if (!ret)
2191 mtd_erase_callback(instr);
2193 /* Deselect and wake up anyone waiting on the device */
2194 nand_release_device(mtd);
2196 /* if BBT requires refresh and erase was successful, rewrite any selected bad block tables */
2197 if ((this->options & BBT_AUTO_REFRESH) && (!ret)) {
2198 for (chipnr = 0; chipnr < this->numchips; chipnr++) {
2199 if (rewrite_bbt[chipnr]) {
2200 /* update the BBT for chip */
2201 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt (%d:0x%0x 0x%0x)\n",
2202 chipnr, rewrite_bbt[chipnr], this->bbt_td->pages[chipnr]);
2203 nand_update_bbt (mtd, rewrite_bbt[chipnr]);
2208 /* Return more or less happy */
2209 return ret;
2213 * nand_sync - [MTD Interface] sync
2214 * @mtd: MTD device structure
2216 * Sync is actually a wait for chip ready function
2218 static void nand_sync (struct mtd_info *mtd)
2220 struct nand_chip *this = mtd->priv;
2222 DEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2224 /* Grab the lock and see if the device is available */
2225 nand_get_device (this, mtd, FL_SYNCING);
2226 /* Release it and go back */
2227 nand_release_device (mtd);
2232 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2233 * @mtd: MTD device structure
2234 * @ofs: offset relative to mtd start
2236 static int nand_block_isbad (struct mtd_info *mtd, loff_t ofs)
2238 /* Check for invalid offset */
2239 if (ofs > mtd->size)
2240 return -EINVAL;
2242 return nand_block_checkbad (mtd, ofs, 1, 0);
2246 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2247 * @mtd: MTD device structure
2248 * @ofs: offset relative to mtd start
2250 static int nand_block_markbad (struct mtd_info *mtd, loff_t ofs)
2252 struct nand_chip *this = mtd->priv;
2253 int ret;
2255 if ((ret = nand_block_isbad(mtd, ofs))) {
2256 /* If it was bad already, return success and do nothing. */
2257 if (ret > 0)
2258 return 0;
2259 return ret;
2262 return this->block_markbad(mtd, ofs);
2266 * nand_scan - [NAND Interface] Scan for the NAND device
2267 * @mtd: MTD device structure
2268 * @maxchips: Number of chips to scan for
2270 * This fills out all the not initialized function pointers
2271 * with the defaults.
2272 * The flash ID is read and the mtd/chip structures are
2273 * filled with the appropriate values. Buffers are allocated if
2274 * they are not provided by the board driver
2277 int nand_scan (struct mtd_info *mtd, int maxchips)
2279 int i, j, nand_maf_id, nand_dev_id, busw;
2280 struct nand_chip *this = mtd->priv;
2282 /* Get buswidth to select the correct functions*/
2283 busw = this->options & NAND_BUSWIDTH_16;
2285 /* check for proper chip_delay setup, set 20us if not */
2286 if (!this->chip_delay)
2287 this->chip_delay = 20;
2289 /* check, if a user supplied command function given */
2290 if (this->cmdfunc == NULL)
2291 this->cmdfunc = nand_command;
2293 /* check, if a user supplied wait function given */
2294 if (this->waitfunc == NULL)
2295 this->waitfunc = nand_wait;
2297 if (!this->select_chip)
2298 this->select_chip = nand_select_chip;
2299 if (!this->write_byte)
2300 this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2301 if (!this->read_byte)
2302 this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2303 if (!this->write_word)
2304 this->write_word = nand_write_word;
2305 if (!this->read_word)
2306 this->read_word = nand_read_word;
2307 if (!this->block_bad)
2308 this->block_bad = nand_block_bad;
2309 if (!this->block_markbad)
2310 this->block_markbad = nand_default_block_markbad;
2311 if (!this->write_buf)
2312 this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2313 if (!this->read_buf)
2314 this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2315 if (!this->verify_buf)
2316 this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2317 if (!this->scan_bbt)
2318 this->scan_bbt = nand_default_bbt;
2320 /* Select the device */
2321 this->select_chip(mtd, 0);
2323 /* Send the command for reading device ID */
2324 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2326 /* Read manufacturer and device IDs */
2327 nand_maf_id = this->read_byte(mtd);
2328 nand_dev_id = this->read_byte(mtd);
2330 /* Print and store flash device information */
2331 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2333 if (nand_dev_id != nand_flash_ids[i].id)
2334 continue;
2336 if (!mtd->name) mtd->name = nand_flash_ids[i].name;
2337 this->chipsize = nand_flash_ids[i].chipsize << 20;
2339 /* New devices have all the information in additional id bytes */
2340 if (!nand_flash_ids[i].pagesize) {
2341 int extid;
2342 /* The 3rd id byte contains non relevant data ATM */
2343 extid = this->read_byte(mtd);
2344 /* The 4th id byte is the important one */
2345 extid = this->read_byte(mtd);
2346 /* Calc pagesize */
2347 mtd->oobblock = 1024 << (extid & 0x3);
2348 extid >>= 2;
2349 /* Calc oobsize */
2350 mtd->oobsize = (8 << (extid & 0x03)) * (mtd->oobblock / 512);
2351 extid >>= 2;
2352 /* Calc blocksize. Blocksize is multiples of 64KiB */
2353 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2354 extid >>= 2;
2355 /* Get buswidth information */
2356 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2358 } else {
2359 /* Old devices have this data hardcoded in the
2360 * device id table */
2361 mtd->erasesize = nand_flash_ids[i].erasesize;
2362 mtd->oobblock = nand_flash_ids[i].pagesize;
2363 mtd->oobsize = mtd->oobblock / 32;
2364 busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
2367 /* Check, if buswidth is correct. Hardware drivers should set
2368 * this correct ! */
2369 if (busw != (this->options & NAND_BUSWIDTH_16)) {
2370 printk (KERN_INFO "NAND device: Manufacturer ID:"
2371 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2372 nand_manuf_ids[i].name , mtd->name);
2373 printk (KERN_WARNING
2374 "NAND bus width %d instead %d bit\n",
2375 (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
2376 busw ? 16 : 8);
2377 this->select_chip(mtd, -1);
2378 return 1;
2381 /* Calculate the address shift from the page size */
2382 this->page_shift = ffs(mtd->oobblock) - 1;
2383 this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
2384 this->chip_shift = ffs(this->chipsize) - 1;
2386 /* Set the bad block position */
2387 this->badblockpos = mtd->oobblock > 512 ?
2388 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2390 /* Get chip options, preserve non chip based options */
2391 this->options &= ~NAND_CHIPOPTIONS_MSK;
2392 this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
2393 /* Set this as a default. Board drivers can override it, if neccecary */
2394 this->options |= NAND_NO_AUTOINCR;
2395 /* Check if this is a not a samsung device. Do not clear the options
2396 * for chips which are not having an extended id.
2398 if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
2399 this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2401 /* Check for AND chips with 4 page planes */
2402 if (this->options & NAND_4PAGE_ARRAY)
2403 this->erase_cmd = multi_erase_cmd;
2404 else
2405 this->erase_cmd = single_erase_cmd;
2407 /* Do not replace user supplied command function ! */
2408 if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
2409 this->cmdfunc = nand_command_lp;
2411 /* Try to identify manufacturer */
2412 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
2413 if (nand_manuf_ids[j].id == nand_maf_id)
2414 break;
2416 printk (KERN_INFO "NAND device: Manufacturer ID:"
2417 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2418 nand_manuf_ids[j].name , nand_flash_ids[i].name);
2419 break;
2422 if (!nand_flash_ids[i].name) {
2423 printk (KERN_WARNING "No NAND device found!!!\n");
2424 this->select_chip(mtd, -1);
2425 return 1;
2428 for (i=1; i < maxchips; i++) {
2429 this->select_chip(mtd, i);
2431 /* Send the command for reading device ID */
2432 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2434 /* Read manufacturer and device IDs */
2435 if (nand_maf_id != this->read_byte(mtd) ||
2436 nand_dev_id != this->read_byte(mtd))
2437 break;
2439 if (i > 1)
2440 printk(KERN_INFO "%d NAND chips detected\n", i);
2442 /* Allocate buffers, if neccecary */
2443 if (!this->oob_buf) {
2444 size_t len;
2445 len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
2446 this->oob_buf = kmalloc (len, GFP_KERNEL);
2447 if (!this->oob_buf) {
2448 printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
2449 return -ENOMEM;
2451 this->options |= NAND_OOBBUF_ALLOC;
2454 if (!this->data_buf) {
2455 size_t len;
2456 len = mtd->oobblock + mtd->oobsize;
2457 this->data_buf = kmalloc (len, GFP_KERNEL);
2458 if (!this->data_buf) {
2459 if (this->options & NAND_OOBBUF_ALLOC)
2460 kfree (this->oob_buf);
2461 printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
2462 return -ENOMEM;
2464 this->options |= NAND_DATABUF_ALLOC;
2467 /* Store the number of chips and calc total size for mtd */
2468 this->numchips = i;
2469 mtd->size = i * this->chipsize;
2470 /* Convert chipsize to number of pages per chip -1. */
2471 this->pagemask = (this->chipsize >> this->page_shift) - 1;
2472 /* Preset the internal oob buffer */
2473 memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
2475 /* If no default placement scheme is given, select an
2476 * appropriate one */
2477 if (!this->autooob) {
2478 /* Select the appropriate default oob placement scheme for
2479 * placement agnostic filesystems */
2480 switch (mtd->oobsize) {
2481 case 8:
2482 this->autooob = &nand_oob_8;
2483 break;
2484 case 16:
2485 this->autooob = &nand_oob_16;
2486 break;
2487 case 64:
2488 this->autooob = &nand_oob_64;
2489 break;
2490 default:
2491 printk (KERN_WARNING "No oob scheme defined for oobsize %d\n",
2492 mtd->oobsize);
2493 BUG();
2497 /* The number of bytes available for the filesystem to place fs dependend
2498 * oob data */
2499 if (this->options & NAND_BUSWIDTH_16) {
2500 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 2);
2501 if (this->autooob->eccbytes & 0x01)
2502 mtd->oobavail--;
2503 } else
2504 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 1);
2507 * check ECC mode, default to software
2508 * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
2509 * fallback to software ECC
2511 this->eccsize = 256; /* set default eccsize */
2512 this->eccbytes = 3;
2514 switch (this->eccmode) {
2515 case NAND_ECC_HW12_2048:
2516 if (mtd->oobblock < 2048) {
2517 printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
2518 mtd->oobblock);
2519 this->eccmode = NAND_ECC_SOFT;
2520 this->calculate_ecc = nand_calculate_ecc;
2521 this->correct_data = nand_correct_data;
2522 } else
2523 this->eccsize = 2048;
2524 break;
2526 case NAND_ECC_HW3_512:
2527 case NAND_ECC_HW6_512:
2528 case NAND_ECC_HW8_512:
2529 if (mtd->oobblock == 256) {
2530 printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
2531 this->eccmode = NAND_ECC_SOFT;
2532 this->calculate_ecc = nand_calculate_ecc;
2533 this->correct_data = nand_correct_data;
2534 } else
2535 this->eccsize = 512; /* set eccsize to 512 */
2536 break;
2538 case NAND_ECC_HW3_256:
2539 break;
2541 case NAND_ECC_NONE:
2542 printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
2543 this->eccmode = NAND_ECC_NONE;
2544 break;
2546 case NAND_ECC_SOFT:
2547 this->calculate_ecc = nand_calculate_ecc;
2548 this->correct_data = nand_correct_data;
2549 break;
2551 default:
2552 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
2553 BUG();
2556 /* Check hardware ecc function availability and adjust number of ecc bytes per
2557 * calculation step
2559 switch (this->eccmode) {
2560 case NAND_ECC_HW12_2048:
2561 this->eccbytes += 4;
2562 case NAND_ECC_HW8_512:
2563 this->eccbytes += 2;
2564 case NAND_ECC_HW6_512:
2565 this->eccbytes += 3;
2566 case NAND_ECC_HW3_512:
2567 case NAND_ECC_HW3_256:
2568 if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
2569 break;
2570 printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
2571 BUG();
2574 mtd->eccsize = this->eccsize;
2576 /* Set the number of read / write steps for one page to ensure ECC generation */
2577 switch (this->eccmode) {
2578 case NAND_ECC_HW12_2048:
2579 this->eccsteps = mtd->oobblock / 2048;
2580 break;
2581 case NAND_ECC_HW3_512:
2582 case NAND_ECC_HW6_512:
2583 case NAND_ECC_HW8_512:
2584 this->eccsteps = mtd->oobblock / 512;
2585 break;
2586 case NAND_ECC_HW3_256:
2587 case NAND_ECC_SOFT:
2588 this->eccsteps = mtd->oobblock / 256;
2589 break;
2591 case NAND_ECC_NONE:
2592 this->eccsteps = 1;
2593 break;
2596 /* Initialize state, waitqueue and spinlock */
2597 this->state = FL_READY;
2598 init_waitqueue_head (&this->wq);
2599 spin_lock_init (&this->chip_lock);
2601 /* De-select the device */
2602 this->select_chip(mtd, -1);
2604 /* Invalidate the pagebuffer reference */
2605 this->pagebuf = -1;
2607 /* Fill in remaining MTD driver data */
2608 mtd->type = MTD_NANDFLASH;
2609 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
2610 mtd->ecctype = MTD_ECC_SW;
2611 mtd->erase = nand_erase;
2612 mtd->point = NULL;
2613 mtd->unpoint = NULL;
2614 mtd->read = nand_read;
2615 mtd->write = nand_write;
2616 mtd->read_ecc = nand_read_ecc;
2617 mtd->write_ecc = nand_write_ecc;
2618 mtd->read_oob = nand_read_oob;
2619 mtd->write_oob = nand_write_oob;
2620 mtd->readv = NULL;
2621 mtd->writev = nand_writev;
2622 mtd->writev_ecc = nand_writev_ecc;
2623 mtd->sync = nand_sync;
2624 mtd->lock = NULL;
2625 mtd->unlock = NULL;
2626 mtd->suspend = NULL;
2627 mtd->resume = NULL;
2628 mtd->block_isbad = nand_block_isbad;
2629 mtd->block_markbad = nand_block_markbad;
2631 /* and make the autooob the default one */
2632 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
2634 mtd->owner = THIS_MODULE;
2636 /* Check, if we should skip the bad block table scan */
2637 if (this->options & NAND_SKIP_BBTSCAN)
2638 return 0;
2640 /* Build bad block table */
2641 return this->scan_bbt (mtd);
2645 * nand_release - [NAND Interface] Free resources held by the NAND device
2646 * @mtd: MTD device structure
2648 void nand_release (struct mtd_info *mtd)
2650 struct nand_chip *this = mtd->priv;
2652 #ifdef CONFIG_MTD_PARTITIONS
2653 /* Deregister partitions */
2654 del_mtd_partitions (mtd);
2655 #endif
2656 /* Deregister the device */
2657 del_mtd_device (mtd);
2659 /* Free bad block table memory, if allocated */
2660 if (this->bbt)
2661 kfree (this->bbt);
2662 /* Buffer allocated by nand_scan ? */
2663 if (this->options & NAND_OOBBUF_ALLOC)
2664 kfree (this->oob_buf);
2665 /* Buffer allocated by nand_scan ? */
2666 if (this->options & NAND_DATABUF_ALLOC)
2667 kfree (this->data_buf);
2670 EXPORT_SYMBOL (nand_scan);
2671 EXPORT_SYMBOL (nand_release);
2673 MODULE_LICENSE ("GPL");
2674 MODULE_AUTHOR ("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2675 MODULE_DESCRIPTION ("Generic NAND flash driver code");