5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
9 * Additional technical information is available on
10 * http://www.linux-mtd.infradead.org/tech/nand.html
12 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
13 * 2002 Thomas Gleixner (tglx@linutronix.de)
15 * 02-08-2004 tglx: support for strange chips, which cannot auto increment
16 * pages on read / read_oob
18 * 03-17-2004 tglx: Check ready before auto increment check. Simon Bayes
19 * pointed this out, as he marked an auto increment capable chip
20 * as NOAUTOINCR in the board driver.
21 * Make reads over block boundaries work too
23 * 04-14-2004 tglx: first working version for 2k page size chips
25 * 05-19-2004 tglx: Basic support for Renesas AG-AND chips
27 * 09-24-2004 tglx: add support for hardware controllers (e.g. ECC) shared
28 * among multiple independend devices. Suggestions and initial patch
29 * from Ben Dooks <ben-mtd@fluff.org>
32 * David Woodhouse for adding multichip support
34 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
35 * rework for 2K page size chips
38 * Enable cached programming for 2k page size chips
39 * Check, if mtd->ecctype should be set to MTD_ECC_HW
40 * if we have HW ecc support.
41 * The AG-AND chips have nice features for speed improvement,
42 * which are not supported yet. Read / program 4 pages in one go.
44 * $Id: nand_base.c,v 1.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.
54 #include <linux/delay.h>
55 #include <linux/errno.h>
56 #include <linux/sched.h>
57 #include <linux/slab.h>
58 #include <linux/types.h>
59 #include <linux/mtd/mtd.h>
60 #include <linux/mtd/nand.h>
61 #include <linux/mtd/nand_ecc.h>
62 #include <linux/mtd/compatmac.h>
63 #include <linux/interrupt.h>
64 #include <linux/bitops.h>
67 #ifdef CONFIG_MTD_PARTITIONS
68 #include <linux/mtd/partitions.h>
75 #if (CONFIG_COMMANDS & CFG_CMD_NAND)
79 #include <linux/mtd/compat.h>
80 #include <linux/mtd/mtd.h>
81 #include <linux/mtd/nand.h>
82 #include <linux/mtd/nand_ecc.h>
85 #include <asm/errno.h>
87 #ifdef CONFIG_JFFS2_NAND
88 #include <jffs2/jffs2.h>
91 /* Define default oob placement schemes for large and small page devices */
92 static struct nand_oobinfo nand_oob_8
= {
93 .useecc
= MTD_NANDECC_AUTOPLACE
,
96 .oobfree
= { {3, 2}, {6, 2} }
99 static struct nand_oobinfo nand_oob_16
= {
100 .useecc
= MTD_NANDECC_AUTOPLACE
,
102 .eccpos
= {0, 1, 2, 3, 6, 7},
103 .oobfree
= { {8, 8} }
106 static struct nand_oobinfo nand_oob_64
= {
107 .useecc
= MTD_NANDECC_AUTOPLACE
,
110 40, 41, 42, 43, 44, 45, 46, 47,
111 48, 49, 50, 51, 52, 53, 54, 55,
112 56, 57, 58, 59, 60, 61, 62, 63},
113 .oobfree
= { {2, 38} }
116 /* This is used for padding purposes in nand_write_oob */
117 static u_char ffchars
[] = {
118 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
119 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
120 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
121 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
122 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
123 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
124 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
125 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
129 * NAND low-level MTD interface functions
131 static void nand_write_buf(struct mtd_info
*mtd
, const u_char
*buf
, int len
);
132 static void nand_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
);
133 static int nand_verify_buf(struct mtd_info
*mtd
, const u_char
*buf
, int len
);
135 static int nand_read (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t * retlen
, u_char
* buf
);
136 static int nand_read_ecc (struct mtd_info
*mtd
, loff_t from
, size_t len
,
137 size_t * retlen
, u_char
* buf
, u_char
* eccbuf
, struct nand_oobinfo
*oobsel
);
138 static int nand_read_oob (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t * retlen
, u_char
* buf
);
139 static int nand_write (struct mtd_info
*mtd
, loff_t to
, size_t len
, size_t * retlen
, const u_char
* buf
);
140 static int nand_write_ecc (struct mtd_info
*mtd
, loff_t to
, size_t len
,
141 size_t * retlen
, const u_char
* buf
, u_char
* eccbuf
, struct nand_oobinfo
*oobsel
);
142 static int nand_write_oob (struct mtd_info
*mtd
, loff_t to
, size_t len
, size_t * retlen
, const u_char
*buf
);
145 static int nand_writev (struct mtd_info
*mtd
, const struct kvec
*vecs
,
146 unsigned long count
, loff_t to
, size_t * retlen
);
147 static int nand_writev_ecc (struct mtd_info
*mtd
, const struct kvec
*vecs
,
148 unsigned long count
, loff_t to
, size_t * retlen
, u_char
*eccbuf
, struct nand_oobinfo
*oobsel
);
150 static int nand_erase (struct mtd_info
*mtd
, struct erase_info
*instr
);
151 static void nand_sync (struct mtd_info
*mtd
);
153 /* Some internal functions */
154 static int nand_write_page (struct mtd_info
*mtd
, struct nand_chip
*this, int page
, u_char
*oob_buf
,
155 struct nand_oobinfo
*oobsel
, int mode
);
156 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
157 static int nand_verify_pages (struct mtd_info
*mtd
, struct nand_chip
*this, int page
, int numpages
,
158 u_char
*oob_buf
, struct nand_oobinfo
*oobsel
, int chipnr
, int oobmode
);
160 #define nand_verify_pages(...) (0)
163 static void nand_get_device (struct nand_chip
*this, struct mtd_info
*mtd
, int new_state
);
166 * nand_release_device - [GENERIC] release chip
167 * @mtd: MTD device structure
169 * Deselect, release chip lock and wake up anyone waiting on the device
173 static void nand_release_device (struct mtd_info
*mtd
)
175 struct nand_chip
*this = mtd
->priv
;
177 /* De-select the NAND device */
178 this->select_chip(mtd
, -1);
179 /* Do we have a hardware controller ? */
180 if (this->controller
) {
181 spin_lock(&this->controller
->lock
);
182 this->controller
->active
= NULL
;
183 spin_unlock(&this->controller
->lock
);
185 /* Release the chip */
186 spin_lock (&this->chip_lock
);
187 this->state
= FL_READY
;
189 spin_unlock (&this->chip_lock
);
192 #define nand_release_device(mtd) do {} while(0)
196 * nand_read_byte - [DEFAULT] read one byte from the chip
197 * @mtd: MTD device structure
199 * Default read function for 8bit buswith
201 static u_char
nand_read_byte(struct mtd_info
*mtd
)
203 struct nand_chip
*this = mtd
->priv
;
204 return readb(this->IO_ADDR_R
);
208 * nand_write_byte - [DEFAULT] write one byte to the chip
209 * @mtd: MTD device structure
210 * @byte: pointer to data byte to write
212 * Default write function for 8it buswith
214 static void nand_write_byte(struct mtd_info
*mtd
, u_char byte
)
216 struct nand_chip
*this = mtd
->priv
;
217 writeb(byte
, this->IO_ADDR_W
);
221 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
222 * @mtd: MTD device structure
224 * Default read function for 16bit buswith with
225 * endianess conversion
227 static u_char
nand_read_byte16(struct mtd_info
*mtd
)
229 struct nand_chip
*this = mtd
->priv
;
230 return (u_char
) cpu_to_le16(readw(this->IO_ADDR_R
));
234 * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
235 * @mtd: MTD device structure
236 * @byte: pointer to data byte to write
238 * Default write function for 16bit buswith with
239 * endianess conversion
241 static void nand_write_byte16(struct mtd_info
*mtd
, u_char byte
)
243 struct nand_chip
*this = mtd
->priv
;
244 writew(le16_to_cpu((u16
) byte
), this->IO_ADDR_W
);
248 * nand_read_word - [DEFAULT] read one word from the chip
249 * @mtd: MTD device structure
251 * Default read function for 16bit buswith without
252 * endianess conversion
254 static u16
nand_read_word(struct mtd_info
*mtd
)
256 struct nand_chip
*this = mtd
->priv
;
257 return readw(this->IO_ADDR_R
);
261 * nand_write_word - [DEFAULT] write one word to the chip
262 * @mtd: MTD device structure
263 * @word: data word to write
265 * Default write function for 16bit buswith without
266 * endianess conversion
268 static void nand_write_word(struct mtd_info
*mtd
, u16 word
)
270 struct nand_chip
*this = mtd
->priv
;
271 writew(word
, this->IO_ADDR_W
);
275 * nand_select_chip - [DEFAULT] control CE line
276 * @mtd: MTD device structure
277 * @chip: chipnumber to select, -1 for deselect
279 * Default select function for 1 chip devices.
281 static void nand_select_chip(struct mtd_info
*mtd
, int chip
)
283 struct nand_chip
*this = mtd
->priv
;
286 this->hwcontrol(mtd
, NAND_CTL_CLRNCE
);
289 this->hwcontrol(mtd
, NAND_CTL_SETNCE
);
298 * nand_write_buf - [DEFAULT] write buffer to chip
299 * @mtd: MTD device structure
301 * @len: number of bytes to write
303 * Default write function for 8bit buswith
305 static void nand_write_buf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
308 struct nand_chip
*this = mtd
->priv
;
310 for (i
=0; i
<len
; i
++)
311 writeb(buf
[i
], this->IO_ADDR_W
);
315 * nand_read_buf - [DEFAULT] read chip data into buffer
316 * @mtd: MTD device structure
317 * @buf: buffer to store date
318 * @len: number of bytes to read
320 * Default read function for 8bit buswith
322 static void nand_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
325 struct nand_chip
*this = mtd
->priv
;
327 for (i
=0; i
<len
; i
++)
328 buf
[i
] = readb(this->IO_ADDR_R
);
332 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
333 * @mtd: MTD device structure
334 * @buf: buffer containing the data to compare
335 * @len: number of bytes to compare
337 * Default verify function for 8bit buswith
339 static int nand_verify_buf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
342 struct nand_chip
*this = mtd
->priv
;
344 for (i
=0; i
<len
; i
++)
345 if (buf
[i
] != readb(this->IO_ADDR_R
))
352 * nand_write_buf16 - [DEFAULT] write buffer to chip
353 * @mtd: MTD device structure
355 * @len: number of bytes to write
357 * Default write function for 16bit buswith
359 static void nand_write_buf16(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
362 struct nand_chip
*this = mtd
->priv
;
363 u16
*p
= (u16
*) buf
;
366 for (i
=0; i
<len
; i
++)
367 writew(p
[i
], this->IO_ADDR_W
);
372 * nand_read_buf16 - [DEFAULT] read chip data into buffer
373 * @mtd: MTD device structure
374 * @buf: buffer to store date
375 * @len: number of bytes to read
377 * Default read function for 16bit buswith
379 static void nand_read_buf16(struct mtd_info
*mtd
, u_char
*buf
, int len
)
382 struct nand_chip
*this = mtd
->priv
;
383 u16
*p
= (u16
*) buf
;
386 for (i
=0; i
<len
; i
++)
387 p
[i
] = readw(this->IO_ADDR_R
);
391 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
392 * @mtd: MTD device structure
393 * @buf: buffer containing the data to compare
394 * @len: number of bytes to compare
396 * Default verify function for 16bit buswith
398 static int nand_verify_buf16(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
401 struct nand_chip
*this = mtd
->priv
;
402 u16
*p
= (u16
*) buf
;
405 for (i
=0; i
<len
; i
++)
406 if (p
[i
] != readw(this->IO_ADDR_R
))
413 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
414 * @mtd: MTD device structure
415 * @ofs: offset from device start
416 * @getchip: 0, if the chip is already selected
418 * Check, if the block is bad.
420 static int nand_block_bad(struct mtd_info
*mtd
, loff_t ofs
, int getchip
)
422 int page
, chipnr
, res
= 0;
423 struct nand_chip
*this = mtd
->priv
;
427 page
= (int)(ofs
>> this->page_shift
);
428 chipnr
= (int)(ofs
>> this->chip_shift
);
430 /* Grab the lock and see if the device is available */
431 nand_get_device (this, mtd
, FL_READING
);
433 /* Select the NAND device */
434 this->select_chip(mtd
, chipnr
);
438 if (this->options
& NAND_BUSWIDTH_16
) {
439 this->cmdfunc (mtd
, NAND_CMD_READOOB
, this->badblockpos
& 0xFE, page
& this->pagemask
);
440 bad
= cpu_to_le16(this->read_word(mtd
));
441 if (this->badblockpos
& 0x1)
443 if ((bad
& 0xFF) != 0xff)
446 this->cmdfunc (mtd
, NAND_CMD_READOOB
, this->badblockpos
, page
& this->pagemask
);
447 if (this->read_byte(mtd
) != 0xff)
452 /* Deselect and wake up anyone waiting on the device */
453 nand_release_device(mtd
);
460 * nand_default_block_markbad - [DEFAULT] mark a block bad
461 * @mtd: MTD device structure
462 * @ofs: offset from device start
464 * This is the default implementation, which can be overridden by
465 * a hardware specific driver.
467 static int nand_default_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
469 struct nand_chip
*this = mtd
->priv
;
470 u_char buf
[2] = {0, 0};
474 /* Get block number */
475 block
= ((int) ofs
) >> this->bbt_erase_shift
;
476 this->bbt
[block
>> 2] |= 0x01 << ((block
& 0x03) << 1);
478 /* Do we have a flash based bad block table ? */
479 if (this->options
& NAND_USE_FLASH_BBT
)
480 return nand_update_bbt (mtd
, ofs
);
482 /* We write two bytes, so we dont have to mess with 16 bit access */
483 ofs
+= mtd
->oobsize
+ (this->badblockpos
& ~0x01);
484 return nand_write_oob (mtd
, ofs
, 2, &retlen
, buf
);
488 * nand_check_wp - [GENERIC] check if the chip is write protected
489 * @mtd: MTD device structure
490 * Check, if the device is write protected
492 * The function expects, that the device is already selected
494 static int nand_check_wp (struct mtd_info
*mtd
)
496 struct nand_chip
*this = mtd
->priv
;
497 /* Check the WP bit */
498 this->cmdfunc (mtd
, NAND_CMD_STATUS
, -1, -1);
499 return (this->read_byte(mtd
) & 0x80) ? 0 : 1;
503 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
504 * @mtd: MTD device structure
505 * @ofs: offset from device start
506 * @getchip: 0, if the chip is already selected
507 * @allowbbt: 1, if its allowed to access the bbt area
509 * Check, if the block is bad. Either by reading the bad block table or
510 * calling of the scan function.
512 static int nand_block_checkbad (struct mtd_info
*mtd
, loff_t ofs
, int getchip
, int allowbbt
)
514 struct nand_chip
*this = mtd
->priv
;
517 return this->block_bad(mtd
, ofs
, getchip
);
519 /* Return info from the table */
520 return nand_isbad_bbt (mtd
, ofs
, allowbbt
);
524 * nand_command - [DEFAULT] Send command to NAND device
525 * @mtd: MTD device structure
526 * @command: the command to be sent
527 * @column: the column address for this command, -1 if none
528 * @page_addr: the page address for this command, -1 if none
530 * Send command to NAND device. This function is used for small page
531 * devices (256/512 Bytes per page)
533 static void nand_command (struct mtd_info
*mtd
, unsigned command
, int column
, int page_addr
)
535 register struct nand_chip
*this = mtd
->priv
;
537 /* Begin command latch cycle */
538 this->hwcontrol(mtd
, NAND_CTL_SETCLE
);
540 * Write out the command to the device.
542 if (command
== NAND_CMD_SEQIN
) {
545 if (column
>= mtd
->oobblock
) {
547 column
-= mtd
->oobblock
;
548 readcmd
= NAND_CMD_READOOB
;
549 } else if (column
< 256) {
550 /* First 256 bytes --> READ0 */
551 readcmd
= NAND_CMD_READ0
;
554 readcmd
= NAND_CMD_READ1
;
556 this->write_byte(mtd
, readcmd
);
558 this->write_byte(mtd
, command
);
560 /* Set ALE and clear CLE to start address cycle */
561 this->hwcontrol(mtd
, NAND_CTL_CLRCLE
);
563 if (column
!= -1 || page_addr
!= -1) {
564 this->hwcontrol(mtd
, NAND_CTL_SETALE
);
566 /* Serially input address */
568 /* Adjust columns for 16 bit buswidth */
569 if (this->options
& NAND_BUSWIDTH_16
)
571 this->write_byte(mtd
, column
);
573 if (page_addr
!= -1) {
574 this->write_byte(mtd
, (unsigned char) (page_addr
& 0xff));
575 this->write_byte(mtd
, (unsigned char) ((page_addr
>> 8) & 0xff));
576 /* One more address cycle for devices > 32MiB */
577 if (this->chipsize
> (32 << 20))
578 this->write_byte(mtd
, (unsigned char) ((page_addr
>> 16) & 0x0f));
580 /* Latch in address */
581 this->hwcontrol(mtd
, NAND_CTL_CLRALE
);
585 * program and erase have their own busy handlers
586 * status and sequential in needs no delay
590 case NAND_CMD_PAGEPROG
:
591 case NAND_CMD_ERASE1
:
592 case NAND_CMD_ERASE2
:
594 case NAND_CMD_STATUS
:
600 udelay(this->chip_delay
);
601 this->hwcontrol(mtd
, NAND_CTL_SETCLE
);
602 this->write_byte(mtd
, NAND_CMD_STATUS
);
603 this->hwcontrol(mtd
, NAND_CTL_CLRCLE
);
604 while ( !(this->read_byte(mtd
) & 0x40));
607 /* This applies to read commands */
610 * If we don't have access to the busy pin, we apply the given
613 if (!this->dev_ready
) {
614 udelay (this->chip_delay
);
619 /* Apply this short delay always to ensure that we do wait tWB in
620 * any case on any machine. */
622 /* wait until command is processed */
623 while (!this->dev_ready(mtd
));
627 * nand_command_lp - [DEFAULT] Send command to NAND large page device
628 * @mtd: MTD device structure
629 * @command: the command to be sent
630 * @column: the column address for this command, -1 if none
631 * @page_addr: the page address for this command, -1 if none
633 * Send command to NAND device. This is the version for the new large page devices
634 * We dont have the seperate regions as we have in the small page devices.
635 * We must emulate NAND_CMD_READOOB to keep the code compatible.
638 static void nand_command_lp (struct mtd_info
*mtd
, unsigned command
, int column
, int page_addr
)
640 register struct nand_chip
*this = mtd
->priv
;
642 /* Emulate NAND_CMD_READOOB */
643 if (command
== NAND_CMD_READOOB
) {
644 column
+= mtd
->oobblock
;
645 command
= NAND_CMD_READ0
;
649 /* Begin command latch cycle */
650 this->hwcontrol(mtd
, NAND_CTL_SETCLE
);
651 /* Write out the command to the device. */
652 this->write_byte(mtd
, command
);
653 /* End command latch cycle */
654 this->hwcontrol(mtd
, NAND_CTL_CLRCLE
);
656 if (column
!= -1 || page_addr
!= -1) {
657 this->hwcontrol(mtd
, NAND_CTL_SETALE
);
659 /* Serially input address */
661 /* Adjust columns for 16 bit buswidth */
662 if (this->options
& NAND_BUSWIDTH_16
)
664 this->write_byte(mtd
, column
& 0xff);
665 this->write_byte(mtd
, column
>> 8);
667 if (page_addr
!= -1) {
668 this->write_byte(mtd
, (unsigned char) (page_addr
& 0xff));
669 this->write_byte(mtd
, (unsigned char) ((page_addr
>> 8) & 0xff));
670 /* One more address cycle for devices > 128MiB */
671 if (this->chipsize
> (128 << 20))
672 this->write_byte(mtd
, (unsigned char) ((page_addr
>> 16) & 0xff));
674 /* Latch in address */
675 this->hwcontrol(mtd
, NAND_CTL_CLRALE
);
679 * program and erase have their own busy handlers
680 * status and sequential in needs no delay
684 case NAND_CMD_CACHEDPROG
:
685 case NAND_CMD_PAGEPROG
:
686 case NAND_CMD_ERASE1
:
687 case NAND_CMD_ERASE2
:
689 case NAND_CMD_STATUS
:
696 udelay(this->chip_delay
);
697 this->hwcontrol(mtd
, NAND_CTL_SETCLE
);
698 this->write_byte(mtd
, NAND_CMD_STATUS
);
699 this->hwcontrol(mtd
, NAND_CTL_CLRCLE
);
700 while ( !(this->read_byte(mtd
) & 0x40));
704 /* Begin command latch cycle */
705 this->hwcontrol(mtd
, NAND_CTL_SETCLE
);
706 /* Write out the start read command */
707 this->write_byte(mtd
, NAND_CMD_READSTART
);
708 /* End command latch cycle */
709 this->hwcontrol(mtd
, NAND_CTL_CLRCLE
);
710 /* Fall through into ready check */
712 /* This applies to read commands */
715 * If we don't have access to the busy pin, we apply the given
718 if (!this->dev_ready
) {
719 udelay (this->chip_delay
);
724 /* Apply this short delay always to ensure that we do wait tWB in
725 * any case on any machine. */
727 /* wait until command is processed */
728 while (!this->dev_ready(mtd
));
732 * nand_get_device - [GENERIC] Get chip for selected access
733 * @this: the nand chip descriptor
734 * @mtd: MTD device structure
735 * @new_state: the state which is requested
737 * Get the device and lock it for exclusive access
741 static void nand_get_device (struct nand_chip
*this, struct mtd_info
*mtd
, int new_state
)
743 struct nand_chip
*active
= this;
745 DECLARE_WAITQUEUE (wait
, current
);
748 * Grab the lock and see if the device is available
751 /* Hardware controller shared among independend devices */
752 if (this->controller
) {
753 spin_lock (&this->controller
->lock
);
754 if (this->controller
->active
)
755 active
= this->controller
->active
;
757 this->controller
->active
= this;
758 spin_unlock (&this->controller
->lock
);
761 if (active
== this) {
762 spin_lock (&this->chip_lock
);
763 if (this->state
== FL_READY
) {
764 this->state
= new_state
;
765 spin_unlock (&this->chip_lock
);
769 set_current_state (TASK_UNINTERRUPTIBLE
);
770 add_wait_queue (&active
->wq
, &wait
);
771 spin_unlock (&active
->chip_lock
);
773 remove_wait_queue (&active
->wq
, &wait
);
777 static void nand_get_device (struct nand_chip
*this, struct mtd_info
*mtd
, int new_state
) {}
781 * nand_wait - [DEFAULT] wait until the command is done
782 * @mtd: MTD device structure
783 * @this: NAND chip structure
784 * @state: state to select the max. timeout value
786 * Wait for command done. This applies to erase and program only
787 * Erase can take up to 400ms and program up to 20ms according to
788 * general NAND and SmartMedia specs
793 static int nand_wait(struct mtd_info
*mtd
, struct nand_chip
*this, int state
)
795 unsigned long timeo
= jiffies
;
798 if (state
== FL_ERASING
)
799 timeo
+= (HZ
* 400) / 1000;
801 timeo
+= (HZ
* 20) / 1000;
803 /* Apply this short delay always to ensure that we do wait tWB in
804 * any case on any machine. */
807 if ((state
== FL_ERASING
) && (this->options
& NAND_IS_AND
))
808 this->cmdfunc (mtd
, NAND_CMD_STATUS_MULTI
, -1, -1);
810 this->cmdfunc (mtd
, NAND_CMD_STATUS
, -1, -1);
812 while (time_before(jiffies
, timeo
)) {
813 /* Check, if we were interrupted */
814 if (this->state
!= state
)
817 if (this->dev_ready
) {
818 if (this->dev_ready(mtd
))
821 if (this->read_byte(mtd
) & NAND_STATUS_READY
)
826 status
= (int) this->read_byte(mtd
);
832 static int nand_wait(struct mtd_info
*mtd
, struct nand_chip
*this, int state
)
840 * nand_write_page - [GENERIC] write one page
841 * @mtd: MTD device structure
842 * @this: NAND chip structure
843 * @page: startpage inside the chip, must be called with (page & this->pagemask)
844 * @oob_buf: out of band data buffer
845 * @oobsel: out of band selecttion structre
846 * @cached: 1 = enable cached programming if supported by chip
848 * Nand_page_program function is used for write and writev !
849 * This function will always program a full page of data
850 * If you call it with a non page aligned buffer, you're lost :)
852 * Cached programming is not supported yet.
854 static int nand_write_page (struct mtd_info
*mtd
, struct nand_chip
*this, int page
,
855 u_char
*oob_buf
, struct nand_oobinfo
*oobsel
, int cached
)
859 int eccmode
= oobsel
->useecc
? this->eccmode
: NAND_ECC_NONE
;
860 int *oob_config
= oobsel
->eccpos
;
861 int datidx
= 0, eccidx
= 0, eccsteps
= this->eccsteps
;
864 /* FIXME: Enable cached programming */
867 /* Send command to begin auto page programming */
868 this->cmdfunc (mtd
, NAND_CMD_SEQIN
, 0x00, page
);
870 /* Write out complete page of data, take care of eccmode */
872 /* No ecc, write all */
874 printk (KERN_WARNING
"Writing data without ECC to NAND-FLASH is not recommended\n");
875 this->write_buf(mtd
, this->data_poi
, mtd
->oobblock
);
878 /* Software ecc 3/256, write all */
880 for (; eccsteps
; eccsteps
--) {
881 this->calculate_ecc(mtd
, &this->data_poi
[datidx
], ecc_code
);
882 for (i
= 0; i
< 3; i
++, eccidx
++)
883 oob_buf
[oob_config
[eccidx
]] = ecc_code
[i
];
884 datidx
+= this->eccsize
;
886 this->write_buf(mtd
, this->data_poi
, mtd
->oobblock
);
889 eccbytes
= this->eccbytes
;
890 for (; eccsteps
; eccsteps
--) {
891 /* enable hardware ecc logic for write */
892 this->enable_hwecc(mtd
, NAND_ECC_WRITE
);
893 this->write_buf(mtd
, &this->data_poi
[datidx
], this->eccsize
);
894 this->calculate_ecc(mtd
, &this->data_poi
[datidx
], ecc_code
);
895 for (i
= 0; i
< eccbytes
; i
++, eccidx
++)
896 oob_buf
[oob_config
[eccidx
]] = ecc_code
[i
];
897 /* If the hardware ecc provides syndromes then
898 * the ecc code must be written immidiately after
899 * the data bytes (words) */
900 if (this->options
& NAND_HWECC_SYNDROME
)
901 this->write_buf(mtd
, ecc_code
, eccbytes
);
902 datidx
+= this->eccsize
;
907 /* Write out OOB data */
908 if (this->options
& NAND_HWECC_SYNDROME
)
909 this->write_buf(mtd
, &oob_buf
[oobsel
->eccbytes
], mtd
->oobsize
- oobsel
->eccbytes
);
911 this->write_buf(mtd
, oob_buf
, mtd
->oobsize
);
913 /* Send command to actually program the data */
914 this->cmdfunc (mtd
, cached
? NAND_CMD_CACHEDPROG
: NAND_CMD_PAGEPROG
, -1, -1);
917 /* call wait ready function */
918 status
= this->waitfunc (mtd
, this, FL_WRITING
);
919 /* See if device thinks it succeeded */
921 DEBUG (MTD_DEBUG_LEVEL0
, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__
, page
);
925 /* FIXME: Implement cached programming ! */
926 /* wait until cache is ready*/
927 /* status = this->waitfunc (mtd, this, FL_CACHEDRPG); */
932 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
934 * nand_verify_pages - [GENERIC] verify the chip contents after a write
935 * @mtd: MTD device structure
936 * @this: NAND chip structure
937 * @page: startpage inside the chip, must be called with (page & this->pagemask)
938 * @numpages: number of pages to verify
939 * @oob_buf: out of band data buffer
940 * @oobsel: out of band selecttion structre
941 * @chipnr: number of the current chip
942 * @oobmode: 1 = full buffer verify, 0 = ecc only
944 * The NAND device assumes that it is always writing to a cleanly erased page.
945 * Hence, it performs its internal write verification only on bits that
946 * transitioned from 1 to 0. The device does NOT verify the whole page on a
947 * byte by byte basis. It is possible that the page was not completely erased
948 * or the page is becoming unusable due to wear. The read with ECC would catch
949 * the error later when the ECC page check fails, but we would rather catch
950 * it early in the page write stage. Better to write no data than invalid data.
952 static int nand_verify_pages (struct mtd_info
*mtd
, struct nand_chip
*this, int page
, int numpages
,
953 u_char
*oob_buf
, struct nand_oobinfo
*oobsel
, int chipnr
, int oobmode
)
955 int i
, j
, datidx
= 0, oobofs
= 0, res
= -EIO
;
956 int eccsteps
= this->eccsteps
;
960 hweccbytes
= (this->options
& NAND_HWECC_SYNDROME
) ? (oobsel
->eccbytes
/ eccsteps
) : 0;
962 /* Send command to read back the first page */
963 this->cmdfunc (mtd
, NAND_CMD_READ0
, 0, page
);
966 for (j
= 0; j
< eccsteps
; j
++) {
967 /* Loop through and verify the data */
968 if (this->verify_buf(mtd
, &this->data_poi
[datidx
], mtd
->eccsize
)) {
969 DEBUG (MTD_DEBUG_LEVEL0
, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__
, page
);
972 datidx
+= mtd
->eccsize
;
973 /* Have we a hw generator layout ? */
976 if (this->verify_buf(mtd
, &this->oob_buf
[oobofs
], hweccbytes
)) {
977 DEBUG (MTD_DEBUG_LEVEL0
, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__
, page
);
980 oobofs
+= hweccbytes
;
983 /* check, if we must compare all data or if we just have to
984 * compare the ecc bytes
987 if (this->verify_buf(mtd
, &oob_buf
[oobofs
], mtd
->oobsize
- hweccbytes
* eccsteps
)) {
988 DEBUG (MTD_DEBUG_LEVEL0
, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__
, page
);
992 /* Read always, else autoincrement fails */
993 this->read_buf(mtd
, oobdata
, mtd
->oobsize
- hweccbytes
* eccsteps
);
995 if (oobsel
->useecc
!= MTD_NANDECC_OFF
&& !hweccbytes
) {
996 int ecccnt
= oobsel
->eccbytes
;
998 for (i
= 0; i
< ecccnt
; i
++) {
999 int idx
= oobsel
->eccpos
[i
];
1000 if (oobdata
[idx
] != oob_buf
[oobofs
+ idx
] ) {
1001 DEBUG (MTD_DEBUG_LEVEL0
,
1002 "%s: Failed ECC write "
1003 "verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__
, page
, i
);
1009 oobofs
+= mtd
->oobsize
- hweccbytes
* eccsteps
;
1013 /* Apply delay or wait for ready/busy pin
1014 * Do this before the AUTOINCR check, so no problems
1015 * arise if a chip which does auto increment
1016 * is marked as NOAUTOINCR by the board driver.
1017 * Do this also before returning, so the chip is
1018 * ready for the next command.
1020 if (!this->dev_ready
)
1021 udelay (this->chip_delay
);
1023 while (!this->dev_ready(mtd
));
1025 /* All done, return happy */
1030 /* Check, if the chip supports auto page increment */
1031 if (!NAND_CANAUTOINCR(this))
1032 this->cmdfunc (mtd
, NAND_CMD_READ0
, 0x00, page
);
1035 * Terminate the read command. We come here in case of an error
1036 * So we must issue a reset command.
1039 this->cmdfunc (mtd
, NAND_CMD_RESET
, -1, -1);
1045 * nand_read - [MTD Interface] MTD compability function for nand_read_ecc
1046 * @mtd: MTD device structure
1047 * @from: offset to read from
1048 * @len: number of bytes to read
1049 * @retlen: pointer to variable to store the number of read bytes
1050 * @buf: the databuffer to put data
1052 * This function simply calls nand_read_ecc with oob buffer and oobsel = NULL
1054 static int nand_read (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t * retlen
, u_char
* buf
)
1056 return nand_read_ecc (mtd
, from
, len
, retlen
, buf
, NULL
, NULL
);
1061 * nand_read_ecc - [MTD Interface] Read data with ECC
1062 * @mtd: MTD device structure
1063 * @from: offset to read from
1064 * @len: number of bytes to read
1065 * @retlen: pointer to variable to store the number of read bytes
1066 * @buf: the databuffer to put data
1067 * @oob_buf: filesystem supplied oob data buffer
1068 * @oobsel: oob selection structure
1070 * NAND read with ECC
1072 static int nand_read_ecc (struct mtd_info
*mtd
, loff_t from
, size_t len
,
1073 size_t * retlen
, u_char
* buf
, u_char
* oob_buf
, struct nand_oobinfo
*oobsel
)
1075 int i
, j
, col
, realpage
, page
, end
, ecc
, chipnr
, sndcmd
= 1;
1076 int read
= 0, oob
= 0, ecc_status
= 0, ecc_failed
= 0;
1077 struct nand_chip
*this = mtd
->priv
;
1078 u_char
*data_poi
, *oob_data
= oob_buf
;
1079 u_char ecc_calc
[32];
1080 u_char ecc_code
[32];
1081 int eccmode
, eccsteps
;
1082 int *oob_config
, datidx
;
1083 int blockcheck
= (1 << (this->phys_erase_shift
- this->page_shift
)) - 1;
1089 DEBUG (MTD_DEBUG_LEVEL3
, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from
, (int) len
);
1091 /* Do not allow reads past end of device */
1092 if ((from
+ len
) > mtd
->size
) {
1093 DEBUG (MTD_DEBUG_LEVEL0
, "nand_read_ecc: Attempt read beyond end of device\n");
1098 /* Grab the lock and see if the device is available */
1099 nand_get_device (this, mtd
,FL_READING
);
1101 /* use userspace supplied oobinfo, if zero */
1103 oobsel
= &mtd
->oobinfo
;
1105 /* Autoplace of oob data ? Use the default placement scheme */
1106 if (oobsel
->useecc
== MTD_NANDECC_AUTOPLACE
)
1107 oobsel
= this->autooob
;
1109 eccmode
= oobsel
->useecc
? this->eccmode
: NAND_ECC_NONE
;
1110 oob_config
= oobsel
->eccpos
;
1112 /* Select the NAND device */
1113 chipnr
= (int)(from
>> this->chip_shift
);
1114 this->select_chip(mtd
, chipnr
);
1116 /* First we calculate the starting page */
1117 realpage
= (int) (from
>> this->page_shift
);
1118 page
= realpage
& this->pagemask
;
1120 /* Get raw starting column */
1121 col
= from
& (mtd
->oobblock
- 1);
1123 end
= mtd
->oobblock
;
1124 ecc
= this->eccsize
;
1125 eccbytes
= this->eccbytes
;
1127 if ((eccmode
== NAND_ECC_NONE
) || (this->options
& NAND_HWECC_SYNDROME
))
1130 oobreadlen
= mtd
->oobsize
;
1131 if (this->options
& NAND_HWECC_SYNDROME
)
1132 oobreadlen
-= oobsel
->eccbytes
;
1134 /* Loop until all data read */
1135 while (read
< len
) {
1137 int aligned
= (!col
&& (len
- read
) >= end
);
1139 * If the read is not page aligned, we have to read into data buffer
1140 * due to ecc, else we read into return buffer direct
1143 data_poi
= &buf
[read
];
1145 data_poi
= this->data_buf
;
1147 /* Check, if we have this page in the buffer
1149 * FIXME: Make it work when we must provide oob data too,
1150 * check the usage of data_buf oob field
1152 if (realpage
== this->pagebuf
&& !oob_buf
) {
1153 /* aligned read ? */
1155 memcpy (data_poi
, this->data_buf
, end
);
1159 /* Check, if we must send the read command */
1161 this->cmdfunc (mtd
, NAND_CMD_READ0
, 0x00, page
);
1165 /* get oob area, if we have no oob buffer from fs-driver */
1166 if (!oob_buf
|| oobsel
->useecc
== MTD_NANDECC_AUTOPLACE
||
1167 oobsel
->useecc
== MTD_NANDECC_AUTOPL_USR
)
1168 oob_data
= &this->data_buf
[end
];
1170 eccsteps
= this->eccsteps
;
1173 case NAND_ECC_NONE
: { /* No ECC, Read in a page */
1174 /* XXX U-BOOT XXX */
1176 static unsigned long lastwhinge
= 0;
1177 if ((lastwhinge
/ HZ
) != (jiffies
/ HZ
)) {
1178 printk (KERN_WARNING
"Reading data from NAND FLASH without ECC is not recommended\n");
1179 lastwhinge
= jiffies
;
1182 puts("Reading data from NAND FLASH without ECC is not recommended\n");
1184 this->read_buf(mtd
, data_poi
, end
);
1188 case NAND_ECC_SOFT
: /* Software ECC 3/256: Read in a page + oob data */
1189 this->read_buf(mtd
, data_poi
, end
);
1190 for (i
= 0, datidx
= 0; eccsteps
; eccsteps
--, i
+=3, datidx
+= ecc
)
1191 this->calculate_ecc(mtd
, &data_poi
[datidx
], &ecc_calc
[i
]);
1195 for (i
= 0, datidx
= 0; eccsteps
; eccsteps
--, i
+=eccbytes
, datidx
+= ecc
) {
1196 this->enable_hwecc(mtd
, NAND_ECC_READ
);
1197 this->read_buf(mtd
, &data_poi
[datidx
], ecc
);
1199 /* HW ecc with syndrome calculation must read the
1200 * syndrome from flash immidiately after the data */
1202 /* Some hw ecc generators need to know when the
1203 * syndrome is read from flash */
1204 this->enable_hwecc(mtd
, NAND_ECC_READSYN
);
1205 this->read_buf(mtd
, &oob_data
[i
], eccbytes
);
1206 /* We calc error correction directly, it checks the hw
1207 * generator for an error, reads back the syndrome and
1208 * does the error correction on the fly */
1209 if (this->correct_data(mtd
, &data_poi
[datidx
], &oob_data
[i
], &ecc_code
[i
]) == -1) {
1210 DEBUG (MTD_DEBUG_LEVEL0
, "nand_read_ecc: "
1211 "Failed ECC read, page 0x%08x on chip %d\n", page
, chipnr
);
1215 this->calculate_ecc(mtd
, &data_poi
[datidx
], &ecc_calc
[i
]);
1222 this->read_buf(mtd
, &oob_data
[mtd
->oobsize
- oobreadlen
], oobreadlen
);
1224 /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
1228 /* Pick the ECC bytes out of the oob data */
1229 for (j
= 0; j
< oobsel
->eccbytes
; j
++)
1230 ecc_code
[j
] = oob_data
[oob_config
[j
]];
1232 /* correct data, if neccecary */
1233 for (i
= 0, j
= 0, datidx
= 0; i
< this->eccsteps
; i
++, datidx
+= ecc
) {
1234 ecc_status
= this->correct_data(mtd
, &data_poi
[datidx
], &ecc_code
[j
], &ecc_calc
[j
]);
1236 /* Get next chunk of ecc bytes */
1239 /* Check, if we have a fs supplied oob-buffer,
1240 * This is the legacy mode. Used by YAFFS1
1241 * Should go away some day
1243 if (oob_buf
&& oobsel
->useecc
== MTD_NANDECC_PLACE
) {
1244 int *p
= (int *)(&oob_data
[mtd
->oobsize
]);
1248 if (ecc_status
== -1) {
1249 DEBUG (MTD_DEBUG_LEVEL0
, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page
);
1255 /* check, if we have a fs supplied oob-buffer */
1257 /* without autoplace. Legacy mode used by YAFFS1 */
1258 switch(oobsel
->useecc
) {
1259 case MTD_NANDECC_AUTOPLACE
:
1260 case MTD_NANDECC_AUTOPL_USR
:
1261 /* Walk through the autoplace chunks */
1262 for (i
= 0, j
= 0; j
< mtd
->oobavail
; i
++) {
1263 int from
= oobsel
->oobfree
[i
][0];
1264 int num
= oobsel
->oobfree
[i
][1];
1265 memcpy(&oob_buf
[oob
], &oob_data
[from
], num
);
1268 oob
+= mtd
->oobavail
;
1270 case MTD_NANDECC_PLACE
:
1271 /* YAFFS1 legacy mode */
1272 oob_data
+= this->eccsteps
* sizeof (int);
1274 oob_data
+= mtd
->oobsize
;
1278 /* Partial page read, transfer data into fs buffer */
1280 for (j
= col
; j
< end
&& read
< len
; j
++)
1281 buf
[read
++] = data_poi
[j
];
1282 this->pagebuf
= realpage
;
1284 read
+= mtd
->oobblock
;
1286 /* Apply delay or wait for ready/busy pin
1287 * Do this before the AUTOINCR check, so no problems
1288 * arise if a chip which does auto increment
1289 * is marked as NOAUTOINCR by the board driver.
1291 if (!this->dev_ready
)
1292 udelay (this->chip_delay
);
1294 while (!this->dev_ready(mtd
));
1299 /* For subsequent reads align to page boundary. */
1301 /* Increment page address */
1304 page
= realpage
& this->pagemask
;
1305 /* Check, if we cross a chip boundary */
1308 this->select_chip(mtd
, -1);
1309 this->select_chip(mtd
, chipnr
);
1311 /* Check, if the chip supports auto page increment
1312 * or if we have hit a block boundary.
1314 if (!NAND_CANAUTOINCR(this) || !(page
& blockcheck
))
1318 /* Deselect and wake up anyone waiting on the device */
1319 nand_release_device(mtd
);
1322 * Return success, if no ECC failures, else -EBADMSG
1323 * fs driver will take care of that, because
1324 * retlen == desired len and result == -EBADMSG
1327 return ecc_failed
? -EBADMSG
: 0;
1331 * nand_read_oob - [MTD Interface] NAND read out-of-band
1332 * @mtd: MTD device structure
1333 * @from: offset to read from
1334 * @len: number of bytes to read
1335 * @retlen: pointer to variable to store the number of read bytes
1336 * @buf: the databuffer to put data
1338 * NAND read out-of-band data from the spare area
1340 static int nand_read_oob (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t * retlen
, u_char
* buf
)
1342 int i
, col
, page
, chipnr
;
1343 struct nand_chip
*this = mtd
->priv
;
1344 int blockcheck
= (1 << (this->phys_erase_shift
- this->page_shift
)) - 1;
1346 DEBUG (MTD_DEBUG_LEVEL3
, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from
, (int) len
);
1348 /* Shift to get page */
1349 page
= (int)(from
>> this->page_shift
);
1350 chipnr
= (int)(from
>> this->chip_shift
);
1352 /* Mask to get column */
1353 col
= from
& (mtd
->oobsize
- 1);
1355 /* Initialize return length value */
1358 /* Do not allow reads past end of device */
1359 if ((from
+ len
) > mtd
->size
) {
1360 DEBUG (MTD_DEBUG_LEVEL0
, "nand_read_oob: Attempt read beyond end of device\n");
1365 /* Grab the lock and see if the device is available */
1366 nand_get_device (this, mtd
, FL_READING
);
1368 /* Select the NAND device */
1369 this->select_chip(mtd
, chipnr
);
1371 /* Send the read command */
1372 this->cmdfunc (mtd
, NAND_CMD_READOOB
, col
, page
& this->pagemask
);
1374 * Read the data, if we read more than one page
1375 * oob data, let the device transfer the data !
1379 int thislen
= mtd
->oobsize
- col
;
1380 thislen
= min_t(int, thislen
, len
);
1381 this->read_buf(mtd
, &buf
[i
], thislen
);
1384 /* Apply delay or wait for ready/busy pin
1385 * Do this before the AUTOINCR check, so no problems
1386 * arise if a chip which does auto increment
1387 * is marked as NOAUTOINCR by the board driver.
1389 if (!this->dev_ready
)
1390 udelay (this->chip_delay
);
1392 while (!this->dev_ready(mtd
));
1399 /* Check, if we cross a chip boundary */
1400 if (!(page
& this->pagemask
)) {
1402 this->select_chip(mtd
, -1);
1403 this->select_chip(mtd
, chipnr
);
1406 /* Check, if the chip supports auto page increment
1407 * or if we have hit a block boundary.
1409 if (!NAND_CANAUTOINCR(this) || !(page
& blockcheck
)) {
1410 /* For subsequent page reads set offset to 0 */
1411 this->cmdfunc (mtd
, NAND_CMD_READOOB
, 0x0, page
& this->pagemask
);
1416 /* Deselect and wake up anyone waiting on the device */
1417 nand_release_device(mtd
);
1425 * nand_read_raw - [GENERIC] Read raw data including oob into buffer
1426 * @mtd: MTD device structure
1427 * @buf: temporary buffer
1428 * @from: offset to read from
1429 * @len: number of bytes to read
1430 * @ooblen: number of oob data bytes to read
1432 * Read raw data including oob into buffer
1434 int nand_read_raw (struct mtd_info
*mtd
, uint8_t *buf
, loff_t from
, size_t len
, size_t ooblen
)
1436 struct nand_chip
*this = mtd
->priv
;
1437 int page
= (int) (from
>> this->page_shift
);
1438 int chip
= (int) (from
>> this->chip_shift
);
1441 int pagesize
= mtd
->oobblock
+ mtd
->oobsize
;
1442 int blockcheck
= (1 << (this->phys_erase_shift
- this->page_shift
)) - 1;
1444 /* Do not allow reads past end of device */
1445 if ((from
+ len
) > mtd
->size
) {
1446 DEBUG (MTD_DEBUG_LEVEL0
, "nand_read_raw: Attempt read beyond end of device\n");
1450 /* Grab the lock and see if the device is available */
1451 nand_get_device (this, mtd
, FL_READING
);
1453 this->select_chip (mtd
, chip
);
1455 /* Add requested oob length */
1460 this->cmdfunc (mtd
, NAND_CMD_READ0
, 0, page
& this->pagemask
);
1463 this->read_buf (mtd
, &buf
[cnt
], pagesize
);
1469 if (!this->dev_ready
)
1470 udelay (this->chip_delay
);
1472 while (!this->dev_ready(mtd
));
1474 /* Check, if the chip supports auto page increment */
1475 if (!NAND_CANAUTOINCR(this) || !(page
& blockcheck
))
1479 /* Deselect and wake up anyone waiting on the device */
1480 nand_release_device(mtd
);
1486 * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer
1487 * @mtd: MTD device structure
1488 * @fsbuf: buffer given by fs driver
1489 * @oobsel: out of band selection structre
1490 * @autoplace: 1 = place given buffer into the oob bytes
1491 * @numpages: number of pages to prepare
1494 * 1. Filesystem buffer available and autoplacement is off,
1495 * return filesystem buffer
1496 * 2. No filesystem buffer or autoplace is off, return internal
1498 * 3. Filesystem buffer is given and autoplace selected
1499 * put data from fs buffer into internal buffer and
1500 * retrun internal buffer
1502 * Note: The internal buffer is filled with 0xff. This must
1503 * be done only once, when no autoplacement happens
1504 * Autoplacement sets the buffer dirty flag, which
1505 * forces the 0xff fill before using the buffer again.
1508 static u_char
* nand_prepare_oobbuf (struct mtd_info
*mtd
, u_char
*fsbuf
, struct nand_oobinfo
*oobsel
,
1509 int autoplace
, int numpages
)
1511 struct nand_chip
*this = mtd
->priv
;
1514 /* Zero copy fs supplied buffer */
1515 if (fsbuf
&& !autoplace
)
1518 /* Check, if the buffer must be filled with ff again */
1519 if (this->oobdirty
) {
1520 memset (this->oob_buf
, 0xff,
1521 mtd
->oobsize
<< (this->phys_erase_shift
- this->page_shift
));
1525 /* If we have no autoplacement or no fs buffer use the internal one */
1526 if (!autoplace
|| !fsbuf
)
1527 return this->oob_buf
;
1529 /* Walk through the pages and place the data */
1532 while (numpages
--) {
1533 for (i
= 0, len
= 0; len
< mtd
->oobavail
; i
++) {
1534 int to
= ofs
+ oobsel
->oobfree
[i
][0];
1535 int num
= oobsel
->oobfree
[i
][1];
1536 memcpy (&this->oob_buf
[to
], fsbuf
, num
);
1540 ofs
+= mtd
->oobavail
;
1542 return this->oob_buf
;
1545 #define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
1548 * nand_write - [MTD Interface] compability function for nand_write_ecc
1549 * @mtd: MTD device structure
1550 * @to: offset to write to
1551 * @len: number of bytes to write
1552 * @retlen: pointer to variable to store the number of written bytes
1553 * @buf: the data to write
1555 * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
1558 static int nand_write (struct mtd_info
*mtd
, loff_t to
, size_t len
, size_t * retlen
, const u_char
* buf
)
1560 return (nand_write_ecc (mtd
, to
, len
, retlen
, buf
, NULL
, NULL
));
1564 * nand_write_ecc - [MTD Interface] NAND write with ECC
1565 * @mtd: MTD device structure
1566 * @to: offset to write to
1567 * @len: number of bytes to write
1568 * @retlen: pointer to variable to store the number of written bytes
1569 * @buf: the data to write
1570 * @eccbuf: filesystem supplied oob data buffer
1571 * @oobsel: oob selection structure
1573 * NAND write with ECC
1575 static int nand_write_ecc (struct mtd_info
*mtd
, loff_t to
, size_t len
,
1576 size_t * retlen
, const u_char
* buf
, u_char
* eccbuf
, struct nand_oobinfo
*oobsel
)
1578 int startpage
, page
, ret
= -EIO
, oob
= 0, written
= 0, chipnr
;
1579 int autoplace
= 0, numpages
, totalpages
;
1580 struct nand_chip
*this = mtd
->priv
;
1581 u_char
*oobbuf
, *bufstart
;
1582 int ppblock
= (1 << (this->phys_erase_shift
- this->page_shift
));
1584 DEBUG (MTD_DEBUG_LEVEL3
, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to
, (int) len
);
1586 /* Initialize retlen, in case of early exit */
1589 /* Do not allow write past end of device */
1590 if ((to
+ len
) > mtd
->size
) {
1591 DEBUG (MTD_DEBUG_LEVEL0
, "nand_write_ecc: Attempt to write past end of page\n");
1595 /* reject writes, which are not page aligned */
1596 if (NOTALIGNED (to
) || NOTALIGNED(len
)) {
1597 printk (KERN_NOTICE
"nand_write_ecc: Attempt to write not page aligned data\n");
1601 /* Grab the lock and see if the device is available */
1602 nand_get_device (this, mtd
, FL_WRITING
);
1604 /* Calculate chipnr */
1605 chipnr
= (int)(to
>> this->chip_shift
);
1606 /* Select the NAND device */
1607 this->select_chip(mtd
, chipnr
);
1609 /* Check, if it is write protected */
1610 if (nand_check_wp(mtd
))
1613 /* if oobsel is NULL, use chip defaults */
1615 oobsel
= &mtd
->oobinfo
;
1617 /* Autoplace of oob data ? Use the default placement scheme */
1618 if (oobsel
->useecc
== MTD_NANDECC_AUTOPLACE
) {
1619 oobsel
= this->autooob
;
1622 if (oobsel
->useecc
== MTD_NANDECC_AUTOPL_USR
)
1625 /* Setup variables and oob buffer */
1626 totalpages
= len
>> this->page_shift
;
1627 page
= (int) (to
>> this->page_shift
);
1628 /* Invalidate the page cache, if we write to the cached page */
1629 if (page
<= this->pagebuf
&& this->pagebuf
< (page
+ totalpages
))
1632 /* Set it relative to chip */
1633 page
&= this->pagemask
;
1635 /* Calc number of pages we can write in one go */
1636 numpages
= min (ppblock
- (startpage
& (ppblock
- 1)), totalpages
);
1637 oobbuf
= nand_prepare_oobbuf (mtd
, eccbuf
, oobsel
, autoplace
, numpages
);
1638 bufstart
= (u_char
*)buf
;
1640 /* Loop until all data is written */
1641 while (written
< len
) {
1643 this->data_poi
= (u_char
*) &buf
[written
];
1644 /* Write one page. If this is the last page to write
1645 * or the last page in this block, then use the
1646 * real pageprogram command, else select cached programming
1647 * if supported by the chip.
1649 ret
= nand_write_page (mtd
, this, page
, &oobbuf
[oob
], oobsel
, (--numpages
> 0));
1651 DEBUG (MTD_DEBUG_LEVEL0
, "nand_write_ecc: write_page failed %d\n", ret
);
1655 oob
+= mtd
->oobsize
;
1656 /* Update written bytes count */
1657 written
+= mtd
->oobblock
;
1661 /* Increment page address */
1664 /* Have we hit a block boundary ? Then we have to verify and
1665 * if verify is ok, we have to setup the oob buffer for
1668 if (!(page
& (ppblock
- 1))){
1670 this->data_poi
= bufstart
;
1671 ret
= nand_verify_pages (mtd
, this, startpage
,
1673 oobbuf
, oobsel
, chipnr
, (eccbuf
!= NULL
));
1675 DEBUG (MTD_DEBUG_LEVEL0
, "nand_write_ecc: verify_pages failed %d\n", ret
);
1680 ofs
= autoplace
? mtd
->oobavail
: mtd
->oobsize
;
1682 eccbuf
+= (page
- startpage
) * ofs
;
1683 totalpages
-= page
- startpage
;
1684 numpages
= min (totalpages
, ppblock
);
1685 page
&= this->pagemask
;
1687 oobbuf
= nand_prepare_oobbuf (mtd
, eccbuf
, oobsel
,
1688 autoplace
, numpages
);
1689 /* Check, if we cross a chip boundary */
1692 this->select_chip(mtd
, -1);
1693 this->select_chip(mtd
, chipnr
);
1697 /* Verify the remaining pages */
1699 this->data_poi
= bufstart
;
1700 ret
= nand_verify_pages (mtd
, this, startpage
, totalpages
,
1701 oobbuf
, oobsel
, chipnr
, (eccbuf
!= NULL
));
1705 DEBUG (MTD_DEBUG_LEVEL0
, "nand_write_ecc: verify_pages failed %d\n", ret
);
1708 /* Deselect and wake up anyone waiting on the device */
1709 nand_release_device(mtd
);
1716 * nand_write_oob - [MTD Interface] NAND write out-of-band
1717 * @mtd: MTD device structure
1718 * @to: offset to write to
1719 * @len: number of bytes to write
1720 * @retlen: pointer to variable to store the number of written bytes
1721 * @buf: the data to write
1723 * NAND write out-of-band
1725 static int nand_write_oob (struct mtd_info
*mtd
, loff_t to
, size_t len
, size_t * retlen
, const u_char
* buf
)
1727 int column
, page
, status
, ret
= -EIO
, chipnr
;
1728 struct nand_chip
*this = mtd
->priv
;
1730 DEBUG (MTD_DEBUG_LEVEL3
, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to
, (int) len
);
1732 /* Shift to get page */
1733 page
= (int) (to
>> this->page_shift
);
1734 chipnr
= (int) (to
>> this->chip_shift
);
1736 /* Mask to get column */
1737 column
= to
& (mtd
->oobsize
- 1);
1739 /* Initialize return length value */
1742 /* Do not allow write past end of page */
1743 if ((column
+ len
) > mtd
->oobsize
) {
1744 DEBUG (MTD_DEBUG_LEVEL0
, "nand_write_oob: Attempt to write past end of page\n");
1748 /* Grab the lock and see if the device is available */
1749 nand_get_device (this, mtd
, FL_WRITING
);
1751 /* Select the NAND device */
1752 this->select_chip(mtd
, chipnr
);
1754 /* Reset the chip. Some chips (like the Toshiba TC5832DC found
1755 in one of my DiskOnChip 2000 test units) will clear the whole
1756 data page too if we don't do this. I have no clue why, but
1757 I seem to have 'fixed' it in the doc2000 driver in
1758 August 1999. dwmw2. */
1759 this->cmdfunc(mtd
, NAND_CMD_RESET
, -1, -1);
1761 /* Check, if it is write protected */
1762 if (nand_check_wp(mtd
))
1765 /* Invalidate the page cache, if we write to the cached page */
1766 if (page
== this->pagebuf
)
1769 if (NAND_MUST_PAD(this)) {
1770 /* Write out desired data */
1771 this->cmdfunc (mtd
, NAND_CMD_SEQIN
, mtd
->oobblock
, page
& this->pagemask
);
1772 /* prepad 0xff for partial programming */
1773 this->write_buf(mtd
, ffchars
, column
);
1775 this->write_buf(mtd
, buf
, len
);
1776 /* postpad 0xff for partial programming */
1777 this->write_buf(mtd
, ffchars
, mtd
->oobsize
- (len
+column
));
1779 /* Write out desired data */
1780 this->cmdfunc (mtd
, NAND_CMD_SEQIN
, mtd
->oobblock
+ column
, page
& this->pagemask
);
1782 this->write_buf(mtd
, buf
, len
);
1784 /* Send command to program the OOB data */
1785 this->cmdfunc (mtd
, NAND_CMD_PAGEPROG
, -1, -1);
1787 status
= this->waitfunc (mtd
, this, FL_WRITING
);
1789 /* See if device thinks it succeeded */
1790 if (status
& 0x01) {
1791 DEBUG (MTD_DEBUG_LEVEL0
, "nand_write_oob: " "Failed write, page 0x%08x\n", page
);
1798 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1799 /* Send command to read back the data */
1800 this->cmdfunc (mtd
, NAND_CMD_READOOB
, column
, page
& this->pagemask
);
1802 if (this->verify_buf(mtd
, buf
, len
)) {
1803 DEBUG (MTD_DEBUG_LEVEL0
, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page
);
1810 /* Deselect and wake up anyone waiting on the device */
1811 nand_release_device(mtd
);
1816 /* XXX U-BOOT XXX */
1819 * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
1820 * @mtd: MTD device structure
1821 * @vecs: the iovectors to write
1822 * @count: number of vectors
1823 * @to: offset to write to
1824 * @retlen: pointer to variable to store the number of written bytes
1826 * NAND write with kvec. This just calls the ecc function
1828 static int nand_writev (struct mtd_info
*mtd
, const struct kvec
*vecs
, unsigned long count
,
1829 loff_t to
, size_t * retlen
)
1831 return (nand_writev_ecc (mtd
, vecs
, count
, to
, retlen
, NULL
, NULL
));
1835 * nand_writev_ecc - [MTD Interface] write with iovec with ecc
1836 * @mtd: MTD device structure
1837 * @vecs: the iovectors to write
1838 * @count: number of vectors
1839 * @to: offset to write to
1840 * @retlen: pointer to variable to store the number of written bytes
1841 * @eccbuf: filesystem supplied oob data buffer
1842 * @oobsel: oob selection structure
1844 * NAND write with iovec with ecc
1846 static int nand_writev_ecc (struct mtd_info
*mtd
, const struct kvec
*vecs
, unsigned long count
,
1847 loff_t to
, size_t * retlen
, u_char
*eccbuf
, struct nand_oobinfo
*oobsel
)
1849 int i
, page
, len
, total_len
, ret
= -EIO
, written
= 0, chipnr
;
1850 int oob
, numpages
, autoplace
= 0, startpage
;
1851 struct nand_chip
*this = mtd
->priv
;
1852 int ppblock
= (1 << (this->phys_erase_shift
- this->page_shift
));
1853 u_char
*oobbuf
, *bufstart
;
1855 /* Preset written len for early exit */
1858 /* Calculate total length of data */
1860 for (i
= 0; i
< count
; i
++)
1861 total_len
+= (int) vecs
[i
].iov_len
;
1863 DEBUG (MTD_DEBUG_LEVEL3
,
1864 "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to
, (unsigned int) total_len
, count
);
1866 /* Do not allow write past end of page */
1867 if ((to
+ total_len
) > mtd
->size
) {
1868 DEBUG (MTD_DEBUG_LEVEL0
, "nand_writev: Attempted write past end of device\n");
1872 /* reject writes, which are not page aligned */
1873 if (NOTALIGNED (to
) || NOTALIGNED(total_len
)) {
1874 printk (KERN_NOTICE
"nand_write_ecc: Attempt to write not page aligned data\n");
1878 /* Grab the lock and see if the device is available */
1879 nand_get_device (this, mtd
, FL_WRITING
);
1881 /* Get the current chip-nr */
1882 chipnr
= (int) (to
>> this->chip_shift
);
1883 /* Select the NAND device */
1884 this->select_chip(mtd
, chipnr
);
1886 /* Check, if it is write protected */
1887 if (nand_check_wp(mtd
))
1890 /* if oobsel is NULL, use chip defaults */
1892 oobsel
= &mtd
->oobinfo
;
1894 /* Autoplace of oob data ? Use the default placement scheme */
1895 if (oobsel
->useecc
== MTD_NANDECC_AUTOPLACE
) {
1896 oobsel
= this->autooob
;
1899 if (oobsel
->useecc
== MTD_NANDECC_AUTOPL_USR
)
1902 /* Setup start page */
1903 page
= (int) (to
>> this->page_shift
);
1904 /* Invalidate the page cache, if we write to the cached page */
1905 if (page
<= this->pagebuf
&& this->pagebuf
< ((to
+ total_len
) >> this->page_shift
))
1908 startpage
= page
& this->pagemask
;
1910 /* Loop until all kvec' data has been written */
1913 /* If the given tuple is >= pagesize then
1914 * write it out from the iov
1916 if ((vecs
->iov_len
- len
) >= mtd
->oobblock
) {
1917 /* Calc number of pages we can write
1918 * out of this iov in one go */
1919 numpages
= (vecs
->iov_len
- len
) >> this->page_shift
;
1920 /* Do not cross block boundaries */
1921 numpages
= min (ppblock
- (startpage
& (ppblock
- 1)), numpages
);
1922 oobbuf
= nand_prepare_oobbuf (mtd
, NULL
, oobsel
, autoplace
, numpages
);
1923 bufstart
= (u_char
*)vecs
->iov_base
;
1925 this->data_poi
= bufstart
;
1927 for (i
= 1; i
<= numpages
; i
++) {
1928 /* Write one page. If this is the last page to write
1929 * then use the real pageprogram command, else select
1930 * cached programming if supported by the chip.
1932 ret
= nand_write_page (mtd
, this, page
& this->pagemask
,
1933 &oobbuf
[oob
], oobsel
, i
!= numpages
);
1936 this->data_poi
+= mtd
->oobblock
;
1937 len
+= mtd
->oobblock
;
1938 oob
+= mtd
->oobsize
;
1941 /* Check, if we have to switch to the next tuple */
1942 if (len
>= (int) vecs
->iov_len
) {
1948 /* We must use the internal buffer, read data out of each
1949 * tuple until we have a full page to write
1952 while (cnt
< mtd
->oobblock
) {
1953 if (vecs
->iov_base
!= NULL
&& vecs
->iov_len
)
1954 this->data_buf
[cnt
++] = ((u_char
*) vecs
->iov_base
)[len
++];
1955 /* Check, if we have to switch to the next tuple */
1956 if (len
>= (int) vecs
->iov_len
) {
1962 this->pagebuf
= page
;
1963 this->data_poi
= this->data_buf
;
1964 bufstart
= this->data_poi
;
1966 oobbuf
= nand_prepare_oobbuf (mtd
, NULL
, oobsel
, autoplace
, numpages
);
1967 ret
= nand_write_page (mtd
, this, page
& this->pagemask
,
1974 this->data_poi
= bufstart
;
1975 ret
= nand_verify_pages (mtd
, this, startpage
, numpages
, oobbuf
, oobsel
, chipnr
, 0);
1979 written
+= mtd
->oobblock
* numpages
;
1984 startpage
= page
& this->pagemask
;
1985 /* Check, if we cross a chip boundary */
1988 this->select_chip(mtd
, -1);
1989 this->select_chip(mtd
, chipnr
);
1994 /* Deselect and wake up anyone waiting on the device */
1995 nand_release_device(mtd
);
2003 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2004 * @mtd: MTD device structure
2005 * @page: the page address of the block which will be erased
2007 * Standard erase command for NAND chips
2009 static void single_erase_cmd (struct mtd_info
*mtd
, int page
)
2011 struct nand_chip
*this = mtd
->priv
;
2012 /* Send commands to erase a block */
2013 this->cmdfunc (mtd
, NAND_CMD_ERASE1
, -1, page
);
2014 this->cmdfunc (mtd
, NAND_CMD_ERASE2
, -1, -1);
2018 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2019 * @mtd: MTD device structure
2020 * @page: the page address of the block which will be erased
2022 * AND multi block erase command function
2023 * Erase 4 consecutive blocks
2025 static void multi_erase_cmd (struct mtd_info
*mtd
, int page
)
2027 struct nand_chip
*this = mtd
->priv
;
2028 /* Send commands to erase a block */
2029 this->cmdfunc (mtd
, NAND_CMD_ERASE1
, -1, page
++);
2030 this->cmdfunc (mtd
, NAND_CMD_ERASE1
, -1, page
++);
2031 this->cmdfunc (mtd
, NAND_CMD_ERASE1
, -1, page
++);
2032 this->cmdfunc (mtd
, NAND_CMD_ERASE1
, -1, page
);
2033 this->cmdfunc (mtd
, NAND_CMD_ERASE2
, -1, -1);
2037 * nand_erase - [MTD Interface] erase block(s)
2038 * @mtd: MTD device structure
2039 * @instr: erase instruction
2041 * Erase one ore more blocks
2043 static int nand_erase (struct mtd_info
*mtd
, struct erase_info
*instr
)
2045 return nand_erase_nand (mtd
, instr
, 0);
2049 * nand_erase_intern - [NAND Interface] erase block(s)
2050 * @mtd: MTD device structure
2051 * @instr: erase instruction
2052 * @allowbbt: allow erasing the bbt area
2054 * Erase one ore more blocks
2056 int nand_erase_nand (struct mtd_info
*mtd
, struct erase_info
*instr
, int allowbbt
)
2058 int page
, len
, status
, pages_per_block
, ret
, chipnr
;
2059 struct nand_chip
*this = mtd
->priv
;
2061 DEBUG (MTD_DEBUG_LEVEL3
,
2062 "nand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr
->addr
, (unsigned int) instr
->len
);
2064 /* Start address must align on block boundary */
2065 if (instr
->addr
& ((1 << this->phys_erase_shift
) - 1)) {
2066 DEBUG (MTD_DEBUG_LEVEL0
, "nand_erase: Unaligned address\n");
2070 /* Length must align on block boundary */
2071 if (instr
->len
& ((1 << this->phys_erase_shift
) - 1)) {
2072 DEBUG (MTD_DEBUG_LEVEL0
, "nand_erase: Length not block aligned\n");
2076 /* Do not allow erase past end of device */
2077 if ((instr
->len
+ instr
->addr
) > mtd
->size
) {
2078 DEBUG (MTD_DEBUG_LEVEL0
, "nand_erase: Erase past end of device\n");
2082 instr
->fail_addr
= 0xffffffff;
2084 /* Grab the lock and see if the device is available */
2085 nand_get_device (this, mtd
, FL_ERASING
);
2087 /* Shift to get first page */
2088 page
= (int) (instr
->addr
>> this->page_shift
);
2089 chipnr
= (int) (instr
->addr
>> this->chip_shift
);
2091 /* Calculate pages in each block */
2092 pages_per_block
= 1 << (this->phys_erase_shift
- this->page_shift
);
2094 /* Select the NAND device */
2095 this->select_chip(mtd
, chipnr
);
2097 /* Check the WP bit */
2098 /* Check, if it is write protected */
2099 if (nand_check_wp(mtd
)) {
2100 DEBUG (MTD_DEBUG_LEVEL0
, "nand_erase: Device is write protected!!!\n");
2101 instr
->state
= MTD_ERASE_FAILED
;
2105 /* Loop through the pages */
2108 instr
->state
= MTD_ERASING
;
2111 /* Check if we have a bad block, we do not erase bad blocks ! */
2112 if (nand_block_checkbad(mtd
, ((loff_t
) page
) << this->page_shift
, 0, allowbbt
)) {
2113 printk (KERN_WARNING
"nand_erase: attempt to erase a bad block at page 0x%08x\n", page
);
2114 instr
->state
= MTD_ERASE_FAILED
;
2118 /* Invalidate the page cache, if we erase the block which contains
2119 the current cached page */
2120 if (page
<= this->pagebuf
&& this->pagebuf
< (page
+ pages_per_block
))
2123 this->erase_cmd (mtd
, page
& this->pagemask
);
2125 status
= this->waitfunc (mtd
, this, FL_ERASING
);
2127 /* See if block erase succeeded */
2128 if (status
& 0x01) {
2129 DEBUG (MTD_DEBUG_LEVEL0
, "nand_erase: " "Failed erase, page 0x%08x\n", page
);
2130 instr
->state
= MTD_ERASE_FAILED
;
2131 instr
->fail_addr
= (page
<< this->page_shift
);
2135 /* Increment page address and decrement length */
2136 len
-= (1 << this->phys_erase_shift
);
2137 page
+= pages_per_block
;
2139 /* Check, if we cross a chip boundary */
2140 if (len
&& !(page
& this->pagemask
)) {
2142 this->select_chip(mtd
, -1);
2143 this->select_chip(mtd
, chipnr
);
2146 instr
->state
= MTD_ERASE_DONE
;
2150 ret
= instr
->state
== MTD_ERASE_DONE
? 0 : -EIO
;
2151 /* Do call back function */
2153 mtd_erase_callback(instr
);
2155 /* Deselect and wake up anyone waiting on the device */
2156 nand_release_device(mtd
);
2158 /* Return more or less happy */
2163 * nand_sync - [MTD Interface] sync
2164 * @mtd: MTD device structure
2166 * Sync is actually a wait for chip ready function
2168 static void nand_sync (struct mtd_info
*mtd
)
2170 struct nand_chip
*this = mtd
->priv
;
2172 DEBUG (MTD_DEBUG_LEVEL3
, "nand_sync: called\n");
2174 /* Grab the lock and see if the device is available */
2175 nand_get_device (this, mtd
, FL_SYNCING
);
2176 /* Release it and go back */
2177 nand_release_device (mtd
);
2182 * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
2183 * @mtd: MTD device structure
2184 * @ofs: offset relative to mtd start
2186 static int nand_block_isbad (struct mtd_info
*mtd
, loff_t ofs
)
2188 /* Check for invalid offset */
2189 if (ofs
> mtd
->size
)
2192 return nand_block_checkbad (mtd
, ofs
, 1, 0);
2196 * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
2197 * @mtd: MTD device structure
2198 * @ofs: offset relative to mtd start
2200 static int nand_block_markbad (struct mtd_info
*mtd
, loff_t ofs
)
2202 struct nand_chip
*this = mtd
->priv
;
2205 if ((ret
= nand_block_isbad(mtd
, ofs
))) {
2206 /* If it was bad already, return success and do nothing. */
2212 return this->block_markbad(mtd
, ofs
);
2216 * nand_scan - [NAND Interface] Scan for the NAND device
2217 * @mtd: MTD device structure
2218 * @maxchips: Number of chips to scan for
2220 * This fills out all the not initialized function pointers
2221 * with the defaults.
2222 * The flash ID is read and the mtd/chip structures are
2223 * filled with the appropriate values. Buffers are allocated if
2224 * they are not provided by the board driver
2227 int nand_scan (struct mtd_info
*mtd
, int maxchips
)
2229 int i
, j
, nand_maf_id
, nand_dev_id
, busw
;
2230 struct nand_chip
*this = mtd
->priv
;
2232 /* Get buswidth to select the correct functions*/
2233 busw
= this->options
& NAND_BUSWIDTH_16
;
2235 /* check for proper chip_delay setup, set 20us if not */
2236 if (!this->chip_delay
)
2237 this->chip_delay
= 20;
2239 /* check, if a user supplied command function given */
2240 if (this->cmdfunc
== NULL
)
2241 this->cmdfunc
= nand_command
;
2243 /* check, if a user supplied wait function given */
2244 if (this->waitfunc
== NULL
)
2245 this->waitfunc
= nand_wait
;
2247 if (!this->select_chip
)
2248 this->select_chip
= nand_select_chip
;
2249 if (!this->write_byte
)
2250 this->write_byte
= busw
? nand_write_byte16
: nand_write_byte
;
2251 if (!this->read_byte
)
2252 this->read_byte
= busw
? nand_read_byte16
: nand_read_byte
;
2253 if (!this->write_word
)
2254 this->write_word
= nand_write_word
;
2255 if (!this->read_word
)
2256 this->read_word
= nand_read_word
;
2257 if (!this->block_bad
)
2258 this->block_bad
= nand_block_bad
;
2259 if (!this->block_markbad
)
2260 this->block_markbad
= nand_default_block_markbad
;
2261 if (!this->write_buf
)
2262 this->write_buf
= busw
? nand_write_buf16
: nand_write_buf
;
2263 if (!this->read_buf
)
2264 this->read_buf
= busw
? nand_read_buf16
: nand_read_buf
;
2265 if (!this->verify_buf
)
2266 this->verify_buf
= busw
? nand_verify_buf16
: nand_verify_buf
;
2267 if (!this->scan_bbt
)
2268 this->scan_bbt
= nand_default_bbt
;
2270 /* Select the device */
2271 this->select_chip(mtd
, 0);
2273 /* Send the command for reading device ID */
2274 this->cmdfunc (mtd
, NAND_CMD_READID
, 0x00, -1);
2276 /* Read manufacturer and device IDs */
2277 nand_maf_id
= this->read_byte(mtd
);
2278 nand_dev_id
= this->read_byte(mtd
);
2280 /* Print and store flash device information */
2281 for (i
= 0; nand_flash_ids
[i
].name
!= NULL
; i
++) {
2283 if (nand_dev_id
!= nand_flash_ids
[i
].id
)
2286 if (!mtd
->name
) mtd
->name
= nand_flash_ids
[i
].name
;
2287 this->chipsize
= nand_flash_ids
[i
].chipsize
<< 20;
2289 /* New devices have all the information in additional id bytes */
2290 if (!nand_flash_ids
[i
].pagesize
) {
2292 /* The 3rd id byte contains non relevant data ATM */
2293 extid
= this->read_byte(mtd
);
2294 /* The 4th id byte is the important one */
2295 extid
= this->read_byte(mtd
);
2297 mtd
->oobblock
= 1024 << (extid
& 0x3);
2300 mtd
->oobsize
= (8 << (extid
& 0x03)) * (mtd
->oobblock
/ 512);
2302 /* Calc blocksize. Blocksize is multiples of 64KiB */
2303 mtd
->erasesize
= (64 * 1024) << (extid
& 0x03);
2305 /* Get buswidth information */
2306 busw
= (extid
& 0x01) ? NAND_BUSWIDTH_16
: 0;
2309 /* Old devices have this data hardcoded in the
2310 * device id table */
2311 mtd
->erasesize
= nand_flash_ids
[i
].erasesize
;
2312 mtd
->oobblock
= nand_flash_ids
[i
].pagesize
;
2313 mtd
->oobsize
= mtd
->oobblock
/ 32;
2314 busw
= nand_flash_ids
[i
].options
& NAND_BUSWIDTH_16
;
2317 /* Check, if buswidth is correct. Hardware drivers should set
2319 if (busw
!= (this->options
& NAND_BUSWIDTH_16
)) {
2320 printk (KERN_INFO
"NAND device: Manufacturer ID:"
2321 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id
, nand_dev_id
,
2322 nand_manuf_ids
[i
].name
, mtd
->name
);
2323 printk (KERN_WARNING
2324 "NAND bus width %d instead %d bit\n",
2325 (this->options
& NAND_BUSWIDTH_16
) ? 16 : 8,
2327 this->select_chip(mtd
, -1);
2331 /* Calculate the address shift from the page size */
2332 this->page_shift
= ffs(mtd
->oobblock
) - 1;
2333 this->bbt_erase_shift
= this->phys_erase_shift
= ffs(mtd
->erasesize
) - 1;
2334 this->chip_shift
= ffs(this->chipsize
) - 1;
2336 /* Set the bad block position */
2337 this->badblockpos
= mtd
->oobblock
> 512 ?
2338 NAND_LARGE_BADBLOCK_POS
: NAND_SMALL_BADBLOCK_POS
;
2340 /* Get chip options, preserve non chip based options */
2341 this->options
&= ~NAND_CHIPOPTIONS_MSK
;
2342 this->options
|= nand_flash_ids
[i
].options
& NAND_CHIPOPTIONS_MSK
;
2343 /* Set this as a default. Board drivers can override it, if neccecary */
2344 this->options
|= NAND_NO_AUTOINCR
;
2345 /* Check if this is a not a samsung device. Do not clear the options
2346 * for chips which are not having an extended id.
2348 if (nand_maf_id
!= NAND_MFR_SAMSUNG
&& !nand_flash_ids
[i
].pagesize
)
2349 this->options
&= ~NAND_SAMSUNG_LP_OPTIONS
;
2351 /* Check for AND chips with 4 page planes */
2352 if (this->options
& NAND_4PAGE_ARRAY
)
2353 this->erase_cmd
= multi_erase_cmd
;
2355 this->erase_cmd
= single_erase_cmd
;
2357 /* Do not replace user supplied command function ! */
2358 if (mtd
->oobblock
> 512 && this->cmdfunc
== nand_command
)
2359 this->cmdfunc
= nand_command_lp
;
2361 /* Try to identify manufacturer */
2362 for (j
= 0; nand_manuf_ids
[j
].id
!= 0x0; j
++) {
2363 if (nand_manuf_ids
[j
].id
== nand_maf_id
)
2366 printk (KERN_INFO
"NAND device: Manufacturer ID:"
2367 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id
, nand_dev_id
,
2368 nand_manuf_ids
[j
].name
, nand_flash_ids
[i
].name
);
2372 if (!nand_flash_ids
[i
].name
) {
2373 printk (KERN_WARNING
"No NAND device found!!!\n");
2374 this->select_chip(mtd
, -1);
2378 for (i
=1; i
< maxchips
; i
++) {
2379 this->select_chip(mtd
, i
);
2381 /* Send the command for reading device ID */
2382 this->cmdfunc (mtd
, NAND_CMD_READID
, 0x00, -1);
2384 /* Read manufacturer and device IDs */
2385 if (nand_maf_id
!= this->read_byte(mtd
) ||
2386 nand_dev_id
!= this->read_byte(mtd
))
2390 printk(KERN_INFO
"%d NAND chips detected\n", i
);
2392 /* Allocate buffers, if neccecary */
2393 if (!this->oob_buf
) {
2395 len
= mtd
->oobsize
<< (this->phys_erase_shift
- this->page_shift
);
2396 this->oob_buf
= kmalloc (len
, GFP_KERNEL
);
2397 if (!this->oob_buf
) {
2398 printk (KERN_ERR
"nand_scan(): Cannot allocate oob_buf\n");
2401 this->options
|= NAND_OOBBUF_ALLOC
;
2404 if (!this->data_buf
) {
2406 len
= mtd
->oobblock
+ mtd
->oobsize
;
2407 this->data_buf
= kmalloc (len
, GFP_KERNEL
);
2408 if (!this->data_buf
) {
2409 if (this->options
& NAND_OOBBUF_ALLOC
)
2410 kfree (this->oob_buf
);
2411 printk (KERN_ERR
"nand_scan(): Cannot allocate data_buf\n");
2414 this->options
|= NAND_DATABUF_ALLOC
;
2417 /* Store the number of chips and calc total size for mtd */
2419 mtd
->size
= i
* this->chipsize
;
2420 /* Convert chipsize to number of pages per chip -1. */
2421 this->pagemask
= (this->chipsize
>> this->page_shift
) - 1;
2422 /* Preset the internal oob buffer */
2423 memset(this->oob_buf
, 0xff, mtd
->oobsize
<< (this->phys_erase_shift
- this->page_shift
));
2425 /* If no default placement scheme is given, select an
2426 * appropriate one */
2427 if (!this->autooob
) {
2428 /* Select the appropriate default oob placement scheme for
2429 * placement agnostic filesystems */
2430 switch (mtd
->oobsize
) {
2432 this->autooob
= &nand_oob_8
;
2435 this->autooob
= &nand_oob_16
;
2438 this->autooob
= &nand_oob_64
;
2441 printk (KERN_WARNING
"No oob scheme defined for oobsize %d\n",
2447 /* The number of bytes available for the filesystem to place fs dependend
2449 if (this->options
& NAND_BUSWIDTH_16
) {
2450 mtd
->oobavail
= mtd
->oobsize
- (this->autooob
->eccbytes
+ 2);
2451 if (this->autooob
->eccbytes
& 0x01)
2454 mtd
->oobavail
= mtd
->oobsize
- (this->autooob
->eccbytes
+ 1);
2457 * check ECC mode, default to software
2458 * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
2459 * fallback to software ECC
2461 this->eccsize
= 256; /* set default eccsize */
2464 switch (this->eccmode
) {
2465 case NAND_ECC_HW12_2048
:
2466 if (mtd
->oobblock
< 2048) {
2467 printk(KERN_WARNING
"2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
2469 this->eccmode
= NAND_ECC_SOFT
;
2470 this->calculate_ecc
= nand_calculate_ecc
;
2471 this->correct_data
= nand_correct_data
;
2473 this->eccsize
= 2048;
2476 case NAND_ECC_HW3_512
:
2477 case NAND_ECC_HW6_512
:
2478 case NAND_ECC_HW8_512
:
2479 if (mtd
->oobblock
== 256) {
2480 printk (KERN_WARNING
"512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
2481 this->eccmode
= NAND_ECC_SOFT
;
2482 this->calculate_ecc
= nand_calculate_ecc
;
2483 this->correct_data
= nand_correct_data
;
2485 this->eccsize
= 512; /* set eccsize to 512 */
2488 case NAND_ECC_HW3_256
:
2492 printk (KERN_WARNING
"NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
2493 this->eccmode
= NAND_ECC_NONE
;
2497 this->calculate_ecc
= nand_calculate_ecc
;
2498 this->correct_data
= nand_correct_data
;
2502 printk (KERN_WARNING
"Invalid NAND_ECC_MODE %d\n", this->eccmode
);
2506 /* Check hardware ecc function availability and adjust number of ecc bytes per
2509 switch (this->eccmode
) {
2510 case NAND_ECC_HW12_2048
:
2511 this->eccbytes
+= 4;
2512 case NAND_ECC_HW8_512
:
2513 this->eccbytes
+= 2;
2514 case NAND_ECC_HW6_512
:
2515 this->eccbytes
+= 3;
2516 case NAND_ECC_HW3_512
:
2517 case NAND_ECC_HW3_256
:
2518 if (this->calculate_ecc
&& this->correct_data
&& this->enable_hwecc
)
2520 printk (KERN_WARNING
"No ECC functions supplied, Hardware ECC not possible\n");
2524 mtd
->eccsize
= this->eccsize
;
2526 /* Set the number of read / write steps for one page to ensure ECC generation */
2527 switch (this->eccmode
) {
2528 case NAND_ECC_HW12_2048
:
2529 this->eccsteps
= mtd
->oobblock
/ 2048;
2531 case NAND_ECC_HW3_512
:
2532 case NAND_ECC_HW6_512
:
2533 case NAND_ECC_HW8_512
:
2534 this->eccsteps
= mtd
->oobblock
/ 512;
2536 case NAND_ECC_HW3_256
:
2538 this->eccsteps
= mtd
->oobblock
/ 256;
2546 /* XXX U-BOOT XXX */
2548 /* Initialize state, waitqueue and spinlock */
2549 this->state
= FL_READY
;
2550 init_waitqueue_head (&this->wq
);
2551 spin_lock_init (&this->chip_lock
);
2554 /* De-select the device */
2555 this->select_chip(mtd
, -1);
2557 /* Invalidate the pagebuffer reference */
2560 /* Fill in remaining MTD driver data */
2561 mtd
->type
= MTD_NANDFLASH
;
2562 mtd
->flags
= MTD_CAP_NANDFLASH
| MTD_ECC
;
2563 mtd
->ecctype
= MTD_ECC_SW
;
2564 mtd
->erase
= nand_erase
;
2566 mtd
->unpoint
= NULL
;
2567 mtd
->read
= nand_read
;
2568 mtd
->write
= nand_write
;
2569 mtd
->read_ecc
= nand_read_ecc
;
2570 mtd
->write_ecc
= nand_write_ecc
;
2571 mtd
->read_oob
= nand_read_oob
;
2572 mtd
->write_oob
= nand_write_oob
;
2573 /* XXX U-BOOT XXX */
2576 mtd
->writev
= nand_writev
;
2577 mtd
->writev_ecc
= nand_writev_ecc
;
2579 mtd
->sync
= nand_sync
;
2580 /* XXX U-BOOT XXX */
2584 mtd
->suspend
= NULL
;
2587 mtd
->block_isbad
= nand_block_isbad
;
2588 mtd
->block_markbad
= nand_block_markbad
;
2590 /* and make the autooob the default one */
2591 memcpy(&mtd
->oobinfo
, this->autooob
, sizeof(mtd
->oobinfo
));
2592 /* XXX U-BOOT XXX */
2594 mtd
->owner
= THIS_MODULE
;
2596 /* Build bad block table */
2597 return this->scan_bbt (mtd
);
2601 * nand_release - [NAND Interface] Free resources held by the NAND device
2602 * @mtd: MTD device structure
2604 void nand_release (struct mtd_info
*mtd
)
2606 struct nand_chip
*this = mtd
->priv
;
2608 #ifdef CONFIG_MTD_PARTITIONS
2609 /* Deregister partitions */
2610 del_mtd_partitions (mtd
);
2612 /* Deregister the device */
2613 /* XXX U-BOOT XXX */
2615 del_mtd_device (mtd
);
2617 /* Free bad block table memory, if allocated */
2620 /* Buffer allocated by nand_scan ? */
2621 if (this->options
& NAND_OOBBUF_ALLOC
)
2622 kfree (this->oob_buf
);
2623 /* Buffer allocated by nand_scan ? */
2624 if (this->options
& NAND_DATABUF_ALLOC
)
2625 kfree (this->data_buf
);