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