1 // SPDX-License-Identifier: GPL-2.0+
3 * EMIF: DDR3 test commands
5 * Copyright (C) 2012-2017 Texas Instruments Incorporated, <www.ti.com>
11 #include <asm/arch/hardware.h>
12 #include <asm/cache.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 #ifdef CONFIG_ARCH_KEYSTONE
20 #include <asm/arch/ddr3.h>
21 #define DDR_MIN_ADDR CONFIG_SYS_SDRAM_BASE
22 #define STACKSIZE (512 << 10) /* 512 KiB */
24 #define DDR_REMAP_ADDR 0x80000000
25 #define ECC_START_ADDR1 ((DDR_MIN_ADDR - DDR_REMAP_ADDR) >> 17)
27 #define ECC_END_ADDR1 (((gd->start_addr_sp - DDR_REMAP_ADDR - \
28 STACKSIZE) >> 17) - 2)
31 #define DDR_TEST_BURST_SIZE 1024
33 static int ddr_memory_test(u32 start_address, u32 end_address, int quick)
35 u32 index_start, value, index;
37 index_start = start_address;
41 for (index = index_start;
42 index < index_start + DDR_TEST_BURST_SIZE;
44 __raw_writel(index, index);
46 /* Read and check the pattern */
47 for (index = index_start;
48 index < index_start + DDR_TEST_BURST_SIZE;
50 value = __raw_readl(index);
52 printf("ddr_memory_test: Failed at address index = 0x%x value = 0x%x *(index) = 0x%x\n",
53 index, value, __raw_readl(index));
59 index_start += DDR_TEST_BURST_SIZE;
60 if (index_start >= end_address)
66 /* Write a pattern for complementary values */
67 for (index = index_start;
68 index < index_start + DDR_TEST_BURST_SIZE;
70 __raw_writel((u32)~index, index);
72 /* Read and check the pattern */
73 for (index = index_start;
74 index < index_start + DDR_TEST_BURST_SIZE;
76 value = __raw_readl(index);
77 if (value != ~index) {
78 printf("ddr_memory_test: Failed at address index = 0x%x value = 0x%x *(index) = 0x%x\n",
79 index, value, __raw_readl(index));
85 index_start += DDR_TEST_BURST_SIZE;
86 if (index_start >= end_address)
90 for (index = index_start;
91 index < index_start + DDR_TEST_BURST_SIZE;
93 __raw_writew((u16)index, index);
95 /* Read and check the pattern */
96 for (index = index_start;
97 index < index_start + DDR_TEST_BURST_SIZE;
99 value = __raw_readw(index);
100 if (value != (u16)index) {
101 printf("ddr_memory_test: Failed at address index = 0x%x value = 0x%x *(index) = 0x%x\n",
102 index, value, __raw_readw(index));
108 index_start += DDR_TEST_BURST_SIZE;
109 if (index_start >= end_address)
112 /* Write a pattern */
113 for (index = index_start;
114 index < index_start + DDR_TEST_BURST_SIZE;
116 __raw_writeb((u8)index, index);
118 /* Read and check the pattern */
119 for (index = index_start;
120 index < index_start + DDR_TEST_BURST_SIZE;
122 value = __raw_readb(index);
123 if (value != (u8)index) {
124 printf("ddr_memory_test: Failed at address index = 0x%x value = 0x%x *(index) = 0x%x\n",
125 index, value, __raw_readb(index));
131 index_start += DDR_TEST_BURST_SIZE;
132 if (index_start >= end_address)
136 puts("ddr memory test PASSED!\n");
140 static int ddr_memory_compare(u32 address1, u32 address2, u32 size)
142 u32 index, value, index2, value2;
144 for (index = address1, index2 = address2;
145 index < address1 + size;
146 index += 4, index2 += 4) {
147 value = __raw_readl(index);
148 value2 = __raw_readl(index2);
150 if (value != value2) {
151 printf("ddr_memory_test: Compare failed at address = 0x%x value = 0x%x, address2 = 0x%x value2 = 0x%x\n",
152 index, value, index2, value2);
158 puts("ddr memory compare PASSED!\n");
162 static void ddr_check_ecc_status(void)
164 struct emif_reg_struct *emif = (struct emif_reg_struct *)EMIF1_BASE;
165 u32 err_1b = readl(&emif->emif_1b_ecc_err_cnt);
166 u32 int_status = readl(&emif->emif_irqstatus_raw_sys);
170 env = env_get("ecc_test");
172 ecc_test = simple_strtol(env, NULL, 0);
174 puts("ECC test Status:\n");
175 if (int_status & EMIF_INT_WR_ECC_ERR_SYS_MASK)
176 puts("\tECC test: DDR ECC write error interrupted\n");
178 if (int_status & EMIF_INT_TWOBIT_ECC_ERR_SYS_MASK)
180 panic("\tECC test: DDR ECC 2-bit error interrupted");
182 if (int_status & EMIF_INT_ONEBIT_ECC_ERR_SYS_MASK)
183 puts("\tECC test: DDR ECC 1-bit error interrupted\n");
186 printf("\tECC test: 1-bit ECC err count: 0x%x\n", err_1b);
189 static int ddr_memory_ecc_err(u32 addr, u32 ecc_err)
191 struct emif_reg_struct *emif = (struct emif_reg_struct *)EMIF1_BASE;
192 u32 ecc_ctrl = readl(&emif->emif_ecc_ctrl_reg);
193 u32 val1, val2, val3;
195 debug("Disabling D-Cache before ECC test\n");
197 invalidate_dcache_all();
199 puts("Testing DDR ECC:\n");
200 puts("\tECC test: Disabling DDR ECC ...\n");
201 writel(0, &emif->emif_ecc_ctrl_reg);
204 val2 = val1 ^ ecc_err;
208 #ifdef CONFIG_ARCH_KEYSTONE
209 ecc_ctrl = ECC_START_ADDR1 | (ECC_END_ADDR1 << 16);
210 writel(ecc_ctrl, EMIF1_BASE + KS2_DDR3_ECC_ADDR_RANGE1_OFFSET);
211 ddr3_enable_ecc(EMIF1_BASE, 1);
213 writel(ecc_ctrl, &emif->emif_ecc_ctrl_reg);
216 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",
217 addr, val1, val2, ecc_err, val3);
219 puts("\tECC test: Enabled DDR ECC ...\n");
222 printf("\tECC test: addr 0x%x, read data 0x%x\n", addr, val1);
224 ddr_check_ecc_status();
226 debug("Enabling D-cache back after ECC test\n");
232 static int is_addr_valid(u32 addr)
234 struct emif_reg_struct *emif = (struct emif_reg_struct *)EMIF1_BASE;
235 u32 start_addr, end_addr, range, ecc_ctrl;
237 #ifdef CONFIG_ARCH_KEYSTONE
238 ecc_ctrl = EMIF_ECC_REG_ECC_ADDR_RGN_1_EN_MASK;
239 range = ECC_START_ADDR1 | (ECC_END_ADDR1 << 16);
241 ecc_ctrl = readl(&emif->emif_ecc_ctrl_reg);
242 range = readl(&emif->emif_ecc_address_range_1);
245 /* Check in ecc address range 1 */
246 if (ecc_ctrl & EMIF_ECC_REG_ECC_ADDR_RGN_1_EN_MASK) {
247 start_addr = ((range & EMIF_ECC_REG_ECC_START_ADDR_MASK) << 16)
248 + CONFIG_SYS_SDRAM_BASE;
249 end_addr = (range & EMIF_ECC_REG_ECC_END_ADDR_MASK) + 0xFFFF +
250 CONFIG_SYS_SDRAM_BASE;
251 if ((addr >= start_addr) && (addr <= end_addr))
252 /* addr within ecc address range 1 */
256 /* Check in ecc address range 2 */
257 if (ecc_ctrl & EMIF_ECC_REG_ECC_ADDR_RGN_2_EN_MASK) {
258 range = readl(&emif->emif_ecc_address_range_2);
259 start_addr = ((range & EMIF_ECC_REG_ECC_START_ADDR_MASK) << 16)
260 + CONFIG_SYS_SDRAM_BASE;
261 end_addr = (range & EMIF_ECC_REG_ECC_END_ADDR_MASK) + 0xFFFF +
262 CONFIG_SYS_SDRAM_BASE;
263 if ((addr >= start_addr) && (addr <= end_addr))
264 /* addr within ecc address range 2 */
271 static int is_ecc_enabled(void)
273 struct emif_reg_struct *emif = (struct emif_reg_struct *)EMIF1_BASE;
274 u32 ecc_ctrl = readl(&emif->emif_ecc_ctrl_reg);
276 return (ecc_ctrl & EMIF_ECC_CTRL_REG_ECC_EN_MASK) &&
277 (ecc_ctrl & EMIF_ECC_REG_RMW_EN_MASK);
280 static int do_ddr_test(struct cmd_tbl *cmdtp,
281 int flag, int argc, char *const argv[])
283 u32 start_addr, end_addr, size, ecc_err;
285 if ((argc == 4) && (strncmp(argv[1], "ecc_err", 8) == 0)) {
286 if (!is_ecc_enabled()) {
287 puts("ECC not enabled. Please Enable ECC any try again\n");
288 return CMD_RET_FAILURE;
291 start_addr = simple_strtoul(argv[2], NULL, 16);
292 ecc_err = simple_strtoul(argv[3], NULL, 16);
294 if (!is_addr_valid(start_addr)) {
295 puts("Invalid address. Please enter ECC supported address!\n");
296 return CMD_RET_FAILURE;
299 ddr_memory_ecc_err(start_addr, ecc_err);
303 if (!(((argc == 4) && (strncmp(argv[1], "test", 5) == 0)) ||
304 ((argc == 5) && (strncmp(argv[1], "compare", 8) == 0))))
305 return cmd_usage(cmdtp);
307 start_addr = simple_strtoul(argv[2], NULL, 16);
308 end_addr = simple_strtoul(argv[3], NULL, 16);
310 if ((start_addr < CONFIG_SYS_SDRAM_BASE) ||
311 (start_addr > (CONFIG_SYS_SDRAM_BASE +
312 get_effective_memsize() - 1)) ||
313 (end_addr < CONFIG_SYS_SDRAM_BASE) ||
314 (end_addr > (CONFIG_SYS_SDRAM_BASE +
315 get_effective_memsize() - 1)) || (start_addr >= end_addr)) {
316 puts("Invalid start or end address!\n");
317 return cmd_usage(cmdtp);
320 puts("Please wait ...\n");
322 size = simple_strtoul(argv[4], NULL, 16);
323 ddr_memory_compare(start_addr, end_addr, size);
325 ddr_memory_test(start_addr, end_addr, 0);
331 U_BOOT_CMD(ddr, 5, 1, do_ddr_test,
333 "test <start_addr in hex> <end_addr in hex> - test DDR from start\n"
334 " address to end address\n"
335 "ddr compare <start_addr in hex> <end_addr in hex> <size in hex> -\n"
336 " compare DDR data of (size) bytes from start address to end\n"
338 "ddr ecc_err <addr in hex> <bit_err in hex> - generate bit errors\n"
339 " in DDR data at <addr>, the command will read a 32-bit data\n"
340 " from <addr>, and write (data ^ bit_err) back to <addr>\n"