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
64 #include <linux/mtd/compat.h>
65 #include <linux/mtd/mtd.h>
66 #include <linux/mtd/nand.h>
68 #include <asm/errno.h>
72 #include <linux/slab.h>
73 #include <linux/types.h>
74 #include <linux/mtd/mtd.h>
75 #include <linux/mtd/nand.h>
76 #include <linux/mtd/nand_ecc.h>
77 #include <linux/mtd/compatmac.h>
78 #include <linux/bitops.h>
79 #include <linux/delay.h>
80 #include <linux/vmalloc.h>
82 static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
86 ret = memcmp(buf, td->pattern, td->len);
93 * check_pattern - [GENERIC] check if a pattern is in the buffer
94 * @buf: the buffer to search
95 * @len: the length of buffer to search
96 * @paglen: the pagelength
97 * @td: search pattern descriptor
99 * Check for a pattern at the given place. Used to search bad block
100 * tables and good / bad block identifiers.
101 * If the SCAN_EMPTY option is set then check, if all bytes except the
102 * pattern area contain 0xff
105 static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
110 if (td->options & NAND_BBT_NO_OOB)
111 return check_pattern_no_oob(buf, td);
113 end = paglen + td->offs;
114 if (td->options & NAND_BBT_SCANEMPTY) {
115 for (i = 0; i < end; i++) {
122 /* Compare the pattern */
123 for (i = 0; i < td->len; i++) {
124 if (p[i] != td->pattern[i])
128 if (td->options & NAND_BBT_SCANEMPTY) {
131 for (i = end; i < len; i++) {
140 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
141 * @buf: the buffer to search
142 * @td: search pattern descriptor
144 * Check for a pattern at the given place. Used to search bad block
145 * tables and good / bad block identifiers. Same as check_pattern, but
146 * no optional empty check
149 static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
154 /* Compare the pattern */
155 for (i = 0; i < td->len; i++) {
156 if (p[td->offs + i] != td->pattern[i])
163 * add_marker_len - compute the length of the marker in data area
164 * @td: BBT descriptor used for computation
166 * The length will be 0 if the markeris located in OOB area.
168 static u32 add_marker_len(struct nand_bbt_descr *td)
172 if (!(td->options & NAND_BBT_NO_OOB))
176 if (td->options & NAND_BBT_VERSION)
182 * read_bbt - [GENERIC] Read the bad block table starting from page
183 * @mtd: MTD device structure
184 * @buf: temporary buffer
185 * @page: the starting page
186 * @num: the number of bbt descriptors to read
187 * @td: the bbt describtion table
188 * @offs: offset in the memory table
190 * Read the bad block table starting from page.
193 static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
194 struct nand_bbt_descr *td, int offs)
196 int res, i, j, act = 0;
197 struct nand_chip *this = mtd->priv;
198 size_t retlen, len, totlen;
200 int bits = td->options & NAND_BBT_NRBITS_MSK;
201 uint8_t msk = (uint8_t) ((1 << bits) - 1);
203 int reserved_block_code = td->reserved_block_code;
205 totlen = (num * bits) >> 3;
206 marker_len = add_marker_len(td);
207 from = ((loff_t) page) << this->page_shift;
210 len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
213 * In case the BBT marker is not in the OOB area it
214 * will be just in the first page.
220 res = mtd->read(mtd, from, len, &retlen, buf);
223 printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
226 printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
230 for (i = 0; i < len; i++) {
231 uint8_t dat = buf[i];
232 for (j = 0; j < 8; j += bits, act += 2) {
233 uint8_t tmp = (dat >> j) & msk;
236 if (reserved_block_code && (tmp == reserved_block_code)) {
237 printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%08x\n",
238 ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
239 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
240 mtd->ecc_stats.bbtblocks++;
243 /* Leave it for now, if its matured we can move this
244 * message to MTD_DEBUG_LEVEL0 */
245 MTDDEBUG(MTD_DEBUG_LEVEL0, "nand_read_bbt: Bad block at 0x%08x\n",
246 ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
247 /* Factory marked bad or worn out ? */
249 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
251 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
252 mtd->ecc_stats.badblocks++;
262 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
263 * @mtd: MTD device structure
264 * @buf: temporary buffer
265 * @td: descriptor for the bad block table
266 * @chip: read the table for a specific chip, -1 read all chips.
267 * Applies only if NAND_BBT_PERCHIP option is set
269 * Read the bad block table for all chips starting at a given page
270 * We assume that the bbt bits are in consecutive order.
272 static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
274 struct nand_chip *this = mtd->priv;
277 if (td->options & NAND_BBT_PERCHIP) {
279 for (i = 0; i < this->numchips; i++) {
280 if (chip == -1 || chip == i)
281 res = read_bbt(mtd, buf, td->pages[i],
282 this->chipsize >> this->bbt_erase_shift,
286 offs += this->chipsize >> (this->bbt_erase_shift + 2);
289 res = read_bbt(mtd, buf, td->pages[0],
290 mtd->size >> this->bbt_erase_shift, td, 0);
298 * BBT marker is in the first page, no OOB.
300 static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
301 struct nand_bbt_descr *td)
307 if (td->options & NAND_BBT_VERSION)
310 return mtd->read(mtd, offs, len, &retlen, buf);
314 * Scan read raw data from flash
316 static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
319 struct mtd_oob_ops ops;
322 ops.mode = MTD_OOB_RAW;
324 ops.ooblen = mtd->oobsize;
328 if (len <= mtd->writesize) {
329 ops.oobbuf = buf + len;
332 return mtd->read_oob(mtd, offs, &ops);
334 ops.oobbuf = buf + mtd->writesize;
336 ops.len = mtd->writesize;
337 res = mtd->read_oob(mtd, offs, &ops);
343 buf += mtd->oobsize + mtd->writesize;
344 len -= mtd->writesize;
349 static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
350 size_t len, struct nand_bbt_descr *td)
352 if (td->options & NAND_BBT_NO_OOB)
353 return scan_read_raw_data(mtd, buf, offs, td);
355 return scan_read_raw_oob(mtd, buf, offs, len);
359 * Scan write data with oob to flash
361 static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
362 uint8_t *buf, uint8_t *oob)
364 struct mtd_oob_ops ops;
366 ops.mode = MTD_OOB_PLACE;
368 ops.ooblen = mtd->oobsize;
373 return mtd->write_oob(mtd, offs, &ops);
376 static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
378 u32 ver_offs = td->veroffs;
380 if (!(td->options & NAND_BBT_NO_OOB))
381 ver_offs += mtd->writesize;
386 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
387 * @mtd: MTD device structure
388 * @buf: temporary buffer
389 * @td: descriptor for the bad block table
390 * @md: descriptor for the bad block table mirror
392 * Read the bad block table(s) for all chips starting at a given page
393 * We assume that the bbt bits are in consecutive order.
396 static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
397 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
399 struct nand_chip *this = mtd->priv;
401 /* Read the primary version, if available */
402 if (td->options & NAND_BBT_VERSION) {
403 scan_read_raw(mtd, buf, td->pages[0] << this->page_shift,
405 td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
406 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
407 td->pages[0], td->version[0]);
410 /* Read the mirror version, if available */
411 if (md && (md->options & NAND_BBT_VERSION)) {
412 scan_read_raw(mtd, buf, md->pages[0] << this->page_shift,
414 md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
415 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
416 md->pages[0], md->version[0]);
422 * Scan a given block full
424 static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
425 loff_t offs, uint8_t *buf, size_t readlen,
426 int scanlen, int len)
430 ret = scan_read_raw_oob(mtd, buf, offs, readlen);
434 for (j = 0; j < len; j++, buf += scanlen) {
435 if (check_pattern(buf, scanlen, mtd->writesize, bd))
442 * Scan a given block partially
444 static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
445 loff_t offs, uint8_t *buf, int len)
447 struct mtd_oob_ops ops;
450 ops.ooblen = mtd->oobsize;
454 ops.mode = MTD_OOB_PLACE;
456 for (j = 0; j < len; j++) {
458 * Read the full oob until read_oob is fixed to
459 * handle single byte reads for 16 bit
462 ret = mtd->read_oob(mtd, offs, &ops);
466 if (check_short_pattern(buf, bd))
469 offs += mtd->writesize;
475 * create_bbt - [GENERIC] Create a bad block table by scanning the device
476 * @mtd: MTD device structure
477 * @buf: temporary buffer
478 * @bd: descriptor for the good/bad block search pattern
479 * @chip: create the table for a specific chip, -1 read all chips.
480 * Applies only if NAND_BBT_PERCHIP option is set
482 * Create a bad block table by scanning the device
483 * for the given good/bad block identify pattern
485 static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
486 struct nand_bbt_descr *bd, int chip)
488 struct nand_chip *this = mtd->priv;
489 int i, numblocks, len, scanlen;
494 printk(KERN_INFO "Scanning device for bad blocks\n");
496 if (bd->options & NAND_BBT_SCANALLPAGES)
497 len = 1 << (this->bbt_erase_shift - this->page_shift);
498 else if (bd->options & NAND_BBT_SCAN2NDPAGE)
503 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
504 /* We need only read few bytes from the OOB area */
508 /* Full page content should be read */
509 scanlen = mtd->writesize + mtd->oobsize;
510 readlen = len * mtd->writesize;
514 /* Note that numblocks is 2 * (real numblocks) here, see i+=2
515 * below as it makes shifting and masking less painful */
516 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
520 if (chip >= this->numchips) {
521 printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
522 chip + 1, this->numchips);
525 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
526 startblock = chip * numblocks;
527 numblocks += startblock;
528 from = startblock << (this->bbt_erase_shift - 1);
531 for (i = startblock; i < numblocks;) {
534 BUG_ON(bd->options & NAND_BBT_NO_OOB);
536 if (bd->options & NAND_BBT_SCANALLPAGES)
537 ret = scan_block_full(mtd, bd, from, buf, readlen,
540 ret = scan_block_fast(mtd, bd, from, buf, len);
546 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
547 printk(KERN_WARNING "Bad eraseblock %d at 0x%012llx\n",
548 i >> 1, (unsigned long long)from);
549 mtd->ecc_stats.badblocks++;
553 from += (1 << this->bbt_erase_shift);
559 * search_bbt - [GENERIC] scan the device for a specific bad block table
560 * @mtd: MTD device structure
561 * @buf: temporary buffer
562 * @td: descriptor for the bad block table
564 * Read the bad block table by searching for a given ident pattern.
565 * Search is preformed either from the beginning up or from the end of
566 * the device downwards. The search starts always at the start of a
568 * If the option NAND_BBT_PERCHIP is given, each chip is searched
569 * for a bbt, which contains the bad block information of this chip.
570 * This is necessary to provide support for certain DOC devices.
572 * The bbt ident pattern resides in the oob area of the first page
575 static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
577 struct nand_chip *this = mtd->priv;
579 int bits, startblock, block, dir;
580 int scanlen = mtd->writesize + mtd->oobsize;
582 int blocktopage = this->bbt_erase_shift - this->page_shift;
584 /* Search direction top -> down ? */
585 if (td->options & NAND_BBT_LASTBLOCK) {
586 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
593 /* Do we have a bbt per chip ? */
594 if (td->options & NAND_BBT_PERCHIP) {
595 chips = this->numchips;
596 bbtblocks = this->chipsize >> this->bbt_erase_shift;
597 startblock &= bbtblocks - 1;
600 bbtblocks = mtd->size >> this->bbt_erase_shift;
603 /* Number of bits for each erase block in the bbt */
604 bits = td->options & NAND_BBT_NRBITS_MSK;
606 for (i = 0; i < chips; i++) {
607 /* Reset version information */
610 /* Scan the maximum number of blocks */
611 for (block = 0; block < td->maxblocks; block++) {
613 int actblock = startblock + dir * block;
614 loff_t offs = actblock << this->bbt_erase_shift;
616 /* Read first page */
617 scan_read_raw(mtd, buf, offs, mtd->writesize, td);
618 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
619 td->pages[i] = actblock << blocktopage;
620 if (td->options & NAND_BBT_VERSION) {
621 offs = bbt_get_ver_offs(mtd, td);
622 td->version[i] = buf[offs];
627 startblock += this->chipsize >> this->bbt_erase_shift;
629 /* Check, if we found a bbt for each requested chip */
630 for (i = 0; i < chips; i++) {
631 if (td->pages[i] == -1)
632 printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
634 MTDDEBUG(MTD_DEBUG_LEVEL0,
635 "Bad block table found at page %d, version 0x%02X\n",
636 td->pages[i], td->version[i]);
642 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
643 * @mtd: MTD device structure
644 * @buf: temporary buffer
645 * @td: descriptor for the bad block table
646 * @md: descriptor for the bad block table mirror
648 * Search and read the bad block table(s)
650 static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
652 /* Search the primary table */
653 search_bbt(mtd, buf, td);
655 /* Search the mirror table */
657 search_bbt(mtd, buf, md);
659 /* Force result check */
664 * write_bbt - [GENERIC] (Re)write the bad block table
666 * @mtd: MTD device structure
667 * @buf: temporary buffer
668 * @td: descriptor for the bad block table
669 * @md: descriptor for the bad block table mirror
670 * @chipsel: selector for a specific chip, -1 for all
672 * (Re)write the bad block table
675 static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
676 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
679 struct nand_chip *this = mtd->priv;
680 struct erase_info einfo;
681 int i, j, res, chip = 0;
682 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
683 int nrchips, bbtoffs, pageoffs, ooboffs;
685 uint8_t rcode = td->reserved_block_code;
686 size_t retlen, len = 0;
688 struct mtd_oob_ops ops;
690 ops.ooblen = mtd->oobsize;
693 ops.mode = MTD_OOB_PLACE;
697 /* Write bad block table per chip rather than per device ? */
698 if (td->options & NAND_BBT_PERCHIP) {
699 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
700 /* Full device write or specific chip ? */
702 nrchips = this->numchips;
704 nrchips = chipsel + 1;
708 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
712 /* Loop through the chips */
713 for (; chip < nrchips; chip++) {
715 /* There was already a version of the table, reuse the page
716 * This applies for absolute placement too, as we have the
717 * page nr. in td->pages.
719 if (td->pages[chip] != -1) {
720 page = td->pages[chip];
724 /* Automatic placement of the bad block table */
725 /* Search direction top -> down ? */
726 if (td->options & NAND_BBT_LASTBLOCK) {
727 startblock = numblocks * (chip + 1) - 1;
730 startblock = chip * numblocks;
734 for (i = 0; i < td->maxblocks; i++) {
735 int block = startblock + dir * i;
736 /* Check, if the block is bad */
737 switch ((this->bbt[block >> 2] >>
738 (2 * (block & 0x03))) & 0x03) {
744 (this->bbt_erase_shift - this->page_shift);
745 /* Check, if the block is used by the mirror table */
746 if (!md || md->pages[chip] != page)
749 printk(KERN_ERR "No space left to write bad block table\n");
753 /* Set up shift count and masks for the flash table */
754 bits = td->options & NAND_BBT_NRBITS_MSK;
757 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
760 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
763 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
766 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
769 default: return -EINVAL;
772 bbtoffs = chip * (numblocks >> 2);
774 to = ((loff_t) page) << this->page_shift;
776 /* Must we save the block contents ? */
777 if (td->options & NAND_BBT_SAVECONTENT) {
778 /* Make it block aligned */
779 to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
780 len = 1 << this->bbt_erase_shift;
781 res = mtd->read(mtd, to, len, &retlen, buf);
784 printk(KERN_INFO "nand_bbt: Error "
785 "reading block for writing "
786 "the bad block table\n");
789 printk(KERN_WARNING "nand_bbt: ECC error "
790 "while reading block for writing "
791 "bad block table\n");
794 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
795 ops.oobbuf = &buf[len];
796 res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
797 if (res < 0 || ops.oobretlen != ops.ooblen)
800 /* Calc the byte offset in the buffer */
801 pageoffs = page - (int)(to >> this->page_shift);
802 offs = pageoffs << this->page_shift;
803 /* Preset the bbt area with 0xff */
804 memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
805 ooboffs = len + (pageoffs * mtd->oobsize);
807 } else if (td->options & NAND_BBT_NO_OOB) {
810 /* the version byte */
811 if (td->options & NAND_BBT_VERSION)
814 len = (size_t) (numblocks >> sft);
816 /* Make it page aligned ! */
817 len = ALIGN(len, mtd->writesize);
818 /* Preset the buffer with 0xff */
819 memset(buf, 0xff, len);
820 /* Pattern is located at the begin of first page */
821 memcpy(buf, td->pattern, td->len);
824 len = (size_t) (numblocks >> sft);
825 /* Make it page aligned ! */
826 len = ALIGN(len, mtd->writesize);
827 /* Preset the buffer with 0xff */
828 memset(buf, 0xff, len +
829 (len >> this->page_shift)* mtd->oobsize);
832 /* Pattern is located in oob area of first page */
833 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
836 if (td->options & NAND_BBT_VERSION)
837 buf[ooboffs + td->veroffs] = td->version[chip];
839 /* walk through the memory table */
840 for (i = 0; i < numblocks;) {
842 dat = this->bbt[bbtoffs + (i >> 2)];
843 for (j = 0; j < 4; j++, i++) {
844 int sftcnt = (i << (3 - sft)) & sftmsk;
845 /* Do not store the reserved bbt blocks ! */
846 buf[offs + (i >> sft)] &=
847 ~(msk[dat & 0x03] << sftcnt);
852 memset(&einfo, 0, sizeof(einfo));
854 einfo.addr = (unsigned long)to;
855 einfo.len = 1 << this->bbt_erase_shift;
856 res = nand_erase_nand(mtd, &einfo, 1);
860 res = scan_write_bbt(mtd, to, len, buf,
861 td->options & NAND_BBT_NO_OOB ? NULL :
866 printk(KERN_DEBUG "Bad block table written to 0x%08x, version "
867 "0x%02X\n", (unsigned int)to, td->version[chip]);
869 /* Mark it as used */
870 td->pages[chip] = page;
876 "nand_bbt: Error while writing bad block table %d\n", res);
881 * nand_memory_bbt - [GENERIC] create a memory based bad block table
882 * @mtd: MTD device structure
883 * @bd: descriptor for the good/bad block search pattern
885 * The function creates a memory based bbt by scanning the device
886 * for manufacturer / software marked good / bad blocks
888 static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
890 struct nand_chip *this = mtd->priv;
892 bd->options &= ~NAND_BBT_SCANEMPTY;
893 return create_bbt(mtd, this->buffers->databuf, bd, -1);
897 * check_create - [GENERIC] create and write bbt(s) if necessary
898 * @mtd: MTD device structure
899 * @buf: temporary buffer
900 * @bd: descriptor for the good/bad block search pattern
902 * The function checks the results of the previous call to read_bbt
903 * and creates / updates the bbt(s) if necessary
904 * Creation is necessary if no bbt was found for the chip/device
905 * Update is necessary if one of the tables is missing or the
906 * version nr. of one table is less than the other
908 static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
910 int i, chips, writeops, chipsel, res;
911 struct nand_chip *this = mtd->priv;
912 struct nand_bbt_descr *td = this->bbt_td;
913 struct nand_bbt_descr *md = this->bbt_md;
914 struct nand_bbt_descr *rd, *rd2;
916 /* Do we have a bbt per chip ? */
917 if (td->options & NAND_BBT_PERCHIP)
918 chips = this->numchips;
922 for (i = 0; i < chips; i++) {
926 /* Per chip or per device ? */
927 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
928 /* Mirrored table avilable ? */
930 if (td->pages[i] == -1 && md->pages[i] == -1) {
935 if (td->pages[i] == -1) {
937 td->version[i] = md->version[i];
942 if (md->pages[i] == -1) {
944 md->version[i] = td->version[i];
949 if (td->version[i] == md->version[i]) {
951 if (!(td->options & NAND_BBT_VERSION))
956 if (((int8_t) (td->version[i] - md->version[i])) > 0) {
958 md->version[i] = td->version[i];
962 td->version[i] = md->version[i];
969 if (td->pages[i] == -1) {
977 /* Create the bad block table by scanning the device ? */
978 if (!(td->options & NAND_BBT_CREATE))
981 /* Create the table in memory by scanning the chip(s) */
982 create_bbt(mtd, buf, bd, chipsel);
988 /* read back first ? */
990 read_abs_bbt(mtd, buf, rd, chipsel);
991 /* If they weren't versioned, read both. */
993 read_abs_bbt(mtd, buf, rd2, chipsel);
995 /* Write the bad block table to the device ? */
996 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
997 res = write_bbt(mtd, buf, td, md, chipsel);
1002 /* Write the mirror bad block table to the device ? */
1003 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1004 res = write_bbt(mtd, buf, md, td, chipsel);
1013 * mark_bbt_regions - [GENERIC] mark the bad block table regions
1014 * @mtd: MTD device structure
1015 * @td: bad block table descriptor
1017 * The bad block table regions are marked as "bad" to prevent
1018 * accidental erasures / writes. The regions are identified by
1021 static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
1023 struct nand_chip *this = mtd->priv;
1024 int i, j, chips, block, nrblocks, update;
1025 uint8_t oldval, newval;
1027 /* Do we have a bbt per chip ? */
1028 if (td->options & NAND_BBT_PERCHIP) {
1029 chips = this->numchips;
1030 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
1033 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
1036 for (i = 0; i < chips; i++) {
1037 if ((td->options & NAND_BBT_ABSPAGE) ||
1038 !(td->options & NAND_BBT_WRITE)) {
1039 if (td->pages[i] == -1)
1041 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
1043 oldval = this->bbt[(block >> 3)];
1044 newval = oldval | (0x2 << (block & 0x06));
1045 this->bbt[(block >> 3)] = newval;
1046 if ((oldval != newval) && td->reserved_block_code)
1047 nand_update_bbt(mtd, block << (this->bbt_erase_shift - 1));
1051 if (td->options & NAND_BBT_LASTBLOCK)
1052 block = ((i + 1) * nrblocks) - td->maxblocks;
1054 block = i * nrblocks;
1056 for (j = 0; j < td->maxblocks; j++) {
1057 oldval = this->bbt[(block >> 3)];
1058 newval = oldval | (0x2 << (block & 0x06));
1059 this->bbt[(block >> 3)] = newval;
1060 if (oldval != newval)
1064 /* If we want reserved blocks to be recorded to flash, and some
1065 new ones have been marked, then we need to update the stored
1066 bbts. This should only happen once. */
1067 if (update && td->reserved_block_code)
1068 nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
1073 * verify_bbt_descr - verify the bad block description
1074 * @mtd: MTD device structure
1075 * @bd: the table to verify
1077 * This functions performs a few sanity checks on the bad block description
1080 static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1082 struct nand_chip *this = mtd->priv;
1083 u32 pattern_len = bd->len;
1084 u32 bits = bd->options & NAND_BBT_NRBITS_MSK;
1089 BUG_ON((this->options & NAND_USE_FLASH_BBT_NO_OOB) &&
1090 !(this->options & NAND_USE_FLASH_BBT));
1093 if (bd->options & NAND_BBT_VERSION)
1096 if (bd->options & NAND_BBT_NO_OOB) {
1097 BUG_ON(!(this->options & NAND_USE_FLASH_BBT));
1098 BUG_ON(!(this->options & NAND_USE_FLASH_BBT_NO_OOB));
1100 if (bd->options & NAND_BBT_VERSION)
1101 BUG_ON(bd->veroffs != bd->len);
1102 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1105 if (bd->options & NAND_BBT_PERCHIP)
1106 table_size = this->chipsize >> this->bbt_erase_shift;
1108 table_size = mtd->size >> this->bbt_erase_shift;
1111 if (bd->options & NAND_BBT_NO_OOB)
1112 table_size += pattern_len;
1113 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1117 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1118 * @mtd: MTD device structure
1119 * @bd: descriptor for the good/bad block search pattern
1121 * The function checks, if a bad block table(s) is/are already
1122 * available. If not it scans the device for manufacturer
1123 * marked good / bad blocks and writes the bad block table(s) to
1124 * the selected place.
1126 * The bad block table memory is allocated here. It must be freed
1127 * by calling the nand_free_bbt function.
1130 int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1132 struct nand_chip *this = mtd->priv;
1135 struct nand_bbt_descr *td = this->bbt_td;
1136 struct nand_bbt_descr *md = this->bbt_md;
1138 len = mtd->size >> (this->bbt_erase_shift + 2);
1139 /* Allocate memory (2bit per block) and clear the memory bad block table */
1140 this->bbt = kzalloc(len, GFP_KERNEL);
1142 printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
1146 /* If no primary table decriptor is given, scan the device
1147 * to build a memory based bad block table
1150 if ((res = nand_memory_bbt(mtd, bd))) {
1151 printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
1157 verify_bbt_descr(mtd, td);
1158 verify_bbt_descr(mtd, md);
1160 /* Allocate a temporary buffer for one eraseblock incl. oob */
1161 len = (1 << this->bbt_erase_shift);
1162 len += (len >> this->page_shift) * mtd->oobsize;
1165 printk(KERN_ERR "nand_bbt: Out of memory\n");
1171 /* Is the bbt at a given page ? */
1172 if (td->options & NAND_BBT_ABSPAGE) {
1173 res = read_abs_bbts(mtd, buf, td, md);
1175 /* Search the bad block table using a pattern in oob */
1176 res = search_read_bbts(mtd, buf, td, md);
1180 res = check_create(mtd, buf, bd);
1182 /* Prevent the bbt regions from erasing / writing */
1183 mark_bbt_region(mtd, td);
1185 mark_bbt_region(mtd, md);
1192 * nand_update_bbt - [NAND Interface] update bad block table(s)
1193 * @mtd: MTD device structure
1194 * @offs: the offset of the newly marked block
1196 * The function updates the bad block table(s)
1198 int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
1200 struct nand_chip *this = mtd->priv;
1201 int len, res = 0, writeops = 0;
1204 struct nand_bbt_descr *td = this->bbt_td;
1205 struct nand_bbt_descr *md = this->bbt_md;
1207 if (!this->bbt || !td)
1210 /* Allocate a temporary buffer for one eraseblock incl. oob */
1211 len = (1 << this->bbt_erase_shift);
1212 len += (len >> this->page_shift) * mtd->oobsize;
1213 buf = kmalloc(len, GFP_KERNEL);
1215 printk(KERN_ERR "nand_update_bbt: Out of memory\n");
1219 writeops = md != NULL ? 0x03 : 0x01;
1221 /* Do we have a bbt per chip ? */
1222 if (td->options & NAND_BBT_PERCHIP) {
1223 chip = (int)(offs >> this->chip_shift);
1230 td->version[chip]++;
1232 md->version[chip]++;
1234 /* Write the bad block table to the device ? */
1235 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1236 res = write_bbt(mtd, buf, td, md, chipsel);
1240 /* Write the mirror bad block table to the device ? */
1241 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1242 res = write_bbt(mtd, buf, md, td, chipsel);
1250 /* Define some generic bad / good block scan pattern which are used
1251 * while scanning a device for factory marked good / bad blocks. */
1252 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1254 static struct nand_bbt_descr smallpage_flashbased = {
1255 .options = NAND_BBT_SCAN2NDPAGE,
1256 .offs = NAND_SMALL_BADBLOCK_POS,
1258 .pattern = scan_ff_pattern
1261 static struct nand_bbt_descr largepage_flashbased = {
1262 .options = NAND_BBT_SCAN2NDPAGE,
1263 .offs = NAND_LARGE_BADBLOCK_POS,
1265 .pattern = scan_ff_pattern
1268 static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1270 static struct nand_bbt_descr agand_flashbased = {
1271 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1274 .pattern = scan_agand_pattern
1277 /* Generic flash bbt decriptors
1279 static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1280 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1282 static struct nand_bbt_descr bbt_main_descr = {
1283 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1284 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1289 .pattern = bbt_pattern
1292 static struct nand_bbt_descr bbt_mirror_descr = {
1293 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1294 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1299 .pattern = mirror_pattern
1302 static struct nand_bbt_descr bbt_main_no_bbt_descr = {
1303 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1304 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1309 .pattern = bbt_pattern
1312 static struct nand_bbt_descr bbt_mirror_no_bbt_descr = {
1313 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1314 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1319 .pattern = mirror_pattern
1322 #define BBT_SCAN_OPTIONS (NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE | \
1323 NAND_BBT_SCANBYTE1AND6)
1325 * nand_create_default_bbt_descr - [Internal] Creates a BBT descriptor structure
1326 * @this: NAND chip to create descriptor for
1328 * This function allocates and initializes a nand_bbt_descr for BBM detection
1329 * based on the properties of "this". The new descriptor is stored in
1330 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1331 * passed to this function.
1333 * TODO: Handle other flags, replace other static structs
1334 * (e.g. handle NAND_BBT_FLASH for flash-based BBT,
1335 * replace smallpage_flashbased)
1338 static int nand_create_default_bbt_descr(struct nand_chip *this)
1340 struct nand_bbt_descr *bd;
1341 if (this->badblock_pattern) {
1342 printk(KERN_WARNING "BBT descr already allocated; not replacing.\n");
1345 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1347 printk(KERN_ERR "nand_create_default_bbt_descr: Out of memory\n");
1350 bd->options = this->options & BBT_SCAN_OPTIONS;
1351 bd->offs = this->badblockpos;
1352 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1353 bd->pattern = scan_ff_pattern;
1354 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1355 this->badblock_pattern = bd;
1360 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
1361 * @mtd: MTD device structure
1363 * This function selects the default bad block table
1364 * support for the device and calls the nand_scan_bbt function
1367 int nand_default_bbt(struct mtd_info *mtd)
1369 struct nand_chip *this = mtd->priv;
1371 /* Default for AG-AND. We must use a flash based
1372 * bad block table as the devices have factory marked
1373 * _good_ blocks. Erasing those blocks leads to loss
1374 * of the good / bad information, so we _must_ store
1375 * this information in a good / bad table during
1378 if (this->options & NAND_IS_AND) {
1379 /* Use the default pattern descriptors */
1380 if (!this->bbt_td) {
1381 this->bbt_td = &bbt_main_descr;
1382 this->bbt_md = &bbt_mirror_descr;
1384 this->options |= NAND_USE_FLASH_BBT;
1385 return nand_scan_bbt(mtd, &agand_flashbased);
1388 /* Is a flash based bad block table requested ? */
1389 if (this->options & NAND_USE_FLASH_BBT) {
1390 /* Use the default pattern descriptors */
1391 if (!this->bbt_td) {
1392 if (this->options & NAND_USE_FLASH_BBT_NO_OOB) {
1393 this->bbt_td = &bbt_main_no_bbt_descr;
1394 this->bbt_md = &bbt_mirror_no_bbt_descr;
1396 this->bbt_td = &bbt_main_descr;
1397 this->bbt_md = &bbt_mirror_descr;
1400 if (!this->badblock_pattern) {
1401 this->badblock_pattern = (mtd->writesize > 512) ? &largepage_flashbased : &smallpage_flashbased;
1404 this->bbt_td = NULL;
1405 this->bbt_md = NULL;
1406 if (!this->badblock_pattern)
1407 nand_create_default_bbt_descr(this);
1409 return nand_scan_bbt(mtd, this->badblock_pattern);
1413 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1414 * @mtd: MTD device structure
1415 * @offs: offset in the device
1416 * @allowbbt: allow access to bad block table region
1419 int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
1421 struct nand_chip *this = mtd->priv;
1425 /* Get block number * 2 */
1426 block = (int)(offs >> (this->bbt_erase_shift - 1));
1427 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1429 MTDDEBUG (MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: "
1430 "(block %d) 0x%02x\n", (unsigned int)offs, res, block >> 1);
1438 return allowbbt ? 0 : 1;
1443 /* XXX U-BOOT XXX */
1445 EXPORT_SYMBOL(nand_scan_bbt);
1446 EXPORT_SYMBOL(nand_default_bbt);