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