denali_ecc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * (C) Copyright 2007
  3. * Developed for DENX Software Engineering GmbH.
  4. *
  5. * Author: Pavel Kolesnikov <concord@emcraft.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. /* define DEBUG for debugging output (obviously ;-)) */
  10. #if 0
  11. #define DEBUG
  12. #endif
  13. #include <common.h>
  14. #include <watchdog.h>
  15. #if defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  16. #include <post.h>
  17. #if CONFIG_POST & CONFIG_SYS_POST_ECC
  18. /*
  19. * MEMORY ECC test
  20. *
  21. * This test performs the checks ECC facility of memory.
  22. */
  23. #include <asm/processor.h>
  24. #include <asm/mmu.h>
  25. #include <asm/io.h>
  26. #include <asm/ppc440.h>
  27. DECLARE_GLOBAL_DATA_PTR;
  28. const static uint8_t syndrome_codes[] = {
  29. 0xF4, 0XF1, 0XEC, 0XEA, 0XE9, 0XE6, 0XE5, 0XE3,
  30. 0XDC, 0XDA, 0XD9, 0XD6, 0XD5, 0XD3, 0XCE, 0XCB,
  31. 0xB5, 0XB0, 0XAD, 0XAB, 0XA8, 0XA7, 0XA4, 0XA2,
  32. 0X9D, 0X9B, 0X98, 0X97, 0X94, 0X92, 0X8F, 0X8A,
  33. 0x75, 0x70, 0X6D, 0X6B, 0X68, 0X67, 0X64, 0X62,
  34. 0X5E, 0X5B, 0X58, 0X57, 0X54, 0X52, 0X4F, 0X4A,
  35. 0x34, 0x31, 0X2C, 0X2A, 0X29, 0X26, 0X25, 0X23,
  36. 0X1C, 0X1A, 0X19, 0X16, 0X15, 0X13, 0X0E, 0X0B,
  37. 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
  38. };
  39. #define ECC_START_ADDR 0x10
  40. #define ECC_STOP_ADDR 0x2000
  41. #define ECC_PATTERN 0x01010101
  42. #define ECC_PATTERN_CORR 0x11010101
  43. #define ECC_PATTERN_UNCORR 0x61010101
  44. inline static void disable_ecc(void)
  45. {
  46. uint32_t value;
  47. sync(); /* Wait for any pending memory accesses to complete. */
  48. mfsdram(DDR0_22, value);
  49. mtsdram(DDR0_22, (value & ~DDR0_22_CTRL_RAW_MASK)
  50. | DDR0_22_CTRL_RAW_ECC_DISABLE);
  51. }
  52. inline static void clear_and_enable_ecc(void)
  53. {
  54. uint32_t value;
  55. sync(); /* Wait for any pending memory accesses to complete. */
  56. mfsdram(DDR0_00, value);
  57. mtsdram(DDR0_00, value | DDR0_00_INT_ACK_ALL);
  58. mfsdram(DDR0_22, value);
  59. mtsdram(DDR0_22, (value & ~DDR0_22_CTRL_RAW_MASK)
  60. | DDR0_22_CTRL_RAW_ECC_ENABLE);
  61. }
  62. static uint32_t get_ecc_status(void)
  63. {
  64. uint32_t int_status;
  65. #if defined(DEBUG)
  66. uint8_t syndrome;
  67. uint32_t hdata, ldata, haddr, laddr;
  68. uint32_t value;
  69. #endif
  70. mfsdram(DDR0_00, int_status);
  71. int_status &= DDR0_00_INT_STATUS_MASK;
  72. #if defined(DEBUG)
  73. if (int_status & (DDR0_00_INT_STATUS_BIT0 | DDR0_00_INT_STATUS_BIT1)) {
  74. mfsdram(DDR0_32, laddr);
  75. mfsdram(DDR0_33, haddr);
  76. haddr &= 0x00000001;
  77. if (int_status & DDR0_00_INT_STATUS_BIT1)
  78. debug("Multiple accesses");
  79. else
  80. debug("A single access");
  81. debug(" outside the defined physical memory space detected\n"
  82. " addr = 0x%01x%08x\n", haddr, laddr);
  83. }
  84. if (int_status & (DDR0_00_INT_STATUS_BIT2 | DDR0_00_INT_STATUS_BIT3)) {
  85. unsigned int bit;
  86. mfsdram(DDR0_23, value);
  87. syndrome = (value >> 16) & 0xff;
  88. for (bit = 0; bit < sizeof(syndrome_codes); bit++)
  89. if (syndrome_codes[bit] == syndrome)
  90. break;
  91. mfsdram(DDR0_38, laddr);
  92. mfsdram(DDR0_39, haddr);
  93. haddr &= 0x00000001;
  94. mfsdram(DDR0_40, ldata);
  95. mfsdram(DDR0_41, hdata);
  96. if (int_status & DDR0_00_INT_STATUS_BIT3)
  97. debug("Multiple correctable ECC events");
  98. else
  99. debug("Single correctable ECC event");
  100. debug(" detected\n 0x%01x%08x - 0x%08x%08x, bit - %d\n",
  101. haddr, laddr, hdata, ldata, bit);
  102. }
  103. if (int_status & (DDR0_00_INT_STATUS_BIT4 | DDR0_00_INT_STATUS_BIT5)) {
  104. mfsdram(DDR0_23, value);
  105. syndrome = (value >> 8) & 0xff;
  106. mfsdram(DDR0_34, laddr);
  107. mfsdram(DDR0_35, haddr);
  108. haddr &= 0x00000001;
  109. mfsdram(DDR0_36, ldata);
  110. mfsdram(DDR0_37, hdata);
  111. if (int_status & DDR0_00_INT_STATUS_BIT5)
  112. debug("Multiple uncorrectable ECC events");
  113. else
  114. debug("Single uncorrectable ECC event");
  115. debug(" detected\n 0x%01x%08x - 0x%08x%08x, "
  116. "syndrome - 0x%02x\n",
  117. haddr, laddr, hdata, ldata, syndrome);
  118. }
  119. if (int_status & DDR0_00_INT_STATUS_BIT6)
  120. debug("DRAM initialization complete\n");
  121. #endif /* defined(DEBUG) */
  122. return int_status;
  123. }
  124. static int test_ecc(uint32_t ecc_addr)
  125. {
  126. uint32_t value;
  127. volatile uint32_t *const ecc_mem = (volatile uint32_t *)ecc_addr;
  128. int ret = 0;
  129. WATCHDOG_RESET();
  130. debug("Entering test_ecc(0x%08x)\n", ecc_addr);
  131. /* Set up correct ECC in memory */
  132. disable_ecc();
  133. clear_and_enable_ecc();
  134. out_be32(ecc_mem, ECC_PATTERN);
  135. out_be32(ecc_mem + 1, ECC_PATTERN);
  136. ppcDcbf((u32)ecc_mem);
  137. /* Verify no ECC error reading back */
  138. value = in_be32(ecc_mem);
  139. disable_ecc();
  140. if (ECC_PATTERN != value) {
  141. debug("Data read error (no-error case): "
  142. "expected 0x%08x, read 0x%08x\n", ECC_PATTERN, value);
  143. ret = 1;
  144. }
  145. value = get_ecc_status();
  146. if (0x00000000 != value) {
  147. /* Expected no ECC status reported */
  148. debug("get_ecc_status(): expected 0x%08x, got 0x%08x\n",
  149. 0x00000000, value);
  150. ret = 1;
  151. }
  152. /* Test for correctable error by creating a one-bit error */
  153. out_be32(ecc_mem, ECC_PATTERN_CORR);
  154. ppcDcbf((u32)ecc_mem);
  155. clear_and_enable_ecc();
  156. value = in_be32(ecc_mem);
  157. disable_ecc();
  158. /* Test that the corrected data was read */
  159. if (ECC_PATTERN != value) {
  160. debug("Data read error (correctable-error case): "
  161. "expected 0x%08x, read 0x%08x\n", ECC_PATTERN, value);
  162. ret = 1;
  163. }
  164. value = get_ecc_status();
  165. if ((DDR0_00_INT_STATUS_BIT2 | DDR0_00_INT_STATUS_BIT7) != value) {
  166. /* Expected a single correctable error reported */
  167. debug("get_ecc_status(): expected 0x%08x, got 0x%08x\n",
  168. DDR0_00_INT_STATUS_BIT2, value);
  169. ret = 1;
  170. }
  171. /* Test for uncorrectable error by creating a two-bit error */
  172. out_be32(ecc_mem, ECC_PATTERN_UNCORR);
  173. ppcDcbf((u32)ecc_mem);
  174. clear_and_enable_ecc();
  175. value = in_be32(ecc_mem);
  176. disable_ecc();
  177. /* Test that the corrected data was read */
  178. if (ECC_PATTERN_UNCORR != value) {
  179. debug("Data read error (uncorrectable-error case): "
  180. "expected 0x%08x, read 0x%08x\n", ECC_PATTERN_UNCORR,
  181. value);
  182. ret = 1;
  183. }
  184. value = get_ecc_status();
  185. if ((DDR0_00_INT_STATUS_BIT4 | DDR0_00_INT_STATUS_BIT7) != value) {
  186. /* Expected a single uncorrectable error reported */
  187. debug("get_ecc_status(): expected 0x%08x, got 0x%08x\n",
  188. DDR0_00_INT_STATUS_BIT4, value);
  189. ret = 1;
  190. }
  191. /* Remove error from SDRAM and enable ECC. */
  192. out_be32(ecc_mem, ECC_PATTERN);
  193. ppcDcbf((u32)ecc_mem);
  194. clear_and_enable_ecc();
  195. return ret;
  196. }
  197. int ecc_post_test(int flags)
  198. {
  199. int ret = 0;
  200. uint32_t value;
  201. uint32_t iaddr;
  202. mfsdram(DDR0_22, value);
  203. if (0x3 != DDR0_22_CTRL_RAW_DECODE(value)) {
  204. debug("SDRAM ECC not enabled, skipping ECC POST.\n");
  205. return 0;
  206. }
  207. /* Mask all interrupts. */
  208. mfsdram(DDR0_01, value);
  209. mtsdram(DDR0_01, (value & ~DDR0_01_INT_MASK_MASK)
  210. | DDR0_01_INT_MASK_ALL_OFF);
  211. for (iaddr = ECC_START_ADDR; iaddr <= ECC_STOP_ADDR; iaddr += iaddr) {
  212. ret = test_ecc(iaddr);
  213. if (ret)
  214. break;
  215. }
  216. /*
  217. * Clear possible errors resulting from ECC testing. (If not done, we
  218. * we could get an interrupt later on when exceptions are enabled.)
  219. */
  220. set_mcsr(get_mcsr());
  221. debug("ecc_post_test() returning %d\n", ret);
  222. return ret;
  223. }
  224. #endif /* CONFIG_POST & CONFIG_SYS_POST_ECC */
  225. #endif /* defined(CONFIG_440EPX) || defined(CONFIG_440GRX) */