Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * drivers/mtd/nand_bbt.c | |
3 | * | |
4 | * Overview: | |
5 | * Bad block table support for the NAND driver | |
6 | * | |
7 | * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de) | |
8 | * | |
eeada24d | 9 | * $Id: nand_bbt.c,v 1.30 2005/02/11 10:14:12 dedekind Exp $ |
1da177e4 LT |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License version 2 as | |
13 | * published by the Free Software Foundation. | |
14 | * | |
15 | * Description: | |
16 | * | |
17 | * When nand_scan_bbt is called, then it tries to find the bad block table | |
18 | * depending on the options in the bbt descriptor(s). If a bbt is found | |
19 | * then the contents are read and the memory based bbt is created. If a | |
20 | * mirrored bbt is selected then the mirror is searched too and the | |
21 | * versions are compared. If the mirror has a greater version number | |
22 | * than the mirror bbt is used to build the memory based bbt. | |
23 | * If the tables are not versioned, then we "or" the bad block information. | |
24 | * If one of the bbt's is out of date or does not exist it is (re)created. | |
25 | * If no bbt exists at all then the device is scanned for factory marked | |
26 | * good / bad blocks and the bad block tables are created. | |
27 | * | |
28 | * For manufacturer created bbts like the one found on M-SYS DOC devices | |
29 | * the bbt is searched and read but never created | |
30 | * | |
31 | * The autogenerated bad block table is located in the last good blocks | |
32 | * of the device. The table is mirrored, so it can be updated eventually. | |
33 | * The table is marked in the oob area with an ident pattern and a version | |
34 | * number which indicates which of both tables is more up to date. | |
35 | * | |
36 | * The table uses 2 bits per block | |
37 | * 11b: block is good | |
38 | * 00b: block is factory marked bad | |
39 | * 01b, 10b: block is marked bad due to wear | |
40 | * | |
41 | * The memory bad block table uses the following scheme: | |
42 | * 00b: block is good | |
43 | * 01b: block is marked bad due to wear | |
44 | * 10b: block is reserved (to protect the bbt area) | |
45 | * 11b: block is factory marked bad | |
46 | * | |
47 | * Multichip devices like DOC store the bad block info per floor. | |
48 | * | |
49 | * Following assumptions are made: | |
50 | * - bbts start at a page boundary, if autolocated on a block boundary | |
51 | * - the space neccecary for a bbt in FLASH does not exceed a block boundary | |
52 | * | |
53 | */ | |
54 | ||
55 | #include <linux/slab.h> | |
56 | #include <linux/types.h> | |
57 | #include <linux/mtd/mtd.h> | |
58 | #include <linux/mtd/nand.h> | |
59 | #include <linux/mtd/nand_ecc.h> | |
60 | #include <linux/mtd/compatmac.h> | |
61 | #include <linux/bitops.h> | |
62 | #include <linux/delay.h> | |
63 | ||
64 | ||
65 | /** | |
66 | * check_pattern - [GENERIC] check if a pattern is in the buffer | |
67 | * @buf: the buffer to search | |
68 | * @len: the length of buffer to search | |
69 | * @paglen: the pagelength | |
70 | * @td: search pattern descriptor | |
71 | * | |
72 | * Check for a pattern at the given place. Used to search bad block | |
73 | * tables and good / bad block identifiers. | |
74 | * If the SCAN_EMPTY option is set then check, if all bytes except the | |
75 | * pattern area contain 0xff | |
76 | * | |
77 | */ | |
78 | static int check_pattern (uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td) | |
79 | { | |
80 | int i, end; | |
81 | uint8_t *p = buf; | |
82 | ||
83 | end = paglen + td->offs; | |
84 | if (td->options & NAND_BBT_SCANEMPTY) { | |
85 | for (i = 0; i < end; i++) { | |
86 | if (p[i] != 0xff) | |
87 | return -1; | |
88 | } | |
89 | } | |
90 | p += end; | |
91 | ||
92 | /* Compare the pattern */ | |
93 | for (i = 0; i < td->len; i++) { | |
94 | if (p[i] != td->pattern[i]) | |
95 | return -1; | |
96 | } | |
97 | ||
98 | p += td->len; | |
99 | end += td->len; | |
100 | if (td->options & NAND_BBT_SCANEMPTY) { | |
101 | for (i = end; i < len; i++) { | |
102 | if (*p++ != 0xff) | |
103 | return -1; | |
104 | } | |
105 | } | |
106 | return 0; | |
107 | } | |
108 | ||
109 | /** | |
110 | * read_bbt - [GENERIC] Read the bad block table starting from page | |
111 | * @mtd: MTD device structure | |
112 | * @buf: temporary buffer | |
113 | * @page: the starting page | |
114 | * @num: the number of bbt descriptors to read | |
115 | * @bits: number of bits per block | |
116 | * @offs: offset in the memory table | |
117 | * @reserved_block_code: Pattern to identify reserved blocks | |
118 | * | |
119 | * Read the bad block table starting from page. | |
120 | * | |
121 | */ | |
122 | static int read_bbt (struct mtd_info *mtd, uint8_t *buf, int page, int num, | |
123 | int bits, int offs, int reserved_block_code) | |
124 | { | |
125 | int res, i, j, act = 0; | |
126 | struct nand_chip *this = mtd->priv; | |
127 | size_t retlen, len, totlen; | |
128 | loff_t from; | |
129 | uint8_t msk = (uint8_t) ((1 << bits) - 1); | |
130 | ||
131 | totlen = (num * bits) >> 3; | |
132 | from = ((loff_t)page) << this->page_shift; | |
133 | ||
134 | while (totlen) { | |
135 | len = min (totlen, (size_t) (1 << this->bbt_erase_shift)); | |
136 | res = mtd->read_ecc (mtd, from, len, &retlen, buf, NULL, this->autooob); | |
137 | if (res < 0) { | |
138 | if (retlen != len) { | |
139 | printk (KERN_INFO "nand_bbt: Error reading bad block table\n"); | |
140 | return res; | |
141 | } | |
142 | printk (KERN_WARNING "nand_bbt: ECC error while reading bad block table\n"); | |
143 | } | |
144 | ||
145 | /* Analyse data */ | |
146 | for (i = 0; i < len; i++) { | |
147 | uint8_t dat = buf[i]; | |
148 | for (j = 0; j < 8; j += bits, act += 2) { | |
149 | uint8_t tmp = (dat >> j) & msk; | |
150 | if (tmp == msk) | |
151 | continue; | |
152 | if (reserved_block_code && | |
153 | (tmp == reserved_block_code)) { | |
154 | printk (KERN_DEBUG "nand_read_bbt: Reserved block at 0x%08x\n", | |
155 | ((offs << 2) + (act >> 1)) << this->bbt_erase_shift); | |
156 | this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06); | |
157 | continue; | |
158 | } | |
159 | /* Leave it for now, if its matured we can move this | |
160 | * message to MTD_DEBUG_LEVEL0 */ | |
161 | printk (KERN_DEBUG "nand_read_bbt: Bad block at 0x%08x\n", | |
162 | ((offs << 2) + (act >> 1)) << this->bbt_erase_shift); | |
163 | /* Factory marked bad or worn out ? */ | |
164 | if (tmp == 0) | |
165 | this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06); | |
166 | else | |
167 | this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06); | |
168 | } | |
169 | } | |
170 | totlen -= len; | |
171 | from += len; | |
172 | } | |
173 | return 0; | |
174 | } | |
175 | ||
176 | /** | |
177 | * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page | |
178 | * @mtd: MTD device structure | |
179 | * @buf: temporary buffer | |
180 | * @td: descriptor for the bad block table | |
181 | * @chip: read the table for a specific chip, -1 read all chips. | |
182 | * Applies only if NAND_BBT_PERCHIP option is set | |
183 | * | |
184 | * Read the bad block table for all chips starting at a given page | |
185 | * We assume that the bbt bits are in consecutive order. | |
186 | */ | |
187 | static int read_abs_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip) | |
188 | { | |
189 | struct nand_chip *this = mtd->priv; | |
190 | int res = 0, i; | |
191 | int bits; | |
192 | ||
193 | bits = td->options & NAND_BBT_NRBITS_MSK; | |
194 | if (td->options & NAND_BBT_PERCHIP) { | |
195 | int offs = 0; | |
196 | for (i = 0; i < this->numchips; i++) { | |
197 | if (chip == -1 || chip == i) | |
198 | res = read_bbt (mtd, buf, td->pages[i], this->chipsize >> this->bbt_erase_shift, bits, offs, td->reserved_block_code); | |
199 | if (res) | |
200 | return res; | |
201 | offs += this->chipsize >> (this->bbt_erase_shift + 2); | |
202 | } | |
203 | } else { | |
204 | res = read_bbt (mtd, buf, td->pages[0], mtd->size >> this->bbt_erase_shift, bits, 0, td->reserved_block_code); | |
205 | if (res) | |
206 | return res; | |
207 | } | |
208 | return 0; | |
209 | } | |
210 | ||
211 | /** | |
212 | * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page | |
213 | * @mtd: MTD device structure | |
214 | * @buf: temporary buffer | |
215 | * @td: descriptor for the bad block table | |
216 | * @md: descriptor for the bad block table mirror | |
217 | * | |
218 | * Read the bad block table(s) for all chips starting at a given page | |
219 | * We assume that the bbt bits are in consecutive order. | |
220 | * | |
221 | */ | |
222 | static int read_abs_bbts (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, | |
223 | struct nand_bbt_descr *md) | |
224 | { | |
225 | struct nand_chip *this = mtd->priv; | |
226 | ||
227 | /* Read the primary version, if available */ | |
228 | if (td->options & NAND_BBT_VERSION) { | |
229 | nand_read_raw (mtd, buf, td->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize); | |
230 | td->version[0] = buf[mtd->oobblock + td->veroffs]; | |
231 | printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", td->pages[0], td->version[0]); | |
232 | } | |
233 | ||
234 | /* Read the mirror version, if available */ | |
235 | if (md && (md->options & NAND_BBT_VERSION)) { | |
236 | nand_read_raw (mtd, buf, md->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize); | |
237 | md->version[0] = buf[mtd->oobblock + md->veroffs]; | |
238 | printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", md->pages[0], md->version[0]); | |
239 | } | |
240 | ||
241 | return 1; | |
242 | } | |
243 | ||
244 | /** | |
245 | * create_bbt - [GENERIC] Create a bad block table by scanning the device | |
246 | * @mtd: MTD device structure | |
247 | * @buf: temporary buffer | |
248 | * @bd: descriptor for the good/bad block search pattern | |
249 | * @chip: create the table for a specific chip, -1 read all chips. | |
250 | * Applies only if NAND_BBT_PERCHIP option is set | |
251 | * | |
252 | * Create a bad block table by scanning the device | |
253 | * for the given good/bad block identify pattern | |
254 | */ | |
eeada24d | 255 | static int create_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip) |
1da177e4 LT |
256 | { |
257 | struct nand_chip *this = mtd->priv; | |
eeada24d | 258 | int i, j, numblocks, len, scanlen, pagelen; |
1da177e4 LT |
259 | int startblock; |
260 | loff_t from; | |
261 | size_t readlen, ooblen; | |
262 | ||
263 | printk (KERN_INFO "Scanning device for bad blocks\n"); | |
264 | ||
265 | if (bd->options & NAND_BBT_SCANALLPAGES) | |
266 | len = 1 << (this->bbt_erase_shift - this->page_shift); | |
267 | else { | |
268 | if (bd->options & NAND_BBT_SCAN2NDPAGE) | |
269 | len = 2; | |
270 | else | |
271 | len = 1; | |
272 | } | |
eeada24d AB |
273 | |
274 | if (bd->options == 0) { | |
275 | /* Memory-based BBT. We may read only needed bytes from the OOB area to | |
276 | * test if block is bad, no need to read the whole page content. */ | |
277 | scanlen = ooblen = pagelen = 0; | |
278 | readlen = bd->len; | |
279 | } else { | |
280 | scanlen = mtd->oobblock + mtd->oobsize; | |
281 | readlen = len * mtd->oobblock; | |
282 | ooblen = len * mtd->oobsize; | |
283 | pagelen = mtd->oobblock; | |
284 | } | |
1da177e4 LT |
285 | |
286 | if (chip == -1) { | |
287 | /* Note that numblocks is 2 * (real numblocks) here, see i+=2 below as it | |
288 | * makes shifting and masking less painful */ | |
289 | numblocks = mtd->size >> (this->bbt_erase_shift - 1); | |
290 | startblock = 0; | |
291 | from = 0; | |
292 | } else { | |
293 | if (chip >= this->numchips) { | |
294 | printk (KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n", | |
295 | chip + 1, this->numchips); | |
eeada24d | 296 | return -EINVAL; |
1da177e4 LT |
297 | } |
298 | numblocks = this->chipsize >> (this->bbt_erase_shift - 1); | |
299 | startblock = chip * numblocks; | |
300 | numblocks += startblock; | |
301 | from = startblock << (this->bbt_erase_shift - 1); | |
302 | } | |
303 | ||
304 | for (i = startblock; i < numblocks;) { | |
eeada24d AB |
305 | int ret; |
306 | ||
307 | if (bd->options == 0) { | |
308 | size_t retlen; | |
309 | if ((ret = mtd->read_oob(mtd, from + bd->offs, bd->len, &retlen, &buf[bd->offs]))) | |
310 | return ret; | |
311 | } else { | |
312 | if ((ret = nand_read_raw (mtd, buf, from, readlen, ooblen))) | |
313 | return ret; | |
314 | } | |
1da177e4 | 315 | for (j = 0; j < len; j++) { |
eeada24d | 316 | if (check_pattern (&buf[j * scanlen], scanlen, pagelen, bd)) { |
1da177e4 LT |
317 | this->bbt[i >> 3] |= 0x03 << (i & 0x6); |
318 | printk (KERN_WARNING "Bad eraseblock %d at 0x%08x\n", | |
319 | i >> 1, (unsigned int) from); | |
320 | break; | |
321 | } | |
322 | } | |
323 | i += 2; | |
324 | from += (1 << this->bbt_erase_shift); | |
325 | } | |
eeada24d | 326 | return 0; |
1da177e4 LT |
327 | } |
328 | ||
329 | /** | |
330 | * search_bbt - [GENERIC] scan the device for a specific bad block table | |
331 | * @mtd: MTD device structure | |
332 | * @buf: temporary buffer | |
333 | * @td: descriptor for the bad block table | |
334 | * | |
335 | * Read the bad block table by searching for a given ident pattern. | |
336 | * Search is preformed either from the beginning up or from the end of | |
337 | * the device downwards. The search starts always at the start of a | |
338 | * block. | |
339 | * If the option NAND_BBT_PERCHIP is given, each chip is searched | |
340 | * for a bbt, which contains the bad block information of this chip. | |
341 | * This is neccecary to provide support for certain DOC devices. | |
342 | * | |
343 | * The bbt ident pattern resides in the oob area of the first page | |
344 | * in a block. | |
345 | */ | |
346 | static int search_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td) | |
347 | { | |
348 | struct nand_chip *this = mtd->priv; | |
349 | int i, chips; | |
350 | int bits, startblock, block, dir; | |
351 | int scanlen = mtd->oobblock + mtd->oobsize; | |
352 | int bbtblocks; | |
353 | ||
354 | /* Search direction top -> down ? */ | |
355 | if (td->options & NAND_BBT_LASTBLOCK) { | |
356 | startblock = (mtd->size >> this->bbt_erase_shift) -1; | |
357 | dir = -1; | |
358 | } else { | |
359 | startblock = 0; | |
360 | dir = 1; | |
361 | } | |
362 | ||
363 | /* Do we have a bbt per chip ? */ | |
364 | if (td->options & NAND_BBT_PERCHIP) { | |
365 | chips = this->numchips; | |
366 | bbtblocks = this->chipsize >> this->bbt_erase_shift; | |
367 | startblock &= bbtblocks - 1; | |
368 | } else { | |
369 | chips = 1; | |
370 | bbtblocks = mtd->size >> this->bbt_erase_shift; | |
371 | } | |
372 | ||
373 | /* Number of bits for each erase block in the bbt */ | |
374 | bits = td->options & NAND_BBT_NRBITS_MSK; | |
375 | ||
376 | for (i = 0; i < chips; i++) { | |
377 | /* Reset version information */ | |
378 | td->version[i] = 0; | |
379 | td->pages[i] = -1; | |
380 | /* Scan the maximum number of blocks */ | |
381 | for (block = 0; block < td->maxblocks; block++) { | |
382 | int actblock = startblock + dir * block; | |
383 | /* Read first page */ | |
384 | nand_read_raw (mtd, buf, actblock << this->bbt_erase_shift, mtd->oobblock, mtd->oobsize); | |
385 | if (!check_pattern(buf, scanlen, mtd->oobblock, td)) { | |
386 | td->pages[i] = actblock << (this->bbt_erase_shift - this->page_shift); | |
387 | if (td->options & NAND_BBT_VERSION) { | |
388 | td->version[i] = buf[mtd->oobblock + td->veroffs]; | |
389 | } | |
390 | break; | |
391 | } | |
392 | } | |
393 | startblock += this->chipsize >> this->bbt_erase_shift; | |
394 | } | |
395 | /* Check, if we found a bbt for each requested chip */ | |
396 | for (i = 0; i < chips; i++) { | |
397 | if (td->pages[i] == -1) | |
398 | printk (KERN_WARNING "Bad block table not found for chip %d\n", i); | |
399 | else | |
400 | printk (KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i], td->version[i]); | |
401 | } | |
402 | return 0; | |
403 | } | |
404 | ||
405 | /** | |
406 | * search_read_bbts - [GENERIC] scan the device for bad block table(s) | |
407 | * @mtd: MTD device structure | |
408 | * @buf: temporary buffer | |
409 | * @td: descriptor for the bad block table | |
410 | * @md: descriptor for the bad block table mirror | |
411 | * | |
412 | * Search and read the bad block table(s) | |
413 | */ | |
414 | static int search_read_bbts (struct mtd_info *mtd, uint8_t *buf, | |
415 | struct nand_bbt_descr *td, struct nand_bbt_descr *md) | |
416 | { | |
417 | /* Search the primary table */ | |
418 | search_bbt (mtd, buf, td); | |
419 | ||
420 | /* Search the mirror table */ | |
421 | if (md) | |
422 | search_bbt (mtd, buf, md); | |
423 | ||
424 | /* Force result check */ | |
425 | return 1; | |
426 | } | |
427 | ||
428 | ||
429 | /** | |
430 | * write_bbt - [GENERIC] (Re)write the bad block table | |
431 | * | |
432 | * @mtd: MTD device structure | |
433 | * @buf: temporary buffer | |
434 | * @td: descriptor for the bad block table | |
435 | * @md: descriptor for the bad block table mirror | |
436 | * @chipsel: selector for a specific chip, -1 for all | |
437 | * | |
438 | * (Re)write the bad block table | |
439 | * | |
440 | */ | |
441 | static int write_bbt (struct mtd_info *mtd, uint8_t *buf, | |
442 | struct nand_bbt_descr *td, struct nand_bbt_descr *md, int chipsel) | |
443 | { | |
444 | struct nand_chip *this = mtd->priv; | |
445 | struct nand_oobinfo oobinfo; | |
446 | struct erase_info einfo; | |
447 | int i, j, res, chip = 0; | |
448 | int bits, startblock, dir, page, offs, numblocks, sft, sftmsk; | |
449 | int nrchips, bbtoffs, pageoffs; | |
450 | uint8_t msk[4]; | |
451 | uint8_t rcode = td->reserved_block_code; | |
452 | size_t retlen, len = 0; | |
453 | loff_t to; | |
454 | ||
455 | if (!rcode) | |
456 | rcode = 0xff; | |
457 | /* Write bad block table per chip rather than per device ? */ | |
458 | if (td->options & NAND_BBT_PERCHIP) { | |
459 | numblocks = (int) (this->chipsize >> this->bbt_erase_shift); | |
460 | /* Full device write or specific chip ? */ | |
461 | if (chipsel == -1) { | |
462 | nrchips = this->numchips; | |
463 | } else { | |
464 | nrchips = chipsel + 1; | |
465 | chip = chipsel; | |
466 | } | |
467 | } else { | |
468 | numblocks = (int) (mtd->size >> this->bbt_erase_shift); | |
469 | nrchips = 1; | |
470 | } | |
471 | ||
472 | /* Loop through the chips */ | |
473 | for (; chip < nrchips; chip++) { | |
474 | ||
475 | /* There was already a version of the table, reuse the page | |
476 | * This applies for absolute placement too, as we have the | |
477 | * page nr. in td->pages. | |
478 | */ | |
479 | if (td->pages[chip] != -1) { | |
480 | page = td->pages[chip]; | |
481 | goto write; | |
482 | } | |
483 | ||
484 | /* Automatic placement of the bad block table */ | |
485 | /* Search direction top -> down ? */ | |
486 | if (td->options & NAND_BBT_LASTBLOCK) { | |
487 | startblock = numblocks * (chip + 1) - 1; | |
488 | dir = -1; | |
489 | } else { | |
490 | startblock = chip * numblocks; | |
491 | dir = 1; | |
492 | } | |
493 | ||
494 | for (i = 0; i < td->maxblocks; i++) { | |
495 | int block = startblock + dir * i; | |
496 | /* Check, if the block is bad */ | |
497 | switch ((this->bbt[block >> 2] >> (2 * (block & 0x03))) & 0x03) { | |
498 | case 0x01: | |
499 | case 0x03: | |
500 | continue; | |
501 | } | |
502 | page = block << (this->bbt_erase_shift - this->page_shift); | |
503 | /* Check, if the block is used by the mirror table */ | |
504 | if (!md || md->pages[chip] != page) | |
505 | goto write; | |
506 | } | |
507 | printk (KERN_ERR "No space left to write bad block table\n"); | |
508 | return -ENOSPC; | |
509 | write: | |
510 | ||
511 | /* Set up shift count and masks for the flash table */ | |
512 | bits = td->options & NAND_BBT_NRBITS_MSK; | |
513 | switch (bits) { | |
514 | case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x01; break; | |
515 | case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x03; break; | |
516 | case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C; msk[2] = ~rcode; msk[3] = 0x0f; break; | |
517 | case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F; msk[2] = ~rcode; msk[3] = 0xff; break; | |
518 | default: return -EINVAL; | |
519 | } | |
520 | ||
521 | bbtoffs = chip * (numblocks >> 2); | |
522 | ||
523 | to = ((loff_t) page) << this->page_shift; | |
524 | ||
525 | memcpy (&oobinfo, this->autooob, sizeof(oobinfo)); | |
526 | oobinfo.useecc = MTD_NANDECC_PLACEONLY; | |
527 | ||
528 | /* Must we save the block contents ? */ | |
529 | if (td->options & NAND_BBT_SAVECONTENT) { | |
530 | /* Make it block aligned */ | |
531 | to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1)); | |
532 | len = 1 << this->bbt_erase_shift; | |
533 | res = mtd->read_ecc (mtd, to, len, &retlen, buf, &buf[len], &oobinfo); | |
534 | if (res < 0) { | |
535 | if (retlen != len) { | |
536 | printk (KERN_INFO "nand_bbt: Error reading block for writing the bad block table\n"); | |
537 | return res; | |
538 | } | |
539 | printk (KERN_WARNING "nand_bbt: ECC error while reading block for writing bad block table\n"); | |
540 | } | |
541 | /* Calc the byte offset in the buffer */ | |
542 | pageoffs = page - (int)(to >> this->page_shift); | |
543 | offs = pageoffs << this->page_shift; | |
544 | /* Preset the bbt area with 0xff */ | |
545 | memset (&buf[offs], 0xff, (size_t)(numblocks >> sft)); | |
546 | /* Preset the bbt's oob area with 0xff */ | |
547 | memset (&buf[len + pageoffs * mtd->oobsize], 0xff, | |
548 | ((len >> this->page_shift) - pageoffs) * mtd->oobsize); | |
549 | if (td->options & NAND_BBT_VERSION) { | |
550 | buf[len + (pageoffs * mtd->oobsize) + td->veroffs] = td->version[chip]; | |
551 | } | |
552 | } else { | |
553 | /* Calc length */ | |
554 | len = (size_t) (numblocks >> sft); | |
555 | /* Make it page aligned ! */ | |
556 | len = (len + (mtd->oobblock-1)) & ~(mtd->oobblock-1); | |
557 | /* Preset the buffer with 0xff */ | |
558 | memset (buf, 0xff, len + (len >> this->page_shift) * mtd->oobsize); | |
559 | offs = 0; | |
560 | /* Pattern is located in oob area of first page */ | |
561 | memcpy (&buf[len + td->offs], td->pattern, td->len); | |
562 | if (td->options & NAND_BBT_VERSION) { | |
563 | buf[len + td->veroffs] = td->version[chip]; | |
564 | } | |
565 | } | |
566 | ||
567 | /* walk through the memory table */ | |
568 | for (i = 0; i < numblocks; ) { | |
569 | uint8_t dat; | |
570 | dat = this->bbt[bbtoffs + (i >> 2)]; | |
571 | for (j = 0; j < 4; j++ , i++) { | |
572 | int sftcnt = (i << (3 - sft)) & sftmsk; | |
573 | /* Do not store the reserved bbt blocks ! */ | |
574 | buf[offs + (i >> sft)] &= ~(msk[dat & 0x03] << sftcnt); | |
575 | dat >>= 2; | |
576 | } | |
577 | } | |
578 | ||
579 | memset (&einfo, 0, sizeof (einfo)); | |
580 | einfo.mtd = mtd; | |
581 | einfo.addr = (unsigned long) to; | |
582 | einfo.len = 1 << this->bbt_erase_shift; | |
583 | res = nand_erase_nand (mtd, &einfo, 1); | |
584 | if (res < 0) { | |
585 | printk (KERN_WARNING "nand_bbt: Error during block erase: %d\n", res); | |
586 | return res; | |
587 | } | |
588 | ||
589 | res = mtd->write_ecc (mtd, to, len, &retlen, buf, &buf[len], &oobinfo); | |
590 | if (res < 0) { | |
591 | printk (KERN_WARNING "nand_bbt: Error while writing bad block table %d\n", res); | |
592 | return res; | |
593 | } | |
594 | printk (KERN_DEBUG "Bad block table written to 0x%08x, version 0x%02X\n", | |
595 | (unsigned int) to, td->version[chip]); | |
596 | ||
597 | /* Mark it as used */ | |
598 | td->pages[chip] = page; | |
599 | } | |
600 | return 0; | |
601 | } | |
602 | ||
603 | /** | |
604 | * nand_memory_bbt - [GENERIC] create a memory based bad block table | |
605 | * @mtd: MTD device structure | |
606 | * @bd: descriptor for the good/bad block search pattern | |
607 | * | |
608 | * The function creates a memory based bbt by scanning the device | |
609 | * for manufacturer / software marked good / bad blocks | |
610 | */ | |
611 | static int nand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd) | |
612 | { | |
613 | struct nand_chip *this = mtd->priv; | |
614 | ||
615 | /* Ensure that we only scan for the pattern and nothing else */ | |
616 | bd->options = 0; | |
eeada24d | 617 | return create_bbt (mtd, this->data_buf, bd, -1); |
1da177e4 LT |
618 | } |
619 | ||
620 | /** | |
621 | * check_create - [GENERIC] create and write bbt(s) if neccecary | |
622 | * @mtd: MTD device structure | |
623 | * @buf: temporary buffer | |
624 | * @bd: descriptor for the good/bad block search pattern | |
625 | * | |
626 | * The function checks the results of the previous call to read_bbt | |
627 | * and creates / updates the bbt(s) if neccecary | |
628 | * Creation is neccecary if no bbt was found for the chip/device | |
629 | * Update is neccecary if one of the tables is missing or the | |
630 | * version nr. of one table is less than the other | |
631 | */ | |
632 | static int check_create (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd) | |
633 | { | |
634 | int i, chips, writeops, chipsel, res; | |
635 | struct nand_chip *this = mtd->priv; | |
636 | struct nand_bbt_descr *td = this->bbt_td; | |
637 | struct nand_bbt_descr *md = this->bbt_md; | |
638 | struct nand_bbt_descr *rd, *rd2; | |
639 | ||
640 | /* Do we have a bbt per chip ? */ | |
641 | if (td->options & NAND_BBT_PERCHIP) | |
642 | chips = this->numchips; | |
643 | else | |
644 | chips = 1; | |
645 | ||
646 | for (i = 0; i < chips; i++) { | |
647 | writeops = 0; | |
648 | rd = NULL; | |
649 | rd2 = NULL; | |
650 | /* Per chip or per device ? */ | |
651 | chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1; | |
652 | /* Mirrored table avilable ? */ | |
653 | if (md) { | |
654 | if (td->pages[i] == -1 && md->pages[i] == -1) { | |
655 | writeops = 0x03; | |
656 | goto create; | |
657 | } | |
658 | ||
659 | if (td->pages[i] == -1) { | |
660 | rd = md; | |
661 | td->version[i] = md->version[i]; | |
662 | writeops = 1; | |
663 | goto writecheck; | |
664 | } | |
665 | ||
666 | if (md->pages[i] == -1) { | |
667 | rd = td; | |
668 | md->version[i] = td->version[i]; | |
669 | writeops = 2; | |
670 | goto writecheck; | |
671 | } | |
672 | ||
673 | if (td->version[i] == md->version[i]) { | |
674 | rd = td; | |
675 | if (!(td->options & NAND_BBT_VERSION)) | |
676 | rd2 = md; | |
677 | goto writecheck; | |
678 | } | |
679 | ||
680 | if (((int8_t) (td->version[i] - md->version[i])) > 0) { | |
681 | rd = td; | |
682 | md->version[i] = td->version[i]; | |
683 | writeops = 2; | |
684 | } else { | |
685 | rd = md; | |
686 | td->version[i] = md->version[i]; | |
687 | writeops = 1; | |
688 | } | |
689 | ||
690 | goto writecheck; | |
691 | ||
692 | } else { | |
693 | if (td->pages[i] == -1) { | |
694 | writeops = 0x01; | |
695 | goto create; | |
696 | } | |
697 | rd = td; | |
698 | goto writecheck; | |
699 | } | |
700 | create: | |
701 | /* Create the bad block table by scanning the device ? */ | |
702 | if (!(td->options & NAND_BBT_CREATE)) | |
703 | continue; | |
704 | ||
705 | /* Create the table in memory by scanning the chip(s) */ | |
706 | create_bbt (mtd, buf, bd, chipsel); | |
707 | ||
708 | td->version[i] = 1; | |
709 | if (md) | |
710 | md->version[i] = 1; | |
711 | writecheck: | |
712 | /* read back first ? */ | |
713 | if (rd) | |
714 | read_abs_bbt (mtd, buf, rd, chipsel); | |
715 | /* If they weren't versioned, read both. */ | |
716 | if (rd2) | |
717 | read_abs_bbt (mtd, buf, rd2, chipsel); | |
718 | ||
719 | /* Write the bad block table to the device ? */ | |
720 | if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) { | |
721 | res = write_bbt (mtd, buf, td, md, chipsel); | |
722 | if (res < 0) | |
723 | return res; | |
724 | } | |
725 | ||
726 | /* Write the mirror bad block table to the device ? */ | |
727 | if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) { | |
728 | res = write_bbt (mtd, buf, md, td, chipsel); | |
729 | if (res < 0) | |
730 | return res; | |
731 | } | |
732 | } | |
733 | return 0; | |
734 | } | |
735 | ||
736 | /** | |
737 | * mark_bbt_regions - [GENERIC] mark the bad block table regions | |
738 | * @mtd: MTD device structure | |
739 | * @td: bad block table descriptor | |
740 | * | |
741 | * The bad block table regions are marked as "bad" to prevent | |
742 | * accidental erasures / writes. The regions are identified by | |
743 | * the mark 0x02. | |
744 | */ | |
745 | static void mark_bbt_region (struct mtd_info *mtd, struct nand_bbt_descr *td) | |
746 | { | |
747 | struct nand_chip *this = mtd->priv; | |
748 | int i, j, chips, block, nrblocks, update; | |
749 | uint8_t oldval, newval; | |
750 | ||
751 | /* Do we have a bbt per chip ? */ | |
752 | if (td->options & NAND_BBT_PERCHIP) { | |
753 | chips = this->numchips; | |
754 | nrblocks = (int)(this->chipsize >> this->bbt_erase_shift); | |
755 | } else { | |
756 | chips = 1; | |
757 | nrblocks = (int)(mtd->size >> this->bbt_erase_shift); | |
758 | } | |
759 | ||
760 | for (i = 0; i < chips; i++) { | |
761 | if ((td->options & NAND_BBT_ABSPAGE) || | |
762 | !(td->options & NAND_BBT_WRITE)) { | |
763 | if (td->pages[i] == -1) continue; | |
764 | block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift); | |
765 | block <<= 1; | |
766 | oldval = this->bbt[(block >> 3)]; | |
767 | newval = oldval | (0x2 << (block & 0x06)); | |
768 | this->bbt[(block >> 3)] = newval; | |
769 | if ((oldval != newval) && td->reserved_block_code) | |
770 | nand_update_bbt(mtd, block << (this->bbt_erase_shift - 1)); | |
771 | continue; | |
772 | } | |
773 | update = 0; | |
774 | if (td->options & NAND_BBT_LASTBLOCK) | |
775 | block = ((i + 1) * nrblocks) - td->maxblocks; | |
776 | else | |
777 | block = i * nrblocks; | |
778 | block <<= 1; | |
779 | for (j = 0; j < td->maxblocks; j++) { | |
780 | oldval = this->bbt[(block >> 3)]; | |
781 | newval = oldval | (0x2 << (block & 0x06)); | |
782 | this->bbt[(block >> 3)] = newval; | |
783 | if (oldval != newval) update = 1; | |
784 | block += 2; | |
785 | } | |
786 | /* If we want reserved blocks to be recorded to flash, and some | |
787 | new ones have been marked, then we need to update the stored | |
788 | bbts. This should only happen once. */ | |
789 | if (update && td->reserved_block_code) | |
790 | nand_update_bbt(mtd, (block - 2) << (this->bbt_erase_shift - 1)); | |
791 | } | |
792 | } | |
793 | ||
794 | /** | |
795 | * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s) | |
796 | * @mtd: MTD device structure | |
797 | * @bd: descriptor for the good/bad block search pattern | |
798 | * | |
799 | * The function checks, if a bad block table(s) is/are already | |
800 | * available. If not it scans the device for manufacturer | |
801 | * marked good / bad blocks and writes the bad block table(s) to | |
802 | * the selected place. | |
803 | * | |
804 | * The bad block table memory is allocated here. It must be freed | |
805 | * by calling the nand_free_bbt function. | |
806 | * | |
807 | */ | |
808 | int nand_scan_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd) | |
809 | { | |
810 | struct nand_chip *this = mtd->priv; | |
811 | int len, res = 0; | |
812 | uint8_t *buf; | |
813 | struct nand_bbt_descr *td = this->bbt_td; | |
814 | struct nand_bbt_descr *md = this->bbt_md; | |
815 | ||
816 | len = mtd->size >> (this->bbt_erase_shift + 2); | |
817 | /* Allocate memory (2bit per block) */ | |
818 | this->bbt = kmalloc (len, GFP_KERNEL); | |
819 | if (!this->bbt) { | |
820 | printk (KERN_ERR "nand_scan_bbt: Out of memory\n"); | |
821 | return -ENOMEM; | |
822 | } | |
823 | /* Clear the memory bad block table */ | |
824 | memset (this->bbt, 0x00, len); | |
825 | ||
826 | /* If no primary table decriptor is given, scan the device | |
827 | * to build a memory based bad block table | |
828 | */ | |
eeada24d AB |
829 | if (!td) { |
830 | if ((res = nand_memory_bbt(mtd, bd))) { | |
831 | printk (KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n"); | |
832 | kfree (this->bbt); | |
833 | this->bbt = NULL; | |
834 | } | |
835 | return res; | |
836 | } | |
1da177e4 LT |
837 | |
838 | /* Allocate a temporary buffer for one eraseblock incl. oob */ | |
839 | len = (1 << this->bbt_erase_shift); | |
840 | len += (len >> this->page_shift) * mtd->oobsize; | |
841 | buf = kmalloc (len, GFP_KERNEL); | |
842 | if (!buf) { | |
843 | printk (KERN_ERR "nand_bbt: Out of memory\n"); | |
844 | kfree (this->bbt); | |
845 | this->bbt = NULL; | |
846 | return -ENOMEM; | |
847 | } | |
848 | ||
849 | /* Is the bbt at a given page ? */ | |
850 | if (td->options & NAND_BBT_ABSPAGE) { | |
851 | res = read_abs_bbts (mtd, buf, td, md); | |
852 | } else { | |
853 | /* Search the bad block table using a pattern in oob */ | |
854 | res = search_read_bbts (mtd, buf, td, md); | |
855 | } | |
856 | ||
857 | if (res) | |
858 | res = check_create (mtd, buf, bd); | |
859 | ||
860 | /* Prevent the bbt regions from erasing / writing */ | |
861 | mark_bbt_region (mtd, td); | |
862 | if (md) | |
863 | mark_bbt_region (mtd, md); | |
864 | ||
865 | kfree (buf); | |
866 | return res; | |
867 | } | |
868 | ||
869 | ||
870 | /** | |
871 | * nand_update_bbt - [NAND Interface] update bad block table(s) | |
872 | * @mtd: MTD device structure | |
873 | * @offs: the offset of the newly marked block | |
874 | * | |
875 | * The function updates the bad block table(s) | |
876 | */ | |
877 | int nand_update_bbt (struct mtd_info *mtd, loff_t offs) | |
878 | { | |
879 | struct nand_chip *this = mtd->priv; | |
880 | int len, res = 0, writeops = 0; | |
881 | int chip, chipsel; | |
882 | uint8_t *buf; | |
883 | struct nand_bbt_descr *td = this->bbt_td; | |
884 | struct nand_bbt_descr *md = this->bbt_md; | |
885 | ||
886 | if (!this->bbt || !td) | |
887 | return -EINVAL; | |
888 | ||
889 | len = mtd->size >> (this->bbt_erase_shift + 2); | |
890 | /* Allocate a temporary buffer for one eraseblock incl. oob */ | |
891 | len = (1 << this->bbt_erase_shift); | |
892 | len += (len >> this->page_shift) * mtd->oobsize; | |
893 | buf = kmalloc (len, GFP_KERNEL); | |
894 | if (!buf) { | |
895 | printk (KERN_ERR "nand_update_bbt: Out of memory\n"); | |
896 | return -ENOMEM; | |
897 | } | |
898 | ||
899 | writeops = md != NULL ? 0x03 : 0x01; | |
900 | ||
901 | /* Do we have a bbt per chip ? */ | |
902 | if (td->options & NAND_BBT_PERCHIP) { | |
903 | chip = (int) (offs >> this->chip_shift); | |
904 | chipsel = chip; | |
905 | } else { | |
906 | chip = 0; | |
907 | chipsel = -1; | |
908 | } | |
909 | ||
910 | td->version[chip]++; | |
911 | if (md) | |
912 | md->version[chip]++; | |
913 | ||
914 | /* Write the bad block table to the device ? */ | |
915 | if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) { | |
916 | res = write_bbt (mtd, buf, td, md, chipsel); | |
917 | if (res < 0) | |
918 | goto out; | |
919 | } | |
920 | /* Write the mirror bad block table to the device ? */ | |
921 | if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) { | |
922 | res = write_bbt (mtd, buf, md, td, chipsel); | |
923 | } | |
924 | ||
925 | out: | |
926 | kfree (buf); | |
927 | return res; | |
928 | } | |
929 | ||
930 | /* Define some generic bad / good block scan pattern which are used | |
931 | * while scanning a device for factory marked good / bad blocks | |
932 | * | |
933 | * The memory based patterns just | |
934 | */ | |
935 | static uint8_t scan_ff_pattern[] = { 0xff, 0xff }; | |
936 | ||
937 | static struct nand_bbt_descr smallpage_memorybased = { | |
938 | .options = 0, | |
939 | .offs = 5, | |
940 | .len = 1, | |
941 | .pattern = scan_ff_pattern | |
942 | }; | |
943 | ||
944 | static struct nand_bbt_descr largepage_memorybased = { | |
945 | .options = 0, | |
946 | .offs = 0, | |
947 | .len = 2, | |
948 | .pattern = scan_ff_pattern | |
949 | }; | |
950 | ||
951 | static struct nand_bbt_descr smallpage_flashbased = { | |
952 | .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES, | |
953 | .offs = 5, | |
954 | .len = 1, | |
955 | .pattern = scan_ff_pattern | |
956 | }; | |
957 | ||
958 | static struct nand_bbt_descr largepage_flashbased = { | |
959 | .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES, | |
960 | .offs = 0, | |
961 | .len = 2, | |
962 | .pattern = scan_ff_pattern | |
963 | }; | |
964 | ||
965 | static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 }; | |
966 | ||
967 | static struct nand_bbt_descr agand_flashbased = { | |
968 | .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES, | |
969 | .offs = 0x20, | |
970 | .len = 6, | |
971 | .pattern = scan_agand_pattern | |
972 | }; | |
973 | ||
974 | /* Generic flash bbt decriptors | |
975 | */ | |
976 | static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' }; | |
977 | static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' }; | |
978 | ||
979 | static struct nand_bbt_descr bbt_main_descr = { | |
980 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE | |
981 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, | |
982 | .offs = 8, | |
983 | .len = 4, | |
984 | .veroffs = 12, | |
985 | .maxblocks = 4, | |
986 | .pattern = bbt_pattern | |
987 | }; | |
988 | ||
989 | static struct nand_bbt_descr bbt_mirror_descr = { | |
990 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE | |
991 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, | |
992 | .offs = 8, | |
993 | .len = 4, | |
994 | .veroffs = 12, | |
995 | .maxblocks = 4, | |
996 | .pattern = mirror_pattern | |
997 | }; | |
998 | ||
999 | /** | |
1000 | * nand_default_bbt - [NAND Interface] Select a default bad block table for the device | |
1001 | * @mtd: MTD device structure | |
1002 | * | |
1003 | * This function selects the default bad block table | |
1004 | * support for the device and calls the nand_scan_bbt function | |
1005 | * | |
1006 | */ | |
1007 | int nand_default_bbt (struct mtd_info *mtd) | |
1008 | { | |
1009 | struct nand_chip *this = mtd->priv; | |
1010 | ||
1011 | /* Default for AG-AND. We must use a flash based | |
1012 | * bad block table as the devices have factory marked | |
1013 | * _good_ blocks. Erasing those blocks leads to loss | |
1014 | * of the good / bad information, so we _must_ store | |
1015 | * this information in a good / bad table during | |
1016 | * startup | |
1017 | */ | |
1018 | if (this->options & NAND_IS_AND) { | |
1019 | /* Use the default pattern descriptors */ | |
1020 | if (!this->bbt_td) { | |
1021 | this->bbt_td = &bbt_main_descr; | |
1022 | this->bbt_md = &bbt_mirror_descr; | |
1023 | } | |
1024 | this->options |= NAND_USE_FLASH_BBT; | |
1025 | return nand_scan_bbt (mtd, &agand_flashbased); | |
1026 | } | |
1027 | ||
1028 | ||
1029 | /* Is a flash based bad block table requested ? */ | |
1030 | if (this->options & NAND_USE_FLASH_BBT) { | |
1031 | /* Use the default pattern descriptors */ | |
1032 | if (!this->bbt_td) { | |
1033 | this->bbt_td = &bbt_main_descr; | |
1034 | this->bbt_md = &bbt_mirror_descr; | |
1035 | } | |
1036 | if (!this->badblock_pattern) { | |
1037 | this->badblock_pattern = (mtd->oobblock > 512) ? | |
1038 | &largepage_flashbased : &smallpage_flashbased; | |
1039 | } | |
1040 | } else { | |
1041 | this->bbt_td = NULL; | |
1042 | this->bbt_md = NULL; | |
1043 | if (!this->badblock_pattern) { | |
1044 | this->badblock_pattern = (mtd->oobblock > 512) ? | |
1045 | &largepage_memorybased : &smallpage_memorybased; | |
1046 | } | |
1047 | } | |
1048 | return nand_scan_bbt (mtd, this->badblock_pattern); | |
1049 | } | |
1050 | ||
1051 | /** | |
1052 | * nand_isbad_bbt - [NAND Interface] Check if a block is bad | |
1053 | * @mtd: MTD device structure | |
1054 | * @offs: offset in the device | |
1055 | * @allowbbt: allow access to bad block table region | |
1056 | * | |
1057 | */ | |
1058 | int nand_isbad_bbt (struct mtd_info *mtd, loff_t offs, int allowbbt) | |
1059 | { | |
1060 | struct nand_chip *this = mtd->priv; | |
1061 | int block; | |
1062 | uint8_t res; | |
1063 | ||
1064 | /* Get block number * 2 */ | |
1065 | block = (int) (offs >> (this->bbt_erase_shift - 1)); | |
1066 | res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03; | |
1067 | ||
1068 | DEBUG (MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n", | |
eeada24d | 1069 | (unsigned int)offs, block >> 1, res); |
1da177e4 LT |
1070 | |
1071 | switch ((int)res) { | |
1072 | case 0x00: return 0; | |
1073 | case 0x01: return 1; | |
1074 | case 0x02: return allowbbt ? 0 : 1; | |
1075 | } | |
1076 | return 1; | |
1077 | } | |
1078 | ||
1079 | EXPORT_SYMBOL (nand_scan_bbt); | |
1080 | EXPORT_SYMBOL (nand_default_bbt); |