2 * drivers/mtd/nand_bbt.c
5 * Bad block table support for the NAND driver
7 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 * When nand_scan_bbt is called, then it tries to find the bad block table
16 * depending on the options in the BBT descriptor(s). If no flash based BBT
17 * (NAND_USE_FLASH_BBT) is specified then the device is scanned for factory
18 * marked good / bad blocks. This information is used to create a memory BBT.
19 * Once a new bad block is discovered then the "factory" information is updated
21 * If a flash based BBT is specified then the function first tries to find the
22 * BBT on flash. If a BBT is found then the contents are read and the memory
23 * based BBT is created. If a mirrored BBT is selected then the mirror is
24 * searched too and the versions are compared. If the mirror has a greater
25 * version number than the mirror BBT is used to build the memory based BBT.
26 * If the tables are not versioned, then we "or" the bad block information.
27 * If one of the BBTs is out of date or does not exist it is (re)created.
28 * If no BBT exists at all then the device is scanned for factory marked
29 * good / bad blocks and the bad block tables are created.
31 * For manufacturer created BBTs like the one found on M-SYS DOC devices
32 * the BBT is searched and read but never created
34 * The auto generated bad block table is located in the last good blocks
35 * of the device. The table is mirrored, so it can be updated eventually.
36 * The table is marked in the OOB area with an ident pattern and a version
37 * number which indicates which of both tables is more up to date. If the NAND
38 * controller needs the complete OOB area for the ECC information then the
39 * option NAND_USE_FLASH_BBT_NO_OOB should be used: it moves the ident pattern
40 * and the version byte into the data area and the OOB area will remain
43 * The table uses 2 bits per block
45 * 00b: block is factory marked bad
46 * 01b, 10b: block is marked bad due to wear
48 * The memory bad block table uses the following scheme:
50 * 01b: block is marked bad due to wear
51 * 10b: block is reserved (to protect the bbt area)
52 * 11b: block is factory marked bad
54 * Multichip devices like DOC store the bad block info per floor.
56 * Following assumptions are made:
57 * - bbts start at a page boundary, if autolocated on a block boundary
58 * - the space necessary for a bbt in FLASH does not exceed a block boundary
62 #include <linux/slab.h>
63 #include <linux/types.h>
64 #include <linux/mtd/mtd.h>
65 #include <linux/mtd/nand.h>
66 #include <linux/mtd/nand_ecc.h>
67 #include <linux/bitops.h>
68 #include <linux/delay.h>
69 #include <linux/vmalloc.h>
71 static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
75 ret = memcmp(buf, td->pattern, td->len);
82 * check_pattern - [GENERIC] check if a pattern is in the buffer
83 * @buf: the buffer to search
84 * @len: the length of buffer to search
85 * @paglen: the pagelength
86 * @td: search pattern descriptor
88 * Check for a pattern at the given place. Used to search bad block
89 * tables and good / bad block identifiers.
90 * If the SCAN_EMPTY option is set then check, if all bytes except the
91 * pattern area contain 0xff
94 static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
99 if (td->options & NAND_BBT_NO_OOB)
100 return check_pattern_no_oob(buf, td);
102 end = paglen + td->offs;
103 if (td->options & NAND_BBT_SCANEMPTY) {
104 for (i = 0; i < end; i++) {
111 /* Compare the pattern */
112 for (i = 0; i < td->len; i++) {
113 if (p[i] != td->pattern[i])
117 /* Check both positions 1 and 6 for pattern? */
118 if (td->options & NAND_BBT_SCANBYTE1AND6) {
119 if (td->options & NAND_BBT_SCANEMPTY) {
121 end += NAND_SMALL_BADBLOCK_POS - td->offs;
122 /* Check region between positions 1 and 6 */
123 for (i = 0; i < NAND_SMALL_BADBLOCK_POS - td->offs - td->len;
130 p += NAND_SMALL_BADBLOCK_POS - td->offs;
132 /* Compare the pattern */
133 for (i = 0; i < td->len; i++) {
134 if (p[i] != td->pattern[i])
139 if (td->options & NAND_BBT_SCANEMPTY) {
142 for (i = end; i < len; i++) {
151 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
152 * @buf: the buffer to search
153 * @td: search pattern descriptor
155 * Check for a pattern at the given place. Used to search bad block
156 * tables and good / bad block identifiers. Same as check_pattern, but
157 * no optional empty check
160 static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
165 /* Compare the pattern */
166 for (i = 0; i < td->len; i++) {
167 if (p[td->offs + i] != td->pattern[i])
170 /* Need to check location 1 AND 6? */
171 if (td->options & NAND_BBT_SCANBYTE1AND6) {
172 for (i = 0; i < td->len; i++) {
173 if (p[NAND_SMALL_BADBLOCK_POS + i] != td->pattern[i])
181 * add_marker_len - compute the length of the marker in data area
182 * @td: BBT descriptor used for computation
184 * The length will be 0 if the markeris located in OOB area.
186 static u32 add_marker_len(struct nand_bbt_descr *td)
190 if (!(td->options & NAND_BBT_NO_OOB))
194 if (td->options & NAND_BBT_VERSION)
200 * read_bbt - [GENERIC] Read the bad block table starting from page
201 * @mtd: MTD device structure
202 * @buf: temporary buffer
203 * @page: the starting page
204 * @num: the number of bbt descriptors to read
205 * @td: the bbt describtion table
206 * @offs: offset in the memory table
208 * Read the bad block table starting from page.
211 static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
212 struct nand_bbt_descr *td, int offs)
214 int res, i, j, act = 0;
215 struct nand_chip *this = mtd->priv;
216 size_t retlen, len, totlen;
218 int bits = td->options & NAND_BBT_NRBITS_MSK;
219 uint8_t msk = (uint8_t) ((1 << bits) - 1);
221 int reserved_block_code = td->reserved_block_code;
223 totlen = (num * bits) >> 3;
224 marker_len = add_marker_len(td);
225 from = ((loff_t) page) << this->page_shift;
228 len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
231 * In case the BBT marker is not in the OOB area it
232 * will be just in the first page.
238 res = mtd->read(mtd, from, len, &retlen, buf);
241 printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
244 printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
248 for (i = 0; i < len; i++) {
249 uint8_t dat = buf[i];
250 for (j = 0; j < 8; j += bits, act += 2) {
251 uint8_t tmp = (dat >> j) & msk;
254 if (reserved_block_code && (tmp == reserved_block_code)) {
255 printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%012llx\n",
256 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
257 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
258 mtd->ecc_stats.bbtblocks++;
261 /* Leave it for now, if its matured we can move this
262 * message to MTD_DEBUG_LEVEL0 */
263 printk(KERN_DEBUG "nand_read_bbt: Bad block at 0x%012llx\n",
264 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
265 /* Factory marked bad or worn out ? */
267 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
269 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
270 mtd->ecc_stats.badblocks++;
280 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
281 * @mtd: MTD device structure
282 * @buf: temporary buffer
283 * @td: descriptor for the bad block table
284 * @chip: read the table for a specific chip, -1 read all chips.
285 * Applies only if NAND_BBT_PERCHIP option is set
287 * Read the bad block table for all chips starting at a given page
288 * We assume that the bbt bits are in consecutive order.
290 static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
292 struct nand_chip *this = mtd->priv;
295 if (td->options & NAND_BBT_PERCHIP) {
297 for (i = 0; i < this->numchips; i++) {
298 if (chip == -1 || chip == i)
299 res = read_bbt(mtd, buf, td->pages[i],
300 this->chipsize >> this->bbt_erase_shift,
304 offs += this->chipsize >> (this->bbt_erase_shift + 2);
307 res = read_bbt(mtd, buf, td->pages[0],
308 mtd->size >> this->bbt_erase_shift, td, 0);
316 * BBT marker is in the first page, no OOB.
318 static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
319 struct nand_bbt_descr *td)
325 if (td->options & NAND_BBT_VERSION)
328 return mtd->read(mtd, offs, len, &retlen, buf);
332 * Scan read raw data from flash
334 static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
337 struct mtd_oob_ops ops;
340 ops.mode = MTD_OOB_RAW;
342 ops.ooblen = mtd->oobsize;
346 if (len <= mtd->writesize) {
347 ops.oobbuf = buf + len;
350 return mtd->read_oob(mtd, offs, &ops);
352 ops.oobbuf = buf + mtd->writesize;
354 ops.len = mtd->writesize;
355 res = mtd->read_oob(mtd, offs, &ops);
361 buf += mtd->oobsize + mtd->writesize;
362 len -= mtd->writesize;
363 offs += mtd->writesize;
368 static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
369 size_t len, struct nand_bbt_descr *td)
371 if (td->options & NAND_BBT_NO_OOB)
372 return scan_read_raw_data(mtd, buf, offs, td);
374 return scan_read_raw_oob(mtd, buf, offs, len);
378 * Scan write data with oob to flash
380 static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
381 uint8_t *buf, uint8_t *oob)
383 struct mtd_oob_ops ops;
385 ops.mode = MTD_OOB_PLACE;
387 ops.ooblen = mtd->oobsize;
392 return mtd->write_oob(mtd, offs, &ops);
395 static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
397 u32 ver_offs = td->veroffs;
399 if (!(td->options & NAND_BBT_NO_OOB))
400 ver_offs += mtd->writesize;
405 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
406 * @mtd: MTD device structure
407 * @buf: temporary buffer
408 * @td: descriptor for the bad block table
409 * @md: descriptor for the bad block table mirror
411 * Read the bad block table(s) for all chips starting at a given page
412 * We assume that the bbt bits are in consecutive order.
415 static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
416 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
418 struct nand_chip *this = mtd->priv;
420 /* Read the primary version, if available */
421 if (td->options & NAND_BBT_VERSION) {
422 scan_read_raw(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
424 td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
425 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
426 td->pages[0], td->version[0]);
429 /* Read the mirror version, if available */
430 if (md && (md->options & NAND_BBT_VERSION)) {
431 scan_read_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
433 md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
434 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
435 md->pages[0], md->version[0]);
441 * Scan a given block full
443 static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
444 loff_t offs, uint8_t *buf, size_t readlen,
445 int scanlen, int len)
449 ret = scan_read_raw_oob(mtd, buf, offs, readlen);
453 for (j = 0; j < len; j++, buf += scanlen) {
454 if (check_pattern(buf, scanlen, mtd->writesize, bd))
461 * Scan a given block partially
463 static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
464 loff_t offs, uint8_t *buf, int len)
466 struct mtd_oob_ops ops;
469 ops.ooblen = mtd->oobsize;
473 ops.mode = MTD_OOB_PLACE;
475 for (j = 0; j < len; j++) {
477 * Read the full oob until read_oob is fixed to
478 * handle single byte reads for 16 bit
481 ret = mtd->read_oob(mtd, offs, &ops);
485 if (check_short_pattern(buf, bd))
488 offs += mtd->writesize;
494 * create_bbt - [GENERIC] Create a bad block table by scanning the device
495 * @mtd: MTD device structure
496 * @buf: temporary buffer
497 * @bd: descriptor for the good/bad block search pattern
498 * @chip: create the table for a specific chip, -1 read all chips.
499 * Applies only if NAND_BBT_PERCHIP option is set
501 * Create a bad block table by scanning the device
502 * for the given good/bad block identify pattern
504 static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
505 struct nand_bbt_descr *bd, int chip)
507 struct nand_chip *this = mtd->priv;
508 int i, numblocks, len, scanlen;
513 printk(KERN_INFO "Scanning device for bad blocks\n");
515 if (bd->options & NAND_BBT_SCANALLPAGES)
516 len = 1 << (this->bbt_erase_shift - this->page_shift);
517 else if (bd->options & NAND_BBT_SCAN2NDPAGE)
522 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
523 /* We need only read few bytes from the OOB area */
527 /* Full page content should be read */
528 scanlen = mtd->writesize + mtd->oobsize;
529 readlen = len * mtd->writesize;
533 /* Note that numblocks is 2 * (real numblocks) here, see i+=2
534 * below as it makes shifting and masking less painful */
535 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
539 if (chip >= this->numchips) {
540 printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
541 chip + 1, this->numchips);
544 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
545 startblock = chip * numblocks;
546 numblocks += startblock;
547 from = (loff_t)startblock << (this->bbt_erase_shift - 1);
550 if (this->options & NAND_BBT_SCANLASTPAGE)
551 from += mtd->erasesize - (mtd->writesize * len);
553 for (i = startblock; i < numblocks;) {
556 BUG_ON(bd->options & NAND_BBT_NO_OOB);
558 if (bd->options & NAND_BBT_SCANALLPAGES)
559 ret = scan_block_full(mtd, bd, from, buf, readlen,
562 ret = scan_block_fast(mtd, bd, from, buf, len);
568 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
569 printk(KERN_WARNING "Bad eraseblock %d at 0x%012llx\n",
570 i >> 1, (unsigned long long)from);
571 mtd->ecc_stats.badblocks++;
575 from += (1 << this->bbt_erase_shift);
581 * search_bbt - [GENERIC] scan the device for a specific bad block table
582 * @mtd: MTD device structure
583 * @buf: temporary buffer
584 * @td: descriptor for the bad block table
586 * Read the bad block table by searching for a given ident pattern.
587 * Search is preformed either from the beginning up or from the end of
588 * the device downwards. The search starts always at the start of a
590 * If the option NAND_BBT_PERCHIP is given, each chip is searched
591 * for a bbt, which contains the bad block information of this chip.
592 * This is necessary to provide support for certain DOC devices.
594 * The bbt ident pattern resides in the oob area of the first page
597 static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
599 struct nand_chip *this = mtd->priv;
601 int bits, startblock, block, dir;
602 int scanlen = mtd->writesize + mtd->oobsize;
604 int blocktopage = this->bbt_erase_shift - this->page_shift;
606 /* Search direction top -> down ? */
607 if (td->options & NAND_BBT_LASTBLOCK) {
608 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
615 /* Do we have a bbt per chip ? */
616 if (td->options & NAND_BBT_PERCHIP) {
617 chips = this->numchips;
618 bbtblocks = this->chipsize >> this->bbt_erase_shift;
619 startblock &= bbtblocks - 1;
622 bbtblocks = mtd->size >> this->bbt_erase_shift;
625 /* Number of bits for each erase block in the bbt */
626 bits = td->options & NAND_BBT_NRBITS_MSK;
628 for (i = 0; i < chips; i++) {
629 /* Reset version information */
632 /* Scan the maximum number of blocks */
633 for (block = 0; block < td->maxblocks; block++) {
635 int actblock = startblock + dir * block;
636 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
638 /* Read first page */
639 scan_read_raw(mtd, buf, offs, mtd->writesize, td);
640 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
641 td->pages[i] = actblock << blocktopage;
642 if (td->options & NAND_BBT_VERSION) {
643 offs = bbt_get_ver_offs(mtd, td);
644 td->version[i] = buf[offs];
649 startblock += this->chipsize >> this->bbt_erase_shift;
651 /* Check, if we found a bbt for each requested chip */
652 for (i = 0; i < chips; i++) {
653 if (td->pages[i] == -1)
654 printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
656 printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i],
663 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
664 * @mtd: MTD device structure
665 * @buf: temporary buffer
666 * @td: descriptor for the bad block table
667 * @md: descriptor for the bad block table mirror
669 * Search and read the bad block table(s)
671 static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
673 /* Search the primary table */
674 search_bbt(mtd, buf, td);
676 /* Search the mirror table */
678 search_bbt(mtd, buf, md);
680 /* Force result check */
685 * write_bbt - [GENERIC] (Re)write the bad block table
687 * @mtd: MTD device structure
688 * @buf: temporary buffer
689 * @td: descriptor for the bad block table
690 * @md: descriptor for the bad block table mirror
691 * @chipsel: selector for a specific chip, -1 for all
693 * (Re)write the bad block table
696 static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
697 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
700 struct nand_chip *this = mtd->priv;
701 struct erase_info einfo;
702 int i, j, res, chip = 0;
703 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
704 int nrchips, bbtoffs, pageoffs, ooboffs;
706 uint8_t rcode = td->reserved_block_code;
707 size_t retlen, len = 0;
709 struct mtd_oob_ops ops;
711 ops.ooblen = mtd->oobsize;
714 ops.mode = MTD_OOB_PLACE;
718 /* Write bad block table per chip rather than per device ? */
719 if (td->options & NAND_BBT_PERCHIP) {
720 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
721 /* Full device write or specific chip ? */
723 nrchips = this->numchips;
725 nrchips = chipsel + 1;
729 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
733 /* Loop through the chips */
734 for (; chip < nrchips; chip++) {
736 /* There was already a version of the table, reuse the page
737 * This applies for absolute placement too, as we have the
738 * page nr. in td->pages.
740 if (td->pages[chip] != -1) {
741 page = td->pages[chip];
745 /* Automatic placement of the bad block table */
746 /* Search direction top -> down ? */
747 if (td->options & NAND_BBT_LASTBLOCK) {
748 startblock = numblocks * (chip + 1) - 1;
751 startblock = chip * numblocks;
755 for (i = 0; i < td->maxblocks; i++) {
756 int block = startblock + dir * i;
757 /* Check, if the block is bad */
758 switch ((this->bbt[block >> 2] >>
759 (2 * (block & 0x03))) & 0x03) {
765 (this->bbt_erase_shift - this->page_shift);
766 /* Check, if the block is used by the mirror table */
767 if (!md || md->pages[chip] != page)
770 printk(KERN_ERR "No space left to write bad block table\n");
774 /* Set up shift count and masks for the flash table */
775 bits = td->options & NAND_BBT_NRBITS_MSK;
778 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
781 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
784 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
787 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
790 default: return -EINVAL;
793 bbtoffs = chip * (numblocks >> 2);
795 to = ((loff_t) page) << this->page_shift;
797 /* Must we save the block contents ? */
798 if (td->options & NAND_BBT_SAVECONTENT) {
799 /* Make it block aligned */
800 to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
801 len = 1 << this->bbt_erase_shift;
802 res = mtd->read(mtd, to, len, &retlen, buf);
805 printk(KERN_INFO "nand_bbt: Error "
806 "reading block for writing "
807 "the bad block table\n");
810 printk(KERN_WARNING "nand_bbt: ECC error "
811 "while reading block for writing "
812 "bad block table\n");
815 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
816 ops.oobbuf = &buf[len];
817 res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
818 if (res < 0 || ops.oobretlen != ops.ooblen)
821 /* Calc the byte offset in the buffer */
822 pageoffs = page - (int)(to >> this->page_shift);
823 offs = pageoffs << this->page_shift;
824 /* Preset the bbt area with 0xff */
825 memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
826 ooboffs = len + (pageoffs * mtd->oobsize);
828 } else if (td->options & NAND_BBT_NO_OOB) {
831 /* the version byte */
832 if (td->options & NAND_BBT_VERSION)
835 len = (size_t) (numblocks >> sft);
837 /* Make it page aligned ! */
838 len = ALIGN(len, mtd->writesize);
839 /* Preset the buffer with 0xff */
840 memset(buf, 0xff, len);
841 /* Pattern is located at the begin of first page */
842 memcpy(buf, td->pattern, td->len);
845 len = (size_t) (numblocks >> sft);
846 /* Make it page aligned ! */
847 len = ALIGN(len, mtd->writesize);
848 /* Preset the buffer with 0xff */
849 memset(buf, 0xff, len +
850 (len >> this->page_shift)* mtd->oobsize);
853 /* Pattern is located in oob area of first page */
854 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
857 if (td->options & NAND_BBT_VERSION)
858 buf[ooboffs + td->veroffs] = td->version[chip];
860 /* walk through the memory table */
861 for (i = 0; i < numblocks;) {
863 dat = this->bbt[bbtoffs + (i >> 2)];
864 for (j = 0; j < 4; j++, i++) {
865 int sftcnt = (i << (3 - sft)) & sftmsk;
866 /* Do not store the reserved bbt blocks ! */
867 buf[offs + (i >> sft)] &=
868 ~(msk[dat & 0x03] << sftcnt);
873 memset(&einfo, 0, sizeof(einfo));
876 einfo.len = 1 << this->bbt_erase_shift;
877 res = nand_erase_nand(mtd, &einfo, 1);
881 res = scan_write_bbt(mtd, to, len, buf,
882 td->options & NAND_BBT_NO_OOB ? NULL :
887 printk(KERN_DEBUG "Bad block table written to 0x%012llx, version "
888 "0x%02X\n", (unsigned long long)to, td->version[chip]);
890 /* Mark it as used */
891 td->pages[chip] = page;
897 "nand_bbt: Error while writing bad block table %d\n", res);
902 * nand_memory_bbt - [GENERIC] create a memory based bad block table
903 * @mtd: MTD device structure
904 * @bd: descriptor for the good/bad block search pattern
906 * The function creates a memory based bbt by scanning the device
907 * for manufacturer / software marked good / bad blocks
909 static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
911 struct nand_chip *this = mtd->priv;
913 bd->options &= ~NAND_BBT_SCANEMPTY;
914 return create_bbt(mtd, this->buffers->databuf, bd, -1);
918 * check_create - [GENERIC] create and write bbt(s) if necessary
919 * @mtd: MTD device structure
920 * @buf: temporary buffer
921 * @bd: descriptor for the good/bad block search pattern
923 * The function checks the results of the previous call to read_bbt
924 * and creates / updates the bbt(s) if necessary
925 * Creation is necessary if no bbt was found for the chip/device
926 * Update is necessary if one of the tables is missing or the
927 * version nr. of one table is less than the other
929 static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
931 int i, chips, writeops, chipsel, res;
932 struct nand_chip *this = mtd->priv;
933 struct nand_bbt_descr *td = this->bbt_td;
934 struct nand_bbt_descr *md = this->bbt_md;
935 struct nand_bbt_descr *rd, *rd2;
937 /* Do we have a bbt per chip ? */
938 if (td->options & NAND_BBT_PERCHIP)
939 chips = this->numchips;
943 for (i = 0; i < chips; i++) {
947 /* Per chip or per device ? */
948 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
949 /* Mirrored table available ? */
951 if (td->pages[i] == -1 && md->pages[i] == -1) {
956 if (td->pages[i] == -1) {
958 td->version[i] = md->version[i];
963 if (md->pages[i] == -1) {
965 md->version[i] = td->version[i];
970 if (td->version[i] == md->version[i]) {
972 if (!(td->options & NAND_BBT_VERSION))
977 if (((int8_t) (td->version[i] - md->version[i])) > 0) {
979 md->version[i] = td->version[i];
983 td->version[i] = md->version[i];
990 if (td->pages[i] == -1) {
998 /* Create the bad block table by scanning the device ? */
999 if (!(td->options & NAND_BBT_CREATE))
1002 /* Create the table in memory by scanning the chip(s) */
1003 if (!(this->options & NAND_CREATE_EMPTY_BBT))
1004 create_bbt(mtd, buf, bd, chipsel);
1010 /* read back first ? */
1012 read_abs_bbt(mtd, buf, rd, chipsel);
1013 /* If they weren't versioned, read both. */
1015 read_abs_bbt(mtd, buf, rd2, chipsel);
1017 /* Write the bad block table to the device ? */
1018 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1019 res = write_bbt(mtd, buf, td, md, chipsel);
1024 /* Write the mirror bad block table to the device ? */
1025 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1026 res = write_bbt(mtd, buf, md, td, chipsel);
1035 * mark_bbt_regions - [GENERIC] mark the bad block table regions
1036 * @mtd: MTD device structure
1037 * @td: bad block table descriptor
1039 * The bad block table regions are marked as "bad" to prevent
1040 * accidental erasures / writes. The regions are identified by
1043 static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
1045 struct nand_chip *this = mtd->priv;
1046 int i, j, chips, block, nrblocks, update;
1047 uint8_t oldval, newval;
1049 /* Do we have a bbt per chip ? */
1050 if (td->options & NAND_BBT_PERCHIP) {
1051 chips = this->numchips;
1052 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
1055 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
1058 for (i = 0; i < chips; i++) {
1059 if ((td->options & NAND_BBT_ABSPAGE) ||
1060 !(td->options & NAND_BBT_WRITE)) {
1061 if (td->pages[i] == -1)
1063 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
1065 oldval = this->bbt[(block >> 3)];
1066 newval = oldval | (0x2 << (block & 0x06));
1067 this->bbt[(block >> 3)] = newval;
1068 if ((oldval != newval) && td->reserved_block_code)
1069 nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
1073 if (td->options & NAND_BBT_LASTBLOCK)
1074 block = ((i + 1) * nrblocks) - td->maxblocks;
1076 block = i * nrblocks;
1078 for (j = 0; j < td->maxblocks; j++) {
1079 oldval = this->bbt[(block >> 3)];
1080 newval = oldval | (0x2 << (block & 0x06));
1081 this->bbt[(block >> 3)] = newval;
1082 if (oldval != newval)
1086 /* If we want reserved blocks to be recorded to flash, and some
1087 new ones have been marked, then we need to update the stored
1088 bbts. This should only happen once. */
1089 if (update && td->reserved_block_code)
1090 nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
1095 * verify_bbt_descr - verify the bad block description
1096 * @mtd: MTD device structure
1097 * @bd: the table to verify
1099 * This functions performs a few sanity checks on the bad block description
1102 static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1104 struct nand_chip *this = mtd->priv;
1112 pattern_len = bd->len;
1113 bits = bd->options & NAND_BBT_NRBITS_MSK;
1115 BUG_ON((this->options & NAND_USE_FLASH_BBT_NO_OOB) &&
1116 !(this->options & NAND_USE_FLASH_BBT));
1119 if (bd->options & NAND_BBT_VERSION)
1122 if (bd->options & NAND_BBT_NO_OOB) {
1123 BUG_ON(!(this->options & NAND_USE_FLASH_BBT));
1124 BUG_ON(!(this->options & NAND_USE_FLASH_BBT_NO_OOB));
1126 if (bd->options & NAND_BBT_VERSION)
1127 BUG_ON(bd->veroffs != bd->len);
1128 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1131 if (bd->options & NAND_BBT_PERCHIP)
1132 table_size = this->chipsize >> this->bbt_erase_shift;
1134 table_size = mtd->size >> this->bbt_erase_shift;
1137 if (bd->options & NAND_BBT_NO_OOB)
1138 table_size += pattern_len;
1139 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1143 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1144 * @mtd: MTD device structure
1145 * @bd: descriptor for the good/bad block search pattern
1147 * The function checks, if a bad block table(s) is/are already
1148 * available. If not it scans the device for manufacturer
1149 * marked good / bad blocks and writes the bad block table(s) to
1150 * the selected place.
1152 * The bad block table memory is allocated here. It must be freed
1153 * by calling the nand_free_bbt function.
1156 int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1158 struct nand_chip *this = mtd->priv;
1161 struct nand_bbt_descr *td = this->bbt_td;
1162 struct nand_bbt_descr *md = this->bbt_md;
1164 len = mtd->size >> (this->bbt_erase_shift + 2);
1165 /* Allocate memory (2bit per block) and clear the memory bad block table */
1166 this->bbt = kzalloc(len, GFP_KERNEL);
1168 printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
1172 /* If no primary table decriptor is given, scan the device
1173 * to build a memory based bad block table
1176 if ((res = nand_memory_bbt(mtd, bd))) {
1177 printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
1183 verify_bbt_descr(mtd, td);
1184 verify_bbt_descr(mtd, md);
1186 /* Allocate a temporary buffer for one eraseblock incl. oob */
1187 len = (1 << this->bbt_erase_shift);
1188 len += (len >> this->page_shift) * mtd->oobsize;
1191 printk(KERN_ERR "nand_bbt: Out of memory\n");
1197 /* Is the bbt at a given page ? */
1198 if (td->options & NAND_BBT_ABSPAGE) {
1199 res = read_abs_bbts(mtd, buf, td, md);
1201 /* Search the bad block table using a pattern in oob */
1202 res = search_read_bbts(mtd, buf, td, md);
1206 res = check_create(mtd, buf, bd);
1208 /* Prevent the bbt regions from erasing / writing */
1209 mark_bbt_region(mtd, td);
1211 mark_bbt_region(mtd, md);
1213 #ifdef CONFIG_MTD_NAND_RK29
1214 extern void mark_reserve_region(struct mtd_info *mtd,struct nand_bbt_descr *td,struct nand_bbt_descr *md);
1215 mark_reserve_region(mtd, td, md);
1222 * nand_update_bbt - [NAND Interface] update bad block table(s)
1223 * @mtd: MTD device structure
1224 * @offs: the offset of the newly marked block
1226 * The function updates the bad block table(s)
1228 int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
1230 struct nand_chip *this = mtd->priv;
1231 int len, res = 0, writeops = 0;
1234 struct nand_bbt_descr *td = this->bbt_td;
1235 struct nand_bbt_descr *md = this->bbt_md;
1237 if (!this->bbt || !td)
1240 /* Allocate a temporary buffer for one eraseblock incl. oob */
1241 len = (1 << this->bbt_erase_shift);
1242 len += (len >> this->page_shift) * mtd->oobsize;
1243 buf = kmalloc(len, GFP_KERNEL);
1245 printk(KERN_ERR "nand_update_bbt: Out of memory\n");
1249 writeops = md != NULL ? 0x03 : 0x01;
1251 /* Do we have a bbt per chip ? */
1252 if (td->options & NAND_BBT_PERCHIP) {
1253 chip = (int)(offs >> this->chip_shift);
1260 td->version[chip]++;
1262 md->version[chip]++;
1264 /* Write the bad block table to the device ? */
1265 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1266 res = write_bbt(mtd, buf, td, md, chipsel);
1270 /* Write the mirror bad block table to the device ? */
1271 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1272 res = write_bbt(mtd, buf, md, td, chipsel);
1280 /* Define some generic bad / good block scan pattern which are used
1281 * while scanning a device for factory marked good / bad blocks. */
1282 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1284 static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1286 static struct nand_bbt_descr agand_flashbased = {
1287 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1290 .pattern = scan_agand_pattern
1293 /* Generic flash bbt decriptors
1295 static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1296 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1298 static struct nand_bbt_descr bbt_main_descr = {
1299 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1300 | NAND_BBT_2BIT /*| NAND_BBT_VERSION */| NAND_BBT_PERCHIP,
1301 .offs =0, //8, // meet to rk2818 nandc spare
1305 .pattern = bbt_pattern
1308 static struct nand_bbt_descr bbt_mirror_descr = {
1309 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1310 | NAND_BBT_2BIT /*| NAND_BBT_VERSION */| NAND_BBT_PERCHIP,
1311 .offs =0, //8, // meet to rk2818 nandc spare
1315 .pattern = mirror_pattern
1318 static struct nand_bbt_descr bbt_main_no_bbt_descr = {
1319 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1320 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1325 .pattern = bbt_pattern
1328 static struct nand_bbt_descr bbt_mirror_no_bbt_descr = {
1329 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1330 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1335 .pattern = mirror_pattern
1338 #define BBT_SCAN_OPTIONS (NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE | \
1339 NAND_BBT_SCANBYTE1AND6)
1341 * nand_create_default_bbt_descr - [Internal] Creates a BBT descriptor structure
1342 * @this: NAND chip to create descriptor for
1344 * This function allocates and initializes a nand_bbt_descr for BBM detection
1345 * based on the properties of "this". The new descriptor is stored in
1346 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1347 * passed to this function.
1350 static int nand_create_default_bbt_descr(struct nand_chip *this)
1352 struct nand_bbt_descr *bd;
1353 if (this->badblock_pattern) {
1354 printk(KERN_WARNING "BBT descr already allocated; not replacing.\n");
1357 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1359 printk(KERN_ERR "nand_create_default_bbt_descr: Out of memory\n");
1362 bd->options = this->options & BBT_SCAN_OPTIONS;
1363 bd->offs = this->badblockpos;
1364 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1365 bd->pattern = scan_ff_pattern;
1366 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1367 this->badblock_pattern = bd;
1372 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
1373 * @mtd: MTD device structure
1375 * This function selects the default bad block table
1376 * support for the device and calls the nand_scan_bbt function
1379 int nand_default_bbt(struct mtd_info *mtd)
1381 struct nand_chip *this = mtd->priv;
1383 /* Default for AG-AND. We must use a flash based
1384 * bad block table as the devices have factory marked
1385 * _good_ blocks. Erasing those blocks leads to loss
1386 * of the good / bad information, so we _must_ store
1387 * this information in a good / bad table during
1390 if (this->options & NAND_IS_AND) {
1391 /* Use the default pattern descriptors */
1392 if (!this->bbt_td) {
1393 this->bbt_td = &bbt_main_descr;
1394 this->bbt_md = &bbt_mirror_descr;
1396 this->options |= NAND_USE_FLASH_BBT;
1397 return nand_scan_bbt(mtd, &agand_flashbased);
1400 /* Is a flash based bad block table requested ? */
1401 if (this->options & NAND_USE_FLASH_BBT) {
1402 /* Use the default pattern descriptors */
1403 if (!this->bbt_td) {
1404 if (this->options & NAND_USE_FLASH_BBT_NO_OOB) {
1405 this->bbt_td = &bbt_main_no_bbt_descr;
1406 this->bbt_md = &bbt_mirror_no_bbt_descr;
1408 this->bbt_td = &bbt_main_descr;
1409 this->bbt_md = &bbt_mirror_descr;
1413 this->bbt_td = NULL;
1414 this->bbt_md = NULL;
1417 if (!this->badblock_pattern)
1418 nand_create_default_bbt_descr(this);
1420 return nand_scan_bbt(mtd, this->badblock_pattern);
1424 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1425 * @mtd: MTD device structure
1426 * @offs: offset in the device
1427 * @allowbbt: allow access to bad block table region
1430 int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
1432 struct nand_chip *this = mtd->priv;
1436 /* Get block number * 2 */
1437 block = (int)(offs >> (this->bbt_erase_shift - 1));
1438 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1440 DEBUG(MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1441 (unsigned int)offs, block >> 1, res);
1449 return allowbbt ? 0 : 1;
1454 EXPORT_SYMBOL(nand_scan_bbt);
1455 EXPORT_SYMBOL(nand_default_bbt);