cmd_ecctest.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * (C) Copyright 2010
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. #include <common.h>
  25. #include <asm/ppc4xx.h>
  26. #include <asm/processor.h>
  27. #include <asm/io.h>
  28. #include <asm/cache.h>
  29. #if defined(CONFIG_SDRAM_PPC4xx_IBM_DDR) || \
  30. defined(CONFIG_SDRAM_PPC4xx_IBM_DDR2)
  31. #if defined(CONFIG_DDR_ECC) || defined(CONFIG_SDRAM_ECC)
  32. #if defined(CONFIG_405EX)
  33. /*
  34. * Currently only 405EX uses 16bit data bus width as an alternative
  35. * option to 32bit data width (SDRAM0_MCOPT1_WDTH)
  36. */
  37. #define SDRAM_DATA_ALT_WIDTH 2
  38. #else
  39. #define SDRAM_DATA_ALT_WIDTH 8
  40. #endif
  41. #if defined(CONFIG_SYS_OCM_BASE)
  42. #define CONFIG_FUNC_ISRAM_ADDR CONFIG_SYS_OCM_BASE
  43. #endif
  44. #if defined(CONFIG_SYS_ISRAM_BASE)
  45. #define CONFIG_FUNC_ISRAM_ADDR CONFIG_SYS_ISRAM_BASE
  46. #endif
  47. #if !defined(CONFIG_FUNC_ISRAM_ADDR)
  48. #error "No internal SRAM/OCM provided!"
  49. #endif
  50. #define force_inline inline __attribute__ ((always_inline))
  51. static inline void machine_check_disable(void)
  52. {
  53. mtmsr(mfmsr() & ~MSR_ME);
  54. }
  55. static inline void machine_check_enable(void)
  56. {
  57. mtmsr(mfmsr() | MSR_ME);
  58. }
  59. /*
  60. * These helper functions need to be inlined, since they
  61. * are called from the functions running from internal SRAM.
  62. * SDRAM operation is forbidden at that time, so calling
  63. * functions in SDRAM has to be avoided.
  64. */
  65. static force_inline void wait_ddr_idle(void)
  66. {
  67. u32 val;
  68. do {
  69. mfsdram(SDRAM_MCSTAT, val);
  70. } while ((val & SDRAM_MCSTAT_IDLE_MASK) == SDRAM_MCSTAT_IDLE_NOT);
  71. }
  72. static force_inline void recalibrate_ddr(void)
  73. {
  74. u32 val;
  75. /*
  76. * Rewrite RQDC & RFDC to calibrate again. If this is not
  77. * done, the SDRAM controller is working correctly after
  78. * changing the MCOPT1_MCHK bits.
  79. */
  80. mfsdram(SDRAM_RQDC, val);
  81. mtsdram(SDRAM_RQDC, val);
  82. mfsdram(SDRAM_RFDC, val);
  83. mtsdram(SDRAM_RFDC, val);
  84. }
  85. static force_inline void set_mcopt1_mchk(u32 bits)
  86. {
  87. u32 val;
  88. wait_ddr_idle();
  89. mfsdram(SDRAM_MCOPT1, val);
  90. mtsdram(SDRAM_MCOPT1, (val & ~SDRAM_MCOPT1_MCHK_MASK) | bits);
  91. recalibrate_ddr();
  92. }
  93. /*
  94. * The next 2 functions are copied to internal SRAM/OCM and run
  95. * there. No function calls allowed here. No SDRAM acitivity should
  96. * be done here.
  97. */
  98. static void inject_ecc_error(void *ptr, int par)
  99. {
  100. u32 val;
  101. /*
  102. * Taken from PPC460EX/EXr/GT users manual (Rev 1.21)
  103. * 22.2.17.13 ECC Diagnostics
  104. *
  105. * Items 1 ... 5 are already done by now, running from RAM
  106. * with ECC enabled
  107. */
  108. out_be32(ptr, 0x00000000);
  109. val = in_be32(ptr);
  110. /* 6. Set memory controller to no error checking */
  111. set_mcopt1_mchk(SDRAM_MCOPT1_MCHK_NON);
  112. /* 7. Modify one or two bits for error simulation */
  113. if (par == 1)
  114. out_be32(ptr, in_be32(ptr) ^ 0x00000001);
  115. else
  116. out_be32(ptr, in_be32(ptr) ^ 0x00000003);
  117. /* 8. Wait for SDRAM idle */
  118. val = in_be32(ptr);
  119. set_mcopt1_mchk(SDRAM_MCOPT1_MCHK_CHK_REP);
  120. /* Wait for SDRAM idle */
  121. wait_ddr_idle();
  122. /* Continue with 9. in calling function... */
  123. }
  124. static void rewrite_ecc_parity(void *ptr, int par)
  125. {
  126. u32 current_address = (u32)ptr;
  127. u32 end_address;
  128. u32 address_increment;
  129. u32 mcopt1;
  130. u32 val;
  131. /*
  132. * Fill ECC parity byte again. Otherwise further accesses to
  133. * the failure address will result in exceptions.
  134. */
  135. /* Wait for SDRAM idle */
  136. val = in_be32(0x00000000);
  137. set_mcopt1_mchk(SDRAM_MCOPT1_MCHK_GEN);
  138. /* ECC bit set method for non-cached memory */
  139. mfsdram(SDRAM_MCOPT1, mcopt1);
  140. if ((mcopt1 & SDRAM_MCOPT1_DMWD_MASK) == SDRAM_MCOPT1_DMWD_32)
  141. address_increment = 4;
  142. else
  143. address_increment = SDRAM_DATA_ALT_WIDTH;
  144. end_address = current_address + CONFIG_SYS_CACHELINE_SIZE;
  145. while (current_address < end_address) {
  146. *((unsigned long *)current_address) = 0;
  147. current_address += address_increment;
  148. }
  149. set_mcopt1_mchk(SDRAM_MCOPT1_MCHK_CHK_REP);
  150. /* Wait for SDRAM idle */
  151. wait_ddr_idle();
  152. }
  153. static int do_ecctest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  154. {
  155. u32 old_val;
  156. u32 val;
  157. u32 *ptr;
  158. void (*sram_func)(u32 *, int);
  159. int error;
  160. if (argc < 3) {
  161. return cmd_usage(cmdtp);
  162. }
  163. ptr = (u32 *)simple_strtoul(argv[1], NULL, 16);
  164. error = simple_strtoul(argv[2], NULL, 16);
  165. if ((error < 1) || (error > 2)) {
  166. return cmd_usage(cmdtp);
  167. }
  168. printf("Using address %p for %d bit ECC error injection\n",
  169. ptr, error);
  170. /*
  171. * Save value to restore it later on
  172. */
  173. old_val = in_be32(ptr);
  174. /*
  175. * Copy ECC injection function into internal SRAM/OCM
  176. */
  177. sram_func = (void *)CONFIG_FUNC_ISRAM_ADDR;
  178. memcpy((void *)CONFIG_FUNC_ISRAM_ADDR, inject_ecc_error, 0x10000);
  179. /*
  180. * Disable interrupts and exceptions before calling this
  181. * function in internal SRAM/OCM
  182. */
  183. disable_interrupts();
  184. machine_check_disable();
  185. eieio();
  186. /*
  187. * Jump to ECC simulation function in internal SRAM/OCM
  188. */
  189. (*sram_func)(ptr, error);
  190. /* 10. Read the corresponding address */
  191. val = in_be32(ptr);
  192. /*
  193. * Read and print ECC status register/info:
  194. * The faulting address is only known upon uncorrectable ECC
  195. * errors.
  196. */
  197. mfsdram(SDRAM_ECCES, val);
  198. if (val & SDRAM_ECCES_CE)
  199. printf("ECC: Correctable error\n");
  200. if (val & SDRAM_ECCES_UE) {
  201. printf("ECC: Uncorrectable error at 0x%02x%08x\n",
  202. mfdcr(SDRAM_ERRADDULL), mfdcr(SDRAM_ERRADDLLL));
  203. }
  204. /*
  205. * Clear pending interrupts/exceptions
  206. */
  207. mtsdram(SDRAM_ECCES, 0xffffffff);
  208. mtdcr(SDRAM_ERRSTATLL, 0xff000000);
  209. set_mcsr(get_mcsr());
  210. /* Now enable interrupts and exceptions again */
  211. eieio();
  212. machine_check_enable();
  213. enable_interrupts();
  214. /*
  215. * The ECC parity byte need to be re-written for the
  216. * corresponding address. Otherwise future accesses to it
  217. * will result in exceptions.
  218. *
  219. * Jump to ECC parity generation function
  220. */
  221. memcpy((void *)CONFIG_FUNC_ISRAM_ADDR, rewrite_ecc_parity, 0x10000);
  222. (*sram_func)(ptr, 0);
  223. /*
  224. * Restore value in corresponding address
  225. */
  226. out_be32(ptr, old_val);
  227. return 0;
  228. }
  229. U_BOOT_CMD(
  230. ecctest, 3, 0, do_ecctest,
  231. "Test ECC by single and double error bit injection",
  232. "address 1/2"
  233. );
  234. #endif /* defined(CONFIG_DDR_ECC) || defined(CONFIG_SDRAM_ECC) */
  235. #endif /* defined(CONFIG_SDRAM_PPC4xx_IBM_DDR)... */