xref: /OK3568_Linux_fs/u-boot/drivers/mtd/onenand/onenand_bbt.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *  linux/drivers/mtd/onenand/onenand_bbt.c
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *  Bad Block Table support for the OneNAND driver
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *  Copyright(c) 2005-2008 Samsung Electronics
7*4882a593Smuzhiyun  *  Kyungmin Park <kyungmin.park@samsung.com>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  *  TODO:
10*4882a593Smuzhiyun  *    Split BBT core and chip specific BBT.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
13*4882a593Smuzhiyun  * it under the terms of the GNU General Public License version 2 as
14*4882a593Smuzhiyun  * published by the Free Software Foundation.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <common.h>
18*4882a593Smuzhiyun #include <linux/compat.h>
19*4882a593Smuzhiyun #include <linux/mtd/mtd.h>
20*4882a593Smuzhiyun #include <linux/mtd/onenand.h>
21*4882a593Smuzhiyun #include <malloc.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include <linux/errno.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /**
26*4882a593Smuzhiyun  * check_short_pattern - [GENERIC] check if a pattern is in the buffer
27*4882a593Smuzhiyun  * @param buf		the buffer to search
28*4882a593Smuzhiyun  * @param len		the length of buffer to search
29*4882a593Smuzhiyun  * @param paglen	the pagelength
30*4882a593Smuzhiyun  * @param td		search pattern descriptor
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * Check for a pattern at the given place. Used to search bad block
33*4882a593Smuzhiyun  * tables and good / bad block identifiers. Same as check_pattern, but
34*4882a593Smuzhiyun  * no optional empty check and the pattern is expected to start
35*4882a593Smuzhiyun  * at offset 0.
36*4882a593Smuzhiyun  */
check_short_pattern(uint8_t * buf,int len,int paglen,struct nand_bbt_descr * td)37*4882a593Smuzhiyun static int check_short_pattern(uint8_t * buf, int len, int paglen,
38*4882a593Smuzhiyun 			       struct nand_bbt_descr *td)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	int i;
41*4882a593Smuzhiyun 	uint8_t *p = buf;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	/* Compare the pattern */
44*4882a593Smuzhiyun 	for (i = 0; i < td->len; i++) {
45*4882a593Smuzhiyun 		if (p[i] != td->pattern[i])
46*4882a593Smuzhiyun 			return -1;
47*4882a593Smuzhiyun 	}
48*4882a593Smuzhiyun 	return 0;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /**
52*4882a593Smuzhiyun  * create_bbt - [GENERIC] Create a bad block table by scanning the device
53*4882a593Smuzhiyun  * @param mtd		MTD device structure
54*4882a593Smuzhiyun  * @param buf		temporary buffer
55*4882a593Smuzhiyun  * @param bd		descriptor for the good/bad block search pattern
56*4882a593Smuzhiyun  * @param chip		create the table for a specific chip, -1 read all chips.
57*4882a593Smuzhiyun  *              Applies only if NAND_BBT_PERCHIP option is set
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * Create a bad block table by scanning the device
60*4882a593Smuzhiyun  * for the given good/bad block identify pattern
61*4882a593Smuzhiyun  */
create_bbt(struct mtd_info * mtd,uint8_t * buf,struct nand_bbt_descr * bd,int chip)62*4882a593Smuzhiyun static int create_bbt(struct mtd_info *mtd, uint8_t * buf,
63*4882a593Smuzhiyun 		      struct nand_bbt_descr *bd, int chip)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	struct onenand_chip *this = mtd->priv;
66*4882a593Smuzhiyun 	struct bbm_info *bbm = this->bbm;
67*4882a593Smuzhiyun 	int i, j, numblocks, len, scanlen;
68*4882a593Smuzhiyun 	int startblock;
69*4882a593Smuzhiyun 	loff_t from;
70*4882a593Smuzhiyun 	size_t readlen, ooblen;
71*4882a593Smuzhiyun 	struct mtd_oob_ops ops;
72*4882a593Smuzhiyun 	int rgn;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	printk(KERN_INFO "Scanning device for bad blocks\n");
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	len = 1;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	/* We need only read few bytes from the OOB area */
79*4882a593Smuzhiyun 	scanlen = ooblen = 0;
80*4882a593Smuzhiyun 	readlen = bd->len;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	/* chip == -1 case only */
83*4882a593Smuzhiyun 	/* Note that numblocks is 2 * (real numblocks) here;
84*4882a593Smuzhiyun 	 * see i += 2 below as it makses shifting and masking less painful
85*4882a593Smuzhiyun 	 */
86*4882a593Smuzhiyun 	numblocks = this->chipsize >> (bbm->bbt_erase_shift - 1);
87*4882a593Smuzhiyun 	startblock = 0;
88*4882a593Smuzhiyun 	from = 0;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	ops.mode = MTD_OPS_PLACE_OOB;
91*4882a593Smuzhiyun 	ops.ooblen = readlen;
92*4882a593Smuzhiyun 	ops.oobbuf = buf;
93*4882a593Smuzhiyun 	ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	for (i = startblock; i < numblocks;) {
96*4882a593Smuzhiyun 		int ret;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 		for (j = 0; j < len; j++) {
99*4882a593Smuzhiyun 			/* No need to read pages fully,
100*4882a593Smuzhiyun 			 * just read required OOB bytes */
101*4882a593Smuzhiyun 			ret = onenand_bbt_read_oob(mtd,
102*4882a593Smuzhiyun 					     from + j * mtd->writesize +
103*4882a593Smuzhiyun 					     bd->offs, &ops);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 			/* If it is a initial bad block, just ignore it */
106*4882a593Smuzhiyun 			if (ret == ONENAND_BBT_READ_FATAL_ERROR)
107*4882a593Smuzhiyun 				return -EIO;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 			if (ret || check_short_pattern
110*4882a593Smuzhiyun 			    (&buf[j * scanlen], scanlen, mtd->writesize, bd)) {
111*4882a593Smuzhiyun 				bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
112*4882a593Smuzhiyun 				printk(KERN_WARNING
113*4882a593Smuzhiyun 				       "Bad eraseblock %d at 0x%08x\n", i >> 1,
114*4882a593Smuzhiyun 				       (unsigned int)from);
115*4882a593Smuzhiyun 				break;
116*4882a593Smuzhiyun 			}
117*4882a593Smuzhiyun 		}
118*4882a593Smuzhiyun 		i += 2;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 		if (FLEXONENAND(this)) {
121*4882a593Smuzhiyun 			rgn = flexonenand_region(mtd, from);
122*4882a593Smuzhiyun 			from += mtd->eraseregions[rgn].erasesize;
123*4882a593Smuzhiyun 		} else
124*4882a593Smuzhiyun 			from += (1 << bbm->bbt_erase_shift);
125*4882a593Smuzhiyun 	}
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	return 0;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /**
131*4882a593Smuzhiyun  * onenand_memory_bbt - [GENERIC] create a memory based bad block table
132*4882a593Smuzhiyun  * @param mtd		MTD device structure
133*4882a593Smuzhiyun  * @param bd		descriptor for the good/bad block search pattern
134*4882a593Smuzhiyun  *
135*4882a593Smuzhiyun  * The function creates a memory based bbt by scanning the device
136*4882a593Smuzhiyun  * for manufacturer / software marked good / bad blocks
137*4882a593Smuzhiyun  */
onenand_memory_bbt(struct mtd_info * mtd,struct nand_bbt_descr * bd)138*4882a593Smuzhiyun static inline int onenand_memory_bbt(struct mtd_info *mtd,
139*4882a593Smuzhiyun 				     struct nand_bbt_descr *bd)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun 	unsigned char data_buf[MAX_ONENAND_PAGESIZE];
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	return create_bbt(mtd, data_buf, bd, -1);
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun /**
147*4882a593Smuzhiyun  * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad
148*4882a593Smuzhiyun  * @param mtd		MTD device structure
149*4882a593Smuzhiyun  * @param offs		offset in the device
150*4882a593Smuzhiyun  * @param allowbbt	allow access to bad block table region
151*4882a593Smuzhiyun  */
onenand_isbad_bbt(struct mtd_info * mtd,loff_t offs,int allowbbt)152*4882a593Smuzhiyun static int onenand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	struct onenand_chip *this = mtd->priv;
155*4882a593Smuzhiyun 	struct bbm_info *bbm = this->bbm;
156*4882a593Smuzhiyun 	int block;
157*4882a593Smuzhiyun 	uint8_t res;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	/* Get block number * 2 */
160*4882a593Smuzhiyun 	block = (int) (onenand_block(this, offs) << 1);
161*4882a593Smuzhiyun 	res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	pr_debug("onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
164*4882a593Smuzhiyun 		 (unsigned int)offs, block >> 1, res);
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	switch ((int)res) {
167*4882a593Smuzhiyun 	case 0x00:
168*4882a593Smuzhiyun 		return 0;
169*4882a593Smuzhiyun 	case 0x01:
170*4882a593Smuzhiyun 		return 1;
171*4882a593Smuzhiyun 	case 0x02:
172*4882a593Smuzhiyun 		return allowbbt ? 0 : 1;
173*4882a593Smuzhiyun 	}
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	return 1;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun /**
179*4882a593Smuzhiyun  * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
180*4882a593Smuzhiyun  * @param mtd		MTD device structure
181*4882a593Smuzhiyun  * @param bd		descriptor for the good/bad block search pattern
182*4882a593Smuzhiyun  *
183*4882a593Smuzhiyun  * The function checks, if a bad block table(s) is/are already
184*4882a593Smuzhiyun  * available. If not it scans the device for manufacturer
185*4882a593Smuzhiyun  * marked good / bad blocks and writes the bad block table(s) to
186*4882a593Smuzhiyun  * the selected place.
187*4882a593Smuzhiyun  *
188*4882a593Smuzhiyun  * The bad block table memory is allocated here. It must be freed
189*4882a593Smuzhiyun  * by calling the onenand_free_bbt function.
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  */
onenand_scan_bbt(struct mtd_info * mtd,struct nand_bbt_descr * bd)192*4882a593Smuzhiyun int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun 	struct onenand_chip *this = mtd->priv;
195*4882a593Smuzhiyun 	struct bbm_info *bbm = this->bbm;
196*4882a593Smuzhiyun 	int len, ret = 0;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	len = this->chipsize >> (this->erase_shift + 2);
199*4882a593Smuzhiyun 	/* Allocate memory (2bit per block) */
200*4882a593Smuzhiyun 	bbm->bbt = malloc(len);
201*4882a593Smuzhiyun 	if (!bbm->bbt)
202*4882a593Smuzhiyun 		return -ENOMEM;
203*4882a593Smuzhiyun 	/* Clear the memory bad block table */
204*4882a593Smuzhiyun 	memset(bbm->bbt, 0x00, len);
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	/* Set the bad block position */
207*4882a593Smuzhiyun 	bbm->badblockpos = ONENAND_BADBLOCK_POS;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	/* Set erase shift */
210*4882a593Smuzhiyun 	bbm->bbt_erase_shift = this->erase_shift;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	if (!bbm->isbad_bbt)
213*4882a593Smuzhiyun 		bbm->isbad_bbt = onenand_isbad_bbt;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	/* Scan the device to build a memory based bad block table */
216*4882a593Smuzhiyun 	if ((ret = onenand_memory_bbt(mtd, bd))) {
217*4882a593Smuzhiyun 		printk(KERN_ERR
218*4882a593Smuzhiyun 		       "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
219*4882a593Smuzhiyun 		free(bbm->bbt);
220*4882a593Smuzhiyun 		bbm->bbt = NULL;
221*4882a593Smuzhiyun 	}
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	return ret;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun  * Define some generic bad / good block scan pattern which are used
228*4882a593Smuzhiyun  * while scanning a device for factory marked good / bad blocks.
229*4882a593Smuzhiyun  */
230*4882a593Smuzhiyun static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun static struct nand_bbt_descr largepage_memorybased = {
233*4882a593Smuzhiyun 	.options = 0,
234*4882a593Smuzhiyun 	.offs = 0,
235*4882a593Smuzhiyun 	.len = 2,
236*4882a593Smuzhiyun 	.pattern = scan_ff_pattern,
237*4882a593Smuzhiyun };
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun /**
240*4882a593Smuzhiyun  * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
241*4882a593Smuzhiyun  * @param mtd		MTD device structure
242*4882a593Smuzhiyun  *
243*4882a593Smuzhiyun  * This function selects the default bad block table
244*4882a593Smuzhiyun  * support for the device and calls the onenand_scan_bbt function
245*4882a593Smuzhiyun  */
onenand_default_bbt(struct mtd_info * mtd)246*4882a593Smuzhiyun int onenand_default_bbt(struct mtd_info *mtd)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	struct onenand_chip *this = mtd->priv;
249*4882a593Smuzhiyun 	struct bbm_info *bbm;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	this->bbm = malloc(sizeof(struct bbm_info));
252*4882a593Smuzhiyun 	if (!this->bbm)
253*4882a593Smuzhiyun 		return -ENOMEM;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	bbm = this->bbm;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	memset(bbm, 0, sizeof(struct bbm_info));
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	/* 1KB page has same configuration as 2KB page */
260*4882a593Smuzhiyun 	if (!bbm->badblock_pattern)
261*4882a593Smuzhiyun 		bbm->badblock_pattern = &largepage_memorybased;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	return onenand_scan_bbt(mtd, bbm->badblock_pattern);
264*4882a593Smuzhiyun }
265