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