yaffs Added some more tests to direct/timothy_tests/mirror_tests
[yaffs2.git] / yaffs_ecc.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
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.
12  */
13
14 /*
15  * This code implements the ECC algorithm used in SmartMedia.
16  *
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.
21  *
22  */
23
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  */
30
31 #include "yportenv.h"
32
33 #include "yaffs_ecc.h"
34
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,
68 };
69
70
71 /* Calculate the ECC for a 256-byte block of data */
72 void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc)
73 {
74         unsigned int i;
75
76         unsigned char col_parity = 0;
77         unsigned char line_parity = 0;
78         unsigned char line_parity_prime = 0;
79         unsigned char t;
80         unsigned char b;
81
82         for (i = 0; i < 256; i++) {
83                 b = column_parity_table[*data++];
84                 col_parity ^= b;
85
86                 if (b & 0x01) { /* odd number of bits in the byte */
87                         line_parity ^= i;
88                         line_parity_prime ^= ~i;
89                 }
90         }
91
92         ecc[2] = (~col_parity) | 0x03;
93
94         t = 0;
95         if (line_parity & 0x80)
96                 t |= 0x80;
97         if (line_parity_prime & 0x80)
98                 t |= 0x40;
99         if (line_parity & 0x40)
100                 t |= 0x20;
101         if (line_parity_prime & 0x40)
102                 t |= 0x10;
103         if (line_parity & 0x20)
104                 t |= 0x08;
105         if (line_parity_prime & 0x20)
106                 t |= 0x04;
107         if (line_parity & 0x10)
108                 t |= 0x02;
109         if (line_parity_prime & 0x10)
110                 t |= 0x01;
111         ecc[1] = ~t;
112
113         t = 0;
114         if (line_parity & 0x08)
115                 t |= 0x80;
116         if (line_parity_prime & 0x08)
117                 t |= 0x40;
118         if (line_parity & 0x04)
119                 t |= 0x20;
120         if (line_parity_prime & 0x04)
121                 t |= 0x10;
122         if (line_parity & 0x02)
123                 t |= 0x08;
124         if (line_parity_prime & 0x02)
125                 t |= 0x04;
126         if (line_parity & 0x01)
127                 t |= 0x02;
128         if (line_parity_prime & 0x01)
129                 t |= 0x01;
130         ecc[0] = ~t;
131
132 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
133         /* Swap the bytes into the wrong order */
134         t = ecc[0];
135         ecc[0] = ecc[1];
136         ecc[1] = t;
137 #endif
138 }
139
140 /* Correct the ECC on a 256 byte block of data */
141
142 int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
143                       const unsigned char *test_ecc)
144 {
145         unsigned char d0, d1, d2;       /* deltas */
146
147         d0 = read_ecc[0] ^ test_ecc[0];
148         d1 = read_ecc[1] ^ test_ecc[1];
149         d2 = read_ecc[2] ^ test_ecc[2];
150
151         if ((d0 | d1 | d2) == 0)
152                 return 0;       /* no error */
153
154         if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
155             ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
156             ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
157                 /* Single bit (recoverable) error in data */
158
159                 unsigned byte;
160                 unsigned bit;
161
162 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
163                 /* swap the bytes to correct for the wrong order */
164                 unsigned char t;
165
166                 t = d0;
167                 d0 = d1;
168                 d1 = t;
169 #endif
170
171                 bit = byte = 0;
172
173                 if (d1 & 0x80)
174                         byte |= 0x80;
175                 if (d1 & 0x20)
176                         byte |= 0x40;
177                 if (d1 & 0x08)
178                         byte |= 0x20;
179                 if (d1 & 0x02)
180                         byte |= 0x10;
181                 if (d0 & 0x80)
182                         byte |= 0x08;
183                 if (d0 & 0x20)
184                         byte |= 0x04;
185                 if (d0 & 0x08)
186                         byte |= 0x02;
187                 if (d0 & 0x02)
188                         byte |= 0x01;
189
190                 if (d2 & 0x80)
191                         bit |= 0x04;
192                 if (d2 & 0x20)
193                         bit |= 0x02;
194                 if (d2 & 0x08)
195                         bit |= 0x01;
196
197                 data[byte] ^= (1 << bit);
198
199                 return 1;       /* Corrected the error */
200         }
201
202         if ((hweight8(d0) + hweight8(d1) + hweight8(d2)) == 1) {
203                 /* Reccoverable error in ecc */
204
205                 read_ecc[0] = test_ecc[0];
206                 read_ecc[1] = test_ecc[1];
207                 read_ecc[2] = test_ecc[2];
208
209                 return 1;       /* Corrected the error */
210         }
211
212         /* Unrecoverable error */
213
214         return -1;
215
216 }
217
218 /*
219  * ECCxxxOther does ECC calcs on arbitrary n bytes of data
220  */
221 void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
222                           struct yaffs_ecc_other *ecc_other)
223 {
224         unsigned int i;
225
226         unsigned char col_parity = 0;
227         unsigned line_parity = 0;
228         unsigned line_parity_prime = 0;
229         unsigned char b;
230
231         for (i = 0; i < n_bytes; i++) {
232                 b = column_parity_table[*data++];
233                 col_parity ^= b;
234
235                 if (b & 0x01) {
236                         /* odd number of bits in the byte */
237                         line_parity ^= i;
238                         line_parity_prime ^= ~i;
239                 }
240
241         }
242
243         ecc_other->col_parity = (col_parity >> 2) & 0x3f;
244         ecc_other->line_parity = line_parity;
245         ecc_other->line_parity_prime = line_parity_prime;
246 }
247
248 int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
249                             struct yaffs_ecc_other *read_ecc,
250                             const struct yaffs_ecc_other *test_ecc)
251 {
252         unsigned char delta_col;        /* column parity delta */
253         unsigned delta_line;    /* line parity delta */
254         unsigned delta_line_prime;      /* line parity delta */
255         unsigned bit;
256
257         delta_col = read_ecc->col_parity ^ test_ecc->col_parity;
258         delta_line = read_ecc->line_parity ^ test_ecc->line_parity;
259         delta_line_prime =
260             read_ecc->line_parity_prime ^ test_ecc->line_parity_prime;
261
262         if ((delta_col | delta_line | delta_line_prime) == 0)
263                 return 0;       /* no error */
264
265         if (delta_line == ~delta_line_prime &&
266             (((delta_col ^ (delta_col >> 1)) & 0x15) == 0x15)) {
267                 /* Single bit (recoverable) error in data */
268
269                 bit = 0;
270
271                 if (delta_col & 0x20)
272                         bit |= 0x04;
273                 if (delta_col & 0x08)
274                         bit |= 0x02;
275                 if (delta_col & 0x02)
276                         bit |= 0x01;
277
278                 if (delta_line >= n_bytes)
279                         return -1;
280
281                 data[delta_line] ^= (1 << bit);
282
283                 return 1;       /* corrected */
284         }
285
286         if ((hweight32(delta_line) +
287              hweight32(delta_line_prime) +
288              hweight8(delta_col)) == 1) {
289                 /* Reccoverable error in ecc */
290
291                 *read_ecc = *test_ecc;
292                 return 1;       /* corrected */
293         }
294
295         /* Unrecoverable error */
296
297         return -1;
298 }