diff options
| author | Tom Rini <trini@ti.com> | 2013-05-31 18:28:47 -0400 | 
|---|---|---|
| committer | Tom Rini <trini@ti.com> | 2013-05-31 18:28:47 -0400 | 
| commit | d6639d10dbfa42dc888f8917012550b632a88959 (patch) | |
| tree | 3c971dcfad4d7ad58570e8d5b0318f58370f9d3d /drivers/mtd/nand/nand_bbt.c | |
| parent | 3da0e5750b24a9491058df6126c7be577a276c09 (diff) | |
| parent | dfe64e2c89731a3f9950d7acd8681b68df2bae03 (diff) | |
| download | olio-uboot-2014.01-d6639d10dbfa42dc888f8917012550b632a88959.tar.xz olio-uboot-2014.01-d6639d10dbfa42dc888f8917012550b632a88959.zip | |
Merge branch 'master' of git://git.denx.de/u-boot-nand-flash
Diffstat (limited to 'drivers/mtd/nand/nand_bbt.c')
| -rw-r--r-- | drivers/mtd/nand/nand_bbt.c | 818 | 
1 files changed, 384 insertions, 434 deletions
| diff --git a/drivers/mtd/nand/nand_bbt.c b/drivers/mtd/nand/nand_bbt.c index 74a7061b0..8ef58451d 100644 --- a/drivers/mtd/nand/nand_bbt.c +++ b/drivers/mtd/nand/nand_bbt.c @@ -4,7 +4,7 @@   *  Overview:   *   Bad block table support for the NAND driver   * - *  Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de) + *  Copyright © 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 @@ -14,7 +14,7 @@   *   * 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 no flash based BBT - * (NAND_USE_FLASH_BBT) is specified then the device is scanned for factory + * (NAND_BBT_USE_FLASH) 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. @@ -22,7 +22,7 @@   * 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. + * version number, then the mirror BBT is used to build the memory based BBT.   * If the tables are not versioned, then we "or" the bad block information.   * 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 @@ -36,9 +36,9 @@   * 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. + * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of + * course): it moves the ident pattern and the version byte into the data area + * and the OOB area will remain untouched.   *   * The table uses 2 bits per block   * 11b:		block is good @@ -63,126 +63,81 @@  #include <malloc.h>  #include <linux/compat.h>  #include <linux/mtd/mtd.h> +#include <linux/mtd/bbm.h>  #include <linux/mtd/nand.h>  #include <linux/mtd/nand_ecc.h>  #include <linux/bitops.h> +#include <linux/string.h>  #include <asm/errno.h>  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; +	if (memcmp(buf, td->pattern, td->len)) +		return -1; +	return 0;  }  /**   * 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 + * @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. + */  static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)  { -	int i, end = 0; +	int end = 0;  	uint8_t *p = buf;  	if (td->options & NAND_BBT_NO_OOB)  		return check_pattern_no_oob(buf, td);  	end = paglen + td->offs; -	if (td->options & NAND_BBT_SCANEMPTY) { -		for (i = 0; i < end; i++) { -			if (p[i] != 0xff) -				return -1; -		} -	} +	if (td->options & NAND_BBT_SCANEMPTY) +		if (memchr_inv(p, 0xff, end)) +			return -1;  	p += end;  	/* Compare the pattern */ -	for (i = 0; i < td->len; i++) { -		if (p[i] != td->pattern[i]) -			return -1; -	} - -	/* 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; -		} -	} +	if (memcmp(p, td->pattern, td->len)) +		return -1;  	if (td->options & NAND_BBT_SCANEMPTY) {  		p += td->len;  		end += td->len; -		for (i = end; i < len; i++) { -			if (*p++ != 0xff) -				return -1; -		} +		if (memchr_inv(p, 0xff, len - end)) +			return -1;  	}  	return 0;  }  /**   * check_short_pattern - [GENERIC] check if a pattern is in the buffer - * @buf:	the buffer to search - * @td:		search pattern descriptor + * @buf: the buffer to search + * @td:	search pattern descriptor   * - * Check for a pattern at the given place. Used to search bad block - * tables and good / bad block identifiers. Same as check_pattern, but - * no optional empty check - * -*/ + * Check for a pattern at the given place. Used to search bad block tables and + * good / bad block identifiers. Same as check_pattern, but no optional empty + * check. + */  static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)  { -	int i; -	uint8_t *p = buf; -  	/* Compare the pattern */ -	for (i = 0; i < td->len; i++) { -		if (p[td->offs + i] != td->pattern[i]) -			return -1; -	} -	/* 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; -		} -	} +	if (memcmp(buf + td->offs, td->pattern, td->len)) +		return -1;  	return 0;  }  /**   * add_marker_len - compute the length of the marker in data area - * @td:		BBT descriptor used for computation + * @td: BBT descriptor used for computation   * - * The length will be 0 if the markeris located in OOB area. + * The length will be 0 if the marker is located in OOB area.   */  static u32 add_marker_len(struct nand_bbt_descr *td)  { @@ -199,34 +154,33 @@ static u32 add_marker_len(struct nand_bbt_descr *td)  /**   * 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 - * @td:		the bbt describtion table - * @offs:	offset in the memory table + * @mtd: MTD device structure + * @buf: temporary buffer + * @page: the starting page + * @num: the number of bbt descriptors to read + * @td: the bbt describtion table + * @offs: offset in the memory table   *   * Read the bad block table starting from page. - *   */  static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,  		struct nand_bbt_descr *td, int offs)  { -	int res, i, j, act = 0; +	int res, ret = 0, i, j, act = 0;  	struct nand_chip *this = mtd->priv;  	size_t retlen, len, totlen;  	loff_t from;  	int bits = td->options & NAND_BBT_NRBITS_MSK; -	uint8_t msk = (uint8_t) ((1 << bits) - 1); +	uint8_t msk = (uint8_t)((1 << bits) - 1);  	u32 marker_len;  	int reserved_block_code = td->reserved_block_code;  	totlen = (num * bits) >> 3;  	marker_len = add_marker_len(td); -	from = ((loff_t) page) << this->page_shift; +	from = ((loff_t)page) << this->page_shift;  	while (totlen) { -		len = min(totlen, (size_t) (1 << this->bbt_erase_shift)); +		len = min(totlen, (size_t)(1 << this->bbt_erase_shift));  		if (marker_len) {  			/*  			 * In case the BBT marker is not in the OOB area it @@ -236,13 +190,20 @@ static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,  			from += marker_len;  			marker_len = 0;  		} -		res = mtd->read(mtd, from, len, &retlen, buf); +		res = mtd_read(mtd, from, len, &retlen, buf);  		if (res < 0) { -			if (retlen != len) { -				printk(KERN_INFO "nand_bbt: Error reading bad block table\n"); +			if (mtd_is_eccerr(res)) { +				pr_info("nand_bbt: ECC error in BBT at " +					"0x%012llx\n", from & ~mtd->writesize); +				return res; +			} else if (mtd_is_bitflip(res)) { +				pr_info("nand_bbt: corrected error in BBT at " +					"0x%012llx\n", from & ~mtd->writesize); +				ret = res; +			} else { +				pr_info("nand_bbt: error reading BBT\n");  				return res;  			} -			printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");  		}  		/* Analyse data */ @@ -253,17 +214,16 @@ static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,  				if (tmp == msk)  					continue;  				if (reserved_block_code && (tmp == reserved_block_code)) { -					printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%012llx\n", -					       (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift); +					pr_info("nand_read_bbt: reserved block at 0x%012llx\n", +						 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);  					this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);  					mtd->ecc_stats.bbtblocks++;  					continue;  				} -				MTDDEBUG(MTD_DEBUG_LEVEL0, "nand_read_bbt: " \ -					"Bad block at 0x%012llx\n", +				pr_info("nand_read_bbt: Bad block at 0x%012llx\n",  					(loff_t)((offs << 2) + (act >> 1))  					<< this->bbt_erase_shift); -				/* Factory marked bad or worn out ? */ +				/* Factory marked bad or worn out? */  				if (tmp == 0)  					this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);  				else @@ -274,20 +234,20 @@ static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,  		totlen -= len;  		from += len;  	} -	return 0; +	return ret;  }  /**   * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page - * @mtd:	MTD device structure - * @buf:	temporary buffer - * @td:		descriptor for the bad block table - * @chip:	read the table for a specific chip, -1 read all chips. - *		Applies only if NAND_BBT_PERCHIP option is set + * @mtd: MTD device structure + * @buf: temporary buffer + * @td: descriptor for the bad block table + * @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. -*/ + * Read the bad block table for all chips starting at a given page. We assume + * that the bbt bits are in consecutive order. + */  static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)  {  	struct nand_chip *this = mtd->priv; @@ -313,10 +273,8 @@ static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc  	return 0;  } -/* - * 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, +/* BBT marker is in the first page, no OOB */ +static int scan_read_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,  			 struct nand_bbt_descr *td)  {  	size_t retlen; @@ -326,70 +284,73 @@ static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,  	if (td->options & NAND_BBT_VERSION)  		len++; -	return mtd->read(mtd, offs, len, &retlen, buf); +	return mtd_read(mtd, offs, len, &retlen, buf);  } -/* - * Scan read raw data from flash +/** + * scan_read_oob - [GENERIC] Scan data+OOB region to buffer + * @mtd: MTD device structure + * @buf: temporary buffer + * @offs: offset at which to scan + * @len: length of data region to read + * + * Scan read data from data+OOB. May traverse multiple pages, interleaving + * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest" + * ECC condition (error or bitflip). May quit on the first (non-ECC) error.   */ -static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs, +static int scan_read_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,  			 size_t len)  {  	struct mtd_oob_ops ops; -	int res; +	int res, ret = 0; -	ops.mode = MTD_OOB_RAW; +	ops.mode = MTD_OPS_PLACE_OOB;  	ops.ooboffs = 0;  	ops.ooblen = mtd->oobsize; -  	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); +		ops.datbuf = buf; +		ops.len = min(len, (size_t)mtd->writesize); +		ops.oobbuf = buf + ops.len; -			if (res) +		res = mtd_read_oob(mtd, offs, &ops); +		if (res) { +			if (!mtd_is_bitflip_or_eccerr(res))  				return res; +			else if (mtd_is_eccerr(res) || !ret) +				ret = res;  		}  		buf += mtd->oobsize + mtd->writesize;  		len -= mtd->writesize; +		offs += mtd->writesize;  	} -	return 0; +	return ret;  } -static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs, +static int scan_read(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); +		return scan_read_data(mtd, buf, offs, td);  	else -		return scan_read_raw_oob(mtd, buf, offs, len); +		return scan_read_oob(mtd, buf, offs, len);  } -/* - * Scan write data with oob to flash - */ +/* 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.mode = MTD_OPS_PLACE_OOB;  	ops.ooboffs = 0;  	ops.ooblen = mtd->oobsize;  	ops.datbuf = buf;  	ops.oobbuf = oob;  	ops.len = len; -	return mtd->write_oob(mtd, offs, &ops); +	return mtd_write_oob(mtd, offs, &ops);  }  static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td) @@ -403,65 +364,60 @@ static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)  /**   * 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 - * @td:		descriptor for the bad block table - * @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. + * @mtd: MTD device structure + * @buf: temporary buffer + * @td: descriptor for the bad block table + * @md:	descriptor for the bad block table mirror   * -*/ -static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf, -			 struct nand_bbt_descr *td, struct nand_bbt_descr *md) + * Read the bad block table(s) for all chips starting at a given page. We + * assume that the bbt bits are in consecutive order. + */ +static void read_abs_bbts(struct mtd_info *mtd, uint8_t *buf, +			  struct nand_bbt_descr *td, struct nand_bbt_descr *md)  {  	struct nand_chip *this = mtd->priv;  	/* Read the primary version, if available */  	if (td->options & NAND_BBT_VERSION) { -		scan_read_raw(mtd, buf, (loff_t)td->pages[0] << this->page_shift, +		scan_read(mtd, buf, (loff_t)td->pages[0] << this->page_shift,  			      mtd->writesize, td);  		td->version[0] = buf[bbt_get_ver_offs(mtd, td)]; -		printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", -		       td->pages[0], td->version[0]); +		pr_info("Bad block table at page %d, version 0x%02X\n", +			 td->pages[0], td->version[0]);  	}  	/* Read the mirror version, if available */  	if (md && (md->options & NAND_BBT_VERSION)) { -		scan_read_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift, -			      mtd->writesize, td); +		scan_read(mtd, buf, (loff_t)md->pages[0] << this->page_shift, +			      mtd->writesize, md);  		md->version[0] = buf[bbt_get_ver_offs(mtd, md)]; -		printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", -		       md->pages[0], md->version[0]); +		pr_info("Bad block table at page %d, version 0x%02X\n", +			 md->pages[0], md->version[0]);  	} -	return 1;  } -/* - * Scan a given block full - */ +/* 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 scanlen, int numpages)  {  	int ret, j; -	ret = scan_read_raw_oob(mtd, buf, offs, readlen); -	if (ret) +	ret = scan_read_oob(mtd, buf, offs, readlen); +	/* Ignore ECC errors when checking for BBM */ +	if (ret && !mtd_is_bitflip_or_eccerr(ret))  		return ret; -	for (j = 0; j < len; j++, buf += scanlen) { +	for (j = 0; j < numpages; j++, buf += scanlen) {  		if (check_pattern(buf, scanlen, mtd->writesize, bd))  			return 1;  	}  	return 0;  } -/* - * Scan a given block partially - */ +/* 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) +			   loff_t offs, uint8_t *buf, int numpages)  {  	struct mtd_oob_ops ops;  	int j, ret; @@ -470,16 +426,16 @@ static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,  	ops.oobbuf = buf;  	ops.ooboffs = 0;  	ops.datbuf = NULL; -	ops.mode = MTD_OOB_PLACE; +	ops.mode = MTD_OPS_PLACE_OOB; -	for (j = 0; j < len; j++) { +	for (j = 0; j < numpages; j++) {  		/* -		 * Read the full oob until read_oob is fixed to -		 * handle single byte reads for 16 bit -		 * buswidth +		 * 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) +		ret = mtd_read_oob(mtd, offs, &ops); +		/* Ignore ECC errors when checking for BBM */ +		if (ret && !mtd_is_bitflip_or_eccerr(ret))  			return ret;  		if (check_short_pattern(buf, bd)) @@ -492,32 +448,32 @@ static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,  /**   * 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 + * @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 + * Create a bad block table by scanning the device for the given good/bad block + * identify pattern.   */  static int create_bbt(struct mtd_info *mtd, uint8_t *buf,  	struct nand_bbt_descr *bd, int chip)  {  	struct nand_chip *this = mtd->priv; -	int i, numblocks, len, scanlen; +	int i, numblocks, numpages, scanlen;  	int startblock;  	loff_t from;  	size_t readlen; -	MTDDEBUG(MTD_DEBUG_LEVEL0, "Scanning device for bad blocks\n"); +	pr_info("Scanning device for bad blocks\n");  	if (bd->options & NAND_BBT_SCANALLPAGES) -		len = 1 << (this->bbt_erase_shift - this->page_shift); +		numpages = 1 << (this->bbt_erase_shift - this->page_shift);  	else if (bd->options & NAND_BBT_SCAN2NDPAGE) -		len = 2; +		numpages = 2;  	else -		len = 1; +		numpages = 1;  	if (!(bd->options & NAND_BBT_SCANEMPTY)) {  		/* We need only read few bytes from the OOB area */ @@ -526,18 +482,20 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf,  	} else {  		/* Full page content should be read */  		scanlen = mtd->writesize + mtd->oobsize; -		readlen = len * mtd->writesize; +		readlen = numpages * mtd->writesize;  	}  	if (chip == -1) { -		/* Note that numblocks is 2 * (real numblocks) here, see i+=2 -		 * below as it makes shifting and masking less painful */ +		/* +		 * Note that numblocks is 2 * (real numblocks) here, see i+=2 +		 * below as it makes shifting and masking less painful +		 */  		numblocks = mtd->size >> (this->bbt_erase_shift - 1);  		startblock = 0;  		from = 0;  	} else {  		if (chip >= this->numchips) { -			printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n", +			pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",  			       chip + 1, this->numchips);  			return -EINVAL;  		} @@ -547,8 +505,8 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf,  		from = (loff_t)startblock << (this->bbt_erase_shift - 1);  	} -	if (this->options & NAND_BBT_SCANLASTPAGE) -		from += mtd->erasesize - (mtd->writesize * len); +	if (this->bbt_options & NAND_BBT_SCANLASTPAGE) +		from += mtd->erasesize - (mtd->writesize * numpages);  	for (i = startblock; i < numblocks;) {  		int ret; @@ -557,17 +515,16 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf,  		if (bd->options & NAND_BBT_SCANALLPAGES)  			ret = scan_block_full(mtd, bd, from, buf, readlen, -					      scanlen, len); +					      scanlen, numpages);  		else -			ret = scan_block_fast(mtd, bd, from, buf, len); +			ret = scan_block_fast(mtd, bd, from, buf, numpages);  		if (ret < 0)  			return ret;  		if (ret) {  			this->bbt[i >> 3] |= 0x03 << (i & 0x6); -			MTDDEBUG(MTD_DEBUG_LEVEL0, -				  "Bad eraseblock %d at 0x%012llx\n", +			pr_warn("Bad eraseblock %d at 0x%012llx\n",  				  i >> 1, (unsigned long long)from);  			mtd->ecc_stats.badblocks++;  		} @@ -580,20 +537,18 @@ static int create_bbt(struct mtd_info *mtd, uint8_t *buf,  /**   * 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 + * @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. - * Search is preformed either from the beginning up or from the end of - * the device downwards. The search starts always at the start of a - * block. - * If the option NAND_BBT_PERCHIP is given, each chip is searched - * for a bbt, which contains the bad block information of this chip. - * This is necessary to provide support for certain DOC devices. + * Read the bad block table by searching for a given ident pattern. Search is + * preformed either from the beginning up or from the end of the device + * downwards. The search starts always at the start of a block. If the option + * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains + * the bad block information of this chip. This is necessary to provide support + * for certain DOC devices.   * - * The bbt ident pattern resides in the oob area of the first page - * in a block. + * The bbt ident pattern resides in the oob area of the first page in a block.   */  static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)  { @@ -604,7 +559,7 @@ static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr  	int bbtblocks;  	int blocktopage = this->bbt_erase_shift - this->page_shift; -	/* Search direction top -> down ? */ +	/* Search direction top -> down? */  	if (td->options & NAND_BBT_LASTBLOCK) {  		startblock = (mtd->size >> this->bbt_erase_shift) - 1;  		dir = -1; @@ -613,7 +568,7 @@ static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr  		dir = 1;  	} -	/* Do we have a bbt per chip ? */ +	/* Do we have a bbt per chip? */  	if (td->options & NAND_BBT_PERCHIP) {  		chips = this->numchips;  		bbtblocks = this->chipsize >> this->bbt_erase_shift; @@ -634,7 +589,7 @@ static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr  			loff_t offs = (loff_t)actblock << this->bbt_erase_shift;  			/* Read first page */ -			scan_read_raw(mtd, buf, offs, mtd->writesize, td); +			scan_read(mtd, buf, offs, mtd->writesize, td);  			if (!check_pattern(buf, scanlen, mtd->writesize, td)) {  				td->pages[i] = actblock << blocktopage;  				if (td->options & NAND_BBT_VERSION) { @@ -649,10 +604,9 @@ static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr  	/* Check, if we found a bbt for each requested chip */  	for (i = 0; i < chips; i++) {  		if (td->pages[i] == -1) -			printk(KERN_WARNING "Bad block table not found for chip %d\n", i); +			pr_warn("Bad block table not found for chip %d\n", i);  		else -			MTDDEBUG(MTD_DEBUG_LEVEL0, "Bad block table found " \ -				"at page %d, version 0x%02X\n", td->pages[i], +			pr_info("Bad block table found at page %d, version 0x%02X\n", td->pages[i],  				td->version[i]);  	}  	return 0; @@ -660,14 +614,16 @@ static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr  /**   * search_read_bbts - [GENERIC] scan the device for bad block table(s) - * @mtd:	MTD device structure - * @buf:	temporary buffer - * @td:		descriptor for the bad block table - * @md:		descriptor for the bad block table mirror + * @mtd: MTD device structure + * @buf: temporary buffer + * @td: descriptor for the bad block table + * @md: descriptor for the bad block table mirror   * - * Search and read the bad block table(s) -*/ -static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md) + * Search and read the bad block table(s). + */ +static void search_read_bbts(struct mtd_info *mtd, uint8_t *buf, +			     struct nand_bbt_descr *td, +			     struct nand_bbt_descr *md)  {  	/* Search the primary table */  	search_bbt(mtd, buf, td); @@ -675,23 +631,18 @@ static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt  	/* Search the mirror table */  	if (md)  		search_bbt(mtd, buf, md); - -	/* Force result check */ -	return 1;  }  /**   * write_bbt - [GENERIC] (Re)write the bad block table + * @mtd: MTD device structure + * @buf: temporary buffer + * @td: descriptor for the bad block table + * @md: descriptor for the bad block table mirror + * @chipsel: selector for a specific chip, -1 for all   * - * @mtd:	MTD device structure - * @buf:	temporary buffer - * @td:		descriptor for the bad block table - * @md:		descriptor for the bad block table mirror - * @chipsel:	selector for a specific chip, -1 for all - * - * (Re)write the bad block table - * -*/ + * (Re)write the bad block table. + */  static int write_bbt(struct mtd_info *mtd, uint8_t *buf,  		     struct nand_bbt_descr *td, struct nand_bbt_descr *md,  		     int chipsel) @@ -710,14 +661,14 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,  	ops.ooblen = mtd->oobsize;  	ops.ooboffs = 0;  	ops.datbuf = NULL; -	ops.mode = MTD_OOB_PLACE; +	ops.mode = MTD_OPS_PLACE_OOB;  	if (!rcode)  		rcode = 0xff; -	/* Write bad block table per chip rather than per device ? */ +	/* Write bad block table per chip rather than per device? */  	if (td->options & NAND_BBT_PERCHIP) {  		numblocks = (int)(this->chipsize >> this->bbt_erase_shift); -		/* Full device write or specific chip ? */ +		/* Full device write or specific chip? */  		if (chipsel == -1) {  			nrchips = this->numchips;  		} else { @@ -731,8 +682,8 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,  	/* Loop through the chips */  	for (; chip < nrchips; chip++) { - -		/* There was already a version of the table, reuse the page +		/* +		 * There was already a version of the table, reuse the page  		 * This applies for absolute placement too, as we have the  		 * page nr. in td->pages.  		 */ @@ -741,8 +692,10 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,  			goto write;  		} -		/* Automatic placement of the bad block table */ -		/* Search direction top -> down ? */ +		/* +		 * Automatic placement of the bad block table. Search direction +		 * top -> down? +		 */  		if (td->options & NAND_BBT_LASTBLOCK) {  			startblock = numblocks * (chip + 1) - 1;  			dir = -1; @@ -766,7 +719,7 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,  			if (!md || md->pages[chip] != page)  				goto write;  		} -		printk(KERN_ERR "No space left to write bad block table\n"); +		pr_err("No space left to write bad block table\n");  		return -ENOSPC;  	write: @@ -791,29 +744,27 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,  		bbtoffs = chip * (numblocks >> 2); -		to = ((loff_t) page) << this->page_shift; +		to = ((loff_t)page) << this->page_shift; -		/* Must we save the block contents ? */ +		/* 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)); +			to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1));  			len = 1 << this->bbt_erase_shift; -			res = mtd->read(mtd, to, len, &retlen, buf); +			res = mtd_read(mtd, to, len, &retlen, buf);  			if (res < 0) {  				if (retlen != len) { -					printk(KERN_INFO "nand_bbt: Error " -					       "reading block for writing " -					       "the bad block table\n"); +					pr_info("nand_bbt: error reading block " +						"for writing the bad block table\n");  					return res;  				} -				printk(KERN_WARNING "nand_bbt: ECC error " -				       "while reading block for writing " -				       "bad block table\n"); +				pr_warn("nand_bbt: ECC error while reading " +					"block for writing bad block table\n");  			}  			/* Read oob data */  			ops.ooblen = (len >> this->page_shift) * mtd->oobsize;  			ops.oobbuf = &buf[len]; -			res = mtd->read_oob(mtd, to + mtd->writesize, &ops); +			res = mtd_read_oob(mtd, to + mtd->writesize, &ops);  			if (res < 0 || ops.oobretlen != ops.ooblen)  				goto outerr; @@ -821,19 +772,19 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,  			pageoffs = page - (int)(to >> this->page_shift);  			offs = pageoffs << this->page_shift;  			/* Preset the bbt area with 0xff */ -			memset(&buf[offs], 0xff, (size_t) (numblocks >> sft)); +			memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));  			ooboffs = len + (pageoffs * mtd->oobsize);  		} else if (td->options & NAND_BBT_NO_OOB) {  			ooboffs = 0;  			offs = td->len; -			/* the version byte */ +			/* The version byte */  			if (td->options & NAND_BBT_VERSION)  				offs++;  			/* Calc length */ -			len = (size_t) (numblocks >> sft); +			len = (size_t)(numblocks >> sft);  			len += offs; -			/* Make it page aligned ! */ +			/* Make it page aligned! */  			len = ALIGN(len, mtd->writesize);  			/* Preset the buffer with 0xff */  			memset(buf, 0xff, len); @@ -841,8 +792,8 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,  			memcpy(buf, td->pattern, td->len);  		} else {  			/* Calc length */ -			len = (size_t) (numblocks >> sft); -			/* Make it page aligned ! */ +			len = (size_t)(numblocks >> sft); +			/* Make it page aligned! */  			len = ALIGN(len, mtd->writesize);  			/* Preset the buffer with 0xff */  			memset(buf, 0xff, len + @@ -856,13 +807,13 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,  		if (td->options & NAND_BBT_VERSION)  			buf[ooboffs + td->veroffs] = td->version[chip]; -		/* walk through the memory table */ +		/* Walk through the memory table */  		for (i = 0; i < numblocks;) {  			uint8_t dat;  			dat = this->bbt[bbtoffs + (i >> 2)];  			for (j = 0; j < 4; j++, i++) {  				int sftcnt = (i << (3 - sft)) & sftmsk; -				/* Do not store the reserved bbt blocks ! */ +				/* Do not store the reserved bbt blocks! */  				buf[offs + (i >> sft)] &=  					~(msk[dat & 0x03] << sftcnt);  				dat >>= 2; @@ -883,8 +834,8 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,  		if (res < 0)  			goto outerr; -		printk(KERN_DEBUG "Bad block table written to 0x%012llx, version " -		       "0x%02X\n", (unsigned long long)to, td->version[chip]); +		pr_info("Bad block table written to 0x%012llx, version 0x%02X\n", +			 (unsigned long long)to, td->version[chip]);  		/* Mark it as used */  		td->pages[chip] = page; @@ -892,19 +843,18 @@ static int write_bbt(struct mtd_info *mtd, uint8_t *buf,  	return 0;   outerr: -	printk(KERN_WARNING -	       "nand_bbt: Error while writing bad block table %d\n", res); +	pr_warn("nand_bbt: error while writing bad block table %d\n", res);  	return res;  }  /**   * nand_memory_bbt - [GENERIC] create a memory based bad block table - * @mtd:	MTD device structure - * @bd:		descriptor for the good/bad block search pattern + * @mtd: MTD device structure + * @bd: descriptor for the good/bad block search pattern   * - * The function creates a memory based bbt by scanning the device - * for manufacturer / software marked good / bad blocks -*/ + * The function creates a memory based bbt by scanning the device for + * manufacturer / software marked good / bad blocks. + */  static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)  {  	struct nand_chip *this = mtd->priv; @@ -915,25 +865,24 @@ static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *b  /**   * check_create - [GENERIC] create and write bbt(s) if necessary - * @mtd:	MTD device structure - * @buf:	temporary buffer - * @bd:		descriptor for the good/bad block search pattern + * @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 - * and creates / updates the bbt(s) if necessary - * Creation is necessary if no bbt was found for the chip/device - * Update is necessary if one of the tables is missing or the - * version nr. of one table is less than the other -*/ + * The function checks the results of the previous call to read_bbt and creates + * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found + * for the chip/device. Update is necessary if one of the tables is missing or + * the version nr. of one table is less than the other. + */  static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)  { -	int i, chips, writeops, chipsel, res; +	int i, chips, writeops, create, chipsel, res, res2;  	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 ? */ +	/* Do we have a bbt per chip? */  	if (td->options & NAND_BBT_PERCHIP)  		chips = this->numchips;  	else @@ -941,86 +890,98 @@ static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc  	for (i = 0; i < chips; i++) {  		writeops = 0; +		create = 0;  		rd = NULL;  		rd2 = NULL; -		/* Per chip or per device ? */ +		res = res2 = 0; +		/* Per chip or per device? */  		chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1; -		/* Mirrored table available ? */ +		/* Mirrored table available? */  		if (md) {  			if (td->pages[i] == -1 && md->pages[i] == -1) { +				create = 1;  				writeops = 0x03; -				goto create; -			} - -			if (td->pages[i] == -1) { +			} else if (td->pages[i] == -1) {  				rd = md; -				td->version[i] = md->version[i]; -				writeops = 1; -				goto writecheck; -			} - -			if (md->pages[i] == -1) { +				writeops = 0x01; +			} else if (md->pages[i] == -1) {  				rd = td; -				md->version[i] = td->version[i]; -				writeops = 2; -				goto writecheck; -			} - -			if (td->version[i] == md->version[i]) { +				writeops = 0x02; +			} else if (td->version[i] == md->version[i]) {  				rd = td;  				if (!(td->options & NAND_BBT_VERSION))  					rd2 = md; -				goto writecheck; -			} - -			if (((int8_t) (td->version[i] - md->version[i])) > 0) { +			} else if (((int8_t)(td->version[i] - md->version[i])) > 0) {  				rd = td; -				md->version[i] = td->version[i]; -				writeops = 2; +				writeops = 0x02;  			} else {  				rd = md; -				td->version[i] = md->version[i]; -				writeops = 1; +				writeops = 0x01;  			} - -			goto writecheck; -  		} else {  			if (td->pages[i] == -1) { +				create = 1;  				writeops = 0x01; -				goto create; +			} else { +				rd = td; +			} +		} + +		if (create) { +			/* Create the bad block table by scanning the device? */ +			if (!(td->options & NAND_BBT_CREATE)) +				continue; + +			/* Create the table in memory by scanning the chip(s) */ +			if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY)) +				create_bbt(mtd, buf, bd, chipsel); + +			td->version[i] = 1; +			if (md) +				md->version[i] = 1; +		} + +		/* Read back first? */ +		if (rd) { +			res = read_abs_bbt(mtd, buf, rd, chipsel); +			if (mtd_is_eccerr(res)) { +				/* Mark table as invalid */ +				rd->pages[i] = -1; +				rd->version[i] = 0; +				i--; +				continue; +			} +		} +		/* If they weren't versioned, read both */ +		if (rd2) { +			res2 = read_abs_bbt(mtd, buf, rd2, chipsel); +			if (mtd_is_eccerr(res2)) { +				/* Mark table as invalid */ +				rd2->pages[i] = -1; +				rd2->version[i] = 0; +				i--; +				continue;  			} -			rd = td; -			goto writecheck;  		} -	create: -		/* Create the bad block table by scanning the device ? */ -		if (!(td->options & NAND_BBT_CREATE)) -			continue; -		/* Create the table in memory by scanning the chip(s) */ -		if (!(this->options & NAND_CREATE_EMPTY_BBT)) -			create_bbt(mtd, buf, bd, chipsel); +		/* Scrub the flash table(s)? */ +		if (mtd_is_bitflip(res) || mtd_is_bitflip(res2)) +			writeops = 0x03; -		td->version[i] = 1; -		if (md) -			md->version[i] = 1; -	writecheck: -		/* read back first ? */ -		if (rd) -			read_abs_bbt(mtd, buf, rd, chipsel); -		/* If they weren't versioned, read both. */ -		if (rd2) -			read_abs_bbt(mtd, buf, rd2, chipsel); +		/* Update version numbers before writing */ +		if (md) { +			td->version[i] = max(td->version[i], md->version[i]); +			md->version[i] = td->version[i]; +		} -		/* Write the bad block table to the device ? */ +		/* Write the bad block table to the device? */  		if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {  			res = write_bbt(mtd, buf, td, md, chipsel);  			if (res < 0)  				return res;  		} -		/* Write the mirror bad block table to the device ? */ +		/* Write the mirror bad block table to the device? */  		if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {  			res = write_bbt(mtd, buf, md, td, chipsel);  			if (res < 0) @@ -1032,20 +993,19 @@ static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_desc  /**   * mark_bbt_regions - [GENERIC] mark the bad block table regions - * @mtd:	MTD device structure - * @td:		bad block table descriptor + * @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. -*/ + * The bad block table regions are marked as "bad" to prevent accidental + * erasures / writes. The regions are identified by the mark 0x02. + */  static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)  {  	struct nand_chip *this = mtd->priv;  	int i, j, chips, block, nrblocks, update;  	uint8_t oldval, newval; -	/* Do we have a bbt per chip ? */ +	/* Do we have a bbt per chip? */  	if (td->options & NAND_BBT_PERCHIP) {  		chips = this->numchips;  		nrblocks = (int)(this->chipsize >> this->bbt_erase_shift); @@ -1082,9 +1042,11 @@ static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)  				update = 1;  			block += 2;  		} -		/* 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 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, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));  	} @@ -1092,8 +1054,8 @@ static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)  /**   * verify_bbt_descr - verify the bad block description - * @mtd:	MTD device structure - * @bd:		the table to verify + * @mtd: MTD device structure + * @bd: the table to verify   *   * This functions performs a few sanity checks on the bad block description   * table. @@ -1111,16 +1073,16 @@ static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)  	pattern_len = bd->len;  	bits = bd->options & NAND_BBT_NRBITS_MSK; -	BUG_ON((this->options & NAND_USE_FLASH_BBT_NO_OOB) && -			!(this->options & NAND_USE_FLASH_BBT)); +	BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) && +			!(this->bbt_options & NAND_BBT_USE_FLASH));  	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(!(this->bbt_options & NAND_BBT_USE_FLASH)); +		BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));  		BUG_ON(bd->offs);  		if (bd->options & NAND_BBT_VERSION)  			BUG_ON(bd->veroffs != bd->len); @@ -1140,18 +1102,16 @@ static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)  /**   * 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 - * - * The function checks, if a bad block table(s) is/are already - * available. If not it scans the device for manufacturer - * marked good / bad blocks and writes the bad block table(s) to - * the selected place. + * @mtd: MTD device structure + * @bd: descriptor for the good/bad block search pattern   * - * The bad block table memory is allocated here. It must be freed - * by calling the nand_free_bbt function. + * The function checks, if a bad block table(s) is/are already available. If + * not it scans the device for manufacturer marked good / bad blocks and writes + * the bad block table(s) to the selected place.   * -*/ + * The bad block table memory is allocated here. It must be freed by calling + * the nand_free_bbt function. + */  int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)  {  	struct nand_chip *this = mtd->priv; @@ -1161,19 +1121,21 @@ int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)  	struct nand_bbt_descr *md = this->bbt_md;  	len = mtd->size >> (this->bbt_erase_shift + 2); -	/* Allocate memory (2bit per block) and clear the memory bad block table */ +	/* +	 * Allocate memory (2bit per block) and clear the memory bad block +	 * table. +	 */  	this->bbt = kzalloc(len, GFP_KERNEL); -	if (!this->bbt) { -		printk(KERN_ERR "nand_scan_bbt: Out of memory\n"); +	if (!this->bbt)  		return -ENOMEM; -	} -	/* If no primary table decriptor is given, scan the device -	 * to build a memory based bad block table +	/* +	 * If no primary table decriptor is given, scan the device to build a +	 * memory based bad block table.  	 */  	if (!td) {  		if ((res = nand_memory_bbt(mtd, bd))) { -			printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n"); +			pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");  			kfree(this->bbt);  			this->bbt = NULL;  		} @@ -1187,22 +1149,20 @@ int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)  	len += (len >> this->page_shift) * mtd->oobsize;  	buf = vmalloc(len);  	if (!buf) { -		printk(KERN_ERR "nand_bbt: Out of memory\n");  		kfree(this->bbt);  		this->bbt = NULL;  		return -ENOMEM;  	} -	/* Is the bbt at a given page ? */ +	/* Is the bbt at a given page? */  	if (td->options & NAND_BBT_ABSPAGE) { -		res = read_abs_bbts(mtd, buf, td, md); +		read_abs_bbts(mtd, buf, td, md);  	} else {  		/* Search the bad block table using a pattern in oob */ -		res = search_read_bbts(mtd, buf, td, md); +		search_read_bbts(mtd, buf, td, md);  	} -	if (res) -		res = check_create(mtd, buf, bd); +	res = check_create(mtd, buf, bd);  	/* Prevent the bbt regions from erasing / writing */  	mark_bbt_region(mtd, td); @@ -1215,15 +1175,15 @@ int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)  /**   * nand_update_bbt - [NAND Interface] update bad block table(s) - * @mtd:	MTD device structure - * @offs:	the offset of the newly marked block + * @mtd: MTD device structure + * @offs: the offset of the newly marked block   * - * The function updates the bad block table(s) -*/ + * The function updates the bad block table(s). + */  int nand_update_bbt(struct mtd_info *mtd, loff_t offs)  {  	struct nand_chip *this = mtd->priv; -	int len, res = 0, writeops = 0; +	int len, res = 0;  	int chip, chipsel;  	uint8_t *buf;  	struct nand_bbt_descr *td = this->bbt_td; @@ -1236,14 +1196,10 @@ int nand_update_bbt(struct mtd_info *mtd, loff_t offs)  	len = (1 << this->bbt_erase_shift);  	len += (len >> this->page_shift) * mtd->oobsize;  	buf = kmalloc(len, GFP_KERNEL); -	if (!buf) { -		printk(KERN_ERR "nand_update_bbt: Out of memory\n"); +	if (!buf)  		return -ENOMEM; -	} -	writeops = md != NULL ? 0x03 : 0x01; - -	/* Do we have a bbt per chip ? */ +	/* Do we have a bbt per chip? */  	if (td->options & NAND_BBT_PERCHIP) {  		chip = (int)(offs >> this->chip_shift);  		chipsel = chip; @@ -1256,14 +1212,14 @@ int nand_update_bbt(struct mtd_info *mtd, loff_t offs)  	if (md)  		md->version[chip]++; -	/* Write the bad block table to the device ? */ -	if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) { +	/* Write the bad block table to the device? */ +	if (td->options & NAND_BBT_WRITE) {  		res = write_bbt(mtd, buf, td, md, chipsel);  		if (res < 0)  			goto out;  	} -	/* Write the mirror bad block table to the device ? */ -	if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) { +	/* Write the mirror bad block table to the device? */ +	if (md && (md->options & NAND_BBT_WRITE)) {  		res = write_bbt(mtd, buf, md, td, chipsel);  	} @@ -1272,8 +1228,10 @@ int nand_update_bbt(struct mtd_info *mtd, loff_t offs)  	return res;  } -/* Define some generic bad / good block scan pattern which are used - * while scanning a device for factory marked good / bad blocks. */ +/* + * Define some generic bad / good block scan pattern which are used + * while scanning a device for factory marked good / bad blocks. + */  static uint8_t scan_ff_pattern[] = { 0xff, 0xff };  static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 }; @@ -1285,8 +1243,7 @@ static struct nand_bbt_descr agand_flashbased = {  	.pattern = scan_agand_pattern  }; -/* Generic flash bbt decriptors -*/ +/* Generic flash bbt descriptors */  static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };  static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' }; @@ -1296,7 +1253,7 @@ static struct nand_bbt_descr bbt_main_descr = {  	.offs =	8,  	.len = 4,  	.veroffs = 12, -	.maxblocks = 4, +	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,  	.pattern = bbt_pattern  }; @@ -1306,55 +1263,51 @@ static struct nand_bbt_descr bbt_mirror_descr = {  	.offs =	8,  	.len = 4,  	.veroffs = 12, -	.maxblocks = 4, +	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,  	.pattern = mirror_pattern  }; -static struct nand_bbt_descr bbt_main_no_bbt_descr = { +static struct nand_bbt_descr bbt_main_no_oob_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, +	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,  	.pattern = bbt_pattern  }; -static struct nand_bbt_descr bbt_mirror_no_bbt_descr = { +static struct nand_bbt_descr bbt_mirror_no_oob_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, +	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,  	.pattern = mirror_pattern  }; -#define BBT_SCAN_OPTIONS (NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE | \ -		NAND_BBT_SCANBYTE1AND6) +#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)  /** - * nand_create_default_bbt_descr - [Internal] Creates a BBT descriptor structure - * @this:	NAND chip to create descriptor for + * nand_create_badblock_pattern - [INTERN] 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 + * 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. - *   */ -static int nand_create_default_bbt_descr(struct nand_chip *this) +static int nand_create_badblock_pattern(struct nand_chip *this)  {  	struct nand_bbt_descr *bd;  	if (this->badblock_pattern) { -		printk(KERN_WARNING "BBT descr already allocated; not replacing.\n"); +		pr_warn("Bad block pattern 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"); +	if (!bd)  		return -ENOMEM; -	} -	bd->options = this->options & BBT_SCAN_OPTIONS; +	bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;  	bd->offs = this->badblockpos;  	bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;  	bd->pattern = scan_ff_pattern; @@ -1365,22 +1318,20 @@ static int nand_create_default_bbt_descr(struct nand_chip *this)  /**   * nand_default_bbt - [NAND Interface] Select a default bad block table for the device - * @mtd:	MTD device structure - * - * This function selects the default bad block table - * support for the device and calls the nand_scan_bbt function + * @mtd: MTD device structure   * -*/ + * This function selects the default bad block table support for the device and + * calls the nand_scan_bbt function. + */  int nand_default_bbt(struct mtd_info *mtd)  {  	struct nand_chip *this = mtd->priv; -	/* Default for AG-AND. We must use a flash based -	 * 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 -	 * this information in a good / bad table during -	 * startup +	/* +	 * Default for AG-AND. We must use a flash based 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 this +	 * information in a good / bad table during startup.  	 */  	if (this->options & NAND_IS_AND) {  		/* Use the default pattern descriptors */ @@ -1388,17 +1339,17 @@ int nand_default_bbt(struct mtd_info *mtd)  			this->bbt_td = &bbt_main_descr;  			this->bbt_md = &bbt_mirror_descr;  		} -		this->options |= NAND_USE_FLASH_BBT; +		this->bbt_options |= NAND_BBT_USE_FLASH;  		return nand_scan_bbt(mtd, &agand_flashbased);  	} -	/* Is a flash based bad block table requested ? */ -	if (this->options & NAND_USE_FLASH_BBT) { +	/* Is a flash based bad block table requested? */ +	if (this->bbt_options & NAND_BBT_USE_FLASH) {  		/* Use the default pattern descriptors */  		if (!this->bbt_td) { -			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; +			if (this->bbt_options & NAND_BBT_NO_OOB) { +				this->bbt_td = &bbt_main_no_oob_descr; +				this->bbt_md = &bbt_mirror_no_oob_descr;  			} else {  				this->bbt_td = &bbt_main_descr;  				this->bbt_md = &bbt_mirror_descr; @@ -1410,18 +1361,17 @@ int nand_default_bbt(struct mtd_info *mtd)  	}  	if (!this->badblock_pattern) -		nand_create_default_bbt_descr(this); +		nand_create_badblock_pattern(this);  	return nand_scan_bbt(mtd, this->badblock_pattern);  }  /**   * nand_isbad_bbt - [NAND Interface] Check if a block is bad - * @mtd:	MTD device structure - * @offs:	offset in the device - * @allowbbt:	allow access to bad block table region - * -*/ + * @mtd: MTD device structure + * @offs: offset in the device + * @allowbbt: allow access to bad block table region + */  int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)  {  	struct nand_chip *this = mtd->priv; |