3 * Bad block table support for the NAND driver
5 * Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 * When nand_scan_bbt is called, then it tries to find the bad block table
14 * depending on the options in the BBT descriptor(s). If no flash based BBT
15 * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
16 * marked good / bad blocks. This information is used to create a memory BBT.
17 * Once a new bad block is discovered then the "factory" information is updated
19 * If a flash based BBT is specified then the function first tries to find the
20 * BBT on flash. If a BBT is found then the contents are read and the memory
21 * based BBT is created. If a mirrored BBT is selected then the mirror is
22 * searched too and the versions are compared. If the mirror has a greater
23 * version number, then the mirror BBT is used to build the memory based BBT.
24 * If the tables are not versioned, then we "or" the bad block information.
25 * If one of the BBTs is out of date or does not exist it is (re)created.
26 * If no BBT exists at all then the device is scanned for factory marked
27 * good / bad blocks and the bad block tables are created.
29 * For manufacturer created BBTs like the one found on M-SYS DOC devices
30 * the BBT is searched and read but never created
32 * The auto generated bad block table is located in the last good blocks
33 * of the device. The table is mirrored, so it can be updated eventually.
34 * The table is marked in the OOB area with an ident pattern and a version
35 * number which indicates which of both tables is more up to date. If the NAND
36 * controller needs the complete OOB area for the ECC information then the
37 * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
38 * course): it moves the ident pattern and the version byte into the data area
39 * and the OOB area will remain untouched.
41 * The table uses 2 bits per block
43 * 00b: block is factory marked bad
44 * 01b, 10b: block is marked bad due to wear
46 * The memory bad block table uses the following scheme:
48 * 01b: block is marked bad due to wear
49 * 10b: block is reserved (to protect the bbt area)
50 * 11b: block is factory marked bad
52 * Multichip devices like DOC store the bad block info per floor.
54 * Following assumptions are made:
55 * - bbts start at a page boundary, if autolocated on a block boundary
56 * - the space necessary for a bbt in FLASH does not exceed a block boundary
60 #include <linux/slab.h>
61 #include <linux/types.h>
62 #include <linux/mtd/mtd.h>
63 #include <linux/mtd/bbm.h>
64 #include <linux/bitops.h>
65 #include <linux/delay.h>
66 #include <linux/vmalloc.h>
67 #include <linux/export.h>
68 #include <linux/string.h>
70 #include "internals.h"
72 #define BBT_BLOCK_GOOD 0x00
73 #define BBT_BLOCK_WORN 0x01
74 #define BBT_BLOCK_RESERVED 0x02
75 #define BBT_BLOCK_FACTORY_BAD 0x03
77 #define BBT_ENTRY_MASK 0x03
78 #define BBT_ENTRY_SHIFT 2
80 static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
82 uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
83 entry >>= (block & BBT_ENTRY_MASK) * 2;
84 return entry & BBT_ENTRY_MASK;
87 static inline void bbt_mark_entry(struct nand_chip *chip, int block,
90 uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
91 chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
94 static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
96 if (memcmp(buf, td->pattern, td->len))
102 * check_pattern - [GENERIC] check if a pattern is in the buffer
103 * @buf: the buffer to search
104 * @len: the length of buffer to search
105 * @paglen: the pagelength
106 * @td: search pattern descriptor
108 * Check for a pattern at the given place. Used to search bad block tables and
109 * good / bad block identifiers.
111 static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
113 if (td->options & NAND_BBT_NO_OOB)
114 return check_pattern_no_oob(buf, td);
116 /* Compare the pattern */
117 if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
124 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
125 * @buf: the buffer to search
126 * @td: search pattern descriptor
128 * Check for a pattern at the given place. Used to search bad block tables and
129 * good / bad block identifiers. Same as check_pattern, but no optional empty
132 static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
134 /* Compare the pattern */
135 if (memcmp(buf + td->offs, td->pattern, td->len))
141 * add_marker_len - compute the length of the marker in data area
142 * @td: BBT descriptor used for computation
144 * The length will be 0 if the marker is located in OOB area.
146 static u32 add_marker_len(struct nand_bbt_descr *td)
150 if (!(td->options & NAND_BBT_NO_OOB))
154 if (td->options & NAND_BBT_VERSION)
160 * read_bbt - [GENERIC] Read the bad block table starting from page
161 * @chip: NAND chip object
162 * @buf: temporary buffer
163 * @page: the starting page
164 * @num: the number of bbt descriptors to read
165 * @td: the bbt describtion table
166 * @offs: block number offset in the table
168 * Read the bad block table starting from page.
170 static int read_bbt(struct nand_chip *this, uint8_t *buf, int page, int num,
171 struct nand_bbt_descr *td, int offs)
173 struct mtd_info *mtd = nand_to_mtd(this);
174 int res, ret = 0, i, j, act = 0;
175 size_t retlen, len, totlen;
177 int bits = td->options & NAND_BBT_NRBITS_MSK;
178 uint8_t msk = (uint8_t)((1 << bits) - 1);
180 int reserved_block_code = td->reserved_block_code;
182 totlen = (num * bits) >> 3;
183 marker_len = add_marker_len(td);
184 from = ((loff_t)page) << this->page_shift;
187 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
190 * In case the BBT marker is not in the OOB area it
191 * will be just in the first page.
197 res = mtd_read(mtd, from, len, &retlen, buf);
199 if (mtd_is_eccerr(res)) {
200 pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
201 from & ~mtd->writesize);
203 } else if (mtd_is_bitflip(res)) {
204 pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
205 from & ~mtd->writesize);
208 pr_info("nand_bbt: error reading BBT\n");
214 for (i = 0; i < len; i++) {
215 uint8_t dat = buf[i];
216 for (j = 0; j < 8; j += bits, act++) {
217 uint8_t tmp = (dat >> j) & msk;
220 if (reserved_block_code && (tmp == reserved_block_code)) {
221 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
222 (loff_t)(offs + act) <<
223 this->bbt_erase_shift);
224 bbt_mark_entry(this, offs + act,
226 mtd->ecc_stats.bbtblocks++;
230 * Leave it for now, if it's matured we can
231 * move this message to pr_debug.
233 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
234 (loff_t)(offs + act) <<
235 this->bbt_erase_shift);
236 /* Factory marked bad or worn out? */
238 bbt_mark_entry(this, offs + act,
239 BBT_BLOCK_FACTORY_BAD);
241 bbt_mark_entry(this, offs + act,
243 mtd->ecc_stats.badblocks++;
253 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
254 * @this: NAND chip object
255 * @buf: temporary buffer
256 * @td: descriptor for the bad block table
257 * @chip: read the table for a specific chip, -1 read all chips; applies only if
258 * NAND_BBT_PERCHIP option is set
260 * Read the bad block table for all chips starting at a given page. We assume
261 * that the bbt bits are in consecutive order.
263 static int read_abs_bbt(struct nand_chip *this, uint8_t *buf,
264 struct nand_bbt_descr *td, int chip)
266 struct mtd_info *mtd = nand_to_mtd(this);
269 if (td->options & NAND_BBT_PERCHIP) {
271 for (i = 0; i < this->numchips; i++) {
272 if (chip == -1 || chip == i)
273 res = read_bbt(this, buf, td->pages[i],
274 this->chipsize >> this->bbt_erase_shift,
278 offs += this->chipsize >> this->bbt_erase_shift;
281 res = read_bbt(this, buf, td->pages[0],
282 mtd->size >> this->bbt_erase_shift, td, 0);
289 /* BBT marker is in the first page, no OOB */
290 static int scan_read_data(struct nand_chip *this, uint8_t *buf, loff_t offs,
291 struct nand_bbt_descr *td)
293 struct mtd_info *mtd = nand_to_mtd(this);
298 if (td->options & NAND_BBT_VERSION)
301 return mtd_read(mtd, offs, len, &retlen, buf);
305 * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
306 * @this: NAND chip object
307 * @buf: temporary buffer
308 * @offs: offset at which to scan
309 * @len: length of data region to read
311 * Scan read data from data+OOB. May traverse multiple pages, interleaving
312 * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"
313 * ECC condition (error or bitflip). May quit on the first (non-ECC) error.
315 static int scan_read_oob(struct nand_chip *this, uint8_t *buf, loff_t offs,
318 struct mtd_info *mtd = nand_to_mtd(this);
319 struct mtd_oob_ops ops;
322 ops.mode = MTD_OPS_PLACE_OOB;
324 ops.ooblen = mtd->oobsize;
328 ops.len = min(len, (size_t)mtd->writesize);
329 ops.oobbuf = buf + ops.len;
331 res = mtd_read_oob(mtd, offs, &ops);
333 if (!mtd_is_bitflip_or_eccerr(res))
335 else if (mtd_is_eccerr(res) || !ret)
339 buf += mtd->oobsize + mtd->writesize;
340 len -= mtd->writesize;
341 offs += mtd->writesize;
346 static int scan_read(struct nand_chip *this, uint8_t *buf, loff_t offs,
347 size_t len, struct nand_bbt_descr *td)
349 if (td->options & NAND_BBT_NO_OOB)
350 return scan_read_data(this, buf, offs, td);
352 return scan_read_oob(this, buf, offs, len);
355 /* Scan write data with oob to flash */
356 static int scan_write_bbt(struct nand_chip *this, loff_t offs, size_t len,
357 uint8_t *buf, uint8_t *oob)
359 struct mtd_info *mtd = nand_to_mtd(this);
360 struct mtd_oob_ops ops;
362 ops.mode = MTD_OPS_PLACE_OOB;
364 ops.ooblen = mtd->oobsize;
369 return mtd_write_oob(mtd, offs, &ops);
372 static u32 bbt_get_ver_offs(struct nand_chip *this, struct nand_bbt_descr *td)
374 struct mtd_info *mtd = nand_to_mtd(this);
375 u32 ver_offs = td->veroffs;
377 if (!(td->options & NAND_BBT_NO_OOB))
378 ver_offs += mtd->writesize;
383 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
384 * @this: NAND chip object
385 * @buf: temporary buffer
386 * @td: descriptor for the bad block table
387 * @md: descriptor for the bad block table mirror
389 * Read the bad block table(s) for all chips starting at a given page. We
390 * assume that the bbt bits are in consecutive order.
392 static void read_abs_bbts(struct nand_chip *this, uint8_t *buf,
393 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
395 struct mtd_info *mtd = nand_to_mtd(this);
397 /* Read the primary version, if available */
398 if (td->options & NAND_BBT_VERSION) {
399 scan_read(this, buf, (loff_t)td->pages[0] << this->page_shift,
401 td->version[0] = buf[bbt_get_ver_offs(this, td)];
402 pr_info("Bad block table at page %d, version 0x%02X\n",
403 td->pages[0], td->version[0]);
406 /* Read the mirror version, if available */
407 if (md && (md->options & NAND_BBT_VERSION)) {
408 scan_read(this, buf, (loff_t)md->pages[0] << this->page_shift,
410 md->version[0] = buf[bbt_get_ver_offs(this, md)];
411 pr_info("Bad block table at page %d, version 0x%02X\n",
412 md->pages[0], md->version[0]);
416 /* Scan a given block partially */
417 static int scan_block_fast(struct nand_chip *this, struct nand_bbt_descr *bd,
418 loff_t offs, uint8_t *buf, int numpages)
420 struct mtd_info *mtd = nand_to_mtd(this);
421 struct mtd_oob_ops ops;
424 ops.ooblen = mtd->oobsize;
428 ops.mode = MTD_OPS_PLACE_OOB;
430 for (j = 0; j < numpages; j++) {
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, &ops);
436 /* Ignore ECC errors when checking for BBM */
437 if (ret && !mtd_is_bitflip_or_eccerr(ret))
440 if (check_short_pattern(buf, bd))
443 offs += mtd->writesize;
449 * create_bbt - [GENERIC] Create a bad block table by scanning the device
450 * @this: NAND chip object
451 * @buf: temporary buffer
452 * @bd: descriptor for the good/bad block search pattern
453 * @chip: create the table for a specific chip, -1 read all chips; applies only
454 * if NAND_BBT_PERCHIP option is set
456 * Create a bad block table by scanning the device for the given good/bad block
459 static int create_bbt(struct nand_chip *this, uint8_t *buf,
460 struct nand_bbt_descr *bd, int chip)
462 struct mtd_info *mtd = nand_to_mtd(this);
463 int i, numblocks, numpages;
467 pr_info("Scanning device for bad blocks\n");
469 if (bd->options & NAND_BBT_SCAN2NDPAGE)
475 numblocks = mtd->size >> this->bbt_erase_shift;
479 if (chip >= this->numchips) {
480 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
481 chip + 1, this->numchips);
484 numblocks = this->chipsize >> this->bbt_erase_shift;
485 startblock = chip * numblocks;
486 numblocks += startblock;
487 from = (loff_t)startblock << this->bbt_erase_shift;
490 if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
491 from += mtd->erasesize - (mtd->writesize * numpages);
493 for (i = startblock; i < numblocks; i++) {
496 BUG_ON(bd->options & NAND_BBT_NO_OOB);
498 ret = scan_block_fast(this, bd, from, buf, numpages);
503 bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
504 pr_warn("Bad eraseblock %d at 0x%012llx\n",
505 i, (unsigned long long)from);
506 mtd->ecc_stats.badblocks++;
509 from += (1 << this->bbt_erase_shift);
515 * search_bbt - [GENERIC] scan the device for a specific bad block table
516 * @this: NAND chip object
517 * @buf: temporary buffer
518 * @td: descriptor for the bad block table
520 * Read the bad block table by searching for a given ident pattern. Search is
521 * preformed either from the beginning up or from the end of the device
522 * downwards. The search starts always at the start of a block. If the option
523 * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
524 * the bad block information of this chip. This is necessary to provide support
525 * for certain DOC devices.
527 * The bbt ident pattern resides in the oob area of the first page in a block.
529 static int search_bbt(struct nand_chip *this, uint8_t *buf,
530 struct nand_bbt_descr *td)
532 struct mtd_info *mtd = nand_to_mtd(this);
534 int startblock, block, dir;
535 int scanlen = mtd->writesize + mtd->oobsize;
537 int blocktopage = this->bbt_erase_shift - this->page_shift;
539 /* Search direction top -> down? */
540 if (td->options & NAND_BBT_LASTBLOCK) {
541 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
548 /* Do we have a bbt per chip? */
549 if (td->options & NAND_BBT_PERCHIP) {
550 chips = this->numchips;
551 bbtblocks = this->chipsize >> this->bbt_erase_shift;
552 startblock &= bbtblocks - 1;
555 bbtblocks = mtd->size >> this->bbt_erase_shift;
558 for (i = 0; i < chips; i++) {
559 /* Reset version information */
562 /* Scan the maximum number of blocks */
563 for (block = 0; block < td->maxblocks; block++) {
565 int actblock = startblock + dir * block;
566 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
568 /* Read first page */
569 scan_read(this, buf, offs, mtd->writesize, td);
570 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
571 td->pages[i] = actblock << blocktopage;
572 if (td->options & NAND_BBT_VERSION) {
573 offs = bbt_get_ver_offs(this, td);
574 td->version[i] = buf[offs];
579 startblock += this->chipsize >> this->bbt_erase_shift;
581 /* Check, if we found a bbt for each requested chip */
582 for (i = 0; i < chips; i++) {
583 if (td->pages[i] == -1)
584 pr_warn("Bad block table not found for chip %d\n", i);
586 pr_info("Bad block table found at page %d, version 0x%02X\n",
587 td->pages[i], td->version[i]);
593 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
594 * @this: NAND chip object
595 * @buf: temporary buffer
596 * @td: descriptor for the bad block table
597 * @md: descriptor for the bad block table mirror
599 * Search and read the bad block table(s).
601 static void search_read_bbts(struct nand_chip *this, uint8_t *buf,
602 struct nand_bbt_descr *td,
603 struct nand_bbt_descr *md)
605 /* Search the primary table */
606 search_bbt(this, buf, td);
608 /* Search the mirror table */
610 search_bbt(this, buf, md);
614 * get_bbt_block - Get the first valid eraseblock suitable to store a BBT
615 * @this: the NAND device
616 * @td: the BBT description
617 * @md: the mirror BBT descriptor
618 * @chip: the CHIP selector
620 * This functions returns a positive block number pointing a valid eraseblock
621 * suitable to store a BBT (i.e. in the range reserved for BBT), or -ENOSPC if
622 * all blocks are already used of marked bad. If td->pages[chip] was already
623 * pointing to a valid block we re-use it, otherwise we search for the next
626 static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td,
627 struct nand_bbt_descr *md, int chip)
629 int startblock, dir, page, numblocks, i;
632 * There was already a version of the table, reuse the page. This
633 * applies for absolute placement too, as we have the page number in
636 if (td->pages[chip] != -1)
637 return td->pages[chip] >>
638 (this->bbt_erase_shift - this->page_shift);
640 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
641 if (!(td->options & NAND_BBT_PERCHIP))
642 numblocks *= this->numchips;
645 * Automatic placement of the bad block table. Search direction
648 if (td->options & NAND_BBT_LASTBLOCK) {
649 startblock = numblocks * (chip + 1) - 1;
652 startblock = chip * numblocks;
656 for (i = 0; i < td->maxblocks; i++) {
657 int block = startblock + dir * i;
659 /* Check, if the block is bad */
660 switch (bbt_get_entry(this, block)) {
662 case BBT_BLOCK_FACTORY_BAD:
666 page = block << (this->bbt_erase_shift - this->page_shift);
668 /* Check, if the block is used by the mirror table */
669 if (!md || md->pages[chip] != page)
677 * mark_bbt_block_bad - Mark one of the block reserved for BBT bad
678 * @this: the NAND device
679 * @td: the BBT description
680 * @chip: the CHIP selector
681 * @block: the BBT block to mark
683 * Blocks reserved for BBT can become bad. This functions is an helper to mark
684 * such blocks as bad. It takes care of updating the in-memory BBT, marking the
685 * block as bad using a bad block marker and invalidating the associated
688 static void mark_bbt_block_bad(struct nand_chip *this,
689 struct nand_bbt_descr *td,
695 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
697 to = (loff_t)block << this->bbt_erase_shift;
698 res = nand_markbad_bbm(this, to);
700 pr_warn("nand_bbt: error %d while marking block %d bad\n",
703 td->pages[chip] = -1;
707 * write_bbt - [GENERIC] (Re)write the bad block table
708 * @this: NAND chip object
709 * @buf: temporary buffer
710 * @td: descriptor for the bad block table
711 * @md: descriptor for the bad block table mirror
712 * @chipsel: selector for a specific chip, -1 for all
714 * (Re)write the bad block table.
716 static int write_bbt(struct nand_chip *this, uint8_t *buf,
717 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
720 struct mtd_info *mtd = nand_to_mtd(this);
721 struct erase_info einfo;
722 int i, res, chip = 0;
723 int bits, page, offs, numblocks, sft, sftmsk;
724 int nrchips, pageoffs, ooboffs;
726 uint8_t rcode = td->reserved_block_code;
727 size_t retlen, len = 0;
729 struct mtd_oob_ops ops;
731 ops.ooblen = mtd->oobsize;
734 ops.mode = MTD_OPS_PLACE_OOB;
738 /* Write bad block table per chip rather than per device? */
739 if (td->options & NAND_BBT_PERCHIP) {
740 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
741 /* Full device write or specific chip? */
743 nrchips = this->numchips;
745 nrchips = chipsel + 1;
749 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
753 /* Loop through the chips */
754 while (chip < nrchips) {
757 block = get_bbt_block(this, td, md, chip);
759 pr_err("No space left to write bad block table\n");
765 * get_bbt_block() returns a block number, shift the value to
768 page = block << (this->bbt_erase_shift - this->page_shift);
770 /* Set up shift count and masks for the flash table */
771 bits = td->options & NAND_BBT_NRBITS_MSK;
774 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
777 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
780 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
783 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
786 default: return -EINVAL;
789 to = ((loff_t)page) << this->page_shift;
791 /* Must we save the block contents? */
792 if (td->options & NAND_BBT_SAVECONTENT) {
793 /* Make it block aligned */
794 to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
795 len = 1 << this->bbt_erase_shift;
796 res = mtd_read(mtd, to, len, &retlen, buf);
799 pr_info("nand_bbt: error reading block for writing the bad block table\n");
802 pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
805 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
806 ops.oobbuf = &buf[len];
807 res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
808 if (res < 0 || ops.oobretlen != ops.ooblen)
811 /* Calc the byte offset in the buffer */
812 pageoffs = page - (int)(to >> this->page_shift);
813 offs = pageoffs << this->page_shift;
814 /* Preset the bbt area with 0xff */
815 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
816 ooboffs = len + (pageoffs * mtd->oobsize);
818 } else if (td->options & NAND_BBT_NO_OOB) {
821 /* The version byte */
822 if (td->options & NAND_BBT_VERSION)
825 len = (size_t)(numblocks >> sft);
827 /* Make it page aligned! */
828 len = ALIGN(len, mtd->writesize);
829 /* Preset the buffer with 0xff */
830 memset(buf, 0xff, len);
831 /* Pattern is located at the begin of first page */
832 memcpy(buf, td->pattern, td->len);
835 len = (size_t)(numblocks >> sft);
836 /* Make it page aligned! */
837 len = ALIGN(len, mtd->writesize);
838 /* Preset the buffer with 0xff */
839 memset(buf, 0xff, len +
840 (len >> this->page_shift)* mtd->oobsize);
843 /* Pattern is located in oob area of first page */
844 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
847 if (td->options & NAND_BBT_VERSION)
848 buf[ooboffs + td->veroffs] = td->version[chip];
850 /* Walk through the memory table */
851 for (i = 0; i < numblocks; i++) {
853 int sftcnt = (i << (3 - sft)) & sftmsk;
854 dat = bbt_get_entry(this, chip * numblocks + i);
855 /* Do not store the reserved bbt blocks! */
856 buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
859 memset(&einfo, 0, sizeof(einfo));
861 einfo.len = 1 << this->bbt_erase_shift;
862 res = nand_erase_nand(this, &einfo, 1);
864 pr_warn("nand_bbt: error while erasing BBT block %d\n",
866 mark_bbt_block_bad(this, td, chip, block);
870 res = scan_write_bbt(this, to, len, buf,
871 td->options & NAND_BBT_NO_OOB ?
874 pr_warn("nand_bbt: error while writing BBT block %d\n",
876 mark_bbt_block_bad(this, td, chip, block);
880 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
881 (unsigned long long)to, td->version[chip]);
883 /* Mark it as used */
884 td->pages[chip++] = page;
889 pr_warn("nand_bbt: error while writing bad block table %d\n", res);
894 * nand_memory_bbt - [GENERIC] create a memory based bad block table
895 * @this: NAND chip object
896 * @bd: descriptor for the good/bad block search pattern
898 * The function creates a memory based bbt by scanning the device for
899 * manufacturer / software marked good / bad blocks.
901 static inline int nand_memory_bbt(struct nand_chip *this,
902 struct nand_bbt_descr *bd)
904 return create_bbt(this, this->data_buf, bd, -1);
908 * check_create - [GENERIC] create and write bbt(s) if necessary
909 * @this: the NAND device
910 * @buf: temporary buffer
911 * @bd: descriptor for the good/bad block search pattern
913 * The function checks the results of the previous call to read_bbt and creates
914 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
915 * for the chip/device. Update is necessary if one of the tables is missing or
916 * the version nr. of one table is less than the other.
918 static int check_create(struct nand_chip *this, uint8_t *buf,
919 struct nand_bbt_descr *bd)
921 int i, chips, writeops, create, chipsel, res, res2;
922 struct nand_bbt_descr *td = this->bbt_td;
923 struct nand_bbt_descr *md = this->bbt_md;
924 struct nand_bbt_descr *rd, *rd2;
926 /* Do we have a bbt per chip? */
927 if (td->options & NAND_BBT_PERCHIP)
928 chips = this->numchips;
932 for (i = 0; i < chips; i++) {
938 /* Per chip or per device? */
939 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
940 /* Mirrored table available? */
942 if (td->pages[i] == -1 && md->pages[i] == -1) {
945 } else if (td->pages[i] == -1) {
948 } else if (md->pages[i] == -1) {
951 } else if (td->version[i] == md->version[i]) {
953 if (!(td->options & NAND_BBT_VERSION))
955 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
963 if (td->pages[i] == -1) {
972 /* Create the bad block table by scanning the device? */
973 if (!(td->options & NAND_BBT_CREATE))
976 /* Create the table in memory by scanning the chip(s) */
977 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
978 create_bbt(this, buf, bd, chipsel);
985 /* Read back first? */
987 res = read_abs_bbt(this, buf, rd, chipsel);
988 if (mtd_is_eccerr(res)) {
989 /* Mark table as invalid */
996 /* If they weren't versioned, read both */
998 res2 = read_abs_bbt(this, buf, rd2, chipsel);
999 if (mtd_is_eccerr(res2)) {
1000 /* Mark table as invalid */
1002 rd2->version[i] = 0;
1008 /* Scrub the flash table(s)? */
1009 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
1012 /* Update version numbers before writing */
1014 td->version[i] = max(td->version[i], md->version[i]);
1015 md->version[i] = td->version[i];
1018 /* Write the bad block table to the device? */
1019 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1020 res = write_bbt(this, buf, td, md, chipsel);
1025 /* Write the mirror bad block table to the device? */
1026 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1027 res = write_bbt(this, buf, md, td, chipsel);
1036 * nand_update_bbt - update bad block table(s)
1037 * @this: the NAND device
1038 * @offs: the offset of the newly marked block
1040 * The function updates the bad block table(s).
1042 static int nand_update_bbt(struct nand_chip *this, loff_t offs)
1044 struct mtd_info *mtd = nand_to_mtd(this);
1048 struct nand_bbt_descr *td = this->bbt_td;
1049 struct nand_bbt_descr *md = this->bbt_md;
1051 if (!this->bbt || !td)
1054 /* Allocate a temporary buffer for one eraseblock incl. oob */
1055 len = (1 << this->bbt_erase_shift);
1056 len += (len >> this->page_shift) * mtd->oobsize;
1057 buf = kmalloc(len, GFP_KERNEL);
1061 /* Do we have a bbt per chip? */
1062 if (td->options & NAND_BBT_PERCHIP) {
1063 chip = (int)(offs >> this->chip_shift);
1070 td->version[chip]++;
1072 md->version[chip]++;
1074 /* Write the bad block table to the device? */
1075 if (td->options & NAND_BBT_WRITE) {
1076 res = write_bbt(this, buf, td, md, chipsel);
1080 /* Write the mirror bad block table to the device? */
1081 if (md && (md->options & NAND_BBT_WRITE)) {
1082 res = write_bbt(this, buf, md, td, chipsel);
1091 * mark_bbt_regions - [GENERIC] mark the bad block table regions
1092 * @this: the NAND device
1093 * @td: bad block table descriptor
1095 * The bad block table regions are marked as "bad" to prevent accidental
1096 * erasures / writes. The regions are identified by the mark 0x02.
1098 static void mark_bbt_region(struct nand_chip *this, struct nand_bbt_descr *td)
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 = this->numchips;
1107 nrblocks = (int)(this->chipsize >> 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 struct mtd_info *mtd = nand_to_mtd(this);
1168 pattern_len = bd->len;
1169 bits = bd->options & NAND_BBT_NRBITS_MSK;
1171 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
1172 !(this->bbt_options & NAND_BBT_USE_FLASH));
1175 if (bd->options & NAND_BBT_VERSION)
1178 if (bd->options & NAND_BBT_NO_OOB) {
1179 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
1180 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
1182 if (bd->options & NAND_BBT_VERSION)
1183 BUG_ON(bd->veroffs != bd->len);
1184 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1187 if (bd->options & NAND_BBT_PERCHIP)
1188 table_size = this->chipsize >> this->bbt_erase_shift;
1190 table_size = mtd->size >> this->bbt_erase_shift;
1193 if (bd->options & NAND_BBT_NO_OOB)
1194 table_size += pattern_len;
1195 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1199 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1200 * @this: the NAND device
1201 * @bd: descriptor for the good/bad block search pattern
1203 * The function checks, if a bad block table(s) is/are already available. If
1204 * not it scans the device for manufacturer marked good / bad blocks and writes
1205 * the bad block table(s) to the selected place.
1207 * The bad block table memory is allocated here. It must be freed by calling
1208 * the nand_free_bbt function.
1210 static int nand_scan_bbt(struct nand_chip *this, struct nand_bbt_descr *bd)
1212 struct mtd_info *mtd = nand_to_mtd(this);
1215 struct nand_bbt_descr *td = this->bbt_td;
1216 struct nand_bbt_descr *md = this->bbt_md;
1218 len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
1220 * Allocate memory (2bit per block) and clear the memory bad block
1223 this->bbt = kzalloc(len, GFP_KERNEL);
1228 * If no primary table decriptor is given, scan the device to build a
1229 * memory based bad block table.
1232 if ((res = nand_memory_bbt(this, bd))) {
1233 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
1238 verify_bbt_descr(this, td);
1239 verify_bbt_descr(this, md);
1241 /* Allocate a temporary buffer for one eraseblock incl. oob */
1242 len = (1 << this->bbt_erase_shift);
1243 len += (len >> this->page_shift) * mtd->oobsize;
1250 /* Is the bbt at a given page? */
1251 if (td->options & NAND_BBT_ABSPAGE) {
1252 read_abs_bbts(this, buf, td, md);
1254 /* Search the bad block table using a pattern in oob */
1255 search_read_bbts(this, buf, td, md);
1258 res = check_create(this, buf, bd);
1262 /* Prevent the bbt regions from erasing / writing */
1263 mark_bbt_region(this, td);
1265 mark_bbt_region(this, md);
1277 * Define some generic bad / good block scan pattern which are used
1278 * while scanning a device for factory marked good / bad blocks.
1280 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1282 /* Generic flash bbt descriptors */
1283 static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1284 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1286 static struct nand_bbt_descr bbt_main_descr = {
1287 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1288 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1292 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1293 .pattern = bbt_pattern
1296 static struct nand_bbt_descr bbt_mirror_descr = {
1297 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1298 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1302 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1303 .pattern = mirror_pattern
1306 static struct nand_bbt_descr bbt_main_no_oob_descr = {
1307 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1308 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1312 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1313 .pattern = bbt_pattern
1316 static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
1317 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1318 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1322 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1323 .pattern = mirror_pattern
1326 #define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
1328 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
1329 * @this: NAND chip to create descriptor for
1331 * This function allocates and initializes a nand_bbt_descr for BBM detection
1332 * based on the properties of @this. The new descriptor is stored in
1333 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1334 * passed to this function.
1336 static int nand_create_badblock_pattern(struct nand_chip *this)
1338 struct nand_bbt_descr *bd;
1339 if (this->badblock_pattern) {
1340 pr_warn("Bad block pattern already allocated; not replacing\n");
1343 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1346 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
1347 bd->offs = this->badblockpos;
1348 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1349 bd->pattern = scan_ff_pattern;
1350 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1351 this->badblock_pattern = bd;
1356 * nand_create_bbt - [NAND Interface] Select a default bad block table for the device
1357 * @this: NAND chip object
1359 * This function selects the default bad block table support for the device and
1360 * calls the nand_scan_bbt function.
1362 int nand_create_bbt(struct nand_chip *this)
1366 /* Is a flash based bad block table requested? */
1367 if (this->bbt_options & NAND_BBT_USE_FLASH) {
1368 /* Use the default pattern descriptors */
1369 if (!this->bbt_td) {
1370 if (this->bbt_options & NAND_BBT_NO_OOB) {
1371 this->bbt_td = &bbt_main_no_oob_descr;
1372 this->bbt_md = &bbt_mirror_no_oob_descr;
1374 this->bbt_td = &bbt_main_descr;
1375 this->bbt_md = &bbt_mirror_descr;
1379 this->bbt_td = NULL;
1380 this->bbt_md = NULL;
1383 if (!this->badblock_pattern) {
1384 ret = nand_create_badblock_pattern(this);
1389 return nand_scan_bbt(this, this->badblock_pattern);
1391 EXPORT_SYMBOL(nand_create_bbt);
1394 * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
1395 * @this: NAND chip object
1396 * @offs: offset in the device
1398 int nand_isreserved_bbt(struct nand_chip *this, loff_t offs)
1402 block = (int)(offs >> this->bbt_erase_shift);
1403 return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
1407 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1408 * @this: NAND chip object
1409 * @offs: offset in the device
1410 * @allowbbt: allow access to bad block table region
1412 int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt)
1416 block = (int)(offs >> this->bbt_erase_shift);
1417 res = bbt_get_entry(this, block);
1419 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1420 (unsigned int)offs, block, res);
1423 case BBT_BLOCK_GOOD:
1425 case BBT_BLOCK_WORN:
1427 case BBT_BLOCK_RESERVED:
1428 return allowbbt ? 0 : 1;
1434 * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
1435 * @this: NAND chip object
1436 * @offs: offset of the bad block
1438 int nand_markbad_bbt(struct nand_chip *this, loff_t offs)
1442 block = (int)(offs >> this->bbt_erase_shift);
1444 /* Mark bad block in memory */
1445 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
1447 /* Update flash-based bad block table */
1448 if (this->bbt_options & NAND_BBT_USE_FLASH)
1449 ret = nand_update_bbt(this, offs);