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