2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
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.
16 * This code implements the ECC algorithm used in SmartMedia.
18 * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
19 * The two unused bit are set to 1.
20 * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
21 * blocks are used on a 512-byte NAND page.
25 /* Table generated by gen-ecc.c
26 * Using a table means we do not have to calculate p1..p4 and p1'..p4'
27 * for each byte of data. These are instead provided in a table in bits7..2.
28 * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
29 * this bytes influence on the line parity.
32 const char *yaffs_ecc_c_version =
33 "$Id: yaffs_ecc.c,v 1.8 2007-02-12 16:55:25 wookey Exp $";
37 #include "yaffs_ecc.h"
39 static const unsigned char column_parity_table[] = {
40 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
41 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
42 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
43 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
44 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
45 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
46 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
47 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
48 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
49 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
50 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
51 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
52 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
53 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
54 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
55 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
56 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
57 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
58 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
59 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
60 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
61 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
62 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
63 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
64 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
65 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
66 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
67 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
68 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
69 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
70 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
71 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
74 /* Count the bits in an unsigned char or a U32 */
76 static int yaffs_CountBits(unsigned char x)
87 static int yaffs_CountBits32(unsigned x)
98 /* Calculate the ECC for a 256-byte block of data */
99 void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
103 unsigned char col_parity = 0;
104 unsigned char line_parity = 0;
105 unsigned char line_parity_prime = 0;
109 for (i = 0; i < 256; i++) {
110 b = column_parity_table[*data++];
113 if (b & 0x01) // odd number of bits in the byte
116 line_parity_prime ^= ~i;
121 ecc[2] = (~col_parity) | 0x03;
124 if (line_parity & 0x80)
126 if (line_parity_prime & 0x80)
128 if (line_parity & 0x40)
130 if (line_parity_prime & 0x40)
132 if (line_parity & 0x20)
134 if (line_parity_prime & 0x20)
136 if (line_parity & 0x10)
138 if (line_parity_prime & 0x10)
143 if (line_parity & 0x08)
145 if (line_parity_prime & 0x08)
147 if (line_parity & 0x04)
149 if (line_parity_prime & 0x04)
151 if (line_parity & 0x02)
153 if (line_parity_prime & 0x02)
155 if (line_parity & 0x01)
157 if (line_parity_prime & 0x01)
161 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
162 // Swap the bytes into the wrong order
170 /* Correct the ECC on a 256 byte block of data */
172 int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
173 const unsigned char *test_ecc)
175 unsigned char d0, d1, d2; /* deltas */
177 d0 = read_ecc[0] ^ test_ecc[0];
178 d1 = read_ecc[1] ^ test_ecc[1];
179 d2 = read_ecc[2] ^ test_ecc[2];
181 if ((d0 | d1 | d2) == 0)
182 return 0; /* no error */
184 if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
185 ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
186 ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
187 /* Single bit (recoverable) error in data */
192 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
193 // swap the bytes to correct for the wrong order
227 data[byte] ^= (1 << bit);
229 return 1; /* Corrected the error */
232 if ((yaffs_CountBits(d0) +
233 yaffs_CountBits(d1) +
234 yaffs_CountBits(d2)) == 1) {
235 /* Reccoverable error in ecc */
237 read_ecc[0] = test_ecc[0];
238 read_ecc[1] = test_ecc[1];
239 read_ecc[2] = test_ecc[2];
241 return 1; /* Corrected the error */
244 /* Unrecoverable error */
252 * ECCxxxOther does ECC calcs on arbitrary n bytes of data
254 void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
255 yaffs_ECCOther * eccOther)
259 unsigned char col_parity = 0;
260 unsigned line_parity = 0;
261 unsigned line_parity_prime = 0;
264 for (i = 0; i < nBytes; i++) {
265 b = column_parity_table[*data++];
269 /* odd number of bits in the byte */
271 line_parity_prime ^= ~i;
276 eccOther->colParity = (col_parity >> 2) & 0x3f;
277 eccOther->lineParity = line_parity;
278 eccOther->lineParityPrime = line_parity_prime;
281 int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
282 yaffs_ECCOther * read_ecc,
283 const yaffs_ECCOther * test_ecc)
285 unsigned char cDelta; /* column parity delta */
286 unsigned lDelta; /* line parity delta */
287 unsigned lDeltaPrime; /* line parity delta */
290 cDelta = read_ecc->colParity ^ test_ecc->colParity;
291 lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
292 lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
294 if ((cDelta | lDelta | lDeltaPrime) == 0)
295 return 0; /* no error */
297 if (lDelta == ~lDeltaPrime &&
298 (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15))
300 /* Single bit (recoverable) error in data */
314 data[lDelta] ^= (1 << bit);
316 return 1; /* corrected */
319 if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
320 yaffs_CountBits(cDelta)) == 1) {
321 /* Reccoverable error in ecc */
323 *read_ecc = *test_ecc;
324 return 1; /* corrected */
327 /* Unrecoverable error */