upstream u-boot with additional patches for our devices/boards:
https://lists.denx.de/pipermail/u-boot/2017-March/282789.html (AXP crashes) ;
Gbit ethernet patch for some LIME2 revisions ;
with SPI flash support
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1374 lines
37 KiB
1374 lines
37 KiB
20 years ago
|
/*
|
||
|
* Overview:
|
||
|
* Bad block table support for the NAND driver
|
||
19 years ago
|
*
|
||
12 years ago
|
* Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
|
||
20 years ago
|
*
|
||
|
* This program is free software; you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License version 2 as
|
||
|
* published by the Free Software Foundation.
|
||
|
*
|
||
|
* Description:
|
||
|
*
|
||
19 years ago
|
* When nand_scan_bbt is called, then it tries to find the bad block table
|
||
13 years ago
|
* depending on the options in the BBT descriptor(s). If no flash based BBT
|
||
12 years ago
|
* (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
|
||
13 years ago
|
* marked good / bad blocks. This information is used to create a memory BBT.
|
||
|
* Once a new bad block is discovered then the "factory" information is updated
|
||
|
* on the device.
|
||
|
* If a flash based BBT is specified then the function first tries to find the
|
||
|
* BBT on flash. If a BBT is found then the contents are read and the memory
|
||
|
* based BBT is created. If a mirrored BBT is selected then the mirror is
|
||
|
* searched too and the versions are compared. If the mirror has a greater
|
||
12 years ago
|
* version number, then the mirror BBT is used to build the memory based BBT.
|
||
20 years ago
|
* If the tables are not versioned, then we "or" the bad block information.
|
||
13 years ago
|
* If one of the BBTs is out of date or does not exist it is (re)created.
|
||
|
* If no BBT exists at all then the device is scanned for factory marked
|
||
19 years ago
|
* good / bad blocks and the bad block tables are created.
|
||
20 years ago
|
*
|
||
13 years ago
|
* For manufacturer created BBTs like the one found on M-SYS DOC devices
|
||
|
* the BBT is searched and read but never created
|
||
20 years ago
|
*
|
||
13 years ago
|
* The auto generated bad block table is located in the last good blocks
|
||
19 years ago
|
* of the device. The table is mirrored, so it can be updated eventually.
|
||
13 years ago
|
* The table is marked in the OOB area with an ident pattern and a version
|
||
|
* number which indicates which of both tables is more up to date. If the NAND
|
||
|
* controller needs the complete OOB area for the ECC information then the
|
||
12 years ago
|
* option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
|
||
|
* course): it moves the ident pattern and the version byte into the data area
|
||
|
* and the OOB area will remain untouched.
|
||
20 years ago
|
*
|
||
|
* The table uses 2 bits per block
|
||
13 years ago
|
* 11b: block is good
|
||
|
* 00b: block is factory marked bad
|
||
17 years ago
|
* 01b, 10b: block is marked bad due to wear
|
||
20 years ago
|
*
|
||
|
* The memory bad block table uses the following scheme:
|
||
|
* 00b: block is good
|
||
|
* 01b: block is marked bad due to wear
|
||
|
* 10b: block is reserved (to protect the bbt area)
|
||
|
* 11b: block is factory marked bad
|
||
19 years ago
|
*
|
||
20 years ago
|
* Multichip devices like DOC store the bad block info per floor.
|
||
|
*
|
||
|
* Following assumptions are made:
|
||
|
* - bbts start at a page boundary, if autolocated on a block boundary
|
||
17 years ago
|
* - the space necessary for a bbt in FLASH does not exceed a block boundary
|
||
19 years ago
|
*
|
||
20 years ago
|
*/
|
||
|
|
||
10 years ago
|
#include <common.h>
|
||
|
#include <malloc.h>
|
||
|
#include <linux/compat.h>
|
||
20 years ago
|
#include <linux/mtd/mtd.h>
|
||
12 years ago
|
#include <linux/mtd/bbm.h>
|
||
7 years ago
|
#include <linux/mtd/rawnand.h>
|
||
13 years ago
|
#include <linux/bitops.h>
|
||
12 years ago
|
#include <linux/string.h>
|
||
11 years ago
|
|
||
|
#define BBT_BLOCK_GOOD 0x00
|
||
|
#define BBT_BLOCK_WORN 0x01
|
||
|
#define BBT_BLOCK_RESERVED 0x02
|
||
|
#define BBT_BLOCK_FACTORY_BAD 0x03
|
||
20 years ago
|
|
||
11 years ago
|
#define BBT_ENTRY_MASK 0x03
|
||
|
#define BBT_ENTRY_SHIFT 2
|
||
|
|
||
|
static int nand_update_bbt(struct mtd_info *mtd, loff_t offs);
|
||
|
|
||
|
static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
|
||
|
{
|
||
|
uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
|
||
|
entry >>= (block & BBT_ENTRY_MASK) * 2;
|
||
|
return entry & BBT_ENTRY_MASK;
|
||
|
}
|
||
|
|
||
|
static inline void bbt_mark_entry(struct nand_chip *chip, int block,
|
||
|
uint8_t mark)
|
||
|
{
|
||
|
uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
|
||
|
chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
|
||
|
}
|
||
20 years ago
|
|
||
13 years ago
|
static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
|
||
|
{
|
||
12 years ago
|
if (memcmp(buf, td->pattern, td->len))
|
||
|
return -1;
|
||
|
return 0;
|
||
13 years ago
|
}
|
||
|
|
||
19 years ago
|
/**
|
||
20 years ago
|
* check_pattern - [GENERIC] check if a pattern is in the buffer
|
||
12 years ago
|
* @buf: the buffer to search
|
||
|
* @len: the length of buffer to search
|
||
|
* @paglen: the pagelength
|
||
|
* @td: search pattern descriptor
|
||
20 years ago
|
*
|
||
12 years ago
|
* Check for a pattern at the given place. Used to search bad block tables and
|
||
11 years ago
|
* good / bad block identifiers.
|
||
12 years ago
|
*/
|
||
17 years ago
|
static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
|
||
20 years ago
|
{
|
||
13 years ago
|
if (td->options & NAND_BBT_NO_OOB)
|
||
|
return check_pattern_no_oob(buf, td);
|
||
|
|
||
20 years ago
|
/* Compare the pattern */
|
||
11 years ago
|
if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
|
||
12 years ago
|
return -1;
|
||
13 years ago
|
|
||
20 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
|
/**
|
||
17 years ago
|
* check_short_pattern - [GENERIC] check if a pattern is in the buffer
|
||
12 years ago
|
* @buf: the buffer to search
|
||
|
* @td: search pattern descriptor
|
||
17 years ago
|
*
|
||
12 years ago
|
* Check for a pattern at the given place. Used to search bad block tables and
|
||
|
* good / bad block identifiers. Same as check_pattern, but no optional empty
|
||
|
* check.
|
||
|
*/
|
||
17 years ago
|
static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
|
||
|
{
|
||
|
/* Compare the pattern */
|
||
12 years ago
|
if (memcmp(buf + td->offs, td->pattern, td->len))
|
||
|
return -1;
|
||
17 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* add_marker_len - compute the length of the marker in data area
|
||
12 years ago
|
* @td: BBT descriptor used for computation
|
||
13 years ago
|
*
|
||
12 years ago
|
* The length will be 0 if the marker is located in OOB area.
|
||
13 years ago
|
*/
|
||
|
static u32 add_marker_len(struct nand_bbt_descr *td)
|
||
|
{
|
||
|
u32 len;
|
||
|
|
||
|
if (!(td->options & NAND_BBT_NO_OOB))
|
||
|
return 0;
|
||
|
|
||
|
len = td->len;
|
||
|
if (td->options & NAND_BBT_VERSION)
|
||
|
len++;
|
||
|
return len;
|
||
|
}
|
||
|
|
||
|
/**
|
||
20 years ago
|
* read_bbt - [GENERIC] Read the bad block table starting from page
|
||
12 years ago
|
* @mtd: MTD device structure
|
||
|
* @buf: temporary buffer
|
||
|
* @page: the starting page
|
||
|
* @num: the number of bbt descriptors to read
|
||
|
* @td: the bbt describtion table
|
||
11 years ago
|
* @offs: block number offset in the table
|
||
20 years ago
|
*
|
||
|
* Read the bad block table starting from page.
|
||
|
*/
|
||
17 years ago
|
static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
|
||
13 years ago
|
struct nand_bbt_descr *td, int offs)
|
||
20 years ago
|
{
|
||
12 years ago
|
int res, ret = 0, i, j, act = 0;
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
20 years ago
|
size_t retlen, len, totlen;
|
||
|
loff_t from;
|
||
13 years ago
|
int bits = td->options & NAND_BBT_NRBITS_MSK;
|
||
12 years ago
|
uint8_t msk = (uint8_t)((1 << bits) - 1);
|
||
13 years ago
|
u32 marker_len;
|
||
|
int reserved_block_code = td->reserved_block_code;
|
||
20 years ago
|
|
||
|
totlen = (num * bits) >> 3;
|
||
13 years ago
|
marker_len = add_marker_len(td);
|
||
12 years ago
|
from = ((loff_t)page) << this->page_shift;
|
||
19 years ago
|
|
||
20 years ago
|
while (totlen) {
|
||
12 years ago
|
len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
|
||
13 years ago
|
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;
|
||
|
}
|
||
12 years ago
|
res = mtd_read(mtd, from, len, &retlen, buf);
|
||
20 years ago
|
if (res < 0) {
|
||
12 years ago
|
if (mtd_is_eccerr(res)) {
|
||
10 years ago
|
pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
|
||
|
from & ~mtd->writesize);
|
||
12 years ago
|
return res;
|
||
|
} else if (mtd_is_bitflip(res)) {
|
||
10 years ago
|
pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
|
||
|
from & ~mtd->writesize);
|
||
12 years ago
|
ret = res;
|
||
|
} else {
|
||
|
pr_info("nand_bbt: error reading BBT\n");
|
||
20 years ago
|
return res;
|
||
|
}
|
||
19 years ago
|
}
|
||
20 years ago
|
|
||
|
/* Analyse data */
|
||
|
for (i = 0; i < len; i++) {
|
||
|
uint8_t dat = buf[i];
|
||
11 years ago
|
for (j = 0; j < 8; j += bits, act++) {
|
||
20 years ago
|
uint8_t tmp = (dat >> j) & msk;
|
||
|
if (tmp == msk)
|
||
|
continue;
|
||
17 years ago
|
if (reserved_block_code && (tmp == reserved_block_code)) {
|
||
12 years ago
|
pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
|
||
11 years ago
|
(loff_t)(offs + act) <<
|
||
|
this->bbt_erase_shift);
|
||
|
bbt_mark_entry(this, offs + act,
|
||
|
BBT_BLOCK_RESERVED);
|
||
17 years ago
|
mtd->ecc_stats.bbtblocks++;
|
||
20 years ago
|
continue;
|
||
|
}
|
||
11 years ago
|
/*
|
||
|
* 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 + act) <<
|
||
|
this->bbt_erase_shift);
|
||
12 years ago
|
/* Factory marked bad or worn out? */
|
||
20 years ago
|
if (tmp == 0)
|
||
11 years ago
|
bbt_mark_entry(this, offs + act,
|
||
|
BBT_BLOCK_FACTORY_BAD);
|
||
20 years ago
|
else
|
||
11 years ago
|
bbt_mark_entry(this, offs + act,
|
||
|
BBT_BLOCK_WORN);
|
||
17 years ago
|
mtd->ecc_stats.badblocks++;
|
||
19 years ago
|
}
|
||
20 years ago
|
}
|
||
|
totlen -= len;
|
||
|
from += len;
|
||
|
}
|
||
12 years ago
|
return ret;
|
||
20 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
|
||
12 years ago
|
* @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; applies only if
|
||
|
* NAND_BBT_PERCHIP option is set
|
||
20 years ago
|
*
|
||
12 years ago
|
* Read the bad block table for all chips starting at a given page. We assume
|
||
|
* that the bbt bits are in consecutive order.
|
||
|
*/
|
||
17 years ago
|
static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
|
||
20 years ago
|
{
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
20 years ago
|
int res = 0, i;
|
||
|
|
||
|
if (td->options & NAND_BBT_PERCHIP) {
|
||
|
int offs = 0;
|
||
|
for (i = 0; i < this->numchips; i++) {
|
||
|
if (chip == -1 || chip == i)
|
||
13 years ago
|
res = read_bbt(mtd, buf, td->pages[i],
|
||
|
this->chipsize >> this->bbt_erase_shift,
|
||
|
td, offs);
|
||
20 years ago
|
if (res)
|
||
|
return res;
|
||
11 years ago
|
offs += this->chipsize >> this->bbt_erase_shift;
|
||
20 years ago
|
}
|
||
|
} else {
|
||
13 years ago
|
res = read_bbt(mtd, buf, td->pages[0],
|
||
|
mtd->size >> this->bbt_erase_shift, td, 0);
|
||
20 years ago
|
if (res)
|
||
|
return res;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
12 years ago
|
/* BBT marker is in the first page, no OOB */
|
||
|
static int scan_read_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
|
||
13 years ago
|
struct nand_bbt_descr *td)
|
||
|
{
|
||
|
size_t retlen;
|
||
|
size_t len;
|
||
|
|
||
|
len = td->len;
|
||
|
if (td->options & NAND_BBT_VERSION)
|
||
|
len++;
|
||
|
|
||
12 years ago
|
return mtd_read(mtd, offs, len, &retlen, buf);
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* scan_read_oob - [GENERIC] Scan data+OOB region to buffer
|
||
|
* @mtd: MTD device structure
|
||
|
* @buf: temporary buffer
|
||
|
* @offs: offset at which to scan
|
||
|
* @len: length of data region to read
|
||
|
*
|
||
|
* Scan read data from data+OOB. May traverse multiple pages, interleaving
|
||
|
* page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"
|
||
|
* ECC condition (error or bitflip). May quit on the first (non-ECC) error.
|
||
17 years ago
|
*/
|
||
12 years ago
|
static int scan_read_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
|
||
17 years ago
|
size_t len)
|
||
|
{
|
||
|
struct mtd_oob_ops ops;
|
||
12 years ago
|
int res, ret = 0;
|
||
17 years ago
|
|
||
12 years ago
|
ops.mode = MTD_OPS_PLACE_OOB;
|
||
17 years ago
|
ops.ooboffs = 0;
|
||
|
ops.ooblen = mtd->oobsize;
|
||
|
|
||
13 years ago
|
while (len > 0) {
|
||
12 years ago
|
ops.datbuf = buf;
|
||
|
ops.len = min(len, (size_t)mtd->writesize);
|
||
|
ops.oobbuf = buf + ops.len;
|
||
13 years ago
|
|
||
12 years ago
|
res = mtd_read_oob(mtd, offs, &ops);
|
||
|
if (res) {
|
||
|
if (!mtd_is_bitflip_or_eccerr(res))
|
||
13 years ago
|
return res;
|
||
12 years ago
|
else if (mtd_is_eccerr(res) || !ret)
|
||
|
ret = res;
|
||
13 years ago
|
}
|
||
|
|
||
|
buf += mtd->oobsize + mtd->writesize;
|
||
|
len -= mtd->writesize;
|
||
12 years ago
|
offs += mtd->writesize;
|
||
13 years ago
|
}
|
||
12 years ago
|
return ret;
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
static int scan_read(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
|
||
13 years ago
|
size_t len, struct nand_bbt_descr *td)
|
||
|
{
|
||
|
if (td->options & NAND_BBT_NO_OOB)
|
||
12 years ago
|
return scan_read_data(mtd, buf, offs, td);
|
||
13 years ago
|
else
|
||
12 years ago
|
return scan_read_oob(mtd, buf, offs, len);
|
||
17 years ago
|
}
|
||
|
|
||
12 years ago
|
/* Scan write data with oob to flash */
|
||
17 years ago
|
static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
|
||
|
uint8_t *buf, uint8_t *oob)
|
||
|
{
|
||
|
struct mtd_oob_ops ops;
|
||
|
|
||
12 years ago
|
ops.mode = MTD_OPS_PLACE_OOB;
|
||
17 years ago
|
ops.ooboffs = 0;
|
||
|
ops.ooblen = mtd->oobsize;
|
||
|
ops.datbuf = buf;
|
||
|
ops.oobbuf = oob;
|
||
|
ops.len = len;
|
||
|
|
||
12 years ago
|
return mtd_write_oob(mtd, offs, &ops);
|
||
17 years ago
|
}
|
||
|
|
||
13 years ago
|
static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
|
||
|
{
|
||
|
u32 ver_offs = td->veroffs;
|
||
|
|
||
|
if (!(td->options & NAND_BBT_NO_OOB))
|
||
|
ver_offs += mtd->writesize;
|
||
|
return ver_offs;
|
||
|
}
|
||
|
|
||
20 years ago
|
/**
|
||
|
* read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
|
||
12 years ago
|
* @mtd: MTD device structure
|
||
|
* @buf: temporary buffer
|
||
|
* @td: descriptor for the bad block table
|
||
|
* @md: descriptor for the bad block table mirror
|
||
20 years ago
|
*
|
||
12 years ago
|
* Read the bad block table(s) for all chips starting at a given page. We
|
||
|
* assume that the bbt bits are in consecutive order.
|
||
|
*/
|
||
|
static void read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
|
||
|
struct nand_bbt_descr *td, struct nand_bbt_descr *md)
|
||
20 years ago
|
{
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
20 years ago
|
|
||
19 years ago
|
/* Read the primary version, if available */
|
||
20 years ago
|
if (td->options & NAND_BBT_VERSION) {
|
||
12 years ago
|
scan_read(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
|
||
13 years ago
|
mtd->writesize, td);
|
||
|
td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
|
||
12 years ago
|
pr_info("Bad block table at page %d, version 0x%02X\n",
|
||
|
td->pages[0], td->version[0]);
|
||
20 years ago
|
}
|
||
|
|
||
19 years ago
|
/* Read the mirror version, if available */
|
||
20 years ago
|
if (md && (md->options & NAND_BBT_VERSION)) {
|
||
12 years ago
|
scan_read(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
|
||
|
mtd->writesize, md);
|
||
13 years ago
|
md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
|
||
12 years ago
|
pr_info("Bad block table at page %d, version 0x%02X\n",
|
||
|
md->pages[0], md->version[0]);
|
||
20 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
/* Scan a given block partially */
|
||
17 years ago
|
static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
|
||
12 years ago
|
loff_t offs, uint8_t *buf, int numpages)
|
||
17 years ago
|
{
|
||
|
struct mtd_oob_ops ops;
|
||
|
int j, ret;
|
||
|
|
||
|
ops.ooblen = mtd->oobsize;
|
||
|
ops.oobbuf = buf;
|
||
|
ops.ooboffs = 0;
|
||
|
ops.datbuf = NULL;
|
||
12 years ago
|
ops.mode = MTD_OPS_PLACE_OOB;
|
||
17 years ago
|
|
||
12 years ago
|
for (j = 0; j < numpages; j++) {
|
||
17 years ago
|
/*
|
||
12 years ago
|
* Read the full oob until read_oob is fixed to handle single
|
||
|
* byte reads for 16 bit buswidth.
|
||
17 years ago
|
*/
|
||
12 years ago
|
ret = mtd_read_oob(mtd, offs, &ops);
|
||
|
/* Ignore ECC errors when checking for BBM */
|
||
|
if (ret && !mtd_is_bitflip_or_eccerr(ret))
|
||
17 years ago
|
return ret;
|
||
|
|
||
|
if (check_short_pattern(buf, bd))
|
||
|
return 1;
|
||
|
|
||
|
offs += mtd->writesize;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
20 years ago
|
/**
|
||
|
* create_bbt - [GENERIC] Create a bad block table by scanning the device
|
||
12 years ago
|
* @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
|
||
20 years ago
|
*
|
||
12 years ago
|
* Create a bad block table by scanning the device for the given good/bad block
|
||
|
* identify pattern.
|
||
20 years ago
|
*/
|
||
17 years ago
|
static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
|
||
|
struct nand_bbt_descr *bd, int chip)
|
||
20 years ago
|
{
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
11 years ago
|
int i, numblocks, numpages;
|
||
20 years ago
|
int startblock;
|
||
|
loff_t from;
|
||
17 years ago
|
|
||
12 years ago
|
pr_info("Scanning device for bad blocks\n");
|
||
20 years ago
|
|
||
11 years ago
|
if (bd->options & NAND_BBT_SCAN2NDPAGE)
|
||
12 years ago
|
numpages = 2;
|
||
13 years ago
|
else
|
||
12 years ago
|
numpages = 1;
|
||
17 years ago
|
|
||
20 years ago
|
if (chip == -1) {
|
||
11 years ago
|
numblocks = mtd->size >> this->bbt_erase_shift;
|
||
20 years ago
|
startblock = 0;
|
||
|
from = 0;
|
||
|
} else {
|
||
|
if (chip >= this->numchips) {
|
||
12 years ago
|
pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
|
||
17 years ago
|
chip + 1, this->numchips);
|
||
|
return -EINVAL;
|
||
20 years ago
|
}
|
||
11 years ago
|
numblocks = this->chipsize >> this->bbt_erase_shift;
|
||
20 years ago
|
startblock = chip * numblocks;
|
||
|
numblocks += startblock;
|
||
11 years ago
|
from = (loff_t)startblock << this->bbt_erase_shift;
|
||
20 years ago
|
}
|
||
19 years ago
|
|
||
12 years ago
|
if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
|
||
|
from += mtd->erasesize - (mtd->writesize * numpages);
|
||
13 years ago
|
|
||
11 years ago
|
for (i = startblock; i < numblocks; i++) {
|
||
17 years ago
|
int ret;
|
||
|
|
||
13 years ago
|
BUG_ON(bd->options & NAND_BBT_NO_OOB);
|
||
|
|
||
11 years ago
|
ret = scan_block_fast(mtd, bd, from, buf, numpages);
|
||
17 years ago
|
if (ret < 0)
|
||
|
return ret;
|
||
|
|
||
|
if (ret) {
|
||
11 years ago
|
bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
|
||
12 years ago
|
pr_warn("Bad eraseblock %d at 0x%012llx\n",
|
||
11 years ago
|
i, (unsigned long long)from);
|
||
17 years ago
|
mtd->ecc_stats.badblocks++;
|
||
20 years ago
|
}
|
||
17 years ago
|
|
||
20 years ago
|
from += (1 << this->bbt_erase_shift);
|
||
|
}
|
||
17 years ago
|
return 0;
|
||
20 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* search_bbt - [GENERIC] scan the device for a specific bad block table
|
||
12 years ago
|
* @mtd: MTD device structure
|
||
|
* @buf: temporary buffer
|
||
|
* @td: descriptor for the bad block table
|
||
20 years ago
|
*
|
||
12 years ago
|
* Read the bad block table by searching for a given ident pattern. Search is
|
||
|
* preformed either from the beginning up or from the end of the device
|
||
|
* downwards. The search starts always at the start of a block. If the option
|
||
|
* NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
|
||
|
* the bad block information of this chip. This is necessary to provide support
|
||
|
* for certain DOC devices.
|
||
20 years ago
|
*
|
||
12 years ago
|
* The bbt ident pattern resides in the oob area of the first page in a block.
|
||
20 years ago
|
*/
|
||
17 years ago
|
static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
|
||
20 years ago
|
{
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
20 years ago
|
int i, chips;
|
||
13 years ago
|
int startblock, block, dir;
|
||
17 years ago
|
int scanlen = mtd->writesize + mtd->oobsize;
|
||
20 years ago
|
int bbtblocks;
|
||
17 years ago
|
int blocktopage = this->bbt_erase_shift - this->page_shift;
|
||
20 years ago
|
|
||
12 years ago
|
/* Search direction top -> down? */
|
||
20 years ago
|
if (td->options & NAND_BBT_LASTBLOCK) {
|
||
17 years ago
|
startblock = (mtd->size >> this->bbt_erase_shift) - 1;
|
||
20 years ago
|
dir = -1;
|
||
|
} else {
|
||
19 years ago
|
startblock = 0;
|
||
20 years ago
|
dir = 1;
|
||
19 years ago
|
}
|
||
|
|
||
12 years ago
|
/* Do we have a bbt per chip? */
|
||
20 years ago
|
if (td->options & NAND_BBT_PERCHIP) {
|
||
|
chips = this->numchips;
|
||
|
bbtblocks = this->chipsize >> this->bbt_erase_shift;
|
||
|
startblock &= bbtblocks - 1;
|
||
|
} else {
|
||
|
chips = 1;
|
||
|
bbtblocks = mtd->size >> this->bbt_erase_shift;
|
||
|
}
|
||
19 years ago
|
|
||
20 years ago
|
for (i = 0; i < chips; i++) {
|
||
|
/* Reset version information */
|
||
19 years ago
|
td->version[i] = 0;
|
||
20 years ago
|
td->pages[i] = -1;
|
||
|
/* Scan the maximum number of blocks */
|
||
|
for (block = 0; block < td->maxblocks; block++) {
|
||
17 years ago
|
|
||
20 years ago
|
int actblock = startblock + dir * block;
|
||
15 years ago
|
loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
|
||
17 years ago
|
|
||
20 years ago
|
/* Read first page */
|
||
12 years ago
|
scan_read(mtd, buf, offs, mtd->writesize, td);
|
||
17 years ago
|
if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
|
||
|
td->pages[i] = actblock << blocktopage;
|
||
20 years ago
|
if (td->options & NAND_BBT_VERSION) {
|
||
13 years ago
|
offs = bbt_get_ver_offs(mtd, td);
|
||
|
td->version[i] = buf[offs];
|
||
20 years ago
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
startblock += this->chipsize >> this->bbt_erase_shift;
|
||
|
}
|
||
|
/* Check, if we found a bbt for each requested chip */
|
||
|
for (i = 0; i < chips; i++) {
|
||
|
if (td->pages[i] == -1)
|
||
12 years ago
|
pr_warn("Bad block table not found for chip %d\n", i);
|
||
20 years ago
|
else
|
||
10 years ago
|
pr_info("Bad block table found at page %d, version 0x%02X\n",
|
||
|
td->pages[i], td->version[i]);
|
||
20 years ago
|
}
|
||
19 years ago
|
return 0;
|
||
20 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* search_read_bbts - [GENERIC] scan the device for bad block table(s)
|
||
12 years ago
|
* @mtd: MTD device structure
|
||
|
* @buf: temporary buffer
|
||
|
* @td: descriptor for the bad block table
|
||
|
* @md: descriptor for the bad block table mirror
|
||
20 years ago
|
*
|
||
12 years ago
|
* Search and read the bad block table(s).
|
||
|
*/
|
||
|
static void search_read_bbts(struct mtd_info *mtd, uint8_t *buf,
|
||
|
struct nand_bbt_descr *td,
|
||
|
struct nand_bbt_descr *md)
|
||
20 years ago
|
{
|
||
|
/* Search the primary table */
|
||
17 years ago
|
search_bbt(mtd, buf, td);
|
||
19 years ago
|
|
||
20 years ago
|
/* Search the mirror table */
|
||
|
if (md)
|
||
17 years ago
|
search_bbt(mtd, buf, md);
|
||
20 years ago
|
}
|
||
|
|
||
19 years ago
|
/**
|
||
20 years ago
|
* write_bbt - [GENERIC] (Re)write the bad block table
|
||
12 years ago
|
* @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
|
||
20 years ago
|
*
|
||
12 years ago
|
* (Re)write the bad block table.
|
||
|
*/
|
||
17 years ago
|
static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
|
||
|
struct nand_bbt_descr *td, struct nand_bbt_descr *md,
|
||
|
int chipsel)
|
||
20 years ago
|
{
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
20 years ago
|
struct erase_info einfo;
|
||
11 years ago
|
int i, res, chip = 0;
|
||
20 years ago
|
int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
|
||
11 years ago
|
int nrchips, pageoffs, ooboffs;
|
||
20 years ago
|
uint8_t msk[4];
|
||
|
uint8_t rcode = td->reserved_block_code;
|
||
|
size_t retlen, len = 0;
|
||
|
loff_t to;
|
||
17 years ago
|
struct mtd_oob_ops ops;
|
||
|
|
||
|
ops.ooblen = mtd->oobsize;
|
||
|
ops.ooboffs = 0;
|
||
|
ops.datbuf = NULL;
|
||
12 years ago
|
ops.mode = MTD_OPS_PLACE_OOB;
|
||
20 years ago
|
|
||
|
if (!rcode)
|
||
|
rcode = 0xff;
|
||
12 years ago
|
/* Write bad block table per chip rather than per device? */
|
||
20 years ago
|
if (td->options & NAND_BBT_PERCHIP) {
|
||
17 years ago
|
numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
|
||
12 years ago
|
/* Full device write or specific chip? */
|
||
20 years ago
|
if (chipsel == -1) {
|
||
|
nrchips = this->numchips;
|
||
|
} else {
|
||
|
nrchips = chipsel + 1;
|
||
|
chip = chipsel;
|
||
|
}
|
||
|
} else {
|
||
17 years ago
|
numblocks = (int)(mtd->size >> this->bbt_erase_shift);
|
||
20 years ago
|
nrchips = 1;
|
||
19 years ago
|
}
|
||
|
|
||
20 years ago
|
/* Loop through the chips */
|
||
|
for (; chip < nrchips; chip++) {
|
||
12 years ago
|
/*
|
||
|
* There was already a version of the table, reuse the page
|
||
19 years ago
|
* This applies for absolute placement too, as we have the
|
||
20 years ago
|
* page nr. in td->pages.
|
||
|
*/
|
||
|
if (td->pages[chip] != -1) {
|
||
|
page = td->pages[chip];
|
||
|
goto write;
|
||
19 years ago
|
}
|
||
20 years ago
|
|
||
12 years ago
|
/*
|
||
|
* Automatic placement of the bad block table. Search direction
|
||
|
* top -> down?
|
||
|
*/
|
||
20 years ago
|
if (td->options & NAND_BBT_LASTBLOCK) {
|
||
|
startblock = numblocks * (chip + 1) - 1;
|
||
|
dir = -1;
|
||
|
} else {
|
||
|
startblock = chip * numblocks;
|
||
|
dir = 1;
|
||
19 years ago
|
}
|
||
20 years ago
|
|
||
|
for (i = 0; i < td->maxblocks; i++) {
|
||
|
int block = startblock + dir * i;
|
||
|
/* Check, if the block is bad */
|
||
11 years ago
|
switch (bbt_get_entry(this, block)) {
|
||
|
case BBT_BLOCK_WORN:
|
||
|
case BBT_BLOCK_FACTORY_BAD:
|
||
20 years ago
|
continue;
|
||
|
}
|
||
17 years ago
|
page = block <<
|
||
|
(this->bbt_erase_shift - this->page_shift);
|
||
20 years ago
|
/* Check, if the block is used by the mirror table */
|
||
|
if (!md || md->pages[chip] != page)
|
||
|
goto write;
|
||
|
}
|
||
12 years ago
|
pr_err("No space left to write bad block table\n");
|
||
20 years ago
|
return -ENOSPC;
|
||
17 years ago
|
write:
|
||
20 years ago
|
|
||
|
/* Set up shift count and masks for the flash table */
|
||
|
bits = td->options & NAND_BBT_NRBITS_MSK;
|
||
17 years ago
|
msk[2] = ~rcode;
|
||
20 years ago
|
switch (bits) {
|
||
17 years ago
|
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] = 0x0C;
|
||
|
msk[3] = 0x0f;
|
||
|
break;
|
||
|
case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
|
||
|
msk[3] = 0xff;
|
||
|
break;
|
||
20 years ago
|
default: return -EINVAL;
|
||
|
}
|
||
19 years ago
|
|
||
12 years ago
|
to = ((loff_t)page) << this->page_shift;
|
||
20 years ago
|
|
||
12 years ago
|
/* Must we save the block contents? */
|
||
20 years ago
|
if (td->options & NAND_BBT_SAVECONTENT) {
|
||
|
/* Make it block aligned */
|
||
9 years ago
|
to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
|
||
20 years ago
|
len = 1 << this->bbt_erase_shift;
|
||
12 years ago
|
res = mtd_read(mtd, to, len, &retlen, buf);
|
||
20 years ago
|
if (res < 0) {
|
||
|
if (retlen != len) {
|
||
10 years ago
|
pr_info("nand_bbt: error reading block for writing the bad block table\n");
|
||
20 years ago
|
return res;
|
||
|
}
|
||
10 years ago
|
pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
|
||
20 years ago
|
}
|
||
17 years ago
|
/* Read oob data */
|
||
|
ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
|
||
|
ops.oobbuf = &buf[len];
|
||
12 years ago
|
res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
|
||
17 years ago
|
if (res < 0 || ops.oobretlen != ops.ooblen)
|
||
|
goto outerr;
|
||
|
|
||
20 years ago
|
/* Calc the byte offset in the buffer */
|
||
|
pageoffs = page - (int)(to >> this->page_shift);
|
||
|
offs = pageoffs << this->page_shift;
|
||
|
/* Preset the bbt area with 0xff */
|
||
12 years ago
|
memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
|
||
17 years ago
|
ooboffs = len + (pageoffs * mtd->oobsize);
|
||
|
|
||
13 years ago
|
} else if (td->options & NAND_BBT_NO_OOB) {
|
||
|
ooboffs = 0;
|
||
|
offs = td->len;
|
||
12 years ago
|
/* The version byte */
|
||
13 years ago
|
if (td->options & NAND_BBT_VERSION)
|
||
|
offs++;
|
||
|
/* Calc length */
|
||
12 years ago
|
len = (size_t)(numblocks >> sft);
|
||
13 years ago
|
len += offs;
|
||
12 years ago
|
/* Make it page aligned! */
|
||
13 years ago
|
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);
|
||
20 years ago
|
} else {
|
||
|
/* Calc length */
|
||
12 years ago
|
len = (size_t)(numblocks >> sft);
|
||
|
/* Make it page aligned! */
|
||
13 years ago
|
len = ALIGN(len, mtd->writesize);
|
||
20 years ago
|
/* Preset the buffer with 0xff */
|
||
17 years ago
|
memset(buf, 0xff, len +
|
||
|
(len >> this->page_shift)* mtd->oobsize);
|
||
20 years ago
|
offs = 0;
|
||
17 years ago
|
ooboffs = len;
|
||
20 years ago
|
/* Pattern is located in oob area of first page */
|
||
17 years ago
|
memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
|
||
20 years ago
|
}
|
||
19 years ago
|
|
||
17 years ago
|
if (td->options & NAND_BBT_VERSION)
|
||
|
buf[ooboffs + td->veroffs] = td->version[chip];
|
||
|
|
||
12 years ago
|
/* Walk through the memory table */
|
||
11 years ago
|
for (i = 0; i < numblocks; i++) {
|
||
20 years ago
|
uint8_t dat;
|
||
11 years ago
|
int sftcnt = (i << (3 - sft)) & sftmsk;
|
||
|
dat = bbt_get_entry(this, chip * numblocks + i);
|
||
|
/* Do not store the reserved bbt blocks! */
|
||
|
buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
|
||
20 years ago
|
}
|
||
19 years ago
|
|
||
17 years ago
|
memset(&einfo, 0, sizeof(einfo));
|
||
20 years ago
|
einfo.mtd = mtd;
|
||
15 years ago
|
einfo.addr = to;
|
||
20 years ago
|
einfo.len = 1 << this->bbt_erase_shift;
|
||
17 years ago
|
res = nand_erase_nand(mtd, &einfo, 1);
|
||
|
if (res < 0)
|
||
|
goto outerr;
|
||
19 years ago
|
|
||
13 years ago
|
res = scan_write_bbt(mtd, to, len, buf,
|
||
|
td->options & NAND_BBT_NO_OOB ? NULL :
|
||
|
&buf[len]);
|
||
17 years ago
|
if (res < 0)
|
||
|
goto outerr;
|
||
|
|
||
12 years ago
|
pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
|
||
|
(unsigned long long)to, td->version[chip]);
|
||
19 years ago
|
|
||
20 years ago
|
/* Mark it as used */
|
||
|
td->pages[chip] = page;
|
||
19 years ago
|
}
|
||
20 years ago
|
return 0;
|
||
17 years ago
|
|
||
|
outerr:
|
||
12 years ago
|
pr_warn("nand_bbt: error while writing bad block table %d\n", res);
|
||
17 years ago
|
return res;
|
||
20 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* nand_memory_bbt - [GENERIC] create a memory based bad block table
|
||
12 years ago
|
* @mtd: MTD device structure
|
||
|
* @bd: descriptor for the good/bad block search pattern
|
||
20 years ago
|
*
|
||
12 years ago
|
* The function creates a memory based bbt by scanning the device for
|
||
|
* manufacturer / software marked good / bad blocks.
|
||
|
*/
|
||
17 years ago
|
static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
|
||
20 years ago
|
{
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
20 years ago
|
|
||
17 years ago
|
return create_bbt(mtd, this->buffers->databuf, bd, -1);
|
||
20 years ago
|
}
|
||
|
|
||
|
/**
|
||
17 years ago
|
* check_create - [GENERIC] create and write bbt(s) if necessary
|
||
12 years ago
|
* @mtd: MTD device structure
|
||
|
* @buf: temporary buffer
|
||
|
* @bd: descriptor for the good/bad block search pattern
|
||
20 years ago
|
*
|
||
12 years ago
|
* The function checks the results of the previous call to read_bbt and creates
|
||
|
* / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
|
||
|
* for the chip/device. Update is necessary if one of the tables is missing or
|
||
|
* the version nr. of one table is less than the other.
|
||
|
*/
|
||
17 years ago
|
static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
|
||
20 years ago
|
{
|
||
12 years ago
|
int i, chips, writeops, create, chipsel, res, res2;
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
20 years ago
|
struct nand_bbt_descr *td = this->bbt_td;
|
||
|
struct nand_bbt_descr *md = this->bbt_md;
|
||
|
struct nand_bbt_descr *rd, *rd2;
|
||
|
|
||
12 years ago
|
/* Do we have a bbt per chip? */
|
||
19 years ago
|
if (td->options & NAND_BBT_PERCHIP)
|
||
20 years ago
|
chips = this->numchips;
|
||
19 years ago
|
else
|
||
20 years ago
|
chips = 1;
|
||
19 years ago
|
|
||
20 years ago
|
for (i = 0; i < chips; i++) {
|
||
|
writeops = 0;
|
||
12 years ago
|
create = 0;
|
||
20 years ago
|
rd = NULL;
|
||
|
rd2 = NULL;
|
||
12 years ago
|
res = res2 = 0;
|
||
|
/* Per chip or per device? */
|
||
20 years ago
|
chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
|
||
12 years ago
|
/* Mirrored table available? */
|
||
20 years ago
|
if (md) {
|
||
|
if (td->pages[i] == -1 && md->pages[i] == -1) {
|
||
12 years ago
|
create = 1;
|
||
20 years ago
|
writeops = 0x03;
|
||
12 years ago
|
} else if (td->pages[i] == -1) {
|
||
19 years ago
|
rd = md;
|
||
12 years ago
|
writeops = 0x01;
|
||
|
} else if (md->pages[i] == -1) {
|
||
20 years ago
|
rd = td;
|
||
12 years ago
|
writeops = 0x02;
|
||
|
} else if (td->version[i] == md->version[i]) {
|
||
20 years ago
|
rd = td;
|
||
|
if (!(td->options & NAND_BBT_VERSION))
|
||
|
rd2 = md;
|
||
12 years ago
|
} else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
|
||
20 years ago
|
rd = td;
|
||
12 years ago
|
writeops = 0x02;
|
||
20 years ago
|
} else {
|
||
|
rd = md;
|
||
12 years ago
|
writeops = 0x01;
|
||
20 years ago
|
}
|
||
|
} else {
|
||
|
if (td->pages[i] == -1) {
|
||
12 years ago
|
create = 1;
|
||
20 years ago
|
writeops = 0x01;
|
||
12 years ago
|
} else {
|
||
|
rd = td;
|
||
20 years ago
|
}
|
||
|
}
|
||
19 years ago
|
|
||
12 years ago
|
if (create) {
|
||
|
/* Create the bad block table by scanning the device? */
|
||
|
if (!(td->options & NAND_BBT_CREATE))
|
||
|
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);
|
||
|
|
||
|
td->version[i] = 1;
|
||
|
if (md)
|
||
|
md->version[i] = 1;
|
||
|
}
|
||
|
|
||
|
/* Read back first? */
|
||
|
if (rd) {
|
||
|
res = read_abs_bbt(mtd, buf, rd, chipsel);
|
||
|
if (mtd_is_eccerr(res)) {
|
||
|
/* Mark table as invalid */
|
||
|
rd->pages[i] = -1;
|
||
|
rd->version[i] = 0;
|
||
|
i--;
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
/* If they weren't versioned, read both */
|
||
|
if (rd2) {
|
||
|
res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
|
||
|
if (mtd_is_eccerr(res2)) {
|
||
|
/* Mark table as invalid */
|
||
|
rd2->pages[i] = -1;
|
||
|
rd2->version[i] = 0;
|
||
|
i--;
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* Scrub the flash table(s)? */
|
||
|
if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
|
||
|
writeops = 0x03;
|
||
|
|
||
|
/* Update version numbers before writing */
|
||
|
if (md) {
|
||
|
td->version[i] = max(td->version[i], md->version[i]);
|
||
|
md->version[i] = td->version[i];
|
||
|
}
|
||
|
|
||
|
/* Write the bad block table to the device? */
|
||
20 years ago
|
if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
|
||
17 years ago
|
res = write_bbt(mtd, buf, td, md, chipsel);
|
||
20 years ago
|
if (res < 0)
|
||
|
return res;
|
||
|
}
|
||
19 years ago
|
|
||
12 years ago
|
/* Write the mirror bad block table to the device? */
|
||
20 years ago
|
if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
|
||
17 years ago
|
res = write_bbt(mtd, buf, md, td, chipsel);
|
||
20 years ago
|
if (res < 0)
|
||
|
return res;
|
||
|
}
|
||
|
}
|
||
19 years ago
|
return 0;
|
||
20 years ago
|
}
|
||
|
|
||
|
/**
|
||
19 years ago
|
* mark_bbt_regions - [GENERIC] mark the bad block table regions
|
||
12 years ago
|
* @mtd: MTD device structure
|
||
|
* @td: bad block table descriptor
|
||
20 years ago
|
*
|
||
12 years ago
|
* The bad block table regions are marked as "bad" to prevent accidental
|
||
|
* erasures / writes. The regions are identified by the mark 0x02.
|
||
|
*/
|
||
17 years ago
|
static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
|
||
20 years ago
|
{
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
20 years ago
|
int i, j, chips, block, nrblocks, update;
|
||
11 years ago
|
uint8_t oldval;
|
||
20 years ago
|
|
||
12 years ago
|
/* Do we have a bbt per chip? */
|
||
20 years ago
|
if (td->options & NAND_BBT_PERCHIP) {
|
||
|
chips = this->numchips;
|
||
|
nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
|
||
|
} else {
|
||
|
chips = 1;
|
||
|
nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
|
||
19 years ago
|
}
|
||
|
|
||
20 years ago
|
for (i = 0; i < chips; i++) {
|
||
|
if ((td->options & NAND_BBT_ABSPAGE) ||
|
||
|
!(td->options & NAND_BBT_WRITE)) {
|
||
17 years ago
|
if (td->pages[i] == -1)
|
||
|
continue;
|
||
20 years ago
|
block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
|
||
11 years ago
|
oldval = bbt_get_entry(this, block);
|
||
|
bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
|
||
|
if ((oldval != BBT_BLOCK_RESERVED) &&
|
||
|
td->reserved_block_code)
|
||
|
nand_update_bbt(mtd, (loff_t)block <<
|
||
|
this->bbt_erase_shift);
|
||
20 years ago
|
continue;
|
||
|
}
|
||
|
update = 0;
|
||
|
if (td->options & NAND_BBT_LASTBLOCK)
|
||
|
block = ((i + 1) * nrblocks) - td->maxblocks;
|
||
19 years ago
|
else
|
||
20 years ago
|
block = i * nrblocks;
|
||
|
for (j = 0; j < td->maxblocks; j++) {
|
||
11 years ago
|
oldval = bbt_get_entry(this, block);
|
||
|
bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
|
||
|
if (oldval != BBT_BLOCK_RESERVED)
|
||
17 years ago
|
update = 1;
|
||
11 years ago
|
block++;
|
||
19 years ago
|
}
|
||
12 years ago
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
20 years ago
|
if (update && td->reserved_block_code)
|
||
11 years ago
|
nand_update_bbt(mtd, (loff_t)(block - 1) <<
|
||
|
this->bbt_erase_shift);
|
||
20 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* verify_bbt_descr - verify the bad block description
|
||
12 years ago
|
* @mtd: MTD device structure
|
||
|
* @bd: the table to verify
|
||
13 years ago
|
*
|
||
|
* This functions performs a few sanity checks on the bad block description
|
||
|
* table.
|
||
|
*/
|
||
|
static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
|
||
|
{
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
13 years ago
|
u32 pattern_len;
|
||
|
u32 bits;
|
||
|
u32 table_size;
|
||
|
|
||
|
if (!bd)
|
||
|
return;
|
||
|
|
||
|
pattern_len = bd->len;
|
||
|
bits = bd->options & NAND_BBT_NRBITS_MSK;
|
||
|
|
||
12 years ago
|
BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
|
||
|
!(this->bbt_options & NAND_BBT_USE_FLASH));
|
||
13 years ago
|
BUG_ON(!bits);
|
||
|
|
||
|
if (bd->options & NAND_BBT_VERSION)
|
||
|
pattern_len++;
|
||
|
|
||
|
if (bd->options & NAND_BBT_NO_OOB) {
|
||
12 years ago
|
BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
|
||
|
BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
|
||
13 years ago
|
BUG_ON(bd->offs);
|
||
|
if (bd->options & NAND_BBT_VERSION)
|
||
|
BUG_ON(bd->veroffs != bd->len);
|
||
|
BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
|
||
|
}
|
||
|
|
||
|
if (bd->options & NAND_BBT_PERCHIP)
|
||
|
table_size = this->chipsize >> this->bbt_erase_shift;
|
||
|
else
|
||
|
table_size = mtd->size >> this->bbt_erase_shift;
|
||
|
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));
|
||
|
}
|
||
|
|
||
|
/**
|
||
20 years ago
|
* nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
|
||
12 years ago
|
* @mtd: MTD device structure
|
||
|
* @bd: descriptor for the good/bad block search pattern
|
||
20 years ago
|
*
|
||
12 years ago
|
* The function checks, if a bad block table(s) is/are already available. If
|
||
|
* not it scans the device for manufacturer marked good / bad blocks and writes
|
||
|
* the bad block table(s) to the selected place.
|
||
20 years ago
|
*
|
||
12 years ago
|
* The bad block table memory is allocated here. It must be freed by calling
|
||
|
* the nand_free_bbt function.
|
||
|
*/
|
||
9 years ago
|
static int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
|
||
20 years ago
|
{
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
9 years ago
|
int len, res;
|
||
20 years ago
|
uint8_t *buf;
|
||
|
struct nand_bbt_descr *td = this->bbt_td;
|
||
|
struct nand_bbt_descr *md = this->bbt_md;
|
||
|
|
||
9 years ago
|
len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
|
||
12 years ago
|
/*
|
||
|
* Allocate memory (2bit per block) and clear the memory bad block
|
||
|
* table.
|
||
|
*/
|
||
17 years ago
|
this->bbt = kzalloc(len, GFP_KERNEL);
|
||
12 years ago
|
if (!this->bbt)
|
||
20 years ago
|
return -ENOMEM;
|
||
|
|
||
12 years ago
|
/*
|
||
|
* If no primary table decriptor is given, scan the device to build a
|
||
|
* memory based bad block table.
|
||
20 years ago
|
*/
|
||
17 years ago
|
if (!td) {
|
||
|
if ((res = nand_memory_bbt(mtd, bd))) {
|
||
12 years ago
|
pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
|
||
9 years ago
|
goto err;
|
||
17 years ago
|
}
|
||
9 years ago
|
return 0;
|
||
17 years ago
|
}
|
||
13 years ago
|
verify_bbt_descr(mtd, td);
|
||
|
verify_bbt_descr(mtd, md);
|
||
20 years ago
|
|
||
|
/* Allocate a temporary buffer for one eraseblock incl. oob */
|
||
|
len = (1 << this->bbt_erase_shift);
|
||
|
len += (len >> this->page_shift) * mtd->oobsize;
|
||
17 years ago
|
buf = vmalloc(len);
|
||
20 years ago
|
if (!buf) {
|
||
9 years ago
|
res = -ENOMEM;
|
||
|
goto err;
|
||
20 years ago
|
}
|
||
19 years ago
|
|
||
12 years ago
|
/* Is the bbt at a given page? */
|
||
20 years ago
|
if (td->options & NAND_BBT_ABSPAGE) {
|
||
12 years ago
|
read_abs_bbts(mtd, buf, td, md);
|
||
19 years ago
|
} else {
|
||
20 years ago
|
/* Search the bad block table using a pattern in oob */
|
||
12 years ago
|
search_read_bbts(mtd, buf, td, md);
|
||
19 years ago
|
}
|
||
20 years ago
|
|
||
12 years ago
|
res = check_create(mtd, buf, bd);
|
||
9 years ago
|
if (res)
|
||
|
goto err;
|
||
19 years ago
|
|
||
20 years ago
|
/* Prevent the bbt regions from erasing / writing */
|
||
17 years ago
|
mark_bbt_region(mtd, td);
|
||
20 years ago
|
if (md)
|
||
17 years ago
|
mark_bbt_region(mtd, md);
|
||
19 years ago
|
|
||
17 years ago
|
vfree(buf);
|
||
9 years ago
|
return 0;
|
||
|
|
||
|
err:
|
||
|
kfree(this->bbt);
|
||
|
this->bbt = NULL;
|
||
20 years ago
|
return res;
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* nand_update_bbt - update bad block table(s)
|
||
12 years ago
|
* @mtd: MTD device structure
|
||
|
* @offs: the offset of the newly marked block
|
||
20 years ago
|
*
|
||
12 years ago
|
* The function updates the bad block table(s).
|
||
|
*/
|
||
11 years ago
|
static int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
|
||
20 years ago
|
{
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
12 years ago
|
int len, res = 0;
|
||
20 years ago
|
int chip, chipsel;
|
||
|
uint8_t *buf;
|
||
|
struct nand_bbt_descr *td = this->bbt_td;
|
||
|
struct nand_bbt_descr *md = this->bbt_md;
|
||
|
|
||
|
if (!this->bbt || !td)
|
||
|
return -EINVAL;
|
||
|
|
||
|
/* Allocate a temporary buffer for one eraseblock incl. oob */
|
||
|
len = (1 << this->bbt_erase_shift);
|
||
|
len += (len >> this->page_shift) * mtd->oobsize;
|
||
17 years ago
|
buf = kmalloc(len, GFP_KERNEL);
|
||
12 years ago
|
if (!buf)
|
||
20 years ago
|
return -ENOMEM;
|
||
19 years ago
|
|
||
12 years ago
|
/* Do we have a bbt per chip? */
|
||
20 years ago
|
if (td->options & NAND_BBT_PERCHIP) {
|
||
17 years ago
|
chip = (int)(offs >> this->chip_shift);
|
||
20 years ago
|
chipsel = chip;
|
||
|
} else {
|
||
|
chip = 0;
|
||
|
chipsel = -1;
|
||
|
}
|
||
|
|
||
|
td->version[chip]++;
|
||
|
if (md)
|
||
19 years ago
|
md->version[chip]++;
|
||
20 years ago
|
|
||
12 years ago
|
/* Write the bad block table to the device? */
|
||
|
if (td->options & NAND_BBT_WRITE) {
|
||
17 years ago
|
res = write_bbt(mtd, buf, td, md, chipsel);
|
||
20 years ago
|
if (res < 0)
|
||
|
goto out;
|
||
|
}
|
||
12 years ago
|
/* Write the mirror bad block table to the device? */
|
||
|
if (md && (md->options & NAND_BBT_WRITE)) {
|
||
17 years ago
|
res = write_bbt(mtd, buf, md, td, chipsel);
|
||
20 years ago
|
}
|
||
|
|
||
17 years ago
|
out:
|
||
|
kfree(buf);
|
||
20 years ago
|
return res;
|
||
|
}
|
||
|
|
||
12 years ago
|
/*
|
||
|
* Define some generic bad / good block scan pattern which are used
|
||
|
* while scanning a device for factory marked good / bad blocks.
|
||
|
*/
|
||
20 years ago
|
static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
|
||
|
|
||
12 years ago
|
/* Generic flash bbt descriptors */
|
||
20 years ago
|
static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
|
||
|
static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
|
||
|
|
||
|
static struct nand_bbt_descr bbt_main_descr = {
|
||
19 years ago
|
.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
|
||
20 years ago
|
| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
|
||
|
.offs = 8,
|
||
|
.len = 4,
|
||
|
.veroffs = 12,
|
||
12 years ago
|
.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
|
||
20 years ago
|
.pattern = bbt_pattern
|
||
|
};
|
||
|
|
||
|
static struct nand_bbt_descr bbt_mirror_descr = {
|
||
19 years ago
|
.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
|
||
20 years ago
|
| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
|
||
|
.offs = 8,
|
||
|
.len = 4,
|
||
|
.veroffs = 12,
|
||
12 years ago
|
.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
|
||
20 years ago
|
.pattern = mirror_pattern
|
||
|
};
|
||
|
|
||
12 years ago
|
static struct nand_bbt_descr bbt_main_no_oob_descr = {
|
||
13 years ago
|
.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
|
||
|
| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
|
||
|
| NAND_BBT_NO_OOB,
|
||
|
.len = 4,
|
||
|
.veroffs = 4,
|
||
12 years ago
|
.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
|
||
13 years ago
|
.pattern = bbt_pattern
|
||
|
};
|
||
|
|
||
12 years ago
|
static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
|
||
13 years ago
|
.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
|
||
|
| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
|
||
|
| NAND_BBT_NO_OOB,
|
||
|
.len = 4,
|
||
|
.veroffs = 4,
|
||
12 years ago
|
.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
|
||
13 years ago
|
.pattern = mirror_pattern
|
||
|
};
|
||
|
|
||
12 years ago
|
#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
|
||
13 years ago
|
/**
|
||
12 years ago
|
* nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
|
||
|
* @this: NAND chip to create descriptor for
|
||
13 years ago
|
*
|
||
|
* This function allocates and initializes a nand_bbt_descr for BBM detection
|
||
12 years ago
|
* based on the properties of @this. The new descriptor is stored in
|
||
13 years ago
|
* this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
|
||
|
* passed to this function.
|
||
|
*/
|
||
12 years ago
|
static int nand_create_badblock_pattern(struct nand_chip *this)
|
||
13 years ago
|
{
|
||
|
struct nand_bbt_descr *bd;
|
||
|
if (this->badblock_pattern) {
|
||
12 years ago
|
pr_warn("Bad block pattern already allocated; not replacing\n");
|
||
13 years ago
|
return -EINVAL;
|
||
|
}
|
||
|
bd = kzalloc(sizeof(*bd), GFP_KERNEL);
|
||
12 years ago
|
if (!bd)
|
||
13 years ago
|
return -ENOMEM;
|
||
12 years ago
|
bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
|
||
13 years ago
|
bd->offs = this->badblockpos;
|
||
|
bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
|
||
|
bd->pattern = scan_ff_pattern;
|
||
|
bd->options |= NAND_BBT_DYNAMICSTRUCT;
|
||
|
this->badblock_pattern = bd;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
20 years ago
|
/**
|
||
19 years ago
|
* nand_default_bbt - [NAND Interface] Select a default bad block table for the device
|
||
12 years ago
|
* @mtd: MTD device structure
|
||
20 years ago
|
*
|
||
12 years ago
|
* This function selects the default bad block table support for the device and
|
||
|
* calls the nand_scan_bbt function.
|
||
|
*/
|
||
17 years ago
|
int nand_default_bbt(struct mtd_info *mtd)
|
||
20 years ago
|
{
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
10 years ago
|
int ret;
|
||
19 years ago
|
|
||
12 years ago
|
/* Is a flash based bad block table requested? */
|
||
|
if (this->bbt_options & NAND_BBT_USE_FLASH) {
|
||
19 years ago
|
/* Use the default pattern descriptors */
|
||
|
if (!this->bbt_td) {
|
||
12 years ago
|
if (this->bbt_options & NAND_BBT_NO_OOB) {
|
||
|
this->bbt_td = &bbt_main_no_oob_descr;
|
||
|
this->bbt_md = &bbt_mirror_no_oob_descr;
|
||
13 years ago
|
} else {
|
||
|
this->bbt_td = &bbt_main_descr;
|
||
|
this->bbt_md = &bbt_mirror_descr;
|
||
|
}
|
||
20 years ago
|
}
|
||
|
} else {
|
||
|
this->bbt_td = NULL;
|
||
|
this->bbt_md = NULL;
|
||
|
}
|
||
13 years ago
|
|
||
10 years ago
|
if (!this->badblock_pattern) {
|
||
|
ret = nand_create_badblock_pattern(this);
|
||
|
if (ret)
|
||
|
return ret;
|
||
|
}
|
||
13 years ago
|
|
||
17 years ago
|
return nand_scan_bbt(mtd, this->badblock_pattern);
|
||
20 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
|
||
|
* @mtd: MTD device structure
|
||
|
* @offs: offset in the device
|
||
|
*/
|
||
|
int nand_isreserved_bbt(struct mtd_info *mtd, loff_t offs)
|
||
|
{
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
11 years ago
|
int block;
|
||
|
|
||
|
block = (int)(offs >> this->bbt_erase_shift);
|
||
|
return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
|
||
|
}
|
||
|
|
||
|
/**
|
||
19 years ago
|
* nand_isbad_bbt - [NAND Interface] Check if a block is bad
|
||
12 years ago
|
* @mtd: MTD device structure
|
||
|
* @offs: offset in the device
|
||
|
* @allowbbt: allow access to bad block table region
|
||
|
*/
|
||
17 years ago
|
int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
|
||
20 years ago
|
{
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
11 years ago
|
int block, res;
|
||
19 years ago
|
|
||
11 years ago
|
block = (int)(offs >> this->bbt_erase_shift);
|
||
|
res = bbt_get_entry(this, block);
|
||
20 years ago
|
|
||
10 years ago
|
pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
|
||
|
(unsigned int)offs, block, res);
|
||
20 years ago
|
|
||
11 years ago
|
switch (res) {
|
||
|
case BBT_BLOCK_GOOD:
|
||
17 years ago
|
return 0;
|
||
11 years ago
|
case BBT_BLOCK_WORN:
|
||
17 years ago
|
return 1;
|
||
11 years ago
|
case BBT_BLOCK_RESERVED:
|
||
17 years ago
|
return allowbbt ? 0 : 1;
|
||
20 years ago
|
}
|
||
|
return 1;
|
||
|
}
|
||
11 years ago
|
|
||
|
/**
|
||
|
* nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
|
||
|
* @mtd: MTD device structure
|
||
|
* @offs: offset of the bad block
|
||
|
*/
|
||
|
int nand_markbad_bbt(struct mtd_info *mtd, loff_t offs)
|
||
|
{
|
||
9 years ago
|
struct nand_chip *this = mtd_to_nand(mtd);
|
||
11 years ago
|
int block, ret = 0;
|
||
|
|
||
|
block = (int)(offs >> this->bbt_erase_shift);
|
||
|
|
||
|
/* Mark bad block in memory */
|
||
|
bbt_mark_entry(this, block, BBT_BLOCK_WORN);
|
||
|
|
||
|
/* Update flash-based bad block table */
|
||
|
if (this->bbt_options & NAND_BBT_USE_FLASH)
|
||
|
ret = nand_update_bbt(mtd, offs);
|
||
|
|
||
|
return ret;
|
||
|
}
|