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>
20 extern int onenand_do_read_oob(struct mtd_info
*mtd
, loff_t from
, size_t len
,
21 size_t *retlen
, u_char
*buf
);
24 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
25 * @param buf the buffer to search
26 * @param len the length of buffer to search
27 * @param paglen the pagelength
28 * @param td search pattern descriptor
30 * Check for a pattern at the given place. Used to search bad block
31 * tables and good / bad block identifiers. Same as check_pattern, but
32 * no optional empty check and the pattern is expected to start
36 static int check_short_pattern(uint8_t *buf
, int len
, int paglen
, struct nand_bbt_descr
*td
)
41 /* Compare the pattern */
42 for (i
= 0; i
< td
->len
; i
++) {
43 if (p
[i
] != td
->pattern
[i
])
50 * create_bbt - [GENERIC] Create a bad block table by scanning the device
51 * @param mtd MTD device structure
52 * @param buf temporary buffer
53 * @param bd descriptor for the good/bad block search pattern
54 * @param chip create the table for a specific chip, -1 read all chips.
55 * Applies only if NAND_BBT_PERCHIP option is set
57 * Create a bad block table by scanning the device
58 * for the given good/bad block identify pattern
60 static int create_bbt(struct mtd_info
*mtd
, uint8_t *buf
, struct nand_bbt_descr
*bd
, int chip
)
62 struct onenand_chip
*this = mtd
->priv
;
63 struct bbm_info
*bbm
= this->bbm
;
64 int i
, j
, numblocks
, len
, scanlen
;
67 size_t readlen
, ooblen
;
69 printk(KERN_INFO
"Scanning device for bad blocks\n");
73 /* We need only read few bytes from the OOB area */
77 /* chip == -1 case only */
78 /* Note that numblocks is 2 * (real numblocks) here;
79 * see i += 2 below as it makses shifting and masking less painful
81 numblocks
= mtd
->size
>> (bbm
->bbt_erase_shift
- 1);
85 for (i
= startblock
; i
< numblocks
; ) {
88 for (j
= 0; j
< len
; j
++) {
91 /* No need to read pages fully,
92 * just read required OOB bytes */
93 ret
= onenand_do_read_oob(mtd
, from
+ j
* mtd
->writesize
+ bd
->offs
,
94 readlen
, &retlen
, &buf
[0]);
96 /* If it is a initial bad block, just ignore it */
97 if (ret
&& !(ret
& ONENAND_CTRL_LOAD
))
100 if (check_short_pattern(&buf
[j
* scanlen
], scanlen
, mtd
->writesize
, bd
)) {
101 bbm
->bbt
[i
>> 3] |= 0x03 << (i
& 0x6);
102 printk(KERN_WARNING
"Bad eraseblock %d at 0x%08x\n",
103 i
>> 1, (unsigned int) from
);
104 mtd
->ecc_stats
.badblocks
++;
109 from
+= (1 << bbm
->bbt_erase_shift
);
117 * onenand_memory_bbt - [GENERIC] create a memory based bad block table
118 * @param mtd MTD device structure
119 * @param bd descriptor for the good/bad block search pattern
121 * The function creates a memory based bbt by scanning the device
122 * for manufacturer / software marked good / bad blocks
124 static inline int onenand_memory_bbt (struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
126 struct onenand_chip
*this = mtd
->priv
;
128 bd
->options
&= ~NAND_BBT_SCANEMPTY
;
129 return create_bbt(mtd
, this->page_buf
, bd
, -1);
133 * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad
134 * @param mtd MTD device structure
135 * @param offs offset in the device
136 * @param allowbbt allow access to bad block table region
138 static int onenand_isbad_bbt(struct mtd_info
*mtd
, loff_t offs
, int allowbbt
)
140 struct onenand_chip
*this = mtd
->priv
;
141 struct bbm_info
*bbm
= this->bbm
;
145 /* Get block number * 2 */
146 block
= (int) (offs
>> (bbm
->bbt_erase_shift
- 1));
147 res
= (bbm
->bbt
[block
>> 3] >> (block
& 0x06)) & 0x03;
149 DEBUG(MTD_DEBUG_LEVEL2
, "onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
150 (unsigned int) offs
, block
>> 1, res
);
155 case 0x02: return allowbbt
? 0 : 1;
162 * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
163 * @param mtd MTD device structure
164 * @param bd descriptor for the good/bad block search pattern
166 * The function checks, if a bad block table(s) is/are already
167 * available. If not it scans the device for manufacturer
168 * marked good / bad blocks and writes the bad block table(s) to
169 * the selected place.
171 * The bad block table memory is allocated here. It must be freed
172 * by calling the onenand_free_bbt function.
175 int onenand_scan_bbt(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
177 struct onenand_chip
*this = mtd
->priv
;
178 struct bbm_info
*bbm
= this->bbm
;
181 len
= mtd
->size
>> (this->erase_shift
+ 2);
182 /* Allocate memory (2bit per block) and clear the memory bad block table */
183 bbm
->bbt
= kzalloc(len
, GFP_KERNEL
);
185 printk(KERN_ERR
"onenand_scan_bbt: Out of memory\n");
189 /* Set the bad block position */
190 bbm
->badblockpos
= ONENAND_BADBLOCK_POS
;
192 /* Set erase shift */
193 bbm
->bbt_erase_shift
= this->erase_shift
;
196 bbm
->isbad_bbt
= onenand_isbad_bbt
;
198 /* Scan the device to build a memory based bad block table */
199 if ((ret
= onenand_memory_bbt(mtd
, bd
))) {
200 printk(KERN_ERR
"onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
209 * Define some generic bad / good block scan pattern which are used
210 * while scanning a device for factory marked good / bad blocks.
212 static uint8_t scan_ff_pattern
[] = { 0xff, 0xff };
214 static struct nand_bbt_descr largepage_memorybased
= {
218 .pattern
= scan_ff_pattern
,
222 * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
223 * @param mtd MTD device structure
225 * This function selects the default bad block table
226 * support for the device and calls the onenand_scan_bbt function
228 int onenand_default_bbt(struct mtd_info
*mtd
)
230 struct onenand_chip
*this = mtd
->priv
;
231 struct bbm_info
*bbm
;
233 this->bbm
= kzalloc(sizeof(struct bbm_info
), GFP_KERNEL
);
239 /* 1KB page has same configuration as 2KB page */
240 if (!bbm
->badblock_pattern
)
241 bbm
->badblock_pattern
= &largepage_memorybased
;
243 return onenand_scan_bbt(mtd
, bbm
->badblock_pattern
);
246 EXPORT_SYMBOL(onenand_scan_bbt
);
247 EXPORT_SYMBOL(onenand_default_bbt
);