ecc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2007-2011 Freescale Semiconductor, Inc.
  4. *
  5. * Dave Liu <daveliu@freescale.com>
  6. * based on the contribution of Marian Balakowicz <m8@semihalf.com>
  7. */
  8. #include <common.h>
  9. #include <mpc83xx.h>
  10. #include <command.h>
  11. #if defined(CONFIG_DDR_ECC) && defined(CONFIG_DDR_ECC_CMD)
  12. void ecc_print_status(void)
  13. {
  14. immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  15. #ifdef CONFIG_SYS_FSL_DDR2
  16. struct ccsr_ddr __iomem *ddr = &immap->ddr;
  17. #else
  18. ddr83xx_t *ddr = &immap->ddr;
  19. #endif
  20. printf("\nECC mode: %s\n\n",
  21. (ddr->sdram_cfg & SDRAM_CFG_ECC_EN) ? "ON" : "OFF");
  22. /* Interrupts */
  23. printf("Memory Error Interrupt Enable:\n");
  24. printf(" Multiple-Bit Error Interrupt Enable: %d\n",
  25. (ddr->err_int_en & ECC_ERR_INT_EN_MBEE) ? 1 : 0);
  26. printf(" Single-Bit Error Interrupt Enable: %d\n",
  27. (ddr->err_int_en & ECC_ERR_INT_EN_SBEE) ? 1 : 0);
  28. printf(" Memory Select Error Interrupt Enable: %d\n\n",
  29. (ddr->err_int_en & ECC_ERR_INT_EN_MSEE) ? 1 : 0);
  30. /* Error disable */
  31. printf("Memory Error Disable:\n");
  32. printf(" Multiple-Bit Error Disable: %d\n",
  33. (ddr->err_disable & ECC_ERROR_DISABLE_MBED) ? 1 : 0);
  34. printf(" Single-Bit Error Disable: %d\n",
  35. (ddr->err_disable & ECC_ERROR_DISABLE_SBED) ? 1 : 0);
  36. printf(" Memory Select Error Disable: %d\n\n",
  37. (ddr->err_disable & ECC_ERROR_DISABLE_MSED) ? 1 : 0);
  38. /* Error injection */
  39. printf("Memory Data Path Error Injection Mask High/Low: %08x %08x\n",
  40. ddr->data_err_inject_hi, ddr->data_err_inject_lo);
  41. printf("Memory Data Path Error Injection Mask ECC:\n");
  42. printf(" ECC Mirror Byte: %d\n",
  43. (ddr->ecc_err_inject & ECC_ERR_INJECT_EMB) ? 1 : 0);
  44. printf(" ECC Injection Enable: %d\n",
  45. (ddr->ecc_err_inject & ECC_ERR_INJECT_EIEN) ? 1 : 0);
  46. printf(" ECC Error Injection Mask: 0x%02x\n\n",
  47. ddr->ecc_err_inject & ECC_ERR_INJECT_EEIM);
  48. /* SBE counter/threshold */
  49. printf("Memory Single-Bit Error Management (0..255):\n");
  50. printf(" Single-Bit Error Threshold: %d\n",
  51. (ddr->err_sbe & ECC_ERROR_MAN_SBET) >> ECC_ERROR_MAN_SBET_SHIFT);
  52. printf(" Single-Bit Error Counter: %d\n\n",
  53. (ddr->err_sbe & ECC_ERROR_MAN_SBEC) >> ECC_ERROR_MAN_SBEC_SHIFT);
  54. /* Error detect */
  55. printf("Memory Error Detect:\n");
  56. printf(" Multiple Memory Errors: %d\n",
  57. (ddr->err_detect & ECC_ERROR_DETECT_MME) ? 1 : 0);
  58. printf(" Multiple-Bit Error: %d\n",
  59. (ddr->err_detect & ECC_ERROR_DETECT_MBE) ? 1 : 0);
  60. printf(" Single-Bit Error: %d\n",
  61. (ddr->err_detect & ECC_ERROR_DETECT_SBE) ? 1 : 0);
  62. printf(" Memory Select Error: %d\n\n",
  63. (ddr->err_detect & ECC_ERROR_DETECT_MSE) ? 1 : 0);
  64. /* Capture data */
  65. printf("Memory Error Address Capture: 0x%08x\n", ddr->capture_address);
  66. printf("Memory Data Path Read Capture High/Low: %08x %08x\n",
  67. ddr->capture_data_hi, ddr->capture_data_lo);
  68. printf("Memory Data Path Read Capture ECC: 0x%02x\n\n",
  69. ddr->capture_ecc & CAPTURE_ECC_ECE);
  70. printf("Memory Error Attributes Capture:\n");
  71. printf(" Data Beat Number: %d\n",
  72. (ddr->capture_attributes & ECC_CAPT_ATTR_BNUM) >>
  73. ECC_CAPT_ATTR_BNUM_SHIFT);
  74. printf(" Transaction Size: %d\n",
  75. (ddr->capture_attributes & ECC_CAPT_ATTR_TSIZ) >>
  76. ECC_CAPT_ATTR_TSIZ_SHIFT);
  77. printf(" Transaction Source: %d\n",
  78. (ddr->capture_attributes & ECC_CAPT_ATTR_TSRC) >>
  79. ECC_CAPT_ATTR_TSRC_SHIFT);
  80. printf(" Transaction Type: %d\n",
  81. (ddr->capture_attributes & ECC_CAPT_ATTR_TTYP) >>
  82. ECC_CAPT_ATTR_TTYP_SHIFT);
  83. printf(" Error Information Valid: %d\n\n",
  84. ddr->capture_attributes & ECC_CAPT_ATTR_VLD);
  85. }
  86. int do_ecc(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  87. {
  88. immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  89. #ifdef CONFIG_SYS_FSL_DDR2
  90. struct ccsr_ddr __iomem *ddr = &immap->ddr;
  91. #else
  92. ddr83xx_t *ddr = &immap->ddr;
  93. #endif
  94. volatile u32 val;
  95. u64 *addr;
  96. u32 count;
  97. register u64 *i;
  98. u32 ret[2];
  99. u32 pattern[2];
  100. u32 writeback[2];
  101. /* The pattern is written into memory to generate error */
  102. pattern[0] = 0xfedcba98UL;
  103. pattern[1] = 0x76543210UL;
  104. /* After injecting error, re-initialize the memory with the value */
  105. writeback[0] = 0x01234567UL;
  106. writeback[1] = 0x89abcdefUL;
  107. if (argc > 4)
  108. return cmd_usage(cmdtp);
  109. if (argc == 2) {
  110. if (strcmp(argv[1], "status") == 0) {
  111. ecc_print_status();
  112. return 0;
  113. } else if (strcmp(argv[1], "captureclear") == 0) {
  114. ddr->capture_address = 0;
  115. ddr->capture_data_hi = 0;
  116. ddr->capture_data_lo = 0;
  117. ddr->capture_ecc = 0;
  118. ddr->capture_attributes = 0;
  119. return 0;
  120. }
  121. }
  122. if (argc == 3) {
  123. if (strcmp(argv[1], "sbecnt") == 0) {
  124. val = simple_strtoul(argv[2], NULL, 10);
  125. if (val > 255) {
  126. printf("Incorrect Counter value, "
  127. "should be 0..255\n");
  128. return 1;
  129. }
  130. val = (val << ECC_ERROR_MAN_SBEC_SHIFT);
  131. val |= (ddr->err_sbe & ECC_ERROR_MAN_SBET);
  132. ddr->err_sbe = val;
  133. return 0;
  134. } else if (strcmp(argv[1], "sbethr") == 0) {
  135. val = simple_strtoul(argv[2], NULL, 10);
  136. if (val > 255) {
  137. printf("Incorrect Counter value, "
  138. "should be 0..255\n");
  139. return 1;
  140. }
  141. val = (val << ECC_ERROR_MAN_SBET_SHIFT);
  142. val |= (ddr->err_sbe & ECC_ERROR_MAN_SBEC);
  143. ddr->err_sbe = val;
  144. return 0;
  145. } else if (strcmp(argv[1], "errdisable") == 0) {
  146. val = ddr->err_disable;
  147. if (strcmp(argv[2], "+sbe") == 0) {
  148. val |= ECC_ERROR_DISABLE_SBED;
  149. } else if (strcmp(argv[2], "+mbe") == 0) {
  150. val |= ECC_ERROR_DISABLE_MBED;
  151. } else if (strcmp(argv[2], "+mse") == 0) {
  152. val |= ECC_ERROR_DISABLE_MSED;
  153. } else if (strcmp(argv[2], "+all") == 0) {
  154. val |= (ECC_ERROR_DISABLE_SBED |
  155. ECC_ERROR_DISABLE_MBED |
  156. ECC_ERROR_DISABLE_MSED);
  157. } else if (strcmp(argv[2], "-sbe") == 0) {
  158. val &= ~ECC_ERROR_DISABLE_SBED;
  159. } else if (strcmp(argv[2], "-mbe") == 0) {
  160. val &= ~ECC_ERROR_DISABLE_MBED;
  161. } else if (strcmp(argv[2], "-mse") == 0) {
  162. val &= ~ECC_ERROR_DISABLE_MSED;
  163. } else if (strcmp(argv[2], "-all") == 0) {
  164. val &= ~(ECC_ERROR_DISABLE_SBED |
  165. ECC_ERROR_DISABLE_MBED |
  166. ECC_ERROR_DISABLE_MSED);
  167. } else {
  168. printf("Incorrect err_disable field\n");
  169. return 1;
  170. }
  171. ddr->err_disable = val;
  172. __asm__ __volatile__("sync");
  173. __asm__ __volatile__("isync");
  174. return 0;
  175. } else if (strcmp(argv[1], "errdetectclr") == 0) {
  176. val = ddr->err_detect;
  177. if (strcmp(argv[2], "mme") == 0) {
  178. val |= ECC_ERROR_DETECT_MME;
  179. } else if (strcmp(argv[2], "sbe") == 0) {
  180. val |= ECC_ERROR_DETECT_SBE;
  181. } else if (strcmp(argv[2], "mbe") == 0) {
  182. val |= ECC_ERROR_DETECT_MBE;
  183. } else if (strcmp(argv[2], "mse") == 0) {
  184. val |= ECC_ERROR_DETECT_MSE;
  185. } else if (strcmp(argv[2], "all") == 0) {
  186. val |= (ECC_ERROR_DETECT_MME |
  187. ECC_ERROR_DETECT_MBE |
  188. ECC_ERROR_DETECT_SBE |
  189. ECC_ERROR_DETECT_MSE);
  190. } else {
  191. printf("Incorrect err_detect field\n");
  192. return 1;
  193. }
  194. ddr->err_detect = val;
  195. return 0;
  196. } else if (strcmp(argv[1], "injectdatahi") == 0) {
  197. val = simple_strtoul(argv[2], NULL, 16);
  198. ddr->data_err_inject_hi = val;
  199. return 0;
  200. } else if (strcmp(argv[1], "injectdatalo") == 0) {
  201. val = simple_strtoul(argv[2], NULL, 16);
  202. ddr->data_err_inject_lo = val;
  203. return 0;
  204. } else if (strcmp(argv[1], "injectecc") == 0) {
  205. val = simple_strtoul(argv[2], NULL, 16);
  206. if (val > 0xff) {
  207. printf("Incorrect ECC inject mask, "
  208. "should be 0x00..0xff\n");
  209. return 1;
  210. }
  211. val |= (ddr->ecc_err_inject & ~ECC_ERR_INJECT_EEIM);
  212. ddr->ecc_err_inject = val;
  213. return 0;
  214. } else if (strcmp(argv[1], "inject") == 0) {
  215. val = ddr->ecc_err_inject;
  216. if (strcmp(argv[2], "en") == 0)
  217. val |= ECC_ERR_INJECT_EIEN;
  218. else if (strcmp(argv[2], "dis") == 0)
  219. val &= ~ECC_ERR_INJECT_EIEN;
  220. else
  221. printf("Incorrect command\n");
  222. ddr->ecc_err_inject = val;
  223. __asm__ __volatile__("sync");
  224. __asm__ __volatile__("isync");
  225. return 0;
  226. } else if (strcmp(argv[1], "mirror") == 0) {
  227. val = ddr->ecc_err_inject;
  228. if (strcmp(argv[2], "en") == 0)
  229. val |= ECC_ERR_INJECT_EMB;
  230. else if (strcmp(argv[2], "dis") == 0)
  231. val &= ~ECC_ERR_INJECT_EMB;
  232. else
  233. printf("Incorrect command\n");
  234. ddr->ecc_err_inject = val;
  235. return 0;
  236. }
  237. }
  238. if (argc == 4) {
  239. if (strcmp(argv[1], "testdw") == 0) {
  240. addr = (u64 *) simple_strtoul(argv[2], NULL, 16);
  241. count = simple_strtoul(argv[3], NULL, 16);
  242. if ((u32) addr % 8) {
  243. printf("Address not aligned on "
  244. "double word boundary\n");
  245. return 1;
  246. }
  247. disable_interrupts();
  248. for (i = addr; i < addr + count; i++) {
  249. /* enable injects */
  250. ddr->ecc_err_inject |= ECC_ERR_INJECT_EIEN;
  251. __asm__ __volatile__("sync");
  252. __asm__ __volatile__("isync");
  253. /* write memory location injecting errors */
  254. ppcDWstore((u32 *) i, pattern);
  255. __asm__ __volatile__("sync");
  256. /* disable injects */
  257. ddr->ecc_err_inject &= ~ECC_ERR_INJECT_EIEN;
  258. __asm__ __volatile__("sync");
  259. __asm__ __volatile__("isync");
  260. /* read data, this generates ECC error */
  261. ppcDWload((u32 *) i, ret);
  262. __asm__ __volatile__("sync");
  263. /* re-initialize memory, double word write the location again,
  264. * generates new ECC code this time */
  265. ppcDWstore((u32 *) i, writeback);
  266. __asm__ __volatile__("sync");
  267. }
  268. enable_interrupts();
  269. return 0;
  270. }
  271. if (strcmp(argv[1], "testword") == 0) {
  272. addr = (u64 *) simple_strtoul(argv[2], NULL, 16);
  273. count = simple_strtoul(argv[3], NULL, 16);
  274. if ((u32) addr % 8) {
  275. printf("Address not aligned on "
  276. "double word boundary\n");
  277. return 1;
  278. }
  279. disable_interrupts();
  280. for (i = addr; i < addr + count; i++) {
  281. /* enable injects */
  282. ddr->ecc_err_inject |= ECC_ERR_INJECT_EIEN;
  283. __asm__ __volatile__("sync");
  284. __asm__ __volatile__("isync");
  285. /* write memory location injecting errors */
  286. *(u32 *) i = 0xfedcba98UL;
  287. __asm__ __volatile__("sync");
  288. /* sub double word write,
  289. * bus will read-modify-write,
  290. * generates ECC error */
  291. *((u32 *) i + 1) = 0x76543210UL;
  292. __asm__ __volatile__("sync");
  293. /* disable injects */
  294. ddr->ecc_err_inject &= ~ECC_ERR_INJECT_EIEN;
  295. __asm__ __volatile__("sync");
  296. __asm__ __volatile__("isync");
  297. /* re-initialize memory,
  298. * double word write the location again,
  299. * generates new ECC code this time */
  300. ppcDWstore((u32 *) i, writeback);
  301. __asm__ __volatile__("sync");
  302. }
  303. enable_interrupts();
  304. return 0;
  305. }
  306. }
  307. return cmd_usage(cmdtp);
  308. }
  309. U_BOOT_CMD(ecc, 4, 0, do_ecc,
  310. "support for DDR ECC features",
  311. "status - print out status info\n"
  312. "ecc captureclear - clear capture regs data\n"
  313. "ecc sbecnt <val> - set Single-Bit Error counter\n"
  314. "ecc sbethr <val> - set Single-Bit Threshold\n"
  315. "ecc errdisable <flag> - clear/set disable Memory Error Disable, flag:\n"
  316. " [-|+]sbe - Single-Bit Error\n"
  317. " [-|+]mbe - Multiple-Bit Error\n"
  318. " [-|+]mse - Memory Select Error\n"
  319. " [-|+]all - all errors\n"
  320. "ecc errdetectclr <flag> - clear Memory Error Detect, flag:\n"
  321. " mme - Multiple Memory Errors\n"
  322. " sbe - Single-Bit Error\n"
  323. " mbe - Multiple-Bit Error\n"
  324. " mse - Memory Select Error\n"
  325. " all - all errors\n"
  326. "ecc injectdatahi <hi> - set Memory Data Path Error Injection Mask High\n"
  327. "ecc injectdatalo <lo> - set Memory Data Path Error Injection Mask Low\n"
  328. "ecc injectecc <ecc> - set ECC Error Injection Mask\n"
  329. "ecc inject <en|dis> - enable/disable error injection\n"
  330. "ecc mirror <en|dis> - enable/disable mirror byte\n"
  331. "ecc testdw <addr> <cnt> - test mem region with double word access:\n"
  332. " - enables injects\n"
  333. " - writes pattern injecting errors with double word access\n"
  334. " - disables injects\n"
  335. " - reads pattern back with double word access, generates error\n"
  336. " - re-inits memory\n"
  337. "ecc testword <addr> <cnt> - test mem region with word access:\n"
  338. " - enables injects\n"
  339. " - writes pattern injecting errors with word access\n"
  340. " - writes pattern with word access, generates error\n"
  341. " - disables injects\n" " - re-inits memory");
  342. #endif