xref: /OK3568_Linux_fs/u-boot/fs/yaffs2/yaffs_ecc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2002-2011 Aleph One Ltd.
5*4882a593Smuzhiyun  *   for Toby Churchill Ltd and Brightstar Engineering
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Created by Charles Manning <charles@aleph1.co.uk>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun  * it under the terms of the GNU General Public License version 2 as
11*4882a593Smuzhiyun  * published by the Free Software Foundation.
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /*
15*4882a593Smuzhiyun  * This code implements the ECC algorithm used in SmartMedia.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
18*4882a593Smuzhiyun  * The two unused bit are set to 1.
19*4882a593Smuzhiyun  * The ECC can correct single bit errors in a 256-byte page of data. Thus, two
20*4882a593Smuzhiyun  * such ECC blocks are used on a 512-byte NAND page.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  */
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #include "yportenv.h"
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #include "yaffs_ecc.h"
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /* Table generated by gen-ecc.c
29*4882a593Smuzhiyun  * Using a table means we do not have to calculate p1..p4 and p1'..p4'
30*4882a593Smuzhiyun  * for each byte of data. These are instead provided in a table in bits7..2.
31*4882a593Smuzhiyun  * Bit 0 of each entry indicates whether the entry has an odd or even parity,
32*4882a593Smuzhiyun  * and therefore this bytes influence on the line parity.
33*4882a593Smuzhiyun  */
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun static const unsigned char column_parity_table[] = {
36*4882a593Smuzhiyun 	0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
37*4882a593Smuzhiyun 	0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
38*4882a593Smuzhiyun 	0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
39*4882a593Smuzhiyun 	0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
40*4882a593Smuzhiyun 	0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
41*4882a593Smuzhiyun 	0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
42*4882a593Smuzhiyun 	0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
43*4882a593Smuzhiyun 	0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
44*4882a593Smuzhiyun 	0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
45*4882a593Smuzhiyun 	0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
46*4882a593Smuzhiyun 	0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
47*4882a593Smuzhiyun 	0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
48*4882a593Smuzhiyun 	0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
49*4882a593Smuzhiyun 	0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
50*4882a593Smuzhiyun 	0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
51*4882a593Smuzhiyun 	0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
52*4882a593Smuzhiyun 	0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
53*4882a593Smuzhiyun 	0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
54*4882a593Smuzhiyun 	0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
55*4882a593Smuzhiyun 	0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
56*4882a593Smuzhiyun 	0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
57*4882a593Smuzhiyun 	0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
58*4882a593Smuzhiyun 	0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
59*4882a593Smuzhiyun 	0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
60*4882a593Smuzhiyun 	0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
61*4882a593Smuzhiyun 	0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
62*4882a593Smuzhiyun 	0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
63*4882a593Smuzhiyun 	0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
64*4882a593Smuzhiyun 	0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
65*4882a593Smuzhiyun 	0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
66*4882a593Smuzhiyun 	0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
67*4882a593Smuzhiyun 	0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
68*4882a593Smuzhiyun };
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /* Calculate the ECC for a 256-byte block of data */
yaffs_ecc_calc(const unsigned char * data,unsigned char * ecc)72*4882a593Smuzhiyun void yaffs_ecc_calc(const unsigned char *data, unsigned char *ecc)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	unsigned int i;
75*4882a593Smuzhiyun 	unsigned char col_parity = 0;
76*4882a593Smuzhiyun 	unsigned char line_parity = 0;
77*4882a593Smuzhiyun 	unsigned char line_parity_prime = 0;
78*4882a593Smuzhiyun 	unsigned char t;
79*4882a593Smuzhiyun 	unsigned char b;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	for (i = 0; i < 256; i++) {
82*4882a593Smuzhiyun 		b = column_parity_table[*data++];
83*4882a593Smuzhiyun 		col_parity ^= b;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 		if (b & 0x01) {	/* odd number of bits in the byte */
86*4882a593Smuzhiyun 			line_parity ^= i;
87*4882a593Smuzhiyun 			line_parity_prime ^= ~i;
88*4882a593Smuzhiyun 		}
89*4882a593Smuzhiyun 	}
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	ecc[2] = (~col_parity) | 0x03;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	t = 0;
94*4882a593Smuzhiyun 	if (line_parity & 0x80)
95*4882a593Smuzhiyun 		t |= 0x80;
96*4882a593Smuzhiyun 	if (line_parity_prime & 0x80)
97*4882a593Smuzhiyun 		t |= 0x40;
98*4882a593Smuzhiyun 	if (line_parity & 0x40)
99*4882a593Smuzhiyun 		t |= 0x20;
100*4882a593Smuzhiyun 	if (line_parity_prime & 0x40)
101*4882a593Smuzhiyun 		t |= 0x10;
102*4882a593Smuzhiyun 	if (line_parity & 0x20)
103*4882a593Smuzhiyun 		t |= 0x08;
104*4882a593Smuzhiyun 	if (line_parity_prime & 0x20)
105*4882a593Smuzhiyun 		t |= 0x04;
106*4882a593Smuzhiyun 	if (line_parity & 0x10)
107*4882a593Smuzhiyun 		t |= 0x02;
108*4882a593Smuzhiyun 	if (line_parity_prime & 0x10)
109*4882a593Smuzhiyun 		t |= 0x01;
110*4882a593Smuzhiyun 	ecc[1] = ~t;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	t = 0;
113*4882a593Smuzhiyun 	if (line_parity & 0x08)
114*4882a593Smuzhiyun 		t |= 0x80;
115*4882a593Smuzhiyun 	if (line_parity_prime & 0x08)
116*4882a593Smuzhiyun 		t |= 0x40;
117*4882a593Smuzhiyun 	if (line_parity & 0x04)
118*4882a593Smuzhiyun 		t |= 0x20;
119*4882a593Smuzhiyun 	if (line_parity_prime & 0x04)
120*4882a593Smuzhiyun 		t |= 0x10;
121*4882a593Smuzhiyun 	if (line_parity & 0x02)
122*4882a593Smuzhiyun 		t |= 0x08;
123*4882a593Smuzhiyun 	if (line_parity_prime & 0x02)
124*4882a593Smuzhiyun 		t |= 0x04;
125*4882a593Smuzhiyun 	if (line_parity & 0x01)
126*4882a593Smuzhiyun 		t |= 0x02;
127*4882a593Smuzhiyun 	if (line_parity_prime & 0x01)
128*4882a593Smuzhiyun 		t |= 0x01;
129*4882a593Smuzhiyun 	ecc[0] = ~t;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun /* Correct the ECC on a 256 byte block of data */
134*4882a593Smuzhiyun 
yaffs_ecc_correct(unsigned char * data,unsigned char * read_ecc,const unsigned char * test_ecc)135*4882a593Smuzhiyun int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
136*4882a593Smuzhiyun 		      const unsigned char *test_ecc)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	unsigned char d0, d1, d2;	/* deltas */
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	d0 = read_ecc[0] ^ test_ecc[0];
141*4882a593Smuzhiyun 	d1 = read_ecc[1] ^ test_ecc[1];
142*4882a593Smuzhiyun 	d2 = read_ecc[2] ^ test_ecc[2];
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	if ((d0 | d1 | d2) == 0)
145*4882a593Smuzhiyun 		return 0;	/* no error */
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
148*4882a593Smuzhiyun 	    ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
149*4882a593Smuzhiyun 	    ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
150*4882a593Smuzhiyun 		/* Single bit (recoverable) error in data */
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 		unsigned byte;
153*4882a593Smuzhiyun 		unsigned bit;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 		bit = byte = 0;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 		if (d1 & 0x80)
158*4882a593Smuzhiyun 			byte |= 0x80;
159*4882a593Smuzhiyun 		if (d1 & 0x20)
160*4882a593Smuzhiyun 			byte |= 0x40;
161*4882a593Smuzhiyun 		if (d1 & 0x08)
162*4882a593Smuzhiyun 			byte |= 0x20;
163*4882a593Smuzhiyun 		if (d1 & 0x02)
164*4882a593Smuzhiyun 			byte |= 0x10;
165*4882a593Smuzhiyun 		if (d0 & 0x80)
166*4882a593Smuzhiyun 			byte |= 0x08;
167*4882a593Smuzhiyun 		if (d0 & 0x20)
168*4882a593Smuzhiyun 			byte |= 0x04;
169*4882a593Smuzhiyun 		if (d0 & 0x08)
170*4882a593Smuzhiyun 			byte |= 0x02;
171*4882a593Smuzhiyun 		if (d0 & 0x02)
172*4882a593Smuzhiyun 			byte |= 0x01;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 		if (d2 & 0x80)
175*4882a593Smuzhiyun 			bit |= 0x04;
176*4882a593Smuzhiyun 		if (d2 & 0x20)
177*4882a593Smuzhiyun 			bit |= 0x02;
178*4882a593Smuzhiyun 		if (d2 & 0x08)
179*4882a593Smuzhiyun 			bit |= 0x01;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 		data[byte] ^= (1 << bit);
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 		return 1;	/* Corrected the error */
184*4882a593Smuzhiyun 	}
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	if ((hweight8(d0) + hweight8(d1) + hweight8(d2)) == 1) {
187*4882a593Smuzhiyun 		/* Reccoverable error in ecc */
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 		read_ecc[0] = test_ecc[0];
190*4882a593Smuzhiyun 		read_ecc[1] = test_ecc[1];
191*4882a593Smuzhiyun 		read_ecc[2] = test_ecc[2];
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 		return 1;	/* Corrected the error */
194*4882a593Smuzhiyun 	}
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	/* Unrecoverable error */
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	return -1;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun /*
203*4882a593Smuzhiyun  * ECCxxxOther does ECC calcs on arbitrary n bytes of data
204*4882a593Smuzhiyun  */
yaffs_ecc_calc_other(const unsigned char * data,unsigned n_bytes,struct yaffs_ecc_other * ecc_other)205*4882a593Smuzhiyun void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
206*4882a593Smuzhiyun 			  struct yaffs_ecc_other *ecc_other)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun 	unsigned int i;
209*4882a593Smuzhiyun 	unsigned char col_parity = 0;
210*4882a593Smuzhiyun 	unsigned line_parity = 0;
211*4882a593Smuzhiyun 	unsigned line_parity_prime = 0;
212*4882a593Smuzhiyun 	unsigned char b;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	for (i = 0; i < n_bytes; i++) {
215*4882a593Smuzhiyun 		b = column_parity_table[*data++];
216*4882a593Smuzhiyun 		col_parity ^= b;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 		if (b & 0x01) {
219*4882a593Smuzhiyun 			/* odd number of bits in the byte */
220*4882a593Smuzhiyun 			line_parity ^= i;
221*4882a593Smuzhiyun 			line_parity_prime ^= ~i;
222*4882a593Smuzhiyun 		}
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	}
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	ecc_other->col_parity = (col_parity >> 2) & 0x3f;
227*4882a593Smuzhiyun 	ecc_other->line_parity = line_parity;
228*4882a593Smuzhiyun 	ecc_other->line_parity_prime = line_parity_prime;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun 
yaffs_ecc_correct_other(unsigned char * data,unsigned n_bytes,struct yaffs_ecc_other * read_ecc,const struct yaffs_ecc_other * test_ecc)231*4882a593Smuzhiyun int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
232*4882a593Smuzhiyun 			    struct yaffs_ecc_other *read_ecc,
233*4882a593Smuzhiyun 			    const struct yaffs_ecc_other *test_ecc)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun 	unsigned char delta_col;	/* column parity delta */
236*4882a593Smuzhiyun 	unsigned delta_line;	/* line parity delta */
237*4882a593Smuzhiyun 	unsigned delta_line_prime;	/* line parity delta */
238*4882a593Smuzhiyun 	unsigned bit;
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	delta_col = read_ecc->col_parity ^ test_ecc->col_parity;
241*4882a593Smuzhiyun 	delta_line = read_ecc->line_parity ^ test_ecc->line_parity;
242*4882a593Smuzhiyun 	delta_line_prime =
243*4882a593Smuzhiyun 	    read_ecc->line_parity_prime ^ test_ecc->line_parity_prime;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	if ((delta_col | delta_line | delta_line_prime) == 0)
246*4882a593Smuzhiyun 		return 0;	/* no error */
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	if (delta_line == ~delta_line_prime &&
249*4882a593Smuzhiyun 	    (((delta_col ^ (delta_col >> 1)) & 0x15) == 0x15)) {
250*4882a593Smuzhiyun 		/* Single bit (recoverable) error in data */
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 		bit = 0;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 		if (delta_col & 0x20)
255*4882a593Smuzhiyun 			bit |= 0x04;
256*4882a593Smuzhiyun 		if (delta_col & 0x08)
257*4882a593Smuzhiyun 			bit |= 0x02;
258*4882a593Smuzhiyun 		if (delta_col & 0x02)
259*4882a593Smuzhiyun 			bit |= 0x01;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 		if (delta_line >= n_bytes)
262*4882a593Smuzhiyun 			return -1;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 		data[delta_line] ^= (1 << bit);
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 		return 1;	/* corrected */
267*4882a593Smuzhiyun 	}
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	if ((hweight32(delta_line) +
270*4882a593Smuzhiyun 	     hweight32(delta_line_prime) +
271*4882a593Smuzhiyun 	     hweight8(delta_col)) == 1) {
272*4882a593Smuzhiyun 		/* Reccoverable error in ecc */
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 		*read_ecc = *test_ecc;
275*4882a593Smuzhiyun 		return 1;	/* corrected */
276*4882a593Smuzhiyun 	}
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	/* Unrecoverable error */
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	return -1;
281*4882a593Smuzhiyun }
282