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 #if defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY)
60 #include <linux/mtd/compat.h>
61 #include <linux/mtd/mtd.h>
62 #include <linux/mtd/nand.h>
64 #include <asm/errno.h>
67 * check_pattern - [GENERIC] check if a pattern is in the buffer
68 * @buf: the buffer to search
69 * @len: the length of buffer to search
70 * @paglen: the pagelength
71 * @td: search pattern descriptor
73 * Check for a pattern at the given place. Used to search bad block
74 * tables and good / bad block identifiers.
75 * If the SCAN_EMPTY option is set then check, if all bytes except the
76 * pattern area contain 0xff
79 static int check_pattern (uint8_t *buf
, int len
, int paglen
, struct nand_bbt_descr
*td
)
84 end
= paglen
+ td
->offs
;
85 if (td
->options
& NAND_BBT_SCANEMPTY
) {
86 for (i
= 0; i
< end
; i
++) {
93 /* Compare the pattern */
94 for (i
= 0; i
< td
->len
; i
++) {
95 if (p
[i
] != td
->pattern
[i
])
101 if (td
->options
& NAND_BBT_SCANEMPTY
) {
102 for (i
= end
; i
< len
; i
++) {
111 * read_bbt - [GENERIC] Read the bad block table starting from page
112 * @mtd: MTD device structure
113 * @buf: temporary buffer
114 * @page: the starting page
115 * @num: the number of bbt descriptors to read
116 * @bits: number of bits per block
117 * @offs: offset in the memory table
118 * @reserved_block_code: Pattern to identify reserved blocks
120 * Read the bad block table starting from page.
123 static int read_bbt (struct mtd_info
*mtd
, uint8_t *buf
, int page
, int num
,
124 int bits
, int offs
, int reserved_block_code
)
126 int res
, i
, j
, act
= 0;
127 struct nand_chip
*this = mtd
->priv
;
128 size_t retlen
, len
, totlen
;
130 uint8_t msk
= (uint8_t) ((1 << bits
) - 1);
132 totlen
= (num
* bits
) >> 3;
133 from
= ((loff_t
)page
) << this->page_shift
;
136 len
= min (totlen
, (size_t) (1 << this->bbt_erase_shift
));
137 res
= mtd
->read_ecc (mtd
, from
, len
, &retlen
, buf
, NULL
, this->autooob
);
140 printk (KERN_INFO
"nand_bbt: Error reading bad block table\n");
143 printk (KERN_WARNING
"nand_bbt: ECC error while reading bad block table\n");
147 for (i
= 0; i
< len
; i
++) {
148 uint8_t dat
= buf
[i
];
149 for (j
= 0; j
< 8; j
+= bits
, act
+= 2) {
150 uint8_t tmp
= (dat
>> j
) & msk
;
153 if (reserved_block_code
&&
154 (tmp
== reserved_block_code
)) {
155 printk (KERN_DEBUG
"nand_read_bbt: Reserved block at 0x%08x\n",
156 ((offs
<< 2) + (act
>> 1)) << this->bbt_erase_shift
);
157 this->bbt
[offs
+ (act
>> 3)] |= 0x2 << (act
& 0x06);
160 /* Leave it for now, if its matured we can move this
161 * message to MTD_DEBUG_LEVEL0 */
162 printk (KERN_DEBUG
"nand_read_bbt: Bad block at 0x%08x\n",
163 ((offs
<< 2) + (act
>> 1)) << this->bbt_erase_shift
);
164 /* Factory marked bad or worn out ? */
166 this->bbt
[offs
+ (act
>> 3)] |= 0x3 << (act
& 0x06);
168 this->bbt
[offs
+ (act
>> 3)] |= 0x1 << (act
& 0x06);
178 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
179 * @mtd: MTD device structure
180 * @buf: temporary buffer
181 * @td: descriptor for the bad block table
182 * @chip: read the table for a specific chip, -1 read all chips.
183 * Applies only if NAND_BBT_PERCHIP option is set
185 * Read the bad block table for all chips starting at a given page
186 * We assume that the bbt bits are in consecutive order.
188 static int read_abs_bbt (struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
, int chip
)
190 struct nand_chip
*this = mtd
->priv
;
194 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
195 if (td
->options
& NAND_BBT_PERCHIP
) {
197 for (i
= 0; i
< this->numchips
; i
++) {
198 if (chip
== -1 || chip
== i
)
199 res
= read_bbt (mtd
, buf
, td
->pages
[i
], this->chipsize
>> this->bbt_erase_shift
, bits
, offs
, td
->reserved_block_code
);
202 offs
+= this->chipsize
>> (this->bbt_erase_shift
+ 2);
205 res
= read_bbt (mtd
, buf
, td
->pages
[0], mtd
->size
>> this->bbt_erase_shift
, bits
, 0, td
->reserved_block_code
);
213 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
214 * @mtd: MTD device structure
215 * @buf: temporary buffer
216 * @td: descriptor for the bad block table
217 * @md: descriptor for the bad block table mirror
219 * Read the bad block table(s) for all chips starting at a given page
220 * We assume that the bbt bits are in consecutive order.
223 static int read_abs_bbts (struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
,
224 struct nand_bbt_descr
*md
)
226 struct nand_chip
*this = mtd
->priv
;
228 /* Read the primary version, if available */
229 if (td
->options
& NAND_BBT_VERSION
) {
230 nand_read_raw (mtd
, buf
, td
->pages
[0] << this->page_shift
, mtd
->oobblock
, mtd
->oobsize
);
231 td
->version
[0] = buf
[mtd
->oobblock
+ td
->veroffs
];
232 printk (KERN_DEBUG
"Bad block table at page %d, version 0x%02X\n", td
->pages
[0], td
->version
[0]);
235 /* Read the mirror version, if available */
236 if (md
&& (md
->options
& NAND_BBT_VERSION
)) {
237 nand_read_raw (mtd
, buf
, md
->pages
[0] << this->page_shift
, mtd
->oobblock
, mtd
->oobsize
);
238 md
->version
[0] = buf
[mtd
->oobblock
+ md
->veroffs
];
239 printk (KERN_DEBUG
"Bad block table at page %d, version 0x%02X\n", md
->pages
[0], md
->version
[0]);
246 * create_bbt - [GENERIC] Create a bad block table by scanning the device
247 * @mtd: MTD device structure
248 * @buf: temporary buffer
249 * @bd: descriptor for the good/bad block search pattern
250 * @chip: create the table for a specific chip, -1 read all chips.
251 * Applies only if NAND_BBT_PERCHIP option is set
253 * Create a bad block table by scanning the device
254 * for the given good/bad block identify pattern
256 static void create_bbt (struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*bd
, int chip
)
258 struct nand_chip
*this = mtd
->priv
;
259 int i
, j
, numblocks
, len
, scanlen
;
262 size_t readlen
, ooblen
;
264 if (bd
->options
& NAND_BBT_SCANALLPAGES
)
265 len
= 1 << (this->bbt_erase_shift
- this->page_shift
);
267 if (bd
->options
& NAND_BBT_SCAN2NDPAGE
)
272 scanlen
= mtd
->oobblock
+ mtd
->oobsize
;
273 readlen
= len
* mtd
->oobblock
;
274 ooblen
= len
* mtd
->oobsize
;
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);
283 if (chip
>= this->numchips
) {
284 printk (KERN_WARNING
"create_bbt(): chipnr (%d) > available chips (%d)\n",
285 chip
+ 1, this->numchips
);
288 numblocks
= this->chipsize
>> (this->bbt_erase_shift
- 1);
289 startblock
= chip
* numblocks
;
290 numblocks
+= startblock
;
291 from
= startblock
<< (this->bbt_erase_shift
- 1);
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);
303 from
+= (1 << this->bbt_erase_shift
);
308 * search_bbt - [GENERIC] scan the device for a specific bad block table
309 * @mtd: MTD device structure
310 * @buf: temporary buffer
311 * @td: descriptor for the bad block table
313 * Read the bad block table by searching for a given ident pattern.
314 * Search is preformed either from the beginning up or from the end of
315 * the device downwards. The search starts always at the start of a
317 * If the option NAND_BBT_PERCHIP is given, each chip is searched
318 * for a bbt, which contains the bad block information of this chip.
319 * This is neccecary to provide support for certain DOC devices.
321 * The bbt ident pattern resides in the oob area of the first page
324 static int search_bbt (struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
)
326 struct nand_chip
*this = mtd
->priv
;
328 int bits
, startblock
, block
, dir
;
329 int scanlen
= mtd
->oobblock
+ mtd
->oobsize
;
332 /* Search direction top -> down ? */
333 if (td
->options
& NAND_BBT_LASTBLOCK
) {
334 startblock
= (mtd
->size
>> this->bbt_erase_shift
) -1;
341 /* Do we have a bbt per chip ? */
342 if (td
->options
& NAND_BBT_PERCHIP
) {
343 chips
= this->numchips
;
344 bbtblocks
= this->chipsize
>> this->bbt_erase_shift
;
345 startblock
&= bbtblocks
- 1;
348 bbtblocks
= mtd
->size
>> this->bbt_erase_shift
;
351 /* Number of bits for each erase block in the bbt */
352 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
354 for (i
= 0; i
< chips
; i
++) {
355 /* Reset version information */
358 /* Scan the maximum number of blocks */
359 for (block
= 0; block
< td
->maxblocks
; block
++) {
360 int actblock
= startblock
+ dir
* block
;
361 /* Read first page */
362 nand_read_raw (mtd
, buf
, actblock
<< this->bbt_erase_shift
, mtd
->oobblock
, mtd
->oobsize
);
363 if (!check_pattern(buf
, scanlen
, mtd
->oobblock
, td
)) {
364 td
->pages
[i
] = actblock
<< (this->bbt_erase_shift
- this->page_shift
);
365 if (td
->options
& NAND_BBT_VERSION
) {
366 td
->version
[i
] = buf
[mtd
->oobblock
+ td
->veroffs
];
371 startblock
+= this->chipsize
>> this->bbt_erase_shift
;
373 /* Check, if we found a bbt for each requested chip */
374 for (i
= 0; i
< chips
; i
++) {
375 if (td
->pages
[i
] == -1)
376 printk (KERN_WARNING
"Bad block table not found for chip %d\n", i
);
378 printk (KERN_DEBUG
"Bad block table found at page %d, version 0x%02X\n", td
->pages
[i
], td
->version
[i
]);
384 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
385 * @mtd: MTD device structure
386 * @buf: temporary buffer
387 * @td: descriptor for the bad block table
388 * @md: descriptor for the bad block table mirror
390 * Search and read the bad block table(s)
392 static int search_read_bbts (struct mtd_info
*mtd
, uint8_t *buf
,
393 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
)
395 /* Search the primary table */
396 search_bbt (mtd
, buf
, td
);
398 /* Search the mirror table */
400 search_bbt (mtd
, buf
, md
);
402 /* Force result check */
408 * write_bbt - [GENERIC] (Re)write the bad block table
410 * @mtd: MTD device structure
411 * @buf: temporary buffer
412 * @td: descriptor for the bad block table
413 * @md: descriptor for the bad block table mirror
414 * @chipsel: selector for a specific chip, -1 for all
416 * (Re)write the bad block table
419 static int write_bbt (struct mtd_info
*mtd
, uint8_t *buf
,
420 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
, int chipsel
)
422 struct nand_chip
*this = mtd
->priv
;
423 struct nand_oobinfo oobinfo
;
424 struct erase_info einfo
;
425 int i
, j
, res
, chip
= 0;
426 int bits
, startblock
, dir
, page
, offs
, numblocks
, sft
, sftmsk
;
427 int nrchips
, bbtoffs
, pageoffs
;
429 uint8_t rcode
= td
->reserved_block_code
;
430 size_t retlen
, len
= 0;
435 /* Write bad block table per chip rather than per device ? */
436 if (td
->options
& NAND_BBT_PERCHIP
) {
437 numblocks
= (int) (this->chipsize
>> this->bbt_erase_shift
);
438 /* Full device write or specific chip ? */
440 nrchips
= this->numchips
;
442 nrchips
= chipsel
+ 1;
446 numblocks
= (int) (mtd
->size
>> this->bbt_erase_shift
);
450 /* Loop through the chips */
451 for (; chip
< nrchips
; chip
++) {
453 /* There was already a version of the table, reuse the page
454 * This applies for absolute placement too, as we have the
455 * page nr. in td->pages.
457 if (td
->pages
[chip
] != -1) {
458 page
= td
->pages
[chip
];
462 /* Automatic placement of the bad block table */
463 /* Search direction top -> down ? */
464 if (td
->options
& NAND_BBT_LASTBLOCK
) {
465 startblock
= numblocks
* (chip
+ 1) - 1;
468 startblock
= chip
* numblocks
;
472 for (i
= 0; i
< td
->maxblocks
; i
++) {
473 int block
= startblock
+ dir
* i
;
474 /* Check, if the block is bad */
475 switch ((this->bbt
[block
>> 2] >> (2 * (block
& 0x03))) & 0x03) {
480 page
= block
<< (this->bbt_erase_shift
- this->page_shift
);
481 /* Check, if the block is used by the mirror table */
482 if (!md
|| md
->pages
[chip
] != page
)
485 printk (KERN_ERR
"No space left to write bad block table\n");
489 /* Set up shift count and masks for the flash table */
490 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
492 case 1: sft
= 3; sftmsk
= 0x07; msk
[0] = 0x00; msk
[1] = 0x01; msk
[2] = ~rcode
; msk
[3] = 0x01; break;
493 case 2: sft
= 2; sftmsk
= 0x06; msk
[0] = 0x00; msk
[1] = 0x01; msk
[2] = ~rcode
; msk
[3] = 0x03; break;
494 case 4: sft
= 1; sftmsk
= 0x04; msk
[0] = 0x00; msk
[1] = 0x0C; msk
[2] = ~rcode
; msk
[3] = 0x0f; break;
495 case 8: sft
= 0; sftmsk
= 0x00; msk
[0] = 0x00; msk
[1] = 0x0F; msk
[2] = ~rcode
; msk
[3] = 0xff; break;
496 default: return -EINVAL
;
499 bbtoffs
= chip
* (numblocks
>> 2);
501 to
= ((loff_t
) page
) << this->page_shift
;
503 memcpy (&oobinfo
, this->autooob
, sizeof(oobinfo
));
504 oobinfo
.useecc
= MTD_NANDECC_PLACEONLY
;
506 /* Must we save the block contents ? */
507 if (td
->options
& NAND_BBT_SAVECONTENT
) {
508 /* Make it block aligned */
509 to
&= ~((loff_t
) ((1 << this->bbt_erase_shift
) - 1));
510 len
= 1 << this->bbt_erase_shift
;
511 res
= mtd
->read_ecc (mtd
, to
, len
, &retlen
, buf
, &buf
[len
], &oobinfo
);
514 printk (KERN_INFO
"nand_bbt: Error reading block for writing the bad block table\n");
517 printk (KERN_WARNING
"nand_bbt: ECC error while reading block for writing bad block table\n");
519 /* Calc the byte offset in the buffer */
520 pageoffs
= page
- (int)(to
>> this->page_shift
);
521 offs
= pageoffs
<< this->page_shift
;
522 /* Preset the bbt area with 0xff */
523 memset (&buf
[offs
], 0xff, (size_t)(numblocks
>> sft
));
524 /* Preset the bbt's oob area with 0xff */
525 memset (&buf
[len
+ pageoffs
* mtd
->oobsize
], 0xff,
526 ((len
>> this->page_shift
) - pageoffs
) * mtd
->oobsize
);
527 if (td
->options
& NAND_BBT_VERSION
) {
528 buf
[len
+ (pageoffs
* mtd
->oobsize
) + td
->veroffs
] = td
->version
[chip
];
532 len
= (size_t) (numblocks
>> sft
);
533 /* Make it page aligned ! */
534 len
= (len
+ (mtd
->oobblock
-1)) & ~(mtd
->oobblock
-1);
535 /* Preset the buffer with 0xff */
536 memset (buf
, 0xff, len
+ (len
>> this->page_shift
) * mtd
->oobsize
);
538 /* Pattern is located in oob area of first page */
539 memcpy (&buf
[len
+ td
->offs
], td
->pattern
, td
->len
);
540 if (td
->options
& NAND_BBT_VERSION
) {
541 buf
[len
+ td
->veroffs
] = td
->version
[chip
];
545 /* walk through the memory table */
546 for (i
= 0; i
< numblocks
; ) {
548 dat
= this->bbt
[bbtoffs
+ (i
>> 2)];
549 for (j
= 0; j
< 4; j
++ , i
++) {
550 int sftcnt
= (i
<< (3 - sft
)) & sftmsk
;
551 /* Do not store the reserved bbt blocks ! */
552 buf
[offs
+ (i
>> sft
)] &= ~(msk
[dat
& 0x03] << sftcnt
);
557 memset (&einfo
, 0, sizeof (einfo
));
559 einfo
.addr
= (unsigned long) to
;
560 einfo
.len
= 1 << this->bbt_erase_shift
;
561 res
= nand_erase_nand (mtd
, &einfo
, 1);
563 printk (KERN_WARNING
"nand_bbt: Error during block erase: %d\n", res
);
567 res
= mtd
->write_ecc (mtd
, to
, len
, &retlen
, buf
, &buf
[len
], &oobinfo
);
569 printk (KERN_WARNING
"nand_bbt: Error while writing bad block table %d\n", res
);
572 printk (KERN_DEBUG
"Bad block table written to 0x%08x, version 0x%02X\n",
573 (unsigned int) to
, td
->version
[chip
]);
575 /* Mark it as used */
576 td
->pages
[chip
] = page
;
582 * nand_memory_bbt - [GENERIC] create a memory based bad block table
583 * @mtd: MTD device structure
584 * @bd: descriptor for the good/bad block search pattern
586 * The function creates a memory based bbt by scanning the device
587 * for manufacturer / software marked good / bad blocks
589 static int nand_memory_bbt (struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
591 struct nand_chip
*this = mtd
->priv
;
593 /* Ensure that we only scan for the pattern and nothing else */
595 create_bbt (mtd
, this->data_buf
, bd
, -1);
600 * check_create - [GENERIC] create and write bbt(s) if neccecary
601 * @mtd: MTD device structure
602 * @buf: temporary buffer
603 * @bd: descriptor for the good/bad block search pattern
605 * The function checks the results of the previous call to read_bbt
606 * and creates / updates the bbt(s) if neccecary
607 * Creation is neccecary if no bbt was found for the chip/device
608 * Update is neccecary if one of the tables is missing or the
609 * version nr. of one table is less than the other
611 static int check_create (struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*bd
)
613 int i
, chips
, writeops
, chipsel
, res
;
614 struct nand_chip
*this = mtd
->priv
;
615 struct nand_bbt_descr
*td
= this->bbt_td
;
616 struct nand_bbt_descr
*md
= this->bbt_md
;
617 struct nand_bbt_descr
*rd
, *rd2
;
619 /* Do we have a bbt per chip ? */
620 if (td
->options
& NAND_BBT_PERCHIP
)
621 chips
= this->numchips
;
625 for (i
= 0; i
< chips
; i
++) {
629 /* Per chip or per device ? */
630 chipsel
= (td
->options
& NAND_BBT_PERCHIP
) ? i
: -1;
631 /* Mirrored table avilable ? */
633 if (td
->pages
[i
] == -1 && md
->pages
[i
] == -1) {
638 if (td
->pages
[i
] == -1) {
640 td
->version
[i
] = md
->version
[i
];
645 if (md
->pages
[i
] == -1) {
647 md
->version
[i
] = td
->version
[i
];
652 if (td
->version
[i
] == md
->version
[i
]) {
654 if (!(td
->options
& NAND_BBT_VERSION
))
659 if (((int8_t) (td
->version
[i
] - md
->version
[i
])) > 0) {
661 md
->version
[i
] = td
->version
[i
];
665 td
->version
[i
] = md
->version
[i
];
672 if (td
->pages
[i
] == -1) {
680 /* Create the bad block table by scanning the device ? */
681 if (!(td
->options
& NAND_BBT_CREATE
) ||
682 (this->options
& NAND_DONT_CREATE_BBT
))
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) */
799 this->bbt
= kmalloc (len
, GFP_KERNEL
);
801 printk (KERN_ERR
"nand_scan_bbt: Out of memory\n");
804 /* Clear the memory bad block table */
805 memset (this->bbt
, 0x00, len
);
807 /* If no primary table decriptor is given, scan the device
808 * to build a memory based bad block table
811 return nand_memory_bbt(mtd
, bd
);
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
);
818 printk (KERN_ERR
"nand_bbt: Out of memory\n");
824 /* Is the bbt at a given page ? */
825 if (td
->options
& NAND_BBT_ABSPAGE
) {
826 res
= read_abs_bbts (mtd
, buf
, td
, md
);
828 /* Search the bad block table using a pattern in oob */
829 res
= search_read_bbts (mtd
, buf
, td
, md
);
833 res
= check_create (mtd
, buf
, bd
);
835 /* Prevent the bbt regions from erasing / writing */
836 mark_bbt_region (mtd
, td
);
838 mark_bbt_region (mtd
, md
);
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
850 * The function updates the bad block table(s)
852 int nand_update_bbt (struct mtd_info
*mtd
, loff_t offs
)
854 struct nand_chip
*this = mtd
->priv
;
855 int len
, res
= 0, writeops
= 0;
858 struct nand_bbt_descr
*td
= this->bbt_td
;
859 struct nand_bbt_descr
*md
= this->bbt_md
;
861 if (!this->bbt
|| !td
)
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
);
870 printk (KERN_ERR
"nand_update_bbt: Out of memory\n");
874 writeops
= md
!= NULL
? 0x03 : 0x01;
876 /* Do we have a bbt per chip ? */
877 if (td
->options
& NAND_BBT_PERCHIP
) {
878 chip
= (int) (offs
>> this->chip_shift
);
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
);
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
);
905 /* Define some generic bad / good block scan pattern which are used
906 * while scanning a device for factory marked good / bad blocks
908 * The memory based patterns just
910 static uint8_t scan_ff_pattern
[] = { 0xff, 0xff };
912 static struct nand_bbt_descr smallpage_memorybased
= {
916 .pattern
= scan_ff_pattern
919 static struct nand_bbt_descr largepage_memorybased
= {
923 .pattern
= scan_ff_pattern
926 static struct nand_bbt_descr smallpage_flashbased
= {
927 .options
= NAND_BBT_SCANEMPTY
| NAND_BBT_SCANALLPAGES
,
930 .pattern
= scan_ff_pattern
933 static struct nand_bbt_descr largepage_flashbased
= {
934 .options
= NAND_BBT_SCANEMPTY
| NAND_BBT_SCANALLPAGES
,
937 .pattern
= scan_ff_pattern
940 static uint8_t scan_agand_pattern
[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
942 static struct nand_bbt_descr agand_flashbased
= {
943 .options
= NAND_BBT_SCANEMPTY
| NAND_BBT_SCANALLPAGES
,
946 .pattern
= scan_agand_pattern
949 /* Generic flash bbt decriptors
951 static uint8_t bbt_pattern
[] = {'B', 'b', 't', '0' };
952 static uint8_t mirror_pattern
[] = {'1', 't', 'b', 'B' };
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
,
961 .pattern
= bbt_pattern
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
,
971 .pattern
= mirror_pattern
975 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
976 * @mtd: MTD device structure
978 * This function selects the default bad block table
979 * support for the device and calls the nand_scan_bbt function
982 int nand_default_bbt (struct mtd_info
*mtd
)
984 struct nand_chip
*this = mtd
->priv
;
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
993 if (this->options
& NAND_IS_AND
) {
994 /* Use the default pattern descriptors */
996 this->bbt_td
= &bbt_main_descr
;
997 this->bbt_md
= &bbt_mirror_descr
;
999 this->options
|= NAND_USE_FLASH_BBT
;
1000 return nand_scan_bbt (mtd
, &agand_flashbased
);
1004 /* Is a flash based bad block table requested ? */
1005 if (this->options
& NAND_USE_FLASH_BBT
) {
1006 /* Use the default pattern descriptors */
1007 if (!this->bbt_td
) {
1008 this->bbt_td
= &bbt_main_descr
;
1009 this->bbt_md
= &bbt_mirror_descr
;
1011 if (!this->badblock_pattern
) {
1012 this->badblock_pattern
= (mtd
->oobblock
> 512) ?
1013 &largepage_flashbased
: &smallpage_flashbased
;
1016 this->bbt_td
= NULL
;
1017 this->bbt_md
= NULL
;
1018 if (!this->badblock_pattern
) {
1019 this->badblock_pattern
= (mtd
->oobblock
> 512) ?
1020 &largepage_memorybased
: &smallpage_memorybased
;
1023 return nand_scan_bbt (mtd
, this->badblock_pattern
);
1027 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1028 * @mtd: MTD device structure
1029 * @offs: offset in the device
1030 * @allowbbt: allow access to bad block table region
1033 int nand_isbad_bbt (struct mtd_info
*mtd
, loff_t offs
, int allowbbt
)
1035 struct nand_chip
*this = mtd
->priv
;
1039 /* Get block number * 2 */
1040 block
= (int) (offs
>> (this->bbt_erase_shift
- 1));
1041 res
= (this->bbt
[block
>> 3] >> (block
& 0x06)) & 0x03;
1043 DEBUG (MTD_DEBUG_LEVEL2
, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1044 (unsigned int)offs
, res
, block
>> 1);
1047 case 0x00: return 0;
1048 case 0x01: return 1;
1049 case 0x03: return 1;
1050 case 0x02: return allowbbt
? 0 : 1;
1055 #if defined(CONFIG_NAND_DYNPART)
1057 extern unsigned int dynpart_size
[];
1058 extern char *dynpart_names
[];
1060 #define MTDPARTS_MAX_SIZE 512
1063 static int skip_offs(const struct nand_chip
*this, unsigned int offs
)
1065 int block
= (int) (offs
>> (this->bbt_erase_shift
- 1));
1066 u_int8_t bbt
= (this->bbt
[block
>> 3] >> (block
& 0x06)) & 0x03;
1071 int nand_create_mtd_dynpart(struct mtd_info
*mtd
)
1073 struct nand_chip
*this = mtd
->priv
;
1076 unsigned int cur_offs
= 0;
1078 mtdparts
= malloc(MTDPARTS_MAX_SIZE
); /* FIXME: bounds checking */
1082 sprintf(mtdparts
, "mtdparts=" CFG_NAND_DYNPART_MTD_KERNEL_NAME
":");
1084 for (part
= 0; dynpart_size
[part
] != 0; part
++) {
1085 unsigned int bb_delta
= 0;
1086 unsigned int offs
= 0;
1089 for (offs
= cur_offs
;
1090 offs
< cur_offs
+ dynpart_size
[part
] + bb_delta
;
1091 offs
+= mtd
->erasesize
) {
1092 if (skip_offs(this, offs
))
1093 bb_delta
+= mtd
->erasesize
;
1097 * Absorb bad blocks immediately following this partition also
1098 * into the partition, in order to make next partition start
1099 * with a good block. This simplifies handling of the
1100 * environment partition.
1102 while (offs
< this->chipsize
&& skip_offs(this, offs
)) {
1103 bb_delta
+= mtd
->erasesize
;
1104 offs
+= mtd
->erasesize
;
1107 if (cur_offs
+ dynpart_size
[part
] + bb_delta
> this->chipsize
)
1108 dynpart_size
[part
] = this->chipsize
- cur_offs
- bb_delta
;
1110 printf("partition %u: start = 0x%08x, end=%08x size=%08x, size_inc_bb=%08x\n",
1111 part
, cur_offs
, cur_offs
+ dynpart_size
[part
] + bb_delta
,
1112 dynpart_size
[part
], dynpart_size
[part
] + bb_delta
);
1114 cur_offs
+= dynpart_size
[part
] + bb_delta
;
1115 sprintf(mtdpart
, "0x%.8x(%.16s),", dynpart_size
[part
] + bb_delta
,
1116 dynpart_names
[part
]);
1117 mtdpart
[sizeof(mtdpart
)-1] = '\0';
1118 strncat(mtdparts
, mtdpart
,
1119 MTDPARTS_MAX_SIZE
-strlen(mtdparts
)-1);
1122 mtdparts
[strlen(mtdparts
)-1] = '\0';
1123 printf("mtdparts %s\n", mtdparts
);
1124 setenv("mtdparts", mtdparts
);
1129 #endif /* CONFIG_NAND_DYNPART */