Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 2 | * This file contains an ECC algorithm that detects and corrects 1 bit |
| 3 | * errors in a 256 byte block of data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * drivers/mtd/nand/nand_ecc.c |
| 6 | * |
David Woodhouse | ccbcd6c | 2008-08-16 11:01:31 +0100 | [diff] [blame] | 7 | * Copyright © 2008 Koninklijke Philips Electronics NV. |
| 8 | * Author: Frans Meulenbroeks |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 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 |
David Woodhouse | ccbcd6c | 2008-08-16 11:01:31 +0100 | [diff] [blame] | 15 | * can be found in Documentation/mtd/nand_ecc.txt |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 16 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 21 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 26 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 30 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | */ |
| 32 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 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. |
David Woodhouse | ccbcd6c | 2008-08-16 11:01:31 +0100 | [diff] [blame] | 38 | * Instead a private definition of mtd_info is given to satisfy the compiler |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 39 | * (the code does not use mtd_info, so the code does not care) |
| 40 | */ |
| 41 | #ifndef STANDALONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/types.h> |
| 43 | #include <linux/kernel.h> |
| 44 | #include <linux/module.h> |
| 45 | #include <linux/mtd/nand_ecc.h> |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 46 | #else |
David Woodhouse | ccbcd6c | 2008-08-16 11:01:31 +0100 | [diff] [blame] | 47 | #include <stdint.h> |
| 48 | struct mtd_info; |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 49 | #define EXPORT_SYMBOL(x) /* x */ |
| 50 | |
| 51 | #define MODULE_LICENSE(x) /* x */ |
| 52 | #define MODULE_AUTHOR(x) /* x */ |
| 53 | #define MODULE_DESCRIPTION(x) /* x */ |
| 54 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 57 | * invparity is a 256 byte table that contains the odd parity |
| 58 | * for each byte. So if the number of bits in a byte is even, |
| 59 | * the array element is 1, and when the number of bits is odd |
| 60 | * the array eleemnt is 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | */ |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 62 | static const char invparity[256] = { |
| 63 | 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, |
| 64 | 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, |
| 65 | 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, |
| 66 | 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, |
| 67 | 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, |
| 68 | 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, |
| 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 | 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, |
| 74 | 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, |
| 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 | }; |
| 80 | |
| 81 | /* |
| 82 | * bitsperbyte contains the number of bits per byte |
| 83 | * this is only used for testing and repairing parity |
| 84 | * (a precalculated value slightly improves performance) |
| 85 | */ |
| 86 | static const char bitsperbyte[256] = { |
| 87 | 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, |
| 88 | 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
| 89 | 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
| 90 | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| 91 | 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
| 92 | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| 93 | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| 94 | 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
| 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 | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| 98 | 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
| 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 | 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
| 102 | 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8, |
| 103 | }; |
| 104 | |
| 105 | /* |
| 106 | * addressbits is a lookup table to filter out the bits from the xor-ed |
| 107 | * ecc data that identify the faulty location. |
| 108 | * this is only used for repairing parity |
| 109 | * see the comments in nand_correct_data for more details |
| 110 | */ |
| 111 | static const char addressbits[256] = { |
| 112 | 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, |
| 113 | 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03, |
| 114 | 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, |
| 115 | 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03, |
| 116 | 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05, |
| 117 | 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07, |
| 118 | 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05, |
| 119 | 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07, |
| 120 | 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, |
| 121 | 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03, |
| 122 | 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, |
| 123 | 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03, |
| 124 | 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05, |
| 125 | 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07, |
| 126 | 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05, |
| 127 | 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07, |
| 128 | 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09, |
| 129 | 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b, |
| 130 | 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09, |
| 131 | 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b, |
| 132 | 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d, |
| 133 | 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f, |
| 134 | 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d, |
| 135 | 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f, |
| 136 | 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09, |
| 137 | 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b, |
| 138 | 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09, |
| 139 | 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b, |
| 140 | 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d, |
| 141 | 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f, |
| 142 | 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d, |
| 143 | 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | /** |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 147 | * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 148 | * @mtd: MTD block structure (unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | * @dat: raw data |
| 150 | * @ecc_code: buffer for ECC |
| 151 | */ |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 152 | int nand_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf, |
| 153 | unsigned char *code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 155 | int i; |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 156 | const uint32_t *bp = (uint32_t *)buf; |
| 157 | uint32_t cur; /* current value in buffer */ |
| 158 | /* rp0..rp15 are the various accumulated parities (per byte) */ |
| 159 | uint32_t rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7; |
| 160 | uint32_t rp8, rp9, rp10, rp11, rp12, rp13, rp14, rp15; |
| 161 | uint32_t par; /* the cumulative parity for all data */ |
| 162 | uint32_t tmppar; /* the cumulative parity for this iteration; |
| 163 | for rp12 and rp14 at the end of the loop */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 164 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 165 | par = 0; |
| 166 | rp4 = 0; |
| 167 | rp6 = 0; |
| 168 | rp8 = 0; |
| 169 | rp10 = 0; |
| 170 | rp12 = 0; |
| 171 | rp14 = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 172 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 173 | /* |
| 174 | * The loop is unrolled a number of times; |
| 175 | * This avoids if statements to decide on which rp value to update |
| 176 | * Also we process the data by longwords. |
| 177 | * Note: passing unaligned data might give a performance penalty. |
| 178 | * It is assumed that the buffers are aligned. |
| 179 | * tmppar is the cumulative sum of this iteration. |
| 180 | * needed for calculating rp12, rp14 and par |
| 181 | * also used as a performance improvement for rp6, rp8 and rp10 |
| 182 | */ |
| 183 | for (i = 0; i < 4; i++) { |
| 184 | cur = *bp++; |
| 185 | tmppar = cur; |
| 186 | rp4 ^= cur; |
| 187 | cur = *bp++; |
| 188 | tmppar ^= cur; |
| 189 | rp6 ^= tmppar; |
| 190 | cur = *bp++; |
| 191 | tmppar ^= cur; |
| 192 | rp4 ^= cur; |
| 193 | cur = *bp++; |
| 194 | tmppar ^= cur; |
| 195 | rp8 ^= tmppar; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 196 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 197 | cur = *bp++; |
| 198 | tmppar ^= cur; |
| 199 | rp4 ^= cur; |
| 200 | rp6 ^= cur; |
| 201 | cur = *bp++; |
| 202 | tmppar ^= cur; |
| 203 | rp6 ^= cur; |
| 204 | cur = *bp++; |
| 205 | tmppar ^= cur; |
| 206 | rp4 ^= cur; |
| 207 | cur = *bp++; |
| 208 | tmppar ^= cur; |
| 209 | rp10 ^= tmppar; |
| 210 | |
| 211 | cur = *bp++; |
| 212 | tmppar ^= cur; |
| 213 | rp4 ^= cur; |
| 214 | rp6 ^= cur; |
| 215 | rp8 ^= cur; |
| 216 | cur = *bp++; |
| 217 | tmppar ^= cur; |
| 218 | rp6 ^= cur; |
| 219 | rp8 ^= cur; |
| 220 | cur = *bp++; |
| 221 | tmppar ^= cur; |
| 222 | rp4 ^= cur; |
| 223 | rp8 ^= cur; |
| 224 | cur = *bp++; |
| 225 | tmppar ^= cur; |
| 226 | rp8 ^= cur; |
| 227 | |
| 228 | cur = *bp++; |
| 229 | tmppar ^= cur; |
| 230 | rp4 ^= cur; |
| 231 | rp6 ^= cur; |
| 232 | cur = *bp++; |
| 233 | tmppar ^= cur; |
| 234 | rp6 ^= cur; |
| 235 | cur = *bp++; |
| 236 | tmppar ^= cur; |
| 237 | rp4 ^= cur; |
| 238 | cur = *bp++; |
| 239 | tmppar ^= cur; |
| 240 | |
| 241 | par ^= tmppar; |
| 242 | if ((i & 0x1) == 0) |
| 243 | rp12 ^= tmppar; |
| 244 | if ((i & 0x2) == 0) |
| 245 | rp14 ^= tmppar; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 247 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 248 | /* |
| 249 | * handle the fact that we use longword operations |
| 250 | * we'll bring rp4..rp14 back to single byte entities by shifting and |
| 251 | * xoring first fold the upper and lower 16 bits, |
| 252 | * then the upper and lower 8 bits. |
| 253 | */ |
| 254 | rp4 ^= (rp4 >> 16); |
| 255 | rp4 ^= (rp4 >> 8); |
| 256 | rp4 &= 0xff; |
| 257 | rp6 ^= (rp6 >> 16); |
| 258 | rp6 ^= (rp6 >> 8); |
| 259 | rp6 &= 0xff; |
| 260 | rp8 ^= (rp8 >> 16); |
| 261 | rp8 ^= (rp8 >> 8); |
| 262 | rp8 &= 0xff; |
| 263 | rp10 ^= (rp10 >> 16); |
| 264 | rp10 ^= (rp10 >> 8); |
| 265 | rp10 &= 0xff; |
| 266 | rp12 ^= (rp12 >> 16); |
| 267 | rp12 ^= (rp12 >> 8); |
| 268 | rp12 &= 0xff; |
| 269 | rp14 ^= (rp14 >> 16); |
| 270 | rp14 ^= (rp14 >> 8); |
| 271 | rp14 &= 0xff; |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 272 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 273 | /* |
| 274 | * we also need to calculate the row parity for rp0..rp3 |
| 275 | * This is present in par, because par is now |
| 276 | * rp3 rp3 rp2 rp2 |
| 277 | * as well as |
| 278 | * rp1 rp0 rp1 rp0 |
| 279 | * First calculate rp2 and rp3 |
| 280 | * (and yes: rp2 = (par ^ rp3) & 0xff; but doing that did not |
| 281 | * give a performance improvement) |
| 282 | */ |
| 283 | rp3 = (par >> 16); |
| 284 | rp3 ^= (rp3 >> 8); |
| 285 | rp3 &= 0xff; |
| 286 | rp2 = par & 0xffff; |
| 287 | rp2 ^= (rp2 >> 8); |
| 288 | rp2 &= 0xff; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 289 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 290 | /* reduce par to 16 bits then calculate rp1 and rp0 */ |
| 291 | par ^= (par >> 16); |
| 292 | rp1 = (par >> 8) & 0xff; |
| 293 | rp0 = (par & 0xff); |
| 294 | |
| 295 | /* finally reduce par to 8 bits */ |
| 296 | par ^= (par >> 8); |
| 297 | par &= 0xff; |
| 298 | |
| 299 | /* |
| 300 | * and calculate rp5..rp15 |
| 301 | * note that par = rp4 ^ rp5 and due to the commutative property |
| 302 | * of the ^ operator we can say: |
| 303 | * rp5 = (par ^ rp4); |
| 304 | * The & 0xff seems superfluous, but benchmarking learned that |
| 305 | * leaving it out gives slightly worse results. No idea why, probably |
| 306 | * it has to do with the way the pipeline in pentium is organized. |
| 307 | */ |
| 308 | rp5 = (par ^ rp4) & 0xff; |
| 309 | rp7 = (par ^ rp6) & 0xff; |
| 310 | rp9 = (par ^ rp8) & 0xff; |
| 311 | rp11 = (par ^ rp10) & 0xff; |
| 312 | rp13 = (par ^ rp12) & 0xff; |
| 313 | rp15 = (par ^ rp14) & 0xff; |
| 314 | |
| 315 | /* |
| 316 | * Finally calculate the ecc bits. |
| 317 | * Again here it might seem that there are performance optimisations |
| 318 | * possible, but benchmarks showed that on the system this is developed |
| 319 | * the code below is the fastest |
| 320 | */ |
Timo Lindhorst | fc02919 | 2006-11-27 13:35:49 +0100 | [diff] [blame] | 321 | #ifdef CONFIG_MTD_NAND_ECC_SMC |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 322 | code[0] = |
| 323 | (invparity[rp7] << 7) | |
| 324 | (invparity[rp6] << 6) | |
| 325 | (invparity[rp5] << 5) | |
| 326 | (invparity[rp4] << 4) | |
| 327 | (invparity[rp3] << 3) | |
| 328 | (invparity[rp2] << 2) | |
| 329 | (invparity[rp1] << 1) | |
| 330 | (invparity[rp0]); |
| 331 | code[1] = |
| 332 | (invparity[rp15] << 7) | |
| 333 | (invparity[rp14] << 6) | |
| 334 | (invparity[rp13] << 5) | |
| 335 | (invparity[rp12] << 4) | |
| 336 | (invparity[rp11] << 3) | |
| 337 | (invparity[rp10] << 2) | |
| 338 | (invparity[rp9] << 1) | |
| 339 | (invparity[rp8]); |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 340 | #else |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 341 | code[1] = |
| 342 | (invparity[rp7] << 7) | |
| 343 | (invparity[rp6] << 6) | |
| 344 | (invparity[rp5] << 5) | |
| 345 | (invparity[rp4] << 4) | |
| 346 | (invparity[rp3] << 3) | |
| 347 | (invparity[rp2] << 2) | |
| 348 | (invparity[rp1] << 1) | |
| 349 | (invparity[rp0]); |
| 350 | code[0] = |
| 351 | (invparity[rp15] << 7) | |
| 352 | (invparity[rp14] << 6) | |
| 353 | (invparity[rp13] << 5) | |
| 354 | (invparity[rp12] << 4) | |
| 355 | (invparity[rp11] << 3) | |
| 356 | (invparity[rp10] << 2) | |
| 357 | (invparity[rp9] << 1) | |
| 358 | (invparity[rp8]); |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 359 | #endif |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 360 | code[2] = |
| 361 | (invparity[par & 0xf0] << 7) | |
| 362 | (invparity[par & 0x0f] << 6) | |
| 363 | (invparity[par & 0xcc] << 5) | |
| 364 | (invparity[par & 0x33] << 4) | |
| 365 | (invparity[par & 0xaa] << 3) | |
| 366 | (invparity[par & 0x55] << 2) | |
| 367 | 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | return 0; |
| 369 | } |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 370 | EXPORT_SYMBOL(nand_calculate_ecc); |
| 371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | /** |
| 373 | * nand_correct_data - [NAND Interface] Detect and correct bit error(s) |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 374 | * @mtd: MTD block structure (unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | * @dat: raw data read from the chip |
| 376 | * @read_ecc: ECC from the chip |
| 377 | * @calc_ecc: the ECC calculated from raw data |
| 378 | * |
| 379 | * Detect and correct a 1 bit error for 256 byte block |
| 380 | */ |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 381 | int nand_correct_data(struct mtd_info *mtd, unsigned char *buf, |
| 382 | unsigned char *read_ecc, unsigned char *calc_ecc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | { |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 384 | int nr_bits; |
| 385 | unsigned char b0, b1, b2; |
| 386 | unsigned char byte_addr, bit_addr; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 387 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 388 | /* |
| 389 | * b0 to b2 indicate which bit is faulty (if any) |
| 390 | * we might need the xor result more than once, |
| 391 | * so keep them in a local var |
| 392 | */ |
Timo Lindhorst | fc02919 | 2006-11-27 13:35:49 +0100 | [diff] [blame] | 393 | #ifdef CONFIG_MTD_NAND_ECC_SMC |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 394 | b0 = read_ecc[0] ^ calc_ecc[0]; |
| 395 | b1 = read_ecc[1] ^ calc_ecc[1]; |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 396 | #else |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 397 | b0 = read_ecc[1] ^ calc_ecc[1]; |
| 398 | b1 = read_ecc[0] ^ calc_ecc[0]; |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 399 | #endif |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 400 | b2 = read_ecc[2] ^ calc_ecc[2]; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 401 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 402 | /* check if there are any bitfaults */ |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 403 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 404 | /* count nr of bits; use table lookup, faster than calculating it */ |
| 405 | nr_bits = bitsperbyte[b0] + bitsperbyte[b1] + bitsperbyte[b2]; |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 406 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 407 | /* repeated if statements are slightly more efficient than switch ... */ |
| 408 | /* ordered in order of likelihood */ |
| 409 | if (nr_bits == 0) |
David Woodhouse | ccbcd6c | 2008-08-16 11:01:31 +0100 | [diff] [blame] | 410 | return 0; /* no error */ |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 411 | if (nr_bits == 11) { /* correctable error */ |
| 412 | /* |
| 413 | * rp15/13/11/9/7/5/3/1 indicate which byte is the faulty byte |
| 414 | * cp 5/3/1 indicate the faulty bit. |
| 415 | * A lookup table (called addressbits) is used to filter |
| 416 | * the bits from the byte they are in. |
| 417 | * A marginal optimisation is possible by having three |
| 418 | * different lookup tables. |
| 419 | * One as we have now (for b0), one for b2 |
| 420 | * (that would avoid the >> 1), and one for b1 (with all values |
| 421 | * << 4). However it was felt that introducing two more tables |
| 422 | * hardly justify the gain. |
| 423 | * |
| 424 | * The b2 shift is there to get rid of the lowest two bits. |
| 425 | * We could also do addressbits[b2] >> 1 but for the |
| 426 | * performace it does not make any difference |
| 427 | */ |
| 428 | byte_addr = (addressbits[b1] << 4) + addressbits[b0]; |
| 429 | bit_addr = addressbits[b2 >> 2]; |
| 430 | /* flip the bit */ |
| 431 | buf[byte_addr] ^= (1 << bit_addr); |
David Woodhouse | ccbcd6c | 2008-08-16 11:01:31 +0100 | [diff] [blame] | 432 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 434 | if (nr_bits == 1) |
David Woodhouse | ccbcd6c | 2008-08-16 11:01:31 +0100 | [diff] [blame] | 435 | return 1; /* error in ecc data; no action needed */ |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 436 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | EXPORT_SYMBOL(nand_correct_data); |
| 439 | |
| 440 | MODULE_LICENSE("GPL"); |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 441 | MODULE_AUTHOR("Frans Meulenbroeks <fransmeulenbroeks@gmail.com>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | MODULE_DESCRIPTION("Generic NAND ECC support"); |