2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 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.
15 * This code implements the ECC algorithm used in SmartMedia.
17 * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
18 * The two unused bit are set to 1.
19 * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
20 * blocks are used on a 512-byte NAND page.
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.
33 #include "yaffs_ecc.h"
35 static const unsigned char column_parity_table[] = {
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,
70 /* Count the bits in an unsigned char or a U32 */
72 static int yaffs_count_bits(unsigned char x)
83 static int yaffs_count_bits32(unsigned x)
94 /* Calculate the ECC for a 256-byte block of data */
95 void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc)
99 unsigned char col_parity = 0;
100 unsigned char line_parity = 0;
101 unsigned char line_parity_prime = 0;
105 for (i = 0; i < 256; i++) {
106 b = column_parity_table[*data++];
109 if (b & 0x01) { /* odd number of bits in the byte */
111 line_parity_prime ^= ~i;
115 ecc[2] = (~col_parity) | 0x03;
118 if (line_parity & 0x80)
120 if (line_parity_prime & 0x80)
122 if (line_parity & 0x40)
124 if (line_parity_prime & 0x40)
126 if (line_parity & 0x20)
128 if (line_parity_prime & 0x20)
130 if (line_parity & 0x10)
132 if (line_parity_prime & 0x10)
137 if (line_parity & 0x08)
139 if (line_parity_prime & 0x08)
141 if (line_parity & 0x04)
143 if (line_parity_prime & 0x04)
145 if (line_parity & 0x02)
147 if (line_parity_prime & 0x02)
149 if (line_parity & 0x01)
151 if (line_parity_prime & 0x01)
155 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
156 /* Swap the bytes into the wrong order */
164 /* Correct the ECC on a 256 byte block of data */
166 int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
167 const unsigned char *test_ecc)
169 unsigned char d0, d1, d2; /* deltas */
171 d0 = read_ecc[0] ^ test_ecc[0];
172 d1 = read_ecc[1] ^ test_ecc[1];
173 d2 = read_ecc[2] ^ test_ecc[2];
175 if ((d0 | d1 | d2) == 0)
176 return 0; /* no error */
178 if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
179 ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
180 ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
181 /* Single bit (recoverable) error in data */
186 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
187 /* swap the bytes to correct for the wrong order */
221 data[byte] ^= (1 << bit);
223 return 1; /* Corrected the error */
226 if ((yaffs_count_bits(d0) +
227 yaffs_count_bits(d1) +
228 yaffs_count_bits(d2)) == 1) {
229 /* Reccoverable error in ecc */
231 read_ecc[0] = test_ecc[0];
232 read_ecc[1] = test_ecc[1];
233 read_ecc[2] = test_ecc[2];
235 return 1; /* Corrected the error */
238 /* Unrecoverable error */
246 * ECCxxxOther does ECC calcs on arbitrary n bytes of data
248 void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
249 yaffs_ECCOther *eccOther)
253 unsigned char col_parity = 0;
254 unsigned line_parity = 0;
255 unsigned line_parity_prime = 0;
258 for (i = 0; i < n_bytes; i++) {
259 b = column_parity_table[*data++];
263 /* odd number of bits in the byte */
265 line_parity_prime ^= ~i;
270 eccOther->colParity = (col_parity >> 2) & 0x3f;
271 eccOther->lineParity = line_parity;
272 eccOther->lineParityPrime = line_parity_prime;
275 int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
276 yaffs_ECCOther *read_ecc,
277 const yaffs_ECCOther *test_ecc)
279 unsigned char cDelta; /* column parity delta */
280 unsigned lDelta; /* line parity delta */
281 unsigned lDeltaPrime; /* line parity delta */
284 cDelta = read_ecc->colParity ^ test_ecc->colParity;
285 lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
286 lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
288 if ((cDelta | lDelta | lDeltaPrime) == 0)
289 return 0; /* no error */
291 if (lDelta == ~lDeltaPrime &&
292 (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15)) {
293 /* Single bit (recoverable) error in data */
304 if (lDelta >= n_bytes)
307 data[lDelta] ^= (1 << bit);
309 return 1; /* corrected */
312 if ((yaffs_count_bits32(lDelta) + yaffs_count_bits32(lDeltaPrime) +
313 yaffs_count_bits(cDelta)) == 1) {
314 /* Reccoverable error in ecc */
316 *read_ecc = *test_ecc;
317 return 1; /* corrected */
320 /* Unrecoverable error */