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