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>
70 #include <linux/export.h>
72 static int check_pattern_no_oob(uint8_t *buf
, struct nand_bbt_descr
*td
)
76 ret
= memcmp(buf
, td
->pattern
, td
->len
);
83 * check_pattern - [GENERIC] check if a pattern is in the buffer
84 * @buf: the buffer to search
85 * @len: the length of buffer to search
86 * @paglen: the pagelength
87 * @td: search pattern descriptor
89 * Check for a pattern at the given place. Used to search bad block
90 * tables and good / bad block identifiers.
91 * If the SCAN_EMPTY option is set then check, if all bytes except the
92 * pattern area contain 0xff
95 static int check_pattern(uint8_t *buf
, int len
, int paglen
, struct nand_bbt_descr
*td
)
100 if (td
->options
& NAND_BBT_NO_OOB
)
101 return check_pattern_no_oob(buf
, td
);
103 end
= paglen
+ td
->offs
;
104 if (td
->options
& NAND_BBT_SCANEMPTY
) {
105 for (i
= 0; i
< end
; i
++) {
112 /* Compare the pattern */
113 for (i
= 0; i
< td
->len
; i
++) {
114 if (p
[i
] != td
->pattern
[i
])
118 /* Check both positions 1 and 6 for pattern? */
119 if (td
->options
& NAND_BBT_SCANBYTE1AND6
) {
120 if (td
->options
& NAND_BBT_SCANEMPTY
) {
122 end
+= NAND_SMALL_BADBLOCK_POS
- td
->offs
;
123 /* Check region between positions 1 and 6 */
124 for (i
= 0; i
< NAND_SMALL_BADBLOCK_POS
- td
->offs
- td
->len
;
131 p
+= NAND_SMALL_BADBLOCK_POS
- td
->offs
;
133 /* Compare the pattern */
134 for (i
= 0; i
< td
->len
; i
++) {
135 if (p
[i
] != td
->pattern
[i
])
140 if (td
->options
& NAND_BBT_SCANEMPTY
) {
143 for (i
= end
; i
< len
; i
++) {
152 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
153 * @buf: the buffer to search
154 * @td: search pattern descriptor
156 * Check for a pattern at the given place. Used to search bad block
157 * tables and good / bad block identifiers. Same as check_pattern, but
158 * no optional empty check
161 static int check_short_pattern(uint8_t *buf
, struct nand_bbt_descr
*td
)
166 /* Compare the pattern */
167 for (i
= 0; i
< td
->len
; i
++) {
168 if (p
[td
->offs
+ i
] != td
->pattern
[i
])
171 /* Need to check location 1 AND 6? */
172 if (td
->options
& NAND_BBT_SCANBYTE1AND6
) {
173 for (i
= 0; i
< td
->len
; i
++) {
174 if (p
[NAND_SMALL_BADBLOCK_POS
+ i
] != td
->pattern
[i
])
182 * add_marker_len - compute the length of the marker in data area
183 * @td: BBT descriptor used for computation
185 * The length will be 0 if the markeris located in OOB area.
187 static u32
add_marker_len(struct nand_bbt_descr
*td
)
191 if (!(td
->options
& NAND_BBT_NO_OOB
))
195 if (td
->options
& NAND_BBT_VERSION
)
201 * read_bbt - [GENERIC] Read the bad block table starting from page
202 * @mtd: MTD device structure
203 * @buf: temporary buffer
204 * @page: the starting page
205 * @num: the number of bbt descriptors to read
206 * @td: the bbt describtion table
207 * @offs: offset in the memory table
209 * Read the bad block table starting from page.
212 static int read_bbt(struct mtd_info
*mtd
, uint8_t *buf
, int page
, int num
,
213 struct nand_bbt_descr
*td
, int offs
)
215 int res
, i
, j
, act
= 0;
216 struct nand_chip
*this = mtd
->priv
;
217 size_t retlen
, len
, totlen
;
219 int bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
220 uint8_t msk
= (uint8_t) ((1 << bits
) - 1);
222 int reserved_block_code
= td
->reserved_block_code
;
224 totlen
= (num
* bits
) >> 3;
225 marker_len
= add_marker_len(td
);
226 from
= ((loff_t
) page
) << this->page_shift
;
229 len
= min(totlen
, (size_t) (1 << this->bbt_erase_shift
));
232 * In case the BBT marker is not in the OOB area it
233 * will be just in the first page.
239 res
= mtd
->read(mtd
, from
, len
, &retlen
, buf
);
242 printk(KERN_INFO
"nand_bbt: Error reading bad block table\n");
245 printk(KERN_WARNING
"nand_bbt: ECC error while reading bad block table\n");
249 for (i
= 0; i
< len
; i
++) {
250 uint8_t dat
= buf
[i
];
251 for (j
= 0; j
< 8; j
+= bits
, act
+= 2) {
252 uint8_t tmp
= (dat
>> j
) & msk
;
255 if (reserved_block_code
&& (tmp
== reserved_block_code
)) {
256 printk(KERN_DEBUG
"nand_read_bbt: Reserved block at 0x%012llx\n",
257 (loff_t
)((offs
<< 2) + (act
>> 1)) << this->bbt_erase_shift
);
258 this->bbt
[offs
+ (act
>> 3)] |= 0x2 << (act
& 0x06);
259 mtd
->ecc_stats
.bbtblocks
++;
262 /* Leave it for now, if its matured we can move this
263 * message to MTD_DEBUG_LEVEL0 */
264 printk(KERN_DEBUG
"nand_read_bbt: Bad block at 0x%012llx\n",
265 (loff_t
)((offs
<< 2) + (act
>> 1)) << this->bbt_erase_shift
);
266 /* Factory marked bad or worn out ? */
268 this->bbt
[offs
+ (act
>> 3)] |= 0x3 << (act
& 0x06);
270 this->bbt
[offs
+ (act
>> 3)] |= 0x1 << (act
& 0x06);
271 mtd
->ecc_stats
.badblocks
++;
281 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
282 * @mtd: MTD device structure
283 * @buf: temporary buffer
284 * @td: descriptor for the bad block table
285 * @chip: read the table for a specific chip, -1 read all chips.
286 * Applies only if NAND_BBT_PERCHIP option is set
288 * Read the bad block table for all chips starting at a given page
289 * We assume that the bbt bits are in consecutive order.
291 static int read_abs_bbt(struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
, int chip
)
293 struct nand_chip
*this = mtd
->priv
;
296 if (td
->options
& NAND_BBT_PERCHIP
) {
298 for (i
= 0; i
< this->numchips
; i
++) {
299 if (chip
== -1 || chip
== i
)
300 res
= read_bbt(mtd
, buf
, td
->pages
[i
],
301 this->chipsize
>> this->bbt_erase_shift
,
305 offs
+= this->chipsize
>> (this->bbt_erase_shift
+ 2);
308 res
= read_bbt(mtd
, buf
, td
->pages
[0],
309 mtd
->size
>> this->bbt_erase_shift
, td
, 0);
317 * BBT marker is in the first page, no OOB.
319 static int scan_read_raw_data(struct mtd_info
*mtd
, uint8_t *buf
, loff_t offs
,
320 struct nand_bbt_descr
*td
)
326 if (td
->options
& NAND_BBT_VERSION
)
329 return mtd
->read(mtd
, offs
, len
, &retlen
, buf
);
333 * Scan read raw data from flash
335 static int scan_read_raw_oob(struct mtd_info
*mtd
, uint8_t *buf
, loff_t offs
,
338 struct mtd_oob_ops ops
;
341 ops
.mode
= MTD_OOB_RAW
;
343 ops
.ooblen
= mtd
->oobsize
;
347 if (len
<= mtd
->writesize
) {
348 ops
.oobbuf
= buf
+ len
;
351 return mtd
->read_oob(mtd
, offs
, &ops
);
353 ops
.oobbuf
= buf
+ mtd
->writesize
;
355 ops
.len
= mtd
->writesize
;
356 res
= mtd
->read_oob(mtd
, offs
, &ops
);
362 buf
+= mtd
->oobsize
+ mtd
->writesize
;
363 len
-= mtd
->writesize
;
368 static int scan_read_raw(struct mtd_info
*mtd
, uint8_t *buf
, loff_t offs
,
369 size_t len
, struct nand_bbt_descr
*td
)
371 if (td
->options
& NAND_BBT_NO_OOB
)
372 return scan_read_raw_data(mtd
, buf
, offs
, td
);
374 return scan_read_raw_oob(mtd
, buf
, offs
, len
);
378 * Scan write data with oob to flash
380 static int scan_write_bbt(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
381 uint8_t *buf
, uint8_t *oob
)
383 struct mtd_oob_ops ops
;
385 ops
.mode
= MTD_OOB_PLACE
;
387 ops
.ooblen
= mtd
->oobsize
;
392 return mtd
->write_oob(mtd
, offs
, &ops
);
395 static u32
bbt_get_ver_offs(struct mtd_info
*mtd
, struct nand_bbt_descr
*td
)
397 u32 ver_offs
= td
->veroffs
;
399 if (!(td
->options
& NAND_BBT_NO_OOB
))
400 ver_offs
+= mtd
->writesize
;
405 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
406 * @mtd: MTD device structure
407 * @buf: temporary buffer
408 * @td: descriptor for the bad block table
409 * @md: descriptor for the bad block table mirror
411 * Read the bad block table(s) for all chips starting at a given page
412 * We assume that the bbt bits are in consecutive order.
415 static int read_abs_bbts(struct mtd_info
*mtd
, uint8_t *buf
,
416 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
)
418 struct nand_chip
*this = mtd
->priv
;
420 /* Read the primary version, if available */
421 if (td
->options
& NAND_BBT_VERSION
) {
422 scan_read_raw(mtd
, buf
, (loff_t
)td
->pages
[0] << this->page_shift
,
424 td
->version
[0] = buf
[bbt_get_ver_offs(mtd
, td
)];
425 printk(KERN_DEBUG
"Bad block table at page %d, version 0x%02X\n",
426 td
->pages
[0], td
->version
[0]);
429 /* Read the mirror version, if available */
430 if (md
&& (md
->options
& NAND_BBT_VERSION
)) {
431 scan_read_raw(mtd
, buf
, (loff_t
)md
->pages
[0] << this->page_shift
,
433 md
->version
[0] = buf
[bbt_get_ver_offs(mtd
, md
)];
434 printk(KERN_DEBUG
"Bad block table at page %d, version 0x%02X\n",
435 md
->pages
[0], md
->version
[0]);
441 * Scan a given block full
443 static int scan_block_full(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
,
444 loff_t offs
, uint8_t *buf
, size_t readlen
,
445 int scanlen
, int len
)
449 ret
= scan_read_raw_oob(mtd
, buf
, offs
, readlen
);
453 for (j
= 0; j
< len
; j
++, buf
+= scanlen
) {
454 if (check_pattern(buf
, scanlen
, mtd
->writesize
, bd
))
461 * Scan a given block partially
463 static int scan_block_fast(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
,
464 loff_t offs
, uint8_t *buf
, int len
)
466 struct mtd_oob_ops ops
;
469 ops
.ooblen
= mtd
->oobsize
;
473 ops
.mode
= MTD_OOB_PLACE
;
475 for (j
= 0; j
< len
; j
++) {
477 * Read the full oob until read_oob is fixed to
478 * handle single byte reads for 16 bit
481 ret
= mtd
->read_oob(mtd
, offs
, &ops
);
485 if (check_short_pattern(buf
, bd
))
488 offs
+= mtd
->writesize
;
494 * create_bbt - [GENERIC] Create a bad block table by scanning the device
495 * @mtd: MTD device structure
496 * @buf: temporary buffer
497 * @bd: descriptor for the good/bad block search pattern
498 * @chip: create the table for a specific chip, -1 read all chips.
499 * Applies only if NAND_BBT_PERCHIP option is set
501 * Create a bad block table by scanning the device
502 * for the given good/bad block identify pattern
504 static int create_bbt(struct mtd_info
*mtd
, uint8_t *buf
,
505 struct nand_bbt_descr
*bd
, int chip
)
507 struct nand_chip
*this = mtd
->priv
;
508 int i
, numblocks
, len
, scanlen
;
513 printk(KERN_INFO
"Scanning device for bad blocks\n");
515 if (bd
->options
& NAND_BBT_SCANALLPAGES
)
516 len
= 1 << (this->bbt_erase_shift
- this->page_shift
);
517 else if (bd
->options
& NAND_BBT_SCAN2NDPAGE
)
522 if (!(bd
->options
& NAND_BBT_SCANEMPTY
)) {
523 /* We need only read few bytes from the OOB area */
527 /* Full page content should be read */
528 scanlen
= mtd
->writesize
+ mtd
->oobsize
;
529 readlen
= len
* mtd
->writesize
;
533 /* Note that numblocks is 2 * (real numblocks) here, see i+=2
534 * below as it makes shifting and masking less painful */
535 numblocks
= mtd
->size
>> (this->bbt_erase_shift
- 1);
539 if (chip
>= this->numchips
) {
540 printk(KERN_WARNING
"create_bbt(): chipnr (%d) > available chips (%d)\n",
541 chip
+ 1, this->numchips
);
544 numblocks
= this->chipsize
>> (this->bbt_erase_shift
- 1);
545 startblock
= chip
* numblocks
;
546 numblocks
+= startblock
;
547 from
= (loff_t
)startblock
<< (this->bbt_erase_shift
- 1);
550 if (this->options
& NAND_BBT_SCANLASTPAGE
)
551 from
+= mtd
->erasesize
- (mtd
->writesize
* len
);
553 for (i
= startblock
; i
< numblocks
;) {
556 BUG_ON(bd
->options
& NAND_BBT_NO_OOB
);
558 if (bd
->options
& NAND_BBT_SCANALLPAGES
)
559 ret
= scan_block_full(mtd
, bd
, from
, buf
, readlen
,
562 ret
= scan_block_fast(mtd
, bd
, from
, buf
, len
);
568 this->bbt
[i
>> 3] |= 0x03 << (i
& 0x6);
569 printk(KERN_WARNING
"Bad eraseblock %d at 0x%012llx\n",
570 i
>> 1, (unsigned long long)from
);
571 mtd
->ecc_stats
.badblocks
++;
575 from
+= (1 << this->bbt_erase_shift
);
581 * search_bbt - [GENERIC] scan the device for a specific bad block table
582 * @mtd: MTD device structure
583 * @buf: temporary buffer
584 * @td: descriptor for the bad block table
586 * Read the bad block table by searching for a given ident pattern.
587 * Search is preformed either from the beginning up or from the end of
588 * the device downwards. The search starts always at the start of a
590 * If the option NAND_BBT_PERCHIP is given, each chip is searched
591 * for a bbt, which contains the bad block information of this chip.
592 * This is necessary to provide support for certain DOC devices.
594 * The bbt ident pattern resides in the oob area of the first page
597 static int search_bbt(struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
)
599 struct nand_chip
*this = mtd
->priv
;
601 int bits
, startblock
, block
, dir
;
602 int scanlen
= mtd
->writesize
+ mtd
->oobsize
;
604 int blocktopage
= this->bbt_erase_shift
- this->page_shift
;
606 /* Search direction top -> down ? */
607 if (td
->options
& NAND_BBT_LASTBLOCK
) {
608 startblock
= (mtd
->size
>> this->bbt_erase_shift
) - 1;
615 /* Do we have a bbt per chip ? */
616 if (td
->options
& NAND_BBT_PERCHIP
) {
617 chips
= this->numchips
;
618 bbtblocks
= this->chipsize
>> this->bbt_erase_shift
;
619 startblock
&= bbtblocks
- 1;
622 bbtblocks
= mtd
->size
>> this->bbt_erase_shift
;
625 /* Number of bits for each erase block in the bbt */
626 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
628 for (i
= 0; i
< chips
; i
++) {
629 /* Reset version information */
632 /* Scan the maximum number of blocks */
633 for (block
= 0; block
< td
->maxblocks
; block
++) {
635 int actblock
= startblock
+ dir
* block
;
636 loff_t offs
= (loff_t
)actblock
<< this->bbt_erase_shift
;
638 /* Read first page */
639 scan_read_raw(mtd
, buf
, offs
, mtd
->writesize
, td
);
640 if (!check_pattern(buf
, scanlen
, mtd
->writesize
, td
)) {
641 td
->pages
[i
] = actblock
<< blocktopage
;
642 if (td
->options
& NAND_BBT_VERSION
) {
643 offs
= bbt_get_ver_offs(mtd
, td
);
644 td
->version
[i
] = buf
[offs
];
649 startblock
+= this->chipsize
>> this->bbt_erase_shift
;
651 /* Check, if we found a bbt for each requested chip */
652 for (i
= 0; i
< chips
; i
++) {
653 if (td
->pages
[i
] == -1)
654 printk(KERN_WARNING
"Bad block table not found for chip %d\n", i
);
656 printk(KERN_DEBUG
"Bad block table found at page %d, version 0x%02X\n", td
->pages
[i
],
663 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
664 * @mtd: MTD device structure
665 * @buf: temporary buffer
666 * @td: descriptor for the bad block table
667 * @md: descriptor for the bad block table mirror
669 * Search and read the bad block table(s)
671 static int search_read_bbts(struct mtd_info
*mtd
, uint8_t * buf
, struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
)
673 /* Search the primary table */
674 search_bbt(mtd
, buf
, td
);
676 /* Search the mirror table */
678 search_bbt(mtd
, buf
, md
);
680 /* Force result check */
685 * write_bbt - [GENERIC] (Re)write the bad block table
687 * @mtd: MTD device structure
688 * @buf: temporary buffer
689 * @td: descriptor for the bad block table
690 * @md: descriptor for the bad block table mirror
691 * @chipsel: selector for a specific chip, -1 for all
693 * (Re)write the bad block table
696 static int write_bbt(struct mtd_info
*mtd
, uint8_t *buf
,
697 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
,
700 struct nand_chip
*this = mtd
->priv
;
701 struct erase_info einfo
;
702 int i
, j
, res
, chip
= 0;
703 int bits
, startblock
, dir
, page
, offs
, numblocks
, sft
, sftmsk
;
704 int nrchips
, bbtoffs
, pageoffs
, ooboffs
;
706 uint8_t rcode
= td
->reserved_block_code
;
707 size_t retlen
, len
= 0;
709 struct mtd_oob_ops ops
;
711 ops
.ooblen
= mtd
->oobsize
;
714 ops
.mode
= MTD_OOB_PLACE
;
718 /* Write bad block table per chip rather than per device ? */
719 if (td
->options
& NAND_BBT_PERCHIP
) {
720 numblocks
= (int)(this->chipsize
>> this->bbt_erase_shift
);
721 /* Full device write or specific chip ? */
723 nrchips
= this->numchips
;
725 nrchips
= chipsel
+ 1;
729 numblocks
= (int)(mtd
->size
>> this->bbt_erase_shift
);
733 /* Loop through the chips */
734 for (; chip
< nrchips
; chip
++) {
736 /* There was already a version of the table, reuse the page
737 * This applies for absolute placement too, as we have the
738 * page nr. in td->pages.
740 if (td
->pages
[chip
] != -1) {
741 page
= td
->pages
[chip
];
745 /* Automatic placement of the bad block table */
746 /* Search direction top -> down ? */
747 if (td
->options
& NAND_BBT_LASTBLOCK
) {
748 startblock
= numblocks
* (chip
+ 1) - 1;
751 startblock
= chip
* numblocks
;
755 for (i
= 0; i
< td
->maxblocks
; i
++) {
756 int block
= startblock
+ dir
* i
;
757 /* Check, if the block is bad */
758 switch ((this->bbt
[block
>> 2] >>
759 (2 * (block
& 0x03))) & 0x03) {
765 (this->bbt_erase_shift
- this->page_shift
);
766 /* Check, if the block is used by the mirror table */
767 if (!md
|| md
->pages
[chip
] != page
)
770 printk(KERN_ERR
"No space left to write bad block table\n");
774 /* Set up shift count and masks for the flash table */
775 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
778 case 1: sft
= 3; sftmsk
= 0x07; msk
[0] = 0x00; msk
[1] = 0x01;
781 case 2: sft
= 2; sftmsk
= 0x06; msk
[0] = 0x00; msk
[1] = 0x01;
784 case 4: sft
= 1; sftmsk
= 0x04; msk
[0] = 0x00; msk
[1] = 0x0C;
787 case 8: sft
= 0; sftmsk
= 0x00; msk
[0] = 0x00; msk
[1] = 0x0F;
790 default: return -EINVAL
;
793 bbtoffs
= chip
* (numblocks
>> 2);
795 to
= ((loff_t
) page
) << this->page_shift
;
797 /* Must we save the block contents ? */
798 if (td
->options
& NAND_BBT_SAVECONTENT
) {
799 /* Make it block aligned */
800 to
&= ~((loff_t
) ((1 << this->bbt_erase_shift
) - 1));
801 len
= 1 << this->bbt_erase_shift
;
802 res
= mtd
->read(mtd
, to
, len
, &retlen
, buf
);
805 printk(KERN_INFO
"nand_bbt: Error "
806 "reading block for writing "
807 "the bad block table\n");
810 printk(KERN_WARNING
"nand_bbt: ECC error "
811 "while reading block for writing "
812 "bad block table\n");
815 ops
.ooblen
= (len
>> this->page_shift
) * mtd
->oobsize
;
816 ops
.oobbuf
= &buf
[len
];
817 res
= mtd
->read_oob(mtd
, to
+ mtd
->writesize
, &ops
);
818 if (res
< 0 || ops
.oobretlen
!= ops
.ooblen
)
821 /* Calc the byte offset in the buffer */
822 pageoffs
= page
- (int)(to
>> this->page_shift
);
823 offs
= pageoffs
<< this->page_shift
;
824 /* Preset the bbt area with 0xff */
825 memset(&buf
[offs
], 0xff, (size_t) (numblocks
>> sft
));
826 ooboffs
= len
+ (pageoffs
* mtd
->oobsize
);
828 } else if (td
->options
& NAND_BBT_NO_OOB
) {
831 /* the version byte */
832 if (td
->options
& NAND_BBT_VERSION
)
835 len
= (size_t) (numblocks
>> sft
);
837 /* Make it page aligned ! */
838 len
= ALIGN(len
, mtd
->writesize
);
839 /* Preset the buffer with 0xff */
840 memset(buf
, 0xff, len
);
841 /* Pattern is located at the begin of first page */
842 memcpy(buf
, td
->pattern
, td
->len
);
845 len
= (size_t) (numblocks
>> sft
);
846 /* Make it page aligned ! */
847 len
= ALIGN(len
, mtd
->writesize
);
848 /* Preset the buffer with 0xff */
849 memset(buf
, 0xff, len
+
850 (len
>> this->page_shift
)* mtd
->oobsize
);
853 /* Pattern is located in oob area of first page */
854 memcpy(&buf
[ooboffs
+ td
->offs
], td
->pattern
, td
->len
);
857 if (td
->options
& NAND_BBT_VERSION
)
858 buf
[ooboffs
+ td
->veroffs
] = td
->version
[chip
];
860 /* walk through the memory table */
861 for (i
= 0; i
< numblocks
;) {
863 dat
= this->bbt
[bbtoffs
+ (i
>> 2)];
864 for (j
= 0; j
< 4; j
++, i
++) {
865 int sftcnt
= (i
<< (3 - sft
)) & sftmsk
;
866 /* Do not store the reserved bbt blocks ! */
867 buf
[offs
+ (i
>> sft
)] &=
868 ~(msk
[dat
& 0x03] << sftcnt
);
873 memset(&einfo
, 0, sizeof(einfo
));
876 einfo
.len
= 1 << this->bbt_erase_shift
;
877 res
= nand_erase_nand(mtd
, &einfo
, 1);
881 res
= scan_write_bbt(mtd
, to
, len
, buf
,
882 td
->options
& NAND_BBT_NO_OOB
? NULL
:
887 printk(KERN_DEBUG
"Bad block table written to 0x%012llx, version "
888 "0x%02X\n", (unsigned long long)to
, td
->version
[chip
]);
890 /* Mark it as used */
891 td
->pages
[chip
] = page
;
897 "nand_bbt: Error while writing bad block table %d\n", res
);
902 * nand_memory_bbt - [GENERIC] create a memory based bad block table
903 * @mtd: MTD device structure
904 * @bd: descriptor for the good/bad block search pattern
906 * The function creates a memory based bbt by scanning the device
907 * for manufacturer / software marked good / bad blocks
909 static inline int nand_memory_bbt(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
911 struct nand_chip
*this = mtd
->priv
;
913 bd
->options
&= ~NAND_BBT_SCANEMPTY
;
914 return create_bbt(mtd
, this->buffers
->databuf
, bd
, -1);
918 * check_create - [GENERIC] create and write bbt(s) if necessary
919 * @mtd: MTD device structure
920 * @buf: temporary buffer
921 * @bd: descriptor for the good/bad block search pattern
923 * The function checks the results of the previous call to read_bbt
924 * and creates / updates the bbt(s) if necessary
925 * Creation is necessary if no bbt was found for the chip/device
926 * Update is necessary if one of the tables is missing or the
927 * version nr. of one table is less than the other
929 static int check_create(struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*bd
)
931 int i
, chips
, writeops
, chipsel
, res
;
932 struct nand_chip
*this = mtd
->priv
;
933 struct nand_bbt_descr
*td
= this->bbt_td
;
934 struct nand_bbt_descr
*md
= this->bbt_md
;
935 struct nand_bbt_descr
*rd
, *rd2
;
937 /* Do we have a bbt per chip ? */
938 if (td
->options
& NAND_BBT_PERCHIP
)
939 chips
= this->numchips
;
943 for (i
= 0; i
< chips
; i
++) {
947 /* Per chip or per device ? */
948 chipsel
= (td
->options
& NAND_BBT_PERCHIP
) ? i
: -1;
949 /* Mirrored table available ? */
951 if (td
->pages
[i
] == -1 && md
->pages
[i
] == -1) {
956 if (td
->pages
[i
] == -1) {
958 td
->version
[i
] = md
->version
[i
];
963 if (md
->pages
[i
] == -1) {
965 md
->version
[i
] = td
->version
[i
];
970 if (td
->version
[i
] == md
->version
[i
]) {
972 if (!(td
->options
& NAND_BBT_VERSION
))
977 if (((int8_t) (td
->version
[i
] - md
->version
[i
])) > 0) {
979 md
->version
[i
] = td
->version
[i
];
983 td
->version
[i
] = md
->version
[i
];
990 if (td
->pages
[i
] == -1) {
998 /* Create the bad block table by scanning the device ? */
999 if (!(td
->options
& NAND_BBT_CREATE
))
1002 /* Create the table in memory by scanning the chip(s) */
1003 if (!(this->options
& NAND_CREATE_EMPTY_BBT
))
1004 create_bbt(mtd
, buf
, bd
, chipsel
);
1010 /* read back first ? */
1012 read_abs_bbt(mtd
, buf
, rd
, chipsel
);
1013 /* If they weren't versioned, read both. */
1015 read_abs_bbt(mtd
, buf
, rd2
, chipsel
);
1017 /* Write the bad block table to the device ? */
1018 if ((writeops
& 0x01) && (td
->options
& NAND_BBT_WRITE
)) {
1019 res
= write_bbt(mtd
, buf
, td
, md
, chipsel
);
1024 /* Write the mirror bad block table to the device ? */
1025 if ((writeops
& 0x02) && md
&& (md
->options
& NAND_BBT_WRITE
)) {
1026 res
= write_bbt(mtd
, buf
, md
, td
, chipsel
);
1035 * mark_bbt_regions - [GENERIC] mark the bad block table regions
1036 * @mtd: MTD device structure
1037 * @td: bad block table descriptor
1039 * The bad block table regions are marked as "bad" to prevent
1040 * accidental erasures / writes. The regions are identified by
1043 static void mark_bbt_region(struct mtd_info
*mtd
, struct nand_bbt_descr
*td
)
1045 struct nand_chip
*this = mtd
->priv
;
1046 int i
, j
, chips
, block
, nrblocks
, update
;
1047 uint8_t oldval
, newval
;
1049 /* Do we have a bbt per chip ? */
1050 if (td
->options
& NAND_BBT_PERCHIP
) {
1051 chips
= this->numchips
;
1052 nrblocks
= (int)(this->chipsize
>> this->bbt_erase_shift
);
1055 nrblocks
= (int)(mtd
->size
>> this->bbt_erase_shift
);
1058 for (i
= 0; i
< chips
; i
++) {
1059 if ((td
->options
& NAND_BBT_ABSPAGE
) ||
1060 !(td
->options
& NAND_BBT_WRITE
)) {
1061 if (td
->pages
[i
] == -1)
1063 block
= td
->pages
[i
] >> (this->bbt_erase_shift
- this->page_shift
);
1065 oldval
= this->bbt
[(block
>> 3)];
1066 newval
= oldval
| (0x2 << (block
& 0x06));
1067 this->bbt
[(block
>> 3)] = newval
;
1068 if ((oldval
!= newval
) && td
->reserved_block_code
)
1069 nand_update_bbt(mtd
, (loff_t
)block
<< (this->bbt_erase_shift
- 1));
1073 if (td
->options
& NAND_BBT_LASTBLOCK
)
1074 block
= ((i
+ 1) * nrblocks
) - td
->maxblocks
;
1076 block
= i
* nrblocks
;
1078 for (j
= 0; j
< td
->maxblocks
; j
++) {
1079 oldval
= this->bbt
[(block
>> 3)];
1080 newval
= oldval
| (0x2 << (block
& 0x06));
1081 this->bbt
[(block
>> 3)] = newval
;
1082 if (oldval
!= newval
)
1086 /* If we want reserved blocks to be recorded to flash, and some
1087 new ones have been marked, then we need to update the stored
1088 bbts. This should only happen once. */
1089 if (update
&& td
->reserved_block_code
)
1090 nand_update_bbt(mtd
, (loff_t
)(block
- 2) << (this->bbt_erase_shift
- 1));
1095 * verify_bbt_descr - verify the bad block description
1096 * @mtd: MTD device structure
1097 * @bd: the table to verify
1099 * This functions performs a few sanity checks on the bad block description
1102 static void verify_bbt_descr(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
1104 struct nand_chip
*this = mtd
->priv
;
1112 pattern_len
= bd
->len
;
1113 bits
= bd
->options
& NAND_BBT_NRBITS_MSK
;
1115 BUG_ON((this->options
& NAND_USE_FLASH_BBT_NO_OOB
) &&
1116 !(this->options
& NAND_USE_FLASH_BBT
));
1119 if (bd
->options
& NAND_BBT_VERSION
)
1122 if (bd
->options
& NAND_BBT_NO_OOB
) {
1123 BUG_ON(!(this->options
& NAND_USE_FLASH_BBT
));
1124 BUG_ON(!(this->options
& NAND_USE_FLASH_BBT_NO_OOB
));
1126 if (bd
->options
& NAND_BBT_VERSION
)
1127 BUG_ON(bd
->veroffs
!= bd
->len
);
1128 BUG_ON(bd
->options
& NAND_BBT_SAVECONTENT
);
1131 if (bd
->options
& NAND_BBT_PERCHIP
)
1132 table_size
= this->chipsize
>> this->bbt_erase_shift
;
1134 table_size
= mtd
->size
>> this->bbt_erase_shift
;
1137 if (bd
->options
& NAND_BBT_NO_OOB
)
1138 table_size
+= pattern_len
;
1139 BUG_ON(table_size
> (1 << this->bbt_erase_shift
));
1143 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1144 * @mtd: MTD device structure
1145 * @bd: descriptor for the good/bad block search pattern
1147 * The function checks, if a bad block table(s) is/are already
1148 * available. If not it scans the device for manufacturer
1149 * marked good / bad blocks and writes the bad block table(s) to
1150 * the selected place.
1152 * The bad block table memory is allocated here. It must be freed
1153 * by calling the nand_free_bbt function.
1156 int nand_scan_bbt(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
1158 struct nand_chip
*this = mtd
->priv
;
1161 struct nand_bbt_descr
*td
= this->bbt_td
;
1162 struct nand_bbt_descr
*md
= this->bbt_md
;
1164 len
= mtd
->size
>> (this->bbt_erase_shift
+ 2);
1165 /* Allocate memory (2bit per block) and clear the memory bad block table */
1166 this->bbt
= kzalloc(len
, GFP_KERNEL
);
1168 printk(KERN_ERR
"nand_scan_bbt: Out of memory\n");
1172 /* If no primary table decriptor is given, scan the device
1173 * to build a memory based bad block table
1176 if ((res
= nand_memory_bbt(mtd
, bd
))) {
1177 printk(KERN_ERR
"nand_bbt: Can't scan flash and build the RAM-based BBT\n");
1183 verify_bbt_descr(mtd
, td
);
1184 verify_bbt_descr(mtd
, md
);
1186 /* Allocate a temporary buffer for one eraseblock incl. oob */
1187 len
= (1 << this->bbt_erase_shift
);
1188 len
+= (len
>> this->page_shift
) * mtd
->oobsize
;
1191 printk(KERN_ERR
"nand_bbt: Out of memory\n");
1197 /* Is the bbt at a given page ? */
1198 if (td
->options
& NAND_BBT_ABSPAGE
) {
1199 res
= read_abs_bbts(mtd
, buf
, td
, md
);
1201 /* Search the bad block table using a pattern in oob */
1202 res
= search_read_bbts(mtd
, buf
, td
, md
);
1206 res
= check_create(mtd
, buf
, bd
);
1208 /* Prevent the bbt regions from erasing / writing */
1209 mark_bbt_region(mtd
, td
);
1211 mark_bbt_region(mtd
, md
);
1218 * nand_update_bbt - [NAND Interface] update bad block table(s)
1219 * @mtd: MTD device structure
1220 * @offs: the offset of the newly marked block
1222 * The function updates the bad block table(s)
1224 int nand_update_bbt(struct mtd_info
*mtd
, loff_t offs
)
1226 struct nand_chip
*this = mtd
->priv
;
1227 int len
, res
= 0, writeops
= 0;
1230 struct nand_bbt_descr
*td
= this->bbt_td
;
1231 struct nand_bbt_descr
*md
= this->bbt_md
;
1233 if (!this->bbt
|| !td
)
1236 /* Allocate a temporary buffer for one eraseblock incl. oob */
1237 len
= (1 << this->bbt_erase_shift
);
1238 len
+= (len
>> this->page_shift
) * mtd
->oobsize
;
1239 buf
= kmalloc(len
, GFP_KERNEL
);
1241 printk(KERN_ERR
"nand_update_bbt: Out of memory\n");
1245 writeops
= md
!= NULL
? 0x03 : 0x01;
1247 /* Do we have a bbt per chip ? */
1248 if (td
->options
& NAND_BBT_PERCHIP
) {
1249 chip
= (int)(offs
>> this->chip_shift
);
1256 td
->version
[chip
]++;
1258 md
->version
[chip
]++;
1260 /* Write the bad block table to the device ? */
1261 if ((writeops
& 0x01) && (td
->options
& NAND_BBT_WRITE
)) {
1262 res
= write_bbt(mtd
, buf
, td
, md
, chipsel
);
1266 /* Write the mirror bad block table to the device ? */
1267 if ((writeops
& 0x02) && md
&& (md
->options
& NAND_BBT_WRITE
)) {
1268 res
= write_bbt(mtd
, buf
, md
, td
, chipsel
);
1276 /* Define some generic bad / good block scan pattern which are used
1277 * while scanning a device for factory marked good / bad blocks. */
1278 static uint8_t scan_ff_pattern
[] = { 0xff, 0xff };
1280 static uint8_t scan_agand_pattern
[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1282 static struct nand_bbt_descr agand_flashbased
= {
1283 .options
= NAND_BBT_SCANEMPTY
| NAND_BBT_SCANALLPAGES
,
1286 .pattern
= scan_agand_pattern
1289 /* Generic flash bbt decriptors
1291 static uint8_t bbt_pattern
[] = {'B', 'b', 't', '0' };
1292 static uint8_t mirror_pattern
[] = {'1', 't', 'b', 'B' };
1294 static struct nand_bbt_descr bbt_main_descr
= {
1295 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1296 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
1301 .pattern
= bbt_pattern
1304 static struct nand_bbt_descr bbt_mirror_descr
= {
1305 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1306 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
1311 .pattern
= mirror_pattern
1314 static struct nand_bbt_descr bbt_main_no_bbt_descr
= {
1315 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1316 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
1321 .pattern
= bbt_pattern
1324 static struct nand_bbt_descr bbt_mirror_no_bbt_descr
= {
1325 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1326 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
1331 .pattern
= mirror_pattern
1334 #define BBT_SCAN_OPTIONS (NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE | \
1335 NAND_BBT_SCANBYTE1AND6)
1337 * nand_create_default_bbt_descr - [Internal] Creates a BBT descriptor structure
1338 * @this: NAND chip to create descriptor for
1340 * This function allocates and initializes a nand_bbt_descr for BBM detection
1341 * based on the properties of "this". The new descriptor is stored in
1342 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1343 * passed to this function.
1346 static int nand_create_default_bbt_descr(struct nand_chip
*this)
1348 struct nand_bbt_descr
*bd
;
1349 if (this->badblock_pattern
) {
1350 printk(KERN_WARNING
"BBT descr already allocated; not replacing.\n");
1353 bd
= kzalloc(sizeof(*bd
), GFP_KERNEL
);
1355 printk(KERN_ERR
"nand_create_default_bbt_descr: Out of memory\n");
1358 bd
->options
= this->options
& BBT_SCAN_OPTIONS
;
1359 bd
->offs
= this->badblockpos
;
1360 bd
->len
= (this->options
& NAND_BUSWIDTH_16
) ? 2 : 1;
1361 bd
->pattern
= scan_ff_pattern
;
1362 bd
->options
|= NAND_BBT_DYNAMICSTRUCT
;
1363 this->badblock_pattern
= bd
;
1368 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
1369 * @mtd: MTD device structure
1371 * This function selects the default bad block table
1372 * support for the device and calls the nand_scan_bbt function
1375 int nand_default_bbt(struct mtd_info
*mtd
)
1377 struct nand_chip
*this = mtd
->priv
;
1379 /* Default for AG-AND. We must use a flash based
1380 * bad block table as the devices have factory marked
1381 * _good_ blocks. Erasing those blocks leads to loss
1382 * of the good / bad information, so we _must_ store
1383 * this information in a good / bad table during
1386 if (this->options
& NAND_IS_AND
) {
1387 /* Use the default pattern descriptors */
1388 if (!this->bbt_td
) {
1389 this->bbt_td
= &bbt_main_descr
;
1390 this->bbt_md
= &bbt_mirror_descr
;
1392 this->options
|= NAND_USE_FLASH_BBT
;
1393 return nand_scan_bbt(mtd
, &agand_flashbased
);
1396 /* Is a flash based bad block table requested ? */
1397 if (this->options
& NAND_USE_FLASH_BBT
) {
1398 /* Use the default pattern descriptors */
1399 if (!this->bbt_td
) {
1400 if (this->options
& NAND_USE_FLASH_BBT_NO_OOB
) {
1401 this->bbt_td
= &bbt_main_no_bbt_descr
;
1402 this->bbt_md
= &bbt_mirror_no_bbt_descr
;
1404 this->bbt_td
= &bbt_main_descr
;
1405 this->bbt_md
= &bbt_mirror_descr
;
1409 this->bbt_td
= NULL
;
1410 this->bbt_md
= NULL
;
1413 if (!this->badblock_pattern
)
1414 nand_create_default_bbt_descr(this);
1416 return nand_scan_bbt(mtd
, this->badblock_pattern
);
1420 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1421 * @mtd: MTD device structure
1422 * @offs: offset in the device
1423 * @allowbbt: allow access to bad block table region
1426 int nand_isbad_bbt(struct mtd_info
*mtd
, loff_t offs
, int allowbbt
)
1428 struct nand_chip
*this = mtd
->priv
;
1432 /* Get block number * 2 */
1433 block
= (int)(offs
>> (this->bbt_erase_shift
- 1));
1434 res
= (this->bbt
[block
>> 3] >> (block
& 0x06)) & 0x03;
1436 DEBUG(MTD_DEBUG_LEVEL2
, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1437 (unsigned int)offs
, block
>> 1, res
);
1445 return allowbbt
? 0 : 1;
1450 EXPORT_SYMBOL(nand_scan_bbt
);
1451 EXPORT_SYMBOL(nand_default_bbt
);