nand_bbt.c 38.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 *  drivers/mtd/nand_bbt.c
 *
 *  Overview:
 *   Bad block table support for the NAND driver
6
 *
7
 *  Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
L
Linus Torvalds 已提交
8 9 10 11 12 13 14
 *
 * 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:
 *
15
 * When nand_scan_bbt is called, then it tries to find the bad block table
16
 * depending on the options in the BBT descriptor(s). If no flash based BBT
17
 * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
18 19 20 21 22 23 24
 * 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
H
Huang Shijie 已提交
25
 * version number, then the mirror BBT is used to build the memory based BBT.
L
Linus Torvalds 已提交
26
 * If the tables are not versioned, then we "or" the bad block information.
27 28
 * 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
29
 * good / bad blocks and the bad block tables are created.
L
Linus Torvalds 已提交
30
 *
31 32
 * For manufacturer created BBTs like the one found on M-SYS DOC devices
 * the BBT is searched and read but never created
L
Linus Torvalds 已提交
33
 *
34
 * The auto generated bad block table is located in the last good blocks
35
 * of the device. The table is mirrored, so it can be updated eventually.
36 37 38
 * 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
39
 * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
40 41
 * course): it moves the ident pattern and the version byte into the data area
 * and the OOB area will remain untouched.
L
Linus Torvalds 已提交
42 43
 *
 * The table uses 2 bits per block
44 45 46
 * 11b:		block is good
 * 00b:		block is factory marked bad
 * 01b, 10b:	block is marked bad due to wear
L
Linus Torvalds 已提交
47 48 49 50 51 52
 *
 * 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
53
 *
L
Linus Torvalds 已提交
54 55 56 57
 * 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
58
 * - the space necessary for a bbt in FLASH does not exceed a block boundary
59
 *
L
Linus Torvalds 已提交
60 61 62 63 64
 */

#include <linux/slab.h>
#include <linux/types.h>
#include <linux/mtd/mtd.h>
65
#include <linux/mtd/bbm.h>
L
Linus Torvalds 已提交
66 67 68 69
#include <linux/mtd/nand.h>
#include <linux/mtd/nand_ecc.h>
#include <linux/bitops.h>
#include <linux/delay.h>
70
#include <linux/vmalloc.h>
71
#include <linux/export.h>
B
Brian Norris 已提交
72
#include <linux/string.h>
L
Linus Torvalds 已提交
73

74 75 76 77 78 79 80 81
#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

82 83
static int nand_update_bbt(struct mtd_info *mtd, loff_t offs);

84 85 86 87 88 89 90 91 92 93 94 95 96 97
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;
}

98 99
static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
{
100 101 102
	if (memcmp(buf, td->pattern, td->len))
		return -1;
	return 0;
103 104
}

105
/**
L
Linus Torvalds 已提交
106
 * check_pattern - [GENERIC] check if a pattern is in the buffer
107 108 109 110
 * @buf: the buffer to search
 * @len: the length of buffer to search
 * @paglen: the pagelength
 * @td: search pattern descriptor
L
Linus Torvalds 已提交
111
 *
112
 * Check for a pattern at the given place. Used to search bad block tables and
113
 * good / bad block identifiers.
114
 */
115
static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
L
Linus Torvalds 已提交
116
{
117 118 119
	if (td->options & NAND_BBT_NO_OOB)
		return check_pattern_no_oob(buf, td);

L
Linus Torvalds 已提交
120
	/* Compare the pattern */
121
	if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
122
		return -1;
123

L
Linus Torvalds 已提交
124 125 126
	return 0;
}

127
/**
128
 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
129 130
 * @buf: the buffer to search
 * @td:	search pattern descriptor
131
 *
132 133 134 135
 * 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.
 */
136
static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
137 138
{
	/* Compare the pattern */
B
Brian Norris 已提交
139 140
	if (memcmp(buf + td->offs, td->pattern, td->len))
		return -1;
141 142 143
	return 0;
}

144 145
/**
 * add_marker_len - compute the length of the marker in data area
146
 * @td: BBT descriptor used for computation
147
 *
148
 * The length will be 0 if the marker is located in OOB area.
149 150 151 152 153 154 155 156 157 158 159 160 161 162
 */
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;
}

L
Linus Torvalds 已提交
163 164
/**
 * read_bbt - [GENERIC] Read the bad block table starting from page
165 166 167 168
 * @mtd: MTD device structure
 * @buf: temporary buffer
 * @page: the starting page
 * @num: the number of bbt descriptors to read
169
 * @td: the bbt describtion table
170
 * @offs: block number offset in the table
L
Linus Torvalds 已提交
171 172 173
 *
 * Read the bad block table starting from page.
 */
174
static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
175
		struct nand_bbt_descr *td, int offs)
L
Linus Torvalds 已提交
176
{
177
	int res, ret = 0, i, j, act = 0;
L
Linus Torvalds 已提交
178 179 180
	struct nand_chip *this = mtd->priv;
	size_t retlen, len, totlen;
	loff_t from;
181
	int bits = td->options & NAND_BBT_NRBITS_MSK;
B
Brian Norris 已提交
182
	uint8_t msk = (uint8_t)((1 << bits) - 1);
183
	u32 marker_len;
184
	int reserved_block_code = td->reserved_block_code;
L
Linus Torvalds 已提交
185 186

	totlen = (num * bits) >> 3;
187
	marker_len = add_marker_len(td);
B
Brian Norris 已提交
188
	from = ((loff_t)page) << this->page_shift;
189

L
Linus Torvalds 已提交
190
	while (totlen) {
B
Brian Norris 已提交
191
		len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
192 193 194 195 196 197 198 199 200
		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;
		}
201
		res = mtd_read(mtd, from, len, &retlen, buf);
L
Linus Torvalds 已提交
202
		if (res < 0) {
203 204 205 206 207 208 209 210 211 212
			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");
L
Linus Torvalds 已提交
213 214
				return res;
			}
215
		}
L
Linus Torvalds 已提交
216 217 218 219

		/* Analyse data */
		for (i = 0; i < len; i++) {
			uint8_t dat = buf[i];
220
			for (j = 0; j < 8; j += bits, act++) {
L
Linus Torvalds 已提交
221 222 223
				uint8_t tmp = (dat >> j) & msk;
				if (tmp == msk)
					continue;
224
				if (reserved_block_code && (tmp == reserved_block_code)) {
225
					pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
226 227 228
						 (loff_t)(offs + act) <<
						 this->bbt_erase_shift);
					bbt_mark_entry(this, offs + act,
229
							BBT_BLOCK_RESERVED);
230
					mtd->ecc_stats.bbtblocks++;
L
Linus Torvalds 已提交
231 232
					continue;
				}
233 234
				/*
				 * Leave it for now, if it's matured we can
235
				 * move this message to pr_debug.
236
				 */
237
				pr_info("nand_read_bbt: bad block at 0x%012llx\n",
238 239
					 (loff_t)(offs + act) <<
					 this->bbt_erase_shift);
240
				/* Factory marked bad or worn out? */
L
Linus Torvalds 已提交
241
				if (tmp == 0)
242
					bbt_mark_entry(this, offs + act,
243
							BBT_BLOCK_FACTORY_BAD);
L
Linus Torvalds 已提交
244
				else
245
					bbt_mark_entry(this, offs + act,
246
							BBT_BLOCK_WORN);
247
				mtd->ecc_stats.badblocks++;
248
			}
L
Linus Torvalds 已提交
249 250 251 252
		}
		totlen -= len;
		from += len;
	}
253
	return ret;
L
Linus Torvalds 已提交
254 255 256 257
}

/**
 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
258 259 260
 * @mtd: MTD device structure
 * @buf: temporary buffer
 * @td: descriptor for the bad block table
B
Brian Norris 已提交
261
 * @chip: read the table for a specific chip, -1 read all chips; applies only if
262
 *        NAND_BBT_PERCHIP option is set
L
Linus Torvalds 已提交
263
 *
264 265 266
 * Read the bad block table for all chips starting at a given page. We assume
 * that the bbt bits are in consecutive order.
 */
267
static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
L
Linus Torvalds 已提交
268 269 270 271 272 273 274 275
{
	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)
276 277 278
				res = read_bbt(mtd, buf, td->pages[i],
					this->chipsize >> this->bbt_erase_shift,
					td, offs);
L
Linus Torvalds 已提交
279 280
			if (res)
				return res;
281
			offs += this->chipsize >> this->bbt_erase_shift;
L
Linus Torvalds 已提交
282 283
		}
	} else {
284 285
		res = read_bbt(mtd, buf, td->pages[0],
				mtd->size >> this->bbt_erase_shift, td, 0);
L
Linus Torvalds 已提交
286 287 288 289 290 291
		if (res)
			return res;
	}
	return 0;
}

292
/* BBT marker is in the first page, no OOB */
293
static int scan_read_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
294 295 296 297 298 299 300 301 302
			 struct nand_bbt_descr *td)
{
	size_t retlen;
	size_t len;

	len = td->len;
	if (td->options & NAND_BBT_VERSION)
		len++;

303
	return mtd_read(mtd, offs, len, &retlen, buf);
304 305
}

306
/**
307
 * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
308 309 310 311 312 313 314 315 316
 * @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.
 */
317
static int scan_read_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
318 319 320
			 size_t len)
{
	struct mtd_oob_ops ops;
321
	int res, ret = 0;
322

323
	ops.mode = MTD_OPS_PLACE_OOB;
324 325 326
	ops.ooboffs = 0;
	ops.ooblen = mtd->oobsize;

327
	while (len > 0) {
B
Brian Norris 已提交
328 329 330
		ops.datbuf = buf;
		ops.len = min(len, (size_t)mtd->writesize);
		ops.oobbuf = buf + ops.len;
331

332
		res = mtd_read_oob(mtd, offs, &ops);
333 334 335 336 337 338
		if (res) {
			if (!mtd_is_bitflip_or_eccerr(res))
				return res;
			else if (mtd_is_eccerr(res) || !ret)
				ret = res;
		}
339 340 341

		buf += mtd->oobsize + mtd->writesize;
		len -= mtd->writesize;
D
Dmitry Maluka 已提交
342
		offs += mtd->writesize;
343
	}
344
	return ret;
345 346
}

347
static int scan_read(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
348 349 350
			 size_t len, struct nand_bbt_descr *td)
{
	if (td->options & NAND_BBT_NO_OOB)
351
		return scan_read_data(mtd, buf, offs, td);
352
	else
353
		return scan_read_oob(mtd, buf, offs, len);
354 355
}

356
/* Scan write data with oob to flash */
357 358 359 360 361
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;

362
	ops.mode = MTD_OPS_PLACE_OOB;
363 364 365 366 367 368
	ops.ooboffs = 0;
	ops.ooblen = mtd->oobsize;
	ops.datbuf = buf;
	ops.oobbuf = oob;
	ops.len = len;

369
	return mtd_write_oob(mtd, offs, &ops);
370 371
}

372 373 374 375 376 377 378 379 380
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;
}

L
Linus Torvalds 已提交
381 382
/**
 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
383 384 385 386
 * @mtd: MTD device structure
 * @buf: temporary buffer
 * @td: descriptor for the bad block table
 * @md:	descriptor for the bad block table mirror
L
Linus Torvalds 已提交
387
 *
388 389 390
 * Read the bad block table(s) for all chips starting at a given page. We
 * assume that the bbt bits are in consecutive order.
 */
391 392
static void read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
			  struct nand_bbt_descr *td, struct nand_bbt_descr *md)
L
Linus Torvalds 已提交
393 394 395
{
	struct nand_chip *this = mtd->priv;

396
	/* Read the primary version, if available */
L
Linus Torvalds 已提交
397
	if (td->options & NAND_BBT_VERSION) {
398
		scan_read(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
399 400
			      mtd->writesize, td);
		td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
401
		pr_info("Bad block table at page %d, version 0x%02X\n",
402
			 td->pages[0], td->version[0]);
L
Linus Torvalds 已提交
403 404
	}

405
	/* Read the mirror version, if available */
L
Linus Torvalds 已提交
406
	if (md && (md->options & NAND_BBT_VERSION)) {
407
		scan_read(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
408
			      mtd->writesize, md);
409
		md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
410
		pr_info("Bad block table at page %d, version 0x%02X\n",
411
			 md->pages[0], md->version[0]);
L
Linus Torvalds 已提交
412 413 414
	}
}

415
/* Scan a given block full */
416 417
static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
			   loff_t offs, uint8_t *buf, size_t readlen,
418
			   int scanlen, int numpages)
419 420 421
{
	int ret, j;

422
	ret = scan_read_oob(mtd, buf, offs, readlen);
423
	/* Ignore ECC errors when checking for BBM */
424
	if (ret && !mtd_is_bitflip_or_eccerr(ret))
425 426
		return ret;

427
	for (j = 0; j < numpages; j++, buf += scanlen) {
428 429 430 431 432 433
		if (check_pattern(buf, scanlen, mtd->writesize, bd))
			return 1;
	}
	return 0;
}

434
/* Scan a given block partially */
435
static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
436
			   loff_t offs, uint8_t *buf, int numpages)
437 438 439 440 441 442 443 444
{
	struct mtd_oob_ops ops;
	int j, ret;

	ops.ooblen = mtd->oobsize;
	ops.oobbuf = buf;
	ops.ooboffs = 0;
	ops.datbuf = NULL;
445
	ops.mode = MTD_OPS_PLACE_OOB;
446

447
	for (j = 0; j < numpages; j++) {
448
		/*
449 450
		 * Read the full oob until read_oob is fixed to handle single
		 * byte reads for 16 bit buswidth.
451
		 */
452
		ret = mtd_read_oob(mtd, offs, &ops);
453
		/* Ignore ECC errors when checking for BBM */
454
		if (ret && !mtd_is_bitflip_or_eccerr(ret))
455 456 457 458 459 460 461 462 463 464
			return ret;

		if (check_short_pattern(buf, bd))
			return 1;

		offs += mtd->writesize;
	}
	return 0;
}

L
Linus Torvalds 已提交
465 466
/**
 * create_bbt - [GENERIC] Create a bad block table by scanning the device
467 468 469 470 471
 * @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
L
Linus Torvalds 已提交
472
 *
473 474
 * Create a bad block table by scanning the device for the given good/bad block
 * identify pattern.
L
Linus Torvalds 已提交
475
 */
476 477
static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
	struct nand_bbt_descr *bd, int chip)
L
Linus Torvalds 已提交
478 479
{
	struct nand_chip *this = mtd->priv;
480
	int i, numblocks, numpages, scanlen;
L
Linus Torvalds 已提交
481 482
	int startblock;
	loff_t from;
483
	size_t readlen;
L
Linus Torvalds 已提交
484

485
	pr_info("Scanning device for bad blocks\n");
L
Linus Torvalds 已提交
486 487

	if (bd->options & NAND_BBT_SCANALLPAGES)
488
		numpages = 1 << (this->bbt_erase_shift - this->page_shift);
489
	else if (bd->options & NAND_BBT_SCAN2NDPAGE)
490
		numpages = 2;
491
	else
492
		numpages = 1;
493

494 495 496
	/* We need only read few bytes from the OOB area */
	scanlen = 0;
	readlen = bd->len;
L
Linus Torvalds 已提交
497 498

	if (chip == -1) {
499
		numblocks = mtd->size >> this->bbt_erase_shift;
L
Linus Torvalds 已提交
500 501 502 503
		startblock = 0;
		from = 0;
	} else {
		if (chip >= this->numchips) {
504
			pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
505
			       chip + 1, this->numchips);
506
			return -EINVAL;
L
Linus Torvalds 已提交
507
		}
508
		numblocks = this->chipsize >> this->bbt_erase_shift;
L
Linus Torvalds 已提交
509 510
		startblock = chip * numblocks;
		numblocks += startblock;
511
		from = (loff_t)startblock << this->bbt_erase_shift;
L
Linus Torvalds 已提交
512
	}
513

514
	if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
515
		from += mtd->erasesize - (mtd->writesize * numpages);
516

517
	for (i = startblock; i < numblocks; i++) {
518
		int ret;
519

520 521
		BUG_ON(bd->options & NAND_BBT_NO_OOB);

522 523
		if (bd->options & NAND_BBT_SCANALLPAGES)
			ret = scan_block_full(mtd, bd, from, buf, readlen,
524
					      scanlen, numpages);
525
		else
526
			ret = scan_block_fast(mtd, bd, from, buf, numpages);
527 528 529 530 531

		if (ret < 0)
			return ret;

		if (ret) {
532
			bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
533
			pr_warn("Bad eraseblock %d at 0x%012llx\n",
534
				i, (unsigned long long)from);
535
			mtd->ecc_stats.badblocks++;
L
Linus Torvalds 已提交
536
		}
537

L
Linus Torvalds 已提交
538 539
		from += (1 << this->bbt_erase_shift);
	}
540
	return 0;
L
Linus Torvalds 已提交
541 542 543 544
}

/**
 * search_bbt - [GENERIC] scan the device for a specific bad block table
545 546 547
 * @mtd: MTD device structure
 * @buf: temporary buffer
 * @td: descriptor for the bad block table
L
Linus Torvalds 已提交
548
 *
549 550 551 552 553 554
 * 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.
L
Linus Torvalds 已提交
555
 *
556
 * The bbt ident pattern resides in the oob area of the first page in a block.
L
Linus Torvalds 已提交
557
 */
558
static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
L
Linus Torvalds 已提交
559 560 561 562
{
	struct nand_chip *this = mtd->priv;
	int i, chips;
	int bits, startblock, block, dir;
J
Joern Engel 已提交
563
	int scanlen = mtd->writesize + mtd->oobsize;
L
Linus Torvalds 已提交
564
	int bbtblocks;
565
	int blocktopage = this->bbt_erase_shift - this->page_shift;
L
Linus Torvalds 已提交
566

567
	/* Search direction top -> down? */
L
Linus Torvalds 已提交
568
	if (td->options & NAND_BBT_LASTBLOCK) {
569
		startblock = (mtd->size >> this->bbt_erase_shift) - 1;
L
Linus Torvalds 已提交
570 571
		dir = -1;
	} else {
572
		startblock = 0;
L
Linus Torvalds 已提交
573
		dir = 1;
574 575
	}

576
	/* Do we have a bbt per chip? */
L
Linus Torvalds 已提交
577 578 579 580 581 582 583 584
	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;
	}
585

L
Linus Torvalds 已提交
586 587
	/* Number of bits for each erase block in the bbt */
	bits = td->options & NAND_BBT_NRBITS_MSK;
588

L
Linus Torvalds 已提交
589 590
	for (i = 0; i < chips; i++) {
		/* Reset version information */
591
		td->version[i] = 0;
L
Linus Torvalds 已提交
592 593 594
		td->pages[i] = -1;
		/* Scan the maximum number of blocks */
		for (block = 0; block < td->maxblocks; block++) {
595

L
Linus Torvalds 已提交
596
			int actblock = startblock + dir * block;
597
			loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
598

L
Linus Torvalds 已提交
599
			/* Read first page */
600
			scan_read(mtd, buf, offs, mtd->writesize, td);
J
Joern Engel 已提交
601
			if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
602
				td->pages[i] = actblock << blocktopage;
L
Linus Torvalds 已提交
603
				if (td->options & NAND_BBT_VERSION) {
604 605
					offs = bbt_get_ver_offs(mtd, td);
					td->version[i] = buf[offs];
L
Linus Torvalds 已提交
606 607 608 609 610 611 612 613 614
				}
				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)
615
			pr_warn("Bad block table not found for chip %d\n", i);
L
Linus Torvalds 已提交
616
		else
617 618
			pr_info("Bad block table found at page %d, version "
				 "0x%02X\n", td->pages[i], td->version[i]);
L
Linus Torvalds 已提交
619
	}
620
	return 0;
L
Linus Torvalds 已提交
621 622 623 624
}

/**
 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
625 626 627 628
 * @mtd: MTD device structure
 * @buf: temporary buffer
 * @td: descriptor for the bad block table
 * @md: descriptor for the bad block table mirror
L
Linus Torvalds 已提交
629
 *
630 631
 * Search and read the bad block table(s).
 */
632 633 634
static void search_read_bbts(struct mtd_info *mtd, uint8_t *buf,
			     struct nand_bbt_descr *td,
			     struct nand_bbt_descr *md)
L
Linus Torvalds 已提交
635 636
{
	/* Search the primary table */
637
	search_bbt(mtd, buf, td);
638

L
Linus Torvalds 已提交
639 640
	/* Search the mirror table */
	if (md)
641
		search_bbt(mtd, buf, md);
L
Linus Torvalds 已提交
642 643
}

644
/**
L
Linus Torvalds 已提交
645
 * write_bbt - [GENERIC] (Re)write the bad block table
646 647 648 649 650
 * @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
L
Linus Torvalds 已提交
651
 *
652 653
 * (Re)write the bad block table.
 */
654
static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
655 656
		     struct nand_bbt_descr *td, struct nand_bbt_descr *md,
		     int chipsel)
L
Linus Torvalds 已提交
657 658 659
{
	struct nand_chip *this = mtd->priv;
	struct erase_info einfo;
660
	int i, res, chip = 0;
L
Linus Torvalds 已提交
661
	int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
662
	int nrchips, pageoffs, ooboffs;
L
Linus Torvalds 已提交
663 664
	uint8_t msk[4];
	uint8_t rcode = td->reserved_block_code;
665
	size_t retlen, len = 0;
L
Linus Torvalds 已提交
666
	loff_t to;
667 668 669 670 671
	struct mtd_oob_ops ops;

	ops.ooblen = mtd->oobsize;
	ops.ooboffs = 0;
	ops.datbuf = NULL;
672
	ops.mode = MTD_OPS_PLACE_OOB;
L
Linus Torvalds 已提交
673 674 675

	if (!rcode)
		rcode = 0xff;
676
	/* Write bad block table per chip rather than per device? */
L
Linus Torvalds 已提交
677
	if (td->options & NAND_BBT_PERCHIP) {
678
		numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
679
		/* Full device write or specific chip? */
L
Linus Torvalds 已提交
680 681 682 683 684 685 686
		if (chipsel == -1) {
			nrchips = this->numchips;
		} else {
			nrchips = chipsel + 1;
			chip = chipsel;
		}
	} else {
687
		numblocks = (int)(mtd->size >> this->bbt_erase_shift);
L
Linus Torvalds 已提交
688
		nrchips = 1;
689 690
	}

L
Linus Torvalds 已提交
691 692
	/* Loop through the chips */
	for (; chip < nrchips; chip++) {
693 694
		/*
		 * There was already a version of the table, reuse the page
695
		 * This applies for absolute placement too, as we have the
L
Linus Torvalds 已提交
696 697 698 699 700
		 * page nr. in td->pages.
		 */
		if (td->pages[chip] != -1) {
			page = td->pages[chip];
			goto write;
701
		}
L
Linus Torvalds 已提交
702

703 704 705 706
		/*
		 * Automatic placement of the bad block table. Search direction
		 * top -> down?
		 */
L
Linus Torvalds 已提交
707 708 709 710 711 712
		if (td->options & NAND_BBT_LASTBLOCK) {
			startblock = numblocks * (chip + 1) - 1;
			dir = -1;
		} else {
			startblock = chip * numblocks;
			dir = 1;
713
		}
L
Linus Torvalds 已提交
714 715 716 717

		for (i = 0; i < td->maxblocks; i++) {
			int block = startblock + dir * i;
			/* Check, if the block is bad */
718 719 720
			switch (bbt_get_entry(this, block)) {
			case BBT_BLOCK_WORN:
			case BBT_BLOCK_FACTORY_BAD:
L
Linus Torvalds 已提交
721 722
				continue;
			}
723 724
			page = block <<
				(this->bbt_erase_shift - this->page_shift);
L
Linus Torvalds 已提交
725 726 727 728
			/* Check, if the block is used by the mirror table */
			if (!md || md->pages[chip] != page)
				goto write;
		}
729
		pr_err("No space left to write bad block table\n");
L
Linus Torvalds 已提交
730
		return -ENOSPC;
731
	write:
L
Linus Torvalds 已提交
732 733 734

		/* Set up shift count and masks for the flash table */
		bits = td->options & NAND_BBT_NRBITS_MSK;
735
		msk[2] = ~rcode;
L
Linus Torvalds 已提交
736
		switch (bits) {
737 738 739 740 741 742 743 744 745 746 747 748
		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;
L
Linus Torvalds 已提交
749 750
		default: return -EINVAL;
		}
751

B
Brian Norris 已提交
752
		to = ((loff_t)page) << this->page_shift;
L
Linus Torvalds 已提交
753

754
		/* Must we save the block contents? */
L
Linus Torvalds 已提交
755 756
		if (td->options & NAND_BBT_SAVECONTENT) {
			/* Make it block aligned */
B
Brian Norris 已提交
757
			to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1));
L
Linus Torvalds 已提交
758
			len = 1 << this->bbt_erase_shift;
759
			res = mtd_read(mtd, to, len, &retlen, buf);
L
Linus Torvalds 已提交
760 761
			if (res < 0) {
				if (retlen != len) {
762 763
					pr_info("nand_bbt: error reading block "
						"for writing the bad block table\n");
L
Linus Torvalds 已提交
764 765
					return res;
				}
766 767
				pr_warn("nand_bbt: ECC error while reading "
					"block for writing bad block table\n");
L
Linus Torvalds 已提交
768
			}
769
			/* Read oob data */
770
			ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
771
			ops.oobbuf = &buf[len];
772
			res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
773
			if (res < 0 || ops.oobretlen != ops.ooblen)
774 775
				goto outerr;

L
Linus Torvalds 已提交
776 777 778 779
			/* 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 */
B
Brian Norris 已提交
780
			memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
781 782
			ooboffs = len + (pageoffs * mtd->oobsize);

783 784 785
		} else if (td->options & NAND_BBT_NO_OOB) {
			ooboffs = 0;
			offs = td->len;
786
			/* The version byte */
787 788 789
			if (td->options & NAND_BBT_VERSION)
				offs++;
			/* Calc length */
B
Brian Norris 已提交
790
			len = (size_t)(numblocks >> sft);
791
			len += offs;
792
			/* Make it page aligned! */
793 794 795 796 797
			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);
L
Linus Torvalds 已提交
798 799
		} else {
			/* Calc length */
B
Brian Norris 已提交
800
			len = (size_t)(numblocks >> sft);
801
			/* Make it page aligned! */
802
			len = ALIGN(len, mtd->writesize);
L
Linus Torvalds 已提交
803
			/* Preset the buffer with 0xff */
804 805
			memset(buf, 0xff, len +
			       (len >> this->page_shift)* mtd->oobsize);
L
Linus Torvalds 已提交
806
			offs = 0;
807
			ooboffs = len;
L
Linus Torvalds 已提交
808
			/* Pattern is located in oob area of first page */
809
			memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
L
Linus Torvalds 已提交
810
		}
811

812 813 814
		if (td->options & NAND_BBT_VERSION)
			buf[ooboffs + td->veroffs] = td->version[chip];

815
		/* Walk through the memory table */
816
		for (i = 0; i < numblocks; i++) {
L
Linus Torvalds 已提交
817
			uint8_t dat;
818 819 820 821
			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);
L
Linus Torvalds 已提交
822
		}
823

824
		memset(&einfo, 0, sizeof(einfo));
L
Linus Torvalds 已提交
825
		einfo.mtd = mtd;
826
		einfo.addr = to;
L
Linus Torvalds 已提交
827
		einfo.len = 1 << this->bbt_erase_shift;
828
		res = nand_erase_nand(mtd, &einfo, 1);
829 830
		if (res < 0)
			goto outerr;
831

832 833 834
		res = scan_write_bbt(mtd, to, len, buf,
				td->options & NAND_BBT_NO_OOB ? NULL :
				&buf[len]);
835 836 837
		if (res < 0)
			goto outerr;

838 839
		pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
			 (unsigned long long)to, td->version[chip]);
840

L
Linus Torvalds 已提交
841 842
		/* Mark it as used */
		td->pages[chip] = page;
843
	}
L
Linus Torvalds 已提交
844
	return 0;
845 846

 outerr:
847
	pr_warn("nand_bbt: error while writing bad block table %d\n", res);
848
	return res;
L
Linus Torvalds 已提交
849 850 851 852
}

/**
 * nand_memory_bbt - [GENERIC] create a memory based bad block table
853 854
 * @mtd: MTD device structure
 * @bd: descriptor for the good/bad block search pattern
L
Linus Torvalds 已提交
855
 *
856 857 858
 * The function creates a memory based bbt by scanning the device for
 * manufacturer / software marked good / bad blocks.
 */
859
static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
L
Linus Torvalds 已提交
860 861 862
{
	struct nand_chip *this = mtd->priv;

863
	return create_bbt(mtd, this->buffers->databuf, bd, -1);
L
Linus Torvalds 已提交
864 865 866
}

/**
867
 * check_create - [GENERIC] create and write bbt(s) if necessary
868 869 870
 * @mtd: MTD device structure
 * @buf: temporary buffer
 * @bd: descriptor for the good/bad block search pattern
L
Linus Torvalds 已提交
871
 *
872 873 874 875 876
 * 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.
 */
877
static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
L
Linus Torvalds 已提交
878
{
879
	int i, chips, writeops, create, chipsel, res, res2;
L
Linus Torvalds 已提交
880 881 882 883 884
	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;

885
	/* Do we have a bbt per chip? */
886
	if (td->options & NAND_BBT_PERCHIP)
L
Linus Torvalds 已提交
887
		chips = this->numchips;
888
	else
L
Linus Torvalds 已提交
889
		chips = 1;
890

L
Linus Torvalds 已提交
891 892
	for (i = 0; i < chips; i++) {
		writeops = 0;
893
		create = 0;
L
Linus Torvalds 已提交
894 895
		rd = NULL;
		rd2 = NULL;
896
		res = res2 = 0;
897
		/* Per chip or per device? */
L
Linus Torvalds 已提交
898
		chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
899
		/* Mirrored table available? */
L
Linus Torvalds 已提交
900 901
		if (md) {
			if (td->pages[i] == -1 && md->pages[i] == -1) {
902
				create = 1;
L
Linus Torvalds 已提交
903
				writeops = 0x03;
904
			} else if (td->pages[i] == -1) {
905
				rd = md;
B
Brian Norris 已提交
906
				writeops = 0x01;
907
			} else if (md->pages[i] == -1) {
L
Linus Torvalds 已提交
908
				rd = td;
B
Brian Norris 已提交
909
				writeops = 0x02;
910
			} else if (td->version[i] == md->version[i]) {
L
Linus Torvalds 已提交
911 912 913
				rd = td;
				if (!(td->options & NAND_BBT_VERSION))
					rd2 = md;
914
			} else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
L
Linus Torvalds 已提交
915
				rd = td;
B
Brian Norris 已提交
916
				writeops = 0x02;
L
Linus Torvalds 已提交
917 918
			} else {
				rd = md;
B
Brian Norris 已提交
919
				writeops = 0x01;
L
Linus Torvalds 已提交
920 921 922
			}
		} else {
			if (td->pages[i] == -1) {
923
				create = 1;
L
Linus Torvalds 已提交
924
				writeops = 0x01;
925 926
			} else {
				rd = td;
L
Linus Torvalds 已提交
927 928
			}
		}
929

930 931 932 933 934 935 936 937 938 939 940 941 942 943
		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;
		}

944
		/* Read back first? */
945 946 947 948 949
		if (rd) {
			res = read_abs_bbt(mtd, buf, rd, chipsel);
			if (mtd_is_eccerr(res)) {
				/* Mark table as invalid */
				rd->pages[i] = -1;
950
				rd->version[i] = 0;
951 952 953 954
				i--;
				continue;
			}
		}
955
		/* If they weren't versioned, read both */
956 957 958 959 960
		if (rd2) {
			res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
			if (mtd_is_eccerr(res2)) {
				/* Mark table as invalid */
				rd2->pages[i] = -1;
961
				rd2->version[i] = 0;
962 963 964 965 966 967 968 969
				i--;
				continue;
			}
		}

		/* Scrub the flash table(s)? */
		if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
			writeops = 0x03;
L
Linus Torvalds 已提交
970

971 972 973 974 975 976
		/* Update version numbers before writing */
		if (md) {
			td->version[i] = max(td->version[i], md->version[i]);
			md->version[i] = td->version[i];
		}

977
		/* Write the bad block table to the device? */
L
Linus Torvalds 已提交
978
		if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
979
			res = write_bbt(mtd, buf, td, md, chipsel);
L
Linus Torvalds 已提交
980 981 982
			if (res < 0)
				return res;
		}
983

984
		/* Write the mirror bad block table to the device? */
L
Linus Torvalds 已提交
985
		if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
986
			res = write_bbt(mtd, buf, md, td, chipsel);
L
Linus Torvalds 已提交
987 988 989 990
			if (res < 0)
				return res;
		}
	}
991
	return 0;
L
Linus Torvalds 已提交
992 993 994
}

/**
995
 * mark_bbt_regions - [GENERIC] mark the bad block table regions
996 997
 * @mtd: MTD device structure
 * @td: bad block table descriptor
L
Linus Torvalds 已提交
998
 *
999 1000 1001
 * The bad block table regions are marked as "bad" to prevent accidental
 * erasures / writes. The regions are identified by the mark 0x02.
 */
1002
static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
L
Linus Torvalds 已提交
1003 1004 1005
{
	struct nand_chip *this = mtd->priv;
	int i, j, chips, block, nrblocks, update;
1006
	uint8_t oldval;
L
Linus Torvalds 已提交
1007

1008
	/* Do we have a bbt per chip? */
L
Linus Torvalds 已提交
1009 1010 1011 1012 1013 1014
	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);
1015 1016
	}

L
Linus Torvalds 已提交
1017 1018 1019
	for (i = 0; i < chips; i++) {
		if ((td->options & NAND_BBT_ABSPAGE) ||
		    !(td->options & NAND_BBT_WRITE)) {
1020 1021
			if (td->pages[i] == -1)
				continue;
L
Linus Torvalds 已提交
1022
			block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
1023 1024
			oldval = bbt_get_entry(this, block);
			bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1025 1026
			if ((oldval != BBT_BLOCK_RESERVED) &&
					td->reserved_block_code)
1027 1028
				nand_update_bbt(mtd, (loff_t)block <<
						this->bbt_erase_shift);
L
Linus Torvalds 已提交
1029 1030 1031 1032 1033
			continue;
		}
		update = 0;
		if (td->options & NAND_BBT_LASTBLOCK)
			block = ((i + 1) * nrblocks) - td->maxblocks;
1034
		else
L
Linus Torvalds 已提交
1035 1036
			block = i * nrblocks;
		for (j = 0; j < td->maxblocks; j++) {
1037 1038
			oldval = bbt_get_entry(this, block);
			bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1039
			if (oldval != BBT_BLOCK_RESERVED)
1040
				update = 1;
1041
			block++;
1042
		}
1043 1044 1045 1046 1047
		/*
		 * 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.
		 */
L
Linus Torvalds 已提交
1048
		if (update && td->reserved_block_code)
1049 1050
			nand_update_bbt(mtd, (loff_t)(block - 1) <<
					this->bbt_erase_shift);
L
Linus Torvalds 已提交
1051 1052 1053
	}
}

1054 1055
/**
 * verify_bbt_descr - verify the bad block description
1056 1057
 * @mtd: MTD device structure
 * @bd: the table to verify
1058 1059 1060 1061 1062 1063 1064
 *
 * 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;
1065 1066
	u32 pattern_len;
	u32 bits;
1067 1068 1069 1070
	u32 table_size;

	if (!bd)
		return;
1071 1072 1073 1074

	pattern_len = bd->len;
	bits = bd->options & NAND_BBT_NRBITS_MSK;

1075
	BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
1076
			!(this->bbt_options & NAND_BBT_USE_FLASH));
1077 1078 1079 1080 1081 1082
	BUG_ON(!bits);

	if (bd->options & NAND_BBT_VERSION)
		pattern_len++;

	if (bd->options & NAND_BBT_NO_OOB) {
1083
		BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
1084
		BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
		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));
}

L
Linus Torvalds 已提交
1102 1103
/**
 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1104 1105
 * @mtd: MTD device structure
 * @bd: descriptor for the good/bad block search pattern
L
Linus Torvalds 已提交
1106
 *
1107 1108 1109
 * 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.
L
Linus Torvalds 已提交
1110
 *
1111 1112 1113
 * The bad block table memory is allocated here. It must be freed by calling
 * the nand_free_bbt function.
 */
1114
int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
L
Linus Torvalds 已提交
1115 1116 1117 1118 1119 1120 1121 1122
{
	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);
1123 1124 1125 1126
	/*
	 * Allocate memory (2bit per block) and clear the memory bad block
	 * table.
	 */
1127
	this->bbt = kzalloc(len, GFP_KERNEL);
1128
	if (!this->bbt)
L
Linus Torvalds 已提交
1129 1130
		return -ENOMEM;

1131 1132 1133
	/*
	 * If no primary table decriptor is given, scan the device to build a
	 * memory based bad block table.
L
Linus Torvalds 已提交
1134
	 */
1135 1136
	if (!td) {
		if ((res = nand_memory_bbt(mtd, bd))) {
1137
			pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
1138
			kfree(this->bbt);
1139 1140 1141 1142
			this->bbt = NULL;
		}
		return res;
	}
1143 1144
	verify_bbt_descr(mtd, td);
	verify_bbt_descr(mtd, md);
L
Linus Torvalds 已提交
1145 1146 1147 1148

	/* Allocate a temporary buffer for one eraseblock incl. oob */
	len = (1 << this->bbt_erase_shift);
	len += (len >> this->page_shift) * mtd->oobsize;
1149
	buf = vmalloc(len);
L
Linus Torvalds 已提交
1150
	if (!buf) {
1151
		kfree(this->bbt);
L
Linus Torvalds 已提交
1152 1153 1154
		this->bbt = NULL;
		return -ENOMEM;
	}
1155

1156
	/* Is the bbt at a given page? */
L
Linus Torvalds 已提交
1157
	if (td->options & NAND_BBT_ABSPAGE) {
1158
		read_abs_bbts(mtd, buf, td, md);
1159
	} else {
L
Linus Torvalds 已提交
1160
		/* Search the bad block table using a pattern in oob */
1161
		search_read_bbts(mtd, buf, td, md);
1162
	}
L
Linus Torvalds 已提交
1163

1164
	res = check_create(mtd, buf, bd);
1165

L
Linus Torvalds 已提交
1166
	/* Prevent the bbt regions from erasing / writing */
1167
	mark_bbt_region(mtd, td);
L
Linus Torvalds 已提交
1168
	if (md)
1169
		mark_bbt_region(mtd, md);
1170

1171
	vfree(buf);
L
Linus Torvalds 已提交
1172 1173 1174 1175
	return res;
}

/**
1176
 * nand_update_bbt - update bad block table(s)
1177 1178
 * @mtd: MTD device structure
 * @offs: the offset of the newly marked block
L
Linus Torvalds 已提交
1179
 *
1180 1181
 * The function updates the bad block table(s).
 */
1182
static int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
L
Linus Torvalds 已提交
1183 1184
{
	struct nand_chip *this = mtd->priv;
1185
	int len, res = 0;
L
Linus Torvalds 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
	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;
1197
	buf = kmalloc(len, GFP_KERNEL);
1198
	if (!buf)
L
Linus Torvalds 已提交
1199 1200
		return -ENOMEM;

1201
	/* Do we have a bbt per chip? */
L
Linus Torvalds 已提交
1202
	if (td->options & NAND_BBT_PERCHIP) {
1203
		chip = (int)(offs >> this->chip_shift);
L
Linus Torvalds 已提交
1204 1205 1206 1207 1208 1209 1210 1211
		chipsel = chip;
	} else {
		chip = 0;
		chipsel = -1;
	}

	td->version[chip]++;
	if (md)
1212
		md->version[chip]++;
L
Linus Torvalds 已提交
1213

1214
	/* Write the bad block table to the device? */
1215
	if (td->options & NAND_BBT_WRITE) {
1216
		res = write_bbt(mtd, buf, td, md, chipsel);
L
Linus Torvalds 已提交
1217 1218 1219
		if (res < 0)
			goto out;
	}
1220
	/* Write the mirror bad block table to the device? */
1221
	if (md && (md->options & NAND_BBT_WRITE)) {
1222
		res = write_bbt(mtd, buf, md, td, chipsel);
L
Linus Torvalds 已提交
1223 1224
	}

1225 1226
 out:
	kfree(buf);
L
Linus Torvalds 已提交
1227 1228 1229
	return res;
}

1230 1231 1232 1233
/*
 * Define some generic bad / good block scan pattern which are used
 * while scanning a device for factory marked good / bad blocks.
 */
L
Linus Torvalds 已提交
1234 1235
static uint8_t scan_ff_pattern[] = { 0xff, 0xff };

1236
/* Generic flash bbt descriptors */
L
Linus Torvalds 已提交
1237 1238 1239 1240
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 = {
1241
	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
L
Linus Torvalds 已提交
1242 1243 1244 1245
		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
	.offs =	8,
	.len = 4,
	.veroffs = 12,
1246
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
L
Linus Torvalds 已提交
1247 1248 1249 1250
	.pattern = bbt_pattern
};

static struct nand_bbt_descr bbt_mirror_descr = {
1251
	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
L
Linus Torvalds 已提交
1252 1253 1254 1255
		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
	.offs =	8,
	.len = 4,
	.veroffs = 12,
1256
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
L
Linus Torvalds 已提交
1257 1258 1259
	.pattern = mirror_pattern
};

1260
static struct nand_bbt_descr bbt_main_no_oob_descr = {
1261 1262 1263 1264 1265
	.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,
1266
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1267 1268 1269
	.pattern = bbt_pattern
};

1270
static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
1271 1272 1273 1274 1275
	.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,
1276
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1277 1278 1279
	.pattern = mirror_pattern
};

1280
#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
1281
/**
1282
 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
1283
 * @this: NAND chip to create descriptor for
1284 1285
 *
 * This function allocates and initializes a nand_bbt_descr for BBM detection
1286
 * based on the properties of @this. The new descriptor is stored in
1287 1288 1289
 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
 * passed to this function.
 */
1290
static int nand_create_badblock_pattern(struct nand_chip *this)
1291 1292 1293
{
	struct nand_bbt_descr *bd;
	if (this->badblock_pattern) {
1294
		pr_warn("Bad block pattern already allocated; not replacing\n");
1295 1296 1297
		return -EINVAL;
	}
	bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1298
	if (!bd)
1299
		return -ENOMEM;
1300
	bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
1301 1302 1303 1304 1305 1306 1307 1308
	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;
}

L
Linus Torvalds 已提交
1309
/**
1310
 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
1311
 * @mtd: MTD device structure
L
Linus Torvalds 已提交
1312
 *
1313 1314 1315
 * This function selects the default bad block table support for the device and
 * calls the nand_scan_bbt function.
 */
1316
int nand_default_bbt(struct mtd_info *mtd)
L
Linus Torvalds 已提交
1317 1318
{
	struct nand_chip *this = mtd->priv;
1319

1320
	/* Is a flash based bad block table requested? */
1321
	if (this->bbt_options & NAND_BBT_USE_FLASH) {
1322 1323
		/* Use the default pattern descriptors */
		if (!this->bbt_td) {
1324
			if (this->bbt_options & NAND_BBT_NO_OOB) {
1325 1326
				this->bbt_td = &bbt_main_no_oob_descr;
				this->bbt_md = &bbt_mirror_no_oob_descr;
1327 1328 1329 1330
			} else {
				this->bbt_td = &bbt_main_descr;
				this->bbt_md = &bbt_mirror_descr;
			}
L
Linus Torvalds 已提交
1331 1332 1333 1334 1335
		}
	} else {
		this->bbt_td = NULL;
		this->bbt_md = NULL;
	}
1336 1337

	if (!this->badblock_pattern)
1338
		nand_create_badblock_pattern(this);
1339

1340
	return nand_scan_bbt(mtd, this->badblock_pattern);
L
Linus Torvalds 已提交
1341 1342 1343
}

/**
1344
 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1345 1346 1347 1348
 * @mtd: MTD device structure
 * @offs: offset in the device
 * @allowbbt: allow access to bad block table region
 */
1349
int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
L
Linus Torvalds 已提交
1350 1351
{
	struct nand_chip *this = mtd->priv;
B
Brian Norris 已提交
1352
	int block, res;
1353

1354 1355
	block = (int)(offs >> this->bbt_erase_shift);
	res = bbt_get_entry(this, block);
L
Linus Torvalds 已提交
1356

1357 1358
	pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: "
			"(block %d) 0x%02x\n",
1359
			(unsigned int)offs, block, res);
L
Linus Torvalds 已提交
1360

B
Brian Norris 已提交
1361
	switch (res) {
1362
	case BBT_BLOCK_GOOD:
1363
		return 0;
1364
	case BBT_BLOCK_WORN:
1365
		return 1;
1366
	case BBT_BLOCK_RESERVED:
1367
		return allowbbt ? 0 : 1;
L
Linus Torvalds 已提交
1368 1369 1370 1371
	}
	return 1;
}

1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
/**
 * 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;
}

1394
EXPORT_SYMBOL(nand_scan_bbt);