1 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
3 * calculate ecc code for nand flash
5 * Copyright (C) 2008 yajin <yajin@vm-kernel.org>
6 * Copyright (C) 2009 Felix Fietkau <nbd@nbd.name>
10 #include <sys/types.h>
18 #define DEF_NAND_PAGE_SIZE 2048
19 #define DEF_NAND_OOB_SIZE 64
20 #define DEF_NAND_ECC_OFFSET 0x28
22 static int page_size
= DEF_NAND_PAGE_SIZE
;
23 static int oob_size
= DEF_NAND_OOB_SIZE
;
24 static int ecc_offset
= DEF_NAND_ECC_OFFSET
;
27 * Pre-calculated 256-way 1 byte column parity
29 static const uint8_t nand_ecc_precalc_table
[] = {
30 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
31 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
32 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
33 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
34 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
35 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
36 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
37 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
38 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
39 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
40 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
41 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
42 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
43 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
44 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
45 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
49 * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block
51 * @ecc_code: buffer for ECC
53 int nand_calculate_ecc(const uint8_t *dat
,
56 uint8_t idx
, reg1
, reg2
, reg3
, tmp1
, tmp2
;
59 /* Initialize variables */
60 reg1
= reg2
= reg3
= 0;
62 /* Build up column parity */
63 for(i
= 0; i
< 256; i
++) {
64 /* Get CP0 - CP5 from table */
65 idx
= nand_ecc_precalc_table
[*dat
++];
68 /* All bit XOR = 1 ? */
71 reg2
^= ~((uint8_t) i
);
75 /* Create non-inverted ECC code from line parity */
76 tmp1
= (reg3
& 0x80) >> 0; /* B7 -> B7 */
77 tmp1
|= (reg2
& 0x80) >> 1; /* B7 -> B6 */
78 tmp1
|= (reg3
& 0x40) >> 1; /* B6 -> B5 */
79 tmp1
|= (reg2
& 0x40) >> 2; /* B6 -> B4 */
80 tmp1
|= (reg3
& 0x20) >> 2; /* B5 -> B3 */
81 tmp1
|= (reg2
& 0x20) >> 3; /* B5 -> B2 */
82 tmp1
|= (reg3
& 0x10) >> 3; /* B4 -> B1 */
83 tmp1
|= (reg2
& 0x10) >> 4; /* B4 -> B0 */
85 tmp2
= (reg3
& 0x08) << 4; /* B3 -> B7 */
86 tmp2
|= (reg2
& 0x08) << 3; /* B3 -> B6 */
87 tmp2
|= (reg3
& 0x04) << 3; /* B2 -> B5 */
88 tmp2
|= (reg2
& 0x04) << 2; /* B2 -> B4 */
89 tmp2
|= (reg3
& 0x02) << 2; /* B1 -> B3 */
90 tmp2
|= (reg2
& 0x02) << 1; /* B1 -> B2 */
91 tmp2
|= (reg3
& 0x01) << 1; /* B0 -> B1 */
92 tmp2
|= (reg2
& 0x01) << 0; /* B7 -> B0 */
94 /* Calculate final ECC code */
95 #ifdef CONFIG_MTD_NAND_ECC_SMC
102 ecc_code
[2] = ((~reg1
) << 2) | 0x03;
108 * usage: bb-nandflash-ecc start_address size
110 void usage(const char *prog
)
112 fprintf(stderr
, "Usage: %s [options] <input> <output>\n"
114 " -p <pagesize> NAND page size (default: %d)\n"
115 " -o <oobsize> NAND OOB size (default: %d)\n"
116 " -e <offset> NAND ECC offset (default: %d)\n"
117 "\n", prog
, DEF_NAND_PAGE_SIZE
, DEF_NAND_OOB_SIZE
,
118 DEF_NAND_ECC_OFFSET
);
122 /*start_address/size does not include oob
124 int main(int argc
, char **argv
)
126 uint8_t *page_data
= NULL
;
128 int infd
= -1, outfd
= -1;
133 while ((ch
= getopt(argc
, argv
, "e:o:p:")) != -1) {
136 page_size
= strtoul(optarg
, NULL
, 0);
139 oob_size
= strtoul(optarg
, NULL
, 0);
142 ecc_offset
= strtoul(optarg
, NULL
, 0);
154 infd
= open(argv
[0], O_RDONLY
, 0);
156 perror("open input file");
160 outfd
= open(argv
[1], O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
162 perror("open output file");
166 page_data
= malloc(page_size
+ oob_size
);
168 while ((bytes
= read(infd
, page_data
, page_size
)) == page_size
) {
171 ecc_data
= page_data
+ page_size
+ ecc_offset
;
172 for (j
= 0; j
< page_size
/ 256; j
++)
174 nand_calculate_ecc(page_data
+ j
* 256, ecc_data
);
177 write(outfd
, page_data
, page_size
+ oob_size
);