2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* drivers/mtd/nand_bbt.c
|
|
|
|
*
|
|
|
|
* Overview:
|
|
|
|
* Bad block table support for the NAND driver
|
2005-11-07 11:15:49 +00:00
|
|
|
*
|
2012-07-28 22:29:25 +00:00
|
|
|
* Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
2005-11-07 11:15:49 +00:00
|
|
|
* When nand_scan_bbt is called, then it tries to find the bad block table
|
2010-09-30 19:28:01 +00:00
|
|
|
* depending on the options in the BBT descriptor(s). If no flash based BBT
|
2011-05-31 23:31:23 +00:00
|
|
|
* (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
|
2010-09-30 19:28:01 +00:00
|
|
|
* 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
|
2012-07-03 08:44:15 +00:00
|
|
|
* version number, then the mirror BBT is used to build the memory based BBT.
|
2005-04-16 22:20:36 +00:00
|
|
|
* If the tables are not versioned, then we "or" the bad block information.
|
2010-09-30 19:28:01 +00:00
|
|
|
* 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
|
2005-11-07 11:15:49 +00:00
|
|
|
* good / bad blocks and the bad block tables are created.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2010-09-30 19:28:01 +00:00
|
|
|
* For manufacturer created BBTs like the one found on M-SYS DOC devices
|
|
|
|
* the BBT is searched and read but never created
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2010-09-30 19:28:01 +00:00
|
|
|
* The auto generated bad block table is located in the last good blocks
|
2005-11-07 11:15:49 +00:00
|
|
|
* of the device. The table is mirrored, so it can be updated eventually.
|
2010-09-30 19:28:01 +00:00
|
|
|
* 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
|
2011-05-31 23:31:23 +00:00
|
|
|
* option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
|
2011-05-31 23:31:22 +00:00
|
|
|
* course): it moves the ident pattern and the version byte into the data area
|
|
|
|
* and the OOB area will remain untouched.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* The table uses 2 bits per block
|
2010-09-30 19:28:01 +00:00
|
|
|
* 11b: block is good
|
|
|
|
* 00b: block is factory marked bad
|
|
|
|
* 01b, 10b: block is marked bad due to wear
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* 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
|
2005-11-07 11:15:49 +00:00
|
|
|
*
|
2005-04-16 22:20:36 +00:00
|
|
|
* 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
|
2006-05-13 17:07:53 +00:00
|
|
|
* - the space necessary for a bbt in FLASH does not exceed a block boundary
|
2005-11-07 11:15:49 +00:00
|
|
|
*
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/mtd/mtd.h>
|
2012-09-11 13:50:54 +00:00
|
|
|
#include <linux/mtd/bbm.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/mtd/nand.h>
|
|
|
|
#include <linux/mtd/nand_ecc.h>
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/delay.h>
|
2006-05-13 03:03:42 +00:00
|
|
|
#include <linux/vmalloc.h>
|
2011-07-10 16:43:28 +00:00
|
|
|
#include <linux/export.h>
|
2012-06-22 23:35:44 +00:00
|
|
|
#include <linux/string.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-07-31 00:52:55 +00:00
|
|
|
#define BBT_BLOCK_GOOD 0x00
|
|
|
|
#define BBT_BLOCK_WORN 0x01
|
|
|
|
#define BBT_BLOCK_RESERVED 0x02
|
|
|
|
#define BBT_BLOCK_FACTORY_BAD 0x03
|
|
|
|
|
|
|
|
#define BBT_ENTRY_MASK 0x03
|
|
|
|
#define BBT_ENTRY_SHIFT 2
|
|
|
|
|
2013-07-31 00:52:59 +00:00
|
|
|
static int nand_update_bbt(struct mtd_info *mtd, loff_t offs);
|
|
|
|
|
2013-07-31 00:52:55 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-09-30 19:28:01 +00:00
|
|
|
static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
|
|
|
|
{
|
2012-06-22 23:35:43 +00:00
|
|
|
if (memcmp(buf, td->pattern, td->len))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2010-09-30 19:28:01 +00:00
|
|
|
}
|
|
|
|
|
2005-11-07 11:15:49 +00:00
|
|
|
/**
|
2005-04-16 22:20:36 +00:00
|
|
|
* check_pattern - [GENERIC] check if a pattern is in the buffer
|
2011-05-25 21:59:01 +00:00
|
|
|
* @buf: the buffer to search
|
|
|
|
* @len: the length of buffer to search
|
|
|
|
* @paglen: the pagelength
|
|
|
|
* @td: search pattern descriptor
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* Check for a pattern at the given place. Used to search bad block tables and
|
2013-07-31 00:53:00 +00:00
|
|
|
* good / bad block identifiers.
|
2011-05-25 21:59:01 +00:00
|
|
|
*/
|
2006-05-13 17:07:53 +00:00
|
|
|
static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2010-09-30 19:28:01 +00:00
|
|
|
if (td->options & NAND_BBT_NO_OOB)
|
|
|
|
return check_pattern_no_oob(buf, td);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Compare the pattern */
|
2013-07-31 00:53:00 +00:00
|
|
|
if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
|
2011-09-07 20:13:41 +00:00
|
|
|
return -1;
|
mtd: nand: more BB Detection refactoring and dynamic scan options
This is a revision to PATCH 2/2 that I sent. Link:
http://lists.infradead.org/pipermail/linux-mtd/2010-July/030911.html
Added new flag for scanning of both bytes 1 and 6 of the OOB for
a BB marker (instead of simply one or the other).
The "check_pattern" and "check_short_pattern" functions were updated
to include support for scanning the two different locations in the OOB.
In order to handle increases in variety of necessary scanning patterns,
I implemented dynamic memory allocation of nand_bbt_descr structs
in new function 'nand_create_default_bbt_descr()'. This replaces
some increasingly-unwieldy, statically-declared descriptors. It can
replace several more (e.g. "flashbased" structs). However, I do not
test the flashbased options personally.
How this was tested:
I referenced 30+ data sheets (covering 100+ parts), and I tested a
selection of 10 different chips to varying degrees. Particularly, I
tested the creation of bad-block descriptors and basic BB scanning on
three parts:
ST NAND04GW3B2D, 2K page
ST NAND128W3A, 512B page
Samsung K9F1G08U0A, 2K page
To test these, I wrote some fake bad block markers to the flash (in OOB
bytes 1, 6, and elsewhere) to see if the scanning routine would detect
them properly. However, this method was somewhat limited because the
driver I am using has some bugs in its OOB write functionality.
Signed-off-by: Brian Norris <norris@broadcom.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2010-07-15 19:15:44 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-07 11:15:49 +00:00
|
|
|
/**
|
2005-06-14 15:39:57 +00:00
|
|
|
* check_short_pattern - [GENERIC] check if a pattern is in the buffer
|
2011-05-25 21:59:01 +00:00
|
|
|
* @buf: the buffer to search
|
|
|
|
* @td: search pattern descriptor
|
2005-06-14 15:39:57 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2006-05-13 17:07:53 +00:00
|
|
|
static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
|
2005-06-14 15:39:57 +00:00
|
|
|
{
|
|
|
|
/* Compare the pattern */
|
2012-06-22 23:35:44 +00:00
|
|
|
if (memcmp(buf + td->offs, td->pattern, td->len))
|
|
|
|
return -1;
|
2005-06-14 15:39:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-30 19:28:01 +00:00
|
|
|
/**
|
|
|
|
* add_marker_len - compute the length of the marker in data area
|
2011-05-25 21:59:01 +00:00
|
|
|
* @td: BBT descriptor used for computation
|
2010-09-30 19:28:01 +00:00
|
|
|
*
|
2011-06-23 21:12:08 +00:00
|
|
|
* The length will be 0 if the marker is located in OOB area.
|
2010-09-30 19:28:01 +00:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
|
|
|
* read_bbt - [GENERIC] Read the bad block table starting from page
|
2011-05-25 21:59:01 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @buf: temporary buffer
|
|
|
|
* @page: the starting page
|
|
|
|
* @num: the number of bbt descriptors to read
|
2011-06-23 21:12:08 +00:00
|
|
|
* @td: the bbt describtion table
|
2013-07-31 00:52:56 +00:00
|
|
|
* @offs: block number offset in the table
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* Read the bad block table starting from page.
|
|
|
|
*/
|
2006-05-13 17:07:53 +00:00
|
|
|
static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
|
2010-09-29 17:43:51 +00:00
|
|
|
struct nand_bbt_descr *td, int offs)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2011-09-21 01:35:08 +00:00
|
|
|
int res, ret = 0, i, j, act = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct nand_chip *this = mtd->priv;
|
|
|
|
size_t retlen, len, totlen;
|
|
|
|
loff_t from;
|
2010-09-29 17:43:51 +00:00
|
|
|
int bits = td->options & NAND_BBT_NRBITS_MSK;
|
2011-09-07 20:13:33 +00:00
|
|
|
uint8_t msk = (uint8_t)((1 << bits) - 1);
|
2010-09-30 19:28:01 +00:00
|
|
|
u32 marker_len;
|
2010-09-29 17:43:51 +00:00
|
|
|
int reserved_block_code = td->reserved_block_code;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
totlen = (num * bits) >> 3;
|
2010-09-30 19:28:01 +00:00
|
|
|
marker_len = add_marker_len(td);
|
2011-09-07 20:13:33 +00:00
|
|
|
from = ((loff_t)page) << this->page_shift;
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
while (totlen) {
|
2011-09-07 20:13:33 +00:00
|
|
|
len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
|
2010-09-30 19:28:01 +00:00
|
|
|
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;
|
|
|
|
}
|
2011-12-23 15:30:16 +00:00
|
|
|
res = mtd_read(mtd, from, len, &retlen, buf);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (res < 0) {
|
2011-09-21 01:35:08 +00:00
|
|
|
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");
|
2005-04-16 22:20:36 +00:00
|
|
|
return res;
|
|
|
|
}
|
2005-11-07 11:15:49 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* Analyse data */
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
uint8_t dat = buf[i];
|
2013-07-31 00:52:56 +00:00
|
|
|
for (j = 0; j < 8; j += bits, act++) {
|
2005-04-16 22:20:36 +00:00
|
|
|
uint8_t tmp = (dat >> j) & msk;
|
|
|
|
if (tmp == msk)
|
|
|
|
continue;
|
2006-05-13 17:07:53 +00:00
|
|
|
if (reserved_block_code && (tmp == reserved_block_code)) {
|
2011-07-19 17:06:08 +00:00
|
|
|
pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
|
2013-07-31 00:52:56 +00:00
|
|
|
(loff_t)(offs + act) <<
|
|
|
|
this->bbt_erase_shift);
|
|
|
|
bbt_mark_entry(this, offs + act,
|
2013-07-31 00:52:55 +00:00
|
|
|
BBT_BLOCK_RESERVED);
|
2006-05-29 22:37:34 +00:00
|
|
|
mtd->ecc_stats.bbtblocks++;
|
2005-04-16 22:20:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-05-25 21:59:01 +00:00
|
|
|
/*
|
|
|
|
* Leave it for now, if it's matured we can
|
2011-07-19 17:06:06 +00:00
|
|
|
* move this message to pr_debug.
|
2011-05-25 21:59:01 +00:00
|
|
|
*/
|
2011-07-19 17:06:08 +00:00
|
|
|
pr_info("nand_read_bbt: bad block at 0x%012llx\n",
|
2013-07-31 00:52:56 +00:00
|
|
|
(loff_t)(offs + act) <<
|
|
|
|
this->bbt_erase_shift);
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Factory marked bad or worn out? */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (tmp == 0)
|
2013-07-31 00:52:56 +00:00
|
|
|
bbt_mark_entry(this, offs + act,
|
2013-07-31 00:52:55 +00:00
|
|
|
BBT_BLOCK_FACTORY_BAD);
|
2005-04-16 22:20:36 +00:00
|
|
|
else
|
2013-07-31 00:52:56 +00:00
|
|
|
bbt_mark_entry(this, offs + act,
|
2013-07-31 00:52:55 +00:00
|
|
|
BBT_BLOCK_WORN);
|
2006-05-29 22:37:34 +00:00
|
|
|
mtd->ecc_stats.badblocks++;
|
2005-11-07 11:15:49 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
totlen -= len;
|
|
|
|
from += len;
|
|
|
|
}
|
2011-09-21 01:35:08 +00:00
|
|
|
return ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
|
2011-05-25 21:59:01 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @buf: temporary buffer
|
|
|
|
* @td: descriptor for the bad block table
|
2011-09-07 20:13:33 +00:00
|
|
|
* @chip: read the table for a specific chip, -1 read all chips; applies only if
|
2011-05-25 21:59:01 +00:00
|
|
|
* NAND_BBT_PERCHIP option is set
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* Read the bad block table for all chips starting at a given page. We assume
|
|
|
|
* that the bbt bits are in consecutive order.
|
|
|
|
*/
|
2006-05-13 17:07:53 +00:00
|
|
|
static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct nand_chip *this = mtd->priv;
|
|
|
|
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)
|
2010-09-29 17:43:51 +00:00
|
|
|
res = read_bbt(mtd, buf, td->pages[i],
|
|
|
|
this->chipsize >> this->bbt_erase_shift,
|
|
|
|
td, offs);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (res)
|
|
|
|
return res;
|
2013-07-31 00:52:56 +00:00
|
|
|
offs += this->chipsize >> this->bbt_erase_shift;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-09-29 17:43:51 +00:00
|
|
|
res = read_bbt(mtd, buf, td->pages[0],
|
|
|
|
mtd->size >> this->bbt_erase_shift, td, 0);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* BBT marker is in the first page, no OOB */
|
2012-06-22 23:35:42 +00:00
|
|
|
static int scan_read_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
|
2010-09-30 19:28:01 +00:00
|
|
|
struct nand_bbt_descr *td)
|
|
|
|
{
|
|
|
|
size_t retlen;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = td->len;
|
|
|
|
if (td->options & NAND_BBT_VERSION)
|
|
|
|
len++;
|
|
|
|
|
2011-12-23 15:30:16 +00:00
|
|
|
return mtd_read(mtd, offs, len, &retlen, buf);
|
2010-09-30 19:28:01 +00:00
|
|
|
}
|
|
|
|
|
2012-06-22 23:35:45 +00:00
|
|
|
/**
|
2012-06-22 23:35:42 +00:00
|
|
|
* scan_read_oob - [GENERIC] Scan data+OOB region to buffer
|
2012-06-22 23:35:45 +00:00
|
|
|
* @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.
|
|
|
|
*/
|
2012-06-22 23:35:42 +00:00
|
|
|
static int scan_read_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
|
2006-05-29 01:26:58 +00:00
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
struct mtd_oob_ops ops;
|
2012-06-22 23:35:45 +00:00
|
|
|
int res, ret = 0;
|
2006-05-29 01:26:58 +00:00
|
|
|
|
2012-06-22 23:35:45 +00:00
|
|
|
ops.mode = MTD_OPS_PLACE_OOB;
|
2006-05-29 01:26:58 +00:00
|
|
|
ops.ooboffs = 0;
|
|
|
|
ops.ooblen = mtd->oobsize;
|
|
|
|
|
2010-02-22 18:39:37 +00:00
|
|
|
while (len > 0) {
|
2011-09-07 20:13:28 +00:00
|
|
|
ops.datbuf = buf;
|
|
|
|
ops.len = min(len, (size_t)mtd->writesize);
|
|
|
|
ops.oobbuf = buf + ops.len;
|
2010-02-22 18:39:37 +00:00
|
|
|
|
2011-12-23 16:27:05 +00:00
|
|
|
res = mtd_read_oob(mtd, offs, &ops);
|
2012-06-22 23:35:45 +00:00
|
|
|
if (res) {
|
|
|
|
if (!mtd_is_bitflip_or_eccerr(res))
|
|
|
|
return res;
|
|
|
|
else if (mtd_is_eccerr(res) || !ret)
|
|
|
|
ret = res;
|
|
|
|
}
|
2010-02-22 18:39:37 +00:00
|
|
|
|
|
|
|
buf += mtd->oobsize + mtd->writesize;
|
|
|
|
len -= mtd->writesize;
|
2012-05-11 17:51:51 +00:00
|
|
|
offs += mtd->writesize;
|
2010-02-22 18:39:37 +00:00
|
|
|
}
|
2012-06-22 23:35:45 +00:00
|
|
|
return ret;
|
2006-05-29 01:26:58 +00:00
|
|
|
}
|
|
|
|
|
2012-06-22 23:35:42 +00:00
|
|
|
static int scan_read(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
|
2010-09-30 19:28:01 +00:00
|
|
|
size_t len, struct nand_bbt_descr *td)
|
|
|
|
{
|
|
|
|
if (td->options & NAND_BBT_NO_OOB)
|
2012-06-22 23:35:42 +00:00
|
|
|
return scan_read_data(mtd, buf, offs, td);
|
2010-09-30 19:28:01 +00:00
|
|
|
else
|
2012-06-22 23:35:42 +00:00
|
|
|
return scan_read_oob(mtd, buf, offs, len);
|
2010-09-30 19:28:01 +00:00
|
|
|
}
|
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Scan write data with oob to flash */
|
2006-05-29 01:26:58 +00:00
|
|
|
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;
|
|
|
|
|
2011-08-31 01:45:40 +00:00
|
|
|
ops.mode = MTD_OPS_PLACE_OOB;
|
2006-05-29 01:26:58 +00:00
|
|
|
ops.ooboffs = 0;
|
|
|
|
ops.ooblen = mtd->oobsize;
|
|
|
|
ops.datbuf = buf;
|
|
|
|
ops.oobbuf = oob;
|
|
|
|
ops.len = len;
|
|
|
|
|
2011-12-23 16:29:55 +00:00
|
|
|
return mtd_write_oob(mtd, offs, &ops);
|
2006-05-29 01:26:58 +00:00
|
|
|
}
|
|
|
|
|
2010-09-30 19:28:01 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
|
|
|
* read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
|
2011-05-25 21:59:01 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @buf: temporary buffer
|
|
|
|
* @td: descriptor for the bad block table
|
|
|
|
* @md: descriptor for the bad block table mirror
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* Read the bad block table(s) for all chips starting at a given page. We
|
|
|
|
* assume that the bbt bits are in consecutive order.
|
|
|
|
*/
|
2012-06-22 23:35:41 +00:00
|
|
|
static void read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
|
|
|
|
struct nand_bbt_descr *td, struct nand_bbt_descr *md)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct nand_chip *this = mtd->priv;
|
|
|
|
|
2005-11-07 11:15:49 +00:00
|
|
|
/* Read the primary version, if available */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (td->options & NAND_BBT_VERSION) {
|
2012-06-22 23:35:42 +00:00
|
|
|
scan_read(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
|
2010-09-30 19:28:01 +00:00
|
|
|
mtd->writesize, td);
|
|
|
|
td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
|
2011-07-19 17:06:07 +00:00
|
|
|
pr_info("Bad block table at page %d, version 0x%02X\n",
|
2011-07-19 17:06:08 +00:00
|
|
|
td->pages[0], td->version[0]);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2005-11-07 11:15:49 +00:00
|
|
|
/* Read the mirror version, if available */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (md && (md->options & NAND_BBT_VERSION)) {
|
2012-06-22 23:35:42 +00:00
|
|
|
scan_read(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
|
2012-06-10 10:58:12 +00:00
|
|
|
mtd->writesize, md);
|
2010-09-30 19:28:01 +00:00
|
|
|
md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
|
2011-07-19 17:06:07 +00:00
|
|
|
pr_info("Bad block table at page %d, version 0x%02X\n",
|
2011-07-19 17:06:08 +00:00
|
|
|
md->pages[0], md->version[0]);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Scan a given block partially */
|
2006-05-29 01:26:58 +00:00
|
|
|
static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
|
2012-08-20 13:29:15 +00:00
|
|
|
loff_t offs, uint8_t *buf, int numpages)
|
2006-05-29 01:26:58 +00:00
|
|
|
{
|
|
|
|
struct mtd_oob_ops ops;
|
|
|
|
int j, ret;
|
|
|
|
|
|
|
|
ops.ooblen = mtd->oobsize;
|
|
|
|
ops.oobbuf = buf;
|
|
|
|
ops.ooboffs = 0;
|
|
|
|
ops.datbuf = NULL;
|
2011-08-31 01:45:40 +00:00
|
|
|
ops.mode = MTD_OPS_PLACE_OOB;
|
2006-05-29 01:26:58 +00:00
|
|
|
|
2012-08-20 13:29:15 +00:00
|
|
|
for (j = 0; j < numpages; j++) {
|
2006-05-29 01:26:58 +00:00
|
|
|
/*
|
2011-05-25 21:59:01 +00:00
|
|
|
* Read the full oob until read_oob is fixed to handle single
|
|
|
|
* byte reads for 16 bit buswidth.
|
2006-05-29 01:26:58 +00:00
|
|
|
*/
|
2011-12-23 16:27:05 +00:00
|
|
|
ret = mtd_read_oob(mtd, offs, &ops);
|
2011-06-28 23:28:58 +00:00
|
|
|
/* Ignore ECC errors when checking for BBM */
|
2011-09-21 01:34:25 +00:00
|
|
|
if (ret && !mtd_is_bitflip_or_eccerr(ret))
|
2006-05-29 01:26:58 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (check_short_pattern(buf, bd))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
offs += mtd->writesize;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
|
|
|
* create_bbt - [GENERIC] Create a bad block table by scanning the device
|
2011-05-25 21:59:01 +00:00
|
|
|
* @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
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* Create a bad block table by scanning the device for the given good/bad block
|
|
|
|
* identify pattern.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2006-05-29 01:26:58 +00:00
|
|
|
static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
|
|
|
|
struct nand_bbt_descr *bd, int chip)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct nand_chip *this = mtd->priv;
|
2013-10-30 04:41:30 +00:00
|
|
|
int i, numblocks, numpages;
|
2005-04-16 22:20:36 +00:00
|
|
|
int startblock;
|
|
|
|
loff_t from;
|
|
|
|
|
2011-07-19 17:06:07 +00:00
|
|
|
pr_info("Scanning device for bad blocks\n");
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-10-30 04:41:30 +00:00
|
|
|
if (bd->options & NAND_BBT_SCAN2NDPAGE)
|
2012-08-20 13:29:15 +00:00
|
|
|
numpages = 2;
|
mtd: nand: more BB Detection refactoring and dynamic scan options
This is a revision to PATCH 2/2 that I sent. Link:
http://lists.infradead.org/pipermail/linux-mtd/2010-July/030911.html
Added new flag for scanning of both bytes 1 and 6 of the OOB for
a BB marker (instead of simply one or the other).
The "check_pattern" and "check_short_pattern" functions were updated
to include support for scanning the two different locations in the OOB.
In order to handle increases in variety of necessary scanning patterns,
I implemented dynamic memory allocation of nand_bbt_descr structs
in new function 'nand_create_default_bbt_descr()'. This replaces
some increasingly-unwieldy, statically-declared descriptors. It can
replace several more (e.g. "flashbased" structs). However, I do not
test the flashbased options personally.
How this was tested:
I referenced 30+ data sheets (covering 100+ parts), and I tested a
selection of 10 different chips to varying degrees. Particularly, I
tested the creation of bad-block descriptors and basic BB scanning on
three parts:
ST NAND04GW3B2D, 2K page
ST NAND128W3A, 512B page
Samsung K9F1G08U0A, 2K page
To test these, I wrote some fake bad block markers to the flash (in OOB
bytes 1, 6, and elsewhere) to see if the scanning routine would detect
them properly. However, this method was somewhat limited because the
driver I am using has some bugs in its OOB write functionality.
Signed-off-by: Brian Norris <norris@broadcom.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2010-07-15 19:15:44 +00:00
|
|
|
else
|
2012-08-20 13:29:15 +00:00
|
|
|
numpages = 1;
|
2005-02-16 17:09:39 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (chip == -1) {
|
2013-07-31 00:52:56 +00:00
|
|
|
numblocks = mtd->size >> this->bbt_erase_shift;
|
2005-04-16 22:20:36 +00:00
|
|
|
startblock = 0;
|
|
|
|
from = 0;
|
|
|
|
} else {
|
|
|
|
if (chip >= this->numchips) {
|
2011-07-19 17:06:07 +00:00
|
|
|
pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
|
2006-05-13 17:07:53 +00:00
|
|
|
chip + 1, this->numchips);
|
2005-02-16 17:09:39 +00:00
|
|
|
return -EINVAL;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2013-07-31 00:52:56 +00:00
|
|
|
numblocks = this->chipsize >> this->bbt_erase_shift;
|
2005-04-16 22:20:36 +00:00
|
|
|
startblock = chip * numblocks;
|
|
|
|
numblocks += startblock;
|
2013-07-31 00:52:56 +00:00
|
|
|
from = (loff_t)startblock << this->bbt_erase_shift;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2011-05-31 23:31:21 +00:00
|
|
|
if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
|
2012-08-20 13:29:15 +00:00
|
|
|
from += mtd->erasesize - (mtd->writesize * numpages);
|
2010-05-05 03:58:10 +00:00
|
|
|
|
2013-07-31 00:52:56 +00:00
|
|
|
for (i = startblock; i < numblocks; i++) {
|
2005-02-11 10:14:15 +00:00
|
|
|
int ret;
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2010-09-30 19:28:01 +00:00
|
|
|
BUG_ON(bd->options & NAND_BBT_NO_OOB);
|
|
|
|
|
2013-10-30 04:41:30 +00:00
|
|
|
ret = scan_block_fast(mtd, bd, from, buf, numpages);
|
2006-05-29 01:26:58 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (ret) {
|
2013-07-31 00:52:56 +00:00
|
|
|
bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
|
2011-07-19 17:06:07 +00:00
|
|
|
pr_warn("Bad eraseblock %d at 0x%012llx\n",
|
2013-07-31 00:52:56 +00:00
|
|
|
i, (unsigned long long)from);
|
2006-05-29 22:37:34 +00:00
|
|
|
mtd->ecc_stats.badblocks++;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2006-05-29 01:26:58 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
from += (1 << this->bbt_erase_shift);
|
|
|
|
}
|
2005-02-11 10:14:15 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* search_bbt - [GENERIC] scan the device for a specific bad block table
|
2011-05-25 21:59:01 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @buf: temporary buffer
|
|
|
|
* @td: descriptor for the bad block table
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* 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.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* The bbt ident pattern resides in the oob area of the first page in a block.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2006-05-13 17:07:53 +00:00
|
|
|
static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct nand_chip *this = mtd->priv;
|
|
|
|
int i, chips;
|
|
|
|
int bits, startblock, block, dir;
|
2006-05-22 21:18:05 +00:00
|
|
|
int scanlen = mtd->writesize + mtd->oobsize;
|
2005-04-16 22:20:36 +00:00
|
|
|
int bbtblocks;
|
2006-05-29 01:26:58 +00:00
|
|
|
int blocktopage = this->bbt_erase_shift - this->page_shift;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Search direction top -> down? */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (td->options & NAND_BBT_LASTBLOCK) {
|
2006-05-13 17:07:53 +00:00
|
|
|
startblock = (mtd->size >> this->bbt_erase_shift) - 1;
|
2005-04-16 22:20:36 +00:00
|
|
|
dir = -1;
|
|
|
|
} else {
|
2005-11-07 11:15:49 +00:00
|
|
|
startblock = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
dir = 1;
|
2005-11-07 11:15:49 +00:00
|
|
|
}
|
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Do we have a bbt per chip? */
|
2005-04-16 22:20:36 +00:00
|
|
|
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;
|
|
|
|
}
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Number of bits for each erase block in the bbt */
|
|
|
|
bits = td->options & NAND_BBT_NRBITS_MSK;
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
for (i = 0; i < chips; i++) {
|
|
|
|
/* Reset version information */
|
2005-11-07 11:15:49 +00:00
|
|
|
td->version[i] = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
td->pages[i] = -1;
|
|
|
|
/* Scan the maximum number of blocks */
|
|
|
|
for (block = 0; block < td->maxblocks; block++) {
|
2006-05-29 01:26:58 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
int actblock = startblock + dir * block;
|
2008-12-10 13:37:21 +00:00
|
|
|
loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
|
2006-05-29 01:26:58 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Read first page */
|
2012-06-22 23:35:42 +00:00
|
|
|
scan_read(mtd, buf, offs, mtd->writesize, td);
|
2006-05-22 21:18:05 +00:00
|
|
|
if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
|
2006-05-29 01:26:58 +00:00
|
|
|
td->pages[i] = actblock << blocktopage;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (td->options & NAND_BBT_VERSION) {
|
2010-09-30 19:28:01 +00:00
|
|
|
offs = bbt_get_ver_offs(mtd, td);
|
|
|
|
td->version[i] = buf[offs];
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
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)
|
2011-07-19 17:06:07 +00:00
|
|
|
pr_warn("Bad block table not found for chip %d\n", i);
|
2005-04-16 22:20:36 +00:00
|
|
|
else
|
2011-07-19 17:06:08 +00:00
|
|
|
pr_info("Bad block table found at page %d, version "
|
|
|
|
"0x%02X\n", td->pages[i], td->version[i]);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2005-11-07 11:15:49 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* search_read_bbts - [GENERIC] scan the device for bad block table(s)
|
2011-05-25 21:59:01 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @buf: temporary buffer
|
|
|
|
* @td: descriptor for the bad block table
|
|
|
|
* @md: descriptor for the bad block table mirror
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* Search and read the bad block table(s).
|
|
|
|
*/
|
2012-06-22 23:35:41 +00:00
|
|
|
static void search_read_bbts(struct mtd_info *mtd, uint8_t *buf,
|
|
|
|
struct nand_bbt_descr *td,
|
|
|
|
struct nand_bbt_descr *md)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
/* Search the primary table */
|
2006-05-13 17:07:53 +00:00
|
|
|
search_bbt(mtd, buf, td);
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Search the mirror table */
|
|
|
|
if (md)
|
2006-05-13 17:07:53 +00:00
|
|
|
search_bbt(mtd, buf, md);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2005-11-07 11:15:49 +00:00
|
|
|
/**
|
2005-04-16 22:20:36 +00:00
|
|
|
* write_bbt - [GENERIC] (Re)write the bad block table
|
2011-05-25 21:59:01 +00:00
|
|
|
* @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
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* (Re)write the bad block table.
|
|
|
|
*/
|
2006-05-13 17:07:53 +00:00
|
|
|
static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
|
2006-05-23 15:21:03 +00:00
|
|
|
struct nand_bbt_descr *td, struct nand_bbt_descr *md,
|
|
|
|
int chipsel)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct nand_chip *this = mtd->priv;
|
|
|
|
struct erase_info einfo;
|
2013-07-31 00:52:56 +00:00
|
|
|
int i, res, chip = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
|
2013-07-31 00:52:56 +00:00
|
|
|
int nrchips, pageoffs, ooboffs;
|
2005-04-16 22:20:36 +00:00
|
|
|
uint8_t msk[4];
|
|
|
|
uint8_t rcode = td->reserved_block_code;
|
2006-05-29 01:26:58 +00:00
|
|
|
size_t retlen, len = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
loff_t to;
|
2006-05-29 01:26:58 +00:00
|
|
|
struct mtd_oob_ops ops;
|
|
|
|
|
|
|
|
ops.ooblen = mtd->oobsize;
|
|
|
|
ops.ooboffs = 0;
|
|
|
|
ops.datbuf = NULL;
|
2011-08-31 01:45:40 +00:00
|
|
|
ops.mode = MTD_OPS_PLACE_OOB;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (!rcode)
|
|
|
|
rcode = 0xff;
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Write bad block table per chip rather than per device? */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (td->options & NAND_BBT_PERCHIP) {
|
2006-05-13 17:07:53 +00:00
|
|
|
numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Full device write or specific chip? */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (chipsel == -1) {
|
|
|
|
nrchips = this->numchips;
|
|
|
|
} else {
|
|
|
|
nrchips = chipsel + 1;
|
|
|
|
chip = chipsel;
|
|
|
|
}
|
|
|
|
} else {
|
2006-05-13 17:07:53 +00:00
|
|
|
numblocks = (int)(mtd->size >> this->bbt_erase_shift);
|
2005-04-16 22:20:36 +00:00
|
|
|
nrchips = 1;
|
2005-11-07 11:15:49 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Loop through the chips */
|
|
|
|
for (; chip < nrchips; chip++) {
|
2011-05-25 21:59:01 +00:00
|
|
|
/*
|
|
|
|
* There was already a version of the table, reuse the page
|
2005-11-07 11:15:49 +00:00
|
|
|
* This applies for absolute placement too, as we have the
|
2005-04-16 22:20:36 +00:00
|
|
|
* page nr. in td->pages.
|
|
|
|
*/
|
|
|
|
if (td->pages[chip] != -1) {
|
|
|
|
page = td->pages[chip];
|
|
|
|
goto write;
|
2005-11-07 11:15:49 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/*
|
|
|
|
* Automatic placement of the bad block table. Search direction
|
|
|
|
* top -> down?
|
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
if (td->options & NAND_BBT_LASTBLOCK) {
|
|
|
|
startblock = numblocks * (chip + 1) - 1;
|
|
|
|
dir = -1;
|
|
|
|
} else {
|
|
|
|
startblock = chip * numblocks;
|
|
|
|
dir = 1;
|
2005-11-07 11:15:49 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
for (i = 0; i < td->maxblocks; i++) {
|
|
|
|
int block = startblock + dir * i;
|
|
|
|
/* Check, if the block is bad */
|
2013-07-31 00:52:55 +00:00
|
|
|
switch (bbt_get_entry(this, block)) {
|
|
|
|
case BBT_BLOCK_WORN:
|
|
|
|
case BBT_BLOCK_FACTORY_BAD:
|
2005-04-16 22:20:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2006-05-23 15:21:03 +00:00
|
|
|
page = block <<
|
|
|
|
(this->bbt_erase_shift - this->page_shift);
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Check, if the block is used by the mirror table */
|
|
|
|
if (!md || md->pages[chip] != page)
|
|
|
|
goto write;
|
|
|
|
}
|
2011-07-19 17:06:07 +00:00
|
|
|
pr_err("No space left to write bad block table\n");
|
2005-04-16 22:20:36 +00:00
|
|
|
return -ENOSPC;
|
2006-05-13 17:07:53 +00:00
|
|
|
write:
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* Set up shift count and masks for the flash table */
|
|
|
|
bits = td->options & NAND_BBT_NRBITS_MSK;
|
2006-05-23 15:21:03 +00:00
|
|
|
msk[2] = ~rcode;
|
2005-04-16 22:20:36 +00:00
|
|
|
switch (bits) {
|
2006-05-23 15:21:03 +00:00
|
|
|
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;
|
2005-04-16 22:20:36 +00:00
|
|
|
default: return -EINVAL;
|
|
|
|
}
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2011-09-07 20:13:33 +00:00
|
|
|
to = ((loff_t)page) << this->page_shift;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Must we save the block contents? */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (td->options & NAND_BBT_SAVECONTENT) {
|
|
|
|
/* Make it block aligned */
|
2011-09-07 20:13:33 +00:00
|
|
|
to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1));
|
2005-04-16 22:20:36 +00:00
|
|
|
len = 1 << this->bbt_erase_shift;
|
2011-12-23 15:30:16 +00:00
|
|
|
res = mtd_read(mtd, to, len, &retlen, buf);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (res < 0) {
|
|
|
|
if (retlen != len) {
|
2011-07-19 17:06:08 +00:00
|
|
|
pr_info("nand_bbt: error reading block "
|
|
|
|
"for writing the bad block table\n");
|
2005-04-16 22:20:36 +00:00
|
|
|
return res;
|
|
|
|
}
|
2011-07-19 17:06:08 +00:00
|
|
|
pr_warn("nand_bbt: ECC error while reading "
|
|
|
|
"block for writing bad block table\n");
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2006-05-23 15:21:03 +00:00
|
|
|
/* Read oob data */
|
2006-11-03 15:20:38 +00:00
|
|
|
ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
|
2006-05-29 01:26:58 +00:00
|
|
|
ops.oobbuf = &buf[len];
|
2011-12-23 16:27:05 +00:00
|
|
|
res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
|
2006-11-03 15:20:38 +00:00
|
|
|
if (res < 0 || ops.oobretlen != ops.ooblen)
|
2006-05-23 15:21:03 +00:00
|
|
|
goto outerr;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* 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 */
|
2011-09-07 20:13:33 +00:00
|
|
|
memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
|
2006-05-23 15:21:03 +00:00
|
|
|
ooboffs = len + (pageoffs * mtd->oobsize);
|
|
|
|
|
2010-09-30 19:28:01 +00:00
|
|
|
} else if (td->options & NAND_BBT_NO_OOB) {
|
|
|
|
ooboffs = 0;
|
|
|
|
offs = td->len;
|
2011-05-25 21:59:01 +00:00
|
|
|
/* The version byte */
|
2010-09-30 19:28:01 +00:00
|
|
|
if (td->options & NAND_BBT_VERSION)
|
|
|
|
offs++;
|
|
|
|
/* Calc length */
|
2011-09-07 20:13:33 +00:00
|
|
|
len = (size_t)(numblocks >> sft);
|
2010-09-30 19:28:01 +00:00
|
|
|
len += offs;
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Make it page aligned! */
|
2010-09-30 19:28:01 +00:00
|
|
|
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);
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
|
|
|
/* Calc length */
|
2011-09-07 20:13:33 +00:00
|
|
|
len = (size_t)(numblocks >> sft);
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Make it page aligned! */
|
2010-09-29 17:43:50 +00:00
|
|
|
len = ALIGN(len, mtd->writesize);
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Preset the buffer with 0xff */
|
2006-05-23 15:21:03 +00:00
|
|
|
memset(buf, 0xff, len +
|
|
|
|
(len >> this->page_shift)* mtd->oobsize);
|
2005-04-16 22:20:36 +00:00
|
|
|
offs = 0;
|
2006-05-23 15:21:03 +00:00
|
|
|
ooboffs = len;
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Pattern is located in oob area of first page */
|
2006-05-23 15:21:03 +00:00
|
|
|
memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2006-05-23 15:21:03 +00:00
|
|
|
if (td->options & NAND_BBT_VERSION)
|
|
|
|
buf[ooboffs + td->veroffs] = td->version[chip];
|
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Walk through the memory table */
|
2013-07-31 00:52:56 +00:00
|
|
|
for (i = 0; i < numblocks; i++) {
|
2005-04-16 22:20:36 +00:00
|
|
|
uint8_t dat;
|
2013-07-31 00:52:56 +00:00
|
|
|
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);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2006-05-13 17:07:53 +00:00
|
|
|
memset(&einfo, 0, sizeof(einfo));
|
2005-04-16 22:20:36 +00:00
|
|
|
einfo.mtd = mtd;
|
2008-12-10 13:37:21 +00:00
|
|
|
einfo.addr = to;
|
2005-04-16 22:20:36 +00:00
|
|
|
einfo.len = 1 << this->bbt_erase_shift;
|
2006-05-13 17:07:53 +00:00
|
|
|
res = nand_erase_nand(mtd, &einfo, 1);
|
2006-05-23 15:21:03 +00:00
|
|
|
if (res < 0)
|
|
|
|
goto outerr;
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2010-09-30 19:28:01 +00:00
|
|
|
res = scan_write_bbt(mtd, to, len, buf,
|
|
|
|
td->options & NAND_BBT_NO_OOB ? NULL :
|
|
|
|
&buf[len]);
|
2006-05-23 15:21:03 +00:00
|
|
|
if (res < 0)
|
|
|
|
goto outerr;
|
|
|
|
|
2011-07-19 17:06:08 +00:00
|
|
|
pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
|
|
|
|
(unsigned long long)to, td->version[chip]);
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Mark it as used */
|
|
|
|
td->pages[chip] = page;
|
2005-11-07 11:15:49 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
2006-05-23 15:21:03 +00:00
|
|
|
|
|
|
|
outerr:
|
2011-07-19 17:06:08 +00:00
|
|
|
pr_warn("nand_bbt: error while writing bad block table %d\n", res);
|
2006-05-23 15:21:03 +00:00
|
|
|
return res;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nand_memory_bbt - [GENERIC] create a memory based bad block table
|
2011-05-25 21:59:01 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @bd: descriptor for the good/bad block search pattern
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* The function creates a memory based bbt by scanning the device for
|
|
|
|
* manufacturer / software marked good / bad blocks.
|
|
|
|
*/
|
2006-05-13 17:07:53 +00:00
|
|
|
static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct nand_chip *this = mtd->priv;
|
|
|
|
|
2006-09-25 16:08:04 +00:00
|
|
|
return create_bbt(mtd, this->buffers->databuf, bd, -1);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-05-13 17:07:53 +00:00
|
|
|
* check_create - [GENERIC] create and write bbt(s) if necessary
|
2011-05-25 21:59:01 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @buf: temporary buffer
|
|
|
|
* @bd: descriptor for the good/bad block search pattern
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2006-05-13 17:07:53 +00:00
|
|
|
static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2011-09-21 01:35:34 +00:00
|
|
|
int i, chips, writeops, create, chipsel, res, res2;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct nand_chip *this = mtd->priv;
|
|
|
|
struct nand_bbt_descr *td = this->bbt_td;
|
|
|
|
struct nand_bbt_descr *md = this->bbt_md;
|
|
|
|
struct nand_bbt_descr *rd, *rd2;
|
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Do we have a bbt per chip? */
|
2005-11-07 11:15:49 +00:00
|
|
|
if (td->options & NAND_BBT_PERCHIP)
|
2005-04-16 22:20:36 +00:00
|
|
|
chips = this->numchips;
|
2005-11-07 11:15:49 +00:00
|
|
|
else
|
2005-04-16 22:20:36 +00:00
|
|
|
chips = 1;
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
for (i = 0; i < chips; i++) {
|
|
|
|
writeops = 0;
|
2011-09-07 20:13:35 +00:00
|
|
|
create = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
rd = NULL;
|
|
|
|
rd2 = NULL;
|
2011-09-21 01:35:34 +00:00
|
|
|
res = res2 = 0;
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Per chip or per device? */
|
2005-04-16 22:20:36 +00:00
|
|
|
chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Mirrored table available? */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (md) {
|
|
|
|
if (td->pages[i] == -1 && md->pages[i] == -1) {
|
2011-09-07 20:13:35 +00:00
|
|
|
create = 1;
|
2005-04-16 22:20:36 +00:00
|
|
|
writeops = 0x03;
|
2011-09-07 20:13:34 +00:00
|
|
|
} else if (td->pages[i] == -1) {
|
2005-11-07 11:15:49 +00:00
|
|
|
rd = md;
|
2011-09-07 20:13:33 +00:00
|
|
|
writeops = 0x01;
|
2011-09-07 20:13:34 +00:00
|
|
|
} else if (md->pages[i] == -1) {
|
2005-04-16 22:20:36 +00:00
|
|
|
rd = td;
|
2011-09-07 20:13:33 +00:00
|
|
|
writeops = 0x02;
|
2011-09-07 20:13:34 +00:00
|
|
|
} else if (td->version[i] == md->version[i]) {
|
2005-04-16 22:20:36 +00:00
|
|
|
rd = td;
|
|
|
|
if (!(td->options & NAND_BBT_VERSION))
|
|
|
|
rd2 = md;
|
2011-09-07 20:13:34 +00:00
|
|
|
} else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
|
2005-04-16 22:20:36 +00:00
|
|
|
rd = td;
|
2011-09-07 20:13:33 +00:00
|
|
|
writeops = 0x02;
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
|
|
|
rd = md;
|
2011-09-07 20:13:33 +00:00
|
|
|
writeops = 0x01;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (td->pages[i] == -1) {
|
2011-09-07 20:13:35 +00:00
|
|
|
create = 1;
|
2005-04-16 22:20:36 +00:00
|
|
|
writeops = 0x01;
|
2011-09-07 20:13:35 +00:00
|
|
|
} else {
|
|
|
|
rd = td;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2011-09-07 20:13:35 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Read back first? */
|
2011-09-21 01:35:34 +00:00
|
|
|
if (rd) {
|
|
|
|
res = read_abs_bbt(mtd, buf, rd, chipsel);
|
|
|
|
if (mtd_is_eccerr(res)) {
|
|
|
|
/* Mark table as invalid */
|
|
|
|
rd->pages[i] = -1;
|
mtd: nand: wait to set BBT version
Because there are so many cases of checking, writing, and re-writing of
the bad block table(s), we might as well wait until the we've settled on
a valid, clean copy of the table. This also prevents us from falsely
incrementing the table version. For example, we may have the following:
Primary table, with version 0x02
Mirror table, with version 0x01
Primary table has uncorrectable ECC errors
If we don't have this fix applied, then we will:
Choose to read the primary table (higher version)
Set mirror table version to 0x02
Read back primary table
Invalidate table because of ECC errors
Retry readback operation with mirror table, now version 0x02
Mirrored table reads cleanly
Writeback BBT to primary table location (with "version 0x02")
However, the mirrored table shouldn't have a new version number.
Instead, we actually want:
Choose to read the primary table (higher version)
Read back primary table
Invalidate table because of ECC errors
Retry readback with mirror table (version 0x01)
Mirrored table reads cleanly
Set both tables to version 0x01
Writeback BBT to primary table location (version 0x01)
Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
2011-09-21 01:35:57 +00:00
|
|
|
rd->version[i] = 0;
|
2011-09-21 01:35:34 +00:00
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2011-05-25 21:59:01 +00:00
|
|
|
/* If they weren't versioned, read both */
|
2011-09-21 01:35:34 +00:00
|
|
|
if (rd2) {
|
|
|
|
res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
|
|
|
|
if (mtd_is_eccerr(res2)) {
|
|
|
|
/* Mark table as invalid */
|
|
|
|
rd2->pages[i] = -1;
|
mtd: nand: wait to set BBT version
Because there are so many cases of checking, writing, and re-writing of
the bad block table(s), we might as well wait until the we've settled on
a valid, clean copy of the table. This also prevents us from falsely
incrementing the table version. For example, we may have the following:
Primary table, with version 0x02
Mirror table, with version 0x01
Primary table has uncorrectable ECC errors
If we don't have this fix applied, then we will:
Choose to read the primary table (higher version)
Set mirror table version to 0x02
Read back primary table
Invalidate table because of ECC errors
Retry readback operation with mirror table, now version 0x02
Mirrored table reads cleanly
Writeback BBT to primary table location (with "version 0x02")
However, the mirrored table shouldn't have a new version number.
Instead, we actually want:
Choose to read the primary table (higher version)
Read back primary table
Invalidate table because of ECC errors
Retry readback with mirror table (version 0x01)
Mirrored table reads cleanly
Set both tables to version 0x01
Writeback BBT to primary table location (version 0x01)
Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
2011-09-21 01:35:57 +00:00
|
|
|
rd2->version[i] = 0;
|
2011-09-21 01:35:34 +00:00
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Scrub the flash table(s)? */
|
|
|
|
if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
|
|
|
|
writeops = 0x03;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
mtd: nand: wait to set BBT version
Because there are so many cases of checking, writing, and re-writing of
the bad block table(s), we might as well wait until the we've settled on
a valid, clean copy of the table. This also prevents us from falsely
incrementing the table version. For example, we may have the following:
Primary table, with version 0x02
Mirror table, with version 0x01
Primary table has uncorrectable ECC errors
If we don't have this fix applied, then we will:
Choose to read the primary table (higher version)
Set mirror table version to 0x02
Read back primary table
Invalidate table because of ECC errors
Retry readback operation with mirror table, now version 0x02
Mirrored table reads cleanly
Writeback BBT to primary table location (with "version 0x02")
However, the mirrored table shouldn't have a new version number.
Instead, we actually want:
Choose to read the primary table (higher version)
Read back primary table
Invalidate table because of ECC errors
Retry readback with mirror table (version 0x01)
Mirrored table reads cleanly
Set both tables to version 0x01
Writeback BBT to primary table location (version 0x01)
Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
2011-09-21 01:35:57 +00:00
|
|
|
/* Update version numbers before writing */
|
|
|
|
if (md) {
|
|
|
|
td->version[i] = max(td->version[i], md->version[i]);
|
|
|
|
md->version[i] = td->version[i];
|
|
|
|
}
|
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Write the bad block table to the device? */
|
2005-04-16 22:20:36 +00:00
|
|
|
if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
|
2006-05-13 17:07:53 +00:00
|
|
|
res = write_bbt(mtd, buf, td, md, chipsel);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
}
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Write the mirror bad block table to the device? */
|
2005-04-16 22:20:36 +00:00
|
|
|
if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
|
2006-05-13 17:07:53 +00:00
|
|
|
res = write_bbt(mtd, buf, md, td, chipsel);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2005-11-07 11:15:49 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-11-07 11:15:49 +00:00
|
|
|
* mark_bbt_regions - [GENERIC] mark the bad block table regions
|
2011-05-25 21:59:01 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @td: bad block table descriptor
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* The bad block table regions are marked as "bad" to prevent accidental
|
|
|
|
* erasures / writes. The regions are identified by the mark 0x02.
|
|
|
|
*/
|
2006-05-13 17:07:53 +00:00
|
|
|
static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct nand_chip *this = mtd->priv;
|
|
|
|
int i, j, chips, block, nrblocks, update;
|
2013-07-31 00:52:55 +00:00
|
|
|
uint8_t oldval;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Do we have a bbt per chip? */
|
2005-04-16 22:20:36 +00:00
|
|
|
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);
|
2005-11-07 11:15:49 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
for (i = 0; i < chips; i++) {
|
|
|
|
if ((td->options & NAND_BBT_ABSPAGE) ||
|
|
|
|
!(td->options & NAND_BBT_WRITE)) {
|
2006-05-13 17:07:53 +00:00
|
|
|
if (td->pages[i] == -1)
|
|
|
|
continue;
|
2005-04-16 22:20:36 +00:00
|
|
|
block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
|
2013-07-31 00:52:56 +00:00
|
|
|
oldval = bbt_get_entry(this, block);
|
|
|
|
bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
|
2013-07-31 00:52:55 +00:00
|
|
|
if ((oldval != BBT_BLOCK_RESERVED) &&
|
|
|
|
td->reserved_block_code)
|
2013-07-31 00:52:56 +00:00
|
|
|
nand_update_bbt(mtd, (loff_t)block <<
|
|
|
|
this->bbt_erase_shift);
|
2005-04-16 22:20:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
update = 0;
|
|
|
|
if (td->options & NAND_BBT_LASTBLOCK)
|
|
|
|
block = ((i + 1) * nrblocks) - td->maxblocks;
|
2005-11-07 11:15:49 +00:00
|
|
|
else
|
2005-04-16 22:20:36 +00:00
|
|
|
block = i * nrblocks;
|
|
|
|
for (j = 0; j < td->maxblocks; j++) {
|
2013-07-31 00:52:56 +00:00
|
|
|
oldval = bbt_get_entry(this, block);
|
|
|
|
bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
|
2013-07-31 00:52:55 +00:00
|
|
|
if (oldval != BBT_BLOCK_RESERVED)
|
2006-05-13 17:07:53 +00:00
|
|
|
update = 1;
|
2013-07-31 00:52:56 +00:00
|
|
|
block++;
|
2005-11-07 11:15:49 +00:00
|
|
|
}
|
2011-05-25 21:59:01 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
if (update && td->reserved_block_code)
|
2013-07-31 00:52:56 +00:00
|
|
|
nand_update_bbt(mtd, (loff_t)(block - 1) <<
|
|
|
|
this->bbt_erase_shift);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-30 19:28:01 +00:00
|
|
|
/**
|
|
|
|
* verify_bbt_descr - verify the bad block description
|
2011-05-25 21:59:01 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @bd: the table to verify
|
2010-09-30 19:28:01 +00:00
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
struct nand_chip *this = mtd->priv;
|
2011-02-07 20:48:25 +00:00
|
|
|
u32 pattern_len;
|
|
|
|
u32 bits;
|
2010-09-30 19:28:01 +00:00
|
|
|
u32 table_size;
|
|
|
|
|
|
|
|
if (!bd)
|
|
|
|
return;
|
2011-02-07 20:48:25 +00:00
|
|
|
|
|
|
|
pattern_len = bd->len;
|
|
|
|
bits = bd->options & NAND_BBT_NRBITS_MSK;
|
|
|
|
|
2011-05-31 23:31:22 +00:00
|
|
|
BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
|
2011-05-31 23:31:23 +00:00
|
|
|
!(this->bbt_options & NAND_BBT_USE_FLASH));
|
2010-09-30 19:28:01 +00:00
|
|
|
BUG_ON(!bits);
|
|
|
|
|
|
|
|
if (bd->options & NAND_BBT_VERSION)
|
|
|
|
pattern_len++;
|
|
|
|
|
|
|
|
if (bd->options & NAND_BBT_NO_OOB) {
|
2011-05-31 23:31:23 +00:00
|
|
|
BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
|
2011-05-31 23:31:22 +00:00
|
|
|
BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
|
2010-09-30 19:28:01 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
|
|
|
* nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
|
2011-05-25 21:59:01 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @bd: descriptor for the good/bad block search pattern
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* 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.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* The bad block table memory is allocated here. It must be freed by calling
|
|
|
|
* the nand_free_bbt function.
|
|
|
|
*/
|
2006-05-13 17:07:53 +00:00
|
|
|
int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct nand_chip *this = mtd->priv;
|
|
|
|
int len, res = 0;
|
|
|
|
uint8_t *buf;
|
|
|
|
struct nand_bbt_descr *td = this->bbt_td;
|
|
|
|
struct nand_bbt_descr *md = this->bbt_md;
|
|
|
|
|
|
|
|
len = mtd->size >> (this->bbt_erase_shift + 2);
|
2011-05-25 21:59:01 +00:00
|
|
|
/*
|
|
|
|
* Allocate memory (2bit per block) and clear the memory bad block
|
|
|
|
* table.
|
|
|
|
*/
|
2006-11-15 19:10:29 +00:00
|
|
|
this->bbt = kzalloc(len, GFP_KERNEL);
|
2011-06-07 23:01:54 +00:00
|
|
|
if (!this->bbt)
|
2005-04-16 22:20:36 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/*
|
|
|
|
* If no primary table decriptor is given, scan the device to build a
|
|
|
|
* memory based bad block table.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2005-02-11 10:14:15 +00:00
|
|
|
if (!td) {
|
|
|
|
if ((res = nand_memory_bbt(mtd, bd))) {
|
2011-07-19 17:06:08 +00:00
|
|
|
pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
|
2006-05-13 17:07:53 +00:00
|
|
|
kfree(this->bbt);
|
2005-02-11 10:14:15 +00:00
|
|
|
this->bbt = NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2010-09-30 19:28:01 +00:00
|
|
|
verify_bbt_descr(mtd, td);
|
|
|
|
verify_bbt_descr(mtd, md);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* Allocate a temporary buffer for one eraseblock incl. oob */
|
|
|
|
len = (1 << this->bbt_erase_shift);
|
|
|
|
len += (len >> this->page_shift) * mtd->oobsize;
|
2006-05-13 03:03:42 +00:00
|
|
|
buf = vmalloc(len);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!buf) {
|
2006-05-13 17:07:53 +00:00
|
|
|
kfree(this->bbt);
|
2005-04-16 22:20:36 +00:00
|
|
|
this->bbt = NULL;
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Is the bbt at a given page? */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (td->options & NAND_BBT_ABSPAGE) {
|
2012-06-22 23:35:41 +00:00
|
|
|
read_abs_bbts(mtd, buf, td, md);
|
2005-11-07 11:15:49 +00:00
|
|
|
} else {
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Search the bad block table using a pattern in oob */
|
2012-06-22 23:35:41 +00:00
|
|
|
search_read_bbts(mtd, buf, td, md);
|
2005-11-07 11:15:49 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2012-06-22 23:35:41 +00:00
|
|
|
res = check_create(mtd, buf, bd);
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Prevent the bbt regions from erasing / writing */
|
2006-05-13 17:07:53 +00:00
|
|
|
mark_bbt_region(mtd, td);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (md)
|
2006-05-13 17:07:53 +00:00
|
|
|
mark_bbt_region(mtd, md);
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2006-05-13 17:07:53 +00:00
|
|
|
vfree(buf);
|
2005-04-16 22:20:36 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-31 00:52:59 +00:00
|
|
|
* nand_update_bbt - update bad block table(s)
|
2011-05-25 21:59:01 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @offs: the offset of the newly marked block
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* The function updates the bad block table(s).
|
|
|
|
*/
|
2013-07-31 00:52:59 +00:00
|
|
|
static int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct nand_chip *this = mtd->priv;
|
2011-09-07 20:13:32 +00:00
|
|
|
int len, res = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
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;
|
2006-05-13 17:07:53 +00:00
|
|
|
buf = kmalloc(len, GFP_KERNEL);
|
2011-06-07 23:01:54 +00:00
|
|
|
if (!buf)
|
2005-04-16 22:20:36 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Do we have a bbt per chip? */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (td->options & NAND_BBT_PERCHIP) {
|
2006-05-13 17:07:53 +00:00
|
|
|
chip = (int)(offs >> this->chip_shift);
|
2005-04-16 22:20:36 +00:00
|
|
|
chipsel = chip;
|
|
|
|
} else {
|
|
|
|
chip = 0;
|
|
|
|
chipsel = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
td->version[chip]++;
|
|
|
|
if (md)
|
2005-11-07 11:15:49 +00:00
|
|
|
md->version[chip]++;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Write the bad block table to the device? */
|
2011-09-07 20:13:32 +00:00
|
|
|
if (td->options & NAND_BBT_WRITE) {
|
2006-05-13 17:07:53 +00:00
|
|
|
res = write_bbt(mtd, buf, td, md, chipsel);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (res < 0)
|
|
|
|
goto out;
|
|
|
|
}
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Write the mirror bad block table to the device? */
|
2011-09-07 20:13:32 +00:00
|
|
|
if (md && (md->options & NAND_BBT_WRITE)) {
|
2006-05-13 17:07:53 +00:00
|
|
|
res = write_bbt(mtd, buf, md, td, chipsel);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-05-13 17:07:53 +00:00
|
|
|
out:
|
|
|
|
kfree(buf);
|
2005-04-16 22:20:36 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/*
|
|
|
|
* Define some generic bad / good block scan pattern which are used
|
|
|
|
* while scanning a device for factory marked good / bad blocks.
|
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
|
|
|
|
|
2011-06-23 21:12:08 +00:00
|
|
|
/* Generic flash bbt descriptors */
|
2005-04-16 22:20:36 +00:00
|
|
|
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 = {
|
2005-11-07 11:15:49 +00:00
|
|
|
.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
|
2005-04-16 22:20:36 +00:00
|
|
|
| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
|
|
|
|
.offs = 8,
|
|
|
|
.len = 4,
|
|
|
|
.veroffs = 12,
|
2012-09-11 13:50:54 +00:00
|
|
|
.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
|
2005-04-16 22:20:36 +00:00
|
|
|
.pattern = bbt_pattern
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct nand_bbt_descr bbt_mirror_descr = {
|
2005-11-07 11:15:49 +00:00
|
|
|
.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
|
2005-04-16 22:20:36 +00:00
|
|
|
| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
|
|
|
|
.offs = 8,
|
|
|
|
.len = 4,
|
|
|
|
.veroffs = 12,
|
2012-09-11 13:50:54 +00:00
|
|
|
.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
|
2005-04-16 22:20:36 +00:00
|
|
|
.pattern = mirror_pattern
|
|
|
|
};
|
|
|
|
|
2012-06-22 23:35:40 +00:00
|
|
|
static struct nand_bbt_descr bbt_main_no_oob_descr = {
|
2010-09-30 19:28:01 +00:00
|
|
|
.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,
|
2012-09-11 13:50:54 +00:00
|
|
|
.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
|
2010-09-30 19:28:01 +00:00
|
|
|
.pattern = bbt_pattern
|
|
|
|
};
|
|
|
|
|
2012-06-22 23:35:40 +00:00
|
|
|
static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
|
2010-09-30 19:28:01 +00:00
|
|
|
.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,
|
2012-09-11 13:50:54 +00:00
|
|
|
.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
|
2010-09-30 19:28:01 +00:00
|
|
|
.pattern = mirror_pattern
|
|
|
|
};
|
|
|
|
|
2011-09-21 01:36:42 +00:00
|
|
|
#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
|
mtd: nand: more BB Detection refactoring and dynamic scan options
This is a revision to PATCH 2/2 that I sent. Link:
http://lists.infradead.org/pipermail/linux-mtd/2010-July/030911.html
Added new flag for scanning of both bytes 1 and 6 of the OOB for
a BB marker (instead of simply one or the other).
The "check_pattern" and "check_short_pattern" functions were updated
to include support for scanning the two different locations in the OOB.
In order to handle increases in variety of necessary scanning patterns,
I implemented dynamic memory allocation of nand_bbt_descr structs
in new function 'nand_create_default_bbt_descr()'. This replaces
some increasingly-unwieldy, statically-declared descriptors. It can
replace several more (e.g. "flashbased" structs). However, I do not
test the flashbased options personally.
How this was tested:
I referenced 30+ data sheets (covering 100+ parts), and I tested a
selection of 10 different chips to varying degrees. Particularly, I
tested the creation of bad-block descriptors and basic BB scanning on
three parts:
ST NAND04GW3B2D, 2K page
ST NAND128W3A, 512B page
Samsung K9F1G08U0A, 2K page
To test these, I wrote some fake bad block markers to the flash (in OOB
bytes 1, 6, and elsewhere) to see if the scanning routine would detect
them properly. However, this method was somewhat limited because the
driver I am using has some bugs in its OOB write functionality.
Signed-off-by: Brian Norris <norris@broadcom.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2010-07-15 19:15:44 +00:00
|
|
|
/**
|
2011-09-21 01:36:42 +00:00
|
|
|
* nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
|
2011-05-25 21:59:01 +00:00
|
|
|
* @this: NAND chip to create descriptor for
|
mtd: nand: more BB Detection refactoring and dynamic scan options
This is a revision to PATCH 2/2 that I sent. Link:
http://lists.infradead.org/pipermail/linux-mtd/2010-July/030911.html
Added new flag for scanning of both bytes 1 and 6 of the OOB for
a BB marker (instead of simply one or the other).
The "check_pattern" and "check_short_pattern" functions were updated
to include support for scanning the two different locations in the OOB.
In order to handle increases in variety of necessary scanning patterns,
I implemented dynamic memory allocation of nand_bbt_descr structs
in new function 'nand_create_default_bbt_descr()'. This replaces
some increasingly-unwieldy, statically-declared descriptors. It can
replace several more (e.g. "flashbased" structs). However, I do not
test the flashbased options personally.
How this was tested:
I referenced 30+ data sheets (covering 100+ parts), and I tested a
selection of 10 different chips to varying degrees. Particularly, I
tested the creation of bad-block descriptors and basic BB scanning on
three parts:
ST NAND04GW3B2D, 2K page
ST NAND128W3A, 512B page
Samsung K9F1G08U0A, 2K page
To test these, I wrote some fake bad block markers to the flash (in OOB
bytes 1, 6, and elsewhere) to see if the scanning routine would detect
them properly. However, this method was somewhat limited because the
driver I am using has some bugs in its OOB write functionality.
Signed-off-by: Brian Norris <norris@broadcom.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2010-07-15 19:15:44 +00:00
|
|
|
*
|
|
|
|
* This function allocates and initializes a nand_bbt_descr for BBM detection
|
2011-09-21 01:36:42 +00:00
|
|
|
* based on the properties of @this. The new descriptor is stored in
|
mtd: nand: more BB Detection refactoring and dynamic scan options
This is a revision to PATCH 2/2 that I sent. Link:
http://lists.infradead.org/pipermail/linux-mtd/2010-July/030911.html
Added new flag for scanning of both bytes 1 and 6 of the OOB for
a BB marker (instead of simply one or the other).
The "check_pattern" and "check_short_pattern" functions were updated
to include support for scanning the two different locations in the OOB.
In order to handle increases in variety of necessary scanning patterns,
I implemented dynamic memory allocation of nand_bbt_descr structs
in new function 'nand_create_default_bbt_descr()'. This replaces
some increasingly-unwieldy, statically-declared descriptors. It can
replace several more (e.g. "flashbased" structs). However, I do not
test the flashbased options personally.
How this was tested:
I referenced 30+ data sheets (covering 100+ parts), and I tested a
selection of 10 different chips to varying degrees. Particularly, I
tested the creation of bad-block descriptors and basic BB scanning on
three parts:
ST NAND04GW3B2D, 2K page
ST NAND128W3A, 512B page
Samsung K9F1G08U0A, 2K page
To test these, I wrote some fake bad block markers to the flash (in OOB
bytes 1, 6, and elsewhere) to see if the scanning routine would detect
them properly. However, this method was somewhat limited because the
driver I am using has some bugs in its OOB write functionality.
Signed-off-by: Brian Norris <norris@broadcom.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2010-07-15 19:15:44 +00:00
|
|
|
* this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
|
|
|
|
* passed to this function.
|
|
|
|
*/
|
2011-09-21 01:36:42 +00:00
|
|
|
static int nand_create_badblock_pattern(struct nand_chip *this)
|
mtd: nand: more BB Detection refactoring and dynamic scan options
This is a revision to PATCH 2/2 that I sent. Link:
http://lists.infradead.org/pipermail/linux-mtd/2010-July/030911.html
Added new flag for scanning of both bytes 1 and 6 of the OOB for
a BB marker (instead of simply one or the other).
The "check_pattern" and "check_short_pattern" functions were updated
to include support for scanning the two different locations in the OOB.
In order to handle increases in variety of necessary scanning patterns,
I implemented dynamic memory allocation of nand_bbt_descr structs
in new function 'nand_create_default_bbt_descr()'. This replaces
some increasingly-unwieldy, statically-declared descriptors. It can
replace several more (e.g. "flashbased" structs). However, I do not
test the flashbased options personally.
How this was tested:
I referenced 30+ data sheets (covering 100+ parts), and I tested a
selection of 10 different chips to varying degrees. Particularly, I
tested the creation of bad-block descriptors and basic BB scanning on
three parts:
ST NAND04GW3B2D, 2K page
ST NAND128W3A, 512B page
Samsung K9F1G08U0A, 2K page
To test these, I wrote some fake bad block markers to the flash (in OOB
bytes 1, 6, and elsewhere) to see if the scanning routine would detect
them properly. However, this method was somewhat limited because the
driver I am using has some bugs in its OOB write functionality.
Signed-off-by: Brian Norris <norris@broadcom.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2010-07-15 19:15:44 +00:00
|
|
|
{
|
|
|
|
struct nand_bbt_descr *bd;
|
|
|
|
if (this->badblock_pattern) {
|
2011-09-21 01:36:42 +00:00
|
|
|
pr_warn("Bad block pattern already allocated; not replacing\n");
|
mtd: nand: more BB Detection refactoring and dynamic scan options
This is a revision to PATCH 2/2 that I sent. Link:
http://lists.infradead.org/pipermail/linux-mtd/2010-July/030911.html
Added new flag for scanning of both bytes 1 and 6 of the OOB for
a BB marker (instead of simply one or the other).
The "check_pattern" and "check_short_pattern" functions were updated
to include support for scanning the two different locations in the OOB.
In order to handle increases in variety of necessary scanning patterns,
I implemented dynamic memory allocation of nand_bbt_descr structs
in new function 'nand_create_default_bbt_descr()'. This replaces
some increasingly-unwieldy, statically-declared descriptors. It can
replace several more (e.g. "flashbased" structs). However, I do not
test the flashbased options personally.
How this was tested:
I referenced 30+ data sheets (covering 100+ parts), and I tested a
selection of 10 different chips to varying degrees. Particularly, I
tested the creation of bad-block descriptors and basic BB scanning on
three parts:
ST NAND04GW3B2D, 2K page
ST NAND128W3A, 512B page
Samsung K9F1G08U0A, 2K page
To test these, I wrote some fake bad block markers to the flash (in OOB
bytes 1, 6, and elsewhere) to see if the scanning routine would detect
them properly. However, this method was somewhat limited because the
driver I am using has some bugs in its OOB write functionality.
Signed-off-by: Brian Norris <norris@broadcom.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2010-07-15 19:15:44 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
bd = kzalloc(sizeof(*bd), GFP_KERNEL);
|
2011-06-07 23:01:54 +00:00
|
|
|
if (!bd)
|
mtd: nand: more BB Detection refactoring and dynamic scan options
This is a revision to PATCH 2/2 that I sent. Link:
http://lists.infradead.org/pipermail/linux-mtd/2010-July/030911.html
Added new flag for scanning of both bytes 1 and 6 of the OOB for
a BB marker (instead of simply one or the other).
The "check_pattern" and "check_short_pattern" functions were updated
to include support for scanning the two different locations in the OOB.
In order to handle increases in variety of necessary scanning patterns,
I implemented dynamic memory allocation of nand_bbt_descr structs
in new function 'nand_create_default_bbt_descr()'. This replaces
some increasingly-unwieldy, statically-declared descriptors. It can
replace several more (e.g. "flashbased" structs). However, I do not
test the flashbased options personally.
How this was tested:
I referenced 30+ data sheets (covering 100+ parts), and I tested a
selection of 10 different chips to varying degrees. Particularly, I
tested the creation of bad-block descriptors and basic BB scanning on
three parts:
ST NAND04GW3B2D, 2K page
ST NAND128W3A, 512B page
Samsung K9F1G08U0A, 2K page
To test these, I wrote some fake bad block markers to the flash (in OOB
bytes 1, 6, and elsewhere) to see if the scanning routine would detect
them properly. However, this method was somewhat limited because the
driver I am using has some bugs in its OOB write functionality.
Signed-off-by: Brian Norris <norris@broadcom.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2010-07-15 19:15:44 +00:00
|
|
|
return -ENOMEM;
|
2011-09-21 01:36:42 +00:00
|
|
|
bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
|
mtd: nand: more BB Detection refactoring and dynamic scan options
This is a revision to PATCH 2/2 that I sent. Link:
http://lists.infradead.org/pipermail/linux-mtd/2010-July/030911.html
Added new flag for scanning of both bytes 1 and 6 of the OOB for
a BB marker (instead of simply one or the other).
The "check_pattern" and "check_short_pattern" functions were updated
to include support for scanning the two different locations in the OOB.
In order to handle increases in variety of necessary scanning patterns,
I implemented dynamic memory allocation of nand_bbt_descr structs
in new function 'nand_create_default_bbt_descr()'. This replaces
some increasingly-unwieldy, statically-declared descriptors. It can
replace several more (e.g. "flashbased" structs). However, I do not
test the flashbased options personally.
How this was tested:
I referenced 30+ data sheets (covering 100+ parts), and I tested a
selection of 10 different chips to varying degrees. Particularly, I
tested the creation of bad-block descriptors and basic BB scanning on
three parts:
ST NAND04GW3B2D, 2K page
ST NAND128W3A, 512B page
Samsung K9F1G08U0A, 2K page
To test these, I wrote some fake bad block markers to the flash (in OOB
bytes 1, 6, and elsewhere) to see if the scanning routine would detect
them properly. However, this method was somewhat limited because the
driver I am using has some bugs in its OOB write functionality.
Signed-off-by: Brian Norris <norris@broadcom.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2010-07-15 19:15:44 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
2005-11-07 11:15:49 +00:00
|
|
|
* nand_default_bbt - [NAND Interface] Select a default bad block table for the device
|
2011-05-25 21:59:01 +00:00
|
|
|
* @mtd: MTD device structure
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2011-05-25 21:59:01 +00:00
|
|
|
* This function selects the default bad block table support for the device and
|
|
|
|
* calls the nand_scan_bbt function.
|
|
|
|
*/
|
2006-05-13 17:07:53 +00:00
|
|
|
int nand_default_bbt(struct mtd_info *mtd)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct nand_chip *this = mtd->priv;
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2011-05-25 21:59:01 +00:00
|
|
|
/* Is a flash based bad block table requested? */
|
2011-05-31 23:31:23 +00:00
|
|
|
if (this->bbt_options & NAND_BBT_USE_FLASH) {
|
2005-11-07 11:15:49 +00:00
|
|
|
/* Use the default pattern descriptors */
|
|
|
|
if (!this->bbt_td) {
|
2011-05-31 23:31:22 +00:00
|
|
|
if (this->bbt_options & NAND_BBT_NO_OOB) {
|
2012-06-22 23:35:40 +00:00
|
|
|
this->bbt_td = &bbt_main_no_oob_descr;
|
|
|
|
this->bbt_md = &bbt_mirror_no_oob_descr;
|
2010-09-30 19:28:01 +00:00
|
|
|
} else {
|
|
|
|
this->bbt_td = &bbt_main_descr;
|
|
|
|
this->bbt_md = &bbt_mirror_descr;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this->bbt_td = NULL;
|
|
|
|
this->bbt_md = NULL;
|
|
|
|
}
|
2011-03-19 04:53:42 +00:00
|
|
|
|
|
|
|
if (!this->badblock_pattern)
|
2011-09-21 01:36:42 +00:00
|
|
|
nand_create_badblock_pattern(this);
|
2011-03-19 04:53:42 +00:00
|
|
|
|
2006-05-13 17:07:53 +00:00
|
|
|
return nand_scan_bbt(mtd, this->badblock_pattern);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-11-07 11:15:49 +00:00
|
|
|
* nand_isbad_bbt - [NAND Interface] Check if a block is bad
|
2011-05-25 21:59:01 +00:00
|
|
|
* @mtd: MTD device structure
|
|
|
|
* @offs: offset in the device
|
|
|
|
* @allowbbt: allow access to bad block table region
|
|
|
|
*/
|
2006-05-13 17:07:53 +00:00
|
|
|
int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct nand_chip *this = mtd->priv;
|
2013-07-31 00:52:57 +00:00
|
|
|
int block, res;
|
2005-11-07 11:15:49 +00:00
|
|
|
|
2013-07-31 00:52:56 +00:00
|
|
|
block = (int)(offs >> this->bbt_erase_shift);
|
|
|
|
res = bbt_get_entry(this, block);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-07-19 17:06:09 +00:00
|
|
|
pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: "
|
|
|
|
"(block %d) 0x%02x\n",
|
2013-07-31 00:52:56 +00:00
|
|
|
(unsigned int)offs, block, res);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-07-31 00:52:57 +00:00
|
|
|
switch (res) {
|
2013-07-31 00:52:55 +00:00
|
|
|
case BBT_BLOCK_GOOD:
|
2006-05-13 17:07:53 +00:00
|
|
|
return 0;
|
2013-07-31 00:52:55 +00:00
|
|
|
case BBT_BLOCK_WORN:
|
2006-05-13 17:07:53 +00:00
|
|
|
return 1;
|
2013-07-31 00:52:55 +00:00
|
|
|
case BBT_BLOCK_RESERVED:
|
2006-05-13 17:07:53 +00:00
|
|
|
return allowbbt ? 0 : 1;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-31 00:52:59 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
struct nand_chip *this = mtd->priv;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-05-13 17:07:53 +00:00
|
|
|
EXPORT_SYMBOL(nand_scan_bbt);
|