0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056 #include <linux/slab.h>
0057 #include <linux/types.h>
0058 #include <linux/mtd/mtd.h>
0059 #include <linux/mtd/bbm.h>
0060 #include <linux/bitops.h>
0061 #include <linux/delay.h>
0062 #include <linux/vmalloc.h>
0063 #include <linux/export.h>
0064 #include <linux/string.h>
0065
0066 #include "internals.h"
0067
0068 #define BBT_BLOCK_GOOD 0x00
0069 #define BBT_BLOCK_WORN 0x01
0070 #define BBT_BLOCK_RESERVED 0x02
0071 #define BBT_BLOCK_FACTORY_BAD 0x03
0072
0073 #define BBT_ENTRY_MASK 0x03
0074 #define BBT_ENTRY_SHIFT 2
0075
0076 static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
0077 {
0078 uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
0079 entry >>= (block & BBT_ENTRY_MASK) * 2;
0080 return entry & BBT_ENTRY_MASK;
0081 }
0082
0083 static inline void bbt_mark_entry(struct nand_chip *chip, int block,
0084 uint8_t mark)
0085 {
0086 uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
0087 chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
0088 }
0089
0090 static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
0091 {
0092 if (memcmp(buf, td->pattern, td->len))
0093 return -1;
0094 return 0;
0095 }
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107 static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
0108 {
0109 if (td->options & NAND_BBT_NO_OOB)
0110 return check_pattern_no_oob(buf, td);
0111
0112
0113 if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
0114 return -1;
0115
0116 return 0;
0117 }
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128 static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
0129 {
0130
0131 if (memcmp(buf + td->offs, td->pattern, td->len))
0132 return -1;
0133 return 0;
0134 }
0135
0136
0137
0138
0139
0140
0141
0142 static u32 add_marker_len(struct nand_bbt_descr *td)
0143 {
0144 u32 len;
0145
0146 if (!(td->options & NAND_BBT_NO_OOB))
0147 return 0;
0148
0149 len = td->len;
0150 if (td->options & NAND_BBT_VERSION)
0151 len++;
0152 return len;
0153 }
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166 static int read_bbt(struct nand_chip *this, uint8_t *buf, int page, int num,
0167 struct nand_bbt_descr *td, int offs)
0168 {
0169 struct mtd_info *mtd = nand_to_mtd(this);
0170 int res, ret = 0, i, j, act = 0;
0171 size_t retlen, len, totlen;
0172 loff_t from;
0173 int bits = td->options & NAND_BBT_NRBITS_MSK;
0174 uint8_t msk = (uint8_t)((1 << bits) - 1);
0175 u32 marker_len;
0176 int reserved_block_code = td->reserved_block_code;
0177
0178 totlen = (num * bits) >> 3;
0179 marker_len = add_marker_len(td);
0180 from = ((loff_t)page) << this->page_shift;
0181
0182 while (totlen) {
0183 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
0184 if (marker_len) {
0185
0186
0187
0188
0189 len -= marker_len;
0190 from += marker_len;
0191 marker_len = 0;
0192 }
0193 res = mtd_read(mtd, from, len, &retlen, buf);
0194 if (res < 0) {
0195 if (mtd_is_eccerr(res)) {
0196 pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
0197 from & ~mtd->writesize);
0198 return res;
0199 } else if (mtd_is_bitflip(res)) {
0200 pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
0201 from & ~mtd->writesize);
0202 ret = res;
0203 } else {
0204 pr_info("nand_bbt: error reading BBT\n");
0205 return res;
0206 }
0207 }
0208
0209
0210 for (i = 0; i < len; i++) {
0211 uint8_t dat = buf[i];
0212 for (j = 0; j < 8; j += bits, act++) {
0213 uint8_t tmp = (dat >> j) & msk;
0214 if (tmp == msk)
0215 continue;
0216 if (reserved_block_code && (tmp == reserved_block_code)) {
0217 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
0218 (loff_t)(offs + act) <<
0219 this->bbt_erase_shift);
0220 bbt_mark_entry(this, offs + act,
0221 BBT_BLOCK_RESERVED);
0222 mtd->ecc_stats.bbtblocks++;
0223 continue;
0224 }
0225
0226
0227
0228
0229 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
0230 (loff_t)(offs + act) <<
0231 this->bbt_erase_shift);
0232
0233 if (tmp == 0)
0234 bbt_mark_entry(this, offs + act,
0235 BBT_BLOCK_FACTORY_BAD);
0236 else
0237 bbt_mark_entry(this, offs + act,
0238 BBT_BLOCK_WORN);
0239 mtd->ecc_stats.badblocks++;
0240 }
0241 }
0242 totlen -= len;
0243 from += len;
0244 }
0245 return ret;
0246 }
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259 static int read_abs_bbt(struct nand_chip *this, uint8_t *buf,
0260 struct nand_bbt_descr *td, int chip)
0261 {
0262 struct mtd_info *mtd = nand_to_mtd(this);
0263 u64 targetsize = nanddev_target_size(&this->base);
0264 int res = 0, i;
0265
0266 if (td->options & NAND_BBT_PERCHIP) {
0267 int offs = 0;
0268 for (i = 0; i < nanddev_ntargets(&this->base); i++) {
0269 if (chip == -1 || chip == i)
0270 res = read_bbt(this, buf, td->pages[i],
0271 targetsize >> this->bbt_erase_shift,
0272 td, offs);
0273 if (res)
0274 return res;
0275 offs += targetsize >> this->bbt_erase_shift;
0276 }
0277 } else {
0278 res = read_bbt(this, buf, td->pages[0],
0279 mtd->size >> this->bbt_erase_shift, td, 0);
0280 if (res)
0281 return res;
0282 }
0283 return 0;
0284 }
0285
0286
0287 static int scan_read_data(struct nand_chip *this, uint8_t *buf, loff_t offs,
0288 struct nand_bbt_descr *td)
0289 {
0290 struct mtd_info *mtd = nand_to_mtd(this);
0291 size_t retlen;
0292 size_t len;
0293
0294 len = td->len;
0295 if (td->options & NAND_BBT_VERSION)
0296 len++;
0297
0298 return mtd_read(mtd, offs, len, &retlen, buf);
0299 }
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312 static int scan_read_oob(struct nand_chip *this, uint8_t *buf, loff_t offs,
0313 size_t len)
0314 {
0315 struct mtd_info *mtd = nand_to_mtd(this);
0316 struct mtd_oob_ops ops;
0317 int res, ret = 0;
0318
0319 ops.mode = MTD_OPS_PLACE_OOB;
0320 ops.ooboffs = 0;
0321 ops.ooblen = mtd->oobsize;
0322
0323 while (len > 0) {
0324 ops.datbuf = buf;
0325 ops.len = min(len, (size_t)mtd->writesize);
0326 ops.oobbuf = buf + ops.len;
0327
0328 res = mtd_read_oob(mtd, offs, &ops);
0329 if (res) {
0330 if (!mtd_is_bitflip_or_eccerr(res))
0331 return res;
0332 else if (mtd_is_eccerr(res) || !ret)
0333 ret = res;
0334 }
0335
0336 buf += mtd->oobsize + mtd->writesize;
0337 len -= mtd->writesize;
0338 offs += mtd->writesize;
0339 }
0340 return ret;
0341 }
0342
0343 static int scan_read(struct nand_chip *this, uint8_t *buf, loff_t offs,
0344 size_t len, struct nand_bbt_descr *td)
0345 {
0346 if (td->options & NAND_BBT_NO_OOB)
0347 return scan_read_data(this, buf, offs, td);
0348 else
0349 return scan_read_oob(this, buf, offs, len);
0350 }
0351
0352
0353 static int scan_write_bbt(struct nand_chip *this, loff_t offs, size_t len,
0354 uint8_t *buf, uint8_t *oob)
0355 {
0356 struct mtd_info *mtd = nand_to_mtd(this);
0357 struct mtd_oob_ops ops;
0358
0359 ops.mode = MTD_OPS_PLACE_OOB;
0360 ops.ooboffs = 0;
0361 ops.ooblen = mtd->oobsize;
0362 ops.datbuf = buf;
0363 ops.oobbuf = oob;
0364 ops.len = len;
0365
0366 return mtd_write_oob(mtd, offs, &ops);
0367 }
0368
0369 static u32 bbt_get_ver_offs(struct nand_chip *this, struct nand_bbt_descr *td)
0370 {
0371 struct mtd_info *mtd = nand_to_mtd(this);
0372 u32 ver_offs = td->veroffs;
0373
0374 if (!(td->options & NAND_BBT_NO_OOB))
0375 ver_offs += mtd->writesize;
0376 return ver_offs;
0377 }
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389 static void read_abs_bbts(struct nand_chip *this, uint8_t *buf,
0390 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
0391 {
0392 struct mtd_info *mtd = nand_to_mtd(this);
0393
0394
0395 if (td->options & NAND_BBT_VERSION) {
0396 scan_read(this, buf, (loff_t)td->pages[0] << this->page_shift,
0397 mtd->writesize, td);
0398 td->version[0] = buf[bbt_get_ver_offs(this, td)];
0399 pr_info("Bad block table at page %d, version 0x%02X\n",
0400 td->pages[0], td->version[0]);
0401 }
0402
0403
0404 if (md && (md->options & NAND_BBT_VERSION)) {
0405 scan_read(this, buf, (loff_t)md->pages[0] << this->page_shift,
0406 mtd->writesize, md);
0407 md->version[0] = buf[bbt_get_ver_offs(this, md)];
0408 pr_info("Bad block table at page %d, version 0x%02X\n",
0409 md->pages[0], md->version[0]);
0410 }
0411 }
0412
0413
0414 static int scan_block_fast(struct nand_chip *this, struct nand_bbt_descr *bd,
0415 loff_t offs, uint8_t *buf)
0416 {
0417 struct mtd_info *mtd = nand_to_mtd(this);
0418
0419 struct mtd_oob_ops ops;
0420 int ret, page_offset;
0421
0422 ops.ooblen = mtd->oobsize;
0423 ops.oobbuf = buf;
0424 ops.ooboffs = 0;
0425 ops.datbuf = NULL;
0426 ops.mode = MTD_OPS_PLACE_OOB;
0427
0428 page_offset = nand_bbm_get_next_page(this, 0);
0429
0430 while (page_offset >= 0) {
0431
0432
0433
0434
0435 ret = mtd_read_oob(mtd, offs + (page_offset * mtd->writesize),
0436 &ops);
0437
0438 if (ret && !mtd_is_bitflip_or_eccerr(ret))
0439 return ret;
0440
0441 if (check_short_pattern(buf, bd))
0442 return 1;
0443
0444 page_offset = nand_bbm_get_next_page(this, page_offset + 1);
0445 }
0446
0447 return 0;
0448 }
0449
0450
0451 static int bbt_block_checkbad(struct nand_chip *this, struct nand_bbt_descr *td,
0452 loff_t offs, uint8_t *buf)
0453 {
0454 struct nand_bbt_descr *bd = this->badblock_pattern;
0455
0456
0457
0458
0459
0460
0461 if (!(td->options & NAND_BBT_NO_OOB) &&
0462 td->offs >= bd->offs && td->offs < bd->offs + bd->len)
0463 return 0;
0464
0465
0466
0467
0468
0469 if (this->bbt_options & NAND_BBT_NO_OOB_BBM ||
0470 this->options & NAND_NO_BBM_QUIRK)
0471 return 0;
0472
0473 if (scan_block_fast(this, bd, offs, buf) > 0)
0474 return 1;
0475
0476 return 0;
0477 }
0478
0479
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490 static int create_bbt(struct nand_chip *this, uint8_t *buf,
0491 struct nand_bbt_descr *bd, int chip)
0492 {
0493 u64 targetsize = nanddev_target_size(&this->base);
0494 struct mtd_info *mtd = nand_to_mtd(this);
0495 int i, numblocks, startblock;
0496 loff_t from;
0497
0498 pr_info("Scanning device for bad blocks\n");
0499
0500 if (chip == -1) {
0501 numblocks = mtd->size >> this->bbt_erase_shift;
0502 startblock = 0;
0503 from = 0;
0504 } else {
0505 if (chip >= nanddev_ntargets(&this->base)) {
0506 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
0507 chip + 1, nanddev_ntargets(&this->base));
0508 return -EINVAL;
0509 }
0510 numblocks = targetsize >> this->bbt_erase_shift;
0511 startblock = chip * numblocks;
0512 numblocks += startblock;
0513 from = (loff_t)startblock << this->bbt_erase_shift;
0514 }
0515
0516 for (i = startblock; i < numblocks; i++) {
0517 int ret;
0518
0519 BUG_ON(bd->options & NAND_BBT_NO_OOB);
0520
0521 ret = scan_block_fast(this, bd, from, buf);
0522 if (ret < 0)
0523 return ret;
0524
0525 if (ret) {
0526 bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
0527 pr_warn("Bad eraseblock %d at 0x%012llx\n",
0528 i, (unsigned long long)from);
0529 mtd->ecc_stats.badblocks++;
0530 }
0531
0532 from += (1 << this->bbt_erase_shift);
0533 }
0534 return 0;
0535 }
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552 static int search_bbt(struct nand_chip *this, uint8_t *buf,
0553 struct nand_bbt_descr *td)
0554 {
0555 u64 targetsize = nanddev_target_size(&this->base);
0556 struct mtd_info *mtd = nand_to_mtd(this);
0557 int i, chips;
0558 int startblock, block, dir;
0559 int scanlen = mtd->writesize + mtd->oobsize;
0560 int bbtblocks;
0561 int blocktopage = this->bbt_erase_shift - this->page_shift;
0562
0563
0564 if (td->options & NAND_BBT_LASTBLOCK) {
0565 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
0566 dir = -1;
0567 } else {
0568 startblock = 0;
0569 dir = 1;
0570 }
0571
0572
0573 if (td->options & NAND_BBT_PERCHIP) {
0574 chips = nanddev_ntargets(&this->base);
0575 bbtblocks = targetsize >> this->bbt_erase_shift;
0576 startblock &= bbtblocks - 1;
0577 } else {
0578 chips = 1;
0579 bbtblocks = mtd->size >> this->bbt_erase_shift;
0580 }
0581
0582 for (i = 0; i < chips; i++) {
0583
0584 td->version[i] = 0;
0585 td->pages[i] = -1;
0586
0587 for (block = 0; block < td->maxblocks; block++) {
0588
0589 int actblock = startblock + dir * block;
0590 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
0591
0592
0593 if (bbt_block_checkbad(this, td, offs, buf))
0594 continue;
0595
0596
0597 scan_read(this, buf, offs, mtd->writesize, td);
0598 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
0599 td->pages[i] = actblock << blocktopage;
0600 if (td->options & NAND_BBT_VERSION) {
0601 offs = bbt_get_ver_offs(this, td);
0602 td->version[i] = buf[offs];
0603 }
0604 break;
0605 }
0606 }
0607 startblock += targetsize >> this->bbt_erase_shift;
0608 }
0609
0610 for (i = 0; i < chips; i++) {
0611 if (td->pages[i] == -1)
0612 pr_warn("Bad block table not found for chip %d\n", i);
0613 else
0614 pr_info("Bad block table found at page %d, version 0x%02X\n",
0615 td->pages[i], td->version[i]);
0616 }
0617 return 0;
0618 }
0619
0620
0621
0622
0623
0624
0625
0626
0627
0628
0629 static void search_read_bbts(struct nand_chip *this, uint8_t *buf,
0630 struct nand_bbt_descr *td,
0631 struct nand_bbt_descr *md)
0632 {
0633
0634 search_bbt(this, buf, td);
0635
0636
0637 if (md)
0638 search_bbt(this, buf, md);
0639 }
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654 static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td,
0655 struct nand_bbt_descr *md, int chip)
0656 {
0657 u64 targetsize = nanddev_target_size(&this->base);
0658 int startblock, dir, page, numblocks, i;
0659
0660
0661
0662
0663
0664
0665 if (td->pages[chip] != -1)
0666 return td->pages[chip] >>
0667 (this->bbt_erase_shift - this->page_shift);
0668
0669 numblocks = (int)(targetsize >> this->bbt_erase_shift);
0670 if (!(td->options & NAND_BBT_PERCHIP))
0671 numblocks *= nanddev_ntargets(&this->base);
0672
0673
0674
0675
0676
0677 if (td->options & NAND_BBT_LASTBLOCK) {
0678 startblock = numblocks * (chip + 1) - 1;
0679 dir = -1;
0680 } else {
0681 startblock = chip * numblocks;
0682 dir = 1;
0683 }
0684
0685 for (i = 0; i < td->maxblocks; i++) {
0686 int block = startblock + dir * i;
0687
0688
0689 switch (bbt_get_entry(this, block)) {
0690 case BBT_BLOCK_WORN:
0691 case BBT_BLOCK_FACTORY_BAD:
0692 continue;
0693 }
0694
0695 page = block << (this->bbt_erase_shift - this->page_shift);
0696
0697
0698 if (!md || md->pages[chip] != page)
0699 return block;
0700 }
0701
0702 return -ENOSPC;
0703 }
0704
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717 static void mark_bbt_block_bad(struct nand_chip *this,
0718 struct nand_bbt_descr *td,
0719 int chip, int block)
0720 {
0721 loff_t to;
0722 int res;
0723
0724 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
0725
0726 to = (loff_t)block << this->bbt_erase_shift;
0727 res = nand_markbad_bbm(this, to);
0728 if (res)
0729 pr_warn("nand_bbt: error %d while marking block %d bad\n",
0730 res, block);
0731
0732 td->pages[chip] = -1;
0733 }
0734
0735
0736
0737
0738
0739
0740
0741
0742
0743
0744
0745 static int write_bbt(struct nand_chip *this, uint8_t *buf,
0746 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
0747 int chipsel)
0748 {
0749 u64 targetsize = nanddev_target_size(&this->base);
0750 struct mtd_info *mtd = nand_to_mtd(this);
0751 struct erase_info einfo;
0752 int i, res, chip = 0;
0753 int bits, page, offs, numblocks, sft, sftmsk;
0754 int nrchips, pageoffs, ooboffs;
0755 uint8_t msk[4];
0756 uint8_t rcode = td->reserved_block_code;
0757 size_t retlen, len = 0;
0758 loff_t to;
0759 struct mtd_oob_ops ops;
0760
0761 ops.ooblen = mtd->oobsize;
0762 ops.ooboffs = 0;
0763 ops.datbuf = NULL;
0764 ops.mode = MTD_OPS_PLACE_OOB;
0765
0766 if (!rcode)
0767 rcode = 0xff;
0768
0769 if (td->options & NAND_BBT_PERCHIP) {
0770 numblocks = (int)(targetsize >> this->bbt_erase_shift);
0771
0772 if (chipsel == -1) {
0773 nrchips = nanddev_ntargets(&this->base);
0774 } else {
0775 nrchips = chipsel + 1;
0776 chip = chipsel;
0777 }
0778 } else {
0779 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
0780 nrchips = 1;
0781 }
0782
0783
0784 while (chip < nrchips) {
0785 int block;
0786
0787 block = get_bbt_block(this, td, md, chip);
0788 if (block < 0) {
0789 pr_err("No space left to write bad block table\n");
0790 res = block;
0791 goto outerr;
0792 }
0793
0794
0795
0796
0797
0798 page = block << (this->bbt_erase_shift - this->page_shift);
0799
0800
0801 bits = td->options & NAND_BBT_NRBITS_MSK;
0802 msk[2] = ~rcode;
0803 switch (bits) {
0804 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
0805 msk[3] = 0x01;
0806 break;
0807 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
0808 msk[3] = 0x03;
0809 break;
0810 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
0811 msk[3] = 0x0f;
0812 break;
0813 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
0814 msk[3] = 0xff;
0815 break;
0816 default: return -EINVAL;
0817 }
0818
0819 to = ((loff_t)page) << this->page_shift;
0820
0821
0822 if (td->options & NAND_BBT_SAVECONTENT) {
0823
0824 to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
0825 len = 1 << this->bbt_erase_shift;
0826 res = mtd_read(mtd, to, len, &retlen, buf);
0827 if (res < 0) {
0828 if (retlen != len) {
0829 pr_info("nand_bbt: error reading block for writing the bad block table\n");
0830 return res;
0831 }
0832 pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
0833 }
0834
0835 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
0836 ops.oobbuf = &buf[len];
0837 res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
0838 if (res < 0 || ops.oobretlen != ops.ooblen)
0839 goto outerr;
0840
0841
0842 pageoffs = page - (int)(to >> this->page_shift);
0843 offs = pageoffs << this->page_shift;
0844
0845 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
0846 ooboffs = len + (pageoffs * mtd->oobsize);
0847
0848 } else if (td->options & NAND_BBT_NO_OOB) {
0849 ooboffs = 0;
0850 offs = td->len;
0851
0852 if (td->options & NAND_BBT_VERSION)
0853 offs++;
0854
0855 len = (size_t)(numblocks >> sft);
0856 len += offs;
0857
0858 len = ALIGN(len, mtd->writesize);
0859
0860 memset(buf, 0xff, len);
0861
0862 memcpy(buf, td->pattern, td->len);
0863 } else {
0864
0865 len = (size_t)(numblocks >> sft);
0866
0867 len = ALIGN(len, mtd->writesize);
0868
0869 memset(buf, 0xff, len +
0870 (len >> this->page_shift)* mtd->oobsize);
0871 offs = 0;
0872 ooboffs = len;
0873
0874 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
0875 }
0876
0877 if (td->options & NAND_BBT_VERSION)
0878 buf[ooboffs + td->veroffs] = td->version[chip];
0879
0880
0881 for (i = 0; i < numblocks; i++) {
0882 uint8_t dat;
0883 int sftcnt = (i << (3 - sft)) & sftmsk;
0884 dat = bbt_get_entry(this, chip * numblocks + i);
0885
0886 buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
0887 }
0888
0889 memset(&einfo, 0, sizeof(einfo));
0890 einfo.addr = to;
0891 einfo.len = 1 << this->bbt_erase_shift;
0892 res = nand_erase_nand(this, &einfo, 1);
0893 if (res < 0) {
0894 pr_warn("nand_bbt: error while erasing BBT block %d\n",
0895 res);
0896 mark_bbt_block_bad(this, td, chip, block);
0897 continue;
0898 }
0899
0900 res = scan_write_bbt(this, to, len, buf,
0901 td->options & NAND_BBT_NO_OOB ?
0902 NULL : &buf[len]);
0903 if (res < 0) {
0904 pr_warn("nand_bbt: error while writing BBT block %d\n",
0905 res);
0906 mark_bbt_block_bad(this, td, chip, block);
0907 continue;
0908 }
0909
0910 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
0911 (unsigned long long)to, td->version[chip]);
0912
0913
0914 td->pages[chip++] = page;
0915 }
0916 return 0;
0917
0918 outerr:
0919 pr_warn("nand_bbt: error while writing bad block table %d\n", res);
0920 return res;
0921 }
0922
0923
0924
0925
0926
0927
0928
0929
0930
0931 static inline int nand_memory_bbt(struct nand_chip *this,
0932 struct nand_bbt_descr *bd)
0933 {
0934 u8 *pagebuf = nand_get_data_buf(this);
0935
0936 return create_bbt(this, pagebuf, bd, -1);
0937 }
0938
0939
0940
0941
0942
0943
0944
0945
0946
0947
0948
0949
0950 static int check_create(struct nand_chip *this, uint8_t *buf,
0951 struct nand_bbt_descr *bd)
0952 {
0953 int i, chips, writeops, create, chipsel, res, res2;
0954 struct nand_bbt_descr *td = this->bbt_td;
0955 struct nand_bbt_descr *md = this->bbt_md;
0956 struct nand_bbt_descr *rd, *rd2;
0957
0958
0959 if (td->options & NAND_BBT_PERCHIP)
0960 chips = nanddev_ntargets(&this->base);
0961 else
0962 chips = 1;
0963
0964 for (i = 0; i < chips; i++) {
0965 writeops = 0;
0966 create = 0;
0967 rd = NULL;
0968 rd2 = NULL;
0969 res = res2 = 0;
0970
0971 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
0972
0973 if (md) {
0974 if (td->pages[i] == -1 && md->pages[i] == -1) {
0975 create = 1;
0976 writeops = 0x03;
0977 } else if (td->pages[i] == -1) {
0978 rd = md;
0979 writeops = 0x01;
0980 } else if (md->pages[i] == -1) {
0981 rd = td;
0982 writeops = 0x02;
0983 } else if (td->version[i] == md->version[i]) {
0984 rd = td;
0985 if (!(td->options & NAND_BBT_VERSION))
0986 rd2 = md;
0987 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
0988 rd = td;
0989 writeops = 0x02;
0990 } else {
0991 rd = md;
0992 writeops = 0x01;
0993 }
0994 } else {
0995 if (td->pages[i] == -1) {
0996 create = 1;
0997 writeops = 0x01;
0998 } else {
0999 rd = td;
1000 }
1001 }
1002
1003 if (create) {
1004
1005 if (!(td->options & NAND_BBT_CREATE))
1006 continue;
1007
1008
1009 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
1010 create_bbt(this, buf, bd, chipsel);
1011
1012 td->version[i] = 1;
1013 if (md)
1014 md->version[i] = 1;
1015 }
1016
1017
1018 if (rd) {
1019 res = read_abs_bbt(this, buf, rd, chipsel);
1020 if (mtd_is_eccerr(res)) {
1021
1022 rd->pages[i] = -1;
1023 rd->version[i] = 0;
1024 i--;
1025 continue;
1026 }
1027 }
1028
1029 if (rd2) {
1030 res2 = read_abs_bbt(this, buf, rd2, chipsel);
1031 if (mtd_is_eccerr(res2)) {
1032
1033 rd2->pages[i] = -1;
1034 rd2->version[i] = 0;
1035 i--;
1036 continue;
1037 }
1038 }
1039
1040
1041 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
1042 writeops = 0x03;
1043
1044
1045 if (md) {
1046 td->version[i] = max(td->version[i], md->version[i]);
1047 md->version[i] = td->version[i];
1048 }
1049
1050
1051 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1052 res = write_bbt(this, buf, td, md, chipsel);
1053 if (res < 0)
1054 return res;
1055 }
1056
1057
1058 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1059 res = write_bbt(this, buf, md, td, chipsel);
1060 if (res < 0)
1061 return res;
1062 }
1063 }
1064 return 0;
1065 }
1066
1067
1068
1069
1070
1071
1072
1073
1074 static int nand_update_bbt(struct nand_chip *this, loff_t offs)
1075 {
1076 struct mtd_info *mtd = nand_to_mtd(this);
1077 int len, res = 0;
1078 int chip, chipsel;
1079 uint8_t *buf;
1080 struct nand_bbt_descr *td = this->bbt_td;
1081 struct nand_bbt_descr *md = this->bbt_md;
1082
1083 if (!this->bbt || !td)
1084 return -EINVAL;
1085
1086
1087 len = (1 << this->bbt_erase_shift);
1088 len += (len >> this->page_shift) * mtd->oobsize;
1089 buf = kmalloc(len, GFP_KERNEL);
1090 if (!buf)
1091 return -ENOMEM;
1092
1093
1094 if (td->options & NAND_BBT_PERCHIP) {
1095 chip = (int)(offs >> this->chip_shift);
1096 chipsel = chip;
1097 } else {
1098 chip = 0;
1099 chipsel = -1;
1100 }
1101
1102 td->version[chip]++;
1103 if (md)
1104 md->version[chip]++;
1105
1106
1107 if (td->options & NAND_BBT_WRITE) {
1108 res = write_bbt(this, buf, td, md, chipsel);
1109 if (res < 0)
1110 goto out;
1111 }
1112
1113 if (md && (md->options & NAND_BBT_WRITE)) {
1114 res = write_bbt(this, buf, md, td, chipsel);
1115 }
1116
1117 out:
1118 kfree(buf);
1119 return res;
1120 }
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130 static void mark_bbt_region(struct nand_chip *this, struct nand_bbt_descr *td)
1131 {
1132 u64 targetsize = nanddev_target_size(&this->base);
1133 struct mtd_info *mtd = nand_to_mtd(this);
1134 int i, j, chips, block, nrblocks, update;
1135 uint8_t oldval;
1136
1137
1138 if (td->options & NAND_BBT_PERCHIP) {
1139 chips = nanddev_ntargets(&this->base);
1140 nrblocks = (int)(targetsize >> this->bbt_erase_shift);
1141 } else {
1142 chips = 1;
1143 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
1144 }
1145
1146 for (i = 0; i < chips; i++) {
1147 if ((td->options & NAND_BBT_ABSPAGE) ||
1148 !(td->options & NAND_BBT_WRITE)) {
1149 if (td->pages[i] == -1)
1150 continue;
1151 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
1152 oldval = bbt_get_entry(this, block);
1153 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1154 if ((oldval != BBT_BLOCK_RESERVED) &&
1155 td->reserved_block_code)
1156 nand_update_bbt(this, (loff_t)block <<
1157 this->bbt_erase_shift);
1158 continue;
1159 }
1160 update = 0;
1161 if (td->options & NAND_BBT_LASTBLOCK)
1162 block = ((i + 1) * nrblocks) - td->maxblocks;
1163 else
1164 block = i * nrblocks;
1165 for (j = 0; j < td->maxblocks; j++) {
1166 oldval = bbt_get_entry(this, block);
1167 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1168 if (oldval != BBT_BLOCK_RESERVED)
1169 update = 1;
1170 block++;
1171 }
1172
1173
1174
1175
1176
1177 if (update && td->reserved_block_code)
1178 nand_update_bbt(this, (loff_t)(block - 1) <<
1179 this->bbt_erase_shift);
1180 }
1181 }
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191 static void verify_bbt_descr(struct nand_chip *this, struct nand_bbt_descr *bd)
1192 {
1193 u64 targetsize = nanddev_target_size(&this->base);
1194 struct mtd_info *mtd = nand_to_mtd(this);
1195 u32 pattern_len;
1196 u32 bits;
1197 u32 table_size;
1198
1199 if (!bd)
1200 return;
1201
1202 pattern_len = bd->len;
1203 bits = bd->options & NAND_BBT_NRBITS_MSK;
1204
1205 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
1206 !(this->bbt_options & NAND_BBT_USE_FLASH));
1207 BUG_ON(!bits);
1208
1209 if (bd->options & NAND_BBT_VERSION)
1210 pattern_len++;
1211
1212 if (bd->options & NAND_BBT_NO_OOB) {
1213 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
1214 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
1215 BUG_ON(bd->offs);
1216 if (bd->options & NAND_BBT_VERSION)
1217 BUG_ON(bd->veroffs != bd->len);
1218 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1219 }
1220
1221 if (bd->options & NAND_BBT_PERCHIP)
1222 table_size = targetsize >> this->bbt_erase_shift;
1223 else
1224 table_size = mtd->size >> this->bbt_erase_shift;
1225 table_size >>= 3;
1226 table_size *= bits;
1227 if (bd->options & NAND_BBT_NO_OOB)
1228 table_size += pattern_len;
1229 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1230 }
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244 static int nand_scan_bbt(struct nand_chip *this, struct nand_bbt_descr *bd)
1245 {
1246 struct mtd_info *mtd = nand_to_mtd(this);
1247 int len, res;
1248 uint8_t *buf;
1249 struct nand_bbt_descr *td = this->bbt_td;
1250 struct nand_bbt_descr *md = this->bbt_md;
1251
1252 len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
1253
1254
1255
1256
1257 this->bbt = kzalloc(len, GFP_KERNEL);
1258 if (!this->bbt)
1259 return -ENOMEM;
1260
1261
1262
1263
1264
1265 if (!td) {
1266 if ((res = nand_memory_bbt(this, bd))) {
1267 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
1268 goto err_free_bbt;
1269 }
1270 return 0;
1271 }
1272 verify_bbt_descr(this, td);
1273 verify_bbt_descr(this, md);
1274
1275
1276 len = (1 << this->bbt_erase_shift);
1277 len += (len >> this->page_shift) * mtd->oobsize;
1278 buf = vmalloc(len);
1279 if (!buf) {
1280 res = -ENOMEM;
1281 goto err_free_bbt;
1282 }
1283
1284
1285 if (td->options & NAND_BBT_ABSPAGE) {
1286 read_abs_bbts(this, buf, td, md);
1287 } else {
1288
1289 search_read_bbts(this, buf, td, md);
1290 }
1291
1292 res = check_create(this, buf, bd);
1293 if (res)
1294 goto err_free_buf;
1295
1296
1297 mark_bbt_region(this, td);
1298 if (md)
1299 mark_bbt_region(this, md);
1300
1301 vfree(buf);
1302 return 0;
1303
1304 err_free_buf:
1305 vfree(buf);
1306 err_free_bbt:
1307 kfree(this->bbt);
1308 this->bbt = NULL;
1309 return res;
1310 }
1311
1312
1313
1314
1315
1316 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1317
1318
1319 static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1320 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1321
1322 static struct nand_bbt_descr bbt_main_descr = {
1323 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1324 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1325 .offs = 8,
1326 .len = 4,
1327 .veroffs = 12,
1328 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1329 .pattern = bbt_pattern
1330 };
1331
1332 static struct nand_bbt_descr bbt_mirror_descr = {
1333 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1334 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1335 .offs = 8,
1336 .len = 4,
1337 .veroffs = 12,
1338 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1339 .pattern = mirror_pattern
1340 };
1341
1342 static struct nand_bbt_descr bbt_main_no_oob_descr = {
1343 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1344 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1345 | NAND_BBT_NO_OOB,
1346 .len = 4,
1347 .veroffs = 4,
1348 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1349 .pattern = bbt_pattern
1350 };
1351
1352 static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
1353 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1354 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1355 | NAND_BBT_NO_OOB,
1356 .len = 4,
1357 .veroffs = 4,
1358 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1359 .pattern = mirror_pattern
1360 };
1361
1362 #define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372 static int nand_create_badblock_pattern(struct nand_chip *this)
1373 {
1374 struct nand_bbt_descr *bd;
1375 if (this->badblock_pattern) {
1376 pr_warn("Bad block pattern already allocated; not replacing\n");
1377 return -EINVAL;
1378 }
1379 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1380 if (!bd)
1381 return -ENOMEM;
1382 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
1383 bd->offs = this->badblockpos;
1384 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1385 bd->pattern = scan_ff_pattern;
1386 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1387 this->badblock_pattern = bd;
1388 return 0;
1389 }
1390
1391
1392
1393
1394
1395
1396
1397
1398 int nand_create_bbt(struct nand_chip *this)
1399 {
1400 int ret;
1401
1402
1403 if (this->bbt_options & NAND_BBT_USE_FLASH) {
1404
1405 if (!this->bbt_td) {
1406 if (this->bbt_options & NAND_BBT_NO_OOB) {
1407 this->bbt_td = &bbt_main_no_oob_descr;
1408 this->bbt_md = &bbt_mirror_no_oob_descr;
1409 } else {
1410 this->bbt_td = &bbt_main_descr;
1411 this->bbt_md = &bbt_mirror_descr;
1412 }
1413 }
1414 } else {
1415 this->bbt_td = NULL;
1416 this->bbt_md = NULL;
1417 }
1418
1419 if (!this->badblock_pattern) {
1420 ret = nand_create_badblock_pattern(this);
1421 if (ret)
1422 return ret;
1423 }
1424
1425 return nand_scan_bbt(this, this->badblock_pattern);
1426 }
1427 EXPORT_SYMBOL(nand_create_bbt);
1428
1429
1430
1431
1432
1433
1434 int nand_isreserved_bbt(struct nand_chip *this, loff_t offs)
1435 {
1436 int block;
1437
1438 block = (int)(offs >> this->bbt_erase_shift);
1439 return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
1440 }
1441
1442
1443
1444
1445
1446
1447
1448 int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt)
1449 {
1450 int block, res;
1451
1452 block = (int)(offs >> this->bbt_erase_shift);
1453 res = bbt_get_entry(this, block);
1454
1455 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1456 (unsigned int)offs, block, res);
1457
1458 if (mtd_check_expert_analysis_mode())
1459 return 0;
1460
1461 switch (res) {
1462 case BBT_BLOCK_GOOD:
1463 return 0;
1464 case BBT_BLOCK_WORN:
1465 return 1;
1466 case BBT_BLOCK_RESERVED:
1467 return allowbbt ? 0 : 1;
1468 }
1469 return 1;
1470 }
1471
1472
1473
1474
1475
1476
1477 int nand_markbad_bbt(struct nand_chip *this, loff_t offs)
1478 {
1479 int block, ret = 0;
1480
1481 block = (int)(offs >> this->bbt_erase_shift);
1482
1483
1484 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
1485
1486
1487 if (this->bbt_options & NAND_BBT_USE_FLASH)
1488 ret = nand_update_bbt(this, offs);
1489
1490 return ret;
1491 }