diff options
Diffstat (limited to 'ANDROID_3.4.5/drivers/mtd/nand/nand_bbt.c')
-rw-r--r-- | ANDROID_3.4.5/drivers/mtd/nand/nand_bbt.c | 1496 |
1 files changed, 1404 insertions, 92 deletions
diff --git a/ANDROID_3.4.5/drivers/mtd/nand/nand_bbt.c b/ANDROID_3.4.5/drivers/mtd/nand/nand_bbt.c index 30d1319f..a6a9e661 100644 --- a/ANDROID_3.4.5/drivers/mtd/nand/nand_bbt.c +++ b/ANDROID_3.4.5/drivers/mtd/nand/nand_bbt.c @@ -68,7 +68,8 @@ #include <linux/delay.h> #include <linux/vmalloc.h> #include <linux/export.h> - +#include <mach/hardware.h> +//#define RETRY_DEBUG static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td) { int ret; @@ -131,7 +132,7 @@ static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_desc * good / bad block identifiers. Same as check_pattern, but no optional empty * check. */ -static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td) +static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td, int ano_bytes) { int i; uint8_t *p = buf; @@ -141,6 +142,16 @@ static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td) if (p[td->offs + i] != td->pattern[i]) return -1; } + if (ano_bytes) { + //printk("sandisk flash"); + for (i = 0; i < ano_bytes; i++) { + //printk("of=0x%x da=0x%x len=%x\n", td->offs + i, p[td->offs + i], td->len); + if (p[i] != td->pattern[0]) { + printk("p[%d]=0x%x of=0x%x da=0x%x len=%x\n", i, p[i], td->offs + i, p[td->offs + i], td->len); + return -1; + } + } + } return 0; } @@ -188,10 +199,12 @@ static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num, totlen = (num * bits) >> 3; marker_len = add_marker_len(td); - from = ((loff_t)page) << this->page_shift; + //from = ((loff_t)page) << this->page_shift; + from = ((loff_t)page*mtd->pageSizek) << 10; while (totlen) { - len = min(totlen, (size_t)(1 << this->bbt_erase_shift)); + //len = min(totlen, (size_t)(1 << this->bbt_erase_shift)); + len = min(totlen, (size_t)(mtd->erasesize)); if (marker_len) { /* * In case the BBT marker is not in the OOB area it @@ -225,8 +238,9 @@ static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num, if (tmp == msk) continue; if (reserved_block_code && (tmp == reserved_block_code)) { - pr_info("nand_read_bbt: reserved block at 0x%012llx\n", - (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift); + pr_info("nand_read_bbt: (read fail)reserved block at 0x%012llx\n", + //(loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift); + (loff_t)(((offs << 2) + (act >> 1))*mtd->pageSizek) << (10+this->pagecnt_shift)); this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06); mtd->ecc_stats.bbtblocks++; continue; @@ -235,8 +249,10 @@ static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num, * Leave it for now, if it's matured we can * move this message to pr_debug. */ - pr_info("nand_read_bbt: bad block at 0x%012llx\n", - (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift); + pr_info("nand_read_bbt: bad block at 0x%012llx (block%d)\n", + //(loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift, + (loff_t)(((offs << 2) + (act >> 1))*mtd->pageSizek) << (10+this->pagecnt_shift), + (offs << 2) + (act >> 1)); /* Factory marked bad or worn out? */ if (tmp == 0) this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06); @@ -250,6 +266,111 @@ static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num, } return ret; } +extern void print_nand_buffer(char *value, unsigned int length); +/** + * read_bbt_multi - [GENERIC] Read the bad block table starting from page + * @mtd: MTD device structure + * @buf: temporary buffer + * @page: the starting page + * @num: the number of bbt descriptors to read + * @td: the bbt describtion table + * @offs: offset in the memory table + * + * Read the bad block table starting from page. + */ +static int read_bbt_multi(struct mtd_info *mtd, uint8_t *buf, int page, int num, + struct nand_bbt_descr *td, int offs) +{ + int res, ret = 0, i, j, act = 0; + struct nand_chip *this = mtd->priv; + size_t retlen, len, totlen; + loff_t from; + int bits = (td->options & NAND_BBT_NRBITS_MSK)<<1; + uint8_t msk = (uint8_t)((1 << bits) - 1); + u32 marker_len; + int reserved_block_code = td->reserved_block_code;//=0 +//printk("--------bit=%d, msk=%d, code=%d\n",bits, msk, reserved_block_code); + totlen = (num * bits) >> 3; + marker_len = add_marker_len(td); + //from = ((loff_t)page) << this->page_shift; + from = ((loff_t)page*mtd->pageSizek) << 10; +//printk("----totlen=%d, marker_len=%d, page=%d\n", totlen, marker_len, page); + while (totlen) { + //len = min(totlen, (size_t)(1 << this->bbt_erase_shift)); + len = min(totlen, (size_t)(mtd->erasesize)); + if (marker_len) { + /* + * In case the BBT marker is not in the OOB area it + * will be just in the first page. + */ + len -= marker_len; + from += marker_len; + marker_len = 0; + } + res = mtd_read(mtd, from, len, &retlen, buf); + if (res < 0) { + if (mtd_is_eccerr(res)) { + pr_info("nand_bbt: ECC error in BBT at " + "0x%012llx\n", from & ~mtd->writesize); + return res; + } else if (mtd_is_bitflip(res)) { + pr_info("nand_bbt: corrected error in BBT at " + "0x%012llx\n", from & ~mtd->writesize); + ret = res; + } else { + pr_info("nand_bbt: error reading BBT\n"); + return res; + } + } +//printk("+++++++++++++++++len=%d, offs=%d\n", len, offs); +//print_nand_buffer(buf, 8192+64); +//print_nand_buffer(buf+8192, 8192+64); + /* Analyse data */ + for (i = 0; i < len; i++) { + uint8_t dat = buf[i]; + if (this->bbt_plane[0] == page || this->bbt_plane[1] == page) + dat = buf[i+mtd->realwritesize]; + for (j = 0; j < 8; j += bits, act += 4) { + uint8_t tmp = (dat >> j) & msk; + if (tmp == msk) + continue; + if (reserved_block_code && (tmp == reserved_block_code)) { + pr_info("nand_read_bbt: (read fail)reserved block at 0x%012llx\n", + //(loff_t)((offs << 1) + (act >> 2)) << this->bbt_erase_shift); + (loff_t)(((offs << 1) + (act >> 2))*mtd->pageSizek) << (10+this->pagecnt_shift)); + this->bbt[offs + (act >> 3)] |= 0xa << (act & 0x04); + mtd->ecc_stats.bbtblocks++; + continue; + } + /* + * Leave it for now, if it's matured we can + * move this message to pr_debug. + */ + pr_info("nand_read_bbt: bad block at 0x%012llx (block%d)\n", + //(loff_t)((offs << 1) + (act >> 2)) << this->bbt_erase_shift, + ((loff_t)(((offs << 1) + (act >> 2))*mtd->pageSizek)) << (10+this->pagecnt_shift), + (offs << 1) + (act >> 2)); + + /* Factory marked bad or worn out? */ + if (tmp == 0) { + this->bbt[offs + (act >> 3)] |= 0xf << (act & 0x04); + //printk("bbt[%d]=0x%x", offs + (act >> 3), this->bbt[offs + (act >> 3)] |= 0xf << (act & 0x04)); + } else if (tmp == 0x3) { + this->bbt[offs + (act >> 3)] |= 0xc << (act & 0x04); + //printk("bbt[%d]=0x%x", offs + (act >> 3), this->bbt[offs + (act >> 3)] |= 0xc << (act & 0x04)); + } else if (tmp == 0xc) { + this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x04); + //printk("bbt[%d]=0x%x", offs + (act >> 3), this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x04)); + } else + this->bbt[offs + (act >> 3)] |= 0x5 << (act & 0x04); + mtd->ecc_stats.badblocks++; + } + } + totlen -= len; + from += len; + } + return ret; +} /** * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page @@ -270,17 +391,155 @@ static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc if (td->options & NAND_BBT_PERCHIP) { int offs = 0; for (i = 0; i < this->numchips; i++) { - if (chip == -1 || chip == i) - res = read_bbt(mtd, buf, td->pages[i], - this->chipsize >> this->bbt_erase_shift, - td, offs); + if (chip == -1 || chip == i) { + if (this->realplanenum) { + /* multi plane mode use 4-bit as an block instead of 2-bit */ + res = read_bbt_multi(mtd, buf, td->pages[i], + //this->chipsize >> this->bbt_erase_shift, + (int)(this->chipsize >> (10+this->pagecnt_shift))/mtd->pageSizek, + td, offs); + } else { + res = read_bbt(mtd, buf, td->pages[i], + //this->chipsize >> this->bbt_erase_shift, + (int)(this->chipsize >> (10+this->pagecnt_shift))/mtd->pageSizek, + td, offs); + } + } if (res) return res; - offs += this->chipsize >> (this->bbt_erase_shift + 2); + if (this->realplanenum) { + //offs += this->chipsize >> (this->bbt_erase_shift + 1); + offs += ((int)(this->chipsize >> (10+this->pagecnt_shift+1))/mtd->pageSizek); + } else { + //offs += this->chipsize >> (this->bbt_erase_shift + 2); + offs += ((int)(this->chipsize >> (10+this->pagecnt_shift+2))/mtd->pageSizek); + } } } else { - res = read_bbt(mtd, buf, td->pages[0], - mtd->size >> this->bbt_erase_shift, td, 0); + if (this->realplanenum) { + /* multi plane mode use 4-bit as an block instead of 2-bit */ + res = read_bbt_multi(mtd, buf, td->pages[0], + //mtd->size >> this->bbt_erase_shift, td, 0); + (int)(mtd->size >> (10+this->pagecnt_shift))/mtd->pageSizek, td, 0); + } else { + res = read_bbt(mtd, buf, td->pages[0], + //mtd->size >> this->bbt_erase_shift, td, 0); + (int)(mtd->size >> (10+this->pagecnt_shift))/mtd->pageSizek, td, 0); + if (res) + return res; + } + } + return 0; +} + + +/** + * read_retry_table - [GENERIC] Read the retry table starting from page + * @mtd: MTD device structure + * @buf: temporary buffer + * @page: the starting page + * @num: the number of bbt descriptors to read + * @td: the bbt describtion table + * @offs: offset in the memory table + * + * Read the read retry table starting from page. + * + */ +static int read_retry_table(struct mtd_info *mtd, uint8_t *buf, int page, int chip) +{ + int res; + struct nand_chip *this = mtd->priv; + struct nand_read_retry_param *rdtry; + size_t retlen; + loff_t from; + + //from = ((loff_t) page) << this->page_shift; + from = ((loff_t) (page*mtd->pageSizek)) << 10; + + res = mtd->_read(mtd, from, mtd->writesize, &retlen, buf); + if (res < 0) { + if (retlen != mtd->writesize) { + printk(KERN_INFO "nand_bbt: Error reading retry table\n"); + return res; + } + printk(KERN_WARNING "nand_bbt: ECC error while reading retry table\n"); + } + + /* Analyse data */ + rdtry = (struct nand_read_retry_param *)buf; + #ifdef RETRY_DEBUG + print_nand_buffer((uint8_t *)this->cur_chip, sizeof(chip_table[0])); + #endif + if (strcmp("readretry", rdtry->magic) /*|| info->data_ecc_uncor_err == 2*/) { + printk(KERN_WARNING "nand_bbt: retry table magic number wrong%s\n", rdtry->magic); + return -1; + } + #ifdef RETRY_DEBUG + printk(KERN_WARNING "nand_bbt: copy from buf\n"); + #endif + memcpy(/*(uint8_t *)*/this->cur_chip, buf, sizeof(chip_table[0])-16); + this->cur_chip->retry_def_value[this->cur_chip->retry_reg_num] = 0xff; + this->cur_chip->retry_def_value[this->cur_chip->retry_reg_num+1] = 0xff; + #ifdef RETRY_DEBUG + print_nand_buffer((uint8_t *)this->cur_chip, sizeof(chip_table[0])); + #endif + + /*if (rdtry->eslc_reg_num) { + if (rdtry->eslc_reg_num > 5) + printk(KERN_WARNING "nand_bbt: eslc reg size=%d is too big\n", rdtry->eslc_reg_num); + this->eslc_reg_num = rdtry->eslc_reg_num; + this->eslc_cmd = kzalloc(this->eslc_reg_num, GFP_KERNEL); + if (!this->eslc_cmd) { + printk(KERN_ERR "nand_scan_bbt: create eslc_cmd Out of memory\n"); + return -ENOMEM; + } + } + memcpy(this->eslc_cmd, ((uint8_t *)&rdtry->retry_reg_num)+4, this->eslc_reg_num); + print_nand_buffer(this->eslc_cmd, this->eslc_reg_num); + + if (rdtry->total_retry_cnt && rdtry->retry_reg_num) { + if ((rdtry->total_retry_cnt * rdtry->retry_reg_num) > 64) + printk(KERN_WARNING "nand_bbt: eslc reg size=%d is too big\n", + (rdtry->total_retry_cnt * rdtry->retry_reg_num)); + this->total_retry_cnt = rdtry->total_retry_cnt; + this->retry_reg_num = rdtry->retry_reg_num; + this->retry_cmd = kzalloc((this->retry_reg_num*this->total_retry_cnt), GFP_KERNEL); + if (!this->retry_cmd) { + printk(KERN_ERR "nand_scan_bbt: create retry_cmd Out of memory\n"); + return -ENOMEM; + } + } + memcpy(this->retry_cmd, ((uint8_t *)&rdtry->retry_reg_num)+4+this->eslc_reg_num, + (this->retry_reg_num*this->total_retry_cnt)); + + + for (i = 0; i < this->total_retry_cnt; i++) { + print_nand_buffer(&this->retry_cmd[i*this->retry_reg_num], this->retry_reg_num); + }*/ + + return 0; +} + +/** + * read_abs_retry_table - [GENERIC] Read the retry table starting at a given page + * @mtd: MTD device structure + * @buf: temporary buffer + * @td: descriptor for the bad block table + * @chip: read the table for a specific chip, -1 read all chips. + * + * Read the retry table for all chips starting at a given page +*/ +static int read_abs_retry_table(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip) +{ + //struct nand_chip *this = mtd->priv; + int res = 0, i, chips; + + //chips = this->numchips + chips = 1; + + for (i = 0; i < chips; i++) { + if (chip == -1 || chip == i) + res = read_retry_table(mtd, buf, td->pages[i], chip); if (res) return res; } @@ -351,7 +610,7 @@ static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len, ops.oobbuf = oob; ops.len = len; - return mtd_write_oob(mtd, offs, &ops); + return mtd_write_oob(mtd, offs, &ops); /* call mtd->_write_oob*/ } static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td) @@ -376,11 +635,12 @@ static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td) static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md) { - struct nand_chip *this = mtd->priv; + //struct nand_chip *this = mtd->priv; /* Read the primary version, if available */ if (td->options & NAND_BBT_VERSION) { - scan_read_raw(mtd, buf, (loff_t)td->pages[0] << this->page_shift, + //scan_read_raw(mtd, buf, (loff_t)td->pages[0] << this->page_shift, + scan_read_raw(mtd, buf, (loff_t)(td->pages[0]*mtd->pageSizek) << 10, mtd->writesize, td); td->version[0] = buf[bbt_get_ver_offs(mtd, td)]; pr_info("Bad block table at page %d, version 0x%02X\n", @@ -389,7 +649,8 @@ static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf, /* Read the mirror version, if available */ if (md && (md->options & NAND_BBT_VERSION)) { - scan_read_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift, + //scan_read_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift, + scan_read_raw(mtd, buf, (loff_t)(md->pages[0]*mtd->pageSizek) << 10, mtd->writesize, td); md->version[0] = buf[bbt_get_ver_offs(mtd, md)]; pr_info("Bad block table at page %d, version 0x%02X\n", @@ -422,7 +683,7 @@ static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd, loff_t offs, uint8_t *buf, int len) { struct mtd_oob_ops ops; - int j, ret; + int j, ret, more_bytes = 0, flag = 0; ops.ooblen = mtd->oobsize; ops.oobbuf = buf; @@ -430,20 +691,54 @@ static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd, ops.datbuf = NULL; ops.mode = MTD_OPS_PLACE_OOB; + if ((mtd->id>>24) == 0x45) { + more_bytes = 6; + } for (j = 0; j < len; j++) { /* * Read the full oob until read_oob is fixed to handle single * byte reads for 16 bit buswidth. */ - ret = mtd_read_oob(mtd, offs, &ops); + /* Dannier Chen patch 2010.04.20: start + check invalid bad block on which page of blocks + should be based on flash spec, for example flash type:HY27UT088G2M-T(P) bad block + is marked at page 125 and 127 of each block. + NOT always page 0 and 1. + */ + //printk("scan_block_fast: j=%d len=%d bd->page_offset[0]=%d offset[1]=%d\n ", j, len, bd->page_offset[0], bd->page_offset[1]); +#ifdef CONFIG_MTD_NAND_WMT_HWECC + ret = mtd->read_bbinfo_facmk(mtd, offs + bd->page_offset[j]*mtd->writesize, &ops); +#else + ret = mtd->read_oob(mtd, offs + bd->page_offset[j]*mtd->writesize, &ops); +#endif /* Ignore ECC errors when checking for BBM */ if (ret && !mtd_is_bitflip_or_eccerr(ret)) return ret; - if (check_short_pattern(buf, bd)) - return 1; - - offs += mtd->writesize; + if (check_short_pattern(buf, bd, more_bytes)) + flag |= 1;//return 1; + + if ((flag&1) == 0) + if (mtd->id == 0xECDED57E && mtd->id2 == 0x68440000) + if (check_short_pattern(buf+1, bd, 0)) + flag |= 1;//return 1; + + if (mtd->planenum > 1) { + if (check_short_pattern(buf+more_bytes, bd, more_bytes)) + flag |= 2;//return 1; + + if (check_short_pattern(buf+32, bd, 0)) + flag |= 2;//return 1; + + if ((flag&2) == 0) + if (mtd->id == 0xECDED57E && mtd->id2 == 0x68440000) + if (check_short_pattern(buf+33, bd, 0)) + flag |= 2;//return 1; + } + if (flag) + return flag; + /*offs += mtd->writesize;*/ + /* Dannier Chen patch 2010.04.20: end */ } return 0; } @@ -471,7 +766,8 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf, pr_info("Scanning device for bad blocks\n"); if (bd->options & NAND_BBT_SCANALLPAGES) - len = 1 << (this->bbt_erase_shift - this->page_shift); + //len = 1 << (this->bbt_erase_shift - this->page_shift); + len = 1 << (this->pagecnt_shift); else if (bd->options & NAND_BBT_SCAN2NDPAGE) len = 2; else @@ -492,7 +788,8 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf, * Note that numblocks is 2 * (real numblocks) here, see i+=2 * below as it makes shifting and masking less painful */ - numblocks = mtd->size >> (this->bbt_erase_shift - 1); + //numblocks = mtd->size >> (this->bbt_erase_shift - 1); + numblocks = ((int)(mtd->size >> (10+this->pagecnt_shift-1)))/mtd->pageSizek; startblock = 0; from = 0; } else { @@ -501,10 +798,12 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf, chip + 1, this->numchips); return -EINVAL; } - numblocks = this->chipsize >> (this->bbt_erase_shift - 1); + //numblocks = this->chipsize >> (this->bbt_erase_shift - 1); + numblocks = ((int)(this->chipsize >> (10+this->pagecnt_shift-1)))/mtd->pageSizek; startblock = chip * numblocks; numblocks += startblock; - from = (loff_t)startblock << (this->bbt_erase_shift - 1); + //from = (loff_t)startblock << (this->bbt_erase_shift - 1); + from = (loff_t)(startblock*mtd->pageSizek) << (10+this->pagecnt_shift-1); } if (this->bbt_options & NAND_BBT_SCANLASTPAGE) @@ -512,6 +811,19 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf, for (i = startblock; i < numblocks;) { int ret; + + if (((mtd->id>>24)&0xFF) == 0x45) { + /* dannierchen add to erase sandisk all blocks before check bad block 20121217 */ + /*printk(KERN_INFO "create_bbt: erase all blocks for sandisk\n");*/ + struct erase_info einfo; + memset(&einfo, 0, sizeof(einfo)); + einfo.mtd = mtd; + einfo.addr = from; + //einfo.len = 1 << this->bbt_erase_shift; + einfo.len = mtd->erasesize; + /*printk("einfo.addr is %llx einfo.len is %llx\n", einfo.addr, einfo.len);*/ + nand_erase_nand(mtd, &einfo, 0xFF); + } /* end of dannierchen erase 20121217 */ BUG_ON(bd->options & NAND_BBT_NO_OOB); @@ -529,14 +841,251 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf, pr_warn("Bad eraseblock %d at 0x%012llx\n", i >> 1, (unsigned long long)from); mtd->ecc_stats.badblocks++; - } + + /* edwardwan add for debug 20071229 start */ +#if 0 + if(mtd->ecc_stats.badblocks > 10){ + printk("\rnand flash bad block number is greater than 10\n"); + return 0; + } + /* edwardwan add for debug 20071229 end */ +#endif + } else if (((mtd->id>>24)&0xFF) != 0x45) { /* dannierchen add to erase good block when first creat table 20091014 */ + /*printk(KERN_INFO "create_bbt: erase good blocks\n");*/ + struct erase_info einfo; + int res = 0; + memset(&einfo, 0, sizeof(einfo)); + einfo.mtd = mtd; + einfo.addr = from; + //einfo.len = 1 << this->bbt_erase_shift; + einfo.len = mtd->erasesize; + /*printk("einfo.addr is %llx\n",einfo.addr); + printk("einfo.len is %llx\n",einfo.len);*/ + res = nand_erase_nand(mtd, &einfo, 0xFF); + if (res < 0) + printk("enand_erase_nand addr 0x%llx result is %x\n", einfo.addr, res); + } /* end of dannierchen erase 20091014 */ i += 2; - from += (1 << this->bbt_erase_shift); + //from += (1 << this->bbt_erase_shift); + from += (mtd->erasesize); + } + return 0; +} + +/** + * create_bbt_multi - [GENERIC] Create a bad block table by scanning the device + * @mtd: MTD device structure + * @buf: temporary buffer + * @bd: descriptor for the good/bad block search pattern + * @chip: create the table for a specific chip, -1 read all chips; applies only + * if NAND_BBT_PERCHIP option is set + * + * Create a bad block table by scanning the device for the given good/bad block + * identify pattern. + */ +static int create_bbt_multi(struct mtd_info *mtd, uint8_t *buf, + struct nand_bbt_descr *bd, int chip) +{ + struct nand_chip *this = mtd->priv; + int i, numblocks, len, scanlen; + int startblock; + loff_t from; + size_t readlen; + + pr_info("Scanning device for bad blocks\n"); + + if (bd->options & NAND_BBT_SCANALLPAGES) + //len = 1 << (this->bbt_erase_shift - this->page_shift); + len = 1 << this->pagecnt_shift; + else if (bd->options & NAND_BBT_SCAN2NDPAGE) + len = 2; + else + len = 1; + + if (!(bd->options & NAND_BBT_SCANEMPTY)) { + /* We need only read few bytes from the OOB area */ + scanlen = 0; + readlen = bd->len; + } else { + /* Full page content should be read */ + scanlen = mtd->writesize + mtd->oobsize; + readlen = len * mtd->writesize; + } + + if (chip == -1) { + /* + * Note that numblocks is 2 * (real numblocks) here, see i+=2 + * below as it makes shifting and masking less painful + */ + //numblocks = mtd->size >> (this->bbt_erase_shift - 2); + numblocks = ((int)(mtd->size >> (10+this->pagecnt_shift-2)))/mtd->pageSizek; + startblock = 0; + from = 0; + } else { + if (chip >= this->numchips) { + pr_warn("create_bbt_multi(): chipnr (%d) > available chips (%d)\n", + chip + 1, this->numchips); + return -EINVAL; + } + //numblocks = this->chipsize >> (this->bbt_erase_shift - 2); + numblocks = ((int)(this->chipsize >> (10+this->pagecnt_shift-2)))/mtd->pageSizek; + startblock = chip * numblocks; + numblocks += startblock; + //from = (loff_t)startblock << (this->bbt_erase_shift - 2); + from = (loff_t)(startblock*mtd->pageSizek) << (10+this->pagecnt_shift-2); + } + + if (this->bbt_options & NAND_BBT_SCANLASTPAGE) + from += mtd->erasesize - (mtd->writesize * len); + + for (i = startblock; i < numblocks;) { + int ret; + + if ((mtd->id>>24) == 0x45) { + /* dannierchen add to erase sandisk all blocks before check bad block 20121217 */ + /*printk(KERN_INFO "create_bbt_multi: erase all blocks for sandisk\n");*/ + struct erase_info einfo; + memset(&einfo, 0, sizeof(einfo)); + einfo.mtd = mtd; + einfo.addr = from; + //einfo.len = 1 << this->bbt_erase_shift; + einfo.len = mtd->erasesize; + /*printk("einfo.addr is %llx einfo.len is %llx\n", einfo.addr, einfo.len);*/ + nand_erase_nand(mtd, &einfo, 0xFF); + } /* end of dannierchen erase 20121217 */ + + BUG_ON(bd->options & NAND_BBT_NO_OOB); + + if (bd->options & NAND_BBT_SCANALLPAGES) + ret = scan_block_full(mtd, bd, from, buf, readlen, + scanlen, len); + else + ret = scan_block_fast(mtd, bd, from, buf, len); + + if (ret < 0) + return ret; + + if (ret) { + this->bbt[i >> 3] |= 0x0F << (i & 0x4); + pr_warn("Bad eraseblock %d at 0x%012llx\n", + i >> 2, (unsigned long long)from); + mtd->ecc_stats.badblocks++; + + /* edwardwan add for debug 20071229 start */ +#if 0 + if(mtd->ecc_stats.badblocks > 10){ + printk("\rnand flash bad block number is greater than 10\n"); + return 0; + } + /* edwardwan add for debug 20071229 end */ +#endif + } else if ((mtd->id>>24) != 0x45) { /* dannierchen add to erase good block when first creat table 20091014 */ + /*printk(KERN_INFO "create_bbt_multi: erase good blocks\n");*/ + struct erase_info einfo; + int res = 0; + memset(&einfo, 0, sizeof(einfo)); + einfo.mtd = mtd; + einfo.addr = from; + //einfo.len = 1 << this->bbt_erase_shift; + einfo.len = mtd->erasesize; + /*printk("einfo.addr is %llx\n",einfo.addr); + printk("einfo.len is %llx\n",einfo.len);*/ + res = nand_erase_nand(mtd, &einfo, 0xFF); + if (res < 0) + printk("enand_erase_nand addr 0x%llx result is %x\n", einfo.addr, res); + } /* end of dannierchen erase 20091014 */ + + i += 4; + //from += (1 << this->bbt_erase_shift); + from += (mtd->erasesize); + } + return 0; +} + +int create_hynix_table(struct mtd_info *mtd, int chip) +{ + int res; + res = mtd->get_para(mtd, chip); + + return res; +} + +static int check_retry_pattern(uint8_t *buf, int paglen, struct nand_bbt_descr *td) +{ + int i; + uint8_t *p = buf+paglen; + + for (i = 0; i < 10; i++) { + if (p[i] != td->pattern[i]) + return -1; + } + return 0; +} +/* +* +* read oob to search retry table +* +*/ + +static int search_hynix_retry_table(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td) +{ + struct nand_chip *this = mtd->priv; + int i, chips; + int startblock, block, dir; + int bbtblocks; + //int blocktopage = this->bbt_erase_shift - this->page_shift; + int blocktopage = this->pagecnt_shift; + + /* Search direction top -> down ? */ + //if (td->options & NAND_BBT_LASTBLOCK) { + //startblock = (mtd->size >> this->bbt_erase_shift) - 1; + startblock = ((int)(mtd->size >> (10+this->pagecnt_shift)))/mtd->pageSizek - 1; + dir = -1; + /*} else { + startblock = 0; + dir = 1; + }*/ + + //so far use first chip parameter for read retry on 2-die chip + //chips = this->numchips; + chips = 1; + + //bbtblocks = this->chipsize >> this->bbt_erase_shift; + bbtblocks = ((int)(this->chipsize >> (10+this->pagecnt_shift)))/mtd->pageSizek; + startblock &= bbtblocks - 5; + + for (i = 0; i < chips; i++) { + td->pages[i] = -1; + /* Scan the maximum number of blocks */ + for (block = 0; block < td->maxblocks; block++) { + + int actblock = startblock + dir * block; + //loff_t offs = (loff_t)actblock << this->bbt_erase_shift; + loff_t offs = (loff_t)(actblock*mtd->pageSizek) << (10+this->pagecnt_shift); + + /* Read first page */ + scan_read_raw(mtd, buf, offs, mtd->writesize, td); + + if (!check_retry_pattern(buf, mtd->writesize, this->retry_pattern)) { + td->pages[i] = actblock << blocktopage; + break; + } + } + //startblock += this->chipsize >> this->bbt_erase_shift; + startblock += ((int)(this->chipsize >> (10+this->pagecnt_shift)))/mtd->pageSizek; + } + /* Check, if we found a bbt for each requested chip */ + for (i = 0; i < chips; i++) { + if (td->pages[i] == -1) + printk(KERN_WARNING "Retry block table not found for chip %d\n", i); + else + printk(KERN_WARNING "Retry block table is found for chip %d\n", i); } return 0; } + /** * search_bbt - [GENERIC] scan the device for a specific bad block table * @mtd: MTD device structure @@ -559,11 +1108,13 @@ static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr int bits, startblock, block, dir; int scanlen = mtd->writesize + mtd->oobsize; int bbtblocks; - int blocktopage = this->bbt_erase_shift - this->page_shift; + //int blocktopage = this->bbt_erase_shift - this->page_shift; + int blocktopage = this->pagecnt_shift; /* Search direction top -> down? */ if (td->options & NAND_BBT_LASTBLOCK) { - startblock = (mtd->size >> this->bbt_erase_shift) - 1; + //startblock = (mtd->size >> this->bbt_erase_shift) - 1; + startblock = ((int)(mtd->size >> (10+this->pagecnt_shift)))/mtd->pageSizek - 1; dir = -1; } else { startblock = 0; @@ -573,15 +1124,19 @@ static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr /* Do we have a bbt per chip? */ if (td->options & NAND_BBT_PERCHIP) { chips = this->numchips; - bbtblocks = this->chipsize >> this->bbt_erase_shift; + //bbtblocks = this->chipsize >> this->bbt_erase_shift; + bbtblocks = ((int)(this->chipsize >> (10+this->pagecnt_shift)))/mtd->pageSizek; startblock &= bbtblocks - 1; } else { chips = 1; - bbtblocks = mtd->size >> this->bbt_erase_shift; + //bbtblocks = mtd->size >> this->bbt_erase_shift; + bbtblocks = ((int)(mtd->size >> (10+this->pagecnt_shift)))/mtd->pageSizek; } /* Number of bits for each erase block in the bbt */ bits = td->options & NAND_BBT_NRBITS_MSK; + if (this->realplanenum) + bits<<=1; for (i = 0; i < chips; i++) { /* Reset version information */ @@ -591,20 +1146,39 @@ static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr for (block = 0; block < td->maxblocks; block++) { int actblock = startblock + dir * block; - loff_t offs = (loff_t)actblock << this->bbt_erase_shift; + //loff_t offs = (loff_t)actblock << this->bbt_erase_shift; + loff_t offs = (loff_t)(actblock*mtd->pageSizek) << (10+this->pagecnt_shift); /* Read first page */ scan_read_raw(mtd, buf, offs, mtd->writesize, td); + //print_nand_buffer(buf+mtd->writesize, 64); if (!check_pattern(buf, scanlen, mtd->writesize, td)) { td->pages[i] = actblock << blocktopage; + printk("get bbt0 from %x\n",td->pages[i]); + this->bbt_plane[i] = 0; if (td->options & NAND_BBT_VERSION) { - offs = bbt_get_ver_offs(mtd, td); - td->version[i] = buf[offs]; + u32 offs_ver; + offs_ver = bbt_get_ver_offs(mtd, td); + td->version[i] = buf[offs_ver]; } break; } + if (this->realplanenum) + if (!check_pattern(buf, scanlen, mtd->writesize+20, td)) { + td->pages[i] = actblock << blocktopage; + //printk("get bbt1 from %x\n",td->pages[i]); + this->bbt_plane[i] = td->pages[i]; + //printk("get bbt plane[%d] from %x\n",i, this->bbt_plane[i]); + if (td->options & NAND_BBT_VERSION) { + u32 offs_ver; + offs_ver = bbt_get_ver_offs(mtd, td); + td->version[i] = buf[20+offs_ver]; + } + break; + } } - startblock += this->chipsize >> this->bbt_erase_shift; + //startblock += this->chipsize >> this->bbt_erase_shift; + startblock += ((int)(this->chipsize >> (10+this->pagecnt_shift)))/mtd->pageSizek; } /* Check, if we found a bbt for each requested chip */ for (i = 0; i < chips; i++) { @@ -617,6 +1191,9 @@ static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr return 0; } +extern int reset_nfc(struct mtd_info *mtd, unsigned int *buf, int step); +extern void nfc_hw_rdmz(struct mtd_info *mtd, int on); +extern void print_nand_register(struct mtd_info *mtd); /** * search_read_bbts - [GENERIC] scan the device for bad block table(s) * @mtd: MTD device structure @@ -673,7 +1250,8 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf, rcode = 0xff; /* Write bad block table per chip rather than per device? */ if (td->options & NAND_BBT_PERCHIP) { - numblocks = (int)(this->chipsize >> this->bbt_erase_shift); + //numblocks = (int)(this->chipsize >> this->bbt_erase_shift); + numblocks = ((int)(this->chipsize >> (10+this->pagecnt_shift)))/mtd->pageSizek; /* Full device write or specific chip? */ if (chipsel == -1) { nrchips = this->numchips; @@ -682,7 +1260,8 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf, chip = chipsel; } } else { - numblocks = (int)(mtd->size >> this->bbt_erase_shift); + //numblocks = (int)(mtd->size >> this->bbt_erase_shift); + numblocks = ((int)(mtd->size >> (10+this->pagecnt_shift)))/mtd->pageSizek; nrchips = 1; } @@ -720,7 +1299,8 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf, continue; } page = block << - (this->bbt_erase_shift - this->page_shift); + //(this->bbt_erase_shift - this->page_shift); + this->pagecnt_shift; /* Check, if the block is used by the mirror table */ if (!md || md->pages[chip] != page) goto write; @@ -731,7 +1311,7 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf, /* Set up shift count and masks for the flash table */ bits = td->options & NAND_BBT_NRBITS_MSK; - msk[2] = ~rcode; + msk[2] = 2;//~rcode; switch (bits) { case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01; msk[3] = 0x01; @@ -749,14 +1329,20 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf, } bbtoffs = chip * (numblocks >> 2); + if (this->realplanenum) + bbtoffs = chip * (numblocks >> 1); - to = ((loff_t)page) << this->page_shift; + //to = ((loff_t)page) << this->page_shift; + to = ((loff_t) (page*mtd->pageSizek)) << 10; /* Must we save the block contents? */ if (td->options & NAND_BBT_SAVECONTENT) { + printk("inlegal------not go-----------\n"); /* Make it block aligned */ - to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1)); - len = 1 << this->bbt_erase_shift; + //to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1)); + to &= ~((loff_t)((mtd->erasesize) - 1));//danbbg + //len = 1 << this->bbt_erase_shift; + len = mtd->erasesize; res = mtd_read(mtd, to, len, &retlen, buf); if (res < 0) { if (retlen != len) { @@ -768,15 +1354,18 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf, "block for writing bad block table\n"); } /* Read oob data */ - ops.ooblen = (len >> this->page_shift) * mtd->oobsize; + //ops.ooblen = (len >> this->page_shift) * mtd->oobsize; + ops.ooblen = (mtd->pagecnt) * mtd->oobsize; ops.oobbuf = &buf[len]; res = mtd_read_oob(mtd, to + mtd->writesize, &ops); if (res < 0 || ops.oobretlen != ops.ooblen) goto outerr; /* Calc the byte offset in the buffer */ - pageoffs = page - (int)(to >> this->page_shift); - offs = pageoffs << this->page_shift; + //pageoffs = page - (int)(to >> this->page_shift); + pageoffs = page - ((int)(to >> 10))/mtd->pageSizek; + //offs = pageoffs << this->page_shift; + offs = (pageoffs*mtd->pageSizek) << 10; /* Preset the bbt area with 0xff */ memset(&buf[offs], 0xff, (size_t)(numblocks >> sft)); ooboffs = len + (pageoffs * mtd->oobsize); @@ -801,9 +1390,12 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf, len = (size_t)(numblocks >> sft); /* Make it page aligned! */ len = ALIGN(len, mtd->writesize); + if (len < mtd->writesize) + len = mtd->writesize; /* Preset the buffer with 0xff */ memset(buf, 0xff, len + - (len >> this->page_shift)* mtd->oobsize); + //(len >> this->page_shift)* mtd->oobsize); + mtd->pagecnt* mtd->oobsize); offs = 0; ooboffs = len; /* Pattern is located in oob area of first page */ @@ -820,17 +1412,276 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf, for (j = 0; j < 4; j++, i++) { int sftcnt = (i << (3 - sft)) & sftmsk; /* Do not store the reserved bbt blocks! */ - buf[offs + (i >> sft)] &= - ~(msk[dat & 0x03] << sftcnt); + + /* dannier 2014/03/01 add a condition only retry and bbt blocks are not store + read retry command fail blocks are marked as reserved blk and need to be stored to flash */ + if (i >= (numblocks - td->maxblocks - 4) && (dat&0x3) == 0x2) { + //buf[offs + (i >> sft)] &= ~(msk[dat & 0x03] << sftcnt); + //printk("offs + (i >> sft)=%d data=0x%x, dat=0x%x sft=%d\n",offs + (i >> sft), ~(msk[dat & 0x03] << sftcnt), dat, sft); + } else + buf[offs + (i >> sft)] &= ~(msk[dat & 0x03] << sftcnt); dat >>= 2; } } + memset(&einfo, 0, sizeof(einfo)); + einfo.mtd = mtd; + einfo.addr = to; + //einfo.len = 1 << this->bbt_erase_shift; + einfo.len = mtd->erasesize; + res = nand_erase_nand(mtd, &einfo, 1); + if (res < 0) + goto outerr; + + res = scan_write_bbt(mtd, to, len, buf, + td->options & NAND_BBT_NO_OOB ? NULL : + &buf[len]); + if (res < 0) + goto outerr; + pr_info("Bad block table written to 0x%012llx, version 0x%02X\n", + (unsigned long long)to, td->version[chip]); +//while(1); + /* Mark it as used */ + td->pages[chip] = page; + } + return 0; + + outerr: + pr_warn("nand_bbt: error while writing bad block table %d\n", res); + return res; +} + +/** + * write_bbt - [GENERIC] (Re)write the bad block table + * @mtd: MTD device structure + * @buf: temporary buffer + * @td: descriptor for the bad block table + * @md: descriptor for the bad block table mirror + * @chipsel: selector for a specific chip, -1 for all + * + * (Re)write the bad block table. + */ +static int write_bbt_multi(struct mtd_info *mtd, uint8_t *buf, + struct nand_bbt_descr *td, struct nand_bbt_descr *md, + int chipsel) +{ + struct nand_chip *this = mtd->priv; + struct erase_info einfo; + int i, j, res, chip = 0; + int bits, startblock, dir, page, offs, numblocks, sft, sftmsk; + int nrchips, bbtoffs, pageoffs, ooboffs; + uint8_t msk[16]; + uint8_t rcode = td->reserved_block_code; + size_t retlen, len = 0; + loff_t to; + struct mtd_oob_ops ops; + + ops.ooblen = mtd->oobsize; + ops.ooboffs = 0; + ops.datbuf = NULL; + ops.mode = MTD_OPS_PLACE_OOB; + + if (!rcode) + rcode = 0xff; + /* Write bad block table per chip rather than per device? */ + if (td->options & NAND_BBT_PERCHIP) { + //numblocks = (int)(this->chipsize >> this->bbt_erase_shift); + numblocks = ((int)(this->chipsize >> (10+this->pagecnt_shift)))/mtd->pageSizek; + /* Full device write or specific chip? */ + if (chipsel == -1) { + nrchips = this->numchips; + } else { + nrchips = chipsel + 1; + chip = chipsel; + } + } else { + //numblocks = (int)(mtd->size >> this->bbt_erase_shift); + numblocks = ((int)(mtd->size >> (10+this->pagecnt_shift)))/mtd->pageSizek; + nrchips = 1; + } + + /* Loop through the chips */ + for (; chip < nrchips; chip++) { + /* + * There was already a version of the table, reuse the page + * This applies for absolute placement too, as we have the + * page nr. in td->pages. + */ + if (td->pages[chip] != -1) { + page = td->pages[chip]; + goto write; + } + + /* + * Automatic placement of the bad block table. Search direction + * top -> down? + */ + if (td->options & NAND_BBT_LASTBLOCK) { + startblock = numblocks * (chip + 1) - 1; + dir = -1; + } else { + startblock = chip * numblocks; + dir = 1; + } + + for (i = 0; i < td->maxblocks; i++) { + int block = startblock + dir * i; + //printk("blockstatus=%x\n",((this->bbt[block >> 1] >> (2 * (block & 0x01))) & 0x0F)); + //printk("block=%x, sht=%d\n",block,(4 * (block & 0x01)) & 0x0F); + /* Check, if the block is bad */ + switch ((this->bbt[block >> 1] >> + (4 * (block & 0x01))) & 0x0F) { + case 0x01: + case 0x04: + case 0x05://case 0x07: case 0x0D: not exist for bad_fact+bad_wort + case 0x03://case 0x0B: case 0x0E: not exist for bad_fact+reserved + case 0x0C://case 0x02: case 0x08: not exist for good + reserved + case 0x0F://case 0x06: case 0x09: not exist for bad_wort+reserved + continue; + //case 0x00: case 0x0A: only good or reserved is used (so far no reserved) + } + page = block << + //(this->bbt_erase_shift - this->page_shift); + this->pagecnt_shift; + /* Check, if the block is used by the mirror table */ + if (!md || md->pages[chip] != page) + goto write; + } + pr_err("No space left to write bad block table\n"); + return -ENOSPC; + write: + + /* Set up shift count and masks for the flash table */ + bits = td->options & NAND_BBT_NRBITS_MSK; + if (this->realplanenum) + bits<<=1; + msk[2] = ~rcode; + switch (bits) { + case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01; + msk[3] = 0x01; + break; + case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01; + msk[3] = 0x03; + break; + case 4: sft = 1; sftmsk = 0x04; + msk[0] = 0x00; msk[1] = 0x01; msk[2] = 0x2; msk[3] = 0x03; + msk[4] = 0x04; msk[5] = 0x05; msk[6] = 0x06; msk[7] = 0x07; + msk[8] = 0x08; msk[9] = 0x09; msk[10] = /*~rcode*/0x0a; msk[11] = 0x0b; + msk[12] = 0x0c; msk[13] = 0x0d; msk[14] = 0x0e; msk[15] = 0x0f; + break; + case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F; + msk[3] = 0xff; + break; + default: return -EINVAL; + } + + bbtoffs = chip * (numblocks >> 2); + if (this->realplanenum) + bbtoffs = chip * (numblocks >> 1); + + //to = ((loff_t)page) << this->page_shift; + to = ((loff_t) (page*mtd->pageSizek)) << 10; + + /* Must we save the block contents? */ + if (td->options & NAND_BBT_SAVECONTENT) { + /* Make it block aligned */printk("write bbt multi inlegal-----------------\n"); + //to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1)); + to &= ~((loff_t)((mtd->erasesize) - 1));//danbbg + //len = 1 << this->bbt_erase_shift; + len = mtd->erasesize; + res = mtd_read(mtd, to, len, &retlen, buf); + if (res < 0) { + if (retlen != len) { + pr_info("nand_bbt: error reading block " + "for writing the bad block table\n"); + return res; + } + pr_warn("nand_bbt: ECC error while reading " + "block for writing bad block table\n"); + } + /* Read oob data */ + //ops.ooblen = (len >> this->page_shift) * mtd->oobsize; + ops.ooblen = (mtd->pagecnt) * mtd->oobsize; + ops.oobbuf = &buf[len]; + res = mtd_read_oob(mtd, to + mtd->writesize, &ops); + if (res < 0 || ops.oobretlen != ops.ooblen) + goto outerr; + + /* Calc the byte offset in the buffer */ + //pageoffs = page - (int)(to >> this->page_shift); + pageoffs = page - ((int)(to >> 10))/mtd->pageSizek; + //offs = pageoffs << this->page_shift; + offs = (pageoffs*mtd->pageSizek) << 10; + /* Preset the bbt area with 0xff */ + memset(&buf[offs], 0xff, (size_t)(numblocks >> sft)); + ooboffs = len + (pageoffs * mtd->oobsize); + + } else if (td->options & NAND_BBT_NO_OOB) { + ooboffs = 0; + offs = td->len; + /* The version byte */ + if (td->options & NAND_BBT_VERSION) + offs++; + /* Calc length */ + len = (size_t)(numblocks >> sft); + len += offs; + /* Make it page aligned! */ + len = ALIGN(len, mtd->writesize); + /* Preset the buffer with 0xff */ + memset(buf, 0xff, len); + /* Pattern is located at the begin of first page */ + memcpy(buf, td->pattern, td->len); + } else { + /* Calc length */ + len = (size_t)(numblocks >> sft); + /* Make it page aligned! */ + len = ALIGN(len, mtd->writesize); + if (len < mtd->writesize) + len = mtd->writesize; + /* Preset the buffer with 0xff */ + memset(buf, 0xff, len + + //(len >> this->page_shift)* mtd->oobsize); + mtd->pagecnt* mtd->oobsize); + offs = 0; + ooboffs = len; + /* Pattern is located in oob area of first page */ + memcpy(&buf[ooboffs + td->offs], td->pattern, td->len); + //printk("td->len=%d ooboffs=%d td->offs=%d\n", td->len, ooboffs, td->offs); + } + + if (td->options & NAND_BBT_VERSION) + buf[ooboffs + td->veroffs] = td->version[chip]; + + /* Walk through the memory table */ + for (i = 0; i < numblocks;) { + uint8_t dat; + dat = this->bbt[bbtoffs + (i >> 1)]; + for (j = 0; j < 2; j++, i++) { + int sftcnt = (i << (3 - sft)) & sftmsk; + /* Do not store the reserved bbt blocks! */ + /* dannier 2014/03/01 add a condition only retry and bbt blocks are not store + read retry command fail blocks are marked as reserved blk and need to be stored to flash */ + if (i >= (numblocks - td->maxblocks - 4) && (dat&0xF)==0xa) { + //buf[offs + (i >> sft)] &= ~(msk[dat & 0x0F] << sftcnt); + //printk("offs + (i >> sft)=%d data=0x%x, dat=0x%x sft=%d\n",offs + (i >> sft), ~(msk[dat & 0x0F] << sftcnt), dat, sft); + } else + buf[offs + (i >> sft)] &= ~(msk[dat & 0x0F] << sftcnt); + + dat >>= 4; + } + } + memcpy(&buf[mtd->realwritesize], buf, (numblocks>>1)); +//printk("print bbt write info ");print_nand_buffer(buf, 1536); +/*printk("Bad block table written to 0x%012llx, version 0x%02X\n", + (unsigned long long)to, td->version[chip]);dump_stack();while(1);*/ +//printk("erase blk=%d, page=0x%x len=%d copy=%d\n", (unsigned int)(to>>this->bbt_erase_shift), page, len, (numblocks>>1)); memset(&einfo, 0, sizeof(einfo)); einfo.mtd = mtd; einfo.addr = to; - einfo.len = 1 << this->bbt_erase_shift; + //einfo.len = 1 << this->bbt_erase_shift; + einfo.len = mtd->erasesize; res = nand_erase_nand(mtd, &einfo, 1); + //printk("erase ret=%d\n",res); if (res < 0) goto outerr; @@ -849,7 +1700,154 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf, return 0; outerr: - pr_warn("nand_bbt: error while writing bad block table %d\n", res); + pr_warn("nand_bbt: multi error while writing bad block table %d\n", res); + return res; +} + +void copy_retry_info_to_buf(struct mtd_info *mtd, uint8_t *buf) +{ + /*uint8_t *bf; + struct nand_read_retry_param *rdtry = (struct nand_read_retry_param *)buf;*/ + struct nand_chip *this = mtd->priv; + + memcpy(buf, /*(uint8_t *)*/this->cur_chip, sizeof(chip_table[0])-16); + #ifdef RETRY_DEBUG + print_nand_buffer((uint8_t *)this->cur_chip, sizeof(chip_table[0])); + #endif + + /* + memcpy(buf, "ANDROID!", 8); + rdtry->nand_id = FlashId;//this->nand_id; + //rdtry->nand_id_5th = this->nand_id_5th; + rdtry->eslc_reg_num = this->eslc_reg_num; + rdtry->total_retry_cnt = this->total_retry_cnt; + rdtry->retry_reg_num = this->retry_reg_num; + bf = buf + 28; + if (this->eslc_reg_num) + memcpy(bf, this->eslc_cmd, this->eslc_reg_num); + bf = buf + this->eslc_reg_num; + if (this->retry_reg_num) + memcpy(bf, this->retry_cmd, this->retry_reg_num * this->total_retry_cnt); + else + printk("no retry param is writen to retry table block\n"); + + printk("save rdtry to block\n"); + print_nand_buffer(buf, 128); + */ +} + +/** + * write_hynix_table - [GENERIC] (Re)write the hynix table + * + * @mtd: MTD device structure + * @buf: temporary buffer + * @td: descriptor for the retry table block + * @md: descriptor for the bad block table mirror + * @chipsel: selector for a specific chip, -1 for all + * + * (Re)write the bad block table + * +*/ +static int write_hynix_table(struct mtd_info *mtd, uint8_t *buf, + struct nand_bbt_descr *td, int chipsel) +{ + struct nand_chip *this = mtd->priv; + struct erase_info einfo; + int i, res, chip = 0; + int startblock, dir, page, numblocks, nrchips; + uint8_t rcode = td->reserved_block_code; + size_t len = 0; + loff_t to; + struct mtd_oob_ops ops; + + ops.ooblen = mtd->oobsize; + ops.ooboffs = 0; + ops.datbuf = NULL; + ops.mode = MTD_OPS_PLACE_OOB; + + if (!rcode) + rcode = 0xff; + + //numblocks = (int)(this->chipsize >> this->bbt_erase_shift); + numblocks = ((int)(this->chipsize >> (10+this->pagecnt_shift)))/mtd->pageSizek; + nrchips = chipsel + 1; + chip = chipsel; + + /* Loop through the chips */ + for (; chip < nrchips; chip++) { + + /* Search direction top -> down ? */ + startblock = numblocks * (chip + 1) - 5; + dir = -1; + + for (i = 0; i < td->maxblocks; i++) { + int block = startblock + dir * i; + /* Check, if the block is bad */ + if (this->realplanenum) { + switch ((this->bbt[block >> 1] >> + (4 * (block & 0x01))) & 0x0F) { + case 0x01: + case 0x04: + case 0x05://case 0x07: case 0x0D: not exist for bad_fact+bad_wort + case 0x03://case 0x0B: case 0x0E: not exist for bad_fact+reserved + case 0x0C://case 0x02: case 0x08: not exist for good + reserved + case 0x0F://case 0x06: case 0x09: not exist for bad_wort+reserved + continue; + //case 0x00: case 0x0A: only good or reserved is used (so far no reserved) + } + } else { + switch ((this->bbt[block >> 2] >> + (2 * (block & 0x03))) & 0x03) { + case 0x01: + case 0x03: + continue; + } + } + //page = block << (this->bbt_erase_shift - this->page_shift); + page = block << this->pagecnt_shift; + goto write; + } + printk(KERN_ERR "No space left to write read retry table\n"); + return -ENOSPC; + write: + + //to = ((loff_t) page) << this->page_shift; + to = ((loff_t) (page*mtd->pageSizek)) << 10; + len = mtd->writesize; + /* Preset the buffer with 0xff */ + //memset(buf, 0xff, len + (len >> this->page_shift)* mtd->oobsize); + memset(buf, 0xff, len + mtd->pagecnt* mtd->oobsize); + /* Pattern is located in oob area of first page */ + memcpy(&buf[len], td->pattern, 10); + + //------write signature into buf retry into--/ + #ifdef RETRY_DEBUG + printk("save rdtry to page=0x%x\n", page); + #endif + copy_retry_info_to_buf(mtd, buf); + + //------erase block-----------/ + memset(&einfo, 0, sizeof(einfo)); + einfo.mtd = mtd; + einfo.addr = to; + //einfo.len = 1 << this->bbt_erase_shift; + einfo.len = mtd->erasesize; + res = nand_erase_nand(mtd, &einfo, 1); + if (res < 0) + goto outerr; + printk("writing rdtry to nand flash and page addr is 0x%x, len=0x%x\n", page, len); + res = scan_write_bbt(mtd, to, len, buf, &buf[len]); + if (res < 0) + goto outerr; + + /* Mark it as used */ + td->pages[chip] = page; + } + return 0; + + outerr: + printk(KERN_WARNING + "nand_bbt: Error while writing read retry table %d\n", res); return res; } @@ -866,7 +1864,10 @@ static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *b struct nand_chip *this = mtd->priv; bd->options &= ~NAND_BBT_SCANEMPTY; - return create_bbt(mtd, this->buffers->databuf, bd, -1); + if (this->realplanenum) + return create_bbt_multi(mtd, this->buffers->databuf, bd, -1); + else + return create_bbt(mtd, this->buffers->databuf, bd, -1); } /** @@ -939,8 +1940,19 @@ static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc continue; /* Create the table in memory by scanning the chip(s) */ - if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY)) - create_bbt(mtd, buf, bd, chipsel); + if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY)) { + //print_nand_register(mtd); + if (mtd->dwRdmz) + reset_nfc(mtd, NULL, 3); + //print_nand_register(mtd); + + if (this->realplanenum) + create_bbt_multi(mtd, buf, bd, chipsel); + else + create_bbt(mtd, buf, bd, chipsel); + + + } td->version[i] = 1; if (md) @@ -982,14 +1994,20 @@ static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc /* Write the bad block table to the device? */ if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) { - res = write_bbt(mtd, buf, td, md, chipsel); + if (this->realplanenum) + res = write_bbt_multi(mtd, buf, td, md, chipsel); + else + res = write_bbt(mtd, buf, td, md, chipsel); if (res < 0) return res; } /* Write the mirror bad block table to the device? */ if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) { - res = write_bbt(mtd, buf, md, td, chipsel); + if (this->realplanenum) + res = write_bbt_multi(mtd, buf, md, td, chipsel); + else + res = write_bbt(mtd, buf, md, td, chipsel); if (res < 0) return res; } @@ -997,6 +2015,53 @@ static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc return 0; } +static int check_retry_table(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *rd) +{ + int i, chips, chipsel, res = 0, need_save = 0; + struct nand_chip *this = mtd->priv; + + /* Do we have a retry table per chip ? */ + /* so far, use chip 0 retry param on chip 0 and chip 1 */ + //chips = this->numchips; + chips = 1; + for (i = 0; i < chips; i++) { + /* Per chip */ + chipsel = i; + if (rd->pages[i] == -1) { + goto create; + } + #ifdef RETRY_DEBUG + printk("read_abs_retry_table\n"); + #endif + /* Read the retry table starting at a given page */ + res = read_abs_retry_table(mtd, buf, rd, chipsel); + if (res == 0) { + if(this->cur_chip != NULL) { + this->select_chip(mtd, 0); + this->cur_chip->get_parameter(mtd, READ_RETRY_MODE); + this->select_chip(mtd, -1); + } + break; + } + + create: + #ifdef RETRY_DEBUG + printk("create_hynix_table\n"); + #endif + /* Create the table in memory by get feature or get otp cmd */ + create_hynix_table(mtd, chipsel); + + need_save = 1; + + //printk("dannier write_hynix_table\n"); + /* Write the retry block table to the device ? => leave it saved after bbt searched*/ + /*res = write_hynix_table(mtd, buf, rd, chipsel); + if (res < 0) + return res;*/ + } + + return need_save; +} /** * mark_bbt_regions - [GENERIC] mark the bad block table regions * @mtd: MTD device structure @@ -1014,10 +2079,12 @@ static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td) /* Do we have a bbt per chip? */ if (td->options & NAND_BBT_PERCHIP) { chips = this->numchips; - nrblocks = (int)(this->chipsize >> this->bbt_erase_shift); + //nrblocks = (int)(this->chipsize >> this->bbt_erase_shift); + nrblocks = ((int)(this->chipsize >> (10+this->pagecnt_shift)))/mtd->pageSizek; } else { chips = 1; - nrblocks = (int)(mtd->size >> this->bbt_erase_shift); + //nrblocks = (int)(mtd->size >> this->bbt_erase_shift); + nrblocks = ((int)(mtd->size >> (10+this->pagecnt_shift)))/mtd->pageSizek; } for (i = 0; i < chips; i++) { @@ -1025,19 +2092,25 @@ static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td) !(td->options & NAND_BBT_WRITE)) { if (td->pages[i] == -1) continue; - block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift); + //block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift); + block = td->pages[i] >> (this->pagecnt_shift); block <<= 1; oldval = this->bbt[(block >> 3)]; newval = oldval | (0x2 << (block & 0x06)); this->bbt[(block >> 3)] = newval; - if ((oldval != newval) && td->reserved_block_code) - nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1)); + if ((oldval != newval) && 0/*td->reserved_block_code*/) + //nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1)); + nand_update_bbt(mtd, (loff_t)(block*mtd->pageSizek) << (10+this->pagecnt_shift-1)); continue; } update = 0; - if (td->options & NAND_BBT_LASTBLOCK) + if (td->options & NAND_BBT_LASTBLOCK) { block = ((i + 1) * nrblocks) - td->maxblocks; - else + if (td->pattern[0] == 'r' && td->pattern[1] == 'e') { + block = ((i + 1) * nrblocks) - td->maxblocks - 4; + //printk("mark_bbt_region set blocks =%d ~ %d\n", block, block+3); + } + } else block = i * nrblocks; block <<= 1; for (j = 0; j < td->maxblocks; j++) { @@ -1053,12 +2126,83 @@ static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td) * new ones have been marked, then we need to update the stored * bbts. This should only happen once. */ - if (update && td->reserved_block_code) - nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1)); + if (update && 0/*td->reserved_block_code*/) + //nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1)); + nand_update_bbt(mtd, (loff_t)((block - 2)*mtd->pageSizek) << (10+this->pagecnt_shift-1)); } } /** + * mark_bbt_regions_multi - [GENERIC] mark the bad block table regions + * @mtd: MTD device structure + * @td: bad block table descriptor + * + * The bad block table regions are marked as "bad" to prevent accidental + * erasures / writes. The regions are identified by the mark 0x02. + */ +static void mark_bbt_region_multi(struct mtd_info *mtd, struct nand_bbt_descr *td) +{ + struct nand_chip *this = mtd->priv; + int i, j, chips, block, nrblocks, update; + uint8_t oldval, newval; + + /* Do we have a bbt per chip? */ + if (td->options & NAND_BBT_PERCHIP) { + chips = this->numchips; + //nrblocks = (int)(this->chipsize >> this->bbt_erase_shift); + nrblocks = ((int)(this->chipsize >> (10+this->pagecnt_shift)))/mtd->pageSizek; + } else { + chips = 1; + //nrblocks = (int)(mtd->size >> this->bbt_erase_shift); + nrblocks = ((int)(mtd->size >> (10+this->pagecnt_shift)))/mtd->pageSizek; + } + + for (i = 0; i < chips; i++) { + if ((td->options & NAND_BBT_ABSPAGE) || + !(td->options & NAND_BBT_WRITE)) { + if (td->pages[i] == -1) + continue; + //block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift); + block = td->pages[i] >> (this->pagecnt_shift); + block <<= 2; + oldval = this->bbt[(block >> 3)]; + newval = oldval | (0xA << (block & 0x04)); + this->bbt[(block >> 3)] = newval; + if ((oldval != newval) && 0/*td->reserved_block_code*/) + //nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 2)); + nand_update_bbt(mtd, (loff_t)(block*mtd->pageSizek) << (10+this->pagecnt_shift-2)); + continue; + } + update = 0; + if (td->options & NAND_BBT_LASTBLOCK) { + block = ((i + 1) * nrblocks) - td->maxblocks; + if (td->pattern[0] == 'r' && td->pattern[1] == 'e') { + block = ((i + 1) * nrblocks) - td->maxblocks - 4; + //printk("mark_bbt_region set blocks =%d ~ %d\n", block, block+3); + } + } else + block = i * nrblocks; + block <<= 2; + for (j = 0; j < td->maxblocks; j++) { + oldval = this->bbt[(block >> 3)]; + newval = oldval | (0xA << (block & 0x04)); + this->bbt[(block >> 3)] = newval; + if (oldval != newval) + update = 1; + block += 4; + } + /* + * If we want reserved blocks to be recorded to flash, and some + * new ones have been marked, then we need to update the stored + * bbts. This should only happen once. + */ + if (update && 0/*td->reserved_block_code*/) + //nand_update_bbt(mtd, (loff_t)(block - 4) << (this->bbt_erase_shift - 2)); + nand_update_bbt(mtd, (loff_t)((block - 4)*mtd->pageSizek) << (10+this->pagecnt_shift-2)); + }//print_nand_buffer(this->bbt, 2048); +} + +/** * verify_bbt_descr - verify the bad block description * @mtd: MTD device structure * @bd: the table to verify @@ -1078,6 +2222,8 @@ static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd) pattern_len = bd->len; bits = bd->options & NAND_BBT_NRBITS_MSK; + if (this->realplanenum) + bits<<=1; BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) && !(this->bbt_options & NAND_BBT_USE_FLASH)); @@ -1096,14 +2242,17 @@ static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd) } if (bd->options & NAND_BBT_PERCHIP) - table_size = this->chipsize >> this->bbt_erase_shift; + //table_size = this->chipsize >> this->bbt_erase_shift; + table_size = ((int)(this->chipsize >> (10+this->pagecnt_shift)))/mtd->pageSizek; else - table_size = mtd->size >> this->bbt_erase_shift; + //table_size = mtd->size >> this->bbt_erase_shift; + table_size = ((int)(mtd->size >> (10+this->pagecnt_shift)))/mtd->pageSizek; table_size >>= 3; table_size *= bits; if (bd->options & NAND_BBT_NO_OOB) table_size += pattern_len; - BUG_ON(table_size > (1 << this->bbt_erase_shift)); + //BUG_ON(table_size > (1 << this->bbt_erase_shift)); + BUG_ON(table_size > mtd->erasesize); } /** @@ -1118,15 +2267,21 @@ static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd) * The bad block table memory is allocated here. It must be freed by calling * the nand_free_bbt function. */ +extern struct nand_read_retry_param chip_table[]; int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd) { struct nand_chip *this = mtd->priv; - int len, res = 0; + int len, res = 0, i, need_save = 0; uint8_t *buf; struct nand_bbt_descr *td = this->bbt_td; struct nand_bbt_descr *md = this->bbt_md; + + this->bbt_plane[1] = this->bbt_plane[0] = 0; - len = mtd->size >> (this->bbt_erase_shift + 2); + //len = mtd->size >> (this->bbt_erase_shift + 2); + len = ((int)(mtd->size >> (10+this->pagecnt_shift+2)))/mtd->pageSizek; + if (this->realplanenum) + len <<=1; /* * Allocate memory (2bit per block) and clear the memory bad block * table. @@ -1135,6 +2290,8 @@ int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd) if (!this->bbt) return -ENOMEM; + /* Clear the memory bad block table */ + memset (this->bbt, 0x00, len); /* * If no primary table decriptor is given, scan the device to build a * memory based bad block table. @@ -1151,14 +2308,40 @@ int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd) verify_bbt_descr(mtd, md); /* Allocate a temporary buffer for one eraseblock incl. oob */ - len = (1 << this->bbt_erase_shift); - len += (len >> this->page_shift) * mtd->oobsize; + //len = (1 << this->bbt_erase_shift); + len = mtd->erasesize; + //len += (len >> this->page_shift) * mtd->oobsize; + len += (mtd->pagecnt) * mtd->oobsize; buf = vmalloc(len); if (!buf) { kfree(this->bbt); this->bbt = NULL; return -ENOMEM; } + + if (mtd->dwRetry /*&& (mtd->id>>24) == NAND_MFR_HYNIX*/) { + for (i = 0; /*i < READ_RETRY_CHIP_NUM*/; i++) { + if (chip_table[i].nand_id == 0 && chip_table[i].nand_id_5th == 0) + break; + if (mtd->id == chip_table[i].nand_id && mtd->id2 == chip_table[i].nand_id_5th) { + #ifdef RETRY_DEBUG + printk("get retry table id 0x%x, 0x%x\n", chip_table[i].nand_id, chip_table[i].nand_id_5th); + #endif + this->cur_chip = &chip_table[i]; + break; + } + } + if(this->cur_chip != NULL && chip_table[i].nand_id != 0) { + #ifdef RETRY_DEBUG + printk("search_hynix_retry_table\n"); + #endif + search_hynix_retry_table(mtd, buf, this->retry_pattern); + #ifdef RETRY_DEBUG + printk("check_retry_table\n"); + #endif + need_save = check_retry_table(mtd, buf, this->retry_pattern); + } + } /* Is the bbt at a given page? */ if (td->options & NAND_BBT_ABSPAGE) { @@ -1171,10 +2354,34 @@ int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd) if (res) res = check_create(mtd, buf, bd); + + if (mtd->dwRetry && this->cur_chip != NULL && need_save) { + //printk("dannier write_hynix_table\n"); + /* Write the retry block table to the device ? */ + res = write_hynix_table(mtd, buf, this->retry_pattern, 0); + + //testing + //this->cur_chip->cur_try_times = 5; + } + + /* Prevent the rdtry block regions from erasing / writing */ + if (this->realplanenum) + mark_bbt_region_multi(mtd, this->retry_pattern); + else + mark_bbt_region(mtd, this->retry_pattern); + /* Prevent the bbt regions from erasing / writing */ - mark_bbt_region(mtd, td); - if (md) - mark_bbt_region(mtd, md); + if (this->realplanenum) + mark_bbt_region_multi(mtd, td); + else + mark_bbt_region(mtd, td); + if (md) { + if (this->realplanenum) + mark_bbt_region_multi(mtd, md); + else + mark_bbt_region(mtd, md); + } + vfree(buf); return res; @@ -1200,15 +2407,20 @@ int nand_update_bbt(struct mtd_info *mtd, loff_t offs) return -EINVAL; /* Allocate a temporary buffer for one eraseblock incl. oob */ - len = (1 << this->bbt_erase_shift); - len += (len >> this->page_shift) * mtd->oobsize; - buf = kmalloc(len, GFP_KERNEL); - if (!buf) + //len = (1 << this->bbt_erase_shift); + len = mtd->erasesize; + //len += (len >> this->page_shift) * mtd->oobsize; + len += (mtd->pagecnt) * mtd->oobsize; + //buf = kmalloc(len, GFP_KERNEL); + buf = vmalloc(len); + if (!buf) { + printk(KERN_ERR "nand_update_bbt: Out of memory\n"); return -ENOMEM; - + } /* Do we have a bbt per chip? */ if (td->options & NAND_BBT_PERCHIP) { - chip = (int)(offs >> this->chip_shift); + //chip = (int)(offs >> this->chip_shift); + chip = ((int)(offs >> (10+this->pagecnt_shift)))/(mtd->pageSizek*mtd->blkcnt); chipsel = chip; } else { chip = 0; @@ -1221,17 +2433,24 @@ int nand_update_bbt(struct mtd_info *mtd, loff_t offs) /* Write the bad block table to the device? */ if (td->options & NAND_BBT_WRITE) { - res = write_bbt(mtd, buf, td, md, chipsel); + if (this->realplanenum) + res = write_bbt_multi(mtd, buf, td, md, chipsel); + else + res = write_bbt(mtd, buf, td, md, chipsel); if (res < 0) goto out; } /* Write the mirror bad block table to the device? */ if (md && (md->options & NAND_BBT_WRITE)) { - res = write_bbt(mtd, buf, md, td, chipsel); + if (this->realplanenum) + res = write_bbt_multi(mtd, buf, md, td, chipsel); + else + res = write_bbt(mtd, buf, md, td, chipsel); } out: - kfree(buf); + vfree(buf); + //printk("nand_update_bbt free mem res=%d\n", res); return res; } @@ -1307,6 +2526,7 @@ static struct nand_bbt_descr bbt_mirror_no_bbt_descr = { static int nand_create_badblock_pattern(struct nand_chip *this) { struct nand_bbt_descr *bd; + //struct mtd_info *mtd = this->priv; if (this->badblock_pattern) { pr_warn("Bad block pattern already allocated; not replacing\n"); return -EINVAL; @@ -1319,6 +2539,10 @@ static int nand_create_badblock_pattern(struct nand_chip *this) bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1; bd->pattern = scan_ff_pattern; bd->options |= NAND_BBT_DYNAMICSTRUCT; + //if ((0xFF&(mtd->id>>24)) == 0x45 || (0xFF&(mtd->id>>24)) == NAND_MFR_HYNIX) + bd->options |= NAND_BBT_SCAN2NDPAGE;//All type of flash need to scan 2 page per block. + bd->page_offset[0] = this->page_offset[0]; + bd->page_offset[1] = this->page_offset[1]; this->badblock_pattern = bd; return 0; } @@ -1366,6 +2590,11 @@ int nand_default_bbt(struct mtd_info *mtd) this->bbt_td = NULL; this->bbt_md = NULL; } + + if (this->bbt_td->reserved_block_code && this->realplanenum) { + this->bbt_td->reserved_block_code = 0x5; + this->bbt_md->reserved_block_code = 0x5; + } if (!this->badblock_pattern) nand_create_badblock_pattern(this); @@ -1379,30 +2608,113 @@ int nand_default_bbt(struct mtd_info *mtd) * @offs: offset in the device * @allowbbt: allow access to bad block table region */ -int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt) +int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt, int allow_readfail) { struct nand_chip *this = mtd->priv; int block; uint8_t res; /* Get block number * 2 */ - block = (int)(offs >> (this->bbt_erase_shift - 1)); + //block = (int)(offs >> (this->bbt_erase_shift - 1)); + block = ((int)(offs >> (10+this->pagecnt_shift - 1)))/mtd->pageSizek; res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03; - pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: " + pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08llx: " "(block %d) 0x%02x\n", - (unsigned int)offs, block >> 1, res); + offs, block >> 1, res); switch ((int)res) { case 0x00: return 0; case 0x01: + #if 0 + if (allowbbt != 0xFF && this->state == FL_READY) { + struct erase_info einfo; + int res1 = 0; + memset(&einfo, 0, sizeof(einfo)); + einfo.mtd = mtd; + einfo.addr = offs; + //einfo.len = (1 << this->bbt_erase_shift); + einfo.len = mtd->erasesize; + printk("einfo.addr is 0x%llx\n",einfo.addr); + printk("einfo.len is 0x%llx\n", einfo.len); + res1 = nand_erase_nand(mtd, &einfo, 0xFF); + if (res1 < 0) + printk("nand_erase_nand addr 0x%llx result is %d\n", einfo.addr, res1); + } + #endif return 1; case 0x02: - return allowbbt ? 0 : 1; + if ((block>>1) < (mtd->blkcnt - 8)) { + if (allow_readfail) + return 0; + else + return 1; + } else + return allowbbt ? 0 : 1; } return 1; } +/** + * nand_isbad_bbt_multi - [NAND Interface] Check if a block is bad + * @mtd: MTD device structure + * @offs: offset in the device + * @allowbbt: allow access to bad block table region + */ +int nand_isbad_bbt_multi(struct mtd_info *mtd, loff_t offs, int allowbbt, int allow_readfail) +{ + struct nand_chip *this = mtd->priv; + int block; + uint8_t res; + + /* Get block number * 4 */ + //block = (int)(offs >> (this->bbt_erase_shift - 2)); + block = ((int)(offs >> (10+this->pagecnt_shift - 2)))/mtd->pageSizek; + res = (this->bbt[block >> 3] >> (block & 0x4)) & 0x0F; + + pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08llx: " + "(block %d) 0x%02x\n", + offs, block >> 2, res); + /*printk("nand_isbad_bbt(): bbt info for offs 0x%08llx: " + "(block %d) 0x%02x\n", + offs, block >> 2, res);*/ + + switch ((int)res) { + case 0x00: + return 0; + case 0x01: + case 0x04: + case 0x05://1 or both 2 blocks worn out! + #if 0 + if (allowbbt != 0xFF && this->state == FL_READY) { + struct erase_info einfo; + int res1 = 0; + memset(&einfo, 0, sizeof(einfo)); + einfo.mtd = mtd; + einfo.addr = offs; + //einfo.len = (1 << this->bbt_erase_shift); + einfo.len = mtd->erasesize; + printk("einfo.addr is 0x%llx\n",einfo.addr); + printk("einfo.len is 0x%llx\n", einfo.len); + res1 = nand_erase_nand(mtd, &einfo, 0xFF); + if (res1 < 0) + printk("nand_erase_nand addr 0x%llx result is %d\n", einfo.addr, res1); + } + #endif + return 1; + case 0x0A://usually two block are reserved + if ((block>>2) < (mtd->blkcnt - 8)) { + if (allow_readfail) + return 0; + else + return 1; + } else + return allowbbt ? 0 : 1; + } + return 1; +} + + EXPORT_SYMBOL(nand_scan_bbt); EXPORT_SYMBOL(nand_default_bbt); |