• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/drivers/mtd/bcm947xx/nand/
1/*
2 * Broadcom NAND flash BBT interface
3 *
4 * Copyright (C) 2013, Broadcom Corporation. All Rights Reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * $Id $
19 */
20/*
21 * When brcmnand_scan_bbt is called, then it tries to find the bad block table
22 * depending on the options in the bbt descriptor(s). If a bbt is found
23 * then the contents are read and the memory based bbt is created. If a
24 * mirrored bbt is selected then the mirror is searched too and the
25 * versions are compared. If the mirror has a greater version number
26 * than the mirror bbt is used to build the memory based bbt.
27 * If the tables are not versioned, then we "or" the bad block information.
28 * If one of the bbt's is out of date or does not exist it is (re)created.
29 * If no bbt exists at all then the device is scanned for factory marked
30 * good / bad blocks and the bad block tables are created.
31 *
32 * For manufacturer created bbts like the one found on M-SYS DOC devices
33 * the bbt is searched and read but never created
34 *
35 * The autogenerated bad block table is located in the last good blocks
36 * of the device. The table is mirrored, so it can be updated eventually.
37 * The table is marked in the oob area with an ident pattern and a version
38 * number which indicates which of both tables is more up to date.
39 *
40 * The table uses 2 bits per block
41 * 11b:     block is good
42 * 00b:     block is factory marked bad
43 * 01b, 10b:    block is marked bad due to wear
44 *
45 * The memory bad block table uses the following scheme:
46 * 00b:     block is good
47 * 01b:     block is marked bad due to wear
48 * 10b:     block is reserved (to protect the bbt area)
49 * 11b:     block is factory marked bad
50 *
51 * Multichip devices like DOC store the bad block info per floor.
52 *
53 * Following assumptions are made:
54 * - bbts start at a page boundary, if autolocated on a block boundary
55 * - the space necessary for a bbt in FLASH does not exceed a block boundary
56 *
57 */
58
59
60#include <linux/slab.h>
61#include <linux/types.h>
62#include <linux/mtd/mtd.h>
63#include <linux/mtd/nand.h>
64#include <linux/mtd/nand_ecc.h>
65#include <linux/bitops.h>
66#include <linux/delay.h>
67#include <linux/vmalloc.h>
68
69#define NFLASH_SUPPORT
70
71#include <typedefs.h>
72#include <osl.h>
73#include <bcmutils.h>
74#include <bcmdevs.h>
75#include <bcmnvram.h>
76#include <siutils.h>
77#include <hndpci.h>
78#include <pcicfg.h>
79#include <hndsoc.h>
80#include <sbchipc.h>
81#include <nflash.h>
82
83#include "brcmnand_priv.h"
84
85#define PRINTK(...)
86
87extern int gClearBBT;
88extern int gdebug;
89
90char brcmNandBBTMsg[1024];
91
92/* brcmnand=
93 *  rescan:     1. Rescan for bad blocks, and update existing BBT
94 *  showbbt:    2. Print out the contents of the BBT on boot up.
95 *
96 * The following commands are implemented but should be removed for
97 * production builds.
98 * Use userspace flash_eraseall instead.
99 * These were intended for development debugging only.
100 *  erase:  7. Erase entire flash, except CFE, and rescan for bad blocks
101 *  eraseall:   8. Erase entire flash, and rescan for bad blocks
102 *  clearbbt:   9. Erase BBT and rescan for bad blocks.
103 *                 (DANGEROUS, may lose Mfg's BIs).
104 */
105#define NANDCMD_RESCAN  1
106#define NANDCMD_SHOWBBT 2
107
108#define NANDCMD_ERASE       7
109#define NANDCMD_ERASEALL    8
110#define NANDCMD_CLEARBBT    9
111
112int brcmnand_update_bbt(struct mtd_info *mtd, loff_t offs);
113
114extern struct brcmnand_mtd brcmnand_info;
115
116static inline uint32_t device_size(struct mtd_info *mtd)
117{
118	return mtd->size;
119}
120
121/**
122 * check_pattern - [GENERIC] check if a pattern is in the buffer
123 * @buf:	the buffer to search
124 * @len:	the length of buffer to search
125 * @paglen:	the pagelength
126 * @td:		search pattern descriptor
127 *
128 * Check for a pattern at the given place. Used to search bad block
129 * tables and good / bad block identifiers.
130 * If the SCAN_EMPTY option is set then check, if all bytes except the
131 * pattern area contain 0xff
132 *
133*/
134static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
135{
136	int i, end = 0;
137	uint8_t *p = buf;
138
139	end = paglen + td->offs;
140	if (td->options & NAND_BBT_SCANEMPTY) {
141		for (i = 0; i < end; i++) {
142			if (p[i] != 0xff)
143				return -1;
144		}
145	}
146	p += end;
147
148	/* Compare the pattern */
149	for (i = 0; i < td->len; i++) {
150		if (p[i] != td->pattern[i])
151			return -1;
152	}
153
154	if (td->options & NAND_BBT_SCANEMPTY) {
155		p += td->len;
156		end += td->len;
157		for (i = end; i < len; i++) {
158			if (*p++ != 0xff)
159				return -1;
160		}
161	}
162	return 0;
163}
164
165/**
166 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
167 * @buf:	the buffer to search
168 * @td:		search pattern descriptor
169 *
170 * Check for a pattern at the given place. Used to search bad block
171 * tables and good / bad block identifiers. Same as check_pattern, but
172 * no optional empty check
173 *
174*/
175static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
176{
177	int i;
178	uint8_t *p = buf;
179
180	/* Compare the pattern */
181	for (i = 0; i < td->len; i++) {
182		if (p[td->offs + i] != td->pattern[i])
183			return -1;
184	}
185	return 0;
186}
187
188/**
189 * brcmnand_read_bbt - [GENERIC] Read the bad block table starting from page
190 * @mtd:	MTD device structure
191 * @buf:	temporary buffer
192 * @page:	the starting page
193 * @num:	the number of bbt descriptors to read
194 * @bits:	number of bits per block
195 * @offs:	offset in the memory table
196 * @reserved_block_code:	Pattern to identify reserved blocks
197 *
198 * Read the bad block table starting from page.
199 *
200 */
201static int brcmnand_read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
202	int bits, int offs, int reserved_block_code)
203{
204	int res, i, j, act = 0;
205	struct nand_chip *this = mtd->priv;
206	size_t retlen, len, totlen;
207	loff_t from;
208	uint8_t msk = (uint8_t) ((1 << bits) - 1);
209	si_t *sih = brcmnand_info.sih;
210	chipcregs_t *cc = brcmnand_info.cc;
211	osl_t *osh;
212
213	totlen = (num * bits) >> 3;
214	from = ((loff_t) page) << this->page_shift;
215
216	osh = si_osh(sih);
217	/* Clear ECC registers */
218	W_REG(osh, &cc->nand_ecc_corr_addr, 0);
219	W_REG(osh, &cc->nand_ecc_corr_addr_x, 0);
220	W_REG(osh, &cc->nand_ecc_unc_addr, 0);
221	W_REG(osh, &cc->nand_ecc_unc_addr_x, 0);
222
223	while (totlen) {
224		len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
225		res = mtd->read(mtd, from, len, &retlen, buf);
226		if (res < 0) {
227			if (retlen != len) {
228				printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
229				return res;
230			}
231			printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
232		}
233
234		/* Analyse data */
235		for (i = 0; i < len; i++) {
236			uint8_t dat = buf[i];
237			for (j = 0; j < 8; j += bits, act += 2) {
238				uint8_t tmp = (dat >> j) & msk;
239				if (tmp == msk)
240					continue;
241				if (reserved_block_code && (tmp == reserved_block_code)) {
242					printk(KERN_DEBUG
243						"nand_read_bbt: Reserved block at 0x%08x\n",
244						((offs << 2) + (act >> 1))
245							<< this->bbt_erase_shift);
246					this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
247					mtd->ecc_stats.bbtblocks++;
248					continue;
249				}
250				/* Leave it for now, if its matured we can move this
251				 * message to MTD_DEBUG_LEVEL0
252				 */
253				printk(KERN_DEBUG "nand_read_bbt: Bad block at 0x%08x\n",
254				       ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
255				printk("nand_read_bbt: Bad block at 0x%08x\n",
256				       ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
257				/* Factory marked bad or worn out ? */
258				if (tmp == 0)
259					this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
260				else
261					this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
262				mtd->ecc_stats.badblocks++;
263			}
264		}
265		totlen -= len;
266		from += len;
267	}
268	return 0;
269}
270
271/**
272 * brcmnand_read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
273 * @mtd:	MTD device structure
274 * @buf:	temporary buffer
275 * @td:		descriptor for the bad block table
276 * @chip:	read the table for a specific chip, -1 read all chips.
277 *		Applies only if NAND_BBT_PERCHIP option is set
278 *
279 * Read the bad block table for all chips starting at a given page
280 * We assume that the bbt bits are in consecutive order.
281*/
282static int brcmnand_read_abs_bbt(struct mtd_info *mtd, uint8_t *buf,
283	struct nand_bbt_descr *td, int chip)
284{
285	struct nand_chip *this = mtd->priv;
286	int res = 0, i;
287	int bits;
288
289	bits = td->options & NAND_BBT_NRBITS_MSK;
290	if (td->options & NAND_BBT_PERCHIP) {
291		int offs = 0;
292		for (i = 0; i < this->numchips; i++) {
293			if (chip == -1 || chip == i)
294				res = brcmnand_read_bbt(mtd, buf, td->pages[i],
295					this->chipsize >> this->bbt_erase_shift,
296					bits, offs, td->reserved_block_code);
297			if (res)
298				return res;
299			offs += this->chipsize >> (this->bbt_erase_shift + 2);
300		}
301	} else {
302		res = brcmnand_read_bbt(mtd, buf, td->pages[0],
303			mtd->size >> this->bbt_erase_shift, bits, 0, td->reserved_block_code);
304		if (res)
305			return res;
306	}
307	return 0;
308}
309
310/*
311 * Scan read raw data from flash
312 */
313static int brcmnand_scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
314	size_t len)
315{
316	struct mtd_oob_ops ops;
317
318	ops.mode = MTD_OOB_RAW;
319	ops.ooboffs = 0;
320	ops.ooblen = mtd->oobsize;
321	ops.oobbuf = buf;
322	ops.datbuf = buf;
323	ops.len = len;
324
325	return mtd->read_oob(mtd, offs, &ops);
326}
327
328/*
329 * Scan write data with oob to flash
330 */
331static int brcmnand_scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
332	uint8_t *buf, uint8_t *oob)
333{
334	struct mtd_oob_ops ops;
335
336	ops.mode = MTD_OOB_PLACE;
337	ops.ooboffs = 0;
338	ops.ooblen = mtd->oobsize;
339	ops.datbuf = buf;
340	ops.oobbuf = oob;
341	ops.len = len;
342
343	return mtd->write_oob(mtd, offs, &ops);
344}
345
346/**
347 * brcmnand_read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips
348 * starting at a given page
349 * @mtd:	MTD device structure
350 * @buf:	temporary buffer
351 * @td:		descriptor for the bad block table
352 * @md:		descriptor for the bad block table mirror
353 *
354 * Read the bad block table(s) for all chips starting at a given page
355 * We assume that the bbt bits are in consecutive order.
356 *
357*/
358static int brcmnand_read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
359	struct nand_bbt_descr *td, struct nand_bbt_descr *md)
360{
361	struct nand_chip *this = mtd->priv;
362
363	/* Read the primary version, if available */
364	if (td->options & NAND_BBT_VERSION) {
365		brcmnand_scan_read_raw(mtd, buf, td->pages[0] << this->page_shift,
366			mtd->writesize);
367		td->version[0] = buf[mtd->writesize + td->veroffs];
368		printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
369		       td->pages[0], td->version[0]);
370	}
371
372	/* Read the mirror version, if available */
373	if (md && (md->options & NAND_BBT_VERSION)) {
374		brcmnand_scan_read_raw(mtd, buf, md->pages[0] << this->page_shift,
375			mtd->writesize);
376		md->version[0] = buf[mtd->writesize + md->veroffs];
377		printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
378		       md->pages[0], md->version[0]);
379	}
380	return 1;
381}
382
383/*
384 * Scan a given block full
385 */
386static int brcmnand_scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
387	loff_t offs, uint8_t *buf, size_t readlen, int scanlen, int len)
388{
389	int ret, j;
390
391	ret = brcmnand_scan_read_raw(mtd, buf, offs, readlen);
392	if (ret)
393		return ret;
394
395	for (j = 0; j < len; j++, buf += scanlen) {
396		if (check_pattern(buf, scanlen, mtd->writesize, bd))
397			return 1;
398	}
399	return 0;
400}
401
402/*
403 * Scan a given block partially
404 */
405static int brcmnand_scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
406	loff_t offs, uint8_t *buf, int len)
407{
408	struct mtd_oob_ops ops;
409	int j, ret;
410	int dir;
411
412		/* SLC: First and 2nd page */
413		dir = 1;
414	ops.ooblen = mtd->oobsize;
415	ops.oobbuf = buf;
416	ops.ooboffs = 0;
417	ops.datbuf = NULL;
418	ops.mode = MTD_OOB_PLACE;
419
420	for (j = 0; j < len; j++) {
421		/*
422		 * Read the full oob until read_oob is fixed to
423		 * handle single byte reads for 16 bit
424		 * buswidth
425		 */
426		ret = mtd->read_oob(mtd, offs, &ops);
427		if (ret)
428			return ret;
429
430		if (check_short_pattern(buf, bd))
431			return 1;
432
433		offs += (dir * mtd->writesize);
434	}
435	return 0;
436}
437
438/**
439 * brcmnand_create_bbt - [GENERIC] Create a bad block table by scanning the device
440 * @mtd:	MTD device structure
441 * @buf:	temporary buffer
442 * @bd:		descriptor for the good/bad block search pattern
443 * @chip:	create the table for a specific chip, -1 read all chips.
444 *		Applies only if NAND_BBT_PERCHIP option is set
445 *
446 * Create a bad block table by scanning the device
447 * for the given good/bad block identify pattern
448 */
449static int brcmnand_create_bbt(struct mtd_info *mtd, uint8_t *buf,
450	struct nand_bbt_descr *bd, int chip)
451{
452	struct nand_chip *this = mtd->priv;
453	int i, numblocks, len, scanlen;
454	int startblock;
455	loff_t from;
456	size_t readlen;
457
458	printk(KERN_INFO "Scanning device for bad blocks\n");
459
460	if (bd->options & NAND_BBT_SCANALLPAGES)
461		len = 1 << (this->bbt_erase_shift - this->page_shift);
462	else {
463		if (bd->options & NAND_BBT_SCAN2NDPAGE)
464			len = 2;
465		else
466			len = 1;
467	}
468
469	if (!(bd->options & NAND_BBT_SCANEMPTY)) {
470		/* We need only read few bytes from the OOB area */
471		scanlen = 0;
472		readlen = bd->len;
473	} else {
474		/* Full page content should be read */
475		scanlen = mtd->writesize + mtd->oobsize;
476		readlen = len * mtd->writesize;
477	}
478
479	if (chip == -1) {
480		/* Note that numblocks is 2 * (real numblocks) here, see i+=2
481		 * below as it makes shifting and masking less painful
482		 */
483		numblocks = mtd->size >> (this->bbt_erase_shift - 1);
484		startblock = 0;
485		from = 0;
486	} else {
487		if (chip >= this->numchips) {
488			printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
489			       chip + 1, this->numchips);
490			return -EINVAL;
491		}
492		numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
493		startblock = chip * numblocks;
494		numblocks += startblock;
495		from = startblock << (this->bbt_erase_shift - 1);
496	}
497
498	for (i = startblock; i < numblocks;) {
499		int ret;
500
501		if (bd->options & NAND_BBT_SCANALLPAGES)
502			ret = brcmnand_scan_block_full(mtd, bd, from, buf, readlen,
503				scanlen, len);
504		else
505			ret = brcmnand_scan_block_fast(mtd, bd, from, buf, len);
506
507		if (ret < 0)
508			return ret;
509
510		if (ret) {
511			this->bbt[i >> 3] |= 0x03 << (i & 0x6);
512			printk(KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
513			       i >> 1, (unsigned int)from);
514			mtd->ecc_stats.badblocks++;
515		}
516
517		i += 2;
518		from += (1 << this->bbt_erase_shift);
519	}
520	return 0;
521}
522
523/**
524 * brcmnand_search_bbt - [GENERIC] scan the device for a specific bad block table
525 * @mtd:	MTD device structure
526 * @buf:	temporary buffer
527 * @td:		descriptor for the bad block table
528 *
529 * Read the bad block table by searching for a given ident pattern.
530 * Search is preformed either from the beginning up or from the end of
531 * the device downwards. The search starts always at the start of a
532 * block.
533 * If the option NAND_BBT_PERCHIP is given, each chip is searched
534 * for a bbt, which contains the bad block information of this chip.
535 * This is necessary to provide support for certain DOC devices.
536 *
537 * The bbt ident pattern resides in the oob area of the first page
538 * in a block.
539 */
540static int brcmnand_search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
541{
542	struct nand_chip *this = mtd->priv;
543	int i, chips;
544	int bits, startblock, block, dir;
545	int scanlen = mtd->writesize + mtd->oobsize;
546	int bbtblocks;
547	int blocktopage = this->bbt_erase_shift - this->page_shift;
548
549	/* Search direction top -> down ? */
550	if (td->options & NAND_BBT_LASTBLOCK) {
551		startblock = (mtd->size >> this->bbt_erase_shift) - 1;
552		dir = -1;
553	} else {
554		startblock = 0;
555		dir = 1;
556	}
557
558	/* Do we have a bbt per chip ? */
559	if (td->options & NAND_BBT_PERCHIP) {
560		chips = this->numchips;
561		bbtblocks = this->chipsize >> this->bbt_erase_shift;
562		startblock &= bbtblocks - 1;
563	} else {
564		chips = 1;
565		bbtblocks = mtd->size >> this->bbt_erase_shift;
566	}
567
568	/* Number of bits for each erase block in the bbt */
569	bits = td->options & NAND_BBT_NRBITS_MSK;
570
571	for (i = 0; i < chips; i++) {
572		/* Reset version information */
573		td->version[i] = 0;
574		td->pages[i] = -1;
575		/* Scan the maximum number of blocks */
576		for (block = 0; block < td->maxblocks; block++) {
577
578			int actblock = startblock + dir * block;
579			loff_t offs = actblock << this->bbt_erase_shift;
580
581			/* Read first page */
582			brcmnand_scan_read_raw(mtd, buf, offs, mtd->writesize);
583			if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
584				td->pages[i] = actblock << blocktopage;
585				if (td->options & NAND_BBT_VERSION) {
586					td->version[i] = buf[mtd->writesize + td->veroffs];
587				}
588				break;
589			}
590		}
591		startblock += this->chipsize >> this->bbt_erase_shift;
592	}
593	/* Check, if we found a bbt for each requested chip */
594	for (i = 0; i < chips; i++) {
595		if (td->pages[i] == -1)
596			printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
597		else
598			printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n",
599				td->pages[i], td->version[i]);
600	}
601	return 0;
602}
603
604/**
605 * brcmnand_search_read_bbts - [GENERIC] scan the device for bad block table(s)
606 * @mtd:	MTD device structure
607 * @buf:	temporary buffer
608 * @td:		descriptor for the bad block table
609 * @md:		descriptor for the bad block table mirror
610 *
611 * Search and read the bad block table(s)
612*/
613static int brcmnand_search_read_bbts(struct mtd_info *mtd, uint8_t *buf,
614	struct nand_bbt_descr *td, struct nand_bbt_descr *md)
615{
616	/* Search the primary table */
617	brcmnand_search_bbt(mtd, buf, td);
618
619	/* Search the mirror table */
620	if (md)
621		brcmnand_search_bbt(mtd, buf, md);
622
623	/* Force result check */
624	return 1;
625}
626
627/**
628 * brcmnand_write_bbt - [GENERIC] (Re)write the bad block table
629 *
630 * @mtd:	MTD device structure
631 * @buf:	temporary buffer
632 * @td:		descriptor for the bad block table
633 * @md:		descriptor for the bad block table mirror
634 * @chipsel:	selector for a specific chip, -1 for all
635 *
636 * (Re)write the bad block table
637 *
638*/
639static int brcmnand_write_bbt(struct mtd_info *mtd, uint8_t *buf,
640	struct nand_bbt_descr *td, struct nand_bbt_descr *md, int chipsel)
641{
642	struct nand_chip *this = mtd->priv;
643	struct erase_info einfo;
644	int i, j, res, chip = 0;
645	int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
646	int nrchips, bbtoffs, pageoffs, ooboffs;
647	uint8_t msk[4];
648	uint8_t rcode = td->reserved_block_code;
649	size_t retlen, len = 0;
650	loff_t to;
651	struct mtd_oob_ops ops;
652
653	ops.ooblen = mtd->oobsize;
654	ops.ooboffs = 0;
655	ops.datbuf = NULL;
656	ops.mode = MTD_OOB_PLACE;
657
658	if (!rcode)
659		rcode = 0xff;
660	/* Write bad block table per chip rather than per device ? */
661	if (td->options & NAND_BBT_PERCHIP) {
662		numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
663		/* Full device write or specific chip ? */
664		if (chipsel == -1) {
665			nrchips = this->numchips;
666		} else {
667			nrchips = chipsel + 1;
668			chip = chipsel;
669		}
670	} else {
671		numblocks = (int)(mtd->size >> this->bbt_erase_shift);
672		nrchips = 1;
673	}
674
675	/* Loop through the chips */
676	for (; chip < nrchips; chip++) {
677
678		/* There was already a version of the table, reuse the page
679		 * This applies for absolute placement too, as we have the
680		 * page nr. in td->pages.
681		 */
682		if (td->pages[chip] != -1) {
683			page = td->pages[chip];
684			goto write;
685		}
686
687		/* Automatic placement of the bad block table */
688		/* Search direction top -> down ? */
689		if (td->options & NAND_BBT_LASTBLOCK) {
690			startblock = numblocks * (chip + 1) - 1;
691			dir = -1;
692		} else {
693			startblock = chip * numblocks;
694			dir = 1;
695		}
696
697		for (i = 0; i < td->maxblocks; i++) {
698			int block = startblock + dir * i;
699			/* Check, if the block is bad */
700			switch ((this->bbt[block >> 2] >>
701				 (2 * (block & 0x03))) & 0x03) {
702			case 0x01:
703			case 0x03:
704				continue;
705			}
706			page = block <<
707				(this->bbt_erase_shift - this->page_shift);
708			/* Check, if the block is used by the mirror table */
709			if (!md || md->pages[chip] != page)
710				goto write;
711		}
712		printk(KERN_ERR "No space left to write bad block table\n");
713		return -ENOSPC;
714	write:
715
716		/* Set up shift count and masks for the flash table */
717		bits = td->options & NAND_BBT_NRBITS_MSK;
718		msk[2] = ~rcode;
719		switch (bits) {
720		case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
721			msk[3] = 0x01;
722			break;
723		case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
724			msk[3] = 0x03;
725			break;
726		case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
727			msk[3] = 0x0f;
728			break;
729		case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
730			msk[3] = 0xff;
731			break;
732		default: return -EINVAL;
733		}
734
735		bbtoffs = chip * (numblocks >> 2);
736
737		to = ((loff_t) page) << this->page_shift;
738
739		/* Must we save the block contents ? */
740		if (td->options & NAND_BBT_SAVECONTENT) {
741			/* Make it block aligned */
742			to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
743			len = 1 << this->bbt_erase_shift;
744			res = mtd->read(mtd, to, len, &retlen, buf);
745			if (res < 0) {
746				if (retlen != len) {
747					printk(KERN_INFO "nand_bbt: Error "
748					       "reading block for writing "
749					       "the bad block table\n");
750					return res;
751				}
752				printk(KERN_WARNING "nand_bbt: ECC error "
753				       "while reading block for writing "
754				       "bad block table\n");
755			}
756			/* Read oob data */
757			ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
758			ops.oobbuf = &buf[len];
759			res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
760			if (res < 0 || ops.oobretlen != ops.ooblen)
761				goto outerr;
762
763			/* Calc the byte offset in the buffer */
764			pageoffs = page - (int)(to >> this->page_shift);
765			offs = pageoffs << this->page_shift;
766			/* Preset the bbt area with 0xff */
767			memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
768			ooboffs = len + (pageoffs * mtd->oobsize);
769
770		} else {
771			/* Calc length */
772			len = (size_t) (numblocks >> sft);
773			/* Make it page aligned ! */
774			len = (len + (mtd->writesize - 1)) &
775				~(mtd->writesize - 1);
776			/* Preset the buffer with 0xff */
777			memset(buf, 0xff, len +
778			       (len >> this->page_shift)* mtd->oobsize);
779			offs = 0;
780			ooboffs = len;
781			/* Pattern is located in oob area of first page */
782			memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
783		}
784
785		if (td->options & NAND_BBT_VERSION)
786			buf[ooboffs + td->veroffs] = td->version[chip];
787
788		/* walk through the memory table */
789		for (i = 0; i < numblocks;) {
790			uint8_t dat;
791			dat = this->bbt[bbtoffs + (i >> 2)];
792			for (j = 0; j < 4; j++, i++) {
793				int sftcnt = (i << (3 - sft)) & sftmsk;
794				/* Do not store the reserved bbt blocks ! */
795				buf[offs + (i >> sft)] &=
796					~(msk[dat & 0x03] << sftcnt);
797				dat >>= 2;
798			}
799		}
800
801		memset(&einfo, 0, sizeof(einfo));
802		einfo.mtd = mtd;
803		einfo.addr = (unsigned long)to;
804		einfo.len = 1 << this->bbt_erase_shift;
805		res = mtd->erase(mtd, &einfo );
806		if (res < 0)
807			goto outerr;
808
809		res = brcmnand_scan_write_bbt(mtd, to, len, buf, &buf[len]);
810		if (res < 0)
811			goto outerr;
812
813		printk(KERN_DEBUG "Bad block table written to 0x%08x, version "
814		       "0x%02X\n", (unsigned int)to, td->version[chip]);
815
816		/* Mark it as used */
817		td->pages[chip] = page;
818	}
819	return 0;
820
821outerr:
822	printk(KERN_WARNING
823	       "nand_bbt: Error while writing bad block table %d\n", res);
824	return res;
825}
826
827/**
828 * brcmnand_memory_bbt - [GENERIC] create a memory based bad block table
829 * @mtd:	MTD device structure
830 * @bd:		descriptor for the good/bad block search pattern
831 *
832 * The function creates a memory based bbt by scanning the device
833 * for manufacturer / software marked good / bad blocks
834*/
835static inline int brcmnand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
836{
837	struct nand_chip *this = mtd->priv;
838
839	bd->options &= ~NAND_BBT_SCANEMPTY;
840	return brcmnand_create_bbt(mtd, this->buffers->databuf, bd, -1);
841}
842
843/**
844 * brcmnand_check_create - [GENERIC] create and write bbt(s) if necessary
845 * @mtd:	MTD device structure
846 * @buf:	temporary buffer
847 * @bd:		descriptor for the good/bad block search pattern
848 *
849 * The function checks the results of the previous call to read_bbt
850 * and creates / updates the bbt(s) if necessary
851 * Creation is necessary if no bbt was found for the chip/device
852 * Update is necessary if one of the tables is missing or the
853 * version nr. of one table is less than the other
854*/
855static int brcmnand_check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
856{
857	int i, chips, writeops, chipsel, res;
858	struct nand_chip *this = mtd->priv;
859	struct nand_bbt_descr *td = this->bbt_td;
860	struct nand_bbt_descr *md = this->bbt_md;
861	struct nand_bbt_descr *rd, *rd2;
862
863	/* Do we have a bbt per chip ? */
864	if (td->options & NAND_BBT_PERCHIP)
865		chips = this->numchips;
866	else
867		chips = 1;
868
869	for (i = 0; i < chips; i++) {
870		writeops = 0;
871		rd = NULL;
872		rd2 = NULL;
873		/* Per chip or per device ? */
874		chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
875		/* Mirrored table avilable ? */
876		if (md) {
877			if (td->pages[i] == -1 && md->pages[i] == -1) {
878				writeops = 0x03;
879				goto create;
880			}
881
882			if (td->pages[i] == -1) {
883				rd = md;
884				td->version[i] = md->version[i];
885				writeops = 1;
886				goto writecheck;
887			}
888
889			if (md->pages[i] == -1) {
890				rd = td;
891				md->version[i] = td->version[i];
892				writeops = 2;
893				goto writecheck;
894			}
895
896			if (td->version[i] == md->version[i]) {
897				rd = td;
898				if (!(td->options & NAND_BBT_VERSION))
899					rd2 = md;
900				goto writecheck;
901			}
902
903			if (((int8_t) (td->version[i] - md->version[i])) > 0) {
904				rd = td;
905				md->version[i] = td->version[i];
906				writeops = 2;
907			} else {
908				rd = md;
909				td->version[i] = md->version[i];
910				writeops = 1;
911			}
912
913			goto writecheck;
914
915		} else {
916			if (td->pages[i] == -1) {
917				writeops = 0x01;
918				goto create;
919			}
920			rd = td;
921			goto writecheck;
922		}
923	create:
924		/* Create the bad block table by scanning the device ? */
925		if (!(td->options & NAND_BBT_CREATE))
926			continue;
927
928		/* Create the table in memory by scanning the chip(s) */
929		brcmnand_create_bbt(mtd, buf, bd, chipsel);
930
931		td->version[i] = 1;
932		if (md)
933			md->version[i] = 1;
934	writecheck:
935		/* read back first ? */
936		if (rd)
937			brcmnand_read_abs_bbt(mtd, buf, rd, chipsel);
938		/* If they weren't versioned, read both. */
939		if (rd2)
940			brcmnand_read_abs_bbt(mtd, buf, rd2, chipsel);
941
942		/* Write the bad block table to the device ? */
943		if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
944			res = brcmnand_write_bbt(mtd, buf, td, md, chipsel);
945			if (res < 0)
946				return res;
947		}
948
949		/* Write the mirror bad block table to the device ? */
950		if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
951			res = brcmnand_write_bbt(mtd, buf, md, td, chipsel);
952			if (res < 0)
953				return res;
954		}
955	}
956	return 0;
957}
958
959/**
960 * mark_bbt_regions - [GENERIC] mark the bad block table regions
961 * @mtd:	MTD device structure
962 * @td:		bad block table descriptor
963 *
964 * The bad block table regions are marked as "bad" to prevent
965 * accidental erasures / writes. The regions are identified by
966 * the mark 0x02.
967*/
968static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
969{
970	struct nand_chip *this = mtd->priv;
971	int i, j, chips, block, nrblocks, update;
972	uint8_t oldval, newval;
973
974	/* Do we have a bbt per chip ? */
975	if (td->options & NAND_BBT_PERCHIP) {
976		chips = this->numchips;
977		nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
978	} else {
979		chips = 1;
980		nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
981	}
982
983	for (i = 0; i < chips; i++) {
984		if ((td->options & NAND_BBT_ABSPAGE) ||
985		    !(td->options & NAND_BBT_WRITE)) {
986			if (td->pages[i] == -1)
987				continue;
988			block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
989			block <<= 1;
990			oldval = this->bbt[(block >> 3)];
991			newval = oldval | (0x2 << (block & 0x06));
992			this->bbt[(block >> 3)] = newval;
993			if ((oldval != newval) && td->reserved_block_code)
994				brcmnand_update_bbt(mtd, block << (this->bbt_erase_shift - 1));
995			continue;
996		}
997		update = 0;
998		if (td->options & NAND_BBT_LASTBLOCK)
999			block = ((i + 1) * nrblocks) - td->maxblocks;
1000		else
1001			block = i * nrblocks;
1002		block <<= 1;
1003		for (j = 0; j < td->maxblocks; j++) {
1004			oldval = this->bbt[(block >> 3)];
1005			newval = oldval | (0x2 << (block & 0x06));
1006			this->bbt[(block >> 3)] = newval;
1007			if (oldval != newval)
1008				update = 1;
1009			block += 2;
1010		}
1011		/* If we want reserved blocks to be recorded to flash, and some
1012		 * new ones have been marked, then we need to update the stored
1013		 * bbts.  This should only happen once.
1014		 */
1015		if (update && td->reserved_block_code)
1016			brcmnand_update_bbt(mtd, (block - 2) << (this->bbt_erase_shift - 1));
1017	}
1018}
1019
1020/**
1021 * brcmnand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1022 * @mtd:	MTD device structure
1023 * @bd:		descriptor for the good/bad block search pattern
1024 *
1025 * The function checks, if a bad block table(s) is/are already
1026 * available. If not it scans the device for manufacturer
1027 * marked good / bad blocks and writes the bad block table(s) to
1028 * the selected place.
1029 *
1030 * The bad block table memory is allocated here. It must be freed
1031 * by calling the nand_free_bbt function.
1032 *
1033*/
1034int brcmnand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1035{
1036	struct nand_chip *this = mtd->priv;
1037	int len, res = 0;
1038	uint8_t *buf;
1039	struct nand_bbt_descr *td = this->bbt_td;
1040	struct nand_bbt_descr *md = this->bbt_md;
1041
1042	len = mtd->size >> (this->bbt_erase_shift + 2);
1043	/* Allocate memory (2bit per block) and clear the memory bad block table */
1044	this->bbt = kzalloc(len, GFP_KERNEL);
1045	if (!this->bbt) {
1046		printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
1047		return -ENOMEM;
1048	}
1049
1050	/* If no primary table decriptor is given, scan the device
1051	 * to build a memory based bad block table
1052	 */
1053	if (!td) {
1054		if ((res = brcmnand_memory_bbt(mtd, bd))) {
1055			printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
1056			kfree(this->bbt);
1057			this->bbt = NULL;
1058		}
1059		return res;
1060	}
1061
1062	/* Allocate a temporary buffer for one eraseblock incl. oob */
1063	len = (1 << this->bbt_erase_shift);
1064	len += (len >> this->page_shift) * mtd->oobsize;
1065	buf = vmalloc(len);
1066	if (!buf) {
1067		printk(KERN_ERR "nand_bbt: Out of memory\n");
1068		kfree(this->bbt);
1069		this->bbt = NULL;
1070		return -ENOMEM;
1071	}
1072
1073	/* Is the bbt at a given page ? */
1074	if (td->options & NAND_BBT_ABSPAGE) {
1075		res = brcmnand_read_abs_bbts(mtd, buf, td, md);
1076	} else {
1077		/* Search the bad block table using a pattern in oob */
1078		res = brcmnand_search_read_bbts(mtd, buf, td, md);
1079	}
1080
1081	if (res)
1082		res = brcmnand_check_create(mtd, buf, bd);
1083
1084	/* Prevent the bbt regions from erasing / writing */
1085	mark_bbt_region(mtd, td);
1086	if (md)
1087		mark_bbt_region(mtd, md);
1088
1089	vfree(buf);
1090	return res;
1091}
1092
1093
1094/**
1095 * brcmnand_update_bbt - [NAND Interface] update bad block table(s)
1096 * @mtd:	MTD device structure
1097 * @offs:	the offset of the newly marked block
1098 *
1099 * The function updates the bad block table(s)
1100*/
1101int brcmnand_update_bbt(struct mtd_info *mtd, loff_t offs)
1102{
1103	struct nand_chip *this = mtd->priv;
1104	int len, res = 0, writeops = 0;
1105	int chip, chipsel;
1106	uint8_t *buf;
1107	struct nand_bbt_descr *td = this->bbt_td;
1108	struct nand_bbt_descr *md = this->bbt_md;
1109
1110	if (!this->bbt || !td)
1111		return -EINVAL;
1112
1113	len = mtd->size >> (this->bbt_erase_shift + 2);
1114	/* Allocate a temporary buffer for one eraseblock incl. oob */
1115	len = (1 << this->bbt_erase_shift);
1116	len += (len >> this->page_shift) * mtd->oobsize;
1117	buf = kmalloc(len, GFP_KERNEL);
1118	if (!buf) {
1119		printk(KERN_ERR "nand_update_bbt: Out of memory\n");
1120		return -ENOMEM;
1121	}
1122
1123	writeops = md != NULL ? 0x03 : 0x01;
1124
1125	/* Do we have a bbt per chip ? */
1126	if (td->options & NAND_BBT_PERCHIP) {
1127		chip = (int)(offs >> this->chip_shift);
1128		chipsel = chip;
1129	} else {
1130		chip = 0;
1131		chipsel = -1;
1132	}
1133
1134	td->version[chip]++;
1135	if (md)
1136		md->version[chip]++;
1137
1138	/* Write the bad block table to the device ? */
1139	if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1140		res = brcmnand_write_bbt(mtd, buf, td, md, chipsel);
1141		if (res < 0)
1142			goto out;
1143	}
1144	/* Write the mirror bad block table to the device ? */
1145	if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1146		res = brcmnand_write_bbt(mtd, buf, md, td, chipsel);
1147	}
1148
1149out:
1150	kfree(buf);
1151	return res;
1152}
1153
1154/* Define some generic bad / good block scan pattern which are used
1155 * while scanning a device for factory marked good / bad blocks.
1156 */
1157static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1158
1159static struct nand_bbt_descr smallpage_memorybased = {
1160	.options = NAND_BBT_SCAN2NDPAGE,
1161	.offs = 5,
1162	.len = 1,
1163	.pattern = scan_ff_pattern
1164};
1165
1166static struct nand_bbt_descr largepage_memorybased = {
1167	.options = 0,
1168	.offs = 0,
1169	.len = 2,
1170	.pattern = scan_ff_pattern
1171};
1172
1173static struct nand_bbt_descr smallpage_flashbased = {
1174	.options = NAND_BBT_SCAN2NDPAGE,
1175	.offs = 5,
1176	.len = 1,
1177	.pattern = scan_ff_pattern
1178};
1179
1180static struct nand_bbt_descr largepage_flashbased = {
1181	.options = NAND_BBT_SCAN2NDPAGE,
1182	.offs = 0,
1183	.len = 2,
1184	.pattern = scan_ff_pattern
1185};
1186
1187/* 2K & 4K page MLC NAND use same pattern */
1188static struct nand_bbt_descr bch4_flashbased = {
1189	.options = NAND_BBT_SCAN2NDPAGE,
1190	.offs = 0,
1191	.len = 1,
1192	.pattern = scan_ff_pattern
1193};
1194
1195static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1196
1197static struct nand_bbt_descr agand_flashbased = {
1198	.options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1199	.offs = 0x20,
1200	.len = 6,
1201	.pattern = scan_agand_pattern
1202};
1203
1204/* Generic flash bbt decriptors
1205*/
1206static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1207static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1208
1209/*
1210 * THT: We only have 1 chip per device
1211 */
1212static struct nand_bbt_descr bbt_main_descr = {
1213	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1214		| NAND_BBT_2BIT | NAND_BBT_VERSION /* | NAND_BBT_PERCHIP */,
1215	.offs = 9, /* THT: Changed from 8 */
1216	.len = 4,
1217	.veroffs = 13, /* THT: Changed from 12 */
1218	.maxblocks = 4, /* THT: Will update later, based on 1MB partition for BBT */
1219	.pattern = bbt_pattern
1220};
1221
1222static struct nand_bbt_descr bbt_mirror_descr = {
1223	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1224		| NAND_BBT_2BIT | NAND_BBT_VERSION /* | NAND_BBT_PERCHIP */,
1225	.offs = 9, /* THT: Changed from 8 */
1226	.len = 4,
1227	.veroffs = 13,  /* THT: Changed from 12 */
1228	.maxblocks = 4,
1229	.pattern = mirror_pattern
1230};
1231
1232/* SLC flash using BCH-4 ECC, SM & Large page use same descriptor template */
1233static struct nand_bbt_descr bbt_slc_bch4_main_descr = {
1234	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1235		| NAND_BBT_2BIT | NAND_BBT_VERSION /* | NAND_BBT_PERCHIP */,
1236	.offs = 1, /* THT: Changed from 8 */
1237	.len = 4,
1238	.veroffs = 6,  /* THT: Changed from 12 */
1239	.maxblocks = 8,
1240	.pattern = bbt_pattern
1241};
1242
1243static struct nand_bbt_descr bbt_slc_bch4_mirror_descr = {
1244	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1245		| NAND_BBT_2BIT | NAND_BBT_VERSION /* | NAND_BBT_PERCHIP */,
1246	.offs = 1,
1247	.len = 4,
1248	.veroffs = 6,
1249	.maxblocks = 8,
1250	.pattern = mirror_pattern
1251};
1252
1253static struct nand_bbt_descr bbt_bch4_main_descr = {
1254	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1255		| NAND_BBT_2BIT | NAND_BBT_VERSION /* | NAND_BBT_PERCHIP */,
1256	.offs = 1,
1257	.len = 4,
1258	.veroffs = 5, /* THT: Changed from 12 */
1259	.maxblocks = 8, /* THT: Will update later, based on 4MB partition for BBT */
1260	.pattern = bbt_pattern
1261};
1262
1263static struct nand_bbt_descr bbt_bch4_mirror_descr = {
1264	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1265		| NAND_BBT_2BIT | NAND_BBT_VERSION /* | NAND_BBT_PERCHIP */,
1266	.offs = 1, /* THT: Changed from 8 */
1267	.len = 4,
1268	.veroffs = 5,  /* THT: Changed from 12 */
1269	.maxblocks = 8,
1270	.pattern = mirror_pattern
1271};
1272
1273/**
1274 * brcmnand_isbad_bbt - [NAND Interface] Check if a block is bad
1275 * @mtd:    MTD device structure
1276 * @offs:   offset in the device
1277 * @allowbbt:   allow access to bad block table region
1278 *
1279 * Each byte in the BBT contains 4 entries, 2 bits each per block.
1280 * So the entry for the block b is:
1281 * bbt[b >> 2] & (0x3 << ((b & 0x3) << 1)))
1282 *
1283*/
1284int brcmnand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
1285{
1286	struct nand_chip *this = mtd->priv;
1287	uint32_t block; /* Used as an index, so 32bit. */
1288	uint8_t res;
1289
1290	/* THT 03/20/07: Get block number. It's more convenient to do it in the
1291	 * following way but is actually the same thing as in comment the above
1292	 */
1293	block = (uint32_t) (offs >>  (this->bbt_erase_shift - 1));
1294	res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1295	DEBUG(MTD_DEBUG_LEVEL3, "brcmnand_isbad_bbt(): bbt info for offs "
1296		"0x%08x: (block %d) 0x%02x\n", (unsigned int)offs, block >> 1, res);
1297	if (res)
1298		printk("brcmnand_isbad_bbt(): bbt info for offs "
1299			"0x%08x: (block %d) 0x%02x\n", (unsigned int)offs, block >> 1, res);
1300
1301	switch ((int)res) {
1302	case 0x00:
1303		/* Good block */
1304		return 0;
1305	case 0x01:
1306		/* Marked bad due to wear */
1307		return 1;
1308	case 0x02:
1309		/* Reserved blocks */
1310		return allowbbt ? 0 : 1;
1311	}
1312	return 1;
1313}
1314
1315/**
1316 * brcmnand_default_bbt - [NAND Interface] Select a default bad block table for the device
1317 * @mtd:	MTD device structure
1318 *
1319 * This function selects the default bad block table
1320 * support for the device and calls the nand_scan_bbt function
1321 *
1322*/
1323int brcmnand_default_bbt(struct mtd_info *mtd)
1324{
1325	struct nand_chip *this = mtd->priv;
1326	int ret;
1327
1328	mtd->get_device( mtd );
1329	/* Default for AG-AND. We must use a flash based
1330	 * bad block table as the devices have factory marked
1331	 * _good_ blocks. Erasing those blocks leads to loss
1332	 * of the good / bad information, so we _must_ store
1333	 * this information in a good / bad table during
1334	 * startup
1335	 */
1336	if (this->options & NAND_IS_AND) {
1337		/* Use the default pattern descriptors */
1338		if (!this->bbt_td) {
1339			this->bbt_td = &bbt_main_descr;
1340			this->bbt_md = &bbt_mirror_descr;
1341		}
1342		this->options |= NAND_USE_FLASH_BBT;
1343		ret = brcmnand_scan_bbt(mtd, &agand_flashbased);
1344		mtd->put_device(mtd);
1345		return ret;
1346	}
1347
1348	/* Is a flash based bad block table requested ? */
1349	if (this->options & NAND_USE_FLASH_BBT) {
1350		if (this->ecc.bytes == 3) {
1351			/* Use the default pattern descriptors */
1352			if (!this->bbt_td) {
1353				this->bbt_td = &bbt_main_descr;
1354				this->bbt_md = &bbt_mirror_descr;
1355			}
1356			if (!this->badblock_pattern) {
1357				this->badblock_pattern = (mtd->writesize > 512) ?
1358					&largepage_flashbased : &smallpage_flashbased;
1359			}
1360		} else {
1361			if(this->cellinfo & NAND_CI_CELLTYPE_MSK) {
1362				if (!this->bbt_td) {
1363					this->bbt_td = &bbt_bch4_main_descr;
1364					this->bbt_md = &bbt_bch4_mirror_descr;
1365				}
1366				if (!this->badblock_pattern) {
1367					/* 2K and 4K MLC NAND use the same pattern */
1368					this->badblock_pattern = &bch4_flashbased;
1369				}
1370			} else {
1371				/* SLC using BCH-4 ECC */
1372				if (!this->bbt_td) {
1373					this->bbt_td = &bbt_slc_bch4_main_descr;
1374					this->bbt_md = &bbt_slc_bch4_mirror_descr;
1375				}
1376				if (!this->badblock_pattern) {
1377					/* 2K and 4K MLC NAND use the same pattern */
1378					this->badblock_pattern = (mtd->writesize > 512) ?
1379						&bch4_flashbased : &smallpage_flashbased;
1380				}
1381			}
1382		}
1383	} else {
1384		this->bbt_td = NULL;
1385		this->bbt_md = NULL;
1386		if (!this->badblock_pattern) {
1387			this->badblock_pattern = (mtd->writesize > 512) ?
1388			    &largepage_memorybased : &smallpage_memorybased;
1389		}
1390	}
1391
1392	/* BBT partition occupies 1 MB at the end of the useable flash, so adjust
1393	 * maxblocks accordingly. Only applies to flash with 512MB or less, since
1394	 * we don't have the extra reserved space at the end of the flash
1395	 * (1FF0_0000 - 1FFF_FFFF).
1396	 */
1397	if (mtd->size <= (512 << 20)) {
1398		this->bbt_td->maxblocks = this->bbt_md->maxblocks =
1399			(1 << (20 - this->bbt_erase_shift));
1400	}
1401	ret = brcmnand_scan_bbt(mtd, this->badblock_pattern);
1402	mtd->put_device(mtd);
1403	return ret;
1404}
1405
1406EXPORT_SYMBOL(brcmnand_scan_bbt);
1407EXPORT_SYMBOL(brcmnand_default_bbt);
1408