1 /* 2 * Overview: 3 * Bad block table support for the NAND driver 4 * 5 * Copyright © 2004 Thomas Gleixner (tglx@linutronix.de) 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 * 11 * Description: 12 * 13 * When nand_scan_bbt is called, then it tries to find the bad block table 14 * depending on the options in the BBT descriptor(s). If no flash based BBT 15 * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory 16 * marked good / bad blocks. This information is used to create a memory BBT. 17 * Once a new bad block is discovered then the "factory" information is updated 18 * on the device. 19 * If a flash based BBT is specified then the function first tries to find the 20 * BBT on flash. If a BBT is found then the contents are read and the memory 21 * based BBT is created. If a mirrored BBT is selected then the mirror is 22 * searched too and the versions are compared. If the mirror has a greater 23 * version number, then the mirror BBT is used to build the memory based BBT. 24 * If the tables are not versioned, then we "or" the bad block information. 25 * If one of the BBTs is out of date or does not exist it is (re)created. 26 * If no BBT exists at all then the device is scanned for factory marked 27 * good / bad blocks and the bad block tables are created. 28 * 29 * For manufacturer created BBTs like the one found on M-SYS DOC devices 30 * the BBT is searched and read but never created 31 * 32 * The auto generated bad block table is located in the last good blocks 33 * of the device. The table is mirrored, so it can be updated eventually. 34 * The table is marked in the OOB area with an ident pattern and a version 35 * number which indicates which of both tables is more up to date. If the NAND 36 * controller needs the complete OOB area for the ECC information then the 37 * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of 38 * course): it moves the ident pattern and the version byte into the data area 39 * and the OOB area will remain untouched. 40 * 41 * The table uses 2 bits per block 42 * 11b: block is good 43 * 00b: block is factory marked bad 44 * 01b, 10b: block is marked bad due to wear 45 * 46 * The memory bad block table uses the following scheme: 47 * 00b: block is good 48 * 01b: block is marked bad due to wear 49 * 10b: block is reserved (to protect the bbt area) 50 * 11b: block is factory marked bad 51 * 52 * Multichip devices like DOC store the bad block info per floor. 53 * 54 * Following assumptions are made: 55 * - bbts start at a page boundary, if autolocated on a block boundary 56 * - the space necessary for a bbt in FLASH does not exceed a block boundary 57 * 58 */ 59 60 #include <linux/slab.h> 61 #include <linux/types.h> 62 #include <linux/mtd/mtd.h> 63 #include <linux/mtd/bbm.h> 64 #include <linux/bitops.h> 65 #include <linux/delay.h> 66 #include <linux/vmalloc.h> 67 #include <linux/export.h> 68 #include <linux/string.h> 69 70 #include "internals.h" 71 72 #define BBT_BLOCK_GOOD 0x00 73 #define BBT_BLOCK_WORN 0x01 74 #define BBT_BLOCK_RESERVED 0x02 75 #define BBT_BLOCK_FACTORY_BAD 0x03 76 77 #define BBT_ENTRY_MASK 0x03 78 #define BBT_ENTRY_SHIFT 2 79 80 static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block) 81 { 82 uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT]; 83 entry >>= (block & BBT_ENTRY_MASK) * 2; 84 return entry & BBT_ENTRY_MASK; 85 } 86 87 static inline void bbt_mark_entry(struct nand_chip *chip, int block, 88 uint8_t mark) 89 { 90 uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2); 91 chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk; 92 } 93 94 static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td) 95 { 96 if (memcmp(buf, td->pattern, td->len)) 97 return -1; 98 return 0; 99 } 100 101 /** 102 * check_pattern - [GENERIC] check if a pattern is in the buffer 103 * @buf: the buffer to search 104 * @len: the length of buffer to search 105 * @paglen: the pagelength 106 * @td: search pattern descriptor 107 * 108 * Check for a pattern at the given place. Used to search bad block tables and 109 * good / bad block identifiers. 110 */ 111 static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td) 112 { 113 if (td->options & NAND_BBT_NO_OOB) 114 return check_pattern_no_oob(buf, td); 115 116 /* Compare the pattern */ 117 if (memcmp(buf + paglen + td->offs, td->pattern, td->len)) 118 return -1; 119 120 return 0; 121 } 122 123 /** 124 * check_short_pattern - [GENERIC] check if a pattern is in the buffer 125 * @buf: the buffer to search 126 * @td: search pattern descriptor 127 * 128 * Check for a pattern at the given place. Used to search bad block tables and 129 * good / bad block identifiers. Same as check_pattern, but no optional empty 130 * check. 131 */ 132 static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td) 133 { 134 /* Compare the pattern */ 135 if (memcmp(buf + td->offs, td->pattern, td->len)) 136 return -1; 137 return 0; 138 } 139 140 /** 141 * add_marker_len - compute the length of the marker in data area 142 * @td: BBT descriptor used for computation 143 * 144 * The length will be 0 if the marker is located in OOB area. 145 */ 146 static u32 add_marker_len(struct nand_bbt_descr *td) 147 { 148 u32 len; 149 150 if (!(td->options & NAND_BBT_NO_OOB)) 151 return 0; 152 153 len = td->len; 154 if (td->options & NAND_BBT_VERSION) 155 len++; 156 return len; 157 } 158 159 /** 160 * read_bbt - [GENERIC] Read the bad block table starting from page 161 * @this: NAND chip object 162 * @buf: temporary buffer 163 * @page: the starting page 164 * @num: the number of bbt descriptors to read 165 * @td: the bbt describtion table 166 * @offs: block number offset in the table 167 * 168 * Read the bad block table starting from page. 169 */ 170 static int read_bbt(struct nand_chip *this, uint8_t *buf, int page, int num, 171 struct nand_bbt_descr *td, int offs) 172 { 173 struct mtd_info *mtd = nand_to_mtd(this); 174 int res, ret = 0, i, j, act = 0; 175 size_t retlen, len, totlen; 176 loff_t from; 177 int bits = td->options & NAND_BBT_NRBITS_MSK; 178 uint8_t msk = (uint8_t)((1 << bits) - 1); 179 u32 marker_len; 180 int reserved_block_code = td->reserved_block_code; 181 182 totlen = (num * bits) >> 3; 183 marker_len = add_marker_len(td); 184 from = ((loff_t)page) << this->page_shift; 185 186 while (totlen) { 187 len = min(totlen, (size_t)(1 << this->bbt_erase_shift)); 188 if (marker_len) { 189 /* 190 * In case the BBT marker is not in the OOB area it 191 * will be just in the first page. 192 */ 193 len -= marker_len; 194 from += marker_len; 195 marker_len = 0; 196 } 197 res = mtd_read(mtd, from, len, &retlen, buf); 198 if (res < 0) { 199 if (mtd_is_eccerr(res)) { 200 pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n", 201 from & ~mtd->writesize); 202 return res; 203 } else if (mtd_is_bitflip(res)) { 204 pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n", 205 from & ~mtd->writesize); 206 ret = res; 207 } else { 208 pr_info("nand_bbt: error reading BBT\n"); 209 return res; 210 } 211 } 212 213 /* Analyse data */ 214 for (i = 0; i < len; i++) { 215 uint8_t dat = buf[i]; 216 for (j = 0; j < 8; j += bits, act++) { 217 uint8_t tmp = (dat >> j) & msk; 218 if (tmp == msk) 219 continue; 220 if (reserved_block_code && (tmp == reserved_block_code)) { 221 pr_info("nand_read_bbt: reserved block at 0x%012llx\n", 222 (loff_t)(offs + act) << 223 this->bbt_erase_shift); 224 bbt_mark_entry(this, offs + act, 225 BBT_BLOCK_RESERVED); 226 mtd->ecc_stats.bbtblocks++; 227 continue; 228 } 229 /* 230 * Leave it for now, if it's matured we can 231 * move this message to pr_debug. 232 */ 233 pr_info("nand_read_bbt: bad block at 0x%012llx\n", 234 (loff_t)(offs + act) << 235 this->bbt_erase_shift); 236 /* Factory marked bad or worn out? */ 237 if (tmp == 0) 238 bbt_mark_entry(this, offs + act, 239 BBT_BLOCK_FACTORY_BAD); 240 else 241 bbt_mark_entry(this, offs + act, 242 BBT_BLOCK_WORN); 243 mtd->ecc_stats.badblocks++; 244 } 245 } 246 totlen -= len; 247 from += len; 248 } 249 return ret; 250 } 251 252 /** 253 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page 254 * @this: NAND chip object 255 * @buf: temporary buffer 256 * @td: descriptor for the bad block table 257 * @chip: read the table for a specific chip, -1 read all chips; applies only if 258 * NAND_BBT_PERCHIP option is set 259 * 260 * Read the bad block table for all chips starting at a given page. We assume 261 * that the bbt bits are in consecutive order. 262 */ 263 static int read_abs_bbt(struct nand_chip *this, uint8_t *buf, 264 struct nand_bbt_descr *td, int chip) 265 { 266 struct mtd_info *mtd = nand_to_mtd(this); 267 int res = 0, i; 268 269 if (td->options & NAND_BBT_PERCHIP) { 270 int offs = 0; 271 for (i = 0; i < this->numchips; i++) { 272 if (chip == -1 || chip == i) 273 res = read_bbt(this, buf, td->pages[i], 274 this->chipsize >> this->bbt_erase_shift, 275 td, offs); 276 if (res) 277 return res; 278 offs += this->chipsize >> this->bbt_erase_shift; 279 } 280 } else { 281 res = read_bbt(this, buf, td->pages[0], 282 mtd->size >> this->bbt_erase_shift, td, 0); 283 if (res) 284 return res; 285 } 286 return 0; 287 } 288 289 /* BBT marker is in the first page, no OOB */ 290 static int scan_read_data(struct nand_chip *this, uint8_t *buf, loff_t offs, 291 struct nand_bbt_descr *td) 292 { 293 struct mtd_info *mtd = nand_to_mtd(this); 294 size_t retlen; 295 size_t len; 296 297 len = td->len; 298 if (td->options & NAND_BBT_VERSION) 299 len++; 300 301 return mtd_read(mtd, offs, len, &retlen, buf); 302 } 303 304 /** 305 * scan_read_oob - [GENERIC] Scan data+OOB region to buffer 306 * @this: NAND chip object 307 * @buf: temporary buffer 308 * @offs: offset at which to scan 309 * @len: length of data region to read 310 * 311 * Scan read data from data+OOB. May traverse multiple pages, interleaving 312 * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest" 313 * ECC condition (error or bitflip). May quit on the first (non-ECC) error. 314 */ 315 static int scan_read_oob(struct nand_chip *this, uint8_t *buf, loff_t offs, 316 size_t len) 317 { 318 struct mtd_info *mtd = nand_to_mtd(this); 319 struct mtd_oob_ops ops; 320 int res, ret = 0; 321 322 ops.mode = MTD_OPS_PLACE_OOB; 323 ops.ooboffs = 0; 324 ops.ooblen = mtd->oobsize; 325 326 while (len > 0) { 327 ops.datbuf = buf; 328 ops.len = min(len, (size_t)mtd->writesize); 329 ops.oobbuf = buf + ops.len; 330 331 res = mtd_read_oob(mtd, offs, &ops); 332 if (res) { 333 if (!mtd_is_bitflip_or_eccerr(res)) 334 return res; 335 else if (mtd_is_eccerr(res) || !ret) 336 ret = res; 337 } 338 339 buf += mtd->oobsize + mtd->writesize; 340 len -= mtd->writesize; 341 offs += mtd->writesize; 342 } 343 return ret; 344 } 345 346 static int scan_read(struct nand_chip *this, uint8_t *buf, loff_t offs, 347 size_t len, struct nand_bbt_descr *td) 348 { 349 if (td->options & NAND_BBT_NO_OOB) 350 return scan_read_data(this, buf, offs, td); 351 else 352 return scan_read_oob(this, buf, offs, len); 353 } 354 355 /* Scan write data with oob to flash */ 356 static int scan_write_bbt(struct nand_chip *this, loff_t offs, size_t len, 357 uint8_t *buf, uint8_t *oob) 358 { 359 struct mtd_info *mtd = nand_to_mtd(this); 360 struct mtd_oob_ops ops; 361 362 ops.mode = MTD_OPS_PLACE_OOB; 363 ops.ooboffs = 0; 364 ops.ooblen = mtd->oobsize; 365 ops.datbuf = buf; 366 ops.oobbuf = oob; 367 ops.len = len; 368 369 return mtd_write_oob(mtd, offs, &ops); 370 } 371 372 static u32 bbt_get_ver_offs(struct nand_chip *this, struct nand_bbt_descr *td) 373 { 374 struct mtd_info *mtd = nand_to_mtd(this); 375 u32 ver_offs = td->veroffs; 376 377 if (!(td->options & NAND_BBT_NO_OOB)) 378 ver_offs += mtd->writesize; 379 return ver_offs; 380 } 381 382 /** 383 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page 384 * @this: NAND chip object 385 * @buf: temporary buffer 386 * @td: descriptor for the bad block table 387 * @md: descriptor for the bad block table mirror 388 * 389 * Read the bad block table(s) for all chips starting at a given page. We 390 * assume that the bbt bits are in consecutive order. 391 */ 392 static void read_abs_bbts(struct nand_chip *this, uint8_t *buf, 393 struct nand_bbt_descr *td, struct nand_bbt_descr *md) 394 { 395 struct mtd_info *mtd = nand_to_mtd(this); 396 397 /* Read the primary version, if available */ 398 if (td->options & NAND_BBT_VERSION) { 399 scan_read(this, buf, (loff_t)td->pages[0] << this->page_shift, 400 mtd->writesize, td); 401 td->version[0] = buf[bbt_get_ver_offs(this, td)]; 402 pr_info("Bad block table at page %d, version 0x%02X\n", 403 td->pages[0], td->version[0]); 404 } 405 406 /* Read the mirror version, if available */ 407 if (md && (md->options & NAND_BBT_VERSION)) { 408 scan_read(this, buf, (loff_t)md->pages[0] << this->page_shift, 409 mtd->writesize, md); 410 md->version[0] = buf[bbt_get_ver_offs(this, md)]; 411 pr_info("Bad block table at page %d, version 0x%02X\n", 412 md->pages[0], md->version[0]); 413 } 414 } 415 416 /* Scan a given block partially */ 417 static int scan_block_fast(struct nand_chip *this, struct nand_bbt_descr *bd, 418 loff_t offs, uint8_t *buf, int numpages) 419 { 420 struct mtd_info *mtd = nand_to_mtd(this); 421 struct mtd_oob_ops ops; 422 int j, ret; 423 424 ops.ooblen = mtd->oobsize; 425 ops.oobbuf = buf; 426 ops.ooboffs = 0; 427 ops.datbuf = NULL; 428 ops.mode = MTD_OPS_PLACE_OOB; 429 430 for (j = 0; j < numpages; j++) { 431 /* 432 * Read the full oob until read_oob is fixed to handle single 433 * byte reads for 16 bit buswidth. 434 */ 435 ret = mtd_read_oob(mtd, offs, &ops); 436 /* Ignore ECC errors when checking for BBM */ 437 if (ret && !mtd_is_bitflip_or_eccerr(ret)) 438 return ret; 439 440 if (check_short_pattern(buf, bd)) 441 return 1; 442 443 offs += mtd->writesize; 444 } 445 return 0; 446 } 447 448 /** 449 * create_bbt - [GENERIC] Create a bad block table by scanning the device 450 * @this: NAND chip object 451 * @buf: temporary buffer 452 * @bd: descriptor for the good/bad block search pattern 453 * @chip: create the table for a specific chip, -1 read all chips; applies only 454 * if NAND_BBT_PERCHIP option is set 455 * 456 * Create a bad block table by scanning the device for the given good/bad block 457 * identify pattern. 458 */ 459 static int create_bbt(struct nand_chip *this, uint8_t *buf, 460 struct nand_bbt_descr *bd, int chip) 461 { 462 struct mtd_info *mtd = nand_to_mtd(this); 463 int i, numblocks, numpages; 464 int startblock; 465 loff_t from; 466 467 pr_info("Scanning device for bad blocks\n"); 468 469 if (bd->options & NAND_BBT_SCAN2NDPAGE) 470 numpages = 2; 471 else 472 numpages = 1; 473 474 if (chip == -1) { 475 numblocks = mtd->size >> this->bbt_erase_shift; 476 startblock = 0; 477 from = 0; 478 } else { 479 if (chip >= this->numchips) { 480 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n", 481 chip + 1, this->numchips); 482 return -EINVAL; 483 } 484 numblocks = this->chipsize >> this->bbt_erase_shift; 485 startblock = chip * numblocks; 486 numblocks += startblock; 487 from = (loff_t)startblock << this->bbt_erase_shift; 488 } 489 490 if (this->bbt_options & NAND_BBT_SCANLASTPAGE) 491 from += mtd->erasesize - (mtd->writesize * numpages); 492 493 for (i = startblock; i < numblocks; i++) { 494 int ret; 495 496 BUG_ON(bd->options & NAND_BBT_NO_OOB); 497 498 ret = scan_block_fast(this, bd, from, buf, numpages); 499 if (ret < 0) 500 return ret; 501 502 if (ret) { 503 bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD); 504 pr_warn("Bad eraseblock %d at 0x%012llx\n", 505 i, (unsigned long long)from); 506 mtd->ecc_stats.badblocks++; 507 } 508 509 from += (1 << this->bbt_erase_shift); 510 } 511 return 0; 512 } 513 514 /** 515 * search_bbt - [GENERIC] scan the device for a specific bad block table 516 * @this: NAND chip object 517 * @buf: temporary buffer 518 * @td: descriptor for the bad block table 519 * 520 * Read the bad block table by searching for a given ident pattern. Search is 521 * preformed either from the beginning up or from the end of the device 522 * downwards. The search starts always at the start of a block. If the option 523 * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains 524 * the bad block information of this chip. This is necessary to provide support 525 * for certain DOC devices. 526 * 527 * The bbt ident pattern resides in the oob area of the first page in a block. 528 */ 529 static int search_bbt(struct nand_chip *this, uint8_t *buf, 530 struct nand_bbt_descr *td) 531 { 532 struct mtd_info *mtd = nand_to_mtd(this); 533 int i, chips; 534 int startblock, block, dir; 535 int scanlen = mtd->writesize + mtd->oobsize; 536 int bbtblocks; 537 int blocktopage = this->bbt_erase_shift - this->page_shift; 538 539 /* Search direction top -> down? */ 540 if (td->options & NAND_BBT_LASTBLOCK) { 541 startblock = (mtd->size >> this->bbt_erase_shift) - 1; 542 dir = -1; 543 } else { 544 startblock = 0; 545 dir = 1; 546 } 547 548 /* Do we have a bbt per chip? */ 549 if (td->options & NAND_BBT_PERCHIP) { 550 chips = this->numchips; 551 bbtblocks = this->chipsize >> this->bbt_erase_shift; 552 startblock &= bbtblocks - 1; 553 } else { 554 chips = 1; 555 bbtblocks = mtd->size >> this->bbt_erase_shift; 556 } 557 558 for (i = 0; i < chips; i++) { 559 /* Reset version information */ 560 td->version[i] = 0; 561 td->pages[i] = -1; 562 /* Scan the maximum number of blocks */ 563 for (block = 0; block < td->maxblocks; block++) { 564 565 int actblock = startblock + dir * block; 566 loff_t offs = (loff_t)actblock << this->bbt_erase_shift; 567 568 /* Read first page */ 569 scan_read(this, buf, offs, mtd->writesize, td); 570 if (!check_pattern(buf, scanlen, mtd->writesize, td)) { 571 td->pages[i] = actblock << blocktopage; 572 if (td->options & NAND_BBT_VERSION) { 573 offs = bbt_get_ver_offs(this, td); 574 td->version[i] = buf[offs]; 575 } 576 break; 577 } 578 } 579 startblock += this->chipsize >> this->bbt_erase_shift; 580 } 581 /* Check, if we found a bbt for each requested chip */ 582 for (i = 0; i < chips; i++) { 583 if (td->pages[i] == -1) 584 pr_warn("Bad block table not found for chip %d\n", i); 585 else 586 pr_info("Bad block table found at page %d, version 0x%02X\n", 587 td->pages[i], td->version[i]); 588 } 589 return 0; 590 } 591 592 /** 593 * search_read_bbts - [GENERIC] scan the device for bad block table(s) 594 * @this: NAND chip object 595 * @buf: temporary buffer 596 * @td: descriptor for the bad block table 597 * @md: descriptor for the bad block table mirror 598 * 599 * Search and read the bad block table(s). 600 */ 601 static void search_read_bbts(struct nand_chip *this, uint8_t *buf, 602 struct nand_bbt_descr *td, 603 struct nand_bbt_descr *md) 604 { 605 /* Search the primary table */ 606 search_bbt(this, buf, td); 607 608 /* Search the mirror table */ 609 if (md) 610 search_bbt(this, buf, md); 611 } 612 613 /** 614 * get_bbt_block - Get the first valid eraseblock suitable to store a BBT 615 * @this: the NAND device 616 * @td: the BBT description 617 * @md: the mirror BBT descriptor 618 * @chip: the CHIP selector 619 * 620 * This functions returns a positive block number pointing a valid eraseblock 621 * suitable to store a BBT (i.e. in the range reserved for BBT), or -ENOSPC if 622 * all blocks are already used of marked bad. If td->pages[chip] was already 623 * pointing to a valid block we re-use it, otherwise we search for the next 624 * valid one. 625 */ 626 static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td, 627 struct nand_bbt_descr *md, int chip) 628 { 629 int startblock, dir, page, numblocks, i; 630 631 /* 632 * There was already a version of the table, reuse the page. This 633 * applies for absolute placement too, as we have the page number in 634 * td->pages. 635 */ 636 if (td->pages[chip] != -1) 637 return td->pages[chip] >> 638 (this->bbt_erase_shift - this->page_shift); 639 640 numblocks = (int)(this->chipsize >> this->bbt_erase_shift); 641 if (!(td->options & NAND_BBT_PERCHIP)) 642 numblocks *= this->numchips; 643 644 /* 645 * Automatic placement of the bad block table. Search direction 646 * top -> down? 647 */ 648 if (td->options & NAND_BBT_LASTBLOCK) { 649 startblock = numblocks * (chip + 1) - 1; 650 dir = -1; 651 } else { 652 startblock = chip * numblocks; 653 dir = 1; 654 } 655 656 for (i = 0; i < td->maxblocks; i++) { 657 int block = startblock + dir * i; 658 659 /* Check, if the block is bad */ 660 switch (bbt_get_entry(this, block)) { 661 case BBT_BLOCK_WORN: 662 case BBT_BLOCK_FACTORY_BAD: 663 continue; 664 } 665 666 page = block << (this->bbt_erase_shift - this->page_shift); 667 668 /* Check, if the block is used by the mirror table */ 669 if (!md || md->pages[chip] != page) 670 return block; 671 } 672 673 return -ENOSPC; 674 } 675 676 /** 677 * mark_bbt_block_bad - Mark one of the block reserved for BBT bad 678 * @this: the NAND device 679 * @td: the BBT description 680 * @chip: the CHIP selector 681 * @block: the BBT block to mark 682 * 683 * Blocks reserved for BBT can become bad. This functions is an helper to mark 684 * such blocks as bad. It takes care of updating the in-memory BBT, marking the 685 * block as bad using a bad block marker and invalidating the associated 686 * td->pages[] entry. 687 */ 688 static void mark_bbt_block_bad(struct nand_chip *this, 689 struct nand_bbt_descr *td, 690 int chip, int block) 691 { 692 loff_t to; 693 int res; 694 695 bbt_mark_entry(this, block, BBT_BLOCK_WORN); 696 697 to = (loff_t)block << this->bbt_erase_shift; 698 res = nand_markbad_bbm(this, to); 699 if (res) 700 pr_warn("nand_bbt: error %d while marking block %d bad\n", 701 res, block); 702 703 td->pages[chip] = -1; 704 } 705 706 /** 707 * write_bbt - [GENERIC] (Re)write the bad block table 708 * @this: NAND chip object 709 * @buf: temporary buffer 710 * @td: descriptor for the bad block table 711 * @md: descriptor for the bad block table mirror 712 * @chipsel: selector for a specific chip, -1 for all 713 * 714 * (Re)write the bad block table. 715 */ 716 static int write_bbt(struct nand_chip *this, uint8_t *buf, 717 struct nand_bbt_descr *td, struct nand_bbt_descr *md, 718 int chipsel) 719 { 720 struct mtd_info *mtd = nand_to_mtd(this); 721 struct erase_info einfo; 722 int i, res, chip = 0; 723 int bits, page, offs, numblocks, sft, sftmsk; 724 int nrchips, pageoffs, ooboffs; 725 uint8_t msk[4]; 726 uint8_t rcode = td->reserved_block_code; 727 size_t retlen, len = 0; 728 loff_t to; 729 struct mtd_oob_ops ops; 730 731 ops.ooblen = mtd->oobsize; 732 ops.ooboffs = 0; 733 ops.datbuf = NULL; 734 ops.mode = MTD_OPS_PLACE_OOB; 735 736 if (!rcode) 737 rcode = 0xff; 738 /* Write bad block table per chip rather than per device? */ 739 if (td->options & NAND_BBT_PERCHIP) { 740 numblocks = (int)(this->chipsize >> this->bbt_erase_shift); 741 /* Full device write or specific chip? */ 742 if (chipsel == -1) { 743 nrchips = this->numchips; 744 } else { 745 nrchips = chipsel + 1; 746 chip = chipsel; 747 } 748 } else { 749 numblocks = (int)(mtd->size >> this->bbt_erase_shift); 750 nrchips = 1; 751 } 752 753 /* Loop through the chips */ 754 while (chip < nrchips) { 755 int block; 756 757 block = get_bbt_block(this, td, md, chip); 758 if (block < 0) { 759 pr_err("No space left to write bad block table\n"); 760 res = block; 761 goto outerr; 762 } 763 764 /* 765 * get_bbt_block() returns a block number, shift the value to 766 * get a page number. 767 */ 768 page = block << (this->bbt_erase_shift - this->page_shift); 769 770 /* Set up shift count and masks for the flash table */ 771 bits = td->options & NAND_BBT_NRBITS_MSK; 772 msk[2] = ~rcode; 773 switch (bits) { 774 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01; 775 msk[3] = 0x01; 776 break; 777 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01; 778 msk[3] = 0x03; 779 break; 780 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C; 781 msk[3] = 0x0f; 782 break; 783 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F; 784 msk[3] = 0xff; 785 break; 786 default: return -EINVAL; 787 } 788 789 to = ((loff_t)page) << this->page_shift; 790 791 /* Must we save the block contents? */ 792 if (td->options & NAND_BBT_SAVECONTENT) { 793 /* Make it block aligned */ 794 to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1); 795 len = 1 << this->bbt_erase_shift; 796 res = mtd_read(mtd, to, len, &retlen, buf); 797 if (res < 0) { 798 if (retlen != len) { 799 pr_info("nand_bbt: error reading block for writing the bad block table\n"); 800 return res; 801 } 802 pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n"); 803 } 804 /* Read oob data */ 805 ops.ooblen = (len >> this->page_shift) * mtd->oobsize; 806 ops.oobbuf = &buf[len]; 807 res = mtd_read_oob(mtd, to + mtd->writesize, &ops); 808 if (res < 0 || ops.oobretlen != ops.ooblen) 809 goto outerr; 810 811 /* Calc the byte offset in the buffer */ 812 pageoffs = page - (int)(to >> this->page_shift); 813 offs = pageoffs << this->page_shift; 814 /* Preset the bbt area with 0xff */ 815 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft)); 816 ooboffs = len + (pageoffs * mtd->oobsize); 817 818 } else if (td->options & NAND_BBT_NO_OOB) { 819 ooboffs = 0; 820 offs = td->len; 821 /* The version byte */ 822 if (td->options & NAND_BBT_VERSION) 823 offs++; 824 /* Calc length */ 825 len = (size_t)(numblocks >> sft); 826 len += offs; 827 /* Make it page aligned! */ 828 len = ALIGN(len, mtd->writesize); 829 /* Preset the buffer with 0xff */ 830 memset(buf, 0xff, len); 831 /* Pattern is located at the begin of first page */ 832 memcpy(buf, td->pattern, td->len); 833 } else { 834 /* Calc length */ 835 len = (size_t)(numblocks >> sft); 836 /* Make it page aligned! */ 837 len = ALIGN(len, mtd->writesize); 838 /* Preset the buffer with 0xff */ 839 memset(buf, 0xff, len + 840 (len >> this->page_shift)* mtd->oobsize); 841 offs = 0; 842 ooboffs = len; 843 /* Pattern is located in oob area of first page */ 844 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len); 845 } 846 847 if (td->options & NAND_BBT_VERSION) 848 buf[ooboffs + td->veroffs] = td->version[chip]; 849 850 /* Walk through the memory table */ 851 for (i = 0; i < numblocks; i++) { 852 uint8_t dat; 853 int sftcnt = (i << (3 - sft)) & sftmsk; 854 dat = bbt_get_entry(this, chip * numblocks + i); 855 /* Do not store the reserved bbt blocks! */ 856 buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt); 857 } 858 859 memset(&einfo, 0, sizeof(einfo)); 860 einfo.addr = to; 861 einfo.len = 1 << this->bbt_erase_shift; 862 res = nand_erase_nand(this, &einfo, 1); 863 if (res < 0) { 864 pr_warn("nand_bbt: error while erasing BBT block %d\n", 865 res); 866 mark_bbt_block_bad(this, td, chip, block); 867 continue; 868 } 869 870 res = scan_write_bbt(this, to, len, buf, 871 td->options & NAND_BBT_NO_OOB ? 872 NULL : &buf[len]); 873 if (res < 0) { 874 pr_warn("nand_bbt: error while writing BBT block %d\n", 875 res); 876 mark_bbt_block_bad(this, td, chip, block); 877 continue; 878 } 879 880 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n", 881 (unsigned long long)to, td->version[chip]); 882 883 /* Mark it as used */ 884 td->pages[chip++] = page; 885 } 886 return 0; 887 888 outerr: 889 pr_warn("nand_bbt: error while writing bad block table %d\n", res); 890 return res; 891 } 892 893 /** 894 * nand_memory_bbt - [GENERIC] create a memory based bad block table 895 * @this: NAND chip object 896 * @bd: descriptor for the good/bad block search pattern 897 * 898 * The function creates a memory based bbt by scanning the device for 899 * manufacturer / software marked good / bad blocks. 900 */ 901 static inline int nand_memory_bbt(struct nand_chip *this, 902 struct nand_bbt_descr *bd) 903 { 904 return create_bbt(this, this->data_buf, bd, -1); 905 } 906 907 /** 908 * check_create - [GENERIC] create and write bbt(s) if necessary 909 * @this: the NAND device 910 * @buf: temporary buffer 911 * @bd: descriptor for the good/bad block search pattern 912 * 913 * The function checks the results of the previous call to read_bbt and creates 914 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found 915 * for the chip/device. Update is necessary if one of the tables is missing or 916 * the version nr. of one table is less than the other. 917 */ 918 static int check_create(struct nand_chip *this, uint8_t *buf, 919 struct nand_bbt_descr *bd) 920 { 921 int i, chips, writeops, create, chipsel, res, res2; 922 struct nand_bbt_descr *td = this->bbt_td; 923 struct nand_bbt_descr *md = this->bbt_md; 924 struct nand_bbt_descr *rd, *rd2; 925 926 /* Do we have a bbt per chip? */ 927 if (td->options & NAND_BBT_PERCHIP) 928 chips = this->numchips; 929 else 930 chips = 1; 931 932 for (i = 0; i < chips; i++) { 933 writeops = 0; 934 create = 0; 935 rd = NULL; 936 rd2 = NULL; 937 res = res2 = 0; 938 /* Per chip or per device? */ 939 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1; 940 /* Mirrored table available? */ 941 if (md) { 942 if (td->pages[i] == -1 && md->pages[i] == -1) { 943 create = 1; 944 writeops = 0x03; 945 } else if (td->pages[i] == -1) { 946 rd = md; 947 writeops = 0x01; 948 } else if (md->pages[i] == -1) { 949 rd = td; 950 writeops = 0x02; 951 } else if (td->version[i] == md->version[i]) { 952 rd = td; 953 if (!(td->options & NAND_BBT_VERSION)) 954 rd2 = md; 955 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) { 956 rd = td; 957 writeops = 0x02; 958 } else { 959 rd = md; 960 writeops = 0x01; 961 } 962 } else { 963 if (td->pages[i] == -1) { 964 create = 1; 965 writeops = 0x01; 966 } else { 967 rd = td; 968 } 969 } 970 971 if (create) { 972 /* Create the bad block table by scanning the device? */ 973 if (!(td->options & NAND_BBT_CREATE)) 974 continue; 975 976 /* Create the table in memory by scanning the chip(s) */ 977 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY)) 978 create_bbt(this, buf, bd, chipsel); 979 980 td->version[i] = 1; 981 if (md) 982 md->version[i] = 1; 983 } 984 985 /* Read back first? */ 986 if (rd) { 987 res = read_abs_bbt(this, buf, rd, chipsel); 988 if (mtd_is_eccerr(res)) { 989 /* Mark table as invalid */ 990 rd->pages[i] = -1; 991 rd->version[i] = 0; 992 i--; 993 continue; 994 } 995 } 996 /* If they weren't versioned, read both */ 997 if (rd2) { 998 res2 = read_abs_bbt(this, buf, rd2, chipsel); 999 if (mtd_is_eccerr(res2)) { 1000 /* Mark table as invalid */ 1001 rd2->pages[i] = -1; 1002 rd2->version[i] = 0; 1003 i--; 1004 continue; 1005 } 1006 } 1007 1008 /* Scrub the flash table(s)? */ 1009 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2)) 1010 writeops = 0x03; 1011 1012 /* Update version numbers before writing */ 1013 if (md) { 1014 td->version[i] = max(td->version[i], md->version[i]); 1015 md->version[i] = td->version[i]; 1016 } 1017 1018 /* Write the bad block table to the device? */ 1019 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) { 1020 res = write_bbt(this, buf, td, md, chipsel); 1021 if (res < 0) 1022 return res; 1023 } 1024 1025 /* Write the mirror bad block table to the device? */ 1026 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) { 1027 res = write_bbt(this, buf, md, td, chipsel); 1028 if (res < 0) 1029 return res; 1030 } 1031 } 1032 return 0; 1033 } 1034 1035 /** 1036 * nand_update_bbt - update bad block table(s) 1037 * @this: the NAND device 1038 * @offs: the offset of the newly marked block 1039 * 1040 * The function updates the bad block table(s). 1041 */ 1042 static int nand_update_bbt(struct nand_chip *this, loff_t offs) 1043 { 1044 struct mtd_info *mtd = nand_to_mtd(this); 1045 int len, res = 0; 1046 int chip, chipsel; 1047 uint8_t *buf; 1048 struct nand_bbt_descr *td = this->bbt_td; 1049 struct nand_bbt_descr *md = this->bbt_md; 1050 1051 if (!this->bbt || !td) 1052 return -EINVAL; 1053 1054 /* Allocate a temporary buffer for one eraseblock incl. oob */ 1055 len = (1 << this->bbt_erase_shift); 1056 len += (len >> this->page_shift) * mtd->oobsize; 1057 buf = kmalloc(len, GFP_KERNEL); 1058 if (!buf) 1059 return -ENOMEM; 1060 1061 /* Do we have a bbt per chip? */ 1062 if (td->options & NAND_BBT_PERCHIP) { 1063 chip = (int)(offs >> this->chip_shift); 1064 chipsel = chip; 1065 } else { 1066 chip = 0; 1067 chipsel = -1; 1068 } 1069 1070 td->version[chip]++; 1071 if (md) 1072 md->version[chip]++; 1073 1074 /* Write the bad block table to the device? */ 1075 if (td->options & NAND_BBT_WRITE) { 1076 res = write_bbt(this, buf, td, md, chipsel); 1077 if (res < 0) 1078 goto out; 1079 } 1080 /* Write the mirror bad block table to the device? */ 1081 if (md && (md->options & NAND_BBT_WRITE)) { 1082 res = write_bbt(this, buf, md, td, chipsel); 1083 } 1084 1085 out: 1086 kfree(buf); 1087 return res; 1088 } 1089 1090 /** 1091 * mark_bbt_regions - [GENERIC] mark the bad block table regions 1092 * @this: the NAND device 1093 * @td: bad block table descriptor 1094 * 1095 * The bad block table regions are marked as "bad" to prevent accidental 1096 * erasures / writes. The regions are identified by the mark 0x02. 1097 */ 1098 static void mark_bbt_region(struct nand_chip *this, struct nand_bbt_descr *td) 1099 { 1100 struct mtd_info *mtd = nand_to_mtd(this); 1101 int i, j, chips, block, nrblocks, update; 1102 uint8_t oldval; 1103 1104 /* Do we have a bbt per chip? */ 1105 if (td->options & NAND_BBT_PERCHIP) { 1106 chips = this->numchips; 1107 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift); 1108 } else { 1109 chips = 1; 1110 nrblocks = (int)(mtd->size >> this->bbt_erase_shift); 1111 } 1112 1113 for (i = 0; i < chips; i++) { 1114 if ((td->options & NAND_BBT_ABSPAGE) || 1115 !(td->options & NAND_BBT_WRITE)) { 1116 if (td->pages[i] == -1) 1117 continue; 1118 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift); 1119 oldval = bbt_get_entry(this, block); 1120 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED); 1121 if ((oldval != BBT_BLOCK_RESERVED) && 1122 td->reserved_block_code) 1123 nand_update_bbt(this, (loff_t)block << 1124 this->bbt_erase_shift); 1125 continue; 1126 } 1127 update = 0; 1128 if (td->options & NAND_BBT_LASTBLOCK) 1129 block = ((i + 1) * nrblocks) - td->maxblocks; 1130 else 1131 block = i * nrblocks; 1132 for (j = 0; j < td->maxblocks; j++) { 1133 oldval = bbt_get_entry(this, block); 1134 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED); 1135 if (oldval != BBT_BLOCK_RESERVED) 1136 update = 1; 1137 block++; 1138 } 1139 /* 1140 * If we want reserved blocks to be recorded to flash, and some 1141 * new ones have been marked, then we need to update the stored 1142 * bbts. This should only happen once. 1143 */ 1144 if (update && td->reserved_block_code) 1145 nand_update_bbt(this, (loff_t)(block - 1) << 1146 this->bbt_erase_shift); 1147 } 1148 } 1149 1150 /** 1151 * verify_bbt_descr - verify the bad block description 1152 * @this: the NAND device 1153 * @bd: the table to verify 1154 * 1155 * This functions performs a few sanity checks on the bad block description 1156 * table. 1157 */ 1158 static void verify_bbt_descr(struct nand_chip *this, struct nand_bbt_descr *bd) 1159 { 1160 struct mtd_info *mtd = nand_to_mtd(this); 1161 u32 pattern_len; 1162 u32 bits; 1163 u32 table_size; 1164 1165 if (!bd) 1166 return; 1167 1168 pattern_len = bd->len; 1169 bits = bd->options & NAND_BBT_NRBITS_MSK; 1170 1171 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) && 1172 !(this->bbt_options & NAND_BBT_USE_FLASH)); 1173 BUG_ON(!bits); 1174 1175 if (bd->options & NAND_BBT_VERSION) 1176 pattern_len++; 1177 1178 if (bd->options & NAND_BBT_NO_OOB) { 1179 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH)); 1180 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB)); 1181 BUG_ON(bd->offs); 1182 if (bd->options & NAND_BBT_VERSION) 1183 BUG_ON(bd->veroffs != bd->len); 1184 BUG_ON(bd->options & NAND_BBT_SAVECONTENT); 1185 } 1186 1187 if (bd->options & NAND_BBT_PERCHIP) 1188 table_size = this->chipsize >> this->bbt_erase_shift; 1189 else 1190 table_size = mtd->size >> this->bbt_erase_shift; 1191 table_size >>= 3; 1192 table_size *= bits; 1193 if (bd->options & NAND_BBT_NO_OOB) 1194 table_size += pattern_len; 1195 BUG_ON(table_size > (1 << this->bbt_erase_shift)); 1196 } 1197 1198 /** 1199 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s) 1200 * @this: the NAND device 1201 * @bd: descriptor for the good/bad block search pattern 1202 * 1203 * The function checks, if a bad block table(s) is/are already available. If 1204 * not it scans the device for manufacturer marked good / bad blocks and writes 1205 * the bad block table(s) to the selected place. 1206 * 1207 * The bad block table memory is allocated here. It must be freed by calling 1208 * the nand_free_bbt function. 1209 */ 1210 static int nand_scan_bbt(struct nand_chip *this, struct nand_bbt_descr *bd) 1211 { 1212 struct mtd_info *mtd = nand_to_mtd(this); 1213 int len, res; 1214 uint8_t *buf; 1215 struct nand_bbt_descr *td = this->bbt_td; 1216 struct nand_bbt_descr *md = this->bbt_md; 1217 1218 len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1; 1219 /* 1220 * Allocate memory (2bit per block) and clear the memory bad block 1221 * table. 1222 */ 1223 this->bbt = kzalloc(len, GFP_KERNEL); 1224 if (!this->bbt) 1225 return -ENOMEM; 1226 1227 /* 1228 * If no primary table decriptor is given, scan the device to build a 1229 * memory based bad block table. 1230 */ 1231 if (!td) { 1232 if ((res = nand_memory_bbt(this, bd))) { 1233 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n"); 1234 goto err; 1235 } 1236 return 0; 1237 } 1238 verify_bbt_descr(this, td); 1239 verify_bbt_descr(this, md); 1240 1241 /* Allocate a temporary buffer for one eraseblock incl. oob */ 1242 len = (1 << this->bbt_erase_shift); 1243 len += (len >> this->page_shift) * mtd->oobsize; 1244 buf = vmalloc(len); 1245 if (!buf) { 1246 res = -ENOMEM; 1247 goto err; 1248 } 1249 1250 /* Is the bbt at a given page? */ 1251 if (td->options & NAND_BBT_ABSPAGE) { 1252 read_abs_bbts(this, buf, td, md); 1253 } else { 1254 /* Search the bad block table using a pattern in oob */ 1255 search_read_bbts(this, buf, td, md); 1256 } 1257 1258 res = check_create(this, buf, bd); 1259 if (res) 1260 goto err; 1261 1262 /* Prevent the bbt regions from erasing / writing */ 1263 mark_bbt_region(this, td); 1264 if (md) 1265 mark_bbt_region(this, md); 1266 1267 vfree(buf); 1268 return 0; 1269 1270 err: 1271 kfree(this->bbt); 1272 this->bbt = NULL; 1273 return res; 1274 } 1275 1276 /* 1277 * Define some generic bad / good block scan pattern which are used 1278 * while scanning a device for factory marked good / bad blocks. 1279 */ 1280 static uint8_t scan_ff_pattern[] = { 0xff, 0xff }; 1281 1282 /* Generic flash bbt descriptors */ 1283 static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' }; 1284 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' }; 1285 1286 static struct nand_bbt_descr bbt_main_descr = { 1287 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE 1288 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, 1289 .offs = 8, 1290 .len = 4, 1291 .veroffs = 12, 1292 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS, 1293 .pattern = bbt_pattern 1294 }; 1295 1296 static struct nand_bbt_descr bbt_mirror_descr = { 1297 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE 1298 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, 1299 .offs = 8, 1300 .len = 4, 1301 .veroffs = 12, 1302 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS, 1303 .pattern = mirror_pattern 1304 }; 1305 1306 static struct nand_bbt_descr bbt_main_no_oob_descr = { 1307 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE 1308 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP 1309 | NAND_BBT_NO_OOB, 1310 .len = 4, 1311 .veroffs = 4, 1312 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS, 1313 .pattern = bbt_pattern 1314 }; 1315 1316 static struct nand_bbt_descr bbt_mirror_no_oob_descr = { 1317 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE 1318 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP 1319 | NAND_BBT_NO_OOB, 1320 .len = 4, 1321 .veroffs = 4, 1322 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS, 1323 .pattern = mirror_pattern 1324 }; 1325 1326 #define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB) 1327 /** 1328 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure 1329 * @this: NAND chip to create descriptor for 1330 * 1331 * This function allocates and initializes a nand_bbt_descr for BBM detection 1332 * based on the properties of @this. The new descriptor is stored in 1333 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when 1334 * passed to this function. 1335 */ 1336 static int nand_create_badblock_pattern(struct nand_chip *this) 1337 { 1338 struct nand_bbt_descr *bd; 1339 if (this->badblock_pattern) { 1340 pr_warn("Bad block pattern already allocated; not replacing\n"); 1341 return -EINVAL; 1342 } 1343 bd = kzalloc(sizeof(*bd), GFP_KERNEL); 1344 if (!bd) 1345 return -ENOMEM; 1346 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK; 1347 bd->offs = this->badblockpos; 1348 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1; 1349 bd->pattern = scan_ff_pattern; 1350 bd->options |= NAND_BBT_DYNAMICSTRUCT; 1351 this->badblock_pattern = bd; 1352 return 0; 1353 } 1354 1355 /** 1356 * nand_create_bbt - [NAND Interface] Select a default bad block table for the device 1357 * @this: NAND chip object 1358 * 1359 * This function selects the default bad block table support for the device and 1360 * calls the nand_scan_bbt function. 1361 */ 1362 int nand_create_bbt(struct nand_chip *this) 1363 { 1364 int ret; 1365 1366 /* Is a flash based bad block table requested? */ 1367 if (this->bbt_options & NAND_BBT_USE_FLASH) { 1368 /* Use the default pattern descriptors */ 1369 if (!this->bbt_td) { 1370 if (this->bbt_options & NAND_BBT_NO_OOB) { 1371 this->bbt_td = &bbt_main_no_oob_descr; 1372 this->bbt_md = &bbt_mirror_no_oob_descr; 1373 } else { 1374 this->bbt_td = &bbt_main_descr; 1375 this->bbt_md = &bbt_mirror_descr; 1376 } 1377 } 1378 } else { 1379 this->bbt_td = NULL; 1380 this->bbt_md = NULL; 1381 } 1382 1383 if (!this->badblock_pattern) { 1384 ret = nand_create_badblock_pattern(this); 1385 if (ret) 1386 return ret; 1387 } 1388 1389 return nand_scan_bbt(this, this->badblock_pattern); 1390 } 1391 EXPORT_SYMBOL(nand_create_bbt); 1392 1393 /** 1394 * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved 1395 * @this: NAND chip object 1396 * @offs: offset in the device 1397 */ 1398 int nand_isreserved_bbt(struct nand_chip *this, loff_t offs) 1399 { 1400 int block; 1401 1402 block = (int)(offs >> this->bbt_erase_shift); 1403 return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED; 1404 } 1405 1406 /** 1407 * nand_isbad_bbt - [NAND Interface] Check if a block is bad 1408 * @this: NAND chip object 1409 * @offs: offset in the device 1410 * @allowbbt: allow access to bad block table region 1411 */ 1412 int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt) 1413 { 1414 int block, res; 1415 1416 block = (int)(offs >> this->bbt_erase_shift); 1417 res = bbt_get_entry(this, block); 1418 1419 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n", 1420 (unsigned int)offs, block, res); 1421 1422 switch (res) { 1423 case BBT_BLOCK_GOOD: 1424 return 0; 1425 case BBT_BLOCK_WORN: 1426 return 1; 1427 case BBT_BLOCK_RESERVED: 1428 return allowbbt ? 0 : 1; 1429 } 1430 return 1; 1431 } 1432 1433 /** 1434 * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT 1435 * @this: NAND chip object 1436 * @offs: offset of the bad block 1437 */ 1438 int nand_markbad_bbt(struct nand_chip *this, loff_t offs) 1439 { 1440 int block, ret = 0; 1441 1442 block = (int)(offs >> this->bbt_erase_shift); 1443 1444 /* Mark bad block in memory */ 1445 bbt_mark_entry(this, block, BBT_BLOCK_WORN); 1446 1447 /* Update flash-based bad block table */ 1448 if (this->bbt_options & NAND_BBT_USE_FLASH) 1449 ret = nand_update_bbt(this, offs); 1450 1451 return ret; 1452 } 1453