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

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

71 72 73 74 75 76 77 78
#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

79 80
static int nand_update_bbt(struct mtd_info *mtd, loff_t offs);

81 82 83 84 85 86 87 88 89 90 91 92 93 94
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;
}

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

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

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

L
Linus Torvalds 已提交
121 122 123
	return 0;
}

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

141 142
/**
 * add_marker_len - compute the length of the marker in data area
143
 * @td: BBT descriptor used for computation
144
 *
145
 * The length will be 0 if the marker is located in OOB area.
146 147 148 149 150 151 152 153 154 155 156 157 158 159
 */
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 已提交
160 161
/**
 * read_bbt - [GENERIC] Read the bad block table starting from page
162 163 164 165
 * @mtd: MTD device structure
 * @buf: temporary buffer
 * @page: the starting page
 * @num: the number of bbt descriptors to read
166
 * @td: the bbt describtion table
167
 * @offs: block number offset in the table
L
Linus Torvalds 已提交
168 169 170
 *
 * Read the bad block table starting from page.
 */
171
static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
172
		struct nand_bbt_descr *td, int offs)
L
Linus Torvalds 已提交
173
{
174
	int res, ret = 0, i, j, act = 0;
175
	struct nand_chip *this = mtd_to_nand(mtd);
L
Linus Torvalds 已提交
176 177
	size_t retlen, len, totlen;
	loff_t from;
178
	int bits = td->options & NAND_BBT_NRBITS_MSK;
B
Brian Norris 已提交
179
	uint8_t msk = (uint8_t)((1 << bits) - 1);
180
	u32 marker_len;
181
	int reserved_block_code = td->reserved_block_code;
L
Linus Torvalds 已提交
182 183

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

L
Linus Torvalds 已提交
187
	while (totlen) {
B
Brian Norris 已提交
188
		len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
189 190 191 192 193 194 195 196 197
		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;
		}
198
		res = mtd_read(mtd, from, len, &retlen, buf);
L
Linus Torvalds 已提交
199
		if (res < 0) {
200
			if (mtd_is_eccerr(res)) {
201 202
				pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
					from & ~mtd->writesize);
203 204
				return res;
			} else if (mtd_is_bitflip(res)) {
205 206
				pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
					from & ~mtd->writesize);
207 208 209
				ret = res;
			} else {
				pr_info("nand_bbt: error reading BBT\n");
L
Linus Torvalds 已提交
210 211
				return res;
			}
212
		}
L
Linus Torvalds 已提交
213 214 215 216

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

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

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

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

300
	return mtd_read(mtd, offs, len, &retlen, buf);
301 302
}

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

320
	ops.mode = MTD_OPS_PLACE_OOB;
321 322 323
	ops.ooboffs = 0;
	ops.ooblen = mtd->oobsize;

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

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

		buf += mtd->oobsize + mtd->writesize;
		len -= mtd->writesize;
D
Dmitry Maluka 已提交
339
		offs += mtd->writesize;
340
	}
341
	return ret;
342 343
}

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

353
/* Scan write data with oob to flash */
354 355 356 357 358
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;

359
	ops.mode = MTD_OPS_PLACE_OOB;
360 361 362 363 364 365
	ops.ooboffs = 0;
	ops.ooblen = mtd->oobsize;
	ops.datbuf = buf;
	ops.oobbuf = oob;
	ops.len = len;

366
	return mtd_write_oob(mtd, offs, &ops);
367 368
}

369 370 371 372 373 374 375 376 377
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 已提交
378 379
/**
 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
380 381 382 383
 * @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 已提交
384
 *
385 386 387
 * Read the bad block table(s) for all chips starting at a given page. We
 * assume that the bbt bits are in consecutive order.
 */
388 389
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 已提交
390
{
391
	struct nand_chip *this = mtd_to_nand(mtd);
L
Linus Torvalds 已提交
392

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

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

412
/* Scan a given block partially */
413
static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
414
			   loff_t offs, uint8_t *buf, int numpages)
415 416 417 418 419 420 421 422
{
	struct mtd_oob_ops ops;
	int j, ret;

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

425
	for (j = 0; j < numpages; j++) {
426
		/*
427 428
		 * Read the full oob until read_oob is fixed to handle single
		 * byte reads for 16 bit buswidth.
429
		 */
430
		ret = mtd_read_oob(mtd, offs, &ops);
431
		/* Ignore ECC errors when checking for BBM */
432
		if (ret && !mtd_is_bitflip_or_eccerr(ret))
433 434 435 436 437 438 439 440 441 442
			return ret;

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

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

L
Linus Torvalds 已提交
443 444
/**
 * create_bbt - [GENERIC] Create a bad block table by scanning the device
445 446 447 448 449
 * @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 已提交
450
 *
451 452
 * Create a bad block table by scanning the device for the given good/bad block
 * identify pattern.
L
Linus Torvalds 已提交
453
 */
454 455
static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
	struct nand_bbt_descr *bd, int chip)
L
Linus Torvalds 已提交
456
{
457
	struct nand_chip *this = mtd_to_nand(mtd);
458
	int i, numblocks, numpages;
L
Linus Torvalds 已提交
459 460 461
	int startblock;
	loff_t from;

462
	pr_info("Scanning device for bad blocks\n");
L
Linus Torvalds 已提交
463

464
	if (bd->options & NAND_BBT_SCAN2NDPAGE)
465
		numpages = 2;
466
	else
467
		numpages = 1;
468

L
Linus Torvalds 已提交
469
	if (chip == -1) {
470
		numblocks = mtd->size >> this->bbt_erase_shift;
L
Linus Torvalds 已提交
471 472 473 474
		startblock = 0;
		from = 0;
	} else {
		if (chip >= this->numchips) {
475
			pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
476
			       chip + 1, this->numchips);
477
			return -EINVAL;
L
Linus Torvalds 已提交
478
		}
479
		numblocks = this->chipsize >> this->bbt_erase_shift;
L
Linus Torvalds 已提交
480 481
		startblock = chip * numblocks;
		numblocks += startblock;
482
		from = (loff_t)startblock << this->bbt_erase_shift;
L
Linus Torvalds 已提交
483
	}
484

485
	if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
486
		from += mtd->erasesize - (mtd->writesize * numpages);
487

488
	for (i = startblock; i < numblocks; i++) {
489
		int ret;
490

491 492
		BUG_ON(bd->options & NAND_BBT_NO_OOB);

493
		ret = scan_block_fast(mtd, bd, from, buf, numpages);
494 495 496 497
		if (ret < 0)
			return ret;

		if (ret) {
498
			bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
499
			pr_warn("Bad eraseblock %d at 0x%012llx\n",
500
				i, (unsigned long long)from);
501
			mtd->ecc_stats.badblocks++;
L
Linus Torvalds 已提交
502
		}
503

L
Linus Torvalds 已提交
504 505
		from += (1 << this->bbt_erase_shift);
	}
506
	return 0;
L
Linus Torvalds 已提交
507 508 509 510
}

/**
 * search_bbt - [GENERIC] scan the device for a specific bad block table
511 512 513
 * @mtd: MTD device structure
 * @buf: temporary buffer
 * @td: descriptor for the bad block table
L
Linus Torvalds 已提交
514
 *
515 516 517 518 519 520
 * 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 已提交
521
 *
522
 * The bbt ident pattern resides in the oob area of the first page in a block.
L
Linus Torvalds 已提交
523
 */
524
static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
L
Linus Torvalds 已提交
525
{
526
	struct nand_chip *this = mtd_to_nand(mtd);
L
Linus Torvalds 已提交
527
	int i, chips;
528
	int startblock, block, dir;
J
Joern Engel 已提交
529
	int scanlen = mtd->writesize + mtd->oobsize;
L
Linus Torvalds 已提交
530
	int bbtblocks;
531
	int blocktopage = this->bbt_erase_shift - this->page_shift;
L
Linus Torvalds 已提交
532

533
	/* Search direction top -> down? */
L
Linus Torvalds 已提交
534
	if (td->options & NAND_BBT_LASTBLOCK) {
535
		startblock = (mtd->size >> this->bbt_erase_shift) - 1;
L
Linus Torvalds 已提交
536 537
		dir = -1;
	} else {
538
		startblock = 0;
L
Linus Torvalds 已提交
539
		dir = 1;
540 541
	}

542
	/* Do we have a bbt per chip? */
L
Linus Torvalds 已提交
543 544 545 546 547 548 549 550
	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;
	}
551

L
Linus Torvalds 已提交
552 553
	for (i = 0; i < chips; i++) {
		/* Reset version information */
554
		td->version[i] = 0;
L
Linus Torvalds 已提交
555 556 557
		td->pages[i] = -1;
		/* Scan the maximum number of blocks */
		for (block = 0; block < td->maxblocks; block++) {
558

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

L
Linus Torvalds 已提交
562
			/* Read first page */
563
			scan_read(mtd, buf, offs, mtd->writesize, td);
J
Joern Engel 已提交
564
			if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
565
				td->pages[i] = actblock << blocktopage;
L
Linus Torvalds 已提交
566
				if (td->options & NAND_BBT_VERSION) {
567 568
					offs = bbt_get_ver_offs(mtd, td);
					td->version[i] = buf[offs];
L
Linus Torvalds 已提交
569 570 571 572 573 574 575 576 577
				}
				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)
578
			pr_warn("Bad block table not found for chip %d\n", i);
L
Linus Torvalds 已提交
579
		else
580 581
			pr_info("Bad block table found at page %d, version 0x%02X\n",
				td->pages[i], td->version[i]);
L
Linus Torvalds 已提交
582
	}
583
	return 0;
L
Linus Torvalds 已提交
584 585 586 587
}

/**
 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
588 589 590 591
 * @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 已提交
592
 *
593 594
 * Search and read the bad block table(s).
 */
595 596 597
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 已提交
598 599
{
	/* Search the primary table */
600
	search_bbt(mtd, buf, td);
601

L
Linus Torvalds 已提交
602 603
	/* Search the mirror table */
	if (md)
604
		search_bbt(mtd, buf, md);
L
Linus Torvalds 已提交
605 606
}

607
/**
L
Linus Torvalds 已提交
608
 * write_bbt - [GENERIC] (Re)write the bad block table
609 610 611 612 613
 * @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 已提交
614
 *
615 616
 * (Re)write the bad block table.
 */
617
static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
618 619
		     struct nand_bbt_descr *td, struct nand_bbt_descr *md,
		     int chipsel)
L
Linus Torvalds 已提交
620
{
621
	struct nand_chip *this = mtd_to_nand(mtd);
L
Linus Torvalds 已提交
622
	struct erase_info einfo;
623
	int i, res, chip = 0;
L
Linus Torvalds 已提交
624
	int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
625
	int nrchips, pageoffs, ooboffs;
L
Linus Torvalds 已提交
626 627
	uint8_t msk[4];
	uint8_t rcode = td->reserved_block_code;
628
	size_t retlen, len = 0;
L
Linus Torvalds 已提交
629
	loff_t to;
630 631 632 633 634
	struct mtd_oob_ops ops;

	ops.ooblen = mtd->oobsize;
	ops.ooboffs = 0;
	ops.datbuf = NULL;
635
	ops.mode = MTD_OPS_PLACE_OOB;
L
Linus Torvalds 已提交
636 637 638

	if (!rcode)
		rcode = 0xff;
639
	/* Write bad block table per chip rather than per device? */
L
Linus Torvalds 已提交
640
	if (td->options & NAND_BBT_PERCHIP) {
641
		numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
642
		/* Full device write or specific chip? */
L
Linus Torvalds 已提交
643 644 645 646 647 648 649
		if (chipsel == -1) {
			nrchips = this->numchips;
		} else {
			nrchips = chipsel + 1;
			chip = chipsel;
		}
	} else {
650
		numblocks = (int)(mtd->size >> this->bbt_erase_shift);
L
Linus Torvalds 已提交
651
		nrchips = 1;
652 653
	}

L
Linus Torvalds 已提交
654 655
	/* Loop through the chips */
	for (; chip < nrchips; chip++) {
656 657
		/*
		 * There was already a version of the table, reuse the page
658
		 * This applies for absolute placement too, as we have the
L
Linus Torvalds 已提交
659 660 661 662 663
		 * page nr. in td->pages.
		 */
		if (td->pages[chip] != -1) {
			page = td->pages[chip];
			goto write;
664
		}
L
Linus Torvalds 已提交
665

666 667 668 669
		/*
		 * Automatic placement of the bad block table. Search direction
		 * top -> down?
		 */
L
Linus Torvalds 已提交
670 671 672 673 674 675
		if (td->options & NAND_BBT_LASTBLOCK) {
			startblock = numblocks * (chip + 1) - 1;
			dir = -1;
		} else {
			startblock = chip * numblocks;
			dir = 1;
676
		}
L
Linus Torvalds 已提交
677 678 679 680

		for (i = 0; i < td->maxblocks; i++) {
			int block = startblock + dir * i;
			/* Check, if the block is bad */
681 682 683
			switch (bbt_get_entry(this, block)) {
			case BBT_BLOCK_WORN:
			case BBT_BLOCK_FACTORY_BAD:
L
Linus Torvalds 已提交
684 685
				continue;
			}
686 687
			page = block <<
				(this->bbt_erase_shift - this->page_shift);
L
Linus Torvalds 已提交
688 689 690 691
			/* Check, if the block is used by the mirror table */
			if (!md || md->pages[chip] != page)
				goto write;
		}
692
		pr_err("No space left to write bad block table\n");
L
Linus Torvalds 已提交
693
		return -ENOSPC;
694
	write:
L
Linus Torvalds 已提交
695 696 697

		/* Set up shift count and masks for the flash table */
		bits = td->options & NAND_BBT_NRBITS_MSK;
698
		msk[2] = ~rcode;
L
Linus Torvalds 已提交
699
		switch (bits) {
700 701 702 703 704 705 706 707 708 709 710 711
		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 已提交
712 713
		default: return -EINVAL;
		}
714

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

717
		/* Must we save the block contents? */
L
Linus Torvalds 已提交
718 719
		if (td->options & NAND_BBT_SAVECONTENT) {
			/* Make it block aligned */
720
			to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
L
Linus Torvalds 已提交
721
			len = 1 << this->bbt_erase_shift;
722
			res = mtd_read(mtd, to, len, &retlen, buf);
L
Linus Torvalds 已提交
723 724
			if (res < 0) {
				if (retlen != len) {
725
					pr_info("nand_bbt: error reading block for writing the bad block table\n");
L
Linus Torvalds 已提交
726 727
					return res;
				}
728
				pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
L
Linus Torvalds 已提交
729
			}
730
			/* Read oob data */
731
			ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
732
			ops.oobbuf = &buf[len];
733
			res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
734
			if (res < 0 || ops.oobretlen != ops.ooblen)
735 736
				goto outerr;

L
Linus Torvalds 已提交
737 738 739 740
			/* 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 已提交
741
			memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
742 743
			ooboffs = len + (pageoffs * mtd->oobsize);

744 745 746
		} else if (td->options & NAND_BBT_NO_OOB) {
			ooboffs = 0;
			offs = td->len;
747
			/* The version byte */
748 749 750
			if (td->options & NAND_BBT_VERSION)
				offs++;
			/* Calc length */
B
Brian Norris 已提交
751
			len = (size_t)(numblocks >> sft);
752
			len += offs;
753
			/* Make it page aligned! */
754 755 756 757 758
			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 已提交
759 760
		} else {
			/* Calc length */
B
Brian Norris 已提交
761
			len = (size_t)(numblocks >> sft);
762
			/* Make it page aligned! */
763
			len = ALIGN(len, mtd->writesize);
L
Linus Torvalds 已提交
764
			/* Preset the buffer with 0xff */
765 766
			memset(buf, 0xff, len +
			       (len >> this->page_shift)* mtd->oobsize);
L
Linus Torvalds 已提交
767
			offs = 0;
768
			ooboffs = len;
L
Linus Torvalds 已提交
769
			/* Pattern is located in oob area of first page */
770
			memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
L
Linus Torvalds 已提交
771
		}
772

773 774 775
		if (td->options & NAND_BBT_VERSION)
			buf[ooboffs + td->veroffs] = td->version[chip];

776
		/* Walk through the memory table */
777
		for (i = 0; i < numblocks; i++) {
L
Linus Torvalds 已提交
778
			uint8_t dat;
779 780 781 782
			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 已提交
783
		}
784

785
		memset(&einfo, 0, sizeof(einfo));
L
Linus Torvalds 已提交
786
		einfo.mtd = mtd;
787
		einfo.addr = to;
L
Linus Torvalds 已提交
788
		einfo.len = 1 << this->bbt_erase_shift;
789
		res = nand_erase_nand(mtd, &einfo, 1);
790 791
		if (res < 0)
			goto outerr;
792

793 794 795
		res = scan_write_bbt(mtd, to, len, buf,
				td->options & NAND_BBT_NO_OOB ? NULL :
				&buf[len]);
796 797 798
		if (res < 0)
			goto outerr;

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

L
Linus Torvalds 已提交
802 803
		/* Mark it as used */
		td->pages[chip] = page;
804
	}
L
Linus Torvalds 已提交
805
	return 0;
806 807

 outerr:
808
	pr_warn("nand_bbt: error while writing bad block table %d\n", res);
809
	return res;
L
Linus Torvalds 已提交
810 811 812 813
}

/**
 * nand_memory_bbt - [GENERIC] create a memory based bad block table
814 815
 * @mtd: MTD device structure
 * @bd: descriptor for the good/bad block search pattern
L
Linus Torvalds 已提交
816
 *
817 818 819
 * The function creates a memory based bbt by scanning the device for
 * manufacturer / software marked good / bad blocks.
 */
820
static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
L
Linus Torvalds 已提交
821
{
822
	struct nand_chip *this = mtd_to_nand(mtd);
L
Linus Torvalds 已提交
823

824
	return create_bbt(mtd, this->buffers->databuf, bd, -1);
L
Linus Torvalds 已提交
825 826 827
}

/**
828
 * check_create - [GENERIC] create and write bbt(s) if necessary
829 830 831
 * @mtd: MTD device structure
 * @buf: temporary buffer
 * @bd: descriptor for the good/bad block search pattern
L
Linus Torvalds 已提交
832
 *
833 834 835 836 837
 * 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.
 */
838
static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
L
Linus Torvalds 已提交
839
{
840
	int i, chips, writeops, create, chipsel, res, res2;
841
	struct nand_chip *this = mtd_to_nand(mtd);
L
Linus Torvalds 已提交
842 843 844 845
	struct nand_bbt_descr *td = this->bbt_td;
	struct nand_bbt_descr *md = this->bbt_md;
	struct nand_bbt_descr *rd, *rd2;

846
	/* Do we have a bbt per chip? */
847
	if (td->options & NAND_BBT_PERCHIP)
L
Linus Torvalds 已提交
848
		chips = this->numchips;
849
	else
L
Linus Torvalds 已提交
850
		chips = 1;
851

L
Linus Torvalds 已提交
852 853
	for (i = 0; i < chips; i++) {
		writeops = 0;
854
		create = 0;
L
Linus Torvalds 已提交
855 856
		rd = NULL;
		rd2 = NULL;
857
		res = res2 = 0;
858
		/* Per chip or per device? */
L
Linus Torvalds 已提交
859
		chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
860
		/* Mirrored table available? */
L
Linus Torvalds 已提交
861 862
		if (md) {
			if (td->pages[i] == -1 && md->pages[i] == -1) {
863
				create = 1;
L
Linus Torvalds 已提交
864
				writeops = 0x03;
865
			} else if (td->pages[i] == -1) {
866
				rd = md;
B
Brian Norris 已提交
867
				writeops = 0x01;
868
			} else if (md->pages[i] == -1) {
L
Linus Torvalds 已提交
869
				rd = td;
B
Brian Norris 已提交
870
				writeops = 0x02;
871
			} else if (td->version[i] == md->version[i]) {
L
Linus Torvalds 已提交
872 873 874
				rd = td;
				if (!(td->options & NAND_BBT_VERSION))
					rd2 = md;
875
			} else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
L
Linus Torvalds 已提交
876
				rd = td;
B
Brian Norris 已提交
877
				writeops = 0x02;
L
Linus Torvalds 已提交
878 879
			} else {
				rd = md;
B
Brian Norris 已提交
880
				writeops = 0x01;
L
Linus Torvalds 已提交
881 882 883
			}
		} else {
			if (td->pages[i] == -1) {
884
				create = 1;
L
Linus Torvalds 已提交
885
				writeops = 0x01;
886 887
			} else {
				rd = td;
L
Linus Torvalds 已提交
888 889
			}
		}
890

891 892 893 894 895 896 897 898 899 900 901 902 903 904
		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;
		}

905
		/* Read back first? */
906 907 908 909 910
		if (rd) {
			res = read_abs_bbt(mtd, buf, rd, chipsel);
			if (mtd_is_eccerr(res)) {
				/* Mark table as invalid */
				rd->pages[i] = -1;
911
				rd->version[i] = 0;
912 913 914 915
				i--;
				continue;
			}
		}
916
		/* If they weren't versioned, read both */
917 918 919 920 921
		if (rd2) {
			res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
			if (mtd_is_eccerr(res2)) {
				/* Mark table as invalid */
				rd2->pages[i] = -1;
922
				rd2->version[i] = 0;
923 924 925 926 927 928 929 930
				i--;
				continue;
			}
		}

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

932 933 934 935 936 937
		/* Update version numbers before writing */
		if (md) {
			td->version[i] = max(td->version[i], md->version[i]);
			md->version[i] = td->version[i];
		}

938
		/* Write the bad block table to the device? */
L
Linus Torvalds 已提交
939
		if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
940
			res = write_bbt(mtd, buf, td, md, chipsel);
L
Linus Torvalds 已提交
941 942 943
			if (res < 0)
				return res;
		}
944

945
		/* Write the mirror bad block table to the device? */
L
Linus Torvalds 已提交
946
		if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
947
			res = write_bbt(mtd, buf, md, td, chipsel);
L
Linus Torvalds 已提交
948 949 950 951
			if (res < 0)
				return res;
		}
	}
952
	return 0;
L
Linus Torvalds 已提交
953 954 955
}

/**
956
 * mark_bbt_regions - [GENERIC] mark the bad block table regions
957 958
 * @mtd: MTD device structure
 * @td: bad block table descriptor
L
Linus Torvalds 已提交
959
 *
960 961 962
 * The bad block table regions are marked as "bad" to prevent accidental
 * erasures / writes. The regions are identified by the mark 0x02.
 */
963
static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
L
Linus Torvalds 已提交
964
{
965
	struct nand_chip *this = mtd_to_nand(mtd);
L
Linus Torvalds 已提交
966
	int i, j, chips, block, nrblocks, update;
967
	uint8_t oldval;
L
Linus Torvalds 已提交
968

969
	/* Do we have a bbt per chip? */
L
Linus Torvalds 已提交
970 971 972 973 974 975
	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);
976 977
	}

L
Linus Torvalds 已提交
978 979 980
	for (i = 0; i < chips; i++) {
		if ((td->options & NAND_BBT_ABSPAGE) ||
		    !(td->options & NAND_BBT_WRITE)) {
981 982
			if (td->pages[i] == -1)
				continue;
L
Linus Torvalds 已提交
983
			block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
984 985
			oldval = bbt_get_entry(this, block);
			bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
986 987
			if ((oldval != BBT_BLOCK_RESERVED) &&
					td->reserved_block_code)
988 989
				nand_update_bbt(mtd, (loff_t)block <<
						this->bbt_erase_shift);
L
Linus Torvalds 已提交
990 991 992 993 994
			continue;
		}
		update = 0;
		if (td->options & NAND_BBT_LASTBLOCK)
			block = ((i + 1) * nrblocks) - td->maxblocks;
995
		else
L
Linus Torvalds 已提交
996 997
			block = i * nrblocks;
		for (j = 0; j < td->maxblocks; j++) {
998 999
			oldval = bbt_get_entry(this, block);
			bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1000
			if (oldval != BBT_BLOCK_RESERVED)
1001
				update = 1;
1002
			block++;
1003
		}
1004 1005 1006 1007 1008
		/*
		 * 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 已提交
1009
		if (update && td->reserved_block_code)
1010 1011
			nand_update_bbt(mtd, (loff_t)(block - 1) <<
					this->bbt_erase_shift);
L
Linus Torvalds 已提交
1012 1013 1014
	}
}

1015 1016
/**
 * verify_bbt_descr - verify the bad block description
1017 1018
 * @mtd: MTD device structure
 * @bd: the table to verify
1019 1020 1021 1022 1023 1024
 *
 * 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)
{
1025
	struct nand_chip *this = mtd_to_nand(mtd);
1026 1027
	u32 pattern_len;
	u32 bits;
1028 1029 1030 1031
	u32 table_size;

	if (!bd)
		return;
1032 1033 1034 1035

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

1036
	BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
1037
			!(this->bbt_options & NAND_BBT_USE_FLASH));
1038 1039 1040 1041 1042 1043
	BUG_ON(!bits);

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

	if (bd->options & NAND_BBT_NO_OOB) {
1044
		BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
1045
		BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
		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 已提交
1063 1064
/**
 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1065 1066
 * @mtd: MTD device structure
 * @bd: descriptor for the good/bad block search pattern
L
Linus Torvalds 已提交
1067
 *
1068 1069 1070
 * 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 已提交
1071
 *
1072 1073 1074
 * The bad block table memory is allocated here. It must be freed by calling
 * the nand_free_bbt function.
 */
1075
static int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
L
Linus Torvalds 已提交
1076
{
1077
	struct nand_chip *this = mtd_to_nand(mtd);
1078
	int len, res;
L
Linus Torvalds 已提交
1079 1080 1081 1082
	uint8_t *buf;
	struct nand_bbt_descr *td = this->bbt_td;
	struct nand_bbt_descr *md = this->bbt_md;

1083
	len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
1084 1085 1086 1087
	/*
	 * Allocate memory (2bit per block) and clear the memory bad block
	 * table.
	 */
1088
	this->bbt = kzalloc(len, GFP_KERNEL);
1089
	if (!this->bbt)
L
Linus Torvalds 已提交
1090 1091
		return -ENOMEM;

1092 1093 1094
	/*
	 * If no primary table decriptor is given, scan the device to build a
	 * memory based bad block table.
L
Linus Torvalds 已提交
1095
	 */
1096 1097
	if (!td) {
		if ((res = nand_memory_bbt(mtd, bd))) {
1098
			pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
1099
			goto err;
1100
		}
1101
		return 0;
1102
	}
1103 1104
	verify_bbt_descr(mtd, td);
	verify_bbt_descr(mtd, md);
L
Linus Torvalds 已提交
1105 1106 1107 1108

	/* Allocate a temporary buffer for one eraseblock incl. oob */
	len = (1 << this->bbt_erase_shift);
	len += (len >> this->page_shift) * mtd->oobsize;
1109
	buf = vmalloc(len);
L
Linus Torvalds 已提交
1110
	if (!buf) {
1111 1112
		res = -ENOMEM;
		goto err;
L
Linus Torvalds 已提交
1113
	}
1114

1115
	/* Is the bbt at a given page? */
L
Linus Torvalds 已提交
1116
	if (td->options & NAND_BBT_ABSPAGE) {
1117
		read_abs_bbts(mtd, buf, td, md);
1118
	} else {
L
Linus Torvalds 已提交
1119
		/* Search the bad block table using a pattern in oob */
1120
		search_read_bbts(mtd, buf, td, md);
1121
	}
L
Linus Torvalds 已提交
1122

1123
	res = check_create(mtd, buf, bd);
1124 1125
	if (res)
		goto err;
1126

L
Linus Torvalds 已提交
1127
	/* Prevent the bbt regions from erasing / writing */
1128
	mark_bbt_region(mtd, td);
L
Linus Torvalds 已提交
1129
	if (md)
1130
		mark_bbt_region(mtd, md);
1131

1132
	vfree(buf);
1133 1134 1135 1136 1137
	return 0;

err:
	kfree(this->bbt);
	this->bbt = NULL;
L
Linus Torvalds 已提交
1138 1139 1140 1141
	return res;
}

/**
1142
 * nand_update_bbt - update bad block table(s)
1143 1144
 * @mtd: MTD device structure
 * @offs: the offset of the newly marked block
L
Linus Torvalds 已提交
1145
 *
1146 1147
 * The function updates the bad block table(s).
 */
1148
static int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
L
Linus Torvalds 已提交
1149
{
1150
	struct nand_chip *this = mtd_to_nand(mtd);
1151
	int len, res = 0;
L
Linus Torvalds 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
	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;
1163
	buf = kmalloc(len, GFP_KERNEL);
1164
	if (!buf)
L
Linus Torvalds 已提交
1165 1166
		return -ENOMEM;

1167
	/* Do we have a bbt per chip? */
L
Linus Torvalds 已提交
1168
	if (td->options & NAND_BBT_PERCHIP) {
1169
		chip = (int)(offs >> this->chip_shift);
L
Linus Torvalds 已提交
1170 1171 1172 1173 1174 1175 1176 1177
		chipsel = chip;
	} else {
		chip = 0;
		chipsel = -1;
	}

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

1180
	/* Write the bad block table to the device? */
1181
	if (td->options & NAND_BBT_WRITE) {
1182
		res = write_bbt(mtd, buf, td, md, chipsel);
L
Linus Torvalds 已提交
1183 1184 1185
		if (res < 0)
			goto out;
	}
1186
	/* Write the mirror bad block table to the device? */
1187
	if (md && (md->options & NAND_BBT_WRITE)) {
1188
		res = write_bbt(mtd, buf, md, td, chipsel);
L
Linus Torvalds 已提交
1189 1190
	}

1191 1192
 out:
	kfree(buf);
L
Linus Torvalds 已提交
1193 1194 1195
	return res;
}

1196 1197 1198 1199
/*
 * Define some generic bad / good block scan pattern which are used
 * while scanning a device for factory marked good / bad blocks.
 */
L
Linus Torvalds 已提交
1200 1201
static uint8_t scan_ff_pattern[] = { 0xff, 0xff };

1202
/* Generic flash bbt descriptors */
L
Linus Torvalds 已提交
1203 1204 1205 1206
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 = {
1207
	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
L
Linus Torvalds 已提交
1208 1209 1210 1211
		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
	.offs =	8,
	.len = 4,
	.veroffs = 12,
1212
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
L
Linus Torvalds 已提交
1213 1214 1215 1216
	.pattern = bbt_pattern
};

static struct nand_bbt_descr bbt_mirror_descr = {
1217
	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
L
Linus Torvalds 已提交
1218 1219 1220 1221
		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
	.offs =	8,
	.len = 4,
	.veroffs = 12,
1222
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
L
Linus Torvalds 已提交
1223 1224 1225
	.pattern = mirror_pattern
};

1226
static struct nand_bbt_descr bbt_main_no_oob_descr = {
1227 1228 1229 1230 1231
	.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,
1232
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1233 1234 1235
	.pattern = bbt_pattern
};

1236
static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
1237 1238 1239 1240 1241
	.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,
1242
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1243 1244 1245
	.pattern = mirror_pattern
};

1246
#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
1247
/**
1248
 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
1249
 * @this: NAND chip to create descriptor for
1250 1251
 *
 * This function allocates and initializes a nand_bbt_descr for BBM detection
1252
 * based on the properties of @this. The new descriptor is stored in
1253 1254 1255
 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
 * passed to this function.
 */
1256
static int nand_create_badblock_pattern(struct nand_chip *this)
1257 1258 1259
{
	struct nand_bbt_descr *bd;
	if (this->badblock_pattern) {
1260
		pr_warn("Bad block pattern already allocated; not replacing\n");
1261 1262 1263
		return -EINVAL;
	}
	bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1264
	if (!bd)
1265
		return -ENOMEM;
1266
	bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
1267 1268 1269 1270 1271 1272 1273 1274
	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 已提交
1275
/**
1276
 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
1277
 * @mtd: MTD device structure
L
Linus Torvalds 已提交
1278
 *
1279 1280 1281
 * This function selects the default bad block table support for the device and
 * calls the nand_scan_bbt function.
 */
1282
int nand_default_bbt(struct mtd_info *mtd)
L
Linus Torvalds 已提交
1283
{
1284
	struct nand_chip *this = mtd_to_nand(mtd);
1285
	int ret;
1286

1287
	/* Is a flash based bad block table requested? */
1288
	if (this->bbt_options & NAND_BBT_USE_FLASH) {
1289 1290
		/* Use the default pattern descriptors */
		if (!this->bbt_td) {
1291
			if (this->bbt_options & NAND_BBT_NO_OOB) {
1292 1293
				this->bbt_td = &bbt_main_no_oob_descr;
				this->bbt_md = &bbt_mirror_no_oob_descr;
1294 1295 1296 1297
			} else {
				this->bbt_td = &bbt_main_descr;
				this->bbt_md = &bbt_mirror_descr;
			}
L
Linus Torvalds 已提交
1298 1299 1300 1301 1302
		}
	} else {
		this->bbt_td = NULL;
		this->bbt_md = NULL;
	}
1303

1304 1305 1306 1307 1308
	if (!this->badblock_pattern) {
		ret = nand_create_badblock_pattern(this);
		if (ret)
			return ret;
	}
1309

1310
	return nand_scan_bbt(mtd, this->badblock_pattern);
L
Linus Torvalds 已提交
1311 1312
}

1313 1314 1315 1316 1317 1318 1319
/**
 * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
 * @mtd: MTD device structure
 * @offs: offset in the device
 */
int nand_isreserved_bbt(struct mtd_info *mtd, loff_t offs)
{
1320
	struct nand_chip *this = mtd_to_nand(mtd);
1321 1322 1323 1324 1325 1326
	int block;

	block = (int)(offs >> this->bbt_erase_shift);
	return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
}

L
Linus Torvalds 已提交
1327
/**
1328
 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1329 1330 1331 1332
 * @mtd: MTD device structure
 * @offs: offset in the device
 * @allowbbt: allow access to bad block table region
 */
1333
int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
L
Linus Torvalds 已提交
1334
{
1335
	struct nand_chip *this = mtd_to_nand(mtd);
B
Brian Norris 已提交
1336
	int block, res;
1337

1338 1339
	block = (int)(offs >> this->bbt_erase_shift);
	res = bbt_get_entry(this, block);
L
Linus Torvalds 已提交
1340

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

B
Brian Norris 已提交
1344
	switch (res) {
1345
	case BBT_BLOCK_GOOD:
1346
		return 0;
1347
	case BBT_BLOCK_WORN:
1348
		return 1;
1349
	case BBT_BLOCK_RESERVED:
1350
		return allowbbt ? 0 : 1;
L
Linus Torvalds 已提交
1351 1352 1353 1354
	}
	return 1;
}

1355 1356 1357 1358 1359 1360 1361
/**
 * 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)
{
1362
	struct nand_chip *this = mtd_to_nand(mtd);
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
	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;
}

1377
EXPORT_SYMBOL(nand_scan_bbt);