1 // SPDX-License-Identifier: GPL-2.0-only
4 * Bad block table support for the NAND driver
6 * Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
10 * When nand_scan_bbt is called, then it tries to find the bad block table
11 * depending on the options in the BBT descriptor(s). If no flash based BBT
12 * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
13 * marked good / bad blocks. This information is used to create a memory BBT.
14 * Once a new bad block is discovered then the "factory" information is updated
16 * If a flash based BBT is specified then the function first tries to find the
17 * BBT on flash. If a BBT is found then the contents are read and the memory
18 * based BBT is created. If a mirrored BBT is selected then the mirror is
19 * searched too and the versions are compared. If the mirror has a greater
20 * version number, then 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 BBTs 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 auto generated 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. If the NAND
33 * controller needs the complete OOB area for the ECC information then the
34 * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
35 * course): it moves the ident pattern and the version byte into the data area
36 * and the OOB area will remain untouched.
38 * The table uses 2 bits per block
40 * 00b: block is factory marked bad
41 * 01b, 10b: block is marked bad due to wear
43 * The memory bad block table uses the following scheme:
45 * 01b: block is marked bad due to wear
46 * 10b: block is reserved (to protect the bbt area)
47 * 11b: block is factory marked bad
49 * Multichip devices like DOC store the bad block info per floor.
51 * Following assumptions are made:
52 * - bbts start at a page boundary, if autolocated on a block boundary
53 * - the space necessary for a bbt in FLASH does not exceed a block boundary
56 #include <linux/slab.h>
57 #include <linux/types.h>
58 #include <linux/mtd/mtd.h>
59 #include <linux/mtd/bbm.h>
60 #include <linux/bitops.h>
61 #include <linux/delay.h>
62 #include <linux/vmalloc.h>
63 #include <linux/export.h>
64 #include <linux/string.h>
66 #include "internals.h"
68 #define BBT_BLOCK_GOOD 0x00
69 #define BBT_BLOCK_WORN 0x01
70 #define BBT_BLOCK_RESERVED 0x02
71 #define BBT_BLOCK_FACTORY_BAD 0x03
73 #define BBT_ENTRY_MASK 0x03
74 #define BBT_ENTRY_SHIFT 2
76 static inline uint8_t bbt_get_entry(struct nand_chip
*chip
, int block
)
78 uint8_t entry
= chip
->bbt
[block
>> BBT_ENTRY_SHIFT
];
79 entry
>>= (block
& BBT_ENTRY_MASK
) * 2;
80 return entry
& BBT_ENTRY_MASK
;
83 static inline void bbt_mark_entry(struct nand_chip
*chip
, int block
,
86 uint8_t msk
= (mark
& BBT_ENTRY_MASK
) << ((block
& BBT_ENTRY_MASK
) * 2);
87 chip
->bbt
[block
>> BBT_ENTRY_SHIFT
] |= msk
;
90 static int check_pattern_no_oob(uint8_t *buf
, struct nand_bbt_descr
*td
)
92 if (memcmp(buf
, td
->pattern
, td
->len
))
98 * check_pattern - [GENERIC] check if a pattern is in the buffer
99 * @buf: the buffer to search
100 * @len: the length of buffer to search
101 * @paglen: the pagelength
102 * @td: search pattern descriptor
104 * Check for a pattern at the given place. Used to search bad block tables and
105 * good / bad block identifiers.
107 static int check_pattern(uint8_t *buf
, int len
, int paglen
, struct nand_bbt_descr
*td
)
109 if (td
->options
& NAND_BBT_NO_OOB
)
110 return check_pattern_no_oob(buf
, td
);
112 /* Compare the pattern */
113 if (memcmp(buf
+ paglen
+ td
->offs
, td
->pattern
, td
->len
))
120 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
121 * @buf: the buffer to search
122 * @td: search pattern descriptor
124 * Check for a pattern at the given place. Used to search bad block tables and
125 * good / bad block identifiers. Same as check_pattern, but no optional empty
128 static int check_short_pattern(uint8_t *buf
, struct nand_bbt_descr
*td
)
130 /* Compare the pattern */
131 if (memcmp(buf
+ td
->offs
, td
->pattern
, td
->len
))
137 * add_marker_len - compute the length of the marker in data area
138 * @td: BBT descriptor used for computation
140 * The length will be 0 if the marker is located in OOB area.
142 static u32
add_marker_len(struct nand_bbt_descr
*td
)
146 if (!(td
->options
& NAND_BBT_NO_OOB
))
150 if (td
->options
& NAND_BBT_VERSION
)
156 * read_bbt - [GENERIC] Read the bad block table starting from page
157 * @this: NAND chip object
158 * @buf: temporary buffer
159 * @page: the starting page
160 * @num: the number of bbt descriptors to read
161 * @td: the bbt describtion table
162 * @offs: block number offset in the table
164 * Read the bad block table starting from page.
166 static int read_bbt(struct nand_chip
*this, uint8_t *buf
, int page
, int num
,
167 struct nand_bbt_descr
*td
, int offs
)
169 struct mtd_info
*mtd
= nand_to_mtd(this);
170 int res
, ret
= 0, i
, j
, act
= 0;
171 size_t retlen
, len
, totlen
;
173 int bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
174 uint8_t msk
= (uint8_t)((1 << bits
) - 1);
176 int reserved_block_code
= td
->reserved_block_code
;
178 totlen
= (num
* bits
) >> 3;
179 marker_len
= add_marker_len(td
);
180 from
= ((loff_t
)page
) << this->page_shift
;
183 len
= min(totlen
, (size_t)(1 << this->bbt_erase_shift
));
186 * In case the BBT marker is not in the OOB area it
187 * will be just in the first page.
193 res
= mtd_read(mtd
, from
, len
, &retlen
, buf
);
195 if (mtd_is_eccerr(res
)) {
196 pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
197 from
& ~mtd
->writesize
);
199 } else if (mtd_is_bitflip(res
)) {
200 pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
201 from
& ~mtd
->writesize
);
204 pr_info("nand_bbt: error reading BBT\n");
210 for (i
= 0; i
< len
; i
++) {
211 uint8_t dat
= buf
[i
];
212 for (j
= 0; j
< 8; j
+= bits
, act
++) {
213 uint8_t tmp
= (dat
>> j
) & msk
;
216 if (reserved_block_code
&& (tmp
== reserved_block_code
)) {
217 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
218 (loff_t
)(offs
+ act
) <<
219 this->bbt_erase_shift
);
220 bbt_mark_entry(this, offs
+ act
,
222 mtd
->ecc_stats
.bbtblocks
++;
226 * Leave it for now, if it's matured we can
227 * move this message to pr_debug.
229 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
230 (loff_t
)(offs
+ act
) <<
231 this->bbt_erase_shift
);
232 /* Factory marked bad or worn out? */
234 bbt_mark_entry(this, offs
+ act
,
235 BBT_BLOCK_FACTORY_BAD
);
237 bbt_mark_entry(this, offs
+ act
,
239 mtd
->ecc_stats
.badblocks
++;
249 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
250 * @this: NAND chip object
251 * @buf: temporary buffer
252 * @td: descriptor for the bad block table
253 * @chip: read the table for a specific chip, -1 read all chips; applies only if
254 * NAND_BBT_PERCHIP option is set
256 * Read the bad block table for all chips starting at a given page. We assume
257 * that the bbt bits are in consecutive order.
259 static int read_abs_bbt(struct nand_chip
*this, uint8_t *buf
,
260 struct nand_bbt_descr
*td
, int chip
)
262 struct mtd_info
*mtd
= nand_to_mtd(this);
263 u64 targetsize
= nanddev_target_size(&this->base
);
266 if (td
->options
& NAND_BBT_PERCHIP
) {
268 for (i
= 0; i
< nanddev_ntargets(&this->base
); i
++) {
269 if (chip
== -1 || chip
== i
)
270 res
= read_bbt(this, buf
, td
->pages
[i
],
271 targetsize
>> this->bbt_erase_shift
,
275 offs
+= targetsize
>> this->bbt_erase_shift
;
278 res
= read_bbt(this, buf
, td
->pages
[0],
279 mtd
->size
>> this->bbt_erase_shift
, td
, 0);
286 /* BBT marker is in the first page, no OOB */
287 static int scan_read_data(struct nand_chip
*this, uint8_t *buf
, loff_t offs
,
288 struct nand_bbt_descr
*td
)
290 struct mtd_info
*mtd
= nand_to_mtd(this);
295 if (td
->options
& NAND_BBT_VERSION
)
298 return mtd_read(mtd
, offs
, len
, &retlen
, buf
);
302 * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
303 * @this: NAND chip object
304 * @buf: temporary buffer
305 * @offs: offset at which to scan
306 * @len: length of data region to read
308 * Scan read data from data+OOB. May traverse multiple pages, interleaving
309 * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"
310 * ECC condition (error or bitflip). May quit on the first (non-ECC) error.
312 static int scan_read_oob(struct nand_chip
*this, uint8_t *buf
, loff_t offs
,
315 struct mtd_info
*mtd
= nand_to_mtd(this);
316 struct mtd_oob_ops ops
;
319 ops
.mode
= MTD_OPS_PLACE_OOB
;
321 ops
.ooblen
= mtd
->oobsize
;
325 ops
.len
= min(len
, (size_t)mtd
->writesize
);
326 ops
.oobbuf
= buf
+ ops
.len
;
328 res
= mtd_read_oob(mtd
, offs
, &ops
);
330 if (!mtd_is_bitflip_or_eccerr(res
))
332 else if (mtd_is_eccerr(res
) || !ret
)
336 buf
+= mtd
->oobsize
+ mtd
->writesize
;
337 len
-= mtd
->writesize
;
338 offs
+= mtd
->writesize
;
343 static int scan_read(struct nand_chip
*this, uint8_t *buf
, loff_t offs
,
344 size_t len
, struct nand_bbt_descr
*td
)
346 if (td
->options
& NAND_BBT_NO_OOB
)
347 return scan_read_data(this, buf
, offs
, td
);
349 return scan_read_oob(this, buf
, offs
, len
);
352 /* Scan write data with oob to flash */
353 static int scan_write_bbt(struct nand_chip
*this, loff_t offs
, size_t len
,
354 uint8_t *buf
, uint8_t *oob
)
356 struct mtd_info
*mtd
= nand_to_mtd(this);
357 struct mtd_oob_ops ops
;
359 ops
.mode
= MTD_OPS_PLACE_OOB
;
361 ops
.ooblen
= mtd
->oobsize
;
366 return mtd_write_oob(mtd
, offs
, &ops
);
369 static u32
bbt_get_ver_offs(struct nand_chip
*this, struct nand_bbt_descr
*td
)
371 struct mtd_info
*mtd
= nand_to_mtd(this);
372 u32 ver_offs
= td
->veroffs
;
374 if (!(td
->options
& NAND_BBT_NO_OOB
))
375 ver_offs
+= mtd
->writesize
;
380 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
381 * @this: NAND chip object
382 * @buf: temporary buffer
383 * @td: descriptor for the bad block table
384 * @md: descriptor for the bad block table mirror
386 * Read the bad block table(s) for all chips starting at a given page. We
387 * assume that the bbt bits are in consecutive order.
389 static void read_abs_bbts(struct nand_chip
*this, uint8_t *buf
,
390 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
)
392 struct mtd_info
*mtd
= nand_to_mtd(this);
394 /* Read the primary version, if available */
395 if (td
->options
& NAND_BBT_VERSION
) {
396 scan_read(this, buf
, (loff_t
)td
->pages
[0] << this->page_shift
,
398 td
->version
[0] = buf
[bbt_get_ver_offs(this, td
)];
399 pr_info("Bad block table at page %d, version 0x%02X\n",
400 td
->pages
[0], td
->version
[0]);
403 /* Read the mirror version, if available */
404 if (md
&& (md
->options
& NAND_BBT_VERSION
)) {
405 scan_read(this, buf
, (loff_t
)md
->pages
[0] << this->page_shift
,
407 md
->version
[0] = buf
[bbt_get_ver_offs(this, md
)];
408 pr_info("Bad block table at page %d, version 0x%02X\n",
409 md
->pages
[0], md
->version
[0]);
413 /* Scan a given block partially */
414 static int scan_block_fast(struct nand_chip
*this, struct nand_bbt_descr
*bd
,
415 loff_t offs
, uint8_t *buf
)
417 struct mtd_info
*mtd
= nand_to_mtd(this);
419 struct mtd_oob_ops ops
;
420 int ret
, page_offset
;
422 ops
.ooblen
= mtd
->oobsize
;
426 ops
.mode
= MTD_OPS_PLACE_OOB
;
428 page_offset
= nand_bbm_get_next_page(this, 0);
430 while (page_offset
>= 0) {
432 * Read the full oob until read_oob is fixed to handle single
433 * byte reads for 16 bit buswidth.
435 ret
= mtd_read_oob(mtd
, offs
+ (page_offset
* mtd
->writesize
),
437 /* Ignore ECC errors when checking for BBM */
438 if (ret
&& !mtd_is_bitflip_or_eccerr(ret
))
441 if (check_short_pattern(buf
, bd
))
444 page_offset
= nand_bbm_get_next_page(this, page_offset
+ 1);
451 * create_bbt - [GENERIC] Create a bad block table by scanning the device
452 * @this: NAND chip object
453 * @buf: temporary buffer
454 * @bd: descriptor for the good/bad block search pattern
455 * @chip: create the table for a specific chip, -1 read all chips; applies only
456 * if NAND_BBT_PERCHIP option is set
458 * Create a bad block table by scanning the device for the given good/bad block
461 static int create_bbt(struct nand_chip
*this, uint8_t *buf
,
462 struct nand_bbt_descr
*bd
, int chip
)
464 u64 targetsize
= nanddev_target_size(&this->base
);
465 struct mtd_info
*mtd
= nand_to_mtd(this);
466 int i
, numblocks
, startblock
;
469 pr_info("Scanning device for bad blocks\n");
472 numblocks
= mtd
->size
>> this->bbt_erase_shift
;
476 if (chip
>= nanddev_ntargets(&this->base
)) {
477 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
478 chip
+ 1, nanddev_ntargets(&this->base
));
481 numblocks
= targetsize
>> this->bbt_erase_shift
;
482 startblock
= chip
* numblocks
;
483 numblocks
+= startblock
;
484 from
= (loff_t
)startblock
<< this->bbt_erase_shift
;
487 for (i
= startblock
; i
< numblocks
; i
++) {
490 BUG_ON(bd
->options
& NAND_BBT_NO_OOB
);
492 ret
= scan_block_fast(this, bd
, from
, buf
);
497 bbt_mark_entry(this, i
, BBT_BLOCK_FACTORY_BAD
);
498 pr_warn("Bad eraseblock %d at 0x%012llx\n",
499 i
, (unsigned long long)from
);
500 mtd
->ecc_stats
.badblocks
++;
503 from
+= (1 << this->bbt_erase_shift
);
509 * search_bbt - [GENERIC] scan the device for a specific bad block table
510 * @this: NAND chip object
511 * @buf: temporary buffer
512 * @td: descriptor for the bad block table
514 * Read the bad block table by searching for a given ident pattern. Search is
515 * preformed either from the beginning up or from the end of the device
516 * downwards. The search starts always at the start of a block. If the option
517 * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
518 * the bad block information of this chip. This is necessary to provide support
519 * for certain DOC devices.
521 * The bbt ident pattern resides in the oob area of the first page in a block.
523 static int search_bbt(struct nand_chip
*this, uint8_t *buf
,
524 struct nand_bbt_descr
*td
)
526 u64 targetsize
= nanddev_target_size(&this->base
);
527 struct mtd_info
*mtd
= nand_to_mtd(this);
529 int startblock
, block
, dir
;
530 int scanlen
= mtd
->writesize
+ mtd
->oobsize
;
532 int blocktopage
= this->bbt_erase_shift
- this->page_shift
;
534 /* Search direction top -> down? */
535 if (td
->options
& NAND_BBT_LASTBLOCK
) {
536 startblock
= (mtd
->size
>> this->bbt_erase_shift
) - 1;
543 /* Do we have a bbt per chip? */
544 if (td
->options
& NAND_BBT_PERCHIP
) {
545 chips
= nanddev_ntargets(&this->base
);
546 bbtblocks
= targetsize
>> this->bbt_erase_shift
;
547 startblock
&= bbtblocks
- 1;
550 bbtblocks
= mtd
->size
>> this->bbt_erase_shift
;
553 for (i
= 0; i
< chips
; i
++) {
554 /* Reset version information */
557 /* Scan the maximum number of blocks */
558 for (block
= 0; block
< td
->maxblocks
; block
++) {
560 int actblock
= startblock
+ dir
* block
;
561 loff_t offs
= (loff_t
)actblock
<< this->bbt_erase_shift
;
563 /* Read first page */
564 scan_read(this, buf
, offs
, mtd
->writesize
, td
);
565 if (!check_pattern(buf
, scanlen
, mtd
->writesize
, td
)) {
566 td
->pages
[i
] = actblock
<< blocktopage
;
567 if (td
->options
& NAND_BBT_VERSION
) {
568 offs
= bbt_get_ver_offs(this, td
);
569 td
->version
[i
] = buf
[offs
];
574 startblock
+= targetsize
>> this->bbt_erase_shift
;
576 /* Check, if we found a bbt for each requested chip */
577 for (i
= 0; i
< chips
; i
++) {
578 if (td
->pages
[i
] == -1)
579 pr_warn("Bad block table not found for chip %d\n", i
);
581 pr_info("Bad block table found at page %d, version 0x%02X\n",
582 td
->pages
[i
], td
->version
[i
]);
588 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
589 * @this: NAND chip object
590 * @buf: temporary buffer
591 * @td: descriptor for the bad block table
592 * @md: descriptor for the bad block table mirror
594 * Search and read the bad block table(s).
596 static void search_read_bbts(struct nand_chip
*this, uint8_t *buf
,
597 struct nand_bbt_descr
*td
,
598 struct nand_bbt_descr
*md
)
600 /* Search the primary table */
601 search_bbt(this, buf
, td
);
603 /* Search the mirror table */
605 search_bbt(this, buf
, md
);
609 * get_bbt_block - Get the first valid eraseblock suitable to store a BBT
610 * @this: the NAND device
611 * @td: the BBT description
612 * @md: the mirror BBT descriptor
613 * @chip: the CHIP selector
615 * This functions returns a positive block number pointing a valid eraseblock
616 * suitable to store a BBT (i.e. in the range reserved for BBT), or -ENOSPC if
617 * all blocks are already used of marked bad. If td->pages[chip] was already
618 * pointing to a valid block we re-use it, otherwise we search for the next
621 static int get_bbt_block(struct nand_chip
*this, struct nand_bbt_descr
*td
,
622 struct nand_bbt_descr
*md
, int chip
)
624 u64 targetsize
= nanddev_target_size(&this->base
);
625 int startblock
, dir
, page
, numblocks
, i
;
628 * There was already a version of the table, reuse the page. This
629 * applies for absolute placement too, as we have the page number in
632 if (td
->pages
[chip
] != -1)
633 return td
->pages
[chip
] >>
634 (this->bbt_erase_shift
- this->page_shift
);
636 numblocks
= (int)(targetsize
>> this->bbt_erase_shift
);
637 if (!(td
->options
& NAND_BBT_PERCHIP
))
638 numblocks
*= nanddev_ntargets(&this->base
);
641 * Automatic placement of the bad block table. Search direction
644 if (td
->options
& NAND_BBT_LASTBLOCK
) {
645 startblock
= numblocks
* (chip
+ 1) - 1;
648 startblock
= chip
* numblocks
;
652 for (i
= 0; i
< td
->maxblocks
; i
++) {
653 int block
= startblock
+ dir
* i
;
655 /* Check, if the block is bad */
656 switch (bbt_get_entry(this, block
)) {
658 case BBT_BLOCK_FACTORY_BAD
:
662 page
= block
<< (this->bbt_erase_shift
- this->page_shift
);
664 /* Check, if the block is used by the mirror table */
665 if (!md
|| md
->pages
[chip
] != page
)
673 * mark_bbt_block_bad - Mark one of the block reserved for BBT bad
674 * @this: the NAND device
675 * @td: the BBT description
676 * @chip: the CHIP selector
677 * @block: the BBT block to mark
679 * Blocks reserved for BBT can become bad. This functions is an helper to mark
680 * such blocks as bad. It takes care of updating the in-memory BBT, marking the
681 * block as bad using a bad block marker and invalidating the associated
684 static void mark_bbt_block_bad(struct nand_chip
*this,
685 struct nand_bbt_descr
*td
,
691 bbt_mark_entry(this, block
, BBT_BLOCK_WORN
);
693 to
= (loff_t
)block
<< this->bbt_erase_shift
;
694 res
= nand_markbad_bbm(this, to
);
696 pr_warn("nand_bbt: error %d while marking block %d bad\n",
699 td
->pages
[chip
] = -1;
703 * write_bbt - [GENERIC] (Re)write the bad block table
704 * @this: NAND chip object
705 * @buf: temporary buffer
706 * @td: descriptor for the bad block table
707 * @md: descriptor for the bad block table mirror
708 * @chipsel: selector for a specific chip, -1 for all
710 * (Re)write the bad block table.
712 static int write_bbt(struct nand_chip
*this, uint8_t *buf
,
713 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
,
716 u64 targetsize
= nanddev_target_size(&this->base
);
717 struct mtd_info
*mtd
= nand_to_mtd(this);
718 struct erase_info einfo
;
719 int i
, res
, chip
= 0;
720 int bits
, page
, offs
, numblocks
, sft
, sftmsk
;
721 int nrchips
, pageoffs
, ooboffs
;
723 uint8_t rcode
= td
->reserved_block_code
;
724 size_t retlen
, len
= 0;
726 struct mtd_oob_ops ops
;
728 ops
.ooblen
= mtd
->oobsize
;
731 ops
.mode
= MTD_OPS_PLACE_OOB
;
735 /* Write bad block table per chip rather than per device? */
736 if (td
->options
& NAND_BBT_PERCHIP
) {
737 numblocks
= (int)(targetsize
>> this->bbt_erase_shift
);
738 /* Full device write or specific chip? */
740 nrchips
= nanddev_ntargets(&this->base
);
742 nrchips
= chipsel
+ 1;
746 numblocks
= (int)(mtd
->size
>> this->bbt_erase_shift
);
750 /* Loop through the chips */
751 while (chip
< nrchips
) {
754 block
= get_bbt_block(this, td
, md
, chip
);
756 pr_err("No space left to write bad block table\n");
762 * get_bbt_block() returns a block number, shift the value to
765 page
= block
<< (this->bbt_erase_shift
- this->page_shift
);
767 /* Set up shift count and masks for the flash table */
768 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
771 case 1: sft
= 3; sftmsk
= 0x07; msk
[0] = 0x00; msk
[1] = 0x01;
774 case 2: sft
= 2; sftmsk
= 0x06; msk
[0] = 0x00; msk
[1] = 0x01;
777 case 4: sft
= 1; sftmsk
= 0x04; msk
[0] = 0x00; msk
[1] = 0x0C;
780 case 8: sft
= 0; sftmsk
= 0x00; msk
[0] = 0x00; msk
[1] = 0x0F;
783 default: return -EINVAL
;
786 to
= ((loff_t
)page
) << this->page_shift
;
788 /* Must we save the block contents? */
789 if (td
->options
& NAND_BBT_SAVECONTENT
) {
790 /* Make it block aligned */
791 to
&= ~(((loff_t
)1 << this->bbt_erase_shift
) - 1);
792 len
= 1 << this->bbt_erase_shift
;
793 res
= mtd_read(mtd
, to
, len
, &retlen
, buf
);
796 pr_info("nand_bbt: error reading block for writing the bad block table\n");
799 pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
802 ops
.ooblen
= (len
>> this->page_shift
) * mtd
->oobsize
;
803 ops
.oobbuf
= &buf
[len
];
804 res
= mtd_read_oob(mtd
, to
+ mtd
->writesize
, &ops
);
805 if (res
< 0 || ops
.oobretlen
!= ops
.ooblen
)
808 /* Calc the byte offset in the buffer */
809 pageoffs
= page
- (int)(to
>> this->page_shift
);
810 offs
= pageoffs
<< this->page_shift
;
811 /* Preset the bbt area with 0xff */
812 memset(&buf
[offs
], 0xff, (size_t)(numblocks
>> sft
));
813 ooboffs
= len
+ (pageoffs
* mtd
->oobsize
);
815 } else if (td
->options
& NAND_BBT_NO_OOB
) {
818 /* The version byte */
819 if (td
->options
& NAND_BBT_VERSION
)
822 len
= (size_t)(numblocks
>> sft
);
824 /* Make it page aligned! */
825 len
= ALIGN(len
, mtd
->writesize
);
826 /* Preset the buffer with 0xff */
827 memset(buf
, 0xff, len
);
828 /* Pattern is located at the begin of first page */
829 memcpy(buf
, td
->pattern
, td
->len
);
832 len
= (size_t)(numblocks
>> sft
);
833 /* Make it page aligned! */
834 len
= ALIGN(len
, mtd
->writesize
);
835 /* Preset the buffer with 0xff */
836 memset(buf
, 0xff, len
+
837 (len
>> this->page_shift
)* mtd
->oobsize
);
840 /* Pattern is located in oob area of first page */
841 memcpy(&buf
[ooboffs
+ td
->offs
], td
->pattern
, td
->len
);
844 if (td
->options
& NAND_BBT_VERSION
)
845 buf
[ooboffs
+ td
->veroffs
] = td
->version
[chip
];
847 /* Walk through the memory table */
848 for (i
= 0; i
< numblocks
; i
++) {
850 int sftcnt
= (i
<< (3 - sft
)) & sftmsk
;
851 dat
= bbt_get_entry(this, chip
* numblocks
+ i
);
852 /* Do not store the reserved bbt blocks! */
853 buf
[offs
+ (i
>> sft
)] &= ~(msk
[dat
] << sftcnt
);
856 memset(&einfo
, 0, sizeof(einfo
));
858 einfo
.len
= 1 << this->bbt_erase_shift
;
859 res
= nand_erase_nand(this, &einfo
, 1);
861 pr_warn("nand_bbt: error while erasing BBT block %d\n",
863 mark_bbt_block_bad(this, td
, chip
, block
);
867 res
= scan_write_bbt(this, to
, len
, buf
,
868 td
->options
& NAND_BBT_NO_OOB
?
871 pr_warn("nand_bbt: error while writing BBT block %d\n",
873 mark_bbt_block_bad(this, td
, chip
, block
);
877 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
878 (unsigned long long)to
, td
->version
[chip
]);
880 /* Mark it as used */
881 td
->pages
[chip
++] = page
;
886 pr_warn("nand_bbt: error while writing bad block table %d\n", res
);
891 * nand_memory_bbt - [GENERIC] create a memory based bad block table
892 * @this: NAND chip object
893 * @bd: descriptor for the good/bad block search pattern
895 * The function creates a memory based bbt by scanning the device for
896 * manufacturer / software marked good / bad blocks.
898 static inline int nand_memory_bbt(struct nand_chip
*this,
899 struct nand_bbt_descr
*bd
)
901 u8
*pagebuf
= nand_get_data_buf(this);
903 return create_bbt(this, pagebuf
, bd
, -1);
907 * check_create - [GENERIC] create and write bbt(s) if necessary
908 * @this: the NAND device
909 * @buf: temporary buffer
910 * @bd: descriptor for the good/bad block search pattern
912 * The function checks the results of the previous call to read_bbt and creates
913 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
914 * for the chip/device. Update is necessary if one of the tables is missing or
915 * the version nr. of one table is less than the other.
917 static int check_create(struct nand_chip
*this, uint8_t *buf
,
918 struct nand_bbt_descr
*bd
)
920 int i
, chips
, writeops
, create
, chipsel
, res
, res2
;
921 struct nand_bbt_descr
*td
= this->bbt_td
;
922 struct nand_bbt_descr
*md
= this->bbt_md
;
923 struct nand_bbt_descr
*rd
, *rd2
;
925 /* Do we have a bbt per chip? */
926 if (td
->options
& NAND_BBT_PERCHIP
)
927 chips
= nanddev_ntargets(&this->base
);
931 for (i
= 0; i
< chips
; i
++) {
937 /* Per chip or per device? */
938 chipsel
= (td
->options
& NAND_BBT_PERCHIP
) ? i
: -1;
939 /* Mirrored table available? */
941 if (td
->pages
[i
] == -1 && md
->pages
[i
] == -1) {
944 } else if (td
->pages
[i
] == -1) {
947 } else if (md
->pages
[i
] == -1) {
950 } else if (td
->version
[i
] == md
->version
[i
]) {
952 if (!(td
->options
& NAND_BBT_VERSION
))
954 } else if (((int8_t)(td
->version
[i
] - md
->version
[i
])) > 0) {
962 if (td
->pages
[i
] == -1) {
971 /* Create the bad block table by scanning the device? */
972 if (!(td
->options
& NAND_BBT_CREATE
))
975 /* Create the table in memory by scanning the chip(s) */
976 if (!(this->bbt_options
& NAND_BBT_CREATE_EMPTY
))
977 create_bbt(this, buf
, bd
, chipsel
);
984 /* Read back first? */
986 res
= read_abs_bbt(this, buf
, rd
, chipsel
);
987 if (mtd_is_eccerr(res
)) {
988 /* Mark table as invalid */
995 /* If they weren't versioned, read both */
997 res2
= read_abs_bbt(this, buf
, rd2
, chipsel
);
998 if (mtd_is_eccerr(res2
)) {
999 /* Mark table as invalid */
1001 rd2
->version
[i
] = 0;
1007 /* Scrub the flash table(s)? */
1008 if (mtd_is_bitflip(res
) || mtd_is_bitflip(res2
))
1011 /* Update version numbers before writing */
1013 td
->version
[i
] = max(td
->version
[i
], md
->version
[i
]);
1014 md
->version
[i
] = td
->version
[i
];
1017 /* Write the bad block table to the device? */
1018 if ((writeops
& 0x01) && (td
->options
& NAND_BBT_WRITE
)) {
1019 res
= write_bbt(this, 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(this, buf
, md
, td
, chipsel
);
1035 * nand_update_bbt - update bad block table(s)
1036 * @this: the NAND device
1037 * @offs: the offset of the newly marked block
1039 * The function updates the bad block table(s).
1041 static int nand_update_bbt(struct nand_chip
*this, loff_t offs
)
1043 struct mtd_info
*mtd
= nand_to_mtd(this);
1047 struct nand_bbt_descr
*td
= this->bbt_td
;
1048 struct nand_bbt_descr
*md
= this->bbt_md
;
1050 if (!this->bbt
|| !td
)
1053 /* Allocate a temporary buffer for one eraseblock incl. oob */
1054 len
= (1 << this->bbt_erase_shift
);
1055 len
+= (len
>> this->page_shift
) * mtd
->oobsize
;
1056 buf
= kmalloc(len
, GFP_KERNEL
);
1060 /* Do we have a bbt per chip? */
1061 if (td
->options
& NAND_BBT_PERCHIP
) {
1062 chip
= (int)(offs
>> this->chip_shift
);
1069 td
->version
[chip
]++;
1071 md
->version
[chip
]++;
1073 /* Write the bad block table to the device? */
1074 if (td
->options
& NAND_BBT_WRITE
) {
1075 res
= write_bbt(this, buf
, td
, md
, chipsel
);
1079 /* Write the mirror bad block table to the device? */
1080 if (md
&& (md
->options
& NAND_BBT_WRITE
)) {
1081 res
= write_bbt(this, buf
, md
, td
, chipsel
);
1090 * mark_bbt_region - [GENERIC] mark the bad block table regions
1091 * @this: the NAND device
1092 * @td: bad block table descriptor
1094 * The bad block table regions are marked as "bad" to prevent accidental
1095 * erasures / writes. The regions are identified by the mark 0x02.
1097 static void mark_bbt_region(struct nand_chip
*this, struct nand_bbt_descr
*td
)
1099 u64 targetsize
= nanddev_target_size(&this->base
);
1100 struct mtd_info
*mtd
= nand_to_mtd(this);
1101 int i
, j
, chips
, block
, nrblocks
, update
;
1104 /* Do we have a bbt per chip? */
1105 if (td
->options
& NAND_BBT_PERCHIP
) {
1106 chips
= nanddev_ntargets(&this->base
);
1107 nrblocks
= (int)(targetsize
>> this->bbt_erase_shift
);
1110 nrblocks
= (int)(mtd
->size
>> this->bbt_erase_shift
);
1113 for (i
= 0; i
< chips
; i
++) {
1114 if ((td
->options
& NAND_BBT_ABSPAGE
) ||
1115 !(td
->options
& NAND_BBT_WRITE
)) {
1116 if (td
->pages
[i
] == -1)
1118 block
= td
->pages
[i
] >> (this->bbt_erase_shift
- this->page_shift
);
1119 oldval
= bbt_get_entry(this, block
);
1120 bbt_mark_entry(this, block
, BBT_BLOCK_RESERVED
);
1121 if ((oldval
!= BBT_BLOCK_RESERVED
) &&
1122 td
->reserved_block_code
)
1123 nand_update_bbt(this, (loff_t
)block
<<
1124 this->bbt_erase_shift
);
1128 if (td
->options
& NAND_BBT_LASTBLOCK
)
1129 block
= ((i
+ 1) * nrblocks
) - td
->maxblocks
;
1131 block
= i
* nrblocks
;
1132 for (j
= 0; j
< td
->maxblocks
; j
++) {
1133 oldval
= bbt_get_entry(this, block
);
1134 bbt_mark_entry(this, block
, BBT_BLOCK_RESERVED
);
1135 if (oldval
!= BBT_BLOCK_RESERVED
)
1140 * If we want reserved blocks to be recorded to flash, and some
1141 * new ones have been marked, then we need to update the stored
1142 * bbts. This should only happen once.
1144 if (update
&& td
->reserved_block_code
)
1145 nand_update_bbt(this, (loff_t
)(block
- 1) <<
1146 this->bbt_erase_shift
);
1151 * verify_bbt_descr - verify the bad block description
1152 * @this: the NAND device
1153 * @bd: the table to verify
1155 * This functions performs a few sanity checks on the bad block description
1158 static void verify_bbt_descr(struct nand_chip
*this, struct nand_bbt_descr
*bd
)
1160 u64 targetsize
= nanddev_target_size(&this->base
);
1161 struct mtd_info
*mtd
= nand_to_mtd(this);
1169 pattern_len
= bd
->len
;
1170 bits
= bd
->options
& NAND_BBT_NRBITS_MSK
;
1172 BUG_ON((this->bbt_options
& NAND_BBT_NO_OOB
) &&
1173 !(this->bbt_options
& NAND_BBT_USE_FLASH
));
1176 if (bd
->options
& NAND_BBT_VERSION
)
1179 if (bd
->options
& NAND_BBT_NO_OOB
) {
1180 BUG_ON(!(this->bbt_options
& NAND_BBT_USE_FLASH
));
1181 BUG_ON(!(this->bbt_options
& NAND_BBT_NO_OOB
));
1183 if (bd
->options
& NAND_BBT_VERSION
)
1184 BUG_ON(bd
->veroffs
!= bd
->len
);
1185 BUG_ON(bd
->options
& NAND_BBT_SAVECONTENT
);
1188 if (bd
->options
& NAND_BBT_PERCHIP
)
1189 table_size
= targetsize
>> this->bbt_erase_shift
;
1191 table_size
= mtd
->size
>> this->bbt_erase_shift
;
1194 if (bd
->options
& NAND_BBT_NO_OOB
)
1195 table_size
+= pattern_len
;
1196 BUG_ON(table_size
> (1 << this->bbt_erase_shift
));
1200 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1201 * @this: the NAND device
1202 * @bd: descriptor for the good/bad block search pattern
1204 * The function checks, if a bad block table(s) is/are already available. If
1205 * not it scans the device for manufacturer marked good / bad blocks and writes
1206 * the bad block table(s) to the selected place.
1208 * The bad block table memory is allocated here. It must be freed by calling
1209 * the nand_free_bbt function.
1211 static int nand_scan_bbt(struct nand_chip
*this, struct nand_bbt_descr
*bd
)
1213 struct mtd_info
*mtd
= nand_to_mtd(this);
1216 struct nand_bbt_descr
*td
= this->bbt_td
;
1217 struct nand_bbt_descr
*md
= this->bbt_md
;
1219 len
= (mtd
->size
>> (this->bbt_erase_shift
+ 2)) ? : 1;
1221 * Allocate memory (2bit per block) and clear the memory bad block
1224 this->bbt
= kzalloc(len
, GFP_KERNEL
);
1229 * If no primary table descriptor is given, scan the device to build a
1230 * memory based bad block table.
1233 if ((res
= nand_memory_bbt(this, bd
))) {
1234 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
1239 verify_bbt_descr(this, td
);
1240 verify_bbt_descr(this, md
);
1242 /* Allocate a temporary buffer for one eraseblock incl. oob */
1243 len
= (1 << this->bbt_erase_shift
);
1244 len
+= (len
>> this->page_shift
) * mtd
->oobsize
;
1251 /* Is the bbt at a given page? */
1252 if (td
->options
& NAND_BBT_ABSPAGE
) {
1253 read_abs_bbts(this, buf
, td
, md
);
1255 /* Search the bad block table using a pattern in oob */
1256 search_read_bbts(this, buf
, td
, md
);
1259 res
= check_create(this, buf
, bd
);
1263 /* Prevent the bbt regions from erasing / writing */
1264 mark_bbt_region(this, td
);
1266 mark_bbt_region(this, md
);
1280 * Define some generic bad / good block scan pattern which are used
1281 * while scanning a device for factory marked good / bad blocks.
1283 static uint8_t scan_ff_pattern
[] = { 0xff, 0xff };
1285 /* Generic flash bbt descriptors */
1286 static uint8_t bbt_pattern
[] = {'B', 'b', 't', '0' };
1287 static uint8_t mirror_pattern
[] = {'1', 't', 'b', 'B' };
1289 static struct nand_bbt_descr bbt_main_descr
= {
1290 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1291 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
1295 .maxblocks
= NAND_BBT_SCAN_MAXBLOCKS
,
1296 .pattern
= bbt_pattern
1299 static struct nand_bbt_descr bbt_mirror_descr
= {
1300 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1301 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
1305 .maxblocks
= NAND_BBT_SCAN_MAXBLOCKS
,
1306 .pattern
= mirror_pattern
1309 static struct nand_bbt_descr bbt_main_no_oob_descr
= {
1310 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1311 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
1315 .maxblocks
= NAND_BBT_SCAN_MAXBLOCKS
,
1316 .pattern
= bbt_pattern
1319 static struct nand_bbt_descr bbt_mirror_no_oob_descr
= {
1320 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1321 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
1325 .maxblocks
= NAND_BBT_SCAN_MAXBLOCKS
,
1326 .pattern
= mirror_pattern
1329 #define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
1331 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
1332 * @this: NAND chip to create descriptor for
1334 * This function allocates and initializes a nand_bbt_descr for BBM detection
1335 * based on the properties of @this. The new descriptor is stored in
1336 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1337 * passed to this function.
1339 static int nand_create_badblock_pattern(struct nand_chip
*this)
1341 struct nand_bbt_descr
*bd
;
1342 if (this->badblock_pattern
) {
1343 pr_warn("Bad block pattern already allocated; not replacing\n");
1346 bd
= kzalloc(sizeof(*bd
), GFP_KERNEL
);
1349 bd
->options
= this->bbt_options
& BADBLOCK_SCAN_MASK
;
1350 bd
->offs
= this->badblockpos
;
1351 bd
->len
= (this->options
& NAND_BUSWIDTH_16
) ? 2 : 1;
1352 bd
->pattern
= scan_ff_pattern
;
1353 bd
->options
|= NAND_BBT_DYNAMICSTRUCT
;
1354 this->badblock_pattern
= bd
;
1359 * nand_create_bbt - [NAND Interface] Select a default bad block table for the device
1360 * @this: NAND chip object
1362 * This function selects the default bad block table support for the device and
1363 * calls the nand_scan_bbt function.
1365 int nand_create_bbt(struct nand_chip
*this)
1369 /* Is a flash based bad block table requested? */
1370 if (this->bbt_options
& NAND_BBT_USE_FLASH
) {
1371 /* Use the default pattern descriptors */
1372 if (!this->bbt_td
) {
1373 if (this->bbt_options
& NAND_BBT_NO_OOB
) {
1374 this->bbt_td
= &bbt_main_no_oob_descr
;
1375 this->bbt_md
= &bbt_mirror_no_oob_descr
;
1377 this->bbt_td
= &bbt_main_descr
;
1378 this->bbt_md
= &bbt_mirror_descr
;
1382 this->bbt_td
= NULL
;
1383 this->bbt_md
= NULL
;
1386 if (!this->badblock_pattern
) {
1387 ret
= nand_create_badblock_pattern(this);
1392 return nand_scan_bbt(this, this->badblock_pattern
);
1394 EXPORT_SYMBOL(nand_create_bbt
);
1397 * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
1398 * @this: NAND chip object
1399 * @offs: offset in the device
1401 int nand_isreserved_bbt(struct nand_chip
*this, loff_t offs
)
1405 block
= (int)(offs
>> this->bbt_erase_shift
);
1406 return bbt_get_entry(this, block
) == BBT_BLOCK_RESERVED
;
1410 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1411 * @this: NAND chip object
1412 * @offs: offset in the device
1413 * @allowbbt: allow access to bad block table region
1415 int nand_isbad_bbt(struct nand_chip
*this, loff_t offs
, int allowbbt
)
1419 block
= (int)(offs
>> this->bbt_erase_shift
);
1420 res
= bbt_get_entry(this, block
);
1422 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1423 (unsigned int)offs
, block
, res
);
1426 case BBT_BLOCK_GOOD
:
1428 case BBT_BLOCK_WORN
:
1430 case BBT_BLOCK_RESERVED
:
1431 return allowbbt
? 0 : 1;
1437 * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
1438 * @this: NAND chip object
1439 * @offs: offset of the bad block
1441 int nand_markbad_bbt(struct nand_chip
*this, loff_t offs
)
1445 block
= (int)(offs
>> this->bbt_erase_shift
);
1447 /* Mark bad block in memory */
1448 bbt_mark_entry(this, block
, BBT_BLOCK_WORN
);
1450 /* Update flash-based bad block table */
1451 if (this->bbt_options
& NAND_BBT_USE_FLASH
)
1452 ret
= nand_update_bbt(this, offs
);