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