Skip to content

Commit 58373ff

Browse files
Brian NorrisDavid Woodhouse
authored andcommitted
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>
1 parent c7b28e2 commit 58373ff

3 files changed

Lines changed: 90 additions & 55 deletions

File tree

‎drivers/mtd/nand/nand_base.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2963,6 +2963,15 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
29632963
*maf_id == NAND_MFR_MICRON))
29642964
chip->options |= NAND_BBT_SCAN2NDPAGE;
29652965

2966+
/*
2967+
* Numonyx/ST 2K pages, x8 bus use BOTH byte 1 and 6
2968+
*/
2969+
if (!(busw & NAND_BUSWIDTH_16) &&
2970+
*maf_id == NAND_MFR_STMICRO &&
2971+
mtd->writesize == 2048) {
2972+
chip->options |= NAND_BBT_SCANBYTE1AND6;
2973+
chip->badblockpos = 0;
2974+
}
29662975

29672976
/* Check for AND chips with 4 page planes */
29682977
if (chip->options & NAND_4PAGE_ARRAY)
@@ -3322,6 +3331,11 @@ void nand_release(struct mtd_info *mtd)
33223331
kfree(chip->bbt);
33233332
if (!(chip->options & NAND_OWN_BUFFERS))
33243333
kfree(chip->buffers);
3334+
3335+
/* Free bad block descriptor memory */
3336+
if (chip->badblock_pattern && chip->badblock_pattern->options
3337+
& NAND_BBT_DYNAMICSTRUCT)
3338+
kfree(chip->badblock_pattern);
33253339
}
33263340

33273341
EXPORT_SYMBOL_GPL(nand_lock);

‎drivers/mtd/nand/nand_bbt.c‎

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,28 @@ static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_desc
9393
return -1;
9494
}
9595

96+
/* Check both positions 1 and 6 for pattern? */
97+
if (td->options & NAND_BBT_SCANBYTE1AND6) {
98+
if (td->options & NAND_BBT_SCANEMPTY) {
99+
p += td->len;
100+
end += NAND_SMALL_BADBLOCK_POS - td->offs;
101+
/* Check region between positions 1 and 6 */
102+
for (i = 0; i < NAND_SMALL_BADBLOCK_POS - td->offs - td->len;
103+
i++) {
104+
if (*p++ != 0xff)
105+
return -1;
106+
}
107+
}
108+
else {
109+
p += NAND_SMALL_BADBLOCK_POS - td->offs;
110+
}
111+
/* Compare the pattern */
112+
for (i = 0; i < td->len; i++) {
113+
if (p[i] != td->pattern[i])
114+
return -1;
115+
}
116+
}
117+
96118
if (td->options & NAND_BBT_SCANEMPTY) {
97119
p += td->len;
98120
end += td->len;
@@ -124,6 +146,13 @@ static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
124146
if (p[td->offs + i] != td->pattern[i])
125147
return -1;
126148
}
149+
/* Need to check location 1 AND 6? */
150+
if (td->options & NAND_BBT_SCANBYTE1AND6) {
151+
for (i = 0; i < td->len; i++) {
152+
if (p[NAND_SMALL_BADBLOCK_POS + i] != td->pattern[i])
153+
return -1;
154+
}
155+
}
127156
return 0;
128157
}
129158

@@ -397,12 +426,10 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
397426

398427
if (bd->options & NAND_BBT_SCANALLPAGES)
399428
len = 1 << (this->bbt_erase_shift - this->page_shift);
400-
else {
401-
if (bd->options & NAND_BBT_SCAN2NDPAGE)
402-
len = 2;
403-
else
404-
len = 1;
405-
}
429+
else if (bd->options & NAND_BBT_SCAN2NDPAGE)
430+
len = 2;
431+
else
432+
len = 1;
406433

407434
if (!(bd->options & NAND_BBT_SCANEMPTY)) {
408435
/* We need only read few bytes from the OOB area */
@@ -1092,41 +1119,6 @@ int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
10921119
* while scanning a device for factory marked good / bad blocks. */
10931120
static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
10941121

1095-
static struct nand_bbt_descr smallpage_memorybased = {
1096-
.options = 0,
1097-
.offs = NAND_SMALL_BADBLOCK_POS,
1098-
.len = 1,
1099-
.pattern = scan_ff_pattern
1100-
};
1101-
1102-
static struct nand_bbt_descr smallpage_scan2nd_memorybased = {
1103-
.options = NAND_BBT_SCAN2NDPAGE,
1104-
.offs = NAND_SMALL_BADBLOCK_POS,
1105-
.len = 2,
1106-
.pattern = scan_ff_pattern
1107-
};
1108-
1109-
static struct nand_bbt_descr largepage_memorybased = {
1110-
.options = 0,
1111-
.offs = NAND_LARGE_BADBLOCK_POS,
1112-
.len = 1,
1113-
.pattern = scan_ff_pattern
1114-
};
1115-
1116-
static struct nand_bbt_descr largepage_scan2nd_memorybased = {
1117-
.options = NAND_BBT_SCAN2NDPAGE,
1118-
.offs = NAND_LARGE_BADBLOCK_POS,
1119-
.len = 2,
1120-
.pattern = scan_ff_pattern
1121-
};
1122-
1123-
static struct nand_bbt_descr lastpage_memorybased = {
1124-
.options = NAND_BBT_SCANLASTPAGE,
1125-
.offs = 0,
1126-
.len = 1,
1127-
.pattern = scan_ff_pattern
1128-
};
1129-
11301122
static struct nand_bbt_descr smallpage_flashbased = {
11311123
.options = NAND_BBT_SCAN2NDPAGE,
11321124
.offs = NAND_SMALL_BADBLOCK_POS,
@@ -1175,6 +1167,43 @@ static struct nand_bbt_descr bbt_mirror_descr = {
11751167
.pattern = mirror_pattern
11761168
};
11771169

1170+
#define BBT_SCAN_OPTIONS (NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE | \
1171+
NAND_BBT_SCANBYTE1AND6)
1172+
/**
1173+
* nand_create_default_bbt_descr - [Internal] Creates a BBT descriptor structure
1174+
* @this: NAND chip to create descriptor for
1175+
*
1176+
* This function allocates and initializes a nand_bbt_descr for BBM detection
1177+
* based on the properties of "this". The new descriptor is stored in
1178+
* this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1179+
* passed to this function.
1180+
*
1181+
* TODO: Handle other flags, replace other static structs
1182+
* (e.g. handle NAND_BBT_FLASH for flash-based BBT,
1183+
* replace smallpage_flashbased)
1184+
*
1185+
*/
1186+
static int nand_create_default_bbt_descr(struct nand_chip *this)
1187+
{
1188+
struct nand_bbt_descr *bd;
1189+
if (this->badblock_pattern) {
1190+
printk(KERN_WARNING "BBT descr already allocated; not replacing.\n");
1191+
return -EINVAL;
1192+
}
1193+
bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1194+
if (!bd) {
1195+
printk(KERN_ERR "nand_create_default_bbt_descr: Out of memory\n");
1196+
return -ENOMEM;
1197+
}
1198+
bd->options = this->options & BBT_SCAN_OPTIONS;
1199+
bd->offs = this->badblockpos;
1200+
bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1201+
bd->pattern = scan_ff_pattern;
1202+
bd->options |= NAND_BBT_DYNAMICSTRUCT;
1203+
this->badblock_pattern = bd;
1204+
return 0;
1205+
}
1206+
11781207
/**
11791208
* nand_default_bbt - [NAND Interface] Select a default bad block table for the device
11801209
* @mtd: MTD device structure
@@ -1217,20 +1246,8 @@ int nand_default_bbt(struct mtd_info *mtd)
12171246
} else {
12181247
this->bbt_td = NULL;
12191248
this->bbt_md = NULL;
1220-
if (!this->badblock_pattern) {
1221-
if (this->options & NAND_BBT_SCANLASTPAGE)
1222-
this->badblock_pattern = &lastpage_memorybased;
1223-
else if (this->options & NAND_BBT_SCAN2NDPAGE)
1224-
this->badblock_pattern = this->badblockpos ==
1225-
NAND_SMALL_BADBLOCK_POS ?
1226-
&smallpage_scan2nd_memorybased :
1227-
&largepage_scan2nd_memorybased;
1228-
else
1229-
this->badblock_pattern = this->badblockpos ==
1230-
NAND_SMALL_BADBLOCK_POS ?
1231-
&smallpage_memorybased :
1232-
&largepage_memorybased;
1233-
}
1249+
if (!this->badblock_pattern)
1250+
nand_create_default_bbt_descr(this);
12341251
}
12351252
return nand_scan_bbt(mtd, this->badblock_pattern);
12361253
}

‎include/linux/mtd/bbm.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ struct nand_bbt_descr {
8484
#define NAND_BBT_SCAN2NDPAGE 0x00004000
8585
/* Search good / bad pattern on the last page of the eraseblock */
8686
#define NAND_BBT_SCANLASTPAGE 0x00008000
87+
/* Chip stores bad block marker on BOTH 1st and 6th bytes of OOB */
88+
#define NAND_BBT_SCANBYTE1AND6 0x00100000
89+
/* The nand_bbt_descr was created dynamicaly and must be freed */
90+
#define NAND_BBT_DYNAMICSTRUCT 0x00200000
8791

8892
/* The maximum number of blocks to scan for a bbt */
8993
#define NAND_BBT_SCAN_MAXBLOCKS 4

0 commit comments

Comments
 (0)