2 * linux/drivers/mtd/onenand/onenand_bbt.c
4 * Bad Block Table support for the OneNAND driver
6 * Copyright(c) 2005-2008 Samsung Electronics
7 * Kyungmin Park <kyungmin.park@samsung.com>
10 * Split BBT core and chip specific BBT.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #include <linux/compat.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/onenand.h>
23 #include <linux/errno.h>
26 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
27 * @param buf the buffer to search
28 * @param len the length of buffer to search
29 * @param paglen the pagelength
30 * @param td search pattern descriptor
32 * Check for a pattern at the given place. Used to search bad block
33 * tables and good / bad block identifiers. Same as check_pattern, but
34 * no optional empty check and the pattern is expected to start
37 static int check_short_pattern(uint8_t * buf
, int len
, int paglen
,
38 struct nand_bbt_descr
*td
)
43 /* Compare the pattern */
44 for (i
= 0; i
< td
->len
; i
++) {
45 if (p
[i
] != td
->pattern
[i
])
52 * create_bbt - [GENERIC] Create a bad block table by scanning the device
53 * @param mtd MTD device structure
54 * @param buf temporary buffer
55 * @param bd descriptor for the good/bad block search pattern
56 * @param chip create the table for a specific chip, -1 read all chips.
57 * Applies only if NAND_BBT_PERCHIP option is set
59 * Create a bad block table by scanning the device
60 * for the given good/bad block identify pattern
62 static int create_bbt(struct mtd_info
*mtd
, uint8_t * buf
,
63 struct nand_bbt_descr
*bd
, int chip
)
65 struct onenand_chip
*this = mtd
->priv
;
66 struct bbm_info
*bbm
= this->bbm
;
67 int i
, j
, numblocks
, len
, scanlen
;
70 size_t readlen
, ooblen
;
71 struct mtd_oob_ops ops
;
74 printk(KERN_INFO
"Scanning device for bad blocks\n");
78 /* We need only read few bytes from the OOB area */
82 /* chip == -1 case only */
83 /* Note that numblocks is 2 * (real numblocks) here;
84 * see i += 2 below as it makses shifting and masking less painful
86 numblocks
= this->chipsize
>> (bbm
->bbt_erase_shift
- 1);
90 ops
.mode
= MTD_OPS_PLACE_OOB
;
93 ops
.len
= ops
.ooboffs
= ops
.retlen
= ops
.oobretlen
= 0;
95 for (i
= startblock
; i
< numblocks
;) {
98 for (j
= 0; j
< len
; j
++) {
99 /* No need to read pages fully,
100 * just read required OOB bytes */
101 ret
= onenand_bbt_read_oob(mtd
,
102 from
+ j
* mtd
->writesize
+
105 /* If it is a initial bad block, just ignore it */
106 if (ret
== ONENAND_BBT_READ_FATAL_ERROR
)
109 if (ret
|| check_short_pattern
110 (&buf
[j
* scanlen
], scanlen
, mtd
->writesize
, bd
)) {
111 bbm
->bbt
[i
>> 3] |= 0x03 << (i
& 0x6);
113 "Bad eraseblock %d at 0x%08x\n", i
>> 1,
120 if (FLEXONENAND(this)) {
121 rgn
= flexonenand_region(mtd
, from
);
122 from
+= mtd
->eraseregions
[rgn
].erasesize
;
124 from
+= (1 << bbm
->bbt_erase_shift
);
131 * onenand_memory_bbt - [GENERIC] create a memory based bad block table
132 * @param mtd MTD device structure
133 * @param bd descriptor for the good/bad block search pattern
135 * The function creates a memory based bbt by scanning the device
136 * for manufacturer / software marked good / bad blocks
138 static inline int onenand_memory_bbt(struct mtd_info
*mtd
,
139 struct nand_bbt_descr
*bd
)
141 unsigned char data_buf
[MAX_ONENAND_PAGESIZE
];
143 return create_bbt(mtd
, data_buf
, bd
, -1);
147 * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad
148 * @param mtd MTD device structure
149 * @param offs offset in the device
150 * @param allowbbt allow access to bad block table region
152 static int onenand_isbad_bbt(struct mtd_info
*mtd
, loff_t offs
, int allowbbt
)
154 struct onenand_chip
*this = mtd
->priv
;
155 struct bbm_info
*bbm
= this->bbm
;
159 /* Get block number * 2 */
160 block
= (int) (onenand_block(this, offs
) << 1);
161 res
= (bbm
->bbt
[block
>> 3] >> (block
& 0x06)) & 0x03;
163 pr_debug("onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
164 (unsigned int)offs
, block
>> 1, res
);
172 return allowbbt
? 0 : 1;
179 * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
180 * @param mtd MTD device structure
181 * @param bd descriptor for the good/bad block search pattern
183 * The function checks, if a bad block table(s) is/are already
184 * available. If not it scans the device for manufacturer
185 * marked good / bad blocks and writes the bad block table(s) to
186 * the selected place.
188 * The bad block table memory is allocated here. It must be freed
189 * by calling the onenand_free_bbt function.
192 int onenand_scan_bbt(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
194 struct onenand_chip
*this = mtd
->priv
;
195 struct bbm_info
*bbm
= this->bbm
;
198 len
= this->chipsize
>> (this->erase_shift
+ 2);
199 /* Allocate memory (2bit per block) */
200 bbm
->bbt
= malloc(len
);
203 /* Clear the memory bad block table */
204 memset(bbm
->bbt
, 0x00, len
);
206 /* Set the bad block position */
207 bbm
->badblockpos
= ONENAND_BADBLOCK_POS
;
209 /* Set erase shift */
210 bbm
->bbt_erase_shift
= this->erase_shift
;
213 bbm
->isbad_bbt
= onenand_isbad_bbt
;
215 /* Scan the device to build a memory based bad block table */
216 if ((ret
= onenand_memory_bbt(mtd
, bd
))) {
218 "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
227 * Define some generic bad / good block scan pattern which are used
228 * while scanning a device for factory marked good / bad blocks.
230 static uint8_t scan_ff_pattern
[] = { 0xff, 0xff };
232 static struct nand_bbt_descr largepage_memorybased
= {
236 .pattern
= scan_ff_pattern
,
240 * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
241 * @param mtd MTD device structure
243 * This function selects the default bad block table
244 * support for the device and calls the onenand_scan_bbt function
246 int onenand_default_bbt(struct mtd_info
*mtd
)
248 struct onenand_chip
*this = mtd
->priv
;
249 struct bbm_info
*bbm
;
251 this->bbm
= malloc(sizeof(struct bbm_info
));
257 memset(bbm
, 0, sizeof(struct bbm_info
));
259 /* 1KB page has same configuration as 2KB page */
260 if (!bbm
->badblock_pattern
)
261 bbm
->badblock_pattern
= &largepage_memorybased
;
263 return onenand_scan_bbt(mtd
, bbm
->badblock_pattern
);