2 * calculate ecc code for nand flash
\r
4 * Copyright (C) 2008 yajin <yajin@vm-kernel.org>
\r
5 * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
\r
7 * This program is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License as
\r
9 * published by the Free Software Foundation; either version 2 or
\r
10 * (at your option) version 3 of the License.
\r
12 * This program is distributed in the hope that it will be useful,
\r
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 * GNU General Public License for more details.
\r
17 * You should have received a copy of the GNU General Public License
\r
18 * along with this program; if not, write to the Free Software
\r
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
\r
24 #include <sys/types.h>
\r
25 #include <sys/stat.h>
\r
32 #define DEF_NAND_PAGE_SIZE 2048
\r
33 #define DEF_NAND_OOB_SIZE 64
\r
34 #define DEF_NAND_ECC_OFFSET 0x28
\r
36 static int page_size = DEF_NAND_PAGE_SIZE;
\r
37 static int oob_size = DEF_NAND_OOB_SIZE;
\r
38 static int ecc_offset = DEF_NAND_ECC_OFFSET;
\r
41 * Pre-calculated 256-way 1 byte column parity
\r
43 static const uint8_t nand_ecc_precalc_table[] = {
\r
44 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
\r
45 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
\r
46 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
\r
47 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
\r
48 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
\r
49 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
\r
50 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
\r
51 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
\r
52 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
\r
53 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
\r
54 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
\r
55 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
\r
56 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
\r
57 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
\r
58 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
\r
59 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
\r
63 * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block
\r
65 * @ecc_code: buffer for ECC
\r
67 int nand_calculate_ecc(const uint8_t *dat,
\r
70 uint8_t idx, reg1, reg2, reg3, tmp1, tmp2;
\r
73 /* Initialize variables */
\r
74 reg1 = reg2 = reg3 = 0;
\r
76 /* Build up column parity */
\r
77 for(i = 0; i < 256; i++) {
\r
78 /* Get CP0 - CP5 from table */
\r
79 idx = nand_ecc_precalc_table[*dat++];
\r
80 reg1 ^= (idx & 0x3f);
\r
82 /* All bit XOR = 1 ? */
\r
84 reg3 ^= (uint8_t) i;
\r
85 reg2 ^= ~((uint8_t) i);
\r
89 /* Create non-inverted ECC code from line parity */
\r
90 tmp1 = (reg3 & 0x80) >> 0; /* B7 -> B7 */
\r
91 tmp1 |= (reg2 & 0x80) >> 1; /* B7 -> B6 */
\r
92 tmp1 |= (reg3 & 0x40) >> 1; /* B6 -> B5 */
\r
93 tmp1 |= (reg2 & 0x40) >> 2; /* B6 -> B4 */
\r
94 tmp1 |= (reg3 & 0x20) >> 2; /* B5 -> B3 */
\r
95 tmp1 |= (reg2 & 0x20) >> 3; /* B5 -> B2 */
\r
96 tmp1 |= (reg3 & 0x10) >> 3; /* B4 -> B1 */
\r
97 tmp1 |= (reg2 & 0x10) >> 4; /* B4 -> B0 */
\r
99 tmp2 = (reg3 & 0x08) << 4; /* B3 -> B7 */
\r
100 tmp2 |= (reg2 & 0x08) << 3; /* B3 -> B6 */
\r
101 tmp2 |= (reg3 & 0x04) << 3; /* B2 -> B5 */
\r
102 tmp2 |= (reg2 & 0x04) << 2; /* B2 -> B4 */
\r
103 tmp2 |= (reg3 & 0x02) << 2; /* B1 -> B3 */
\r
104 tmp2 |= (reg2 & 0x02) << 1; /* B1 -> B2 */
\r
105 tmp2 |= (reg3 & 0x01) << 1; /* B0 -> B1 */
\r
106 tmp2 |= (reg2 & 0x01) << 0; /* B7 -> B0 */
\r
108 /* Calculate final ECC code */
\r
109 #ifdef CONFIG_MTD_NAND_ECC_SMC
\r
110 ecc_code[0] = ~tmp2;
\r
111 ecc_code[1] = ~tmp1;
\r
113 ecc_code[0] = ~tmp1;
\r
114 ecc_code[1] = ~tmp2;
\r
116 ecc_code[2] = ((~reg1) << 2) | 0x03;
\r
122 * usage: bb-nandflash-ecc start_address size
\r
124 void usage(const char *prog)
\r
126 fprintf(stderr, "Usage: %s [options] <input> <output>\n"
\r
128 " -p <pagesize> NAND page size (default: %d)\n"
\r
129 " -o <oobsize> NAND OOB size (default: %d)\n"
\r
130 " -e <offset> NAND ECC offset (default: %d)\n"
\r
131 "\n", prog, DEF_NAND_PAGE_SIZE, DEF_NAND_OOB_SIZE,
\r
132 DEF_NAND_ECC_OFFSET);
\r
136 /*start_address/size does not include oob
\r
138 int main(int argc, char **argv)
\r
140 uint8_t *page_data = NULL;
\r
142 int infd = -1, outfd = -1;
\r
147 while ((ch = getopt(argc, argv, "e:o:p:")) != -1) {
\r
150 page_size = strtoul(optarg, NULL, 0);
\r
153 oob_size = strtoul(optarg, NULL, 0);
\r
156 ecc_offset = strtoul(optarg, NULL, 0);
\r
168 infd = open(argv[0], O_RDONLY, 0);
\r
170 perror("open input file");
\r
174 outfd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);
\r
176 perror("open output file");
\r
180 page_data = malloc(page_size + oob_size);
\r
182 while ((bytes = read(infd, page_data, page_size)) == page_size) {
\r
185 ecc_data = page_data + page_size + ecc_offset;
\r
186 for (j = 0; j < page_size / 256; j++)
\r
188 nand_calculate_ecc(page_data + j * 256, ecc_data);
\r
191 write(outfd, page_data, page_size + oob_size);
\r