2 * linux/drivers/mtd/onenand/onenand_bbt.c
4 * Bad Block Table support for the OneNAND driver
6 * Copyright(c) 2005 Samsung Electronics
7 * Kyungmin Park <kyungmin.park@samsung.com>
9 * Derived from nand_bbt.c
12 * Split BBT core and chip specific BBT.
15 #include <linux/slab.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/onenand.h>
18 #include <linux/mtd/compatmac.h>
21 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
22 * @param buf the buffer to search
23 * @param len the length of buffer to search
24 * @param paglen the pagelength
25 * @param td search pattern descriptor
27 * Check for a pattern at the given place. Used to search bad block
28 * tables and good / bad block identifiers. Same as check_pattern, but
29 * no optional empty check and the pattern is expected to start
33 static int check_short_pattern(uint8_t *buf
, int len
, int paglen
, struct nand_bbt_descr
*td
)
38 /* Compare the pattern */
39 for (i
= 0; i
< td
->len
; i
++) {
40 if (p
[i
] != td
->pattern
[i
])
47 * create_bbt - [GENERIC] Create a bad block table by scanning the device
48 * @param mtd MTD device structure
49 * @param buf temporary buffer
50 * @param bd descriptor for the good/bad block search pattern
51 * @param chip create the table for a specific chip, -1 read all chips.
52 * Applies only if NAND_BBT_PERCHIP option is set
54 * Create a bad block table by scanning the device
55 * for the given good/bad block identify pattern
57 static int create_bbt(struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*bd
, int chip
)
59 struct onenand_chip
*this = mtd
->priv
;
60 struct bbm_info
*bbm
= this->bbm
;
61 int i
, j
, numblocks
, len
, scanlen
;
64 size_t readlen
, ooblen
;
65 struct mtd_oob_ops ops
;
68 printk(KERN_INFO
"Scanning device for bad blocks\n");
72 /* We need only read few bytes from the OOB area */
76 /* chip == -1 case only */
77 /* Note that numblocks is 2 * (real numblocks) here;
78 * see i += 2 below as it makses shifting and masking less painful
80 numblocks
= this->chipsize
>> (bbm
->bbt_erase_shift
- 1);
84 ops
.mode
= MTD_OOB_PLACE
;
87 ops
.len
= ops
.ooboffs
= ops
.retlen
= ops
.oobretlen
= 0;
89 for (i
= startblock
; i
< numblocks
; ) {
92 for (j
= 0; j
< len
; j
++) {
93 /* No need to read pages fully,
94 * just read required OOB bytes */
95 ret
= onenand_bbt_read_oob(mtd
, from
+ j
* mtd
->writesize
+ bd
->offs
, &ops
);
97 /* If it is a initial bad block, just ignore it */
98 if (ret
== ONENAND_BBT_READ_FATAL_ERROR
)
101 if (ret
|| check_short_pattern(&buf
[j
* scanlen
], scanlen
, mtd
->writesize
, bd
)) {
102 bbm
->bbt
[i
>> 3] |= 0x03 << (i
& 0x6);
103 printk(KERN_WARNING
"Bad eraseblock %d at 0x%08x\n",
104 i
>> 1, (unsigned int) from
);
105 mtd
->ecc_stats
.badblocks
++;
111 if (FLEXONENAND(this)) {
112 rgn
= flexonenand_region(mtd
, from
);
113 from
+= mtd
->eraseregions
[rgn
].erasesize
;
115 from
+= (1 << bbm
->bbt_erase_shift
);
123 * onenand_memory_bbt - [GENERIC] create a memory based bad block table
124 * @param mtd MTD device structure
125 * @param bd descriptor for the good/bad block search pattern
127 * The function creates a memory based bbt by scanning the device
128 * for manufacturer / software marked good / bad blocks
130 static inline int onenand_memory_bbt (struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
132 struct onenand_chip
*this = mtd
->priv
;
134 bd
->options
&= ~NAND_BBT_SCANEMPTY
;
135 return create_bbt(mtd
, this->page_buf
, bd
, -1);
139 * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad
140 * @param mtd MTD device structure
141 * @param offs offset in the device
142 * @param allowbbt allow access to bad block table region
144 static int onenand_isbad_bbt(struct mtd_info
*mtd
, loff_t offs
, int allowbbt
)
146 struct onenand_chip
*this = mtd
->priv
;
147 struct bbm_info
*bbm
= this->bbm
;
151 /* Get block number * 2 */
152 block
= (int) (onenand_block(this, offs
) << 1);
153 res
= (bbm
->bbt
[block
>> 3] >> (block
& 0x06)) & 0x03;
155 DEBUG(MTD_DEBUG_LEVEL2
, "onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
156 (unsigned int) offs
, block
>> 1, res
);
161 case 0x02: return allowbbt
? 0 : 1;
168 * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
169 * @param mtd MTD device structure
170 * @param bd descriptor for the good/bad block search pattern
172 * The function checks, if a bad block table(s) is/are already
173 * available. If not it scans the device for manufacturer
174 * marked good / bad blocks and writes the bad block table(s) to
175 * the selected place.
177 * The bad block table memory is allocated here. It is freed
178 * by the onenand_release function.
181 int onenand_scan_bbt(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
183 struct onenand_chip
*this = mtd
->priv
;
184 struct bbm_info
*bbm
= this->bbm
;
187 len
= this->chipsize
>> (this->erase_shift
+ 2);
188 /* Allocate memory (2bit per block) and clear the memory bad block table */
189 bbm
->bbt
= kzalloc(len
, GFP_KERNEL
);
191 printk(KERN_ERR
"onenand_scan_bbt: Out of memory\n");
195 /* Set the bad block position */
196 bbm
->badblockpos
= ONENAND_BADBLOCK_POS
;
198 /* Set erase shift */
199 bbm
->bbt_erase_shift
= this->erase_shift
;
202 bbm
->isbad_bbt
= onenand_isbad_bbt
;
204 /* Scan the device to build a memory based bad block table */
205 if ((ret
= onenand_memory_bbt(mtd
, bd
))) {
206 printk(KERN_ERR
"onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
215 * Define some generic bad / good block scan pattern which are used
216 * while scanning a device for factory marked good / bad blocks.
218 static uint8_t scan_ff_pattern
[] = { 0xff, 0xff };
220 static struct nand_bbt_descr largepage_memorybased
= {
224 .pattern
= scan_ff_pattern
,
228 * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
229 * @param mtd MTD device structure
231 * This function selects the default bad block table
232 * support for the device and calls the onenand_scan_bbt function
234 int onenand_default_bbt(struct mtd_info
*mtd
)
236 struct onenand_chip
*this = mtd
->priv
;
237 struct bbm_info
*bbm
;
239 this->bbm
= kzalloc(sizeof(struct bbm_info
), GFP_KERNEL
);
245 /* 1KB page has same configuration as 2KB page */
246 if (!bbm
->badblock_pattern
)
247 bbm
->badblock_pattern
= &largepage_memorybased
;
249 return onenand_scan_bbt(mtd
, bbm
->badblock_pattern
);
252 EXPORT_SYMBOL(onenand_scan_bbt
);
253 EXPORT_SYMBOL(onenand_default_bbt
);