Mercurial > yaffs-ecoscentric-gpl
annotate yaffs_ecc.c @ 414:60b3b6fbb259
yaffs: Speed up test script
Set tracing to zero so that test script runs faster
Signed-off-by: Charles Manning <cdhmanning@gmail.com>
| author | Charles Manning <cdhmanning@gmail.com> |
|---|---|
| date | Wed, 28 Apr 2010 08:38:44 +1200 |
| parents | b5309c9b6d7f |
| children |
| rev | line source |
|---|---|
| 0 | 1 /* |
| 168 | 2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. |
| 0 | 3 * |
|
409
b5309c9b6d7f
yaffs Some cleanups.
Charles Manning <cdhmanning@gmail.com>
parents:
271
diff
changeset
|
4 * Copyright (C) 2002-2010 Aleph One Ltd. |
| 168 | 5 * for Toby Churchill Ltd and Brightstar Engineering |
| 0 | 6 * |
| 7 * Created by Charles Manning <charles@aleph1.co.uk> | |
| 8 * | |
| 168 | 9 * This program is free software; you can redistribute it and/or modify |
| 10 * it under the terms of the GNU General Public License version 2 as | |
| 11 * published by the Free Software Foundation. | |
| 0 | 12 */ |
| 75 | 13 |
|
171
fcfa82492193
Updated headers so they really are the same
wookey <wookey>
parents:
168
diff
changeset
|
14 /* |
|
fcfa82492193
Updated headers so they really are the same
wookey <wookey>
parents:
168
diff
changeset
|
15 * This code implements the ECC algorithm used in SmartMedia. |
|
fcfa82492193
Updated headers so they really are the same
wookey <wookey>
parents:
168
diff
changeset
|
16 * |
|
198
b7f785925166
Cleanup patch - Remove all trailing whitespace and fix a few typos.
wookey <wookey>
parents:
171
diff
changeset
|
17 * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes. |
|
171
fcfa82492193
Updated headers so they really are the same
wookey <wookey>
parents:
168
diff
changeset
|
18 * The two unused bit are set to 1. |
|
198
b7f785925166
Cleanup patch - Remove all trailing whitespace and fix a few typos.
wookey <wookey>
parents:
171
diff
changeset
|
19 * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC |
|
171
fcfa82492193
Updated headers so they really are the same
wookey <wookey>
parents:
168
diff
changeset
|
20 * blocks are used on a 512-byte NAND page. |
|
fcfa82492193
Updated headers so they really are the same
wookey <wookey>
parents:
168
diff
changeset
|
21 * |
|
fcfa82492193
Updated headers so they really are the same
wookey <wookey>
parents:
168
diff
changeset
|
22 */ |
| 0 | 23 |
| 78 | 24 /* Table generated by gen-ecc.c |
| 25 * Using a table means we do not have to calculate p1..p4 and p1'..p4' | |
| 26 * for each byte of data. These are instead provided in a table in bits7..2. | |
| 27 * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore | |
| 28 * this bytes influence on the line parity. | |
| 29 */ | |
| 0 | 30 |
|
40
53c4d1987a84
Replace linux/config.h with yportenv.h and remove some dead code
charles <charles>
parents:
30
diff
changeset
|
31 #include "yportenv.h" |
| 0 | 32 |
| 33 #include "yaffs_ecc.h" | |
| 34 | |
| 35 static const unsigned char column_parity_table[] = { | |
| 75 | 36 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69, |
| 37 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00, | |
| 38 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc, | |
| 39 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95, | |
| 40 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0, | |
| 41 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99, | |
| 42 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65, | |
| 43 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c, | |
| 44 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc, | |
| 45 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5, | |
| 46 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59, | |
| 47 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30, | |
| 48 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55, | |
| 49 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c, | |
| 50 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0, | |
| 51 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9, | |
| 52 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0, | |
| 53 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9, | |
| 54 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55, | |
| 55 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c, | |
| 56 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59, | |
| 57 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30, | |
| 58 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc, | |
| 59 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5, | |
| 60 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65, | |
| 61 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c, | |
| 62 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0, | |
| 63 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99, | |
| 64 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc, | |
| 65 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95, | |
| 66 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69, | |
| 67 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00, | |
| 0 | 68 }; |
| 69 | |
| 78 | 70 /* Count the bits in an unsigned char or a U32 */ |
| 71 | |
| 0 | 72 static int yaffs_CountBits(unsigned char x) |
| 73 { | |
| 74 int r = 0; | |
| 75 | 75 while (x) { |
| 76 if (x & 1) | |
| 77 r++; | |
| 0 | 78 x >>= 1; |
| 79 } | |
| 80 return r; | |
| 81 } | |
| 82 | |
| 75 | 83 static int yaffs_CountBits32(unsigned x) |
| 0 | 84 { |
| 85 int r = 0; | |
| 75 | 86 while (x) { |
| 87 if (x & 1) | |
| 88 r++; | |
| 0 | 89 x >>= 1; |
| 90 } | |
| 91 return r; | |
| 92 } | |
| 93 | |
| 78 | 94 /* Calculate the ECC for a 256-byte block of data */ |
| 75 | 95 void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc) |
| 0 | 96 { |
| 97 unsigned int i; | |
| 75 | 98 |
| 0 | 99 unsigned char col_parity = 0; |
| 100 unsigned char line_parity = 0; | |
| 101 unsigned char line_parity_prime = 0; | |
| 102 unsigned char t; | |
| 103 unsigned char b; | |
| 75 | 104 |
| 105 for (i = 0; i < 256; i++) { | |
| 0 | 106 b = column_parity_table[*data++]; |
| 107 col_parity ^= b; | |
| 108 | |
|
271
c0dfae9e8073
Major whitespace/style changes to match Linux checkpatch.pl code style
wookey <wookey>
parents:
198
diff
changeset
|
109 if (b & 0x01) { /* odd number of bits in the byte */ |
| 0 | 110 line_parity ^= i; |
| 111 line_parity_prime ^= ~i; | |
| 112 } | |
| 113 } | |
| 75 | 114 |
| 0 | 115 ecc[2] = (~col_parity) | 0x03; |
| 75 | 116 |
| 0 | 117 t = 0; |
| 75 | 118 if (line_parity & 0x80) |
| 119 t |= 0x80; | |
| 120 if (line_parity_prime & 0x80) | |
| 121 t |= 0x40; | |
| 122 if (line_parity & 0x40) | |
| 123 t |= 0x20; | |
| 124 if (line_parity_prime & 0x40) | |
| 125 t |= 0x10; | |
| 126 if (line_parity & 0x20) | |
| 127 t |= 0x08; | |
| 128 if (line_parity_prime & 0x20) | |
| 129 t |= 0x04; | |
| 130 if (line_parity & 0x10) | |
| 131 t |= 0x02; | |
| 132 if (line_parity_prime & 0x10) | |
| 133 t |= 0x01; | |
| 0 | 134 ecc[1] = ~t; |
| 75 | 135 |
| 0 | 136 t = 0; |
| 75 | 137 if (line_parity & 0x08) |
| 138 t |= 0x80; | |
| 139 if (line_parity_prime & 0x08) | |
| 140 t |= 0x40; | |
| 141 if (line_parity & 0x04) | |
| 142 t |= 0x20; | |
| 143 if (line_parity_prime & 0x04) | |
| 144 t |= 0x10; | |
| 145 if (line_parity & 0x02) | |
| 146 t |= 0x08; | |
| 147 if (line_parity_prime & 0x02) | |
| 148 t |= 0x04; | |
| 149 if (line_parity & 0x01) | |
| 150 t |= 0x02; | |
| 151 if (line_parity_prime & 0x01) | |
| 152 t |= 0x01; | |
| 0 | 153 ecc[0] = ~t; |
| 154 | |
| 155 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER | |
|
271
c0dfae9e8073
Major whitespace/style changes to match Linux checkpatch.pl code style
wookey <wookey>
parents:
198
diff
changeset
|
156 /* Swap the bytes into the wrong order */ |
| 0 | 157 t = ecc[0]; |
| 158 ecc[0] = ecc[1]; | |
| 159 ecc[1] = t; | |
| 75 | 160 #endif |
| 0 | 161 } |
| 162 | |
| 78 | 163 |
| 164 /* Correct the ECC on a 256 byte block of data */ | |
| 165 | |
| 75 | 166 int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc, |
| 167 const unsigned char *test_ecc) | |
| 0 | 168 { |
| 78 | 169 unsigned char d0, d1, d2; /* deltas */ |
| 0 | 170 |
| 171 d0 = read_ecc[0] ^ test_ecc[0]; | |
| 172 d1 = read_ecc[1] ^ test_ecc[1]; | |
| 173 d2 = read_ecc[2] ^ test_ecc[2]; | |
| 75 | 174 |
| 78 | 175 if ((d0 | d1 | d2) == 0) |
| 176 return 0; /* no error */ | |
| 75 | 177 |
| 178 if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 && | |
| 0 | 179 ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 && |
| 75 | 180 ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) { |
| 78 | 181 /* Single bit (recoverable) error in data */ |
| 0 | 182 |
| 183 unsigned byte; | |
| 184 unsigned bit; | |
| 185 | |
| 186 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER | |
|
271
c0dfae9e8073
Major whitespace/style changes to match Linux checkpatch.pl code style
wookey <wookey>
parents:
198
diff
changeset
|
187 /* swap the bytes to correct for the wrong order */ |
| 0 | 188 unsigned char t; |
| 75 | 189 |
| 0 | 190 t = d0; |
| 191 d0 = d1; | |
| 192 d1 = t; | |
| 193 #endif | |
| 75 | 194 |
| 0 | 195 bit = byte = 0; |
| 196 | |
| 75 | 197 if (d1 & 0x80) |
| 198 byte |= 0x80; | |
| 199 if (d1 & 0x20) | |
| 200 byte |= 0x40; | |
| 201 if (d1 & 0x08) | |
| 202 byte |= 0x20; | |
| 203 if (d1 & 0x02) | |
| 204 byte |= 0x10; | |
| 205 if (d0 & 0x80) | |
| 206 byte |= 0x08; | |
| 207 if (d0 & 0x20) | |
| 208 byte |= 0x04; | |
| 209 if (d0 & 0x08) | |
| 210 byte |= 0x02; | |
| 211 if (d0 & 0x02) | |
| 212 byte |= 0x01; | |
| 213 | |
| 214 if (d2 & 0x80) | |
| 215 bit |= 0x04; | |
| 216 if (d2 & 0x20) | |
| 217 bit |= 0x02; | |
| 218 if (d2 & 0x08) | |
| 219 bit |= 0x01; | |
| 0 | 220 |
| 221 data[byte] ^= (1 << bit); | |
| 75 | 222 |
| 78 | 223 return 1; /* Corrected the error */ |
| 0 | 224 } |
| 75 | 225 |
|
198
b7f785925166
Cleanup patch - Remove all trailing whitespace and fix a few typos.
wookey <wookey>
parents:
171
diff
changeset
|
226 if ((yaffs_CountBits(d0) + |
|
b7f785925166
Cleanup patch - Remove all trailing whitespace and fix a few typos.
wookey <wookey>
parents:
171
diff
changeset
|
227 yaffs_CountBits(d1) + |
| 78 | 228 yaffs_CountBits(d2)) == 1) { |
| 229 /* Reccoverable error in ecc */ | |
| 75 | 230 |
| 0 | 231 read_ecc[0] = test_ecc[0]; |
| 232 read_ecc[1] = test_ecc[1]; | |
| 233 read_ecc[2] = test_ecc[2]; | |
| 75 | 234 |
| 78 | 235 return 1; /* Corrected the error */ |
| 0 | 236 } |
|
198
b7f785925166
Cleanup patch - Remove all trailing whitespace and fix a few typos.
wookey <wookey>
parents:
171
diff
changeset
|
237 |
| 78 | 238 /* Unrecoverable error */ |
| 75 | 239 |
| 0 | 240 return -1; |
| 241 | |
| 242 } | |
| 243 | |
| 78 | 244 |
| 245 /* | |
| 246 * ECCxxxOther does ECC calcs on arbitrary n bytes of data | |
| 247 */ | |
| 75 | 248 void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes, |
|
271
c0dfae9e8073
Major whitespace/style changes to match Linux checkpatch.pl code style
wookey <wookey>
parents:
198
diff
changeset
|
249 yaffs_ECCOther *eccOther) |
| 0 | 250 { |
| 251 unsigned int i; | |
| 75 | 252 |
| 0 | 253 unsigned char col_parity = 0; |
| 254 unsigned line_parity = 0; | |
| 255 unsigned line_parity_prime = 0; | |
| 256 unsigned char b; | |
| 75 | 257 |
| 258 for (i = 0; i < nBytes; i++) { | |
| 0 | 259 b = column_parity_table[*data++]; |
| 260 col_parity ^= b; | |
| 261 | |
| 78 | 262 if (b & 0x01) { |
| 263 /* odd number of bits in the byte */ | |
| 0 | 264 line_parity ^= i; |
| 265 line_parity_prime ^= ~i; | |
| 266 } | |
| 75 | 267 |
| 0 | 268 } |
| 75 | 269 |
| 0 | 270 eccOther->colParity = (col_parity >> 2) & 0x3f; |
| 271 eccOther->lineParity = line_parity; | |
| 272 eccOther->lineParityPrime = line_parity_prime; | |
| 273 } | |
| 274 | |
| 75 | 275 int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes, |
|
271
c0dfae9e8073
Major whitespace/style changes to match Linux checkpatch.pl code style
wookey <wookey>
parents:
198
diff
changeset
|
276 yaffs_ECCOther *read_ecc, |
|
c0dfae9e8073
Major whitespace/style changes to match Linux checkpatch.pl code style
wookey <wookey>
parents:
198
diff
changeset
|
277 const yaffs_ECCOther *test_ecc) |
| 0 | 278 { |
| 78 | 279 unsigned char cDelta; /* column parity delta */ |
| 280 unsigned lDelta; /* line parity delta */ | |
| 281 unsigned lDeltaPrime; /* line parity delta */ | |
| 0 | 282 unsigned bit; |
| 283 | |
| 284 cDelta = read_ecc->colParity ^ test_ecc->colParity; | |
| 75 | 285 lDelta = read_ecc->lineParity ^ test_ecc->lineParity; |
| 0 | 286 lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime; |
| 75 | 287 |
| 78 | 288 if ((cDelta | lDelta | lDeltaPrime) == 0) |
| 289 return 0; /* no error */ | |
| 75 | 290 |
|
198
b7f785925166
Cleanup patch - Remove all trailing whitespace and fix a few typos.
wookey <wookey>
parents:
171
diff
changeset
|
291 if (lDelta == ~lDeltaPrime && |
|
271
c0dfae9e8073
Major whitespace/style changes to match Linux checkpatch.pl code style
wookey <wookey>
parents:
198
diff
changeset
|
292 (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15)) { |
| 78 | 293 /* Single bit (recoverable) error in data */ |
| 0 | 294 |
| 295 bit = 0; | |
| 296 | |
| 75 | 297 if (cDelta & 0x20) |
| 298 bit |= 0x04; | |
| 299 if (cDelta & 0x08) | |
| 300 bit |= 0x02; | |
| 301 if (cDelta & 0x02) | |
| 302 bit |= 0x01; | |
| 0 | 303 |
|
271
c0dfae9e8073
Major whitespace/style changes to match Linux checkpatch.pl code style
wookey <wookey>
parents:
198
diff
changeset
|
304 if (lDelta >= nBytes) |
| 141 | 305 return -1; |
|
198
b7f785925166
Cleanup patch - Remove all trailing whitespace and fix a few typos.
wookey <wookey>
parents:
171
diff
changeset
|
306 |
| 0 | 307 data[lDelta] ^= (1 << bit); |
| 75 | 308 |
| 78 | 309 return 1; /* corrected */ |
| 0 | 310 } |
| 75 | 311 |
| 312 if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) + | |
|
271
c0dfae9e8073
Major whitespace/style changes to match Linux checkpatch.pl code style
wookey <wookey>
parents:
198
diff
changeset
|
313 yaffs_CountBits(cDelta)) == 1) { |
| 78 | 314 /* Reccoverable error in ecc */ |
| 75 | 315 |
| 316 *read_ecc = *test_ecc; | |
| 78 | 317 return 1; /* corrected */ |
| 0 | 318 } |
| 78 | 319 |
| 320 /* Unrecoverable error */ | |
| 75 | 321 |
| 0 | 322 return -1; |
| 323 } |
