denali_ecc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 <asm/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. ppcDcbf((u32)ecc_mem);
  153. /* Verify no ECC error reading back */
  154. value = in_be32(ecc_mem);
  155. disable_ecc();
  156. if (ECC_PATTERN != value) {
  157. debug("Data read error (no-error case): "
  158. "expected 0x%08x, read 0x%08x\n", ECC_PATTERN, value);
  159. ret = 1;
  160. }
  161. value = get_ecc_status();
  162. if (0x00000000 != value) {
  163. /* Expected no ECC status reported */
  164. debug("get_ecc_status(): expected 0x%08x, got 0x%08x\n",
  165. 0x00000000, value);
  166. ret = 1;
  167. }
  168. /* Test for correctable error by creating a one-bit error */
  169. out_be32(ecc_mem, ECC_PATTERN_CORR);
  170. ppcDcbf((u32)ecc_mem);
  171. clear_and_enable_ecc();
  172. value = in_be32(ecc_mem);
  173. disable_ecc();
  174. /* Test that the corrected data was read */
  175. if (ECC_PATTERN != value) {
  176. debug("Data read error (correctable-error case): "
  177. "expected 0x%08x, read 0x%08x\n", ECC_PATTERN, value);
  178. ret = 1;
  179. }
  180. value = get_ecc_status();
  181. if ((DDR0_00_INT_STATUS_BIT2 | DDR0_00_INT_STATUS_BIT7) != value) {
  182. /* Expected a single correctable error reported */
  183. debug("get_ecc_status(): expected 0x%08x, got 0x%08x\n",
  184. DDR0_00_INT_STATUS_BIT2, value);
  185. ret = 1;
  186. }
  187. /* Test for uncorrectable error by creating a two-bit error */
  188. out_be32(ecc_mem, ECC_PATTERN_UNCORR);
  189. ppcDcbf((u32)ecc_mem);
  190. clear_and_enable_ecc();
  191. value = in_be32(ecc_mem);
  192. disable_ecc();
  193. /* Test that the corrected data was read */
  194. if (ECC_PATTERN_UNCORR != value) {
  195. debug("Data read error (uncorrectable-error case): "
  196. "expected 0x%08x, read 0x%08x\n", ECC_PATTERN_UNCORR,
  197. value);
  198. ret = 1;
  199. }
  200. value = get_ecc_status();
  201. if ((DDR0_00_INT_STATUS_BIT4 | DDR0_00_INT_STATUS_BIT7) != value) {
  202. /* Expected a single uncorrectable error reported */
  203. debug("get_ecc_status(): expected 0x%08x, got 0x%08x\n",
  204. DDR0_00_INT_STATUS_BIT4, value);
  205. ret = 1;
  206. }
  207. /* Remove error from SDRAM and enable ECC. */
  208. out_be32(ecc_mem, ECC_PATTERN);
  209. ppcDcbf((u32)ecc_mem);
  210. clear_and_enable_ecc();
  211. return ret;
  212. }
  213. int ecc_post_test(int flags)
  214. {
  215. int ret = 0;
  216. uint32_t value;
  217. uint32_t iaddr;
  218. mfsdram(DDR0_22, value);
  219. if (0x3 != DDR0_22_CTRL_RAW_DECODE(value)) {
  220. debug("SDRAM ECC not enabled, skipping ECC POST.\n");
  221. return 0;
  222. }
  223. /* Mask all interrupts. */
  224. mfsdram(DDR0_01, value);
  225. mtsdram(DDR0_01, (value & ~DDR0_01_INT_MASK_MASK)
  226. | DDR0_01_INT_MASK_ALL_OFF);
  227. for (iaddr = ECC_START_ADDR; iaddr <= ECC_STOP_ADDR; iaddr += iaddr) {
  228. ret = test_ecc(iaddr);
  229. if (ret)
  230. break;
  231. }
  232. /*
  233. * Clear possible errors resulting from ECC testing. (If not done, we
  234. * we could get an interrupt later on when exceptions are enabled.)
  235. */
  236. set_mcsr(get_mcsr());
  237. debug("ecc_post_test() returning %d\n", ret);
  238. return ret;
  239. }
  240. #endif /* CONFIG_POST & CONFIG_SYS_POST_ECC */
  241. #endif /* defined(CONFIG_440EPX) || defined(CONFIG_440GRX) */