nand_bbt.c 38.6 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 113 114 115
 * Check for a pattern at the given place. Used to search bad block tables and
 * good / bad block identifiers. If the SCAN_EMPTY option is set then check, if
 * all bytes except the pattern area contain 0xff.
 */
116
static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
L
Linus Torvalds 已提交
117
{
B
Brian Norris 已提交
118
	int end = 0;
L
Linus Torvalds 已提交
119 120
	uint8_t *p = buf;

121 122 123
	if (td->options & NAND_BBT_NO_OOB)
		return check_pattern_no_oob(buf, td);

124
	end = paglen + td->offs;
B
Brian Norris 已提交
125 126 127
	if (td->options & NAND_BBT_SCANEMPTY)
		if (memchr_inv(p, 0xff, end))
			return -1;
128
	p += end;
129

L
Linus Torvalds 已提交
130
	/* Compare the pattern */
131 132
	if (memcmp(p, td->pattern, td->len))
		return -1;
133

L
Linus Torvalds 已提交
134
	if (td->options & NAND_BBT_SCANEMPTY) {
135 136
		p += td->len;
		end += td->len;
B
Brian Norris 已提交
137 138
		if (memchr_inv(p, 0xff, len - end))
			return -1;
L
Linus Torvalds 已提交
139 140 141 142
	}
	return 0;
}

143
/**
144
 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
145 146
 * @buf: the buffer to search
 * @td:	search pattern descriptor
147
 *
148 149 150 151
 * 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.
 */
152
static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
153 154
{
	/* Compare the pattern */
B
Brian Norris 已提交
155 156
	if (memcmp(buf + td->offs, td->pattern, td->len))
		return -1;
157 158 159
	return 0;
}

160 161
/**
 * add_marker_len - compute the length of the marker in data area
162
 * @td: BBT descriptor used for computation
163
 *
164
 * The length will be 0 if the marker is located in OOB area.
165 166 167 168 169 170 171 172 173 174 175 176 177 178
 */
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 已提交
179 180
/**
 * read_bbt - [GENERIC] Read the bad block table starting from page
181 182 183 184
 * @mtd: MTD device structure
 * @buf: temporary buffer
 * @page: the starting page
 * @num: the number of bbt descriptors to read
185
 * @td: the bbt describtion table
186
 * @offs: block number offset in the table
L
Linus Torvalds 已提交
187 188 189
 *
 * Read the bad block table starting from page.
 */
190
static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
191
		struct nand_bbt_descr *td, int offs)
L
Linus Torvalds 已提交
192
{
193
	int res, ret = 0, i, j, act = 0;
L
Linus Torvalds 已提交
194 195 196
	struct nand_chip *this = mtd->priv;
	size_t retlen, len, totlen;
	loff_t from;
197
	int bits = td->options & NAND_BBT_NRBITS_MSK;
B
Brian Norris 已提交
198
	uint8_t msk = (uint8_t)((1 << bits) - 1);
199
	u32 marker_len;
200
	int reserved_block_code = td->reserved_block_code;
L
Linus Torvalds 已提交
201 202

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

L
Linus Torvalds 已提交
206
	while (totlen) {
B
Brian Norris 已提交
207
		len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
208 209 210 211 212 213 214 215 216
		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;
		}
217
		res = mtd_read(mtd, from, len, &retlen, buf);
L
Linus Torvalds 已提交
218
		if (res < 0) {
219 220 221 222 223 224 225 226 227 228
			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 已提交
229 230
				return res;
			}
231
		}
L
Linus Torvalds 已提交
232 233 234 235

		/* Analyse data */
		for (i = 0; i < len; i++) {
			uint8_t dat = buf[i];
236
			for (j = 0; j < 8; j += bits, act++) {
L
Linus Torvalds 已提交
237 238 239
				uint8_t tmp = (dat >> j) & msk;
				if (tmp == msk)
					continue;
240
				if (reserved_block_code && (tmp == reserved_block_code)) {
241
					pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
242 243 244
						 (loff_t)(offs + act) <<
						 this->bbt_erase_shift);
					bbt_mark_entry(this, offs + act,
245
							BBT_BLOCK_RESERVED);
246
					mtd->ecc_stats.bbtblocks++;
L
Linus Torvalds 已提交
247 248
					continue;
				}
249 250
				/*
				 * Leave it for now, if it's matured we can
251
				 * move this message to pr_debug.
252
				 */
253
				pr_info("nand_read_bbt: bad block at 0x%012llx\n",
254 255
					 (loff_t)(offs + act) <<
					 this->bbt_erase_shift);
256
				/* Factory marked bad or worn out? */
L
Linus Torvalds 已提交
257
				if (tmp == 0)
258
					bbt_mark_entry(this, offs + act,
259
							BBT_BLOCK_FACTORY_BAD);
L
Linus Torvalds 已提交
260
				else
261
					bbt_mark_entry(this, offs + act,
262
							BBT_BLOCK_WORN);
263
				mtd->ecc_stats.badblocks++;
264
			}
L
Linus Torvalds 已提交
265 266 267 268
		}
		totlen -= len;
		from += len;
	}
269
	return ret;
L
Linus Torvalds 已提交
270 271 272 273
}

/**
 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
274 275 276
 * @mtd: MTD device structure
 * @buf: temporary buffer
 * @td: descriptor for the bad block table
B
Brian Norris 已提交
277
 * @chip: read the table for a specific chip, -1 read all chips; applies only if
278
 *        NAND_BBT_PERCHIP option is set
L
Linus Torvalds 已提交
279
 *
280 281 282
 * Read the bad block table for all chips starting at a given page. We assume
 * that the bbt bits are in consecutive order.
 */
283
static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
L
Linus Torvalds 已提交
284 285 286 287 288 289 290 291
{
	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)
292 293 294
				res = read_bbt(mtd, buf, td->pages[i],
					this->chipsize >> this->bbt_erase_shift,
					td, offs);
L
Linus Torvalds 已提交
295 296
			if (res)
				return res;
297
			offs += this->chipsize >> this->bbt_erase_shift;
L
Linus Torvalds 已提交
298 299
		}
	} else {
300 301
		res = read_bbt(mtd, buf, td->pages[0],
				mtd->size >> this->bbt_erase_shift, td, 0);
L
Linus Torvalds 已提交
302 303 304 305 306 307
		if (res)
			return res;
	}
	return 0;
}

308
/* BBT marker is in the first page, no OOB */
309
static int scan_read_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
310 311 312 313 314 315 316 317 318
			 struct nand_bbt_descr *td)
{
	size_t retlen;
	size_t len;

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

319
	return mtd_read(mtd, offs, len, &retlen, buf);
320 321
}

322
/**
323
 * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
324 325 326 327 328 329 330 331 332
 * @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.
 */
333
static int scan_read_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
334 335 336
			 size_t len)
{
	struct mtd_oob_ops ops;
337
	int res, ret = 0;
338

339
	ops.mode = MTD_OPS_PLACE_OOB;
340 341 342
	ops.ooboffs = 0;
	ops.ooblen = mtd->oobsize;

343
	while (len > 0) {
B
Brian Norris 已提交
344 345 346
		ops.datbuf = buf;
		ops.len = min(len, (size_t)mtd->writesize);
		ops.oobbuf = buf + ops.len;
347

348
		res = mtd_read_oob(mtd, offs, &ops);
349 350 351 352 353 354
		if (res) {
			if (!mtd_is_bitflip_or_eccerr(res))
				return res;
			else if (mtd_is_eccerr(res) || !ret)
				ret = res;
		}
355 356 357

		buf += mtd->oobsize + mtd->writesize;
		len -= mtd->writesize;
D
Dmitry Maluka 已提交
358
		offs += mtd->writesize;
359
	}
360
	return ret;
361 362
}

363
static int scan_read(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
364 365 366
			 size_t len, struct nand_bbt_descr *td)
{
	if (td->options & NAND_BBT_NO_OOB)
367
		return scan_read_data(mtd, buf, offs, td);
368
	else
369
		return scan_read_oob(mtd, buf, offs, len);
370 371
}

372
/* Scan write data with oob to flash */
373 374 375 376 377
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;

378
	ops.mode = MTD_OPS_PLACE_OOB;
379 380 381 382 383 384
	ops.ooboffs = 0;
	ops.ooblen = mtd->oobsize;
	ops.datbuf = buf;
	ops.oobbuf = oob;
	ops.len = len;

385
	return mtd_write_oob(mtd, offs, &ops);
386 387
}

388 389 390 391 392 393 394 395 396
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 已提交
397 398
/**
 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
399 400 401 402
 * @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 已提交
403
 *
404 405 406
 * Read the bad block table(s) for all chips starting at a given page. We
 * assume that the bbt bits are in consecutive order.
 */
407 408
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 已提交
409 410 411
{
	struct nand_chip *this = mtd->priv;

412
	/* Read the primary version, if available */
L
Linus Torvalds 已提交
413
	if (td->options & NAND_BBT_VERSION) {
414
		scan_read(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
415 416
			      mtd->writesize, td);
		td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
417
		pr_info("Bad block table at page %d, version 0x%02X\n",
418
			 td->pages[0], td->version[0]);
L
Linus Torvalds 已提交
419 420
	}

421
	/* Read the mirror version, if available */
L
Linus Torvalds 已提交
422
	if (md && (md->options & NAND_BBT_VERSION)) {
423
		scan_read(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
424
			      mtd->writesize, md);
425
		md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
426
		pr_info("Bad block table at page %d, version 0x%02X\n",
427
			 md->pages[0], md->version[0]);
L
Linus Torvalds 已提交
428 429 430
	}
}

431
/* Scan a given block full */
432 433
static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
			   loff_t offs, uint8_t *buf, size_t readlen,
434
			   int scanlen, int numpages)
435 436 437
{
	int ret, j;

438
	ret = scan_read_oob(mtd, buf, offs, readlen);
439
	/* Ignore ECC errors when checking for BBM */
440
	if (ret && !mtd_is_bitflip_or_eccerr(ret))
441 442
		return ret;

443
	for (j = 0; j < numpages; j++, buf += scanlen) {
444 445 446 447 448 449
		if (check_pattern(buf, scanlen, mtd->writesize, bd))
			return 1;
	}
	return 0;
}

450
/* Scan a given block partially */
451
static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
452
			   loff_t offs, uint8_t *buf, int numpages)
453 454 455 456 457 458 459 460
{
	struct mtd_oob_ops ops;
	int j, ret;

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

463
	for (j = 0; j < numpages; j++) {
464
		/*
465 466
		 * Read the full oob until read_oob is fixed to handle single
		 * byte reads for 16 bit buswidth.
467
		 */
468
		ret = mtd_read_oob(mtd, offs, &ops);
469
		/* Ignore ECC errors when checking for BBM */
470
		if (ret && !mtd_is_bitflip_or_eccerr(ret))
471 472 473 474 475 476 477 478 479 480
			return ret;

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

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

L
Linus Torvalds 已提交
481 482
/**
 * create_bbt - [GENERIC] Create a bad block table by scanning the device
483 484 485 486 487
 * @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 已提交
488
 *
489 490
 * Create a bad block table by scanning the device for the given good/bad block
 * identify pattern.
L
Linus Torvalds 已提交
491
 */
492 493
static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
	struct nand_bbt_descr *bd, int chip)
L
Linus Torvalds 已提交
494 495
{
	struct nand_chip *this = mtd->priv;
496
	int i, numblocks, numpages, scanlen;
L
Linus Torvalds 已提交
497 498
	int startblock;
	loff_t from;
499
	size_t readlen;
L
Linus Torvalds 已提交
500

501
	pr_info("Scanning device for bad blocks\n");
L
Linus Torvalds 已提交
502 503

	if (bd->options & NAND_BBT_SCANALLPAGES)
504
		numpages = 1 << (this->bbt_erase_shift - this->page_shift);
505
	else if (bd->options & NAND_BBT_SCAN2NDPAGE)
506
		numpages = 2;
507
	else
508
		numpages = 1;
509 510 511

	if (!(bd->options & NAND_BBT_SCANEMPTY)) {
		/* We need only read few bytes from the OOB area */
512
		scanlen = 0;
513 514
		readlen = bd->len;
	} else {
515
		/* Full page content should be read */
J
Joern Engel 已提交
516
		scanlen = mtd->writesize + mtd->oobsize;
517
		readlen = numpages * mtd->writesize;
518
	}
L
Linus Torvalds 已提交
519 520

	if (chip == -1) {
521
		numblocks = mtd->size >> this->bbt_erase_shift;
L
Linus Torvalds 已提交
522 523 524 525
		startblock = 0;
		from = 0;
	} else {
		if (chip >= this->numchips) {
526
			pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
527
			       chip + 1, this->numchips);
528
			return -EINVAL;
L
Linus Torvalds 已提交
529
		}
530
		numblocks = this->chipsize >> this->bbt_erase_shift;
L
Linus Torvalds 已提交
531 532
		startblock = chip * numblocks;
		numblocks += startblock;
533
		from = (loff_t)startblock << this->bbt_erase_shift;
L
Linus Torvalds 已提交
534
	}
535

536
	if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
537
		from += mtd->erasesize - (mtd->writesize * numpages);
538

539
	for (i = startblock; i < numblocks; i++) {
540
		int ret;
541

542 543
		BUG_ON(bd->options & NAND_BBT_NO_OOB);

544 545
		if (bd->options & NAND_BBT_SCANALLPAGES)
			ret = scan_block_full(mtd, bd, from, buf, readlen,
546
					      scanlen, numpages);
547
		else
548
			ret = scan_block_fast(mtd, bd, from, buf, numpages);
549 550 551 552 553

		if (ret < 0)
			return ret;

		if (ret) {
554
			bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
555
			pr_warn("Bad eraseblock %d at 0x%012llx\n",
556
				i, (unsigned long long)from);
557
			mtd->ecc_stats.badblocks++;
L
Linus Torvalds 已提交
558
		}
559

L
Linus Torvalds 已提交
560 561
		from += (1 << this->bbt_erase_shift);
	}
562
	return 0;
L
Linus Torvalds 已提交
563 564 565 566
}

/**
 * search_bbt - [GENERIC] scan the device for a specific bad block table
567 568 569
 * @mtd: MTD device structure
 * @buf: temporary buffer
 * @td: descriptor for the bad block table
L
Linus Torvalds 已提交
570
 *
571 572 573 574 575 576
 * 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 已提交
577
 *
578
 * The bbt ident pattern resides in the oob area of the first page in a block.
L
Linus Torvalds 已提交
579
 */
580
static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
L
Linus Torvalds 已提交
581 582 583 584
{
	struct nand_chip *this = mtd->priv;
	int i, chips;
	int bits, startblock, block, dir;
J
Joern Engel 已提交
585
	int scanlen = mtd->writesize + mtd->oobsize;
L
Linus Torvalds 已提交
586
	int bbtblocks;
587
	int blocktopage = this->bbt_erase_shift - this->page_shift;
L
Linus Torvalds 已提交
588

589
	/* Search direction top -> down? */
L
Linus Torvalds 已提交
590
	if (td->options & NAND_BBT_LASTBLOCK) {
591
		startblock = (mtd->size >> this->bbt_erase_shift) - 1;
L
Linus Torvalds 已提交
592 593
		dir = -1;
	} else {
594
		startblock = 0;
L
Linus Torvalds 已提交
595
		dir = 1;
596 597
	}

598
	/* Do we have a bbt per chip? */
L
Linus Torvalds 已提交
599 600 601 602 603 604 605 606
	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;
	}
607

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

L
Linus Torvalds 已提交
611 612
	for (i = 0; i < chips; i++) {
		/* Reset version information */
613
		td->version[i] = 0;
L
Linus Torvalds 已提交
614 615 616
		td->pages[i] = -1;
		/* Scan the maximum number of blocks */
		for (block = 0; block < td->maxblocks; block++) {
617

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

L
Linus Torvalds 已提交
621
			/* Read first page */
622
			scan_read(mtd, buf, offs, mtd->writesize, td);
J
Joern Engel 已提交
623
			if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
624
				td->pages[i] = actblock << blocktopage;
L
Linus Torvalds 已提交
625
				if (td->options & NAND_BBT_VERSION) {
626 627
					offs = bbt_get_ver_offs(mtd, td);
					td->version[i] = buf[offs];
L
Linus Torvalds 已提交
628 629 630 631 632 633 634 635 636
				}
				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)
637
			pr_warn("Bad block table not found for chip %d\n", i);
L
Linus Torvalds 已提交
638
		else
639 640
			pr_info("Bad block table found at page %d, version "
				 "0x%02X\n", td->pages[i], td->version[i]);
L
Linus Torvalds 已提交
641
	}
642
	return 0;
L
Linus Torvalds 已提交
643 644 645 646
}

/**
 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
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
L
Linus Torvalds 已提交
651
 *
652 653
 * Search and read the bad block table(s).
 */
654 655 656
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 已提交
657 658
{
	/* Search the primary table */
659
	search_bbt(mtd, buf, td);
660

L
Linus Torvalds 已提交
661 662
	/* Search the mirror table */
	if (md)
663
		search_bbt(mtd, buf, md);
L
Linus Torvalds 已提交
664 665
}

666
/**
L
Linus Torvalds 已提交
667
 * write_bbt - [GENERIC] (Re)write the bad block table
668 669 670 671 672
 * @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 已提交
673
 *
674 675
 * (Re)write the bad block table.
 */
676
static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
677 678
		     struct nand_bbt_descr *td, struct nand_bbt_descr *md,
		     int chipsel)
L
Linus Torvalds 已提交
679 680 681
{
	struct nand_chip *this = mtd->priv;
	struct erase_info einfo;
682
	int i, res, chip = 0;
L
Linus Torvalds 已提交
683
	int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
684
	int nrchips, pageoffs, ooboffs;
L
Linus Torvalds 已提交
685 686
	uint8_t msk[4];
	uint8_t rcode = td->reserved_block_code;
687
	size_t retlen, len = 0;
L
Linus Torvalds 已提交
688
	loff_t to;
689 690 691 692 693
	struct mtd_oob_ops ops;

	ops.ooblen = mtd->oobsize;
	ops.ooboffs = 0;
	ops.datbuf = NULL;
694
	ops.mode = MTD_OPS_PLACE_OOB;
L
Linus Torvalds 已提交
695 696 697

	if (!rcode)
		rcode = 0xff;
698
	/* Write bad block table per chip rather than per device? */
L
Linus Torvalds 已提交
699
	if (td->options & NAND_BBT_PERCHIP) {
700
		numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
701
		/* Full device write or specific chip? */
L
Linus Torvalds 已提交
702 703 704 705 706 707 708
		if (chipsel == -1) {
			nrchips = this->numchips;
		} else {
			nrchips = chipsel + 1;
			chip = chipsel;
		}
	} else {
709
		numblocks = (int)(mtd->size >> this->bbt_erase_shift);
L
Linus Torvalds 已提交
710
		nrchips = 1;
711 712
	}

L
Linus Torvalds 已提交
713 714
	/* Loop through the chips */
	for (; chip < nrchips; chip++) {
715 716
		/*
		 * There was already a version of the table, reuse the page
717
		 * This applies for absolute placement too, as we have the
L
Linus Torvalds 已提交
718 719 720 721 722
		 * page nr. in td->pages.
		 */
		if (td->pages[chip] != -1) {
			page = td->pages[chip];
			goto write;
723
		}
L
Linus Torvalds 已提交
724

725 726 727 728
		/*
		 * Automatic placement of the bad block table. Search direction
		 * top -> down?
		 */
L
Linus Torvalds 已提交
729 730 731 732 733 734
		if (td->options & NAND_BBT_LASTBLOCK) {
			startblock = numblocks * (chip + 1) - 1;
			dir = -1;
		} else {
			startblock = chip * numblocks;
			dir = 1;
735
		}
L
Linus Torvalds 已提交
736 737 738 739

		for (i = 0; i < td->maxblocks; i++) {
			int block = startblock + dir * i;
			/* Check, if the block is bad */
740 741 742
			switch (bbt_get_entry(this, block)) {
			case BBT_BLOCK_WORN:
			case BBT_BLOCK_FACTORY_BAD:
L
Linus Torvalds 已提交
743 744
				continue;
			}
745 746
			page = block <<
				(this->bbt_erase_shift - this->page_shift);
L
Linus Torvalds 已提交
747 748 749 750
			/* Check, if the block is used by the mirror table */
			if (!md || md->pages[chip] != page)
				goto write;
		}
751
		pr_err("No space left to write bad block table\n");
L
Linus Torvalds 已提交
752
		return -ENOSPC;
753
	write:
L
Linus Torvalds 已提交
754 755 756

		/* Set up shift count and masks for the flash table */
		bits = td->options & NAND_BBT_NRBITS_MSK;
757
		msk[2] = ~rcode;
L
Linus Torvalds 已提交
758
		switch (bits) {
759 760 761 762 763 764 765 766 767 768 769 770
		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 已提交
771 772
		default: return -EINVAL;
		}
773

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

776
		/* Must we save the block contents? */
L
Linus Torvalds 已提交
777 778
		if (td->options & NAND_BBT_SAVECONTENT) {
			/* Make it block aligned */
B
Brian Norris 已提交
779
			to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1));
L
Linus Torvalds 已提交
780
			len = 1 << this->bbt_erase_shift;
781
			res = mtd_read(mtd, to, len, &retlen, buf);
L
Linus Torvalds 已提交
782 783
			if (res < 0) {
				if (retlen != len) {
784 785
					pr_info("nand_bbt: error reading block "
						"for writing the bad block table\n");
L
Linus Torvalds 已提交
786 787
					return res;
				}
788 789
				pr_warn("nand_bbt: ECC error while reading "
					"block for writing bad block table\n");
L
Linus Torvalds 已提交
790
			}
791
			/* Read oob data */
792
			ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
793
			ops.oobbuf = &buf[len];
794
			res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
795
			if (res < 0 || ops.oobretlen != ops.ooblen)
796 797
				goto outerr;

L
Linus Torvalds 已提交
798 799 800 801
			/* 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 已提交
802
			memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
803 804
			ooboffs = len + (pageoffs * mtd->oobsize);

805 806 807
		} else if (td->options & NAND_BBT_NO_OOB) {
			ooboffs = 0;
			offs = td->len;
808
			/* The version byte */
809 810 811
			if (td->options & NAND_BBT_VERSION)
				offs++;
			/* Calc length */
B
Brian Norris 已提交
812
			len = (size_t)(numblocks >> sft);
813
			len += offs;
814
			/* Make it page aligned! */
815 816 817 818 819
			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 已提交
820 821
		} else {
			/* Calc length */
B
Brian Norris 已提交
822
			len = (size_t)(numblocks >> sft);
823
			/* Make it page aligned! */
824
			len = ALIGN(len, mtd->writesize);
L
Linus Torvalds 已提交
825
			/* Preset the buffer with 0xff */
826 827
			memset(buf, 0xff, len +
			       (len >> this->page_shift)* mtd->oobsize);
L
Linus Torvalds 已提交
828
			offs = 0;
829
			ooboffs = len;
L
Linus Torvalds 已提交
830
			/* Pattern is located in oob area of first page */
831
			memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
L
Linus Torvalds 已提交
832
		}
833

834 835 836
		if (td->options & NAND_BBT_VERSION)
			buf[ooboffs + td->veroffs] = td->version[chip];

837
		/* Walk through the memory table */
838
		for (i = 0; i < numblocks; i++) {
L
Linus Torvalds 已提交
839
			uint8_t dat;
840 841 842 843
			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 已提交
844
		}
845

846
		memset(&einfo, 0, sizeof(einfo));
L
Linus Torvalds 已提交
847
		einfo.mtd = mtd;
848
		einfo.addr = to;
L
Linus Torvalds 已提交
849
		einfo.len = 1 << this->bbt_erase_shift;
850
		res = nand_erase_nand(mtd, &einfo, 1);
851 852
		if (res < 0)
			goto outerr;
853

854 855 856
		res = scan_write_bbt(mtd, to, len, buf,
				td->options & NAND_BBT_NO_OOB ? NULL :
				&buf[len]);
857 858 859
		if (res < 0)
			goto outerr;

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

L
Linus Torvalds 已提交
863 864
		/* Mark it as used */
		td->pages[chip] = page;
865
	}
L
Linus Torvalds 已提交
866
	return 0;
867 868

 outerr:
869
	pr_warn("nand_bbt: error while writing bad block table %d\n", res);
870
	return res;
L
Linus Torvalds 已提交
871 872 873 874
}

/**
 * nand_memory_bbt - [GENERIC] create a memory based bad block table
875 876
 * @mtd: MTD device structure
 * @bd: descriptor for the good/bad block search pattern
L
Linus Torvalds 已提交
877
 *
878 879 880
 * The function creates a memory based bbt by scanning the device for
 * manufacturer / software marked good / bad blocks.
 */
881
static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
L
Linus Torvalds 已提交
882 883 884
{
	struct nand_chip *this = mtd->priv;

885
	bd->options &= ~NAND_BBT_SCANEMPTY;
886
	return create_bbt(mtd, this->buffers->databuf, bd, -1);
L
Linus Torvalds 已提交
887 888 889
}

/**
890
 * check_create - [GENERIC] create and write bbt(s) if necessary
891 892 893
 * @mtd: MTD device structure
 * @buf: temporary buffer
 * @bd: descriptor for the good/bad block search pattern
L
Linus Torvalds 已提交
894
 *
895 896 897 898 899
 * 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.
 */
900
static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
L
Linus Torvalds 已提交
901
{
902
	int i, chips, writeops, create, chipsel, res, res2;
L
Linus Torvalds 已提交
903 904 905 906 907
	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;

908
	/* Do we have a bbt per chip? */
909
	if (td->options & NAND_BBT_PERCHIP)
L
Linus Torvalds 已提交
910
		chips = this->numchips;
911
	else
L
Linus Torvalds 已提交
912
		chips = 1;
913

L
Linus Torvalds 已提交
914 915
	for (i = 0; i < chips; i++) {
		writeops = 0;
916
		create = 0;
L
Linus Torvalds 已提交
917 918
		rd = NULL;
		rd2 = NULL;
919
		res = res2 = 0;
920
		/* Per chip or per device? */
L
Linus Torvalds 已提交
921
		chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
922
		/* Mirrored table available? */
L
Linus Torvalds 已提交
923 924
		if (md) {
			if (td->pages[i] == -1 && md->pages[i] == -1) {
925
				create = 1;
L
Linus Torvalds 已提交
926
				writeops = 0x03;
927
			} else if (td->pages[i] == -1) {
928
				rd = md;
B
Brian Norris 已提交
929
				writeops = 0x01;
930
			} else if (md->pages[i] == -1) {
L
Linus Torvalds 已提交
931
				rd = td;
B
Brian Norris 已提交
932
				writeops = 0x02;
933
			} else if (td->version[i] == md->version[i]) {
L
Linus Torvalds 已提交
934 935 936
				rd = td;
				if (!(td->options & NAND_BBT_VERSION))
					rd2 = md;
937
			} else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
L
Linus Torvalds 已提交
938
				rd = td;
B
Brian Norris 已提交
939
				writeops = 0x02;
L
Linus Torvalds 已提交
940 941
			} else {
				rd = md;
B
Brian Norris 已提交
942
				writeops = 0x01;
L
Linus Torvalds 已提交
943 944 945
			}
		} else {
			if (td->pages[i] == -1) {
946
				create = 1;
L
Linus Torvalds 已提交
947
				writeops = 0x01;
948 949
			} else {
				rd = td;
L
Linus Torvalds 已提交
950 951
			}
		}
952

953 954 955 956 957 958 959 960 961 962 963 964 965 966
		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;
		}

967
		/* Read back first? */
968 969 970 971 972
		if (rd) {
			res = read_abs_bbt(mtd, buf, rd, chipsel);
			if (mtd_is_eccerr(res)) {
				/* Mark table as invalid */
				rd->pages[i] = -1;
973
				rd->version[i] = 0;
974 975 976 977
				i--;
				continue;
			}
		}
978
		/* If they weren't versioned, read both */
979 980 981 982 983
		if (rd2) {
			res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
			if (mtd_is_eccerr(res2)) {
				/* Mark table as invalid */
				rd2->pages[i] = -1;
984
				rd2->version[i] = 0;
985 986 987 988 989 990 991 992
				i--;
				continue;
			}
		}

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

994 995 996 997 998 999
		/* Update version numbers before writing */
		if (md) {
			td->version[i] = max(td->version[i], md->version[i]);
			md->version[i] = td->version[i];
		}

1000
		/* Write the bad block table to the device? */
L
Linus Torvalds 已提交
1001
		if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1002
			res = write_bbt(mtd, buf, td, md, chipsel);
L
Linus Torvalds 已提交
1003 1004 1005
			if (res < 0)
				return res;
		}
1006

1007
		/* Write the mirror bad block table to the device? */
L
Linus Torvalds 已提交
1008
		if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1009
			res = write_bbt(mtd, buf, md, td, chipsel);
L
Linus Torvalds 已提交
1010 1011 1012 1013
			if (res < 0)
				return res;
		}
	}
1014
	return 0;
L
Linus Torvalds 已提交
1015 1016 1017
}

/**
1018
 * mark_bbt_regions - [GENERIC] mark the bad block table regions
1019 1020
 * @mtd: MTD device structure
 * @td: bad block table descriptor
L
Linus Torvalds 已提交
1021
 *
1022 1023 1024
 * The bad block table regions are marked as "bad" to prevent accidental
 * erasures / writes. The regions are identified by the mark 0x02.
 */
1025
static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
L
Linus Torvalds 已提交
1026 1027 1028
{
	struct nand_chip *this = mtd->priv;
	int i, j, chips, block, nrblocks, update;
1029
	uint8_t oldval;
L
Linus Torvalds 已提交
1030

1031
	/* Do we have a bbt per chip? */
L
Linus Torvalds 已提交
1032 1033 1034 1035 1036 1037
	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);
1038 1039
	}

L
Linus Torvalds 已提交
1040 1041 1042
	for (i = 0; i < chips; i++) {
		if ((td->options & NAND_BBT_ABSPAGE) ||
		    !(td->options & NAND_BBT_WRITE)) {
1043 1044
			if (td->pages[i] == -1)
				continue;
L
Linus Torvalds 已提交
1045
			block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
1046 1047
			oldval = bbt_get_entry(this, block);
			bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1048 1049
			if ((oldval != BBT_BLOCK_RESERVED) &&
					td->reserved_block_code)
1050 1051
				nand_update_bbt(mtd, (loff_t)block <<
						this->bbt_erase_shift);
L
Linus Torvalds 已提交
1052 1053 1054 1055 1056
			continue;
		}
		update = 0;
		if (td->options & NAND_BBT_LASTBLOCK)
			block = ((i + 1) * nrblocks) - td->maxblocks;
1057
		else
L
Linus Torvalds 已提交
1058 1059
			block = i * nrblocks;
		for (j = 0; j < td->maxblocks; j++) {
1060 1061
			oldval = bbt_get_entry(this, block);
			bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1062
			if (oldval != BBT_BLOCK_RESERVED)
1063
				update = 1;
1064
			block++;
1065
		}
1066 1067 1068 1069 1070
		/*
		 * 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 已提交
1071
		if (update && td->reserved_block_code)
1072 1073
			nand_update_bbt(mtd, (loff_t)(block - 1) <<
					this->bbt_erase_shift);
L
Linus Torvalds 已提交
1074 1075 1076
	}
}

1077 1078
/**
 * verify_bbt_descr - verify the bad block description
1079 1080
 * @mtd: MTD device structure
 * @bd: the table to verify
1081 1082 1083 1084 1085 1086 1087
 *
 * 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;
1088 1089
	u32 pattern_len;
	u32 bits;
1090 1091 1092 1093
	u32 table_size;

	if (!bd)
		return;
1094 1095 1096 1097

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

1098
	BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
1099
			!(this->bbt_options & NAND_BBT_USE_FLASH));
1100 1101 1102 1103 1104 1105
	BUG_ON(!bits);

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

	if (bd->options & NAND_BBT_NO_OOB) {
1106
		BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
1107
		BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
		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 已提交
1125 1126
/**
 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1127 1128
 * @mtd: MTD device structure
 * @bd: descriptor for the good/bad block search pattern
L
Linus Torvalds 已提交
1129
 *
1130 1131 1132
 * 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 已提交
1133
 *
1134 1135 1136
 * The bad block table memory is allocated here. It must be freed by calling
 * the nand_free_bbt function.
 */
1137
int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
L
Linus Torvalds 已提交
1138 1139 1140 1141 1142 1143 1144 1145
{
	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);
1146 1147 1148 1149
	/*
	 * Allocate memory (2bit per block) and clear the memory bad block
	 * table.
	 */
1150
	this->bbt = kzalloc(len, GFP_KERNEL);
1151
	if (!this->bbt)
L
Linus Torvalds 已提交
1152 1153
		return -ENOMEM;

1154 1155 1156
	/*
	 * If no primary table decriptor is given, scan the device to build a
	 * memory based bad block table.
L
Linus Torvalds 已提交
1157
	 */
1158 1159
	if (!td) {
		if ((res = nand_memory_bbt(mtd, bd))) {
1160
			pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
1161
			kfree(this->bbt);
1162 1163 1164 1165
			this->bbt = NULL;
		}
		return res;
	}
1166 1167
	verify_bbt_descr(mtd, td);
	verify_bbt_descr(mtd, md);
L
Linus Torvalds 已提交
1168 1169 1170 1171

	/* Allocate a temporary buffer for one eraseblock incl. oob */
	len = (1 << this->bbt_erase_shift);
	len += (len >> this->page_shift) * mtd->oobsize;
1172
	buf = vmalloc(len);
L
Linus Torvalds 已提交
1173
	if (!buf) {
1174
		kfree(this->bbt);
L
Linus Torvalds 已提交
1175 1176 1177
		this->bbt = NULL;
		return -ENOMEM;
	}
1178

1179
	/* Is the bbt at a given page? */
L
Linus Torvalds 已提交
1180
	if (td->options & NAND_BBT_ABSPAGE) {
1181
		read_abs_bbts(mtd, buf, td, md);
1182
	} else {
L
Linus Torvalds 已提交
1183
		/* Search the bad block table using a pattern in oob */
1184
		search_read_bbts(mtd, buf, td, md);
1185
	}
L
Linus Torvalds 已提交
1186

1187
	res = check_create(mtd, buf, bd);
1188

L
Linus Torvalds 已提交
1189
	/* Prevent the bbt regions from erasing / writing */
1190
	mark_bbt_region(mtd, td);
L
Linus Torvalds 已提交
1191
	if (md)
1192
		mark_bbt_region(mtd, md);
1193

1194
	vfree(buf);
L
Linus Torvalds 已提交
1195 1196 1197 1198
	return res;
}

/**
1199
 * nand_update_bbt - update bad block table(s)
1200 1201
 * @mtd: MTD device structure
 * @offs: the offset of the newly marked block
L
Linus Torvalds 已提交
1202
 *
1203 1204
 * The function updates the bad block table(s).
 */
1205
static int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
L
Linus Torvalds 已提交
1206 1207
{
	struct nand_chip *this = mtd->priv;
1208
	int len, res = 0;
L
Linus Torvalds 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
	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;
1220
	buf = kmalloc(len, GFP_KERNEL);
1221
	if (!buf)
L
Linus Torvalds 已提交
1222 1223
		return -ENOMEM;

1224
	/* Do we have a bbt per chip? */
L
Linus Torvalds 已提交
1225
	if (td->options & NAND_BBT_PERCHIP) {
1226
		chip = (int)(offs >> this->chip_shift);
L
Linus Torvalds 已提交
1227 1228 1229 1230 1231 1232 1233 1234
		chipsel = chip;
	} else {
		chip = 0;
		chipsel = -1;
	}

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

1237
	/* Write the bad block table to the device? */
1238
	if (td->options & NAND_BBT_WRITE) {
1239
		res = write_bbt(mtd, buf, td, md, chipsel);
L
Linus Torvalds 已提交
1240 1241 1242
		if (res < 0)
			goto out;
	}
1243
	/* Write the mirror bad block table to the device? */
1244
	if (md && (md->options & NAND_BBT_WRITE)) {
1245
		res = write_bbt(mtd, buf, md, td, chipsel);
L
Linus Torvalds 已提交
1246 1247
	}

1248 1249
 out:
	kfree(buf);
L
Linus Torvalds 已提交
1250 1251 1252
	return res;
}

1253 1254 1255 1256
/*
 * Define some generic bad / good block scan pattern which are used
 * while scanning a device for factory marked good / bad blocks.
 */
L
Linus Torvalds 已提交
1257 1258
static uint8_t scan_ff_pattern[] = { 0xff, 0xff };

1259
/* Generic flash bbt descriptors */
L
Linus Torvalds 已提交
1260 1261 1262 1263
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 = {
1264
	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
L
Linus Torvalds 已提交
1265 1266 1267 1268
		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
	.offs =	8,
	.len = 4,
	.veroffs = 12,
1269
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
L
Linus Torvalds 已提交
1270 1271 1272 1273
	.pattern = bbt_pattern
};

static struct nand_bbt_descr bbt_mirror_descr = {
1274
	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
L
Linus Torvalds 已提交
1275 1276 1277 1278
		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
	.offs =	8,
	.len = 4,
	.veroffs = 12,
1279
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
L
Linus Torvalds 已提交
1280 1281 1282
	.pattern = mirror_pattern
};

1283
static struct nand_bbt_descr bbt_main_no_oob_descr = {
1284 1285 1286 1287 1288
	.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,
1289
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1290 1291 1292
	.pattern = bbt_pattern
};

1293
static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
1294 1295 1296 1297 1298
	.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,
1299
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1300 1301 1302
	.pattern = mirror_pattern
};

1303
#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
1304
/**
1305
 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
1306
 * @this: NAND chip to create descriptor for
1307 1308
 *
 * This function allocates and initializes a nand_bbt_descr for BBM detection
1309
 * based on the properties of @this. The new descriptor is stored in
1310 1311 1312
 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
 * passed to this function.
 */
1313
static int nand_create_badblock_pattern(struct nand_chip *this)
1314 1315 1316
{
	struct nand_bbt_descr *bd;
	if (this->badblock_pattern) {
1317
		pr_warn("Bad block pattern already allocated; not replacing\n");
1318 1319 1320
		return -EINVAL;
	}
	bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1321
	if (!bd)
1322
		return -ENOMEM;
1323
	bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
1324 1325 1326 1327 1328 1329 1330 1331
	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 已提交
1332
/**
1333
 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
1334
 * @mtd: MTD device structure
L
Linus Torvalds 已提交
1335
 *
1336 1337 1338
 * This function selects the default bad block table support for the device and
 * calls the nand_scan_bbt function.
 */
1339
int nand_default_bbt(struct mtd_info *mtd)
L
Linus Torvalds 已提交
1340 1341
{
	struct nand_chip *this = mtd->priv;
1342

1343
	/* Is a flash based bad block table requested? */
1344
	if (this->bbt_options & NAND_BBT_USE_FLASH) {
1345 1346
		/* Use the default pattern descriptors */
		if (!this->bbt_td) {
1347
			if (this->bbt_options & NAND_BBT_NO_OOB) {
1348 1349
				this->bbt_td = &bbt_main_no_oob_descr;
				this->bbt_md = &bbt_mirror_no_oob_descr;
1350 1351 1352 1353
			} else {
				this->bbt_td = &bbt_main_descr;
				this->bbt_md = &bbt_mirror_descr;
			}
L
Linus Torvalds 已提交
1354 1355 1356 1357 1358
		}
	} else {
		this->bbt_td = NULL;
		this->bbt_md = NULL;
	}
1359 1360

	if (!this->badblock_pattern)
1361
		nand_create_badblock_pattern(this);
1362

1363
	return nand_scan_bbt(mtd, this->badblock_pattern);
L
Linus Torvalds 已提交
1364 1365 1366
}

/**
1367
 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1368 1369 1370 1371
 * @mtd: MTD device structure
 * @offs: offset in the device
 * @allowbbt: allow access to bad block table region
 */
1372
int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
L
Linus Torvalds 已提交
1373 1374
{
	struct nand_chip *this = mtd->priv;
B
Brian Norris 已提交
1375
	int block, res;
1376

1377 1378
	block = (int)(offs >> this->bbt_erase_shift);
	res = bbt_get_entry(this, block);
L
Linus Torvalds 已提交
1379

1380 1381
	pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: "
			"(block %d) 0x%02x\n",
1382
			(unsigned int)offs, block, res);
L
Linus Torvalds 已提交
1383

B
Brian Norris 已提交
1384
	switch (res) {
1385
	case BBT_BLOCK_GOOD:
1386
		return 0;
1387
	case BBT_BLOCK_WORN:
1388
		return 1;
1389
	case BBT_BLOCK_RESERVED:
1390
		return allowbbt ? 0 : 1;
L
Linus Torvalds 已提交
1391 1392 1393 1394
	}
	return 1;
}

1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
/**
 * 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;
}

1417 1418
EXPORT_SYMBOL(nand_scan_bbt);
EXPORT_SYMBOL(nand_default_bbt);