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