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