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;
71 struct mtd_oob_ops ops;
73 printk(KERN_INFO "Scanning device for bad blocks\n");
77 /* We need only read few bytes from the OOB area */
81 /* chip == -1 case only */
82 /* Note that numblocks is 2 * (real numblocks) here;
83 * see i += 2 below as it makses shifting and masking less painful
85 numblocks = mtd->size >> (bbm->bbt_erase_shift - 1);
89 ops.mode = MTD_OOB_PLACE;
92 ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
94 for (i = startblock; i < numblocks;) {
97 for (j = 0; j < len; j++) {
98 /* No need to read pages fully,
99 * just read required OOB bytes */
100 ret = onenand_bbt_read_oob(mtd,
101 from + j * mtd->writesize +
104 /* If it is a initial bad block, just ignore it */
105 if (ret == ONENAND_BBT_READ_FATAL_ERROR)
108 if (ret || check_short_pattern
109 (&buf[j * scanlen], scanlen, mtd->writesize, bd)) {
110 bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
112 "Bad eraseblock %d at 0x%08x\n", i >> 1,
118 from += (1 << bbm->bbt_erase_shift);
125 * onenand_memory_bbt - [GENERIC] create a memory based bad block table
126 * @param mtd MTD device structure
127 * @param bd descriptor for the good/bad block search pattern
129 * The function creates a memory based bbt by scanning the device
130 * for manufacturer / software marked good / bad blocks
132 static inline int onenand_memory_bbt(struct mtd_info *mtd,
133 struct nand_bbt_descr *bd)
135 unsigned char data_buf[MAX_ONENAND_PAGESIZE];
137 bd->options &= ~NAND_BBT_SCANEMPTY;
138 return create_bbt(mtd, data_buf, bd, -1);
142 * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad
143 * @param mtd MTD device structure
144 * @param offs offset in the device
145 * @param allowbbt allow access to bad block table region
147 static int onenand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
149 struct onenand_chip *this = mtd->priv;
150 struct bbm_info *bbm = this->bbm;
154 /* Get block number * 2 */
155 block = (int)(offs >> (bbm->bbt_erase_shift - 1));
156 res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
158 MTDDEBUG (MTD_DEBUG_LEVEL2,
159 "onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
160 (unsigned int)offs, block >> 1, res);
168 return allowbbt ? 0 : 1;
175 * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
176 * @param mtd MTD device structure
177 * @param bd descriptor for the good/bad block search pattern
179 * The function checks, if a bad block table(s) is/are already
180 * available. If not it scans the device for manufacturer
181 * marked good / bad blocks and writes the bad block table(s) to
182 * the selected place.
184 * The bad block table memory is allocated here. It must be freed
185 * by calling the onenand_free_bbt function.
188 int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
190 struct onenand_chip *this = mtd->priv;
191 struct bbm_info *bbm = this->bbm;
194 len = mtd->size >> (this->erase_shift + 2);
195 /* Allocate memory (2bit per block) */
196 bbm->bbt = malloc(len);
198 printk(KERN_ERR "onenand_scan_bbt: Out of memory\n");
201 /* Clear the memory bad block table */
202 memset(bbm->bbt, 0x00, len);
204 /* Set the bad block position */
205 bbm->badblockpos = ONENAND_BADBLOCK_POS;
207 /* Set erase shift */
208 bbm->bbt_erase_shift = this->erase_shift;
211 bbm->isbad_bbt = onenand_isbad_bbt;
213 /* Scan the device to build a memory based bad block table */
214 if ((ret = onenand_memory_bbt(mtd, bd))) {
216 "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
225 * Define some generic bad / good block scan pattern which are used
226 * while scanning a device for factory marked good / bad blocks.
228 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
230 static struct nand_bbt_descr largepage_memorybased = {
234 .pattern = scan_ff_pattern,
238 * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
239 * @param mtd MTD device structure
241 * This function selects the default bad block table
242 * support for the device and calls the onenand_scan_bbt function
244 int onenand_default_bbt(struct mtd_info *mtd)
246 struct onenand_chip *this = mtd->priv;
247 struct bbm_info *bbm;
249 this->bbm = malloc(sizeof(struct bbm_info));
255 memset(bbm, 0, sizeof(struct bbm_info));
257 /* 1KB page has same configuration as 2KB page */
258 if (!bbm->badblock_pattern)
259 bbm->badblock_pattern = &largepage_memorybased;
261 return onenand_scan_bbt(mtd, bbm->badblock_pattern);