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