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