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