2 * drivers/mtd/nand_bbt.c
5 * Bad block table support for the NAND driver
7 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
9 * $Id: nand_bbt.c,v 1.28 2004/11/13 10:19:09 gleixner Exp $
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.
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.
28 * For manufacturer created bbts like the one found on M-SYS DOC devices
29 * the bbt is searched and read but never created
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.
36 * The table uses 2 bits per block
38 * 00b: block is factory marked bad
39 * 01b, 10b: block is marked bad due to wear
41 * The memory bad block table uses the following scheme:
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
47 * Multichip devices like DOC store the bad block info per floor.
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
57 #ifdef CONFIG_NEW_NAND_CODE
58 #if (CONFIG_COMMANDS & CFG_CMD_NAND)
61 #include <linux/mtd/compat.h>
62 #include <linux/mtd/mtd.h>
63 #include <linux/mtd/nand.h>
65 #include <asm/errno.h>
68 * check_pattern - [GENERIC] check if a pattern is in the buffer
69 * @buf: the buffer to search
70 * @len: the length of buffer to search
71 * @paglen: the pagelength
72 * @td: search pattern descriptor
74 * Check for a pattern at the given place. Used to search bad block
75 * tables and good / bad block identifiers.
76 * If the SCAN_EMPTY option is set then check, if all bytes except the
77 * pattern area contain 0xff
80 static int check_pattern (uint8_t *buf
, int len
, int paglen
, struct nand_bbt_descr
*td
)
85 end
= paglen
+ td
->offs
;
86 if (td
->options
& NAND_BBT_SCANEMPTY
) {
87 for (i
= 0; i
< end
; i
++) {
94 /* Compare the pattern */
95 for (i
= 0; i
< td
->len
; i
++) {
96 if (p
[i
] != td
->pattern
[i
])
102 if (td
->options
& NAND_BBT_SCANEMPTY
) {
103 for (i
= end
; i
< len
; i
++) {
112 * read_bbt - [GENERIC] Read the bad block table starting from page
113 * @mtd: MTD device structure
114 * @buf: temporary buffer
115 * @page: the starting page
116 * @num: the number of bbt descriptors to read
117 * @bits: number of bits per block
118 * @offs: offset in the memory table
119 * @reserved_block_code: Pattern to identify reserved blocks
121 * Read the bad block table starting from page.
124 static int read_bbt (struct mtd_info
*mtd
, uint8_t *buf
, int page
, int num
,
125 int bits
, int offs
, int reserved_block_code
)
127 int res
, i
, j
, act
= 0;
128 struct nand_chip
*this = mtd
->priv
;
129 size_t retlen
, len
, totlen
;
131 uint8_t msk
= (uint8_t) ((1 << bits
) - 1);
133 totlen
= (num
* bits
) >> 3;
134 from
= ((loff_t
)page
) << this->page_shift
;
137 len
= min (totlen
, (size_t) (1 << this->bbt_erase_shift
));
138 res
= mtd
->read_ecc (mtd
, from
, len
, &retlen
, buf
, NULL
, this->autooob
);
141 printk (KERN_INFO
"nand_bbt: Error reading bad block table\n");
144 printk (KERN_WARNING
"nand_bbt: ECC error while reading bad block table\n");
148 for (i
= 0; i
< len
; i
++) {
149 uint8_t dat
= buf
[i
];
150 for (j
= 0; j
< 8; j
+= bits
, act
+= 2) {
151 uint8_t tmp
= (dat
>> j
) & msk
;
154 if (reserved_block_code
&&
155 (tmp
== reserved_block_code
)) {
156 printk (KERN_DEBUG
"nand_read_bbt: Reserved block at 0x%08x\n",
157 ((offs
<< 2) + (act
>> 1)) << this->bbt_erase_shift
);
158 this->bbt
[offs
+ (act
>> 3)] |= 0x2 << (act
& 0x06);
161 /* Leave it for now, if its matured we can move this
162 * message to MTD_DEBUG_LEVEL0 */
163 printk (KERN_DEBUG
"nand_read_bbt: Bad block at 0x%08x\n",
164 ((offs
<< 2) + (act
>> 1)) << this->bbt_erase_shift
);
165 /* Factory marked bad or worn out ? */
167 this->bbt
[offs
+ (act
>> 3)] |= 0x3 << (act
& 0x06);
169 this->bbt
[offs
+ (act
>> 3)] |= 0x1 << (act
& 0x06);
179 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
180 * @mtd: MTD device structure
181 * @buf: temporary buffer
182 * @td: descriptor for the bad block table
183 * @chip: read the table for a specific chip, -1 read all chips.
184 * Applies only if NAND_BBT_PERCHIP option is set
186 * Read the bad block table for all chips starting at a given page
187 * We assume that the bbt bits are in consecutive order.
189 static int read_abs_bbt (struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
, int chip
)
191 struct nand_chip
*this = mtd
->priv
;
195 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
196 if (td
->options
& NAND_BBT_PERCHIP
) {
198 for (i
= 0; i
< this->numchips
; i
++) {
199 if (chip
== -1 || chip
== i
)
200 res
= read_bbt (mtd
, buf
, td
->pages
[i
], this->chipsize
>> this->bbt_erase_shift
, bits
, offs
, td
->reserved_block_code
);
203 offs
+= this->chipsize
>> (this->bbt_erase_shift
+ 2);
206 res
= read_bbt (mtd
, buf
, td
->pages
[0], mtd
->size
>> this->bbt_erase_shift
, bits
, 0, td
->reserved_block_code
);
214 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
215 * @mtd: MTD device structure
216 * @buf: temporary buffer
217 * @td: descriptor for the bad block table
218 * @md: descriptor for the bad block table mirror
220 * Read the bad block table(s) for all chips starting at a given page
221 * We assume that the bbt bits are in consecutive order.
224 static int read_abs_bbts (struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
,
225 struct nand_bbt_descr
*md
)
227 struct nand_chip
*this = mtd
->priv
;
229 /* Read the primary version, if available */
230 if (td
->options
& NAND_BBT_VERSION
) {
231 nand_read_raw (mtd
, buf
, td
->pages
[0] << this->page_shift
, mtd
->oobblock
, mtd
->oobsize
);
232 td
->version
[0] = buf
[mtd
->oobblock
+ td
->veroffs
];
233 printk (KERN_DEBUG
"Bad block table at page %d, version 0x%02X\n", td
->pages
[0], td
->version
[0]);
236 /* Read the mirror version, if available */
237 if (md
&& (md
->options
& NAND_BBT_VERSION
)) {
238 nand_read_raw (mtd
, buf
, md
->pages
[0] << this->page_shift
, mtd
->oobblock
, mtd
->oobsize
);
239 md
->version
[0] = buf
[mtd
->oobblock
+ md
->veroffs
];
240 printk (KERN_DEBUG
"Bad block table at page %d, version 0x%02X\n", md
->pages
[0], md
->version
[0]);
247 * create_bbt - [GENERIC] Create a bad block table by scanning the device
248 * @mtd: MTD device structure
249 * @buf: temporary buffer
250 * @bd: descriptor for the good/bad block search pattern
251 * @chip: create the table for a specific chip, -1 read all chips.
252 * Applies only if NAND_BBT_PERCHIP option is set
254 * Create a bad block table by scanning the device
255 * for the given good/bad block identify pattern
257 static void create_bbt (struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*bd
, int chip
)
259 struct nand_chip
*this = mtd
->priv
;
260 int i
, j
, numblocks
, len
, scanlen
;
263 size_t readlen
, ooblen
;
265 if (bd
->options
& NAND_BBT_SCANALLPAGES
)
266 len
= 1 << (this->bbt_erase_shift
- this->page_shift
);
268 if (bd
->options
& NAND_BBT_SCAN2NDPAGE
)
273 scanlen
= mtd
->oobblock
+ mtd
->oobsize
;
274 readlen
= len
* mtd
->oobblock
;
275 ooblen
= len
* mtd
->oobsize
;
278 /* Note that numblocks is 2 * (real numblocks) here, see i+=2 below as it
279 * makes shifting and masking less painful */
280 numblocks
= mtd
->size
>> (this->bbt_erase_shift
- 1);
284 if (chip
>= this->numchips
) {
285 printk (KERN_WARNING
"create_bbt(): chipnr (%d) > available chips (%d)\n",
286 chip
+ 1, this->numchips
);
289 numblocks
= this->chipsize
>> (this->bbt_erase_shift
- 1);
290 startblock
= chip
* numblocks
;
291 numblocks
+= startblock
;
292 from
= startblock
<< (this->bbt_erase_shift
- 1);
295 for (i
= startblock
; i
< numblocks
;) {
296 nand_read_raw (mtd
, buf
, from
, readlen
, ooblen
);
297 for (j
= 0; j
< len
; j
++) {
298 if (check_pattern (&buf
[j
* scanlen
], scanlen
, mtd
->oobblock
, bd
)) {
299 this->bbt
[i
>> 3] |= 0x03 << (i
& 0x6);
304 from
+= (1 << this->bbt_erase_shift
);
309 * search_bbt - [GENERIC] scan the device for a specific bad block table
310 * @mtd: MTD device structure
311 * @buf: temporary buffer
312 * @td: descriptor for the bad block table
314 * Read the bad block table by searching for a given ident pattern.
315 * Search is preformed either from the beginning up or from the end of
316 * the device downwards. The search starts always at the start of a
318 * If the option NAND_BBT_PERCHIP is given, each chip is searched
319 * for a bbt, which contains the bad block information of this chip.
320 * This is neccecary to provide support for certain DOC devices.
322 * The bbt ident pattern resides in the oob area of the first page
325 static int search_bbt (struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
)
327 struct nand_chip
*this = mtd
->priv
;
329 int bits
, startblock
, block
, dir
;
330 int scanlen
= mtd
->oobblock
+ mtd
->oobsize
;
333 /* Search direction top -> down ? */
334 if (td
->options
& NAND_BBT_LASTBLOCK
) {
335 startblock
= (mtd
->size
>> this->bbt_erase_shift
) -1;
342 /* Do we have a bbt per chip ? */
343 if (td
->options
& NAND_BBT_PERCHIP
) {
344 chips
= this->numchips
;
345 bbtblocks
= this->chipsize
>> this->bbt_erase_shift
;
346 startblock
&= bbtblocks
- 1;
349 bbtblocks
= mtd
->size
>> this->bbt_erase_shift
;
352 /* Number of bits for each erase block in the bbt */
353 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
355 for (i
= 0; i
< chips
; i
++) {
356 /* Reset version information */
359 /* Scan the maximum number of blocks */
360 for (block
= 0; block
< td
->maxblocks
; block
++) {
361 int actblock
= startblock
+ dir
* block
;
362 /* Read first page */
363 nand_read_raw (mtd
, buf
, actblock
<< this->bbt_erase_shift
, mtd
->oobblock
, mtd
->oobsize
);
364 if (!check_pattern(buf
, scanlen
, mtd
->oobblock
, td
)) {
365 td
->pages
[i
] = actblock
<< (this->bbt_erase_shift
- this->page_shift
);
366 if (td
->options
& NAND_BBT_VERSION
) {
367 td
->version
[i
] = buf
[mtd
->oobblock
+ td
->veroffs
];
372 startblock
+= this->chipsize
>> this->bbt_erase_shift
;
374 /* Check, if we found a bbt for each requested chip */
375 for (i
= 0; i
< chips
; i
++) {
376 if (td
->pages
[i
] == -1)
377 printk (KERN_WARNING
"Bad block table not found for chip %d\n", i
);
379 printk (KERN_DEBUG
"Bad block table found at page %d, version 0x%02X\n", td
->pages
[i
], td
->version
[i
]);
385 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
386 * @mtd: MTD device structure
387 * @buf: temporary buffer
388 * @td: descriptor for the bad block table
389 * @md: descriptor for the bad block table mirror
391 * Search and read the bad block table(s)
393 static int search_read_bbts (struct mtd_info
*mtd
, uint8_t *buf
,
394 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
)
396 /* Search the primary table */
397 search_bbt (mtd
, buf
, td
);
399 /* Search the mirror table */
401 search_bbt (mtd
, buf
, md
);
403 /* Force result check */
409 * write_bbt - [GENERIC] (Re)write the bad block table
411 * @mtd: MTD device structure
412 * @buf: temporary buffer
413 * @td: descriptor for the bad block table
414 * @md: descriptor for the bad block table mirror
415 * @chipsel: selector for a specific chip, -1 for all
417 * (Re)write the bad block table
420 static int write_bbt (struct mtd_info
*mtd
, uint8_t *buf
,
421 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
, int chipsel
)
423 struct nand_chip
*this = mtd
->priv
;
424 struct nand_oobinfo oobinfo
;
425 struct erase_info einfo
;
426 int i
, j
, res
, chip
= 0;
427 int bits
, startblock
, dir
, page
, offs
, numblocks
, sft
, sftmsk
;
428 int nrchips
, bbtoffs
, pageoffs
;
430 uint8_t rcode
= td
->reserved_block_code
;
431 size_t retlen
, len
= 0;
436 /* Write bad block table per chip rather than per device ? */
437 if (td
->options
& NAND_BBT_PERCHIP
) {
438 numblocks
= (int) (this->chipsize
>> this->bbt_erase_shift
);
439 /* Full device write or specific chip ? */
441 nrchips
= this->numchips
;
443 nrchips
= chipsel
+ 1;
447 numblocks
= (int) (mtd
->size
>> this->bbt_erase_shift
);
451 /* Loop through the chips */
452 for (; chip
< nrchips
; chip
++) {
454 /* There was already a version of the table, reuse the page
455 * This applies for absolute placement too, as we have the
456 * page nr. in td->pages.
458 if (td
->pages
[chip
] != -1) {
459 page
= td
->pages
[chip
];
463 /* Automatic placement of the bad block table */
464 /* Search direction top -> down ? */
465 if (td
->options
& NAND_BBT_LASTBLOCK
) {
466 startblock
= numblocks
* (chip
+ 1) - 1;
469 startblock
= chip
* numblocks
;
473 for (i
= 0; i
< td
->maxblocks
; i
++) {
474 int block
= startblock
+ dir
* i
;
475 /* Check, if the block is bad */
476 switch ((this->bbt
[block
>> 2] >> (2 * (block
& 0x03))) & 0x03) {
481 page
= block
<< (this->bbt_erase_shift
- this->page_shift
);
482 /* Check, if the block is used by the mirror table */
483 if (!md
|| md
->pages
[chip
] != page
)
486 printk (KERN_ERR
"No space left to write bad block table\n");
490 /* Set up shift count and masks for the flash table */
491 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
493 case 1: sft
= 3; sftmsk
= 0x07; msk
[0] = 0x00; msk
[1] = 0x01; msk
[2] = ~rcode
; msk
[3] = 0x01; break;
494 case 2: sft
= 2; sftmsk
= 0x06; msk
[0] = 0x00; msk
[1] = 0x01; msk
[2] = ~rcode
; msk
[3] = 0x03; break;
495 case 4: sft
= 1; sftmsk
= 0x04; msk
[0] = 0x00; msk
[1] = 0x0C; msk
[2] = ~rcode
; msk
[3] = 0x0f; break;
496 case 8: sft
= 0; sftmsk
= 0x00; msk
[0] = 0x00; msk
[1] = 0x0F; msk
[2] = ~rcode
; msk
[3] = 0xff; break;
497 default: return -EINVAL
;
500 bbtoffs
= chip
* (numblocks
>> 2);
502 to
= ((loff_t
) page
) << this->page_shift
;
504 memcpy (&oobinfo
, this->autooob
, sizeof(oobinfo
));
505 oobinfo
.useecc
= MTD_NANDECC_PLACEONLY
;
507 /* Must we save the block contents ? */
508 if (td
->options
& NAND_BBT_SAVECONTENT
) {
509 /* Make it block aligned */
510 to
&= ~((loff_t
) ((1 << this->bbt_erase_shift
) - 1));
511 len
= 1 << this->bbt_erase_shift
;
512 res
= mtd
->read_ecc (mtd
, to
, len
, &retlen
, buf
, &buf
[len
], &oobinfo
);
515 printk (KERN_INFO
"nand_bbt: Error reading block for writing the bad block table\n");
518 printk (KERN_WARNING
"nand_bbt: ECC error while reading block for writing bad block table\n");
520 /* Calc the byte offset in the buffer */
521 pageoffs
= page
- (int)(to
>> this->page_shift
);
522 offs
= pageoffs
<< this->page_shift
;
523 /* Preset the bbt area with 0xff */
524 memset (&buf
[offs
], 0xff, (size_t)(numblocks
>> sft
));
525 /* Preset the bbt's oob area with 0xff */
526 memset (&buf
[len
+ pageoffs
* mtd
->oobsize
], 0xff,
527 ((len
>> this->page_shift
) - pageoffs
) * mtd
->oobsize
);
528 if (td
->options
& NAND_BBT_VERSION
) {
529 buf
[len
+ (pageoffs
* mtd
->oobsize
) + td
->veroffs
] = td
->version
[chip
];
533 len
= (size_t) (numblocks
>> sft
);
534 /* Make it page aligned ! */
535 len
= (len
+ (mtd
->oobblock
-1)) & ~(mtd
->oobblock
-1);
536 /* Preset the buffer with 0xff */
537 memset (buf
, 0xff, len
+ (len
>> this->page_shift
) * mtd
->oobsize
);
539 /* Pattern is located in oob area of first page */
540 memcpy (&buf
[len
+ td
->offs
], td
->pattern
, td
->len
);
541 if (td
->options
& NAND_BBT_VERSION
) {
542 buf
[len
+ td
->veroffs
] = td
->version
[chip
];
546 /* walk through the memory table */
547 for (i
= 0; i
< numblocks
; ) {
549 dat
= this->bbt
[bbtoffs
+ (i
>> 2)];
550 for (j
= 0; j
< 4; j
++ , i
++) {
551 int sftcnt
= (i
<< (3 - sft
)) & sftmsk
;
552 /* Do not store the reserved bbt blocks ! */
553 buf
[offs
+ (i
>> sft
)] &= ~(msk
[dat
& 0x03] << sftcnt
);
558 memset (&einfo
, 0, sizeof (einfo
));
560 einfo
.addr
= (unsigned long) to
;
561 einfo
.len
= 1 << this->bbt_erase_shift
;
562 res
= nand_erase_nand (mtd
, &einfo
, 1);
564 printk (KERN_WARNING
"nand_bbt: Error during block erase: %d\n", res
);
568 res
= mtd
->write_ecc (mtd
, to
, len
, &retlen
, buf
, &buf
[len
], &oobinfo
);
570 printk (KERN_WARNING
"nand_bbt: Error while writing bad block table %d\n", res
);
573 printk (KERN_DEBUG
"Bad block table written to 0x%08x, version 0x%02X\n",
574 (unsigned int) to
, td
->version
[chip
]);
576 /* Mark it as used */
577 td
->pages
[chip
] = page
;
583 * nand_memory_bbt - [GENERIC] create a memory based bad block table
584 * @mtd: MTD device structure
585 * @bd: descriptor for the good/bad block search pattern
587 * The function creates a memory based bbt by scanning the device
588 * for manufacturer / software marked good / bad blocks
590 static int nand_memory_bbt (struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
592 struct nand_chip
*this = mtd
->priv
;
594 /* Ensure that we only scan for the pattern and nothing else */
596 create_bbt (mtd
, this->data_buf
, bd
, -1);
601 * check_create - [GENERIC] create and write bbt(s) if neccecary
602 * @mtd: MTD device structure
603 * @buf: temporary buffer
604 * @bd: descriptor for the good/bad block search pattern
606 * The function checks the results of the previous call to read_bbt
607 * and creates / updates the bbt(s) if neccecary
608 * Creation is neccecary if no bbt was found for the chip/device
609 * Update is neccecary if one of the tables is missing or the
610 * version nr. of one table is less than the other
612 static int check_create (struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*bd
)
614 int i
, chips
, writeops
, chipsel
, res
;
615 struct nand_chip
*this = mtd
->priv
;
616 struct nand_bbt_descr
*td
= this->bbt_td
;
617 struct nand_bbt_descr
*md
= this->bbt_md
;
618 struct nand_bbt_descr
*rd
, *rd2
;
620 /* Do we have a bbt per chip ? */
621 if (td
->options
& NAND_BBT_PERCHIP
)
622 chips
= this->numchips
;
626 for (i
= 0; i
< chips
; i
++) {
630 /* Per chip or per device ? */
631 chipsel
= (td
->options
& NAND_BBT_PERCHIP
) ? i
: -1;
632 /* Mirrored table avilable ? */
634 if (td
->pages
[i
] == -1 && md
->pages
[i
] == -1) {
639 if (td
->pages
[i
] == -1) {
641 td
->version
[i
] = md
->version
[i
];
646 if (md
->pages
[i
] == -1) {
648 md
->version
[i
] = td
->version
[i
];
653 if (td
->version
[i
] == md
->version
[i
]) {
655 if (!(td
->options
& NAND_BBT_VERSION
))
660 if (((int8_t) (td
->version
[i
] - md
->version
[i
])) > 0) {
662 md
->version
[i
] = td
->version
[i
];
666 td
->version
[i
] = md
->version
[i
];
673 if (td
->pages
[i
] == -1) {
681 /* Create the bad block table by scanning the device ? */
682 if (!(td
->options
& NAND_BBT_CREATE
))
685 /* Create the table in memory by scanning the chip(s) */
686 create_bbt (mtd
, buf
, bd
, chipsel
);
692 /* read back first ? */
694 read_abs_bbt (mtd
, buf
, rd
, chipsel
);
695 /* If they weren't versioned, read both. */
697 read_abs_bbt (mtd
, buf
, rd2
, chipsel
);
699 /* Write the bad block table to the device ? */
700 if ((writeops
& 0x01) && (td
->options
& NAND_BBT_WRITE
)) {
701 res
= write_bbt (mtd
, buf
, td
, md
, chipsel
);
706 /* Write the mirror bad block table to the device ? */
707 if ((writeops
& 0x02) && md
&& (md
->options
& NAND_BBT_WRITE
)) {
708 res
= write_bbt (mtd
, buf
, md
, td
, chipsel
);
717 * mark_bbt_regions - [GENERIC] mark the bad block table regions
718 * @mtd: MTD device structure
719 * @td: bad block table descriptor
721 * The bad block table regions are marked as "bad" to prevent
722 * accidental erasures / writes. The regions are identified by
725 static void mark_bbt_region (struct mtd_info
*mtd
, struct nand_bbt_descr
*td
)
727 struct nand_chip
*this = mtd
->priv
;
728 int i
, j
, chips
, block
, nrblocks
, update
;
729 uint8_t oldval
, newval
;
731 /* Do we have a bbt per chip ? */
732 if (td
->options
& NAND_BBT_PERCHIP
) {
733 chips
= this->numchips
;
734 nrblocks
= (int)(this->chipsize
>> this->bbt_erase_shift
);
737 nrblocks
= (int)(mtd
->size
>> this->bbt_erase_shift
);
740 for (i
= 0; i
< chips
; i
++) {
741 if ((td
->options
& NAND_BBT_ABSPAGE
) ||
742 !(td
->options
& NAND_BBT_WRITE
)) {
743 if (td
->pages
[i
] == -1) continue;
744 block
= td
->pages
[i
] >> (this->bbt_erase_shift
- this->page_shift
);
746 oldval
= this->bbt
[(block
>> 3)];
747 newval
= oldval
| (0x2 << (block
& 0x06));
748 this->bbt
[(block
>> 3)] = newval
;
749 if ((oldval
!= newval
) && td
->reserved_block_code
)
750 nand_update_bbt(mtd
, block
<< (this->bbt_erase_shift
- 1));
754 if (td
->options
& NAND_BBT_LASTBLOCK
)
755 block
= ((i
+ 1) * nrblocks
) - td
->maxblocks
;
757 block
= i
* nrblocks
;
759 for (j
= 0; j
< td
->maxblocks
; j
++) {
760 oldval
= this->bbt
[(block
>> 3)];
761 newval
= oldval
| (0x2 << (block
& 0x06));
762 this->bbt
[(block
>> 3)] = newval
;
763 if (oldval
!= newval
) update
= 1;
766 /* If we want reserved blocks to be recorded to flash, and some
767 new ones have been marked, then we need to update the stored
768 bbts. This should only happen once. */
769 if (update
&& td
->reserved_block_code
)
770 nand_update_bbt(mtd
, (block
- 2) << (this->bbt_erase_shift
- 1));
775 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
776 * @mtd: MTD device structure
777 * @bd: descriptor for the good/bad block search pattern
779 * The function checks, if a bad block table(s) is/are already
780 * available. If not it scans the device for manufacturer
781 * marked good / bad blocks and writes the bad block table(s) to
782 * the selected place.
784 * The bad block table memory is allocated here. It must be freed
785 * by calling the nand_free_bbt function.
788 int nand_scan_bbt (struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
790 struct nand_chip
*this = mtd
->priv
;
793 struct nand_bbt_descr
*td
= this->bbt_td
;
794 struct nand_bbt_descr
*md
= this->bbt_md
;
796 len
= mtd
->size
>> (this->bbt_erase_shift
+ 2);
797 /* Allocate memory (2bit per block) */
798 this->bbt
= kmalloc (len
, GFP_KERNEL
);
800 printk (KERN_ERR
"nand_scan_bbt: Out of memory\n");
803 /* Clear the memory bad block table */
804 memset (this->bbt
, 0x00, len
);
806 /* If no primary table decriptor is given, scan the device
807 * to build a memory based bad block table
810 return nand_memory_bbt(mtd
, bd
);
812 /* Allocate a temporary buffer for one eraseblock incl. oob */
813 len
= (1 << this->bbt_erase_shift
);
814 len
+= (len
>> this->page_shift
) * mtd
->oobsize
;
815 buf
= kmalloc (len
, GFP_KERNEL
);
817 printk (KERN_ERR
"nand_bbt: Out of memory\n");
823 /* Is the bbt at a given page ? */
824 if (td
->options
& NAND_BBT_ABSPAGE
) {
825 res
= read_abs_bbts (mtd
, buf
, td
, md
);
827 /* Search the bad block table using a pattern in oob */
828 res
= search_read_bbts (mtd
, buf
, td
, md
);
832 res
= check_create (mtd
, buf
, bd
);
834 /* Prevent the bbt regions from erasing / writing */
835 mark_bbt_region (mtd
, td
);
837 mark_bbt_region (mtd
, md
);
845 * nand_update_bbt - [NAND Interface] update bad block table(s)
846 * @mtd: MTD device structure
847 * @offs: the offset of the newly marked block
849 * The function updates the bad block table(s)
851 int nand_update_bbt (struct mtd_info
*mtd
, loff_t offs
)
853 struct nand_chip
*this = mtd
->priv
;
854 int len
, res
= 0, writeops
= 0;
857 struct nand_bbt_descr
*td
= this->bbt_td
;
858 struct nand_bbt_descr
*md
= this->bbt_md
;
860 if (!this->bbt
|| !td
)
863 len
= mtd
->size
>> (this->bbt_erase_shift
+ 2);
864 /* Allocate a temporary buffer for one eraseblock incl. oob */
865 len
= (1 << this->bbt_erase_shift
);
866 len
+= (len
>> this->page_shift
) * mtd
->oobsize
;
867 buf
= kmalloc (len
, GFP_KERNEL
);
869 printk (KERN_ERR
"nand_update_bbt: Out of memory\n");
873 writeops
= md
!= NULL
? 0x03 : 0x01;
875 /* Do we have a bbt per chip ? */
876 if (td
->options
& NAND_BBT_PERCHIP
) {
877 chip
= (int) (offs
>> this->chip_shift
);
888 /* Write the bad block table to the device ? */
889 if ((writeops
& 0x01) && (td
->options
& NAND_BBT_WRITE
)) {
890 res
= write_bbt (mtd
, buf
, td
, md
, chipsel
);
894 /* Write the mirror bad block table to the device ? */
895 if ((writeops
& 0x02) && md
&& (md
->options
& NAND_BBT_WRITE
)) {
896 res
= write_bbt (mtd
, buf
, md
, td
, chipsel
);
904 /* Define some generic bad / good block scan pattern which are used
905 * while scanning a device for factory marked good / bad blocks
907 * The memory based patterns just
909 static uint8_t scan_ff_pattern
[] = { 0xff, 0xff };
911 static struct nand_bbt_descr smallpage_memorybased
= {
915 .pattern
= scan_ff_pattern
918 static struct nand_bbt_descr largepage_memorybased
= {
922 .pattern
= scan_ff_pattern
925 static struct nand_bbt_descr smallpage_flashbased
= {
926 .options
= NAND_BBT_SCANEMPTY
| NAND_BBT_SCANALLPAGES
,
929 .pattern
= scan_ff_pattern
932 static struct nand_bbt_descr largepage_flashbased
= {
933 .options
= NAND_BBT_SCANEMPTY
| NAND_BBT_SCANALLPAGES
,
936 .pattern
= scan_ff_pattern
939 static uint8_t scan_agand_pattern
[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
941 static struct nand_bbt_descr agand_flashbased
= {
942 .options
= NAND_BBT_SCANEMPTY
| NAND_BBT_SCANALLPAGES
,
945 .pattern
= scan_agand_pattern
948 /* Generic flash bbt decriptors
950 static uint8_t bbt_pattern
[] = {'B', 'b', 't', '0' };
951 static uint8_t mirror_pattern
[] = {'1', 't', 'b', 'B' };
953 static struct nand_bbt_descr bbt_main_descr
= {
954 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
955 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
960 .pattern
= bbt_pattern
963 static struct nand_bbt_descr bbt_mirror_descr
= {
964 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
965 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
970 .pattern
= mirror_pattern
974 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
975 * @mtd: MTD device structure
977 * This function selects the default bad block table
978 * support for the device and calls the nand_scan_bbt function
981 int nand_default_bbt (struct mtd_info
*mtd
)
983 struct nand_chip
*this = mtd
->priv
;
985 /* Default for AG-AND. We must use a flash based
986 * bad block table as the devices have factory marked
987 * _good_ blocks. Erasing those blocks leads to loss
988 * of the good / bad information, so we _must_ store
989 * this information in a good / bad table during
992 if (this->options
& NAND_IS_AND
) {
993 /* Use the default pattern descriptors */
995 this->bbt_td
= &bbt_main_descr
;
996 this->bbt_md
= &bbt_mirror_descr
;
998 this->options
|= NAND_USE_FLASH_BBT
;
999 return nand_scan_bbt (mtd
, &agand_flashbased
);
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
;
1010 if (!this->badblock_pattern
) {
1011 this->badblock_pattern
= (mtd
->oobblock
> 512) ?
1012 &largepage_flashbased
: &smallpage_flashbased
;
1015 this->bbt_td
= NULL
;
1016 this->bbt_md
= NULL
;
1017 if (!this->badblock_pattern
) {
1018 this->badblock_pattern
= (mtd
->oobblock
> 512) ?
1019 &largepage_memorybased
: &smallpage_memorybased
;
1022 return nand_scan_bbt (mtd
, this->badblock_pattern
);
1026 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1027 * @mtd: MTD device structure
1028 * @offs: offset in the device
1029 * @allowbbt: allow access to bad block table region
1032 int nand_isbad_bbt (struct mtd_info
*mtd
, loff_t offs
, int allowbbt
)
1034 struct nand_chip
*this = mtd
->priv
;
1038 /* Get block number * 2 */
1039 block
= (int) (offs
>> (this->bbt_erase_shift
- 1));
1040 res
= (this->bbt
[block
>> 3] >> (block
& 0x06)) & 0x03;
1042 DEBUG (MTD_DEBUG_LEVEL2
, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1043 (unsigned int)offs
, res
, block
>> 1);
1046 case 0x00: return 0;
1047 case 0x01: return 1;
1048 case 0x02: return allowbbt
? 0 : 1;
1054 #endif /* CONFIG_NEW_NAND_CODE */