ar71xx: add preliminary support for the TL-WR841ND v5 board
[openwrt.git] / tools / firmware-utils / src / nand_ecc.c
1 /*\r
2  * calculate ecc code for nand flash\r
3  *\r
4  * Copyright (C) 2008 yajin <yajin@vm-kernel.org>\r
5  * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>\r
6  *\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
11  *\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
16  *\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
20  * MA 02111-1307 USA\r
21  */\r
22 \r
23 \r
24 #include <sys/types.h>\r
25 #include <sys/stat.h>\r
26 #include <unistd.h>\r
27 #include <stdlib.h>\r
28 #include <stdint.h>\r
29 #include <fcntl.h>\r
30 #include <stdio.h>\r
31 \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
35 \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
39 \r
40 /*\r
41  * Pre-calculated 256-way 1 byte column parity\r
42  */\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
60 };\r
61 \r
62 /**\r
63  * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block\r
64  * @dat:        raw data\r
65  * @ecc_code:   buffer for ECC\r
66  */\r
67 int nand_calculate_ecc(const uint8_t *dat,\r
68                        uint8_t *ecc_code)\r
69 {\r
70         uint8_t idx, reg1, reg2, reg3, tmp1, tmp2;\r
71         int i;\r
72 \r
73         /* Initialize variables */\r
74         reg1 = reg2 = reg3 = 0;\r
75 \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
81 \r
82                 /* All bit XOR = 1 ? */\r
83                 if (idx & 0x40) {\r
84                         reg3 ^= (uint8_t) i;\r
85                         reg2 ^= ~((uint8_t) i);\r
86                 }\r
87         }\r
88 \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
98 \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
107 \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
112 #else\r
113         ecc_code[0] = ~tmp1;\r
114         ecc_code[1] = ~tmp2;\r
115 #endif\r
116         ecc_code[2] = ((~reg1) << 2) | 0x03;\r
117 \r
118         return 0;\r
119 }\r
120 \r
121 /*\r
122  *  usage: bb-nandflash-ecc    start_address  size\r
123  */\r
124 void usage(const char *prog)\r
125 {\r
126         fprintf(stderr, "Usage: %s [options] <input> <output>\n"\r
127                 "Options:\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
133         exit(1);\r
134 }\r
135 \r
136 /*start_address/size does not include oob\r
137   */\r
138 int main(int argc, char **argv)\r
139 {\r
140         uint8_t *page_data = NULL;\r
141         uint8_t *ecc_data;\r
142         int infd = -1, outfd = -1;\r
143         int ret = 1;\r
144         ssize_t bytes;\r
145         int ch;\r
146 \r
147         while ((ch = getopt(argc, argv, "e:o:p:")) != -1) {\r
148                 switch(ch) {\r
149                 case 'p':\r
150                         page_size = strtoul(optarg, NULL, 0);\r
151                         break;\r
152                 case 'o':\r
153                         oob_size = strtoul(optarg, NULL, 0);\r
154                         break;\r
155                 case 'e':\r
156                         ecc_offset = strtoul(optarg, NULL, 0);\r
157                         break;\r
158                 default:\r
159                         usage(argv[0]);\r
160                 }\r
161         }\r
162         argc -= optind;\r
163         if (argc < 2)\r
164                 usage(argv[0]);\r
165 \r
166         argv += optind;\r
167 \r
168         infd = open(argv[0], O_RDONLY, 0);\r
169         if (infd < 0) {\r
170                 perror("open input file");\r
171                 goto out;\r
172         }\r
173 \r
174         outfd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);\r
175         if (outfd < 0) {\r
176                 perror("open output file");\r
177                 goto out;\r
178         }\r
179 \r
180         page_data = malloc(page_size + oob_size);\r
181 \r
182         while ((bytes = read(infd, page_data, page_size)) == page_size) {\r
183                 int j;\r
184 \r
185                 ecc_data = page_data + page_size + ecc_offset;\r
186                 for (j = 0; j < page_size / 256; j++)\r
187                 {\r
188                         nand_calculate_ecc(page_data + j * 256, ecc_data);\r
189                         ecc_data += 3;\r
190                 }\r
191                 write(outfd, page_data, page_size + oob_size);\r
192         }\r
193 \r
194         ret = 0;\r
195 out:\r
196         if (infd >= 0)\r
197                 close(infd);\r
198         if (outfd >= 0)\r
199                 close(outfd);\r
200         if (page_data)\r
201                 free(page_data);\r
202         return ret;\r
203 }\r
204 \r