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