5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
9 * Additional technical information is available on
10 * http://www.linux-mtd.infradead.org/tech/nand.html
12 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
13 * 2002 Thomas Gleixner (tglx@linutronix.de)
15 * 02-08-2004 tglx: support for strange chips, which cannot auto increment
16 * pages on read / read_oob
18 * 03-17-2004 tglx: Check ready before auto increment check. Simon Bayes
19 * pointed this out, as he marked an auto increment capable chip
20 * as NOAUTOINCR in the board driver.
21 * Make reads over block boundaries work too
23 * 04-14-2004 tglx: first working version for 2k page size chips
25 * 05-19-2004 tglx: Basic support for Renesas AG-AND chips
28 * David Woodhouse for adding multichip support
30 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
31 * rework for 2K page size chips
34 * Enable cached programming for 2k page size chips
35 * Check, if mtd->ecctype should be set to MTD_ECC_HW
36 * if we have HW ecc support.
37 * The AG-AND chips have nice features for speed improvement,
38 * which are not supported yet. Read / program 4 pages in one go.
40 * $Id: nand_base.c,v 1.113 2004/07/14 16:31:31 gleixner Exp $
42 * This program is free software; you can redistribute it and/or modify
43 * it under the terms of the GNU General Public License version 2 as
44 * published by the Free Software Foundation.
48 #include <linux/delay.h>
49 #include <linux/errno.h>
50 #include <linux/sched.h>
51 #include <linux/slab.h>
52 #include <linux/types.h>
53 #include <linux/mtd/mtd.h>
54 #include <linux/mtd/nand.h>
55 #include <linux/mtd/nand_ecc.h>
56 #include <linux/mtd/compatmac.h>
57 #include <linux/interrupt.h>
58 #include <linux/bitops.h>
61 #if defined(CONFIG_MTD_PARTITIONS) || defined(CONFIG_MTD_PARTITIONS_MODULE)
62 #include <linux/mtd/partitions.h>
65 /* Define default oob placement schemes for large and small page devices */
66 static struct nand_oobinfo nand_oob_8 = {
67 .useecc = MTD_NANDECC_AUTOPLACE,
70 .oobfree = { {3, 2}, {6, 2} }
73 static struct nand_oobinfo nand_oob_16 = {
74 .useecc = MTD_NANDECC_AUTOPLACE,
76 .eccpos = {0, 1, 2, 3, 6, 7},
80 static struct nand_oobinfo nand_oob_64 = {
81 .useecc = MTD_NANDECC_AUTOPLACE,
84 40, 41, 42, 43, 44, 45, 46, 47,
85 48, 49, 50, 51, 52, 53, 54, 55,
86 56, 57, 58, 59, 60, 61, 62, 63},
87 .oobfree = { {2, 38} }
90 /* This is used for padding purposes in nand_write_oob */
91 static u_char ffchars[] = {
92 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
93 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
94 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
95 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
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,
103 * NAND low-level MTD interface functions
105 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
106 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
107 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
109 static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
110 static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
111 size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
112 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
113 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf);
114 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
115 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
116 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char *buf);
117 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
118 unsigned long count, loff_t to, size_t * retlen);
119 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs,
120 unsigned long count, loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
121 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
122 static void nand_sync (struct mtd_info *mtd);
124 /* Some internal functions */
125 static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf,
126 struct nand_oobinfo *oobsel, int mode);
127 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
128 static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages,
129 u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode);
131 #define nand_verify_pages(...) (0)
134 static void nand_get_chip (struct nand_chip *this, struct mtd_info *mtd, int new_state);
137 * nand_release_chip - [GENERIC] release chip
138 * @mtd: MTD device structure
140 * Deselect, release chip lock and wake up anyone waiting on the device
142 static void nand_release_chip (struct mtd_info *mtd)
144 struct nand_chip *this = mtd->priv;
146 /* De-select the NAND device */
147 this->select_chip(mtd, -1);
148 /* Release the chip */
149 spin_lock_bh (&this->chip_lock);
150 this->state = FL_READY;
152 spin_unlock_bh (&this->chip_lock);
156 * nand_read_byte - [DEFAULT] read one byte from the chip
157 * @mtd: MTD device structure
159 * Default read function for 8bit buswith
161 static u_char nand_read_byte(struct mtd_info *mtd)
163 struct nand_chip *this = mtd->priv;
164 return readb(this->IO_ADDR_R);
168 * nand_write_byte - [DEFAULT] write one byte to the chip
169 * @mtd: MTD device structure
170 * @byte: pointer to data byte to write
172 * Default write function for 8it buswith
174 static void nand_write_byte(struct mtd_info *mtd, u_char byte)
176 struct nand_chip *this = mtd->priv;
177 writeb(byte, this->IO_ADDR_W);
181 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
182 * @mtd: MTD device structure
184 * Default read function for 16bit buswith with
185 * endianess conversion
187 static u_char nand_read_byte16(struct mtd_info *mtd)
189 struct nand_chip *this = mtd->priv;
190 return (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
194 * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
195 * @mtd: MTD device structure
196 * @byte: pointer to data byte to write
198 * Default write function for 16bit buswith with
199 * endianess conversion
201 static void nand_write_byte16(struct mtd_info *mtd, u_char byte)
203 struct nand_chip *this = mtd->priv;
204 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
208 * nand_read_word - [DEFAULT] read one word from the chip
209 * @mtd: MTD device structure
211 * Default read function for 16bit buswith without
212 * endianess conversion
214 static u16 nand_read_word(struct mtd_info *mtd)
216 struct nand_chip *this = mtd->priv;
217 return readw(this->IO_ADDR_R);
221 * nand_write_word - [DEFAULT] write one word to the chip
222 * @mtd: MTD device structure
223 * @word: data word to write
225 * Default write function for 16bit buswith without
226 * endianess conversion
228 static void nand_write_word(struct mtd_info *mtd, u16 word)
230 struct nand_chip *this = mtd->priv;
231 writew(word, this->IO_ADDR_W);
235 * nand_select_chip - [DEFAULT] control CE line
236 * @mtd: MTD device structure
237 * @chip: chipnumber to select, -1 for deselect
239 * Default select function for 1 chip devices.
241 static void nand_select_chip(struct mtd_info *mtd, int chip)
243 struct nand_chip *this = mtd->priv;
246 this->hwcontrol(mtd, NAND_CTL_CLRNCE);
249 this->hwcontrol(mtd, NAND_CTL_SETNCE);
258 * nand_write_buf - [DEFAULT] write buffer to chip
259 * @mtd: MTD device structure
261 * @len: number of bytes to write
263 * Default write function for 8bit buswith
265 static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
268 struct nand_chip *this = mtd->priv;
270 for (i=0; i<len; i++)
271 writeb(buf[i], this->IO_ADDR_W);
275 * nand_read_buf - [DEFAULT] read chip data into buffer
276 * @mtd: MTD device structure
277 * @buf: buffer to store date
278 * @len: number of bytes to read
280 * Default read function for 8bit buswith
282 static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
285 struct nand_chip *this = mtd->priv;
287 for (i=0; i<len; i++)
288 buf[i] = readb(this->IO_ADDR_R);
292 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
293 * @mtd: MTD device structure
294 * @buf: buffer containing the data to compare
295 * @len: number of bytes to compare
297 * Default verify function for 8bit buswith
299 static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
302 struct nand_chip *this = mtd->priv;
304 for (i=0; i<len; i++)
305 if (buf[i] != readb(this->IO_ADDR_R))
312 * nand_write_buf16 - [DEFAULT] write buffer to chip
313 * @mtd: MTD device structure
315 * @len: number of bytes to write
317 * Default write function for 16bit buswith
319 static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
322 struct nand_chip *this = mtd->priv;
323 u16 *p = (u16 *) buf;
326 for (i=0; i<len; i++)
327 writew(p[i], this->IO_ADDR_W);
332 * nand_read_buf16 - [DEFAULT] read chip data into buffer
333 * @mtd: MTD device structure
334 * @buf: buffer to store date
335 * @len: number of bytes to read
337 * Default read function for 16bit buswith
339 static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
342 struct nand_chip *this = mtd->priv;
343 u16 *p = (u16 *) buf;
346 for (i=0; i<len; i++)
347 p[i] = readw(this->IO_ADDR_R);
351 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
352 * @mtd: MTD device structure
353 * @buf: buffer containing the data to compare
354 * @len: number of bytes to compare
356 * Default verify function for 16bit buswith
358 static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
361 struct nand_chip *this = mtd->priv;
362 u16 *p = (u16 *) buf;
365 for (i=0; i<len; i++)
366 if (p[i] != readw(this->IO_ADDR_R))
373 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
374 * @mtd: MTD device structure
375 * @ofs: offset from device start
376 * @getchip: 0, if the chip is already selected
378 * Check, if the block is bad.
380 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
382 int page, chipnr, res = 0;
383 struct nand_chip *this = mtd->priv;
387 page = (int)(ofs >> this->page_shift);
388 chipnr = (int)(ofs >> this->chip_shift);
390 /* Grab the lock and see if the device is available */
391 nand_get_chip (this, mtd, FL_READING);
393 /* Select the NAND device */
394 this->select_chip(mtd, chipnr);
398 if (this->options & NAND_BUSWIDTH_16) {
399 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page & this->pagemask);
400 bad = cpu_to_le16(this->read_word(mtd));
401 if (this->badblockpos & 0x1)
403 if ((bad & 0xFF) != 0xff)
406 this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page & this->pagemask);
407 if (this->read_byte(mtd) != 0xff)
412 /* Deselect and wake up anyone waiting on the device */
413 nand_release_chip(mtd);
420 * nand_default_block_markbad - [DEFAULT] mark a block bad
421 * @mtd: MTD device structure
422 * @ofs: offset from device start
424 * This is the default implementation, which can be overridden by
425 * a hardware specific driver.
427 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
429 struct nand_chip *this = mtd->priv;
430 u_char buf[2] = {0, 0};
434 /* Get block number */
435 block = ((int) ofs) >> this->bbt_erase_shift;
436 this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
438 /* Do we have a flash based bad block table ? */
439 if (this->options & NAND_USE_FLASH_BBT)
440 return nand_update_bbt (mtd, ofs);
442 /* We write two bytes, so we dont have to mess with 16 bit access */
443 ofs += mtd->oobsize + (this->badblockpos & ~0x01);
444 return nand_write_oob (mtd, ofs , 2, &retlen, buf);
448 * nand_check_wp - [GENERIC] check if the chip is write protected
449 * @mtd: MTD device structure
450 * Check, if the device is write protected
452 * The function expects, that the device is already selected
454 static int nand_check_wp (struct mtd_info *mtd)
456 struct nand_chip *this = mtd->priv;
457 /* Check the WP bit */
458 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
459 return (this->read_byte(mtd) & 0x80) ? 0 : 1;
463 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
464 * @mtd: MTD device structure
465 * @ofs: offset from device start
466 * @getchip: 0, if the chip is already selected
467 * @allowbbt: 1, if its allowed to access the bbt area
469 * Check, if the block is bad. Either by reading the bad block table or
470 * calling of the scan function.
472 static int nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
474 struct nand_chip *this = mtd->priv;
477 return this->block_bad(mtd, ofs, getchip);
479 /* Return info from the table */
480 return nand_isbad_bbt (mtd, ofs, allowbbt);
484 * nand_command - [DEFAULT] Send command to NAND device
485 * @mtd: MTD device structure
486 * @command: the command to be sent
487 * @column: the column address for this command, -1 if none
488 * @page_addr: the page address for this command, -1 if none
490 * Send command to NAND device. This function is used for small page
491 * devices (256/512 Bytes per page)
493 static void nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
495 register struct nand_chip *this = mtd->priv;
497 /* Begin command latch cycle */
498 this->hwcontrol(mtd, NAND_CTL_SETCLE);
500 * Write out the command to the device.
502 if (command == NAND_CMD_SEQIN) {
505 if (column >= mtd->oobblock) {
507 column -= mtd->oobblock;
508 readcmd = NAND_CMD_READOOB;
509 } else if (column < 256) {
510 /* First 256 bytes --> READ0 */
511 readcmd = NAND_CMD_READ0;
514 readcmd = NAND_CMD_READ1;
516 this->write_byte(mtd, readcmd);
518 this->write_byte(mtd, command);
520 /* Set ALE and clear CLE to start address cycle */
521 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
523 if (column != -1 || page_addr != -1) {
524 this->hwcontrol(mtd, NAND_CTL_SETALE);
526 /* Serially input address */
528 /* Adjust columns for 16 bit buswidth */
529 if (this->options & NAND_BUSWIDTH_16)
531 this->write_byte(mtd, column);
533 if (page_addr != -1) {
534 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
535 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
536 /* One more address cycle for higher density devices */
537 if (this->chipsize & 0x0c000000)
538 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));
540 /* Latch in address */
541 this->hwcontrol(mtd, NAND_CTL_CLRALE);
545 * program and erase have their own busy handlers
546 * status and sequential in needs no delay
550 case NAND_CMD_PAGEPROG:
551 case NAND_CMD_ERASE1:
552 case NAND_CMD_ERASE2:
554 case NAND_CMD_STATUS:
560 udelay(this->chip_delay);
561 this->hwcontrol(mtd, NAND_CTL_SETCLE);
562 this->write_byte(mtd, NAND_CMD_STATUS);
563 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
564 while ( !(this->read_byte(mtd) & 0x40));
567 /* This applies to read commands */
570 * If we don't have access to the busy pin, we apply the given
573 if (!this->dev_ready) {
574 udelay (this->chip_delay);
579 /* Apply this short delay always to ensure that we do wait tWB in
580 * any case on any machine. */
582 /* wait until command is processed */
583 while (!this->dev_ready(mtd));
587 * nand_command_lp - [DEFAULT] Send command to NAND large page device
588 * @mtd: MTD device structure
589 * @command: the command to be sent
590 * @column: the column address for this command, -1 if none
591 * @page_addr: the page address for this command, -1 if none
593 * Send command to NAND device. This is the version for the new large page devices
594 * We dont have the seperate regions as we have in the small page devices.
595 * We must emulate NAND_CMD_READOOB to keep the code compatible.
598 static void nand_command_lp (struct mtd_info *mtd, unsigned command, int column, int page_addr)
600 register struct nand_chip *this = mtd->priv;
602 /* Emulate NAND_CMD_READOOB */
603 if (command == NAND_CMD_READOOB) {
604 column += mtd->oobblock;
605 command = NAND_CMD_READ0;
609 /* Begin command latch cycle */
610 this->hwcontrol(mtd, NAND_CTL_SETCLE);
611 /* Write out the command to the device. */
612 this->write_byte(mtd, command);
613 /* End command latch cycle */
614 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
616 if (column != -1 || page_addr != -1) {
617 this->hwcontrol(mtd, NAND_CTL_SETALE);
619 /* Serially input address */
621 /* Adjust columns for 16 bit buswidth */
622 if (this->options & NAND_BUSWIDTH_16)
624 this->write_byte(mtd, column & 0xff);
625 this->write_byte(mtd, column >> 8);
627 if (page_addr != -1) {
628 this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
629 this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
630 /* One more address cycle for devices > 128MiB */
631 if (this->chipsize > (128 << 20))
632 this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0xff));
634 /* Latch in address */
635 this->hwcontrol(mtd, NAND_CTL_CLRALE);
639 * program and erase have their own busy handlers
640 * status and sequential in needs no delay
644 case NAND_CMD_CACHEDPROG:
645 case NAND_CMD_PAGEPROG:
646 case NAND_CMD_ERASE1:
647 case NAND_CMD_ERASE2:
649 case NAND_CMD_STATUS:
656 udelay(this->chip_delay);
657 this->hwcontrol(mtd, NAND_CTL_SETCLE);
658 this->write_byte(mtd, NAND_CMD_STATUS);
659 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
660 while ( !(this->read_byte(mtd) & 0x40));
664 /* Begin command latch cycle */
665 this->hwcontrol(mtd, NAND_CTL_SETCLE);
666 /* Write out the start read command */
667 this->write_byte(mtd, NAND_CMD_READSTART);
668 /* End command latch cycle */
669 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
670 /* Fall through into ready check */
672 /* This applies to read commands */
675 * If we don't have access to the busy pin, we apply the given
678 if (!this->dev_ready) {
679 udelay (this->chip_delay);
684 /* Apply this short delay always to ensure that we do wait tWB in
685 * any case on any machine. */
687 /* wait until command is processed */
688 while (!this->dev_ready(mtd));
692 * nand_get_chip - [GENERIC] Get chip for selected access
693 * @this: the nand chip descriptor
694 * @mtd: MTD device structure
695 * @new_state: the state which is requested
697 * Get the device and lock it for exclusive access
699 static void nand_get_chip (struct nand_chip *this, struct mtd_info *mtd, int new_state)
702 DECLARE_WAITQUEUE (wait, current);
705 * Grab the lock and see if the device is available
708 spin_lock_bh (&this->chip_lock);
710 if (this->state == FL_READY) {
711 this->state = new_state;
712 spin_unlock_bh (&this->chip_lock);
716 set_current_state (TASK_UNINTERRUPTIBLE);
717 add_wait_queue (&this->wq, &wait);
718 spin_unlock_bh (&this->chip_lock);
720 remove_wait_queue (&this->wq, &wait);
725 * nand_wait - [DEFAULT] wait until the command is done
726 * @mtd: MTD device structure
727 * @this: NAND chip structure
728 * @state: state to select the max. timeout value
730 * Wait for command done. This applies to erase and program only
731 * Erase can take up to 400ms and program up to 20ms according to
732 * general NAND and SmartMedia specs
735 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
738 unsigned long timeo = jiffies;
741 if (state == FL_ERASING)
742 timeo += (HZ * 400) / 1000;
744 timeo += (HZ * 20) / 1000;
746 /* Apply this short delay always to ensure that we do wait tWB in
747 * any case on any machine. */
750 spin_lock_bh (&this->chip_lock);
751 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
752 this->cmdfunc (mtd, NAND_CMD_STATUS_MULTI, -1, -1);
754 this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
756 while (time_before(jiffies, timeo)) {
757 /* Check, if we were interrupted */
758 if (this->state != state) {
759 spin_unlock_bh (&this->chip_lock);
762 if (this->dev_ready) {
763 if (this->dev_ready(mtd))
766 if (this->read_byte(mtd) & NAND_STATUS_READY)
769 spin_unlock_bh (&this->chip_lock);
771 spin_lock_bh (&this->chip_lock);
773 status = (int) this->read_byte(mtd);
774 spin_unlock_bh (&this->chip_lock);
780 * nand_write_page - [GENERIC] write one page
781 * @mtd: MTD device structure
782 * @this: NAND chip structure
783 * @page: startpage inside the chip, must be called with (page & this->pagemask)
784 * @oob_buf: out of band data buffer
785 * @oobsel: out of band selecttion structre
786 * @cached: 1 = enable cached programming if supported by chip
788 * Nand_page_program function is used for write and writev !
789 * This function will always program a full page of data
790 * If you call it with a non page aligned buffer, you're lost :)
792 * Cached programming is not supported yet.
794 static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page,
795 u_char *oob_buf, struct nand_oobinfo *oobsel, int cached)
799 int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
800 int *oob_config = oobsel->eccpos;
801 int datidx = 0, eccidx = 0, eccsteps = this->eccsteps;
804 /* FIXME: Enable cached programming */
807 /* Send command to begin auto page programming */
808 this->cmdfunc (mtd, NAND_CMD_SEQIN, 0x00, page);
810 /* Write out complete page of data, take care of eccmode */
812 /* No ecc, write all */
814 printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
815 this->write_buf(mtd, this->data_poi, mtd->oobblock);
818 /* Software ecc 3/256, write all */
820 for (; eccsteps; eccsteps--) {
821 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
822 for (i = 0; i < 3; i++, eccidx++)
823 oob_buf[oob_config[eccidx]] = ecc_code[i];
824 datidx += this->eccsize;
826 this->write_buf(mtd, this->data_poi, mtd->oobblock);
829 /* Hardware ecc 8 byte / 512 byte data */
830 case NAND_ECC_HW8_512:
832 /* Hardware ecc 6 byte / 512 byte data */
833 case NAND_ECC_HW6_512:
835 /* Hardware ecc 3 byte / 256 data */
836 /* Hardware ecc 3 byte / 512 byte data */
837 case NAND_ECC_HW3_256:
838 case NAND_ECC_HW3_512:
840 for (; eccsteps; eccsteps--) {
841 /* enable hardware ecc logic for write */
842 this->enable_hwecc(mtd, NAND_ECC_WRITE);
843 this->write_buf(mtd, &this->data_poi[datidx], this->eccsize);
844 this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
845 for (i = 0; i < eccbytes; i++, eccidx++)
846 oob_buf[oob_config[eccidx]] = ecc_code[i];
847 /* If the hardware ecc provides syndromes then
848 * the ecc code must be written immidiately after
849 * the data bytes (words) */
850 if (this->options & NAND_HWECC_SYNDROME)
851 this->write_buf(mtd, ecc_code, eccbytes);
853 datidx += this->eccsize;
858 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
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);
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);
872 /* call wait ready function */
873 status = this->waitfunc (mtd, this, FL_WRITING);
874 /* See if device thinks it succeeded */
876 DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
880 /* FIXME: Implement cached programming ! */
881 /* wait until cache is ready*/
882 // status = this->waitfunc (mtd, this, FL_CACHEDRPG);
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;
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);
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);
927 datidx += mtd->eccsize;
928 /* Have we a hw generator layout ? */
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);
935 oobofs += hweccbytes;
938 /* check, if we must compare all data or if we just have to
939 * compare the ecc bytes
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);
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);
964 oobofs += mtd->oobsize - hweccbytes * eccsteps;
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);
978 while (!this->dev_ready(mtd));
980 /* All done, return happy */
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.
994 this->cmdfunc (mtd, NAND_CMD_RESET, -1, -1);
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;
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");
1053 /* Grab the lock and see if the device is available */
1054 nand_get_chip (this, mtd ,FL_READING);
1056 /* use userspace supplied oobinfo, if zero */
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;
1081 case NAND_ECC_HW6_512: /* Hardware ECC 6 byte / 512 byte data */
1084 case NAND_ECC_HW8_512: /* Hardware ECC 8 byte / 512 byte data */
1092 if (this->options & NAND_HWECC_SYNDROME)
1095 oobreadlen = mtd->oobsize;
1096 if (this->options & NAND_HWECC_SYNDROME)
1097 oobreadlen -= oobsel->eccbytes;
1099 /* Loop until all data read */
1100 while (read < len) {
1102 int aligned = (!col && (len - read) >= end);
1104 * If the read is not page aligned, we have to read into data buffer
1105 * due to ecc, else we read into return buffer direct
1108 data_poi = &buf[read];
1110 data_poi = this->data_buf;
1112 /* Check, if we have this page in the buffer
1114 * FIXME: Make it work when we must provide oob data too,
1115 * check the usage of data_buf oob field
1117 if (realpage == this->pagebuf && !oob_buf) {
1118 /* aligned read ? */
1120 memcpy (data_poi, this->data_buf, end);
1124 /* Check, if we must send the read command */
1126 this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
1130 /* get oob area, if we have no oob buffer from fs-driver */
1131 if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE)
1132 oob_data = &this->data_buf[end];
1134 eccsteps = this->eccsteps;
1137 case NAND_ECC_NONE: { /* No ECC, Read in a page */
1138 static unsigned long lastwhinge = 0;
1139 if ((lastwhinge / HZ) != (jiffies / HZ)) {
1140 printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended\n");
1141 lastwhinge = jiffies;
1143 this->read_buf(mtd, data_poi, end);
1147 case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
1148 this->read_buf(mtd, data_poi, end);
1149 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc)
1150 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1153 case NAND_ECC_HW3_256: /* Hardware ECC 3 byte /256 byte data */
1154 case NAND_ECC_HW3_512: /* Hardware ECC 3 byte /512 byte data */
1155 case NAND_ECC_HW6_512: /* Hardware ECC 6 byte / 512 byte data */
1156 case NAND_ECC_HW8_512: /* Hardware ECC 8 byte / 512 byte data */
1157 for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) {
1158 this->enable_hwecc(mtd, NAND_ECC_READ);
1159 this->read_buf(mtd, &data_poi[datidx], ecc);
1161 /* HW ecc with syndrome calculation must read the
1162 * syndrome from flash immidiately after the data */
1164 /* Some hw ecc generators need to know when the
1165 * syndrome is read from flash */
1166 this->enable_hwecc(mtd, NAND_ECC_READSYN);
1167 this->read_buf(mtd, &oob_data[i], eccbytes);
1168 /* We calc error correction directly, it checks the hw
1169 * generator for an error, reads back the syndrome and
1170 * does the error correction on the fly */
1171 if (this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]) == -1) {
1172 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: "
1173 "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
1177 this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
1183 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
1188 this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
1190 /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
1194 /* Pick the ECC bytes out of the oob data */
1195 for (j = 0; j < oobsel->eccbytes; j++)
1196 ecc_code[j] = oob_data[oob_config[j]];
1198 /* correct data, if neccecary */
1199 for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {
1200 ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
1202 /* Get next chunk of ecc bytes */
1205 /* Check, if we have a fs supplied oob-buffer,
1206 * This is the legacy mode. Used by YAFFS1
1207 * Should go away some day
1209 if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) {
1210 int *p = (int *)(&oob_data[mtd->oobsize]);
1214 if (ecc_status == -1) {
1215 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
1221 /* check, if we have a fs supplied oob-buffer */
1223 /* without autoplace. Legacy mode used by YAFFS1 */
1224 switch(oobsel->useecc) {
1225 case MTD_NANDECC_AUTOPLACE:
1226 /* Walk through the autoplace chunks */
1227 for (i = 0, j = 0; j < mtd->oobavail; i++) {
1228 int from = oobsel->oobfree[i][0];
1229 int num = oobsel->oobfree[i][1];
1230 memcpy(&oob_buf[oob], &oob_data[from], num);
1233 oob += mtd->oobavail;
1235 case MTD_NANDECC_PLACE:
1236 /* YAFFS1 legacy mode */
1237 oob_data += this->eccsteps * sizeof (int);
1239 oob_data += mtd->oobsize;
1243 /* Partial page read, transfer data into fs buffer */
1245 for (j = col; j < end && read < len; j++)
1246 buf[read++] = data_poi[j];
1247 this->pagebuf = realpage;
1249 read += mtd->oobblock;
1251 /* Apply delay or wait for ready/busy pin
1252 * Do this before the AUTOINCR check, so no problems
1253 * arise if a chip which does auto increment
1254 * is marked as NOAUTOINCR by the board driver.
1256 if (!this->dev_ready)
1257 udelay (this->chip_delay);
1259 while (!this->dev_ready(mtd));
1264 /* For subsequent reads align to page boundary. */
1266 /* Increment page address */
1269 page = realpage & this->pagemask;
1270 /* Check, if we cross a chip boundary */
1273 this->select_chip(mtd, -1);
1274 this->select_chip(mtd, chipnr);
1276 /* Check, if the chip supports auto page increment
1277 * or if we have hit a block boundary.
1279 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1283 /* Deselect and wake up anyone waiting on the device */
1284 nand_release_chip(mtd);
1287 * Return success, if no ECC failures, else -EIO
1288 * fs driver will take care of that, because
1289 * retlen == desired len and result == -EIO
1292 return ecc_failed ? -EIO : 0;
1296 * nand_read_oob - [MTD Interface] NAND read out-of-band
1297 * @mtd: MTD device structure
1298 * @from: offset to read from
1299 * @len: number of bytes to read
1300 * @retlen: pointer to variable to store the number of read bytes
1301 * @buf: the databuffer to put data
1303 * NAND read out-of-band data from the spare area
1305 static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
1307 int i, col, page, chipnr;
1308 struct nand_chip *this = mtd->priv;
1309 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1311 DEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
1313 /* Shift to get page */
1314 page = (int)(from >> this->page_shift);
1315 chipnr = (int)(from >> this->chip_shift);
1317 /* Mask to get column */
1318 col = from & (mtd->oobsize - 1);
1320 /* Initialize return length value */
1323 /* Do not allow reads past end of device */
1324 if ((from + len) > mtd->size) {
1325 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n");
1330 /* Grab the lock and see if the device is available */
1331 nand_get_chip (this, mtd , FL_READING);
1333 /* Select the NAND device */
1334 this->select_chip(mtd, chipnr);
1336 /* Send the read command */
1337 this->cmdfunc (mtd, NAND_CMD_READOOB, col, page & this->pagemask);
1339 * Read the data, if we read more than one page
1340 * oob data, let the device transfer the data !
1344 int thislen = mtd->oobsize - col;
1345 thislen = min_t(int, thislen, len);
1346 this->read_buf(mtd, &buf[i], thislen);
1349 /* Apply delay or wait for ready/busy pin
1350 * Do this before the AUTOINCR check, so no problems
1351 * arise if a chip which does auto increment
1352 * is marked as NOAUTOINCR by the board driver.
1354 if (!this->dev_ready)
1355 udelay (this->chip_delay);
1357 while (!this->dev_ready(mtd));
1364 /* Check, if we cross a chip boundary */
1365 if (!(page & this->pagemask)) {
1367 this->select_chip(mtd, -1);
1368 this->select_chip(mtd, chipnr);
1371 /* Check, if the chip supports auto page increment
1372 * or if we have hit a block boundary.
1374 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
1375 /* For subsequent page reads set offset to 0 */
1376 this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
1381 /* Deselect and wake up anyone waiting on the device */
1382 nand_release_chip(mtd);
1390 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
1391 * @mtd: MTD device structure
1392 * @buf: temporary buffer
1393 * @from: offset to read from
1394 * @len: number of bytes to read
1395 * @ooblen: number of oob data bytes to read
1397 * Read raw data including oob into buffer
1399 int nand_read_raw (struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
1401 struct nand_chip *this = mtd->priv;
1402 int page = (int) (from >> this->page_shift);
1403 int chip = (int) (from >> this->chip_shift);
1406 int pagesize = mtd->oobblock + mtd->oobsize;
1407 int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
1409 /* Do not allow reads past end of device */
1410 if ((from + len) > mtd->size) {
1411 DEBUG (MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n");
1415 /* Grab the lock and see if the device is available */
1416 nand_get_chip (this, mtd , FL_READING);
1418 this->select_chip (mtd, chip);
1420 /* Add requested oob length */
1425 this->cmdfunc (mtd, NAND_CMD_READ0, 0, page & this->pagemask);
1428 this->read_buf (mtd, &buf[cnt], pagesize);
1434 if (!this->dev_ready)
1435 udelay (this->chip_delay);
1437 while (!this->dev_ready(mtd));
1439 /* Check, if the chip supports auto page increment */
1440 if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
1444 /* Deselect and wake up anyone waiting on the device */
1445 nand_release_chip(mtd);
1451 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
1452 * @mtd: MTD device structure
1453 * @fsbuf: buffer given by fs driver
1454 * @oobsel: out of band selection structre
1455 * @autoplace: 1 = place given buffer into the oob bytes
1456 * @numpages: number of pages to prepare
1459 * 1. Filesystem buffer available and autoplacement is off,
1460 * return filesystem buffer
1461 * 2. No filesystem buffer or autoplace is off, return internal
1463 * 3. Filesystem buffer is given and autoplace selected
1464 * put data from fs buffer into internal buffer and
1465 * retrun internal buffer
1467 * Note: The internal buffer is filled with 0xff. This must
1468 * be done only once, when no autoplacement happens
1469 * Autoplacement sets the buffer dirty flag, which
1470 * forces the 0xff fill before using the buffer again.
1473 static u_char * nand_prepare_oobbuf (struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel,
1474 int autoplace, int numpages)
1476 struct nand_chip *this = mtd->priv;
1479 /* Zero copy fs supplied buffer */
1480 if (fsbuf && !autoplace)
1483 /* Check, if the buffer must be filled with ff again */
1484 if (this->oobdirty) {
1485 memset (this->oob_buf, 0xff,
1486 mtd->oobsize << (this->phys_erase_shift - this->page_shift));
1490 /* If we have no autoplacement or no fs buffer use the internal one */
1491 if (!autoplace || !fsbuf)
1492 return this->oob_buf;
1494 /* Walk through the pages and place the data */
1497 while (numpages--) {
1498 for (i = 0, len = 0; len < mtd->oobavail; i++) {
1499 int to = ofs + oobsel->oobfree[i][0];
1500 int num = oobsel->oobfree[i][1];
1501 memcpy (&this->oob_buf[to], fsbuf, num);
1505 ofs += mtd->oobavail;
1507 return this->oob_buf;
1510 #define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
1513 * nand_write - [MTD Interface] compability function for nand_write_ecc
1514 * @mtd: MTD device structure
1515 * @to: offset to write to
1516 * @len: number of bytes to write
1517 * @retlen: pointer to variable to store the number of written bytes
1518 * @buf: the data to write
1520 * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
1523 static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1525 return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));
1529 * nand_write_ecc - [MTD Interface] NAND write with ECC
1530 * @mtd: MTD device structure
1531 * @to: offset to write to
1532 * @len: number of bytes to write
1533 * @retlen: pointer to variable to store the number of written bytes
1534 * @buf: the data to write
1535 * @eccbuf: filesystem supplied oob data buffer
1536 * @oobsel: oob selection structure
1538 * NAND write with ECC
1540 static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
1541 size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
1543 int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
1544 int autoplace = 0, numpages, totalpages;
1545 struct nand_chip *this = mtd->priv;
1546 u_char *oobbuf, *bufstart;
1547 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1549 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1551 /* Initialize retlen, in case of early exit */
1554 /* Do not allow write past end of device */
1555 if ((to + len) > mtd->size) {
1556 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n");
1560 /* reject writes, which are not page aligned */
1561 if (NOTALIGNED (to) || NOTALIGNED(len)) {
1562 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1566 /* Grab the lock and see if the device is available */
1567 nand_get_chip (this, mtd, FL_WRITING);
1569 /* Calculate chipnr */
1570 chipnr = (int)(to >> this->chip_shift);
1571 /* Select the NAND device */
1572 this->select_chip(mtd, chipnr);
1574 /* Check, if it is write protected */
1575 if (nand_check_wp(mtd))
1578 /* if oobsel is NULL, use chip defaults */
1580 oobsel = &mtd->oobinfo;
1582 /* Autoplace of oob data ? Use the default placement scheme */
1583 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1584 oobsel = this->autooob;
1588 /* Setup variables and oob buffer */
1589 totalpages = len >> this->page_shift;
1590 page = (int) (to >> this->page_shift);
1591 /* Invalidate the page cache, if we write to the cached page */
1592 if (page <= this->pagebuf && this->pagebuf < (page + totalpages))
1595 /* Set it relative to chip */
1596 page &= this->pagemask;
1598 /* Calc number of pages we can write in one go */
1599 numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages);
1600 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages);
1601 bufstart = (u_char *)buf;
1603 /* Loop until all data is written */
1604 while (written < len) {
1606 this->data_poi = (u_char*) &buf[written];
1607 /* Write one page. If this is the last page to write
1608 * or the last page in this block, then use the
1609 * real pageprogram command, else select cached programming
1610 * if supported by the chip.
1612 ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
1614 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret);
1618 oob += mtd->oobsize;
1619 /* Update written bytes count */
1620 written += mtd->oobblock;
1624 /* Increment page address */
1627 /* Have we hit a block boundary ? Then we have to verify and
1628 * if verify is ok, we have to setup the oob buffer for
1631 if (!(page & (ppblock - 1))){
1633 this->data_poi = bufstart;
1634 ret = nand_verify_pages (mtd, this, startpage,
1636 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1638 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1643 ofs = autoplace ? mtd->oobavail : mtd->oobsize;
1645 eccbuf += (page - startpage) * ofs;
1646 totalpages -= page - startpage;
1647 numpages = min (totalpages, ppblock);
1648 page &= this->pagemask;
1650 oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel,
1651 autoplace, numpages);
1652 /* Check, if we cross a chip boundary */
1655 this->select_chip(mtd, -1);
1656 this->select_chip(mtd, chipnr);
1660 /* Verify the remaining pages */
1662 this->data_poi = bufstart;
1663 ret = nand_verify_pages (mtd, this, startpage, totalpages,
1664 oobbuf, oobsel, chipnr, (eccbuf != NULL));
1668 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
1671 /* Deselect and wake up anyone waiting on the device */
1672 nand_release_chip(mtd);
1679 * nand_write_oob - [MTD Interface] NAND write out-of-band
1680 * @mtd: MTD device structure
1681 * @to: offset to write to
1682 * @len: number of bytes to write
1683 * @retlen: pointer to variable to store the number of written bytes
1684 * @buf: the data to write
1686 * NAND write out-of-band
1688 static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
1690 int column, page, status, ret = -EIO, chipnr;
1691 struct nand_chip *this = mtd->priv;
1693 DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
1695 /* Shift to get page */
1696 page = (int) (to >> this->page_shift);
1697 chipnr = (int) (to >> this->chip_shift);
1699 /* Mask to get column */
1700 column = to & (mtd->oobsize - 1);
1702 /* Initialize return length value */
1705 /* Do not allow write past end of page */
1706 if ((column + len) > mtd->oobsize) {
1707 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
1711 /* Grab the lock and see if the device is available */
1712 nand_get_chip (this, mtd, FL_WRITING);
1714 /* Select the NAND device */
1715 this->select_chip(mtd, chipnr);
1717 /* Reset the chip. Some chips (like the Toshiba TC5832DC found
1718 in one of my DiskOnChip 2000 test units) will clear the whole
1719 data page too if we don't do this. I have no clue why, but
1720 I seem to have 'fixed' it in the doc2000 driver in
1721 August 1999. dwmw2. */
1722 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1724 /* Check, if it is write protected */
1725 if (nand_check_wp(mtd))
1728 /* Invalidate the page cache, if we write to the cached page */
1729 if (page == this->pagebuf)
1732 if (NAND_MUST_PAD(this)) {
1733 /* Write out desired data */
1734 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
1735 /* prepad 0xff for partial programming */
1736 this->write_buf(mtd, ffchars, column);
1738 this->write_buf(mtd, buf, len);
1739 /* postpad 0xff for partial programming */
1740 this->write_buf(mtd, ffchars, mtd->oobsize - (len+column));
1742 /* Write out desired data */
1743 this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
1745 this->write_buf(mtd, buf, len);
1747 /* Send command to program the OOB data */
1748 this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1);
1750 status = this->waitfunc (mtd, this, FL_WRITING);
1752 /* See if device thinks it succeeded */
1753 if (status & 0x01) {
1754 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
1761 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1762 /* Send command to read back the data */
1763 this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask);
1765 if (this->verify_buf(mtd, buf, len)) {
1766 DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
1773 /* Deselect and wake up anyone waiting on the device */
1774 nand_release_chip(mtd);
1781 * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
1782 * @mtd: MTD device structure
1783 * @vecs: the iovectors to write
1784 * @count: number of vectors
1785 * @to: offset to write to
1786 * @retlen: pointer to variable to store the number of written bytes
1788 * NAND write with kvec. This just calls the ecc function
1790 static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1791 loff_t to, size_t * retlen)
1793 return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL));
1797 * nand_writev_ecc - [MTD Interface] write with iovec with ecc
1798 * @mtd: MTD device structure
1799 * @vecs: the iovectors to write
1800 * @count: number of vectors
1801 * @to: offset to write to
1802 * @retlen: pointer to variable to store the number of written bytes
1803 * @eccbuf: filesystem supplied oob data buffer
1804 * @oobsel: oob selection structure
1806 * NAND write with iovec with ecc
1808 static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
1809 loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
1811 int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
1812 int oob, numpages, autoplace = 0, startpage;
1813 struct nand_chip *this = mtd->priv;
1814 int ppblock = (1 << (this->phys_erase_shift - this->page_shift));
1815 u_char *oobbuf, *bufstart;
1817 /* Preset written len for early exit */
1820 /* Calculate total length of data */
1822 for (i = 0; i < count; i++)
1823 total_len += (int) vecs[i].iov_len;
1825 DEBUG (MTD_DEBUG_LEVEL3,
1826 "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
1828 /* Do not allow write past end of page */
1829 if ((to + total_len) > mtd->size) {
1830 DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
1834 /* reject writes, which are not page aligned */
1835 if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
1836 printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
1840 /* Grab the lock and see if the device is available */
1841 nand_get_chip (this, mtd, FL_WRITING);
1843 /* Get the current chip-nr */
1844 chipnr = (int) (to >> this->chip_shift);
1845 /* Select the NAND device */
1846 this->select_chip(mtd, chipnr);
1848 /* Check, if it is write protected */
1849 if (nand_check_wp(mtd))
1852 /* if oobsel is NULL, use chip defaults */
1854 oobsel = &mtd->oobinfo;
1856 /* Autoplace of oob data ? Use the default placement scheme */
1857 if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
1858 oobsel = this->autooob;
1862 /* Setup start page */
1863 page = (int) (to >> this->page_shift);
1864 /* Invalidate the page cache, if we write to the cached page */
1865 if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))
1868 startpage = page & this->pagemask;
1870 /* Loop until all kvec' data has been written */
1873 /* If the given tuple is >= pagesize then
1874 * write it out from the iov
1876 if ((vecs->iov_len - len) >= mtd->oobblock) {
1877 /* Calc number of pages we can write
1878 * out of this iov in one go */
1879 numpages = (vecs->iov_len - len) >> this->page_shift;
1880 /* Do not cross block boundaries */
1881 numpages = min (ppblock - (startpage & (ppblock - 1)), numpages);
1882 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1883 bufstart = (u_char *)vecs->iov_base;
1885 this->data_poi = bufstart;
1887 for (i = 1; i <= numpages; i++) {
1888 /* Write one page. If this is the last page to write
1889 * then use the real pageprogram command, else select
1890 * cached programming if supported by the chip.
1892 ret = nand_write_page (mtd, this, page & this->pagemask,
1893 &oobbuf[oob], oobsel, i != numpages);
1896 this->data_poi += mtd->oobblock;
1897 len += mtd->oobblock;
1898 oob += mtd->oobsize;
1901 /* Check, if we have to switch to the next tuple */
1902 if (len >= (int) vecs->iov_len) {
1908 /* We must use the internal buffer, read data out of each
1909 * tuple until we have a full page to write
1912 while (cnt < mtd->oobblock) {
1913 if (vecs->iov_base != NULL && vecs->iov_len)
1914 this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
1915 /* Check, if we have to switch to the next tuple */
1916 if (len >= (int) vecs->iov_len) {
1922 this->pagebuf = page;
1923 this->data_poi = this->data_buf;
1924 bufstart = this->data_poi;
1926 oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
1927 ret = nand_write_page (mtd, this, page & this->pagemask,
1934 this->data_poi = bufstart;
1935 ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
1939 written += mtd->oobblock * numpages;
1944 startpage = page & this->pagemask;
1945 /* Check, if we cross a chip boundary */
1948 this->select_chip(mtd, -1);
1949 this->select_chip(mtd, chipnr);
1954 /* Deselect and wake up anyone waiting on the device */
1955 nand_release_chip(mtd);
1962 * single_erease_cmd - [GENERIC] NAND standard block erase command function
1963 * @mtd: MTD device structure
1964 * @page: the page address of the block which will be erased
1966 * Standard erase command for NAND chips
1968 static void single_erase_cmd (struct mtd_info *mtd, int page)
1970 struct nand_chip *this = mtd->priv;
1971 /* Send commands to erase a block */
1972 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
1973 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
1977 * multi_erease_cmd - [GENERIC] AND specific block erase command function
1978 * @mtd: MTD device structure
1979 * @page: the page address of the block which will be erased
1981 * AND multi block erase command function
1982 * Erase 4 consecutive blocks
1984 static void multi_erase_cmd (struct mtd_info *mtd, int page)
1986 struct nand_chip *this = mtd->priv;
1987 /* Send commands to erase a block */
1988 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
1989 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
1990 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++);
1991 this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page);
1992 this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1);
1996 * nand_erase - [MTD Interface] erase block(s)
1997 * @mtd: MTD device structure
1998 * @instr: erase instruction
2000 * Erase one ore more blocks
2002 static int nand_erase (struct mtd_info *mtd, struct erase_info *instr)
2004 return nand_erase_nand (mtd, instr, 0);
2008 * nand_erase_intern - [NAND Interface] erase block(s)
2009 * @mtd: MTD device structure
2010 * @instr: erase instruction
2011 * @allowbbt: allow erasing the bbt area
2013 * Erase one ore more blocks
2015 int nand_erase_nand (struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
2017 int page, len, status, pages_per_block, ret, chipnr;
2018 struct nand_chip *this = mtd->priv;
2020 DEBUG (MTD_DEBUG_LEVEL3,
2021 "nand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
2023 /* Start address must align on block boundary */
2024 if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
2025 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2029 /* Length must align on block boundary */
2030 if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
2031 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
2035 /* Do not allow erase past end of device */
2036 if ((instr->len + instr->addr) > mtd->size) {
2037 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
2041 instr->fail_addr = 0xffffffff;
2043 /* Grab the lock and see if the device is available */
2044 nand_get_chip (this, mtd, FL_ERASING);
2046 /* Shift to get first page */
2047 page = (int) (instr->addr >> this->page_shift);
2048 chipnr = (int) (instr->addr >> this->chip_shift);
2050 /* Calculate pages in each block */
2051 pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
2053 /* Select the NAND device */
2054 this->select_chip(mtd, chipnr);
2056 /* Check the WP bit */
2057 /* Check, if it is write protected */
2058 if (nand_check_wp(mtd)) {
2059 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
2060 instr->state = MTD_ERASE_FAILED;
2064 /* Loop through the pages */
2067 instr->state = MTD_ERASING;
2070 /* Check if we have a bad block, we do not erase bad blocks ! */
2071 if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) {
2072 printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
2073 instr->state = MTD_ERASE_FAILED;
2077 /* Invalidate the page cache, if we erase the block which contains
2078 the current cached page */
2079 if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
2082 this->erase_cmd (mtd, page & this->pagemask);
2084 status = this->waitfunc (mtd, this, FL_ERASING);
2086 /* See if block erase succeeded */
2087 if (status & 0x01) {
2088 DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
2089 instr->state = MTD_ERASE_FAILED;
2090 instr->fail_addr = (page << this->page_shift);
2094 /* Increment page address and decrement length */
2095 len -= (1 << this->phys_erase_shift);
2096 page += pages_per_block;
2098 /* Check, if we cross a chip boundary */
2099 if (len && !(page & this->pagemask)) {
2101 this->select_chip(mtd, -1);
2102 this->select_chip(mtd, chipnr);
2105 instr->state = MTD_ERASE_DONE;
2109 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2110 /* Do call back function */
2111 if (!ret && instr->callback)
2112 instr->callback (instr);
2114 /* Deselect and wake up anyone waiting on the device */
2115 nand_release_chip(mtd);
2117 /* Return more or less happy */
2122 * nand_sync - [MTD Interface] sync
2123 * @mtd: MTD device structure
2125 * Sync is actually a wait for chip ready function
2127 static void nand_sync (struct mtd_info *mtd)
2129 struct nand_chip *this = mtd->priv;
2130 DECLARE_WAITQUEUE (wait, current);
2132 DEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2135 /* Grab the spinlock */
2136 spin_lock_bh (&this->chip_lock);
2138 /* See what's going on */
2139 switch (this->state) {
2142 this->state = FL_SYNCING;
2143 spin_unlock_bh (&this->chip_lock);
2147 /* Not an idle state */
2148 add_wait_queue (&this->wq, &wait);
2149 spin_unlock_bh (&this->chip_lock);
2152 remove_wait_queue (&this->wq, &wait);
2156 /* Lock the device */
2157 spin_lock_bh (&this->chip_lock);
2159 /* Set the device to be ready again */
2160 if (this->state == FL_SYNCING) {
2161 this->state = FL_READY;
2162 wake_up (&this->wq);
2165 /* Unlock the device */
2166 spin_unlock_bh (&this->chip_lock);
2171 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2172 * @mtd: MTD device structure
2173 * @ofs: offset relative to mtd start
2175 static int nand_block_isbad (struct mtd_info *mtd, loff_t ofs)
2177 /* Check for invalid offset */
2178 if (ofs > mtd->size)
2181 return nand_block_checkbad (mtd, ofs, 1, 0);
2185 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2186 * @mtd: MTD device structure
2187 * @ofs: offset relative to mtd start
2189 static int nand_block_markbad (struct mtd_info *mtd, loff_t ofs)
2191 struct nand_chip *this = mtd->priv;
2194 if ((ret = nand_block_isbad(mtd, ofs))) {
2195 /* If it was bad already, return success and do nothing. */
2201 return this->block_markbad(mtd, ofs);
2205 * nand_scan - [NAND Interface] Scan for the NAND device
2206 * @mtd: MTD device structure
2207 * @maxchips: Number of chips to scan for
2209 * This fills out all the not initialized function pointers
2210 * with the defaults.
2211 * The flash ID is read and the mtd/chip structures are
2212 * filled with the appropriate values. Buffers are allocated if
2213 * they are not provided by the board driver
2216 int nand_scan (struct mtd_info *mtd, int maxchips)
2218 int i, j, nand_maf_id, nand_dev_id, busw;
2219 struct nand_chip *this = mtd->priv;
2221 /* Get buswidth to select the correct functions*/
2222 busw = this->options & NAND_BUSWIDTH_16;
2224 /* check for proper chip_delay setup, set 20us if not */
2225 if (!this->chip_delay)
2226 this->chip_delay = 20;
2228 /* check, if a user supplied command function given */
2229 if (this->cmdfunc == NULL)
2230 this->cmdfunc = nand_command;
2232 /* check, if a user supplied wait function given */
2233 if (this->waitfunc == NULL)
2234 this->waitfunc = nand_wait;
2236 if (!this->select_chip)
2237 this->select_chip = nand_select_chip;
2238 if (!this->write_byte)
2239 this->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2240 if (!this->read_byte)
2241 this->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2242 if (!this->write_word)
2243 this->write_word = nand_write_word;
2244 if (!this->read_word)
2245 this->read_word = nand_read_word;
2246 if (!this->block_bad)
2247 this->block_bad = nand_block_bad;
2248 if (!this->block_markbad)
2249 this->block_markbad = nand_default_block_markbad;
2250 if (!this->write_buf)
2251 this->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2252 if (!this->read_buf)
2253 this->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2254 if (!this->verify_buf)
2255 this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2256 if (!this->scan_bbt)
2257 this->scan_bbt = nand_default_bbt;
2259 /* Select the device */
2260 this->select_chip(mtd, 0);
2262 /* Send the command for reading device ID */
2263 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2265 /* Read manufacturer and device IDs */
2266 nand_maf_id = this->read_byte(mtd);
2267 nand_dev_id = this->read_byte(mtd);
2269 /* Print and store flash device information */
2270 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2272 if (nand_dev_id != nand_flash_ids[i].id)
2275 if (!mtd->name) mtd->name = nand_flash_ids[i].name;
2276 this->chipsize = nand_flash_ids[i].chipsize << 20;
2278 /* New devices have all the information in additional id bytes */
2279 if (!nand_flash_ids[i].pagesize) {
2281 /* The 3rd id byte contains non relevant data ATM */
2282 extid = this->read_byte(mtd);
2283 /* The 4th id byte is the important one */
2284 extid = this->read_byte(mtd);
2286 mtd->oobblock = 1024 << (extid & 0x3);
2289 mtd->oobsize = (8 << (extid & 0x03)) * (mtd->oobblock / 512);
2291 /* Calc blocksize. Blocksize is multiples of 64KiB */
2292 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2294 /* Get buswidth information */
2295 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2298 /* Old devices have this data hardcoded in the
2299 * device id table */
2300 mtd->erasesize = nand_flash_ids[i].erasesize;
2301 mtd->oobblock = nand_flash_ids[i].pagesize;
2302 mtd->oobsize = mtd->oobblock / 32;
2303 busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
2306 /* Check, if buswidth is correct. Hardware drivers should set
2308 if (busw != (this->options & NAND_BUSWIDTH_16)) {
2309 printk (KERN_INFO "NAND device: Manufacturer ID:"
2310 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2311 nand_manuf_ids[i].name , mtd->name);
2312 printk (KERN_WARNING
2313 "NAND bus width %d instead %d bit\n",
2314 (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
2316 this->select_chip(mtd, -1);
2320 /* Calculate the address shift from the page size */
2321 this->page_shift = ffs(mtd->oobblock) - 1;
2322 this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
2323 this->chip_shift = ffs(this->chipsize) - 1;
2325 /* Set the bad block position */
2326 this->badblockpos = mtd->oobblock > 512 ?
2327 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2329 /* Get chip options, preserve non chip based options */
2330 this->options &= ~NAND_CHIPOPTIONS_MSK;
2331 this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
2332 /* Set this as a default. Board drivers can override it, if neccecary */
2333 this->options |= NAND_NO_AUTOINCR;
2334 /* Check if this is a not a samsung device. Do not clear the options
2335 * for chips which are not having an extended id.
2337 if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
2338 this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2340 /* Check for AND chips with 4 page planes */
2341 if (this->options & NAND_4PAGE_ARRAY)
2342 this->erase_cmd = multi_erase_cmd;
2344 this->erase_cmd = single_erase_cmd;
2346 /* Do not replace user supplied command function ! */
2347 if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
2348 this->cmdfunc = nand_command_lp;
2350 /* Try to identify manufacturer */
2351 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
2352 if (nand_manuf_ids[j].id == nand_maf_id)
2355 printk (KERN_INFO "NAND device: Manufacturer ID:"
2356 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
2357 nand_manuf_ids[j].name , nand_flash_ids[i].name);
2361 if (!nand_flash_ids[i].name) {
2362 printk (KERN_WARNING "No NAND device found!!!\n");
2363 this->select_chip(mtd, -1);
2367 for (i=1; i < maxchips; i++) {
2368 this->select_chip(mtd, i);
2370 /* Send the command for reading device ID */
2371 this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
2373 /* Read manufacturer and device IDs */
2374 if (nand_maf_id != this->read_byte(mtd) ||
2375 nand_dev_id != this->read_byte(mtd))
2379 printk(KERN_INFO "%d NAND chips detected\n", i);
2381 /* Allocate buffers, if neccecary */
2382 if (!this->oob_buf) {
2384 len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
2385 this->oob_buf = kmalloc (len, GFP_KERNEL);
2386 if (!this->oob_buf) {
2387 printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
2390 this->options |= NAND_OOBBUF_ALLOC;
2393 if (!this->data_buf) {
2395 len = mtd->oobblock + mtd->oobsize;
2396 this->data_buf = kmalloc (len, GFP_KERNEL);
2397 if (!this->data_buf) {
2398 if (this->options & NAND_OOBBUF_ALLOC)
2399 kfree (this->oob_buf);
2400 printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
2403 this->options |= NAND_DATABUF_ALLOC;
2406 /* Store the number of chips and calc total size for mtd */
2408 mtd->size = i * this->chipsize;
2409 /* Convert chipsize to number of pages per chip -1. */
2410 this->pagemask = (this->chipsize >> this->page_shift) - 1;
2411 /* Preset the internal oob buffer */
2412 memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
2414 /* If no default placement scheme is given, select an
2415 * appropriate one */
2416 if (!this->autooob) {
2417 /* Select the appropriate default oob placement scheme for
2418 * placement agnostic filesystems */
2419 switch (mtd->oobsize) {
2421 this->autooob = &nand_oob_8;
2424 this->autooob = &nand_oob_16;
2427 this->autooob = &nand_oob_64;
2430 printk (KERN_WARNING "No oob scheme defined for oobsize %d\n",
2436 /* The number of bytes available for the filesystem to place fs dependend
2438 if (this->options & NAND_BUSWIDTH_16) {
2439 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 2);
2440 if (this->autooob->eccbytes & 0x01)
2443 mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 1);
2446 * check ECC mode, default to software
2447 * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
2448 * fallback to software ECC
2450 this->eccsize = 256; /* set default eccsize */
2452 switch (this->eccmode) {
2454 case NAND_ECC_HW3_512:
2455 case NAND_ECC_HW6_512:
2456 case NAND_ECC_HW8_512:
2457 if (mtd->oobblock == 256) {
2458 printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
2459 this->eccmode = NAND_ECC_SOFT;
2460 this->calculate_ecc = nand_calculate_ecc;
2461 this->correct_data = nand_correct_data;
2464 this->eccsize = 512; /* set eccsize to 512 and fall through for function check */
2466 case NAND_ECC_HW3_256:
2467 if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
2469 printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
2473 printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
2474 this->eccmode = NAND_ECC_NONE;
2478 this->calculate_ecc = nand_calculate_ecc;
2479 this->correct_data = nand_correct_data;
2483 printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
2487 mtd->eccsize = this->eccsize;
2489 /* Set the number of read / write steps for one page to ensure ECC generation */
2490 switch (this->eccmode) {
2491 case NAND_ECC_HW3_512:
2492 case NAND_ECC_HW6_512:
2493 case NAND_ECC_HW8_512:
2494 this->eccsteps = mtd->oobblock / 512;
2496 case NAND_ECC_HW3_256:
2498 this->eccsteps = mtd->oobblock / 256;
2506 /* Initialize state, waitqueue and spinlock */
2507 this->state = FL_READY;
2508 init_waitqueue_head (&this->wq);
2509 spin_lock_init (&this->chip_lock);
2511 /* De-select the device */
2512 this->select_chip(mtd, -1);
2514 /* Invalidate the pagebuffer reference */
2517 /* Fill in remaining MTD driver data */
2518 mtd->type = MTD_NANDFLASH;
2519 mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
2520 mtd->ecctype = MTD_ECC_SW;
2521 mtd->erase = nand_erase;
2523 mtd->unpoint = NULL;
2524 mtd->read = nand_read;
2525 mtd->write = nand_write;
2526 mtd->read_ecc = nand_read_ecc;
2527 mtd->write_ecc = nand_write_ecc;
2528 mtd->read_oob = nand_read_oob;
2529 mtd->write_oob = nand_write_oob;
2531 mtd->writev = nand_writev;
2532 mtd->writev_ecc = nand_writev_ecc;
2533 mtd->sync = nand_sync;
2536 mtd->suspend = NULL;
2538 mtd->block_isbad = nand_block_isbad;
2539 mtd->block_markbad = nand_block_markbad;
2541 /* and make the autooob the default one */
2542 memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
2544 mtd->owner = THIS_MODULE;
2546 /* Build bad block table */
2547 return this->scan_bbt (mtd);
2551 * nand_release - [NAND Interface] Free resources held by the NAND device
2552 * @mtd: MTD device structure
2554 void nand_release (struct mtd_info *mtd)
2556 struct nand_chip *this = mtd->priv;
2558 #if defined(CONFIG_MTD_PARTITIONS) || defined(CONFIG_MTD_PARTITIONS_MODULE)
2559 /* Unregister partitions */
2560 del_mtd_partitions (mtd);
2562 /* Unregister the device */
2563 del_mtd_device (mtd);
2565 /* Free bad block table memory, if allocated */
2568 /* Buffer allocated by nand_scan ? */
2569 if (this->options & NAND_OOBBUF_ALLOC)
2570 kfree (this->oob_buf);
2571 /* Buffer allocated by nand_scan ? */
2572 if (this->options & NAND_DATABUF_ALLOC)
2573 kfree (this->data_buf);
2576 EXPORT_SYMBOL (nand_scan);
2577 EXPORT_SYMBOL (nand_release);
2579 MODULE_LICENSE ("GPL");
2580 MODULE_AUTHOR ("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2581 MODULE_DESCRIPTION ("Generic NAND flash driver code");