2 * Reed-Solomon ECC handling for the Marvell Kirkwood SOC
3 * Copyright (C) 2009 Marvell Semiconductor, Inc.
5 * Authors: Lennert Buytenhek <buytenh@wantstofly.org>
6 * Nicolas Pitre <nico@fluxnic.net>
8 * This file is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /*****************************************************************************
29 * Arithmetic in GF(2^10) ("F") modulo x^10 + x^3 + 1.
31 * For multiplication, a discrete log/exponent table is used, with
32 * primitive element x (F is a primitive field, so x is primitive).
34 #define MODPOLY 0x409 /* x^10 + x^3 + 1 in binary */
37 * Maps an integer a [0..1022] to a polynomial b = gf_exp[a] in
38 * GF(2^10) mod x^10 + x^3 + 1 such that b = x ^ a. There's two
39 * identical copies of this array back-to-back so that we can save
40 * the mod 1023 operation when doing a GF multiplication.
42 static uint16_t gf_exp[1023 + 1023];
45 * Maps a polynomial b in GF(2^10) mod x^10 + x^3 + 1 to an index
46 * a = gf_log[b] in [0..1022] such that b = x ^ a.
48 static uint16_t gf_log[1024];
50 static void gf_build_log_exp_table(void)
58 * Initialise to 1 for i = 0.
62 for (i = 0; i < 1023; i++) {
64 gf_exp[i + 1023] = p_i;
77 /*****************************************************************************
80 * This implements a (1023,1015) Reed-Solomon ECC code over GF(2^10)
81 * mod x^10 + x^3 + 1, shortened to (520,512). The ECC data consists
82 * of 8 10-bit symbols, or 10 8-bit bytes.
84 * Given 512 bytes of data, computes 10 bytes of ECC.
86 * This is done by converting the 512 bytes to 512 10-bit symbols
87 * (elements of F), interpreting those symbols as a polynomial in F[X]
88 * by taking symbol 0 as the coefficient of X^8 and symbol 511 as the
89 * coefficient of X^519, and calculating the residue of that polynomial
90 * divided by the generator polynomial, which gives us the 8 ECC symbols
91 * as the remainder. Finally, we convert the 8 10-bit ECC symbols to 10
94 * The generator polynomial is hardcoded, as that is faster, but it
95 * can be computed by taking the primitive element a = x (in F), and
96 * constructing a polynomial in F[X] with roots a, a^2, a^3, ..., a^8
97 * by multiplying the minimal polynomials for those roots (which are
98 * just 'x - a^i' for each i).
100 * Note: due to unfortunate circumstances, the bootrom in the Kirkwood SOC
101 * expects the ECC to be computed backward, i.e. from the last byte down
104 int nand_calculate_ecc_kw(struct nand_device *nand, const uint8_t *data, uint8_t *ecc)
106 unsigned int r7, r6, r5, r4, r3, r2, r1, r0;
108 static int tables_initialized;
110 if (!tables_initialized) {
111 gf_build_log_exp_table();
112 tables_initialized = 1;
116 * Load bytes 504..511 of the data into r.
128 * Shift bytes 503..0 (in that order) into r0, followed
129 * by eight zero bytes, while reducing the polynomial by the
130 * generator polynomial in every step.
132 for (i = 503; i >= -8; i--) {
140 uint16_t *t = gf_exp + gf_log[r7];
163 ecc[1] = (r0 >> 8) | (r1 << 2);
164 ecc[2] = (r1 >> 6) | (r2 << 4);
165 ecc[3] = (r2 >> 4) | (r3 << 6);
168 ecc[6] = (r4 >> 8) | (r5 << 2);
169 ecc[7] = (r5 >> 6) | (r6 << 4);
170 ecc[8] = (r6 >> 4) | (r7 << 6);