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 /* Factory marked bad or worn out ? */
162 this->bbt
[offs
+ (act
>> 3)] |= 0x3 << (act
& 0x06);
164 this->bbt
[offs
+ (act
>> 3)] |= 0x1 << (act
& 0x06);
174 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
175 * @mtd: MTD device structure
176 * @buf: temporary buffer
177 * @td: descriptor for the bad block table
178 * @chip: read the table for a specific chip, -1 read all chips.
179 * Applies only if NAND_BBT_PERCHIP option is set
181 * Read the bad block table for all chips starting at a given page
182 * We assume that the bbt bits are in consecutive order.
184 static int read_abs_bbt (struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
, int chip
)
186 struct nand_chip
*this = mtd
->priv
;
190 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
191 if (td
->options
& NAND_BBT_PERCHIP
) {
193 for (i
= 0; i
< this->numchips
; i
++) {
194 if (chip
== -1 || chip
== i
)
195 res
= read_bbt (mtd
, buf
, td
->pages
[i
], this->chipsize
>> this->bbt_erase_shift
, bits
, offs
, td
->reserved_block_code
);
198 offs
+= this->chipsize
>> (this->bbt_erase_shift
+ 2);
201 res
= read_bbt (mtd
, buf
, td
->pages
[0], mtd
->size
>> this->bbt_erase_shift
, bits
, 0, td
->reserved_block_code
);
209 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
210 * @mtd: MTD device structure
211 * @buf: temporary buffer
212 * @td: descriptor for the bad block table
213 * @md: descriptor for the bad block table mirror
215 * Read the bad block table(s) for all chips starting at a given page
216 * We assume that the bbt bits are in consecutive order.
219 static int read_abs_bbts (struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
,
220 struct nand_bbt_descr
*md
)
222 struct nand_chip
*this = mtd
->priv
;
224 /* Read the primary version, if available */
225 if (td
->options
& NAND_BBT_VERSION
) {
226 nand_read_raw (mtd
, buf
, td
->pages
[0] << this->page_shift
, mtd
->oobblock
, mtd
->oobsize
);
227 td
->version
[0] = buf
[mtd
->oobblock
+ td
->veroffs
];
230 /* Read the mirror version, if available */
231 if (md
&& (md
->options
& NAND_BBT_VERSION
)) {
232 nand_read_raw (mtd
, buf
, md
->pages
[0] << this->page_shift
, mtd
->oobblock
, mtd
->oobsize
);
233 md
->version
[0] = buf
[mtd
->oobblock
+ md
->veroffs
];
240 * create_bbt - [GENERIC] Create a bad block table by scanning the device
241 * @mtd: MTD device structure
242 * @buf: temporary buffer
243 * @bd: descriptor for the good/bad block search pattern
244 * @chip: create the table for a specific chip, -1 read all chips.
245 * Applies only if NAND_BBT_PERCHIP option is set
247 * Create a bad block table by scanning the device
248 * for the given good/bad block identify pattern
250 static void create_bbt (struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*bd
, int chip
)
252 struct nand_chip
*this = mtd
->priv
;
253 int i
, j
, numblocks
, len
, scanlen
;
256 size_t readlen
, ooblen
;
258 if (bd
->options
& NAND_BBT_SCANALLPAGES
)
259 len
= 1 << (this->bbt_erase_shift
- this->page_shift
);
261 if (bd
->options
& NAND_BBT_SCAN2NDPAGE
)
266 scanlen
= mtd
->oobblock
+ mtd
->oobsize
;
267 readlen
= len
* mtd
->oobblock
;
268 ooblen
= len
* mtd
->oobsize
;
271 /* Note that numblocks is 2 * (real numblocks) here, see i+=2 below as it
272 * makes shifting and masking less painful */
273 numblocks
= mtd
->size
>> (this->bbt_erase_shift
- 1);
277 if (chip
>= this->numchips
) {
278 printk (KERN_WARNING
"create_bbt(): chipnr (%d) > available chips (%d)\n",
279 chip
+ 1, this->numchips
);
282 numblocks
= this->chipsize
>> (this->bbt_erase_shift
- 1);
283 startblock
= chip
* numblocks
;
284 numblocks
+= startblock
;
285 from
= startblock
<< (this->bbt_erase_shift
- 1);
288 for (i
= startblock
; i
< numblocks
;) {
289 nand_read_raw (mtd
, buf
, from
, readlen
, ooblen
);
290 for (j
= 0; j
< len
; j
++) {
291 if (check_pattern (&buf
[j
* scanlen
], scanlen
, mtd
->oobblock
, bd
)) {
292 this->bbt
[i
>> 3] |= 0x03 << (i
& 0x6);
297 from
+= (1 << this->bbt_erase_shift
);
302 * search_bbt - [GENERIC] scan the device for a specific bad block table
303 * @mtd: MTD device structure
304 * @buf: temporary buffer
305 * @td: descriptor for the bad block table
307 * Read the bad block table by searching for a given ident pattern.
308 * Search is preformed either from the beginning up or from the end of
309 * the device downwards. The search starts always at the start of a
311 * If the option NAND_BBT_PERCHIP is given, each chip is searched
312 * for a bbt, which contains the bad block information of this chip.
313 * This is neccecary to provide support for certain DOC devices.
315 * The bbt ident pattern resides in the oob area of the first page
318 static int search_bbt (struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
)
320 struct nand_chip
*this = mtd
->priv
;
322 int bits
, startblock
, block
, dir
;
323 int scanlen
= mtd
->oobblock
+ mtd
->oobsize
;
326 /* Search direction top -> down ? */
327 if (td
->options
& NAND_BBT_LASTBLOCK
) {
328 startblock
= (mtd
->size
>> this->bbt_erase_shift
) -1;
335 /* Do we have a bbt per chip ? */
336 if (td
->options
& NAND_BBT_PERCHIP
) {
337 chips
= this->numchips
;
338 bbtblocks
= this->chipsize
>> this->bbt_erase_shift
;
339 startblock
&= bbtblocks
- 1;
342 bbtblocks
= mtd
->size
>> this->bbt_erase_shift
;
345 /* Number of bits for each erase block in the bbt */
346 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
348 for (i
= 0; i
< chips
; i
++) {
349 /* Reset version information */
352 /* Scan the maximum number of blocks */
353 for (block
= 0; block
< td
->maxblocks
; block
++) {
354 int actblock
= startblock
+ dir
* block
;
355 /* Read first page */
356 nand_read_raw (mtd
, buf
, actblock
<< this->bbt_erase_shift
, mtd
->oobblock
, mtd
->oobsize
);
357 if (!check_pattern(buf
, scanlen
, mtd
->oobblock
, td
)) {
358 td
->pages
[i
] = actblock
<< (this->bbt_erase_shift
- this->page_shift
);
359 if (td
->options
& NAND_BBT_VERSION
) {
360 td
->version
[i
] = buf
[mtd
->oobblock
+ td
->veroffs
];
365 startblock
+= this->chipsize
>> this->bbt_erase_shift
;
367 /* Check, if we found a bbt for each requested chip */
368 for (i
= 0; i
< chips
; i
++) {
369 if (td
->pages
[i
] == -1)
370 printk (KERN_WARNING
"Bad block table not found for chip %d\n", i
);
376 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
377 * @mtd: MTD device structure
378 * @buf: temporary buffer
379 * @td: descriptor for the bad block table
380 * @md: descriptor for the bad block table mirror
382 * Search and read the bad block table(s)
384 static int search_read_bbts (struct mtd_info
*mtd
, uint8_t *buf
,
385 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
)
387 /* Search the primary table */
388 search_bbt (mtd
, buf
, td
);
390 /* Search the mirror table */
392 search_bbt (mtd
, buf
, md
);
394 /* Force result check */
400 * write_bbt - [GENERIC] (Re)write the bad block table
402 * @mtd: MTD device structure
403 * @buf: temporary buffer
404 * @td: descriptor for the bad block table
405 * @md: descriptor for the bad block table mirror
406 * @chipsel: selector for a specific chip, -1 for all
408 * (Re)write the bad block table
411 static int write_bbt (struct mtd_info
*mtd
, uint8_t *buf
,
412 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
, int chipsel
)
414 struct nand_chip
*this = mtd
->priv
;
415 struct nand_oobinfo oobinfo
;
416 struct erase_info einfo
;
417 int i
, j
, res
, chip
= 0;
418 int bits
, startblock
, dir
, page
, offs
, numblocks
, sft
, sftmsk
;
419 int nrchips
, bbtoffs
, pageoffs
;
421 uint8_t rcode
= td
->reserved_block_code
;
422 size_t retlen
, len
= 0;
427 /* Write bad block table per chip rather than per device ? */
428 if (td
->options
& NAND_BBT_PERCHIP
) {
429 numblocks
= (int) (this->chipsize
>> this->bbt_erase_shift
);
430 /* Full device write or specific chip ? */
432 nrchips
= this->numchips
;
434 nrchips
= chipsel
+ 1;
438 numblocks
= (int) (mtd
->size
>> this->bbt_erase_shift
);
442 /* Loop through the chips */
443 for (; chip
< nrchips
; chip
++) {
445 /* There was already a version of the table, reuse the page
446 * This applies for absolute placement too, as we have the
447 * page nr. in td->pages.
449 if (td
->pages
[chip
] != -1) {
450 page
= td
->pages
[chip
];
454 /* Automatic placement of the bad block table */
455 /* Search direction top -> down ? */
456 if (td
->options
& NAND_BBT_LASTBLOCK
) {
457 startblock
= numblocks
* (chip
+ 1) - 1;
460 startblock
= chip
* numblocks
;
464 for (i
= 0; i
< td
->maxblocks
; i
++) {
465 int block
= startblock
+ dir
* i
;
466 /* Check, if the block is bad */
467 switch ((this->bbt
[block
>> 2] >> (2 * (block
& 0x03))) & 0x03) {
472 page
= block
<< (this->bbt_erase_shift
- this->page_shift
);
473 /* Check, if the block is used by the mirror table */
474 if (!md
|| md
->pages
[chip
] != page
)
477 printk (KERN_ERR
"No space left to write bad block table\n");
481 /* Set up shift count and masks for the flash table */
482 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
484 case 1: sft
= 3; sftmsk
= 0x07; msk
[0] = 0x00; msk
[1] = 0x01; msk
[2] = ~rcode
; msk
[3] = 0x01; break;
485 case 2: sft
= 2; sftmsk
= 0x06; msk
[0] = 0x00; msk
[1] = 0x01; msk
[2] = ~rcode
; msk
[3] = 0x03; break;
486 case 4: sft
= 1; sftmsk
= 0x04; msk
[0] = 0x00; msk
[1] = 0x0C; msk
[2] = ~rcode
; msk
[3] = 0x0f; break;
487 case 8: sft
= 0; sftmsk
= 0x00; msk
[0] = 0x00; msk
[1] = 0x0F; msk
[2] = ~rcode
; msk
[3] = 0xff; break;
488 default: return -EINVAL
;
491 bbtoffs
= chip
* (numblocks
>> 2);
493 to
= ((loff_t
) page
) << this->page_shift
;
495 memcpy (&oobinfo
, this->autooob
, sizeof(oobinfo
));
496 oobinfo
.useecc
= MTD_NANDECC_PLACEONLY
;
498 /* Must we save the block contents ? */
499 if (td
->options
& NAND_BBT_SAVECONTENT
) {
500 /* Make it block aligned */
501 to
&= ~((loff_t
) ((1 << this->bbt_erase_shift
) - 1));
502 len
= 1 << this->bbt_erase_shift
;
503 res
= mtd
->read_ecc (mtd
, to
, len
, &retlen
, buf
, &buf
[len
], &oobinfo
);
506 printk (KERN_INFO
"nand_bbt: Error reading block for writing the bad block table\n");
509 printk (KERN_WARNING
"nand_bbt: ECC error while reading block for writing bad block table\n");
511 /* Calc the byte offset in the buffer */
512 pageoffs
= page
- (int)(to
>> this->page_shift
);
513 offs
= pageoffs
<< this->page_shift
;
514 /* Preset the bbt area with 0xff */
515 memset (&buf
[offs
], 0xff, (size_t)(numblocks
>> sft
));
516 /* Preset the bbt's oob area with 0xff */
517 memset (&buf
[len
+ pageoffs
* mtd
->oobsize
], 0xff,
518 ((len
>> this->page_shift
) - pageoffs
) * mtd
->oobsize
);
519 if (td
->options
& NAND_BBT_VERSION
) {
520 buf
[len
+ (pageoffs
* mtd
->oobsize
) + td
->veroffs
] = td
->version
[chip
];
524 len
= (size_t) (numblocks
>> sft
);
525 /* Make it page aligned ! */
526 len
= (len
+ (mtd
->oobblock
-1)) & ~(mtd
->oobblock
-1);
527 /* Preset the buffer with 0xff */
528 memset (buf
, 0xff, len
+ (len
>> this->page_shift
) * mtd
->oobsize
);
530 /* Pattern is located in oob area of first page */
531 memcpy (&buf
[len
+ td
->offs
], td
->pattern
, td
->len
);
532 if (td
->options
& NAND_BBT_VERSION
) {
533 buf
[len
+ td
->veroffs
] = td
->version
[chip
];
537 /* walk through the memory table */
538 for (i
= 0; i
< numblocks
; ) {
540 dat
= this->bbt
[bbtoffs
+ (i
>> 2)];
541 for (j
= 0; j
< 4; j
++ , i
++) {
542 int sftcnt
= (i
<< (3 - sft
)) & sftmsk
;
543 /* Do not store the reserved bbt blocks ! */
544 buf
[offs
+ (i
>> sft
)] &= ~(msk
[dat
& 0x03] << sftcnt
);
549 memset (&einfo
, 0, sizeof (einfo
));
551 einfo
.addr
= (unsigned long) to
;
552 einfo
.len
= 1 << this->bbt_erase_shift
;
553 res
= nand_erase_nand (mtd
, &einfo
, 1);
555 printk (KERN_WARNING
"nand_bbt: Error during block erase: %d\n", res
);
559 res
= mtd
->write_ecc (mtd
, to
, len
, &retlen
, buf
, &buf
[len
], &oobinfo
);
561 printk (KERN_WARNING
"nand_bbt: Error while writing bad block table %d\n", res
);
564 printk (KERN_DEBUG
"Bad block table written to 0x%08x, version 0x%02X\n",
565 (unsigned int) to
, td
->version
[chip
]);
567 /* Mark it as used */
568 td
->pages
[chip
] = page
;
574 * nand_memory_bbt - [GENERIC] create a memory based bad block table
575 * @mtd: MTD device structure
576 * @bd: descriptor for the good/bad block search pattern
578 * The function creates a memory based bbt by scanning the device
579 * for manufacturer / software marked good / bad blocks
581 static int nand_memory_bbt (struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
583 struct nand_chip
*this = mtd
->priv
;
585 /* Ensure that we only scan for the pattern and nothing else */
587 create_bbt (mtd
, this->data_buf
, bd
, -1);
592 * check_create - [GENERIC] create and write bbt(s) if neccecary
593 * @mtd: MTD device structure
594 * @buf: temporary buffer
595 * @bd: descriptor for the good/bad block search pattern
597 * The function checks the results of the previous call to read_bbt
598 * and creates / updates the bbt(s) if neccecary
599 * Creation is neccecary if no bbt was found for the chip/device
600 * Update is neccecary if one of the tables is missing or the
601 * version nr. of one table is less than the other
603 static int check_create (struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*bd
)
605 int i
, chips
, writeops
, chipsel
, res
;
606 struct nand_chip
*this = mtd
->priv
;
607 struct nand_bbt_descr
*td
= this->bbt_td
;
608 struct nand_bbt_descr
*md
= this->bbt_md
;
609 struct nand_bbt_descr
*rd
, *rd2
;
611 /* Do we have a bbt per chip ? */
612 if (td
->options
& NAND_BBT_PERCHIP
)
613 chips
= this->numchips
;
617 for (i
= 0; i
< chips
; i
++) {
621 /* Per chip or per device ? */
622 chipsel
= (td
->options
& NAND_BBT_PERCHIP
) ? i
: -1;
623 /* Mirrored table avilable ? */
625 if (td
->pages
[i
] == -1 && md
->pages
[i
] == -1) {
630 if (td
->pages
[i
] == -1) {
632 td
->version
[i
] = md
->version
[i
];
637 if (md
->pages
[i
] == -1) {
639 md
->version
[i
] = td
->version
[i
];
644 if (td
->version
[i
] == md
->version
[i
]) {
646 if (!(td
->options
& NAND_BBT_VERSION
))
651 if (((int8_t) (td
->version
[i
] - md
->version
[i
])) > 0) {
653 md
->version
[i
] = td
->version
[i
];
657 td
->version
[i
] = md
->version
[i
];
664 if (td
->pages
[i
] == -1) {
672 /* Create the bad block table by scanning the device ? */
673 if (!(td
->options
& NAND_BBT_CREATE
) ||
674 (this->options
& NAND_DONT_CREATE_BBT
))
677 /* Create the table in memory by scanning the chip(s) */
678 create_bbt (mtd
, buf
, bd
, chipsel
);
684 /* read back first ? */
686 read_abs_bbt (mtd
, buf
, rd
, chipsel
);
687 /* If they weren't versioned, read both. */
689 read_abs_bbt (mtd
, buf
, rd2
, chipsel
);
691 /* Write the bad block table to the device ? */
692 if ((writeops
& 0x01) && (td
->options
& NAND_BBT_WRITE
)) {
693 res
= write_bbt (mtd
, buf
, td
, md
, chipsel
);
698 /* Write the mirror bad block table to the device ? */
699 if ((writeops
& 0x02) && md
&& (md
->options
& NAND_BBT_WRITE
)) {
700 res
= write_bbt (mtd
, buf
, md
, td
, chipsel
);
709 * mark_bbt_regions - [GENERIC] mark the bad block table regions
710 * @mtd: MTD device structure
711 * @td: bad block table descriptor
713 * The bad block table regions are marked as "bad" to prevent
714 * accidental erasures / writes. The regions are identified by
717 static void mark_bbt_region (struct mtd_info
*mtd
, struct nand_bbt_descr
*td
)
719 struct nand_chip
*this = mtd
->priv
;
720 int i
, j
, chips
, block
, nrblocks
, update
;
721 uint8_t oldval
, newval
;
723 /* Do we have a bbt per chip ? */
724 if (td
->options
& NAND_BBT_PERCHIP
) {
725 chips
= this->numchips
;
726 nrblocks
= (int)(this->chipsize
>> this->bbt_erase_shift
);
729 nrblocks
= (int)(mtd
->size
>> this->bbt_erase_shift
);
732 for (i
= 0; i
< chips
; i
++) {
733 if ((td
->options
& NAND_BBT_ABSPAGE
) ||
734 !(td
->options
& NAND_BBT_WRITE
)) {
735 if (td
->pages
[i
] == -1) continue;
736 block
= td
->pages
[i
] >> (this->bbt_erase_shift
- this->page_shift
);
738 oldval
= this->bbt
[(block
>> 3)];
739 newval
= oldval
| (0x2 << (block
& 0x06));
740 this->bbt
[(block
>> 3)] = newval
;
741 if ((oldval
!= newval
) && td
->reserved_block_code
)
742 nand_update_bbt(mtd
, block
<< (this->bbt_erase_shift
- 1));
746 if (td
->options
& NAND_BBT_LASTBLOCK
)
747 block
= ((i
+ 1) * nrblocks
) - td
->maxblocks
;
749 block
= i
* nrblocks
;
751 for (j
= 0; j
< td
->maxblocks
; j
++) {
752 oldval
= this->bbt
[(block
>> 3)];
753 newval
= oldval
| (0x2 << (block
& 0x06));
754 this->bbt
[(block
>> 3)] = newval
;
755 if (oldval
!= newval
) update
= 1;
758 /* If we want reserved blocks to be recorded to flash, and some
759 new ones have been marked, then we need to update the stored
760 bbts. This should only happen once. */
761 if (update
&& td
->reserved_block_code
)
762 nand_update_bbt(mtd
, (block
- 2) << (this->bbt_erase_shift
- 1));
767 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
768 * @mtd: MTD device structure
769 * @bd: descriptor for the good/bad block search pattern
771 * The function checks, if a bad block table(s) is/are already
772 * available. If not it scans the device for manufacturer
773 * marked good / bad blocks and writes the bad block table(s) to
774 * the selected place.
776 * The bad block table memory is allocated here. It must be freed
777 * by calling the nand_free_bbt function.
780 int nand_scan_bbt (struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
782 struct nand_chip
*this = mtd
->priv
;
785 struct nand_bbt_descr
*td
= this->bbt_td
;
786 struct nand_bbt_descr
*md
= this->bbt_md
;
788 len
= mtd
->size
>> (this->bbt_erase_shift
+ 2);
789 /* Allocate memory (2bit per block) */
791 this->bbt
= kmalloc (len
, GFP_KERNEL
);
793 printk (KERN_ERR
"nand_scan_bbt: Out of memory\n");
796 /* Clear the memory bad block table */
797 memset (this->bbt
, 0x00, len
);
799 /* If no primary table decriptor is given, scan the device
800 * to build a memory based bad block table
803 return nand_memory_bbt(mtd
, bd
);
805 /* Allocate a temporary buffer for one eraseblock incl. oob */
806 len
= (1 << this->bbt_erase_shift
);
807 len
+= (len
>> this->page_shift
) * mtd
->oobsize
;
808 buf
= kmalloc (len
, GFP_KERNEL
);
810 printk (KERN_ERR
"nand_bbt: Out of memory\n");
816 /* Is the bbt at a given page ? */
817 if (td
->options
& NAND_BBT_ABSPAGE
) {
818 res
= read_abs_bbts (mtd
, buf
, td
, md
);
820 /* Search the bad block table using a pattern in oob */
821 res
= search_read_bbts (mtd
, buf
, td
, md
);
825 res
= check_create (mtd
, buf
, bd
);
827 /* Prevent the bbt regions from erasing / writing */
828 mark_bbt_region (mtd
, td
);
830 mark_bbt_region (mtd
, md
);
838 * nand_update_bbt - [NAND Interface] update bad block table(s)
839 * @mtd: MTD device structure
840 * @offs: the offset of the newly marked block
842 * The function updates the bad block table(s)
844 int nand_update_bbt (struct mtd_info
*mtd
, loff_t offs
)
846 struct nand_chip
*this = mtd
->priv
;
847 int len
, res
= 0, writeops
= 0;
850 struct nand_bbt_descr
*td
= this->bbt_td
;
851 struct nand_bbt_descr
*md
= this->bbt_md
;
853 if (!this->bbt
|| !td
)
856 len
= mtd
->size
>> (this->bbt_erase_shift
+ 2);
857 /* Allocate a temporary buffer for one eraseblock incl. oob */
858 len
= (1 << this->bbt_erase_shift
);
859 len
+= (len
>> this->page_shift
) * mtd
->oobsize
;
860 buf
= kmalloc (len
, GFP_KERNEL
);
862 printk (KERN_ERR
"nand_update_bbt: Out of memory\n");
866 writeops
= md
!= NULL
? 0x03 : 0x01;
868 /* Do we have a bbt per chip ? */
869 if (td
->options
& NAND_BBT_PERCHIP
) {
870 chip
= (int) (offs
>> this->chip_shift
);
881 /* Write the bad block table to the device ? */
882 if ((writeops
& 0x01) && (td
->options
& NAND_BBT_WRITE
)) {
883 res
= write_bbt (mtd
, buf
, td
, md
, chipsel
);
887 /* Write the mirror bad block table to the device ? */
888 if ((writeops
& 0x02) && md
&& (md
->options
& NAND_BBT_WRITE
)) {
889 res
= write_bbt (mtd
, buf
, md
, td
, chipsel
);
897 /* Define some generic bad / good block scan pattern which are used
898 * while scanning a device for factory marked good / bad blocks
900 * The memory based patterns just
902 static uint8_t scan_ff_pattern
[] = { 0xff, 0xff };
904 static struct nand_bbt_descr smallpage_memorybased
= {
908 .pattern
= scan_ff_pattern
911 static struct nand_bbt_descr largepage_memorybased
= {
915 .pattern
= scan_ff_pattern
918 static struct nand_bbt_descr smallpage_flashbased
= {
919 .options
= NAND_BBT_SCANEMPTY
| NAND_BBT_SCANALLPAGES
,
922 .pattern
= scan_ff_pattern
925 static struct nand_bbt_descr largepage_flashbased
= {
926 .options
= NAND_BBT_SCANEMPTY
| NAND_BBT_SCANALLPAGES
,
929 .pattern
= scan_ff_pattern
932 static uint8_t scan_agand_pattern
[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
934 static struct nand_bbt_descr agand_flashbased
= {
935 .options
= NAND_BBT_SCANEMPTY
| NAND_BBT_SCANALLPAGES
,
938 .pattern
= scan_agand_pattern
941 /* Generic flash bbt decriptors
943 static uint8_t bbt_pattern
[] = {'B', 'b', 't', '0' };
944 static uint8_t mirror_pattern
[] = {'1', 't', 'b', 'B' };
946 static struct nand_bbt_descr bbt_main_descr
= {
947 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
948 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
953 .pattern
= bbt_pattern
956 static struct nand_bbt_descr bbt_mirror_descr
= {
957 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
958 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
963 .pattern
= mirror_pattern
967 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
968 * @mtd: MTD device structure
970 * This function selects the default bad block table
971 * support for the device and calls the nand_scan_bbt function
974 int nand_default_bbt (struct mtd_info
*mtd
)
976 struct nand_chip
*this = mtd
->priv
;
978 /* Default for AG-AND. We must use a flash based
979 * bad block table as the devices have factory marked
980 * _good_ blocks. Erasing those blocks leads to loss
981 * of the good / bad information, so we _must_ store
982 * this information in a good / bad table during
985 if (this->options
& NAND_IS_AND
) {
986 /* Use the default pattern descriptors */
988 this->bbt_td
= &bbt_main_descr
;
989 this->bbt_md
= &bbt_mirror_descr
;
991 this->options
|= NAND_USE_FLASH_BBT
;
992 return nand_scan_bbt (mtd
, &agand_flashbased
);
996 /* Is a flash based bad block table requested ? */
997 if (this->options
& NAND_USE_FLASH_BBT
) {
998 /* Use the default pattern descriptors */
1000 this->bbt_td
= &bbt_main_descr
;
1001 this->bbt_md
= &bbt_mirror_descr
;
1003 if (!this->badblock_pattern
) {
1004 this->badblock_pattern
= (mtd
->oobblock
> 512) ?
1005 &largepage_flashbased
: &smallpage_flashbased
;
1008 this->bbt_td
= NULL
;
1009 this->bbt_md
= NULL
;
1010 if (!this->badblock_pattern
) {
1011 this->badblock_pattern
= (mtd
->oobblock
> 512) ?
1012 &largepage_memorybased
: &smallpage_memorybased
;
1015 return nand_scan_bbt (mtd
, this->badblock_pattern
);
1019 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1020 * @mtd: MTD device structure
1021 * @offs: offset in the device
1022 * @allowbbt: allow access to bad block table region
1025 int nand_isbad_bbt (struct mtd_info
*mtd
, loff_t offs
, int allowbbt
)
1027 struct nand_chip
*this = mtd
->priv
;
1031 /* Get block number * 2 */
1032 block
= (int) (offs
>> (this->bbt_erase_shift
- 1));
1033 res
= (this->bbt
[block
>> 3] >> (block
& 0x06)) & 0x03;
1035 DEBUG (MTD_DEBUG_LEVEL2
, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1036 (unsigned int)offs
, res
, block
>> 1);
1039 case 0x00: return 0;
1040 case 0x01: return 1;
1041 case 0x03: return 1;
1042 case 0x02: return allowbbt
? 0 : 1;
1047 #if defined(CONFIG_NAND_DYNPART)
1049 extern unsigned int dynpart_size
[];
1050 extern char *dynpart_names
[];
1052 #define MTDPARTS_MAX_SIZE 512
1055 static int skip_offs(const struct nand_chip
*this, unsigned int offs
)
1057 int block
= (int) (offs
>> (this->bbt_erase_shift
- 1));
1058 u_int8_t bbt
= (this->bbt
[block
>> 3] >> (block
& 0x06)) & 0x03;
1063 int nand_create_mtd_dynpart(struct mtd_info
*mtd
)
1065 struct nand_chip
*this = mtd
->priv
;
1068 unsigned int cur_offs
= 0;
1070 mtdparts
= malloc(MTDPARTS_MAX_SIZE
); /* FIXME: bounds checking */
1074 sprintf(mtdparts
, "mtdparts="
1075 #ifdef CFG_MTDPARTS_PREFIX
1078 CFG_NAND_DYNPART_MTD_KERNEL_NAME
":");
1080 for (part
= 0; dynpart_size
[part
] != 0; part
++) {
1081 unsigned int bb_delta
= 0;
1082 unsigned int offs
= 0;
1085 for (offs
= cur_offs
;
1086 offs
< cur_offs
+ dynpart_size
[part
] + bb_delta
;
1087 offs
+= mtd
->erasesize
) {
1088 if (skip_offs(this, offs
))
1089 bb_delta
+= mtd
->erasesize
;
1093 * Absorb bad blocks immediately following this partition also
1094 * into the partition, in order to make next partition start
1095 * with a good block. This simplifies handling of the
1096 * environment partition.
1098 while (offs
< this->chipsize
&& skip_offs(this, offs
)) {
1099 bb_delta
+= mtd
->erasesize
;
1100 offs
+= mtd
->erasesize
;
1103 if (cur_offs
+ dynpart_size
[part
] + bb_delta
> this->chipsize
)
1104 dynpart_size
[part
] = this->chipsize
- cur_offs
- bb_delta
;
1106 printf("partition %u: start = 0x%08x, end=%08x size=%08x, size_inc_bb=%08x\n",
1107 part
, cur_offs
, cur_offs
+ dynpart_size
[part
] + bb_delta
,
1108 dynpart_size
[part
], dynpart_size
[part
] + bb_delta
);
1110 cur_offs
+= dynpart_size
[part
] + bb_delta
;
1111 sprintf(mtdpart
, "0x%.8x(%.16s),", dynpart_size
[part
] + bb_delta
,
1112 dynpart_names
[part
]);
1113 mtdpart
[sizeof(mtdpart
)-1] = '\0';
1114 strncat(mtdparts
, mtdpart
,
1115 MTDPARTS_MAX_SIZE
-strlen(mtdparts
)-1);
1118 mtdparts
[strlen(mtdparts
)-1] = '\0';
1119 printf("mtdparts %s\n", mtdparts
);
1120 setenv("mtdparts", mtdparts
);
1125 #endif /* CONFIG_NAND_DYNPART */