Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.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  * $Id: nand_bbt.c,v 1.36 2005/11/07 11:14:30 gleixner Exp $
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  *
15  * Description:
16  *
17  * When nand_scan_bbt is called, then it tries to find the bad block table
18  * depending on the options in the bbt descriptor(s). If a bbt is found
19  * then the contents are read and the memory based bbt is created. If a
20  * mirrored bbt is selected then the mirror is searched too and the
21  * versions are compared. If the mirror has a greater version number
22  * than the mirror bbt is used to build the memory based bbt.
23  * If the tables are not versioned, then we "or" the bad block information.
24  * If one of the bbt's is out of date or does not exist it is (re)created.
25  * If no bbt exists at all then the device is scanned for factory marked
26  * good / bad blocks and the bad block tables are created.
27  *
28  * For manufacturer created bbts like the one found on M-SYS DOC devices
29  * the bbt is searched and read but never created
30  *
31  * The autogenerated bad block table is located in the last good blocks
32  * of the device. The table is mirrored, so it can be updated eventually.
33  * The table is marked in the oob area with an ident pattern and a version
34  * number which indicates which of both tables is more up to date.
35  *
36  * The table uses 2 bits per block
37  * 11b:         block is good
38  * 00b:         block is factory marked bad
39  * 01b, 10b:    block is marked bad due to wear
40  *
41  * The memory bad block table uses the following scheme:
42  * 00b:         block is good
43  * 01b:         block is marked bad due to wear
44  * 10b:         block is reserved (to protect the bbt area)
45  * 11b:         block is factory marked bad
46  *
47  * Multichip devices like DOC store the bad block info per floor.
48  *
49  * Following assumptions are made:
50  * - bbts start at a page boundary, if autolocated on a block boundary
51  * - the space neccecary for a bbt in FLASH does not exceed a block boundary
52  *
53  */
54
55 #include <linux/slab.h>
56 #include <linux/types.h>
57 #include <linux/mtd/mtd.h>
58 #include <linux/mtd/nand.h>
59 #include <linux/mtd/nand_ecc.h>
60 #include <linux/mtd/compatmac.h>
61 #include <linux/bitops.h>
62 #include <linux/delay.h>
63
64
65 /**
66  * check_pattern - [GENERIC] check if a pattern is in the buffer
67  * @buf:        the buffer to search
68  * @len:        the length of buffer to search
69  * @paglen:     the pagelength
70  * @td:         search pattern descriptor
71  *
72  * Check for a pattern at the given place. Used to search bad block
73  * tables and good / bad block identifiers.
74  * If the SCAN_EMPTY option is set then check, if all bytes except the
75  * pattern area contain 0xff
76  *
77 */
78 static int check_pattern (uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
79 {
80         int i, end = 0;
81         uint8_t *p = buf;
82
83         end = paglen + td->offs;
84         if (td->options & NAND_BBT_SCANEMPTY) {
85                 for (i = 0; i < end; i++) {
86                         if (p[i] != 0xff)
87                                 return -1;
88                 }
89         }
90         p += end;
91
92         /* Compare the pattern */
93         for (i = 0; i < td->len; i++) {
94                 if (p[i] != td->pattern[i])
95                         return -1;
96         }
97
98         if (td->options & NAND_BBT_SCANEMPTY) {
99                 p += td->len;
100                 end += td->len;
101                 for (i = end; i < len; i++) {
102                         if (*p++ != 0xff)
103                                 return -1;
104                 }
105         }
106         return 0;
107 }
108
109 /**
110  * check_short_pattern - [GENERIC] check if a pattern is in the buffer
111  * @buf:        the buffer to search
112  * @td:         search pattern descriptor
113  *
114  * Check for a pattern at the given place. Used to search bad block
115  * tables and good / bad block identifiers. Same as check_pattern, but
116  * no optional empty check
117  *
118 */
119 static int check_short_pattern (uint8_t *buf, struct nand_bbt_descr *td)
120 {
121         int i;
122         uint8_t *p = buf;
123
124         /* Compare the pattern */
125         for (i = 0; i < td->len; i++) {
126                 if (p[td->offs + i] != td->pattern[i])
127                         return -1;
128         }
129         return 0;
130 }
131
132 /**
133  * read_bbt - [GENERIC] Read the bad block table starting from page
134  * @mtd:        MTD device structure
135  * @buf:        temporary buffer
136  * @page:       the starting page
137  * @num:        the number of bbt descriptors to read
138  * @bits:       number of bits per block
139  * @offs:       offset in the memory table
140  * @reserved_block_code:        Pattern to identify reserved blocks
141  *
142  * Read the bad block table starting from page.
143  *
144  */
145 static int read_bbt (struct mtd_info *mtd, uint8_t *buf, int page, int num,
146         int bits, int offs, int reserved_block_code)
147 {
148         int res, i, j, act = 0;
149         struct nand_chip *this = mtd->priv;
150         size_t retlen, len, totlen;
151         loff_t from;
152         uint8_t msk = (uint8_t) ((1 << bits) - 1);
153
154         totlen = (num * bits) >> 3;
155         from = ((loff_t)page) << this->page_shift;
156
157         while (totlen) {
158                 len = min (totlen, (size_t) (1 << this->bbt_erase_shift));
159                 res = mtd->read_ecc (mtd, from, len, &retlen, buf, NULL, this->autooob);
160                 if (res < 0) {
161                         if (retlen != len) {
162                                 printk (KERN_INFO "nand_bbt: Error reading bad block table\n");
163                                 return res;
164                         }
165                         printk (KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
166                 }
167
168                 /* Analyse data */
169                 for (i = 0; i < len; i++) {
170                         uint8_t dat = buf[i];
171                         for (j = 0; j < 8; j += bits, act += 2) {
172                                 uint8_t tmp = (dat >> j) & msk;
173                                 if (tmp == msk)
174                                         continue;
175                                 if (reserved_block_code &&
176                                     (tmp == reserved_block_code)) {
177                                         printk (KERN_DEBUG "nand_read_bbt: Reserved block at 0x%08x\n",
178                                                 ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
179                                         this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
180                                         continue;
181                                 }
182                                 /* Leave it for now, if its matured we can move this
183                                  * message to MTD_DEBUG_LEVEL0 */
184                                 printk (KERN_DEBUG "nand_read_bbt: Bad block at 0x%08x\n",
185                                         ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
186                                 /* Factory marked bad or worn out ? */
187                                 if (tmp == 0)
188                                         this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
189                                 else
190                                         this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
191                         }
192                 }
193                 totlen -= len;
194                 from += len;
195         }
196         return 0;
197 }
198
199 /**
200  * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
201  * @mtd:        MTD device structure
202  * @buf:        temporary buffer
203  * @td:         descriptor for the bad block table
204  * @chip:       read the table for a specific chip, -1 read all chips.
205  *              Applies only if NAND_BBT_PERCHIP option is set
206  *
207  * Read the bad block table for all chips starting at a given page
208  * We assume that the bbt bits are in consecutive order.
209 */
210 static int read_abs_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
211 {
212         struct nand_chip *this = mtd->priv;
213         int res = 0, i;
214         int bits;
215
216         bits = td->options & NAND_BBT_NRBITS_MSK;
217         if (td->options & NAND_BBT_PERCHIP) {
218                 int offs = 0;
219                 for (i = 0; i < this->numchips; i++) {
220                         if (chip == -1 || chip == i)
221                                 res = read_bbt (mtd, buf, td->pages[i], this->chipsize >> this->bbt_erase_shift, bits, offs, td->reserved_block_code);
222                         if (res)
223                                 return res;
224                         offs += this->chipsize >> (this->bbt_erase_shift + 2);
225                 }
226         } else {
227                 res = read_bbt (mtd, buf, td->pages[0], mtd->size >> this->bbt_erase_shift, bits, 0, td->reserved_block_code);
228                 if (res)
229                         return res;
230         }
231         return 0;
232 }
233
234 /**
235  * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
236  * @mtd:        MTD device structure
237  * @buf:        temporary buffer
238  * @td:         descriptor for the bad block table
239  * @md:         descriptor for the bad block table mirror
240  *
241  * Read the bad block table(s) for all chips starting at a given page
242  * We assume that the bbt bits are in consecutive order.
243  *
244 */
245 static int read_abs_bbts (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td,
246         struct nand_bbt_descr *md)
247 {
248         struct nand_chip *this = mtd->priv;
249
250         /* Read the primary version, if available */
251         if (td->options & NAND_BBT_VERSION) {
252                 nand_read_raw (mtd, buf, td->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize);
253                 td->version[0] = buf[mtd->oobblock + td->veroffs];
254                 printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", td->pages[0], td->version[0]);
255         }
256
257         /* Read the mirror version, if available */
258         if (md && (md->options & NAND_BBT_VERSION)) {
259                 nand_read_raw (mtd, buf, md->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize);
260                 md->version[0] = buf[mtd->oobblock + md->veroffs];
261                 printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", md->pages[0], md->version[0]);
262         }
263
264         return 1;
265 }
266
267 /**
268  * create_bbt - [GENERIC] Create a bad block table by scanning the device
269  * @mtd:        MTD device structure
270  * @buf:        temporary buffer
271  * @bd:         descriptor for the good/bad block search pattern
272  * @chip:       create the table for a specific chip, -1 read all chips.
273  *              Applies only if NAND_BBT_PERCHIP option is set
274  *
275  * Create a bad block table by scanning the device
276  * for the given good/bad block identify pattern
277  */
278 static int create_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip)
279 {
280         struct nand_chip *this = mtd->priv;
281         int i, j, numblocks, len, scanlen;
282         int startblock;
283         loff_t from;
284         size_t readlen, ooblen;
285
286         printk (KERN_INFO "Scanning device for bad blocks\n");
287
288         if (bd->options & NAND_BBT_SCANALLPAGES)
289                 len = 1 << (this->bbt_erase_shift - this->page_shift);
290         else {
291                 if (bd->options & NAND_BBT_SCAN2NDPAGE)
292                         len = 2;
293                 else
294                         len = 1;
295         }
296
297         if (!(bd->options & NAND_BBT_SCANEMPTY)) {
298                 /* We need only read few bytes from the OOB area */
299                 scanlen = ooblen = 0;
300                 readlen = bd->len;
301         } else {
302                 /* Full page content should be read */
303                 scanlen = mtd->oobblock + mtd->oobsize;
304                 readlen = len * mtd->oobblock;
305                 ooblen = len * mtd->oobsize;
306         }
307
308         if (chip == -1) {
309                 /* Note that numblocks is 2 * (real numblocks) here, see i+=2 below as it
310                  * makes shifting and masking less painful */
311                 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
312                 startblock = 0;
313                 from = 0;
314         } else {
315                 if (chip >= this->numchips) {
316                         printk (KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
317                                 chip + 1, this->numchips);
318                         return -EINVAL;
319                 }
320                 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
321                 startblock = chip * numblocks;
322                 numblocks += startblock;
323                 from = startblock << (this->bbt_erase_shift - 1);
324         }
325
326         for (i = startblock; i < numblocks;) {
327                 int ret;
328
329                 if (bd->options & NAND_BBT_SCANEMPTY)
330                         if ((ret = nand_read_raw (mtd, buf, from, readlen, ooblen)))
331                                 return ret;
332
333                 for (j = 0; j < len; j++) {
334                         if (!(bd->options & NAND_BBT_SCANEMPTY)) {
335                                 size_t retlen;
336
337                                 /* Read the full oob until read_oob is fixed to
338                                  * handle single byte reads for 16 bit buswidth */
339                                 ret = mtd->read_oob(mtd, from + j * mtd->oobblock,
340                                                         mtd->oobsize, &retlen, buf);
341                                 if (ret)
342                                         return ret;
343
344                                 if (check_short_pattern (buf, bd)) {
345                                         this->bbt[i >> 3] |= 0x03 << (i & 0x6);
346                                         printk (KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
347                                                 i >> 1, (unsigned int) from);
348                                         break;
349                                 }
350                         } else {
351                                 if (check_pattern (&buf[j * scanlen], scanlen, mtd->oobblock, bd)) {
352                                         this->bbt[i >> 3] |= 0x03 << (i & 0x6);
353                                         printk (KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
354                                                 i >> 1, (unsigned int) from);
355                                         break;
356                                 }
357                         }
358                 }
359                 i += 2;
360                 from += (1 << this->bbt_erase_shift);
361         }
362         return 0;
363 }
364
365 /**
366  * search_bbt - [GENERIC] scan the device for a specific bad block table
367  * @mtd:        MTD device structure
368  * @buf:        temporary buffer
369  * @td:         descriptor for the bad block table
370  *
371  * Read the bad block table by searching for a given ident pattern.
372  * Search is preformed either from the beginning up or from the end of
373  * the device downwards. The search starts always at the start of a
374  * block.
375  * If the option NAND_BBT_PERCHIP is given, each chip is searched
376  * for a bbt, which contains the bad block information of this chip.
377  * This is neccecary to provide support for certain DOC devices.
378  *
379  * The bbt ident pattern resides in the oob area of the first page
380  * in a block.
381  */
382 static int search_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
383 {
384         struct nand_chip *this = mtd->priv;
385         int i, chips;
386         int bits, startblock, block, dir;
387         int scanlen = mtd->oobblock + mtd->oobsize;
388         int bbtblocks;
389
390         /* Search direction top -> down ? */
391         if (td->options & NAND_BBT_LASTBLOCK) {
392                 startblock = (mtd->size >> this->bbt_erase_shift) -1;
393                 dir = -1;
394         } else {
395                 startblock = 0;
396                 dir = 1;
397         }
398
399         /* Do we have a bbt per chip ? */
400         if (td->options & NAND_BBT_PERCHIP) {
401                 chips = this->numchips;
402                 bbtblocks = this->chipsize >> this->bbt_erase_shift;
403                 startblock &= bbtblocks - 1;
404         } else {
405                 chips = 1;
406                 bbtblocks = mtd->size >> this->bbt_erase_shift;
407         }
408
409         /* Number of bits for each erase block in the bbt */
410         bits = td->options & NAND_BBT_NRBITS_MSK;
411
412         for (i = 0; i < chips; i++) {
413                 /* Reset version information */
414                 td->version[i] = 0;
415                 td->pages[i] = -1;
416                 /* Scan the maximum number of blocks */
417                 for (block = 0; block < td->maxblocks; block++) {
418                         int actblock = startblock + dir * block;
419                         /* Read first page */
420                         nand_read_raw (mtd, buf, actblock << this->bbt_erase_shift, mtd->oobblock, mtd->oobsize);
421                         if (!check_pattern(buf, scanlen, mtd->oobblock, td)) {
422                                 td->pages[i] = actblock << (this->bbt_erase_shift - this->page_shift);
423                                 if (td->options & NAND_BBT_VERSION) {
424                                         td->version[i] = buf[mtd->oobblock + td->veroffs];
425                                 }
426                                 break;
427                         }
428                 }
429                 startblock += this->chipsize >> this->bbt_erase_shift;
430         }
431         /* Check, if we found a bbt for each requested chip */
432         for (i = 0; i < chips; i++) {
433                 if (td->pages[i] == -1)
434                         printk (KERN_WARNING "Bad block table not found for chip %d\n", i);
435                 else
436                         printk (KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i], td->version[i]);
437         }
438         return 0;
439 }
440
441 /**
442  * search_read_bbts - [GENERIC] scan the device for bad block table(s)
443  * @mtd:        MTD device structure
444  * @buf:        temporary buffer
445  * @td:         descriptor for the bad block table
446  * @md:         descriptor for the bad block table mirror
447  *
448  * Search and read the bad block table(s)
449 */
450 static int search_read_bbts (struct mtd_info *mtd, uint8_t *buf,
451         struct nand_bbt_descr *td, struct nand_bbt_descr *md)
452 {
453         /* Search the primary table */
454         search_bbt (mtd, buf, td);
455
456         /* Search the mirror table */
457         if (md)
458                 search_bbt (mtd, buf, md);
459
460         /* Force result check */
461         return 1;
462 }
463
464
465 /**
466  * write_bbt - [GENERIC] (Re)write the bad block table
467  *
468  * @mtd:        MTD device structure
469  * @buf:        temporary buffer
470  * @td:         descriptor for the bad block table
471  * @md:         descriptor for the bad block table mirror
472  * @chipsel:    selector for a specific chip, -1 for all
473  *
474  * (Re)write the bad block table
475  *
476 */
477 static int write_bbt (struct mtd_info *mtd, uint8_t *buf,
478         struct nand_bbt_descr *td, struct nand_bbt_descr *md, int chipsel)
479 {
480         struct nand_chip *this = mtd->priv;
481         struct nand_oobinfo oobinfo;
482         struct erase_info einfo;
483         int i, j, res, chip = 0;
484         int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
485         int nrchips, bbtoffs, pageoffs;
486         uint8_t msk[4];
487         uint8_t rcode = td->reserved_block_code;
488         size_t retlen, len = 0;
489         loff_t to;
490
491         if (!rcode)
492                 rcode = 0xff;
493         /* Write bad block table per chip rather than per device ? */
494         if (td->options & NAND_BBT_PERCHIP) {
495                 numblocks = (int) (this->chipsize >> this->bbt_erase_shift);
496                 /* Full device write or specific chip ? */
497                 if (chipsel == -1) {
498                         nrchips = this->numchips;
499                 } else {
500                         nrchips = chipsel + 1;
501                         chip = chipsel;
502                 }
503         } else {
504                 numblocks = (int) (mtd->size >> this->bbt_erase_shift);
505                 nrchips = 1;
506         }
507
508         /* Loop through the chips */
509         for (; chip < nrchips; chip++) {
510
511                 /* There was already a version of the table, reuse the page
512                  * This applies for absolute placement too, as we have the
513                  * page nr. in td->pages.
514                  */
515                 if (td->pages[chip] != -1) {
516                         page = td->pages[chip];
517                         goto write;
518                 }
519
520                 /* Automatic placement of the bad block table */
521                 /* Search direction top -> down ? */
522                 if (td->options & NAND_BBT_LASTBLOCK) {
523                         startblock = numblocks * (chip + 1) - 1;
524                         dir = -1;
525                 } else {
526                         startblock = chip * numblocks;
527                         dir = 1;
528                 }
529
530                 for (i = 0; i < td->maxblocks; i++) {
531                         int block = startblock + dir * i;
532                         /* Check, if the block is bad */
533                         switch ((this->bbt[block >> 2] >> (2 * (block & 0x03))) & 0x03) {
534                         case 0x01:
535                         case 0x03:
536                                 continue;
537                         }
538                         page = block << (this->bbt_erase_shift - this->page_shift);
539                         /* Check, if the block is used by the mirror table */
540                         if (!md || md->pages[chip] != page)
541                                 goto write;
542                 }
543                 printk (KERN_ERR "No space left to write bad block table\n");
544                 return -ENOSPC;
545 write:
546
547                 /* Set up shift count and masks for the flash table */
548                 bits = td->options & NAND_BBT_NRBITS_MSK;
549                 switch (bits) {
550                 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x01; break;
551                 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x03; break;
552                 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C; msk[2] = ~rcode; msk[3] = 0x0f; break;
553                 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F; msk[2] = ~rcode; msk[3] = 0xff; break;
554                 default: return -EINVAL;
555                 }
556
557                 bbtoffs = chip * (numblocks >> 2);
558
559                 to = ((loff_t) page) << this->page_shift;
560
561                 memcpy (&oobinfo, this->autooob, sizeof(oobinfo));
562                 oobinfo.useecc = MTD_NANDECC_PLACEONLY;
563
564                 /* Must we save the block contents ? */
565                 if (td->options & NAND_BBT_SAVECONTENT) {
566                         /* Make it block aligned */
567                         to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
568                         len = 1 << this->bbt_erase_shift;
569                         res = mtd->read_ecc (mtd, to, len, &retlen, buf, &buf[len], &oobinfo);
570                         if (res < 0) {
571                                 if (retlen != len) {
572                                         printk (KERN_INFO "nand_bbt: Error reading block for writing the bad block table\n");
573                                         return res;
574                                 }
575                                 printk (KERN_WARNING "nand_bbt: ECC error while reading block for writing bad block table\n");
576                         }
577                         /* Calc the byte offset in the buffer */
578                         pageoffs = page - (int)(to >> this->page_shift);
579                         offs = pageoffs << this->page_shift;
580                         /* Preset the bbt area with 0xff */
581                         memset (&buf[offs], 0xff, (size_t)(numblocks >> sft));
582                         /* Preset the bbt's oob area with 0xff */
583                         memset (&buf[len + pageoffs * mtd->oobsize], 0xff,
584                                 ((len >> this->page_shift) - pageoffs) * mtd->oobsize);
585                         if (td->options & NAND_BBT_VERSION) {
586                                 buf[len + (pageoffs * mtd->oobsize) + td->veroffs] = td->version[chip];
587                         }
588                 } else {
589                         /* Calc length */
590                         len = (size_t) (numblocks >> sft);
591                         /* Make it page aligned ! */
592                         len = (len + (mtd->oobblock-1)) & ~(mtd->oobblock-1);
593                         /* Preset the buffer with 0xff */
594                         memset (buf, 0xff, len + (len >> this->page_shift) * mtd->oobsize);
595                         offs = 0;
596                         /* Pattern is located in oob area of first page */
597                         memcpy (&buf[len + td->offs], td->pattern, td->len);
598                         if (td->options & NAND_BBT_VERSION) {
599                                 buf[len + td->veroffs] = td->version[chip];
600                         }
601                 }
602
603                 /* walk through the memory table */
604                 for (i = 0; i < numblocks; ) {
605                         uint8_t dat;
606                         dat = this->bbt[bbtoffs + (i >> 2)];
607                         for (j = 0; j < 4; j++ , i++) {
608                                 int sftcnt = (i << (3 - sft)) & sftmsk;
609                                 /* Do not store the reserved bbt blocks ! */
610                                 buf[offs + (i >> sft)] &= ~(msk[dat & 0x03] << sftcnt);
611                                 dat >>= 2;
612                         }
613                 }
614
615                 memset (&einfo, 0, sizeof (einfo));
616                 einfo.mtd = mtd;
617                 einfo.addr = (unsigned long) to;
618                 einfo.len = 1 << this->bbt_erase_shift;
619                 res = nand_erase_nand (mtd, &einfo, 1);
620                 if (res < 0) {
621                         printk (KERN_WARNING "nand_bbt: Error during block erase: %d\n", res);
622                         return res;
623                 }
624
625                 res = mtd->write_ecc (mtd, to, len, &retlen, buf, &buf[len], &oobinfo);
626                 if (res < 0) {
627                         printk (KERN_WARNING "nand_bbt: Error while writing bad block table %d\n", res);
628                         return res;
629                 }
630                 printk (KERN_DEBUG "Bad block table written to 0x%08x, version 0x%02X\n",
631                         (unsigned int) to, td->version[chip]);
632
633                 /* Mark it as used */
634                 td->pages[chip] = page;
635         }
636         return 0;
637 }
638
639 /**
640  * nand_memory_bbt - [GENERIC] create a memory based bad block table
641  * @mtd:        MTD device structure
642  * @bd:         descriptor for the good/bad block search pattern
643  *
644  * The function creates a memory based bbt by scanning the device
645  * for manufacturer / software marked good / bad blocks
646 */
647 static inline int nand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
648 {
649         struct nand_chip *this = mtd->priv;
650
651         bd->options &= ~NAND_BBT_SCANEMPTY;
652         return create_bbt (mtd, this->data_buf, bd, -1);
653 }
654
655 /**
656  * check_create - [GENERIC] create and write bbt(s) if neccecary
657  * @mtd:        MTD device structure
658  * @buf:        temporary buffer
659  * @bd:         descriptor for the good/bad block search pattern
660  *
661  * The function checks the results of the previous call to read_bbt
662  * and creates / updates the bbt(s) if neccecary
663  * Creation is neccecary if no bbt was found for the chip/device
664  * Update is neccecary if one of the tables is missing or the
665  * version nr. of one table is less than the other
666 */
667 static int check_create (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
668 {
669         int i, chips, writeops, chipsel, res;
670         struct nand_chip *this = mtd->priv;
671         struct nand_bbt_descr *td = this->bbt_td;
672         struct nand_bbt_descr *md = this->bbt_md;
673         struct nand_bbt_descr *rd, *rd2;
674
675         /* Do we have a bbt per chip ? */
676         if (td->options & NAND_BBT_PERCHIP)
677                 chips = this->numchips;
678         else
679                 chips = 1;
680
681         for (i = 0; i < chips; i++) {
682                 writeops = 0;
683                 rd = NULL;
684                 rd2 = NULL;
685                 /* Per chip or per device ? */
686                 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
687                 /* Mirrored table avilable ? */
688                 if (md) {
689                         if (td->pages[i] == -1 && md->pages[i] == -1) {
690                                 writeops = 0x03;
691                                 goto create;
692                         }
693
694                         if (td->pages[i] == -1) {
695                                 rd = md;
696                                 td->version[i] = md->version[i];
697                                 writeops = 1;
698                                 goto writecheck;
699                         }
700
701                         if (md->pages[i] == -1) {
702                                 rd = td;
703                                 md->version[i] = td->version[i];
704                                 writeops = 2;
705                                 goto writecheck;
706                         }
707
708                         if (td->version[i] == md->version[i]) {
709                                 rd = td;
710                                 if (!(td->options & NAND_BBT_VERSION))
711                                         rd2 = md;
712                                 goto writecheck;
713                         }
714
715                         if (((int8_t) (td->version[i] - md->version[i])) > 0) {
716                                 rd = td;
717                                 md->version[i] = td->version[i];
718                                 writeops = 2;
719                         } else {
720                                 rd = md;
721                                 td->version[i] = md->version[i];
722                                 writeops = 1;
723                         }
724
725                         goto writecheck;
726
727                 } else {
728                         if (td->pages[i] == -1) {
729                                 writeops = 0x01;
730                                 goto create;
731                         }
732                         rd = td;
733                         goto writecheck;
734                 }
735 create:
736                 /* Create the bad block table by scanning the device ? */
737                 if (!(td->options & NAND_BBT_CREATE))
738                         continue;
739
740                 /* Create the table in memory by scanning the chip(s) */
741                 create_bbt (mtd, buf, bd, chipsel);
742
743                 td->version[i] = 1;
744                 if (md)
745                         md->version[i] = 1;
746 writecheck:
747                 /* read back first ? */
748                 if (rd)
749                         read_abs_bbt (mtd, buf, rd, chipsel);
750                 /* If they weren't versioned, read both. */
751                 if (rd2)
752                         read_abs_bbt (mtd, buf, rd2, chipsel);
753
754                 /* Write the bad block table to the device ? */
755                 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
756                         res = write_bbt (mtd, buf, td, md, chipsel);
757                         if (res < 0)
758                                 return res;
759                 }
760
761                 /* Write the mirror bad block table to the device ? */
762                 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
763                         res = write_bbt (mtd, buf, md, td, chipsel);
764                         if (res < 0)
765                                 return res;
766                 }
767         }
768         return 0;
769 }
770
771 /**
772  * mark_bbt_regions - [GENERIC] mark the bad block table regions
773  * @mtd:        MTD device structure
774  * @td:         bad block table descriptor
775  *
776  * The bad block table regions are marked as "bad" to prevent
777  * accidental erasures / writes. The regions are identified by
778  * the mark 0x02.
779 */
780 static void mark_bbt_region (struct mtd_info *mtd, struct nand_bbt_descr *td)
781 {
782         struct nand_chip *this = mtd->priv;
783         int i, j, chips, block, nrblocks, update;
784         uint8_t oldval, newval;
785
786         /* Do we have a bbt per chip ? */
787         if (td->options & NAND_BBT_PERCHIP) {
788                 chips = this->numchips;
789                 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
790         } else {
791                 chips = 1;
792                 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
793         }
794
795         for (i = 0; i < chips; i++) {
796                 if ((td->options & NAND_BBT_ABSPAGE) ||
797                     !(td->options & NAND_BBT_WRITE)) {
798                         if (td->pages[i] == -1) continue;
799                         block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
800                         block <<= 1;
801                         oldval = this->bbt[(block >> 3)];
802                         newval = oldval | (0x2 << (block & 0x06));
803                         this->bbt[(block >> 3)] = newval;
804                         if ((oldval != newval) && td->reserved_block_code)
805                                 nand_update_bbt(mtd, block << (this->bbt_erase_shift - 1));
806                         continue;
807                 }
808                 update = 0;
809                 if (td->options & NAND_BBT_LASTBLOCK)
810                         block = ((i + 1) * nrblocks) - td->maxblocks;
811                 else
812                         block = i * nrblocks;
813                 block <<= 1;
814                 for (j = 0; j < td->maxblocks; j++) {
815                         oldval = this->bbt[(block >> 3)];
816                         newval = oldval | (0x2 << (block & 0x06));
817                         this->bbt[(block >> 3)] = newval;
818                         if (oldval != newval) update = 1;
819                         block += 2;
820                 }
821                 /* If we want reserved blocks to be recorded to flash, and some
822                    new ones have been marked, then we need to update the stored
823                    bbts.  This should only happen once. */
824                 if (update && td->reserved_block_code)
825                         nand_update_bbt(mtd, (block - 2) << (this->bbt_erase_shift - 1));
826         }
827 }
828
829 /**
830  * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
831  * @mtd:        MTD device structure
832  * @bd:         descriptor for the good/bad block search pattern
833  *
834  * The function checks, if a bad block table(s) is/are already
835  * available. If not it scans the device for manufacturer
836  * marked good / bad blocks and writes the bad block table(s) to
837  * the selected place.
838  *
839  * The bad block table memory is allocated here. It must be freed
840  * by calling the nand_free_bbt function.
841  *
842 */
843 int nand_scan_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
844 {
845         struct nand_chip *this = mtd->priv;
846         int len, res = 0;
847         uint8_t *buf;
848         struct nand_bbt_descr *td = this->bbt_td;
849         struct nand_bbt_descr *md = this->bbt_md;
850
851         len = mtd->size >> (this->bbt_erase_shift + 2);
852         /* Allocate memory (2bit per block) */
853         this->bbt = kmalloc (len, GFP_KERNEL);
854         if (!this->bbt) {
855                 printk (KERN_ERR "nand_scan_bbt: Out of memory\n");
856                 return -ENOMEM;
857         }
858         /* Clear the memory bad block table */
859         memset (this->bbt, 0x00, len);
860
861         /* If no primary table decriptor is given, scan the device
862          * to build a memory based bad block table
863          */
864         if (!td) {
865                 if ((res = nand_memory_bbt(mtd, bd))) {
866                         printk (KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
867                         kfree (this->bbt);
868                         this->bbt = NULL;
869                 }
870                 return res;
871         }
872
873         /* Allocate a temporary buffer for one eraseblock incl. oob */
874         len = (1 << this->bbt_erase_shift);
875         len += (len >> this->page_shift) * mtd->oobsize;
876         buf = kmalloc (len, GFP_KERNEL);
877         if (!buf) {
878                 printk (KERN_ERR "nand_bbt: Out of memory\n");
879                 kfree (this->bbt);
880                 this->bbt = NULL;
881                 return -ENOMEM;
882         }
883
884         /* Is the bbt at a given page ? */
885         if (td->options & NAND_BBT_ABSPAGE) {
886                 res = read_abs_bbts (mtd, buf, td, md);
887         } else {
888                 /* Search the bad block table using a pattern in oob */
889                 res = search_read_bbts (mtd, buf, td, md);
890         }
891
892         if (res)
893                 res = check_create (mtd, buf, bd);
894
895         /* Prevent the bbt regions from erasing / writing */
896         mark_bbt_region (mtd, td);
897         if (md)
898                 mark_bbt_region (mtd, md);
899
900         kfree (buf);
901         return res;
902 }
903
904
905 /**
906  * nand_update_bbt - [NAND Interface] update bad block table(s)
907  * @mtd:        MTD device structure
908  * @offs:       the offset of the newly marked block
909  *
910  * The function updates the bad block table(s)
911 */
912 int nand_update_bbt (struct mtd_info *mtd, loff_t offs)
913 {
914         struct nand_chip *this = mtd->priv;
915         int len, res = 0, writeops = 0;
916         int chip, chipsel;
917         uint8_t *buf;
918         struct nand_bbt_descr *td = this->bbt_td;
919         struct nand_bbt_descr *md = this->bbt_md;
920
921         if (!this->bbt || !td)
922                 return -EINVAL;
923
924         len = mtd->size >> (this->bbt_erase_shift + 2);
925         /* Allocate a temporary buffer for one eraseblock incl. oob */
926         len = (1 << this->bbt_erase_shift);
927         len += (len >> this->page_shift) * mtd->oobsize;
928         buf = kmalloc (len, GFP_KERNEL);
929         if (!buf) {
930                 printk (KERN_ERR "nand_update_bbt: Out of memory\n");
931                 return -ENOMEM;
932         }
933
934         writeops = md != NULL ? 0x03 : 0x01;
935
936         /* Do we have a bbt per chip ? */
937         if (td->options & NAND_BBT_PERCHIP) {
938                 chip = (int) (offs >> this->chip_shift);
939                 chipsel = chip;
940         } else {
941                 chip = 0;
942                 chipsel = -1;
943         }
944
945         td->version[chip]++;
946         if (md)
947                 md->version[chip]++;
948
949         /* Write the bad block table to the device ? */
950         if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
951                 res = write_bbt (mtd, buf, td, md, chipsel);
952                 if (res < 0)
953                         goto out;
954         }
955         /* Write the mirror bad block table to the device ? */
956         if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
957                 res = write_bbt (mtd, buf, md, td, chipsel);
958         }
959
960 out:
961         kfree (buf);
962         return res;
963 }
964
965 /* Define some generic bad / good block scan pattern which are used
966  * while scanning a device for factory marked good / bad blocks. */
967 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
968
969 static struct nand_bbt_descr smallpage_memorybased = {
970         .options = NAND_BBT_SCAN2NDPAGE,
971         .offs = 5,
972         .len = 1,
973         .pattern = scan_ff_pattern
974 };
975
976 static struct nand_bbt_descr largepage_memorybased = {
977         .options = 0,
978         .offs = 0,
979         .len = 2,
980         .pattern = scan_ff_pattern
981 };
982
983 static struct nand_bbt_descr smallpage_flashbased = {
984         .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
985         .offs = 5,
986         .len = 1,
987         .pattern = scan_ff_pattern
988 };
989
990 static struct nand_bbt_descr largepage_flashbased = {
991         .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
992         .offs = 0,
993         .len = 2,
994         .pattern = scan_ff_pattern
995 };
996
997 static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
998
999 static struct nand_bbt_descr agand_flashbased = {
1000         .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1001         .offs = 0x20,
1002         .len = 6,
1003         .pattern = scan_agand_pattern
1004 };
1005
1006 /* Generic flash bbt decriptors
1007 */
1008 static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1009 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1010
1011 static struct nand_bbt_descr bbt_main_descr = {
1012         .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1013                 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1014         .offs = 8,
1015         .len = 4,
1016         .veroffs = 12,
1017         .maxblocks = 4,
1018         .pattern = bbt_pattern
1019 };
1020
1021 static struct nand_bbt_descr bbt_mirror_descr = {
1022         .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1023                 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1024         .offs = 8,
1025         .len = 4,
1026         .veroffs = 12,
1027         .maxblocks = 4,
1028         .pattern = mirror_pattern
1029 };
1030
1031 /**
1032  * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
1033  * @mtd:        MTD device structure
1034  *
1035  * This function selects the default bad block table
1036  * support for the device and calls the nand_scan_bbt function
1037  *
1038 */
1039 int nand_default_bbt (struct mtd_info *mtd)
1040 {
1041         struct nand_chip *this = mtd->priv;
1042
1043         /* Default for AG-AND. We must use a flash based
1044          * bad block table as the devices have factory marked
1045          * _good_ blocks. Erasing those blocks leads to loss
1046          * of the good / bad information, so we _must_ store
1047          * this information in a good / bad table during
1048          * startup
1049         */
1050         if (this->options & NAND_IS_AND) {
1051                 /* Use the default pattern descriptors */
1052                 if (!this->bbt_td) {
1053                         this->bbt_td = &bbt_main_descr;
1054                         this->bbt_md = &bbt_mirror_descr;
1055                 }
1056                 this->options |= NAND_USE_FLASH_BBT;
1057                 return nand_scan_bbt (mtd, &agand_flashbased);
1058         }
1059
1060
1061         /* Is a flash based bad block table requested ? */
1062         if (this->options & NAND_USE_FLASH_BBT) {
1063                 /* Use the default pattern descriptors */
1064                 if (!this->bbt_td) {
1065                         this->bbt_td = &bbt_main_descr;
1066                         this->bbt_md = &bbt_mirror_descr;
1067                 }
1068                 if (!this->badblock_pattern) {
1069                         this->badblock_pattern = (mtd->oobblock > 512) ?
1070                                 &largepage_flashbased : &smallpage_flashbased;
1071                 }
1072         } else {
1073                 this->bbt_td = NULL;
1074                 this->bbt_md = NULL;
1075                 if (!this->badblock_pattern) {
1076                         this->badblock_pattern = (mtd->oobblock > 512) ?
1077                                 &largepage_memorybased : &smallpage_memorybased;
1078                 }
1079         }
1080         return nand_scan_bbt (mtd, this->badblock_pattern);
1081 }
1082
1083 /**
1084  * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1085  * @mtd:        MTD device structure
1086  * @offs:       offset in the device
1087  * @allowbbt:   allow access to bad block table region
1088  *
1089 */
1090 int nand_isbad_bbt (struct mtd_info *mtd, loff_t offs, int allowbbt)
1091 {
1092         struct nand_chip *this = mtd->priv;
1093         int block;
1094         uint8_t res;
1095
1096         /* Get block number * 2 */
1097         block = (int) (offs >> (this->bbt_erase_shift - 1));
1098         res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1099
1100         DEBUG (MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1101                 (unsigned int)offs, block >> 1, res);
1102
1103         switch ((int)res) {
1104         case 0x00:      return 0;
1105         case 0x01:      return 1;
1106         case 0x02:      return allowbbt ? 0 : 1;
1107         }
1108         return 1;
1109 }
1110
1111 EXPORT_SYMBOL (nand_scan_bbt);
1112 EXPORT_SYMBOL (nand_default_bbt);