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