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