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