aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/nand/nand_bbt.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mtd/nand/nand_bbt.c')
-rw-r--r--drivers/mtd/nand/nand_bbt.c502
1 files changed, 307 insertions, 195 deletions
diff --git a/drivers/mtd/nand/nand_bbt.c b/drivers/mtd/nand/nand_bbt.c
index ca286999fe08..a612c4ea8194 100644
--- a/drivers/mtd/nand/nand_bbt.c
+++ b/drivers/mtd/nand/nand_bbt.c
@@ -48,7 +48,7 @@
48 * 48 *
49 * Following assumptions are made: 49 * Following assumptions are made:
50 * - bbts start at a page boundary, if autolocated on a block boundary 50 * - bbts start at a page boundary, if autolocated on a block boundary
51 * - the space neccecary for a bbt in FLASH does not exceed a block boundary 51 * - the space necessary for a bbt in FLASH does not exceed a block boundary
52 * 52 *
53 */ 53 */
54 54
@@ -60,7 +60,7 @@
60#include <linux/mtd/compatmac.h> 60#include <linux/mtd/compatmac.h>
61#include <linux/bitops.h> 61#include <linux/bitops.h>
62#include <linux/delay.h> 62#include <linux/delay.h>
63 63#include <linux/vmalloc.h>
64 64
65/** 65/**
66 * check_pattern - [GENERIC] check if a pattern is in the buffer 66 * check_pattern - [GENERIC] check if a pattern is in the buffer
@@ -75,7 +75,7 @@
75 * pattern area contain 0xff 75 * pattern area contain 0xff
76 * 76 *
77*/ 77*/
78static int check_pattern (uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td) 78static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
79{ 79{
80 int i, end = 0; 80 int i, end = 0;
81 uint8_t *p = buf; 81 uint8_t *p = buf;
@@ -116,7 +116,7 @@ static int check_pattern (uint8_t *buf, int len, int paglen, struct nand_bbt_des
116 * no optional empty check 116 * no optional empty check
117 * 117 *
118*/ 118*/
119static int check_short_pattern (uint8_t *buf, struct nand_bbt_descr *td) 119static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
120{ 120{
121 int i; 121 int i;
122 uint8_t *p = buf; 122 uint8_t *p = buf;
@@ -142,8 +142,8 @@ static int check_short_pattern (uint8_t *buf, struct nand_bbt_descr *td)
142 * Read the bad block table starting from page. 142 * Read the bad block table starting from page.
143 * 143 *
144 */ 144 */
145static int read_bbt (struct mtd_info *mtd, uint8_t *buf, int page, int num, 145static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
146 int bits, int offs, int reserved_block_code) 146 int bits, int offs, int reserved_block_code)
147{ 147{
148 int res, i, j, act = 0; 148 int res, i, j, act = 0;
149 struct nand_chip *this = mtd->priv; 149 struct nand_chip *this = mtd->priv;
@@ -152,17 +152,17 @@ static int read_bbt (struct mtd_info *mtd, uint8_t *buf, int page, int num,
152 uint8_t msk = (uint8_t) ((1 << bits) - 1); 152 uint8_t msk = (uint8_t) ((1 << bits) - 1);
153 153
154 totlen = (num * bits) >> 3; 154 totlen = (num * bits) >> 3;
155 from = ((loff_t)page) << this->page_shift; 155 from = ((loff_t) page) << this->page_shift;
156 156
157 while (totlen) { 157 while (totlen) {
158 len = min (totlen, (size_t) (1 << this->bbt_erase_shift)); 158 len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
159 res = mtd->read_ecc (mtd, from, len, &retlen, buf, NULL, this->autooob); 159 res = mtd->read(mtd, from, len, &retlen, buf);
160 if (res < 0) { 160 if (res < 0) {
161 if (retlen != len) { 161 if (retlen != len) {
162 printk (KERN_INFO "nand_bbt: Error reading bad block table\n"); 162 printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
163 return res; 163 return res;
164 } 164 }
165 printk (KERN_WARNING "nand_bbt: ECC error while reading bad block table\n"); 165 printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
166 } 166 }
167 167
168 /* Analyse data */ 168 /* Analyse data */
@@ -172,22 +172,23 @@ static int read_bbt (struct mtd_info *mtd, uint8_t *buf, int page, int num,
172 uint8_t tmp = (dat >> j) & msk; 172 uint8_t tmp = (dat >> j) & msk;
173 if (tmp == msk) 173 if (tmp == msk)
174 continue; 174 continue;
175 if (reserved_block_code && 175 if (reserved_block_code && (tmp == reserved_block_code)) {
176 (tmp == reserved_block_code)) { 176 printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%08x\n",
177 printk (KERN_DEBUG "nand_read_bbt: Reserved block at 0x%08x\n", 177 ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
178 ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
179 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06); 178 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
179 mtd->ecc_stats.bbtblocks++;
180 continue; 180 continue;
181 } 181 }
182 /* Leave it for now, if its matured we can move this 182 /* Leave it for now, if its matured we can move this
183 * message to MTD_DEBUG_LEVEL0 */ 183 * message to MTD_DEBUG_LEVEL0 */
184 printk (KERN_DEBUG "nand_read_bbt: Bad block at 0x%08x\n", 184 printk(KERN_DEBUG "nand_read_bbt: Bad block at 0x%08x\n",
185 ((offs << 2) + (act >> 1)) << this->bbt_erase_shift); 185 ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
186 /* Factory marked bad or worn out ? */ 186 /* Factory marked bad or worn out ? */
187 if (tmp == 0) 187 if (tmp == 0)
188 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06); 188 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
189 else 189 else
190 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06); 190 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
191 mtd->ecc_stats.badblocks++;
191 } 192 }
192 } 193 }
193 totlen -= len; 194 totlen -= len;
@@ -207,7 +208,7 @@ static int read_bbt (struct mtd_info *mtd, uint8_t *buf, int page, int num,
207 * Read the bad block table for all chips starting at a given page 208 * Read the bad block table for all chips starting at a given page
208 * We assume that the bbt bits are in consecutive order. 209 * We assume that the bbt bits are in consecutive order.
209*/ 210*/
210static int read_abs_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip) 211static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
211{ 212{
212 struct nand_chip *this = mtd->priv; 213 struct nand_chip *this = mtd->priv;
213 int res = 0, i; 214 int res = 0, i;
@@ -231,6 +232,42 @@ static int read_abs_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_des
231 return 0; 232 return 0;
232} 233}
233 234
235/*
236 * Scan read raw data from flash
237 */
238static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
239 size_t len)
240{
241 struct mtd_oob_ops ops;
242
243 ops.mode = MTD_OOB_RAW;
244 ops.ooboffs = 0;
245 ops.ooblen = mtd->oobsize;
246 ops.oobbuf = buf;
247 ops.datbuf = buf;
248 ops.len = len;
249
250 return mtd->read_oob(mtd, offs, &ops);
251}
252
253/*
254 * Scan write data with oob to flash
255 */
256static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
257 uint8_t *buf, uint8_t *oob)
258{
259 struct mtd_oob_ops ops;
260
261 ops.mode = MTD_OOB_PLACE;
262 ops.ooboffs = 0;
263 ops.ooblen = mtd->oobsize;
264 ops.datbuf = buf;
265 ops.oobbuf = oob;
266 ops.len = len;
267
268 return mtd->write_oob(mtd, offs, &ops);
269}
270
234/** 271/**
235 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page 272 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
236 * @mtd: MTD device structure 273 * @mtd: MTD device structure
@@ -242,28 +279,85 @@ static int read_abs_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_des
242 * We assume that the bbt bits are in consecutive order. 279 * We assume that the bbt bits are in consecutive order.
243 * 280 *
244*/ 281*/
245static int read_abs_bbts (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, 282static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
246 struct nand_bbt_descr *md) 283 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
247{ 284{
248 struct nand_chip *this = mtd->priv; 285 struct nand_chip *this = mtd->priv;
249 286
250 /* Read the primary version, if available */ 287 /* Read the primary version, if available */
251 if (td->options & NAND_BBT_VERSION) { 288 if (td->options & NAND_BBT_VERSION) {
252 nand_read_raw (mtd, buf, td->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize); 289 scan_read_raw(mtd, buf, td->pages[0] << this->page_shift,
253 td->version[0] = buf[mtd->oobblock + td->veroffs]; 290 mtd->writesize);
254 printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", td->pages[0], td->version[0]); 291 td->version[0] = buf[mtd->writesize + td->veroffs];
292 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
293 td->pages[0], td->version[0]);
255 } 294 }
256 295
257 /* Read the mirror version, if available */ 296 /* Read the mirror version, if available */
258 if (md && (md->options & NAND_BBT_VERSION)) { 297 if (md && (md->options & NAND_BBT_VERSION)) {
259 nand_read_raw (mtd, buf, md->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize); 298 scan_read_raw(mtd, buf, md->pages[0] << this->page_shift,
260 md->version[0] = buf[mtd->oobblock + md->veroffs]; 299 mtd->writesize);
261 printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", md->pages[0], md->version[0]); 300 md->version[0] = buf[mtd->writesize + md->veroffs];
301 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
302 md->pages[0], md->version[0]);
262 } 303 }
263
264 return 1; 304 return 1;
265} 305}
266 306
307/*
308 * Scan a given block full
309 */
310static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
311 loff_t offs, uint8_t *buf, size_t readlen,
312 int scanlen, int len)
313{
314 int ret, j;
315
316 ret = scan_read_raw(mtd, buf, offs, readlen);
317 if (ret)
318 return ret;
319
320 for (j = 0; j < len; j++, buf += scanlen) {
321 if (check_pattern(buf, scanlen, mtd->writesize, bd))
322 return 1;
323 }
324 return 0;
325}
326
327/*
328 * Scan a given block partially
329 */
330static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
331 loff_t offs, uint8_t *buf, int len)
332{
333 struct mtd_oob_ops ops;
334 int j, ret;
335
336 ops.len = mtd->oobsize;
337 ops.ooblen = mtd->oobsize;
338 ops.oobbuf = buf;
339 ops.ooboffs = 0;
340 ops.datbuf = NULL;
341 ops.mode = MTD_OOB_PLACE;
342
343 for (j = 0; j < len; j++) {
344 /*
345 * Read the full oob until read_oob is fixed to
346 * handle single byte reads for 16 bit
347 * buswidth
348 */
349 ret = mtd->read_oob(mtd, offs, &ops);
350 if (ret)
351 return ret;
352
353 if (check_short_pattern(buf, bd))
354 return 1;
355
356 offs += mtd->writesize;
357 }
358 return 0;
359}
360
267/** 361/**
268 * create_bbt - [GENERIC] Create a bad block table by scanning the device 362 * create_bbt - [GENERIC] Create a bad block table by scanning the device
269 * @mtd: MTD device structure 363 * @mtd: MTD device structure
@@ -275,15 +369,16 @@ static int read_abs_bbts (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_de
275 * Create a bad block table by scanning the device 369 * Create a bad block table by scanning the device
276 * for the given good/bad block identify pattern 370 * for the given good/bad block identify pattern
277 */ 371 */
278static int create_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip) 372static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
373 struct nand_bbt_descr *bd, int chip)
279{ 374{
280 struct nand_chip *this = mtd->priv; 375 struct nand_chip *this = mtd->priv;
281 int i, j, numblocks, len, scanlen; 376 int i, numblocks, len, scanlen;
282 int startblock; 377 int startblock;
283 loff_t from; 378 loff_t from;
284 size_t readlen, ooblen; 379 size_t readlen;
285 380
286 printk (KERN_INFO "Scanning device for bad blocks\n"); 381 printk(KERN_INFO "Scanning device for bad blocks\n");
287 382
288 if (bd->options & NAND_BBT_SCANALLPAGES) 383 if (bd->options & NAND_BBT_SCANALLPAGES)
289 len = 1 << (this->bbt_erase_shift - this->page_shift); 384 len = 1 << (this->bbt_erase_shift - this->page_shift);
@@ -296,25 +391,24 @@ static int create_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
296 391
297 if (!(bd->options & NAND_BBT_SCANEMPTY)) { 392 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
298 /* We need only read few bytes from the OOB area */ 393 /* We need only read few bytes from the OOB area */
299 scanlen = ooblen = 0; 394 scanlen = 0;
300 readlen = bd->len; 395 readlen = bd->len;
301 } else { 396 } else {
302 /* Full page content should be read */ 397 /* Full page content should be read */
303 scanlen = mtd->oobblock + mtd->oobsize; 398 scanlen = mtd->writesize + mtd->oobsize;
304 readlen = len * mtd->oobblock; 399 readlen = len * mtd->writesize;
305 ooblen = len * mtd->oobsize;
306 } 400 }
307 401
308 if (chip == -1) { 402 if (chip == -1) {
309 /* Note that numblocks is 2 * (real numblocks) here, see i+=2 below as it 403 /* Note that numblocks is 2 * (real numblocks) here, see i+=2
310 * makes shifting and masking less painful */ 404 * below as it makes shifting and masking less painful */
311 numblocks = mtd->size >> (this->bbt_erase_shift - 1); 405 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
312 startblock = 0; 406 startblock = 0;
313 from = 0; 407 from = 0;
314 } else { 408 } else {
315 if (chip >= this->numchips) { 409 if (chip >= this->numchips) {
316 printk (KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n", 410 printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
317 chip + 1, this->numchips); 411 chip + 1, this->numchips);
318 return -EINVAL; 412 return -EINVAL;
319 } 413 }
320 numblocks = this->chipsize >> (this->bbt_erase_shift - 1); 414 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
@@ -326,36 +420,22 @@ static int create_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
326 for (i = startblock; i < numblocks;) { 420 for (i = startblock; i < numblocks;) {
327 int ret; 421 int ret;
328 422
329 if (bd->options & NAND_BBT_SCANEMPTY) 423 if (bd->options & NAND_BBT_SCANALLPAGES)
330 if ((ret = nand_read_raw (mtd, buf, from, readlen, ooblen))) 424 ret = scan_block_full(mtd, bd, from, buf, readlen,
331 return ret; 425 scanlen, len);
332 426 else
333 for (j = 0; j < len; j++) { 427 ret = scan_block_fast(mtd, bd, from, buf, len);
334 if (!(bd->options & NAND_BBT_SCANEMPTY)) { 428
335 size_t retlen; 429 if (ret < 0)
336 430 return ret;
337 /* Read the full oob until read_oob is fixed to 431
338 * handle single byte reads for 16 bit buswidth */ 432 if (ret) {
339 ret = mtd->read_oob(mtd, from + j * mtd->oobblock, 433 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
340 mtd->oobsize, &retlen, buf); 434 printk(KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
341 if (ret) 435 i >> 1, (unsigned int)from);
342 return ret; 436 mtd->ecc_stats.badblocks++;
343
344 if (check_short_pattern (buf, bd)) {
345 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
346 printk (KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
347 i >> 1, (unsigned int) from);
348 break;
349 }
350 } else {
351 if (check_pattern (&buf[j * scanlen], scanlen, mtd->oobblock, bd)) {
352 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
353 printk (KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
354 i >> 1, (unsigned int) from);
355 break;
356 }
357 }
358 } 437 }
438
359 i += 2; 439 i += 2;
360 from += (1 << this->bbt_erase_shift); 440 from += (1 << this->bbt_erase_shift);
361 } 441 }
@@ -374,22 +454,23 @@ static int create_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
374 * block. 454 * block.
375 * If the option NAND_BBT_PERCHIP is given, each chip is searched 455 * If the option NAND_BBT_PERCHIP is given, each chip is searched
376 * for a bbt, which contains the bad block information of this chip. 456 * for a bbt, which contains the bad block information of this chip.
377 * This is neccecary to provide support for certain DOC devices. 457 * This is necessary to provide support for certain DOC devices.
378 * 458 *
379 * The bbt ident pattern resides in the oob area of the first page 459 * The bbt ident pattern resides in the oob area of the first page
380 * in a block. 460 * in a block.
381 */ 461 */
382static int search_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td) 462static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
383{ 463{
384 struct nand_chip *this = mtd->priv; 464 struct nand_chip *this = mtd->priv;
385 int i, chips; 465 int i, chips;
386 int bits, startblock, block, dir; 466 int bits, startblock, block, dir;
387 int scanlen = mtd->oobblock + mtd->oobsize; 467 int scanlen = mtd->writesize + mtd->oobsize;
388 int bbtblocks; 468 int bbtblocks;
469 int blocktopage = this->bbt_erase_shift - this->page_shift;
389 470
390 /* Search direction top -> down ? */ 471 /* Search direction top -> down ? */
391 if (td->options & NAND_BBT_LASTBLOCK) { 472 if (td->options & NAND_BBT_LASTBLOCK) {
392 startblock = (mtd->size >> this->bbt_erase_shift) -1; 473 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
393 dir = -1; 474 dir = -1;
394 } else { 475 } else {
395 startblock = 0; 476 startblock = 0;
@@ -415,13 +496,16 @@ static int search_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
415 td->pages[i] = -1; 496 td->pages[i] = -1;
416 /* Scan the maximum number of blocks */ 497 /* Scan the maximum number of blocks */
417 for (block = 0; block < td->maxblocks; block++) { 498 for (block = 0; block < td->maxblocks; block++) {
499
418 int actblock = startblock + dir * block; 500 int actblock = startblock + dir * block;
501 loff_t offs = actblock << this->bbt_erase_shift;
502
419 /* Read first page */ 503 /* Read first page */
420 nand_read_raw (mtd, buf, actblock << this->bbt_erase_shift, mtd->oobblock, mtd->oobsize); 504 scan_read_raw(mtd, buf, offs, mtd->writesize);
421 if (!check_pattern(buf, scanlen, mtd->oobblock, td)) { 505 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
422 td->pages[i] = actblock << (this->bbt_erase_shift - this->page_shift); 506 td->pages[i] = actblock << blocktopage;
423 if (td->options & NAND_BBT_VERSION) { 507 if (td->options & NAND_BBT_VERSION) {
424 td->version[i] = buf[mtd->oobblock + td->veroffs]; 508 td->version[i] = buf[mtd->writesize + td->veroffs];
425 } 509 }
426 break; 510 break;
427 } 511 }
@@ -431,9 +515,10 @@ static int search_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
431 /* Check, if we found a bbt for each requested chip */ 515 /* Check, if we found a bbt for each requested chip */
432 for (i = 0; i < chips; i++) { 516 for (i = 0; i < chips; i++) {
433 if (td->pages[i] == -1) 517 if (td->pages[i] == -1)
434 printk (KERN_WARNING "Bad block table not found for chip %d\n", i); 518 printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
435 else 519 else
436 printk (KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i], td->version[i]); 520 printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i],
521 td->version[i]);
437 } 522 }
438 return 0; 523 return 0;
439} 524}
@@ -447,21 +532,19 @@ static int search_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr
447 * 532 *
448 * Search and read the bad block table(s) 533 * Search and read the bad block table(s)
449*/ 534*/
450static int search_read_bbts (struct mtd_info *mtd, uint8_t *buf, 535static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
451 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
452{ 536{
453 /* Search the primary table */ 537 /* Search the primary table */
454 search_bbt (mtd, buf, td); 538 search_bbt(mtd, buf, td);
455 539
456 /* Search the mirror table */ 540 /* Search the mirror table */
457 if (md) 541 if (md)
458 search_bbt (mtd, buf, md); 542 search_bbt(mtd, buf, md);
459 543
460 /* Force result check */ 544 /* Force result check */
461 return 1; 545 return 1;
462} 546}
463 547
464
465/** 548/**
466 * write_bbt - [GENERIC] (Re)write the bad block table 549 * write_bbt - [GENERIC] (Re)write the bad block table
467 * 550 *
@@ -474,25 +557,31 @@ static int search_read_bbts (struct mtd_info *mtd, uint8_t *buf,
474 * (Re)write the bad block table 557 * (Re)write the bad block table
475 * 558 *
476*/ 559*/
477static int write_bbt (struct mtd_info *mtd, uint8_t *buf, 560static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
478 struct nand_bbt_descr *td, struct nand_bbt_descr *md, int chipsel) 561 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
562 int chipsel)
479{ 563{
480 struct nand_chip *this = mtd->priv; 564 struct nand_chip *this = mtd->priv;
481 struct nand_oobinfo oobinfo;
482 struct erase_info einfo; 565 struct erase_info einfo;
483 int i, j, res, chip = 0; 566 int i, j, res, chip = 0;
484 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk; 567 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
485 int nrchips, bbtoffs, pageoffs; 568 int nrchips, bbtoffs, pageoffs, ooboffs;
486 uint8_t msk[4]; 569 uint8_t msk[4];
487 uint8_t rcode = td->reserved_block_code; 570 uint8_t rcode = td->reserved_block_code;
488 size_t retlen, len = 0; 571 size_t retlen, len = 0;
489 loff_t to; 572 loff_t to;
573 struct mtd_oob_ops ops;
574
575 ops.ooblen = mtd->oobsize;
576 ops.ooboffs = 0;
577 ops.datbuf = NULL;
578 ops.mode = MTD_OOB_PLACE;
490 579
491 if (!rcode) 580 if (!rcode)
492 rcode = 0xff; 581 rcode = 0xff;
493 /* Write bad block table per chip rather than per device ? */ 582 /* Write bad block table per chip rather than per device ? */
494 if (td->options & NAND_BBT_PERCHIP) { 583 if (td->options & NAND_BBT_PERCHIP) {
495 numblocks = (int) (this->chipsize >> this->bbt_erase_shift); 584 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
496 /* Full device write or specific chip ? */ 585 /* Full device write or specific chip ? */
497 if (chipsel == -1) { 586 if (chipsel == -1) {
498 nrchips = this->numchips; 587 nrchips = this->numchips;
@@ -501,7 +590,7 @@ static int write_bbt (struct mtd_info *mtd, uint8_t *buf,
501 chip = chipsel; 590 chip = chipsel;
502 } 591 }
503 } else { 592 } else {
504 numblocks = (int) (mtd->size >> this->bbt_erase_shift); 593 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
505 nrchips = 1; 594 nrchips = 1;
506 } 595 }
507 596
@@ -530,27 +619,38 @@ static int write_bbt (struct mtd_info *mtd, uint8_t *buf,
530 for (i = 0; i < td->maxblocks; i++) { 619 for (i = 0; i < td->maxblocks; i++) {
531 int block = startblock + dir * i; 620 int block = startblock + dir * i;
532 /* Check, if the block is bad */ 621 /* Check, if the block is bad */
533 switch ((this->bbt[block >> 2] >> (2 * (block & 0x03))) & 0x03) { 622 switch ((this->bbt[block >> 2] >>
623 (2 * (block & 0x03))) & 0x03) {
534 case 0x01: 624 case 0x01:
535 case 0x03: 625 case 0x03:
536 continue; 626 continue;
537 } 627 }
538 page = block << (this->bbt_erase_shift - this->page_shift); 628 page = block <<
629 (this->bbt_erase_shift - this->page_shift);
539 /* Check, if the block is used by the mirror table */ 630 /* Check, if the block is used by the mirror table */
540 if (!md || md->pages[chip] != page) 631 if (!md || md->pages[chip] != page)
541 goto write; 632 goto write;
542 } 633 }
543 printk (KERN_ERR "No space left to write bad block table\n"); 634 printk(KERN_ERR "No space left to write bad block table\n");
544 return -ENOSPC; 635 return -ENOSPC;
545write: 636 write:
546 637
547 /* Set up shift count and masks for the flash table */ 638 /* Set up shift count and masks for the flash table */
548 bits = td->options & NAND_BBT_NRBITS_MSK; 639 bits = td->options & NAND_BBT_NRBITS_MSK;
640 msk[2] = ~rcode;
549 switch (bits) { 641 switch (bits) {
550 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x01; break; 642 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
551 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x03; break; 643 msk[3] = 0x01;
552 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C; msk[2] = ~rcode; msk[3] = 0x0f; break; 644 break;
553 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F; msk[2] = ~rcode; msk[3] = 0xff; break; 645 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
646 msk[3] = 0x03;
647 break;
648 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
649 msk[3] = 0x0f;
650 break;
651 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
652 msk[3] = 0xff;
653 break;
554 default: return -EINVAL; 654 default: return -EINVAL;
555 } 655 }
556 656
@@ -558,82 +658,92 @@ write:
558 658
559 to = ((loff_t) page) << this->page_shift; 659 to = ((loff_t) page) << this->page_shift;
560 660
561 memcpy (&oobinfo, this->autooob, sizeof(oobinfo));
562 oobinfo.useecc = MTD_NANDECC_PLACEONLY;
563
564 /* Must we save the block contents ? */ 661 /* Must we save the block contents ? */
565 if (td->options & NAND_BBT_SAVECONTENT) { 662 if (td->options & NAND_BBT_SAVECONTENT) {
566 /* Make it block aligned */ 663 /* Make it block aligned */
567 to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1)); 664 to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
568 len = 1 << this->bbt_erase_shift; 665 len = 1 << this->bbt_erase_shift;
569 res = mtd->read_ecc (mtd, to, len, &retlen, buf, &buf[len], &oobinfo); 666 res = mtd->read(mtd, to, len, &retlen, buf);
570 if (res < 0) { 667 if (res < 0) {
571 if (retlen != len) { 668 if (retlen != len) {
572 printk (KERN_INFO "nand_bbt: Error reading block for writing the bad block table\n"); 669 printk(KERN_INFO "nand_bbt: Error "
670 "reading block for writing "
671 "the bad block table\n");
573 return res; 672 return res;
574 } 673 }
575 printk (KERN_WARNING "nand_bbt: ECC error while reading block for writing bad block table\n"); 674 printk(KERN_WARNING "nand_bbt: ECC error "
675 "while reading block for writing "
676 "bad block table\n");
576 } 677 }
678 /* Read oob data */
679 ops.len = (len >> this->page_shift) * mtd->oobsize;
680 ops.oobbuf = &buf[len];
681 res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
682 if (res < 0 || ops.retlen != ops.len)
683 goto outerr;
684
577 /* Calc the byte offset in the buffer */ 685 /* Calc the byte offset in the buffer */
578 pageoffs = page - (int)(to >> this->page_shift); 686 pageoffs = page - (int)(to >> this->page_shift);
579 offs = pageoffs << this->page_shift; 687 offs = pageoffs << this->page_shift;
580 /* Preset the bbt area with 0xff */ 688 /* Preset the bbt area with 0xff */
581 memset (&buf[offs], 0xff, (size_t)(numblocks >> sft)); 689 memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
582 /* Preset the bbt's oob area with 0xff */ 690 ooboffs = len + (pageoffs * mtd->oobsize);
583 memset (&buf[len + pageoffs * mtd->oobsize], 0xff, 691
584 ((len >> this->page_shift) - pageoffs) * mtd->oobsize);
585 if (td->options & NAND_BBT_VERSION) {
586 buf[len + (pageoffs * mtd->oobsize) + td->veroffs] = td->version[chip];
587 }
588 } else { 692 } else {
589 /* Calc length */ 693 /* Calc length */
590 len = (size_t) (numblocks >> sft); 694 len = (size_t) (numblocks >> sft);
591 /* Make it page aligned ! */ 695 /* Make it page aligned ! */
592 len = (len + (mtd->oobblock-1)) & ~(mtd->oobblock-1); 696 len = (len + (mtd->writesize - 1)) &
697 ~(mtd->writesize - 1);
593 /* Preset the buffer with 0xff */ 698 /* Preset the buffer with 0xff */
594 memset (buf, 0xff, len + (len >> this->page_shift) * mtd->oobsize); 699 memset(buf, 0xff, len +
700 (len >> this->page_shift)* mtd->oobsize);
595 offs = 0; 701 offs = 0;
702 ooboffs = len;
596 /* Pattern is located in oob area of first page */ 703 /* Pattern is located in oob area of first page */
597 memcpy (&buf[len + td->offs], td->pattern, td->len); 704 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
598 if (td->options & NAND_BBT_VERSION) {
599 buf[len + td->veroffs] = td->version[chip];
600 }
601 } 705 }
602 706
707 if (td->options & NAND_BBT_VERSION)
708 buf[ooboffs + td->veroffs] = td->version[chip];
709
603 /* walk through the memory table */ 710 /* walk through the memory table */
604 for (i = 0; i < numblocks; ) { 711 for (i = 0; i < numblocks;) {
605 uint8_t dat; 712 uint8_t dat;
606 dat = this->bbt[bbtoffs + (i >> 2)]; 713 dat = this->bbt[bbtoffs + (i >> 2)];
607 for (j = 0; j < 4; j++ , i++) { 714 for (j = 0; j < 4; j++, i++) {
608 int sftcnt = (i << (3 - sft)) & sftmsk; 715 int sftcnt = (i << (3 - sft)) & sftmsk;
609 /* Do not store the reserved bbt blocks ! */ 716 /* Do not store the reserved bbt blocks ! */
610 buf[offs + (i >> sft)] &= ~(msk[dat & 0x03] << sftcnt); 717 buf[offs + (i >> sft)] &=
718 ~(msk[dat & 0x03] << sftcnt);
611 dat >>= 2; 719 dat >>= 2;
612 } 720 }
613 } 721 }
614 722
615 memset (&einfo, 0, sizeof (einfo)); 723 memset(&einfo, 0, sizeof(einfo));
616 einfo.mtd = mtd; 724 einfo.mtd = mtd;
617 einfo.addr = (unsigned long) to; 725 einfo.addr = (unsigned long)to;
618 einfo.len = 1 << this->bbt_erase_shift; 726 einfo.len = 1 << this->bbt_erase_shift;
619 res = nand_erase_nand (mtd, &einfo, 1); 727 res = nand_erase_nand(mtd, &einfo, 1);
620 if (res < 0) { 728 if (res < 0)
621 printk (KERN_WARNING "nand_bbt: Error during block erase: %d\n", res); 729 goto outerr;
622 return res;
623 }
624 730
625 res = mtd->write_ecc (mtd, to, len, &retlen, buf, &buf[len], &oobinfo); 731 res = scan_write_bbt(mtd, to, len, buf, &buf[len]);
626 if (res < 0) { 732 if (res < 0)
627 printk (KERN_WARNING "nand_bbt: Error while writing bad block table %d\n", res); 733 goto outerr;
628 return res; 734
629 } 735 printk(KERN_DEBUG "Bad block table written to 0x%08x, version "
630 printk (KERN_DEBUG "Bad block table written to 0x%08x, version 0x%02X\n", 736 "0x%02X\n", (unsigned int)to, td->version[chip]);
631 (unsigned int) to, td->version[chip]);
632 737
633 /* Mark it as used */ 738 /* Mark it as used */
634 td->pages[chip] = page; 739 td->pages[chip] = page;
635 } 740 }
636 return 0; 741 return 0;
742
743 outerr:
744 printk(KERN_WARNING
745 "nand_bbt: Error while writing bad block table %d\n", res);
746 return res;
637} 747}
638 748
639/** 749/**
@@ -644,27 +754,27 @@ write:
644 * The function creates a memory based bbt by scanning the device 754 * The function creates a memory based bbt by scanning the device
645 * for manufacturer / software marked good / bad blocks 755 * for manufacturer / software marked good / bad blocks
646*/ 756*/
647static inline int nand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd) 757static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
648{ 758{
649 struct nand_chip *this = mtd->priv; 759 struct nand_chip *this = mtd->priv;
650 760
651 bd->options &= ~NAND_BBT_SCANEMPTY; 761 bd->options &= ~NAND_BBT_SCANEMPTY;
652 return create_bbt (mtd, this->data_buf, bd, -1); 762 return create_bbt(mtd, this->buffers.databuf, bd, -1);
653} 763}
654 764
655/** 765/**
656 * check_create - [GENERIC] create and write bbt(s) if neccecary 766 * check_create - [GENERIC] create and write bbt(s) if necessary
657 * @mtd: MTD device structure 767 * @mtd: MTD device structure
658 * @buf: temporary buffer 768 * @buf: temporary buffer
659 * @bd: descriptor for the good/bad block search pattern 769 * @bd: descriptor for the good/bad block search pattern
660 * 770 *
661 * The function checks the results of the previous call to read_bbt 771 * The function checks the results of the previous call to read_bbt
662 * and creates / updates the bbt(s) if neccecary 772 * and creates / updates the bbt(s) if necessary
663 * Creation is neccecary if no bbt was found for the chip/device 773 * Creation is necessary if no bbt was found for the chip/device
664 * Update is neccecary if one of the tables is missing or the 774 * Update is necessary if one of the tables is missing or the
665 * version nr. of one table is less than the other 775 * version nr. of one table is less than the other
666*/ 776*/
667static int check_create (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd) 777static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
668{ 778{
669 int i, chips, writeops, chipsel, res; 779 int i, chips, writeops, chipsel, res;
670 struct nand_chip *this = mtd->priv; 780 struct nand_chip *this = mtd->priv;
@@ -732,35 +842,35 @@ static int check_create (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_des
732 rd = td; 842 rd = td;
733 goto writecheck; 843 goto writecheck;
734 } 844 }
735create: 845 create:
736 /* Create the bad block table by scanning the device ? */ 846 /* Create the bad block table by scanning the device ? */
737 if (!(td->options & NAND_BBT_CREATE)) 847 if (!(td->options & NAND_BBT_CREATE))
738 continue; 848 continue;
739 849
740 /* Create the table in memory by scanning the chip(s) */ 850 /* Create the table in memory by scanning the chip(s) */
741 create_bbt (mtd, buf, bd, chipsel); 851 create_bbt(mtd, buf, bd, chipsel);
742 852
743 td->version[i] = 1; 853 td->version[i] = 1;
744 if (md) 854 if (md)
745 md->version[i] = 1; 855 md->version[i] = 1;
746writecheck: 856 writecheck:
747 /* read back first ? */ 857 /* read back first ? */
748 if (rd) 858 if (rd)
749 read_abs_bbt (mtd, buf, rd, chipsel); 859 read_abs_bbt(mtd, buf, rd, chipsel);
750 /* If they weren't versioned, read both. */ 860 /* If they weren't versioned, read both. */
751 if (rd2) 861 if (rd2)
752 read_abs_bbt (mtd, buf, rd2, chipsel); 862 read_abs_bbt(mtd, buf, rd2, chipsel);
753 863
754 /* Write the bad block table to the device ? */ 864 /* Write the bad block table to the device ? */
755 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) { 865 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
756 res = write_bbt (mtd, buf, td, md, chipsel); 866 res = write_bbt(mtd, buf, td, md, chipsel);
757 if (res < 0) 867 if (res < 0)
758 return res; 868 return res;
759 } 869 }
760 870
761 /* Write the mirror bad block table to the device ? */ 871 /* Write the mirror bad block table to the device ? */
762 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) { 872 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
763 res = write_bbt (mtd, buf, md, td, chipsel); 873 res = write_bbt(mtd, buf, md, td, chipsel);
764 if (res < 0) 874 if (res < 0)
765 return res; 875 return res;
766 } 876 }
@@ -777,7 +887,7 @@ writecheck:
777 * accidental erasures / writes. The regions are identified by 887 * accidental erasures / writes. The regions are identified by
778 * the mark 0x02. 888 * the mark 0x02.
779*/ 889*/
780static void mark_bbt_region (struct mtd_info *mtd, struct nand_bbt_descr *td) 890static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
781{ 891{
782 struct nand_chip *this = mtd->priv; 892 struct nand_chip *this = mtd->priv;
783 int i, j, chips, block, nrblocks, update; 893 int i, j, chips, block, nrblocks, update;
@@ -795,7 +905,8 @@ static void mark_bbt_region (struct mtd_info *mtd, struct nand_bbt_descr *td)
795 for (i = 0; i < chips; i++) { 905 for (i = 0; i < chips; i++) {
796 if ((td->options & NAND_BBT_ABSPAGE) || 906 if ((td->options & NAND_BBT_ABSPAGE) ||
797 !(td->options & NAND_BBT_WRITE)) { 907 !(td->options & NAND_BBT_WRITE)) {
798 if (td->pages[i] == -1) continue; 908 if (td->pages[i] == -1)
909 continue;
799 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift); 910 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
800 block <<= 1; 911 block <<= 1;
801 oldval = this->bbt[(block >> 3)]; 912 oldval = this->bbt[(block >> 3)];
@@ -815,7 +926,8 @@ static void mark_bbt_region (struct mtd_info *mtd, struct nand_bbt_descr *td)
815 oldval = this->bbt[(block >> 3)]; 926 oldval = this->bbt[(block >> 3)];
816 newval = oldval | (0x2 << (block & 0x06)); 927 newval = oldval | (0x2 << (block & 0x06));
817 this->bbt[(block >> 3)] = newval; 928 this->bbt[(block >> 3)] = newval;
818 if (oldval != newval) update = 1; 929 if (oldval != newval)
930 update = 1;
819 block += 2; 931 block += 2;
820 } 932 }
821 /* If we want reserved blocks to be recorded to flash, and some 933 /* If we want reserved blocks to be recorded to flash, and some
@@ -840,7 +952,7 @@ static void mark_bbt_region (struct mtd_info *mtd, struct nand_bbt_descr *td)
840 * by calling the nand_free_bbt function. 952 * by calling the nand_free_bbt function.
841 * 953 *
842*/ 954*/
843int nand_scan_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd) 955int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
844{ 956{
845 struct nand_chip *this = mtd->priv; 957 struct nand_chip *this = mtd->priv;
846 int len, res = 0; 958 int len, res = 0;
@@ -850,21 +962,21 @@ int nand_scan_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
850 962
851 len = mtd->size >> (this->bbt_erase_shift + 2); 963 len = mtd->size >> (this->bbt_erase_shift + 2);
852 /* Allocate memory (2bit per block) */ 964 /* Allocate memory (2bit per block) */
853 this->bbt = kmalloc (len, GFP_KERNEL); 965 this->bbt = kmalloc(len, GFP_KERNEL);
854 if (!this->bbt) { 966 if (!this->bbt) {
855 printk (KERN_ERR "nand_scan_bbt: Out of memory\n"); 967 printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
856 return -ENOMEM; 968 return -ENOMEM;
857 } 969 }
858 /* Clear the memory bad block table */ 970 /* Clear the memory bad block table */
859 memset (this->bbt, 0x00, len); 971 memset(this->bbt, 0x00, len);
860 972
861 /* If no primary table decriptor is given, scan the device 973 /* If no primary table decriptor is given, scan the device
862 * to build a memory based bad block table 974 * to build a memory based bad block table
863 */ 975 */
864 if (!td) { 976 if (!td) {
865 if ((res = nand_memory_bbt(mtd, bd))) { 977 if ((res = nand_memory_bbt(mtd, bd))) {
866 printk (KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n"); 978 printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
867 kfree (this->bbt); 979 kfree(this->bbt);
868 this->bbt = NULL; 980 this->bbt = NULL;
869 } 981 }
870 return res; 982 return res;
@@ -873,35 +985,34 @@ int nand_scan_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
873 /* Allocate a temporary buffer for one eraseblock incl. oob */ 985 /* Allocate a temporary buffer for one eraseblock incl. oob */
874 len = (1 << this->bbt_erase_shift); 986 len = (1 << this->bbt_erase_shift);
875 len += (len >> this->page_shift) * mtd->oobsize; 987 len += (len >> this->page_shift) * mtd->oobsize;
876 buf = kmalloc (len, GFP_KERNEL); 988 buf = vmalloc(len);
877 if (!buf) { 989 if (!buf) {
878 printk (KERN_ERR "nand_bbt: Out of memory\n"); 990 printk(KERN_ERR "nand_bbt: Out of memory\n");
879 kfree (this->bbt); 991 kfree(this->bbt);
880 this->bbt = NULL; 992 this->bbt = NULL;
881 return -ENOMEM; 993 return -ENOMEM;
882 } 994 }
883 995
884 /* Is the bbt at a given page ? */ 996 /* Is the bbt at a given page ? */
885 if (td->options & NAND_BBT_ABSPAGE) { 997 if (td->options & NAND_BBT_ABSPAGE) {
886 res = read_abs_bbts (mtd, buf, td, md); 998 res = read_abs_bbts(mtd, buf, td, md);
887 } else { 999 } else {
888 /* Search the bad block table using a pattern in oob */ 1000 /* Search the bad block table using a pattern in oob */
889 res = search_read_bbts (mtd, buf, td, md); 1001 res = search_read_bbts(mtd, buf, td, md);
890 } 1002 }
891 1003
892 if (res) 1004 if (res)
893 res = check_create (mtd, buf, bd); 1005 res = check_create(mtd, buf, bd);
894 1006
895 /* Prevent the bbt regions from erasing / writing */ 1007 /* Prevent the bbt regions from erasing / writing */
896 mark_bbt_region (mtd, td); 1008 mark_bbt_region(mtd, td);
897 if (md) 1009 if (md)
898 mark_bbt_region (mtd, md); 1010 mark_bbt_region(mtd, md);
899 1011
900 kfree (buf); 1012 vfree(buf);
901 return res; 1013 return res;
902} 1014}
903 1015
904
905/** 1016/**
906 * nand_update_bbt - [NAND Interface] update bad block table(s) 1017 * nand_update_bbt - [NAND Interface] update bad block table(s)
907 * @mtd: MTD device structure 1018 * @mtd: MTD device structure
@@ -909,7 +1020,7 @@ int nand_scan_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
909 * 1020 *
910 * The function updates the bad block table(s) 1021 * The function updates the bad block table(s)
911*/ 1022*/
912int nand_update_bbt (struct mtd_info *mtd, loff_t offs) 1023int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
913{ 1024{
914 struct nand_chip *this = mtd->priv; 1025 struct nand_chip *this = mtd->priv;
915 int len, res = 0, writeops = 0; 1026 int len, res = 0, writeops = 0;
@@ -925,9 +1036,9 @@ int nand_update_bbt (struct mtd_info *mtd, loff_t offs)
925 /* Allocate a temporary buffer for one eraseblock incl. oob */ 1036 /* Allocate a temporary buffer for one eraseblock incl. oob */
926 len = (1 << this->bbt_erase_shift); 1037 len = (1 << this->bbt_erase_shift);
927 len += (len >> this->page_shift) * mtd->oobsize; 1038 len += (len >> this->page_shift) * mtd->oobsize;
928 buf = kmalloc (len, GFP_KERNEL); 1039 buf = kmalloc(len, GFP_KERNEL);
929 if (!buf) { 1040 if (!buf) {
930 printk (KERN_ERR "nand_update_bbt: Out of memory\n"); 1041 printk(KERN_ERR "nand_update_bbt: Out of memory\n");
931 return -ENOMEM; 1042 return -ENOMEM;
932 } 1043 }
933 1044
@@ -935,7 +1046,7 @@ int nand_update_bbt (struct mtd_info *mtd, loff_t offs)
935 1046
936 /* Do we have a bbt per chip ? */ 1047 /* Do we have a bbt per chip ? */
937 if (td->options & NAND_BBT_PERCHIP) { 1048 if (td->options & NAND_BBT_PERCHIP) {
938 chip = (int) (offs >> this->chip_shift); 1049 chip = (int)(offs >> this->chip_shift);
939 chipsel = chip; 1050 chipsel = chip;
940 } else { 1051 } else {
941 chip = 0; 1052 chip = 0;
@@ -948,17 +1059,17 @@ int nand_update_bbt (struct mtd_info *mtd, loff_t offs)
948 1059
949 /* Write the bad block table to the device ? */ 1060 /* Write the bad block table to the device ? */
950 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) { 1061 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
951 res = write_bbt (mtd, buf, td, md, chipsel); 1062 res = write_bbt(mtd, buf, td, md, chipsel);
952 if (res < 0) 1063 if (res < 0)
953 goto out; 1064 goto out;
954 } 1065 }
955 /* Write the mirror bad block table to the device ? */ 1066 /* Write the mirror bad block table to the device ? */
956 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) { 1067 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
957 res = write_bbt (mtd, buf, md, td, chipsel); 1068 res = write_bbt(mtd, buf, md, td, chipsel);
958 } 1069 }
959 1070
960out: 1071 out:
961 kfree (buf); 1072 kfree(buf);
962 return res; 1073 return res;
963} 1074}
964 1075
@@ -981,14 +1092,14 @@ static struct nand_bbt_descr largepage_memorybased = {
981}; 1092};
982 1093
983static struct nand_bbt_descr smallpage_flashbased = { 1094static struct nand_bbt_descr smallpage_flashbased = {
984 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES, 1095 .options = NAND_BBT_SCAN2NDPAGE,
985 .offs = 5, 1096 .offs = 5,
986 .len = 1, 1097 .len = 1,
987 .pattern = scan_ff_pattern 1098 .pattern = scan_ff_pattern
988}; 1099};
989 1100
990static struct nand_bbt_descr largepage_flashbased = { 1101static struct nand_bbt_descr largepage_flashbased = {
991 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES, 1102 .options = NAND_BBT_SCAN2NDPAGE,
992 .offs = 0, 1103 .offs = 0,
993 .len = 2, 1104 .len = 2,
994 .pattern = scan_ff_pattern 1105 .pattern = scan_ff_pattern
@@ -1036,7 +1147,7 @@ static struct nand_bbt_descr bbt_mirror_descr = {
1036 * support for the device and calls the nand_scan_bbt function 1147 * support for the device and calls the nand_scan_bbt function
1037 * 1148 *
1038*/ 1149*/
1039int nand_default_bbt (struct mtd_info *mtd) 1150int nand_default_bbt(struct mtd_info *mtd)
1040{ 1151{
1041 struct nand_chip *this = mtd->priv; 1152 struct nand_chip *this = mtd->priv;
1042 1153
@@ -1046,7 +1157,7 @@ int nand_default_bbt (struct mtd_info *mtd)
1046 * of the good / bad information, so we _must_ store 1157 * of the good / bad information, so we _must_ store
1047 * this information in a good / bad table during 1158 * this information in a good / bad table during
1048 * startup 1159 * startup
1049 */ 1160 */
1050 if (this->options & NAND_IS_AND) { 1161 if (this->options & NAND_IS_AND) {
1051 /* Use the default pattern descriptors */ 1162 /* Use the default pattern descriptors */
1052 if (!this->bbt_td) { 1163 if (!this->bbt_td) {
@@ -1054,10 +1165,9 @@ int nand_default_bbt (struct mtd_info *mtd)
1054 this->bbt_md = &bbt_mirror_descr; 1165 this->bbt_md = &bbt_mirror_descr;
1055 } 1166 }
1056 this->options |= NAND_USE_FLASH_BBT; 1167 this->options |= NAND_USE_FLASH_BBT;
1057 return nand_scan_bbt (mtd, &agand_flashbased); 1168 return nand_scan_bbt(mtd, &agand_flashbased);
1058 } 1169 }
1059 1170
1060
1061 /* Is a flash based bad block table requested ? */ 1171 /* Is a flash based bad block table requested ? */
1062 if (this->options & NAND_USE_FLASH_BBT) { 1172 if (this->options & NAND_USE_FLASH_BBT) {
1063 /* Use the default pattern descriptors */ 1173 /* Use the default pattern descriptors */
@@ -1066,18 +1176,17 @@ int nand_default_bbt (struct mtd_info *mtd)
1066 this->bbt_md = &bbt_mirror_descr; 1176 this->bbt_md = &bbt_mirror_descr;
1067 } 1177 }
1068 if (!this->badblock_pattern) { 1178 if (!this->badblock_pattern) {
1069 this->badblock_pattern = (mtd->oobblock > 512) ? 1179 this->badblock_pattern = (mtd->writesize > 512) ? &largepage_flashbased : &smallpage_flashbased;
1070 &largepage_flashbased : &smallpage_flashbased;
1071 } 1180 }
1072 } else { 1181 } else {
1073 this->bbt_td = NULL; 1182 this->bbt_td = NULL;
1074 this->bbt_md = NULL; 1183 this->bbt_md = NULL;
1075 if (!this->badblock_pattern) { 1184 if (!this->badblock_pattern) {
1076 this->badblock_pattern = (mtd->oobblock > 512) ? 1185 this->badblock_pattern = (mtd->writesize > 512) ?
1077 &largepage_memorybased : &smallpage_memorybased; 1186 &largepage_memorybased : &smallpage_memorybased;
1078 } 1187 }
1079 } 1188 }
1080 return nand_scan_bbt (mtd, this->badblock_pattern); 1189 return nand_scan_bbt(mtd, this->badblock_pattern);
1081} 1190}
1082 1191
1083/** 1192/**
@@ -1087,26 +1196,29 @@ int nand_default_bbt (struct mtd_info *mtd)
1087 * @allowbbt: allow access to bad block table region 1196 * @allowbbt: allow access to bad block table region
1088 * 1197 *
1089*/ 1198*/
1090int nand_isbad_bbt (struct mtd_info *mtd, loff_t offs, int allowbbt) 1199int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
1091{ 1200{
1092 struct nand_chip *this = mtd->priv; 1201 struct nand_chip *this = mtd->priv;
1093 int block; 1202 int block;
1094 uint8_t res; 1203 uint8_t res;
1095 1204
1096 /* Get block number * 2 */ 1205 /* Get block number * 2 */
1097 block = (int) (offs >> (this->bbt_erase_shift - 1)); 1206 block = (int)(offs >> (this->bbt_erase_shift - 1));
1098 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03; 1207 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1099 1208
1100 DEBUG (MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n", 1209 DEBUG(MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1101 (unsigned int)offs, block >> 1, res); 1210 (unsigned int)offs, block >> 1, res);
1102 1211
1103 switch ((int)res) { 1212 switch ((int)res) {
1104 case 0x00: return 0; 1213 case 0x00:
1105 case 0x01: return 1; 1214 return 0;
1106 case 0x02: return allowbbt ? 0 : 1; 1215 case 0x01:
1216 return 1;
1217 case 0x02:
1218 return allowbbt ? 0 : 1;
1107 } 1219 }
1108 return 1; 1220 return 1;
1109} 1221}
1110 1222
1111EXPORT_SYMBOL (nand_scan_bbt); 1223EXPORT_SYMBOL(nand_scan_bbt);
1112EXPORT_SYMBOL (nand_default_bbt); 1224EXPORT_SYMBOL(nand_default_bbt);