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