xref: /rk3399_rockchip-uboot/drivers/mtd/nand/raw/nand_ecc.c (revision cfcc706c901d603707657919484e4f65467be9ff)
1*cfcc706cSMiquel Raynal /*
2*cfcc706cSMiquel Raynal  * This file contains an ECC algorithm from Toshiba that detects and
3*cfcc706cSMiquel Raynal  * corrects 1 bit errors in a 256 byte block of data.
4*cfcc706cSMiquel Raynal  *
5*cfcc706cSMiquel Raynal  * drivers/mtd/nand/raw/nand_ecc.c
6*cfcc706cSMiquel Raynal  *
7*cfcc706cSMiquel Raynal  * Copyright (C) 2000-2004 Steven J. Hill (sjhill@realitydiluted.com)
8*cfcc706cSMiquel Raynal  *                         Toshiba America Electronics Components, Inc.
9*cfcc706cSMiquel Raynal  *
10*cfcc706cSMiquel Raynal  * Copyright (C) 2006 Thomas Gleixner <tglx@linutronix.de>
11*cfcc706cSMiquel Raynal  *
12*cfcc706cSMiquel Raynal  * SPDX-License-Identifier:	GPL-2.0+
13*cfcc706cSMiquel Raynal  *
14*cfcc706cSMiquel Raynal  * As a special exception, if other files instantiate templates or use
15*cfcc706cSMiquel Raynal  * macros or inline functions from these files, or you compile these
16*cfcc706cSMiquel Raynal  * files and link them with other works to produce a work based on these
17*cfcc706cSMiquel Raynal  * files, these files do not by themselves cause the resulting work to be
18*cfcc706cSMiquel Raynal  * covered by the GNU General Public License. However the source code for
19*cfcc706cSMiquel Raynal  * these files must still be made available in accordance with section (3)
20*cfcc706cSMiquel Raynal  * of the GNU General Public License.
21*cfcc706cSMiquel Raynal  *
22*cfcc706cSMiquel Raynal  * This exception does not invalidate any other reasons why a work based on
23*cfcc706cSMiquel Raynal  * this file might be covered by the GNU General Public License.
24*cfcc706cSMiquel Raynal  */
25*cfcc706cSMiquel Raynal 
26*cfcc706cSMiquel Raynal #include <common.h>
27*cfcc706cSMiquel Raynal 
28*cfcc706cSMiquel Raynal #include <linux/errno.h>
29*cfcc706cSMiquel Raynal #include <linux/mtd/mtd.h>
30*cfcc706cSMiquel Raynal #include <linux/mtd/nand_ecc.h>
31*cfcc706cSMiquel Raynal 
32*cfcc706cSMiquel Raynal /*
33*cfcc706cSMiquel Raynal  * NAND-SPL has no sofware ECC for now, so don't include nand_calculate_ecc(),
34*cfcc706cSMiquel Raynal  * only nand_correct_data() is needed
35*cfcc706cSMiquel Raynal  */
36*cfcc706cSMiquel Raynal 
37*cfcc706cSMiquel Raynal #if !defined(CONFIG_NAND_SPL) || defined(CONFIG_SPL_NAND_SOFTECC)
38*cfcc706cSMiquel Raynal /*
39*cfcc706cSMiquel Raynal  * Pre-calculated 256-way 1 byte column parity
40*cfcc706cSMiquel Raynal  */
41*cfcc706cSMiquel Raynal static const u_char nand_ecc_precalc_table[] = {
42*cfcc706cSMiquel Raynal 	0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
43*cfcc706cSMiquel Raynal 	0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
44*cfcc706cSMiquel Raynal 	0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
45*cfcc706cSMiquel Raynal 	0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
46*cfcc706cSMiquel Raynal 	0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
47*cfcc706cSMiquel Raynal 	0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
48*cfcc706cSMiquel Raynal 	0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
49*cfcc706cSMiquel Raynal 	0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
50*cfcc706cSMiquel Raynal 	0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
51*cfcc706cSMiquel Raynal 	0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
52*cfcc706cSMiquel Raynal 	0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
53*cfcc706cSMiquel Raynal 	0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
54*cfcc706cSMiquel Raynal 	0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
55*cfcc706cSMiquel Raynal 	0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
56*cfcc706cSMiquel Raynal 	0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
57*cfcc706cSMiquel Raynal 	0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
58*cfcc706cSMiquel Raynal };
59*cfcc706cSMiquel Raynal 
60*cfcc706cSMiquel Raynal /**
61*cfcc706cSMiquel Raynal  * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block
62*cfcc706cSMiquel Raynal  * @mtd:	MTD block structure
63*cfcc706cSMiquel Raynal  * @dat:	raw data
64*cfcc706cSMiquel Raynal  * @ecc_code:	buffer for ECC
65*cfcc706cSMiquel Raynal  */
nand_calculate_ecc(struct mtd_info * mtd,const u_char * dat,u_char * ecc_code)66*cfcc706cSMiquel Raynal int nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
67*cfcc706cSMiquel Raynal 		       u_char *ecc_code)
68*cfcc706cSMiquel Raynal {
69*cfcc706cSMiquel Raynal 	uint8_t idx, reg1, reg2, reg3, tmp1, tmp2;
70*cfcc706cSMiquel Raynal 	int i;
71*cfcc706cSMiquel Raynal 
72*cfcc706cSMiquel Raynal 	/* Initialize variables */
73*cfcc706cSMiquel Raynal 	reg1 = reg2 = reg3 = 0;
74*cfcc706cSMiquel Raynal 
75*cfcc706cSMiquel Raynal 	/* Build up column parity */
76*cfcc706cSMiquel Raynal 	for(i = 0; i < 256; i++) {
77*cfcc706cSMiquel Raynal 		/* Get CP0 - CP5 from table */
78*cfcc706cSMiquel Raynal 		idx = nand_ecc_precalc_table[*dat++];
79*cfcc706cSMiquel Raynal 		reg1 ^= (idx & 0x3f);
80*cfcc706cSMiquel Raynal 
81*cfcc706cSMiquel Raynal 		/* All bit XOR = 1 ? */
82*cfcc706cSMiquel Raynal 		if (idx & 0x40) {
83*cfcc706cSMiquel Raynal 			reg3 ^= (uint8_t) i;
84*cfcc706cSMiquel Raynal 			reg2 ^= ~((uint8_t) i);
85*cfcc706cSMiquel Raynal 		}
86*cfcc706cSMiquel Raynal 	}
87*cfcc706cSMiquel Raynal 
88*cfcc706cSMiquel Raynal 	/* Create non-inverted ECC code from line parity */
89*cfcc706cSMiquel Raynal 	tmp1  = (reg3 & 0x80) >> 0; /* B7 -> B7 */
90*cfcc706cSMiquel Raynal 	tmp1 |= (reg2 & 0x80) >> 1; /* B7 -> B6 */
91*cfcc706cSMiquel Raynal 	tmp1 |= (reg3 & 0x40) >> 1; /* B6 -> B5 */
92*cfcc706cSMiquel Raynal 	tmp1 |= (reg2 & 0x40) >> 2; /* B6 -> B4 */
93*cfcc706cSMiquel Raynal 	tmp1 |= (reg3 & 0x20) >> 2; /* B5 -> B3 */
94*cfcc706cSMiquel Raynal 	tmp1 |= (reg2 & 0x20) >> 3; /* B5 -> B2 */
95*cfcc706cSMiquel Raynal 	tmp1 |= (reg3 & 0x10) >> 3; /* B4 -> B1 */
96*cfcc706cSMiquel Raynal 	tmp1 |= (reg2 & 0x10) >> 4; /* B4 -> B0 */
97*cfcc706cSMiquel Raynal 
98*cfcc706cSMiquel Raynal 	tmp2  = (reg3 & 0x08) << 4; /* B3 -> B7 */
99*cfcc706cSMiquel Raynal 	tmp2 |= (reg2 & 0x08) << 3; /* B3 -> B6 */
100*cfcc706cSMiquel Raynal 	tmp2 |= (reg3 & 0x04) << 3; /* B2 -> B5 */
101*cfcc706cSMiquel Raynal 	tmp2 |= (reg2 & 0x04) << 2; /* B2 -> B4 */
102*cfcc706cSMiquel Raynal 	tmp2 |= (reg3 & 0x02) << 2; /* B1 -> B3 */
103*cfcc706cSMiquel Raynal 	tmp2 |= (reg2 & 0x02) << 1; /* B1 -> B2 */
104*cfcc706cSMiquel Raynal 	tmp2 |= (reg3 & 0x01) << 1; /* B0 -> B1 */
105*cfcc706cSMiquel Raynal 	tmp2 |= (reg2 & 0x01) << 0; /* B7 -> B0 */
106*cfcc706cSMiquel Raynal 
107*cfcc706cSMiquel Raynal 	/* Calculate final ECC code */
108*cfcc706cSMiquel Raynal 	ecc_code[0] = ~tmp1;
109*cfcc706cSMiquel Raynal 	ecc_code[1] = ~tmp2;
110*cfcc706cSMiquel Raynal 	ecc_code[2] = ((~reg1) << 2) | 0x03;
111*cfcc706cSMiquel Raynal 
112*cfcc706cSMiquel Raynal 	return 0;
113*cfcc706cSMiquel Raynal }
114*cfcc706cSMiquel Raynal #endif /* CONFIG_NAND_SPL */
115*cfcc706cSMiquel Raynal 
countbits(uint32_t byte)116*cfcc706cSMiquel Raynal static inline int countbits(uint32_t byte)
117*cfcc706cSMiquel Raynal {
118*cfcc706cSMiquel Raynal 	int res = 0;
119*cfcc706cSMiquel Raynal 
120*cfcc706cSMiquel Raynal 	for (;byte; byte >>= 1)
121*cfcc706cSMiquel Raynal 		res += byte & 0x01;
122*cfcc706cSMiquel Raynal 	return res;
123*cfcc706cSMiquel Raynal }
124*cfcc706cSMiquel Raynal 
125*cfcc706cSMiquel Raynal /**
126*cfcc706cSMiquel Raynal  * nand_correct_data - [NAND Interface] Detect and correct bit error(s)
127*cfcc706cSMiquel Raynal  * @mtd:	MTD block structure
128*cfcc706cSMiquel Raynal  * @dat:	raw data read from the chip
129*cfcc706cSMiquel Raynal  * @read_ecc:	ECC from the chip
130*cfcc706cSMiquel Raynal  * @calc_ecc:	the ECC calculated from raw data
131*cfcc706cSMiquel Raynal  *
132*cfcc706cSMiquel Raynal  * Detect and correct a 1 bit error for 256 byte block
133*cfcc706cSMiquel Raynal  */
nand_correct_data(struct mtd_info * mtd,u_char * dat,u_char * read_ecc,u_char * calc_ecc)134*cfcc706cSMiquel Raynal int nand_correct_data(struct mtd_info *mtd, u_char *dat,
135*cfcc706cSMiquel Raynal 		      u_char *read_ecc, u_char *calc_ecc)
136*cfcc706cSMiquel Raynal {
137*cfcc706cSMiquel Raynal 	uint8_t s0, s1, s2;
138*cfcc706cSMiquel Raynal 
139*cfcc706cSMiquel Raynal 	s1 = calc_ecc[0] ^ read_ecc[0];
140*cfcc706cSMiquel Raynal 	s0 = calc_ecc[1] ^ read_ecc[1];
141*cfcc706cSMiquel Raynal 	s2 = calc_ecc[2] ^ read_ecc[2];
142*cfcc706cSMiquel Raynal 	if ((s0 | s1 | s2) == 0)
143*cfcc706cSMiquel Raynal 		return 0;
144*cfcc706cSMiquel Raynal 
145*cfcc706cSMiquel Raynal 	/* Check for a single bit error */
146*cfcc706cSMiquel Raynal 	if( ((s0 ^ (s0 >> 1)) & 0x55) == 0x55 &&
147*cfcc706cSMiquel Raynal 	    ((s1 ^ (s1 >> 1)) & 0x55) == 0x55 &&
148*cfcc706cSMiquel Raynal 	    ((s2 ^ (s2 >> 1)) & 0x54) == 0x54) {
149*cfcc706cSMiquel Raynal 
150*cfcc706cSMiquel Raynal 		uint32_t byteoffs, bitnum;
151*cfcc706cSMiquel Raynal 
152*cfcc706cSMiquel Raynal 		byteoffs = (s1 << 0) & 0x80;
153*cfcc706cSMiquel Raynal 		byteoffs |= (s1 << 1) & 0x40;
154*cfcc706cSMiquel Raynal 		byteoffs |= (s1 << 2) & 0x20;
155*cfcc706cSMiquel Raynal 		byteoffs |= (s1 << 3) & 0x10;
156*cfcc706cSMiquel Raynal 
157*cfcc706cSMiquel Raynal 		byteoffs |= (s0 >> 4) & 0x08;
158*cfcc706cSMiquel Raynal 		byteoffs |= (s0 >> 3) & 0x04;
159*cfcc706cSMiquel Raynal 		byteoffs |= (s0 >> 2) & 0x02;
160*cfcc706cSMiquel Raynal 		byteoffs |= (s0 >> 1) & 0x01;
161*cfcc706cSMiquel Raynal 
162*cfcc706cSMiquel Raynal 		bitnum = (s2 >> 5) & 0x04;
163*cfcc706cSMiquel Raynal 		bitnum |= (s2 >> 4) & 0x02;
164*cfcc706cSMiquel Raynal 		bitnum |= (s2 >> 3) & 0x01;
165*cfcc706cSMiquel Raynal 
166*cfcc706cSMiquel Raynal 		dat[byteoffs] ^= (1 << bitnum);
167*cfcc706cSMiquel Raynal 
168*cfcc706cSMiquel Raynal 		return 1;
169*cfcc706cSMiquel Raynal 	}
170*cfcc706cSMiquel Raynal 
171*cfcc706cSMiquel Raynal 	if(countbits(s0 | ((uint32_t)s1 << 8) | ((uint32_t)s2 <<16)) == 1)
172*cfcc706cSMiquel Raynal 		return 1;
173*cfcc706cSMiquel Raynal 
174*cfcc706cSMiquel Raynal 	return -EBADMSG;
175*cfcc706cSMiquel Raynal }
176