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