denali_ecc.c 7.4 KB

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