blob: 8f86eed40b70cdc660bcc6e4ef174f17772d2f87 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
franse6cf5df2008-08-15 23:14:31 +02002 * This file contains an ECC algorithm that detects and corrects 1 bit
3 * errors in a 256 byte block of data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
David Woodhouseccbcd6c2008-08-16 11:01:31 +01005 * Copyright © 2008 Koninklijke Philips Electronics NV.
6 * Author: Frans Meulenbroeks
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
franse6cf5df2008-08-15 23:14:31 +02008 * Completely replaces the previous ECC implementation which was written by:
9 * Steven J. Hill (sjhill@realitydiluted.com)
10 * Thomas Gleixner (tglx@linutronix.de)
11 *
12 * Information on how this algorithm works and how it was developed
David Woodhouseccbcd6c2008-08-16 11:01:31 +010013 * can be found in Documentation/mtd/nand_ecc.txt
Thomas Gleixner819d6a32006-05-23 11:32:45 +020014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * This file is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 or (at your option) any
18 * later version.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * This file is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 * for more details.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000024 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * You should have received a copy of the GNU General Public License along
26 * with this file; if not, write to the Free Software Foundation, Inc.,
27 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30
31#include <linux/types.h>
32#include <linux/kernel.h>
33#include <linux/module.h>
Singh, Vimald68156c2008-08-23 18:18:34 +020034#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020035#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/mtd/nand_ecc.h>
frans1077be52008-08-20 21:11:50 +020037#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
franse6cf5df2008-08-15 23:14:31 +020040 * invparity is a 256 byte table that contains the odd parity
41 * for each byte. So if the number of bits in a byte is even,
42 * the array element is 1, and when the number of bits is odd
43 * the array eleemnt is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
franse6cf5df2008-08-15 23:14:31 +020045static const char invparity[256] = {
46 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
47 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
48 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
49 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
50 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
51 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
52 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
53 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
54 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
55 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
56 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
57 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
58 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
59 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
60 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
61 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
62};
63
64/*
65 * bitsperbyte contains the number of bits per byte
66 * this is only used for testing and repairing parity
67 * (a precalculated value slightly improves performance)
68 */
69static const char bitsperbyte[256] = {
70 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
71 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
72 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
73 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
74 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
75 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
76 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
77 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
78 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
79 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
80 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
81 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
82 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
83 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
84 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
85 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
86};
87
88/*
89 * addressbits is a lookup table to filter out the bits from the xor-ed
Brian Norris7854d3f2011-06-23 14:12:08 -070090 * ECC data that identify the faulty location.
franse6cf5df2008-08-15 23:14:31 +020091 * this is only used for repairing parity
92 * see the comments in nand_correct_data for more details
93 */
94static const char addressbits[256] = {
95 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
96 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
97 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
98 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
99 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
100 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
101 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
102 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
103 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
104 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
105 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
106 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
107 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
108 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
109 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
110 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
111 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
112 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
113 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
114 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
115 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
116 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
117 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
118 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
119 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
120 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
121 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
122 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
123 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
124 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
125 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
126 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/**
Akinobu Mita1c63aca2009-10-22 16:53:32 +0900130 * __nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256/512-byte
Singh, Vimald68156c2008-08-23 18:18:34 +0200131 * block
Alexey Korolev17c1d2be2008-08-20 22:32:08 +0100132 * @buf: input buffer with raw data
Brian Norris7854d3f2011-06-23 14:12:08 -0700133 * @eccsize: data bytes per ECC step (256 or 512)
Alexey Korolev17c1d2be2008-08-20 22:32:08 +0100134 * @code: output buffer with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 */
Akinobu Mita1c63aca2009-10-22 16:53:32 +0900136void __nand_calculate_ecc(const unsigned char *buf, unsigned int eccsize,
franse6cf5df2008-08-15 23:14:31 +0200137 unsigned char *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200139 int i;
franse6cf5df2008-08-15 23:14:31 +0200140 const uint32_t *bp = (uint32_t *)buf;
Singh, Vimald68156c2008-08-23 18:18:34 +0200141 /* 256 or 512 bytes/ecc */
Akinobu Mita1c63aca2009-10-22 16:53:32 +0900142 const uint32_t eccsize_mult = eccsize >> 8;
franse6cf5df2008-08-15 23:14:31 +0200143 uint32_t cur; /* current value in buffer */
Singh, Vimald68156c2008-08-23 18:18:34 +0200144 /* rp0..rp15..rp17 are the various accumulated parities (per byte) */
franse6cf5df2008-08-15 23:14:31 +0200145 uint32_t rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7;
Singh, Vimald68156c2008-08-23 18:18:34 +0200146 uint32_t rp8, rp9, rp10, rp11, rp12, rp13, rp14, rp15, rp16;
147 uint32_t uninitialized_var(rp17); /* to make compiler happy */
franse6cf5df2008-08-15 23:14:31 +0200148 uint32_t par; /* the cumulative parity for all data */
149 uint32_t tmppar; /* the cumulative parity for this iteration;
Singh, Vimald68156c2008-08-23 18:18:34 +0200150 for rp12, rp14 and rp16 at the end of the
151 loop */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000152
franse6cf5df2008-08-15 23:14:31 +0200153 par = 0;
154 rp4 = 0;
155 rp6 = 0;
156 rp8 = 0;
157 rp10 = 0;
158 rp12 = 0;
159 rp14 = 0;
Singh, Vimald68156c2008-08-23 18:18:34 +0200160 rp16 = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000161
franse6cf5df2008-08-15 23:14:31 +0200162 /*
163 * The loop is unrolled a number of times;
164 * This avoids if statements to decide on which rp value to update
165 * Also we process the data by longwords.
166 * Note: passing unaligned data might give a performance penalty.
167 * It is assumed that the buffers are aligned.
168 * tmppar is the cumulative sum of this iteration.
Singh, Vimald68156c2008-08-23 18:18:34 +0200169 * needed for calculating rp12, rp14, rp16 and par
franse6cf5df2008-08-15 23:14:31 +0200170 * also used as a performance improvement for rp6, rp8 and rp10
171 */
Singh, Vimald68156c2008-08-23 18:18:34 +0200172 for (i = 0; i < eccsize_mult << 2; i++) {
franse6cf5df2008-08-15 23:14:31 +0200173 cur = *bp++;
174 tmppar = cur;
175 rp4 ^= cur;
176 cur = *bp++;
177 tmppar ^= cur;
178 rp6 ^= tmppar;
179 cur = *bp++;
180 tmppar ^= cur;
181 rp4 ^= cur;
182 cur = *bp++;
183 tmppar ^= cur;
184 rp8 ^= tmppar;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000185
franse6cf5df2008-08-15 23:14:31 +0200186 cur = *bp++;
187 tmppar ^= cur;
188 rp4 ^= cur;
189 rp6 ^= cur;
190 cur = *bp++;
191 tmppar ^= cur;
192 rp6 ^= cur;
193 cur = *bp++;
194 tmppar ^= cur;
195 rp4 ^= cur;
196 cur = *bp++;
197 tmppar ^= cur;
198 rp10 ^= tmppar;
199
200 cur = *bp++;
201 tmppar ^= cur;
202 rp4 ^= cur;
203 rp6 ^= cur;
204 rp8 ^= cur;
205 cur = *bp++;
206 tmppar ^= cur;
207 rp6 ^= cur;
208 rp8 ^= cur;
209 cur = *bp++;
210 tmppar ^= cur;
211 rp4 ^= cur;
212 rp8 ^= cur;
213 cur = *bp++;
214 tmppar ^= cur;
215 rp8 ^= cur;
216
217 cur = *bp++;
218 tmppar ^= cur;
219 rp4 ^= cur;
220 rp6 ^= cur;
221 cur = *bp++;
222 tmppar ^= cur;
223 rp6 ^= cur;
224 cur = *bp++;
225 tmppar ^= cur;
226 rp4 ^= cur;
227 cur = *bp++;
228 tmppar ^= cur;
229
230 par ^= tmppar;
231 if ((i & 0x1) == 0)
232 rp12 ^= tmppar;
233 if ((i & 0x2) == 0)
234 rp14 ^= tmppar;
Singh, Vimald68156c2008-08-23 18:18:34 +0200235 if (eccsize_mult == 2 && (i & 0x4) == 0)
236 rp16 ^= tmppar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000238
franse6cf5df2008-08-15 23:14:31 +0200239 /*
240 * handle the fact that we use longword operations
Singh, Vimald68156c2008-08-23 18:18:34 +0200241 * we'll bring rp4..rp14..rp16 back to single byte entities by
242 * shifting and xoring first fold the upper and lower 16 bits,
franse6cf5df2008-08-15 23:14:31 +0200243 * then the upper and lower 8 bits.
244 */
245 rp4 ^= (rp4 >> 16);
246 rp4 ^= (rp4 >> 8);
247 rp4 &= 0xff;
248 rp6 ^= (rp6 >> 16);
249 rp6 ^= (rp6 >> 8);
250 rp6 &= 0xff;
251 rp8 ^= (rp8 >> 16);
252 rp8 ^= (rp8 >> 8);
253 rp8 &= 0xff;
254 rp10 ^= (rp10 >> 16);
255 rp10 ^= (rp10 >> 8);
256 rp10 &= 0xff;
257 rp12 ^= (rp12 >> 16);
258 rp12 ^= (rp12 >> 8);
259 rp12 &= 0xff;
260 rp14 ^= (rp14 >> 16);
261 rp14 ^= (rp14 >> 8);
262 rp14 &= 0xff;
Singh, Vimald68156c2008-08-23 18:18:34 +0200263 if (eccsize_mult == 2) {
264 rp16 ^= (rp16 >> 16);
265 rp16 ^= (rp16 >> 8);
266 rp16 &= 0xff;
267 }
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200268
franse6cf5df2008-08-15 23:14:31 +0200269 /*
270 * we also need to calculate the row parity for rp0..rp3
271 * This is present in par, because par is now
frans1077be52008-08-20 21:11:50 +0200272 * rp3 rp3 rp2 rp2 in little endian and
273 * rp2 rp2 rp3 rp3 in big endian
franse6cf5df2008-08-15 23:14:31 +0200274 * as well as
frans1077be52008-08-20 21:11:50 +0200275 * rp1 rp0 rp1 rp0 in little endian and
276 * rp0 rp1 rp0 rp1 in big endian
franse6cf5df2008-08-15 23:14:31 +0200277 * First calculate rp2 and rp3
franse6cf5df2008-08-15 23:14:31 +0200278 */
frans1077be52008-08-20 21:11:50 +0200279#ifdef __BIG_ENDIAN
280 rp2 = (par >> 16);
281 rp2 ^= (rp2 >> 8);
282 rp2 &= 0xff;
283 rp3 = par & 0xffff;
284 rp3 ^= (rp3 >> 8);
285 rp3 &= 0xff;
286#else
franse6cf5df2008-08-15 23:14:31 +0200287 rp3 = (par >> 16);
288 rp3 ^= (rp3 >> 8);
289 rp3 &= 0xff;
290 rp2 = par & 0xffff;
291 rp2 ^= (rp2 >> 8);
292 rp2 &= 0xff;
frans1077be52008-08-20 21:11:50 +0200293#endif
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000294
franse6cf5df2008-08-15 23:14:31 +0200295 /* reduce par to 16 bits then calculate rp1 and rp0 */
296 par ^= (par >> 16);
frans1077be52008-08-20 21:11:50 +0200297#ifdef __BIG_ENDIAN
298 rp0 = (par >> 8) & 0xff;
299 rp1 = (par & 0xff);
300#else
franse6cf5df2008-08-15 23:14:31 +0200301 rp1 = (par >> 8) & 0xff;
302 rp0 = (par & 0xff);
frans1077be52008-08-20 21:11:50 +0200303#endif
franse6cf5df2008-08-15 23:14:31 +0200304
305 /* finally reduce par to 8 bits */
306 par ^= (par >> 8);
307 par &= 0xff;
308
309 /*
Singh, Vimald68156c2008-08-23 18:18:34 +0200310 * and calculate rp5..rp15..rp17
franse6cf5df2008-08-15 23:14:31 +0200311 * note that par = rp4 ^ rp5 and due to the commutative property
312 * of the ^ operator we can say:
313 * rp5 = (par ^ rp4);
314 * The & 0xff seems superfluous, but benchmarking learned that
315 * leaving it out gives slightly worse results. No idea why, probably
316 * it has to do with the way the pipeline in pentium is organized.
317 */
318 rp5 = (par ^ rp4) & 0xff;
319 rp7 = (par ^ rp6) & 0xff;
320 rp9 = (par ^ rp8) & 0xff;
321 rp11 = (par ^ rp10) & 0xff;
322 rp13 = (par ^ rp12) & 0xff;
323 rp15 = (par ^ rp14) & 0xff;
Singh, Vimald68156c2008-08-23 18:18:34 +0200324 if (eccsize_mult == 2)
325 rp17 = (par ^ rp16) & 0xff;
franse6cf5df2008-08-15 23:14:31 +0200326
327 /*
Brian Norris7854d3f2011-06-23 14:12:08 -0700328 * Finally calculate the ECC bits.
franse6cf5df2008-08-15 23:14:31 +0200329 * Again here it might seem that there are performance optimisations
330 * possible, but benchmarks showed that on the system this is developed
331 * the code below is the fastest
332 */
Timo Lindhorstfc029192006-11-27 13:35:49 +0100333#ifdef CONFIG_MTD_NAND_ECC_SMC
franse6cf5df2008-08-15 23:14:31 +0200334 code[0] =
335 (invparity[rp7] << 7) |
336 (invparity[rp6] << 6) |
337 (invparity[rp5] << 5) |
338 (invparity[rp4] << 4) |
339 (invparity[rp3] << 3) |
340 (invparity[rp2] << 2) |
341 (invparity[rp1] << 1) |
342 (invparity[rp0]);
343 code[1] =
344 (invparity[rp15] << 7) |
345 (invparity[rp14] << 6) |
346 (invparity[rp13] << 5) |
347 (invparity[rp12] << 4) |
348 (invparity[rp11] << 3) |
349 (invparity[rp10] << 2) |
350 (invparity[rp9] << 1) |
351 (invparity[rp8]);
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200352#else
franse6cf5df2008-08-15 23:14:31 +0200353 code[1] =
354 (invparity[rp7] << 7) |
355 (invparity[rp6] << 6) |
356 (invparity[rp5] << 5) |
357 (invparity[rp4] << 4) |
358 (invparity[rp3] << 3) |
359 (invparity[rp2] << 2) |
360 (invparity[rp1] << 1) |
361 (invparity[rp0]);
362 code[0] =
363 (invparity[rp15] << 7) |
364 (invparity[rp14] << 6) |
365 (invparity[rp13] << 5) |
366 (invparity[rp12] << 4) |
367 (invparity[rp11] << 3) |
368 (invparity[rp10] << 2) |
369 (invparity[rp9] << 1) |
370 (invparity[rp8]);
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200371#endif
Singh, Vimald68156c2008-08-23 18:18:34 +0200372 if (eccsize_mult == 1)
373 code[2] =
374 (invparity[par & 0xf0] << 7) |
375 (invparity[par & 0x0f] << 6) |
376 (invparity[par & 0xcc] << 5) |
377 (invparity[par & 0x33] << 4) |
378 (invparity[par & 0xaa] << 3) |
379 (invparity[par & 0x55] << 2) |
380 3;
381 else
382 code[2] =
383 (invparity[par & 0xf0] << 7) |
384 (invparity[par & 0x0f] << 6) |
385 (invparity[par & 0xcc] << 5) |
386 (invparity[par & 0x33] << 4) |
387 (invparity[par & 0xaa] << 3) |
388 (invparity[par & 0x55] << 2) |
389 (invparity[rp17] << 1) |
390 (invparity[rp16] << 0);
Akinobu Mita1c63aca2009-10-22 16:53:32 +0900391}
392EXPORT_SYMBOL(__nand_calculate_ecc);
393
394/**
395 * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256/512-byte
396 * block
397 * @mtd: MTD block structure
398 * @buf: input buffer with raw data
399 * @code: output buffer with ECC
400 */
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200401int nand_calculate_ecc(struct nand_chip *chip, const unsigned char *buf,
Akinobu Mita1c63aca2009-10-22 16:53:32 +0900402 unsigned char *code)
403{
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200404 __nand_calculate_ecc(buf, chip->ecc.size, code);
Akinobu Mita1c63aca2009-10-22 16:53:32 +0900405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return 0;
407}
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200408EXPORT_SYMBOL(nand_calculate_ecc);
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/**
Atsushi Nemotobe2f0922009-09-05 01:20:43 +0900411 * __nand_correct_data - [NAND Interface] Detect and correct bit error(s)
Alexey Korolev17c1d2be2008-08-20 22:32:08 +0100412 * @buf: raw data read from the chip
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * @read_ecc: ECC from the chip
414 * @calc_ecc: the ECC calculated from raw data
Brian Norris7854d3f2011-06-23 14:12:08 -0700415 * @eccsize: data bytes per ECC step (256 or 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 *
Atsushi Nemotobe2f0922009-09-05 01:20:43 +0900417 * Detect and correct a 1 bit error for eccsize byte block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
Atsushi Nemotobe2f0922009-09-05 01:20:43 +0900419int __nand_correct_data(unsigned char *buf,
420 unsigned char *read_ecc, unsigned char *calc_ecc,
421 unsigned int eccsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Vimal Singh260dc002009-02-23 13:46:08 +0530423 unsigned char b0, b1, b2, bit_addr;
424 unsigned int byte_addr;
Singh, Vimald68156c2008-08-23 18:18:34 +0200425 /* 256 or 512 bytes/ecc */
Atsushi Nemotobe2f0922009-09-05 01:20:43 +0900426 const uint32_t eccsize_mult = eccsize >> 8;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000427
franse6cf5df2008-08-15 23:14:31 +0200428 /*
429 * b0 to b2 indicate which bit is faulty (if any)
430 * we might need the xor result more than once,
431 * so keep them in a local var
432 */
Timo Lindhorstfc029192006-11-27 13:35:49 +0100433#ifdef CONFIG_MTD_NAND_ECC_SMC
franse6cf5df2008-08-15 23:14:31 +0200434 b0 = read_ecc[0] ^ calc_ecc[0];
435 b1 = read_ecc[1] ^ calc_ecc[1];
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200436#else
franse6cf5df2008-08-15 23:14:31 +0200437 b0 = read_ecc[1] ^ calc_ecc[1];
438 b1 = read_ecc[0] ^ calc_ecc[0];
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200439#endif
franse6cf5df2008-08-15 23:14:31 +0200440 b2 = read_ecc[2] ^ calc_ecc[2];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000441
franse6cf5df2008-08-15 23:14:31 +0200442 /* check if there are any bitfaults */
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200443
franse6cf5df2008-08-15 23:14:31 +0200444 /* repeated if statements are slightly more efficient than switch ... */
445 /* ordered in order of likelihood */
frans1077be52008-08-20 21:11:50 +0200446
447 if ((b0 | b1 | b2) == 0)
David Woodhouseccbcd6c2008-08-16 11:01:31 +0100448 return 0; /* no error */
frans1077be52008-08-20 21:11:50 +0200449
450 if ((((b0 ^ (b0 >> 1)) & 0x55) == 0x55) &&
451 (((b1 ^ (b1 >> 1)) & 0x55) == 0x55) &&
Singh, Vimald68156c2008-08-23 18:18:34 +0200452 ((eccsize_mult == 1 && ((b2 ^ (b2 >> 1)) & 0x54) == 0x54) ||
453 (eccsize_mult == 2 && ((b2 ^ (b2 >> 1)) & 0x55) == 0x55))) {
454 /* single bit error */
franse6cf5df2008-08-15 23:14:31 +0200455 /*
Singh, Vimald68156c2008-08-23 18:18:34 +0200456 * rp17/rp15/13/11/9/7/5/3/1 indicate which byte is the faulty
457 * byte, cp 5/3/1 indicate the faulty bit.
franse6cf5df2008-08-15 23:14:31 +0200458 * A lookup table (called addressbits) is used to filter
459 * the bits from the byte they are in.
460 * A marginal optimisation is possible by having three
461 * different lookup tables.
462 * One as we have now (for b0), one for b2
463 * (that would avoid the >> 1), and one for b1 (with all values
464 * << 4). However it was felt that introducing two more tables
465 * hardly justify the gain.
466 *
467 * The b2 shift is there to get rid of the lowest two bits.
468 * We could also do addressbits[b2] >> 1 but for the
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200469 * performance it does not make any difference
franse6cf5df2008-08-15 23:14:31 +0200470 */
Singh, Vimald68156c2008-08-23 18:18:34 +0200471 if (eccsize_mult == 1)
472 byte_addr = (addressbits[b1] << 4) + addressbits[b0];
473 else
474 byte_addr = (addressbits[b2 & 0x3] << 8) +
475 (addressbits[b1] << 4) + addressbits[b0];
franse6cf5df2008-08-15 23:14:31 +0200476 bit_addr = addressbits[b2 >> 2];
477 /* flip the bit */
478 buf[byte_addr] ^= (1 << bit_addr);
David Woodhouseccbcd6c2008-08-16 11:01:31 +0100479 return 1;
frans1077be52008-08-20 21:11:50 +0200480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
frans1077be52008-08-20 21:11:50 +0200482 /* count nr of bits; use table lookup, faster than calculating it */
483 if ((bitsperbyte[b0] + bitsperbyte[b1] + bitsperbyte[b2]) == 1)
Brian Norris7854d3f2011-06-23 14:12:08 -0700484 return 1; /* error in ECC data; no action needed */
frans1077be52008-08-20 21:11:50 +0200485
Raphaël Poggi85a3bd92014-04-08 10:19:48 -0700486 pr_err("%s: uncorrectable ECC error\n", __func__);
Boris BREZILLON6e941192015-12-30 20:32:03 +0100487 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
Atsushi Nemotobe2f0922009-09-05 01:20:43 +0900489EXPORT_SYMBOL(__nand_correct_data);
490
491/**
492 * nand_correct_data - [NAND Interface] Detect and correct bit error(s)
Boris Brezillon00da2ea2018-09-06 14:05:19 +0200493 * @chip: NAND chip object
Atsushi Nemotobe2f0922009-09-05 01:20:43 +0900494 * @buf: raw data read from the chip
495 * @read_ecc: ECC from the chip
496 * @calc_ecc: the ECC calculated from raw data
497 *
498 * Detect and correct a 1 bit error for 256/512 byte block
499 */
Boris Brezillon00da2ea2018-09-06 14:05:19 +0200500int nand_correct_data(struct nand_chip *chip, unsigned char *buf,
Atsushi Nemotobe2f0922009-09-05 01:20:43 +0900501 unsigned char *read_ecc, unsigned char *calc_ecc)
502{
Boris Brezillon00da2ea2018-09-06 14:05:19 +0200503 return __nand_correct_data(buf, read_ecc, calc_ecc, chip->ecc.size);
Atsushi Nemotobe2f0922009-09-05 01:20:43 +0900504}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505EXPORT_SYMBOL(nand_correct_data);
506
507MODULE_LICENSE("GPL");
franse6cf5df2008-08-15 23:14:31 +0200508MODULE_AUTHOR("Frans Meulenbroeks <fransmeulenbroeks@gmail.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509MODULE_DESCRIPTION("Generic NAND ECC support");