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.
 
 
 
 
 
 

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