xref: /OK3568_Linux_fs/kernel/drivers/mtd/nand/raw/nand_bbt.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  Overview:
4*4882a593Smuzhiyun  *   Bad block table support for the NAND driver
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *  Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Description:
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * When nand_scan_bbt is called, then it tries to find the bad block table
11*4882a593Smuzhiyun  * depending on the options in the BBT descriptor(s). If no flash based BBT
12*4882a593Smuzhiyun  * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
13*4882a593Smuzhiyun  * marked good / bad blocks. This information is used to create a memory BBT.
14*4882a593Smuzhiyun  * Once a new bad block is discovered then the "factory" information is updated
15*4882a593Smuzhiyun  * on the device.
16*4882a593Smuzhiyun  * If a flash based BBT is specified then the function first tries to find the
17*4882a593Smuzhiyun  * BBT on flash. If a BBT is found then the contents are read and the memory
18*4882a593Smuzhiyun  * based BBT is created. If a mirrored BBT is selected then the mirror is
19*4882a593Smuzhiyun  * searched too and the versions are compared. If the mirror has a greater
20*4882a593Smuzhiyun  * version number, then the mirror BBT is used to build the memory based BBT.
21*4882a593Smuzhiyun  * If the tables are not versioned, then we "or" the bad block information.
22*4882a593Smuzhiyun  * If one of the BBTs is out of date or does not exist it is (re)created.
23*4882a593Smuzhiyun  * If no BBT exists at all then the device is scanned for factory marked
24*4882a593Smuzhiyun  * good / bad blocks and the bad block tables are created.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * For manufacturer created BBTs like the one found on M-SYS DOC devices
27*4882a593Smuzhiyun  * the BBT is searched and read but never created
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * The auto generated bad block table is located in the last good blocks
30*4882a593Smuzhiyun  * of the device. The table is mirrored, so it can be updated eventually.
31*4882a593Smuzhiyun  * The table is marked in the OOB area with an ident pattern and a version
32*4882a593Smuzhiyun  * number which indicates which of both tables is more up to date. If the NAND
33*4882a593Smuzhiyun  * controller needs the complete OOB area for the ECC information then the
34*4882a593Smuzhiyun  * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
35*4882a593Smuzhiyun  * course): it moves the ident pattern and the version byte into the data area
36*4882a593Smuzhiyun  * and the OOB area will remain untouched.
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * The table uses 2 bits per block
39*4882a593Smuzhiyun  * 11b:		block is good
40*4882a593Smuzhiyun  * 00b:		block is factory marked bad
41*4882a593Smuzhiyun  * 01b, 10b:	block is marked bad due to wear
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * The memory bad block table uses the following scheme:
44*4882a593Smuzhiyun  * 00b:		block is good
45*4882a593Smuzhiyun  * 01b:		block is marked bad due to wear
46*4882a593Smuzhiyun  * 10b:		block is reserved (to protect the bbt area)
47*4882a593Smuzhiyun  * 11b:		block is factory marked bad
48*4882a593Smuzhiyun  *
49*4882a593Smuzhiyun  * Multichip devices like DOC store the bad block info per floor.
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * Following assumptions are made:
52*4882a593Smuzhiyun  * - bbts start at a page boundary, if autolocated on a block boundary
53*4882a593Smuzhiyun  * - the space necessary for a bbt in FLASH does not exceed a block boundary
54*4882a593Smuzhiyun  */
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #include <linux/slab.h>
57*4882a593Smuzhiyun #include <linux/types.h>
58*4882a593Smuzhiyun #include <linux/mtd/mtd.h>
59*4882a593Smuzhiyun #include <linux/mtd/bbm.h>
60*4882a593Smuzhiyun #include <linux/bitops.h>
61*4882a593Smuzhiyun #include <linux/delay.h>
62*4882a593Smuzhiyun #include <linux/vmalloc.h>
63*4882a593Smuzhiyun #include <linux/export.h>
64*4882a593Smuzhiyun #include <linux/string.h>
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun #include "internals.h"
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun #define BBT_BLOCK_GOOD		0x00
69*4882a593Smuzhiyun #define BBT_BLOCK_WORN		0x01
70*4882a593Smuzhiyun #define BBT_BLOCK_RESERVED	0x02
71*4882a593Smuzhiyun #define BBT_BLOCK_FACTORY_BAD	0x03
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun #define BBT_ENTRY_MASK		0x03
74*4882a593Smuzhiyun #define BBT_ENTRY_SHIFT		2
75*4882a593Smuzhiyun 
bbt_get_entry(struct nand_chip * chip,int block)76*4882a593Smuzhiyun static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
79*4882a593Smuzhiyun 	entry >>= (block & BBT_ENTRY_MASK) * 2;
80*4882a593Smuzhiyun 	return entry & BBT_ENTRY_MASK;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
bbt_mark_entry(struct nand_chip * chip,int block,uint8_t mark)83*4882a593Smuzhiyun static inline void bbt_mark_entry(struct nand_chip *chip, int block,
84*4882a593Smuzhiyun 		uint8_t mark)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
87*4882a593Smuzhiyun 	chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
check_pattern_no_oob(uint8_t * buf,struct nand_bbt_descr * td)90*4882a593Smuzhiyun static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	if (memcmp(buf, td->pattern, td->len))
93*4882a593Smuzhiyun 		return -1;
94*4882a593Smuzhiyun 	return 0;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /**
98*4882a593Smuzhiyun  * check_pattern - [GENERIC] check if a pattern is in the buffer
99*4882a593Smuzhiyun  * @buf: the buffer to search
100*4882a593Smuzhiyun  * @len: the length of buffer to search
101*4882a593Smuzhiyun  * @paglen: the pagelength
102*4882a593Smuzhiyun  * @td: search pattern descriptor
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  * Check for a pattern at the given place. Used to search bad block tables and
105*4882a593Smuzhiyun  * good / bad block identifiers.
106*4882a593Smuzhiyun  */
check_pattern(uint8_t * buf,int len,int paglen,struct nand_bbt_descr * td)107*4882a593Smuzhiyun static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	if (td->options & NAND_BBT_NO_OOB)
110*4882a593Smuzhiyun 		return check_pattern_no_oob(buf, td);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	/* Compare the pattern */
113*4882a593Smuzhiyun 	if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
114*4882a593Smuzhiyun 		return -1;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	return 0;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /**
120*4882a593Smuzhiyun  * check_short_pattern - [GENERIC] check if a pattern is in the buffer
121*4882a593Smuzhiyun  * @buf: the buffer to search
122*4882a593Smuzhiyun  * @td:	search pattern descriptor
123*4882a593Smuzhiyun  *
124*4882a593Smuzhiyun  * Check for a pattern at the given place. Used to search bad block tables and
125*4882a593Smuzhiyun  * good / bad block identifiers. Same as check_pattern, but no optional empty
126*4882a593Smuzhiyun  * check.
127*4882a593Smuzhiyun  */
check_short_pattern(uint8_t * buf,struct nand_bbt_descr * td)128*4882a593Smuzhiyun static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	/* Compare the pattern */
131*4882a593Smuzhiyun 	if (memcmp(buf + td->offs, td->pattern, td->len))
132*4882a593Smuzhiyun 		return -1;
133*4882a593Smuzhiyun 	return 0;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun /**
137*4882a593Smuzhiyun  * add_marker_len - compute the length of the marker in data area
138*4882a593Smuzhiyun  * @td: BBT descriptor used for computation
139*4882a593Smuzhiyun  *
140*4882a593Smuzhiyun  * The length will be 0 if the marker is located in OOB area.
141*4882a593Smuzhiyun  */
add_marker_len(struct nand_bbt_descr * td)142*4882a593Smuzhiyun static u32 add_marker_len(struct nand_bbt_descr *td)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	u32 len;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	if (!(td->options & NAND_BBT_NO_OOB))
147*4882a593Smuzhiyun 		return 0;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	len = td->len;
150*4882a593Smuzhiyun 	if (td->options & NAND_BBT_VERSION)
151*4882a593Smuzhiyun 		len++;
152*4882a593Smuzhiyun 	return len;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun /**
156*4882a593Smuzhiyun  * read_bbt - [GENERIC] Read the bad block table starting from page
157*4882a593Smuzhiyun  * @this: NAND chip object
158*4882a593Smuzhiyun  * @buf: temporary buffer
159*4882a593Smuzhiyun  * @page: the starting page
160*4882a593Smuzhiyun  * @num: the number of bbt descriptors to read
161*4882a593Smuzhiyun  * @td: the bbt describtion table
162*4882a593Smuzhiyun  * @offs: block number offset in the table
163*4882a593Smuzhiyun  *
164*4882a593Smuzhiyun  * Read the bad block table starting from page.
165*4882a593Smuzhiyun  */
read_bbt(struct nand_chip * this,uint8_t * buf,int page,int num,struct nand_bbt_descr * td,int offs)166*4882a593Smuzhiyun static int read_bbt(struct nand_chip *this, uint8_t *buf, int page, int num,
167*4882a593Smuzhiyun 		    struct nand_bbt_descr *td, int offs)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(this);
170*4882a593Smuzhiyun 	int res, ret = 0, i, j, act = 0;
171*4882a593Smuzhiyun 	size_t retlen, len, totlen;
172*4882a593Smuzhiyun 	loff_t from;
173*4882a593Smuzhiyun 	int bits = td->options & NAND_BBT_NRBITS_MSK;
174*4882a593Smuzhiyun 	uint8_t msk = (uint8_t)((1 << bits) - 1);
175*4882a593Smuzhiyun 	u32 marker_len;
176*4882a593Smuzhiyun 	int reserved_block_code = td->reserved_block_code;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	totlen = (num * bits) >> 3;
179*4882a593Smuzhiyun 	marker_len = add_marker_len(td);
180*4882a593Smuzhiyun 	from = ((loff_t)page) << this->page_shift;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	while (totlen) {
183*4882a593Smuzhiyun 		len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
184*4882a593Smuzhiyun 		if (marker_len) {
185*4882a593Smuzhiyun 			/*
186*4882a593Smuzhiyun 			 * In case the BBT marker is not in the OOB area it
187*4882a593Smuzhiyun 			 * will be just in the first page.
188*4882a593Smuzhiyun 			 */
189*4882a593Smuzhiyun 			len -= marker_len;
190*4882a593Smuzhiyun 			from += marker_len;
191*4882a593Smuzhiyun 			marker_len = 0;
192*4882a593Smuzhiyun 		}
193*4882a593Smuzhiyun 		res = mtd_read(mtd, from, len, &retlen, buf);
194*4882a593Smuzhiyun 		if (res < 0) {
195*4882a593Smuzhiyun 			if (mtd_is_eccerr(res)) {
196*4882a593Smuzhiyun 				pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
197*4882a593Smuzhiyun 					from & ~mtd->writesize);
198*4882a593Smuzhiyun 				return res;
199*4882a593Smuzhiyun 			} else if (mtd_is_bitflip(res)) {
200*4882a593Smuzhiyun 				pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
201*4882a593Smuzhiyun 					from & ~mtd->writesize);
202*4882a593Smuzhiyun 				ret = res;
203*4882a593Smuzhiyun 			} else {
204*4882a593Smuzhiyun 				pr_info("nand_bbt: error reading BBT\n");
205*4882a593Smuzhiyun 				return res;
206*4882a593Smuzhiyun 			}
207*4882a593Smuzhiyun 		}
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 		/* Analyse data */
210*4882a593Smuzhiyun 		for (i = 0; i < len; i++) {
211*4882a593Smuzhiyun 			uint8_t dat = buf[i];
212*4882a593Smuzhiyun 			for (j = 0; j < 8; j += bits, act++) {
213*4882a593Smuzhiyun 				uint8_t tmp = (dat >> j) & msk;
214*4882a593Smuzhiyun 				if (tmp == msk)
215*4882a593Smuzhiyun 					continue;
216*4882a593Smuzhiyun 				if (reserved_block_code && (tmp == reserved_block_code)) {
217*4882a593Smuzhiyun 					pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
218*4882a593Smuzhiyun 						 (loff_t)(offs + act) <<
219*4882a593Smuzhiyun 						 this->bbt_erase_shift);
220*4882a593Smuzhiyun 					bbt_mark_entry(this, offs + act,
221*4882a593Smuzhiyun 							BBT_BLOCK_RESERVED);
222*4882a593Smuzhiyun 					mtd->ecc_stats.bbtblocks++;
223*4882a593Smuzhiyun 					continue;
224*4882a593Smuzhiyun 				}
225*4882a593Smuzhiyun 				/*
226*4882a593Smuzhiyun 				 * Leave it for now, if it's matured we can
227*4882a593Smuzhiyun 				 * move this message to pr_debug.
228*4882a593Smuzhiyun 				 */
229*4882a593Smuzhiyun 				pr_info("nand_read_bbt: bad block at 0x%012llx\n",
230*4882a593Smuzhiyun 					 (loff_t)(offs + act) <<
231*4882a593Smuzhiyun 					 this->bbt_erase_shift);
232*4882a593Smuzhiyun 				/* Factory marked bad or worn out? */
233*4882a593Smuzhiyun 				if (tmp == 0)
234*4882a593Smuzhiyun 					bbt_mark_entry(this, offs + act,
235*4882a593Smuzhiyun 							BBT_BLOCK_FACTORY_BAD);
236*4882a593Smuzhiyun 				else
237*4882a593Smuzhiyun 					bbt_mark_entry(this, offs + act,
238*4882a593Smuzhiyun 							BBT_BLOCK_WORN);
239*4882a593Smuzhiyun 				mtd->ecc_stats.badblocks++;
240*4882a593Smuzhiyun 			}
241*4882a593Smuzhiyun 		}
242*4882a593Smuzhiyun 		totlen -= len;
243*4882a593Smuzhiyun 		from += len;
244*4882a593Smuzhiyun 	}
245*4882a593Smuzhiyun 	return ret;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun /**
249*4882a593Smuzhiyun  * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
250*4882a593Smuzhiyun  * @this: NAND chip object
251*4882a593Smuzhiyun  * @buf: temporary buffer
252*4882a593Smuzhiyun  * @td: descriptor for the bad block table
253*4882a593Smuzhiyun  * @chip: read the table for a specific chip, -1 read all chips; applies only if
254*4882a593Smuzhiyun  *        NAND_BBT_PERCHIP option is set
255*4882a593Smuzhiyun  *
256*4882a593Smuzhiyun  * Read the bad block table for all chips starting at a given page. We assume
257*4882a593Smuzhiyun  * that the bbt bits are in consecutive order.
258*4882a593Smuzhiyun  */
read_abs_bbt(struct nand_chip * this,uint8_t * buf,struct nand_bbt_descr * td,int chip)259*4882a593Smuzhiyun static int read_abs_bbt(struct nand_chip *this, uint8_t *buf,
260*4882a593Smuzhiyun 			struct nand_bbt_descr *td, int chip)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(this);
263*4882a593Smuzhiyun 	u64 targetsize = nanddev_target_size(&this->base);
264*4882a593Smuzhiyun 	int res = 0, i;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	if (td->options & NAND_BBT_PERCHIP) {
267*4882a593Smuzhiyun 		int offs = 0;
268*4882a593Smuzhiyun 		for (i = 0; i < nanddev_ntargets(&this->base); i++) {
269*4882a593Smuzhiyun 			if (chip == -1 || chip == i)
270*4882a593Smuzhiyun 				res = read_bbt(this, buf, td->pages[i],
271*4882a593Smuzhiyun 					targetsize >> this->bbt_erase_shift,
272*4882a593Smuzhiyun 					td, offs);
273*4882a593Smuzhiyun 			if (res)
274*4882a593Smuzhiyun 				return res;
275*4882a593Smuzhiyun 			offs += targetsize >> this->bbt_erase_shift;
276*4882a593Smuzhiyun 		}
277*4882a593Smuzhiyun 	} else {
278*4882a593Smuzhiyun 		res = read_bbt(this, buf, td->pages[0],
279*4882a593Smuzhiyun 				mtd->size >> this->bbt_erase_shift, td, 0);
280*4882a593Smuzhiyun 		if (res)
281*4882a593Smuzhiyun 			return res;
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun 	return 0;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun /* BBT marker is in the first page, no OOB */
scan_read_data(struct nand_chip * this,uint8_t * buf,loff_t offs,struct nand_bbt_descr * td)287*4882a593Smuzhiyun static int scan_read_data(struct nand_chip *this, uint8_t *buf, loff_t offs,
288*4882a593Smuzhiyun 			  struct nand_bbt_descr *td)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(this);
291*4882a593Smuzhiyun 	size_t retlen;
292*4882a593Smuzhiyun 	size_t len;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	len = td->len;
295*4882a593Smuzhiyun 	if (td->options & NAND_BBT_VERSION)
296*4882a593Smuzhiyun 		len++;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	return mtd_read(mtd, offs, len, &retlen, buf);
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun /**
302*4882a593Smuzhiyun  * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
303*4882a593Smuzhiyun  * @this: NAND chip object
304*4882a593Smuzhiyun  * @buf: temporary buffer
305*4882a593Smuzhiyun  * @offs: offset at which to scan
306*4882a593Smuzhiyun  * @len: length of data region to read
307*4882a593Smuzhiyun  *
308*4882a593Smuzhiyun  * Scan read data from data+OOB. May traverse multiple pages, interleaving
309*4882a593Smuzhiyun  * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"
310*4882a593Smuzhiyun  * ECC condition (error or bitflip). May quit on the first (non-ECC) error.
311*4882a593Smuzhiyun  */
scan_read_oob(struct nand_chip * this,uint8_t * buf,loff_t offs,size_t len)312*4882a593Smuzhiyun static int scan_read_oob(struct nand_chip *this, uint8_t *buf, loff_t offs,
313*4882a593Smuzhiyun 			 size_t len)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(this);
316*4882a593Smuzhiyun 	struct mtd_oob_ops ops;
317*4882a593Smuzhiyun 	int res, ret = 0;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	ops.mode = MTD_OPS_PLACE_OOB;
320*4882a593Smuzhiyun 	ops.ooboffs = 0;
321*4882a593Smuzhiyun 	ops.ooblen = mtd->oobsize;
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	while (len > 0) {
324*4882a593Smuzhiyun 		ops.datbuf = buf;
325*4882a593Smuzhiyun 		ops.len = min(len, (size_t)mtd->writesize);
326*4882a593Smuzhiyun 		ops.oobbuf = buf + ops.len;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 		res = mtd_read_oob(mtd, offs, &ops);
329*4882a593Smuzhiyun 		if (res) {
330*4882a593Smuzhiyun 			if (!mtd_is_bitflip_or_eccerr(res))
331*4882a593Smuzhiyun 				return res;
332*4882a593Smuzhiyun 			else if (mtd_is_eccerr(res) || !ret)
333*4882a593Smuzhiyun 				ret = res;
334*4882a593Smuzhiyun 		}
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 		buf += mtd->oobsize + mtd->writesize;
337*4882a593Smuzhiyun 		len -= mtd->writesize;
338*4882a593Smuzhiyun 		offs += mtd->writesize;
339*4882a593Smuzhiyun 	}
340*4882a593Smuzhiyun 	return ret;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun 
scan_read(struct nand_chip * this,uint8_t * buf,loff_t offs,size_t len,struct nand_bbt_descr * td)343*4882a593Smuzhiyun static int scan_read(struct nand_chip *this, uint8_t *buf, loff_t offs,
344*4882a593Smuzhiyun 		     size_t len, struct nand_bbt_descr *td)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun 	if (td->options & NAND_BBT_NO_OOB)
347*4882a593Smuzhiyun 		return scan_read_data(this, buf, offs, td);
348*4882a593Smuzhiyun 	else
349*4882a593Smuzhiyun 		return scan_read_oob(this, buf, offs, len);
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun /* Scan write data with oob to flash */
scan_write_bbt(struct nand_chip * this,loff_t offs,size_t len,uint8_t * buf,uint8_t * oob)353*4882a593Smuzhiyun static int scan_write_bbt(struct nand_chip *this, loff_t offs, size_t len,
354*4882a593Smuzhiyun 			  uint8_t *buf, uint8_t *oob)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(this);
357*4882a593Smuzhiyun 	struct mtd_oob_ops ops;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	ops.mode = MTD_OPS_PLACE_OOB;
360*4882a593Smuzhiyun 	ops.ooboffs = 0;
361*4882a593Smuzhiyun 	ops.ooblen = mtd->oobsize;
362*4882a593Smuzhiyun 	ops.datbuf = buf;
363*4882a593Smuzhiyun 	ops.oobbuf = oob;
364*4882a593Smuzhiyun 	ops.len = len;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	return mtd_write_oob(mtd, offs, &ops);
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun 
bbt_get_ver_offs(struct nand_chip * this,struct nand_bbt_descr * td)369*4882a593Smuzhiyun static u32 bbt_get_ver_offs(struct nand_chip *this, struct nand_bbt_descr *td)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(this);
372*4882a593Smuzhiyun 	u32 ver_offs = td->veroffs;
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	if (!(td->options & NAND_BBT_NO_OOB))
375*4882a593Smuzhiyun 		ver_offs += mtd->writesize;
376*4882a593Smuzhiyun 	return ver_offs;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun /**
380*4882a593Smuzhiyun  * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
381*4882a593Smuzhiyun  * @this: NAND chip object
382*4882a593Smuzhiyun  * @buf: temporary buffer
383*4882a593Smuzhiyun  * @td: descriptor for the bad block table
384*4882a593Smuzhiyun  * @md:	descriptor for the bad block table mirror
385*4882a593Smuzhiyun  *
386*4882a593Smuzhiyun  * Read the bad block table(s) for all chips starting at a given page. We
387*4882a593Smuzhiyun  * assume that the bbt bits are in consecutive order.
388*4882a593Smuzhiyun  */
read_abs_bbts(struct nand_chip * this,uint8_t * buf,struct nand_bbt_descr * td,struct nand_bbt_descr * md)389*4882a593Smuzhiyun static void read_abs_bbts(struct nand_chip *this, uint8_t *buf,
390*4882a593Smuzhiyun 			  struct nand_bbt_descr *td, struct nand_bbt_descr *md)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(this);
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	/* Read the primary version, if available */
395*4882a593Smuzhiyun 	if (td->options & NAND_BBT_VERSION) {
396*4882a593Smuzhiyun 		scan_read(this, buf, (loff_t)td->pages[0] << this->page_shift,
397*4882a593Smuzhiyun 			  mtd->writesize, td);
398*4882a593Smuzhiyun 		td->version[0] = buf[bbt_get_ver_offs(this, td)];
399*4882a593Smuzhiyun 		pr_info("Bad block table at page %d, version 0x%02X\n",
400*4882a593Smuzhiyun 			 td->pages[0], td->version[0]);
401*4882a593Smuzhiyun 	}
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	/* Read the mirror version, if available */
404*4882a593Smuzhiyun 	if (md && (md->options & NAND_BBT_VERSION)) {
405*4882a593Smuzhiyun 		scan_read(this, buf, (loff_t)md->pages[0] << this->page_shift,
406*4882a593Smuzhiyun 			  mtd->writesize, md);
407*4882a593Smuzhiyun 		md->version[0] = buf[bbt_get_ver_offs(this, md)];
408*4882a593Smuzhiyun 		pr_info("Bad block table at page %d, version 0x%02X\n",
409*4882a593Smuzhiyun 			 md->pages[0], md->version[0]);
410*4882a593Smuzhiyun 	}
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun /* Scan a given block partially */
scan_block_fast(struct nand_chip * this,struct nand_bbt_descr * bd,loff_t offs,uint8_t * buf)414*4882a593Smuzhiyun static int scan_block_fast(struct nand_chip *this, struct nand_bbt_descr *bd,
415*4882a593Smuzhiyun 			   loff_t offs, uint8_t *buf)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(this);
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	struct mtd_oob_ops ops;
420*4882a593Smuzhiyun 	int ret, page_offset;
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	ops.ooblen = mtd->oobsize;
423*4882a593Smuzhiyun 	ops.oobbuf = buf;
424*4882a593Smuzhiyun 	ops.ooboffs = 0;
425*4882a593Smuzhiyun 	ops.datbuf = NULL;
426*4882a593Smuzhiyun 	ops.mode = MTD_OPS_PLACE_OOB;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	page_offset = nand_bbm_get_next_page(this, 0);
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	while (page_offset >= 0) {
431*4882a593Smuzhiyun 		/*
432*4882a593Smuzhiyun 		 * Read the full oob until read_oob is fixed to handle single
433*4882a593Smuzhiyun 		 * byte reads for 16 bit buswidth.
434*4882a593Smuzhiyun 		 */
435*4882a593Smuzhiyun 		ret = mtd_read_oob(mtd, offs + (page_offset * mtd->writesize),
436*4882a593Smuzhiyun 				   &ops);
437*4882a593Smuzhiyun 		/* Ignore ECC errors when checking for BBM */
438*4882a593Smuzhiyun 		if (ret && !mtd_is_bitflip_or_eccerr(ret))
439*4882a593Smuzhiyun 			return ret;
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 		if (check_short_pattern(buf, bd))
442*4882a593Smuzhiyun 			return 1;
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 		page_offset = nand_bbm_get_next_page(this, page_offset + 1);
445*4882a593Smuzhiyun 	}
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	return 0;
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun /**
451*4882a593Smuzhiyun  * create_bbt - [GENERIC] Create a bad block table by scanning the device
452*4882a593Smuzhiyun  * @this: NAND chip object
453*4882a593Smuzhiyun  * @buf: temporary buffer
454*4882a593Smuzhiyun  * @bd: descriptor for the good/bad block search pattern
455*4882a593Smuzhiyun  * @chip: create the table for a specific chip, -1 read all chips; applies only
456*4882a593Smuzhiyun  *        if NAND_BBT_PERCHIP option is set
457*4882a593Smuzhiyun  *
458*4882a593Smuzhiyun  * Create a bad block table by scanning the device for the given good/bad block
459*4882a593Smuzhiyun  * identify pattern.
460*4882a593Smuzhiyun  */
create_bbt(struct nand_chip * this,uint8_t * buf,struct nand_bbt_descr * bd,int chip)461*4882a593Smuzhiyun static int create_bbt(struct nand_chip *this, uint8_t *buf,
462*4882a593Smuzhiyun 		      struct nand_bbt_descr *bd, int chip)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun 	u64 targetsize = nanddev_target_size(&this->base);
465*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(this);
466*4882a593Smuzhiyun 	int i, numblocks, startblock;
467*4882a593Smuzhiyun 	loff_t from;
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	pr_info("Scanning device for bad blocks\n");
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	if (chip == -1) {
472*4882a593Smuzhiyun 		numblocks = mtd->size >> this->bbt_erase_shift;
473*4882a593Smuzhiyun 		startblock = 0;
474*4882a593Smuzhiyun 		from = 0;
475*4882a593Smuzhiyun 	} else {
476*4882a593Smuzhiyun 		if (chip >= nanddev_ntargets(&this->base)) {
477*4882a593Smuzhiyun 			pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
478*4882a593Smuzhiyun 			        chip + 1, nanddev_ntargets(&this->base));
479*4882a593Smuzhiyun 			return -EINVAL;
480*4882a593Smuzhiyun 		}
481*4882a593Smuzhiyun 		numblocks = targetsize >> this->bbt_erase_shift;
482*4882a593Smuzhiyun 		startblock = chip * numblocks;
483*4882a593Smuzhiyun 		numblocks += startblock;
484*4882a593Smuzhiyun 		from = (loff_t)startblock << this->bbt_erase_shift;
485*4882a593Smuzhiyun 	}
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	for (i = startblock; i < numblocks; i++) {
488*4882a593Smuzhiyun 		int ret;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 		BUG_ON(bd->options & NAND_BBT_NO_OOB);
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 		ret = scan_block_fast(this, bd, from, buf);
493*4882a593Smuzhiyun 		if (ret < 0)
494*4882a593Smuzhiyun 			return ret;
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 		if (ret) {
497*4882a593Smuzhiyun 			bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
498*4882a593Smuzhiyun 			pr_warn("Bad eraseblock %d at 0x%012llx\n",
499*4882a593Smuzhiyun 				i, (unsigned long long)from);
500*4882a593Smuzhiyun 			mtd->ecc_stats.badblocks++;
501*4882a593Smuzhiyun 		}
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 		from += (1 << this->bbt_erase_shift);
504*4882a593Smuzhiyun 	}
505*4882a593Smuzhiyun 	return 0;
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun /**
509*4882a593Smuzhiyun  * search_bbt - [GENERIC] scan the device for a specific bad block table
510*4882a593Smuzhiyun  * @this: NAND chip object
511*4882a593Smuzhiyun  * @buf: temporary buffer
512*4882a593Smuzhiyun  * @td: descriptor for the bad block table
513*4882a593Smuzhiyun  *
514*4882a593Smuzhiyun  * Read the bad block table by searching for a given ident pattern. Search is
515*4882a593Smuzhiyun  * preformed either from the beginning up or from the end of the device
516*4882a593Smuzhiyun  * downwards. The search starts always at the start of a block. If the option
517*4882a593Smuzhiyun  * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
518*4882a593Smuzhiyun  * the bad block information of this chip. This is necessary to provide support
519*4882a593Smuzhiyun  * for certain DOC devices.
520*4882a593Smuzhiyun  *
521*4882a593Smuzhiyun  * The bbt ident pattern resides in the oob area of the first page in a block.
522*4882a593Smuzhiyun  */
search_bbt(struct nand_chip * this,uint8_t * buf,struct nand_bbt_descr * td)523*4882a593Smuzhiyun static int search_bbt(struct nand_chip *this, uint8_t *buf,
524*4882a593Smuzhiyun 		      struct nand_bbt_descr *td)
525*4882a593Smuzhiyun {
526*4882a593Smuzhiyun 	u64 targetsize = nanddev_target_size(&this->base);
527*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(this);
528*4882a593Smuzhiyun 	int i, chips;
529*4882a593Smuzhiyun 	int startblock, block, dir;
530*4882a593Smuzhiyun 	int scanlen = mtd->writesize + mtd->oobsize;
531*4882a593Smuzhiyun 	int bbtblocks;
532*4882a593Smuzhiyun 	int blocktopage = this->bbt_erase_shift - this->page_shift;
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	/* Search direction top -> down? */
535*4882a593Smuzhiyun 	if (td->options & NAND_BBT_LASTBLOCK) {
536*4882a593Smuzhiyun 		startblock = (mtd->size >> this->bbt_erase_shift) - 1;
537*4882a593Smuzhiyun 		dir = -1;
538*4882a593Smuzhiyun 	} else {
539*4882a593Smuzhiyun 		startblock = 0;
540*4882a593Smuzhiyun 		dir = 1;
541*4882a593Smuzhiyun 	}
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	/* Do we have a bbt per chip? */
544*4882a593Smuzhiyun 	if (td->options & NAND_BBT_PERCHIP) {
545*4882a593Smuzhiyun 		chips = nanddev_ntargets(&this->base);
546*4882a593Smuzhiyun 		bbtblocks = targetsize >> this->bbt_erase_shift;
547*4882a593Smuzhiyun 		startblock &= bbtblocks - 1;
548*4882a593Smuzhiyun 	} else {
549*4882a593Smuzhiyun 		chips = 1;
550*4882a593Smuzhiyun 		bbtblocks = mtd->size >> this->bbt_erase_shift;
551*4882a593Smuzhiyun 	}
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	for (i = 0; i < chips; i++) {
554*4882a593Smuzhiyun 		/* Reset version information */
555*4882a593Smuzhiyun 		td->version[i] = 0;
556*4882a593Smuzhiyun 		td->pages[i] = -1;
557*4882a593Smuzhiyun 		/* Scan the maximum number of blocks */
558*4882a593Smuzhiyun 		for (block = 0; block < td->maxblocks; block++) {
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 			int actblock = startblock + dir * block;
561*4882a593Smuzhiyun 			loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 			/* Read first page */
564*4882a593Smuzhiyun 			scan_read(this, buf, offs, mtd->writesize, td);
565*4882a593Smuzhiyun 			if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
566*4882a593Smuzhiyun 				td->pages[i] = actblock << blocktopage;
567*4882a593Smuzhiyun 				if (td->options & NAND_BBT_VERSION) {
568*4882a593Smuzhiyun 					offs = bbt_get_ver_offs(this, td);
569*4882a593Smuzhiyun 					td->version[i] = buf[offs];
570*4882a593Smuzhiyun 				}
571*4882a593Smuzhiyun 				break;
572*4882a593Smuzhiyun 			}
573*4882a593Smuzhiyun 		}
574*4882a593Smuzhiyun 		startblock += targetsize >> this->bbt_erase_shift;
575*4882a593Smuzhiyun 	}
576*4882a593Smuzhiyun 	/* Check, if we found a bbt for each requested chip */
577*4882a593Smuzhiyun 	for (i = 0; i < chips; i++) {
578*4882a593Smuzhiyun 		if (td->pages[i] == -1)
579*4882a593Smuzhiyun 			pr_warn("Bad block table not found for chip %d\n", i);
580*4882a593Smuzhiyun 		else
581*4882a593Smuzhiyun 			pr_info("Bad block table found at page %d, version 0x%02X\n",
582*4882a593Smuzhiyun 				td->pages[i], td->version[i]);
583*4882a593Smuzhiyun 	}
584*4882a593Smuzhiyun 	return 0;
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun /**
588*4882a593Smuzhiyun  * search_read_bbts - [GENERIC] scan the device for bad block table(s)
589*4882a593Smuzhiyun  * @this: NAND chip object
590*4882a593Smuzhiyun  * @buf: temporary buffer
591*4882a593Smuzhiyun  * @td: descriptor for the bad block table
592*4882a593Smuzhiyun  * @md: descriptor for the bad block table mirror
593*4882a593Smuzhiyun  *
594*4882a593Smuzhiyun  * Search and read the bad block table(s).
595*4882a593Smuzhiyun  */
search_read_bbts(struct nand_chip * this,uint8_t * buf,struct nand_bbt_descr * td,struct nand_bbt_descr * md)596*4882a593Smuzhiyun static void search_read_bbts(struct nand_chip *this, uint8_t *buf,
597*4882a593Smuzhiyun 			     struct nand_bbt_descr *td,
598*4882a593Smuzhiyun 			     struct nand_bbt_descr *md)
599*4882a593Smuzhiyun {
600*4882a593Smuzhiyun 	/* Search the primary table */
601*4882a593Smuzhiyun 	search_bbt(this, buf, td);
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	/* Search the mirror table */
604*4882a593Smuzhiyun 	if (md)
605*4882a593Smuzhiyun 		search_bbt(this, buf, md);
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun /**
609*4882a593Smuzhiyun  * get_bbt_block - Get the first valid eraseblock suitable to store a BBT
610*4882a593Smuzhiyun  * @this: the NAND device
611*4882a593Smuzhiyun  * @td: the BBT description
612*4882a593Smuzhiyun  * @md: the mirror BBT descriptor
613*4882a593Smuzhiyun  * @chip: the CHIP selector
614*4882a593Smuzhiyun  *
615*4882a593Smuzhiyun  * This functions returns a positive block number pointing a valid eraseblock
616*4882a593Smuzhiyun  * suitable to store a BBT (i.e. in the range reserved for BBT), or -ENOSPC if
617*4882a593Smuzhiyun  * all blocks are already used of marked bad. If td->pages[chip] was already
618*4882a593Smuzhiyun  * pointing to a valid block we re-use it, otherwise we search for the next
619*4882a593Smuzhiyun  * valid one.
620*4882a593Smuzhiyun  */
get_bbt_block(struct nand_chip * this,struct nand_bbt_descr * td,struct nand_bbt_descr * md,int chip)621*4882a593Smuzhiyun static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td,
622*4882a593Smuzhiyun 			 struct nand_bbt_descr *md, int chip)
623*4882a593Smuzhiyun {
624*4882a593Smuzhiyun 	u64 targetsize = nanddev_target_size(&this->base);
625*4882a593Smuzhiyun 	int startblock, dir, page, numblocks, i;
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	/*
628*4882a593Smuzhiyun 	 * There was already a version of the table, reuse the page. This
629*4882a593Smuzhiyun 	 * applies for absolute placement too, as we have the page number in
630*4882a593Smuzhiyun 	 * td->pages.
631*4882a593Smuzhiyun 	 */
632*4882a593Smuzhiyun 	if (td->pages[chip] != -1)
633*4882a593Smuzhiyun 		return td->pages[chip] >>
634*4882a593Smuzhiyun 				(this->bbt_erase_shift - this->page_shift);
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	numblocks = (int)(targetsize >> this->bbt_erase_shift);
637*4882a593Smuzhiyun 	if (!(td->options & NAND_BBT_PERCHIP))
638*4882a593Smuzhiyun 		numblocks *= nanddev_ntargets(&this->base);
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 	/*
641*4882a593Smuzhiyun 	 * Automatic placement of the bad block table. Search direction
642*4882a593Smuzhiyun 	 * top -> down?
643*4882a593Smuzhiyun 	 */
644*4882a593Smuzhiyun 	if (td->options & NAND_BBT_LASTBLOCK) {
645*4882a593Smuzhiyun 		startblock = numblocks * (chip + 1) - 1;
646*4882a593Smuzhiyun 		dir = -1;
647*4882a593Smuzhiyun 	} else {
648*4882a593Smuzhiyun 		startblock = chip * numblocks;
649*4882a593Smuzhiyun 		dir = 1;
650*4882a593Smuzhiyun 	}
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 	for (i = 0; i < td->maxblocks; i++) {
653*4882a593Smuzhiyun 		int block = startblock + dir * i;
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 		/* Check, if the block is bad */
656*4882a593Smuzhiyun 		switch (bbt_get_entry(this, block)) {
657*4882a593Smuzhiyun 		case BBT_BLOCK_WORN:
658*4882a593Smuzhiyun 		case BBT_BLOCK_FACTORY_BAD:
659*4882a593Smuzhiyun 			continue;
660*4882a593Smuzhiyun 		}
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 		page = block << (this->bbt_erase_shift - this->page_shift);
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 		/* Check, if the block is used by the mirror table */
665*4882a593Smuzhiyun 		if (!md || md->pages[chip] != page)
666*4882a593Smuzhiyun 			return block;
667*4882a593Smuzhiyun 	}
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	return -ENOSPC;
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun /**
673*4882a593Smuzhiyun  * mark_bbt_block_bad - Mark one of the block reserved for BBT bad
674*4882a593Smuzhiyun  * @this: the NAND device
675*4882a593Smuzhiyun  * @td: the BBT description
676*4882a593Smuzhiyun  * @chip: the CHIP selector
677*4882a593Smuzhiyun  * @block: the BBT block to mark
678*4882a593Smuzhiyun  *
679*4882a593Smuzhiyun  * Blocks reserved for BBT can become bad. This functions is an helper to mark
680*4882a593Smuzhiyun  * such blocks as bad. It takes care of updating the in-memory BBT, marking the
681*4882a593Smuzhiyun  * block as bad using a bad block marker and invalidating the associated
682*4882a593Smuzhiyun  * td->pages[] entry.
683*4882a593Smuzhiyun  */
mark_bbt_block_bad(struct nand_chip * this,struct nand_bbt_descr * td,int chip,int block)684*4882a593Smuzhiyun static void mark_bbt_block_bad(struct nand_chip *this,
685*4882a593Smuzhiyun 			       struct nand_bbt_descr *td,
686*4882a593Smuzhiyun 			       int chip, int block)
687*4882a593Smuzhiyun {
688*4882a593Smuzhiyun 	loff_t to;
689*4882a593Smuzhiyun 	int res;
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	bbt_mark_entry(this, block, BBT_BLOCK_WORN);
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	to = (loff_t)block << this->bbt_erase_shift;
694*4882a593Smuzhiyun 	res = nand_markbad_bbm(this, to);
695*4882a593Smuzhiyun 	if (res)
696*4882a593Smuzhiyun 		pr_warn("nand_bbt: error %d while marking block %d bad\n",
697*4882a593Smuzhiyun 			res, block);
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	td->pages[chip] = -1;
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun /**
703*4882a593Smuzhiyun  * write_bbt - [GENERIC] (Re)write the bad block table
704*4882a593Smuzhiyun  * @this: NAND chip object
705*4882a593Smuzhiyun  * @buf: temporary buffer
706*4882a593Smuzhiyun  * @td: descriptor for the bad block table
707*4882a593Smuzhiyun  * @md: descriptor for the bad block table mirror
708*4882a593Smuzhiyun  * @chipsel: selector for a specific chip, -1 for all
709*4882a593Smuzhiyun  *
710*4882a593Smuzhiyun  * (Re)write the bad block table.
711*4882a593Smuzhiyun  */
write_bbt(struct nand_chip * this,uint8_t * buf,struct nand_bbt_descr * td,struct nand_bbt_descr * md,int chipsel)712*4882a593Smuzhiyun static int write_bbt(struct nand_chip *this, uint8_t *buf,
713*4882a593Smuzhiyun 		     struct nand_bbt_descr *td, struct nand_bbt_descr *md,
714*4882a593Smuzhiyun 		     int chipsel)
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun 	u64 targetsize = nanddev_target_size(&this->base);
717*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(this);
718*4882a593Smuzhiyun 	struct erase_info einfo;
719*4882a593Smuzhiyun 	int i, res, chip = 0;
720*4882a593Smuzhiyun 	int bits, page, offs, numblocks, sft, sftmsk;
721*4882a593Smuzhiyun 	int nrchips, pageoffs, ooboffs;
722*4882a593Smuzhiyun 	uint8_t msk[4];
723*4882a593Smuzhiyun 	uint8_t rcode = td->reserved_block_code;
724*4882a593Smuzhiyun 	size_t retlen, len = 0;
725*4882a593Smuzhiyun 	loff_t to;
726*4882a593Smuzhiyun 	struct mtd_oob_ops ops;
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	ops.ooblen = mtd->oobsize;
729*4882a593Smuzhiyun 	ops.ooboffs = 0;
730*4882a593Smuzhiyun 	ops.datbuf = NULL;
731*4882a593Smuzhiyun 	ops.mode = MTD_OPS_PLACE_OOB;
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	if (!rcode)
734*4882a593Smuzhiyun 		rcode = 0xff;
735*4882a593Smuzhiyun 	/* Write bad block table per chip rather than per device? */
736*4882a593Smuzhiyun 	if (td->options & NAND_BBT_PERCHIP) {
737*4882a593Smuzhiyun 		numblocks = (int)(targetsize >> this->bbt_erase_shift);
738*4882a593Smuzhiyun 		/* Full device write or specific chip? */
739*4882a593Smuzhiyun 		if (chipsel == -1) {
740*4882a593Smuzhiyun 			nrchips = nanddev_ntargets(&this->base);
741*4882a593Smuzhiyun 		} else {
742*4882a593Smuzhiyun 			nrchips = chipsel + 1;
743*4882a593Smuzhiyun 			chip = chipsel;
744*4882a593Smuzhiyun 		}
745*4882a593Smuzhiyun 	} else {
746*4882a593Smuzhiyun 		numblocks = (int)(mtd->size >> this->bbt_erase_shift);
747*4882a593Smuzhiyun 		nrchips = 1;
748*4882a593Smuzhiyun 	}
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	/* Loop through the chips */
751*4882a593Smuzhiyun 	while (chip < nrchips) {
752*4882a593Smuzhiyun 		int block;
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 		block = get_bbt_block(this, td, md, chip);
755*4882a593Smuzhiyun 		if (block < 0) {
756*4882a593Smuzhiyun 			pr_err("No space left to write bad block table\n");
757*4882a593Smuzhiyun 			res = block;
758*4882a593Smuzhiyun 			goto outerr;
759*4882a593Smuzhiyun 		}
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 		/*
762*4882a593Smuzhiyun 		 * get_bbt_block() returns a block number, shift the value to
763*4882a593Smuzhiyun 		 * get a page number.
764*4882a593Smuzhiyun 		 */
765*4882a593Smuzhiyun 		page = block << (this->bbt_erase_shift - this->page_shift);
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 		/* Set up shift count and masks for the flash table */
768*4882a593Smuzhiyun 		bits = td->options & NAND_BBT_NRBITS_MSK;
769*4882a593Smuzhiyun 		msk[2] = ~rcode;
770*4882a593Smuzhiyun 		switch (bits) {
771*4882a593Smuzhiyun 		case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
772*4882a593Smuzhiyun 			msk[3] = 0x01;
773*4882a593Smuzhiyun 			break;
774*4882a593Smuzhiyun 		case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
775*4882a593Smuzhiyun 			msk[3] = 0x03;
776*4882a593Smuzhiyun 			break;
777*4882a593Smuzhiyun 		case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
778*4882a593Smuzhiyun 			msk[3] = 0x0f;
779*4882a593Smuzhiyun 			break;
780*4882a593Smuzhiyun 		case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
781*4882a593Smuzhiyun 			msk[3] = 0xff;
782*4882a593Smuzhiyun 			break;
783*4882a593Smuzhiyun 		default: return -EINVAL;
784*4882a593Smuzhiyun 		}
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 		to = ((loff_t)page) << this->page_shift;
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 		/* Must we save the block contents? */
789*4882a593Smuzhiyun 		if (td->options & NAND_BBT_SAVECONTENT) {
790*4882a593Smuzhiyun 			/* Make it block aligned */
791*4882a593Smuzhiyun 			to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
792*4882a593Smuzhiyun 			len = 1 << this->bbt_erase_shift;
793*4882a593Smuzhiyun 			res = mtd_read(mtd, to, len, &retlen, buf);
794*4882a593Smuzhiyun 			if (res < 0) {
795*4882a593Smuzhiyun 				if (retlen != len) {
796*4882a593Smuzhiyun 					pr_info("nand_bbt: error reading block for writing the bad block table\n");
797*4882a593Smuzhiyun 					return res;
798*4882a593Smuzhiyun 				}
799*4882a593Smuzhiyun 				pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
800*4882a593Smuzhiyun 			}
801*4882a593Smuzhiyun 			/* Read oob data */
802*4882a593Smuzhiyun 			ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
803*4882a593Smuzhiyun 			ops.oobbuf = &buf[len];
804*4882a593Smuzhiyun 			res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
805*4882a593Smuzhiyun 			if (res < 0 || ops.oobretlen != ops.ooblen)
806*4882a593Smuzhiyun 				goto outerr;
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 			/* Calc the byte offset in the buffer */
809*4882a593Smuzhiyun 			pageoffs = page - (int)(to >> this->page_shift);
810*4882a593Smuzhiyun 			offs = pageoffs << this->page_shift;
811*4882a593Smuzhiyun 			/* Preset the bbt area with 0xff */
812*4882a593Smuzhiyun 			memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
813*4882a593Smuzhiyun 			ooboffs = len + (pageoffs * mtd->oobsize);
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun 		} else if (td->options & NAND_BBT_NO_OOB) {
816*4882a593Smuzhiyun 			ooboffs = 0;
817*4882a593Smuzhiyun 			offs = td->len;
818*4882a593Smuzhiyun 			/* The version byte */
819*4882a593Smuzhiyun 			if (td->options & NAND_BBT_VERSION)
820*4882a593Smuzhiyun 				offs++;
821*4882a593Smuzhiyun 			/* Calc length */
822*4882a593Smuzhiyun 			len = (size_t)(numblocks >> sft);
823*4882a593Smuzhiyun 			len += offs;
824*4882a593Smuzhiyun 			/* Make it page aligned! */
825*4882a593Smuzhiyun 			len = ALIGN(len, mtd->writesize);
826*4882a593Smuzhiyun 			/* Preset the buffer with 0xff */
827*4882a593Smuzhiyun 			memset(buf, 0xff, len);
828*4882a593Smuzhiyun 			/* Pattern is located at the begin of first page */
829*4882a593Smuzhiyun 			memcpy(buf, td->pattern, td->len);
830*4882a593Smuzhiyun 		} else {
831*4882a593Smuzhiyun 			/* Calc length */
832*4882a593Smuzhiyun 			len = (size_t)(numblocks >> sft);
833*4882a593Smuzhiyun 			/* Make it page aligned! */
834*4882a593Smuzhiyun 			len = ALIGN(len, mtd->writesize);
835*4882a593Smuzhiyun 			/* Preset the buffer with 0xff */
836*4882a593Smuzhiyun 			memset(buf, 0xff, len +
837*4882a593Smuzhiyun 			       (len >> this->page_shift)* mtd->oobsize);
838*4882a593Smuzhiyun 			offs = 0;
839*4882a593Smuzhiyun 			ooboffs = len;
840*4882a593Smuzhiyun 			/* Pattern is located in oob area of first page */
841*4882a593Smuzhiyun 			memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
842*4882a593Smuzhiyun 		}
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 		if (td->options & NAND_BBT_VERSION)
845*4882a593Smuzhiyun 			buf[ooboffs + td->veroffs] = td->version[chip];
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 		/* Walk through the memory table */
848*4882a593Smuzhiyun 		for (i = 0; i < numblocks; i++) {
849*4882a593Smuzhiyun 			uint8_t dat;
850*4882a593Smuzhiyun 			int sftcnt = (i << (3 - sft)) & sftmsk;
851*4882a593Smuzhiyun 			dat = bbt_get_entry(this, chip * numblocks + i);
852*4882a593Smuzhiyun 			/* Do not store the reserved bbt blocks! */
853*4882a593Smuzhiyun 			buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
854*4882a593Smuzhiyun 		}
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 		memset(&einfo, 0, sizeof(einfo));
857*4882a593Smuzhiyun 		einfo.addr = to;
858*4882a593Smuzhiyun 		einfo.len = 1 << this->bbt_erase_shift;
859*4882a593Smuzhiyun 		res = nand_erase_nand(this, &einfo, 1);
860*4882a593Smuzhiyun 		if (res < 0) {
861*4882a593Smuzhiyun 			pr_warn("nand_bbt: error while erasing BBT block %d\n",
862*4882a593Smuzhiyun 				res);
863*4882a593Smuzhiyun 			mark_bbt_block_bad(this, td, chip, block);
864*4882a593Smuzhiyun 			continue;
865*4882a593Smuzhiyun 		}
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun 		res = scan_write_bbt(this, to, len, buf,
868*4882a593Smuzhiyun 				     td->options & NAND_BBT_NO_OOB ?
869*4882a593Smuzhiyun 				     NULL : &buf[len]);
870*4882a593Smuzhiyun 		if (res < 0) {
871*4882a593Smuzhiyun 			pr_warn("nand_bbt: error while writing BBT block %d\n",
872*4882a593Smuzhiyun 				res);
873*4882a593Smuzhiyun 			mark_bbt_block_bad(this, td, chip, block);
874*4882a593Smuzhiyun 			continue;
875*4882a593Smuzhiyun 		}
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun 		pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
878*4882a593Smuzhiyun 			 (unsigned long long)to, td->version[chip]);
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 		/* Mark it as used */
881*4882a593Smuzhiyun 		td->pages[chip++] = page;
882*4882a593Smuzhiyun 	}
883*4882a593Smuzhiyun 	return 0;
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun  outerr:
886*4882a593Smuzhiyun 	pr_warn("nand_bbt: error while writing bad block table %d\n", res);
887*4882a593Smuzhiyun 	return res;
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun /**
891*4882a593Smuzhiyun  * nand_memory_bbt - [GENERIC] create a memory based bad block table
892*4882a593Smuzhiyun  * @this: NAND chip object
893*4882a593Smuzhiyun  * @bd: descriptor for the good/bad block search pattern
894*4882a593Smuzhiyun  *
895*4882a593Smuzhiyun  * The function creates a memory based bbt by scanning the device for
896*4882a593Smuzhiyun  * manufacturer / software marked good / bad blocks.
897*4882a593Smuzhiyun  */
nand_memory_bbt(struct nand_chip * this,struct nand_bbt_descr * bd)898*4882a593Smuzhiyun static inline int nand_memory_bbt(struct nand_chip *this,
899*4882a593Smuzhiyun 				  struct nand_bbt_descr *bd)
900*4882a593Smuzhiyun {
901*4882a593Smuzhiyun 	u8 *pagebuf = nand_get_data_buf(this);
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun 	return create_bbt(this, pagebuf, bd, -1);
904*4882a593Smuzhiyun }
905*4882a593Smuzhiyun 
906*4882a593Smuzhiyun /**
907*4882a593Smuzhiyun  * check_create - [GENERIC] create and write bbt(s) if necessary
908*4882a593Smuzhiyun  * @this: the NAND device
909*4882a593Smuzhiyun  * @buf: temporary buffer
910*4882a593Smuzhiyun  * @bd: descriptor for the good/bad block search pattern
911*4882a593Smuzhiyun  *
912*4882a593Smuzhiyun  * The function checks the results of the previous call to read_bbt and creates
913*4882a593Smuzhiyun  * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
914*4882a593Smuzhiyun  * for the chip/device. Update is necessary if one of the tables is missing or
915*4882a593Smuzhiyun  * the version nr. of one table is less than the other.
916*4882a593Smuzhiyun  */
check_create(struct nand_chip * this,uint8_t * buf,struct nand_bbt_descr * bd)917*4882a593Smuzhiyun static int check_create(struct nand_chip *this, uint8_t *buf,
918*4882a593Smuzhiyun 			struct nand_bbt_descr *bd)
919*4882a593Smuzhiyun {
920*4882a593Smuzhiyun 	int i, chips, writeops, create, chipsel, res, res2;
921*4882a593Smuzhiyun 	struct nand_bbt_descr *td = this->bbt_td;
922*4882a593Smuzhiyun 	struct nand_bbt_descr *md = this->bbt_md;
923*4882a593Smuzhiyun 	struct nand_bbt_descr *rd, *rd2;
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	/* Do we have a bbt per chip? */
926*4882a593Smuzhiyun 	if (td->options & NAND_BBT_PERCHIP)
927*4882a593Smuzhiyun 		chips = nanddev_ntargets(&this->base);
928*4882a593Smuzhiyun 	else
929*4882a593Smuzhiyun 		chips = 1;
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 	for (i = 0; i < chips; i++) {
932*4882a593Smuzhiyun 		writeops = 0;
933*4882a593Smuzhiyun 		create = 0;
934*4882a593Smuzhiyun 		rd = NULL;
935*4882a593Smuzhiyun 		rd2 = NULL;
936*4882a593Smuzhiyun 		res = res2 = 0;
937*4882a593Smuzhiyun 		/* Per chip or per device? */
938*4882a593Smuzhiyun 		chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
939*4882a593Smuzhiyun 		/* Mirrored table available? */
940*4882a593Smuzhiyun 		if (md) {
941*4882a593Smuzhiyun 			if (td->pages[i] == -1 && md->pages[i] == -1) {
942*4882a593Smuzhiyun 				create = 1;
943*4882a593Smuzhiyun 				writeops = 0x03;
944*4882a593Smuzhiyun 			} else if (td->pages[i] == -1) {
945*4882a593Smuzhiyun 				rd = md;
946*4882a593Smuzhiyun 				writeops = 0x01;
947*4882a593Smuzhiyun 			} else if (md->pages[i] == -1) {
948*4882a593Smuzhiyun 				rd = td;
949*4882a593Smuzhiyun 				writeops = 0x02;
950*4882a593Smuzhiyun 			} else if (td->version[i] == md->version[i]) {
951*4882a593Smuzhiyun 				rd = td;
952*4882a593Smuzhiyun 				if (!(td->options & NAND_BBT_VERSION))
953*4882a593Smuzhiyun 					rd2 = md;
954*4882a593Smuzhiyun 			} else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
955*4882a593Smuzhiyun 				rd = td;
956*4882a593Smuzhiyun 				writeops = 0x02;
957*4882a593Smuzhiyun 			} else {
958*4882a593Smuzhiyun 				rd = md;
959*4882a593Smuzhiyun 				writeops = 0x01;
960*4882a593Smuzhiyun 			}
961*4882a593Smuzhiyun 		} else {
962*4882a593Smuzhiyun 			if (td->pages[i] == -1) {
963*4882a593Smuzhiyun 				create = 1;
964*4882a593Smuzhiyun 				writeops = 0x01;
965*4882a593Smuzhiyun 			} else {
966*4882a593Smuzhiyun 				rd = td;
967*4882a593Smuzhiyun 			}
968*4882a593Smuzhiyun 		}
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 		if (create) {
971*4882a593Smuzhiyun 			/* Create the bad block table by scanning the device? */
972*4882a593Smuzhiyun 			if (!(td->options & NAND_BBT_CREATE))
973*4882a593Smuzhiyun 				continue;
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun 			/* Create the table in memory by scanning the chip(s) */
976*4882a593Smuzhiyun 			if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
977*4882a593Smuzhiyun 				create_bbt(this, buf, bd, chipsel);
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 			td->version[i] = 1;
980*4882a593Smuzhiyun 			if (md)
981*4882a593Smuzhiyun 				md->version[i] = 1;
982*4882a593Smuzhiyun 		}
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 		/* Read back first? */
985*4882a593Smuzhiyun 		if (rd) {
986*4882a593Smuzhiyun 			res = read_abs_bbt(this, buf, rd, chipsel);
987*4882a593Smuzhiyun 			if (mtd_is_eccerr(res)) {
988*4882a593Smuzhiyun 				/* Mark table as invalid */
989*4882a593Smuzhiyun 				rd->pages[i] = -1;
990*4882a593Smuzhiyun 				rd->version[i] = 0;
991*4882a593Smuzhiyun 				i--;
992*4882a593Smuzhiyun 				continue;
993*4882a593Smuzhiyun 			}
994*4882a593Smuzhiyun 		}
995*4882a593Smuzhiyun 		/* If they weren't versioned, read both */
996*4882a593Smuzhiyun 		if (rd2) {
997*4882a593Smuzhiyun 			res2 = read_abs_bbt(this, buf, rd2, chipsel);
998*4882a593Smuzhiyun 			if (mtd_is_eccerr(res2)) {
999*4882a593Smuzhiyun 				/* Mark table as invalid */
1000*4882a593Smuzhiyun 				rd2->pages[i] = -1;
1001*4882a593Smuzhiyun 				rd2->version[i] = 0;
1002*4882a593Smuzhiyun 				i--;
1003*4882a593Smuzhiyun 				continue;
1004*4882a593Smuzhiyun 			}
1005*4882a593Smuzhiyun 		}
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 		/* Scrub the flash table(s)? */
1008*4882a593Smuzhiyun 		if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
1009*4882a593Smuzhiyun 			writeops = 0x03;
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 		/* Update version numbers before writing */
1012*4882a593Smuzhiyun 		if (md) {
1013*4882a593Smuzhiyun 			td->version[i] = max(td->version[i], md->version[i]);
1014*4882a593Smuzhiyun 			md->version[i] = td->version[i];
1015*4882a593Smuzhiyun 		}
1016*4882a593Smuzhiyun 
1017*4882a593Smuzhiyun 		/* Write the bad block table to the device? */
1018*4882a593Smuzhiyun 		if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1019*4882a593Smuzhiyun 			res = write_bbt(this, buf, td, md, chipsel);
1020*4882a593Smuzhiyun 			if (res < 0)
1021*4882a593Smuzhiyun 				return res;
1022*4882a593Smuzhiyun 		}
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 		/* Write the mirror bad block table to the device? */
1025*4882a593Smuzhiyun 		if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1026*4882a593Smuzhiyun 			res = write_bbt(this, buf, md, td, chipsel);
1027*4882a593Smuzhiyun 			if (res < 0)
1028*4882a593Smuzhiyun 				return res;
1029*4882a593Smuzhiyun 		}
1030*4882a593Smuzhiyun 	}
1031*4882a593Smuzhiyun 	return 0;
1032*4882a593Smuzhiyun }
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun /**
1035*4882a593Smuzhiyun  * nand_update_bbt - update bad block table(s)
1036*4882a593Smuzhiyun  * @this: the NAND device
1037*4882a593Smuzhiyun  * @offs: the offset of the newly marked block
1038*4882a593Smuzhiyun  *
1039*4882a593Smuzhiyun  * The function updates the bad block table(s).
1040*4882a593Smuzhiyun  */
nand_update_bbt(struct nand_chip * this,loff_t offs)1041*4882a593Smuzhiyun static int nand_update_bbt(struct nand_chip *this, loff_t offs)
1042*4882a593Smuzhiyun {
1043*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(this);
1044*4882a593Smuzhiyun 	int len, res = 0;
1045*4882a593Smuzhiyun 	int chip, chipsel;
1046*4882a593Smuzhiyun 	uint8_t *buf;
1047*4882a593Smuzhiyun 	struct nand_bbt_descr *td = this->bbt_td;
1048*4882a593Smuzhiyun 	struct nand_bbt_descr *md = this->bbt_md;
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	if (!this->bbt || !td)
1051*4882a593Smuzhiyun 		return -EINVAL;
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 	/* Allocate a temporary buffer for one eraseblock incl. oob */
1054*4882a593Smuzhiyun 	len = (1 << this->bbt_erase_shift);
1055*4882a593Smuzhiyun 	len += (len >> this->page_shift) * mtd->oobsize;
1056*4882a593Smuzhiyun 	buf = kmalloc(len, GFP_KERNEL);
1057*4882a593Smuzhiyun 	if (!buf)
1058*4882a593Smuzhiyun 		return -ENOMEM;
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 	/* Do we have a bbt per chip? */
1061*4882a593Smuzhiyun 	if (td->options & NAND_BBT_PERCHIP) {
1062*4882a593Smuzhiyun 		chip = (int)(offs >> this->chip_shift);
1063*4882a593Smuzhiyun 		chipsel = chip;
1064*4882a593Smuzhiyun 	} else {
1065*4882a593Smuzhiyun 		chip = 0;
1066*4882a593Smuzhiyun 		chipsel = -1;
1067*4882a593Smuzhiyun 	}
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun 	td->version[chip]++;
1070*4882a593Smuzhiyun 	if (md)
1071*4882a593Smuzhiyun 		md->version[chip]++;
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 	/* Write the bad block table to the device? */
1074*4882a593Smuzhiyun 	if (td->options & NAND_BBT_WRITE) {
1075*4882a593Smuzhiyun 		res = write_bbt(this, buf, td, md, chipsel);
1076*4882a593Smuzhiyun 		if (res < 0)
1077*4882a593Smuzhiyun 			goto out;
1078*4882a593Smuzhiyun 	}
1079*4882a593Smuzhiyun 	/* Write the mirror bad block table to the device? */
1080*4882a593Smuzhiyun 	if (md && (md->options & NAND_BBT_WRITE)) {
1081*4882a593Smuzhiyun 		res = write_bbt(this, buf, md, td, chipsel);
1082*4882a593Smuzhiyun 	}
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun  out:
1085*4882a593Smuzhiyun 	kfree(buf);
1086*4882a593Smuzhiyun 	return res;
1087*4882a593Smuzhiyun }
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun /**
1090*4882a593Smuzhiyun  * mark_bbt_regions - [GENERIC] mark the bad block table regions
1091*4882a593Smuzhiyun  * @this: the NAND device
1092*4882a593Smuzhiyun  * @td: bad block table descriptor
1093*4882a593Smuzhiyun  *
1094*4882a593Smuzhiyun  * The bad block table regions are marked as "bad" to prevent accidental
1095*4882a593Smuzhiyun  * erasures / writes. The regions are identified by the mark 0x02.
1096*4882a593Smuzhiyun  */
mark_bbt_region(struct nand_chip * this,struct nand_bbt_descr * td)1097*4882a593Smuzhiyun static void mark_bbt_region(struct nand_chip *this, struct nand_bbt_descr *td)
1098*4882a593Smuzhiyun {
1099*4882a593Smuzhiyun 	u64 targetsize = nanddev_target_size(&this->base);
1100*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(this);
1101*4882a593Smuzhiyun 	int i, j, chips, block, nrblocks, update;
1102*4882a593Smuzhiyun 	uint8_t oldval;
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 	/* Do we have a bbt per chip? */
1105*4882a593Smuzhiyun 	if (td->options & NAND_BBT_PERCHIP) {
1106*4882a593Smuzhiyun 		chips = nanddev_ntargets(&this->base);
1107*4882a593Smuzhiyun 		nrblocks = (int)(targetsize >> this->bbt_erase_shift);
1108*4882a593Smuzhiyun 	} else {
1109*4882a593Smuzhiyun 		chips = 1;
1110*4882a593Smuzhiyun 		nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
1111*4882a593Smuzhiyun 	}
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun 	for (i = 0; i < chips; i++) {
1114*4882a593Smuzhiyun 		if ((td->options & NAND_BBT_ABSPAGE) ||
1115*4882a593Smuzhiyun 		    !(td->options & NAND_BBT_WRITE)) {
1116*4882a593Smuzhiyun 			if (td->pages[i] == -1)
1117*4882a593Smuzhiyun 				continue;
1118*4882a593Smuzhiyun 			block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
1119*4882a593Smuzhiyun 			oldval = bbt_get_entry(this, block);
1120*4882a593Smuzhiyun 			bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1121*4882a593Smuzhiyun 			if ((oldval != BBT_BLOCK_RESERVED) &&
1122*4882a593Smuzhiyun 					td->reserved_block_code)
1123*4882a593Smuzhiyun 				nand_update_bbt(this, (loff_t)block <<
1124*4882a593Smuzhiyun 						this->bbt_erase_shift);
1125*4882a593Smuzhiyun 			continue;
1126*4882a593Smuzhiyun 		}
1127*4882a593Smuzhiyun 		update = 0;
1128*4882a593Smuzhiyun 		if (td->options & NAND_BBT_LASTBLOCK)
1129*4882a593Smuzhiyun 			block = ((i + 1) * nrblocks) - td->maxblocks;
1130*4882a593Smuzhiyun 		else
1131*4882a593Smuzhiyun 			block = i * nrblocks;
1132*4882a593Smuzhiyun 		for (j = 0; j < td->maxblocks; j++) {
1133*4882a593Smuzhiyun 			oldval = bbt_get_entry(this, block);
1134*4882a593Smuzhiyun 			bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1135*4882a593Smuzhiyun 			if (oldval != BBT_BLOCK_RESERVED)
1136*4882a593Smuzhiyun 				update = 1;
1137*4882a593Smuzhiyun 			block++;
1138*4882a593Smuzhiyun 		}
1139*4882a593Smuzhiyun 		/*
1140*4882a593Smuzhiyun 		 * If we want reserved blocks to be recorded to flash, and some
1141*4882a593Smuzhiyun 		 * new ones have been marked, then we need to update the stored
1142*4882a593Smuzhiyun 		 * bbts.  This should only happen once.
1143*4882a593Smuzhiyun 		 */
1144*4882a593Smuzhiyun 		if (update && td->reserved_block_code)
1145*4882a593Smuzhiyun 			nand_update_bbt(this, (loff_t)(block - 1) <<
1146*4882a593Smuzhiyun 					this->bbt_erase_shift);
1147*4882a593Smuzhiyun 	}
1148*4882a593Smuzhiyun }
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun /**
1151*4882a593Smuzhiyun  * verify_bbt_descr - verify the bad block description
1152*4882a593Smuzhiyun  * @this: the NAND device
1153*4882a593Smuzhiyun  * @bd: the table to verify
1154*4882a593Smuzhiyun  *
1155*4882a593Smuzhiyun  * This functions performs a few sanity checks on the bad block description
1156*4882a593Smuzhiyun  * table.
1157*4882a593Smuzhiyun  */
verify_bbt_descr(struct nand_chip * this,struct nand_bbt_descr * bd)1158*4882a593Smuzhiyun static void verify_bbt_descr(struct nand_chip *this, struct nand_bbt_descr *bd)
1159*4882a593Smuzhiyun {
1160*4882a593Smuzhiyun 	u64 targetsize = nanddev_target_size(&this->base);
1161*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(this);
1162*4882a593Smuzhiyun 	u32 pattern_len;
1163*4882a593Smuzhiyun 	u32 bits;
1164*4882a593Smuzhiyun 	u32 table_size;
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun 	if (!bd)
1167*4882a593Smuzhiyun 		return;
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 	pattern_len = bd->len;
1170*4882a593Smuzhiyun 	bits = bd->options & NAND_BBT_NRBITS_MSK;
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
1173*4882a593Smuzhiyun 			!(this->bbt_options & NAND_BBT_USE_FLASH));
1174*4882a593Smuzhiyun 	BUG_ON(!bits);
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun 	if (bd->options & NAND_BBT_VERSION)
1177*4882a593Smuzhiyun 		pattern_len++;
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun 	if (bd->options & NAND_BBT_NO_OOB) {
1180*4882a593Smuzhiyun 		BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
1181*4882a593Smuzhiyun 		BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
1182*4882a593Smuzhiyun 		BUG_ON(bd->offs);
1183*4882a593Smuzhiyun 		if (bd->options & NAND_BBT_VERSION)
1184*4882a593Smuzhiyun 			BUG_ON(bd->veroffs != bd->len);
1185*4882a593Smuzhiyun 		BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1186*4882a593Smuzhiyun 	}
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	if (bd->options & NAND_BBT_PERCHIP)
1189*4882a593Smuzhiyun 		table_size = targetsize >> this->bbt_erase_shift;
1190*4882a593Smuzhiyun 	else
1191*4882a593Smuzhiyun 		table_size = mtd->size >> this->bbt_erase_shift;
1192*4882a593Smuzhiyun 	table_size >>= 3;
1193*4882a593Smuzhiyun 	table_size *= bits;
1194*4882a593Smuzhiyun 	if (bd->options & NAND_BBT_NO_OOB)
1195*4882a593Smuzhiyun 		table_size += pattern_len;
1196*4882a593Smuzhiyun 	BUG_ON(table_size > (1 << this->bbt_erase_shift));
1197*4882a593Smuzhiyun }
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun /**
1200*4882a593Smuzhiyun  * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1201*4882a593Smuzhiyun  * @this: the NAND device
1202*4882a593Smuzhiyun  * @bd: descriptor for the good/bad block search pattern
1203*4882a593Smuzhiyun  *
1204*4882a593Smuzhiyun  * The function checks, if a bad block table(s) is/are already available. If
1205*4882a593Smuzhiyun  * not it scans the device for manufacturer marked good / bad blocks and writes
1206*4882a593Smuzhiyun  * the bad block table(s) to the selected place.
1207*4882a593Smuzhiyun  *
1208*4882a593Smuzhiyun  * The bad block table memory is allocated here. It must be freed by calling
1209*4882a593Smuzhiyun  * the nand_free_bbt function.
1210*4882a593Smuzhiyun  */
nand_scan_bbt(struct nand_chip * this,struct nand_bbt_descr * bd)1211*4882a593Smuzhiyun static int nand_scan_bbt(struct nand_chip *this, struct nand_bbt_descr *bd)
1212*4882a593Smuzhiyun {
1213*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(this);
1214*4882a593Smuzhiyun 	int len, res;
1215*4882a593Smuzhiyun 	uint8_t *buf;
1216*4882a593Smuzhiyun 	struct nand_bbt_descr *td = this->bbt_td;
1217*4882a593Smuzhiyun 	struct nand_bbt_descr *md = this->bbt_md;
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 	len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
1220*4882a593Smuzhiyun 	/*
1221*4882a593Smuzhiyun 	 * Allocate memory (2bit per block) and clear the memory bad block
1222*4882a593Smuzhiyun 	 * table.
1223*4882a593Smuzhiyun 	 */
1224*4882a593Smuzhiyun 	this->bbt = kzalloc(len, GFP_KERNEL);
1225*4882a593Smuzhiyun 	if (!this->bbt)
1226*4882a593Smuzhiyun 		return -ENOMEM;
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun 	/*
1229*4882a593Smuzhiyun 	 * If no primary table descriptor is given, scan the device to build a
1230*4882a593Smuzhiyun 	 * memory based bad block table.
1231*4882a593Smuzhiyun 	 */
1232*4882a593Smuzhiyun 	if (!td) {
1233*4882a593Smuzhiyun 		if ((res = nand_memory_bbt(this, bd))) {
1234*4882a593Smuzhiyun 			pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
1235*4882a593Smuzhiyun 			goto err_free_bbt;
1236*4882a593Smuzhiyun 		}
1237*4882a593Smuzhiyun 		return 0;
1238*4882a593Smuzhiyun 	}
1239*4882a593Smuzhiyun 	verify_bbt_descr(this, td);
1240*4882a593Smuzhiyun 	verify_bbt_descr(this, md);
1241*4882a593Smuzhiyun 
1242*4882a593Smuzhiyun 	/* Allocate a temporary buffer for one eraseblock incl. oob */
1243*4882a593Smuzhiyun 	len = (1 << this->bbt_erase_shift);
1244*4882a593Smuzhiyun 	len += (len >> this->page_shift) * mtd->oobsize;
1245*4882a593Smuzhiyun 	buf = vmalloc(len);
1246*4882a593Smuzhiyun 	if (!buf) {
1247*4882a593Smuzhiyun 		res = -ENOMEM;
1248*4882a593Smuzhiyun 		goto err_free_bbt;
1249*4882a593Smuzhiyun 	}
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	/* Is the bbt at a given page? */
1252*4882a593Smuzhiyun 	if (td->options & NAND_BBT_ABSPAGE) {
1253*4882a593Smuzhiyun 		read_abs_bbts(this, buf, td, md);
1254*4882a593Smuzhiyun 	} else {
1255*4882a593Smuzhiyun 		/* Search the bad block table using a pattern in oob */
1256*4882a593Smuzhiyun 		search_read_bbts(this, buf, td, md);
1257*4882a593Smuzhiyun 	}
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun 	res = check_create(this, buf, bd);
1260*4882a593Smuzhiyun 	if (res)
1261*4882a593Smuzhiyun 		goto err_free_buf;
1262*4882a593Smuzhiyun 
1263*4882a593Smuzhiyun 	/* Prevent the bbt regions from erasing / writing */
1264*4882a593Smuzhiyun 	mark_bbt_region(this, td);
1265*4882a593Smuzhiyun 	if (md)
1266*4882a593Smuzhiyun 		mark_bbt_region(this, md);
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 	vfree(buf);
1269*4882a593Smuzhiyun 	return 0;
1270*4882a593Smuzhiyun 
1271*4882a593Smuzhiyun err_free_buf:
1272*4882a593Smuzhiyun 	vfree(buf);
1273*4882a593Smuzhiyun err_free_bbt:
1274*4882a593Smuzhiyun 	kfree(this->bbt);
1275*4882a593Smuzhiyun 	this->bbt = NULL;
1276*4882a593Smuzhiyun 	return res;
1277*4882a593Smuzhiyun }
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun /*
1280*4882a593Smuzhiyun  * Define some generic bad / good block scan pattern which are used
1281*4882a593Smuzhiyun  * while scanning a device for factory marked good / bad blocks.
1282*4882a593Smuzhiyun  */
1283*4882a593Smuzhiyun static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun /* Generic flash bbt descriptors */
1286*4882a593Smuzhiyun static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1287*4882a593Smuzhiyun static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun static struct nand_bbt_descr bbt_main_descr = {
1290*4882a593Smuzhiyun 	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1291*4882a593Smuzhiyun 		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1292*4882a593Smuzhiyun 	.offs =	8,
1293*4882a593Smuzhiyun 	.len = 4,
1294*4882a593Smuzhiyun 	.veroffs = 12,
1295*4882a593Smuzhiyun 	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1296*4882a593Smuzhiyun 	.pattern = bbt_pattern
1297*4882a593Smuzhiyun };
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun static struct nand_bbt_descr bbt_mirror_descr = {
1300*4882a593Smuzhiyun 	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1301*4882a593Smuzhiyun 		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1302*4882a593Smuzhiyun 	.offs =	8,
1303*4882a593Smuzhiyun 	.len = 4,
1304*4882a593Smuzhiyun 	.veroffs = 12,
1305*4882a593Smuzhiyun 	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1306*4882a593Smuzhiyun 	.pattern = mirror_pattern
1307*4882a593Smuzhiyun };
1308*4882a593Smuzhiyun 
1309*4882a593Smuzhiyun static struct nand_bbt_descr bbt_main_no_oob_descr = {
1310*4882a593Smuzhiyun 	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1311*4882a593Smuzhiyun 		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1312*4882a593Smuzhiyun 		| NAND_BBT_NO_OOB,
1313*4882a593Smuzhiyun 	.len = 4,
1314*4882a593Smuzhiyun 	.veroffs = 4,
1315*4882a593Smuzhiyun 	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1316*4882a593Smuzhiyun 	.pattern = bbt_pattern
1317*4882a593Smuzhiyun };
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
1320*4882a593Smuzhiyun 	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1321*4882a593Smuzhiyun 		| NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1322*4882a593Smuzhiyun 		| NAND_BBT_NO_OOB,
1323*4882a593Smuzhiyun 	.len = 4,
1324*4882a593Smuzhiyun 	.veroffs = 4,
1325*4882a593Smuzhiyun 	.maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1326*4882a593Smuzhiyun 	.pattern = mirror_pattern
1327*4882a593Smuzhiyun };
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun #define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
1330*4882a593Smuzhiyun /**
1331*4882a593Smuzhiyun  * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
1332*4882a593Smuzhiyun  * @this: NAND chip to create descriptor for
1333*4882a593Smuzhiyun  *
1334*4882a593Smuzhiyun  * This function allocates and initializes a nand_bbt_descr for BBM detection
1335*4882a593Smuzhiyun  * based on the properties of @this. The new descriptor is stored in
1336*4882a593Smuzhiyun  * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1337*4882a593Smuzhiyun  * passed to this function.
1338*4882a593Smuzhiyun  */
nand_create_badblock_pattern(struct nand_chip * this)1339*4882a593Smuzhiyun static int nand_create_badblock_pattern(struct nand_chip *this)
1340*4882a593Smuzhiyun {
1341*4882a593Smuzhiyun 	struct nand_bbt_descr *bd;
1342*4882a593Smuzhiyun 	if (this->badblock_pattern) {
1343*4882a593Smuzhiyun 		pr_warn("Bad block pattern already allocated; not replacing\n");
1344*4882a593Smuzhiyun 		return -EINVAL;
1345*4882a593Smuzhiyun 	}
1346*4882a593Smuzhiyun 	bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1347*4882a593Smuzhiyun 	if (!bd)
1348*4882a593Smuzhiyun 		return -ENOMEM;
1349*4882a593Smuzhiyun 	bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
1350*4882a593Smuzhiyun 	bd->offs = this->badblockpos;
1351*4882a593Smuzhiyun 	bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1352*4882a593Smuzhiyun 	bd->pattern = scan_ff_pattern;
1353*4882a593Smuzhiyun 	bd->options |= NAND_BBT_DYNAMICSTRUCT;
1354*4882a593Smuzhiyun 	this->badblock_pattern = bd;
1355*4882a593Smuzhiyun 	return 0;
1356*4882a593Smuzhiyun }
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun /**
1359*4882a593Smuzhiyun  * nand_create_bbt - [NAND Interface] Select a default bad block table for the device
1360*4882a593Smuzhiyun  * @this: NAND chip object
1361*4882a593Smuzhiyun  *
1362*4882a593Smuzhiyun  * This function selects the default bad block table support for the device and
1363*4882a593Smuzhiyun  * calls the nand_scan_bbt function.
1364*4882a593Smuzhiyun  */
nand_create_bbt(struct nand_chip * this)1365*4882a593Smuzhiyun int nand_create_bbt(struct nand_chip *this)
1366*4882a593Smuzhiyun {
1367*4882a593Smuzhiyun 	int ret;
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 	/* Is a flash based bad block table requested? */
1370*4882a593Smuzhiyun 	if (this->bbt_options & NAND_BBT_USE_FLASH) {
1371*4882a593Smuzhiyun 		/* Use the default pattern descriptors */
1372*4882a593Smuzhiyun 		if (!this->bbt_td) {
1373*4882a593Smuzhiyun 			if (this->bbt_options & NAND_BBT_NO_OOB) {
1374*4882a593Smuzhiyun 				this->bbt_td = &bbt_main_no_oob_descr;
1375*4882a593Smuzhiyun 				this->bbt_md = &bbt_mirror_no_oob_descr;
1376*4882a593Smuzhiyun 			} else {
1377*4882a593Smuzhiyun 				this->bbt_td = &bbt_main_descr;
1378*4882a593Smuzhiyun 				this->bbt_md = &bbt_mirror_descr;
1379*4882a593Smuzhiyun 			}
1380*4882a593Smuzhiyun 		}
1381*4882a593Smuzhiyun 	} else {
1382*4882a593Smuzhiyun 		this->bbt_td = NULL;
1383*4882a593Smuzhiyun 		this->bbt_md = NULL;
1384*4882a593Smuzhiyun 	}
1385*4882a593Smuzhiyun 
1386*4882a593Smuzhiyun 	if (!this->badblock_pattern) {
1387*4882a593Smuzhiyun 		ret = nand_create_badblock_pattern(this);
1388*4882a593Smuzhiyun 		if (ret)
1389*4882a593Smuzhiyun 			return ret;
1390*4882a593Smuzhiyun 	}
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun 	return nand_scan_bbt(this, this->badblock_pattern);
1393*4882a593Smuzhiyun }
1394*4882a593Smuzhiyun EXPORT_SYMBOL(nand_create_bbt);
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun /**
1397*4882a593Smuzhiyun  * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
1398*4882a593Smuzhiyun  * @this: NAND chip object
1399*4882a593Smuzhiyun  * @offs: offset in the device
1400*4882a593Smuzhiyun  */
nand_isreserved_bbt(struct nand_chip * this,loff_t offs)1401*4882a593Smuzhiyun int nand_isreserved_bbt(struct nand_chip *this, loff_t offs)
1402*4882a593Smuzhiyun {
1403*4882a593Smuzhiyun 	int block;
1404*4882a593Smuzhiyun 
1405*4882a593Smuzhiyun 	block = (int)(offs >> this->bbt_erase_shift);
1406*4882a593Smuzhiyun 	return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
1407*4882a593Smuzhiyun }
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun /**
1410*4882a593Smuzhiyun  * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1411*4882a593Smuzhiyun  * @this: NAND chip object
1412*4882a593Smuzhiyun  * @offs: offset in the device
1413*4882a593Smuzhiyun  * @allowbbt: allow access to bad block table region
1414*4882a593Smuzhiyun  */
nand_isbad_bbt(struct nand_chip * this,loff_t offs,int allowbbt)1415*4882a593Smuzhiyun int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt)
1416*4882a593Smuzhiyun {
1417*4882a593Smuzhiyun 	int block, res;
1418*4882a593Smuzhiyun 
1419*4882a593Smuzhiyun 	block = (int)(offs >> this->bbt_erase_shift);
1420*4882a593Smuzhiyun 	res = bbt_get_entry(this, block);
1421*4882a593Smuzhiyun 
1422*4882a593Smuzhiyun 	pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1423*4882a593Smuzhiyun 		 (unsigned int)offs, block, res);
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun 	switch (res) {
1426*4882a593Smuzhiyun 	case BBT_BLOCK_GOOD:
1427*4882a593Smuzhiyun 		return 0;
1428*4882a593Smuzhiyun 	case BBT_BLOCK_WORN:
1429*4882a593Smuzhiyun 		return 1;
1430*4882a593Smuzhiyun 	case BBT_BLOCK_RESERVED:
1431*4882a593Smuzhiyun 		return allowbbt ? 0 : 1;
1432*4882a593Smuzhiyun 	}
1433*4882a593Smuzhiyun 	return 1;
1434*4882a593Smuzhiyun }
1435*4882a593Smuzhiyun 
1436*4882a593Smuzhiyun /**
1437*4882a593Smuzhiyun  * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
1438*4882a593Smuzhiyun  * @this: NAND chip object
1439*4882a593Smuzhiyun  * @offs: offset of the bad block
1440*4882a593Smuzhiyun  */
nand_markbad_bbt(struct nand_chip * this,loff_t offs)1441*4882a593Smuzhiyun int nand_markbad_bbt(struct nand_chip *this, loff_t offs)
1442*4882a593Smuzhiyun {
1443*4882a593Smuzhiyun 	int block, ret = 0;
1444*4882a593Smuzhiyun 
1445*4882a593Smuzhiyun 	block = (int)(offs >> this->bbt_erase_shift);
1446*4882a593Smuzhiyun 
1447*4882a593Smuzhiyun 	/* Mark bad block in memory */
1448*4882a593Smuzhiyun 	bbt_mark_entry(this, block, BBT_BLOCK_WORN);
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun 	/* Update flash-based bad block table */
1451*4882a593Smuzhiyun 	if (this->bbt_options & NAND_BBT_USE_FLASH)
1452*4882a593Smuzhiyun 		ret = nand_update_bbt(this, offs);
1453*4882a593Smuzhiyun 
1454*4882a593Smuzhiyun 	return ret;
1455*4882a593Smuzhiyun }
1456