1491 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2/*3 * Overview:4 * Bad block table support for the NAND driver5 *6 * Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)7 *8 * Description:9 *10 * When nand_scan_bbt is called, then it tries to find the bad block table11 * depending on the options in the BBT descriptor(s). If no flash based BBT12 * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory13 * marked good / bad blocks. This information is used to create a memory BBT.14 * Once a new bad block is discovered then the "factory" information is updated15 * on the device.16 * If a flash based BBT is specified then the function first tries to find the17 * BBT on flash. If a BBT is found then the contents are read and the memory18 * based BBT is created. If a mirrored BBT is selected then the mirror is19 * searched too and the versions are compared. If the mirror has a greater20 * version number, then the mirror BBT is used to build the memory based BBT.21 * If the tables are not versioned, then we "or" the bad block information.22 * If one of the BBTs is out of date or does not exist it is (re)created.23 * If no BBT exists at all then the device is scanned for factory marked24 * good / bad blocks and the bad block tables are created.25 *26 * For manufacturer created BBTs like the one found on M-SYS DOC devices27 * the BBT is searched and read but never created28 *29 * The auto generated bad block table is located in the last good blocks30 * of the device. The table is mirrored, so it can be updated eventually.31 * The table is marked in the OOB area with an ident pattern and a version32 * number which indicates which of both tables is more up to date. If the NAND33 * controller needs the complete OOB area for the ECC information then the34 * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of35 * course): it moves the ident pattern and the version byte into the data area36 * and the OOB area will remain untouched.37 *38 * The table uses 2 bits per block39 * 11b: block is good40 * 00b: block is factory marked bad41 * 01b, 10b: block is marked bad due to wear42 *43 * The memory bad block table uses the following scheme:44 * 00b: block is good45 * 01b: block is marked bad due to wear46 * 10b: block is reserved (to protect the bbt area)47 * 11b: block is factory marked bad48 *49 * Multichip devices like DOC store the bad block info per floor.50 *51 * Following assumptions are made:52 * - bbts start at a page boundary, if autolocated on a block boundary53 * - the space necessary for a bbt in FLASH does not exceed a block boundary54 */55 56#include <linux/slab.h>57#include <linux/types.h>58#include <linux/mtd/mtd.h>59#include <linux/mtd/bbm.h>60#include <linux/bitops.h>61#include <linux/delay.h>62#include <linux/vmalloc.h>63#include <linux/export.h>64#include <linux/string.h>65 66#include "internals.h"67 68#define BBT_BLOCK_GOOD 0x0069#define BBT_BLOCK_WORN 0x0170#define BBT_BLOCK_RESERVED 0x0271#define BBT_BLOCK_FACTORY_BAD 0x0372 73#define BBT_ENTRY_MASK 0x0374#define BBT_ENTRY_SHIFT 275 76static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)77{78 uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];79 entry >>= (block & BBT_ENTRY_MASK) * 2;80 return entry & BBT_ENTRY_MASK;81}82 83static inline void bbt_mark_entry(struct nand_chip *chip, int block,84 uint8_t mark)85{86 uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);87 chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;88}89 90static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)91{92 if (memcmp(buf, td->pattern, td->len))93 return -1;94 return 0;95}96 97/**98 * check_pattern - [GENERIC] check if a pattern is in the buffer99 * @buf: the buffer to search100 * @len: the length of buffer to search101 * @paglen: the pagelength102 * @td: search pattern descriptor103 *104 * Check for a pattern at the given place. Used to search bad block tables and105 * good / bad block identifiers.106 */107static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)108{109 if (td->options & NAND_BBT_NO_OOB)110 return check_pattern_no_oob(buf, td);111 112 /* Compare the pattern */113 if (memcmp(buf + paglen + td->offs, td->pattern, td->len))114 return -1;115 116 return 0;117}118 119/**120 * check_short_pattern - [GENERIC] check if a pattern is in the buffer121 * @buf: the buffer to search122 * @td: search pattern descriptor123 *124 * Check for a pattern at the given place. Used to search bad block tables and125 * good / bad block identifiers. Same as check_pattern, but no optional empty126 * check.127 */128static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)129{130 /* Compare the pattern */131 if (memcmp(buf + td->offs, td->pattern, td->len))132 return -1;133 return 0;134}135 136/**137 * add_marker_len - compute the length of the marker in data area138 * @td: BBT descriptor used for computation139 *140 * The length will be 0 if the marker is located in OOB area.141 */142static u32 add_marker_len(struct nand_bbt_descr *td)143{144 u32 len;145 146 if (!(td->options & NAND_BBT_NO_OOB))147 return 0;148 149 len = td->len;150 if (td->options & NAND_BBT_VERSION)151 len++;152 return len;153}154 155/**156 * read_bbt - [GENERIC] Read the bad block table starting from page157 * @this: NAND chip object158 * @buf: temporary buffer159 * @page: the starting page160 * @num: the number of bbt descriptors to read161 * @td: the bbt describtion table162 * @offs: block number offset in the table163 *164 * Read the bad block table starting from page.165 */166static int read_bbt(struct nand_chip *this, uint8_t *buf, int page, int num,167 struct nand_bbt_descr *td, int offs)168{169 struct mtd_info *mtd = nand_to_mtd(this);170 int res, ret = 0, i, j, act = 0;171 size_t retlen, len, totlen;172 loff_t from;173 int bits = td->options & NAND_BBT_NRBITS_MSK;174 uint8_t msk = (uint8_t)((1 << bits) - 1);175 u32 marker_len;176 int reserved_block_code = td->reserved_block_code;177 178 totlen = (num * bits) >> 3;179 marker_len = add_marker_len(td);180 from = ((loff_t)page) << this->page_shift;181 182 while (totlen) {183 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));184 if (marker_len) {185 /*186 * In case the BBT marker is not in the OOB area it187 * will be just in the first page.188 */189 len -= marker_len;190 from += marker_len;191 marker_len = 0;192 }193 res = mtd_read(mtd, from, len, &retlen, buf);194 if (res < 0) {195 if (mtd_is_eccerr(res)) {196 pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",197 from & ~mtd->writesize);198 return res;199 } else if (mtd_is_bitflip(res)) {200 pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",201 from & ~mtd->writesize);202 ret = res;203 } else {204 pr_info("nand_bbt: error reading BBT\n");205 return res;206 }207 }208 209 /* Analyse data */210 for (i = 0; i < len; i++) {211 uint8_t dat = buf[i];212 for (j = 0; j < 8; j += bits, act++) {213 uint8_t tmp = (dat >> j) & msk;214 if (tmp == msk)215 continue;216 if (reserved_block_code && (tmp == reserved_block_code)) {217 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",218 (loff_t)(offs + act) <<219 this->bbt_erase_shift);220 bbt_mark_entry(this, offs + act,221 BBT_BLOCK_RESERVED);222 mtd->ecc_stats.bbtblocks++;223 continue;224 }225 /*226 * Leave it for now, if it's matured we can227 * move this message to pr_debug.228 */229 pr_info("nand_read_bbt: bad block at 0x%012llx\n",230 (loff_t)(offs + act) <<231 this->bbt_erase_shift);232 /* Factory marked bad or worn out? */233 if (tmp == 0)234 bbt_mark_entry(this, offs + act,235 BBT_BLOCK_FACTORY_BAD);236 else237 bbt_mark_entry(this, offs + act,238 BBT_BLOCK_WORN);239 mtd->ecc_stats.badblocks++;240 }241 }242 totlen -= len;243 from += len;244 }245 return ret;246}247 248/**249 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page250 * @this: NAND chip object251 * @buf: temporary buffer252 * @td: descriptor for the bad block table253 * @chip: read the table for a specific chip, -1 read all chips; applies only if254 * NAND_BBT_PERCHIP option is set255 *256 * Read the bad block table for all chips starting at a given page. We assume257 * that the bbt bits are in consecutive order.258 */259static int read_abs_bbt(struct nand_chip *this, uint8_t *buf,260 struct nand_bbt_descr *td, int chip)261{262 struct mtd_info *mtd = nand_to_mtd(this);263 u64 targetsize = nanddev_target_size(&this->base);264 int res = 0, i;265 266 if (td->options & NAND_BBT_PERCHIP) {267 int offs = 0;268 for (i = 0; i < nanddev_ntargets(&this->base); i++) {269 if (chip == -1 || chip == i)270 res = read_bbt(this, buf, td->pages[i],271 targetsize >> this->bbt_erase_shift,272 td, offs);273 if (res)274 return res;275 offs += targetsize >> this->bbt_erase_shift;276 }277 } else {278 res = read_bbt(this, buf, td->pages[0],279 mtd->size >> this->bbt_erase_shift, td, 0);280 if (res)281 return res;282 }283 return 0;284}285 286/* BBT marker is in the first page, no OOB */287static int scan_read_data(struct nand_chip *this, uint8_t *buf, loff_t offs,288 struct nand_bbt_descr *td)289{290 struct mtd_info *mtd = nand_to_mtd(this);291 size_t retlen;292 size_t len;293 294 len = td->len;295 if (td->options & NAND_BBT_VERSION)296 len++;297 298 return mtd_read(mtd, offs, len, &retlen, buf);299}300 301/**302 * scan_read_oob - [GENERIC] Scan data+OOB region to buffer303 * @this: NAND chip object304 * @buf: temporary buffer305 * @offs: offset at which to scan306 * @len: length of data region to read307 *308 * Scan read data from data+OOB. May traverse multiple pages, interleaving309 * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"310 * ECC condition (error or bitflip). May quit on the first (non-ECC) error.311 */312static int scan_read_oob(struct nand_chip *this, uint8_t *buf, loff_t offs,313 size_t len)314{315 struct mtd_info *mtd = nand_to_mtd(this);316 struct mtd_oob_ops ops = { };317 int res, ret = 0;318 319 ops.mode = MTD_OPS_PLACE_OOB;320 ops.ooboffs = 0;321 ops.ooblen = mtd->oobsize;322 323 while (len > 0) {324 ops.datbuf = buf;325 ops.len = min(len, (size_t)mtd->writesize);326 ops.oobbuf = buf + ops.len;327 328 res = mtd_read_oob(mtd, offs, &ops);329 if (res) {330 if (!mtd_is_bitflip_or_eccerr(res))331 return res;332 else if (mtd_is_eccerr(res) || !ret)333 ret = res;334 }335 336 buf += mtd->oobsize + mtd->writesize;337 len -= mtd->writesize;338 offs += mtd->writesize;339 }340 return ret;341}342 343static int scan_read(struct nand_chip *this, uint8_t *buf, loff_t offs,344 size_t len, struct nand_bbt_descr *td)345{346 if (td->options & NAND_BBT_NO_OOB)347 return scan_read_data(this, buf, offs, td);348 else349 return scan_read_oob(this, buf, offs, len);350}351 352/* Scan write data with oob to flash */353static int scan_write_bbt(struct nand_chip *this, loff_t offs, size_t len,354 uint8_t *buf, uint8_t *oob)355{356 struct mtd_info *mtd = nand_to_mtd(this);357 struct mtd_oob_ops ops = { };358 359 ops.mode = MTD_OPS_PLACE_OOB;360 ops.ooboffs = 0;361 ops.ooblen = mtd->oobsize;362 ops.datbuf = buf;363 ops.oobbuf = oob;364 ops.len = len;365 366 return mtd_write_oob(mtd, offs, &ops);367}368 369static u32 bbt_get_ver_offs(struct nand_chip *this, struct nand_bbt_descr *td)370{371 struct mtd_info *mtd = nand_to_mtd(this);372 u32 ver_offs = td->veroffs;373 374 if (!(td->options & NAND_BBT_NO_OOB))375 ver_offs += mtd->writesize;376 return ver_offs;377}378 379/**380 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page381 * @this: NAND chip object382 * @buf: temporary buffer383 * @td: descriptor for the bad block table384 * @md: descriptor for the bad block table mirror385 *386 * Read the bad block table(s) for all chips starting at a given page. We387 * assume that the bbt bits are in consecutive order.388 */389static void read_abs_bbts(struct nand_chip *this, uint8_t *buf,390 struct nand_bbt_descr *td, struct nand_bbt_descr *md)391{392 struct mtd_info *mtd = nand_to_mtd(this);393 394 /* Read the primary version, if available */395 if (td->options & NAND_BBT_VERSION) {396 scan_read(this, buf, (loff_t)td->pages[0] << this->page_shift,397 mtd->writesize, td);398 td->version[0] = buf[bbt_get_ver_offs(this, td)];399 pr_info("Bad block table at page %d, version 0x%02X\n",400 td->pages[0], td->version[0]);401 }402 403 /* Read the mirror version, if available */404 if (md && (md->options & NAND_BBT_VERSION)) {405 scan_read(this, buf, (loff_t)md->pages[0] << this->page_shift,406 mtd->writesize, md);407 md->version[0] = buf[bbt_get_ver_offs(this, md)];408 pr_info("Bad block table at page %d, version 0x%02X\n",409 md->pages[0], md->version[0]);410 }411}412 413/* Scan a given block partially */414static int scan_block_fast(struct nand_chip *this, struct nand_bbt_descr *bd,415 loff_t offs, uint8_t *buf)416{417 struct mtd_info *mtd = nand_to_mtd(this);418 419 struct mtd_oob_ops ops = { };420 int ret, page_offset;421 422 ops.ooblen = mtd->oobsize;423 ops.oobbuf = buf;424 ops.ooboffs = 0;425 ops.datbuf = NULL;426 ops.mode = MTD_OPS_PLACE_OOB;427 428 page_offset = nand_bbm_get_next_page(this, 0);429 430 while (page_offset >= 0) {431 /*432 * Read the full oob until read_oob is fixed to handle single433 * byte reads for 16 bit buswidth.434 */435 ret = mtd_read_oob(mtd, offs + (page_offset * mtd->writesize),436 &ops);437 /* Ignore ECC errors when checking for BBM */438 if (ret && !mtd_is_bitflip_or_eccerr(ret))439 return ret;440 441 if (check_short_pattern(buf, bd))442 return 1;443 444 page_offset = nand_bbm_get_next_page(this, page_offset + 1);445 }446 447 return 0;448}449 450/* Check if a potential BBT block is marked as bad */451static int bbt_block_checkbad(struct nand_chip *this, struct nand_bbt_descr *td,452 loff_t offs, uint8_t *buf)453{454 struct nand_bbt_descr *bd = this->badblock_pattern;455 456 /*457 * No need to check for a bad BBT block if the BBM area overlaps with458 * the bad block table marker area in OOB since writing a BBM here459 * invalidates the bad block table marker anyway.460 */461 if (!(td->options & NAND_BBT_NO_OOB) &&462 td->offs >= bd->offs && td->offs < bd->offs + bd->len)463 return 0;464 465 /*466 * There is no point in checking for a bad block marker if writing467 * such marker is not supported468 */469 if (this->bbt_options & NAND_BBT_NO_OOB_BBM ||470 this->options & NAND_NO_BBM_QUIRK)471 return 0;472 473 if (scan_block_fast(this, bd, offs, buf) > 0)474 return 1;475 476 return 0;477}478 479/**480 * create_bbt - [GENERIC] Create a bad block table by scanning the device481 * @this: NAND chip object482 * @buf: temporary buffer483 * @bd: descriptor for the good/bad block search pattern484 * @chip: create the table for a specific chip, -1 read all chips; applies only485 * if NAND_BBT_PERCHIP option is set486 *487 * Create a bad block table by scanning the device for the given good/bad block488 * identify pattern.489 */490static int create_bbt(struct nand_chip *this, uint8_t *buf,491 struct nand_bbt_descr *bd, int chip)492{493 u64 targetsize = nanddev_target_size(&this->base);494 struct mtd_info *mtd = nand_to_mtd(this);495 int i, numblocks, startblock;496 loff_t from;497 498 pr_info("Scanning device for bad blocks\n");499 500 if (chip == -1) {501 numblocks = mtd->size >> this->bbt_erase_shift;502 startblock = 0;503 from = 0;504 } else {505 if (chip >= nanddev_ntargets(&this->base)) {506 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",507 chip + 1, nanddev_ntargets(&this->base));508 return -EINVAL;509 }510 numblocks = targetsize >> this->bbt_erase_shift;511 startblock = chip * numblocks;512 numblocks += startblock;513 from = (loff_t)startblock << this->bbt_erase_shift;514 }515 516 for (i = startblock; i < numblocks; i++) {517 int ret;518 519 BUG_ON(bd->options & NAND_BBT_NO_OOB);520 521 ret = scan_block_fast(this, bd, from, buf);522 if (ret < 0)523 return ret;524 525 if (ret) {526 bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);527 pr_warn("Bad eraseblock %d at 0x%012llx\n",528 i, (unsigned long long)from);529 mtd->ecc_stats.badblocks++;530 }531 532 from += (1 << this->bbt_erase_shift);533 }534 return 0;535}536 537/**538 * search_bbt - [GENERIC] scan the device for a specific bad block table539 * @this: NAND chip object540 * @buf: temporary buffer541 * @td: descriptor for the bad block table542 *543 * Read the bad block table by searching for a given ident pattern. Search is544 * preformed either from the beginning up or from the end of the device545 * downwards. The search starts always at the start of a block. If the option546 * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains547 * the bad block information of this chip. This is necessary to provide support548 * for certain DOC devices.549 *550 * The bbt ident pattern resides in the oob area of the first page in a block.551 */552static int search_bbt(struct nand_chip *this, uint8_t *buf,553 struct nand_bbt_descr *td)554{555 u64 targetsize = nanddev_target_size(&this->base);556 struct mtd_info *mtd = nand_to_mtd(this);557 int i, chips;558 int startblock, block, dir;559 int scanlen = mtd->writesize + mtd->oobsize;560 int bbtblocks;561 int blocktopage = this->bbt_erase_shift - this->page_shift;562 563 /* Search direction top -> down? */564 if (td->options & NAND_BBT_LASTBLOCK) {565 startblock = (mtd->size >> this->bbt_erase_shift) - 1;566 dir = -1;567 } else {568 startblock = 0;569 dir = 1;570 }571 572 /* Do we have a bbt per chip? */573 if (td->options & NAND_BBT_PERCHIP) {574 chips = nanddev_ntargets(&this->base);575 bbtblocks = targetsize >> this->bbt_erase_shift;576 startblock &= bbtblocks - 1;577 } else {578 chips = 1;579 }580 581 for (i = 0; i < chips; i++) {582 /* Reset version information */583 td->version[i] = 0;584 td->pages[i] = -1;585 /* Scan the maximum number of blocks */586 for (block = 0; block < td->maxblocks; block++) {587 588 int actblock = startblock + dir * block;589 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;590 591 /* Check if block is marked bad */592 if (bbt_block_checkbad(this, td, offs, buf))593 continue;594 595 /* Read first page */596 scan_read(this, buf, offs, mtd->writesize, td);597 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {598 td->pages[i] = actblock << blocktopage;599 if (td->options & NAND_BBT_VERSION) {600 offs = bbt_get_ver_offs(this, td);601 td->version[i] = buf[offs];602 }603 break;604 }605 }606 startblock += targetsize >> this->bbt_erase_shift;607 }608 /* Check, if we found a bbt for each requested chip */609 for (i = 0; i < chips; i++) {610 if (td->pages[i] == -1)611 pr_warn("Bad block table not found for chip %d\n", i);612 else613 pr_info("Bad block table found at page %d, version 0x%02X\n",614 td->pages[i], td->version[i]);615 }616 return 0;617}618 619/**620 * search_read_bbts - [GENERIC] scan the device for bad block table(s)621 * @this: NAND chip object622 * @buf: temporary buffer623 * @td: descriptor for the bad block table624 * @md: descriptor for the bad block table mirror625 *626 * Search and read the bad block table(s).627 */628static void search_read_bbts(struct nand_chip *this, uint8_t *buf,629 struct nand_bbt_descr *td,630 struct nand_bbt_descr *md)631{632 /* Search the primary table */633 search_bbt(this, buf, td);634 635 /* Search the mirror table */636 if (md)637 search_bbt(this, buf, md);638}639 640/**641 * get_bbt_block - Get the first valid eraseblock suitable to store a BBT642 * @this: the NAND device643 * @td: the BBT description644 * @md: the mirror BBT descriptor645 * @chip: the CHIP selector646 *647 * This functions returns a positive block number pointing a valid eraseblock648 * suitable to store a BBT (i.e. in the range reserved for BBT), or -ENOSPC if649 * all blocks are already used of marked bad. If td->pages[chip] was already650 * pointing to a valid block we re-use it, otherwise we search for the next651 * valid one.652 */653static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td,654 struct nand_bbt_descr *md, int chip)655{656 u64 targetsize = nanddev_target_size(&this->base);657 int startblock, dir, page, numblocks, i;658 659 /*660 * There was already a version of the table, reuse the page. This661 * applies for absolute placement too, as we have the page number in662 * td->pages.663 */664 if (td->pages[chip] != -1)665 return td->pages[chip] >>666 (this->bbt_erase_shift - this->page_shift);667 668 numblocks = (int)(targetsize >> this->bbt_erase_shift);669 if (!(td->options & NAND_BBT_PERCHIP))670 numblocks *= nanddev_ntargets(&this->base);671 672 /*673 * Automatic placement of the bad block table. Search direction674 * top -> down?675 */676 if (td->options & NAND_BBT_LASTBLOCK) {677 startblock = numblocks * (chip + 1) - 1;678 dir = -1;679 } else {680 startblock = chip * numblocks;681 dir = 1;682 }683 684 for (i = 0; i < td->maxblocks; i++) {685 int block = startblock + dir * i;686 687 /* Check, if the block is bad */688 switch (bbt_get_entry(this, block)) {689 case BBT_BLOCK_WORN:690 case BBT_BLOCK_FACTORY_BAD:691 continue;692 }693 694 page = block << (this->bbt_erase_shift - this->page_shift);695 696 /* Check, if the block is used by the mirror table */697 if (!md || md->pages[chip] != page)698 return block;699 }700 701 return -ENOSPC;702}703 704/**705 * mark_bbt_block_bad - Mark one of the block reserved for BBT bad706 * @this: the NAND device707 * @td: the BBT description708 * @chip: the CHIP selector709 * @block: the BBT block to mark710 *711 * Blocks reserved for BBT can become bad. This functions is an helper to mark712 * such blocks as bad. It takes care of updating the in-memory BBT, marking the713 * block as bad using a bad block marker and invalidating the associated714 * td->pages[] entry.715 */716static void mark_bbt_block_bad(struct nand_chip *this,717 struct nand_bbt_descr *td,718 int chip, int block)719{720 loff_t to;721 int res;722 723 bbt_mark_entry(this, block, BBT_BLOCK_WORN);724 725 to = (loff_t)block << this->bbt_erase_shift;726 res = nand_markbad_bbm(this, to);727 if (res)728 pr_warn("nand_bbt: error %d while marking block %d bad\n",729 res, block);730 731 td->pages[chip] = -1;732}733 734/**735 * write_bbt - [GENERIC] (Re)write the bad block table736 * @this: NAND chip object737 * @buf: temporary buffer738 * @td: descriptor for the bad block table739 * @md: descriptor for the bad block table mirror740 * @chipsel: selector for a specific chip, -1 for all741 *742 * (Re)write the bad block table.743 */744static int write_bbt(struct nand_chip *this, uint8_t *buf,745 struct nand_bbt_descr *td, struct nand_bbt_descr *md,746 int chipsel)747{748 u64 targetsize = nanddev_target_size(&this->base);749 struct mtd_info *mtd = nand_to_mtd(this);750 struct erase_info einfo;751 int i, res, chip = 0;752 int bits, page, offs, numblocks, sft, sftmsk;753 int nrchips, pageoffs, ooboffs;754 uint8_t msk[4];755 uint8_t rcode = td->reserved_block_code;756 size_t retlen, len = 0;757 loff_t to;758 struct mtd_oob_ops ops = { };759 760 ops.ooblen = mtd->oobsize;761 ops.ooboffs = 0;762 ops.datbuf = NULL;763 ops.mode = MTD_OPS_PLACE_OOB;764 765 if (!rcode)766 rcode = 0xff;767 /* Write bad block table per chip rather than per device? */768 if (td->options & NAND_BBT_PERCHIP) {769 numblocks = (int)(targetsize >> this->bbt_erase_shift);770 /* Full device write or specific chip? */771 if (chipsel == -1) {772 nrchips = nanddev_ntargets(&this->base);773 } else {774 nrchips = chipsel + 1;775 chip = chipsel;776 }777 } else {778 numblocks = (int)(mtd->size >> this->bbt_erase_shift);779 nrchips = 1;780 }781 782 /* Loop through the chips */783 while (chip < nrchips) {784 int block;785 786 block = get_bbt_block(this, td, md, chip);787 if (block < 0) {788 pr_err("No space left to write bad block table\n");789 res = block;790 goto outerr;791 }792 793 /*794 * get_bbt_block() returns a block number, shift the value to795 * get a page number.796 */797 page = block << (this->bbt_erase_shift - this->page_shift);798 799 /* Set up shift count and masks for the flash table */800 bits = td->options & NAND_BBT_NRBITS_MSK;801 msk[2] = ~rcode;802 switch (bits) {803 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;804 msk[3] = 0x01;805 break;806 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;807 msk[3] = 0x03;808 break;809 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;810 msk[3] = 0x0f;811 break;812 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;813 msk[3] = 0xff;814 break;815 default: return -EINVAL;816 }817 818 to = ((loff_t)page) << this->page_shift;819 820 /* Must we save the block contents? */821 if (td->options & NAND_BBT_SAVECONTENT) {822 /* Make it block aligned */823 to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);824 len = 1 << this->bbt_erase_shift;825 res = mtd_read(mtd, to, len, &retlen, buf);826 if (res < 0) {827 if (retlen != len) {828 pr_info("nand_bbt: error reading block for writing the bad block table\n");829 return res;830 }831 pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");832 }833 /* Read oob data */834 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;835 ops.oobbuf = &buf[len];836 res = mtd_read_oob(mtd, to + mtd->writesize, &ops);837 if (res < 0 || ops.oobretlen != ops.ooblen)838 goto outerr;839 840 /* Calc the byte offset in the buffer */841 pageoffs = page - (int)(to >> this->page_shift);842 offs = pageoffs << this->page_shift;843 /* Preset the bbt area with 0xff */844 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));845 ooboffs = len + (pageoffs * mtd->oobsize);846 847 } else if (td->options & NAND_BBT_NO_OOB) {848 ooboffs = 0;849 offs = td->len;850 /* The version byte */851 if (td->options & NAND_BBT_VERSION)852 offs++;853 /* Calc length */854 len = (size_t)(numblocks >> sft);855 len += offs;856 /* Make it page aligned! */857 len = ALIGN(len, mtd->writesize);858 /* Preset the buffer with 0xff */859 memset(buf, 0xff, len);860 /* Pattern is located at the begin of first page */861 memcpy(buf, td->pattern, td->len);862 } else {863 /* Calc length */864 len = (size_t)(numblocks >> sft);865 /* Make it page aligned! */866 len = ALIGN(len, mtd->writesize);867 /* Preset the buffer with 0xff */868 memset(buf, 0xff, len +869 (len >> this->page_shift)* mtd->oobsize);870 offs = 0;871 ooboffs = len;872 /* Pattern is located in oob area of first page */873 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);874 }875 876 if (td->options & NAND_BBT_VERSION)877 buf[ooboffs + td->veroffs] = td->version[chip];878 879 /* Walk through the memory table */880 for (i = 0; i < numblocks; i++) {881 uint8_t dat;882 int sftcnt = (i << (3 - sft)) & sftmsk;883 dat = bbt_get_entry(this, chip * numblocks + i);884 /* Do not store the reserved bbt blocks! */885 buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);886 }887 888 memset(&einfo, 0, sizeof(einfo));889 einfo.addr = to;890 einfo.len = 1 << this->bbt_erase_shift;891 res = nand_erase_nand(this, &einfo, 1);892 if (res < 0) {893 pr_warn("nand_bbt: error while erasing BBT block %d\n",894 res);895 mark_bbt_block_bad(this, td, chip, block);896 continue;897 }898 899 res = scan_write_bbt(this, to, len, buf,900 td->options & NAND_BBT_NO_OOB ?901 NULL : &buf[len]);902 if (res < 0) {903 pr_warn("nand_bbt: error while writing BBT block %d\n",904 res);905 mark_bbt_block_bad(this, td, chip, block);906 continue;907 }908 909 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",910 (unsigned long long)to, td->version[chip]);911 912 /* Mark it as used */913 td->pages[chip++] = page;914 }915 return 0;916 917 outerr:918 pr_warn("nand_bbt: error while writing bad block table %d\n", res);919 return res;920}921 922/**923 * nand_memory_bbt - [GENERIC] create a memory based bad block table924 * @this: NAND chip object925 * @bd: descriptor for the good/bad block search pattern926 *927 * The function creates a memory based bbt by scanning the device for928 * manufacturer / software marked good / bad blocks.929 */930static inline int nand_memory_bbt(struct nand_chip *this,931 struct nand_bbt_descr *bd)932{933 u8 *pagebuf = nand_get_data_buf(this);934 935 return create_bbt(this, pagebuf, bd, -1);936}937 938/**939 * check_create - [GENERIC] create and write bbt(s) if necessary940 * @this: the NAND device941 * @buf: temporary buffer942 * @bd: descriptor for the good/bad block search pattern943 *944 * The function checks the results of the previous call to read_bbt and creates945 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found946 * for the chip/device. Update is necessary if one of the tables is missing or947 * the version nr. of one table is less than the other.948 */949static int check_create(struct nand_chip *this, uint8_t *buf,950 struct nand_bbt_descr *bd)951{952 int i, chips, writeops, create, chipsel, res, res2;953 struct nand_bbt_descr *td = this->bbt_td;954 struct nand_bbt_descr *md = this->bbt_md;955 struct nand_bbt_descr *rd, *rd2;956 957 /* Do we have a bbt per chip? */958 if (td->options & NAND_BBT_PERCHIP)959 chips = nanddev_ntargets(&this->base);960 else961 chips = 1;962 963 for (i = 0; i < chips; i++) {964 writeops = 0;965 create = 0;966 rd = NULL;967 rd2 = NULL;968 res = res2 = 0;969 /* Per chip or per device? */970 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;971 /* Mirrored table available? */972 if (md) {973 if (td->pages[i] == -1 && md->pages[i] == -1) {974 create = 1;975 writeops = 0x03;976 } else if (td->pages[i] == -1) {977 rd = md;978 writeops = 0x01;979 } else if (md->pages[i] == -1) {980 rd = td;981 writeops = 0x02;982 } else if (td->version[i] == md->version[i]) {983 rd = td;984 if (!(td->options & NAND_BBT_VERSION))985 rd2 = md;986 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {987 rd = td;988 writeops = 0x02;989 } else {990 rd = md;991 writeops = 0x01;992 }993 } else {994 if (td->pages[i] == -1) {995 create = 1;996 writeops = 0x01;997 } else {998 rd = td;999 }1000 }1001 1002 if (create) {1003 /* Create the bad block table by scanning the device? */1004 if (!(td->options & NAND_BBT_CREATE))1005 continue;1006 1007 /* Create the table in memory by scanning the chip(s) */1008 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))1009 create_bbt(this, buf, bd, chipsel);1010 1011 td->version[i] = 1;1012 if (md)1013 md->version[i] = 1;1014 }1015 1016 /* Read back first? */1017 if (rd) {1018 res = read_abs_bbt(this, buf, rd, chipsel);1019 if (mtd_is_eccerr(res)) {1020 /* Mark table as invalid */1021 rd->pages[i] = -1;1022 rd->version[i] = 0;1023 i--;1024 continue;1025 }1026 }1027 /* If they weren't versioned, read both */1028 if (rd2) {1029 res2 = read_abs_bbt(this, buf, rd2, chipsel);1030 if (mtd_is_eccerr(res2)) {1031 /* Mark table as invalid */1032 rd2->pages[i] = -1;1033 rd2->version[i] = 0;1034 i--;1035 continue;1036 }1037 }1038 1039 /* Scrub the flash table(s)? */1040 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))1041 writeops = 0x03;1042 1043 /* Update version numbers before writing */1044 if (md) {1045 td->version[i] = max(td->version[i], md->version[i]);1046 md->version[i] = td->version[i];1047 }1048 1049 /* Write the bad block table to the device? */1050 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {1051 res = write_bbt(this, buf, td, md, chipsel);1052 if (res < 0)1053 return res;1054 }1055 1056 /* Write the mirror bad block table to the device? */1057 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {1058 res = write_bbt(this, buf, md, td, chipsel);1059 if (res < 0)1060 return res;1061 }1062 }1063 return 0;1064}1065 1066/**1067 * nand_update_bbt - update bad block table(s)1068 * @this: the NAND device1069 * @offs: the offset of the newly marked block1070 *1071 * The function updates the bad block table(s).1072 */1073static int nand_update_bbt(struct nand_chip *this, loff_t offs)1074{1075 struct mtd_info *mtd = nand_to_mtd(this);1076 int len, res = 0;1077 int chip, chipsel;1078 uint8_t *buf;1079 struct nand_bbt_descr *td = this->bbt_td;1080 struct nand_bbt_descr *md = this->bbt_md;1081 1082 if (!this->bbt || !td)1083 return -EINVAL;1084 1085 /* Allocate a temporary buffer for one eraseblock incl. oob */1086 len = (1 << this->bbt_erase_shift);1087 len += (len >> this->page_shift) * mtd->oobsize;1088 buf = kmalloc(len, GFP_KERNEL);1089 if (!buf)1090 return -ENOMEM;1091 1092 /* Do we have a bbt per chip? */1093 if (td->options & NAND_BBT_PERCHIP) {1094 chip = (int)(offs >> this->chip_shift);1095 chipsel = chip;1096 } else {1097 chip = 0;1098 chipsel = -1;1099 }1100 1101 td->version[chip]++;1102 if (md)1103 md->version[chip]++;1104 1105 /* Write the bad block table to the device? */1106 if (td->options & NAND_BBT_WRITE) {1107 res = write_bbt(this, buf, td, md, chipsel);1108 if (res < 0)1109 goto out;1110 }1111 /* Write the mirror bad block table to the device? */1112 if (md && (md->options & NAND_BBT_WRITE)) {1113 res = write_bbt(this, buf, md, td, chipsel);1114 }1115 1116 out:1117 kfree(buf);1118 return res;1119}1120 1121/**1122 * mark_bbt_region - [GENERIC] mark the bad block table regions1123 * @this: the NAND device1124 * @td: bad block table descriptor1125 *1126 * The bad block table regions are marked as "bad" to prevent accidental1127 * erasures / writes. The regions are identified by the mark 0x02.1128 */1129static void mark_bbt_region(struct nand_chip *this, struct nand_bbt_descr *td)1130{1131 u64 targetsize = nanddev_target_size(&this->base);1132 struct mtd_info *mtd = nand_to_mtd(this);1133 int i, j, chips, block, nrblocks, update;1134 uint8_t oldval;1135 1136 /* Do we have a bbt per chip? */1137 if (td->options & NAND_BBT_PERCHIP) {1138 chips = nanddev_ntargets(&this->base);1139 nrblocks = (int)(targetsize >> this->bbt_erase_shift);1140 } else {1141 chips = 1;1142 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);1143 }1144 1145 for (i = 0; i < chips; i++) {1146 if ((td->options & NAND_BBT_ABSPAGE) ||1147 !(td->options & NAND_BBT_WRITE)) {1148 if (td->pages[i] == -1)1149 continue;1150 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);1151 oldval = bbt_get_entry(this, block);1152 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);1153 if ((oldval != BBT_BLOCK_RESERVED) &&1154 td->reserved_block_code)1155 nand_update_bbt(this, (loff_t)block <<1156 this->bbt_erase_shift);1157 continue;1158 }1159 update = 0;1160 if (td->options & NAND_BBT_LASTBLOCK)1161 block = ((i + 1) * nrblocks) - td->maxblocks;1162 else1163 block = i * nrblocks;1164 for (j = 0; j < td->maxblocks; j++) {1165 oldval = bbt_get_entry(this, block);1166 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);1167 if (oldval != BBT_BLOCK_RESERVED)1168 update = 1;1169 block++;1170 }1171 /*1172 * If we want reserved blocks to be recorded to flash, and some1173 * new ones have been marked, then we need to update the stored1174 * bbts. This should only happen once.1175 */1176 if (update && td->reserved_block_code)1177 nand_update_bbt(this, (loff_t)(block - 1) <<1178 this->bbt_erase_shift);1179 }1180}1181 1182/**1183 * verify_bbt_descr - verify the bad block description1184 * @this: the NAND device1185 * @bd: the table to verify1186 *1187 * This functions performs a few sanity checks on the bad block description1188 * table.1189 */1190static void verify_bbt_descr(struct nand_chip *this, struct nand_bbt_descr *bd)1191{1192 u64 targetsize = nanddev_target_size(&this->base);1193 struct mtd_info *mtd = nand_to_mtd(this);1194 u32 pattern_len;1195 u32 bits;1196 u32 table_size;1197 1198 if (!bd)1199 return;1200 1201 pattern_len = bd->len;1202 bits = bd->options & NAND_BBT_NRBITS_MSK;1203 1204 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&1205 !(this->bbt_options & NAND_BBT_USE_FLASH));1206 BUG_ON(!bits);1207 1208 if (bd->options & NAND_BBT_VERSION)1209 pattern_len++;1210 1211 if (bd->options & NAND_BBT_NO_OOB) {1212 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));1213 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));1214 BUG_ON(bd->offs);1215 if (bd->options & NAND_BBT_VERSION)1216 BUG_ON(bd->veroffs != bd->len);1217 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);1218 }1219 1220 if (bd->options & NAND_BBT_PERCHIP)1221 table_size = targetsize >> this->bbt_erase_shift;1222 else1223 table_size = mtd->size >> this->bbt_erase_shift;1224 table_size >>= 3;1225 table_size *= bits;1226 if (bd->options & NAND_BBT_NO_OOB)1227 table_size += pattern_len;1228 BUG_ON(table_size > (1 << this->bbt_erase_shift));1229}1230 1231/**1232 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)1233 * @this: the NAND device1234 * @bd: descriptor for the good/bad block search pattern1235 *1236 * The function checks, if a bad block table(s) is/are already available. If1237 * not it scans the device for manufacturer marked good / bad blocks and writes1238 * the bad block table(s) to the selected place.1239 *1240 * The bad block table memory is allocated here. It must be freed by calling1241 * the nand_free_bbt function.1242 */1243static int nand_scan_bbt(struct nand_chip *this, struct nand_bbt_descr *bd)1244{1245 struct mtd_info *mtd = nand_to_mtd(this);1246 int len, res;1247 uint8_t *buf;1248 struct nand_bbt_descr *td = this->bbt_td;1249 struct nand_bbt_descr *md = this->bbt_md;1250 1251 len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;1252 /*1253 * Allocate memory (2bit per block) and clear the memory bad block1254 * table.1255 */1256 this->bbt = kzalloc(len, GFP_KERNEL);1257 if (!this->bbt)1258 return -ENOMEM;1259 1260 /*1261 * If no primary table descriptor is given, scan the device to build a1262 * memory based bad block table.1263 */1264 if (!td) {1265 if ((res = nand_memory_bbt(this, bd))) {1266 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");1267 goto err_free_bbt;1268 }1269 return 0;1270 }1271 verify_bbt_descr(this, td);1272 verify_bbt_descr(this, md);1273 1274 /* Allocate a temporary buffer for one eraseblock incl. oob */1275 len = (1 << this->bbt_erase_shift);1276 len += (len >> this->page_shift) * mtd->oobsize;1277 buf = vmalloc(len);1278 if (!buf) {1279 res = -ENOMEM;1280 goto err_free_bbt;1281 }1282 1283 /* Is the bbt at a given page? */1284 if (td->options & NAND_BBT_ABSPAGE) {1285 read_abs_bbts(this, buf, td, md);1286 } else {1287 /* Search the bad block table using a pattern in oob */1288 search_read_bbts(this, buf, td, md);1289 }1290 1291 res = check_create(this, buf, bd);1292 if (res)1293 goto err_free_buf;1294 1295 /* Prevent the bbt regions from erasing / writing */1296 mark_bbt_region(this, td);1297 if (md)1298 mark_bbt_region(this, md);1299 1300 vfree(buf);1301 return 0;1302 1303err_free_buf:1304 vfree(buf);1305err_free_bbt:1306 kfree(this->bbt);1307 this->bbt = NULL;1308 return res;1309}1310 1311/*1312 * Define some generic bad / good block scan pattern which are used1313 * while scanning a device for factory marked good / bad blocks.1314 */1315static uint8_t scan_ff_pattern[] = { 0xff, 0xff };1316 1317/* Generic flash bbt descriptors */1318static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };1319static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };1320 1321static struct nand_bbt_descr bbt_main_descr = {1322 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE1323 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,1324 .offs = 8,1325 .len = 4,1326 .veroffs = 12,1327 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,1328 .pattern = bbt_pattern1329};1330 1331static struct nand_bbt_descr bbt_mirror_descr = {1332 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE1333 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,1334 .offs = 8,1335 .len = 4,1336 .veroffs = 12,1337 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,1338 .pattern = mirror_pattern1339};1340 1341static struct nand_bbt_descr bbt_main_no_oob_descr = {1342 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE1343 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP1344 | NAND_BBT_NO_OOB,1345 .len = 4,1346 .veroffs = 4,1347 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,1348 .pattern = bbt_pattern1349};1350 1351static struct nand_bbt_descr bbt_mirror_no_oob_descr = {1352 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE1353 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP1354 | NAND_BBT_NO_OOB,1355 .len = 4,1356 .veroffs = 4,1357 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,1358 .pattern = mirror_pattern1359};1360 1361#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)1362/**1363 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure1364 * @this: NAND chip to create descriptor for1365 *1366 * This function allocates and initializes a nand_bbt_descr for BBM detection1367 * based on the properties of @this. The new descriptor is stored in1368 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when1369 * passed to this function.1370 */1371static int nand_create_badblock_pattern(struct nand_chip *this)1372{1373 struct nand_bbt_descr *bd;1374 if (this->badblock_pattern) {1375 pr_warn("Bad block pattern already allocated; not replacing\n");1376 return -EINVAL;1377 }1378 bd = kzalloc(sizeof(*bd), GFP_KERNEL);1379 if (!bd)1380 return -ENOMEM;1381 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;1382 bd->offs = this->badblockpos;1383 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;1384 bd->pattern = scan_ff_pattern;1385 bd->options |= NAND_BBT_DYNAMICSTRUCT;1386 this->badblock_pattern = bd;1387 return 0;1388}1389 1390/**1391 * nand_create_bbt - [NAND Interface] Select a default bad block table for the device1392 * @this: NAND chip object1393 *1394 * This function selects the default bad block table support for the device and1395 * calls the nand_scan_bbt function.1396 */1397int nand_create_bbt(struct nand_chip *this)1398{1399 int ret;1400 1401 /* Is a flash based bad block table requested? */1402 if (this->bbt_options & NAND_BBT_USE_FLASH) {1403 /* Use the default pattern descriptors */1404 if (!this->bbt_td) {1405 if (this->bbt_options & NAND_BBT_NO_OOB) {1406 this->bbt_td = &bbt_main_no_oob_descr;1407 this->bbt_md = &bbt_mirror_no_oob_descr;1408 } else {1409 this->bbt_td = &bbt_main_descr;1410 this->bbt_md = &bbt_mirror_descr;1411 }1412 }1413 } else {1414 this->bbt_td = NULL;1415 this->bbt_md = NULL;1416 }1417 1418 if (!this->badblock_pattern) {1419 ret = nand_create_badblock_pattern(this);1420 if (ret)1421 return ret;1422 }1423 1424 return nand_scan_bbt(this, this->badblock_pattern);1425}1426EXPORT_SYMBOL(nand_create_bbt);1427 1428/**1429 * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved1430 * @this: NAND chip object1431 * @offs: offset in the device1432 */1433int nand_isreserved_bbt(struct nand_chip *this, loff_t offs)1434{1435 int block;1436 1437 block = (int)(offs >> this->bbt_erase_shift);1438 return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;1439}1440 1441/**1442 * nand_isbad_bbt - [NAND Interface] Check if a block is bad1443 * @this: NAND chip object1444 * @offs: offset in the device1445 * @allowbbt: allow access to bad block table region1446 */1447int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt)1448{1449 int block, res;1450 1451 block = (int)(offs >> this->bbt_erase_shift);1452 res = bbt_get_entry(this, block);1453 1454 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",1455 (unsigned int)offs, block, res);1456 1457 if (mtd_check_expert_analysis_mode())1458 return 0;1459 1460 switch (res) {1461 case BBT_BLOCK_GOOD:1462 return 0;1463 case BBT_BLOCK_WORN:1464 return 1;1465 case BBT_BLOCK_RESERVED:1466 return allowbbt ? 0 : 1;1467 }1468 return 1;1469}1470 1471/**1472 * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT1473 * @this: NAND chip object1474 * @offs: offset of the bad block1475 */1476int nand_markbad_bbt(struct nand_chip *this, loff_t offs)1477{1478 int block, ret = 0;1479 1480 block = (int)(offs >> this->bbt_erase_shift);1481 1482 /* Mark bad block in memory */1483 bbt_mark_entry(this, block, BBT_BLOCK_WORN);1484 1485 /* Update flash-based bad block table */1486 if (this->bbt_options & NAND_BBT_USE_FLASH)1487 ret = nand_update_bbt(this, offs);1488 1489 return ret;1490}1491