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_BBT_USE_FLASH) 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_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
40 * course): it moves the ident pattern and the version byte into the data area
41 * and the OOB area will remain untouched.
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 if (td->options & NAND_BBT_SCANEMPTY) {
120 for (i = end; i < len; i++) {
129 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
130 * @buf: the buffer to search
131 * @td: search pattern descriptor
133 * Check for a pattern at the given place. Used to search bad block
134 * tables and good / bad block identifiers. Same as check_pattern, but
135 * no optional empty check
138 static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
143 /* Compare the pattern */
144 for (i = 0; i < td->len; i++) {
145 if (p[td->offs + i] != td->pattern[i])
152 * add_marker_len - compute the length of the marker in data area
153 * @td: BBT descriptor used for computation
155 * The length will be 0 if the markeris located in OOB area.
157 static u32 add_marker_len(struct nand_bbt_descr *td)
161 if (!(td->options & NAND_BBT_NO_OOB))
165 if (td->options & NAND_BBT_VERSION)
171 * read_bbt - [GENERIC] Read the bad block table starting from page
172 * @mtd: MTD device structure
173 * @buf: temporary buffer
174 * @page: the starting page
175 * @num: the number of bbt descriptors to read
176 * @td: the bbt describtion table
177 * @offs: offset in the memory table
179 * Read the bad block table starting from page.
182 static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
183 struct nand_bbt_descr *td, int offs)
185 int res, i, j, act = 0;
186 struct nand_chip *this = mtd->priv;
187 size_t retlen, len, totlen;
189 int bits = td->options & NAND_BBT_NRBITS_MSK;
190 uint8_t msk = (uint8_t) ((1 << bits) - 1);
192 int reserved_block_code = td->reserved_block_code;
194 totlen = (num * bits) >> 3;
195 marker_len = add_marker_len(td);
196 from = ((loff_t) page) << this->page_shift;
199 len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
202 * In case the BBT marker is not in the OOB area it
203 * will be just in the first page.
209 res = mtd->read(mtd, from, len, &retlen, buf);
212 printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
215 printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
219 for (i = 0; i < len; i++) {
220 uint8_t dat = buf[i];
221 for (j = 0; j < 8; j += bits, act += 2) {
222 uint8_t tmp = (dat >> j) & msk;
225 if (reserved_block_code && (tmp == reserved_block_code)) {
226 printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%012llx\n",
227 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
228 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
229 mtd->ecc_stats.bbtblocks++;
232 /* Leave it for now, if its matured we can move this
233 * message to MTD_DEBUG_LEVEL0 */
234 printk(KERN_DEBUG "nand_read_bbt: Bad block at 0x%012llx\n",
235 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
236 /* Factory marked bad or worn out ? */
238 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
240 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
241 mtd->ecc_stats.badblocks++;
251 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
252 * @mtd: MTD device structure
253 * @buf: temporary buffer
254 * @td: descriptor for the bad block table
255 * @chip: read the table for a specific chip, -1 read all chips.
256 * Applies only if NAND_BBT_PERCHIP option is set
258 * Read the bad block table for all chips starting at a given page
259 * We assume that the bbt bits are in consecutive order.
261 static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
263 struct nand_chip *this = mtd->priv;
266 if (td->options & NAND_BBT_PERCHIP) {
268 for (i = 0; i < this->numchips; i++) {
269 if (chip == -1 || chip == i)
270 res = read_bbt(mtd, buf, td->pages[i],
271 this->chipsize >> this->bbt_erase_shift,
275 offs += this->chipsize >> (this->bbt_erase_shift + 2);
278 res = read_bbt(mtd, buf, td->pages[0],
279 mtd->size >> this->bbt_erase_shift, td, 0);
287 * BBT marker is in the first page, no OOB.
289 static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
290 struct nand_bbt_descr *td)
296 if (td->options & NAND_BBT_VERSION)
299 return mtd->read(mtd, offs, len, &retlen, buf);
303 * Scan read raw data from flash
305 static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
308 struct mtd_oob_ops ops;
311 ops.mode = MTD_OOB_RAW;
313 ops.ooblen = mtd->oobsize;
317 if (len <= mtd->writesize) {
318 ops.oobbuf = buf + len;
321 return mtd->read_oob(mtd, offs, &ops);
323 ops.oobbuf = buf + mtd->writesize;
325 ops.len = mtd->writesize;
326 res = mtd->read_oob(mtd, offs, &ops);
332 buf += mtd->oobsize + mtd->writesize;
333 len -= mtd->writesize;
338 static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
339 size_t len, struct nand_bbt_descr *td)
341 if (td->options & NAND_BBT_NO_OOB)
342 return scan_read_raw_data(mtd, buf, offs, td);
344 return scan_read_raw_oob(mtd, buf, offs, len);
348 * Scan write data with oob to flash
350 static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
351 uint8_t *buf, uint8_t *oob)
353 struct mtd_oob_ops ops;
355 ops.mode = MTD_OOB_PLACE;
357 ops.ooblen = mtd->oobsize;
362 return mtd->write_oob(mtd, offs, &ops);
365 static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
367 u32 ver_offs = td->veroffs;
369 if (!(td->options & NAND_BBT_NO_OOB))
370 ver_offs += mtd->writesize;
375 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
376 * @mtd: MTD device structure
377 * @buf: temporary buffer
378 * @td: descriptor for the bad block table
379 * @md: descriptor for the bad block table mirror
381 * Read the bad block table(s) for all chips starting at a given page
382 * We assume that the bbt bits are in consecutive order.
385 static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
386 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
388 struct nand_chip *this = mtd->priv;
390 /* Read the primary version, if available */
391 if (td->options & NAND_BBT_VERSION) {
392 scan_read_raw(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
394 td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
395 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
396 td->pages[0], td->version[0]);
399 /* Read the mirror version, if available */
400 if (md && (md->options & NAND_BBT_VERSION)) {
401 scan_read_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
403 md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
404 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
405 md->pages[0], md->version[0]);
411 * Scan a given block full
413 static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
414 loff_t offs, uint8_t *buf, size_t readlen,
415 int scanlen, int len)
419 ret = scan_read_raw_oob(mtd, buf, offs, readlen);
423 for (j = 0; j < len; j++, buf += scanlen) {
424 if (check_pattern(buf, scanlen, mtd->writesize, bd))
431 * Scan a given block partially
433 static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
434 loff_t offs, uint8_t *buf, int len)
436 struct mtd_oob_ops ops;
439 ops.ooblen = mtd->oobsize;
443 ops.mode = MTD_OOB_PLACE;
445 for (j = 0; j < len; j++) {
447 * Read the full oob until read_oob is fixed to
448 * handle single byte reads for 16 bit
451 ret = mtd->read_oob(mtd, offs, &ops);
455 if (check_short_pattern(buf, bd))
458 offs += mtd->writesize;
464 * create_bbt - [GENERIC] Create a bad block table by scanning the device
465 * @mtd: MTD device structure
466 * @buf: temporary buffer
467 * @bd: descriptor for the good/bad block search pattern
468 * @chip: create the table for a specific chip, -1 read all chips.
469 * Applies only if NAND_BBT_PERCHIP option is set
471 * Create a bad block table by scanning the device
472 * for the given good/bad block identify pattern
474 static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
475 struct nand_bbt_descr *bd, int chip)
477 struct nand_chip *this = mtd->priv;
478 int i, numblocks, len, scanlen;
483 printk(KERN_INFO "Scanning device for bad blocks\n");
485 if (bd->options & NAND_BBT_SCANALLPAGES)
486 len = 1 << (this->bbt_erase_shift - this->page_shift);
487 else if (bd->options & NAND_BBT_SCAN2NDPAGE)
492 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
493 /* We need only read few bytes from the OOB area */
497 /* Full page content should be read */
498 scanlen = mtd->writesize + mtd->oobsize;
499 readlen = len * mtd->writesize;
503 /* Note that numblocks is 2 * (real numblocks) here, see i+=2
504 * below as it makes shifting and masking less painful */
505 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
509 if (chip >= this->numchips) {
510 printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
511 chip + 1, this->numchips);
514 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
515 startblock = chip * numblocks;
516 numblocks += startblock;
517 from = (loff_t)startblock << (this->bbt_erase_shift - 1);
520 if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
521 from += mtd->erasesize - (mtd->writesize * len);
523 for (i = startblock; i < numblocks;) {
526 BUG_ON(bd->options & NAND_BBT_NO_OOB);
528 if (bd->options & NAND_BBT_SCANALLPAGES)
529 ret = scan_block_full(mtd, bd, from, buf, readlen,
532 ret = scan_block_fast(mtd, bd, from, buf, len);
538 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
539 printk(KERN_WARNING "Bad eraseblock %d at 0x%012llx\n",
540 i >> 1, (unsigned long long)from);
541 mtd->ecc_stats.badblocks++;
545 from += (1 << this->bbt_erase_shift);
551 * search_bbt - [GENERIC] scan the device for a specific bad block table
552 * @mtd: MTD device structure
553 * @buf: temporary buffer
554 * @td: descriptor for the bad block table
556 * Read the bad block table by searching for a given ident pattern.
557 * Search is preformed either from the beginning up or from the end of
558 * the device downwards. The search starts always at the start of a
560 * If the option NAND_BBT_PERCHIP is given, each chip is searched
561 * for a bbt, which contains the bad block information of this chip.
562 * This is necessary to provide support for certain DOC devices.
564 * The bbt ident pattern resides in the oob area of the first page
567 static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
569 struct nand_chip *this = mtd->priv;
571 int bits, startblock, block, dir;
572 int scanlen = mtd->writesize + mtd->oobsize;
574 int blocktopage = this->bbt_erase_shift - this->page_shift;
576 /* Search direction top -> down ? */
577 if (td->options & NAND_BBT_LASTBLOCK) {
578 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
585 /* Do we have a bbt per chip ? */
586 if (td->options & NAND_BBT_PERCHIP) {
587 chips = this->numchips;
588 bbtblocks = this->chipsize >> this->bbt_erase_shift;
589 startblock &= bbtblocks - 1;
592 bbtblocks = mtd->size >> this->bbt_erase_shift;
595 /* Number of bits for each erase block in the bbt */
596 bits = td->options & NAND_BBT_NRBITS_MSK;
598 for (i = 0; i < chips; i++) {
599 /* Reset version information */
602 /* Scan the maximum number of blocks */
603 for (block = 0; block < td->maxblocks; block++) {
605 int actblock = startblock + dir * block;
606 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
608 /* Read first page */
609 scan_read_raw(mtd, buf, offs, mtd->writesize, td);
610 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
611 td->pages[i] = actblock << blocktopage;
612 if (td->options & NAND_BBT_VERSION) {
613 offs = bbt_get_ver_offs(mtd, td);
614 td->version[i] = buf[offs];
619 startblock += this->chipsize >> this->bbt_erase_shift;
621 /* Check, if we found a bbt for each requested chip */
622 for (i = 0; i < chips; i++) {
623 if (td->pages[i] == -1)
624 printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
626 printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i],
633 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
634 * @mtd: MTD device structure
635 * @buf: temporary buffer
636 * @td: descriptor for the bad block table
637 * @md: descriptor for the bad block table mirror
639 * Search and read the bad block table(s)
641 static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
643 /* Search the primary table */
644 search_bbt(mtd, buf, td);
646 /* Search the mirror table */
648 search_bbt(mtd, buf, md);
650 /* Force result check */
655 * write_bbt - [GENERIC] (Re)write the bad block table
657 * @mtd: MTD device structure
658 * @buf: temporary buffer
659 * @td: descriptor for the bad block table
660 * @md: descriptor for the bad block table mirror
661 * @chipsel: selector for a specific chip, -1 for all
663 * (Re)write the bad block table
666 static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
667 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
670 struct nand_chip *this = mtd->priv;
671 struct erase_info einfo;
672 int i, j, res, chip = 0;
673 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
674 int nrchips, bbtoffs, pageoffs, ooboffs;
676 uint8_t rcode = td->reserved_block_code;
677 size_t retlen, len = 0;
679 struct mtd_oob_ops ops;
681 ops.ooblen = mtd->oobsize;
684 ops.mode = MTD_OOB_PLACE;
688 /* Write bad block table per chip rather than per device ? */
689 if (td->options & NAND_BBT_PERCHIP) {
690 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
691 /* Full device write or specific chip ? */
693 nrchips = this->numchips;
695 nrchips = chipsel + 1;
699 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
703 /* Loop through the chips */
704 for (; chip < nrchips; chip++) {
706 /* There was already a version of the table, reuse the page
707 * This applies for absolute placement too, as we have the
708 * page nr. in td->pages.
710 if (td->pages[chip] != -1) {
711 page = td->pages[chip];
715 /* Automatic placement of the bad block table */
716 /* Search direction top -> down ? */
717 if (td->options & NAND_BBT_LASTBLOCK) {
718 startblock = numblocks * (chip + 1) - 1;
721 startblock = chip * numblocks;
725 for (i = 0; i < td->maxblocks; i++) {
726 int block = startblock + dir * i;
727 /* Check, if the block is bad */
728 switch ((this->bbt[block >> 2] >>
729 (2 * (block & 0x03))) & 0x03) {
735 (this->bbt_erase_shift - this->page_shift);
736 /* Check, if the block is used by the mirror table */
737 if (!md || md->pages[chip] != page)
740 printk(KERN_ERR "No space left to write bad block table\n");
744 /* Set up shift count and masks for the flash table */
745 bits = td->options & NAND_BBT_NRBITS_MSK;
748 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
751 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
754 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
757 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
760 default: return -EINVAL;
763 bbtoffs = chip * (numblocks >> 2);
765 to = ((loff_t) page) << this->page_shift;
767 /* Must we save the block contents ? */
768 if (td->options & NAND_BBT_SAVECONTENT) {
769 /* Make it block aligned */
770 to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
771 len = 1 << this->bbt_erase_shift;
772 res = mtd->read(mtd, to, len, &retlen, buf);
775 printk(KERN_INFO "nand_bbt: Error "
776 "reading block for writing "
777 "the bad block table\n");
780 printk(KERN_WARNING "nand_bbt: ECC error "
781 "while reading block for writing "
782 "bad block table\n");
785 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
786 ops.oobbuf = &buf[len];
787 res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
788 if (res < 0 || ops.oobretlen != ops.ooblen)
791 /* Calc the byte offset in the buffer */
792 pageoffs = page - (int)(to >> this->page_shift);
793 offs = pageoffs << this->page_shift;
794 /* Preset the bbt area with 0xff */
795 memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
796 ooboffs = len + (pageoffs * mtd->oobsize);
798 } else if (td->options & NAND_BBT_NO_OOB) {
801 /* the version byte */
802 if (td->options & NAND_BBT_VERSION)
805 len = (size_t) (numblocks >> sft);
807 /* Make it page aligned ! */
808 len = ALIGN(len, mtd->writesize);
809 /* Preset the buffer with 0xff */
810 memset(buf, 0xff, len);
811 /* Pattern is located at the begin of first page */
812 memcpy(buf, td->pattern, td->len);
815 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 (len >> this->page_shift)* mtd->oobsize);
823 /* Pattern is located in oob area of first page */
824 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
827 if (td->options & NAND_BBT_VERSION)
828 buf[ooboffs + td->veroffs] = td->version[chip];
830 /* walk through the memory table */
831 for (i = 0; i < numblocks;) {
833 dat = this->bbt[bbtoffs + (i >> 2)];
834 for (j = 0; j < 4; j++, i++) {
835 int sftcnt = (i << (3 - sft)) & sftmsk;
836 /* Do not store the reserved bbt blocks ! */
837 buf[offs + (i >> sft)] &=
838 ~(msk[dat & 0x03] << sftcnt);
843 memset(&einfo, 0, sizeof(einfo));
846 einfo.len = 1 << this->bbt_erase_shift;
847 res = nand_erase_nand(mtd, &einfo, 1);
851 res = scan_write_bbt(mtd, to, len, buf,
852 td->options & NAND_BBT_NO_OOB ? NULL :
857 printk(KERN_DEBUG "Bad block table written to 0x%012llx, version "
858 "0x%02X\n", (unsigned long long)to, td->version[chip]);
860 /* Mark it as used */
861 td->pages[chip] = page;
867 "nand_bbt: Error while writing bad block table %d\n", res);
872 * nand_memory_bbt - [GENERIC] create a memory based bad block table
873 * @mtd: MTD device structure
874 * @bd: descriptor for the good/bad block search pattern
876 * The function creates a memory based bbt by scanning the device
877 * for manufacturer / software marked good / bad blocks
879 static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
881 struct nand_chip *this = mtd->priv;
883 bd->options &= ~NAND_BBT_SCANEMPTY;
884 return create_bbt(mtd, this->buffers->databuf, bd, -1);
888 * check_create - [GENERIC] create and write bbt(s) if necessary
889 * @mtd: MTD device structure
890 * @buf: temporary buffer
891 * @bd: descriptor for the good/bad block search pattern
893 * The function checks the results of the previous call to read_bbt
894 * and creates / updates the bbt(s) if necessary
895 * Creation is necessary if no bbt was found for the chip/device
896 * Update is necessary if one of the tables is missing or the
897 * version nr. of one table is less than the other
899 static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
901 int i, chips, writeops, chipsel, res;
902 struct nand_chip *this = mtd->priv;
903 struct nand_bbt_descr *td = this->bbt_td;
904 struct nand_bbt_descr *md = this->bbt_md;
905 struct nand_bbt_descr *rd, *rd2;
907 /* Do we have a bbt per chip ? */
908 if (td->options & NAND_BBT_PERCHIP)
909 chips = this->numchips;
913 for (i = 0; i < chips; i++) {
917 /* Per chip or per device ? */
918 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
919 /* Mirrored table available ? */
921 if (td->pages[i] == -1 && md->pages[i] == -1) {
926 if (td->pages[i] == -1) {
928 td->version[i] = md->version[i];
933 if (md->pages[i] == -1) {
935 md->version[i] = td->version[i];
940 if (td->version[i] == md->version[i]) {
942 if (!(td->options & NAND_BBT_VERSION))
947 if (((int8_t) (td->version[i] - md->version[i])) > 0) {
949 md->version[i] = td->version[i];
953 td->version[i] = md->version[i];
960 if (td->pages[i] == -1) {
968 /* Create the bad block table by scanning the device ? */
969 if (!(td->options & NAND_BBT_CREATE))
972 /* Create the table in memory by scanning the chip(s) */
973 if (!(this->bbt_options & NAND_CREATE_EMPTY_BBT))
974 create_bbt(mtd, buf, bd, chipsel);
980 /* read back first ? */
982 read_abs_bbt(mtd, buf, rd, chipsel);
983 /* If they weren't versioned, read both. */
985 read_abs_bbt(mtd, buf, rd2, chipsel);
987 /* Write the bad block table to the device ? */
988 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
989 res = write_bbt(mtd, buf, td, md, chipsel);
994 /* Write the mirror bad block table to the device ? */
995 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
996 res = write_bbt(mtd, buf, md, td, chipsel);
1005 * mark_bbt_regions - [GENERIC] mark the bad block table regions
1006 * @mtd: MTD device structure
1007 * @td: bad block table descriptor
1009 * The bad block table regions are marked as "bad" to prevent
1010 * accidental erasures / writes. The regions are identified by
1013 static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
1015 struct nand_chip *this = mtd->priv;
1016 int i, j, chips, block, nrblocks, update;
1017 uint8_t oldval, newval;
1019 /* Do we have a bbt per chip ? */
1020 if (td->options & NAND_BBT_PERCHIP) {
1021 chips = this->numchips;
1022 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
1025 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
1028 for (i = 0; i < chips; i++) {
1029 if ((td->options & NAND_BBT_ABSPAGE) ||
1030 !(td->options & NAND_BBT_WRITE)) {
1031 if (td->pages[i] == -1)
1033 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
1035 oldval = this->bbt[(block >> 3)];
1036 newval = oldval | (0x2 << (block & 0x06));
1037 this->bbt[(block >> 3)] = newval;
1038 if ((oldval != newval) && td->reserved_block_code)
1039 nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
1043 if (td->options & NAND_BBT_LASTBLOCK)
1044 block = ((i + 1) * nrblocks) - td->maxblocks;
1046 block = i * nrblocks;
1048 for (j = 0; j < td->maxblocks; j++) {
1049 oldval = this->bbt[(block >> 3)];
1050 newval = oldval | (0x2 << (block & 0x06));
1051 this->bbt[(block >> 3)] = newval;
1052 if (oldval != newval)
1056 /* If we want reserved blocks to be recorded to flash, and some
1057 new ones have been marked, then we need to update the stored
1058 bbts. This should only happen once. */
1059 if (update && td->reserved_block_code)
1060 nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
1065 * verify_bbt_descr - verify the bad block description
1066 * @mtd: MTD device structure
1067 * @bd: the table to verify
1069 * This functions performs a few sanity checks on the bad block description
1072 static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1074 struct nand_chip *this = mtd->priv;
1082 pattern_len = bd->len;
1083 bits = bd->options & NAND_BBT_NRBITS_MSK;
1085 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
1086 !(this->bbt_options & NAND_BBT_USE_FLASH));
1089 if (bd->options & NAND_BBT_VERSION)
1092 if (bd->options & NAND_BBT_NO_OOB) {
1093 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
1094 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
1096 if (bd->options & NAND_BBT_VERSION)
1097 BUG_ON(bd->veroffs != bd->len);
1098 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1101 if (bd->options & NAND_BBT_PERCHIP)
1102 table_size = this->chipsize >> this->bbt_erase_shift;
1104 table_size = mtd->size >> this->bbt_erase_shift;
1107 if (bd->options & NAND_BBT_NO_OOB)
1108 table_size += pattern_len;
1109 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1113 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1114 * @mtd: MTD device structure
1115 * @bd: descriptor for the good/bad block search pattern
1117 * The function checks, if a bad block table(s) is/are already
1118 * available. If not it scans the device for manufacturer
1119 * marked good / bad blocks and writes the bad block table(s) to
1120 * the selected place.
1122 * The bad block table memory is allocated here. It must be freed
1123 * by calling the nand_free_bbt function.
1126 int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1128 struct nand_chip *this = mtd->priv;
1131 struct nand_bbt_descr *td = this->bbt_td;
1132 struct nand_bbt_descr *md = this->bbt_md;
1134 len = mtd->size >> (this->bbt_erase_shift + 2);
1135 /* Allocate memory (2bit per block) and clear the memory bad block table */
1136 this->bbt = kzalloc(len, GFP_KERNEL);
1138 printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
1142 /* If no primary table decriptor is given, scan the device
1143 * to build a memory based bad block table
1146 if ((res = nand_memory_bbt(mtd, bd))) {
1147 printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
1153 verify_bbt_descr(mtd, td);
1154 verify_bbt_descr(mtd, md);
1156 /* Allocate a temporary buffer for one eraseblock incl. oob */
1157 len = (1 << this->bbt_erase_shift);
1158 len += (len >> this->page_shift) * mtd->oobsize;
1161 printk(KERN_ERR "nand_bbt: Out of memory\n");
1167 /* Is the bbt at a given page ? */
1168 if (td->options & NAND_BBT_ABSPAGE) {
1169 res = read_abs_bbts(mtd, buf, td, md);
1171 /* Search the bad block table using a pattern in oob */
1172 res = search_read_bbts(mtd, buf, td, md);
1176 res = check_create(mtd, buf, bd);
1178 /* Prevent the bbt regions from erasing / writing */
1179 mark_bbt_region(mtd, td);
1181 mark_bbt_region(mtd, md);
1188 * nand_update_bbt - [NAND Interface] update bad block table(s)
1189 * @mtd: MTD device structure
1190 * @offs: the offset of the newly marked block
1192 * The function updates the bad block table(s)
1194 int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
1196 struct nand_chip *this = mtd->priv;
1197 int len, res = 0, writeops = 0;
1200 struct nand_bbt_descr *td = this->bbt_td;
1201 struct nand_bbt_descr *md = this->bbt_md;
1203 if (!this->bbt || !td)
1206 /* Allocate a temporary buffer for one eraseblock incl. oob */
1207 len = (1 << this->bbt_erase_shift);
1208 len += (len >> this->page_shift) * mtd->oobsize;
1209 buf = kmalloc(len, GFP_KERNEL);
1211 printk(KERN_ERR "nand_update_bbt: Out of memory\n");
1215 writeops = md != NULL ? 0x03 : 0x01;
1217 /* Do we have a bbt per chip ? */
1218 if (td->options & NAND_BBT_PERCHIP) {
1219 chip = (int)(offs >> this->chip_shift);
1226 td->version[chip]++;
1228 md->version[chip]++;
1230 /* Write the bad block table to the device ? */
1231 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1232 res = write_bbt(mtd, buf, td, md, chipsel);
1236 /* Write the mirror bad block table to the device ? */
1237 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1238 res = write_bbt(mtd, buf, md, td, chipsel);
1246 /* Define some generic bad / good block scan pattern which are used
1247 * while scanning a device for factory marked good / bad blocks. */
1248 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1250 static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1252 static struct nand_bbt_descr agand_flashbased = {
1253 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1256 .pattern = scan_agand_pattern
1259 /* Generic flash bbt decriptors
1261 static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1262 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1264 static struct nand_bbt_descr bbt_main_descr = {
1265 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1266 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1271 .pattern = bbt_pattern
1274 static struct nand_bbt_descr bbt_mirror_descr = {
1275 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1276 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1281 .pattern = mirror_pattern
1284 static struct nand_bbt_descr bbt_main_no_bbt_descr = {
1285 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1286 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1291 .pattern = bbt_pattern
1294 static struct nand_bbt_descr bbt_mirror_no_bbt_descr = {
1295 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1296 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1301 .pattern = mirror_pattern
1305 * nand_create_default_bbt_descr - [Internal] Creates a BBT descriptor structure
1306 * @this: NAND chip to create descriptor for
1308 * This function allocates and initializes a nand_bbt_descr for BBM detection
1309 * based on the properties of "this". The new descriptor is stored in
1310 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1311 * passed to this function.
1314 static int nand_create_default_bbt_descr(struct nand_chip *this)
1316 struct nand_bbt_descr *bd;
1317 if (this->badblock_pattern) {
1318 printk(KERN_WARNING "BBT descr already allocated; not replacing.\n");
1321 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1323 printk(KERN_ERR "nand_create_default_bbt_descr: Out of memory\n");
1326 bd->options = this->bbt_options;
1327 bd->offs = this->badblockpos;
1328 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1329 bd->pattern = scan_ff_pattern;
1330 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1331 this->badblock_pattern = bd;
1336 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
1337 * @mtd: MTD device structure
1339 * This function selects the default bad block table
1340 * support for the device and calls the nand_scan_bbt function
1343 int nand_default_bbt(struct mtd_info *mtd)
1345 struct nand_chip *this = mtd->priv;
1347 /* Default for AG-AND. We must use a flash based
1348 * bad block table as the devices have factory marked
1349 * _good_ blocks. Erasing those blocks leads to loss
1350 * of the good / bad information, so we _must_ store
1351 * this information in a good / bad table during
1354 if (this->options & NAND_IS_AND) {
1355 /* Use the default pattern descriptors */
1356 if (!this->bbt_td) {
1357 this->bbt_td = &bbt_main_descr;
1358 this->bbt_md = &bbt_mirror_descr;
1360 this->bbt_options |= NAND_BBT_USE_FLASH;
1361 return nand_scan_bbt(mtd, &agand_flashbased);
1364 /* Is a flash based bad block table requested ? */
1365 if (this->bbt_options & NAND_BBT_USE_FLASH) {
1366 /* Use the default pattern descriptors */
1367 if (!this->bbt_td) {
1368 if (this->bbt_options & NAND_BBT_NO_OOB) {
1369 this->bbt_td = &bbt_main_no_bbt_descr;
1370 this->bbt_md = &bbt_mirror_no_bbt_descr;
1372 this->bbt_td = &bbt_main_descr;
1373 this->bbt_md = &bbt_mirror_descr;
1377 this->bbt_td = NULL;
1378 this->bbt_md = NULL;
1381 if (!this->badblock_pattern)
1382 nand_create_default_bbt_descr(this);
1384 return nand_scan_bbt(mtd, this->badblock_pattern);
1388 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1389 * @mtd: MTD device structure
1390 * @offs: offset in the device
1391 * @allowbbt: allow access to bad block table region
1394 int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
1396 struct nand_chip *this = mtd->priv;
1400 /* Get block number * 2 */
1401 block = (int)(offs >> (this->bbt_erase_shift - 1));
1402 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1404 DEBUG(MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1405 (unsigned int)offs, block >> 1, res);
1413 return allowbbt ? 0 : 1;
1418 EXPORT_SYMBOL(nand_scan_bbt);
1419 EXPORT_SYMBOL(nand_default_bbt);