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