ecc.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. #ifdef CONFIG_POST
  32. #include <post.h>
  33. #if CONFIG_POST & CFG_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. #include "../../../board/lwmon5/sdram.h"
  44. DECLARE_GLOBAL_DATA_PTR;
  45. const static unsigned char syndrome_codes[] = {
  46. 0xF4, 0XF1, 0XEC ,0XEA, 0XE9, 0XE6, 0XE5, 0XE3,
  47. 0XDC, 0XDA, 0XD9, 0XD6, 0XD5, 0XD3, 0XCE, 0XCB,
  48. 0xB5, 0XB0, 0XAD, 0XAB, 0XA8, 0XA7, 0XA4, 0XA2,
  49. 0X9D, 0X9B, 0X98, 0X97, 0X94, 0X92, 0X8F, 0X8A,
  50. 0x75, 0x70, 0X6D, 0X6B, 0X68, 0X67, 0X64, 0X62,
  51. 0X5E, 0X5B, 0X58, 0X57, 0X54, 0X52, 0X4F, 0X4A,
  52. 0x34, 0x31, 0X2C, 0X2A, 0X29, 0X26, 0X25, 0X23,
  53. 0X1C, 0X1A, 0X19, 0X16, 0X15, 0X13, 0X0E, 0X0B,
  54. 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
  55. };
  56. #define ECC_START_ADDR 0x10
  57. #define ECC_STOP_ADDR 0x2000
  58. #define ECC_PATTERN 0x0101010101010101ull
  59. #define ECC_PATTERN_CORR 0x0101010101010100ull
  60. #define ECC_PATTERN_UNCORR 0x010101010101010Full
  61. static int test_ecc_error(void)
  62. {
  63. unsigned long value;
  64. unsigned long hdata, ldata, haddr, laddr;
  65. unsigned int bit;
  66. int ret = 0;
  67. mfsdram(DDR0_23, value);
  68. for (bit = 0; bit < sizeof(syndrome_codes); bit++)
  69. if (syndrome_codes[bit] == ((value >> 16) & 0xff))
  70. break;
  71. mfsdram(DDR0_00, value);
  72. if (value & DDR0_00_INT_STATUS_BIT0) {
  73. debug("Bit0. A single access outside the defined PHYSICAL"
  74. " memory space detected\n");
  75. mfsdram(DDR0_32, laddr);
  76. mfsdram(DDR0_33, haddr);
  77. debug(" addr = 0x%08x%08x\n", haddr, laddr);
  78. ret = 1;
  79. }
  80. if (value & DDR0_00_INT_STATUS_BIT1) {
  81. debug("Bit1. Multiple accesses outside the defined PHYSICAL"
  82. " memory space detected\n");
  83. ret = 2;
  84. }
  85. if (value & DDR0_00_INT_STATUS_BIT2) {
  86. debug("Bit2. Single correctable ECC event detected\n");
  87. mfsdram(DDR0_38, laddr);
  88. mfsdram(DDR0_39, haddr);
  89. mfsdram(DDR0_40, ldata);
  90. mfsdram(DDR0_41, hdata);
  91. debug(" 0x%08x - 0x%08x%08x, bit - %d\n",
  92. laddr, hdata, ldata, bit);
  93. ret = 3;
  94. }
  95. if (value & DDR0_00_INT_STATUS_BIT3) {
  96. debug("Bit3. Multiple correctable ECC events detected\n");
  97. mfsdram(DDR0_38, laddr);
  98. mfsdram(DDR0_39, haddr);
  99. mfsdram(DDR0_40, ldata);
  100. mfsdram(DDR0_41, hdata);
  101. debug(" 0x%08x - 0x%08x%08x, bit - %d\n",
  102. laddr, hdata, ldata, bit);
  103. ret = 4;
  104. }
  105. if (value & DDR0_00_INT_STATUS_BIT4) {
  106. debug("Bit4. Single uncorrectable ECC event detected\n");
  107. mfsdram(DDR0_34, laddr);
  108. mfsdram(DDR0_35, haddr);
  109. mfsdram(DDR0_36, ldata);
  110. mfsdram(DDR0_37, hdata);
  111. debug(" 0x%08x - 0x%08x%08x, bit - %d\n",
  112. laddr, hdata, ldata, bit);
  113. ret = 5;
  114. }
  115. if (value & DDR0_00_INT_STATUS_BIT5) {
  116. debug("Bit5. Multiple uncorrectable ECC events detected\n");
  117. mfsdram(DDR0_34, laddr);
  118. mfsdram(DDR0_35, haddr);
  119. mfsdram(DDR0_36, ldata);
  120. mfsdram(DDR0_37, hdata);
  121. debug(" 0x%08x - 0x%08x%08x, bit - %d\n",
  122. laddr, hdata, ldata, bit);
  123. ret = 6;
  124. }
  125. if (value & DDR0_00_INT_STATUS_BIT6) {
  126. debug("Bit6. DRAM initialization complete\n");
  127. ret = 7;
  128. }
  129. /* error status cleared */
  130. mfsdram(DDR0_00, value);
  131. mtsdram(DDR0_00, value | DDR0_00_INT_ACK_ALL);
  132. return ret;
  133. }
  134. static int test_ecc(unsigned long ecc_addr)
  135. {
  136. volatile unsigned long long *ecc_mem;
  137. unsigned long value;
  138. unsigned long ecc_data;
  139. volatile unsigned long *lecc_mem;
  140. int pret, ret = 0;
  141. sync();
  142. eieio();
  143. WATCHDOG_RESET();
  144. ecc_mem = (unsigned long long *)ecc_addr;
  145. lecc_mem = (ulong *)ecc_addr;
  146. *ecc_mem = ECC_PATTERN;
  147. pret = test_ecc_error();
  148. if (pret != 0)
  149. ret = 1;
  150. /* disconnect ecc */
  151. mfsdram(DDR0_22, value);
  152. mtsdram(DDR0_22, (value &~ DDR0_22_CTRL_RAW_MASK)
  153. | DDR0_22_CTRL_RAW_ECC_DISABLE);
  154. /* injecting error */
  155. *ecc_mem = ECC_PATTERN_CORR;
  156. /* enable ecc */
  157. mfsdram(DDR0_22, value);
  158. mtsdram(DDR0_22, (value &~ DDR0_22_CTRL_RAW_MASK)
  159. | DDR0_22_CTRL_RAW_ECC_ENABLE);
  160. ecc_data = *lecc_mem;
  161. pret = test_ecc_error();
  162. /* if read data ok, 1 correctable error must be fixed */
  163. if (pret != 3)
  164. ret = 1;
  165. /* test for uncorrectable error */
  166. /* disconnect from ecc storage */
  167. mfsdram(DDR0_22, value);
  168. mtsdram(DDR0_22, (value &~ DDR0_22_CTRL_RAW_MASK)
  169. | DDR0_22_CTRL_RAW_NO_ECC_RAM);
  170. /* injecting multiply bit error */
  171. *ecc_mem = ECC_PATTERN_UNCORR;
  172. /* enable ecc */
  173. mfsdram(DDR0_22, value);
  174. mtsdram(DDR0_22, (value &~ DDR0_22_CTRL_RAW_MASK)
  175. | DDR0_22_CTRL_RAW_ECC_ENABLE);
  176. ecc_data = *lecc_mem;
  177. /* what the data should be read? */
  178. pret = test_ecc_error();
  179. /* info about uncorrectable error must appear */
  180. if (pret != 5)
  181. ret = 1;
  182. sync();
  183. eieio();
  184. return ret;
  185. }
  186. int ecc_post_test (int flags)
  187. {
  188. int ret = 0;
  189. unsigned long value;
  190. unsigned long iaddr;
  191. #if CONFIG_DDR_ECC
  192. sync();
  193. eieio();
  194. /* mask all int */
  195. mfsdram(DDR0_01, value);
  196. mtsdram(DDR0_01, (value &~ DDR0_01_INT_MASK_MASK)
  197. | DDR0_01_INT_MASK_ALL_OFF);
  198. /* clear error status */
  199. mfsdram(DDR0_00, value);
  200. mtsdram(DDR0_00, value | DDR0_00_INT_ACK_ALL);
  201. /* enable full support of ECC */
  202. mfsdram(DDR0_22, value);
  203. mtsdram(DDR0_22, (value &~ DDR0_22_CTRL_RAW_MASK)
  204. | DDR0_22_CTRL_RAW_ECC_ENABLE);
  205. for (iaddr = ECC_START_ADDR; iaddr < ECC_STOP_ADDR; iaddr += iaddr) {
  206. ret = test_ecc(iaddr);
  207. if (ret)
  208. break;
  209. }
  210. /* clear error status */
  211. mfsdram(DDR0_00, value);
  212. mtsdram(DDR0_00, value | DDR0_00_INT_ACK_ALL);
  213. /*
  214. * Clear possible errors resulting from ECC testing.
  215. * If not done, then we could get an interrupt later on when
  216. * exceptions are enabled.
  217. */
  218. set_mcsr(get_mcsr());
  219. #endif
  220. return ret;
  221. }
  222. #endif /* CONFIG_POST & CFG_POST_ECC */
  223. #endif /* CONFIG_POST */