blob: 2f53b51c68054cb5a11aa17bf9646c56c7d975e3 [file] [log] [blame]
Kyungmin Park87590e22005-09-27 11:26:39 +01001/*
2 * linux/drivers/mtd/onenand/onenand_bbt.c
3 *
4 * Bad Block Table support for the OneNAND driver
5 *
6 * Copyright(c) 2005 Samsung Electronics
7 * Kyungmin Park <kyungmin.park@samsung.com>
8 *
9 * Derived from nand_bbt.c
10 *
11 * TODO:
12 * Split BBT core and chip specific BBT.
13 */
14
15#include <linux/slab.h>
16#include <linux/mtd/mtd.h>
17#include <linux/mtd/onenand.h>
18#include <linux/mtd/compatmac.h>
19
20/**
21 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
22 * @param buf the buffer to search
23 * @param len the length of buffer to search
24 * @param paglen the pagelength
25 * @param td search pattern descriptor
26 *
27 * Check for a pattern at the given place. Used to search bad block
28 * tables and good / bad block identifiers. Same as check_pattern, but
29 * no optional empty check and the pattern is expected to start
30 * at offset 0.
31 *
32 */
33static int check_short_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
34{
35 int i;
36 uint8_t *p = buf;
37
38 /* Compare the pattern */
39 for (i = 0; i < td->len; i++) {
40 if (p[i] != td->pattern[i])
41 return -1;
42 }
43 return 0;
44}
45
46/**
47 * create_bbt - [GENERIC] Create a bad block table by scanning the device
48 * @param mtd MTD device structure
49 * @param buf temporary buffer
50 * @param bd descriptor for the good/bad block search pattern
51 * @param chip create the table for a specific chip, -1 read all chips.
52 * Applies only if NAND_BBT_PERCHIP option is set
53 *
54 * Create a bad block table by scanning the device
55 * for the given good/bad block identify pattern
56 */
57static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip)
58{
59 struct onenand_chip *this = mtd->priv;
60 struct bbm_info *bbm = this->bbm;
61 int i, j, numblocks, len, scanlen;
62 int startblock;
63 loff_t from;
64 size_t readlen, ooblen;
Kyungmin Park211ac752007-02-07 12:15:01 +090065 struct mtd_oob_ops ops;
Kyungmin Park87590e22005-09-27 11:26:39 +010066
67 printk(KERN_INFO "Scanning device for bad blocks\n");
68
Adrian Hunterec255e32007-01-22 21:30:31 +090069 len = 2;
Kyungmin Park87590e22005-09-27 11:26:39 +010070
71 /* We need only read few bytes from the OOB area */
72 scanlen = ooblen = 0;
73 readlen = bd->len;
74
75 /* chip == -1 case only */
76 /* Note that numblocks is 2 * (real numblocks) here;
77 * see i += 2 below as it makses shifting and masking less painful
78 */
79 numblocks = mtd->size >> (bbm->bbt_erase_shift - 1);
80 startblock = 0;
81 from = 0;
82
Kyungmin Park211ac752007-02-07 12:15:01 +090083 ops.mode = MTD_OOB_PLACE;
84 ops.ooblen = readlen;
85 ops.oobbuf = buf;
86 ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
87
Kyungmin Park87590e22005-09-27 11:26:39 +010088 for (i = startblock; i < numblocks; ) {
89 int ret;
90
91 for (j = 0; j < len; j++) {
Kyungmin Park87590e22005-09-27 11:26:39 +010092 /* No need to read pages fully,
93 * just read required OOB bytes */
Kyungmin Park211ac752007-02-07 12:15:01 +090094 ret = onenand_bbt_read_oob(mtd, from + j * mtd->writesize + bd->offs, &ops);
Kyungmin Park87590e22005-09-27 11:26:39 +010095
Kyungmin Parkf6272482006-12-22 16:02:50 +090096 /* If it is a initial bad block, just ignore it */
Kyungmin Park211ac752007-02-07 12:15:01 +090097 if (ret == ONENAND_BBT_READ_FATAL_ERROR)
98 return -EIO;
Kyungmin Park87590e22005-09-27 11:26:39 +010099
Kyungmin Park211ac752007-02-07 12:15:01 +0900100 if (ret || check_short_pattern(&buf[j * scanlen], scanlen, mtd->writesize, bd)) {
Kyungmin Park87590e22005-09-27 11:26:39 +0100101 bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
102 printk(KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
103 i >> 1, (unsigned int) from);
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900104 mtd->ecc_stats.badblocks++;
Kyungmin Park87590e22005-09-27 11:26:39 +0100105 break;
106 }
107 }
108 i += 2;
109 from += (1 << bbm->bbt_erase_shift);
110 }
111
112 return 0;
113}
114
115
116/**
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
120 *
121 * The function creates a memory based bbt by scanning the device
122 * for manufacturer / software marked good / bad blocks
123 */
124static inline int onenand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
125{
Kyungmin Park532a37c2005-12-16 11:17:29 +0900126 struct onenand_chip *this = mtd->priv;
Kyungmin Park87590e22005-09-27 11:26:39 +0100127
128 bd->options &= ~NAND_BBT_SCANEMPTY;
Kyungmin Park532a37c2005-12-16 11:17:29 +0900129 return create_bbt(mtd, this->page_buf, bd, -1);
Kyungmin Park87590e22005-09-27 11:26:39 +0100130}
131
132/**
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
137 */
138static int onenand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
139{
140 struct onenand_chip *this = mtd->priv;
141 struct bbm_info *bbm = this->bbm;
142 int block;
143 uint8_t res;
144
145 /* Get block number * 2 */
146 block = (int) (offs >> (bbm->bbt_erase_shift - 1));
147 res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
148
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);
151
152 switch ((int) res) {
153 case 0x00: return 0;
154 case 0x01: return 1;
155 case 0x02: return allowbbt ? 0 : 1;
156 }
157
158 return 1;
159}
160
161/**
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
165 *
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.
170 *
Adrian Hunterf00b0042007-01-22 17:01:01 +0900171 * The bad block table memory is allocated here. It is freed
172 * by the onenand_release function.
Kyungmin Park87590e22005-09-27 11:26:39 +0100173 *
174 */
175int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
176{
177 struct onenand_chip *this = mtd->priv;
178 struct bbm_info *bbm = this->bbm;
179 int len, ret = 0;
180
181 len = mtd->size >> (this->erase_shift + 2);
Burman Yan95b93a02006-11-15 21:10:29 +0200182 /* Allocate memory (2bit per block) and clear the memory bad block table */
183 bbm->bbt = kzalloc(len, GFP_KERNEL);
Kyungmin Park87590e22005-09-27 11:26:39 +0100184 if (!bbm->bbt) {
185 printk(KERN_ERR "onenand_scan_bbt: Out of memory\n");
186 return -ENOMEM;
187 }
Kyungmin Park87590e22005-09-27 11:26:39 +0100188
189 /* Set the bad block position */
190 bbm->badblockpos = ONENAND_BADBLOCK_POS;
191
192 /* Set erase shift */
193 bbm->bbt_erase_shift = this->erase_shift;
194
195 if (!bbm->isbad_bbt)
196 bbm->isbad_bbt = onenand_isbad_bbt;
197
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");
201 kfree(bbm->bbt);
202 bbm->bbt = NULL;
203 }
204
205 return ret;
206}
207
208/*
209 * Define some generic bad / good block scan pattern which are used
210 * while scanning a device for factory marked good / bad blocks.
211 */
212static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
213
214static struct nand_bbt_descr largepage_memorybased = {
215 .options = 0,
216 .offs = 0,
217 .len = 2,
218 .pattern = scan_ff_pattern,
219};
220
221/**
222 * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
223 * @param mtd MTD device structure
224 *
225 * This function selects the default bad block table
226 * support for the device and calls the onenand_scan_bbt function
227 */
228int onenand_default_bbt(struct mtd_info *mtd)
229{
230 struct onenand_chip *this = mtd->priv;
231 struct bbm_info *bbm;
232
Burman Yan95b93a02006-11-15 21:10:29 +0200233 this->bbm = kzalloc(sizeof(struct bbm_info), GFP_KERNEL);
Kyungmin Park87590e22005-09-27 11:26:39 +0100234 if (!this->bbm)
235 return -ENOMEM;
236
237 bbm = this->bbm;
238
Kyungmin Park87590e22005-09-27 11:26:39 +0100239 /* 1KB page has same configuration as 2KB page */
240 if (!bbm->badblock_pattern)
241 bbm->badblock_pattern = &largepage_memorybased;
242
243 return onenand_scan_bbt(mtd, bbm->badblock_pattern);
244}
245
246EXPORT_SYMBOL(onenand_scan_bbt);
247EXPORT_SYMBOL(onenand_default_bbt);