Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mtd / nand / nand_base.c
blob44d5b128911f665a6071565216c3a786ade11acf
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 * Credits:
32 * David Woodhouse for adding multichip support
34 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
35 * rework for 2K page size chips
37 * TODO:
38 * Enable cached programming for 2k page size chips
39 * Check, if mtd->ecctype should be set to MTD_ECC_HW
40 * if we have HW ecc support.
41 * The AG-AND chips have nice features for speed improvement,
42 * which are not supported yet. Read / program 4 pages in one go.
44 * $Id: nand_base.c,v 1.126 2004/12/13 11:22:25 lavinen Exp $
46 * This program is free software; you can redistribute it and/or modify
47 * it under the terms of the GNU General Public License version 2 as
48 * published by the Free Software Foundation.
52 #include <linux/delay.h>
53 #include <linux/errno.h>
54 #include <linux/sched.h>
55 #include <linux/slab.h>
56 #include <linux/types.h>
57 #include <linux/mtd/mtd.h>
58 #include <linux/mtd/nand.h>
59 #include <linux/mtd/nand_ecc.h>
60 #include <linux/mtd/compatmac.h>
61 #include <linux/interrupt.h>
62 #include <linux/bitops.h>
63 #include <asm/io.h>
65 #ifdef CONFIG_MTD_PARTITIONS
66 #include <linux/mtd/partitions.h>
67 #endif
69 /* Define default oob placement schemes for large and small page devices */
70 static struct nand_oobinfo nand_oob_8 = {
71 .useecc = MTD_NANDECC_AUTOPLACE,
72 .eccbytes = 3,
73 .eccpos = {0, 1, 2},
74 .oobfree = { {3, 2}, {6, 2} }
77 static struct nand_oobinfo nand_oob_16 = {
78 .useecc = MTD_NANDECC_AUTOPLACE,
79 .eccbytes = 6,
80 .eccpos = {0, 1, 2, 3, 6, 7},
81 .oobfree = { {8, 8} }
84 static struct nand_oobinfo nand_oob_64 = {
85 .useecc = MTD_NANDECC_AUTOPLACE,
86 .eccbytes = 24,
87 .eccpos = {
88 40, 41, 42, 43, 44, 45, 46, 47,
89 48, 49, 50, 51, 52, 53, 54, 55,
90 56, 57, 58, 59, 60, 61, 62, 63},
91 .oobfree = { {2, 38} }
94 /* This is used for padding purposes in nand_write_oob */
95 static u_char ffchars[] = {
96 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
97 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
98 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
99 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
100 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
101 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
107 * NAND low-level MTD interface functions
109 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
110 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
111 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
113 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
114 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
115 size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
116 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
117 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf);
118 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
119 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
120 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char *buf);
121 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
122 unsigned long count, loff_t to, size_t * retlen);
123 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs,
124 unsigned long count, loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
125 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
126 static void nand_sync (struct mtd_info *mtd);
128 /* Some internal functions */
129 static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf,
130 struct nand_oobinfo *oobsel, int mode);
131 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
132 static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
133 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode);
134 #else
135 #define nand_verify_pages(...) (0)
136 #endif
138 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state);
141 * nand_release_device - [GENERIC] release chip
142 * @mtd: MTD device structure
144 * Deselect, release chip lock and wake up anyone waiting on the device
146 static void nand_release_device (struct mtd_info *mtd)
148 struct nand_chip *this = mtd->priv;
150 /* De-select the NAND device */
151 this->select_chip(mtd, -1);
152 /* Do we have a hardware controller ? */
153 if (this->controller) {
154 spin_lock(&this->controller->lock);
155 this->controller->active = NULL;
156 spin_unlock(&this->controller->lock);
158 /* Release the chip */
159 spin_lock (&this->chip_lock);
160 this->state = FL_READY;
161 wake_up (&this->wq);
162 spin_unlock (&this->chip_lock);
166 * nand_read_byte - [DEFAULT] read one byte from the chip
167 * @mtd: MTD device structure
169 * Default read function for 8bit buswith
171 static u_char nand_read_byte(struct mtd_info *mtd)
173 struct nand_chip *this = mtd->priv;
174 return readb(this->IO_ADDR_R);
178 * nand_write_byte - [DEFAULT] write one byte to the chip
179 * @mtd: MTD device structure
180 * @byte: pointer to data byte to write
182 * Default write function for 8it buswith
184 static void nand_write_byte(struct mtd_info *mtd, u_char byte)
186 struct nand_chip *this = mtd->priv;
187 writeb(byte, this->IO_ADDR_W);
191 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
192 * @mtd: MTD device structure
194 * Default read function for 16bit buswith with
195 * endianess conversion
197 static u_char nand_read_byte16(struct mtd_info *mtd)
199 struct nand_chip *this = mtd->priv;
200 return (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
204 * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
205 * @mtd: MTD device structure
206 * @byte: pointer to data byte to write
208 * Default write function for 16bit buswith with
209 * endianess conversion
211 static void nand_write_byte16(struct mtd_info *mtd, u_char byte)
213 struct nand_chip *this = mtd->priv;
214 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
218 * nand_read_word - [DEFAULT] read one word from the chip
219 * @mtd: MTD device structure
221 * Default read function for 16bit buswith without
222 * endianess conversion
224 static u16 nand_read_word(struct mtd_info *mtd)
226 struct nand_chip *this = mtd->priv;
227 return readw(this->IO_ADDR_R);
231 * nand_write_word - [DEFAULT] write one word to the chip
232 * @mtd: MTD device structure
233 * @word: data word to write
235 * Default write function for 16bit buswith without
236 * endianess conversion
238 static void nand_write_word(struct mtd_info *mtd, u16 word)
240 struct nand_chip *this = mtd->priv;
241 writew(word, this->IO_ADDR_W);
245 * nand_select_chip - [DEFAULT] control CE line
246 * @mtd: MTD device structure
247 * @chip: chipnumber to select, -1 for deselect
249 * Default select function for 1 chip devices.
251 static void nand_select_chip(struct mtd_info *mtd, int chip)
253 struct nand_chip *this = mtd->priv;
254 switch(chip) {
255 case -1:
256 this->hwcontrol(mtd, NAND_CTL_CLRNCE);
257 break;
258 case 0:
259 this->hwcontrol(mtd, NAND_CTL_SETNCE);
260 break;
262 default:
263 BUG();
268 * nand_write_buf - [DEFAULT] write buffer to chip
269 * @mtd: MTD device structure
270 * @buf: data buffer
271 * @len: number of bytes to write
273 * Default write function for 8bit buswith
275 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
277 int i;
278 struct nand_chip *this = mtd->priv;
280 for (i=0; i<len; i++)
281 writeb(buf[i], this->IO_ADDR_W);
285 * nand_read_buf - [DEFAULT] read chip data into buffer
286 * @mtd: MTD device structure
287 * @buf: buffer to store date
288 * @len: number of bytes to read
290 * Default read function for 8bit buswith
292 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
294 int i;
295 struct nand_chip *this = mtd->priv;
297 for (i=0; i<len; i++)
298 buf[i] = readb(this->IO_ADDR_R);
302 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
303 * @mtd: MTD device structure
304 * @buf: buffer containing the data to compare
305 * @len: number of bytes to compare
307 * Default verify function for 8bit buswith
309 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
311 int i;
312 struct nand_chip *this = mtd->priv;
314 for (i=0; i<len; i++)
315 if (buf[i] != readb(this->IO_ADDR_R))
316 return -EFAULT;
318 return 0;
322 * nand_write_buf16 - [DEFAULT] write buffer to chip
323 * @mtd: MTD device structure
324 * @buf: data buffer
325 * @len: number of bytes to write
327 * Default write function for 16bit buswith
329 static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
331 int i;
332 struct nand_chip *this = mtd->priv;
333 u16 *p = (u16 *) buf;
334 len >>= 1;
336 for (i=0; i<len; i++)
337 writew(p[i], this->IO_ADDR_W);
342 * nand_read_buf16 - [DEFAULT] read chip data into buffer
343 * @mtd: MTD device structure
344 * @buf: buffer to store date
345 * @len: number of bytes to read
347 * Default read function for 16bit buswith
349 static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
351 int i;
352 struct nand_chip *this = mtd->priv;
353 u16 *p = (u16 *) buf;
354 len >>= 1;
356 for (i=0; i<len; i++)
357 p[i] = readw(this->IO_ADDR_R);
361 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
362 * @mtd: MTD device structure
363 * @buf: buffer containing the data to compare
364 * @len: number of bytes to compare
366 * Default verify function for 16bit buswith
368 static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
370 int i;
371 struct nand_chip *this = mtd->priv;
372 u16 *p = (u16 *) buf;
373 len >>= 1;
375 for (i=0; i<len; i++)
376 if (p[i] != readw(this->IO_ADDR_R))
377 return -EFAULT;
379 return 0;
383 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
384 * @mtd: MTD device structure
385 * @ofs: offset from device start
386 * @getchip: 0, if the chip is already selected
388 * Check, if the block is bad.
390 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
392 int page, chipnr, res = 0;
393 struct nand_chip *this = mtd->priv;
394 u16 bad;
396 if (getchip) {
397 page = (int)(ofs >> this->page_shift);
398 chipnr = (int)(ofs >> this->chip_shift);
400 /* Grab the lock and see if the device is available */
401 nand_get_device (this, mtd, FL_READING);
403 /* Select the NAND device */
404 this->select_chip(mtd, chipnr);
405 } else
406 page = (int) ofs;
408 if (this->options & NAND_BUSWIDTH_16) {
409 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page & this->pagemask);
410 bad = cpu_to_le16(this->read_word(mtd));
411 if (this->badblockpos & 0x1)
412 bad >>= 1;
413 if ((bad & 0xFF) != 0xff)
414 res = 1;
415 } else {
416 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page & this->pagemask);
417 if (this->read_byte(mtd) != 0xff)
418 res = 1;
421 if (getchip) {
422 /* Deselect and wake up anyone waiting on the device */
423 nand_release_device(mtd);
426 return res;
430 * nand_default_block_markbad - [DEFAULT] mark a block bad
431 * @mtd: MTD device structure
432 * @ofs: offset from device start
434 * This is the default implementation, which can be overridden by
435 * a hardware specific driver.
437 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
439 struct nand_chip *this = mtd->priv;
440 u_char buf[2] = {0, 0};
441 size_t retlen;
442 int block;
444 /* Get block number */
445 block = ((int) ofs) >> this->bbt_erase_shift;
446 this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
448 /* Do we have a flash based bad block table ? */
449 if (this->options & NAND_USE_FLASH_BBT)
450 return nand_update_bbt (mtd, ofs);
452 /* We write two bytes, so we dont have to mess with 16 bit access */
453 ofs += mtd->oobsize + (this->badblockpos & ~0x01);
454 return nand_write_oob (mtd, ofs , 2, &retlen, buf);
457 /**
458 * nand_check_wp - [GENERIC] check if the chip is write protected
459 * @mtd: MTD device structure
460 * Check, if the device is write protected
462 * The function expects, that the device is already selected
464 static int nand_check_wp (struct mtd_info *mtd)
466 struct nand_chip *this = mtd->priv;
467 /* Check the WP bit */
468 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
469 return (this->read_byte(mtd) & 0x80) ? 0 : 1;
473 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
474 * @mtd: MTD device structure
475 * @ofs: offset from device start
476 * @getchip: 0, if the chip is already selected
477 * @allowbbt: 1, if its allowed to access the bbt area
479 * Check, if the block is bad. Either by reading the bad block table or
480 * calling of the scan function.
482 static int nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
484 struct nand_chip *this = mtd->priv;
486 if (!this->bbt)
487 return this->block_bad(mtd, ofs, getchip);
489 /* Return info from the table */
490 return nand_isbad_bbt (mtd, ofs, allowbbt);
494 * nand_command - [DEFAULT] Send command to NAND device
495 * @mtd: MTD device structure
496 * @command: the command to be sent
497 * @column: the column address for this command, -1 if none
498 * @page_addr: the page address for this command, -1 if none
500 * Send command to NAND device. This function is used for small page
501 * devices (256/512 Bytes per page)
503 static void nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
505 register struct nand_chip *this = mtd->priv;
507 /* Begin command latch cycle */
508 this->hwcontrol(mtd, NAND_CTL_SETCLE);
510 * Write out the command to the device.
512 if (command == NAND_CMD_SEQIN) {
513 int readcmd;
515 if (column >= mtd->oobblock) {
516 /* OOB area */
517 column -= mtd->oobblock;
518 readcmd = NAND_CMD_READOOB;
519 } else if (column < 256) {
520 /* First 256 bytes --> READ0 */
521 readcmd = NAND_CMD_READ0;
522 } else {
523 column -= 256;
524 readcmd = NAND_CMD_READ1;
526 this->write_byte(mtd, readcmd);
528 this->write_byte(mtd, command);
530 /* Set ALE and clear CLE to start address cycle */
531 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
533 if (column != -1 || page_addr != -1) {
534 this->hwcontrol(mtd, NAND_CTL_SETALE);
536 /* Serially input address */
537 if (column != -1) {
538 /* Adjust columns for 16 bit buswidth */
539 if (this->options & NAND_BUSWIDTH_16)
540 column >>= 1;
541 this->write_byte(mtd, column);
543 if (page_addr != -1) {
544 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
545 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
546 /* One more address cycle for devices > 32MiB */
547 if (this->chipsize > (32 << 20))
548 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));
550 /* Latch in address */
551 this->hwcontrol(mtd, NAND_CTL_CLRALE);
555 * program and erase have their own busy handlers
556 * status and sequential in needs no delay
558 switch (command) {
560 case NAND_CMD_PAGEPROG:
561 case NAND_CMD_ERASE1:
562 case NAND_CMD_ERASE2:
563 case NAND_CMD_SEQIN:
564 case NAND_CMD_STATUS:
565 return;
567 case NAND_CMD_RESET:
568 if (this->dev_ready)
569 break;
570 udelay(this->chip_delay);
571 this->hwcontrol(mtd, NAND_CTL_SETCLE);
572 this->write_byte(mtd, NAND_CMD_STATUS);
573 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
574 while ( !(this->read_byte(mtd) & 0x40));
575 return;
577 /* This applies to read commands */
578 default:
580 * If we don't have access to the busy pin, we apply the given
581 * command delay
583 if (!this->dev_ready) {
584 udelay (this->chip_delay);
585 return;
589 /* Apply this short delay always to ensure that we do wait tWB in
590 * any case on any machine. */
591 ndelay (100);
592 /* wait until command is processed */
593 while (!this->dev_ready(mtd));
597 * nand_command_lp - [DEFAULT] Send command to NAND large page device
598 * @mtd: MTD device structure
599 * @command: the command to be sent
600 * @column: the column address for this command, -1 if none
601 * @page_addr: the page address for this command, -1 if none
603 * Send command to NAND device. This is the version for the new large page devices
604 * We dont have the seperate regions as we have in the small page devices.
605 * We must emulate NAND_CMD_READOOB to keep the code compatible.
608 static void nand_command_lp (struct mtd_info *mtd, unsigned command, int column, int page_addr)
610 register struct nand_chip *this = mtd->priv;
612 /* Emulate NAND_CMD_READOOB */
613 if (command == NAND_CMD_READOOB) {
614 column += mtd->oobblock;
615 command = NAND_CMD_READ0;
619 /* Begin command latch cycle */
620 this->hwcontrol(mtd, NAND_CTL_SETCLE);
621 /* Write out the command to the device. */
622 this->write_byte(mtd, command);
623 /* End command latch cycle */
624 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
626 if (column != -1 || page_addr != -1) {
627 this->hwcontrol(mtd, NAND_CTL_SETALE);
629 /* Serially input address */
630 if (column != -1) {
631 /* Adjust columns for 16 bit buswidth */
632 if (this->options & NAND_BUSWIDTH_16)
633 column >>= 1;
634 this->write_byte(mtd, column & 0xff);
635 this->write_byte(mtd, column >> 8);
637 if (page_addr != -1) {
638 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
639 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
640 /* One more address cycle for devices > 128MiB */
641 if (this->chipsize > (128 << 20))
642 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0xff));
644 /* Latch in address */
645 this->hwcontrol(mtd, NAND_CTL_CLRALE);
649 * program and erase have their own busy handlers
650 * status and sequential in needs no delay
652 switch (command) {
654 case NAND_CMD_CACHEDPROG:
655 case NAND_CMD_PAGEPROG:
656 case NAND_CMD_ERASE1:
657 case NAND_CMD_ERASE2:
658 case NAND_CMD_SEQIN:
659 case NAND_CMD_STATUS:
660 return;
663 case NAND_CMD_RESET:
664 if (this->dev_ready)
665 break;
666 udelay(this->chip_delay);
667 this->hwcontrol(mtd, NAND_CTL_SETCLE);
668 this->write_byte(mtd, NAND_CMD_STATUS);
669 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
670 while ( !(this->read_byte(mtd) & 0x40));
671 return;
673 case NAND_CMD_READ0:
674 /* Begin command latch cycle */
675 this->hwcontrol(mtd, NAND_CTL_SETCLE);
676 /* Write out the start read command */
677 this->write_byte(mtd, NAND_CMD_READSTART);
678 /* End command latch cycle */
679 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
680 /* Fall through into ready check */
682 /* This applies to read commands */
683 default:
685 * If we don't have access to the busy pin, we apply the given
686 * command delay
688 if (!this->dev_ready) {
689 udelay (this->chip_delay);
690 return;
694 /* Apply this short delay always to ensure that we do wait tWB in
695 * any case on any machine. */
696 ndelay (100);
697 /* wait until command is processed */
698 while (!this->dev_ready(mtd));
702 * nand_get_device - [GENERIC] Get chip for selected access
703 * @this: the nand chip descriptor
704 * @mtd: MTD device structure
705 * @new_state: the state which is requested
707 * Get the device and lock it for exclusive access
709 static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
711 struct nand_chip *active = this;
713 DECLARE_WAITQUEUE (wait, current);
716 * Grab the lock and see if the device is available
718 retry:
719 /* Hardware controller shared among independend devices */
720 if (this->controller) {
721 spin_lock (&this->controller->lock);
722 if (this->controller->active)
723 active = this->controller->active;
724 else
725 this->controller->active = this;
726 spin_unlock (&this->controller->lock);
729 if (active == this) {
730 spin_lock (&this->chip_lock);
731 if (this->state == FL_READY) {
732 this->state = new_state;
733 spin_unlock (&this->chip_lock);
734 return;
737 set_current_state (TASK_UNINTERRUPTIBLE);
738 add_wait_queue (&active->wq, &wait);
739 spin_unlock (&active->chip_lock);
740 schedule ();
741 remove_wait_queue (&active->wq, &wait);
742 goto retry;
746 * nand_wait - [DEFAULT] wait until the command is done
747 * @mtd: MTD device structure
748 * @this: NAND chip structure
749 * @state: state to select the max. timeout value
751 * Wait for command done. This applies to erase and program only
752 * Erase can take up to 400ms and program up to 20ms according to
753 * general NAND and SmartMedia specs
756 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
759 unsigned long timeo = jiffies;
760 int status;
762 if (state == FL_ERASING)
763 timeo += (HZ * 400) / 1000;
764 else
765 timeo += (HZ * 20) / 1000;
767 /* Apply this short delay always to ensure that we do wait tWB in
768 * any case on any machine. */
769 ndelay (100);
771 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
772 this->cmdfunc (mtd, NAND_CMD_STATUS_MULTI, -1, -1);
773 else
774 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
776 while (time_before(jiffies, timeo)) {
777 /* Check, if we were interrupted */
778 if (this->state != state)
779 return 0;
781 if (this->dev_ready) {
782 if (this->dev_ready(mtd))
783 break;
784 } else {
785 if (this->read_byte(mtd) & NAND_STATUS_READY)
786 break;
788 yield ();
790 status = (int) this->read_byte(mtd);
791 return status;
795 * nand_write_page - [GENERIC] write one page
796 * @mtd: MTD device structure
797 * @this: NAND chip structure
798 * @page: startpage inside the chip, must be called with (page & this->pagemask)
799 * @oob_buf: out of band data buffer
800 * @oobsel: out of band selecttion structre
801 * @cached: 1 = enable cached programming if supported by chip
803 * Nand_page_program function is used for write and writev !
804 * This function will always program a full page of data
805 * If you call it with a non page aligned buffer, you're lost :)
807 * Cached programming is not supported yet.
809 static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page,
810 u_char *oob_buf, struct nand_oobinfo *oobsel, int cached)
812 int i, status;
813 u_char ecc_code[32];
814 int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
815 int *oob_config = oobsel->eccpos;
816 int datidx = 0, eccidx = 0, eccsteps = this->eccsteps;
817 int eccbytes = 0;
819 /* FIXME: Enable cached programming */
820 cached = 0;
822 /* Send command to begin auto page programming */
823 this->cmdfunc (mtd, NAND_CMD_SEQIN, 0x00, page);
825 /* Write out complete page of data, take care of eccmode */
826 switch (eccmode) {
827 /* No ecc, write all */
828 case NAND_ECC_NONE:
829 printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
830 this->write_buf(mtd, this->data_poi, mtd->oobblock);
831 break;
833 /* Software ecc 3/256, write all */
834 case NAND_ECC_SOFT:
835 for (; eccsteps; eccsteps--) {
836 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
837 for (i = 0; i < 3; i++, eccidx++)
838 oob_buf[oob_config[eccidx]] = ecc_code[i];
839 datidx += this->eccsize;
841 this->write_buf(mtd, this->data_poi, mtd->oobblock);
842 break;
843 default:
844 eccbytes = this->eccbytes;
845 for (; eccsteps; eccsteps--) {
846 /* enable hardware ecc logic for write */
847 this->enable_hwecc(mtd, NAND_ECC_WRITE);
848 this->write_buf(mtd, &this->data_poi[datidx], this->eccsize);
849 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
850 for (i = 0; i < eccbytes; i++, eccidx++)
851 oob_buf[oob_config[eccidx]] = ecc_code[i];
852 /* If the hardware ecc provides syndromes then
853 * the ecc code must be written immidiately after
854 * the data bytes (words) */
855 if (this->options & NAND_HWECC_SYNDROME)
856 this->write_buf(mtd, ecc_code, eccbytes);
857 datidx += this->eccsize;
859 break;
862 /* Write out OOB data */
863 if (this->options & NAND_HWECC_SYNDROME)
864 this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
865 else
866 this->write_buf(mtd, oob_buf, mtd->oobsize);
868 /* Send command to actually program the data */
869 this->cmdfunc (mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
871 if (!cached) {
872 /* call wait ready function */
873 status = this->waitfunc (mtd, this, FL_WRITING);
874 /* See if device thinks it succeeded */
875 if (status & 0x01) {
876 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
877 return -EIO;
879 } else {
880 /* FIXME: Implement cached programming ! */
881 /* wait until cache is ready*/
882 // status = this->waitfunc (mtd, this, FL_CACHEDRPG);
884 return 0;
887 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
889 * nand_verify_pages - [GENERIC] verify the chip contents after a write
890 * @mtd: MTD device structure
891 * @this: NAND chip structure
892 * @page: startpage inside the chip, must be called with (page & this->pagemask)
893 * @numpages: number of pages to verify
894 * @oob_buf: out of band data buffer
895 * @oobsel: out of band selecttion structre
896 * @chipnr: number of the current chip
897 * @oobmode: 1 = full buffer verify, 0 = ecc only
899 * The NAND device assumes that it is always writing to a cleanly erased page.
900 * Hence, it performs its internal write verification only on bits that
901 * transitioned from 1 to 0. The device does NOT verify the whole page on a
902 * byte by byte basis. It is possible that the page was not completely erased
903 * or the page is becoming unusable due to wear. The read with ECC would catch
904 * the error later when the ECC page check fails, but we would rather catch
905 * it early in the page write stage. Better to write no data than invalid data.
907 static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
908 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
910 int i, j, datidx = 0, oobofs = 0, res = -EIO;
911 int eccsteps = this->eccsteps;
912 int hweccbytes;
913 u_char oobdata[64];
915 hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
917 /* Send command to read back the first page */
918 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page);
920 for(;;) {
921 for (j = 0; j < eccsteps; j++) {
922 /* Loop through and verify the data */
923 if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
924 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
925 goto out;
927 datidx += mtd->eccsize;
928 /* Have we a hw generator layout ? */
929 if (!hweccbytes)
930 continue;
931 if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
932 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
933 goto out;
935 oobofs += hweccbytes;
938 /* check, if we must compare all data or if we just have to
939 * compare the ecc bytes
941 if (oobmode) {
942 if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
943 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
944 goto out;
946 } else {
947 /* Read always, else autoincrement fails */
948 this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
950 if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
951 int ecccnt = oobsel->eccbytes;
953 for (i = 0; i < ecccnt; i++) {
954 int idx = oobsel->eccpos[i];
955 if (oobdata[idx] != oob_buf[oobofs + idx] ) {
956 DEBUG (MTD_DEBUG_LEVEL0,
957 "%s: Failed ECC write "
958 "verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__, page, i);
959 goto out;
964 oobofs += mtd->oobsize - hweccbytes * eccsteps;
965 page++;
966 numpages--;
968 /* Apply delay or wait for ready/busy pin
969 * Do this before the AUTOINCR check, so no problems
970 * arise if a chip which does auto increment
971 * is marked as NOAUTOINCR by the board driver.
972 * Do this also before returning, so the chip is
973 * ready for the next command.
975 if (!this->dev_ready)
976 udelay (this->chip_delay);
977 else
978 while (!this->dev_ready(mtd));
980 /* All done, return happy */
981 if (!numpages)
982 return 0;
985 /* Check, if the chip supports auto page increment */
986 if (!NAND_CANAUTOINCR(this))
987 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
990 * Terminate the read command. We come here in case of an error
991 * So we must issue a reset command.
993 out:
994 this->cmdfunc (mtd, NAND_CMD_RESET, -1, -1);
995 return res;
997 #endif
1000 * nand_read - [MTD Interface] MTD compability function for nand_read_ecc
1001 * @mtd: MTD device structure
1002 * @from: offset to read from
1003 * @len: number of bytes to read
1004 * @retlen: pointer to variable to store the number of read bytes
1005 * @buf: the databuffer to put data
1007 * This function simply calls nand_read_ecc with oob buffer and oobsel = NULL
1009 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1011 return nand_read_ecc (mtd, from, len, retlen, buf, NULL, NULL);
1016 * nand_read_ecc - [MTD Interface] Read data with ECC
1017 * @mtd: MTD device structure
1018 * @from: offset to read from
1019 * @len: number of bytes to read
1020 * @retlen: pointer to variable to store the number of read bytes
1021 * @buf: the databuffer to put data
1022 * @oob_buf: filesystem supplied oob data buffer
1023 * @oobsel: oob selection structure
1025 * NAND read with ECC
1027 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
1028 size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel)
1030 int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
1031 int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
1032 struct nand_chip *this = mtd->priv;
1033 u_char *data_poi, *oob_data = oob_buf;
1034 u_char ecc_calc[32];
1035 u_char ecc_code[32];
1036 int eccmode, eccsteps;
1037 int *oob_config, datidx;
1038 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1039 int eccbytes;
1040 int compareecc = 1;
1041 int oobreadlen;
1044 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1046 /* Do not allow reads past end of device */
1047 if ((from + len) > mtd->size) {
1048 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
1049 *retlen = 0;
1050 return -EINVAL;
1053 /* Grab the lock and see if the device is available */
1054 nand_get_device (this, mtd ,FL_READING);
1056 /* use userspace supplied oobinfo, if zero */
1057 if (oobsel == NULL)
1058 oobsel = &mtd->oobinfo;
1060 /* Autoplace of oob data ? Use the default placement scheme */
1061 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1062 oobsel = this->autooob;
1064 eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
1065 oob_config = oobsel->eccpos;
1067 /* Select the NAND device */
1068 chipnr = (int)(from >> this->chip_shift);
1069 this->select_chip(mtd, chipnr);
1071 /* First we calculate the starting page */
1072 realpage = (int) (from >> this->page_shift);
1073 page = realpage & this->pagemask;
1075 /* Get raw starting column */
1076 col = from & (mtd->oobblock - 1);
1078 end = mtd->oobblock;
1079 ecc = this->eccsize;
1080 eccbytes = this->eccbytes;
1082 if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
1083 compareecc = 0;
1085 oobreadlen = mtd->oobsize;
1086 if (this->options & NAND_HWECC_SYNDROME)
1087 oobreadlen -= oobsel->eccbytes;
1089 /* Loop until all data read */
1090 while (read < len) {
1092 int aligned = (!col && (len - read) >= end);
1094 * If the read is not page aligned, we have to read into data buffer
1095 * due to ecc, else we read into return buffer direct
1097 if (aligned)
1098 data_poi = &buf[read];
1099 else
1100 data_poi = this->data_buf;
1102 /* Check, if we have this page in the buffer
1104 * FIXME: Make it work when we must provide oob data too,
1105 * check the usage of data_buf oob field
1107 if (realpage == this->pagebuf && !oob_buf) {
1108 /* aligned read ? */
1109 if (aligned)
1110 memcpy (data_poi, this->data_buf, end);
1111 goto readdata;
1114 /* Check, if we must send the read command */
1115 if (sndcmd) {
1116 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1117 sndcmd = 0;
1120 /* get oob area, if we have no oob buffer from fs-driver */
1121 if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1122 oob_data = &this->data_buf[end];
1124 eccsteps = this->eccsteps;
1126 switch (eccmode) {
1127 case NAND_ECC_NONE: { /* No ECC, Read in a page */
1128 static unsigned long lastwhinge = 0;
1129 if ((lastwhinge / HZ) != (jiffies / HZ)) {
1130 printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended\n");
1131 lastwhinge = jiffies;
1133 this->read_buf(mtd, data_poi, end);
1134 break;
1137 case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
1138 this->read_buf(mtd, data_poi, end);
1139 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc)
1140 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1141 break;
1143 default:
1144 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) {
1145 this->enable_hwecc(mtd, NAND_ECC_READ);
1146 this->read_buf(mtd, &data_poi[datidx], ecc);
1148 /* HW ecc with syndrome calculation must read the
1149 * syndrome from flash immidiately after the data */
1150 if (!compareecc) {
1151 /* Some hw ecc generators need to know when the
1152 * syndrome is read from flash */
1153 this->enable_hwecc(mtd, NAND_ECC_READSYN);
1154 this->read_buf(mtd, &oob_data[i], eccbytes);
1155 /* We calc error correction directly, it checks the hw
1156 * generator for an error, reads back the syndrome and
1157 * does the error correction on the fly */
1158 if (this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]) == -1) {
1159 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: "
1160 "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
1161 ecc_failed++;
1163 } else {
1164 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1167 break;
1170 /* read oobdata */
1171 this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
1173 /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
1174 if (!compareecc)
1175 goto readoob;
1177 /* Pick the ECC bytes out of the oob data */
1178 for (j = 0; j < oobsel->eccbytes; j++)
1179 ecc_code[j] = oob_data[oob_config[j]];
1181 /* correct data, if neccecary */
1182 for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {
1183 ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
1185 /* Get next chunk of ecc bytes */
1186 j += eccbytes;
1188 /* Check, if we have a fs supplied oob-buffer,
1189 * This is the legacy mode. Used by YAFFS1
1190 * Should go away some day
1192 if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
1193 int *p = (int *)(&oob_data[mtd->oobsize]);
1194 p[i] = ecc_status;
1197 if (ecc_status == -1) {
1198 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
1199 ecc_failed++;
1203 readoob:
1204 /* check, if we have a fs supplied oob-buffer */
1205 if (oob_buf) {
1206 /* without autoplace. Legacy mode used by YAFFS1 */
1207 switch(oobsel->useecc) {
1208 case MTD_NANDECC_AUTOPLACE:
1209 /* Walk through the autoplace chunks */
1210 for (i = 0, j = 0; j < mtd->oobavail; i++) {
1211 int from = oobsel->oobfree[i][0];
1212 int num = oobsel->oobfree[i][1];
1213 memcpy(&oob_buf[oob], &oob_data[from], num);
1214 j+= num;
1216 oob += mtd->oobavail;
1217 break;
1218 case MTD_NANDECC_PLACE:
1219 /* YAFFS1 legacy mode */
1220 oob_data += this->eccsteps * sizeof (int);
1221 default:
1222 oob_data += mtd->oobsize;
1225 readdata:
1226 /* Partial page read, transfer data into fs buffer */
1227 if (!aligned) {
1228 for (j = col; j < end && read < len; j++)
1229 buf[read++] = data_poi[j];
1230 this->pagebuf = realpage;
1231 } else
1232 read += mtd->oobblock;
1234 /* Apply delay or wait for ready/busy pin
1235 * Do this before the AUTOINCR check, so no problems
1236 * arise if a chip which does auto increment
1237 * is marked as NOAUTOINCR by the board driver.
1239 if (!this->dev_ready)
1240 udelay (this->chip_delay);
1241 else
1242 while (!this->dev_ready(mtd));
1244 if (read == len)
1245 break;
1247 /* For subsequent reads align to page boundary. */
1248 col = 0;
1249 /* Increment page address */
1250 realpage++;
1252 page = realpage & this->pagemask;
1253 /* Check, if we cross a chip boundary */
1254 if (!page) {
1255 chipnr++;
1256 this->select_chip(mtd, -1);
1257 this->select_chip(mtd, chipnr);
1259 /* Check, if the chip supports auto page increment
1260 * or if we have hit a block boundary.
1262 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1263 sndcmd = 1;
1266 /* Deselect and wake up anyone waiting on the device */
1267 nand_release_device(mtd);
1270 * Return success, if no ECC failures, else -EBADMSG
1271 * fs driver will take care of that, because
1272 * retlen == desired len and result == -EBADMSG
1274 *retlen = read;
1275 return ecc_failed ? -EBADMSG : 0;
1279 * nand_read_oob - [MTD Interface] NAND read out-of-band
1280 * @mtd: MTD device structure
1281 * @from: offset to read from
1282 * @len: number of bytes to read
1283 * @retlen: pointer to variable to store the number of read bytes
1284 * @buf: the databuffer to put data
1286 * NAND read out-of-band data from the spare area
1288 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1290 int i, col, page, chipnr;
1291 struct nand_chip *this = mtd->priv;
1292 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1294 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1296 /* Shift to get page */
1297 page = (int)(from >> this->page_shift);
1298 chipnr = (int)(from >> this->chip_shift);
1300 /* Mask to get column */
1301 col = from & (mtd->oobsize - 1);
1303 /* Initialize return length value */
1304 *retlen = 0;
1306 /* Do not allow reads past end of device */
1307 if ((from + len) > mtd->size) {
1308 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n");
1309 *retlen = 0;
1310 return -EINVAL;
1313 /* Grab the lock and see if the device is available */
1314 nand_get_device (this, mtd , FL_READING);
1316 /* Select the NAND device */
1317 this->select_chip(mtd, chipnr);
1319 /* Send the read command */
1320 this->cmdfunc (mtd, NAND_CMD_READOOB, col, page & this->pagemask);
1322 * Read the data, if we read more than one page
1323 * oob data, let the device transfer the data !
1325 i = 0;
1326 while (i < len) {
1327 int thislen = mtd->oobsize - col;
1328 thislen = min_t(int, thislen, len);
1329 this->read_buf(mtd, &buf[i], thislen);
1330 i += thislen;
1332 /* Apply delay or wait for ready/busy pin
1333 * Do this before the AUTOINCR check, so no problems
1334 * arise if a chip which does auto increment
1335 * is marked as NOAUTOINCR by the board driver.
1337 if (!this->dev_ready)
1338 udelay (this->chip_delay);
1339 else
1340 while (!this->dev_ready(mtd));
1342 /* Read more ? */
1343 if (i < len) {
1344 page++;
1345 col = 0;
1347 /* Check, if we cross a chip boundary */
1348 if (!(page & this->pagemask)) {
1349 chipnr++;
1350 this->select_chip(mtd, -1);
1351 this->select_chip(mtd, chipnr);
1354 /* Check, if the chip supports auto page increment
1355 * or if we have hit a block boundary.
1357 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
1358 /* For subsequent page reads set offset to 0 */
1359 this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
1364 /* Deselect and wake up anyone waiting on the device */
1365 nand_release_device(mtd);
1367 /* Return happy */
1368 *retlen = len;
1369 return 0;
1373 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
1374 * @mtd: MTD device structure
1375 * @buf: temporary buffer
1376 * @from: offset to read from
1377 * @len: number of bytes to read
1378 * @ooblen: number of oob data bytes to read
1380 * Read raw data including oob into buffer
1382 int nand_read_raw (struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
1384 struct nand_chip *this = mtd->priv;
1385 int page = (int) (from >> this->page_shift);
1386 int chip = (int) (from >> this->chip_shift);
1387 int sndcmd = 1;
1388 int cnt = 0;
1389 int pagesize = mtd->oobblock + mtd->oobsize;
1390 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1392 /* Do not allow reads past end of device */
1393 if ((from + len) > mtd->size) {
1394 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n");
1395 return -EINVAL;
1398 /* Grab the lock and see if the device is available */
1399 nand_get_device (this, mtd , FL_READING);
1401 this->select_chip (mtd, chip);
1403 /* Add requested oob length */
1404 len += ooblen;
1406 while (len) {
1407 if (sndcmd)
1408 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page & this->pagemask);
1409 sndcmd = 0;
1411 this->read_buf (mtd, &buf[cnt], pagesize);
1413 len -= pagesize;
1414 cnt += pagesize;
1415 page++;
1417 if (!this->dev_ready)
1418 udelay (this->chip_delay);
1419 else
1420 while (!this->dev_ready(mtd));
1422 /* Check, if the chip supports auto page increment */
1423 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1424 sndcmd = 1;
1427 /* Deselect and wake up anyone waiting on the device */
1428 nand_release_device(mtd);
1429 return 0;
1433 /**
1434 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
1435 * @mtd: MTD device structure
1436 * @fsbuf: buffer given by fs driver
1437 * @oobsel: out of band selection structre
1438 * @autoplace: 1 = place given buffer into the oob bytes
1439 * @numpages: number of pages to prepare
1441 * Return:
1442 * 1. Filesystem buffer available and autoplacement is off,
1443 * return filesystem buffer
1444 * 2. No filesystem buffer or autoplace is off, return internal
1445 * buffer
1446 * 3. Filesystem buffer is given and autoplace selected
1447 * put data from fs buffer into internal buffer and
1448 * retrun internal buffer
1450 * Note: The internal buffer is filled with 0xff. This must
1451 * be done only once, when no autoplacement happens
1452 * Autoplacement sets the buffer dirty flag, which
1453 * forces the 0xff fill before using the buffer again.
1456 static u_char * nand_prepare_oobbuf (struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel,
1457 int autoplace, int numpages)
1459 struct nand_chip *this = mtd->priv;
1460 int i, len, ofs;
1462 /* Zero copy fs supplied buffer */
1463 if (fsbuf && !autoplace)
1464 return fsbuf;
1466 /* Check, if the buffer must be filled with ff again */
1467 if (this->oobdirty) {
1468 memset (this->oob_buf, 0xff,
1469 mtd->oobsize << (this->phys_erase_shift - this->page_shift));
1470 this->oobdirty = 0;
1473 /* If we have no autoplacement or no fs buffer use the internal one */
1474 if (!autoplace || !fsbuf)
1475 return this->oob_buf;
1477 /* Walk through the pages and place the data */
1478 this->oobdirty = 1;
1479 ofs = 0;
1480 while (numpages--) {
1481 for (i = 0, len = 0; len < mtd->oobavail; i++) {
1482 int to = ofs + oobsel->oobfree[i][0];
1483 int num = oobsel->oobfree[i][1];
1484 memcpy (&this->oob_buf[to], fsbuf, num);
1485 len += num;
1486 fsbuf += num;
1488 ofs += mtd->oobavail;
1490 return this->oob_buf;
1493 #define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
1496 * nand_write - [MTD Interface] compability function for nand_write_ecc
1497 * @mtd: MTD device structure
1498 * @to: offset to write to
1499 * @len: number of bytes to write
1500 * @retlen: pointer to variable to store the number of written bytes
1501 * @buf: the data to write
1503 * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
1506 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1508 return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));
1512 * nand_write_ecc - [MTD Interface] NAND write with ECC
1513 * @mtd: MTD device structure
1514 * @to: offset to write to
1515 * @len: number of bytes to write
1516 * @retlen: pointer to variable to store the number of written bytes
1517 * @buf: the data to write
1518 * @eccbuf: filesystem supplied oob data buffer
1519 * @oobsel: oob selection structure
1521 * NAND write with ECC
1523 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
1524 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
1526 int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
1527 int autoplace = 0, numpages, totalpages;
1528 struct nand_chip *this = mtd->priv;
1529 u_char *oobbuf, *bufstart;
1530 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1532 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1534 /* Initialize retlen, in case of early exit */
1535 *retlen = 0;
1537 /* Do not allow write past end of device */
1538 if ((to + len) > mtd->size) {
1539 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n");
1540 return -EINVAL;
1543 /* reject writes, which are not page aligned */
1544 if (NOTALIGNED (to) || NOTALIGNED(len)) {
1545 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1546 return -EINVAL;
1549 /* Grab the lock and see if the device is available */
1550 nand_get_device (this, mtd, FL_WRITING);
1552 /* Calculate chipnr */
1553 chipnr = (int)(to >> this->chip_shift);
1554 /* Select the NAND device */
1555 this->select_chip(mtd, chipnr);
1557 /* Check, if it is write protected */
1558 if (nand_check_wp(mtd))
1559 goto out;
1561 /* if oobsel is NULL, use chip defaults */
1562 if (oobsel == NULL)
1563 oobsel = &mtd->oobinfo;
1565 /* Autoplace of oob data ? Use the default placement scheme */
1566 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1567 oobsel = this->autooob;
1568 autoplace = 1;
1571 /* Setup variables and oob buffer */
1572 totalpages = len >> this->page_shift;
1573 page = (int) (to >> this->page_shift);
1574 /* Invalidate the page cache, if we write to the cached page */
1575 if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
1576 this->pagebuf = -1;
1578 /* Set it relative to chip */
1579 page &= this->pagemask;
1580 startpage = page;
1581 /* Calc number of pages we can write in one go */
1582 numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages);
1583 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages);
1584 bufstart = (u_char *)buf;
1586 /* Loop until all data is written */
1587 while (written < len) {
1589 this->data_poi = (u_char*) &buf[written];
1590 /* Write one page. If this is the last page to write
1591 * or the last page in this block, then use the
1592 * real pageprogram command, else select cached programming
1593 * if supported by the chip.
1595 ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
1596 if (ret) {
1597 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret);
1598 goto out;
1600 /* Next oob page */
1601 oob += mtd->oobsize;
1602 /* Update written bytes count */
1603 written += mtd->oobblock;
1604 if (written == len)
1605 goto cmp;
1607 /* Increment page address */
1608 page++;
1610 /* Have we hit a block boundary ? Then we have to verify and
1611 * if verify is ok, we have to setup the oob buffer for
1612 * the next pages.
1614 if (!(page & (ppblock - 1))){
1615 int ofs;
1616 this->data_poi = bufstart;
1617 ret = nand_verify_pages (mtd, this, startpage,
1618 page - startpage,
1619 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1620 if (ret) {
1621 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1622 goto out;
1624 *retlen = written;
1626 ofs = autoplace ? mtd->oobavail : mtd->oobsize;
1627 if (eccbuf)
1628 eccbuf += (page - startpage) * ofs;
1629 totalpages -= page - startpage;
1630 numpages = min (totalpages, ppblock);
1631 page &= this->pagemask;
1632 startpage = page;
1633 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel,
1634 autoplace, numpages);
1635 /* Check, if we cross a chip boundary */
1636 if (!page) {
1637 chipnr++;
1638 this->select_chip(mtd, -1);
1639 this->select_chip(mtd, chipnr);
1643 /* Verify the remaining pages */
1644 cmp:
1645 this->data_poi = bufstart;
1646 ret = nand_verify_pages (mtd, this, startpage, totalpages,
1647 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1648 if (!ret)
1649 *retlen = written;
1650 else
1651 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1653 out:
1654 /* Deselect and wake up anyone waiting on the device */
1655 nand_release_device(mtd);
1657 return ret;
1662 * nand_write_oob - [MTD Interface] NAND write out-of-band
1663 * @mtd: MTD device structure
1664 * @to: offset to write to
1665 * @len: number of bytes to write
1666 * @retlen: pointer to variable to store the number of written bytes
1667 * @buf: the data to write
1669 * NAND write out-of-band
1671 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1673 int column, page, status, ret = -EIO, chipnr;
1674 struct nand_chip *this = mtd->priv;
1676 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1678 /* Shift to get page */
1679 page = (int) (to >> this->page_shift);
1680 chipnr = (int) (to >> this->chip_shift);
1682 /* Mask to get column */
1683 column = to & (mtd->oobsize - 1);
1685 /* Initialize return length value */
1686 *retlen = 0;
1688 /* Do not allow write past end of page */
1689 if ((column + len) > mtd->oobsize) {
1690 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
1691 return -EINVAL;
1694 /* Grab the lock and see if the device is available */
1695 nand_get_device (this, mtd, FL_WRITING);
1697 /* Select the NAND device */
1698 this->select_chip(mtd, chipnr);
1700 /* Reset the chip. Some chips (like the Toshiba TC5832DC found
1701 in one of my DiskOnChip 2000 test units) will clear the whole
1702 data page too if we don't do this. I have no clue why, but
1703 I seem to have 'fixed' it in the doc2000 driver in
1704 August 1999. dwmw2. */
1705 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1707 /* Check, if it is write protected */
1708 if (nand_check_wp(mtd))
1709 goto out;
1711 /* Invalidate the page cache, if we write to the cached page */
1712 if (page == this->pagebuf)
1713 this->pagebuf = -1;
1715 if (NAND_MUST_PAD(this)) {
1716 /* Write out desired data */
1717 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
1718 /* prepad 0xff for partial programming */
1719 this->write_buf(mtd, ffchars, column);
1720 /* write data */
1721 this->write_buf(mtd, buf, len);
1722 /* postpad 0xff for partial programming */
1723 this->write_buf(mtd, ffchars, mtd->oobsize - (len+column));
1724 } else {
1725 /* Write out desired data */
1726 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
1727 /* write data */
1728 this->write_buf(mtd, buf, len);
1730 /* Send command to program the OOB data */
1731 this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1);
1733 status = this->waitfunc (mtd, this, FL_WRITING);
1735 /* See if device thinks it succeeded */
1736 if (status & 0x01) {
1737 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
1738 ret = -EIO;
1739 goto out;
1741 /* Return happy */
1742 *retlen = len;
1744 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1745 /* Send command to read back the data */
1746 this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask);
1748 if (this->verify_buf(mtd, buf, len)) {
1749 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
1750 ret = -EIO;
1751 goto out;
1753 #endif
1754 ret = 0;
1755 out:
1756 /* Deselect and wake up anyone waiting on the device */
1757 nand_release_device(mtd);
1759 return ret;
1764 * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
1765 * @mtd: MTD device structure
1766 * @vecs: the iovectors to write
1767 * @count: number of vectors
1768 * @to: offset to write to
1769 * @retlen: pointer to variable to store the number of written bytes
1771 * NAND write with kvec. This just calls the ecc function
1773 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1774 loff_t to, size_t * retlen)
1776 return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL));
1780 * nand_writev_ecc - [MTD Interface] write with iovec with ecc
1781 * @mtd: MTD device structure
1782 * @vecs: the iovectors to write
1783 * @count: number of vectors
1784 * @to: offset to write to
1785 * @retlen: pointer to variable to store the number of written bytes
1786 * @eccbuf: filesystem supplied oob data buffer
1787 * @oobsel: oob selection structure
1789 * NAND write with iovec with ecc
1791 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1792 loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
1794 int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
1795 int oob, numpages, autoplace = 0, startpage;
1796 struct nand_chip *this = mtd->priv;
1797 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1798 u_char *oobbuf, *bufstart;
1800 /* Preset written len for early exit */
1801 *retlen = 0;
1803 /* Calculate total length of data */
1804 total_len = 0;
1805 for (i = 0; i < count; i++)
1806 total_len += (int) vecs[i].iov_len;
1808 DEBUG (MTD_DEBUG_LEVEL3,
1809 "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
1811 /* Do not allow write past end of page */
1812 if ((to + total_len) > mtd->size) {
1813 DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
1814 return -EINVAL;
1817 /* reject writes, which are not page aligned */
1818 if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
1819 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1820 return -EINVAL;
1823 /* Grab the lock and see if the device is available */
1824 nand_get_device (this, mtd, FL_WRITING);
1826 /* Get the current chip-nr */
1827 chipnr = (int) (to >> this->chip_shift);
1828 /* Select the NAND device */
1829 this->select_chip(mtd, chipnr);
1831 /* Check, if it is write protected */
1832 if (nand_check_wp(mtd))
1833 goto out;
1835 /* if oobsel is NULL, use chip defaults */
1836 if (oobsel == NULL)
1837 oobsel = &mtd->oobinfo;
1839 /* Autoplace of oob data ? Use the default placement scheme */
1840 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1841 oobsel = this->autooob;
1842 autoplace = 1;
1845 /* Setup start page */
1846 page = (int) (to >> this->page_shift);
1847 /* Invalidate the page cache, if we write to the cached page */
1848 if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))
1849 this->pagebuf = -1;
1851 startpage = page & this->pagemask;
1853 /* Loop until all kvec' data has been written */
1854 len = 0;
1855 while (count) {
1856 /* If the given tuple is >= pagesize then
1857 * write it out from the iov
1859 if ((vecs->iov_len - len) >= mtd->oobblock) {
1860 /* Calc number of pages we can write
1861 * out of this iov in one go */
1862 numpages = (vecs->iov_len - len) >> this->page_shift;
1863 /* Do not cross block boundaries */
1864 numpages = min (ppblock - (startpage & (ppblock - 1)), numpages);
1865 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1866 bufstart = (u_char *)vecs->iov_base;
1867 bufstart += len;
1868 this->data_poi = bufstart;
1869 oob = 0;
1870 for (i = 1; i <= numpages; i++) {
1871 /* Write one page. If this is the last page to write
1872 * then use the real pageprogram command, else select
1873 * cached programming if supported by the chip.
1875 ret = nand_write_page (mtd, this, page & this->pagemask,
1876 &oobbuf[oob], oobsel, i != numpages);
1877 if (ret)
1878 goto out;
1879 this->data_poi += mtd->oobblock;
1880 len += mtd->oobblock;
1881 oob += mtd->oobsize;
1882 page++;
1884 /* Check, if we have to switch to the next tuple */
1885 if (len >= (int) vecs->iov_len) {
1886 vecs++;
1887 len = 0;
1888 count--;
1890 } else {
1891 /* We must use the internal buffer, read data out of each
1892 * tuple until we have a full page to write
1894 int cnt = 0;
1895 while (cnt < mtd->oobblock) {
1896 if (vecs->iov_base != NULL && vecs->iov_len)
1897 this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
1898 /* Check, if we have to switch to the next tuple */
1899 if (len >= (int) vecs->iov_len) {
1900 vecs++;
1901 len = 0;
1902 count--;
1905 this->pagebuf = page;
1906 this->data_poi = this->data_buf;
1907 bufstart = this->data_poi;
1908 numpages = 1;
1909 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1910 ret = nand_write_page (mtd, this, page & this->pagemask,
1911 oobbuf, oobsel, 0);
1912 if (ret)
1913 goto out;
1914 page++;
1917 this->data_poi = bufstart;
1918 ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
1919 if (ret)
1920 goto out;
1922 written += mtd->oobblock * numpages;
1923 /* All done ? */
1924 if (!count)
1925 break;
1927 startpage = page & this->pagemask;
1928 /* Check, if we cross a chip boundary */
1929 if (!startpage) {
1930 chipnr++;
1931 this->select_chip(mtd, -1);
1932 this->select_chip(mtd, chipnr);
1935 ret = 0;
1936 out:
1937 /* Deselect and wake up anyone waiting on the device */
1938 nand_release_device(mtd);
1940 *retlen = written;
1941 return ret;
1945 * single_erease_cmd - [GENERIC] NAND standard block erase command function
1946 * @mtd: MTD device structure
1947 * @page: the page address of the block which will be erased
1949 * Standard erase command for NAND chips
1951 static void single_erase_cmd (struct mtd_info *mtd, int page)
1953 struct nand_chip *this = mtd->priv;
1954 /* Send commands to erase a block */
1955 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
1956 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
1960 * multi_erease_cmd - [GENERIC] AND specific block erase command function
1961 * @mtd: MTD device structure
1962 * @page: the page address of the block which will be erased
1964 * AND multi block erase command function
1965 * Erase 4 consecutive blocks
1967 static void multi_erase_cmd (struct mtd_info *mtd, int page)
1969 struct nand_chip *this = mtd->priv;
1970 /* Send commands to erase a block */
1971 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
1972 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
1973 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
1974 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
1975 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
1979 * nand_erase - [MTD Interface] erase block(s)
1980 * @mtd: MTD device structure
1981 * @instr: erase instruction
1983 * Erase one ore more blocks
1985 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
1987 return nand_erase_nand (mtd, instr, 0);
1991 * nand_erase_intern - [NAND Interface] erase block(s)
1992 * @mtd: MTD device structure
1993 * @instr: erase instruction
1994 * @allowbbt: allow erasing the bbt area
1996 * Erase one ore more blocks
1998 int nand_erase_nand (struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
2000 int page, len, status, pages_per_block, ret, chipnr;
2001 struct nand_chip *this = mtd->priv;
2003 DEBUG (MTD_DEBUG_LEVEL3,
2004 "nand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
2006 /* Start address must align on block boundary */
2007 if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
2008 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2009 return -EINVAL;
2012 /* Length must align on block boundary */
2013 if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
2014 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
2015 return -EINVAL;
2018 /* Do not allow erase past end of device */
2019 if ((instr->len + instr->addr) > mtd->size) {
2020 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
2021 return -EINVAL;
2024 instr->fail_addr = 0xffffffff;
2026 /* Grab the lock and see if the device is available */
2027 nand_get_device (this, mtd, FL_ERASING);
2029 /* Shift to get first page */
2030 page = (int) (instr->addr >> this->page_shift);
2031 chipnr = (int) (instr->addr >> this->chip_shift);
2033 /* Calculate pages in each block */
2034 pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
2036 /* Select the NAND device */
2037 this->select_chip(mtd, chipnr);
2039 /* Check the WP bit */
2040 /* Check, if it is write protected */
2041 if (nand_check_wp(mtd)) {
2042 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
2043 instr->state = MTD_ERASE_FAILED;
2044 goto erase_exit;
2047 /* Loop through the pages */
2048 len = instr->len;
2050 instr->state = MTD_ERASING;
2052 while (len) {
2053 /* Check if we have a bad block, we do not erase bad blocks ! */
2054 if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
2055 printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
2056 instr->state = MTD_ERASE_FAILED;
2057 goto erase_exit;
2060 /* Invalidate the page cache, if we erase the block which contains
2061 the current cached page */
2062 if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
2063 this->pagebuf = -1;
2065 this->erase_cmd (mtd, page & this->pagemask);
2067 status = this->waitfunc (mtd, this, FL_ERASING);
2069 /* See if block erase succeeded */
2070 if (status & 0x01) {
2071 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
2072 instr->state = MTD_ERASE_FAILED;
2073 instr->fail_addr = (page << this->page_shift);
2074 goto erase_exit;
2077 /* Increment page address and decrement length */
2078 len -= (1 << this->phys_erase_shift);
2079 page += pages_per_block;
2081 /* Check, if we cross a chip boundary */
2082 if (len && !(page & this->pagemask)) {
2083 chipnr++;
2084 this->select_chip(mtd, -1);
2085 this->select_chip(mtd, chipnr);
2088 instr->state = MTD_ERASE_DONE;
2090 erase_exit:
2092 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2093 /* Do call back function */
2094 if (!ret)
2095 mtd_erase_callback(instr);
2097 /* Deselect and wake up anyone waiting on the device */
2098 nand_release_device(mtd);
2100 /* Return more or less happy */
2101 return ret;
2105 * nand_sync - [MTD Interface] sync
2106 * @mtd: MTD device structure
2108 * Sync is actually a wait for chip ready function
2110 static void nand_sync (struct mtd_info *mtd)
2112 struct nand_chip *this = mtd->priv;
2114 DEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2116 /* Grab the lock and see if the device is available */
2117 nand_get_device (this, mtd, FL_SYNCING);
2118 /* Release it and go back */
2119 nand_release_device (mtd);
2124 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2125 * @mtd: MTD device structure
2126 * @ofs: offset relative to mtd start
2128 static int nand_block_isbad (struct mtd_info *mtd, loff_t ofs)
2130 /* Check for invalid offset */
2131 if (ofs > mtd->size)
2132 return -EINVAL;
2134 return nand_block_checkbad (mtd, ofs, 1, 0);
2138 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2139 * @mtd: MTD device structure
2140 * @ofs: offset relative to mtd start
2142 static int nand_block_markbad (struct mtd_info *mtd, loff_t ofs)
2144 struct nand_chip *this = mtd->priv;
2145 int ret;
2147 if ((ret = nand_block_isbad(mtd, ofs))) {
2148 /* If it was bad already, return success and do nothing. */
2149 if (ret > 0)
2150 return 0;
2151 return ret;
2154 return this->block_markbad(mtd, ofs);
2158 * nand_scan - [NAND Interface] Scan for the NAND device
2159 * @mtd: MTD device structure
2160 * @maxchips: Number of chips to scan for
2162 * This fills out all the not initialized function pointers
2163 * with the defaults.
2164 * The flash ID is read and the mtd/chip structures are
2165 * filled with the appropriate values. Buffers are allocated if
2166 * they are not provided by the board driver
2169 int nand_scan (struct mtd_info *mtd, int maxchips)
2171 int i, j, nand_maf_id, nand_dev_id, busw;
2172 struct nand_chip *this = mtd->priv;
2174 /* Get buswidth to select the correct functions*/
2175 busw = this->options & NAND_BUSWIDTH_16;
2177 /* check for proper chip_delay setup, set 20us if not */
2178 if (!this->chip_delay)
2179 this->chip_delay = 20;
2181 /* check, if a user supplied command function given */
2182 if (this->cmdfunc == NULL)
2183 this->cmdfunc = nand_command;
2185 /* check, if a user supplied wait function given */
2186 if (this->waitfunc == NULL)
2187 this->waitfunc = nand_wait;
2189 if (!this->select_chip)
2190 this->select_chip = nand_select_chip;
2191 if (!this->write_byte)
2192 this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2193 if (!this->read_byte)
2194 this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2195 if (!this->write_word)
2196 this->write_word = nand_write_word;
2197 if (!this->read_word)
2198 this->read_word = nand_read_word;
2199 if (!this->block_bad)
2200 this->block_bad = nand_block_bad;
2201 if (!this->block_markbad)
2202 this->block_markbad = nand_default_block_markbad;
2203 if (!this->write_buf)
2204 this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2205 if (!this->read_buf)
2206 this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2207 if (!this->verify_buf)
2208 this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2209 if (!this->scan_bbt)
2210 this->scan_bbt = nand_default_bbt;
2212 /* Select the device */
2213 this->select_chip(mtd, 0);
2215 /* Send the command for reading device ID */
2216 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2218 /* Read manufacturer and device IDs */
2219 nand_maf_id = this->read_byte(mtd);
2220 nand_dev_id = this->read_byte(mtd);
2222 /* Print and store flash device information */
2223 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2225 if (nand_dev_id != nand_flash_ids[i].id)
2226 continue;
2228 if (!mtd->name) mtd->name = nand_flash_ids[i].name;
2229 this->chipsize = nand_flash_ids[i].chipsize << 20;
2231 /* New devices have all the information in additional id bytes */
2232 if (!nand_flash_ids[i].pagesize) {
2233 int extid;
2234 /* The 3rd id byte contains non relevant data ATM */
2235 extid = this->read_byte(mtd);
2236 /* The 4th id byte is the important one */
2237 extid = this->read_byte(mtd);
2238 /* Calc pagesize */
2239 mtd->oobblock = 1024 << (extid & 0x3);
2240 extid >>= 2;
2241 /* Calc oobsize */
2242 mtd->oobsize = (8 << (extid & 0x03)) * (mtd->oobblock / 512);
2243 extid >>= 2;
2244 /* Calc blocksize. Blocksize is multiples of 64KiB */
2245 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2246 extid >>= 2;
2247 /* Get buswidth information */
2248 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2250 } else {
2251 /* Old devices have this data hardcoded in the
2252 * device id table */
2253 mtd->erasesize = nand_flash_ids[i].erasesize;
2254 mtd->oobblock = nand_flash_ids[i].pagesize;
2255 mtd->oobsize = mtd->oobblock / 32;
2256 busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
2259 /* Check, if buswidth is correct. Hardware drivers should set
2260 * this correct ! */
2261 if (busw != (this->options & NAND_BUSWIDTH_16)) {
2262 printk (KERN_INFO "NAND device: Manufacturer ID:"
2263 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2264 nand_manuf_ids[i].name , mtd->name);
2265 printk (KERN_WARNING
2266 "NAND bus width %d instead %d bit\n",
2267 (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
2268 busw ? 16 : 8);
2269 this->select_chip(mtd, -1);
2270 return 1;
2273 /* Calculate the address shift from the page size */
2274 this->page_shift = ffs(mtd->oobblock) - 1;
2275 this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
2276 this->chip_shift = ffs(this->chipsize) - 1;
2278 /* Set the bad block position */
2279 this->badblockpos = mtd->oobblock > 512 ?
2280 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2282 /* Get chip options, preserve non chip based options */
2283 this->options &= ~NAND_CHIPOPTIONS_MSK;
2284 this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
2285 /* Set this as a default. Board drivers can override it, if neccecary */
2286 this->options |= NAND_NO_AUTOINCR;
2287 /* Check if this is a not a samsung device. Do not clear the options
2288 * for chips which are not having an extended id.
2290 if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
2291 this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2293 /* Check for AND chips with 4 page planes */
2294 if (this->options & NAND_4PAGE_ARRAY)
2295 this->erase_cmd = multi_erase_cmd;
2296 else
2297 this->erase_cmd = single_erase_cmd;
2299 /* Do not replace user supplied command function ! */
2300 if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
2301 this->cmdfunc = nand_command_lp;
2303 /* Try to identify manufacturer */
2304 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
2305 if (nand_manuf_ids[j].id == nand_maf_id)
2306 break;
2308 printk (KERN_INFO "NAND device: Manufacturer ID:"
2309 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2310 nand_manuf_ids[j].name , nand_flash_ids[i].name);
2311 break;
2314 if (!nand_flash_ids[i].name) {
2315 printk (KERN_WARNING "No NAND device found!!!\n");
2316 this->select_chip(mtd, -1);
2317 return 1;
2320 for (i=1; i < maxchips; i++) {
2321 this->select_chip(mtd, i);
2323 /* Send the command for reading device ID */
2324 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2326 /* Read manufacturer and device IDs */
2327 if (nand_maf_id != this->read_byte(mtd) ||
2328 nand_dev_id != this->read_byte(mtd))
2329 break;
2331 if (i > 1)
2332 printk(KERN_INFO "%d NAND chips detected\n", i);
2334 /* Allocate buffers, if neccecary */
2335 if (!this->oob_buf) {
2336 size_t len;
2337 len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
2338 this->oob_buf = kmalloc (len, GFP_KERNEL);
2339 if (!this->oob_buf) {
2340 printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
2341 return -ENOMEM;
2343 this->options |= NAND_OOBBUF_ALLOC;
2346 if (!this->data_buf) {
2347 size_t len;
2348 len = mtd->oobblock + mtd->oobsize;
2349 this->data_buf = kmalloc (len, GFP_KERNEL);
2350 if (!this->data_buf) {
2351 if (this->options & NAND_OOBBUF_ALLOC)
2352 kfree (this->oob_buf);
2353 printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
2354 return -ENOMEM;
2356 this->options |= NAND_DATABUF_ALLOC;
2359 /* Store the number of chips and calc total size for mtd */
2360 this->numchips = i;
2361 mtd->size = i * this->chipsize;
2362 /* Convert chipsize to number of pages per chip -1. */
2363 this->pagemask = (this->chipsize >> this->page_shift) - 1;
2364 /* Preset the internal oob buffer */
2365 memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
2367 /* If no default placement scheme is given, select an
2368 * appropriate one */
2369 if (!this->autooob) {
2370 /* Select the appropriate default oob placement scheme for
2371 * placement agnostic filesystems */
2372 switch (mtd->oobsize) {
2373 case 8:
2374 this->autooob = &nand_oob_8;
2375 break;
2376 case 16:
2377 this->autooob = &nand_oob_16;
2378 break;
2379 case 64:
2380 this->autooob = &nand_oob_64;
2381 break;
2382 default:
2383 printk (KERN_WARNING "No oob scheme defined for oobsize %d\n",
2384 mtd->oobsize);
2385 BUG();
2389 /* The number of bytes available for the filesystem to place fs dependend
2390 * oob data */
2391 if (this->options & NAND_BUSWIDTH_16) {
2392 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 2);
2393 if (this->autooob->eccbytes & 0x01)
2394 mtd->oobavail--;
2395 } else
2396 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 1);
2399 * check ECC mode, default to software
2400 * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
2401 * fallback to software ECC
2403 this->eccsize = 256; /* set default eccsize */
2404 this->eccbytes = 3;
2406 switch (this->eccmode) {
2407 case NAND_ECC_HW12_2048:
2408 if (mtd->oobblock < 2048) {
2409 printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
2410 mtd->oobblock);
2411 this->eccmode = NAND_ECC_SOFT;
2412 this->calculate_ecc = nand_calculate_ecc;
2413 this->correct_data = nand_correct_data;
2414 } else
2415 this->eccsize = 2048;
2416 break;
2418 case NAND_ECC_HW3_512:
2419 case NAND_ECC_HW6_512:
2420 case NAND_ECC_HW8_512:
2421 if (mtd->oobblock == 256) {
2422 printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
2423 this->eccmode = NAND_ECC_SOFT;
2424 this->calculate_ecc = nand_calculate_ecc;
2425 this->correct_data = nand_correct_data;
2426 } else
2427 this->eccsize = 512; /* set eccsize to 512 */
2428 break;
2430 case NAND_ECC_HW3_256:
2431 break;
2433 case NAND_ECC_NONE:
2434 printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
2435 this->eccmode = NAND_ECC_NONE;
2436 break;
2438 case NAND_ECC_SOFT:
2439 this->calculate_ecc = nand_calculate_ecc;
2440 this->correct_data = nand_correct_data;
2441 break;
2443 default:
2444 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
2445 BUG();
2448 /* Check hardware ecc function availability and adjust number of ecc bytes per
2449 * calculation step
2451 switch (this->eccmode) {
2452 case NAND_ECC_HW12_2048:
2453 this->eccbytes += 4;
2454 case NAND_ECC_HW8_512:
2455 this->eccbytes += 2;
2456 case NAND_ECC_HW6_512:
2457 this->eccbytes += 3;
2458 case NAND_ECC_HW3_512:
2459 case NAND_ECC_HW3_256:
2460 if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
2461 break;
2462 printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
2463 BUG();
2466 mtd->eccsize = this->eccsize;
2468 /* Set the number of read / write steps for one page to ensure ECC generation */
2469 switch (this->eccmode) {
2470 case NAND_ECC_HW12_2048:
2471 this->eccsteps = mtd->oobblock / 2048;
2472 break;
2473 case NAND_ECC_HW3_512:
2474 case NAND_ECC_HW6_512:
2475 case NAND_ECC_HW8_512:
2476 this->eccsteps = mtd->oobblock / 512;
2477 break;
2478 case NAND_ECC_HW3_256:
2479 case NAND_ECC_SOFT:
2480 this->eccsteps = mtd->oobblock / 256;
2481 break;
2483 case NAND_ECC_NONE:
2484 this->eccsteps = 1;
2485 break;
2488 /* Initialize state, waitqueue and spinlock */
2489 this->state = FL_READY;
2490 init_waitqueue_head (&this->wq);
2491 spin_lock_init (&this->chip_lock);
2493 /* De-select the device */
2494 this->select_chip(mtd, -1);
2496 /* Invalidate the pagebuffer reference */
2497 this->pagebuf = -1;
2499 /* Fill in remaining MTD driver data */
2500 mtd->type = MTD_NANDFLASH;
2501 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
2502 mtd->ecctype = MTD_ECC_SW;
2503 mtd->erase = nand_erase;
2504 mtd->point = NULL;
2505 mtd->unpoint = NULL;
2506 mtd->read = nand_read;
2507 mtd->write = nand_write;
2508 mtd->read_ecc = nand_read_ecc;
2509 mtd->write_ecc = nand_write_ecc;
2510 mtd->read_oob = nand_read_oob;
2511 mtd->write_oob = nand_write_oob;
2512 mtd->readv = NULL;
2513 mtd->writev = nand_writev;
2514 mtd->writev_ecc = nand_writev_ecc;
2515 mtd->sync = nand_sync;
2516 mtd->lock = NULL;
2517 mtd->unlock = NULL;
2518 mtd->suspend = NULL;
2519 mtd->resume = NULL;
2520 mtd->block_isbad = nand_block_isbad;
2521 mtd->block_markbad = nand_block_markbad;
2523 /* and make the autooob the default one */
2524 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
2526 mtd->owner = THIS_MODULE;
2528 /* Build bad block table */
2529 return this->scan_bbt (mtd);
2533 * nand_release - [NAND Interface] Free resources held by the NAND device
2534 * @mtd: MTD device structure
2536 void nand_release (struct mtd_info *mtd)
2538 struct nand_chip *this = mtd->priv;
2540 #ifdef CONFIG_MTD_PARTITIONS
2541 /* Deregister partitions */
2542 del_mtd_partitions (mtd);
2543 #endif
2544 /* Deregister the device */
2545 del_mtd_device (mtd);
2547 /* Free bad block table memory, if allocated */
2548 if (this->bbt)
2549 kfree (this->bbt);
2550 /* Buffer allocated by nand_scan ? */
2551 if (this->options & NAND_OOBBUF_ALLOC)
2552 kfree (this->oob_buf);
2553 /* Buffer allocated by nand_scan ? */
2554 if (this->options & NAND_DATABUF_ALLOC)
2555 kfree (this->data_buf);
2558 EXPORT_SYMBOL (nand_scan);
2559 EXPORT_SYMBOL (nand_release);
2561 MODULE_LICENSE ("GPL");
2562 MODULE_AUTHOR ("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2563 MODULE_DESCRIPTION ("Generic NAND flash driver code");