Skip to content
Newer
Older
100644 535 lines (503 sloc) 15.8 KB
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
1 /*
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
2 * This file contains an ECC algorithm that detects and corrects 1 bit
3 * errors in a 256 byte block of data.
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
4 *
5 * drivers/mtd/nand/nand_ecc.c
6 *
ccbcd6c @dwmw2 [MTD] [NAND] Minor cleanup of nand_ecc.c
dwmw2 authored Aug 16, 2008
7 * Copyright © 2008 Koninklijke Philips Electronics NV.
8 * Author: Frans Meulenbroeks
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
9 *
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
10 * Completely replaces the previous ECC implementation which was written by:
11 * Steven J. Hill (sjhill@realitydiluted.com)
12 * Thomas Gleixner (tglx@linutronix.de)
13 *
14 * Information on how this algorithm works and how it was developed
ccbcd6c @dwmw2 [MTD] [NAND] Minor cleanup of nand_ecc.c
dwmw2 authored Aug 16, 2008
15 * can be found in Documentation/mtd/nand_ecc.txt
819d6a3 [MTD] Improve software ECC calculation
Thomas Gleixner authored May 23, 2006
16 *
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
17 * This file is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 2 or (at your option) any
20 * later version.
61b03bd [MTD] NAND: Clean up trailing white spaces
Thomas Gleixner authored Nov 7, 2005
21 *
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
22 * This file is distributed in the hope that it will be useful, but WITHOUT
23 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * for more details.
61b03bd [MTD] NAND: Clean up trailing white spaces
Thomas Gleixner authored Nov 7, 2005
26 *
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
27 * You should have received a copy of the GNU General Public License along
28 * with this file; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
61b03bd [MTD] NAND: Clean up trailing white spaces
Thomas Gleixner authored Nov 7, 2005
30 *
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
31 */
32
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
33 /*
34 * The STANDALONE macro is useful when running the code outside the kernel
35 * e.g. when running the code in a testbed or a benchmark program.
36 * When STANDALONE is used, the module related macros are commented out
37 * as well as the linux include files.
ccbcd6c @dwmw2 [MTD] [NAND] Minor cleanup of nand_ecc.c
dwmw2 authored Aug 16, 2008
38 * Instead a private definition of mtd_info is given to satisfy the compiler
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
39 * (the code does not use mtd_info, so the code does not care)
40 */
41 #ifndef STANDALONE
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/module.h>
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
45 #include <linux/mtd/mtd.h>
46 #include <linux/mtd/nand.h>
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
47 #include <linux/mtd/nand_ecc.h>
1077be5 [MTD] [NAND] nand_ecc.c: fix big endian, strengthen test, add printk
frans authored Aug 20, 2008
48 #include <asm/byteorder.h>
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
49 #else
ccbcd6c @dwmw2 [MTD] [NAND] Minor cleanup of nand_ecc.c
dwmw2 authored Aug 16, 2008
50 #include <stdint.h>
51 struct mtd_info;
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
52 #define EXPORT_SYMBOL(x) /* x */
53
54 #define MODULE_LICENSE(x) /* x */
55 #define MODULE_AUTHOR(x) /* x */
56 #define MODULE_DESCRIPTION(x) /* x */
1077be5 [MTD] [NAND] nand_ecc.c: fix big endian, strengthen test, add printk
frans authored Aug 20, 2008
57
58 #define printk printf
59 #define KERN_ERR ""
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
60 #endif
61
62 /*
63 * invparity is a 256 byte table that contains the odd parity
64 * for each byte. So if the number of bits in a byte is even,
65 * the array element is 1, and when the number of bits is odd
66 * the array eleemnt is 0.
67 */
68 static const char invparity[256] = {
69 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
70 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
71 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
72 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
73 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
74 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
75 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
76 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
77 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
78 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
79 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
80 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
81 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
82 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
83 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
84 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
85 };
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
86
87 /*
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
88 * bitsperbyte contains the number of bits per byte
89 * this is only used for testing and repairing parity
90 * (a precalculated value slightly improves performance)
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
91 */
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
92 static const char bitsperbyte[256] = {
93 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
94 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
95 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
96 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
97 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
98 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
99 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
100 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
101 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
102 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
103 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
104 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
105 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
106 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
107 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
108 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
109 };
110
111 /*
112 * addressbits is a lookup table to filter out the bits from the xor-ed
113 * ecc data that identify the faulty location.
114 * this is only used for repairing parity
115 * see the comments in nand_correct_data for more details
116 */
117 static const char addressbits[256] = {
118 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
119 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
120 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
121 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
122 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
123 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
124 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
125 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
126 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
127 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
128 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
129 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
130 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
131 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
132 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
133 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
134 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
135 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
136 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
137 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
138 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
139 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
140 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
141 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
142 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
143 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
144 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
145 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
146 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
147 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
148 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
149 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
150 };
151
152 /**
1c63aca @mita mtd: Add __nand_calculate_ecc() to NAND ECC functions
mita authored Oct 22, 2009
153 * __nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256/512-byte
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
154 * block
17c1d2b [MTD] [NAND] Fix missing kernel-doc
Alexey Korolev authored Aug 20, 2008
155 * @buf: input buffer with raw data
1c63aca @mita mtd: Add __nand_calculate_ecc() to NAND ECC functions
mita authored Oct 22, 2009
156 * @eccsize: data bytes per ecc step (256 or 512)
17c1d2b [MTD] [NAND] Fix missing kernel-doc
Alexey Korolev authored Aug 20, 2008
157 * @code: output buffer with ECC
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
158 */
1c63aca @mita mtd: Add __nand_calculate_ecc() to NAND ECC functions
mita authored Oct 22, 2009
159 void __nand_calculate_ecc(const unsigned char *buf, unsigned int eccsize,
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
160 unsigned char *code)
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
161 {
819d6a3 [MTD] Improve software ECC calculation
Thomas Gleixner authored May 23, 2006
162 int i;
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
163 const uint32_t *bp = (uint32_t *)buf;
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
164 /* 256 or 512 bytes/ecc */
1c63aca @mita mtd: Add __nand_calculate_ecc() to NAND ECC functions
mita authored Oct 22, 2009
165 const uint32_t eccsize_mult = eccsize >> 8;
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
166 uint32_t cur; /* current value in buffer */
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
167 /* rp0..rp15..rp17 are the various accumulated parities (per byte) */
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
168 uint32_t rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7;
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
169 uint32_t rp8, rp9, rp10, rp11, rp12, rp13, rp14, rp15, rp16;
170 uint32_t uninitialized_var(rp17); /* to make compiler happy */
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
171 uint32_t par; /* the cumulative parity for all data */
172 uint32_t tmppar; /* the cumulative parity for this iteration;
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
173 for rp12, rp14 and rp16 at the end of the
174 loop */
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
175
176 par = 0;
177 rp4 = 0;
178 rp6 = 0;
179 rp8 = 0;
180 rp10 = 0;
181 rp12 = 0;
182 rp14 = 0;
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
183 rp16 = 0;
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
184
185 /*
186 * The loop is unrolled a number of times;
187 * This avoids if statements to decide on which rp value to update
188 * Also we process the data by longwords.
189 * Note: passing unaligned data might give a performance penalty.
190 * It is assumed that the buffers are aligned.
191 * tmppar is the cumulative sum of this iteration.
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
192 * needed for calculating rp12, rp14, rp16 and par
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
193 * also used as a performance improvement for rp6, rp8 and rp10
194 */
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
195 for (i = 0; i < eccsize_mult << 2; i++) {
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
196 cur = *bp++;
197 tmppar = cur;
198 rp4 ^= cur;
199 cur = *bp++;
200 tmppar ^= cur;
201 rp6 ^= tmppar;
202 cur = *bp++;
203 tmppar ^= cur;
204 rp4 ^= cur;
205 cur = *bp++;
206 tmppar ^= cur;
207 rp8 ^= tmppar;
61b03bd [MTD] NAND: Clean up trailing white spaces
Thomas Gleixner authored Nov 7, 2005
208
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
209 cur = *bp++;
210 tmppar ^= cur;
211 rp4 ^= cur;
212 rp6 ^= cur;
213 cur = *bp++;
214 tmppar ^= cur;
215 rp6 ^= cur;
216 cur = *bp++;
217 tmppar ^= cur;
218 rp4 ^= cur;
219 cur = *bp++;
220 tmppar ^= cur;
221 rp10 ^= tmppar;
61b03bd [MTD] NAND: Clean up trailing white spaces
Thomas Gleixner authored Nov 7, 2005
222
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
223 cur = *bp++;
224 tmppar ^= cur;
225 rp4 ^= cur;
226 rp6 ^= cur;
227 rp8 ^= cur;
228 cur = *bp++;
229 tmppar ^= cur;
230 rp6 ^= cur;
231 rp8 ^= cur;
232 cur = *bp++;
233 tmppar ^= cur;
234 rp4 ^= cur;
235 rp8 ^= cur;
236 cur = *bp++;
237 tmppar ^= cur;
238 rp8 ^= cur;
61b03bd [MTD] NAND: Clean up trailing white spaces
Thomas Gleixner authored Nov 7, 2005
239
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
240 cur = *bp++;
241 tmppar ^= cur;
242 rp4 ^= cur;
243 rp6 ^= cur;
244 cur = *bp++;
245 tmppar ^= cur;
246 rp6 ^= cur;
247 cur = *bp++;
248 tmppar ^= cur;
249 rp4 ^= cur;
250 cur = *bp++;
251 tmppar ^= cur;
252
253 par ^= tmppar;
254 if ((i & 0x1) == 0)
255 rp12 ^= tmppar;
256 if ((i & 0x2) == 0)
257 rp14 ^= tmppar;
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
258 if (eccsize_mult == 2 && (i & 0x4) == 0)
259 rp16 ^= tmppar;
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
260 }
61b03bd [MTD] NAND: Clean up trailing white spaces
Thomas Gleixner authored Nov 7, 2005
261
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
262 /*
263 * handle the fact that we use longword operations
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
264 * we'll bring rp4..rp14..rp16 back to single byte entities by
265 * shifting and xoring first fold the upper and lower 16 bits,
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
266 * then the upper and lower 8 bits.
267 */
268 rp4 ^= (rp4 >> 16);
269 rp4 ^= (rp4 >> 8);
270 rp4 &= 0xff;
271 rp6 ^= (rp6 >> 16);
272 rp6 ^= (rp6 >> 8);
273 rp6 &= 0xff;
274 rp8 ^= (rp8 >> 16);
275 rp8 ^= (rp8 >> 8);
276 rp8 &= 0xff;
277 rp10 ^= (rp10 >> 16);
278 rp10 ^= (rp10 >> 8);
279 rp10 &= 0xff;
280 rp12 ^= (rp12 >> 16);
281 rp12 ^= (rp12 >> 8);
282 rp12 &= 0xff;
283 rp14 ^= (rp14 >> 16);
284 rp14 ^= (rp14 >> 8);
285 rp14 &= 0xff;
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
286 if (eccsize_mult == 2) {
287 rp16 ^= (rp16 >> 16);
288 rp16 ^= (rp16 >> 8);
289 rp16 &= 0xff;
290 }
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
291
292 /*
293 * we also need to calculate the row parity for rp0..rp3
294 * This is present in par, because par is now
1077be5 [MTD] [NAND] nand_ecc.c: fix big endian, strengthen test, add printk
frans authored Aug 20, 2008
295 * rp3 rp3 rp2 rp2 in little endian and
296 * rp2 rp2 rp3 rp3 in big endian
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
297 * as well as
1077be5 [MTD] [NAND] nand_ecc.c: fix big endian, strengthen test, add printk
frans authored Aug 20, 2008
298 * rp1 rp0 rp1 rp0 in little endian and
299 * rp0 rp1 rp0 rp1 in big endian
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
300 * First calculate rp2 and rp3
301 */
1077be5 [MTD] [NAND] nand_ecc.c: fix big endian, strengthen test, add printk
frans authored Aug 20, 2008
302 #ifdef __BIG_ENDIAN
303 rp2 = (par >> 16);
304 rp2 ^= (rp2 >> 8);
305 rp2 &= 0xff;
306 rp3 = par & 0xffff;
307 rp3 ^= (rp3 >> 8);
308 rp3 &= 0xff;
309 #else
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
310 rp3 = (par >> 16);
311 rp3 ^= (rp3 >> 8);
312 rp3 &= 0xff;
313 rp2 = par & 0xffff;
314 rp2 ^= (rp2 >> 8);
315 rp2 &= 0xff;
1077be5 [MTD] [NAND] nand_ecc.c: fix big endian, strengthen test, add printk
frans authored Aug 20, 2008
316 #endif
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
317
318 /* reduce par to 16 bits then calculate rp1 and rp0 */
319 par ^= (par >> 16);
1077be5 [MTD] [NAND] nand_ecc.c: fix big endian, strengthen test, add printk
frans authored Aug 20, 2008
320 #ifdef __BIG_ENDIAN
321 rp0 = (par >> 8) & 0xff;
322 rp1 = (par & 0xff);
323 #else
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
324 rp1 = (par >> 8) & 0xff;
325 rp0 = (par & 0xff);
1077be5 [MTD] [NAND] nand_ecc.c: fix big endian, strengthen test, add printk
frans authored Aug 20, 2008
326 #endif
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
327
328 /* finally reduce par to 8 bits */
329 par ^= (par >> 8);
330 par &= 0xff;
331
332 /*
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
333 * and calculate rp5..rp15..rp17
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
334 * note that par = rp4 ^ rp5 and due to the commutative property
335 * of the ^ operator we can say:
336 * rp5 = (par ^ rp4);
337 * The & 0xff seems superfluous, but benchmarking learned that
338 * leaving it out gives slightly worse results. No idea why, probably
339 * it has to do with the way the pipeline in pentium is organized.
340 */
341 rp5 = (par ^ rp4) & 0xff;
342 rp7 = (par ^ rp6) & 0xff;
343 rp9 = (par ^ rp8) & 0xff;
344 rp11 = (par ^ rp10) & 0xff;
345 rp13 = (par ^ rp12) & 0xff;
346 rp15 = (par ^ rp14) & 0xff;
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
347 if (eccsize_mult == 2)
348 rp17 = (par ^ rp16) & 0xff;
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
349
350 /*
351 * Finally calculate the ecc bits.
352 * Again here it might seem that there are performance optimisations
353 * possible, but benchmarks showed that on the system this is developed
354 * the code below is the fastest
355 */
fc02919 [MTD] [NAND] fix ifdef option in nand_ecc.c
Timo Lindhorst authored Nov 27, 2006
356 #ifdef CONFIG_MTD_NAND_ECC_SMC
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
357 code[0] =
358 (invparity[rp7] << 7) |
359 (invparity[rp6] << 6) |
360 (invparity[rp5] << 5) |
361 (invparity[rp4] << 4) |
362 (invparity[rp3] << 3) |
363 (invparity[rp2] << 2) |
364 (invparity[rp1] << 1) |
365 (invparity[rp0]);
366 code[1] =
367 (invparity[rp15] << 7) |
368 (invparity[rp14] << 6) |
369 (invparity[rp13] << 5) |
370 (invparity[rp12] << 4) |
371 (invparity[rp11] << 3) |
372 (invparity[rp10] << 2) |
373 (invparity[rp9] << 1) |
374 (invparity[rp8]);
819d6a3 [MTD] Improve software ECC calculation
Thomas Gleixner authored May 23, 2006
375 #else
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
376 code[1] =
377 (invparity[rp7] << 7) |
378 (invparity[rp6] << 6) |
379 (invparity[rp5] << 5) |
380 (invparity[rp4] << 4) |
381 (invparity[rp3] << 3) |
382 (invparity[rp2] << 2) |
383 (invparity[rp1] << 1) |
384 (invparity[rp0]);
385 code[0] =
386 (invparity[rp15] << 7) |
387 (invparity[rp14] << 6) |
388 (invparity[rp13] << 5) |
389 (invparity[rp12] << 4) |
390 (invparity[rp11] << 3) |
391 (invparity[rp10] << 2) |
392 (invparity[rp9] << 1) |
393 (invparity[rp8]);
819d6a3 [MTD] Improve software ECC calculation
Thomas Gleixner authored May 23, 2006
394 #endif
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
395 if (eccsize_mult == 1)
396 code[2] =
397 (invparity[par & 0xf0] << 7) |
398 (invparity[par & 0x0f] << 6) |
399 (invparity[par & 0xcc] << 5) |
400 (invparity[par & 0x33] << 4) |
401 (invparity[par & 0xaa] << 3) |
402 (invparity[par & 0x55] << 2) |
403 3;
404 else
405 code[2] =
406 (invparity[par & 0xf0] << 7) |
407 (invparity[par & 0x0f] << 6) |
408 (invparity[par & 0xcc] << 5) |
409 (invparity[par & 0x33] << 4) |
410 (invparity[par & 0xaa] << 3) |
411 (invparity[par & 0x55] << 2) |
412 (invparity[rp17] << 1) |
413 (invparity[rp16] << 0);
1c63aca @mita mtd: Add __nand_calculate_ecc() to NAND ECC functions
mita authored Oct 22, 2009
414 }
415 EXPORT_SYMBOL(__nand_calculate_ecc);
416
417 /**
418 * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256/512-byte
419 * block
420 * @mtd: MTD block structure
421 * @buf: input buffer with raw data
422 * @code: output buffer with ECC
423 */
424 int nand_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
425 unsigned char *code)
426 {
427 __nand_calculate_ecc(buf,
428 ((struct nand_chip *)mtd->priv)->ecc.size, code);
429
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
430 return 0;
431 }
819d6a3 [MTD] Improve software ECC calculation
Thomas Gleixner authored May 23, 2006
432 EXPORT_SYMBOL(nand_calculate_ecc);
433
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
434 /**
be2f092 mtd: nand: add __nand_correct_data helper function
Atsushi Nemoto authored Sep 5, 2009
435 * __nand_correct_data - [NAND Interface] Detect and correct bit error(s)
17c1d2b [MTD] [NAND] Fix missing kernel-doc
Alexey Korolev authored Aug 20, 2008
436 * @buf: raw data read from the chip
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
437 * @read_ecc: ECC from the chip
438 * @calc_ecc: the ECC calculated from raw data
be2f092 mtd: nand: add __nand_correct_data helper function
Atsushi Nemoto authored Sep 4, 2009
439 * @eccsize: data bytes per ecc step (256 or 512)
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
440 *
be2f092 mtd: nand: add __nand_correct_data helper function
Atsushi Nemoto authored Sep 4, 2009
441 * Detect and correct a 1 bit error for eccsize byte block
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
442 */
be2f092 mtd: nand: add __nand_correct_data helper function
Atsushi Nemoto authored Sep 4, 2009
443 int __nand_correct_data(unsigned char *buf,
444 unsigned char *read_ecc, unsigned char *calc_ecc,
445 unsigned int eccsize)
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
446 {
260dc00 mtd: nand: fix 512 byte software ecc support
Vimal Singh authored Feb 23, 2009
447 unsigned char b0, b1, b2, bit_addr;
448 unsigned int byte_addr;
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
449 /* 256 or 512 bytes/ecc */
be2f092 mtd: nand: add __nand_correct_data helper function
Atsushi Nemoto authored Sep 4, 2009
450 const uint32_t eccsize_mult = eccsize >> 8;
819d6a3 [MTD] Improve software ECC calculation
Thomas Gleixner authored May 23, 2006
451
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
452 /*
453 * b0 to b2 indicate which bit is faulty (if any)
454 * we might need the xor result more than once,
455 * so keep them in a local var
456 */
fc02919 [MTD] [NAND] fix ifdef option in nand_ecc.c
Timo Lindhorst authored Nov 27, 2006
457 #ifdef CONFIG_MTD_NAND_ECC_SMC
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
458 b0 = read_ecc[0] ^ calc_ecc[0];
459 b1 = read_ecc[1] ^ calc_ecc[1];
819d6a3 [MTD] Improve software ECC calculation
Thomas Gleixner authored May 23, 2006
460 #else
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
461 b0 = read_ecc[1] ^ calc_ecc[1];
462 b1 = read_ecc[0] ^ calc_ecc[0];
819d6a3 [MTD] Improve software ECC calculation
Thomas Gleixner authored May 23, 2006
463 #endif
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
464 b2 = read_ecc[2] ^ calc_ecc[2];
819d6a3 [MTD] Improve software ECC calculation
Thomas Gleixner authored May 23, 2006
465
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
466 /* check if there are any bitfaults */
819d6a3 [MTD] Improve software ECC calculation
Thomas Gleixner authored May 23, 2006
467
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
468 /* repeated if statements are slightly more efficient than switch ... */
469 /* ordered in order of likelihood */
1077be5 [MTD] [NAND] nand_ecc.c: fix big endian, strengthen test, add printk
frans authored Aug 20, 2008
470
471 if ((b0 | b1 | b2) == 0)
ccbcd6c @dwmw2 [MTD] [NAND] Minor cleanup of nand_ecc.c
dwmw2 authored Aug 16, 2008
472 return 0; /* no error */
1077be5 [MTD] [NAND] nand_ecc.c: fix big endian, strengthen test, add printk
frans authored Aug 20, 2008
473
474 if ((((b0 ^ (b0 >> 1)) & 0x55) == 0x55) &&
475 (((b1 ^ (b1 >> 1)) & 0x55) == 0x55) &&
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
476 ((eccsize_mult == 1 && ((b2 ^ (b2 >> 1)) & 0x54) == 0x54) ||
477 (eccsize_mult == 2 && ((b2 ^ (b2 >> 1)) & 0x55) == 0x55))) {
478 /* single bit error */
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
479 /*
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
480 * rp17/rp15/13/11/9/7/5/3/1 indicate which byte is the faulty
481 * byte, cp 5/3/1 indicate the faulty bit.
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
482 * A lookup table (called addressbits) is used to filter
483 * the bits from the byte they are in.
484 * A marginal optimisation is possible by having three
485 * different lookup tables.
486 * One as we have now (for b0), one for b2
487 * (that would avoid the >> 1), and one for b1 (with all values
488 * << 4). However it was felt that introducing two more tables
489 * hardly justify the gain.
490 *
491 * The b2 shift is there to get rid of the lowest two bits.
492 * We could also do addressbits[b2] >> 1 but for the
af901ca @andre-rosa tree-wide: fix assorted typos all over the place
andre-rosa authored Nov 14, 2009
493 * performance it does not make any difference
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
494 */
d68156c [MTD] [NAND] nand_ecc.c: adding support for 512 byte ecc
Singh, Vimal authored Aug 23, 2008
495 if (eccsize_mult == 1)
496 byte_addr = (addressbits[b1] << 4) + addressbits[b0];
497 else
498 byte_addr = (addressbits[b2 & 0x3] << 8) +
499 (addressbits[b1] << 4) + addressbits[b0];
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
500 bit_addr = addressbits[b2 >> 2];
501 /* flip the bit */
502 buf[byte_addr] ^= (1 << bit_addr);
ccbcd6c @dwmw2 [MTD] [NAND] Minor cleanup of nand_ecc.c
dwmw2 authored Aug 16, 2008
503 return 1;
1077be5 [MTD] [NAND] nand_ecc.c: fix big endian, strengthen test, add printk
frans authored Aug 20, 2008
504
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
505 }
1077be5 [MTD] [NAND] nand_ecc.c: fix big endian, strengthen test, add printk
frans authored Aug 20, 2008
506 /* count nr of bits; use table lookup, faster than calculating it */
507 if ((bitsperbyte[b0] + bitsperbyte[b1] + bitsperbyte[b2]) == 1)
ccbcd6c @dwmw2 [MTD] [NAND] Minor cleanup of nand_ecc.c
dwmw2 authored Aug 16, 2008
508 return 1; /* error in ecc data; no action needed */
1077be5 [MTD] [NAND] nand_ecc.c: fix big endian, strengthen test, add printk
frans authored Aug 20, 2008
509
510 printk(KERN_ERR "uncorrectable error : ");
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
511 return -1;
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
512 }
be2f092 mtd: nand: add __nand_correct_data helper function
Atsushi Nemoto authored Sep 4, 2009
513 EXPORT_SYMBOL(__nand_correct_data);
514
515 /**
516 * nand_correct_data - [NAND Interface] Detect and correct bit error(s)
517 * @mtd: MTD block structure
518 * @buf: raw data read from the chip
519 * @read_ecc: ECC from the chip
520 * @calc_ecc: the ECC calculated from raw data
521 *
522 * Detect and correct a 1 bit error for 256/512 byte block
523 */
524 int nand_correct_data(struct mtd_info *mtd, unsigned char *buf,
525 unsigned char *read_ecc, unsigned char *calc_ecc)
526 {
527 return __nand_correct_data(buf, read_ecc, calc_ecc,
528 ((struct nand_chip *)mtd->priv)->ecc.size);
529 }
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
530 EXPORT_SYMBOL(nand_correct_data);
531
532 MODULE_LICENSE("GPL");
e6cf5df [MTD] [NAND] nand_ecc.c: rewrite for improved performance
frans authored Aug 15, 2008
533 MODULE_AUTHOR("Frans Meulenbroeks <fransmeulenbroeks@gmail.com>");
1da177e Linux-2.6.12-rc2
Linus Torvalds authored Apr 16, 2005
534 MODULE_DESCRIPTION("Generic NAND ECC support");
Something went wrong with that request. Please try again.