2 * linux/drivers/mtd/onenand/onenand_bbt.c
4 * Bad Block Table support for the OneNAND driver
6 * Copyright(c) 2005-2007 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/mtd/compat.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/onenand.h>
23 #include <asm/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
;
72 printk(KERN_INFO
"Scanning device for bad blocks\n");
76 /* We need only read few bytes from the OOB area */
80 /* chip == -1 case only */
81 /* Note that numblocks is 2 * (real numblocks) here;
82 * see i += 2 below as it makses shifting and masking less painful
84 numblocks
= mtd
->size
>> (bbm
->bbt_erase_shift
- 1);
88 for (i
= startblock
; i
< numblocks
;) {
91 for (j
= 0; j
< len
; j
++) {
94 /* No need to read pages fully,
95 * just read required OOB bytes */
96 ret
= onenand_read_oob(mtd
,
97 from
+ j
* mtd
->writesize
+
98 bd
->offs
, readlen
, &retlen
,
101 if (ret
&& ret
!= -EAGAIN
) {
102 printk("ret = %d\n", ret
);
106 if (check_short_pattern
107 (&buf
[j
* scanlen
], scanlen
, mtd
->writesize
, bd
)) {
108 bbm
->bbt
[i
>> 3] |= 0x03 << (i
& 0x6);
110 "Bad eraseblock %d at 0x%08x\n", i
>> 1,
116 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
,
131 struct nand_bbt_descr
*bd
)
133 unsigned char data_buf
[MAX_ONENAND_PAGESIZE
];
135 bd
->options
&= ~NAND_BBT_SCANEMPTY
;
136 return create_bbt(mtd
, data_buf
, bd
, -1);
140 * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad
141 * @param mtd MTD device structure
142 * @param offs offset in the device
143 * @param allowbbt allow access to bad block table region
145 static int onenand_isbad_bbt(struct mtd_info
*mtd
, loff_t offs
, int allowbbt
)
147 struct onenand_chip
*this = mtd
->priv
;
148 struct bbm_info
*bbm
= this->bbm
;
152 /* Get block number * 2 */
153 block
= (int)(offs
>> (bbm
->bbt_erase_shift
- 1));
154 res
= (bbm
->bbt
[block
>> 3] >> (block
& 0x06)) & 0x03;
156 MTDDEBUG (MTD_DEBUG_LEVEL2
,
157 "onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
158 (unsigned int)offs
, block
>> 1, res
);
166 return allowbbt
? 0 : 1;
173 * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
174 * @param mtd MTD device structure
175 * @param bd descriptor for the good/bad block search pattern
177 * The function checks, if a bad block table(s) is/are already
178 * available. If not it scans the device for manufacturer
179 * marked good / bad blocks and writes the bad block table(s) to
180 * the selected place.
182 * The bad block table memory is allocated here. It must be freed
183 * by calling the onenand_free_bbt function.
186 int onenand_scan_bbt(struct mtd_info
*mtd
, struct nand_bbt_descr
*bd
)
188 struct onenand_chip
*this = mtd
->priv
;
189 struct bbm_info
*bbm
= this->bbm
;
192 len
= mtd
->size
>> (this->erase_shift
+ 2);
193 /* Allocate memory (2bit per block) */
194 bbm
->bbt
= malloc(len
);
196 printk(KERN_ERR
"onenand_scan_bbt: Out of memory\n");
199 /* Clear the memory bad block table */
200 memset(bbm
->bbt
, 0x00, len
);
202 /* Set the bad block position */
203 bbm
->badblockpos
= ONENAND_BADBLOCK_POS
;
205 /* Set erase shift */
206 bbm
->bbt_erase_shift
= this->erase_shift
;
209 bbm
->isbad_bbt
= onenand_isbad_bbt
;
211 /* Scan the device to build a memory based bad block table */
212 if ((ret
= onenand_memory_bbt(mtd
, bd
))) {
214 "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
223 * Define some generic bad / good block scan pattern which are used
224 * while scanning a device for factory marked good / bad blocks.
226 static uint8_t scan_ff_pattern
[] = { 0xff, 0xff };
228 static struct nand_bbt_descr largepage_memorybased
= {
232 .pattern
= scan_ff_pattern
,
236 * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
237 * @param mtd MTD device structure
239 * This function selects the default bad block table
240 * support for the device and calls the onenand_scan_bbt function
242 int onenand_default_bbt(struct mtd_info
*mtd
)
244 struct onenand_chip
*this = mtd
->priv
;
245 struct bbm_info
*bbm
;
247 this->bbm
= malloc(sizeof(struct bbm_info
));
253 memset(bbm
, 0, sizeof(struct bbm_info
));
255 /* 1KB page has same configuration as 2KB page */
256 if (!bbm
->badblock_pattern
)
257 bbm
->badblock_pattern
= &largepage_memorybased
;
259 return onenand_scan_bbt(mtd
, bbm
->badblock_pattern
);