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