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