2 * Broadcom NAND flash BBT interface
4 * Copyright (C) 2012, Broadcom Corporation. All Rights Reserved.
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * When brcmnand_scan_bbt is called, then it tries to find the bad block table
22 * depending on the options in the bbt descriptor(s). If a bbt is found
23 * then the contents are read and the memory based bbt is created. If a
24 * mirrored bbt is selected then the mirror is searched too and the
25 * versions are compared. If the mirror has a greater version number
26 * than the mirror bbt is used to build the memory based bbt.
27 * If the tables are not versioned, then we "or" the bad block information.
28 * If one of the bbt's is out of date or does not exist it is (re)created.
29 * If no bbt exists at all then the device is scanned for factory marked
30 * good / bad blocks and the bad block tables are created.
32 * For manufacturer created bbts like the one found on M-SYS DOC devices
33 * the bbt is searched and read but never created
35 * The autogenerated bad block table is located in the last good blocks
36 * of the device. The table is mirrored, so it can be updated eventually.
37 * The table is marked in the oob area with an ident pattern and a version
38 * number which indicates which of both tables is more up to date.
40 * The table uses 2 bits per block
42 * 00b: block is factory marked bad
43 * 01b, 10b: block is marked bad due to wear
45 * The memory bad block table uses the following scheme:
47 * 01b: block is marked bad due to wear
48 * 10b: block is reserved (to protect the bbt area)
49 * 11b: block is factory marked bad
51 * Multichip devices like DOC store the bad block info per floor.
53 * Following assumptions are made:
54 * - bbts start at a page boundary, if autolocated on a block boundary
55 * - the space necessary for a bbt in FLASH does not exceed a block boundary
60 #include <linux/slab.h>
61 #include <linux/types.h>
62 #include <linux/mtd/mtd.h>
63 #include <linux/mtd/nand.h>
64 #include <linux/mtd/nand_ecc.h>
65 #include <linux/bitops.h>
66 #include <linux/delay.h>
67 #include <linux/vmalloc.h>
69 #define NFLASH_SUPPORT
83 #include "brcmnand_priv.h"
90 char brcmNandBBTMsg
[1024];
93 * rescan: 1. Rescan for bad blocks, and update existing BBT
94 * showbbt: 2. Print out the contents of the BBT on boot up.
96 * The following commands are implemented but should be removed for
98 * Use userspace flash_eraseall instead.
99 * These were intended for development debugging only.
100 * erase: 7. Erase entire flash, except CFE, and rescan for bad blocks
101 * eraseall: 8. Erase entire flash, and rescan for bad blocks
102 * clearbbt: 9. Erase BBT and rescan for bad blocks.
103 * (DANGEROUS, may lose Mfg's BIs).
105 #define NANDCMD_RESCAN 1
106 #define NANDCMD_SHOWBBT 2
108 #define NANDCMD_ERASE 7
109 #define NANDCMD_ERASEALL 8
110 #define NANDCMD_CLEARBBT 9
112 int brcmnand_update_bbt(struct mtd_info
*mtd
, loff_t offs
);
114 extern struct brcmnand_mtd brcmnand_info
;
116 static inline uint32_t device_size(struct mtd_info
*mtd
)
122 * check_pattern - [GENERIC] check if a pattern is in the buffer
123 * @buf: the buffer to search
124 * @len: the length of buffer to search
125 * @paglen: the pagelength
126 * @td: search pattern descriptor
128 * Check for a pattern at the given place. Used to search bad block
129 * tables and good / bad block identifiers.
130 * If the SCAN_EMPTY option is set then check, if all bytes except the
131 * pattern area contain 0xff
134 static int check_pattern(uint8_t *buf
, int len
, int paglen
, struct nand_bbt_descr
*td
)
139 end
= paglen
+ td
->offs
;
140 if (td
->options
& NAND_BBT_SCANEMPTY
) {
141 for (i
= 0; i
< end
; i
++) {
148 /* Compare the pattern */
149 for (i
= 0; i
< td
->len
; i
++) {
150 if (p
[i
] != td
->pattern
[i
])
154 if (td
->options
& NAND_BBT_SCANEMPTY
) {
157 for (i
= end
; i
< len
; i
++) {
166 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
167 * @buf: the buffer to search
168 * @td: search pattern descriptor
170 * Check for a pattern at the given place. Used to search bad block
171 * tables and good / bad block identifiers. Same as check_pattern, but
172 * no optional empty check
175 static int check_short_pattern(uint8_t *buf
, struct nand_bbt_descr
*td
)
180 /* Compare the pattern */
181 for (i
= 0; i
< td
->len
; i
++) {
182 if (p
[td
->offs
+ i
] != td
->pattern
[i
])
189 * brcmnand_read_bbt - [GENERIC] Read the bad block table starting from page
190 * @mtd: MTD device structure
191 * @buf: temporary buffer
192 * @page: the starting page
193 * @num: the number of bbt descriptors to read
194 * @bits: number of bits per block
195 * @offs: offset in the memory table
196 * @reserved_block_code: Pattern to identify reserved blocks
198 * Read the bad block table starting from page.
201 static int brcmnand_read_bbt(struct mtd_info
*mtd
, uint8_t *buf
, int page
, int num
,
202 int bits
, int offs
, int reserved_block_code
)
204 int res
, i
, j
, act
= 0;
205 struct nand_chip
*this = mtd
->priv
;
206 size_t retlen
, len
, totlen
;
208 uint8_t msk
= (uint8_t) ((1 << bits
) - 1);
209 si_t
*sih
= brcmnand_info
.sih
;
210 chipcregs_t
*cc
= brcmnand_info
.cc
;
213 totlen
= (num
* bits
) >> 3;
214 from
= ((loff_t
) page
) << this->page_shift
;
217 /* Clear ECC registers */
218 W_REG(osh
, &cc
->nand_ecc_corr_addr
, 0);
219 W_REG(osh
, &cc
->nand_ecc_corr_addr_x
, 0);
220 W_REG(osh
, &cc
->nand_ecc_unc_addr
, 0);
221 W_REG(osh
, &cc
->nand_ecc_unc_addr_x
, 0);
224 len
= min(totlen
, (size_t) (1 << this->bbt_erase_shift
));
225 res
= mtd
->read(mtd
, from
, len
, &retlen
, buf
);
228 printk(KERN_INFO
"nand_bbt: Error reading bad block table\n");
231 printk(KERN_WARNING
"nand_bbt: ECC error while reading bad block table\n");
235 for (i
= 0; i
< len
; i
++) {
236 uint8_t dat
= buf
[i
];
237 for (j
= 0; j
< 8; j
+= bits
, act
+= 2) {
238 uint8_t tmp
= (dat
>> j
) & msk
;
241 if (reserved_block_code
&& (tmp
== reserved_block_code
)) {
243 "nand_read_bbt: Reserved block at 0x%08x\n",
244 ((offs
<< 2) + (act
>> 1))
245 << this->bbt_erase_shift
);
246 this->bbt
[offs
+ (act
>> 3)] |= 0x2 << (act
& 0x06);
247 mtd
->ecc_stats
.bbtblocks
++;
250 /* Leave it for now, if its matured we can move this
251 * message to MTD_DEBUG_LEVEL0
253 printk(KERN_DEBUG
"nand_read_bbt: Bad block at 0x%08x\n",
254 ((offs
<< 2) + (act
>> 1)) << this->bbt_erase_shift
);
255 printk("nand_read_bbt: Bad block at 0x%08x\n",
256 ((offs
<< 2) + (act
>> 1)) << this->bbt_erase_shift
);
257 /* Factory marked bad or worn out ? */
259 this->bbt
[offs
+ (act
>> 3)] |= 0x3 << (act
& 0x06);
261 this->bbt
[offs
+ (act
>> 3)] |= 0x1 << (act
& 0x06);
262 mtd
->ecc_stats
.badblocks
++;
272 * brcmnand_read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
273 * @mtd: MTD device structure
274 * @buf: temporary buffer
275 * @td: descriptor for the bad block table
276 * @chip: read the table for a specific chip, -1 read all chips.
277 * Applies only if NAND_BBT_PERCHIP option is set
279 * Read the bad block table for all chips starting at a given page
280 * We assume that the bbt bits are in consecutive order.
282 static int brcmnand_read_abs_bbt(struct mtd_info
*mtd
, uint8_t *buf
,
283 struct nand_bbt_descr
*td
, int chip
)
285 struct nand_chip
*this = mtd
->priv
;
289 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
290 if (td
->options
& NAND_BBT_PERCHIP
) {
292 for (i
= 0; i
< this->numchips
; i
++) {
293 if (chip
== -1 || chip
== i
)
294 res
= brcmnand_read_bbt(mtd
, buf
, td
->pages
[i
],
295 this->chipsize
>> this->bbt_erase_shift
,
296 bits
, offs
, td
->reserved_block_code
);
299 offs
+= this->chipsize
>> (this->bbt_erase_shift
+ 2);
302 res
= brcmnand_read_bbt(mtd
, buf
, td
->pages
[0],
303 mtd
->size
>> this->bbt_erase_shift
, bits
, 0, td
->reserved_block_code
);
311 * Scan read raw data from flash
313 static int brcmnand_scan_read_raw(struct mtd_info
*mtd
, uint8_t *buf
, loff_t offs
,
316 struct mtd_oob_ops ops
;
318 ops
.mode
= MTD_OOB_RAW
;
320 ops
.ooblen
= mtd
->oobsize
;
325 return mtd
->read_oob(mtd
, offs
, &ops
);
329 * Scan write data with oob to flash
331 static int brcmnand_scan_write_bbt(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
332 uint8_t *buf
, uint8_t *oob
)
334 struct mtd_oob_ops ops
;
336 ops
.mode
= MTD_OOB_PLACE
;
338 ops
.ooblen
= mtd
->oobsize
;
343 return mtd
->write_oob(mtd
, offs
, &ops
);
347 * brcmnand_read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips
348 * starting at a given page
349 * @mtd: MTD device structure
350 * @buf: temporary buffer
351 * @td: descriptor for the bad block table
352 * @md: descriptor for the bad block table mirror
354 * Read the bad block table(s) for all chips starting at a given page
355 * We assume that the bbt bits are in consecutive order.
358 static int brcmnand_read_abs_bbts(struct mtd_info
*mtd
, uint8_t *buf
,
359 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
)
361 struct nand_chip
*this = mtd
->priv
;
363 /* Read the primary version, if available */
364 if (td
->options
& NAND_BBT_VERSION
) {
365 brcmnand_scan_read_raw(mtd
, buf
, td
->pages
[0] << this->page_shift
,
367 td
->version
[0] = buf
[mtd
->writesize
+ td
->veroffs
];
368 printk(KERN_DEBUG
"Bad block table at page %d, version 0x%02X\n",
369 td
->pages
[0], td
->version
[0]);
372 /* Read the mirror version, if available */
373 if (md
&& (md
->options
& NAND_BBT_VERSION
)) {
374 brcmnand_scan_read_raw(mtd
, buf
, md
->pages
[0] << this->page_shift
,
376 md
->version
[0] = buf
[mtd
->writesize
+ md
->veroffs
];
377 printk(KERN_DEBUG
"Bad block table at page %d, version 0x%02X\n",
378 md
->pages
[0], md
->version
[0]);
384 * Scan a given block full
386 static int brcmnand_scan_block_full(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
,
387 loff_t offs
, uint8_t *buf
, size_t readlen
, int scanlen
, int len
)
391 ret
= brcmnand_scan_read_raw(mtd
, buf
, offs
, readlen
);
395 for (j
= 0; j
< len
; j
++, buf
+= scanlen
) {
396 if (check_pattern(buf
, scanlen
, mtd
->writesize
, bd
))
403 * Scan a given block partially
405 static int brcmnand_scan_block_fast(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
,
406 loff_t offs
, uint8_t *buf
, int len
)
408 struct mtd_oob_ops ops
;
412 /* SLC: First and 2nd page */
414 ops
.ooblen
= mtd
->oobsize
;
418 ops
.mode
= MTD_OOB_PLACE
;
420 for (j
= 0; j
< len
; j
++) {
422 * Read the full oob until read_oob is fixed to
423 * handle single byte reads for 16 bit
426 ret
= mtd
->read_oob(mtd
, offs
, &ops
);
430 if (check_short_pattern(buf
, bd
))
433 offs
+= (dir
* mtd
->writesize
);
439 * brcmnand_create_bbt - [GENERIC] Create a bad block table by scanning the device
440 * @mtd: MTD device structure
441 * @buf: temporary buffer
442 * @bd: descriptor for the good/bad block search pattern
443 * @chip: create the table for a specific chip, -1 read all chips.
444 * Applies only if NAND_BBT_PERCHIP option is set
446 * Create a bad block table by scanning the device
447 * for the given good/bad block identify pattern
449 static int brcmnand_create_bbt(struct mtd_info
*mtd
, uint8_t *buf
,
450 struct nand_bbt_descr
*bd
, int chip
)
452 struct nand_chip
*this = mtd
->priv
;
453 int i
, numblocks
, len
, scanlen
;
458 printk(KERN_INFO
"Scanning device for bad blocks\n");
460 if (bd
->options
& NAND_BBT_SCANALLPAGES
)
461 len
= 1 << (this->bbt_erase_shift
- this->page_shift
);
463 if (bd
->options
& NAND_BBT_SCAN2NDPAGE
)
469 if (!(bd
->options
& NAND_BBT_SCANEMPTY
)) {
470 /* We need only read few bytes from the OOB area */
474 /* Full page content should be read */
475 scanlen
= mtd
->writesize
+ mtd
->oobsize
;
476 readlen
= len
* mtd
->writesize
;
480 /* Note that numblocks is 2 * (real numblocks) here, see i+=2
481 * below as it makes shifting and masking less painful
483 numblocks
= mtd
->size
>> (this->bbt_erase_shift
- 1);
487 if (chip
>= this->numchips
) {
488 printk(KERN_WARNING
"create_bbt(): chipnr (%d) > available chips (%d)\n",
489 chip
+ 1, this->numchips
);
492 numblocks
= this->chipsize
>> (this->bbt_erase_shift
- 1);
493 startblock
= chip
* numblocks
;
494 numblocks
+= startblock
;
495 from
= startblock
<< (this->bbt_erase_shift
- 1);
498 for (i
= startblock
; i
< numblocks
;) {
501 if (bd
->options
& NAND_BBT_SCANALLPAGES
)
502 ret
= brcmnand_scan_block_full(mtd
, bd
, from
, buf
, readlen
,
505 ret
= brcmnand_scan_block_fast(mtd
, bd
, from
, buf
, len
);
511 this->bbt
[i
>> 3] |= 0x03 << (i
& 0x6);
512 printk(KERN_WARNING
"Bad eraseblock %d at 0x%08x\n",
513 i
>> 1, (unsigned int)from
);
514 mtd
->ecc_stats
.badblocks
++;
518 from
+= (1 << this->bbt_erase_shift
);
524 * brcmnand_search_bbt - [GENERIC] scan the device for a specific bad block table
525 * @mtd: MTD device structure
526 * @buf: temporary buffer
527 * @td: descriptor for the bad block table
529 * Read the bad block table by searching for a given ident pattern.
530 * Search is preformed either from the beginning up or from the end of
531 * the device downwards. The search starts always at the start of a
533 * If the option NAND_BBT_PERCHIP is given, each chip is searched
534 * for a bbt, which contains the bad block information of this chip.
535 * This is necessary to provide support for certain DOC devices.
537 * The bbt ident pattern resides in the oob area of the first page
540 static int brcmnand_search_bbt(struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*td
)
542 struct nand_chip
*this = mtd
->priv
;
544 int bits
, startblock
, block
, dir
;
545 int scanlen
= mtd
->writesize
+ mtd
->oobsize
;
547 int blocktopage
= this->bbt_erase_shift
- this->page_shift
;
549 /* Search direction top -> down ? */
550 if (td
->options
& NAND_BBT_LASTBLOCK
) {
551 startblock
= (mtd
->size
>> this->bbt_erase_shift
) - 1;
558 /* Do we have a bbt per chip ? */
559 if (td
->options
& NAND_BBT_PERCHIP
) {
560 chips
= this->numchips
;
561 bbtblocks
= this->chipsize
>> this->bbt_erase_shift
;
562 startblock
&= bbtblocks
- 1;
565 bbtblocks
= mtd
->size
>> this->bbt_erase_shift
;
568 /* Number of bits for each erase block in the bbt */
569 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
571 for (i
= 0; i
< chips
; i
++) {
572 /* Reset version information */
575 /* Scan the maximum number of blocks */
576 for (block
= 0; block
< td
->maxblocks
; block
++) {
578 int actblock
= startblock
+ dir
* block
;
579 loff_t offs
= actblock
<< this->bbt_erase_shift
;
581 /* Read first page */
582 brcmnand_scan_read_raw(mtd
, buf
, offs
, mtd
->writesize
);
583 if (!check_pattern(buf
, scanlen
, mtd
->writesize
, td
)) {
584 td
->pages
[i
] = actblock
<< blocktopage
;
585 if (td
->options
& NAND_BBT_VERSION
) {
586 td
->version
[i
] = buf
[mtd
->writesize
+ td
->veroffs
];
591 startblock
+= this->chipsize
>> this->bbt_erase_shift
;
593 /* Check, if we found a bbt for each requested chip */
594 for (i
= 0; i
< chips
; i
++) {
595 if (td
->pages
[i
] == -1)
596 printk(KERN_WARNING
"Bad block table not found for chip %d\n", i
);
598 printk(KERN_DEBUG
"Bad block table found at page %d, version 0x%02X\n",
599 td
->pages
[i
], td
->version
[i
]);
605 * brcmnand_search_read_bbts - [GENERIC] scan the device for bad block table(s)
606 * @mtd: MTD device structure
607 * @buf: temporary buffer
608 * @td: descriptor for the bad block table
609 * @md: descriptor for the bad block table mirror
611 * Search and read the bad block table(s)
613 static int brcmnand_search_read_bbts(struct mtd_info
*mtd
, uint8_t *buf
,
614 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
)
616 /* Search the primary table */
617 brcmnand_search_bbt(mtd
, buf
, td
);
619 /* Search the mirror table */
621 brcmnand_search_bbt(mtd
, buf
, md
);
623 /* Force result check */
628 * brcmnand_write_bbt - [GENERIC] (Re)write the bad block table
630 * @mtd: MTD device structure
631 * @buf: temporary buffer
632 * @td: descriptor for the bad block table
633 * @md: descriptor for the bad block table mirror
634 * @chipsel: selector for a specific chip, -1 for all
636 * (Re)write the bad block table
639 static int brcmnand_write_bbt(struct mtd_info
*mtd
, uint8_t *buf
,
640 struct nand_bbt_descr
*td
, struct nand_bbt_descr
*md
, int chipsel
)
642 struct nand_chip
*this = mtd
->priv
;
643 struct erase_info einfo
;
644 int i
, j
, res
, chip
= 0;
645 int bits
, startblock
, dir
, page
, offs
, numblocks
, sft
, sftmsk
;
646 int nrchips
, bbtoffs
, pageoffs
, ooboffs
;
648 uint8_t rcode
= td
->reserved_block_code
;
649 size_t retlen
, len
= 0;
651 struct mtd_oob_ops ops
;
653 ops
.ooblen
= mtd
->oobsize
;
656 ops
.mode
= MTD_OOB_PLACE
;
660 /* Write bad block table per chip rather than per device ? */
661 if (td
->options
& NAND_BBT_PERCHIP
) {
662 numblocks
= (int)(this->chipsize
>> this->bbt_erase_shift
);
663 /* Full device write or specific chip ? */
665 nrchips
= this->numchips
;
667 nrchips
= chipsel
+ 1;
671 numblocks
= (int)(mtd
->size
>> this->bbt_erase_shift
);
675 /* Loop through the chips */
676 for (; chip
< nrchips
; chip
++) {
678 /* There was already a version of the table, reuse the page
679 * This applies for absolute placement too, as we have the
680 * page nr. in td->pages.
682 if (td
->pages
[chip
] != -1) {
683 page
= td
->pages
[chip
];
687 /* Automatic placement of the bad block table */
688 /* Search direction top -> down ? */
689 if (td
->options
& NAND_BBT_LASTBLOCK
) {
690 startblock
= numblocks
* (chip
+ 1) - 1;
693 startblock
= chip
* numblocks
;
697 for (i
= 0; i
< td
->maxblocks
; i
++) {
698 int block
= startblock
+ dir
* i
;
699 /* Check, if the block is bad */
700 switch ((this->bbt
[block
>> 2] >>
701 (2 * (block
& 0x03))) & 0x03) {
707 (this->bbt_erase_shift
- this->page_shift
);
708 /* Check, if the block is used by the mirror table */
709 if (!md
|| md
->pages
[chip
] != page
)
712 printk(KERN_ERR
"No space left to write bad block table\n");
716 /* Set up shift count and masks for the flash table */
717 bits
= td
->options
& NAND_BBT_NRBITS_MSK
;
720 case 1: sft
= 3; sftmsk
= 0x07; msk
[0] = 0x00; msk
[1] = 0x01;
723 case 2: sft
= 2; sftmsk
= 0x06; msk
[0] = 0x00; msk
[1] = 0x01;
726 case 4: sft
= 1; sftmsk
= 0x04; msk
[0] = 0x00; msk
[1] = 0x0C;
729 case 8: sft
= 0; sftmsk
= 0x00; msk
[0] = 0x00; msk
[1] = 0x0F;
732 default: return -EINVAL
;
735 bbtoffs
= chip
* (numblocks
>> 2);
737 to
= ((loff_t
) page
) << this->page_shift
;
739 /* Must we save the block contents ? */
740 if (td
->options
& NAND_BBT_SAVECONTENT
) {
741 /* Make it block aligned */
742 to
&= ~((loff_t
) ((1 << this->bbt_erase_shift
) - 1));
743 len
= 1 << this->bbt_erase_shift
;
744 res
= mtd
->read(mtd
, to
, len
, &retlen
, buf
);
747 printk(KERN_INFO
"nand_bbt: Error "
748 "reading block for writing "
749 "the bad block table\n");
752 printk(KERN_WARNING
"nand_bbt: ECC error "
753 "while reading block for writing "
754 "bad block table\n");
757 ops
.ooblen
= (len
>> this->page_shift
) * mtd
->oobsize
;
758 ops
.oobbuf
= &buf
[len
];
759 res
= mtd
->read_oob(mtd
, to
+ mtd
->writesize
, &ops
);
760 if (res
< 0 || ops
.oobretlen
!= ops
.ooblen
)
763 /* Calc the byte offset in the buffer */
764 pageoffs
= page
- (int)(to
>> this->page_shift
);
765 offs
= pageoffs
<< this->page_shift
;
766 /* Preset the bbt area with 0xff */
767 memset(&buf
[offs
], 0xff, (size_t) (numblocks
>> sft
));
768 ooboffs
= len
+ (pageoffs
* mtd
->oobsize
);
772 len
= (size_t) (numblocks
>> sft
);
773 /* Make it page aligned ! */
774 len
= (len
+ (mtd
->writesize
- 1)) &
775 ~(mtd
->writesize
- 1);
776 /* Preset the buffer with 0xff */
777 memset(buf
, 0xff, len
+
778 (len
>> this->page_shift
)* mtd
->oobsize
);
781 /* Pattern is located in oob area of first page */
782 memcpy(&buf
[ooboffs
+ td
->offs
], td
->pattern
, td
->len
);
785 if (td
->options
& NAND_BBT_VERSION
)
786 buf
[ooboffs
+ td
->veroffs
] = td
->version
[chip
];
788 /* walk through the memory table */
789 for (i
= 0; i
< numblocks
;) {
791 dat
= this->bbt
[bbtoffs
+ (i
>> 2)];
792 for (j
= 0; j
< 4; j
++, i
++) {
793 int sftcnt
= (i
<< (3 - sft
)) & sftmsk
;
794 /* Do not store the reserved bbt blocks ! */
795 buf
[offs
+ (i
>> sft
)] &=
796 ~(msk
[dat
& 0x03] << sftcnt
);
801 memset(&einfo
, 0, sizeof(einfo
));
803 einfo
.addr
= (unsigned long)to
;
804 einfo
.len
= 1 << this->bbt_erase_shift
;
805 res
= mtd
->erase(mtd
, &einfo
);
809 res
= brcmnand_scan_write_bbt(mtd
, to
, len
, buf
, &buf
[len
]);
813 printk(KERN_DEBUG
"Bad block table written to 0x%08x, version "
814 "0x%02X\n", (unsigned int)to
, td
->version
[chip
]);
816 /* Mark it as used */
817 td
->pages
[chip
] = page
;
823 "nand_bbt: Error while writing bad block table %d\n", res
);
828 * brcmnand_memory_bbt - [GENERIC] create a memory based bad block table
829 * @mtd: MTD device structure
830 * @bd: descriptor for the good/bad block search pattern
832 * The function creates a memory based bbt by scanning the device
833 * for manufacturer / software marked good / bad blocks
835 static inline int brcmnand_memory_bbt(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
837 struct nand_chip
*this = mtd
->priv
;
839 bd
->options
&= ~NAND_BBT_SCANEMPTY
;
840 return brcmnand_create_bbt(mtd
, this->buffers
->databuf
, bd
, -1);
844 * brcmnand_check_create - [GENERIC] create and write bbt(s) if necessary
845 * @mtd: MTD device structure
846 * @buf: temporary buffer
847 * @bd: descriptor for the good/bad block search pattern
849 * The function checks the results of the previous call to read_bbt
850 * and creates / updates the bbt(s) if necessary
851 * Creation is necessary if no bbt was found for the chip/device
852 * Update is necessary if one of the tables is missing or the
853 * version nr. of one table is less than the other
855 static int brcmnand_check_create(struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*bd
)
857 int i
, chips
, writeops
, chipsel
, res
;
858 struct nand_chip
*this = mtd
->priv
;
859 struct nand_bbt_descr
*td
= this->bbt_td
;
860 struct nand_bbt_descr
*md
= this->bbt_md
;
861 struct nand_bbt_descr
*rd
, *rd2
;
863 /* Do we have a bbt per chip ? */
864 if (td
->options
& NAND_BBT_PERCHIP
)
865 chips
= this->numchips
;
869 for (i
= 0; i
< chips
; i
++) {
873 /* Per chip or per device ? */
874 chipsel
= (td
->options
& NAND_BBT_PERCHIP
) ? i
: -1;
875 /* Mirrored table avilable ? */
877 if (td
->pages
[i
] == -1 && md
->pages
[i
] == -1) {
882 if (td
->pages
[i
] == -1) {
884 td
->version
[i
] = md
->version
[i
];
889 if (md
->pages
[i
] == -1) {
891 md
->version
[i
] = td
->version
[i
];
896 if (td
->version
[i
] == md
->version
[i
]) {
898 if (!(td
->options
& NAND_BBT_VERSION
))
903 if (((int8_t) (td
->version
[i
] - md
->version
[i
])) > 0) {
905 md
->version
[i
] = td
->version
[i
];
909 td
->version
[i
] = md
->version
[i
];
916 if (td
->pages
[i
] == -1) {
924 /* Create the bad block table by scanning the device ? */
925 if (!(td
->options
& NAND_BBT_CREATE
))
928 /* Create the table in memory by scanning the chip(s) */
929 brcmnand_create_bbt(mtd
, buf
, bd
, chipsel
);
935 /* read back first ? */
937 brcmnand_read_abs_bbt(mtd
, buf
, rd
, chipsel
);
938 /* If they weren't versioned, read both. */
940 brcmnand_read_abs_bbt(mtd
, buf
, rd2
, chipsel
);
942 /* Write the bad block table to the device ? */
943 if ((writeops
& 0x01) && (td
->options
& NAND_BBT_WRITE
)) {
944 res
= brcmnand_write_bbt(mtd
, buf
, td
, md
, chipsel
);
949 /* Write the mirror bad block table to the device ? */
950 if ((writeops
& 0x02) && md
&& (md
->options
& NAND_BBT_WRITE
)) {
951 res
= brcmnand_write_bbt(mtd
, buf
, md
, td
, chipsel
);
960 * mark_bbt_regions - [GENERIC] mark the bad block table regions
961 * @mtd: MTD device structure
962 * @td: bad block table descriptor
964 * The bad block table regions are marked as "bad" to prevent
965 * accidental erasures / writes. The regions are identified by
968 static void mark_bbt_region(struct mtd_info
*mtd
, struct nand_bbt_descr
*td
)
970 struct nand_chip
*this = mtd
->priv
;
971 int i
, j
, chips
, block
, nrblocks
, update
;
972 uint8_t oldval
, newval
;
974 /* Do we have a bbt per chip ? */
975 if (td
->options
& NAND_BBT_PERCHIP
) {
976 chips
= this->numchips
;
977 nrblocks
= (int)(this->chipsize
>> this->bbt_erase_shift
);
980 nrblocks
= (int)(mtd
->size
>> this->bbt_erase_shift
);
983 for (i
= 0; i
< chips
; i
++) {
984 if ((td
->options
& NAND_BBT_ABSPAGE
) ||
985 !(td
->options
& NAND_BBT_WRITE
)) {
986 if (td
->pages
[i
] == -1)
988 block
= td
->pages
[i
] >> (this->bbt_erase_shift
- this->page_shift
);
990 oldval
= this->bbt
[(block
>> 3)];
991 newval
= oldval
| (0x2 << (block
& 0x06));
992 this->bbt
[(block
>> 3)] = newval
;
993 if ((oldval
!= newval
) && td
->reserved_block_code
)
994 brcmnand_update_bbt(mtd
, block
<< (this->bbt_erase_shift
- 1));
998 if (td
->options
& NAND_BBT_LASTBLOCK
)
999 block
= ((i
+ 1) * nrblocks
) - td
->maxblocks
;
1001 block
= i
* nrblocks
;
1003 for (j
= 0; j
< td
->maxblocks
; j
++) {
1004 oldval
= this->bbt
[(block
>> 3)];
1005 newval
= oldval
| (0x2 << (block
& 0x06));
1006 this->bbt
[(block
>> 3)] = newval
;
1007 if (oldval
!= newval
)
1011 /* If we want reserved blocks to be recorded to flash, and some
1012 * new ones have been marked, then we need to update the stored
1013 * bbts. This should only happen once.
1015 if (update
&& td
->reserved_block_code
)
1016 brcmnand_update_bbt(mtd
, (block
- 2) << (this->bbt_erase_shift
- 1));
1021 * brcmnand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1022 * @mtd: MTD device structure
1023 * @bd: descriptor for the good/bad block search pattern
1025 * The function checks, if a bad block table(s) is/are already
1026 * available. If not it scans the device for manufacturer
1027 * marked good / bad blocks and writes the bad block table(s) to
1028 * the selected place.
1030 * The bad block table memory is allocated here. It must be freed
1031 * by calling the nand_free_bbt function.
1034 int brcmnand_scan_bbt(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
1036 struct nand_chip
*this = mtd
->priv
;
1039 struct nand_bbt_descr
*td
= this->bbt_td
;
1040 struct nand_bbt_descr
*md
= this->bbt_md
;
1042 len
= mtd
->size
>> (this->bbt_erase_shift
+ 2);
1043 /* Allocate memory (2bit per block) and clear the memory bad block table */
1044 this->bbt
= kzalloc(len
, GFP_KERNEL
);
1046 printk(KERN_ERR
"nand_scan_bbt: Out of memory\n");
1050 /* If no primary table decriptor is given, scan the device
1051 * to build a memory based bad block table
1054 if ((res
= brcmnand_memory_bbt(mtd
, bd
))) {
1055 printk(KERN_ERR
"nand_bbt: Can't scan flash and build the RAM-based BBT\n");
1062 /* Allocate a temporary buffer for one eraseblock incl. oob */
1063 len
= (1 << this->bbt_erase_shift
);
1064 len
+= (len
>> this->page_shift
) * mtd
->oobsize
;
1067 printk(KERN_ERR
"nand_bbt: Out of memory\n");
1073 /* Is the bbt at a given page ? */
1074 if (td
->options
& NAND_BBT_ABSPAGE
) {
1075 res
= brcmnand_read_abs_bbts(mtd
, buf
, td
, md
);
1077 /* Search the bad block table using a pattern in oob */
1078 res
= brcmnand_search_read_bbts(mtd
, buf
, td
, md
);
1082 res
= brcmnand_check_create(mtd
, buf
, bd
);
1084 /* Prevent the bbt regions from erasing / writing */
1085 mark_bbt_region(mtd
, td
);
1087 mark_bbt_region(mtd
, md
);
1095 * brcmnand_update_bbt - [NAND Interface] update bad block table(s)
1096 * @mtd: MTD device structure
1097 * @offs: the offset of the newly marked block
1099 * The function updates the bad block table(s)
1101 int brcmnand_update_bbt(struct mtd_info
*mtd
, loff_t offs
)
1103 struct nand_chip
*this = mtd
->priv
;
1104 int len
, res
= 0, writeops
= 0;
1107 struct nand_bbt_descr
*td
= this->bbt_td
;
1108 struct nand_bbt_descr
*md
= this->bbt_md
;
1110 if (!this->bbt
|| !td
)
1113 len
= mtd
->size
>> (this->bbt_erase_shift
+ 2);
1114 /* Allocate a temporary buffer for one eraseblock incl. oob */
1115 len
= (1 << this->bbt_erase_shift
);
1116 len
+= (len
>> this->page_shift
) * mtd
->oobsize
;
1117 buf
= kmalloc(len
, GFP_KERNEL
);
1119 printk(KERN_ERR
"nand_update_bbt: Out of memory\n");
1123 writeops
= md
!= NULL
? 0x03 : 0x01;
1125 /* Do we have a bbt per chip ? */
1126 if (td
->options
& NAND_BBT_PERCHIP
) {
1127 chip
= (int)(offs
>> this->chip_shift
);
1134 td
->version
[chip
]++;
1136 md
->version
[chip
]++;
1138 /* Write the bad block table to the device ? */
1139 if ((writeops
& 0x01) && (td
->options
& NAND_BBT_WRITE
)) {
1140 res
= brcmnand_write_bbt(mtd
, buf
, td
, md
, chipsel
);
1144 /* Write the mirror bad block table to the device ? */
1145 if ((writeops
& 0x02) && md
&& (md
->options
& NAND_BBT_WRITE
)) {
1146 res
= brcmnand_write_bbt(mtd
, buf
, md
, td
, chipsel
);
1154 /* Define some generic bad / good block scan pattern which are used
1155 * while scanning a device for factory marked good / bad blocks.
1157 static uint8_t scan_ff_pattern
[] = { 0xff, 0xff };
1159 static struct nand_bbt_descr smallpage_memorybased
= {
1160 .options
= NAND_BBT_SCAN2NDPAGE
,
1163 .pattern
= scan_ff_pattern
1166 static struct nand_bbt_descr largepage_memorybased
= {
1170 .pattern
= scan_ff_pattern
1173 static struct nand_bbt_descr smallpage_flashbased
= {
1174 .options
= NAND_BBT_SCAN2NDPAGE
,
1177 .pattern
= scan_ff_pattern
1180 static struct nand_bbt_descr largepage_flashbased
= {
1181 .options
= NAND_BBT_SCAN2NDPAGE
,
1184 .pattern
= scan_ff_pattern
1187 /* 2K & 4K page MLC NAND use same pattern */
1188 static struct nand_bbt_descr bch4_flashbased
= {
1189 .options
= NAND_BBT_SCAN2NDPAGE
,
1192 .pattern
= scan_ff_pattern
1195 static uint8_t scan_agand_pattern
[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1197 static struct nand_bbt_descr agand_flashbased
= {
1198 .options
= NAND_BBT_SCANEMPTY
| NAND_BBT_SCANALLPAGES
,
1201 .pattern
= scan_agand_pattern
1204 /* Generic flash bbt decriptors
1206 static uint8_t bbt_pattern
[] = {'B', 'b', 't', '0' };
1207 static uint8_t mirror_pattern
[] = {'1', 't', 'b', 'B' };
1210 * THT: We only have 1 chip per device
1212 static struct nand_bbt_descr bbt_main_descr
= {
1213 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1214 | NAND_BBT_2BIT
| NAND_BBT_VERSION
/* | NAND_BBT_PERCHIP */,
1215 .offs
= 9, /* THT: Changed from 8 */
1217 .veroffs
= 13, /* THT: Changed from 12 */
1218 .maxblocks
= 4, /* THT: Will update later, based on 1MB partition for BBT */
1219 .pattern
= bbt_pattern
1222 static struct nand_bbt_descr bbt_mirror_descr
= {
1223 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1224 | NAND_BBT_2BIT
| NAND_BBT_VERSION
/* | NAND_BBT_PERCHIP */,
1225 .offs
= 9, /* THT: Changed from 8 */
1227 .veroffs
= 13, /* THT: Changed from 12 */
1229 .pattern
= mirror_pattern
1232 /* SLC flash using BCH-4 ECC, SM & Large page use same descriptor template */
1233 static struct nand_bbt_descr bbt_slc_bch4_main_descr
= {
1234 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1235 | NAND_BBT_2BIT
| NAND_BBT_VERSION
/* | NAND_BBT_PERCHIP */,
1236 .offs
= 1, /* THT: Changed from 8 */
1238 .veroffs
= 6, /* THT: Changed from 12 */
1240 .pattern
= bbt_pattern
1243 static struct nand_bbt_descr bbt_slc_bch4_mirror_descr
= {
1244 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1245 | NAND_BBT_2BIT
| NAND_BBT_VERSION
/* | NAND_BBT_PERCHIP */,
1250 .pattern
= mirror_pattern
1253 static struct nand_bbt_descr bbt_bch4_main_descr
= {
1254 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1255 | NAND_BBT_2BIT
| NAND_BBT_VERSION
/* | NAND_BBT_PERCHIP */,
1258 .veroffs
= 5, /* THT: Changed from 12 */
1259 .maxblocks
= 8, /* THT: Will update later, based on 4MB partition for BBT */
1260 .pattern
= bbt_pattern
1263 static struct nand_bbt_descr bbt_bch4_mirror_descr
= {
1264 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1265 | NAND_BBT_2BIT
| NAND_BBT_VERSION
/* | NAND_BBT_PERCHIP */,
1266 .offs
= 1, /* THT: Changed from 8 */
1268 .veroffs
= 5, /* THT: Changed from 12 */
1270 .pattern
= mirror_pattern
1274 * brcmnand_isbad_bbt - [NAND Interface] Check if a block is bad
1275 * @mtd: MTD device structure
1276 * @offs: offset in the device
1277 * @allowbbt: allow access to bad block table region
1279 * Each byte in the BBT contains 4 entries, 2 bits each per block.
1280 * So the entry for the block b is:
1281 * bbt[b >> 2] & (0x3 << ((b & 0x3) << 1)))
1284 int brcmnand_isbad_bbt(struct mtd_info
*mtd
, loff_t offs
, int allowbbt
)
1286 struct nand_chip
*this = mtd
->priv
;
1287 uint32_t block
; /* Used as an index, so 32bit. */
1290 /* THT 03/20/07: Get block number. It's more convenient to do it in the
1291 * following way but is actually the same thing as in comment the above
1293 block
= (uint32_t) (offs
>> (this->bbt_erase_shift
- 1));
1294 res
= (this->bbt
[block
>> 3] >> (block
& 0x06)) & 0x03;
1295 DEBUG(MTD_DEBUG_LEVEL3
, "brcmnand_isbad_bbt(): bbt info for offs "
1296 "0x%08x: (block %d) 0x%02x\n", (unsigned int)offs
, block
>> 1, res
);
1298 printk("brcmnand_isbad_bbt(): bbt info for offs "
1299 "0x%08x: (block %d) 0x%02x\n", (unsigned int)offs
, block
>> 1, res
);
1306 /* Marked bad due to wear */
1309 /* Reserved blocks */
1310 return allowbbt
? 0 : 1;
1316 * brcmnand_default_bbt - [NAND Interface] Select a default bad block table for the device
1317 * @mtd: MTD device structure
1319 * This function selects the default bad block table
1320 * support for the device and calls the nand_scan_bbt function
1323 int brcmnand_default_bbt(struct mtd_info
*mtd
)
1325 struct nand_chip
*this = mtd
->priv
;
1328 mtd
->get_device( mtd
);
1329 /* Default for AG-AND. We must use a flash based
1330 * bad block table as the devices have factory marked
1331 * _good_ blocks. Erasing those blocks leads to loss
1332 * of the good / bad information, so we _must_ store
1333 * this information in a good / bad table during
1336 if (this->options
& NAND_IS_AND
) {
1337 /* Use the default pattern descriptors */
1338 if (!this->bbt_td
) {
1339 this->bbt_td
= &bbt_main_descr
;
1340 this->bbt_md
= &bbt_mirror_descr
;
1342 this->options
|= NAND_USE_FLASH_BBT
;
1343 ret
= brcmnand_scan_bbt(mtd
, &agand_flashbased
);
1344 mtd
->put_device(mtd
);
1348 /* Is a flash based bad block table requested ? */
1349 if (this->options
& NAND_USE_FLASH_BBT
) {
1350 if (this->ecc
.bytes
== 3) {
1351 /* Use the default pattern descriptors */
1352 if (!this->bbt_td
) {
1353 this->bbt_td
= &bbt_main_descr
;
1354 this->bbt_md
= &bbt_mirror_descr
;
1356 if (!this->badblock_pattern
) {
1357 this->badblock_pattern
= (mtd
->writesize
> 512) ?
1358 &largepage_flashbased
: &smallpage_flashbased
;
1361 if(this->cellinfo
& NAND_CI_CELLTYPE_MSK
) {
1362 if (!this->bbt_td
) {
1363 this->bbt_td
= &bbt_bch4_main_descr
;
1364 this->bbt_md
= &bbt_bch4_mirror_descr
;
1366 if (!this->badblock_pattern
) {
1367 /* 2K and 4K MLC NAND use the same pattern */
1368 this->badblock_pattern
= &bch4_flashbased
;
1371 /* SLC using BCH-4 ECC */
1372 if (!this->bbt_td
) {
1373 this->bbt_td
= &bbt_slc_bch4_main_descr
;
1374 this->bbt_md
= &bbt_slc_bch4_mirror_descr
;
1376 if (!this->badblock_pattern
) {
1377 /* 2K and 4K MLC NAND use the same pattern */
1378 this->badblock_pattern
= (mtd
->writesize
> 512) ?
1379 &bch4_flashbased
: &smallpage_flashbased
;
1384 this->bbt_td
= NULL
;
1385 this->bbt_md
= NULL
;
1386 if (!this->badblock_pattern
) {
1387 this->badblock_pattern
= (mtd
->writesize
> 512) ?
1388 &largepage_memorybased
: &smallpage_memorybased
;
1392 /* BBT partition occupies 1 MB at the end of the useable flash, so adjust
1393 * maxblocks accordingly. Only applies to flash with 512MB or less, since
1394 * we don't have the extra reserved space at the end of the flash
1395 * (1FF0_0000 - 1FFF_FFFF).
1397 if (mtd
->size
<= (512 << 20)) {
1398 this->bbt_td
->maxblocks
= this->bbt_md
->maxblocks
=
1399 (1 << (20 - this->bbt_erase_shift
));
1401 ret
= brcmnand_scan_bbt(mtd
, this->badblock_pattern
);
1402 mtd
->put_device(mtd
);
1406 EXPORT_SYMBOL(brcmnand_scan_bbt
);
1407 EXPORT_SYMBOL(brcmnand_default_bbt
);