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