nand_bbt.c 39.5 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
 *
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14
 *  Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
 *
 * 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 17 18 19 20 21 22 23 24 25
 * depending on the options in the BBT descriptor(s). If no flash based BBT
 * (NAND_USE_FLASH_BBT) is specified then the device is scanned for factory
 * 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
 * version number than 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 39 40 41
 * 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
 * option NAND_USE_FLASH_BBT_NO_OOB should be used: 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 65 66 67 68
 */

#include <linux/slab.h>
#include <linux/types.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/nand_ecc.h>
#include <linux/bitops.h>
#include <linux/delay.h>
69
#include <linux/vmalloc.h>
L
Linus Torvalds 已提交
70

71 72 73 74 75 76 77 78 79 80
static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
{
	int ret;

	ret = memcmp(buf, td->pattern, td->len);
	if (!ret)
		return ret;
	return -1;
}

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

99 100 101
	if (td->options & NAND_BBT_NO_OOB)
		return check_pattern_no_oob(buf, td);

102
	end = paglen + td->offs;
L
Linus Torvalds 已提交
103 104 105 106 107
	if (td->options & NAND_BBT_SCANEMPTY) {
		for (i = 0; i < end; i++) {
			if (p[i] != 0xff)
				return -1;
		}
108
	}
109
	p += end;
110

L
Linus Torvalds 已提交
111 112 113 114 115 116
	/* Compare the pattern */
	for (i = 0; i < td->len; i++) {
		if (p[i] != td->pattern[i])
			return -1;
	}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	/* Check both positions 1 and 6 for pattern? */
	if (td->options & NAND_BBT_SCANBYTE1AND6) {
		if (td->options & NAND_BBT_SCANEMPTY) {
			p += td->len;
			end += NAND_SMALL_BADBLOCK_POS - td->offs;
			/* Check region between positions 1 and 6 */
			for (i = 0; i < NAND_SMALL_BADBLOCK_POS - td->offs - td->len;
					i++) {
				if (*p++ != 0xff)
					return -1;
			}
		}
		else {
			p += NAND_SMALL_BADBLOCK_POS - td->offs;
		}
		/* Compare the pattern */
		for (i = 0; i < td->len; i++) {
			if (p[i] != td->pattern[i])
				return -1;
		}
	}

L
Linus Torvalds 已提交
139
	if (td->options & NAND_BBT_SCANEMPTY) {
140 141
		p += td->len;
		end += td->len;
L
Linus Torvalds 已提交
142 143 144 145 146 147 148 149
		for (i = end; i < len; i++) {
			if (*p++ != 0xff)
				return -1;
		}
	}
	return 0;
}

150
/**
151 152 153 154 155
 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
 * @buf:	the buffer to search
 * @td:		search pattern descriptor
 *
 * Check for a pattern at the given place. Used to search bad block
156
 * tables and good / bad block identifiers. Same as check_pattern, but
157
 * no optional empty check
158 159
 *
*/
160
static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
161 162 163 164 165 166
{
	int i;
	uint8_t *p = buf;

	/* Compare the pattern */
	for (i = 0; i < td->len; i++) {
167
		if (p[td->offs + i] != td->pattern[i])
168 169
			return -1;
	}
170 171 172 173 174 175 176
	/* Need to check location 1 AND 6? */
	if (td->options & NAND_BBT_SCANBYTE1AND6) {
		for (i = 0; i < td->len; i++) {
			if (p[NAND_SMALL_BADBLOCK_POS + i] != td->pattern[i])
				return -1;
		}
	}
177 178 179
	return 0;
}

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
/**
 * add_marker_len - compute the length of the marker in data area
 * @td:		BBT descriptor used for computation
 *
 * The length will be 0 if the markeris located in OOB area.
 */
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 已提交
199 200 201 202 203 204
/**
 * read_bbt - [GENERIC] Read the bad block table starting from page
 * @mtd:	MTD device structure
 * @buf:	temporary buffer
 * @page:	the starting page
 * @num:	the number of bbt descriptors to read
205
 * @td:		the bbt describtion table
L
Linus Torvalds 已提交
206 207 208 209 210
 * @offs:	offset in the memory table
 *
 * Read the bad block table starting from page.
 *
 */
211
static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
212
		struct nand_bbt_descr *td, int offs)
L
Linus Torvalds 已提交
213 214 215 216 217
{
	int res, i, j, act = 0;
	struct nand_chip *this = mtd->priv;
	size_t retlen, len, totlen;
	loff_t from;
218
	int bits = td->options & NAND_BBT_NRBITS_MSK;
L
Linus Torvalds 已提交
219
	uint8_t msk = (uint8_t) ((1 << bits) - 1);
220
	u32 marker_len;
221
	int reserved_block_code = td->reserved_block_code;
L
Linus Torvalds 已提交
222 223

	totlen = (num * bits) >> 3;
224
	marker_len = add_marker_len(td);
225
	from = ((loff_t) page) << this->page_shift;
226

L
Linus Torvalds 已提交
227
	while (totlen) {
228
		len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
229 230 231 232 233 234 235 236 237
		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;
		}
238
		res = mtd->read(mtd, from, len, &retlen, buf);
L
Linus Torvalds 已提交
239 240
		if (res < 0) {
			if (retlen != len) {
241
				printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
L
Linus Torvalds 已提交
242 243
				return res;
			}
244
			printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
245
		}
L
Linus Torvalds 已提交
246 247 248 249 250 251 252 253

		/* 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;
254
				if (reserved_block_code && (tmp == reserved_block_code)) {
255 256
					printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%012llx\n",
					       (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
L
Linus Torvalds 已提交
257
					this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
258
					mtd->ecc_stats.bbtblocks++;
L
Linus Torvalds 已提交
259 260 261 262
					continue;
				}
				/* Leave it for now, if its matured we can move this
				 * message to MTD_DEBUG_LEVEL0 */
263 264
				printk(KERN_DEBUG "nand_read_bbt: Bad block at 0x%012llx\n",
				       (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
265
				/* Factory marked bad or worn out ? */
L
Linus Torvalds 已提交
266 267 268 269
				if (tmp == 0)
					this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
				else
					this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
270
				mtd->ecc_stats.badblocks++;
271
			}
L
Linus Torvalds 已提交
272 273 274 275 276 277 278 279 280 281 282
		}
		totlen -= len;
		from += len;
	}
	return 0;
}

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

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
/*
 * BBT marker is in the first page, no OOB.
 */
static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
			 struct nand_bbt_descr *td)
{
	size_t retlen;
	size_t len;

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

	return mtd->read(mtd, offs, len, &retlen, buf);
}

331 332 333
/*
 * Scan read raw data from flash
 */
334
static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
335 336 337
			 size_t len)
{
	struct mtd_oob_ops ops;
338
	int res;
339 340 341 342 343

	ops.mode = MTD_OOB_RAW;
	ops.ooboffs = 0;
	ops.ooblen = mtd->oobsize;

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

	while (len > 0) {
		if (len <= mtd->writesize) {
			ops.oobbuf = buf + len;
			ops.datbuf = buf;
			ops.len = len;
			return mtd->read_oob(mtd, offs, &ops);
		} else {
			ops.oobbuf = buf + mtd->writesize;
			ops.datbuf = buf;
			ops.len = mtd->writesize;
			res = mtd->read_oob(mtd, offs, &ops);

			if (res)
				return res;
		}

		buf += mtd->oobsize + mtd->writesize;
		len -= mtd->writesize;
	}
	return 0;
365 366
}

367 368 369 370 371 372 373 374 375
static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
			 size_t len, struct nand_bbt_descr *td)
{
	if (td->options & NAND_BBT_NO_OOB)
		return scan_read_raw_data(mtd, buf, offs, td);
	else
		return scan_read_raw_oob(mtd, buf, offs, len);
}

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
/*
 * Scan write data with oob to flash
 */
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;

	ops.mode = MTD_OOB_PLACE;
	ops.ooboffs = 0;
	ops.ooblen = mtd->oobsize;
	ops.datbuf = buf;
	ops.oobbuf = oob;
	ops.len = len;

	return mtd->write_oob(mtd, offs, &ops);
}

394 395 396 397 398 399 400 401 402
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 已提交
403 404 405 406
/**
 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
 * @mtd:	MTD device structure
 * @buf:	temporary buffer
407
 * @td:		descriptor for the bad block table
L
Linus Torvalds 已提交
408 409 410 411 412 413
 * @md:		descriptor for the bad block table mirror
 *
 * Read the bad block table(s) for all chips starting at a given page
 * We assume that the bbt bits are in consecutive order.
 *
*/
414 415
static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
			 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
L
Linus Torvalds 已提交
416 417 418
{
	struct nand_chip *this = mtd->priv;

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

428
	/* Read the mirror version, if available */
L
Linus Torvalds 已提交
429
	if (md && (md->options & NAND_BBT_VERSION)) {
430
		scan_read_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
431 432
			      mtd->writesize, td);
		md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
433 434
		printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
		       md->pages[0], md->version[0]);
L
Linus Torvalds 已提交
435 436 437 438
	}
	return 1;
}

439 440 441 442 443 444 445 446 447
/*
 * Scan a given block full
 */
static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
			   loff_t offs, uint8_t *buf, size_t readlen,
			   int scanlen, int len)
{
	int ret, j;

448
	ret = scan_read_raw_oob(mtd, buf, offs, readlen);
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
	if (ret)
		return ret;

	for (j = 0; j < len; j++, buf += scanlen) {
		if (check_pattern(buf, scanlen, mtd->writesize, bd))
			return 1;
	}
	return 0;
}

/*
 * Scan a given block partially
 */
static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
			   loff_t offs, uint8_t *buf, int len)
{
	struct mtd_oob_ops ops;
	int j, ret;

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

	for (j = 0; j < len; j++) {
		/*
		 * Read the full oob until read_oob is fixed to
		 * handle single byte reads for 16 bit
		 * buswidth
		 */
		ret = mtd->read_oob(mtd, offs, &ops);
		if (ret)
			return ret;

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

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

L
Linus Torvalds 已提交
492 493 494 495 496 497 498 499 500 501 502
/**
 * create_bbt - [GENERIC] Create a bad block table by scanning the device
 * @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
 *
 * Create a bad block table by scanning the device
 * for the given good/bad block identify pattern
 */
503 504
static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
	struct nand_bbt_descr *bd, int chip)
L
Linus Torvalds 已提交
505 506
{
	struct nand_chip *this = mtd->priv;
507
	int i, numblocks, len, scanlen;
L
Linus Torvalds 已提交
508 509
	int startblock;
	loff_t from;
510
	size_t readlen;
L
Linus Torvalds 已提交
511

512
	printk(KERN_INFO "Scanning device for bad blocks\n");
L
Linus Torvalds 已提交
513 514 515

	if (bd->options & NAND_BBT_SCANALLPAGES)
		len = 1 << (this->bbt_erase_shift - this->page_shift);
516 517 518 519
	else if (bd->options & NAND_BBT_SCAN2NDPAGE)
		len = 2;
	else
		len = 1;
520 521 522

	if (!(bd->options & NAND_BBT_SCANEMPTY)) {
		/* We need only read few bytes from the OOB area */
523
		scanlen = 0;
524 525
		readlen = bd->len;
	} else {
526
		/* Full page content should be read */
J
Joern Engel 已提交
527 528
		scanlen = mtd->writesize + mtd->oobsize;
		readlen = len * mtd->writesize;
529
	}
L
Linus Torvalds 已提交
530 531

	if (chip == -1) {
532 533
		/* Note that numblocks is 2 * (real numblocks) here, see i+=2
		 * below as it makes shifting and masking less painful */
L
Linus Torvalds 已提交
534 535 536 537 538
		numblocks = mtd->size >> (this->bbt_erase_shift - 1);
		startblock = 0;
		from = 0;
	} else {
		if (chip >= this->numchips) {
539 540
			printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
			       chip + 1, this->numchips);
541
			return -EINVAL;
L
Linus Torvalds 已提交
542 543 544 545
		}
		numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
		startblock = chip * numblocks;
		numblocks += startblock;
546
		from = (loff_t)startblock << (this->bbt_erase_shift - 1);
L
Linus Torvalds 已提交
547
	}
548

549
	if (this->options & NAND_BBT_SCANLASTPAGE)
550 551
		from += mtd->erasesize - (mtd->writesize * len);

L
Linus Torvalds 已提交
552
	for (i = startblock; i < numblocks;) {
553
		int ret;
554

555 556
		BUG_ON(bd->options & NAND_BBT_NO_OOB);

557 558 559 560 561 562 563 564 565 566 567
		if (bd->options & NAND_BBT_SCANALLPAGES)
			ret = scan_block_full(mtd, bd, from, buf, readlen,
					      scanlen, len);
		else
			ret = scan_block_fast(mtd, bd, from, buf, len);

		if (ret < 0)
			return ret;

		if (ret) {
			this->bbt[i >> 3] |= 0x03 << (i & 0x6);
568 569
			printk(KERN_WARNING "Bad eraseblock %d at 0x%012llx\n",
			       i >> 1, (unsigned long long)from);
570
			mtd->ecc_stats.badblocks++;
L
Linus Torvalds 已提交
571
		}
572

L
Linus Torvalds 已提交
573 574 575
		i += 2;
		from += (1 << this->bbt_erase_shift);
	}
576
	return 0;
L
Linus Torvalds 已提交
577 578 579 580 581 582 583 584 585
}

/**
 * search_bbt - [GENERIC] scan the device for a specific bad block table
 * @mtd:	MTD device structure
 * @buf:	temporary buffer
 * @td:		descriptor for the bad block table
 *
 * Read the bad block table by searching for a given ident pattern.
586
 * Search is preformed either from the beginning up or from the end of
L
Linus Torvalds 已提交
587 588
 * the device downwards. The search starts always at the start of a
 * block.
589
 * If the option NAND_BBT_PERCHIP is given, each chip is searched
L
Linus Torvalds 已提交
590
 * for a bbt, which contains the bad block information of this chip.
591
 * This is necessary to provide support for certain DOC devices.
L
Linus Torvalds 已提交
592
 *
593 594
 * The bbt ident pattern resides in the oob area of the first page
 * in a block.
L
Linus Torvalds 已提交
595
 */
596
static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
L
Linus Torvalds 已提交
597 598 599 600
{
	struct nand_chip *this = mtd->priv;
	int i, chips;
	int bits, startblock, block, dir;
J
Joern Engel 已提交
601
	int scanlen = mtd->writesize + mtd->oobsize;
L
Linus Torvalds 已提交
602
	int bbtblocks;
603
	int blocktopage = this->bbt_erase_shift - this->page_shift;
L
Linus Torvalds 已提交
604 605 606

	/* Search direction top -> down ? */
	if (td->options & NAND_BBT_LASTBLOCK) {
607
		startblock = (mtd->size >> this->bbt_erase_shift) - 1;
L
Linus Torvalds 已提交
608 609
		dir = -1;
	} else {
610
		startblock = 0;
L
Linus Torvalds 已提交
611
		dir = 1;
612 613
	}

L
Linus Torvalds 已提交
614 615 616 617 618 619 620 621 622
	/* Do we have a bbt per chip ? */
	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;
	}
623

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

L
Linus Torvalds 已提交
627 628
	for (i = 0; i < chips; i++) {
		/* Reset version information */
629
		td->version[i] = 0;
L
Linus Torvalds 已提交
630 631 632
		td->pages[i] = -1;
		/* Scan the maximum number of blocks */
		for (block = 0; block < td->maxblocks; block++) {
633

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

L
Linus Torvalds 已提交
637
			/* Read first page */
638
			scan_read_raw(mtd, buf, offs, mtd->writesize, td);
J
Joern Engel 已提交
639
			if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
640
				td->pages[i] = actblock << blocktopage;
L
Linus Torvalds 已提交
641
				if (td->options & NAND_BBT_VERSION) {
642 643
					offs = bbt_get_ver_offs(mtd, td);
					td->version[i] = buf[offs];
L
Linus Torvalds 已提交
644 645 646 647 648 649 650 651 652
				}
				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)
653
			printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
L
Linus Torvalds 已提交
654
		else
655 656
			printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i],
			       td->version[i]);
L
Linus Torvalds 已提交
657
	}
658
	return 0;
L
Linus Torvalds 已提交
659 660 661 662 663 664
}

/**
 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
 * @mtd:	MTD device structure
 * @buf:	temporary buffer
665
 * @td:		descriptor for the bad block table
L
Linus Torvalds 已提交
666 667 668 669
 * @md:		descriptor for the bad block table mirror
 *
 * Search and read the bad block table(s)
*/
670
static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
L
Linus Torvalds 已提交
671 672
{
	/* Search the primary table */
673
	search_bbt(mtd, buf, td);
674

L
Linus Torvalds 已提交
675 676
	/* Search the mirror table */
	if (md)
677
		search_bbt(mtd, buf, md);
678

L
Linus Torvalds 已提交
679
	/* Force result check */
680
	return 1;
L
Linus Torvalds 已提交
681 682
}

683
/**
L
Linus Torvalds 已提交
684 685 686 687
 * write_bbt - [GENERIC] (Re)write the bad block table
 *
 * @mtd:	MTD device structure
 * @buf:	temporary buffer
688
 * @td:		descriptor for the bad block table
L
Linus Torvalds 已提交
689 690 691 692 693 694
 * @md:		descriptor for the bad block table mirror
 * @chipsel:	selector for a specific chip, -1 for all
 *
 * (Re)write the bad block table
 *
*/
695
static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
696 697
		     struct nand_bbt_descr *td, struct nand_bbt_descr *md,
		     int chipsel)
L
Linus Torvalds 已提交
698 699 700 701 702
{
	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;
703
	int nrchips, bbtoffs, pageoffs, ooboffs;
L
Linus Torvalds 已提交
704 705
	uint8_t msk[4];
	uint8_t rcode = td->reserved_block_code;
706
	size_t retlen, len = 0;
L
Linus Torvalds 已提交
707
	loff_t to;
708 709 710 711 712 713
	struct mtd_oob_ops ops;

	ops.ooblen = mtd->oobsize;
	ops.ooboffs = 0;
	ops.datbuf = NULL;
	ops.mode = MTD_OOB_PLACE;
L
Linus Torvalds 已提交
714 715 716 717 718

	if (!rcode)
		rcode = 0xff;
	/* Write bad block table per chip rather than per device ? */
	if (td->options & NAND_BBT_PERCHIP) {
719
		numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
720
		/* Full device write or specific chip ? */
L
Linus Torvalds 已提交
721 722 723 724 725 726 727
		if (chipsel == -1) {
			nrchips = this->numchips;
		} else {
			nrchips = chipsel + 1;
			chip = chipsel;
		}
	} else {
728
		numblocks = (int)(mtd->size >> this->bbt_erase_shift);
L
Linus Torvalds 已提交
729
		nrchips = 1;
730 731
	}

L
Linus Torvalds 已提交
732 733
	/* Loop through the chips */
	for (; chip < nrchips; chip++) {
734 735 736

		/* There was already a version of the table, reuse the page
		 * This applies for absolute placement too, as we have the
L
Linus Torvalds 已提交
737 738 739 740 741
		 * page nr. in td->pages.
		 */
		if (td->pages[chip] != -1) {
			page = td->pages[chip];
			goto write;
742
		}
L
Linus Torvalds 已提交
743 744 745 746 747 748 749 750 751

		/* Automatic placement of the bad block table */
		/* Search direction top -> down ? */
		if (td->options & NAND_BBT_LASTBLOCK) {
			startblock = numblocks * (chip + 1) - 1;
			dir = -1;
		} else {
			startblock = chip * numblocks;
			dir = 1;
752
		}
L
Linus Torvalds 已提交
753 754 755 756

		for (i = 0; i < td->maxblocks; i++) {
			int block = startblock + dir * i;
			/* Check, if the block is bad */
757 758
			switch ((this->bbt[block >> 2] >>
				 (2 * (block & 0x03))) & 0x03) {
L
Linus Torvalds 已提交
759 760 761 762
			case 0x01:
			case 0x03:
				continue;
			}
763 764
			page = block <<
				(this->bbt_erase_shift - this->page_shift);
L
Linus Torvalds 已提交
765 766 767 768
			/* Check, if the block is used by the mirror table */
			if (!md || md->pages[chip] != page)
				goto write;
		}
769
		printk(KERN_ERR "No space left to write bad block table\n");
L
Linus Torvalds 已提交
770
		return -ENOSPC;
771
	write:
L
Linus Torvalds 已提交
772 773 774

		/* Set up shift count and masks for the flash table */
		bits = td->options & NAND_BBT_NRBITS_MSK;
775
		msk[2] = ~rcode;
L
Linus Torvalds 已提交
776
		switch (bits) {
777 778 779 780 781 782 783 784 785 786 787 788
		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 已提交
789 790
		default: return -EINVAL;
		}
791

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

L
Linus Torvalds 已提交
794 795 796 797 798 799 800
		to = ((loff_t) page) << this->page_shift;

		/* Must we save the block contents ? */
		if (td->options & NAND_BBT_SAVECONTENT) {
			/* Make it block aligned */
			to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
			len = 1 << this->bbt_erase_shift;
801
			res = mtd->read(mtd, to, len, &retlen, buf);
L
Linus Torvalds 已提交
802 803
			if (res < 0) {
				if (retlen != len) {
804 805 806
					printk(KERN_INFO "nand_bbt: Error "
					       "reading block for writing "
					       "the bad block table\n");
L
Linus Torvalds 已提交
807 808
					return res;
				}
809 810 811
				printk(KERN_WARNING "nand_bbt: ECC error "
				       "while reading block for writing "
				       "bad block table\n");
L
Linus Torvalds 已提交
812
			}
813
			/* Read oob data */
814
			ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
815 816
			ops.oobbuf = &buf[len];
			res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
817
			if (res < 0 || ops.oobretlen != ops.ooblen)
818 819
				goto outerr;

L
Linus Torvalds 已提交
820 821 822 823
			/* 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 */
824
			memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
825 826
			ooboffs = len + (pageoffs * mtd->oobsize);

827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
		} else if (td->options & NAND_BBT_NO_OOB) {
			ooboffs = 0;
			offs = td->len;
			/* the version byte */
			if (td->options & NAND_BBT_VERSION)
				offs++;
			/* Calc length */
			len = (size_t) (numblocks >> sft);
			len += offs;
			/* Make it page aligned ! */
			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 已提交
842 843 844 845
		} else {
			/* Calc length */
			len = (size_t) (numblocks >> sft);
			/* Make it page aligned ! */
846
			len = ALIGN(len, mtd->writesize);
L
Linus Torvalds 已提交
847
			/* Preset the buffer with 0xff */
848 849
			memset(buf, 0xff, len +
			       (len >> this->page_shift)* mtd->oobsize);
L
Linus Torvalds 已提交
850
			offs = 0;
851
			ooboffs = len;
L
Linus Torvalds 已提交
852
			/* Pattern is located in oob area of first page */
853
			memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
L
Linus Torvalds 已提交
854
		}
855

856 857 858
		if (td->options & NAND_BBT_VERSION)
			buf[ooboffs + td->veroffs] = td->version[chip];

L
Linus Torvalds 已提交
859
		/* walk through the memory table */
860
		for (i = 0; i < numblocks;) {
L
Linus Torvalds 已提交
861 862
			uint8_t dat;
			dat = this->bbt[bbtoffs + (i >> 2)];
863
			for (j = 0; j < 4; j++, i++) {
L
Linus Torvalds 已提交
864 865
				int sftcnt = (i << (3 - sft)) & sftmsk;
				/* Do not store the reserved bbt blocks ! */
866 867
				buf[offs + (i >> sft)] &=
					~(msk[dat & 0x03] << sftcnt);
L
Linus Torvalds 已提交
868 869 870
				dat >>= 2;
			}
		}
871

872
		memset(&einfo, 0, sizeof(einfo));
L
Linus Torvalds 已提交
873
		einfo.mtd = mtd;
874
		einfo.addr = to;
L
Linus Torvalds 已提交
875
		einfo.len = 1 << this->bbt_erase_shift;
876
		res = nand_erase_nand(mtd, &einfo, 1);
877 878
		if (res < 0)
			goto outerr;
879

880 881 882
		res = scan_write_bbt(mtd, to, len, buf,
				td->options & NAND_BBT_NO_OOB ? NULL :
				&buf[len]);
883 884 885
		if (res < 0)
			goto outerr;

886 887
		printk(KERN_DEBUG "Bad block table written to 0x%012llx, version "
		       "0x%02X\n", (unsigned long long)to, td->version[chip]);
888

L
Linus Torvalds 已提交
889 890
		/* Mark it as used */
		td->pages[chip] = page;
891
	}
L
Linus Torvalds 已提交
892
	return 0;
893 894 895 896 897

 outerr:
	printk(KERN_WARNING
	       "nand_bbt: Error while writing bad block table %d\n", res);
	return res;
L
Linus Torvalds 已提交
898 899 900 901 902 903 904
}

/**
 * nand_memory_bbt - [GENERIC] create a memory based bad block table
 * @mtd:	MTD device structure
 * @bd:		descriptor for the good/bad block search pattern
 *
905
 * The function creates a memory based bbt by scanning the device
L
Linus Torvalds 已提交
906 907
 * for manufacturer / software marked good / bad blocks
*/
908
static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
L
Linus Torvalds 已提交
909 910 911
{
	struct nand_chip *this = mtd->priv;

912
	bd->options &= ~NAND_BBT_SCANEMPTY;
913
	return create_bbt(mtd, this->buffers->databuf, bd, -1);
L
Linus Torvalds 已提交
914 915 916
}

/**
917
 * check_create - [GENERIC] create and write bbt(s) if necessary
L
Linus Torvalds 已提交
918 919 920 921 922
 * @mtd:	MTD device structure
 * @buf:	temporary buffer
 * @bd:		descriptor for the good/bad block search pattern
 *
 * The function checks the results of the previous call to read_bbt
923 924 925
 * 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
L
Linus Torvalds 已提交
926 927
 * version nr. of one table is less than the other
*/
928
static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
L
Linus Torvalds 已提交
929 930 931 932 933 934 935 936
{
	int i, chips, writeops, chipsel, res;
	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;

	/* Do we have a bbt per chip ? */
937
	if (td->options & NAND_BBT_PERCHIP)
L
Linus Torvalds 已提交
938
		chips = this->numchips;
939
	else
L
Linus Torvalds 已提交
940
		chips = 1;
941

L
Linus Torvalds 已提交
942 943 944 945 946 947
	for (i = 0; i < chips; i++) {
		writeops = 0;
		rd = NULL;
		rd2 = NULL;
		/* Per chip or per device ? */
		chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
L
Lucas De Marchi 已提交
948
		/* Mirrored table available ? */
L
Linus Torvalds 已提交
949 950 951 952 953 954 955
		if (md) {
			if (td->pages[i] == -1 && md->pages[i] == -1) {
				writeops = 0x03;
				goto create;
			}

			if (td->pages[i] == -1) {
956
				rd = md;
L
Linus Torvalds 已提交
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
				td->version[i] = md->version[i];
				writeops = 1;
				goto writecheck;
			}

			if (md->pages[i] == -1) {
				rd = td;
				md->version[i] = td->version[i];
				writeops = 2;
				goto writecheck;
			}

			if (td->version[i] == md->version[i]) {
				rd = td;
				if (!(td->options & NAND_BBT_VERSION))
					rd2 = md;
				goto writecheck;
974
			}
L
Linus Torvalds 已提交
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995

			if (((int8_t) (td->version[i] - md->version[i])) > 0) {
				rd = td;
				md->version[i] = td->version[i];
				writeops = 2;
			} else {
				rd = md;
				td->version[i] = md->version[i];
				writeops = 1;
			}

			goto writecheck;

		} else {
			if (td->pages[i] == -1) {
				writeops = 0x01;
				goto create;
			}
			rd = td;
			goto writecheck;
		}
996
	create:
L
Linus Torvalds 已提交
997 998
		/* Create the bad block table by scanning the device ? */
		if (!(td->options & NAND_BBT_CREATE))
999 1000
			continue;

L
Linus Torvalds 已提交
1001
		/* Create the table in memory by scanning the chip(s) */
1002 1003
		if (!(this->options & NAND_CREATE_EMPTY_BBT))
			create_bbt(mtd, buf, bd, chipsel);
1004

L
Linus Torvalds 已提交
1005 1006
		td->version[i] = 1;
		if (md)
1007
			md->version[i] = 1;
1008
	writecheck:
L
Linus Torvalds 已提交
1009 1010
		/* read back first ? */
		if (rd)
1011
			read_abs_bbt(mtd, buf, rd, chipsel);
L
Linus Torvalds 已提交
1012 1013
		/* If they weren't versioned, read both. */
		if (rd2)
1014
			read_abs_bbt(mtd, buf, rd2, chipsel);
L
Linus Torvalds 已提交
1015 1016 1017

		/* Write the bad block table to the device ? */
		if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1018
			res = write_bbt(mtd, buf, td, md, chipsel);
L
Linus Torvalds 已提交
1019 1020 1021
			if (res < 0)
				return res;
		}
1022

L
Linus Torvalds 已提交
1023 1024
		/* Write the mirror bad block table to the device ? */
		if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1025
			res = write_bbt(mtd, buf, md, td, chipsel);
L
Linus Torvalds 已提交
1026 1027 1028 1029
			if (res < 0)
				return res;
		}
	}
1030
	return 0;
L
Linus Torvalds 已提交
1031 1032 1033
}

/**
1034
 * mark_bbt_regions - [GENERIC] mark the bad block table regions
L
Linus Torvalds 已提交
1035 1036 1037 1038 1039 1040 1041
 * @mtd:	MTD device structure
 * @td:		bad block table descriptor
 *
 * The bad block table regions are marked as "bad" to prevent
 * accidental erasures / writes. The regions are identified by
 * the mark 0x02.
*/
1042
static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
L
Linus Torvalds 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
{
	struct nand_chip *this = mtd->priv;
	int i, j, chips, block, nrblocks, update;
	uint8_t oldval, newval;

	/* Do we have a bbt per chip ? */
	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);
1055 1056
	}

L
Linus Torvalds 已提交
1057 1058 1059
	for (i = 0; i < chips; i++) {
		if ((td->options & NAND_BBT_ABSPAGE) ||
		    !(td->options & NAND_BBT_WRITE)) {
1060 1061
			if (td->pages[i] == -1)
				continue;
L
Linus Torvalds 已提交
1062
			block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
1063
			block <<= 1;
L
Linus Torvalds 已提交
1064 1065 1066 1067
			oldval = this->bbt[(block >> 3)];
			newval = oldval | (0x2 << (block & 0x06));
			this->bbt[(block >> 3)] = newval;
			if ((oldval != newval) && td->reserved_block_code)
1068
				nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
L
Linus Torvalds 已提交
1069 1070 1071 1072 1073
			continue;
		}
		update = 0;
		if (td->options & NAND_BBT_LASTBLOCK)
			block = ((i + 1) * nrblocks) - td->maxblocks;
1074
		else
L
Linus Torvalds 已提交
1075
			block = i * nrblocks;
1076
		block <<= 1;
L
Linus Torvalds 已提交
1077 1078 1079 1080
		for (j = 0; j < td->maxblocks; j++) {
			oldval = this->bbt[(block >> 3)];
			newval = oldval | (0x2 << (block & 0x06));
			this->bbt[(block >> 3)] = newval;
1081 1082
			if (oldval != newval)
				update = 1;
L
Linus Torvalds 已提交
1083
			block += 2;
1084
		}
L
Linus Torvalds 已提交
1085 1086 1087 1088
		/* 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. */
		if (update && td->reserved_block_code)
1089
			nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
L
Linus Torvalds 已提交
1090 1091 1092
	}
}

1093 1094
/**
 * verify_bbt_descr - verify the bad block description
R
Randy Dunlap 已提交
1095 1096
 * @mtd:	MTD device structure
 * @bd:		the table to verify
1097 1098 1099 1100 1101 1102 1103
 *
 * 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;
1104 1105
	u32 pattern_len;
	u32 bits;
1106 1107 1108 1109
	u32 table_size;

	if (!bd)
		return;
1110 1111 1112 1113

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

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
	BUG_ON((this->options & NAND_USE_FLASH_BBT_NO_OOB) &&
			!(this->options & NAND_USE_FLASH_BBT));
	BUG_ON(!bits);

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

	if (bd->options & NAND_BBT_NO_OOB) {
		BUG_ON(!(this->options & NAND_USE_FLASH_BBT));
		BUG_ON(!(this->options & NAND_USE_FLASH_BBT_NO_OOB));
		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 已提交
1141 1142 1143 1144 1145
/**
 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
 * @mtd:	MTD device structure
 * @bd:		descriptor for the good/bad block search pattern
 *
1146
 * The function checks, if a bad block table(s) is/are already
L
Linus Torvalds 已提交
1147 1148 1149 1150 1151 1152 1153 1154
 * available. If not it scans the device for manufacturer
 * marked good / bad blocks and writes the bad block table(s) to
 * the selected place.
 *
 * The bad block table memory is allocated here. It must be freed
 * by calling the nand_free_bbt function.
 *
*/
1155
int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
L
Linus Torvalds 已提交
1156 1157 1158 1159 1160 1161 1162 1163
{
	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);
1164 1165
	/* Allocate memory (2bit per block) and clear the memory bad block table */
	this->bbt = kzalloc(len, GFP_KERNEL);
L
Linus Torvalds 已提交
1166
	if (!this->bbt) {
1167
		printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
L
Linus Torvalds 已提交
1168 1169 1170 1171 1172 1173
		return -ENOMEM;
	}

	/* If no primary table decriptor is given, scan the device
	 * to build a memory based bad block table
	 */
1174 1175
	if (!td) {
		if ((res = nand_memory_bbt(mtd, bd))) {
1176 1177
			printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
			kfree(this->bbt);
1178 1179 1180 1181
			this->bbt = NULL;
		}
		return res;
	}
1182 1183
	verify_bbt_descr(mtd, td);
	verify_bbt_descr(mtd, md);
L
Linus Torvalds 已提交
1184 1185 1186 1187

	/* Allocate a temporary buffer for one eraseblock incl. oob */
	len = (1 << this->bbt_erase_shift);
	len += (len >> this->page_shift) * mtd->oobsize;
1188
	buf = vmalloc(len);
L
Linus Torvalds 已提交
1189
	if (!buf) {
1190 1191
		printk(KERN_ERR "nand_bbt: Out of memory\n");
		kfree(this->bbt);
L
Linus Torvalds 已提交
1192 1193 1194
		this->bbt = NULL;
		return -ENOMEM;
	}
1195

L
Linus Torvalds 已提交
1196 1197
	/* Is the bbt at a given page ? */
	if (td->options & NAND_BBT_ABSPAGE) {
1198
		res = read_abs_bbts(mtd, buf, td, md);
1199
	} else {
L
Linus Torvalds 已提交
1200
		/* Search the bad block table using a pattern in oob */
1201
		res = search_read_bbts(mtd, buf, td, md);
1202
	}
L
Linus Torvalds 已提交
1203

1204
	if (res)
1205
		res = check_create(mtd, buf, bd);
1206

L
Linus Torvalds 已提交
1207
	/* Prevent the bbt regions from erasing / writing */
1208
	mark_bbt_region(mtd, td);
L
Linus Torvalds 已提交
1209
	if (md)
1210
		mark_bbt_region(mtd, md);
1211

1212
	vfree(buf);
L
Linus Torvalds 已提交
1213 1214 1215 1216
	return res;
}

/**
1217
 * nand_update_bbt - [NAND Interface] update bad block table(s)
L
Linus Torvalds 已提交
1218 1219 1220 1221 1222
 * @mtd:	MTD device structure
 * @offs:	the offset of the newly marked block
 *
 * The function updates the bad block table(s)
*/
1223
int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
L
Linus Torvalds 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
{
	struct nand_chip *this = mtd->priv;
	int len, res = 0, writeops = 0;
	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;
1238
	buf = kmalloc(len, GFP_KERNEL);
L
Linus Torvalds 已提交
1239
	if (!buf) {
1240
		printk(KERN_ERR "nand_update_bbt: Out of memory\n");
L
Linus Torvalds 已提交
1241 1242
		return -ENOMEM;
	}
1243

L
Linus Torvalds 已提交
1244 1245 1246 1247
	writeops = md != NULL ? 0x03 : 0x01;

	/* Do we have a bbt per chip ? */
	if (td->options & NAND_BBT_PERCHIP) {
1248
		chip = (int)(offs >> this->chip_shift);
L
Linus Torvalds 已提交
1249 1250 1251 1252 1253 1254 1255 1256
		chipsel = chip;
	} else {
		chip = 0;
		chipsel = -1;
	}

	td->version[chip]++;
	if (md)
1257
		md->version[chip]++;
L
Linus Torvalds 已提交
1258 1259 1260

	/* Write the bad block table to the device ? */
	if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1261
		res = write_bbt(mtd, buf, td, md, chipsel);
L
Linus Torvalds 已提交
1262 1263 1264 1265 1266
		if (res < 0)
			goto out;
	}
	/* Write the mirror bad block table to the device ? */
	if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1267
		res = write_bbt(mtd, buf, md, td, chipsel);
L
Linus Torvalds 已提交
1268 1269
	}

1270 1271
 out:
	kfree(buf);
L
Linus Torvalds 已提交
1272 1273 1274
	return res;
}

1275
/* Define some generic bad / good block scan pattern which are used
1276
 * while scanning a device for factory marked good / bad blocks. */
L
Linus Torvalds 已提交
1277 1278 1279
static uint8_t scan_ff_pattern[] = { 0xff, 0xff };

static struct nand_bbt_descr smallpage_flashbased = {
1280
	.options = NAND_BBT_SCAN2NDPAGE,
1281
	.offs = NAND_SMALL_BADBLOCK_POS,
L
Linus Torvalds 已提交
1282 1283 1284 1285 1286
	.len = 1,
	.pattern = scan_ff_pattern
};

static struct nand_bbt_descr largepage_flashbased = {
1287
	.options = NAND_BBT_SCAN2NDPAGE,
1288
	.offs = NAND_LARGE_BADBLOCK_POS,
L
Linus Torvalds 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
	.len = 2,
	.pattern = scan_ff_pattern
};

static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };

static struct nand_bbt_descr agand_flashbased = {
	.options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
	.offs = 0x20,
	.len = 6,
	.pattern = scan_agand_pattern
};

/* Generic flash bbt decriptors
*/
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 = {
1308
	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
L
Linus Torvalds 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317
		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
	.offs =	8,
	.len = 4,
	.veroffs = 12,
	.maxblocks = 4,
	.pattern = bbt_pattern
};

static struct nand_bbt_descr bbt_mirror_descr = {
1318
	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
L
Linus Torvalds 已提交
1319 1320 1321 1322 1323 1324 1325 1326
		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
	.offs =	8,
	.len = 4,
	.veroffs = 12,
	.maxblocks = 4,
	.pattern = mirror_pattern
};

1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
static struct nand_bbt_descr bbt_main_no_bbt_descr = {
	.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,
	.maxblocks = 4,
	.pattern = bbt_pattern
};

static struct nand_bbt_descr bbt_mirror_no_bbt_descr = {
	.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,
	.maxblocks = 4,
	.pattern = mirror_pattern
};

1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
#define BBT_SCAN_OPTIONS (NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE | \
		NAND_BBT_SCANBYTE1AND6)
/**
 * nand_create_default_bbt_descr - [Internal] Creates a BBT descriptor structure
 * @this:	NAND chip to create descriptor for
 *
 * This function allocates and initializes a nand_bbt_descr for BBM detection
 * based on the properties of "this". The new descriptor is stored in
 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
 * passed to this function.
 *
 * TODO: Handle other flags, replace other static structs
 *        (e.g. handle NAND_BBT_FLASH for flash-based BBT,
 *             replace smallpage_flashbased)
 *
 */
static int nand_create_default_bbt_descr(struct nand_chip *this)
{
	struct nand_bbt_descr *bd;
	if (this->badblock_pattern) {
		printk(KERN_WARNING "BBT descr already allocated; not replacing.\n");
		return -EINVAL;
	}
	bd = kzalloc(sizeof(*bd), GFP_KERNEL);
	if (!bd) {
		printk(KERN_ERR "nand_create_default_bbt_descr: Out of memory\n");
		return -ENOMEM;
	}
	bd->options = this->options & BBT_SCAN_OPTIONS;
	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 已提交
1384
/**
1385
 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
L
Linus Torvalds 已提交
1386 1387 1388 1389 1390 1391
 * @mtd:	MTD device structure
 *
 * This function selects the default bad block table
 * support for the device and calls the nand_scan_bbt function
 *
*/
1392
int nand_default_bbt(struct mtd_info *mtd)
L
Linus Torvalds 已提交
1393 1394
{
	struct nand_chip *this = mtd->priv;
1395 1396

	/* Default for AG-AND. We must use a flash based
L
Linus Torvalds 已提交
1397 1398 1399
	 * bad block table as the devices have factory marked
	 * _good_ blocks. Erasing those blocks leads to loss
	 * of the good / bad information, so we _must_ store
1400
	 * this information in a good / bad table during
L
Linus Torvalds 已提交
1401
	 * startup
1402
	 */
L
Linus Torvalds 已提交
1403 1404
	if (this->options & NAND_IS_AND) {
		/* Use the default pattern descriptors */
1405
		if (!this->bbt_td) {
L
Linus Torvalds 已提交
1406 1407
			this->bbt_td = &bbt_main_descr;
			this->bbt_md = &bbt_mirror_descr;
1408
		}
L
Linus Torvalds 已提交
1409
		this->options |= NAND_USE_FLASH_BBT;
1410
		return nand_scan_bbt(mtd, &agand_flashbased);
L
Linus Torvalds 已提交
1411
	}
1412

L
Linus Torvalds 已提交
1413 1414
	/* Is a flash based bad block table requested ? */
	if (this->options & NAND_USE_FLASH_BBT) {
1415 1416
		/* Use the default pattern descriptors */
		if (!this->bbt_td) {
1417 1418 1419 1420 1421 1422 1423
			if (this->options & NAND_USE_FLASH_BBT_NO_OOB) {
				this->bbt_td = &bbt_main_no_bbt_descr;
				this->bbt_md = &bbt_mirror_no_bbt_descr;
			} else {
				this->bbt_td = &bbt_main_descr;
				this->bbt_md = &bbt_mirror_descr;
			}
L
Linus Torvalds 已提交
1424 1425
		}
		if (!this->badblock_pattern) {
J
Joern Engel 已提交
1426
			this->badblock_pattern = (mtd->writesize > 512) ? &largepage_flashbased : &smallpage_flashbased;
L
Linus Torvalds 已提交
1427 1428 1429 1430
		}
	} else {
		this->bbt_td = NULL;
		this->bbt_md = NULL;
1431 1432
		if (!this->badblock_pattern)
			nand_create_default_bbt_descr(this);
L
Linus Torvalds 已提交
1433
	}
1434
	return nand_scan_bbt(mtd, this->badblock_pattern);
L
Linus Torvalds 已提交
1435 1436 1437
}

/**
1438
 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
L
Linus Torvalds 已提交
1439 1440 1441 1442 1443
 * @mtd:	MTD device structure
 * @offs:	offset in the device
 * @allowbbt:	allow access to bad block table region
 *
*/
1444
int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
L
Linus Torvalds 已提交
1445 1446 1447
{
	struct nand_chip *this = mtd->priv;
	int block;
1448
	uint8_t res;
1449

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

1454 1455
	DEBUG(MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
	      (unsigned int)offs, block >> 1, res);
L
Linus Torvalds 已提交
1456 1457

	switch ((int)res) {
1458 1459 1460 1461 1462 1463
	case 0x00:
		return 0;
	case 0x01:
		return 1;
	case 0x02:
		return allowbbt ? 0 : 1;
L
Linus Torvalds 已提交
1464 1465 1466 1467
	}
	return 1;
}

1468 1469
EXPORT_SYMBOL(nand_scan_bbt);
EXPORT_SYMBOL(nand_default_bbt);