2 * drivers/mtd/nand_bbt.c
5 * Bad block table support for the NAND driver
7 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 * When nand_scan_bbt is called, then it tries to find the bad block table
16 * depending on the options in the BBT descriptor(s). If no flash based BBT
17 * (NAND_USE_FLASH_BBT) is specified then the device is scanned for factory
18 * marked good / bad blocks. This information is used to create a memory BBT.
19 * Once a new bad block is discovered then the "factory" information is updated
21 * If a flash based BBT is specified then the function first tries to find the
22 * BBT on flash. If a BBT is found then the contents are read and the memory
23 * based BBT is created. If a mirrored BBT is selected then the mirror is
24 * searched too and the versions are compared. If the mirror has a greater
25 * version number than the mirror BBT is used to build the memory based BBT.
26 * If the tables are not versioned, then we "or" the bad block information.
27 * If one of the BBTs is out of date or does not exist it is (re)created.
28 * If no BBT exists at all then the device is scanned for factory marked
29 * good / bad blocks and the bad block tables are created.
31 * For manufacturer created BBTs like the one found on M-SYS DOC devices
32 * the BBT is searched and read but never created
34 * The auto generated bad block table is located in the last good blocks
35 * of the device. The table is mirrored, so it can be updated eventually.
36 * The table is marked in the OOB area with an ident pattern and a version
37 * number which indicates which of both tables is more up to date. If the NAND
38 * controller needs the complete OOB area for the ECC information then the
39 * option NAND_USE_FLASH_BBT_NO_OOB should be used: it moves the ident pattern
40 * and the version byte into the data area and the OOB area will remain
43 * The table uses 2 bits per block
45 * 00b: block is factory marked bad
46 * 01b, 10b: block is marked bad due to wear
48 * The memory bad block table uses the following scheme:
50 * 01b: block is marked bad due to wear
51 * 10b: block is reserved (to protect the bbt area)
52 * 11b: block is factory marked bad
54 * Multichip devices like DOC store the bad block info per floor.
56 * Following assumptions are made:
57 * - bbts start at a page boundary, if autolocated on a block boundary
58 * - the space necessary for a bbt in FLASH does not exceed a block boundary
62 #include <linux/slab.h>
63 #include <linux/types.h>
64 #include <linux/mtd/mtd.h>
65 #include <linux/mtd/nand.h>
66 #include <linux/mtd/nand_ecc.h>
67 #include <linux/bitops.h>
68 #include <linux/delay.h>
69 #include <linux/vmalloc.h>
71 static int check_pattern_no_oob(uint8_t *buf
, struct nand_bbt_descr
*td
)
75 ret
= memcmp(buf
, td
->pattern
, td
->len
);
82 * check_pattern - [GENERIC] check if a pattern is in the buffer
83 * @buf: the buffer to search
84 * @len: the length of buffer to search
85 * @paglen: the pagelength
86 * @td: search pattern descriptor
88 * Check for a pattern at the given place. Used to search bad block
89 * tables and good / bad block identifiers.
90 * If the SCAN_EMPTY option is set then check, if all bytes except the
91 * pattern area contain 0xff
94 static int check_pattern(uint8_t *buf
, int len
, int paglen
, struct nand_bbt_descr
*td
)
99 if (td
->options
& NAND_BBT_NO_OOB
)
100 return check_pattern_no_oob(buf
, td
);
102 end
= paglen
+ td
->offs
;
103 if (td
->options
& NAND_BBT_SCANEMPTY
) {
104 for (i
= 0; i
< end
; i
++) {
111 /* Compare the pattern */
112 for (i
= 0; i
< td
->len
; i
++) {
113 if (p
[i
] != td
->pattern
[i
])
117 /* Check both positions 1 and 6 for pattern? */
118 if (td
->options
& NAND_BBT_SCANBYTE1AND6
) {
119 if (td
->options
& NAND_BBT_SCANEMPTY
) {
121 end
+= NAND_SMALL_BADBLOCK_POS
- td
->offs
;
122 /* Check region between positions 1 and 6 */
123 for (i
= 0; i
< NAND_SMALL_BADBLOCK_POS
- td
->offs
- td
->len
;
130 p
+= NAND_SMALL_BADBLOCK_POS
- td
->offs
;
132 /* Compare the pattern */
133 for (i
= 0; i
< td
->len
; i
++) {
134 if (p
[i
] != td
->pattern
[i
])
139 if (td
->options
& NAND_BBT_SCANEMPTY
) {
142 for (i
= end
; i
< len
; i
++) {
151 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
152 * @buf: the buffer to search
153 * @td: search pattern descriptor
155 * Check for a pattern at the given place. Used to search bad block
156 * tables and good / bad block identifiers. Same as check_pattern, but
157 * no optional empty check
160 static int check_short_pattern(uint8_t *buf
, struct nand_bbt_descr
*td
)
165 /* Compare the pattern */
166 for (i
= 0; i
< td
->len
; i
++) {
167 if (p
[td
->offs
+ i
] != td
->pattern
[i
])
170 /* Need to check location 1 AND 6? */
171 if (td
->options
& NAND_BBT_SCANBYTE1AND6
) {
172 for (i
= 0; i
< td
->len
; i
++) {
173 if (p
[NAND_SMALL_BADBLOCK_POS
+ i
] != td
->pattern
[i
])
181 * add_marker_len - compute the length of the marker in data area
182 * @td: BBT descriptor used for computation
184 * The length will be 0 if the markeris located in OOB area.
186 static u32
add_marker_len(struct nand_bbt_descr
*td
)
190 if (!(td
->options
& NAND_BBT_NO_OOB
))
194 if (td
->options
& NAND_BBT_VERSION
)
200 * read_bbt - [GENERIC] Read the bad block table starting from page
201 * @mtd: MTD device structure
202 * @buf: temporary buffer
203 * @page: the starting page
204 * @num: the number of bbt descriptors to read
205 * @td: the bbt describtion table
206 * @offs: offset in the memory table
208 * Read the bad block table starting from page.
211 static int read_bbt(struct mtd_info
*mtd
, uint8_t *buf
, int page
, int num
,
212 struct nand_bbt_descr
*td
, int offs
)
214 int res
, i
, j
, act
= 0;
215 struct nand_chip
*this = mtd
->priv
;
216 size_t retlen
, len
, totlen
;
218 int bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
219 uint8_t msk
= (uint8_t) ((1 << bits
) - 1);
221 int reserved_block_code
= td
->reserved_block_code
;
223 totlen
= (num
* bits
) >> 3;
224 marker_len
= add_marker_len(td
);
225 from
= ((loff_t
) page
) << this->page_shift
;
228 len
= min(totlen
, (size_t) (1 << this->bbt_erase_shift
));
231 * In case the BBT marker is not in the OOB area it
232 * will be just in the first page.
238 res
= mtd
->read(mtd
, from
, len
, &retlen
, buf
);
241 printk(KERN_INFO
"nand_bbt: Error reading bad block table\n");
244 printk(KERN_WARNING
"nand_bbt: ECC error while reading bad block table\n");
248 for (i
= 0; i
< len
; i
++) {
249 uint8_t dat
= buf
[i
];
250 for (j
= 0; j
< 8; j
+= bits
, act
+= 2) {
251 uint8_t tmp
= (dat
>> j
) & msk
;
254 if (reserved_block_code
&& (tmp
== reserved_block_code
)) {
255 printk(KERN_DEBUG
"nand_read_bbt: Reserved block at 0x%012llx\n",
256 (loff_t
)((offs
<< 2) + (act
>> 1)) << this->bbt_erase_shift
);
257 this->bbt
[offs
+ (act
>> 3)] |= 0x2 << (act
& 0x06);
258 mtd
->ecc_stats
.bbtblocks
++;
261 /* Leave it for now, if its matured we can move this
262 * message to MTD_DEBUG_LEVEL0 */
263 printk(KERN_DEBUG
"nand_read_bbt: Bad block at 0x%012llx\n",
264 (loff_t
)((offs
<< 2) + (act
>> 1)) << this->bbt_erase_shift
);
265 /* Factory marked bad or worn out ? */
267 this->bbt
[offs
+ (act
>> 3)] |= 0x3 << (act
& 0x06);
269 this->bbt
[offs
+ (act
>> 3)] |= 0x1 << (act
& 0x06);
270 mtd
->ecc_stats
.badblocks
++;
280 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
281 * @mtd: MTD device structure
282 * @buf: temporary buffer
283 * @td: descriptor for the bad block table
284 * @chip: read the table for a specific chip, -1 read all chips.
285 * Applies only if NAND_BBT_PERCHIP option is set
287 * Read the bad block table for all chips starting at a given page
288 * We assume that the bbt bits are in consecutive order.
290 static int read_abs_bbt(struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
, int chip
)
292 struct nand_chip
*this = mtd
->priv
;
295 if (td
->options
& NAND_BBT_PERCHIP
) {
297 for (i
= 0; i
< this->numchips
; i
++) {
298 if (chip
== -1 || chip
== i
)
299 res
= read_bbt(mtd
, buf
, td
->pages
[i
],
300 this->chipsize
>> this->bbt_erase_shift
,
304 offs
+= this->chipsize
>> (this->bbt_erase_shift
+ 2);
307 res
= read_bbt(mtd
, buf
, td
->pages
[0],
308 mtd
->size
>> this->bbt_erase_shift
, td
, 0);
316 * BBT marker is in the first page, no OOB.
318 static int scan_read_raw_data(struct mtd_info
*mtd
, uint8_t *buf
, loff_t offs
,
319 struct nand_bbt_descr
*td
)
325 if (td
->options
& NAND_BBT_VERSION
)
328 return mtd
->read(mtd
, offs
, len
, &retlen
, buf
);
332 * Scan read raw data from flash
334 static int scan_read_raw_oob(struct mtd_info
*mtd
, uint8_t *buf
, loff_t offs
,
337 struct mtd_oob_ops ops
;
340 ops
.mode
= MTD_OOB_RAW
;
342 ops
.ooblen
= mtd
->oobsize
;
346 if (len
<= mtd
->writesize
) {
347 ops
.oobbuf
= buf
+ len
;
350 return mtd
->read_oob(mtd
, offs
, &ops
);
352 ops
.oobbuf
= buf
+ mtd
->writesize
;
354 ops
.len
= mtd
->writesize
;
355 res
= mtd
->read_oob(mtd
, offs
, &ops
);
361 buf
+= mtd
->oobsize
+ mtd
->writesize
;
362 len
-= mtd
->writesize
;
367 static int scan_read_raw(struct mtd_info
*mtd
, uint8_t *buf
, loff_t offs
,
368 size_t len
, struct nand_bbt_descr
*td
)
370 if (td
->options
& NAND_BBT_NO_OOB
)
371 return scan_read_raw_data(mtd
, buf
, offs
, td
);
373 return scan_read_raw_oob(mtd
, buf
, offs
, len
);
377 * Scan write data with oob to flash
379 static int scan_write_bbt(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
380 uint8_t *buf
, uint8_t *oob
)
382 struct mtd_oob_ops ops
;
384 ops
.mode
= MTD_OOB_PLACE
;
386 ops
.ooblen
= mtd
->oobsize
;
391 return mtd
->write_oob(mtd
, offs
, &ops
);
394 static u32
bbt_get_ver_offs(struct mtd_info
*mtd
, struct nand_bbt_descr
*td
)
396 u32 ver_offs
= td
->veroffs
;
398 if (!(td
->options
& NAND_BBT_NO_OOB
))
399 ver_offs
+= mtd
->writesize
;
404 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
405 * @mtd: MTD device structure
406 * @buf: temporary buffer
407 * @td: descriptor for the bad block table
408 * @md: descriptor for the bad block table mirror
410 * Read the bad block table(s) for all chips starting at a given page
411 * We assume that the bbt bits are in consecutive order.
414 static int read_abs_bbts(struct mtd_info
*mtd
, uint8_t *buf
,
415 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
)
417 struct nand_chip
*this = mtd
->priv
;
419 /* Read the primary version, if available */
420 if (td
->options
& NAND_BBT_VERSION
) {
421 scan_read_raw(mtd
, buf
, (loff_t
)td
->pages
[0] << this->page_shift
,
423 td
->version
[0] = buf
[bbt_get_ver_offs(mtd
, td
)];
424 printk(KERN_DEBUG
"Bad block table at page %d, version 0x%02X\n",
425 td
->pages
[0], td
->version
[0]);
428 /* Read the mirror version, if available */
429 if (md
&& (md
->options
& NAND_BBT_VERSION
)) {
430 scan_read_raw(mtd
, buf
, (loff_t
)md
->pages
[0] << this->page_shift
,
432 md
->version
[0] = buf
[bbt_get_ver_offs(mtd
, md
)];
433 printk(KERN_DEBUG
"Bad block table at page %d, version 0x%02X\n",
434 md
->pages
[0], md
->version
[0]);
440 * Scan a given block full
442 static int scan_block_full(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
,
443 loff_t offs
, uint8_t *buf
, size_t readlen
,
444 int scanlen
, int len
)
448 ret
= scan_read_raw_oob(mtd
, buf
, offs
, readlen
);
452 for (j
= 0; j
< len
; j
++, buf
+= scanlen
) {
453 if (check_pattern(buf
, scanlen
, mtd
->writesize
, bd
))
460 * Scan a given block partially
462 static int scan_block_fast(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
,
463 loff_t offs
, uint8_t *buf
, int len
)
465 struct mtd_oob_ops ops
;
468 ops
.ooblen
= mtd
->oobsize
;
472 ops
.mode
= MTD_OOB_PLACE
;
474 for (j
= 0; j
< len
; j
++) {
476 * Read the full oob until read_oob is fixed to
477 * handle single byte reads for 16 bit
480 ret
= mtd
->read_oob(mtd
, offs
, &ops
);
484 if (check_short_pattern(buf
, bd
))
487 offs
+= mtd
->writesize
;
493 * create_bbt - [GENERIC] Create a bad block table by scanning the device
494 * @mtd: MTD device structure
495 * @buf: temporary buffer
496 * @bd: descriptor for the good/bad block search pattern
497 * @chip: create the table for a specific chip, -1 read all chips.
498 * Applies only if NAND_BBT_PERCHIP option is set
500 * Create a bad block table by scanning the device
501 * for the given good/bad block identify pattern
503 static int create_bbt(struct mtd_info
*mtd
, uint8_t *buf
,
504 struct nand_bbt_descr
*bd
, int chip
)
506 struct nand_chip
*this = mtd
->priv
;
507 int i
, numblocks
, len
, scanlen
;
512 printk(KERN_INFO
"Scanning device for bad blocks\n");
514 if (bd
->options
& NAND_BBT_SCANALLPAGES
)
515 len
= 1 << (this->bbt_erase_shift
- this->page_shift
);
516 else if (bd
->options
& NAND_BBT_SCAN2NDPAGE
)
521 if (!(bd
->options
& NAND_BBT_SCANEMPTY
)) {
522 /* We need only read few bytes from the OOB area */
526 /* Full page content should be read */
527 scanlen
= mtd
->writesize
+ mtd
->oobsize
;
528 readlen
= len
* mtd
->writesize
;
532 /* Note that numblocks is 2 * (real numblocks) here, see i+=2
533 * below as it makes shifting and masking less painful */
534 numblocks
= mtd
->size
>> (this->bbt_erase_shift
- 1);
538 if (chip
>= this->numchips
) {
539 printk(KERN_WARNING
"create_bbt(): chipnr (%d) > available chips (%d)\n",
540 chip
+ 1, this->numchips
);
543 numblocks
= this->chipsize
>> (this->bbt_erase_shift
- 1);
544 startblock
= chip
* numblocks
;
545 numblocks
+= startblock
;
546 from
= (loff_t
)startblock
<< (this->bbt_erase_shift
- 1);
549 if (this->options
& NAND_BBT_SCANLASTPAGE
)
550 from
+= mtd
->erasesize
- (mtd
->writesize
* len
);
552 for (i
= startblock
; i
< numblocks
;) {
555 BUG_ON(bd
->options
& NAND_BBT_NO_OOB
);
557 if (bd
->options
& NAND_BBT_SCANALLPAGES
)
558 ret
= scan_block_full(mtd
, bd
, from
, buf
, readlen
,
561 ret
= scan_block_fast(mtd
, bd
, from
, buf
, len
);
567 this->bbt
[i
>> 3] |= 0x03 << (i
& 0x6);
568 printk(KERN_WARNING
"Bad eraseblock %d at 0x%012llx\n",
569 i
>> 1, (unsigned long long)from
);
570 mtd
->ecc_stats
.badblocks
++;
574 from
+= (1 << this->bbt_erase_shift
);
580 * search_bbt - [GENERIC] scan the device for a specific bad block table
581 * @mtd: MTD device structure
582 * @buf: temporary buffer
583 * @td: descriptor for the bad block table
585 * Read the bad block table by searching for a given ident pattern.
586 * Search is preformed either from the beginning up or from the end of
587 * the device downwards. The search starts always at the start of a
589 * If the option NAND_BBT_PERCHIP is given, each chip is searched
590 * for a bbt, which contains the bad block information of this chip.
591 * This is necessary to provide support for certain DOC devices.
593 * The bbt ident pattern resides in the oob area of the first page
596 static int search_bbt(struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
)
598 struct nand_chip
*this = mtd
->priv
;
600 int bits
, startblock
, block
, dir
;
601 int scanlen
= mtd
->writesize
+ mtd
->oobsize
;
603 int blocktopage
= this->bbt_erase_shift
- this->page_shift
;
605 /* Search direction top -> down ? */
606 if (td
->options
& NAND_BBT_LASTBLOCK
) {
607 startblock
= (mtd
->size
>> this->bbt_erase_shift
) - 1;
614 /* Do we have a bbt per chip ? */
615 if (td
->options
& NAND_BBT_PERCHIP
) {
616 chips
= this->numchips
;
617 bbtblocks
= this->chipsize
>> this->bbt_erase_shift
;
618 startblock
&= bbtblocks
- 1;
621 bbtblocks
= mtd
->size
>> this->bbt_erase_shift
;
624 /* Number of bits for each erase block in the bbt */
625 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
627 for (i
= 0; i
< chips
; i
++) {
628 /* Reset version information */
631 /* Scan the maximum number of blocks */
632 for (block
= 0; block
< td
->maxblocks
; block
++) {
634 int actblock
= startblock
+ dir
* block
;
635 loff_t offs
= (loff_t
)actblock
<< this->bbt_erase_shift
;
637 /* Read first page */
638 scan_read_raw(mtd
, buf
, offs
, mtd
->writesize
, td
);
639 if (!check_pattern(buf
, scanlen
, mtd
->writesize
, td
)) {
640 td
->pages
[i
] = actblock
<< blocktopage
;
641 if (td
->options
& NAND_BBT_VERSION
) {
642 offs
= bbt_get_ver_offs(mtd
, td
);
643 td
->version
[i
] = buf
[offs
];
648 startblock
+= this->chipsize
>> this->bbt_erase_shift
;
650 /* Check, if we found a bbt for each requested chip */
651 for (i
= 0; i
< chips
; i
++) {
652 if (td
->pages
[i
] == -1)
653 printk(KERN_WARNING
"Bad block table not found for chip %d\n", i
);
655 printk(KERN_DEBUG
"Bad block table found at page %d, version 0x%02X\n", td
->pages
[i
],
662 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
663 * @mtd: MTD device structure
664 * @buf: temporary buffer
665 * @td: descriptor for the bad block table
666 * @md: descriptor for the bad block table mirror
668 * Search and read the bad block table(s)
670 static int search_read_bbts(struct mtd_info
*mtd
, uint8_t * buf
, struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
)
672 /* Search the primary table */
673 search_bbt(mtd
, buf
, td
);
675 /* Search the mirror table */
677 search_bbt(mtd
, buf
, md
);
679 /* Force result check */
684 * write_bbt - [GENERIC] (Re)write the bad block table
686 * @mtd: MTD device structure
687 * @buf: temporary buffer
688 * @td: descriptor for the bad block table
689 * @md: descriptor for the bad block table mirror
690 * @chipsel: selector for a specific chip, -1 for all
692 * (Re)write the bad block table
695 static int write_bbt(struct mtd_info
*mtd
, uint8_t *buf
,
696 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
,
699 struct nand_chip
*this = mtd
->priv
;
700 struct erase_info einfo
;
701 int i
, j
, res
, chip
= 0;
702 int bits
, startblock
, dir
, page
, offs
, numblocks
, sft
, sftmsk
;
703 int nrchips
, bbtoffs
, pageoffs
, ooboffs
;
705 uint8_t rcode
= td
->reserved_block_code
;
706 size_t retlen
, len
= 0;
708 struct mtd_oob_ops ops
;
710 ops
.ooblen
= mtd
->oobsize
;
713 ops
.mode
= MTD_OOB_PLACE
;
717 /* Write bad block table per chip rather than per device ? */
718 if (td
->options
& NAND_BBT_PERCHIP
) {
719 numblocks
= (int)(this->chipsize
>> this->bbt_erase_shift
);
720 /* Full device write or specific chip ? */
722 nrchips
= this->numchips
;
724 nrchips
= chipsel
+ 1;
728 numblocks
= (int)(mtd
->size
>> this->bbt_erase_shift
);
732 /* Loop through the chips */
733 for (; chip
< nrchips
; chip
++) {
735 /* There was already a version of the table, reuse the page
736 * This applies for absolute placement too, as we have the
737 * page nr. in td->pages.
739 if (td
->pages
[chip
] != -1) {
740 page
= td
->pages
[chip
];
744 /* Automatic placement of the bad block table */
745 /* Search direction top -> down ? */
746 if (td
->options
& NAND_BBT_LASTBLOCK
) {
747 startblock
= numblocks
* (chip
+ 1) - 1;
750 startblock
= chip
* numblocks
;
754 for (i
= 0; i
< td
->maxblocks
; i
++) {
755 int block
= startblock
+ dir
* i
;
756 /* Check, if the block is bad */
757 switch ((this->bbt
[block
>> 2] >>
758 (2 * (block
& 0x03))) & 0x03) {
764 (this->bbt_erase_shift
- this->page_shift
);
765 /* Check, if the block is used by the mirror table */
766 if (!md
|| md
->pages
[chip
] != page
)
769 printk(KERN_ERR
"No space left to write bad block table\n");
773 /* Set up shift count and masks for the flash table */
774 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
777 case 1: sft
= 3; sftmsk
= 0x07; msk
[0] = 0x00; msk
[1] = 0x01;
780 case 2: sft
= 2; sftmsk
= 0x06; msk
[0] = 0x00; msk
[1] = 0x01;
783 case 4: sft
= 1; sftmsk
= 0x04; msk
[0] = 0x00; msk
[1] = 0x0C;
786 case 8: sft
= 0; sftmsk
= 0x00; msk
[0] = 0x00; msk
[1] = 0x0F;
789 default: return -EINVAL
;
792 bbtoffs
= chip
* (numblocks
>> 2);
794 to
= ((loff_t
) page
) << this->page_shift
;
796 /* Must we save the block contents ? */
797 if (td
->options
& NAND_BBT_SAVECONTENT
) {
798 /* Make it block aligned */
799 to
&= ~((loff_t
) ((1 << this->bbt_erase_shift
) - 1));
800 len
= 1 << this->bbt_erase_shift
;
801 res
= mtd
->read(mtd
, to
, len
, &retlen
, buf
);
804 printk(KERN_INFO
"nand_bbt: Error "
805 "reading block for writing "
806 "the bad block table\n");
809 printk(KERN_WARNING
"nand_bbt: ECC error "
810 "while reading block for writing "
811 "bad block table\n");
814 ops
.ooblen
= (len
>> this->page_shift
) * mtd
->oobsize
;
815 ops
.oobbuf
= &buf
[len
];
816 res
= mtd
->read_oob(mtd
, to
+ mtd
->writesize
, &ops
);
817 if (res
< 0 || ops
.oobretlen
!= ops
.ooblen
)
820 /* Calc the byte offset in the buffer */
821 pageoffs
= page
- (int)(to
>> this->page_shift
);
822 offs
= pageoffs
<< this->page_shift
;
823 /* Preset the bbt area with 0xff */
824 memset(&buf
[offs
], 0xff, (size_t) (numblocks
>> sft
));
825 ooboffs
= len
+ (pageoffs
* mtd
->oobsize
);
827 } else if (td
->options
& NAND_BBT_NO_OOB
) {
830 /* the version byte */
831 if (td
->options
& NAND_BBT_VERSION
)
834 len
= (size_t) (numblocks
>> sft
);
836 /* Make it page aligned ! */
837 len
= ALIGN(len
, mtd
->writesize
);
838 /* Preset the buffer with 0xff */
839 memset(buf
, 0xff, len
);
840 /* Pattern is located at the begin of first page */
841 memcpy(buf
, td
->pattern
, td
->len
);
844 len
= (size_t) (numblocks
>> sft
);
845 /* Make it page aligned ! */
846 len
= ALIGN(len
, mtd
->writesize
);
847 /* Preset the buffer with 0xff */
848 memset(buf
, 0xff, len
+
849 (len
>> this->page_shift
)* mtd
->oobsize
);
852 /* Pattern is located in oob area of first page */
853 memcpy(&buf
[ooboffs
+ td
->offs
], td
->pattern
, td
->len
);
856 if (td
->options
& NAND_BBT_VERSION
)
857 buf
[ooboffs
+ td
->veroffs
] = td
->version
[chip
];
859 /* walk through the memory table */
860 for (i
= 0; i
< numblocks
;) {
862 dat
= this->bbt
[bbtoffs
+ (i
>> 2)];
863 for (j
= 0; j
< 4; j
++, i
++) {
864 int sftcnt
= (i
<< (3 - sft
)) & sftmsk
;
865 /* Do not store the reserved bbt blocks ! */
866 buf
[offs
+ (i
>> sft
)] &=
867 ~(msk
[dat
& 0x03] << sftcnt
);
872 memset(&einfo
, 0, sizeof(einfo
));
875 einfo
.len
= 1 << this->bbt_erase_shift
;
876 res
= nand_erase_nand(mtd
, &einfo
, 1);
880 res
= scan_write_bbt(mtd
, to
, len
, buf
,
881 td
->options
& NAND_BBT_NO_OOB
? NULL
:
886 printk(KERN_DEBUG
"Bad block table written to 0x%012llx, version "
887 "0x%02X\n", (unsigned long long)to
, td
->version
[chip
]);
889 /* Mark it as used */
890 td
->pages
[chip
] = page
;
896 "nand_bbt: Error while writing bad block table %d\n", res
);
901 * nand_memory_bbt - [GENERIC] create a memory based bad block table
902 * @mtd: MTD device structure
903 * @bd: descriptor for the good/bad block search pattern
905 * The function creates a memory based bbt by scanning the device
906 * for manufacturer / software marked good / bad blocks
908 static inline int nand_memory_bbt(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
910 struct nand_chip
*this = mtd
->priv
;
912 bd
->options
&= ~NAND_BBT_SCANEMPTY
;
913 return create_bbt(mtd
, this->buffers
->databuf
, bd
, -1);
917 * check_create - [GENERIC] create and write bbt(s) if necessary
918 * @mtd: MTD device structure
919 * @buf: temporary buffer
920 * @bd: descriptor for the good/bad block search pattern
922 * The function checks the results of the previous call to read_bbt
923 * and creates / updates the bbt(s) if necessary
924 * Creation is necessary if no bbt was found for the chip/device
925 * Update is necessary if one of the tables is missing or the
926 * version nr. of one table is less than the other
928 static int check_create(struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*bd
)
930 int i
, chips
, writeops
, chipsel
, res
;
931 struct nand_chip
*this = mtd
->priv
;
932 struct nand_bbt_descr
*td
= this->bbt_td
;
933 struct nand_bbt_descr
*md
= this->bbt_md
;
934 struct nand_bbt_descr
*rd
, *rd2
;
936 /* Do we have a bbt per chip ? */
937 if (td
->options
& NAND_BBT_PERCHIP
)
938 chips
= this->numchips
;
942 for (i
= 0; i
< chips
; i
++) {
946 /* Per chip or per device ? */
947 chipsel
= (td
->options
& NAND_BBT_PERCHIP
) ? i
: -1;
948 /* Mirrored table avilable ? */
950 if (td
->pages
[i
] == -1 && md
->pages
[i
] == -1) {
955 if (td
->pages
[i
] == -1) {
957 td
->version
[i
] = md
->version
[i
];
962 if (md
->pages
[i
] == -1) {
964 md
->version
[i
] = td
->version
[i
];
969 if (td
->version
[i
] == md
->version
[i
]) {
971 if (!(td
->options
& NAND_BBT_VERSION
))
976 if (((int8_t) (td
->version
[i
] - md
->version
[i
])) > 0) {
978 md
->version
[i
] = td
->version
[i
];
982 td
->version
[i
] = md
->version
[i
];
989 if (td
->pages
[i
] == -1) {
997 /* Create the bad block table by scanning the device ? */
998 if (!(td
->options
& NAND_BBT_CREATE
))
1001 /* Create the table in memory by scanning the chip(s) */
1002 if (!(this->options
& NAND_CREATE_EMPTY_BBT
))
1003 create_bbt(mtd
, buf
, bd
, chipsel
);
1009 /* read back first ? */
1011 read_abs_bbt(mtd
, buf
, rd
, chipsel
);
1012 /* If they weren't versioned, read both. */
1014 read_abs_bbt(mtd
, buf
, rd2
, chipsel
);
1016 /* Write the bad block table to the device ? */
1017 if ((writeops
& 0x01) && (td
->options
& NAND_BBT_WRITE
)) {
1018 res
= write_bbt(mtd
, buf
, td
, md
, chipsel
);
1023 /* Write the mirror bad block table to the device ? */
1024 if ((writeops
& 0x02) && md
&& (md
->options
& NAND_BBT_WRITE
)) {
1025 res
= write_bbt(mtd
, buf
, md
, td
, chipsel
);
1034 * mark_bbt_regions - [GENERIC] mark the bad block table regions
1035 * @mtd: MTD device structure
1036 * @td: bad block table descriptor
1038 * The bad block table regions are marked as "bad" to prevent
1039 * accidental erasures / writes. The regions are identified by
1042 static void mark_bbt_region(struct mtd_info
*mtd
, struct nand_bbt_descr
*td
)
1044 struct nand_chip
*this = mtd
->priv
;
1045 int i
, j
, chips
, block
, nrblocks
, update
;
1046 uint8_t oldval
, newval
;
1048 /* Do we have a bbt per chip ? */
1049 if (td
->options
& NAND_BBT_PERCHIP
) {
1050 chips
= this->numchips
;
1051 nrblocks
= (int)(this->chipsize
>> this->bbt_erase_shift
);
1054 nrblocks
= (int)(mtd
->size
>> this->bbt_erase_shift
);
1057 for (i
= 0; i
< chips
; i
++) {
1058 if ((td
->options
& NAND_BBT_ABSPAGE
) ||
1059 !(td
->options
& NAND_BBT_WRITE
)) {
1060 if (td
->pages
[i
] == -1)
1062 block
= td
->pages
[i
] >> (this->bbt_erase_shift
- this->page_shift
);
1064 oldval
= this->bbt
[(block
>> 3)];
1065 newval
= oldval
| (0x2 << (block
& 0x06));
1066 this->bbt
[(block
>> 3)] = newval
;
1067 if ((oldval
!= newval
) && td
->reserved_block_code
)
1068 nand_update_bbt(mtd
, (loff_t
)block
<< (this->bbt_erase_shift
- 1));
1072 if (td
->options
& NAND_BBT_LASTBLOCK
)
1073 block
= ((i
+ 1) * nrblocks
) - td
->maxblocks
;
1075 block
= i
* nrblocks
;
1077 for (j
= 0; j
< td
->maxblocks
; j
++) {
1078 oldval
= this->bbt
[(block
>> 3)];
1079 newval
= oldval
| (0x2 << (block
& 0x06));
1080 this->bbt
[(block
>> 3)] = newval
;
1081 if (oldval
!= newval
)
1085 /* If we want reserved blocks to be recorded to flash, and some
1086 new ones have been marked, then we need to update the stored
1087 bbts. This should only happen once. */
1088 if (update
&& td
->reserved_block_code
)
1089 nand_update_bbt(mtd
, (loff_t
)(block
- 2) << (this->bbt_erase_shift
- 1));
1094 * verify_bbt_descr - verify the bad block description
1095 * @mtd: MTD device structure
1096 * @bd: the table to verify
1098 * This functions performs a few sanity checks on the bad block description
1101 static void verify_bbt_descr(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
1103 struct nand_chip
*this = mtd
->priv
;
1104 u32 pattern_len
= bd
->len
;
1105 u32 bits
= bd
->options
& NAND_BBT_NRBITS_MSK
;
1110 BUG_ON((this->options
& NAND_USE_FLASH_BBT_NO_OOB
) &&
1111 !(this->options
& NAND_USE_FLASH_BBT
));
1114 if (bd
->options
& NAND_BBT_VERSION
)
1117 if (bd
->options
& NAND_BBT_NO_OOB
) {
1118 BUG_ON(!(this->options
& NAND_USE_FLASH_BBT
));
1119 BUG_ON(!(this->options
& NAND_USE_FLASH_BBT_NO_OOB
));
1121 if (bd
->options
& NAND_BBT_VERSION
)
1122 BUG_ON(bd
->veroffs
!= bd
->len
);
1123 BUG_ON(bd
->options
& NAND_BBT_SAVECONTENT
);
1126 if (bd
->options
& NAND_BBT_PERCHIP
)
1127 table_size
= this->chipsize
>> this->bbt_erase_shift
;
1129 table_size
= mtd
->size
>> this->bbt_erase_shift
;
1132 if (bd
->options
& NAND_BBT_NO_OOB
)
1133 table_size
+= pattern_len
;
1134 BUG_ON(table_size
> (1 << this->bbt_erase_shift
));
1138 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1139 * @mtd: MTD device structure
1140 * @bd: descriptor for the good/bad block search pattern
1142 * The function checks, if a bad block table(s) is/are already
1143 * available. If not it scans the device for manufacturer
1144 * marked good / bad blocks and writes the bad block table(s) to
1145 * the selected place.
1147 * The bad block table memory is allocated here. It must be freed
1148 * by calling the nand_free_bbt function.
1151 int nand_scan_bbt(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
1153 struct nand_chip
*this = mtd
->priv
;
1156 struct nand_bbt_descr
*td
= this->bbt_td
;
1157 struct nand_bbt_descr
*md
= this->bbt_md
;
1159 len
= mtd
->size
>> (this->bbt_erase_shift
+ 2);
1160 /* Allocate memory (2bit per block) and clear the memory bad block table */
1161 this->bbt
= kzalloc(len
, GFP_KERNEL
);
1163 printk(KERN_ERR
"nand_scan_bbt: Out of memory\n");
1167 /* If no primary table decriptor is given, scan the device
1168 * to build a memory based bad block table
1171 if ((res
= nand_memory_bbt(mtd
, bd
))) {
1172 printk(KERN_ERR
"nand_bbt: Can't scan flash and build the RAM-based BBT\n");
1178 verify_bbt_descr(mtd
, td
);
1179 verify_bbt_descr(mtd
, md
);
1181 /* Allocate a temporary buffer for one eraseblock incl. oob */
1182 len
= (1 << this->bbt_erase_shift
);
1183 len
+= (len
>> this->page_shift
) * mtd
->oobsize
;
1186 printk(KERN_ERR
"nand_bbt: Out of memory\n");
1192 /* Is the bbt at a given page ? */
1193 if (td
->options
& NAND_BBT_ABSPAGE
) {
1194 res
= read_abs_bbts(mtd
, buf
, td
, md
);
1196 /* Search the bad block table using a pattern in oob */
1197 res
= search_read_bbts(mtd
, buf
, td
, md
);
1201 res
= check_create(mtd
, buf
, bd
);
1203 /* Prevent the bbt regions from erasing / writing */
1204 mark_bbt_region(mtd
, td
);
1206 mark_bbt_region(mtd
, md
);
1213 * nand_update_bbt - [NAND Interface] update bad block table(s)
1214 * @mtd: MTD device structure
1215 * @offs: the offset of the newly marked block
1217 * The function updates the bad block table(s)
1219 int nand_update_bbt(struct mtd_info
*mtd
, loff_t offs
)
1221 struct nand_chip
*this = mtd
->priv
;
1222 int len
, res
= 0, writeops
= 0;
1225 struct nand_bbt_descr
*td
= this->bbt_td
;
1226 struct nand_bbt_descr
*md
= this->bbt_md
;
1228 if (!this->bbt
|| !td
)
1231 /* Allocate a temporary buffer for one eraseblock incl. oob */
1232 len
= (1 << this->bbt_erase_shift
);
1233 len
+= (len
>> this->page_shift
) * mtd
->oobsize
;
1234 buf
= kmalloc(len
, GFP_KERNEL
);
1236 printk(KERN_ERR
"nand_update_bbt: Out of memory\n");
1240 writeops
= md
!= NULL
? 0x03 : 0x01;
1242 /* Do we have a bbt per chip ? */
1243 if (td
->options
& NAND_BBT_PERCHIP
) {
1244 chip
= (int)(offs
>> this->chip_shift
);
1251 td
->version
[chip
]++;
1253 md
->version
[chip
]++;
1255 /* Write the bad block table to the device ? */
1256 if ((writeops
& 0x01) && (td
->options
& NAND_BBT_WRITE
)) {
1257 res
= write_bbt(mtd
, buf
, td
, md
, chipsel
);
1261 /* Write the mirror bad block table to the device ? */
1262 if ((writeops
& 0x02) && md
&& (md
->options
& NAND_BBT_WRITE
)) {
1263 res
= write_bbt(mtd
, buf
, md
, td
, chipsel
);
1271 /* Define some generic bad / good block scan pattern which are used
1272 * while scanning a device for factory marked good / bad blocks. */
1273 static uint8_t scan_ff_pattern
[] = { 0xff, 0xff };
1275 static struct nand_bbt_descr smallpage_flashbased
= {
1276 .options
= NAND_BBT_SCAN2NDPAGE
,
1277 .offs
= NAND_SMALL_BADBLOCK_POS
,
1279 .pattern
= scan_ff_pattern
1282 static struct nand_bbt_descr largepage_flashbased
= {
1283 .options
= NAND_BBT_SCAN2NDPAGE
,
1284 .offs
= NAND_LARGE_BADBLOCK_POS
,
1286 .pattern
= scan_ff_pattern
1289 static uint8_t scan_agand_pattern
[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1291 static struct nand_bbt_descr agand_flashbased
= {
1292 .options
= NAND_BBT_SCANEMPTY
| NAND_BBT_SCANALLPAGES
,
1295 .pattern
= scan_agand_pattern
1298 /* Generic flash bbt decriptors
1300 static uint8_t bbt_pattern
[] = {'B', 'b', 't', '0' };
1301 static uint8_t mirror_pattern
[] = {'1', 't', 'b', 'B' };
1303 static struct nand_bbt_descr bbt_main_descr
= {
1304 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1305 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
1310 .pattern
= bbt_pattern
1313 static struct nand_bbt_descr bbt_mirror_descr
= {
1314 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1315 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
1320 .pattern
= mirror_pattern
1323 static struct nand_bbt_descr bbt_main_no_bbt_descr
= {
1324 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1325 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
1330 .pattern
= bbt_pattern
1333 static struct nand_bbt_descr bbt_mirror_no_bbt_descr
= {
1334 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1335 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
1340 .pattern
= mirror_pattern
1343 #define BBT_SCAN_OPTIONS (NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE | \
1344 NAND_BBT_SCANBYTE1AND6)
1346 * nand_create_default_bbt_descr - [Internal] Creates a BBT descriptor structure
1347 * @this: NAND chip to create descriptor for
1349 * This function allocates and initializes a nand_bbt_descr for BBM detection
1350 * based on the properties of "this". The new descriptor is stored in
1351 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1352 * passed to this function.
1354 * TODO: Handle other flags, replace other static structs
1355 * (e.g. handle NAND_BBT_FLASH for flash-based BBT,
1356 * replace smallpage_flashbased)
1359 static int nand_create_default_bbt_descr(struct nand_chip
*this)
1361 struct nand_bbt_descr
*bd
;
1362 if (this->badblock_pattern
) {
1363 printk(KERN_WARNING
"BBT descr already allocated; not replacing.\n");
1366 bd
= kzalloc(sizeof(*bd
), GFP_KERNEL
);
1368 printk(KERN_ERR
"nand_create_default_bbt_descr: Out of memory\n");
1371 bd
->options
= this->options
& BBT_SCAN_OPTIONS
;
1372 bd
->offs
= this->badblockpos
;
1373 bd
->len
= (this->options
& NAND_BUSWIDTH_16
) ? 2 : 1;
1374 bd
->pattern
= scan_ff_pattern
;
1375 bd
->options
|= NAND_BBT_DYNAMICSTRUCT
;
1376 this->badblock_pattern
= bd
;
1381 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
1382 * @mtd: MTD device structure
1384 * This function selects the default bad block table
1385 * support for the device and calls the nand_scan_bbt function
1388 int nand_default_bbt(struct mtd_info
*mtd
)
1390 struct nand_chip
*this = mtd
->priv
;
1392 /* Default for AG-AND. We must use a flash based
1393 * bad block table as the devices have factory marked
1394 * _good_ blocks. Erasing those blocks leads to loss
1395 * of the good / bad information, so we _must_ store
1396 * this information in a good / bad table during
1399 if (this->options
& NAND_IS_AND
) {
1400 /* Use the default pattern descriptors */
1401 if (!this->bbt_td
) {
1402 this->bbt_td
= &bbt_main_descr
;
1403 this->bbt_md
= &bbt_mirror_descr
;
1405 this->options
|= NAND_USE_FLASH_BBT
;
1406 return nand_scan_bbt(mtd
, &agand_flashbased
);
1409 /* Is a flash based bad block table requested ? */
1410 if (this->options
& NAND_USE_FLASH_BBT
) {
1411 /* Use the default pattern descriptors */
1412 if (!this->bbt_td
) {
1413 if (this->options
& NAND_USE_FLASH_BBT_NO_OOB
) {
1414 this->bbt_td
= &bbt_main_no_bbt_descr
;
1415 this->bbt_md
= &bbt_mirror_no_bbt_descr
;
1417 this->bbt_td
= &bbt_main_descr
;
1418 this->bbt_md
= &bbt_mirror_descr
;
1421 if (!this->badblock_pattern
) {
1422 this->badblock_pattern
= (mtd
->writesize
> 512) ? &largepage_flashbased
: &smallpage_flashbased
;
1425 this->bbt_td
= NULL
;
1426 this->bbt_md
= NULL
;
1427 if (!this->badblock_pattern
)
1428 nand_create_default_bbt_descr(this);
1430 return nand_scan_bbt(mtd
, this->badblock_pattern
);
1434 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1435 * @mtd: MTD device structure
1436 * @offs: offset in the device
1437 * @allowbbt: allow access to bad block table region
1440 int nand_isbad_bbt(struct mtd_info
*mtd
, loff_t offs
, int allowbbt
)
1442 struct nand_chip
*this = mtd
->priv
;
1446 /* Get block number * 2 */
1447 block
= (int)(offs
>> (this->bbt_erase_shift
- 1));
1448 res
= (this->bbt
[block
>> 3] >> (block
& 0x06)) & 0x03;
1450 DEBUG(MTD_DEBUG_LEVEL2
, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1451 (unsigned int)offs
, block
>> 1, res
);
1459 return allowbbt
? 0 : 1;
1464 EXPORT_SYMBOL(nand_scan_bbt
);
1465 EXPORT_SYMBOL(nand_default_bbt
);