2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
4 * yaffs_ecc.c: ECC generation/correction algorithms.
6 * Copyright (C) 2002 Aleph One Ltd.
8 * Created by Charles Manning <charles@aleph1.co.uk>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * version 2.1 as published by the Free Software Foundation.
17 * This code implements the ECC algorithm used in SmartMedia.
19 * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
20 * The two unused bit are set to 1.
21 * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
22 * blocks are used on a 512-byte NAND page.
26 /* Table generated by gen-ecc.c
27 * Using a table means we do not have to calculate p1..p4 and p1'..p4'
28 * for each byte of data. These are instead provided in a table in bits7..2.
29 * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
30 * this bytes influence on the line parity.
33 const char *yaffs_ecc_c_version =
34 "$Id: yaffs_ecc.c,v 1.6 2005-08-11 02:51:49 charles Exp $";
38 #include "yaffs_ecc.h"
40 static const unsigned char column_parity_table[] = {
41 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
42 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
43 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
44 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
45 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
46 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
47 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
48 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
49 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
50 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
51 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
52 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
53 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
54 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
55 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
56 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
57 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
58 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
59 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
60 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
61 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
62 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
63 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
64 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
65 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
66 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
67 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
68 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
69 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
70 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
71 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
72 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
75 /* Count the bits in an unsigned char or a U32 */
77 static int yaffs_CountBits(unsigned char x)
88 static int yaffs_CountBits32(unsigned x)
99 /* Calculate the ECC for a 256-byte block of data */
100 void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
104 unsigned char col_parity = 0;
105 unsigned char line_parity = 0;
106 unsigned char line_parity_prime = 0;
110 for (i = 0; i < 256; i++) {
111 b = column_parity_table[*data++];
114 if (b & 0x01) // odd number of bits in the byte
117 line_parity_prime ^= ~i;
122 ecc[2] = (~col_parity) | 0x03;
125 if (line_parity & 0x80)
127 if (line_parity_prime & 0x80)
129 if (line_parity & 0x40)
131 if (line_parity_prime & 0x40)
133 if (line_parity & 0x20)
135 if (line_parity_prime & 0x20)
137 if (line_parity & 0x10)
139 if (line_parity_prime & 0x10)
144 if (line_parity & 0x08)
146 if (line_parity_prime & 0x08)
148 if (line_parity & 0x04)
150 if (line_parity_prime & 0x04)
152 if (line_parity & 0x02)
154 if (line_parity_prime & 0x02)
156 if (line_parity & 0x01)
158 if (line_parity_prime & 0x01)
162 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
163 // Swap the bytes into the wrong order
171 /* Correct the ECC on a 256 byte block of data */
173 int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
174 const unsigned char *test_ecc)
176 unsigned char d0, d1, d2; /* deltas */
178 d0 = read_ecc[0] ^ test_ecc[0];
179 d1 = read_ecc[1] ^ test_ecc[1];
180 d2 = read_ecc[2] ^ test_ecc[2];
182 if ((d0 | d1 | d2) == 0)
183 return 0; /* no error */
185 if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
186 ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
187 ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
188 /* Single bit (recoverable) error in data */
193 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
194 // swap the bytes to correct for the wrong order
228 data[byte] ^= (1 << bit);
230 return 1; /* Corrected the error */
233 if ((yaffs_CountBits(d0) +
234 yaffs_CountBits(d1) +
235 yaffs_CountBits(d2)) == 1) {
236 /* Reccoverable error in ecc */
238 read_ecc[0] = test_ecc[0];
239 read_ecc[1] = test_ecc[1];
240 read_ecc[2] = test_ecc[2];
242 return 1; /* Corrected the error */
245 /* Unrecoverable error */
253 * ECCxxxOther does ECC calcs on arbitrary n bytes of data
255 void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
256 yaffs_ECCOther * eccOther)
260 unsigned char col_parity = 0;
261 unsigned line_parity = 0;
262 unsigned line_parity_prime = 0;
265 for (i = 0; i < nBytes; i++) {
266 b = column_parity_table[*data++];
270 /* odd number of bits in the byte */
272 line_parity_prime ^= ~i;
277 eccOther->colParity = (col_parity >> 2) & 0x3f;
278 eccOther->lineParity = line_parity;
279 eccOther->lineParityPrime = line_parity_prime;
282 int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
283 yaffs_ECCOther * read_ecc,
284 const yaffs_ECCOther * test_ecc)
286 unsigned char cDelta; /* column parity delta */
287 unsigned lDelta; /* line parity delta */
288 unsigned lDeltaPrime; /* line parity delta */
291 cDelta = read_ecc->colParity ^ test_ecc->colParity;
292 lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
293 lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
295 if ((cDelta | lDelta | lDeltaPrime) == 0)
296 return 0; /* no error */
298 if (lDelta == ~lDeltaPrime && (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15))
300 /* Single bit (recoverable) error in data */
311 data[lDelta] ^= (1 << bit);
313 return 1; /* corrected */
316 if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
317 yaffs_CountBits(cDelta)) == 1) {
318 /* Reccoverable error in ecc */
320 *read_ecc = *test_ecc;
321 return 1; /* corrected */
324 /* Unrecoverable error */