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.
19 #ifdef CONFIG_CMD_ONENAND
21 #include <linux/mtd/compat.h>
22 #include <linux/mtd/mtd.h>
23 #include <linux/mtd/onenand.h>
26 #include <asm/errno.h>
29 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
30 * @param buf the buffer to search
31 * @param len the length of buffer to search
32 * @param paglen the pagelength
33 * @param td search pattern descriptor
35 * Check for a pattern at the given place. Used to search bad block
36 * tables and good / bad block identifiers. Same as check_pattern, but
37 * no optional empty check and the pattern is expected to start
40 static int check_short_pattern(uint8_t * buf, int len, int paglen,
41 struct nand_bbt_descr *td)
46 /* Compare the pattern */
47 for (i = 0; i < td->len; i++) {
48 if (p[i] != td->pattern[i])
55 * create_bbt - [GENERIC] Create a bad block table by scanning the device
56 * @param mtd MTD device structure
57 * @param buf temporary buffer
58 * @param bd descriptor for the good/bad block search pattern
59 * @param chip create the table for a specific chip, -1 read all chips.
60 * Applies only if NAND_BBT_PERCHIP option is set
62 * Create a bad block table by scanning the device
63 * for the given good/bad block identify pattern
65 static int create_bbt(struct mtd_info *mtd, uint8_t * buf,
66 struct nand_bbt_descr *bd, int chip)
68 struct onenand_chip *this = mtd->priv;
69 struct bbm_info *bbm = this->bbm;
70 int i, j, numblocks, len, scanlen;
73 size_t readlen, ooblen;
75 printk(KERN_INFO "Scanning device for bad blocks\n");
79 /* We need only read few bytes from the OOB area */
83 /* chip == -1 case only */
84 /* Note that numblocks is 2 * (real numblocks) here;
85 * see i += 2 below as it makses shifting and masking less painful
87 numblocks = mtd->size >> (bbm->bbt_erase_shift - 1);
91 for (i = startblock; i < numblocks;) {
94 for (j = 0; j < len; j++) {
97 /* No need to read pages fully,
98 * just read required OOB bytes */
99 ret = onenand_read_oob(mtd,
100 from + j * mtd->oobblock +
101 bd->offs, readlen, &retlen,
104 if (ret && ret != -EAGAIN) {
105 printk("ret = %d\n", ret);
109 if (check_short_pattern
110 (&buf[j * scanlen], scanlen, mtd->oobblock, bd)) {
111 bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
113 "Bad eraseblock %d at 0x%08x\n", i >> 1,
119 from += (1 << bbm->bbt_erase_shift);
126 * onenand_memory_bbt - [GENERIC] create a memory based bad block table
127 * @param mtd MTD device structure
128 * @param bd descriptor for the good/bad block search pattern
130 * The function creates a memory based bbt by scanning the device
131 * for manufacturer / software marked good / bad blocks
133 static inline int onenand_memory_bbt(struct mtd_info *mtd,
134 struct nand_bbt_descr *bd)
136 unsigned char data_buf[MAX_ONENAND_PAGESIZE];
138 bd->options &= ~NAND_BBT_SCANEMPTY;
139 return create_bbt(mtd, data_buf, bd, -1);
143 * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad
144 * @param mtd MTD device structure
145 * @param offs offset in the device
146 * @param allowbbt allow access to bad block table region
148 static int onenand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
150 struct onenand_chip *this = mtd->priv;
151 struct bbm_info *bbm = this->bbm;
155 /* Get block number * 2 */
156 block = (int)(offs >> (bbm->bbt_erase_shift - 1));
157 res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
159 DEBUG(MTD_DEBUG_LEVEL2,
160 "onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
161 (unsigned int)offs, block >> 1, res);
169 return allowbbt ? 0 : 1;
176 * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
177 * @param mtd MTD device structure
178 * @param bd descriptor for the good/bad block search pattern
180 * The function checks, if a bad block table(s) is/are already
181 * available. If not it scans the device for manufacturer
182 * marked good / bad blocks and writes the bad block table(s) to
183 * the selected place.
185 * The bad block table memory is allocated here. It must be freed
186 * by calling the onenand_free_bbt function.
189 int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
191 struct onenand_chip *this = mtd->priv;
192 struct bbm_info *bbm = this->bbm;
195 len = mtd->size >> (this->erase_shift + 2);
196 /* Allocate memory (2bit per block) */
197 bbm->bbt = malloc(len);
199 printk(KERN_ERR "onenand_scan_bbt: Out of memory\n");
202 /* Clear the memory bad block table */
203 memset(bbm->bbt, 0x00, len);
205 /* Set the bad block position */
206 bbm->badblockpos = ONENAND_BADBLOCK_POS;
208 /* Set erase shift */
209 bbm->bbt_erase_shift = this->erase_shift;
212 bbm->isbad_bbt = onenand_isbad_bbt;
214 /* Scan the device to build a memory based bad block table */
215 if ((ret = onenand_memory_bbt(mtd, bd))) {
217 "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
226 * Define some generic bad / good block scan pattern which are used
227 * while scanning a device for factory marked good / bad blocks.
229 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
231 static struct nand_bbt_descr largepage_memorybased = {
235 .pattern = scan_ff_pattern,
239 * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
240 * @param mtd MTD device structure
242 * This function selects the default bad block table
243 * support for the device and calls the onenand_scan_bbt function
245 int onenand_default_bbt(struct mtd_info *mtd)
247 struct onenand_chip *this = mtd->priv;
248 struct bbm_info *bbm;
250 this->bbm = malloc(sizeof(struct bbm_info));
256 memset(bbm, 0, sizeof(struct bbm_info));
258 /* 1KB page has same configuration as 2KB page */
259 if (!bbm->badblock_pattern)
260 bbm->badblock_pattern = &largepage_memorybased;
262 return onenand_scan_bbt(mtd, bbm->badblock_pattern);
265 #endif /* CFG_CMD_ONENAND */