blob: e3822c1cdb8d99ed15b412a5f8bc63617ef2ac9a [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
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020020extern int onenand_do_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
21 size_t *retlen, u_char *buf);
22
Kyungmin Park87590e22005-09-27 11:26:39 +010023/**
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
29 *
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
33 * at offset 0.
34 *
35 */
36static int check_short_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
37{
38 int i;
39 uint8_t *p = buf;
40
41 /* Compare the pattern */
42 for (i = 0; i < td->len; i++) {
43 if (p[i] != td->pattern[i])
44 return -1;
45 }
46 return 0;
47}
48
49/**
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
56 *
57 * Create a bad block table by scanning the device
58 * for the given good/bad block identify pattern
59 */
60static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip)
61{
62 struct onenand_chip *this = mtd->priv;
63 struct bbm_info *bbm = this->bbm;
64 int i, j, numblocks, len, scanlen;
65 int startblock;
66 loff_t from;
67 size_t readlen, ooblen;
68
69 printk(KERN_INFO "Scanning device for bad blocks\n");
70
71 len = 1;
72
73 /* We need only read few bytes from the OOB area */
74 scanlen = ooblen = 0;
75 readlen = bd->len;
76
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
80 */
81 numblocks = mtd->size >> (bbm->bbt_erase_shift - 1);
82 startblock = 0;
83 from = 0;
84
85 for (i = startblock; i < numblocks; ) {
86 int ret;
87
88 for (j = 0; j < len; j++) {
89 size_t retlen;
90
91 /* No need to read pages fully,
92 * just read required OOB bytes */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020093 ret = onenand_do_read_oob(mtd, from + j * mtd->writesize + bd->offs,
94 readlen, &retlen, &buf[0]);
Kyungmin Park87590e22005-09-27 11:26:39 +010095
96 if (ret)
97 return ret;
98
Joern Engel28318772006-05-22 23:18:05 +020099 if (check_short_pattern(&buf[j * scanlen], scanlen, mtd->writesize, bd)) {
Kyungmin Park87590e22005-09-27 11:26:39 +0100100 bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
101 printk(KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
102 i >> 1, (unsigned int) from);
103 break;
104 }
105 }
106 i += 2;
107 from += (1 << bbm->bbt_erase_shift);
108 }
109
110 return 0;
111}
112
113
114/**
115 * onenand_memory_bbt - [GENERIC] create a memory based bad block table
116 * @param mtd MTD device structure
117 * @param bd descriptor for the good/bad block search pattern
118 *
119 * The function creates a memory based bbt by scanning the device
120 * for manufacturer / software marked good / bad blocks
121 */
122static inline int onenand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
123{
Kyungmin Park532a37c2005-12-16 11:17:29 +0900124 struct onenand_chip *this = mtd->priv;
Kyungmin Park87590e22005-09-27 11:26:39 +0100125
126 bd->options &= ~NAND_BBT_SCANEMPTY;
Kyungmin Park532a37c2005-12-16 11:17:29 +0900127 return create_bbt(mtd, this->page_buf, bd, -1);
Kyungmin Park87590e22005-09-27 11:26:39 +0100128}
129
130/**
131 * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad
132 * @param mtd MTD device structure
133 * @param offs offset in the device
134 * @param allowbbt allow access to bad block table region
135 */
136static int onenand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
137{
138 struct onenand_chip *this = mtd->priv;
139 struct bbm_info *bbm = this->bbm;
140 int block;
141 uint8_t res;
142
143 /* Get block number * 2 */
144 block = (int) (offs >> (bbm->bbt_erase_shift - 1));
145 res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
146
147 DEBUG(MTD_DEBUG_LEVEL2, "onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
148 (unsigned int) offs, block >> 1, res);
149
150 switch ((int) res) {
151 case 0x00: return 0;
152 case 0x01: return 1;
153 case 0x02: return allowbbt ? 0 : 1;
154 }
155
156 return 1;
157}
158
159/**
160 * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
161 * @param mtd MTD device structure
162 * @param bd descriptor for the good/bad block search pattern
163 *
164 * The function checks, if a bad block table(s) is/are already
165 * available. If not it scans the device for manufacturer
166 * marked good / bad blocks and writes the bad block table(s) to
167 * the selected place.
168 *
169 * The bad block table memory is allocated here. It must be freed
170 * by calling the onenand_free_bbt function.
171 *
172 */
173int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
174{
175 struct onenand_chip *this = mtd->priv;
176 struct bbm_info *bbm = this->bbm;
177 int len, ret = 0;
178
179 len = mtd->size >> (this->erase_shift + 2);
Burman Yan95b93a02006-11-15 21:10:29 +0200180 /* Allocate memory (2bit per block) and clear the memory bad block table */
181 bbm->bbt = kzalloc(len, GFP_KERNEL);
Kyungmin Park87590e22005-09-27 11:26:39 +0100182 if (!bbm->bbt) {
183 printk(KERN_ERR "onenand_scan_bbt: Out of memory\n");
184 return -ENOMEM;
185 }
Kyungmin Park87590e22005-09-27 11:26:39 +0100186
187 /* Set the bad block position */
188 bbm->badblockpos = ONENAND_BADBLOCK_POS;
189
190 /* Set erase shift */
191 bbm->bbt_erase_shift = this->erase_shift;
192
193 if (!bbm->isbad_bbt)
194 bbm->isbad_bbt = onenand_isbad_bbt;
195
196 /* Scan the device to build a memory based bad block table */
197 if ((ret = onenand_memory_bbt(mtd, bd))) {
198 printk(KERN_ERR "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
199 kfree(bbm->bbt);
200 bbm->bbt = NULL;
201 }
202
203 return ret;
204}
205
206/*
207 * Define some generic bad / good block scan pattern which are used
208 * while scanning a device for factory marked good / bad blocks.
209 */
210static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
211
212static struct nand_bbt_descr largepage_memorybased = {
213 .options = 0,
214 .offs = 0,
215 .len = 2,
216 .pattern = scan_ff_pattern,
217};
218
219/**
220 * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
221 * @param mtd MTD device structure
222 *
223 * This function selects the default bad block table
224 * support for the device and calls the onenand_scan_bbt function
225 */
226int onenand_default_bbt(struct mtd_info *mtd)
227{
228 struct onenand_chip *this = mtd->priv;
229 struct bbm_info *bbm;
230
Burman Yan95b93a02006-11-15 21:10:29 +0200231 this->bbm = kzalloc(sizeof(struct bbm_info), GFP_KERNEL);
Kyungmin Park87590e22005-09-27 11:26:39 +0100232 if (!this->bbm)
233 return -ENOMEM;
234
235 bbm = this->bbm;
236
Kyungmin Park87590e22005-09-27 11:26:39 +0100237 /* 1KB page has same configuration as 2KB page */
238 if (!bbm->badblock_pattern)
239 bbm->badblock_pattern = &largepage_memorybased;
240
241 return onenand_scan_bbt(mtd, bbm->badblock_pattern);
242}
243
244EXPORT_SYMBOL(onenand_scan_bbt);
245EXPORT_SYMBOL(onenand_default_bbt);