nand_bbt.c 37.8 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
static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
{
76 77 78
	if (memcmp(buf, td->pattern, td->len))
		return -1;
	return 0;
79 80
}

81
/**
L
Linus Torvalds 已提交
82
 * check_pattern - [GENERIC] check if a pattern is in the buffer
83 84 85 86
 * @buf: the buffer to search
 * @len: the length of buffer to search
 * @paglen: the pagelength
 * @td: search pattern descriptor
L
Linus Torvalds 已提交
87
 *
88 89 90 91
 * 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.
 */
92
static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
L
Linus Torvalds 已提交
93
{
B
Brian Norris 已提交
94
	int end = 0;
L
Linus Torvalds 已提交
95 96
	uint8_t *p = buf;

97 98 99
	if (td->options & NAND_BBT_NO_OOB)
		return check_pattern_no_oob(buf, td);

100
	end = paglen + td->offs;
B
Brian Norris 已提交
101 102 103
	if (td->options & NAND_BBT_SCANEMPTY)
		if (memchr_inv(p, 0xff, end))
			return -1;
104
	p += end;
105

L
Linus Torvalds 已提交
106
	/* Compare the pattern */
107 108
	if (memcmp(p, td->pattern, td->len))
		return -1;
109

L
Linus Torvalds 已提交
110
	if (td->options & NAND_BBT_SCANEMPTY) {
111 112
		p += td->len;
		end += td->len;
B
Brian Norris 已提交
113 114
		if (memchr_inv(p, 0xff, len - end))
			return -1;
L
Linus Torvalds 已提交
115 116 117 118
	}
	return 0;
}

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

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

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

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

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

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

279
/* BBT marker is in the first page, no OOB */
280
static int scan_read_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
281 282 283 284 285 286 287 288 289
			 struct nand_bbt_descr *td)
{
	size_t retlen;
	size_t len;

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

290
	return mtd_read(mtd, offs, len, &retlen, buf);
291 292
}

293
/**
294
 * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
295 296 297 298 299 300 301 302 303
 * @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.
 */
304
static int scan_read_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
305 306 307
			 size_t len)
{
	struct mtd_oob_ops ops;
308
	int res, ret = 0;
309

310
	ops.mode = MTD_OPS_PLACE_OOB;
311 312 313
	ops.ooboffs = 0;
	ops.ooblen = mtd->oobsize;

314
	while (len > 0) {
B
Brian Norris 已提交
315 316 317
		ops.datbuf = buf;
		ops.len = min(len, (size_t)mtd->writesize);
		ops.oobbuf = buf + ops.len;
318

319
		res = mtd_read_oob(mtd, offs, &ops);
320 321 322 323 324 325
		if (res) {
			if (!mtd_is_bitflip_or_eccerr(res))
				return res;
			else if (mtd_is_eccerr(res) || !ret)
				ret = res;
		}
326 327 328

		buf += mtd->oobsize + mtd->writesize;
		len -= mtd->writesize;
D
Dmitry Maluka 已提交
329
		offs += mtd->writesize;
330
	}
331
	return ret;
332 333
}

334
static int scan_read(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
335 336 337
			 size_t len, struct nand_bbt_descr *td)
{
	if (td->options & NAND_BBT_NO_OOB)
338
		return scan_read_data(mtd, buf, offs, td);
339
	else
340
		return scan_read_oob(mtd, buf, offs, len);
341 342
}

343
/* Scan write data with oob to flash */
344 345 346 347 348
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;

349
	ops.mode = MTD_OPS_PLACE_OOB;
350 351 352 353 354 355
	ops.ooboffs = 0;
	ops.ooblen = mtd->oobsize;
	ops.datbuf = buf;
	ops.oobbuf = oob;
	ops.len = len;

356
	return mtd_write_oob(mtd, offs, &ops);
357 358
}

359 360 361 362 363 364 365 366 367
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 已提交
368 369
/**
 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
370 371 372 373
 * @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 已提交
374
 *
375 376 377
 * Read the bad block table(s) for all chips starting at a given page. We
 * assume that the bbt bits are in consecutive order.
 */
378 379
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 已提交
380 381 382
{
	struct nand_chip *this = mtd->priv;

383
	/* Read the primary version, if available */
L
Linus Torvalds 已提交
384
	if (td->options & NAND_BBT_VERSION) {
385
		scan_read(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
386 387
			      mtd->writesize, td);
		td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
388
		pr_info("Bad block table at page %d, version 0x%02X\n",
389
			 td->pages[0], td->version[0]);
L
Linus Torvalds 已提交
390 391
	}

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

402
/* Scan a given block full */
403 404
static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
			   loff_t offs, uint8_t *buf, size_t readlen,
405
			   int scanlen, int numpages)
406 407 408
{
	int ret, j;

409
	ret = scan_read_oob(mtd, buf, offs, readlen);
410
	/* Ignore ECC errors when checking for BBM */
411
	if (ret && !mtd_is_bitflip_or_eccerr(ret))
412 413
		return ret;

414
	for (j = 0; j < numpages; j++, buf += scanlen) {
415 416 417 418 419 420
		if (check_pattern(buf, scanlen, mtd->writesize, bd))
			return 1;
	}
	return 0;
}

421
/* Scan a given block partially */
422
static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
423
			   loff_t offs, uint8_t *buf, int numpages)
424 425 426 427 428 429 430 431
{
	struct mtd_oob_ops ops;
	int j, ret;

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

434
	for (j = 0; j < numpages; j++) {
435
		/*
436 437
		 * Read the full oob until read_oob is fixed to handle single
		 * byte reads for 16 bit buswidth.
438
		 */
439
		ret = mtd_read_oob(mtd, offs, &ops);
440
		/* Ignore ECC errors when checking for BBM */
441
		if (ret && !mtd_is_bitflip_or_eccerr(ret))
442 443 444 445 446 447 448 449 450 451
			return ret;

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

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

L
Linus Torvalds 已提交
452 453
/**
 * create_bbt - [GENERIC] Create a bad block table by scanning the device
454 455 456 457 458
 * @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 已提交
459
 *
460 461
 * Create a bad block table by scanning the device for the given good/bad block
 * identify pattern.
L
Linus Torvalds 已提交
462
 */
463 464
static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
	struct nand_bbt_descr *bd, int chip)
L
Linus Torvalds 已提交
465 466
{
	struct nand_chip *this = mtd->priv;
467
	int i, numblocks, numpages, scanlen;
L
Linus Torvalds 已提交
468 469
	int startblock;
	loff_t from;
470
	size_t readlen;
L
Linus Torvalds 已提交
471

472
	pr_info("Scanning device for bad blocks\n");
L
Linus Torvalds 已提交
473 474

	if (bd->options & NAND_BBT_SCANALLPAGES)
475
		numpages = 1 << (this->bbt_erase_shift - this->page_shift);
476
	else if (bd->options & NAND_BBT_SCAN2NDPAGE)
477
		numpages = 2;
478
	else
479
		numpages = 1;
480 481 482

	if (!(bd->options & NAND_BBT_SCANEMPTY)) {
		/* We need only read few bytes from the OOB area */
483
		scanlen = 0;
484 485
		readlen = bd->len;
	} else {
486
		/* Full page content should be read */
J
Joern Engel 已提交
487
		scanlen = mtd->writesize + mtd->oobsize;
488
		readlen = numpages * mtd->writesize;
489
	}
L
Linus Torvalds 已提交
490 491

	if (chip == -1) {
492 493 494 495
		/*
		 * Note that numblocks is 2 * (real numblocks) here, see i+=2
		 * below as it makes shifting and masking less painful
		 */
L
Linus Torvalds 已提交
496 497 498 499 500
		numblocks = mtd->size >> (this->bbt_erase_shift - 1);
		startblock = 0;
		from = 0;
	} else {
		if (chip >= this->numchips) {
501
			pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
502
			       chip + 1, this->numchips);
503
			return -EINVAL;
L
Linus Torvalds 已提交
504 505 506 507
		}
		numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
		startblock = chip * numblocks;
		numblocks += startblock;
508
		from = (loff_t)startblock << (this->bbt_erase_shift - 1);
L
Linus Torvalds 已提交
509
	}
510

511
	if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
512
		from += mtd->erasesize - (mtd->writesize * numpages);
513

L
Linus Torvalds 已提交
514
	for (i = startblock; i < numblocks;) {
515
		int ret;
516

517 518
		BUG_ON(bd->options & NAND_BBT_NO_OOB);

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

		if (ret < 0)
			return ret;

		if (ret) {
			this->bbt[i >> 3] |= 0x03 << (i & 0x6);
530
			pr_warn("Bad eraseblock %d at 0x%012llx\n",
531
				i >> 1, (unsigned long long)from);
532
			mtd->ecc_stats.badblocks++;
L
Linus Torvalds 已提交
533
		}
534

L
Linus Torvalds 已提交
535 536 537
		i += 2;
		from += (1 << this->bbt_erase_shift);
	}
538
	return 0;
L
Linus Torvalds 已提交
539 540 541 542
}

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

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

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

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

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

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

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

/**
 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
623 624 625 626
 * @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 已提交
627
 *
628 629
 * Search and read the bad block table(s).
 */
630 631 632
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 已提交
633 634
{
	/* Search the primary table */
635
	search_bbt(mtd, buf, td);
636

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

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

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

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

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

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

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

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

L
Linus Torvalds 已提交
751
		bbtoffs = chip * (numblocks >> 2);
752

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

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

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

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

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

816
		/* Walk through the memory table */
817
		for (i = 0; i < numblocks;) {
L
Linus Torvalds 已提交
818 819
			uint8_t dat;
			dat = this->bbt[bbtoffs + (i >> 2)];
820
			for (j = 0; j < 4; j++, i++) {
L
Linus Torvalds 已提交
821
				int sftcnt = (i << (3 - sft)) & sftmsk;
822
				/* Do not store the reserved bbt blocks! */
823 824
				buf[offs + (i >> sft)] &=
					~(msk[dat & 0x03] << sftcnt);
L
Linus Torvalds 已提交
825 826 827
				dat >>= 2;
			}
		}
828

829
		memset(&einfo, 0, sizeof(einfo));
L
Linus Torvalds 已提交
830
		einfo.mtd = mtd;
831
		einfo.addr = to;
L
Linus Torvalds 已提交
832
		einfo.len = 1 << this->bbt_erase_shift;
833
		res = nand_erase_nand(mtd, &einfo, 1);
834 835
		if (res < 0)
			goto outerr;
836

837 838 839
		res = scan_write_bbt(mtd, to, len, buf,
				td->options & NAND_BBT_NO_OOB ? NULL :
				&buf[len]);
840 841 842
		if (res < 0)
			goto outerr;

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

L
Linus Torvalds 已提交
846 847
		/* Mark it as used */
		td->pages[chip] = page;
848
	}
L
Linus Torvalds 已提交
849
	return 0;
850 851

 outerr:
852
	pr_warn("nand_bbt: error while writing bad block table %d\n", res);
853
	return res;
L
Linus Torvalds 已提交
854 855 856 857
}

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

868
	bd->options &= ~NAND_BBT_SCANEMPTY;
869
	return create_bbt(mtd, this->buffers->databuf, bd, -1);
L
Linus Torvalds 已提交
870 871 872
}

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

891
	/* Do we have a bbt per chip? */
892
	if (td->options & NAND_BBT_PERCHIP)
L
Linus Torvalds 已提交
893
		chips = this->numchips;
894
	else
L
Linus Torvalds 已提交
895
		chips = 1;
896

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

936 937 938 939 940 941 942 943 944 945 946 947 948 949
		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;
		}

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

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

977 978 979 980 981 982
		/* Update version numbers before writing */
		if (md) {
			td->version[i] = max(td->version[i], md->version[i]);
			md->version[i] = td->version[i];
		}

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

990
		/* Write the mirror bad block table to the device? */
L
Linus Torvalds 已提交
991
		if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
992
			res = write_bbt(mtd, buf, md, td, chipsel);
L
Linus Torvalds 已提交
993 994 995 996
			if (res < 0)
				return res;
		}
	}
997
	return 0;
L
Linus Torvalds 已提交
998 999 1000
}

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

1014
	/* Do we have a bbt per chip? */
L
Linus Torvalds 已提交
1015 1016 1017 1018 1019 1020
	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);
1021 1022
	}

L
Linus Torvalds 已提交
1023 1024 1025
	for (i = 0; i < chips; i++) {
		if ((td->options & NAND_BBT_ABSPAGE) ||
		    !(td->options & NAND_BBT_WRITE)) {
1026 1027
			if (td->pages[i] == -1)
				continue;
L
Linus Torvalds 已提交
1028
			block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
1029
			block <<= 1;
L
Linus Torvalds 已提交
1030 1031 1032 1033
			oldval = this->bbt[(block >> 3)];
			newval = oldval | (0x2 << (block & 0x06));
			this->bbt[(block >> 3)] = newval;
			if ((oldval != newval) && td->reserved_block_code)
1034
				nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
L
Linus Torvalds 已提交
1035 1036 1037 1038 1039
			continue;
		}
		update = 0;
		if (td->options & NAND_BBT_LASTBLOCK)
			block = ((i + 1) * nrblocks) - td->maxblocks;
1040
		else
L
Linus Torvalds 已提交
1041
			block = i * nrblocks;
1042
		block <<= 1;
L
Linus Torvalds 已提交
1043 1044 1045 1046
		for (j = 0; j < td->maxblocks; j++) {
			oldval = this->bbt[(block >> 3)];
			newval = oldval | (0x2 << (block & 0x06));
			this->bbt[(block >> 3)] = newval;
1047 1048
			if (oldval != newval)
				update = 1;
L
Linus Torvalds 已提交
1049
			block += 2;
1050
		}
1051 1052 1053 1054 1055
		/*
		 * 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 已提交
1056
		if (update && td->reserved_block_code)
1057
			nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
L
Linus Torvalds 已提交
1058 1059 1060
	}
}

1061 1062
/**
 * verify_bbt_descr - verify the bad block description
1063 1064
 * @mtd: MTD device structure
 * @bd: the table to verify
1065 1066 1067 1068 1069 1070 1071
 *
 * 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;
1072 1073
	u32 pattern_len;
	u32 bits;
1074 1075 1076 1077
	u32 table_size;

	if (!bd)
		return;
1078 1079 1080 1081

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

1082
	BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
1083
			!(this->bbt_options & NAND_BBT_USE_FLASH));
1084 1085 1086 1087 1088 1089
	BUG_ON(!bits);

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

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

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

	/* Allocate a temporary buffer for one eraseblock incl. oob */
	len = (1 << this->bbt_erase_shift);
	len += (len >> this->page_shift) * mtd->oobsize;
1156
	buf = vmalloc(len);
L
Linus Torvalds 已提交
1157
	if (!buf) {
1158
		kfree(this->bbt);
L
Linus Torvalds 已提交
1159 1160 1161
		this->bbt = NULL;
		return -ENOMEM;
	}
1162

1163
	/* Is the bbt at a given page? */
L
Linus Torvalds 已提交
1164
	if (td->options & NAND_BBT_ABSPAGE) {
1165
		read_abs_bbts(mtd, buf, td, md);
1166
	} else {
L
Linus Torvalds 已提交
1167
		/* Search the bad block table using a pattern in oob */
1168
		search_read_bbts(mtd, buf, td, md);
1169
	}
L
Linus Torvalds 已提交
1170

1171
	res = check_create(mtd, buf, bd);
1172

L
Linus Torvalds 已提交
1173
	/* Prevent the bbt regions from erasing / writing */
1174
	mark_bbt_region(mtd, td);
L
Linus Torvalds 已提交
1175
	if (md)
1176
		mark_bbt_region(mtd, md);
1177

1178
	vfree(buf);
L
Linus Torvalds 已提交
1179 1180 1181 1182
	return res;
}

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

1208
	/* Do we have a bbt per chip? */
L
Linus Torvalds 已提交
1209
	if (td->options & NAND_BBT_PERCHIP) {
1210
		chip = (int)(offs >> this->chip_shift);
L
Linus Torvalds 已提交
1211 1212 1213 1214 1215 1216 1217 1218
		chipsel = chip;
	} else {
		chip = 0;
		chipsel = -1;
	}

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

1221
	/* Write the bad block table to the device? */
1222
	if (td->options & NAND_BBT_WRITE) {
1223
		res = write_bbt(mtd, buf, td, md, chipsel);
L
Linus Torvalds 已提交
1224 1225 1226
		if (res < 0)
			goto out;
	}
1227
	/* Write the mirror bad block table to the device? */
1228
	if (md && (md->options & NAND_BBT_WRITE)) {
1229
		res = write_bbt(mtd, buf, md, td, chipsel);
L
Linus Torvalds 已提交
1230 1231
	}

1232 1233
 out:
	kfree(buf);
L
Linus Torvalds 已提交
1234 1235 1236
	return res;
}

1237 1238 1239 1240
/*
 * Define some generic bad / good block scan pattern which are used
 * while scanning a device for factory marked good / bad blocks.
 */
L
Linus Torvalds 已提交
1241 1242
static uint8_t scan_ff_pattern[] = { 0xff, 0xff };

1243
/* Generic flash bbt descriptors */
L
Linus Torvalds 已提交
1244 1245 1246 1247
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 = {
1248
	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
L
Linus Torvalds 已提交
1249 1250 1251 1252
		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
	.offs =	8,
	.len = 4,
	.veroffs = 12,
1253
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
L
Linus Torvalds 已提交
1254 1255 1256 1257
	.pattern = bbt_pattern
};

static struct nand_bbt_descr bbt_mirror_descr = {
1258
	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
L
Linus Torvalds 已提交
1259 1260 1261 1262
		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
	.offs =	8,
	.len = 4,
	.veroffs = 12,
1263
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
L
Linus Torvalds 已提交
1264 1265 1266
	.pattern = mirror_pattern
};

1267
static struct nand_bbt_descr bbt_main_no_oob_descr = {
1268 1269 1270 1271 1272
	.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,
1273
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1274 1275 1276
	.pattern = bbt_pattern
};

1277
static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
1278 1279 1280 1281 1282
	.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,
1283
	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1284 1285 1286
	.pattern = mirror_pattern
};

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

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

	if (!this->badblock_pattern)
1345
		nand_create_badblock_pattern(this);
1346

1347
	return nand_scan_bbt(mtd, this->badblock_pattern);
L
Linus Torvalds 已提交
1348 1349 1350
}

/**
1351
 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1352 1353 1354 1355
 * @mtd: MTD device structure
 * @offs: offset in the device
 * @allowbbt: allow access to bad block table region
 */
1356
int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
L
Linus Torvalds 已提交
1357 1358 1359
{
	struct nand_chip *this = mtd->priv;
	int block;
1360
	uint8_t res;
1361

L
Linus Torvalds 已提交
1362
	/* Get block number * 2 */
1363
	block = (int)(offs >> (this->bbt_erase_shift - 1));
L
Linus Torvalds 已提交
1364 1365
	res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;

1366 1367 1368
	pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: "
			"(block %d) 0x%02x\n",
			(unsigned int)offs, block >> 1, res);
L
Linus Torvalds 已提交
1369 1370

	switch ((int)res) {
1371 1372 1373 1374 1375 1376
	case 0x00:
		return 0;
	case 0x01:
		return 1;
	case 0x02:
		return allowbbt ? 0 : 1;
L
Linus Torvalds 已提交
1377 1378 1379 1380
	}
	return 1;
}

1381 1382
EXPORT_SYMBOL(nand_scan_bbt);
EXPORT_SYMBOL(nand_default_bbt);
1383
EXPORT_SYMBOL_GPL(nand_update_bbt);