ddr3.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EMIF: DDR3 test commands
  4. *
  5. * Copyright (C) 2012-2017 Texas Instruments Incorporated, <www.ti.com>
  6. */
  7. #include <asm/arch/hardware.h>
  8. #include <asm/cache.h>
  9. #include <asm/emif.h>
  10. #include <common.h>
  11. #include <command.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. #ifdef CONFIG_ARCH_KEYSTONE
  14. #include <asm/arch/ddr3.h>
  15. #define DDR_MIN_ADDR CONFIG_SYS_SDRAM_BASE
  16. #define STACKSIZE (512 << 10) /* 512 KiB */
  17. #define DDR_REMAP_ADDR 0x80000000
  18. #define ECC_START_ADDR1 ((DDR_MIN_ADDR - DDR_REMAP_ADDR) >> 17)
  19. #define ECC_END_ADDR1 (((gd->start_addr_sp - DDR_REMAP_ADDR - \
  20. STACKSIZE) >> 17) - 2)
  21. #endif
  22. #define DDR_TEST_BURST_SIZE 1024
  23. static int ddr_memory_test(u32 start_address, u32 end_address, int quick)
  24. {
  25. u32 index_start, value, index;
  26. index_start = start_address;
  27. while (1) {
  28. /* Write a pattern */
  29. for (index = index_start;
  30. index < index_start + DDR_TEST_BURST_SIZE;
  31. index += 4)
  32. __raw_writel(index, index);
  33. /* Read and check the pattern */
  34. for (index = index_start;
  35. index < index_start + DDR_TEST_BURST_SIZE;
  36. index += 4) {
  37. value = __raw_readl(index);
  38. if (value != index) {
  39. printf("ddr_memory_test: Failed at address index = 0x%x value = 0x%x *(index) = 0x%x\n",
  40. index, value, __raw_readl(index));
  41. return -1;
  42. }
  43. }
  44. index_start += DDR_TEST_BURST_SIZE;
  45. if (index_start >= end_address)
  46. break;
  47. if (quick)
  48. continue;
  49. /* Write a pattern for complementary values */
  50. for (index = index_start;
  51. index < index_start + DDR_TEST_BURST_SIZE;
  52. index += 4)
  53. __raw_writel((u32)~index, index);
  54. /* Read and check the pattern */
  55. for (index = index_start;
  56. index < index_start + DDR_TEST_BURST_SIZE;
  57. index += 4) {
  58. value = __raw_readl(index);
  59. if (value != ~index) {
  60. printf("ddr_memory_test: Failed at address index = 0x%x value = 0x%x *(index) = 0x%x\n",
  61. index, value, __raw_readl(index));
  62. return -1;
  63. }
  64. }
  65. index_start += DDR_TEST_BURST_SIZE;
  66. if (index_start >= end_address)
  67. break;
  68. /* Write a pattern */
  69. for (index = index_start;
  70. index < index_start + DDR_TEST_BURST_SIZE;
  71. index += 2)
  72. __raw_writew((u16)index, index);
  73. /* Read and check the pattern */
  74. for (index = index_start;
  75. index < index_start + DDR_TEST_BURST_SIZE;
  76. index += 2) {
  77. value = __raw_readw(index);
  78. if (value != (u16)index) {
  79. printf("ddr_memory_test: Failed at address index = 0x%x value = 0x%x *(index) = 0x%x\n",
  80. index, value, __raw_readw(index));
  81. return -1;
  82. }
  83. }
  84. index_start += DDR_TEST_BURST_SIZE;
  85. if (index_start >= end_address)
  86. break;
  87. /* Write a pattern */
  88. for (index = index_start;
  89. index < index_start + DDR_TEST_BURST_SIZE;
  90. index += 1)
  91. __raw_writeb((u8)index, index);
  92. /* Read and check the pattern */
  93. for (index = index_start;
  94. index < index_start + DDR_TEST_BURST_SIZE;
  95. index += 1) {
  96. value = __raw_readb(index);
  97. if (value != (u8)index) {
  98. printf("ddr_memory_test: Failed at address index = 0x%x value = 0x%x *(index) = 0x%x\n",
  99. index, value, __raw_readb(index));
  100. return -1;
  101. }
  102. }
  103. index_start += DDR_TEST_BURST_SIZE;
  104. if (index_start >= end_address)
  105. break;
  106. }
  107. puts("ddr memory test PASSED!\n");
  108. return 0;
  109. }
  110. static int ddr_memory_compare(u32 address1, u32 address2, u32 size)
  111. {
  112. u32 index, value, index2, value2;
  113. for (index = address1, index2 = address2;
  114. index < address1 + size;
  115. index += 4, index2 += 4) {
  116. value = __raw_readl(index);
  117. value2 = __raw_readl(index2);
  118. if (value != value2) {
  119. printf("ddr_memory_test: Compare failed at address = 0x%x value = 0x%x, address2 = 0x%x value2 = 0x%x\n",
  120. index, value, index2, value2);
  121. return -1;
  122. }
  123. }
  124. puts("ddr memory compare PASSED!\n");
  125. return 0;
  126. }
  127. static void ddr_check_ecc_status(void)
  128. {
  129. struct emif_reg_struct *emif = (struct emif_reg_struct *)EMIF1_BASE;
  130. u32 err_1b = readl(&emif->emif_1b_ecc_err_cnt);
  131. u32 int_status = readl(&emif->emif_irqstatus_raw_sys);
  132. int ecc_test = 0;
  133. char *env;
  134. env = env_get("ecc_test");
  135. if (env)
  136. ecc_test = simple_strtol(env, NULL, 0);
  137. puts("ECC test Status:\n");
  138. if (int_status & EMIF_INT_WR_ECC_ERR_SYS_MASK)
  139. puts("\tECC test: DDR ECC write error interrupted\n");
  140. if (int_status & EMIF_INT_TWOBIT_ECC_ERR_SYS_MASK)
  141. if (!ecc_test)
  142. panic("\tECC test: DDR ECC 2-bit error interrupted");
  143. if (int_status & EMIF_INT_ONEBIT_ECC_ERR_SYS_MASK)
  144. puts("\tECC test: DDR ECC 1-bit error interrupted\n");
  145. if (err_1b)
  146. printf("\tECC test: 1-bit ECC err count: 0x%x\n", err_1b);
  147. }
  148. static int ddr_memory_ecc_err(u32 addr, u32 ecc_err)
  149. {
  150. struct emif_reg_struct *emif = (struct emif_reg_struct *)EMIF1_BASE;
  151. u32 ecc_ctrl = readl(&emif->emif_ecc_ctrl_reg);
  152. u32 val1, val2, val3;
  153. debug("Disabling D-Cache before ECC test\n");
  154. dcache_disable();
  155. invalidate_dcache_all();
  156. puts("Testing DDR ECC:\n");
  157. puts("\tECC test: Disabling DDR ECC ...\n");
  158. writel(0, &emif->emif_ecc_ctrl_reg);
  159. val1 = readl(addr);
  160. val2 = val1 ^ ecc_err;
  161. writel(val2, addr);
  162. val3 = readl(addr);
  163. printf("\tECC test: addr 0x%x, read data 0x%x, written data 0x%x, err pattern: 0x%x, read after write data 0x%x\n",
  164. addr, val1, val2, ecc_err, val3);
  165. puts("\tECC test: Enabling DDR ECC ...\n");
  166. #ifdef CONFIG_ARCH_KEYSTONE
  167. ecc_ctrl = ECC_START_ADDR1 | (ECC_END_ADDR1 << 16);
  168. writel(ecc_ctrl, EMIF1_BASE + KS2_DDR3_ECC_ADDR_RANGE1_OFFSET);
  169. ddr3_enable_ecc(EMIF1_BASE, 1);
  170. #else
  171. writel(ecc_ctrl, &emif->emif_ecc_ctrl_reg);
  172. #endif
  173. val1 = readl(addr);
  174. printf("\tECC test: addr 0x%x, read data 0x%x\n", addr, val1);
  175. ddr_check_ecc_status();
  176. debug("Enabling D-cache back after ECC test\n");
  177. enable_caches();
  178. return 0;
  179. }
  180. static int is_addr_valid(u32 addr)
  181. {
  182. struct emif_reg_struct *emif = (struct emif_reg_struct *)EMIF1_BASE;
  183. u32 start_addr, end_addr, range, ecc_ctrl;
  184. #ifdef CONFIG_ARCH_KEYSTONE
  185. ecc_ctrl = EMIF_ECC_REG_ECC_ADDR_RGN_1_EN_MASK;
  186. range = ECC_START_ADDR1 | (ECC_END_ADDR1 << 16);
  187. #else
  188. ecc_ctrl = readl(&emif->emif_ecc_ctrl_reg);
  189. range = readl(&emif->emif_ecc_address_range_1);
  190. #endif
  191. /* Check in ecc address range 1 */
  192. if (ecc_ctrl & EMIF_ECC_REG_ECC_ADDR_RGN_1_EN_MASK) {
  193. start_addr = ((range & EMIF_ECC_REG_ECC_START_ADDR_MASK) << 16)
  194. + CONFIG_SYS_SDRAM_BASE;
  195. end_addr = start_addr + (range & EMIF_ECC_REG_ECC_END_ADDR_MASK)
  196. + 0xFFFF;
  197. if ((addr >= start_addr) && (addr <= end_addr))
  198. /* addr within ecc address range 1 */
  199. return 1;
  200. }
  201. /* Check in ecc address range 2 */
  202. if (ecc_ctrl & EMIF_ECC_REG_ECC_ADDR_RGN_2_EN_MASK) {
  203. range = readl(&emif->emif_ecc_address_range_2);
  204. start_addr = ((range & EMIF_ECC_REG_ECC_START_ADDR_MASK) << 16)
  205. + CONFIG_SYS_SDRAM_BASE;
  206. end_addr = start_addr + (range & EMIF_ECC_REG_ECC_END_ADDR_MASK)
  207. + 0xFFFF;
  208. if ((addr >= start_addr) && (addr <= end_addr))
  209. /* addr within ecc address range 2 */
  210. return 1;
  211. }
  212. return 0;
  213. }
  214. static int is_ecc_enabled(void)
  215. {
  216. struct emif_reg_struct *emif = (struct emif_reg_struct *)EMIF1_BASE;
  217. u32 ecc_ctrl = readl(&emif->emif_ecc_ctrl_reg);
  218. return (ecc_ctrl & EMIF_ECC_CTRL_REG_ECC_EN_MASK) &&
  219. (ecc_ctrl & EMIF_ECC_REG_RMW_EN_MASK);
  220. }
  221. static int do_ddr_test(cmd_tbl_t *cmdtp,
  222. int flag, int argc, char * const argv[])
  223. {
  224. u32 start_addr, end_addr, size, ecc_err;
  225. if ((argc == 4) && (strncmp(argv[1], "ecc_err", 8) == 0)) {
  226. if (!is_ecc_enabled()) {
  227. puts("ECC not enabled. Please Enable ECC any try again\n");
  228. return CMD_RET_FAILURE;
  229. }
  230. start_addr = simple_strtoul(argv[2], NULL, 16);
  231. ecc_err = simple_strtoul(argv[3], NULL, 16);
  232. if (!is_addr_valid(start_addr)) {
  233. puts("Invalid address. Please enter ECC supported address!\n");
  234. return CMD_RET_FAILURE;
  235. }
  236. ddr_memory_ecc_err(start_addr, ecc_err);
  237. return 0;
  238. }
  239. if (!(((argc == 4) && (strncmp(argv[1], "test", 5) == 0)) ||
  240. ((argc == 5) && (strncmp(argv[1], "compare", 8) == 0))))
  241. return cmd_usage(cmdtp);
  242. start_addr = simple_strtoul(argv[2], NULL, 16);
  243. end_addr = simple_strtoul(argv[3], NULL, 16);
  244. if ((start_addr < CONFIG_SYS_SDRAM_BASE) ||
  245. (start_addr > (CONFIG_SYS_SDRAM_BASE +
  246. get_effective_memsize() - 1)) ||
  247. (end_addr < CONFIG_SYS_SDRAM_BASE) ||
  248. (end_addr > (CONFIG_SYS_SDRAM_BASE +
  249. get_effective_memsize() - 1)) || (start_addr >= end_addr)) {
  250. puts("Invalid start or end address!\n");
  251. return cmd_usage(cmdtp);
  252. }
  253. puts("Please wait ...\n");
  254. if (argc == 5) {
  255. size = simple_strtoul(argv[4], NULL, 16);
  256. ddr_memory_compare(start_addr, end_addr, size);
  257. } else {
  258. ddr_memory_test(start_addr, end_addr, 0);
  259. }
  260. return 0;
  261. }
  262. U_BOOT_CMD(ddr, 5, 1, do_ddr_test,
  263. "DDR3 test",
  264. "test <start_addr in hex> <end_addr in hex> - test DDR from start\n"
  265. " address to end address\n"
  266. "ddr compare <start_addr in hex> <end_addr in hex> <size in hex> -\n"
  267. " compare DDR data of (size) bytes from start address to end\n"
  268. " address\n"
  269. "ddr ecc_err <addr in hex> <bit_err in hex> - generate bit errors\n"
  270. " in DDR data at <addr>, the command will read a 32-bit data\n"
  271. " from <addr>, and write (data ^ bit_err) back to <addr>\n"
  272. );