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