1
2
3
4
5
6
7
8
9
10#include <common.h>
11
12#include <linux/types.h>
13
14#include <linux/bitops.h>
15#include <linux/mtd/mtd.h>
16#include <linux/mtd/nand.h>
17#include <linux/mtd/nand_bch.h>
18#include <linux/bch.h>
19#include <malloc.h>
20
21
22
23
24
25
26
27
28struct nand_bch_control {
29 struct bch_control *bch;
30 struct nand_ecclayout ecclayout;
31 unsigned int *errloc;
32 unsigned char *eccmask;
33};
34
35
36
37
38
39
40
41int nand_bch_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
42 unsigned char *code)
43{
44 const struct nand_chip *chip = mtd->priv;
45 struct nand_bch_control *nbc = chip->ecc.priv;
46 unsigned int i;
47
48 memset(code, 0, chip->ecc.bytes);
49 encode_bch(nbc->bch, buf, chip->ecc.size, code);
50
51
52 for (i = 0; i < chip->ecc.bytes; i++)
53 code[i] ^= nbc->eccmask[i];
54
55 return 0;
56}
57
58
59
60
61
62
63
64
65
66
67int nand_bch_correct_data(struct mtd_info *mtd, unsigned char *buf,
68 unsigned char *read_ecc, unsigned char *calc_ecc)
69{
70 const struct nand_chip *chip = mtd->priv;
71 struct nand_bch_control *nbc = chip->ecc.priv;
72 unsigned int *errloc = nbc->errloc;
73 int i, count;
74
75 count = decode_bch(nbc->bch, NULL, chip->ecc.size, read_ecc, calc_ecc,
76 NULL, errloc);
77 if (count > 0) {
78 for (i = 0; i < count; i++) {
79 if (errloc[i] < (chip->ecc.size*8))
80
81 buf[errloc[i] >> 3] ^= (1 << (errloc[i] & 7));
82
83
84 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: corrected bitflip %u\n",
85 __func__, errloc[i]);
86 }
87 } else if (count < 0) {
88 printk(KERN_ERR "ecc unrecoverable error\n");
89 count = -1;
90 }
91 return count;
92}
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113struct nand_bch_control *
114nand_bch_init(struct mtd_info *mtd, unsigned int eccsize, unsigned int eccbytes,
115 struct nand_ecclayout **ecclayout)
116{
117 unsigned int m, t, eccsteps, i;
118 struct nand_ecclayout *layout;
119 struct nand_bch_control *nbc = NULL;
120 unsigned char *erased_page;
121
122 if (!eccsize || !eccbytes) {
123 printk(KERN_WARNING "ecc parameters not supplied\n");
124 goto fail;
125 }
126
127 m = fls(1+8*eccsize);
128 t = (eccbytes*8)/m;
129
130 nbc = kzalloc(sizeof(*nbc), GFP_KERNEL);
131 if (!nbc)
132 goto fail;
133
134 nbc->bch = init_bch(m, t, 0);
135 if (!nbc->bch)
136 goto fail;
137
138
139 if (nbc->bch->ecc_bytes != eccbytes) {
140 printk(KERN_WARNING "invalid eccbytes %u, should be %u\n",
141 eccbytes, nbc->bch->ecc_bytes);
142 goto fail;
143 }
144
145 eccsteps = mtd->writesize/eccsize;
146
147
148 if (!*ecclayout) {
149
150
151 if (mtd->oobsize < 64) {
152 printk(KERN_WARNING "must provide an oob scheme for "
153 "oobsize %d\n", mtd->oobsize);
154 goto fail;
155 }
156
157 layout = &nbc->ecclayout;
158 layout->eccbytes = eccsteps*eccbytes;
159
160
161 if (layout->eccbytes+2 > mtd->oobsize) {
162 printk(KERN_WARNING "no suitable oob scheme available "
163 "for oobsize %d eccbytes %u\n", mtd->oobsize,
164 eccbytes);
165 goto fail;
166 }
167
168 for (i = 0; i < layout->eccbytes; i++)
169 layout->eccpos[i] = mtd->oobsize-layout->eccbytes+i;
170
171 layout->oobfree[0].offset = 2;
172 layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
173
174 *ecclayout = layout;
175 }
176
177
178 if (8*(eccsize+eccbytes) >= (1 << m)) {
179 printk(KERN_WARNING "eccsize %u is too large\n", eccsize);
180 goto fail;
181 }
182 if ((*ecclayout)->eccbytes != (eccsteps*eccbytes)) {
183 printk(KERN_WARNING "invalid ecc layout\n");
184 goto fail;
185 }
186
187 nbc->eccmask = kmalloc(eccbytes, GFP_KERNEL);
188 nbc->errloc = kmalloc(t*sizeof(*nbc->errloc), GFP_KERNEL);
189 if (!nbc->eccmask || !nbc->errloc)
190 goto fail;
191
192
193
194 erased_page = kmalloc(eccsize, GFP_KERNEL);
195 if (!erased_page)
196 goto fail;
197
198 memset(erased_page, 0xff, eccsize);
199 memset(nbc->eccmask, 0, eccbytes);
200 encode_bch(nbc->bch, erased_page, eccsize, nbc->eccmask);
201 kfree(erased_page);
202
203 for (i = 0; i < eccbytes; i++)
204 nbc->eccmask[i] ^= 0xff;
205
206 return nbc;
207fail:
208 nand_bch_free(nbc);
209 return NULL;
210}
211
212
213
214
215
216void nand_bch_free(struct nand_bch_control *nbc)
217{
218 if (nbc) {
219 free_bch(nbc->bch);
220 kfree(nbc->errloc);
221 kfree(nbc->eccmask);
222 kfree(nbc);
223 }
224}
225