Mercurial > yaffs-ecoscentric-gpl
annotate yaffs_ecc.c @ 406:dc1a01765cbe
yaffs: Change trace flag for partially written blocks
This was YAFFS_TRACE_ALWAYS which generated a lot of pointless tracing.
Switched to YAFFS_TRACE_SCAN.
Signed-off-by: Charles Manning <cdhmanning@gmail.com>
| author | Charles Manning <cdhmanning@gmail.com> |
|---|---|
| date | Mon, 26 Apr 2010 11:20:03 +1200 |
| parents | c0dfae9e8073 |
| children | b5309c9b6d7f |
| rev | line source |
|---|---|
| 0 | 1 /* |
| 168 | 2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. |
| 0 | 3 * |
| 168 | 4 * Copyright (C) 2002-2007 Aleph One Ltd. |
| 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 |
| 75 | 31 const char *yaffs_ecc_c_version = |
|
271
c0dfae9e8073
Major whitespace/style changes to match Linux checkpatch.pl code style
wookey <wookey>
parents:
198
diff
changeset
|
32 "$Id: yaffs_ecc.c,v 1.11 2009-03-06 17:20:50 wookey Exp $"; |
| 0 | 33 |
|
40
53c4d1987a84
Replace linux/config.h with yportenv.h and remove some dead code
charles <charles>
parents:
30
diff
changeset
|
34 #include "yportenv.h" |
| 0 | 35 |
| 36 #include "yaffs_ecc.h" | |
| 37 | |
| 38 static const unsigned char column_parity_table[] = { | |
| 75 | 39 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69, |
| 40 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00, | |
| 41 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc, | |
| 42 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95, | |
| 43 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0, | |
| 44 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99, | |
| 45 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65, | |
| 46 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c, | |
| 47 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc, | |
| 48 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5, | |
| 49 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59, | |
| 50 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30, | |
| 51 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55, | |
| 52 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c, | |
| 53 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0, | |
| 54 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9, | |
| 55 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0, | |
| 56 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9, | |
| 57 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55, | |
| 58 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c, | |
| 59 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59, | |
| 60 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30, | |
| 61 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc, | |
| 62 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5, | |
| 63 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65, | |
| 64 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c, | |
| 65 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0, | |
| 66 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99, | |
| 67 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc, | |
| 68 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95, | |
| 69 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69, | |
| 70 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00, | |
| 0 | 71 }; |
| 72 | |
| 78 | 73 /* Count the bits in an unsigned char or a U32 */ |
| 74 | |
| 0 | 75 static int yaffs_CountBits(unsigned char x) |
| 76 { | |
| 77 int r = 0; | |
| 75 | 78 while (x) { |
| 79 if (x & 1) | |
| 80 r++; | |
| 0 | 81 x >>= 1; |
| 82 } | |
| 83 return r; | |
| 84 } | |
| 85 | |
| 75 | 86 static int yaffs_CountBits32(unsigned x) |
| 0 | 87 { |
| 88 int r = 0; | |
| 75 | 89 while (x) { |
| 90 if (x & 1) | |
| 91 r++; | |
| 0 | 92 x >>= 1; |
| 93 } | |
| 94 return r; | |
| 95 } | |
| 96 | |
| 78 | 97 /* Calculate the ECC for a 256-byte block of data */ |
| 75 | 98 void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc) |
| 0 | 99 { |
| 100 unsigned int i; | |
| 75 | 101 |
| 0 | 102 unsigned char col_parity = 0; |
| 103 unsigned char line_parity = 0; | |
| 104 unsigned char line_parity_prime = 0; | |
| 105 unsigned char t; | |
| 106 unsigned char b; | |
| 75 | 107 |
| 108 for (i = 0; i < 256; i++) { | |
| 0 | 109 b = column_parity_table[*data++]; |
| 110 col_parity ^= b; | |
| 111 | |
|
271
c0dfae9e8073
Major whitespace/style changes to match Linux checkpatch.pl code style
wookey <wookey>
parents:
198
diff
changeset
|
112 if (b & 0x01) { /* odd number of bits in the byte */ |
| 0 | 113 line_parity ^= i; |
| 114 line_parity_prime ^= ~i; | |
| 115 } | |
| 116 } | |
| 75 | 117 |
| 0 | 118 ecc[2] = (~col_parity) | 0x03; |
| 75 | 119 |
| 0 | 120 t = 0; |
| 75 | 121 if (line_parity & 0x80) |
| 122 t |= 0x80; | |
| 123 if (line_parity_prime & 0x80) | |
| 124 t |= 0x40; | |
| 125 if (line_parity & 0x40) | |
| 126 t |= 0x20; | |
| 127 if (line_parity_prime & 0x40) | |
| 128 t |= 0x10; | |
| 129 if (line_parity & 0x20) | |
| 130 t |= 0x08; | |
| 131 if (line_parity_prime & 0x20) | |
| 132 t |= 0x04; | |
| 133 if (line_parity & 0x10) | |
| 134 t |= 0x02; | |
| 135 if (line_parity_prime & 0x10) | |
| 136 t |= 0x01; | |
| 0 | 137 ecc[1] = ~t; |
| 75 | 138 |
| 0 | 139 t = 0; |
| 75 | 140 if (line_parity & 0x08) |
| 141 t |= 0x80; | |
| 142 if (line_parity_prime & 0x08) | |
| 143 t |= 0x40; | |
| 144 if (line_parity & 0x04) | |
| 145 t |= 0x20; | |
| 146 if (line_parity_prime & 0x04) | |
| 147 t |= 0x10; | |
| 148 if (line_parity & 0x02) | |
| 149 t |= 0x08; | |
| 150 if (line_parity_prime & 0x02) | |
| 151 t |= 0x04; | |
| 152 if (line_parity & 0x01) | |
| 153 t |= 0x02; | |
| 154 if (line_parity_prime & 0x01) | |
| 155 t |= 0x01; | |
| 0 | 156 ecc[0] = ~t; |
| 157 | |
| 158 #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
|
159 /* Swap the bytes into the wrong order */ |
| 0 | 160 t = ecc[0]; |
| 161 ecc[0] = ecc[1]; | |
| 162 ecc[1] = t; | |
| 75 | 163 #endif |
| 0 | 164 } |
| 165 | |
| 78 | 166 |
| 167 /* Correct the ECC on a 256 byte block of data */ | |
| 168 | |
| 75 | 169 int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc, |
| 170 const unsigned char *test_ecc) | |
| 0 | 171 { |
| 78 | 172 unsigned char d0, d1, d2; /* deltas */ |
| 0 | 173 |
| 174 d0 = read_ecc[0] ^ test_ecc[0]; | |
| 175 d1 = read_ecc[1] ^ test_ecc[1]; | |
| 176 d2 = read_ecc[2] ^ test_ecc[2]; | |
| 75 | 177 |
| 78 | 178 if ((d0 | d1 | d2) == 0) |
| 179 return 0; /* no error */ | |
| 75 | 180 |
| 181 if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 && | |
| 0 | 182 ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 && |
| 75 | 183 ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) { |
| 78 | 184 /* Single bit (recoverable) error in data */ |
| 0 | 185 |
| 186 unsigned byte; | |
| 187 unsigned bit; | |
| 188 | |
| 189 #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
|
190 /* swap the bytes to correct for the wrong order */ |
| 0 | 191 unsigned char t; |
| 75 | 192 |
| 0 | 193 t = d0; |
| 194 d0 = d1; | |
| 195 d1 = t; | |
| 196 #endif | |
| 75 | 197 |
| 0 | 198 bit = byte = 0; |
| 199 | |
| 75 | 200 if (d1 & 0x80) |
| 201 byte |= 0x80; | |
| 202 if (d1 & 0x20) | |
| 203 byte |= 0x40; | |
| 204 if (d1 & 0x08) | |
| 205 byte |= 0x20; | |
| 206 if (d1 & 0x02) | |
| 207 byte |= 0x10; | |
| 208 if (d0 & 0x80) | |
| 209 byte |= 0x08; | |
| 210 if (d0 & 0x20) | |
| 211 byte |= 0x04; | |
| 212 if (d0 & 0x08) | |
| 213 byte |= 0x02; | |
| 214 if (d0 & 0x02) | |
| 215 byte |= 0x01; | |
| 216 | |
| 217 if (d2 & 0x80) | |
| 218 bit |= 0x04; | |
| 219 if (d2 & 0x20) | |
| 220 bit |= 0x02; | |
| 221 if (d2 & 0x08) | |
| 222 bit |= 0x01; | |
| 0 | 223 |
| 224 data[byte] ^= (1 << bit); | |
| 75 | 225 |
| 78 | 226 return 1; /* Corrected the error */ |
| 0 | 227 } |
| 75 | 228 |
|
198
b7f785925166
Cleanup patch - Remove all trailing whitespace and fix a few typos.
wookey <wookey>
parents:
171
diff
changeset
|
229 if ((yaffs_CountBits(d0) + |
|
b7f785925166
Cleanup patch - Remove all trailing whitespace and fix a few typos.
wookey <wookey>
parents:
171
diff
changeset
|
230 yaffs_CountBits(d1) + |
| 78 | 231 yaffs_CountBits(d2)) == 1) { |
| 232 /* Reccoverable error in ecc */ | |
| 75 | 233 |
| 0 | 234 read_ecc[0] = test_ecc[0]; |
| 235 read_ecc[1] = test_ecc[1]; | |
| 236 read_ecc[2] = test_ecc[2]; | |
| 75 | 237 |
| 78 | 238 return 1; /* Corrected the error */ |
| 0 | 239 } |
|
198
b7f785925166
Cleanup patch - Remove all trailing whitespace and fix a few typos.
wookey <wookey>
parents:
171
diff
changeset
|
240 |
| 78 | 241 /* Unrecoverable error */ |
| 75 | 242 |
| 0 | 243 return -1; |
| 244 | |
| 245 } | |
| 246 | |
| 78 | 247 |
| 248 /* | |
| 249 * ECCxxxOther does ECC calcs on arbitrary n bytes of data | |
| 250 */ | |
| 75 | 251 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
|
252 yaffs_ECCOther *eccOther) |
| 0 | 253 { |
| 254 unsigned int i; | |
| 75 | 255 |
| 0 | 256 unsigned char col_parity = 0; |
| 257 unsigned line_parity = 0; | |
| 258 unsigned line_parity_prime = 0; | |
| 259 unsigned char b; | |
| 75 | 260 |
| 261 for (i = 0; i < nBytes; i++) { | |
| 0 | 262 b = column_parity_table[*data++]; |
| 263 col_parity ^= b; | |
| 264 | |
| 78 | 265 if (b & 0x01) { |
| 266 /* odd number of bits in the byte */ | |
| 0 | 267 line_parity ^= i; |
| 268 line_parity_prime ^= ~i; | |
| 269 } | |
| 75 | 270 |
| 0 | 271 } |
| 75 | 272 |
| 0 | 273 eccOther->colParity = (col_parity >> 2) & 0x3f; |
| 274 eccOther->lineParity = line_parity; | |
| 275 eccOther->lineParityPrime = line_parity_prime; | |
| 276 } | |
| 277 | |
| 75 | 278 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
|
279 yaffs_ECCOther *read_ecc, |
|
c0dfae9e8073
Major whitespace/style changes to match Linux checkpatch.pl code style
wookey <wookey>
parents:
198
diff
changeset
|
280 const yaffs_ECCOther *test_ecc) |
| 0 | 281 { |
| 78 | 282 unsigned char cDelta; /* column parity delta */ |
| 283 unsigned lDelta; /* line parity delta */ | |
| 284 unsigned lDeltaPrime; /* line parity delta */ | |
| 0 | 285 unsigned bit; |
| 286 | |
| 287 cDelta = read_ecc->colParity ^ test_ecc->colParity; | |
| 75 | 288 lDelta = read_ecc->lineParity ^ test_ecc->lineParity; |
| 0 | 289 lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime; |
| 75 | 290 |
| 78 | 291 if ((cDelta | lDelta | lDeltaPrime) == 0) |
| 292 return 0; /* no error */ | |
| 75 | 293 |
|
198
b7f785925166
Cleanup patch - Remove all trailing whitespace and fix a few typos.
wookey <wookey>
parents:
171
diff
changeset
|
294 if (lDelta == ~lDeltaPrime && |
|
271
c0dfae9e8073
Major whitespace/style changes to match Linux checkpatch.pl code style
wookey <wookey>
parents:
198
diff
changeset
|
295 (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15)) { |
| 78 | 296 /* Single bit (recoverable) error in data */ |
| 0 | 297 |
| 298 bit = 0; | |
| 299 | |
| 75 | 300 if (cDelta & 0x20) |
| 301 bit |= 0x04; | |
| 302 if (cDelta & 0x08) | |
| 303 bit |= 0x02; | |
| 304 if (cDelta & 0x02) | |
| 305 bit |= 0x01; | |
| 0 | 306 |
|
271
c0dfae9e8073
Major whitespace/style changes to match Linux checkpatch.pl code style
wookey <wookey>
parents:
198
diff
changeset
|
307 if (lDelta >= nBytes) |
| 141 | 308 return -1; |
|
198
b7f785925166
Cleanup patch - Remove all trailing whitespace and fix a few typos.
wookey <wookey>
parents:
171
diff
changeset
|
309 |
| 0 | 310 data[lDelta] ^= (1 << bit); |
| 75 | 311 |
| 78 | 312 return 1; /* corrected */ |
| 0 | 313 } |
| 75 | 314 |
| 315 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
|
316 yaffs_CountBits(cDelta)) == 1) { |
| 78 | 317 /* Reccoverable error in ecc */ |
| 75 | 318 |
| 319 *read_ecc = *test_ecc; | |
| 78 | 320 return 1; /* corrected */ |
| 0 | 321 } |
| 78 | 322 |
| 323 /* Unrecoverable error */ | |
| 75 | 324 |
| 0 | 325 return -1; |
| 326 } |
