1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <common.h>
18#include <linux/compat.h>
19#include <linux/mtd/mtd.h>
20#include <linux/mtd/onenand.h>
21#include <malloc.h>
22
23#include <linux/errno.h>
24
25
26
27
28
29
30
31
32
33
34
35
36
37static int check_short_pattern(uint8_t * buf, int len, int paglen,
38 struct nand_bbt_descr *td)
39{
40 int i;
41 uint8_t *p = buf;
42
43
44 for (i = 0; i < td->len; i++) {
45 if (p[i] != td->pattern[i])
46 return -1;
47 }
48 return 0;
49}
50
51
52
53
54
55
56
57
58
59
60
61
62static int create_bbt(struct mtd_info *mtd, uint8_t * buf,
63 struct nand_bbt_descr *bd, int chip)
64{
65 struct onenand_chip *this = mtd->priv;
66 struct bbm_info *bbm = this->bbm;
67 int i, j, numblocks, len, scanlen;
68 int startblock;
69 loff_t from;
70 size_t readlen, ooblen;
71 struct mtd_oob_ops ops;
72 int rgn;
73
74 printk(KERN_INFO "Scanning device for bad blocks\n");
75
76 len = 1;
77
78
79 scanlen = ooblen = 0;
80 readlen = bd->len;
81
82
83
84
85
86 numblocks = this->chipsize >> (bbm->bbt_erase_shift - 1);
87 startblock = 0;
88 from = 0;
89
90 ops.mode = MTD_OPS_PLACE_OOB;
91 ops.ooblen = readlen;
92 ops.oobbuf = buf;
93 ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
94
95 for (i = startblock; i < numblocks;) {
96 int ret;
97
98 for (j = 0; j < len; j++) {
99
100
101 ret = onenand_bbt_read_oob(mtd,
102 from + j * mtd->writesize +
103 bd->offs, &ops);
104
105
106 if (ret == ONENAND_BBT_READ_FATAL_ERROR)
107 return -EIO;
108
109 if (ret || check_short_pattern
110 (&buf[j * scanlen], scanlen, mtd->writesize, bd)) {
111 bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
112 printk(KERN_WARNING
113 "Bad eraseblock %d at 0x%08x\n", i >> 1,
114 (unsigned int)from);
115 break;
116 }
117 }
118 i += 2;
119
120 if (FLEXONENAND(this)) {
121 rgn = flexonenand_region(mtd, from);
122 from += mtd->eraseregions[rgn].erasesize;
123 } else
124 from += (1 << bbm->bbt_erase_shift);
125 }
126
127 return 0;
128}
129
130
131
132
133
134
135
136
137
138static inline int onenand_memory_bbt(struct mtd_info *mtd,
139 struct nand_bbt_descr *bd)
140{
141 unsigned char data_buf[MAX_ONENAND_PAGESIZE];
142
143 return create_bbt(mtd, data_buf, bd, -1);
144}
145
146
147
148
149
150
151
152static int onenand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
153{
154 struct onenand_chip *this = mtd->priv;
155 struct bbm_info *bbm = this->bbm;
156 int block;
157 uint8_t res;
158
159
160 block = (int) (onenand_block(this, offs) << 1);
161 res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
162
163 pr_debug("onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
164 (unsigned int)offs, block >> 1, res);
165
166 switch ((int)res) {
167 case 0x00:
168 return 0;
169 case 0x01:
170 return 1;
171 case 0x02:
172 return allowbbt ? 0 : 1;
173 }
174
175 return 1;
176}
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
193{
194 struct onenand_chip *this = mtd->priv;
195 struct bbm_info *bbm = this->bbm;
196 int len, ret = 0;
197
198 len = this->chipsize >> (this->erase_shift + 2);
199
200 bbm->bbt = malloc(len);
201 if (!bbm->bbt)
202 return -ENOMEM;
203
204 memset(bbm->bbt, 0x00, len);
205
206
207 bbm->badblockpos = ONENAND_BADBLOCK_POS;
208
209
210 bbm->bbt_erase_shift = this->erase_shift;
211
212 if (!bbm->isbad_bbt)
213 bbm->isbad_bbt = onenand_isbad_bbt;
214
215
216 if ((ret = onenand_memory_bbt(mtd, bd))) {
217 printk(KERN_ERR
218 "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
219 free(bbm->bbt);
220 bbm->bbt = NULL;
221 }
222
223 return ret;
224}
225
226
227
228
229
230static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
231
232static struct nand_bbt_descr largepage_memorybased = {
233 .options = 0,
234 .offs = 0,
235 .len = 2,
236 .pattern = scan_ff_pattern,
237};
238
239
240
241
242
243
244
245
246int onenand_default_bbt(struct mtd_info *mtd)
247{
248 struct onenand_chip *this = mtd->priv;
249 struct bbm_info *bbm;
250
251 this->bbm = malloc(sizeof(struct bbm_info));
252 if (!this->bbm)
253 return -ENOMEM;
254
255 bbm = this->bbm;
256
257 memset(bbm, 0, sizeof(struct bbm_info));
258
259
260 if (!bbm->badblock_pattern)
261 bbm->badblock_pattern = &largepage_memorybased;
262
263 return onenand_scan_bbt(mtd, bbm->badblock_pattern);
264}
265