You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

175 lines
4.4 KiB

  1. /*
  2. * Reed-Solomon ECC handling for the Marvell Kirkwood SOC
  3. * Copyright (C) 2009 Marvell Semiconductor, Inc.
  4. *
  5. * Authors: Lennert Buytenhek <buytenh@wantstofly.org>
  6. * Nicolas Pitre <nico@cam.org>
  7. *
  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
  11. * later version.
  12. *
  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
  16. * for more details.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include <sys/types.h>
  22. #include "nand.h"
  23. /*****************************************************************************
  24. * Arithmetic in GF(2^10) ("F") modulo x^10 + x^3 + 1.
  25. *
  26. * For multiplication, a discrete log/exponent table is used, with
  27. * primitive element x (F is a primitive field, so x is primitive).
  28. */
  29. #define MODPOLY 0x409 /* x^10 + x^3 + 1 in binary */
  30. /*
  31. * Maps an integer a [0..1022] to a polynomial b = gf_exp[a] in
  32. * GF(2^10) mod x^10 + x^3 + 1 such that b = x ^ a. There's two
  33. * identical copies of this array back-to-back so that we can save
  34. * the mod 1023 operation when doing a GF multiplication.
  35. */
  36. static uint16_t gf_exp[1023 + 1023];
  37. /*
  38. * Maps a polynomial b in GF(2^10) mod x^10 + x^3 + 1 to an index
  39. * a = gf_log[b] in [0..1022] such that b = x ^ a.
  40. */
  41. static uint16_t gf_log[1024];
  42. static void gf_build_log_exp_table(void)
  43. {
  44. int i;
  45. int p_i;
  46. /*
  47. * p_i = x ^ i
  48. *
  49. * Initialise to 1 for i = 0.
  50. */
  51. p_i = 1;
  52. for (i = 0; i < 1023; i++) {
  53. gf_exp[i] = p_i;
  54. gf_exp[i + 1023] = p_i;
  55. gf_log[p_i] = i;
  56. /*
  57. * p_i = p_i * x
  58. */
  59. p_i <<= 1;
  60. if (p_i & (1 << 10))
  61. p_i ^= MODPOLY;
  62. }
  63. }
  64. /*****************************************************************************
  65. * Reed-Solomon code
  66. *
  67. * This implements a (1023,1015) Reed-Solomon ECC code over GF(2^10)
  68. * mod x^10 + x^3 + 1, shortened to (520,512). The ECC data consists
  69. * of 8 10-bit symbols, or 10 8-bit bytes.
  70. *
  71. * Given 512 bytes of data, computes 10 bytes of ECC.
  72. *
  73. * This is done by converting the 512 bytes to 512 10-bit symbols
  74. * (elements of F), interpreting those symbols as a polynomial in F[X]
  75. * by taking symbol 0 as the coefficient of X^8 and symbol 511 as the
  76. * coefficient of X^519, and calculating the residue of that polynomial
  77. * divided by the generator polynomial, which gives us the 8 ECC symbols
  78. * as the remainder. Finally, we convert the 8 10-bit ECC symbols to 10
  79. * 8-bit bytes.
  80. *
  81. * The generator polynomial is hardcoded, as that is faster, but it
  82. * can be computed by taking the primitive element a = x (in F), and
  83. * constructing a polynomial in F[X] with roots a, a^2, a^3, ..., a^8
  84. * by multiplying the minimal polynomials for those roots (which are
  85. * just 'x - a^i' for each i).
  86. *
  87. * Note: due to unfortunate circumstances, the bootrom in the Kirkwood SOC
  88. * expects the ECC to be computed backward, i.e. from the last byte down
  89. * to the first one.
  90. */
  91. int nand_calculate_ecc_kw(struct nand_device_s *device, const uint8_t *data, uint8_t *ecc)
  92. {
  93. unsigned int r7, r6, r5, r4, r3, r2, r1, r0;
  94. int i;
  95. static int tables_initialized = 0;
  96. if (!tables_initialized) {
  97. gf_build_log_exp_table();
  98. tables_initialized = 1;
  99. }
  100. /*
  101. * Load bytes 504..511 of the data into r.
  102. */
  103. r0 = data[504];
  104. r1 = data[505];
  105. r2 = data[506];
  106. r3 = data[507];
  107. r4 = data[508];
  108. r5 = data[509];
  109. r6 = data[510];
  110. r7 = data[511];
  111. /*
  112. * Shift bytes 503..0 (in that order) into r0, followed
  113. * by eight zero bytes, while reducing the polynomial by the
  114. * generator polynomial in every step.
  115. */
  116. for (i = 503; i >= -8; i--) {
  117. unsigned int d;
  118. d = 0;
  119. if (i >= 0)
  120. d = data[i];
  121. if (r7) {
  122. uint16_t *t = gf_exp + gf_log[r7];
  123. r7 = r6 ^ t[0x21c];
  124. r6 = r5 ^ t[0x181];
  125. r5 = r4 ^ t[0x18e];
  126. r4 = r3 ^ t[0x25f];
  127. r3 = r2 ^ t[0x197];
  128. r2 = r1 ^ t[0x193];
  129. r1 = r0 ^ t[0x237];
  130. r0 = d ^ t[0x024];
  131. } else {
  132. r7 = r6;
  133. r6 = r5;
  134. r5 = r4;
  135. r4 = r3;
  136. r3 = r2;
  137. r2 = r1;
  138. r1 = r0;
  139. r0 = d;
  140. }
  141. }
  142. ecc[0] = r0;
  143. ecc[1] = (r0 >> 8) | (r1 << 2);
  144. ecc[2] = (r1 >> 6) | (r2 << 4);
  145. ecc[3] = (r2 >> 4) | (r3 << 6);
  146. ecc[4] = (r3 >> 2);
  147. ecc[5] = r4;
  148. ecc[6] = (r4 >> 8) | (r5 << 2);
  149. ecc[7] = (r5 >> 6) | (r6 << 4);
  150. ecc[8] = (r6 >> 4) | (r7 << 6);
  151. ecc[9] = (r7 >> 2);
  152. return 0;
  153. }