62 #include <linux/slab.h>
63 #include <linux/types.h>
68 #include <linux/bitops.h>
71 #include <linux/export.h>
72 #include <linux/string.h>
98 return check_pattern_no_oob(buf, td);
100 end = paglen + td->
offs;
171 size_t retlen, len,
totlen;
178 totlen = (num *
bits) >> 3;
179 marker_len = add_marker_len(td);
193 res =
mtd_read(mtd, from, len, &retlen, buf);
195 if (mtd_is_eccerr(res)) {
196 pr_info(
"nand_bbt: ECC error in BBT at "
199 }
else if (mtd_is_bitflip(res)) {
200 pr_info(
"nand_bbt: corrected error in BBT at "
204 pr_info(
"nand_bbt: error reading BBT\n");
210 for (
i = 0;
i < len;
i++) {
212 for (j = 0; j < 8; j +=
bits, act += 2) {
216 if (reserved_block_code && (tmp == reserved_block_code)) {
217 pr_info(
"nand_read_bbt: reserved block at 0x%012llx\n",
219 this->
bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
227 pr_info(
"nand_read_bbt: bad block at 0x%012llx\n",
231 this->
bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
233 this->
bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
262 if (chip == -1 || chip ==
i)
263 res = read_bbt(mtd, buf, td->
pages[
i],
264 this->chipsize >> this->bbt_erase_shift,
271 res = read_bbt(mtd, buf, td->
pages[0],
272 mtd->
size >> this->bbt_erase_shift, td, 0);
280 static int scan_read_data(
struct mtd_info *mtd,
uint8_t *buf, loff_t offs,
290 return mtd_read(mtd, offs, len, &retlen, buf);
304 static int scan_read_oob(
struct mtd_info *mtd,
uint8_t *buf, loff_t offs,
317 ops.oobbuf = buf +
ops.len;
321 if (!mtd_is_bitflip_or_eccerr(res))
323 else if (mtd_is_eccerr(res) || !ret)
338 return scan_read_data(mtd, buf, offs, td);
340 return scan_read_oob(mtd, buf, offs, len);
344 static int scan_write_bbt(
struct mtd_info *mtd, loff_t offs,
size_t len,
356 return mtd_write_oob(mtd, offs, &ops);
385 scan_read(mtd, buf, (loff_t)td->
pages[0] << this->page_shift,
387 td->
version[0] = buf[bbt_get_ver_offs(mtd, td)];
388 pr_info(
"Bad block table at page %d, version 0x%02X\n",
394 scan_read(mtd, buf, (loff_t)md->
pages[0] << this->page_shift,
396 md->
version[0] = buf[bbt_get_ver_offs(mtd, md)];
397 pr_info(
"Bad block table at page %d, version 0x%02X\n",
404 loff_t offs,
uint8_t *buf,
size_t readlen,
405 int scanlen,
int numpages)
409 ret = scan_read_oob(mtd, buf, offs, readlen);
411 if (ret && !mtd_is_bitflip_or_eccerr(ret))
414 for (j = 0; j < numpages; j++, buf += scanlen) {
415 if (check_pattern(buf, scanlen, mtd->
writesize, bd))
423 loff_t offs,
uint8_t *buf,
int numpages)
434 for (j = 0; j < numpages; j++) {
441 if (ret && !mtd_is_bitflip_or_eccerr(ret))
444 if (check_short_pattern(buf, bd))
467 int i, numblocks, numpages, scanlen;
472 pr_info(
"Scanning device for bad blocks\n");
500 if (chip >= this->numchips) {
501 pr_warn(
"create_bbt(): chipnr (%d) > available chips (%d)\n",
502 chip + 1, this->numchips);
506 startblock = chip * numblocks;
507 numblocks += startblock;
514 for (i = startblock; i < numblocks;) {
520 ret = scan_block_full(mtd, bd, from, buf, readlen,
523 ret = scan_block_fast(mtd, bd, from, buf, numpages);
529 this->
bbt[i >> 3] |= 0x03 << (i & 0x6);
530 pr_warn(
"Bad eraseblock %d at 0x%012llx\n",
531 i >> 1, (
unsigned long long)from);
578 startblock &= bbtblocks - 1;
587 for (i = 0; i <
chips; i++) {
592 for (block = 0; block < td->
maxblocks; block++) {
594 int actblock = startblock + dir *
block;
598 scan_read(mtd, buf, offs, mtd->
writesize, td);
599 if (!check_pattern(buf, scanlen, mtd->
writesize, td)) {
600 td->
pages[
i] = actblock << blocktopage;
602 offs = bbt_get_ver_offs(mtd, td);
611 for (i = 0; i <
chips; i++) {
612 if (td->
pages[i] == -1)
613 pr_warn(
"Bad block table not found for chip %d\n", i);
615 pr_info(
"Bad block table found at page %d, version "
635 search_bbt(mtd, buf, td);
639 search_bbt(mtd, buf, md);
658 int i,
j,
res, chip = 0;
659 int bits, startblock, dir,
page,
offs, numblocks, sft, sftmsk;
660 int nrchips, bbtoffs, pageoffs, ooboffs;
663 size_t retlen, len = 0;
676 numblocks = (
int)(this->chipsize >> this->bbt_erase_shift);
679 nrchips = this->numchips;
681 nrchips = chipsel + 1;
685 numblocks = (
int)(mtd->
size >> this->bbt_erase_shift);
690 for (; chip < nrchips; chip++) {
696 if (td->
pages[chip] != -1) {
706 startblock = numblocks * (chip + 1) - 1;
709 startblock = chip * numblocks;
714 int block = startblock + dir *
i;
716 switch ((this->bbt[block >> 2] >>
717 (2 * (block & 0x03))) & 0x03) {
723 (this->bbt_erase_shift - this->page_shift);
725 if (!md || md->
pages[chip] != page)
728 pr_err(
"No space left to write bad block table\n");
736 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
739 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
742 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
745 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
751 bbtoffs = chip * (numblocks >> 2);
753 to = ((loff_t)page) << this->page_shift;
758 to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1));
759 len = 1 << this->bbt_erase_shift;
760 res =
mtd_read(mtd, to, len, &retlen, buf);
763 pr_info(
"nand_bbt: error reading block "
764 "for writing the bad block table\n");
767 pr_warn(
"nand_bbt: ECC error while reading "
768 "block for writing bad block table\n");
771 ops.ooblen = (len >> this->page_shift) * mtd->
oobsize;
772 ops.oobbuf = &buf[len];
774 if (res < 0 ||
ops.oobretlen !=
ops.ooblen)
778 pageoffs = page - (
int)(to >> this->page_shift);
779 offs = pageoffs << this->page_shift;
781 memset(&buf[offs], 0xff, (
size_t)(numblocks >> sft));
782 ooboffs = len + (pageoffs * mtd->
oobsize);
791 len = (
size_t)(numblocks >> sft);
801 len = (
size_t)(numblocks >> sft);
806 (len >> this->page_shift)* mtd->
oobsize);
817 for (i = 0; i < numblocks;) {
819 dat = this->bbt[bbtoffs + (i >> 2)];
820 for (j = 0; j < 4; j++, i++) {
821 int sftcnt = (i << (3 - sft)) & sftmsk;
823 buf[offs + (i >> sft)] &=
824 ~(msk[dat & 0x03] << sftcnt);
829 memset(&einfo, 0,
sizeof(einfo));
832 einfo.len = 1 << this->bbt_erase_shift;
837 res = scan_write_bbt(mtd, to, len, buf,
843 pr_info(
"Bad block table written to 0x%012llx, version 0x%02X\n",
844 (
unsigned long long)to, td->
version[chip]);
852 pr_warn(
"nand_bbt: error while writing bad block table %d\n", res);
869 return create_bbt(mtd, this->
buffers->databuf, bd, -1);
893 chips = this->numchips;
897 for (i = 0; i <
chips; i++) {
907 if (td->
pages[i] == -1 && md->
pages[i] == -1) {
910 }
else if (td->
pages[i] == -1) {
913 }
else if (md->
pages[i] == -1) {
928 if (td->
pages[i] == -1) {
943 create_bbt(mtd, buf, bd, chipsel);
952 res = read_abs_bbt(mtd, buf, rd, chipsel);
953 if (mtd_is_eccerr(res)) {
963 res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
964 if (mtd_is_eccerr(res2)) {
974 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
985 res = write_bbt(mtd, buf, td, md, chipsel);
992 res = write_bbt(mtd, buf, md, td, chipsel);
1020 nrblocks = (
int)(mtd->
size >> this->bbt_erase_shift);
1023 for (i = 0; i <
chips; i++) {
1026 if (td->
pages[i] == -1)
1030 oldval = this->
bbt[(block >> 3)];
1031 newval = oldval | (0x2 << (block & 0x06));
1032 this->
bbt[(block >> 3)] = newval;
1039 block = ((i + 1) * nrblocks) - td->
maxblocks;
1041 block = i * nrblocks;
1044 oldval = this->
bbt[(block >> 3)];
1045 newval = oldval | (0x2 << (block & 0x06));
1046 this->
bbt[(block >> 3)] = newval;
1047 if (oldval != newval)
1079 pattern_len = bd->
len;
1089 if (bd->
options & NAND_BBT_NO_OOB) {
1104 if (bd->
options & NAND_BBT_NO_OOB)
1105 table_size += pattern_len;
1106 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1129 len = mtd->
size >> (this->bbt_erase_shift + 2);
1143 if ((res = nand_memory_bbt(mtd, bd))) {
1144 pr_err(
"nand_bbt: can't scan flash and build the RAM-based BBT\n");
1150 verify_bbt_descr(mtd, td);
1151 verify_bbt_descr(mtd, md);
1154 len = (1 << this->bbt_erase_shift);
1155 len += (len >> this->page_shift) * mtd->
oobsize;
1165 read_abs_bbts(mtd, buf, td, md);
1168 search_read_bbts(mtd, buf, td, md);
1171 res = check_create(mtd, buf, bd);
1174 mark_bbt_region(mtd, td);
1176 mark_bbt_region(mtd, md);
1198 if (!this->bbt || !td)
1202 len = (1 << this->bbt_erase_shift);
1203 len += (len >> this->page_shift) * mtd->
oobsize;
1210 chip = (
int)(offs >> this->chip_shift);
1223 res = write_bbt(mtd, buf, td, md, chipsel);
1229 res = write_bbt(mtd, buf, md, td, chipsel);
1241 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1243 static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1249 .pattern = scan_agand_pattern
1253 static uint8_t bbt_pattern[] = {
'B',
'b',
't',
'0' };
1254 static uint8_t mirror_pattern[] = {
'1',
't',
'b',
'B' };
1263 .pattern = bbt_pattern
1273 .pattern = mirror_pattern
1283 .pattern = bbt_pattern
1293 .pattern = mirror_pattern
1296 #define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
1306 static int nand_create_badblock_pattern(
struct nand_chip *
this)
1309 if (this->badblock_pattern) {
1310 pr_warn(
"Bad block pattern already allocated; not replacing\n");
1317 bd->
offs = this->badblockpos;
1319 bd->
pattern = scan_ff_pattern;
1321 this->badblock_pattern = bd;
1345 this->
bbt_td = &bbt_main_descr;
1346 this->
bbt_md = &bbt_mirror_descr;
1357 this->
bbt_td = &bbt_main_no_oob_descr;
1358 this->
bbt_md = &bbt_mirror_no_oob_descr;
1360 this->
bbt_td = &bbt_main_descr;
1361 this->
bbt_md = &bbt_mirror_descr;
1370 nand_create_badblock_pattern(
this);
1388 block = (
int)(offs >> (this->bbt_erase_shift - 1));
1389 res = (this->
bbt[block >> 3] >> (block & 0x06)) & 0x03;
1391 pr_debug(
"nand_isbad_bbt(): bbt info for offs 0x%08x: "
1392 "(block %d) 0x%02x\n",
1393 (
unsigned int)offs, block >> 1, res);
1401 return allowbbt ? 0 : 1;