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