2 * Reed-Solomon ECC handling for the Marvell Kirkwood SOC
3 * Copyright (C) 2017 Lothar Waßmann <LW@KARO-electronics.de>
5 * derived from openocd src/flash/nand/ecc_kw.c:
6 * Copyright (C) 2009 Marvell Semiconductor, Inc.
8 * Authors: Lennert Buytenhek <buytenh@wantstofly.org>
9 * Nicolas Pitre <nico@fluxnic.net>
11 * This file is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 or (at your option) any
16 * This file is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 #include <asm-generic/errno.h>
23 #include <linux/mtd/nand.h>
24 #include <linux/mtd/nand_ecc.h>
26 /*****************************************************************************
27 * Arithmetic in GF(2^10) ("F") modulo x^10 + x^3 + 1.
29 * For multiplication, a discrete log/exponent table is used, with
30 * primitive element x (F is a primitive field, so x is primitive).
32 #define MODPOLY 0x409 /* x^10 + x^3 + 1 in binary */
35 * Maps an integer a [0..1022] to a polynomial b = gf_exp[a] in
36 * GF(2^10) mod x^10 + x^3 + 1 such that b = x ^ a. There's two
37 * identical copies of this array back-to-back so that we can save
38 * the mod 1023 operation when doing a GF multiplication.
40 static uint16_t gf_exp[1023 + 1023];
43 * Maps a polynomial b in GF(2^10) mod x^10 + x^3 + 1 to an index
44 * a = gf_log[b] in [0..1022] such that b = x ^ a.
46 static uint16_t gf_log[1024];
48 static void gf_build_log_exp_table(void)
56 * Initialise to 1 for i = 0.
60 for (i = 0; i < 1023; i++) {
62 gf_exp[i + 1023] = p_i;
73 for (i = 0; i < ARRAY_SIZE(gf_log); i++) {
74 printf("exp[%03x]=%4d log[%03x]=%4d\n", i, gf_exp[i], i, gf_log[i]);
76 for (; i < ARRAY_SIZE(gf_exp); i++) {
77 printf("exp[%03x]=%4d\n", i, gf_exp[i]);
82 /*****************************************************************************
85 * This implements a (1023,1015) Reed-Solomon ECC code over GF(2^10)
86 * mod x^10 + x^3 + 1, shortened to (520,512). The ECC data consists
87 * of 8 10-bit symbols, or 10 8-bit bytes.
89 * Given 512 bytes of data, computes 10 bytes of ECC.
91 * This is done by converting the 512 bytes to 512 10-bit symbols
92 * (elements of F), interpreting those symbols as a polynomial in F[X]
93 * by taking symbol 0 as the coefficient of X^8 and symbol 511 as the
94 * coefficient of X^519, and calculating the residue of that polynomial
95 * divided by the generator polynomial, which gives us the 8 ECC symbols
96 * as the remainder. Finally, we convert the 8 10-bit ECC symbols to 10
99 * The generator polynomial is hardcoded, as that is faster, but it
100 * can be computed by taking the primitive element a = x (in F), and
101 * constructing a polynomial in F[X] with roots a, a^2, a^3, ..., a^8
102 * by multiplying the minimal polynomials for those roots (which are
103 * just 'x - a^i' for each i).
105 * Note: due to unfortunate circumstances, the bootrom in the Kirkwood SOC
106 * expects the ECC to be computed backward, i.e. from the last byte down
109 int nand_rs_calculate_ecc(struct mtd_info *mtd, const uint8_t *data,
112 unsigned int r7, r6, r5, r4, r3, r2, r1, r0;
114 static int tables_initialized;
116 if (!tables_initialized) {
117 printf("Using RS-ECC\n");
118 gf_build_log_exp_table();
119 tables_initialized = 1;
123 * Load bytes 504..511 of the data into r.
135 * Shift bytes 503..0 (in that order) into r0, followed
136 * by eight zero bytes, while reducing the polynomial by the
137 * generator polynomial in every step.
139 for (i = 503; i >= -8; i--) {
140 unsigned int d = (i >= 0) ? data[i] : 0;
143 uint16_t *t = &gf_exp[gf_log[r7]];
145 r7 = r6 ^ t[0x21c]; // 540
146 r6 = r5 ^ t[0x181]; // 385
147 r5 = r4 ^ t[0x18e]; // 398
148 r4 = r3 ^ t[0x25f]; // 607
149 r3 = r2 ^ t[0x197]; // 407
150 r2 = r1 ^ t[0x193]; // 403
151 r1 = r0 ^ t[0x237]; // 567
152 r0 = d ^ t[0x024]; // 36
166 ecc[1] = (r0 >> 8) | (r1 << 2);
167 ecc[2] = (r1 >> 6) | (r2 << 4);
168 ecc[3] = (r2 >> 4) | (r3 << 6);
171 ecc[6] = (r4 >> 8) | (r5 << 2);
172 ecc[7] = (r5 >> 6) | (r6 << 4);
173 ecc[8] = (r6 >> 4) | (r7 << 6);
176 debug("ECC: %03x %03x %03x %03x %03x %03x %03x %03x\n",
177 r0, r1, r2, r3, r4, r5, r6, r7);
182 int nand_rs_correct_data(struct mtd_info *mtd, u_char *dat,
183 u_char *read_ecc, u_char *calc_ecc)
187 printf("Reading NAND with RS-ECC not supported\n");