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> |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 45 | #include <linux/mtd/mtd.h> |
| 46 | #include <linux/mtd/nand.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/mtd/nand_ecc.h> |
frans | 1077be5 | 2008-08-20 21:11:50 +0200 | [diff] [blame] | 48 | #include <asm/byteorder.h> |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 49 | #else |
David Woodhouse | ccbcd6c | 2008-08-16 11:01:31 +0100 | [diff] [blame] | 50 | #include <stdint.h> |
| 51 | struct mtd_info; |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 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 */ |
frans | 1077be5 | 2008-08-20 21:11:50 +0200 | [diff] [blame] | 57 | |
| 58 | #define printk printf |
| 59 | #define KERN_ERR "" |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 60 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | /* |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | */ |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 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 | }; |
| 86 | |
| 87 | /* |
| 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) |
| 91 | */ |
| 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /** |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 153 | * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256/512-byte |
| 154 | * block |
| 155 | * @mtd: MTD block structure |
Alexey Korolev | 17c1d2b | 2008-08-20 22:32:08 +0100 | [diff] [blame] | 156 | * @buf: input buffer with raw data |
| 157 | * @code: output buffer with ECC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | */ |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 159 | int nand_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf, |
| 160 | unsigned char *code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 162 | int i; |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 163 | const uint32_t *bp = (uint32_t *)buf; |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 164 | /* 256 or 512 bytes/ecc */ |
| 165 | const uint32_t eccsize_mult = |
| 166 | (((struct nand_chip *)mtd->priv)->ecc.size) >> 8; |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 167 | uint32_t cur; /* current value in buffer */ |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 168 | /* rp0..rp15..rp17 are the various accumulated parities (per byte) */ |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 169 | uint32_t rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7; |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 170 | uint32_t rp8, rp9, rp10, rp11, rp12, rp13, rp14, rp15, rp16; |
| 171 | uint32_t uninitialized_var(rp17); /* to make compiler happy */ |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 172 | uint32_t par; /* the cumulative parity for all data */ |
| 173 | uint32_t tmppar; /* the cumulative parity for this iteration; |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 174 | for rp12, rp14 and rp16 at the end of the |
| 175 | loop */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 176 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 177 | par = 0; |
| 178 | rp4 = 0; |
| 179 | rp6 = 0; |
| 180 | rp8 = 0; |
| 181 | rp10 = 0; |
| 182 | rp12 = 0; |
| 183 | rp14 = 0; |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 184 | rp16 = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 185 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 186 | /* |
| 187 | * The loop is unrolled a number of times; |
| 188 | * This avoids if statements to decide on which rp value to update |
| 189 | * Also we process the data by longwords. |
| 190 | * Note: passing unaligned data might give a performance penalty. |
| 191 | * It is assumed that the buffers are aligned. |
| 192 | * tmppar is the cumulative sum of this iteration. |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 193 | * needed for calculating rp12, rp14, rp16 and par |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 194 | * also used as a performance improvement for rp6, rp8 and rp10 |
| 195 | */ |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 196 | for (i = 0; i < eccsize_mult << 2; i++) { |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 197 | cur = *bp++; |
| 198 | tmppar = cur; |
| 199 | rp4 ^= cur; |
| 200 | cur = *bp++; |
| 201 | tmppar ^= cur; |
| 202 | rp6 ^= tmppar; |
| 203 | cur = *bp++; |
| 204 | tmppar ^= cur; |
| 205 | rp4 ^= cur; |
| 206 | cur = *bp++; |
| 207 | tmppar ^= cur; |
| 208 | rp8 ^= tmppar; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 209 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 210 | cur = *bp++; |
| 211 | tmppar ^= cur; |
| 212 | rp4 ^= cur; |
| 213 | rp6 ^= cur; |
| 214 | cur = *bp++; |
| 215 | tmppar ^= cur; |
| 216 | rp6 ^= cur; |
| 217 | cur = *bp++; |
| 218 | tmppar ^= cur; |
| 219 | rp4 ^= cur; |
| 220 | cur = *bp++; |
| 221 | tmppar ^= cur; |
| 222 | rp10 ^= tmppar; |
| 223 | |
| 224 | cur = *bp++; |
| 225 | tmppar ^= cur; |
| 226 | rp4 ^= cur; |
| 227 | rp6 ^= cur; |
| 228 | rp8 ^= cur; |
| 229 | cur = *bp++; |
| 230 | tmppar ^= cur; |
| 231 | rp6 ^= cur; |
| 232 | rp8 ^= cur; |
| 233 | cur = *bp++; |
| 234 | tmppar ^= cur; |
| 235 | rp4 ^= cur; |
| 236 | rp8 ^= cur; |
| 237 | cur = *bp++; |
| 238 | tmppar ^= cur; |
| 239 | rp8 ^= cur; |
| 240 | |
| 241 | cur = *bp++; |
| 242 | tmppar ^= cur; |
| 243 | rp4 ^= cur; |
| 244 | rp6 ^= cur; |
| 245 | cur = *bp++; |
| 246 | tmppar ^= cur; |
| 247 | rp6 ^= cur; |
| 248 | cur = *bp++; |
| 249 | tmppar ^= cur; |
| 250 | rp4 ^= cur; |
| 251 | cur = *bp++; |
| 252 | tmppar ^= cur; |
| 253 | |
| 254 | par ^= tmppar; |
| 255 | if ((i & 0x1) == 0) |
| 256 | rp12 ^= tmppar; |
| 257 | if ((i & 0x2) == 0) |
| 258 | rp14 ^= tmppar; |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 259 | if (eccsize_mult == 2 && (i & 0x4) == 0) |
| 260 | rp16 ^= tmppar; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 262 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 263 | /* |
| 264 | * handle the fact that we use longword operations |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 265 | * we'll bring rp4..rp14..rp16 back to single byte entities by |
| 266 | * shifting and xoring first fold the upper and lower 16 bits, |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 267 | * then the upper and lower 8 bits. |
| 268 | */ |
| 269 | rp4 ^= (rp4 >> 16); |
| 270 | rp4 ^= (rp4 >> 8); |
| 271 | rp4 &= 0xff; |
| 272 | rp6 ^= (rp6 >> 16); |
| 273 | rp6 ^= (rp6 >> 8); |
| 274 | rp6 &= 0xff; |
| 275 | rp8 ^= (rp8 >> 16); |
| 276 | rp8 ^= (rp8 >> 8); |
| 277 | rp8 &= 0xff; |
| 278 | rp10 ^= (rp10 >> 16); |
| 279 | rp10 ^= (rp10 >> 8); |
| 280 | rp10 &= 0xff; |
| 281 | rp12 ^= (rp12 >> 16); |
| 282 | rp12 ^= (rp12 >> 8); |
| 283 | rp12 &= 0xff; |
| 284 | rp14 ^= (rp14 >> 16); |
| 285 | rp14 ^= (rp14 >> 8); |
| 286 | rp14 &= 0xff; |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 287 | if (eccsize_mult == 2) { |
| 288 | rp16 ^= (rp16 >> 16); |
| 289 | rp16 ^= (rp16 >> 8); |
| 290 | rp16 &= 0xff; |
| 291 | } |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 292 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 293 | /* |
| 294 | * we also need to calculate the row parity for rp0..rp3 |
| 295 | * This is present in par, because par is now |
frans | 1077be5 | 2008-08-20 21:11:50 +0200 | [diff] [blame] | 296 | * rp3 rp3 rp2 rp2 in little endian and |
| 297 | * rp2 rp2 rp3 rp3 in big endian |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 298 | * as well as |
frans | 1077be5 | 2008-08-20 21:11:50 +0200 | [diff] [blame] | 299 | * rp1 rp0 rp1 rp0 in little endian and |
| 300 | * rp0 rp1 rp0 rp1 in big endian |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 301 | * First calculate rp2 and rp3 |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 302 | */ |
frans | 1077be5 | 2008-08-20 21:11:50 +0200 | [diff] [blame] | 303 | #ifdef __BIG_ENDIAN |
| 304 | rp2 = (par >> 16); |
| 305 | rp2 ^= (rp2 >> 8); |
| 306 | rp2 &= 0xff; |
| 307 | rp3 = par & 0xffff; |
| 308 | rp3 ^= (rp3 >> 8); |
| 309 | rp3 &= 0xff; |
| 310 | #else |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 311 | rp3 = (par >> 16); |
| 312 | rp3 ^= (rp3 >> 8); |
| 313 | rp3 &= 0xff; |
| 314 | rp2 = par & 0xffff; |
| 315 | rp2 ^= (rp2 >> 8); |
| 316 | rp2 &= 0xff; |
frans | 1077be5 | 2008-08-20 21:11:50 +0200 | [diff] [blame] | 317 | #endif |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 318 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 319 | /* reduce par to 16 bits then calculate rp1 and rp0 */ |
| 320 | par ^= (par >> 16); |
frans | 1077be5 | 2008-08-20 21:11:50 +0200 | [diff] [blame] | 321 | #ifdef __BIG_ENDIAN |
| 322 | rp0 = (par >> 8) & 0xff; |
| 323 | rp1 = (par & 0xff); |
| 324 | #else |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 325 | rp1 = (par >> 8) & 0xff; |
| 326 | rp0 = (par & 0xff); |
frans | 1077be5 | 2008-08-20 21:11:50 +0200 | [diff] [blame] | 327 | #endif |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 328 | |
| 329 | /* finally reduce par to 8 bits */ |
| 330 | par ^= (par >> 8); |
| 331 | par &= 0xff; |
| 332 | |
| 333 | /* |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 334 | * and calculate rp5..rp15..rp17 |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 335 | * note that par = rp4 ^ rp5 and due to the commutative property |
| 336 | * of the ^ operator we can say: |
| 337 | * rp5 = (par ^ rp4); |
| 338 | * The & 0xff seems superfluous, but benchmarking learned that |
| 339 | * leaving it out gives slightly worse results. No idea why, probably |
| 340 | * it has to do with the way the pipeline in pentium is organized. |
| 341 | */ |
| 342 | rp5 = (par ^ rp4) & 0xff; |
| 343 | rp7 = (par ^ rp6) & 0xff; |
| 344 | rp9 = (par ^ rp8) & 0xff; |
| 345 | rp11 = (par ^ rp10) & 0xff; |
| 346 | rp13 = (par ^ rp12) & 0xff; |
| 347 | rp15 = (par ^ rp14) & 0xff; |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 348 | if (eccsize_mult == 2) |
| 349 | rp17 = (par ^ rp16) & 0xff; |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 350 | |
| 351 | /* |
| 352 | * Finally calculate the ecc bits. |
| 353 | * Again here it might seem that there are performance optimisations |
| 354 | * possible, but benchmarks showed that on the system this is developed |
| 355 | * the code below is the fastest |
| 356 | */ |
Timo Lindhorst | fc02919 | 2006-11-27 13:35:49 +0100 | [diff] [blame] | 357 | #ifdef CONFIG_MTD_NAND_ECC_SMC |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 358 | code[0] = |
| 359 | (invparity[rp7] << 7) | |
| 360 | (invparity[rp6] << 6) | |
| 361 | (invparity[rp5] << 5) | |
| 362 | (invparity[rp4] << 4) | |
| 363 | (invparity[rp3] << 3) | |
| 364 | (invparity[rp2] << 2) | |
| 365 | (invparity[rp1] << 1) | |
| 366 | (invparity[rp0]); |
| 367 | code[1] = |
| 368 | (invparity[rp15] << 7) | |
| 369 | (invparity[rp14] << 6) | |
| 370 | (invparity[rp13] << 5) | |
| 371 | (invparity[rp12] << 4) | |
| 372 | (invparity[rp11] << 3) | |
| 373 | (invparity[rp10] << 2) | |
| 374 | (invparity[rp9] << 1) | |
| 375 | (invparity[rp8]); |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 376 | #else |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 377 | code[1] = |
| 378 | (invparity[rp7] << 7) | |
| 379 | (invparity[rp6] << 6) | |
| 380 | (invparity[rp5] << 5) | |
| 381 | (invparity[rp4] << 4) | |
| 382 | (invparity[rp3] << 3) | |
| 383 | (invparity[rp2] << 2) | |
| 384 | (invparity[rp1] << 1) | |
| 385 | (invparity[rp0]); |
| 386 | code[0] = |
| 387 | (invparity[rp15] << 7) | |
| 388 | (invparity[rp14] << 6) | |
| 389 | (invparity[rp13] << 5) | |
| 390 | (invparity[rp12] << 4) | |
| 391 | (invparity[rp11] << 3) | |
| 392 | (invparity[rp10] << 2) | |
| 393 | (invparity[rp9] << 1) | |
| 394 | (invparity[rp8]); |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 395 | #endif |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 396 | if (eccsize_mult == 1) |
| 397 | code[2] = |
| 398 | (invparity[par & 0xf0] << 7) | |
| 399 | (invparity[par & 0x0f] << 6) | |
| 400 | (invparity[par & 0xcc] << 5) | |
| 401 | (invparity[par & 0x33] << 4) | |
| 402 | (invparity[par & 0xaa] << 3) | |
| 403 | (invparity[par & 0x55] << 2) | |
| 404 | 3; |
| 405 | else |
| 406 | code[2] = |
| 407 | (invparity[par & 0xf0] << 7) | |
| 408 | (invparity[par & 0x0f] << 6) | |
| 409 | (invparity[par & 0xcc] << 5) | |
| 410 | (invparity[par & 0x33] << 4) | |
| 411 | (invparity[par & 0xaa] << 3) | |
| 412 | (invparity[par & 0x55] << 2) | |
| 413 | (invparity[rp17] << 1) | |
| 414 | (invparity[rp16] << 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | return 0; |
| 416 | } |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 417 | EXPORT_SYMBOL(nand_calculate_ecc); |
| 418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | /** |
| 420 | * nand_correct_data - [NAND Interface] Detect and correct bit error(s) |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 421 | * @mtd: MTD block structure |
Alexey Korolev | 17c1d2b | 2008-08-20 22:32:08 +0100 | [diff] [blame] | 422 | * @buf: raw data read from the chip |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | * @read_ecc: ECC from the chip |
| 424 | * @calc_ecc: the ECC calculated from raw data |
| 425 | * |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 426 | * Detect and correct a 1 bit error for 256/512 byte block |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | */ |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 428 | int nand_correct_data(struct mtd_info *mtd, unsigned char *buf, |
| 429 | unsigned char *read_ecc, unsigned char *calc_ecc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { |
Vimal Singh | 260dc00 | 2009-02-23 13:46:08 +0530 | [diff] [blame^] | 431 | unsigned char b0, b1, b2, bit_addr; |
| 432 | unsigned int byte_addr; |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 433 | /* 256 or 512 bytes/ecc */ |
| 434 | const uint32_t eccsize_mult = |
| 435 | (((struct nand_chip *)mtd->priv)->ecc.size) >> 8; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 436 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 437 | /* |
| 438 | * b0 to b2 indicate which bit is faulty (if any) |
| 439 | * we might need the xor result more than once, |
| 440 | * so keep them in a local var |
| 441 | */ |
Timo Lindhorst | fc02919 | 2006-11-27 13:35:49 +0100 | [diff] [blame] | 442 | #ifdef CONFIG_MTD_NAND_ECC_SMC |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 443 | b0 = read_ecc[0] ^ calc_ecc[0]; |
| 444 | b1 = read_ecc[1] ^ calc_ecc[1]; |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 445 | #else |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 446 | b0 = read_ecc[1] ^ calc_ecc[1]; |
| 447 | b1 = read_ecc[0] ^ calc_ecc[0]; |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 448 | #endif |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 449 | b2 = read_ecc[2] ^ calc_ecc[2]; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 450 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 451 | /* check if there are any bitfaults */ |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 452 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 453 | /* repeated if statements are slightly more efficient than switch ... */ |
| 454 | /* ordered in order of likelihood */ |
frans | 1077be5 | 2008-08-20 21:11:50 +0200 | [diff] [blame] | 455 | |
| 456 | if ((b0 | b1 | b2) == 0) |
David Woodhouse | ccbcd6c | 2008-08-16 11:01:31 +0100 | [diff] [blame] | 457 | return 0; /* no error */ |
frans | 1077be5 | 2008-08-20 21:11:50 +0200 | [diff] [blame] | 458 | |
| 459 | if ((((b0 ^ (b0 >> 1)) & 0x55) == 0x55) && |
| 460 | (((b1 ^ (b1 >> 1)) & 0x55) == 0x55) && |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 461 | ((eccsize_mult == 1 && ((b2 ^ (b2 >> 1)) & 0x54) == 0x54) || |
| 462 | (eccsize_mult == 2 && ((b2 ^ (b2 >> 1)) & 0x55) == 0x55))) { |
| 463 | /* single bit error */ |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 464 | /* |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 465 | * rp17/rp15/13/11/9/7/5/3/1 indicate which byte is the faulty |
| 466 | * byte, cp 5/3/1 indicate the faulty bit. |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 467 | * A lookup table (called addressbits) is used to filter |
| 468 | * the bits from the byte they are in. |
| 469 | * A marginal optimisation is possible by having three |
| 470 | * different lookup tables. |
| 471 | * One as we have now (for b0), one for b2 |
| 472 | * (that would avoid the >> 1), and one for b1 (with all values |
| 473 | * << 4). However it was felt that introducing two more tables |
| 474 | * hardly justify the gain. |
| 475 | * |
| 476 | * The b2 shift is there to get rid of the lowest two bits. |
| 477 | * We could also do addressbits[b2] >> 1 but for the |
| 478 | * performace it does not make any difference |
| 479 | */ |
Singh, Vimal | d68156c | 2008-08-23 18:18:34 +0200 | [diff] [blame] | 480 | if (eccsize_mult == 1) |
| 481 | byte_addr = (addressbits[b1] << 4) + addressbits[b0]; |
| 482 | else |
| 483 | byte_addr = (addressbits[b2 & 0x3] << 8) + |
| 484 | (addressbits[b1] << 4) + addressbits[b0]; |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 485 | bit_addr = addressbits[b2 >> 2]; |
| 486 | /* flip the bit */ |
| 487 | buf[byte_addr] ^= (1 << bit_addr); |
David Woodhouse | ccbcd6c | 2008-08-16 11:01:31 +0100 | [diff] [blame] | 488 | return 1; |
frans | 1077be5 | 2008-08-20 21:11:50 +0200 | [diff] [blame] | 489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
frans | 1077be5 | 2008-08-20 21:11:50 +0200 | [diff] [blame] | 491 | /* count nr of bits; use table lookup, faster than calculating it */ |
| 492 | if ((bitsperbyte[b0] + bitsperbyte[b1] + bitsperbyte[b2]) == 1) |
David Woodhouse | ccbcd6c | 2008-08-16 11:01:31 +0100 | [diff] [blame] | 493 | return 1; /* error in ecc data; no action needed */ |
frans | 1077be5 | 2008-08-20 21:11:50 +0200 | [diff] [blame] | 494 | |
| 495 | printk(KERN_ERR "uncorrectable error : "); |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 496 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | EXPORT_SYMBOL(nand_correct_data); |
| 499 | |
| 500 | MODULE_LICENSE("GPL"); |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame] | 501 | MODULE_AUTHOR("Frans Meulenbroeks <fransmeulenbroeks@gmail.com>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | MODULE_DESCRIPTION("Generic NAND ECC support"); |