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