From cbfa0304f96c7e3d30f83d9feb7b7f23a197e4b1 Mon Sep 17 00:00:00 2001
From: oharboe <oharboe@b42882b7-edfa-0310-969c-e2dbd0fdcd60>
Date: Tue, 12 May 2009 17:29:16 +0000
Subject: [PATCH] 4-bit ECC support for Marvell Kirkwood SOC

git-svn-id: svn://svn.berlios.de/openocd/trunk@1768 b42882b7-edfa-0310-969c-e2dbd0fdcd60
---
 src/flash/Makefile.am           |   2 +-
 src/flash/nand.c                |  19 +++-
 src/flash/nand.h                |   2 +
 src/flash/nand_ecc_kw.c         | 174 ++++++++++++++++++++++++++++++++
 src/target/board/sheevaplug.cfg |   4 +-
 5 files changed, 197 insertions(+), 4 deletions(-)
 create mode 100644 src/flash/nand_ecc_kw.c

diff --git a/src/flash/Makefile.am b/src/flash/Makefile.am
index 7895edc85..e5b76cb61 100644
--- a/src/flash/Makefile.am
+++ b/src/flash/Makefile.am
@@ -7,7 +7,7 @@ METASOURCES = AUTO
 noinst_LTLIBRARIES = libflash.la
 libflash_la_SOURCES = \
 	flash.c lpc2000.c cfi.c non_cfi.c at91sam7.c \
-	str7x.c str9x.c aduc702x.c nand.c nand_ecc.c \
+	str7x.c str9x.c aduc702x.c nand.c nand_ecc.c nand_ecc_kw.c \
 	lpc3180_nand_controller.c stellaris.c str9xpec.c stm32x.c tms470.c \
 	ecos.c orion_nand.c s3c24xx_nand.c s3c2410_nand.c s3c2412_nand.c \
 	s3c2440_nand.c s3c2443_nand.c lpc288x.c ocl.c mflash.c pic32mx.c avrf.c
diff --git a/src/flash/nand.c b/src/flash/nand.c
index 8efed037c..057feb7d9 100644
--- a/src/flash/nand.c
+++ b/src/flash/nand.c
@@ -1332,6 +1332,8 @@ static int handle_nand_write_command(struct command_context_s *cmd_ctx, char *cm
 					oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
 				else if (!strcmp(args[i], "oob_softecc"))
 					oob_format |= NAND_OOB_SW_ECC;
+				else if (!strcmp(args[i], "oob_softecc_kw"))
+					oob_format |= NAND_OOB_SW_ECC_KW;
 				else
 				{
 					command_print(cmd_ctx, "unknown option: %s", args[i]);
@@ -1355,7 +1357,7 @@ static int handle_nand_write_command(struct command_context_s *cmd_ctx, char *cm
 			page = malloc(p->page_size);
 		}
 
-		if (oob_format & (NAND_OOB_RAW | NAND_OOB_SW_ECC))
+		if (oob_format & (NAND_OOB_RAW | NAND_OOB_SW_ECC | NAND_OOB_SW_ECC_KW))
 		{
 			if (p->page_size == 512) {
 				oob_size = 16;
@@ -1401,6 +1403,21 @@ static int handle_nand_write_command(struct command_context_s *cmd_ctx, char *cm
 					oob[eccpos[j++]] = ecc[1];
 					oob[eccpos[j++]] = ecc[2];
 				}
+			} else if (oob_format & NAND_OOB_SW_ECC_KW)
+			{
+				/*
+				 * In this case eccpos is not used as
+				 * the ECC data is always stored contigously
+				 * at the end of the OOB area.  It consists
+				 * of 10 bytes per 512-byte data block.
+				 */
+				u32 i;
+				u8 *ecc = oob + oob_size - page_size/512 * 10;
+				memset(oob, 0xff, oob_size);
+				for (i = 0; i < page_size; i += 512) {
+					nand_calculate_ecc_kw(p, page+i, ecc);
+					ecc += 10;
+				}
 			}
 			else if (NULL != oob)
 			{
diff --git a/src/flash/nand.h b/src/flash/nand.h
index bd9554c34..b3c6b6b56 100644
--- a/src/flash/nand.h
+++ b/src/flash/nand.h
@@ -200,6 +200,7 @@ enum oob_formats
 	NAND_OOB_ONLY = 0x2,	/* only OOB data */
 	NAND_OOB_SW_ECC = 0x10,	/* when writing, use SW ECC (as opposed to no ECC) */ 
 	NAND_OOB_HW_ECC = 0x20, /* when writing, use HW ECC (as opposed to no ECC) */
+	NAND_OOB_SW_ECC_KW = 0x40, /* when writing, use Marvell's Kirkwood bootrom format */
 	NAND_OOB_JFFS2 = 0x100,	/* when writing, use JFFS2 OOB layout */
 	NAND_OOB_YAFFS2 = 0x100,/* when writing, use YAFFS2 OOB layout */
 };
@@ -210,6 +211,7 @@ extern int nand_read_page_raw(struct nand_device_s *device, u32 page, u8 *data,
 extern int nand_write_page_raw(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size);
 extern int nand_read_status(struct nand_device_s *device, u8 *status);
 extern int nand_calculate_ecc(struct nand_device_s *device, const u8 *dat, u8 *ecc_code);
+extern int nand_calculate_ecc_kw(struct nand_device_s *device, const u8 *dat, u8 *ecc_code);
 
 extern int nand_register_commands(struct command_context_s *cmd_ctx);
 extern int nand_init(struct command_context_s *cmd_ctx);
diff --git a/src/flash/nand_ecc_kw.c b/src/flash/nand_ecc_kw.c
new file mode 100644
index 000000000..ecc7adc26
--- /dev/null
+++ b/src/flash/nand_ecc_kw.c
@@ -0,0 +1,174 @@
+/*
+ * Reed-Solomon ECC handling for the Marvell Kirkwood SOC
+ * Copyright (C) 2009 Marvell Semiconductor, Inc.
+ *
+ * Authors: Lennert Buytenhek <buytenh@wantstofly.org>
+ *          Nicolas Pitre <nico@cam.org>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 or (at your option) any
+ * later version.
+ *
+ * This file is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/types.h>
+#include "nand.h"
+
+
+/*****************************************************************************
+ * Arithmetic in GF(2^10) ("F") modulo x^10 + x^3 + 1.
+ *
+ * For multiplication, a discrete log/exponent table is used, with
+ * primitive element x (F is a primitive field, so x is primitive).
+ */
+#define MODPOLY		0x409		/* x^10 + x^3 + 1 in binary */
+
+/*
+ * Maps an integer a [0..1022] to a polynomial b = gf_exp[a] in
+ * GF(2^10) mod x^10 + x^3 + 1 such that b = x ^ a.  There's two
+ * identical copies of this array back-to-back so that we can save
+ * the mod 1023 operation when doing a GF multiplication.
+ */
+static uint16_t gf_exp[1023 + 1023];
+
+/*
+ * Maps a polynomial b in GF(2^10) mod x^10 + x^3 + 1 to an index
+ * a = gf_log[b] in [0..1022] such that b = x ^ a.
+ */
+static uint16_t gf_log[1024];
+
+static void gf_build_log_exp_table(void)
+{
+	int i;
+	int p_i;
+
+	/*
+	 * p_i = x ^ i
+	 *
+	 * Initialise to 1 for i = 0.
+	 */
+	p_i = 1;
+
+	for (i = 0; i < 1023; i++) {
+		gf_exp[i] = p_i;
+		gf_exp[i + 1023] = p_i;
+		gf_log[p_i] = i;
+
+		/*
+		 * p_i = p_i * x
+		 */
+		p_i <<= 1;
+		if (p_i & (1 << 10))
+			p_i ^= MODPOLY;
+	}
+}
+
+
+/*****************************************************************************
+ * Reed-Solomon code
+ *
+ * This implements a (1023,1015) Reed-Solomon ECC code over GF(2^10)
+ * mod x^10 + x^3 + 1, shortened to (520,512).  The ECC data consists
+ * of 8 10-bit symbols, or 10 8-bit bytes.
+ *
+ * Given 512 bytes of data, computes 10 bytes of ECC.
+ *
+ * This is done by converting the 512 bytes to 512 10-bit symbols
+ * (elements of F), interpreting those symbols as a polynomial in F[X]
+ * by taking symbol 0 as the coefficient of X^8 and symbol 511 as the
+ * coefficient of X^519, and calculating the residue of that polynomial
+ * divided by the generator polynomial, which gives us the 8 ECC symbols
+ * as the remainder.  Finally, we convert the 8 10-bit ECC symbols to 10
+ * 8-bit bytes.
+ *
+ * The generator polynomial is hardcoded, as that is faster, but it
+ * can be computed by taking the primitive element a = x (in F), and
+ * constructing a polynomial in F[X] with roots a, a^2, a^3, ..., a^8
+ * by multiplying the minimal polynomials for those roots (which are
+ * just 'x - a^i' for each i).
+ *
+ * Note: due to unfortunate circumstances, the bootrom in the Kirkwood SOC
+ * expects the ECC to be computed backward, i.e. from the last byte down
+ * to the first one.
+ */
+int nand_calculate_ecc_kw(struct nand_device_s *device, const u8 *data, u8 *ecc)
+{
+	unsigned int r7, r6, r5, r4, r3, r2, r1, r0;
+	int i;
+	static int tables_initialized = 0;
+
+	if (!tables_initialized) {
+		gf_build_log_exp_table();
+		tables_initialized = 1;
+	}
+
+	/*
+	 * Load bytes 504..511 of the data into r.
+	 */
+	r0 = data[504];
+	r1 = data[505];
+	r2 = data[506];
+	r3 = data[507];
+	r4 = data[508];
+	r5 = data[509];
+	r6 = data[510];
+	r7 = data[511];
+
+
+	/*
+	 * Shift bytes 503..0 (in that order) into r0, followed
+	 * by eight zero bytes, while reducing the polynomial by the
+	 * generator polynomial in every step.
+	 */
+	for (i = 503; i >= -8; i--) {
+		unsigned int d;
+
+		d = 0;
+		if (i >= 0)
+			d = data[i];
+
+		if (r7) {
+			u16 *t = gf_exp + gf_log[r7];
+
+			r7 = r6 ^ t[0x21c];
+			r6 = r5 ^ t[0x181];
+			r5 = r4 ^ t[0x18e];
+			r4 = r3 ^ t[0x25f];
+			r3 = r2 ^ t[0x197];
+			r2 = r1 ^ t[0x193];
+			r1 = r0 ^ t[0x237];
+			r0 = d  ^ t[0x024];
+		} else {
+			r7 = r6;
+			r6 = r5;
+			r5 = r4;
+			r4 = r3;
+			r3 = r2;
+			r2 = r1;
+			r1 = r0;
+			r0 = d;
+		}
+	}
+
+	ecc[0] = r0;
+	ecc[1] = (r0 >> 8) | (r1 << 2);
+	ecc[2] = (r1 >> 6) | (r2 << 4);
+	ecc[3] = (r2 >> 4) | (r3 << 6);
+	ecc[4] = (r3 >> 2);
+	ecc[5] = r4;
+	ecc[6] = (r4 >> 8) | (r5 << 2);
+	ecc[7] = (r5 >> 6) | (r6 << 4);
+	ecc[8] = (r6 >> 4) | (r7 << 6);
+	ecc[9] = (r7 >> 2);
+
+	return 0;
+}
diff --git a/src/target/board/sheevaplug.cfg b/src/target/board/sheevaplug.cfg
index 64e259678..276d6f246 100644
--- a/src/target/board/sheevaplug.cfg
+++ b/src/target/board/sheevaplug.cfg
@@ -99,7 +99,7 @@ proc sheevaplug_reflash_uboot { } {
 	sheevaplug_init
 	nand probe 0
 	nand erase 0 0 4
-	nand write 0 uboot.bin 0 oob_softecc
+	nand write 0 uboot.bin 0 oob_softecc_kw
 	resume
 
 }
@@ -108,7 +108,7 @@ proc sheevaplug_load_uboot { } {
 
 	# load u-Boot into RAM and execute it
 	sheevaplug_init
-	load_image /tmp/uboot.elf
+	load_image uboot.elf
 	verify_image uboot.elf
 	resume 0x00600000
 
-- 
GitLab