nand_ecc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * This file contains an ECC algorithm from Toshiba that detects and
  4. * corrects 1 bit errors in a 256 byte block of data.
  5. *
  6. * drivers/mtd/nand/raw/nand_ecc.c
  7. *
  8. * Copyright (C) 2000-2004 Steven J. Hill (sjhill@realitydiluted.com)
  9. * Toshiba America Electronics Components, Inc.
  10. *
  11. * Copyright (C) 2006 Thomas Gleixner <tglx@linutronix.de>
  12. *
  13. * As a special exception, if other files instantiate templates or use
  14. * macros or inline functions from these files, or you compile these
  15. * files and link them with other works to produce a work based on these
  16. * files, these files do not by themselves cause the resulting work to be
  17. * covered by the GNU General Public License. However the source code for
  18. * these files must still be made available in accordance with section (3)
  19. * of the GNU General Public License.
  20. *
  21. * This exception does not invalidate any other reasons why a work based on
  22. * this file might be covered by the GNU General Public License.
  23. */
  24. #include <common.h>
  25. #include <linux/errno.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/mtd/nand_ecc.h>
  28. /*
  29. * NAND-SPL has no sofware ECC for now, so don't include nand_calculate_ecc(),
  30. * only nand_correct_data() is needed
  31. */
  32. #if !defined(CONFIG_NAND_SPL) || defined(CONFIG_SPL_NAND_SOFTECC)
  33. /*
  34. * Pre-calculated 256-way 1 byte column parity
  35. */
  36. static const u_char nand_ecc_precalc_table[] = {
  37. 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
  38. 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
  39. 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
  40. 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
  41. 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
  42. 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
  43. 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
  44. 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
  45. 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
  46. 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
  47. 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
  48. 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
  49. 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
  50. 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
  51. 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
  52. 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
  53. };
  54. /**
  55. * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block
  56. * @mtd: MTD block structure
  57. * @dat: raw data
  58. * @ecc_code: buffer for ECC
  59. */
  60. int nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  61. u_char *ecc_code)
  62. {
  63. uint8_t idx, reg1, reg2, reg3, tmp1, tmp2;
  64. int i;
  65. /* Initialize variables */
  66. reg1 = reg2 = reg3 = 0;
  67. /* Build up column parity */
  68. for(i = 0; i < 256; i++) {
  69. /* Get CP0 - CP5 from table */
  70. idx = nand_ecc_precalc_table[*dat++];
  71. reg1 ^= (idx & 0x3f);
  72. /* All bit XOR = 1 ? */
  73. if (idx & 0x40) {
  74. reg3 ^= (uint8_t) i;
  75. reg2 ^= ~((uint8_t) i);
  76. }
  77. }
  78. /* Create non-inverted ECC code from line parity */
  79. tmp1 = (reg3 & 0x80) >> 0; /* B7 -> B7 */
  80. tmp1 |= (reg2 & 0x80) >> 1; /* B7 -> B6 */
  81. tmp1 |= (reg3 & 0x40) >> 1; /* B6 -> B5 */
  82. tmp1 |= (reg2 & 0x40) >> 2; /* B6 -> B4 */
  83. tmp1 |= (reg3 & 0x20) >> 2; /* B5 -> B3 */
  84. tmp1 |= (reg2 & 0x20) >> 3; /* B5 -> B2 */
  85. tmp1 |= (reg3 & 0x10) >> 3; /* B4 -> B1 */
  86. tmp1 |= (reg2 & 0x10) >> 4; /* B4 -> B0 */
  87. tmp2 = (reg3 & 0x08) << 4; /* B3 -> B7 */
  88. tmp2 |= (reg2 & 0x08) << 3; /* B3 -> B6 */
  89. tmp2 |= (reg3 & 0x04) << 3; /* B2 -> B5 */
  90. tmp2 |= (reg2 & 0x04) << 2; /* B2 -> B4 */
  91. tmp2 |= (reg3 & 0x02) << 2; /* B1 -> B3 */
  92. tmp2 |= (reg2 & 0x02) << 1; /* B1 -> B2 */
  93. tmp2 |= (reg3 & 0x01) << 1; /* B0 -> B1 */
  94. tmp2 |= (reg2 & 0x01) << 0; /* B7 -> B0 */
  95. /* Calculate final ECC code */
  96. ecc_code[0] = ~tmp1;
  97. ecc_code[1] = ~tmp2;
  98. ecc_code[2] = ((~reg1) << 2) | 0x03;
  99. return 0;
  100. }
  101. #endif /* CONFIG_NAND_SPL */
  102. static inline int countbits(uint32_t byte)
  103. {
  104. int res = 0;
  105. for (;byte; byte >>= 1)
  106. res += byte & 0x01;
  107. return res;
  108. }
  109. /**
  110. * nand_correct_data - [NAND Interface] Detect and correct bit error(s)
  111. * @mtd: MTD block structure
  112. * @dat: raw data read from the chip
  113. * @read_ecc: ECC from the chip
  114. * @calc_ecc: the ECC calculated from raw data
  115. *
  116. * Detect and correct a 1 bit error for 256 byte block
  117. */
  118. int nand_correct_data(struct mtd_info *mtd, u_char *dat,
  119. u_char *read_ecc, u_char *calc_ecc)
  120. {
  121. uint8_t s0, s1, s2;
  122. s1 = calc_ecc[0] ^ read_ecc[0];
  123. s0 = calc_ecc[1] ^ read_ecc[1];
  124. s2 = calc_ecc[2] ^ read_ecc[2];
  125. if ((s0 | s1 | s2) == 0)
  126. return 0;
  127. /* Check for a single bit error */
  128. if( ((s0 ^ (s0 >> 1)) & 0x55) == 0x55 &&
  129. ((s1 ^ (s1 >> 1)) & 0x55) == 0x55 &&
  130. ((s2 ^ (s2 >> 1)) & 0x54) == 0x54) {
  131. uint32_t byteoffs, bitnum;
  132. byteoffs = (s1 << 0) & 0x80;
  133. byteoffs |= (s1 << 1) & 0x40;
  134. byteoffs |= (s1 << 2) & 0x20;
  135. byteoffs |= (s1 << 3) & 0x10;
  136. byteoffs |= (s0 >> 4) & 0x08;
  137. byteoffs |= (s0 >> 3) & 0x04;
  138. byteoffs |= (s0 >> 2) & 0x02;
  139. byteoffs |= (s0 >> 1) & 0x01;
  140. bitnum = (s2 >> 5) & 0x04;
  141. bitnum |= (s2 >> 4) & 0x02;
  142. bitnum |= (s2 >> 3) & 0x01;
  143. dat[byteoffs] ^= (1 << bitnum);
  144. return 1;
  145. }
  146. if(countbits(s0 | ((uint32_t)s1 << 8) | ((uint32_t)s2 <<16)) == 1)
  147. return 1;
  148. return -EBADMSG;
  149. }