nand_bbt.c 32.9 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
 *  Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
 *
9
 * $Id: nand_bbt.c,v 1.36 2005/11/07 11:14:30 gleixner Exp $
L
Linus Torvalds 已提交
10 11 12 13 14 15 16
 *
 * 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:
 *
17 18 19
 * When nand_scan_bbt is called, then it tries to find the bad block table
 * depending on the options in the bbt descriptor(s). If a bbt is found
 * then the contents are read and the memory based bbt is created. If a
L
Linus Torvalds 已提交
20
 * mirrored bbt is selected then the mirror is searched too and the
21
 * versions are compared. If the mirror has a greater version number
L
Linus Torvalds 已提交
22 23
 * than the mirror bbt is used to build the memory based bbt.
 * If the tables are not versioned, then we "or" the bad block information.
24 25 26
 * If one of the bbt's 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
 * good / bad blocks and the bad block tables are created.
L
Linus Torvalds 已提交
27
 *
28
 * For manufacturer created bbts like the one found on M-SYS DOC devices
L
Linus Torvalds 已提交
29 30
 * the bbt is searched and read but never created
 *
31 32 33
 * The autogenerated bad block table is located in the last good blocks
 * of the device. The table is mirrored, so it can be updated eventually.
 * The table is marked in the oob area with an ident pattern and a version
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43 44 45
 * number which indicates which of both tables is more up to date.
 *
 * The table uses 2 bits per block
 * 11b: 	block is good
 * 00b: 	block is factory marked bad
 * 01b, 10b: 	block is marked bad due to wear
 *
 * 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
46
 *
L
Linus Torvalds 已提交
47 48 49 50
 * 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
51
 * - the space necessary for a bbt in FLASH does not exceed a block boundary
52
 *
L
Linus Torvalds 已提交
53 54 55 56 57 58 59 60 61 62
 */

#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/mtd/compatmac.h>
#include <linux/bitops.h>
#include <linux/delay.h>
63
#include <linux/vmalloc.h>
L
Linus Torvalds 已提交
64

65
/**
L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73 74 75 76 77
 * 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
 *
*/
78
static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
L
Linus Torvalds 已提交
79
{
80
	int i, end = 0;
L
Linus Torvalds 已提交
81 82
	uint8_t *p = buf;

83
	end = paglen + td->offs;
L
Linus Torvalds 已提交
84 85 86 87 88
	if (td->options & NAND_BBT_SCANEMPTY) {
		for (i = 0; i < end; i++) {
			if (p[i] != 0xff)
				return -1;
		}
89
	}
90
	p += end;
91

L
Linus Torvalds 已提交
92 93 94 95 96 97 98
	/* Compare the pattern */
	for (i = 0; i < td->len; i++) {
		if (p[i] != td->pattern[i])
			return -1;
	}

	if (td->options & NAND_BBT_SCANEMPTY) {
99 100
		p += td->len;
		end += td->len;
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108
		for (i = end; i < len; i++) {
			if (*p++ != 0xff)
				return -1;
		}
	}
	return 0;
}

109
/**
110 111 112 113 114
 * 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
115
 * tables and good / bad block identifiers. Same as check_pattern, but
116
 * no optional empty check
117 118
 *
*/
119
static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
120 121 122 123 124 125
{
	int i;
	uint8_t *p = buf;

	/* Compare the pattern */
	for (i = 0; i < td->len; i++) {
126
		if (p[td->offs + i] != td->pattern[i])
127 128 129 130 131
			return -1;
	}
	return 0;
}

L
Linus Torvalds 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144
/**
 * 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
 * @bits:	number of bits per block
 * @offs:	offset in the memory table
 * @reserved_block_code:	Pattern to identify reserved blocks
 *
 * Read the bad block table starting from page.
 *
 */
145 146
static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
		    int bits, int offs, int reserved_block_code)
L
Linus Torvalds 已提交
147 148 149 150 151 152 153 154
{
	int res, i, j, act = 0;
	struct nand_chip *this = mtd->priv;
	size_t retlen, len, totlen;
	loff_t from;
	uint8_t msk = (uint8_t) ((1 << bits) - 1);

	totlen = (num * bits) >> 3;
155
	from = ((loff_t) page) << this->page_shift;
156

L
Linus Torvalds 已提交
157
	while (totlen) {
158
		len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
159
		res = mtd->read(mtd, from, len, &retlen, buf);
L
Linus Torvalds 已提交
160 161
		if (res < 0) {
			if (retlen != len) {
162
				printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
L
Linus Torvalds 已提交
163 164
				return res;
			}
165
			printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
166
		}
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174

		/* 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;
175 176 177
				if (reserved_block_code && (tmp == reserved_block_code)) {
					printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%08x\n",
					       ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
L
Linus Torvalds 已提交
178
					this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
179
					mtd->ecc_stats.bbtblocks++;
L
Linus Torvalds 已提交
180 181 182 183
					continue;
				}
				/* Leave it for now, if its matured we can move this
				 * message to MTD_DEBUG_LEVEL0 */
184 185
				printk(KERN_DEBUG "nand_read_bbt: Bad block at 0x%08x\n",
				       ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
186
				/* Factory marked bad or worn out ? */
L
Linus Torvalds 已提交
187 188 189 190
				if (tmp == 0)
					this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
				else
					this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
191
				mtd->ecc_stats.badblocks++;
192
			}
L
Linus Torvalds 已提交
193 194 195 196 197 198 199 200 201 202 203
		}
		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
204
 * @td:		descriptor for the bad block table
L
Linus Torvalds 已提交
205 206 207 208 209 210
 * @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.
*/
211
static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
L
Linus Torvalds 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
{
	struct nand_chip *this = mtd->priv;
	int res = 0, i;
	int bits;

	bits = td->options & NAND_BBT_NRBITS_MSK;
	if (td->options & NAND_BBT_PERCHIP) {
		int offs = 0;
		for (i = 0; i < this->numchips; i++) {
			if (chip == -1 || chip == i)
				res = read_bbt (mtd, buf, td->pages[i], this->chipsize >> this->bbt_erase_shift, bits, offs, td->reserved_block_code);
			if (res)
				return res;
			offs += this->chipsize >> (this->bbt_erase_shift + 2);
		}
	} else {
		res = read_bbt (mtd, buf, td->pages[0], mtd->size >> this->bbt_erase_shift, bits, 0, td->reserved_block_code);
		if (res)
			return res;
	}
	return 0;
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
/*
 * Scan read raw data from flash
 */
static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
			 size_t len)
{
	struct mtd_oob_ops ops;

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

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

/*
 * 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);
}

L
Linus Torvalds 已提交
271 272 273 274
/**
 * 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
275
 * @td:		descriptor for the bad block table
L
Linus Torvalds 已提交
276 277 278 279 280 281
 * @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.
 *
*/
282 283
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 已提交
284 285 286
{
	struct nand_chip *this = mtd->priv;

287
	/* Read the primary version, if available */
L
Linus Torvalds 已提交
288
	if (td->options & NAND_BBT_VERSION) {
289 290
		scan_read_raw(mtd, buf, td->pages[0] << this->page_shift,
			      mtd->writesize);
J
Joern Engel 已提交
291
		td->version[0] = buf[mtd->writesize + td->veroffs];
292 293
		printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
		       td->pages[0], td->version[0]);
L
Linus Torvalds 已提交
294 295
	}

296
	/* Read the mirror version, if available */
L
Linus Torvalds 已提交
297
	if (md && (md->options & NAND_BBT_VERSION)) {
298 299
		scan_read_raw(mtd, buf, md->pages[0] << this->page_shift,
			      mtd->writesize);
J
Joern Engel 已提交
300
		md->version[0] = buf[mtd->writesize + md->veroffs];
301 302
		printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
		       md->pages[0], md->version[0]);
L
Linus Torvalds 已提交
303 304 305 306
	}
	return 1;
}

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
/*
 * 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;

	ret = scan_read_raw(mtd, buf, offs, readlen);
	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.len = mtd->oobsize;
	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 已提交
361 362 363 364 365 366 367 368 369 370 371
/**
 * 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
 */
372 373
static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
	struct nand_bbt_descr *bd, int chip)
L
Linus Torvalds 已提交
374 375
{
	struct nand_chip *this = mtd->priv;
376
	int i, numblocks, len, scanlen;
L
Linus Torvalds 已提交
377 378
	int startblock;
	loff_t from;
379
	size_t readlen;
L
Linus Torvalds 已提交
380

381
	printk(KERN_INFO "Scanning device for bad blocks\n");
L
Linus Torvalds 已提交
382 383 384 385 386 387

	if (bd->options & NAND_BBT_SCANALLPAGES)
		len = 1 << (this->bbt_erase_shift - this->page_shift);
	else {
		if (bd->options & NAND_BBT_SCAN2NDPAGE)
			len = 2;
388
		else
L
Linus Torvalds 已提交
389 390
			len = 1;
	}
391 392 393

	if (!(bd->options & NAND_BBT_SCANEMPTY)) {
		/* We need only read few bytes from the OOB area */
394
		scanlen = 0;
395 396
		readlen = bd->len;
	} else {
397
		/* Full page content should be read */
J
Joern Engel 已提交
398 399
		scanlen = mtd->writesize + mtd->oobsize;
		readlen = len * mtd->writesize;
400
	}
L
Linus Torvalds 已提交
401 402

	if (chip == -1) {
403 404
		/* Note that numblocks is 2 * (real numblocks) here, see i+=2
		 * below as it makes shifting and masking less painful */
L
Linus Torvalds 已提交
405 406 407 408 409
		numblocks = mtd->size >> (this->bbt_erase_shift - 1);
		startblock = 0;
		from = 0;
	} else {
		if (chip >= this->numchips) {
410 411
			printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
			       chip + 1, this->numchips);
412
			return -EINVAL;
L
Linus Torvalds 已提交
413 414 415 416 417 418
		}
		numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
		startblock = chip * numblocks;
		numblocks += startblock;
		from = startblock << (this->bbt_erase_shift - 1);
	}
419

L
Linus Torvalds 已提交
420
	for (i = startblock; i < numblocks;) {
421
		int ret;
422

423 424 425 426 427 428 429 430 431 432 433 434 435
		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);
			printk(KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
			       i >> 1, (unsigned int)from);
436
			mtd->ecc_stats.badblocks++;
L
Linus Torvalds 已提交
437
		}
438

L
Linus Torvalds 已提交
439 440 441
		i += 2;
		from += (1 << this->bbt_erase_shift);
	}
442
	return 0;
L
Linus Torvalds 已提交
443 444 445 446 447 448 449 450 451
}

/**
 * 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.
452
 * Search is preformed either from the beginning up or from the end of
L
Linus Torvalds 已提交
453 454
 * the device downwards. The search starts always at the start of a
 * block.
455
 * If the option NAND_BBT_PERCHIP is given, each chip is searched
L
Linus Torvalds 已提交
456
 * for a bbt, which contains the bad block information of this chip.
457
 * This is necessary to provide support for certain DOC devices.
L
Linus Torvalds 已提交
458
 *
459 460
 * The bbt ident pattern resides in the oob area of the first page
 * in a block.
L
Linus Torvalds 已提交
461
 */
462
static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
L
Linus Torvalds 已提交
463 464 465 466
{
	struct nand_chip *this = mtd->priv;
	int i, chips;
	int bits, startblock, block, dir;
J
Joern Engel 已提交
467
	int scanlen = mtd->writesize + mtd->oobsize;
L
Linus Torvalds 已提交
468
	int bbtblocks;
469
	int blocktopage = this->bbt_erase_shift - this->page_shift;
L
Linus Torvalds 已提交
470 471 472

	/* Search direction top -> down ? */
	if (td->options & NAND_BBT_LASTBLOCK) {
473
		startblock = (mtd->size >> this->bbt_erase_shift) - 1;
L
Linus Torvalds 已提交
474 475
		dir = -1;
	} else {
476
		startblock = 0;
L
Linus Torvalds 已提交
477
		dir = 1;
478 479
	}

L
Linus Torvalds 已提交
480 481 482 483 484 485 486 487 488
	/* 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;
	}
489

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

L
Linus Torvalds 已提交
493 494
	for (i = 0; i < chips; i++) {
		/* Reset version information */
495
		td->version[i] = 0;
L
Linus Torvalds 已提交
496 497 498
		td->pages[i] = -1;
		/* Scan the maximum number of blocks */
		for (block = 0; block < td->maxblocks; block++) {
499

L
Linus Torvalds 已提交
500
			int actblock = startblock + dir * block;
501 502
			loff_t offs = actblock << this->bbt_erase_shift;

L
Linus Torvalds 已提交
503
			/* Read first page */
504
			scan_read_raw(mtd, buf, offs, mtd->writesize);
J
Joern Engel 已提交
505
			if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
506
				td->pages[i] = actblock << blocktopage;
L
Linus Torvalds 已提交
507
				if (td->options & NAND_BBT_VERSION) {
J
Joern Engel 已提交
508
					td->version[i] = buf[mtd->writesize + td->veroffs];
L
Linus Torvalds 已提交
509 510 511 512 513 514 515 516 517
				}
				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)
518
			printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
L
Linus Torvalds 已提交
519
		else
520 521
			printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i],
			       td->version[i]);
L
Linus Torvalds 已提交
522
	}
523
	return 0;
L
Linus Torvalds 已提交
524 525 526 527 528 529
}

/**
 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
 * @mtd:	MTD device structure
 * @buf:	temporary buffer
530
 * @td:		descriptor for the bad block table
L
Linus Torvalds 已提交
531 532 533 534
 * @md:		descriptor for the bad block table mirror
 *
 * Search and read the bad block table(s)
*/
535
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 已提交
536 537
{
	/* Search the primary table */
538
	search_bbt(mtd, buf, td);
539

L
Linus Torvalds 已提交
540 541
	/* Search the mirror table */
	if (md)
542
		search_bbt(mtd, buf, md);
543

L
Linus Torvalds 已提交
544
	/* Force result check */
545
	return 1;
L
Linus Torvalds 已提交
546 547
}

548
/**
L
Linus Torvalds 已提交
549 550 551 552
 * write_bbt - [GENERIC] (Re)write the bad block table
 *
 * @mtd:	MTD device structure
 * @buf:	temporary buffer
553
 * @td:		descriptor for the bad block table
L
Linus Torvalds 已提交
554 555 556 557 558 559
 * @md:		descriptor for the bad block table mirror
 * @chipsel:	selector for a specific chip, -1 for all
 *
 * (Re)write the bad block table
 *
*/
560
static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
561 562
		     struct nand_bbt_descr *td, struct nand_bbt_descr *md,
		     int chipsel)
L
Linus Torvalds 已提交
563 564 565 566 567
{
	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;
568
	int nrchips, bbtoffs, pageoffs, ooboffs;
L
Linus Torvalds 已提交
569 570
	uint8_t msk[4];
	uint8_t rcode = td->reserved_block_code;
571
	size_t retlen, len = 0;
L
Linus Torvalds 已提交
572
	loff_t to;
573 574 575 576 577 578
	struct mtd_oob_ops ops;

	ops.ooblen = mtd->oobsize;
	ops.ooboffs = 0;
	ops.datbuf = NULL;
	ops.mode = MTD_OOB_PLACE;
L
Linus Torvalds 已提交
579 580 581 582 583

	if (!rcode)
		rcode = 0xff;
	/* Write bad block table per chip rather than per device ? */
	if (td->options & NAND_BBT_PERCHIP) {
584
		numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
585
		/* Full device write or specific chip ? */
L
Linus Torvalds 已提交
586 587 588 589 590 591 592
		if (chipsel == -1) {
			nrchips = this->numchips;
		} else {
			nrchips = chipsel + 1;
			chip = chipsel;
		}
	} else {
593
		numblocks = (int)(mtd->size >> this->bbt_erase_shift);
L
Linus Torvalds 已提交
594
		nrchips = 1;
595 596
	}

L
Linus Torvalds 已提交
597 598
	/* Loop through the chips */
	for (; chip < nrchips; chip++) {
599 600 601

		/* There was already a version of the table, reuse the page
		 * This applies for absolute placement too, as we have the
L
Linus Torvalds 已提交
602 603 604 605 606
		 * page nr. in td->pages.
		 */
		if (td->pages[chip] != -1) {
			page = td->pages[chip];
			goto write;
607
		}
L
Linus Torvalds 已提交
608 609 610 611 612 613 614 615 616

		/* 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;
617
		}
L
Linus Torvalds 已提交
618 619 620 621

		for (i = 0; i < td->maxblocks; i++) {
			int block = startblock + dir * i;
			/* Check, if the block is bad */
622 623
			switch ((this->bbt[block >> 2] >>
				 (2 * (block & 0x03))) & 0x03) {
L
Linus Torvalds 已提交
624 625 626 627
			case 0x01:
			case 0x03:
				continue;
			}
628 629
			page = block <<
				(this->bbt_erase_shift - this->page_shift);
L
Linus Torvalds 已提交
630 631 632 633
			/* Check, if the block is used by the mirror table */
			if (!md || md->pages[chip] != page)
				goto write;
		}
634
		printk(KERN_ERR "No space left to write bad block table\n");
L
Linus Torvalds 已提交
635
		return -ENOSPC;
636
	write:
L
Linus Torvalds 已提交
637 638 639

		/* Set up shift count and masks for the flash table */
		bits = td->options & NAND_BBT_NRBITS_MSK;
640
		msk[2] = ~rcode;
L
Linus Torvalds 已提交
641
		switch (bits) {
642 643 644 645 646 647 648 649 650 651 652 653
		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 已提交
654 655
		default: return -EINVAL;
		}
656

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

L
Linus Torvalds 已提交
659 660 661 662 663 664 665
		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;
666
			res = mtd->read(mtd, to, len, &retlen, buf);
L
Linus Torvalds 已提交
667 668
			if (res < 0) {
				if (retlen != len) {
669 670 671
					printk(KERN_INFO "nand_bbt: Error "
					       "reading block for writing "
					       "the bad block table\n");
L
Linus Torvalds 已提交
672 673
					return res;
				}
674 675 676
				printk(KERN_WARNING "nand_bbt: ECC error "
				       "while reading block for writing "
				       "bad block table\n");
L
Linus Torvalds 已提交
677
			}
678
			/* Read oob data */
679 680 681 682
			ops.len = (len >> this->page_shift) * mtd->oobsize;
			ops.oobbuf = &buf[len];
			res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
			if (res < 0 || ops.retlen != ops.len)
683 684
				goto outerr;

L
Linus Torvalds 已提交
685 686 687 688
			/* 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 */
689
			memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
690 691
			ooboffs = len + (pageoffs * mtd->oobsize);

L
Linus Torvalds 已提交
692 693 694 695
		} else {
			/* Calc length */
			len = (size_t) (numblocks >> sft);
			/* Make it page aligned ! */
696 697
			len = (len + (mtd->writesize - 1)) &
				~(mtd->writesize - 1);
L
Linus Torvalds 已提交
698
			/* Preset the buffer with 0xff */
699 700
			memset(buf, 0xff, len +
			       (len >> this->page_shift)* mtd->oobsize);
L
Linus Torvalds 已提交
701
			offs = 0;
702
			ooboffs = len;
L
Linus Torvalds 已提交
703
			/* Pattern is located in oob area of first page */
704
			memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
L
Linus Torvalds 已提交
705
		}
706

707 708 709
		if (td->options & NAND_BBT_VERSION)
			buf[ooboffs + td->veroffs] = td->version[chip];

L
Linus Torvalds 已提交
710
		/* walk through the memory table */
711
		for (i = 0; i < numblocks;) {
L
Linus Torvalds 已提交
712 713
			uint8_t dat;
			dat = this->bbt[bbtoffs + (i >> 2)];
714
			for (j = 0; j < 4; j++, i++) {
L
Linus Torvalds 已提交
715 716
				int sftcnt = (i << (3 - sft)) & sftmsk;
				/* Do not store the reserved bbt blocks ! */
717 718
				buf[offs + (i >> sft)] &=
					~(msk[dat & 0x03] << sftcnt);
L
Linus Torvalds 已提交
719 720 721
				dat >>= 2;
			}
		}
722

723
		memset(&einfo, 0, sizeof(einfo));
L
Linus Torvalds 已提交
724
		einfo.mtd = mtd;
725
		einfo.addr = (unsigned long)to;
L
Linus Torvalds 已提交
726
		einfo.len = 1 << this->bbt_erase_shift;
727
		res = nand_erase_nand(mtd, &einfo, 1);
728 729
		if (res < 0)
			goto outerr;
730

731
		res = scan_write_bbt(mtd, to, len, buf, &buf[len]);
732 733 734 735 736
		if (res < 0)
			goto outerr;

		printk(KERN_DEBUG "Bad block table written to 0x%08x, version "
		       "0x%02X\n", (unsigned int)to, td->version[chip]);
737

L
Linus Torvalds 已提交
738 739
		/* Mark it as used */
		td->pages[chip] = page;
740
	}
L
Linus Torvalds 已提交
741
	return 0;
742 743 744 745 746

 outerr:
	printk(KERN_WARNING
	       "nand_bbt: Error while writing bad block table %d\n", res);
	return res;
L
Linus Torvalds 已提交
747 748 749 750 751 752 753
}

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

761
	bd->options &= ~NAND_BBT_SCANEMPTY;
762
	return create_bbt(mtd, this->buffers.databuf, bd, -1);
L
Linus Torvalds 已提交
763 764 765
}

/**
766
 * check_create - [GENERIC] create and write bbt(s) if necessary
L
Linus Torvalds 已提交
767 768 769 770 771
 * @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
772 773 774
 * 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 已提交
775 776
 * version nr. of one table is less than the other
*/
777
static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
L
Linus Torvalds 已提交
778 779 780 781 782 783 784 785
{
	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 ? */
786
	if (td->options & NAND_BBT_PERCHIP)
L
Linus Torvalds 已提交
787
		chips = this->numchips;
788
	else
L
Linus Torvalds 已提交
789
		chips = 1;
790

L
Linus Torvalds 已提交
791 792 793 794 795 796 797 798 799 800 801 802 803 804
	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;
		/* Mirrored table avilable ? */
		if (md) {
			if (td->pages[i] == -1 && md->pages[i] == -1) {
				writeops = 0x03;
				goto create;
			}

			if (td->pages[i] == -1) {
805
				rd = md;
L
Linus Torvalds 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
				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;
823
			}
L
Linus Torvalds 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844

			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;
		}
845
	create:
L
Linus Torvalds 已提交
846 847
		/* Create the bad block table by scanning the device ? */
		if (!(td->options & NAND_BBT_CREATE))
848 849
			continue;

L
Linus Torvalds 已提交
850
		/* Create the table in memory by scanning the chip(s) */
851
		create_bbt(mtd, buf, bd, chipsel);
852

L
Linus Torvalds 已提交
853 854
		td->version[i] = 1;
		if (md)
855
			md->version[i] = 1;
856
	writecheck:
L
Linus Torvalds 已提交
857 858
		/* read back first ? */
		if (rd)
859
			read_abs_bbt(mtd, buf, rd, chipsel);
L
Linus Torvalds 已提交
860 861
		/* If they weren't versioned, read both. */
		if (rd2)
862
			read_abs_bbt(mtd, buf, rd2, chipsel);
L
Linus Torvalds 已提交
863 864 865

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

L
Linus Torvalds 已提交
871 872
		/* Write the mirror bad block table to the device ? */
		if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
873
			res = write_bbt(mtd, buf, md, td, chipsel);
L
Linus Torvalds 已提交
874 875 876 877
			if (res < 0)
				return res;
		}
	}
878
	return 0;
L
Linus Torvalds 已提交
879 880 881
}

/**
882
 * mark_bbt_regions - [GENERIC] mark the bad block table regions
L
Linus Torvalds 已提交
883 884 885 886 887 888 889
 * @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.
*/
890
static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
L
Linus Torvalds 已提交
891 892 893 894 895 896 897 898 899 900 901 902
{
	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);
903 904
	}

L
Linus Torvalds 已提交
905 906 907
	for (i = 0; i < chips; i++) {
		if ((td->options & NAND_BBT_ABSPAGE) ||
		    !(td->options & NAND_BBT_WRITE)) {
908 909
			if (td->pages[i] == -1)
				continue;
L
Linus Torvalds 已提交
910
			block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
911
			block <<= 1;
L
Linus Torvalds 已提交
912 913 914 915 916 917 918 919 920 921
			oldval = this->bbt[(block >> 3)];
			newval = oldval | (0x2 << (block & 0x06));
			this->bbt[(block >> 3)] = newval;
			if ((oldval != newval) && td->reserved_block_code)
				nand_update_bbt(mtd, block << (this->bbt_erase_shift - 1));
			continue;
		}
		update = 0;
		if (td->options & NAND_BBT_LASTBLOCK)
			block = ((i + 1) * nrblocks) - td->maxblocks;
922
		else
L
Linus Torvalds 已提交
923
			block = i * nrblocks;
924
		block <<= 1;
L
Linus Torvalds 已提交
925 926 927 928
		for (j = 0; j < td->maxblocks; j++) {
			oldval = this->bbt[(block >> 3)];
			newval = oldval | (0x2 << (block & 0x06));
			this->bbt[(block >> 3)] = newval;
929 930
			if (oldval != newval)
				update = 1;
L
Linus Torvalds 已提交
931
			block += 2;
932
		}
L
Linus Torvalds 已提交
933 934 935 936 937 938 939 940 941 942 943 944 945
		/* 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)
			nand_update_bbt(mtd, (block - 2) << (this->bbt_erase_shift - 1));
	}
}

/**
 * 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
 *
946
 * The function checks, if a bad block table(s) is/are already
L
Linus Torvalds 已提交
947 948 949 950 951 952 953 954
 * 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.
 *
*/
955
int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
L
Linus Torvalds 已提交
956 957 958 959 960 961 962 963 964
{
	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);
	/* Allocate memory (2bit per block) */
965
	this->bbt = kmalloc(len, GFP_KERNEL);
L
Linus Torvalds 已提交
966
	if (!this->bbt) {
967
		printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
L
Linus Torvalds 已提交
968 969 970
		return -ENOMEM;
	}
	/* Clear the memory bad block table */
971
	memset(this->bbt, 0x00, len);
L
Linus Torvalds 已提交
972 973 974 975

	/* If no primary table decriptor is given, scan the device
	 * to build a memory based bad block table
	 */
976 977
	if (!td) {
		if ((res = nand_memory_bbt(mtd, bd))) {
978 979
			printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
			kfree(this->bbt);
980 981 982 983
			this->bbt = NULL;
		}
		return res;
	}
L
Linus Torvalds 已提交
984 985 986 987

	/* Allocate a temporary buffer for one eraseblock incl. oob */
	len = (1 << this->bbt_erase_shift);
	len += (len >> this->page_shift) * mtd->oobsize;
988
	buf = vmalloc(len);
L
Linus Torvalds 已提交
989
	if (!buf) {
990 991
		printk(KERN_ERR "nand_bbt: Out of memory\n");
		kfree(this->bbt);
L
Linus Torvalds 已提交
992 993 994
		this->bbt = NULL;
		return -ENOMEM;
	}
995

L
Linus Torvalds 已提交
996 997
	/* Is the bbt at a given page ? */
	if (td->options & NAND_BBT_ABSPAGE) {
998
		res = read_abs_bbts(mtd, buf, td, md);
999
	} else {
L
Linus Torvalds 已提交
1000
		/* Search the bad block table using a pattern in oob */
1001
		res = search_read_bbts(mtd, buf, td, md);
1002
	}
L
Linus Torvalds 已提交
1003

1004
	if (res)
1005
		res = check_create(mtd, buf, bd);
1006

L
Linus Torvalds 已提交
1007
	/* Prevent the bbt regions from erasing / writing */
1008
	mark_bbt_region(mtd, td);
L
Linus Torvalds 已提交
1009
	if (md)
1010
		mark_bbt_region(mtd, md);
1011

1012
	vfree(buf);
L
Linus Torvalds 已提交
1013 1014 1015 1016
	return res;
}

/**
1017
 * nand_update_bbt - [NAND Interface] update bad block table(s)
L
Linus Torvalds 已提交
1018 1019 1020 1021 1022
 * @mtd:	MTD device structure
 * @offs:	the offset of the newly marked block
 *
 * The function updates the bad block table(s)
*/
1023
int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
L
Linus Torvalds 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
{
	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;

	len = mtd->size >> (this->bbt_erase_shift + 2);
	/* Allocate a temporary buffer for one eraseblock incl. oob */
	len = (1 << this->bbt_erase_shift);
	len += (len >> this->page_shift) * mtd->oobsize;
1039
	buf = kmalloc(len, GFP_KERNEL);
L
Linus Torvalds 已提交
1040
	if (!buf) {
1041
		printk(KERN_ERR "nand_update_bbt: Out of memory\n");
L
Linus Torvalds 已提交
1042 1043
		return -ENOMEM;
	}
1044

L
Linus Torvalds 已提交
1045 1046 1047 1048
	writeops = md != NULL ? 0x03 : 0x01;

	/* Do we have a bbt per chip ? */
	if (td->options & NAND_BBT_PERCHIP) {
1049
		chip = (int)(offs >> this->chip_shift);
L
Linus Torvalds 已提交
1050 1051 1052 1053 1054 1055 1056 1057
		chipsel = chip;
	} else {
		chip = 0;
		chipsel = -1;
	}

	td->version[chip]++;
	if (md)
1058
		md->version[chip]++;
L
Linus Torvalds 已提交
1059 1060 1061

	/* Write the bad block table to the device ? */
	if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1062
		res = write_bbt(mtd, buf, td, md, chipsel);
L
Linus Torvalds 已提交
1063 1064 1065 1066 1067
		if (res < 0)
			goto out;
	}
	/* Write the mirror bad block table to the device ? */
	if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1068
		res = write_bbt(mtd, buf, md, td, chipsel);
L
Linus Torvalds 已提交
1069 1070
	}

1071 1072
 out:
	kfree(buf);
L
Linus Torvalds 已提交
1073 1074 1075
	return res;
}

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

static struct nand_bbt_descr smallpage_memorybased = {
1081
	.options = NAND_BBT_SCAN2NDPAGE,
L
Linus Torvalds 已提交
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
	.offs = 5,
	.len = 1,
	.pattern = scan_ff_pattern
};

static struct nand_bbt_descr largepage_memorybased = {
	.options = 0,
	.offs = 0,
	.len = 2,
	.pattern = scan_ff_pattern
};

static struct nand_bbt_descr smallpage_flashbased = {
1095
	.options = NAND_BBT_SCAN2NDPAGE,
L
Linus Torvalds 已提交
1096 1097 1098 1099 1100 1101
	.offs = 5,
	.len = 1,
	.pattern = scan_ff_pattern
};

static struct nand_bbt_descr largepage_flashbased = {
1102
	.options = NAND_BBT_SCAN2NDPAGE,
L
Linus Torvalds 已提交
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
	.offs = 0,
	.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 = {
1123
	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
L
Linus Torvalds 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132
		| 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 = {
1133
	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
L
Linus Torvalds 已提交
1134 1135 1136 1137 1138 1139 1140 1141 1142
		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
	.offs =	8,
	.len = 4,
	.veroffs = 12,
	.maxblocks = 4,
	.pattern = mirror_pattern
};

/**
1143
 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
L
Linus Torvalds 已提交
1144 1145 1146 1147 1148 1149
 * @mtd:	MTD device structure
 *
 * This function selects the default bad block table
 * support for the device and calls the nand_scan_bbt function
 *
*/
1150
int nand_default_bbt(struct mtd_info *mtd)
L
Linus Torvalds 已提交
1151 1152
{
	struct nand_chip *this = mtd->priv;
1153 1154

	/* Default for AG-AND. We must use a flash based
L
Linus Torvalds 已提交
1155 1156 1157
	 * 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
1158
	 * this information in a good / bad table during
L
Linus Torvalds 已提交
1159
	 * startup
1160
	 */
L
Linus Torvalds 已提交
1161 1162
	if (this->options & NAND_IS_AND) {
		/* Use the default pattern descriptors */
1163
		if (!this->bbt_td) {
L
Linus Torvalds 已提交
1164 1165
			this->bbt_td = &bbt_main_descr;
			this->bbt_md = &bbt_mirror_descr;
1166
		}
L
Linus Torvalds 已提交
1167
		this->options |= NAND_USE_FLASH_BBT;
1168
		return nand_scan_bbt(mtd, &agand_flashbased);
L
Linus Torvalds 已提交
1169
	}
1170

L
Linus Torvalds 已提交
1171 1172
	/* Is a flash based bad block table requested ? */
	if (this->options & NAND_USE_FLASH_BBT) {
1173 1174
		/* Use the default pattern descriptors */
		if (!this->bbt_td) {
L
Linus Torvalds 已提交
1175 1176 1177 1178
			this->bbt_td = &bbt_main_descr;
			this->bbt_md = &bbt_mirror_descr;
		}
		if (!this->badblock_pattern) {
J
Joern Engel 已提交
1179
			this->badblock_pattern = (mtd->writesize > 512) ? &largepage_flashbased : &smallpage_flashbased;
L
Linus Torvalds 已提交
1180 1181 1182 1183 1184
		}
	} else {
		this->bbt_td = NULL;
		this->bbt_md = NULL;
		if (!this->badblock_pattern) {
J
Joern Engel 已提交
1185
			this->badblock_pattern = (mtd->writesize > 512) ?
1186
			    &largepage_memorybased : &smallpage_memorybased;
L
Linus Torvalds 已提交
1187 1188
		}
	}
1189
	return nand_scan_bbt(mtd, this->badblock_pattern);
L
Linus Torvalds 已提交
1190 1191 1192
}

/**
1193
 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
L
Linus Torvalds 已提交
1194 1195 1196 1197 1198
 * @mtd:	MTD device structure
 * @offs:	offset in the device
 * @allowbbt:	allow access to bad block table region
 *
*/
1199
int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
L
Linus Torvalds 已提交
1200 1201 1202
{
	struct nand_chip *this = mtd->priv;
	int block;
1203
	uint8_t res;
1204

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

1209 1210
	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 已提交
1211 1212

	switch ((int)res) {
1213 1214 1215 1216 1217 1218
	case 0x00:
		return 0;
	case 0x01:
		return 1;
	case 0x02:
		return allowbbt ? 0 : 1;
L
Linus Torvalds 已提交
1219 1220 1221 1222
	}
	return 1;
}

1223 1224
EXPORT_SYMBOL(nand_scan_bbt);
EXPORT_SYMBOL(nand_default_bbt);