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 a bbt is found
17 * then the contents are read and the memory based bbt is created. If a
18 * mirrored bbt is selected then the mirror is searched too and the
19 * versions are compared. If the mirror has a greater version number
20 * than the mirror bbt is used to build the memory based bbt.
21 * If the tables are not versioned, then we "or" the bad block information.
22 * If one of the bbt's is out of date or does not exist it is (re)created.
23 * If no bbt exists at all then the device is scanned for factory marked
24 * good / bad blocks and the bad block tables are created.
26 * For manufacturer created bbts like the one found on M-SYS DOC devices
27 * the bbt is searched and read but never created
29 * The autogenerated bad block table is located in the last good blocks
30 * of the device. The table is mirrored, so it can be updated eventually.
31 * The table is marked in the oob area with an ident pattern and a version
32 * number which indicates which of both tables is more up to date.
34 * The table uses 2 bits per block
36 * 00b: block is factory marked bad
37 * 01b, 10b: block is marked bad due to wear
39 * The memory bad block table uses the following scheme:
41 * 01b: block is marked bad due to wear
42 * 10b: block is reserved (to protect the bbt area)
43 * 11b: block is factory marked bad
45 * Multichip devices like DOC store the bad block info per floor.
47 * Following assumptions are made:
48 * - bbts start at a page boundary, if autolocated on a block boundary
49 * - the space necessary for a bbt in FLASH does not exceed a block boundary
55 #include <linux/mtd/compat.h>
56 #include <linux/mtd/mtd.h>
57 #include <linux/mtd/nand.h>
59 #include <asm/errno.h>
63 #include <linux/slab.h>
64 #include <linux/types.h>
65 #include <linux/mtd/mtd.h>
66 #include <linux/mtd/nand.h>
67 #include <linux/mtd/nand_ecc.h>
68 #include <linux/mtd/compatmac.h>
69 #include <linux/bitops.h>
70 #include <linux/delay.h>
71 #include <linux/vmalloc.h>
75 * check_pattern - [GENERIC] check if a pattern is in the buffer
76 * @buf: the buffer to search
77 * @len: the length of buffer to search
78 * @paglen: the pagelength
79 * @td: search pattern descriptor
81 * Check for a pattern at the given place. Used to search bad block
82 * tables and good / bad block identifiers.
83 * If the SCAN_EMPTY option is set then check, if all bytes except the
84 * pattern area contain 0xff
87 static int check_pattern(uint8_t *buf
, int len
, int paglen
, struct nand_bbt_descr
*td
)
92 end
= paglen
+ td
->offs
;
93 if (td
->options
& NAND_BBT_SCANEMPTY
) {
94 for (i
= 0; i
< end
; i
++) {
101 /* Compare the pattern */
102 for (i
= 0; i
< td
->len
; i
++) {
103 if (p
[i
] != td
->pattern
[i
])
107 if (td
->options
& NAND_BBT_SCANEMPTY
) {
110 for (i
= end
; i
< len
; i
++) {
119 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
120 * @buf: the buffer to search
121 * @td: search pattern descriptor
123 * Check for a pattern at the given place. Used to search bad block
124 * tables and good / bad block identifiers. Same as check_pattern, but
125 * no optional empty check
128 static int check_short_pattern(uint8_t *buf
, struct nand_bbt_descr
*td
)
133 /* Compare the pattern */
134 for (i
= 0; i
< td
->len
; i
++) {
135 if (p
[td
->offs
+ i
] != td
->pattern
[i
])
142 * read_bbt - [GENERIC] Read the bad block table starting from page
143 * @mtd: MTD device structure
144 * @buf: temporary buffer
145 * @page: the starting page
146 * @num: the number of bbt descriptors to read
147 * @bits: number of bits per block
148 * @offs: offset in the memory table
149 * @reserved_block_code: Pattern to identify reserved blocks
151 * Read the bad block table starting from page.
154 static int read_bbt(struct mtd_info
*mtd
, uint8_t *buf
, int page
, int num
,
155 int bits
, int offs
, int reserved_block_code
)
157 int res
, i
, j
, act
= 0;
158 struct nand_chip
*this = mtd
->priv
;
159 size_t retlen
, len
, totlen
;
161 uint8_t msk
= (uint8_t) ((1 << bits
) - 1);
163 totlen
= (num
* bits
) >> 3;
164 from
= ((loff_t
) page
) << this->page_shift
;
167 len
= min(totlen
, (size_t) (1 << this->bbt_erase_shift
));
168 res
= mtd
->read(mtd
, from
, len
, &retlen
, buf
);
171 printk(KERN_INFO
"nand_bbt: Error reading bad block table\n");
174 printk(KERN_WARNING
"nand_bbt: ECC error while reading bad block table\n");
178 for (i
= 0; i
< len
; i
++) {
179 uint8_t dat
= buf
[i
];
180 for (j
= 0; j
< 8; j
+= bits
, act
+= 2) {
181 uint8_t tmp
= (dat
>> j
) & msk
;
184 if (reserved_block_code
&& (tmp
== reserved_block_code
)) {
185 printk(KERN_DEBUG
"nand_read_bbt: Reserved block at 0x%08x\n",
186 ((offs
<< 2) + (act
>> 1)) << this->bbt_erase_shift
);
187 this->bbt
[offs
+ (act
>> 3)] |= 0x2 << (act
& 0x06);
188 mtd
->ecc_stats
.bbtblocks
++;
191 /* Leave it for now, if its matured we can move this
192 * message to MTD_DEBUG_LEVEL0 */
193 printk(KERN_DEBUG
"nand_read_bbt: Bad block at 0x%08x\n",
194 ((offs
<< 2) + (act
>> 1)) << this->bbt_erase_shift
);
195 /* Factory marked bad or worn out ? */
197 this->bbt
[offs
+ (act
>> 3)] |= 0x3 << (act
& 0x06);
199 this->bbt
[offs
+ (act
>> 3)] |= 0x1 << (act
& 0x06);
200 mtd
->ecc_stats
.badblocks
++;
210 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
211 * @mtd: MTD device structure
212 * @buf: temporary buffer
213 * @td: descriptor for the bad block table
214 * @chip: read the table for a specific chip, -1 read all chips.
215 * Applies only if NAND_BBT_PERCHIP option is set
217 * Read the bad block table for all chips starting at a given page
218 * We assume that the bbt bits are in consecutive order.
220 static int read_abs_bbt(struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
, int chip
)
222 struct nand_chip
*this = mtd
->priv
;
226 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
227 if (td
->options
& NAND_BBT_PERCHIP
) {
229 for (i
= 0; i
< this->numchips
; i
++) {
230 if (chip
== -1 || chip
== i
)
231 res
= read_bbt (mtd
, buf
, td
->pages
[i
], this->chipsize
>> this->bbt_erase_shift
, bits
, offs
, td
->reserved_block_code
);
234 offs
+= this->chipsize
>> (this->bbt_erase_shift
+ 2);
237 res
= read_bbt (mtd
, buf
, td
->pages
[0], mtd
->size
>> this->bbt_erase_shift
, bits
, 0, td
->reserved_block_code
);
245 * Scan read raw data from flash
247 static int scan_read_raw(struct mtd_info
*mtd
, uint8_t *buf
, loff_t offs
,
250 struct mtd_oob_ops ops
;
252 ops
.mode
= MTD_OOB_RAW
;
254 ops
.ooblen
= mtd
->oobsize
;
259 return mtd
->read_oob(mtd
, offs
, &ops
);
263 * Scan write data with oob to flash
265 static int scan_write_bbt(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
266 uint8_t *buf
, uint8_t *oob
)
268 struct mtd_oob_ops ops
;
270 ops
.mode
= MTD_OOB_PLACE
;
272 ops
.ooblen
= mtd
->oobsize
;
277 return mtd
->write_oob(mtd
, offs
, &ops
);
281 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
282 * @mtd: MTD device structure
283 * @buf: temporary buffer
284 * @td: descriptor for the bad block table
285 * @md: descriptor for the bad block table mirror
287 * Read the bad block table(s) for all chips starting at a given page
288 * We assume that the bbt bits are in consecutive order.
291 static int read_abs_bbts(struct mtd_info
*mtd
, uint8_t *buf
,
292 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
)
294 struct nand_chip
*this = mtd
->priv
;
296 /* Read the primary version, if available */
297 if (td
->options
& NAND_BBT_VERSION
) {
298 scan_read_raw(mtd
, buf
, td
->pages
[0] << this->page_shift
,
300 td
->version
[0] = buf
[mtd
->writesize
+ td
->veroffs
];
301 printk(KERN_DEBUG
"Bad block table at page %d, version 0x%02X\n",
302 td
->pages
[0], td
->version
[0]);
305 /* Read the mirror version, if available */
306 if (md
&& (md
->options
& NAND_BBT_VERSION
)) {
307 scan_read_raw(mtd
, buf
, md
->pages
[0] << this->page_shift
,
309 md
->version
[0] = buf
[mtd
->writesize
+ md
->veroffs
];
310 printk(KERN_DEBUG
"Bad block table at page %d, version 0x%02X\n",
311 md
->pages
[0], md
->version
[0]);
317 * Scan a given block full
319 static int scan_block_full(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
,
320 loff_t offs
, uint8_t *buf
, size_t readlen
,
321 int scanlen
, int len
)
325 ret
= scan_read_raw(mtd
, buf
, offs
, readlen
);
329 for (j
= 0; j
< len
; j
++, buf
+= scanlen
) {
330 if (check_pattern(buf
, scanlen
, mtd
->writesize
, bd
))
337 * Scan a given block partially
339 static int scan_block_fast(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
,
340 loff_t offs
, uint8_t *buf
, int len
)
342 struct mtd_oob_ops ops
;
345 ops
.ooblen
= mtd
->oobsize
;
349 ops
.mode
= MTD_OOB_PLACE
;
351 for (j
= 0; j
< len
; j
++) {
353 * Read the full oob until read_oob is fixed to
354 * handle single byte reads for 16 bit
357 ret
= mtd
->read_oob(mtd
, offs
, &ops
);
361 if (check_short_pattern(buf
, bd
))
364 offs
+= mtd
->writesize
;
370 * create_bbt - [GENERIC] Create a bad block table by scanning the device
371 * @mtd: MTD device structure
372 * @buf: temporary buffer
373 * @bd: descriptor for the good/bad block search pattern
374 * @chip: create the table for a specific chip, -1 read all chips.
375 * Applies only if NAND_BBT_PERCHIP option is set
377 * Create a bad block table by scanning the device
378 * for the given good/bad block identify pattern
380 static int create_bbt(struct mtd_info
*mtd
, uint8_t *buf
,
381 struct nand_bbt_descr
*bd
, int chip
)
383 struct nand_chip
*this = mtd
->priv
;
384 int i
, numblocks
, len
, scanlen
;
389 MTDDEBUG (MTD_DEBUG_LEVEL0
, "Scanning device for bad blocks\n");
391 if (bd
->options
& NAND_BBT_SCANALLPAGES
)
392 len
= 1 << (this->bbt_erase_shift
- this->page_shift
);
394 if (bd
->options
& NAND_BBT_SCAN2NDPAGE
)
400 if (!(bd
->options
& NAND_BBT_SCANEMPTY
)) {
401 /* We need only read few bytes from the OOB area */
405 /* Full page content should be read */
406 scanlen
= mtd
->writesize
+ mtd
->oobsize
;
407 readlen
= len
* mtd
->writesize
;
411 /* Note that numblocks is 2 * (real numblocks) here, see i+=2
412 * below as it makes shifting and masking less painful */
413 numblocks
= mtd
->size
>> (this->bbt_erase_shift
- 1);
417 if (chip
>= this->numchips
) {
418 printk(KERN_WARNING
"create_bbt(): chipnr (%d) > available chips (%d)\n",
419 chip
+ 1, this->numchips
);
422 numblocks
= this->chipsize
>> (this->bbt_erase_shift
- 1);
423 startblock
= chip
* numblocks
;
424 numblocks
+= startblock
;
425 from
= startblock
<< (this->bbt_erase_shift
- 1);
428 for (i
= startblock
; i
< numblocks
;) {
431 if (bd
->options
& NAND_BBT_SCANALLPAGES
)
432 ret
= scan_block_full(mtd
, bd
, from
, buf
, readlen
,
435 ret
= scan_block_fast(mtd
, bd
, from
, buf
, len
);
441 this->bbt
[i
>> 3] |= 0x03 << (i
& 0x6);
442 MTDDEBUG (MTD_DEBUG_LEVEL0
,
443 "Bad eraseblock %d at 0x%08x\n",
444 i
>> 1, (unsigned int)from
);
445 mtd
->ecc_stats
.badblocks
++;
449 from
+= (1 << this->bbt_erase_shift
);
455 * search_bbt - [GENERIC] scan the device for a specific bad block table
456 * @mtd: MTD device structure
457 * @buf: temporary buffer
458 * @td: descriptor for the bad block table
460 * Read the bad block table by searching for a given ident pattern.
461 * Search is preformed either from the beginning up or from the end of
462 * the device downwards. The search starts always at the start of a
464 * If the option NAND_BBT_PERCHIP is given, each chip is searched
465 * for a bbt, which contains the bad block information of this chip.
466 * This is necessary to provide support for certain DOC devices.
468 * The bbt ident pattern resides in the oob area of the first page
471 static int search_bbt(struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
)
473 struct nand_chip
*this = mtd
->priv
;
475 int bits
, startblock
, block
, dir
;
476 int scanlen
= mtd
->writesize
+ mtd
->oobsize
;
478 int blocktopage
= this->bbt_erase_shift
- this->page_shift
;
480 /* Search direction top -> down ? */
481 if (td
->options
& NAND_BBT_LASTBLOCK
) {
482 startblock
= (mtd
->size
>> this->bbt_erase_shift
) - 1;
489 /* Do we have a bbt per chip ? */
490 if (td
->options
& NAND_BBT_PERCHIP
) {
491 chips
= this->numchips
;
492 bbtblocks
= this->chipsize
>> this->bbt_erase_shift
;
493 startblock
&= bbtblocks
- 1;
496 bbtblocks
= mtd
->size
>> this->bbt_erase_shift
;
499 /* Number of bits for each erase block in the bbt */
500 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
502 for (i
= 0; i
< chips
; i
++) {
503 /* Reset version information */
506 /* Scan the maximum number of blocks */
507 for (block
= 0; block
< td
->maxblocks
; block
++) {
509 int actblock
= startblock
+ dir
* block
;
510 loff_t offs
= actblock
<< this->bbt_erase_shift
;
512 /* Read first page */
513 scan_read_raw(mtd
, buf
, offs
, mtd
->writesize
);
514 if (!check_pattern(buf
, scanlen
, mtd
->writesize
, td
)) {
515 td
->pages
[i
] = actblock
<< blocktopage
;
516 if (td
->options
& NAND_BBT_VERSION
) {
517 td
->version
[i
] = buf
[mtd
->writesize
+ td
->veroffs
];
522 startblock
+= this->chipsize
>> this->bbt_erase_shift
;
524 /* Check, if we found a bbt for each requested chip */
525 for (i
= 0; i
< chips
; i
++) {
526 if (td
->pages
[i
] == -1)
527 printk(KERN_WARNING
"Bad block table not found for chip %d\n", i
);
529 printk(KERN_DEBUG
"Bad block table found at page %d, version 0x%02X\n", td
->pages
[i
],
536 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
537 * @mtd: MTD device structure
538 * @buf: temporary buffer
539 * @td: descriptor for the bad block table
540 * @md: descriptor for the bad block table mirror
542 * Search and read the bad block table(s)
544 static int search_read_bbts(struct mtd_info
*mtd
, uint8_t * buf
, struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
)
546 /* Search the primary table */
547 search_bbt(mtd
, buf
, td
);
549 /* Search the mirror table */
551 search_bbt(mtd
, buf
, md
);
553 /* Force result check */
558 * write_bbt - [GENERIC] (Re)write the bad block table
560 * @mtd: MTD device structure
561 * @buf: temporary buffer
562 * @td: descriptor for the bad block table
563 * @md: descriptor for the bad block table mirror
564 * @chipsel: selector for a specific chip, -1 for all
566 * (Re)write the bad block table
569 static int write_bbt(struct mtd_info
*mtd
, uint8_t *buf
,
570 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
,
573 struct nand_chip
*this = mtd
->priv
;
574 struct erase_info einfo
;
575 int i
, j
, res
, chip
= 0;
576 int bits
, startblock
, dir
, page
, offs
, numblocks
, sft
, sftmsk
;
577 int nrchips
, bbtoffs
, pageoffs
, ooboffs
;
579 uint8_t rcode
= td
->reserved_block_code
;
580 size_t retlen
, len
= 0;
582 struct mtd_oob_ops ops
;
584 ops
.ooblen
= mtd
->oobsize
;
587 ops
.mode
= MTD_OOB_PLACE
;
591 /* Write bad block table per chip rather than per device ? */
592 if (td
->options
& NAND_BBT_PERCHIP
) {
593 numblocks
= (int)(this->chipsize
>> this->bbt_erase_shift
);
594 /* Full device write or specific chip ? */
596 nrchips
= this->numchips
;
598 nrchips
= chipsel
+ 1;
602 numblocks
= (int)(mtd
->size
>> this->bbt_erase_shift
);
606 /* Loop through the chips */
607 for (; chip
< nrchips
; chip
++) {
609 /* There was already a version of the table, reuse the page
610 * This applies for absolute placement too, as we have the
611 * page nr. in td->pages.
613 if (td
->pages
[chip
] != -1) {
614 page
= td
->pages
[chip
];
618 /* Automatic placement of the bad block table */
619 /* Search direction top -> down ? */
620 if (td
->options
& NAND_BBT_LASTBLOCK
) {
621 startblock
= numblocks
* (chip
+ 1) - 1;
624 startblock
= chip
* numblocks
;
628 for (i
= 0; i
< td
->maxblocks
; i
++) {
629 int block
= startblock
+ dir
* i
;
630 /* Check, if the block is bad */
631 switch ((this->bbt
[block
>> 2] >>
632 (2 * (block
& 0x03))) & 0x03) {
638 (this->bbt_erase_shift
- this->page_shift
);
639 /* Check, if the block is used by the mirror table */
640 if (!md
|| md
->pages
[chip
] != page
)
643 printk(KERN_ERR
"No space left to write bad block table\n");
647 /* Set up shift count and masks for the flash table */
648 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
651 case 1: sft
= 3; sftmsk
= 0x07; msk
[0] = 0x00; msk
[1] = 0x01;
654 case 2: sft
= 2; sftmsk
= 0x06; msk
[0] = 0x00; msk
[1] = 0x01;
657 case 4: sft
= 1; sftmsk
= 0x04; msk
[0] = 0x00; msk
[1] = 0x0C;
660 case 8: sft
= 0; sftmsk
= 0x00; msk
[0] = 0x00; msk
[1] = 0x0F;
663 default: return -EINVAL
;
666 bbtoffs
= chip
* (numblocks
>> 2);
668 to
= ((loff_t
) page
) << this->page_shift
;
670 /* Must we save the block contents ? */
671 if (td
->options
& NAND_BBT_SAVECONTENT
) {
672 /* Make it block aligned */
673 to
&= ~((loff_t
) ((1 << this->bbt_erase_shift
) - 1));
674 len
= 1 << this->bbt_erase_shift
;
675 res
= mtd
->read(mtd
, to
, len
, &retlen
, buf
);
678 printk(KERN_INFO
"nand_bbt: Error "
679 "reading block for writing "
680 "the bad block table\n");
683 printk(KERN_WARNING
"nand_bbt: ECC error "
684 "while reading block for writing "
685 "bad block table\n");
688 ops
.ooblen
= (len
>> this->page_shift
) * mtd
->oobsize
;
689 ops
.oobbuf
= &buf
[len
];
690 res
= mtd
->read_oob(mtd
, to
+ mtd
->writesize
, &ops
);
691 if (res
< 0 || ops
.oobretlen
!= ops
.ooblen
)
694 /* Calc the byte offset in the buffer */
695 pageoffs
= page
- (int)(to
>> this->page_shift
);
696 offs
= pageoffs
<< this->page_shift
;
697 /* Preset the bbt area with 0xff */
698 memset(&buf
[offs
], 0xff, (size_t) (numblocks
>> sft
));
699 ooboffs
= len
+ (pageoffs
* mtd
->oobsize
);
703 len
= (size_t) (numblocks
>> sft
);
704 /* Make it page aligned ! */
705 len
= (len
+ (mtd
->writesize
- 1)) &
706 ~(mtd
->writesize
- 1);
707 /* Preset the buffer with 0xff */
708 memset(buf
, 0xff, len
+
709 (len
>> this->page_shift
)* mtd
->oobsize
);
712 /* Pattern is located in oob area of first page */
713 memcpy(&buf
[ooboffs
+ td
->offs
], td
->pattern
, td
->len
);
716 if (td
->options
& NAND_BBT_VERSION
)
717 buf
[ooboffs
+ td
->veroffs
] = td
->version
[chip
];
719 /* walk through the memory table */
720 for (i
= 0; i
< numblocks
;) {
722 dat
= this->bbt
[bbtoffs
+ (i
>> 2)];
723 for (j
= 0; j
< 4; j
++, i
++) {
724 int sftcnt
= (i
<< (3 - sft
)) & sftmsk
;
725 /* Do not store the reserved bbt blocks ! */
726 buf
[offs
+ (i
>> sft
)] &=
727 ~(msk
[dat
& 0x03] << sftcnt
);
732 memset(&einfo
, 0, sizeof(einfo
));
734 einfo
.addr
= (unsigned long)to
;
735 einfo
.len
= 1 << this->bbt_erase_shift
;
736 res
= nand_erase_nand(mtd
, &einfo
, 1);
740 res
= scan_write_bbt(mtd
, to
, len
, buf
, &buf
[len
]);
744 printk(KERN_DEBUG
"Bad block table written to 0x%08x, version "
745 "0x%02X\n", (unsigned int)to
, td
->version
[chip
]);
747 /* Mark it as used */
748 td
->pages
[chip
] = page
;
754 "nand_bbt: Error while writing bad block table %d\n", res
);
759 * nand_memory_bbt - [GENERIC] create a memory based bad block table
760 * @mtd: MTD device structure
761 * @bd: descriptor for the good/bad block search pattern
763 * The function creates a memory based bbt by scanning the device
764 * for manufacturer / software marked good / bad blocks
766 static inline int nand_memory_bbt(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
768 struct nand_chip
*this = mtd
->priv
;
770 bd
->options
&= ~NAND_BBT_SCANEMPTY
;
771 return create_bbt(mtd
, this->buffers
->databuf
, bd
, -1);
775 * check_create - [GENERIC] create and write bbt(s) if necessary
776 * @mtd: MTD device structure
777 * @buf: temporary buffer
778 * @bd: descriptor for the good/bad block search pattern
780 * The function checks the results of the previous call to read_bbt
781 * and creates / updates the bbt(s) if necessary
782 * Creation is necessary if no bbt was found for the chip/device
783 * Update is necessary if one of the tables is missing or the
784 * version nr. of one table is less than the other
786 static int check_create(struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*bd
)
788 int i
, chips
, writeops
, chipsel
, res
;
789 struct nand_chip
*this = mtd
->priv
;
790 struct nand_bbt_descr
*td
= this->bbt_td
;
791 struct nand_bbt_descr
*md
= this->bbt_md
;
792 struct nand_bbt_descr
*rd
, *rd2
;
794 /* Do we have a bbt per chip ? */
795 if (td
->options
& NAND_BBT_PERCHIP
)
796 chips
= this->numchips
;
800 for (i
= 0; i
< chips
; i
++) {
804 /* Per chip or per device ? */
805 chipsel
= (td
->options
& NAND_BBT_PERCHIP
) ? i
: -1;
806 /* Mirrored table avilable ? */
808 if (td
->pages
[i
] == -1 && md
->pages
[i
] == -1) {
813 if (td
->pages
[i
] == -1) {
815 td
->version
[i
] = md
->version
[i
];
820 if (md
->pages
[i
] == -1) {
822 md
->version
[i
] = td
->version
[i
];
827 if (td
->version
[i
] == md
->version
[i
]) {
829 if (!(td
->options
& NAND_BBT_VERSION
))
834 if (((int8_t) (td
->version
[i
] - md
->version
[i
])) > 0) {
836 md
->version
[i
] = td
->version
[i
];
840 td
->version
[i
] = md
->version
[i
];
847 if (td
->pages
[i
] == -1) {
855 /* Create the bad block table by scanning the device ? */
856 if (!(td
->options
& NAND_BBT_CREATE
))
859 /* Create the table in memory by scanning the chip(s) */
860 create_bbt(mtd
, buf
, bd
, chipsel
);
866 /* read back first ? */
868 read_abs_bbt(mtd
, buf
, rd
, chipsel
);
869 /* If they weren't versioned, read both. */
871 read_abs_bbt(mtd
, buf
, rd2
, chipsel
);
873 /* Write the bad block table to the device ? */
874 if ((writeops
& 0x01) && (td
->options
& NAND_BBT_WRITE
)) {
875 res
= write_bbt(mtd
, buf
, td
, md
, chipsel
);
880 /* Write the mirror bad block table to the device ? */
881 if ((writeops
& 0x02) && md
&& (md
->options
& NAND_BBT_WRITE
)) {
882 res
= write_bbt(mtd
, buf
, md
, td
, chipsel
);
891 * mark_bbt_regions - [GENERIC] mark the bad block table regions
892 * @mtd: MTD device structure
893 * @td: bad block table descriptor
895 * The bad block table regions are marked as "bad" to prevent
896 * accidental erasures / writes. The regions are identified by
899 static void mark_bbt_region(struct mtd_info
*mtd
, struct nand_bbt_descr
*td
)
901 struct nand_chip
*this = mtd
->priv
;
902 int i
, j
, chips
, block
, nrblocks
, update
;
903 uint8_t oldval
, newval
;
905 /* Do we have a bbt per chip ? */
906 if (td
->options
& NAND_BBT_PERCHIP
) {
907 chips
= this->numchips
;
908 nrblocks
= (int)(this->chipsize
>> this->bbt_erase_shift
);
911 nrblocks
= (int)(mtd
->size
>> this->bbt_erase_shift
);
914 for (i
= 0; i
< chips
; i
++) {
915 if ((td
->options
& NAND_BBT_ABSPAGE
) ||
916 !(td
->options
& NAND_BBT_WRITE
)) {
917 if (td
->pages
[i
] == -1)
919 block
= td
->pages
[i
] >> (this->bbt_erase_shift
- this->page_shift
);
921 oldval
= this->bbt
[(block
>> 3)];
922 newval
= oldval
| (0x2 << (block
& 0x06));
923 this->bbt
[(block
>> 3)] = newval
;
924 if ((oldval
!= newval
) && td
->reserved_block_code
)
925 nand_update_bbt(mtd
, block
<< (this->bbt_erase_shift
- 1));
929 if (td
->options
& NAND_BBT_LASTBLOCK
)
930 block
= ((i
+ 1) * nrblocks
) - td
->maxblocks
;
932 block
= i
* nrblocks
;
934 for (j
= 0; j
< td
->maxblocks
; j
++) {
935 oldval
= this->bbt
[(block
>> 3)];
936 newval
= oldval
| (0x2 << (block
& 0x06));
937 this->bbt
[(block
>> 3)] = newval
;
938 if (oldval
!= newval
)
942 /* If we want reserved blocks to be recorded to flash, and some
943 new ones have been marked, then we need to update the stored
944 bbts. This should only happen once. */
945 if (update
&& td
->reserved_block_code
)
946 nand_update_bbt(mtd
, (block
- 2) << (this->bbt_erase_shift
- 1));
951 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
952 * @mtd: MTD device structure
953 * @bd: descriptor for the good/bad block search pattern
955 * The function checks, if a bad block table(s) is/are already
956 * available. If not it scans the device for manufacturer
957 * marked good / bad blocks and writes the bad block table(s) to
958 * the selected place.
960 * The bad block table memory is allocated here. It must be freed
961 * by calling the nand_free_bbt function.
964 int nand_scan_bbt(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
966 struct nand_chip
*this = mtd
->priv
;
969 struct nand_bbt_descr
*td
= this->bbt_td
;
970 struct nand_bbt_descr
*md
= this->bbt_md
;
972 len
= mtd
->size
>> (this->bbt_erase_shift
+ 2);
973 /* Allocate memory (2bit per block) and clear the memory bad block table */
974 this->bbt
= kzalloc(len
, GFP_KERNEL
);
976 printk(KERN_ERR
"nand_scan_bbt: Out of memory\n");
980 /* If no primary table decriptor is given, scan the device
981 * to build a memory based bad block table
984 if ((res
= nand_memory_bbt(mtd
, bd
))) {
985 printk(KERN_ERR
"nand_bbt: Can't scan flash and build the RAM-based BBT\n");
992 /* Allocate a temporary buffer for one eraseblock incl. oob */
993 len
= (1 << this->bbt_erase_shift
);
994 len
+= (len
>> this->page_shift
) * mtd
->oobsize
;
997 printk(KERN_ERR
"nand_bbt: Out of memory\n");
1003 /* Is the bbt at a given page ? */
1004 if (td
->options
& NAND_BBT_ABSPAGE
) {
1005 res
= read_abs_bbts(mtd
, buf
, td
, md
);
1007 /* Search the bad block table using a pattern in oob */
1008 res
= search_read_bbts(mtd
, buf
, td
, md
);
1012 res
= check_create(mtd
, buf
, bd
);
1014 /* Prevent the bbt regions from erasing / writing */
1015 mark_bbt_region(mtd
, td
);
1017 mark_bbt_region(mtd
, md
);
1024 * nand_update_bbt - [NAND Interface] update bad block table(s)
1025 * @mtd: MTD device structure
1026 * @offs: the offset of the newly marked block
1028 * The function updates the bad block table(s)
1030 int nand_update_bbt(struct mtd_info
*mtd
, loff_t offs
)
1032 struct nand_chip
*this = mtd
->priv
;
1033 int len
, res
= 0, writeops
= 0;
1036 struct nand_bbt_descr
*td
= this->bbt_td
;
1037 struct nand_bbt_descr
*md
= this->bbt_md
;
1039 if (!this->bbt
|| !td
)
1042 len
= mtd
->size
>> (this->bbt_erase_shift
+ 2);
1043 /* Allocate a temporary buffer for one eraseblock incl. oob */
1044 len
= (1 << this->bbt_erase_shift
);
1045 len
+= (len
>> this->page_shift
) * mtd
->oobsize
;
1046 buf
= kmalloc(len
, GFP_KERNEL
);
1048 printk(KERN_ERR
"nand_update_bbt: Out of memory\n");
1052 writeops
= md
!= NULL
? 0x03 : 0x01;
1054 /* Do we have a bbt per chip ? */
1055 if (td
->options
& NAND_BBT_PERCHIP
) {
1056 chip
= (int)(offs
>> this->chip_shift
);
1063 td
->version
[chip
]++;
1065 md
->version
[chip
]++;
1067 /* Write the bad block table to the device ? */
1068 if ((writeops
& 0x01) && (td
->options
& NAND_BBT_WRITE
)) {
1069 res
= write_bbt(mtd
, buf
, td
, md
, chipsel
);
1073 /* Write the mirror bad block table to the device ? */
1074 if ((writeops
& 0x02) && md
&& (md
->options
& NAND_BBT_WRITE
)) {
1075 res
= write_bbt(mtd
, buf
, md
, td
, chipsel
);
1083 /* Define some generic bad / good block scan pattern which are used
1084 * while scanning a device for factory marked good / bad blocks. */
1085 static uint8_t scan_ff_pattern
[] = { 0xff, 0xff };
1087 static struct nand_bbt_descr smallpage_memorybased
= {
1088 .options
= NAND_BBT_SCAN2NDPAGE
,
1091 .pattern
= scan_ff_pattern
1094 static struct nand_bbt_descr largepage_memorybased
= {
1098 .pattern
= scan_ff_pattern
1101 static struct nand_bbt_descr smallpage_flashbased
= {
1102 .options
= NAND_BBT_SCAN2NDPAGE
,
1105 .pattern
= scan_ff_pattern
1108 static struct nand_bbt_descr largepage_flashbased
= {
1109 .options
= NAND_BBT_SCAN2NDPAGE
,
1112 .pattern
= scan_ff_pattern
1115 static uint8_t scan_agand_pattern
[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1117 static struct nand_bbt_descr agand_flashbased
= {
1118 .options
= NAND_BBT_SCANEMPTY
| NAND_BBT_SCANALLPAGES
,
1121 .pattern
= scan_agand_pattern
1124 /* Generic flash bbt decriptors
1126 static uint8_t bbt_pattern
[] = {'B', 'b', 't', '0' };
1127 static uint8_t mirror_pattern
[] = {'1', 't', 'b', 'B' };
1129 static struct nand_bbt_descr bbt_main_descr
= {
1130 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1131 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
1136 .pattern
= bbt_pattern
1139 static struct nand_bbt_descr bbt_mirror_descr
= {
1140 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1141 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
1146 .pattern
= mirror_pattern
1150 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
1151 * @mtd: MTD device structure
1153 * This function selects the default bad block table
1154 * support for the device and calls the nand_scan_bbt function
1157 int nand_default_bbt(struct mtd_info
*mtd
)
1159 struct nand_chip
*this = mtd
->priv
;
1161 /* Default for AG-AND. We must use a flash based
1162 * bad block table as the devices have factory marked
1163 * _good_ blocks. Erasing those blocks leads to loss
1164 * of the good / bad information, so we _must_ store
1165 * this information in a good / bad table during
1168 if (this->options
& NAND_IS_AND
) {
1169 /* Use the default pattern descriptors */
1170 if (!this->bbt_td
) {
1171 this->bbt_td
= &bbt_main_descr
;
1172 this->bbt_md
= &bbt_mirror_descr
;
1174 this->options
|= NAND_USE_FLASH_BBT
;
1175 return nand_scan_bbt(mtd
, &agand_flashbased
);
1178 /* Is a flash based bad block table requested ? */
1179 if (this->options
& NAND_USE_FLASH_BBT
) {
1180 /* Use the default pattern descriptors */
1181 if (!this->bbt_td
) {
1182 this->bbt_td
= &bbt_main_descr
;
1183 this->bbt_md
= &bbt_mirror_descr
;
1185 if (!this->badblock_pattern
) {
1186 this->badblock_pattern
= (mtd
->writesize
> 512) ? &largepage_flashbased
: &smallpage_flashbased
;
1189 this->bbt_td
= NULL
;
1190 this->bbt_md
= NULL
;
1191 if (!this->badblock_pattern
) {
1192 this->badblock_pattern
= (mtd
->writesize
> 512) ?
1193 &largepage_memorybased
: &smallpage_memorybased
;
1196 return nand_scan_bbt(mtd
, this->badblock_pattern
);
1200 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1201 * @mtd: MTD device structure
1202 * @offs: offset in the device
1203 * @allowbbt: allow access to bad block table region
1206 int nand_isbad_bbt(struct mtd_info
*mtd
, loff_t offs
, int allowbbt
)
1208 struct nand_chip
*this = mtd
->priv
;
1212 /* Get block number * 2 */
1213 block
= (int)(offs
>> (this->bbt_erase_shift
- 1));
1214 res
= (this->bbt
[block
>> 3] >> (block
& 0x06)) & 0x03;
1216 MTDDEBUG (MTD_DEBUG_LEVEL2
, "nand_isbad_bbt(): bbt info for offs 0x%08x: "
1217 "(block %d) 0x%02x\n", (unsigned int)offs
, res
, block
>> 1);
1225 return allowbbt
? 0 : 1;
1230 /* XXX U-BOOT XXX */
1232 EXPORT_SYMBOL(nand_scan_bbt
);
1233 EXPORT_SYMBOL(nand_default_bbt
);