1 // SPDX-License-Identifier: GPL-2.0+
3 * EMIF: DDR3 test commands
5 * Copyright (C) 2012-2017 Texas Instruments Incorporated, <www.ti.com>
12 #include <asm/arch/hardware.h>
13 #include <asm/cache.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 #ifdef CONFIG_ARCH_KEYSTONE
21 #include <asm/arch/ddr3.h>
22 #define DDR_MIN_ADDR CONFIG_SYS_SDRAM_BASE
23 #define STACKSIZE (512 << 10) /* 512 KiB */
25 #define DDR_REMAP_ADDR 0x80000000
26 #define ECC_START_ADDR1 ((DDR_MIN_ADDR - DDR_REMAP_ADDR) >> 17)
28 #define ECC_END_ADDR1 (((gd->start_addr_sp - DDR_REMAP_ADDR - \
29 STACKSIZE) >> 17) - 2)
32 #define DDR_TEST_BURST_SIZE 1024
34 static int ddr_memory_test(u32 start_address, u32 end_address, int quick)
36 u32 index_start, value, index;
38 index_start = start_address;
42 for (index = index_start;
43 index < index_start + DDR_TEST_BURST_SIZE;
45 __raw_writel(index, index);
47 /* Read and check the pattern */
48 for (index = index_start;
49 index < index_start + DDR_TEST_BURST_SIZE;
51 value = __raw_readl(index);
53 printf("ddr_memory_test: Failed at address index = 0x%x value = 0x%x *(index) = 0x%x\n",
54 index, value, __raw_readl(index));
60 index_start += DDR_TEST_BURST_SIZE;
61 if (index_start >= end_address)
67 /* Write a pattern for complementary values */
68 for (index = index_start;
69 index < index_start + DDR_TEST_BURST_SIZE;
71 __raw_writel((u32)~index, index);
73 /* Read and check the pattern */
74 for (index = index_start;
75 index < index_start + DDR_TEST_BURST_SIZE;
77 value = __raw_readl(index);
78 if (value != ~index) {
79 printf("ddr_memory_test: Failed at address index = 0x%x value = 0x%x *(index) = 0x%x\n",
80 index, value, __raw_readl(index));
86 index_start += DDR_TEST_BURST_SIZE;
87 if (index_start >= end_address)
91 for (index = index_start;
92 index < index_start + DDR_TEST_BURST_SIZE;
94 __raw_writew((u16)index, index);
96 /* Read and check the pattern */
97 for (index = index_start;
98 index < index_start + DDR_TEST_BURST_SIZE;
100 value = __raw_readw(index);
101 if (value != (u16)index) {
102 printf("ddr_memory_test: Failed at address index = 0x%x value = 0x%x *(index) = 0x%x\n",
103 index, value, __raw_readw(index));
109 index_start += DDR_TEST_BURST_SIZE;
110 if (index_start >= end_address)
113 /* Write a pattern */
114 for (index = index_start;
115 index < index_start + DDR_TEST_BURST_SIZE;
117 __raw_writeb((u8)index, index);
119 /* Read and check the pattern */
120 for (index = index_start;
121 index < index_start + DDR_TEST_BURST_SIZE;
123 value = __raw_readb(index);
124 if (value != (u8)index) {
125 printf("ddr_memory_test: Failed at address index = 0x%x value = 0x%x *(index) = 0x%x\n",
126 index, value, __raw_readb(index));
132 index_start += DDR_TEST_BURST_SIZE;
133 if (index_start >= end_address)
137 puts("ddr memory test PASSED!\n");
141 static int ddr_memory_compare(u32 address1, u32 address2, u32 size)
143 u32 index, value, index2, value2;
145 for (index = address1, index2 = address2;
146 index < address1 + size;
147 index += 4, index2 += 4) {
148 value = __raw_readl(index);
149 value2 = __raw_readl(index2);
151 if (value != value2) {
152 printf("ddr_memory_test: Compare failed at address = 0x%x value = 0x%x, address2 = 0x%x value2 = 0x%x\n",
153 index, value, index2, value2);
159 puts("ddr memory compare PASSED!\n");
163 static void ddr_check_ecc_status(void)
165 struct emif_reg_struct *emif = (struct emif_reg_struct *)EMIF1_BASE;
166 u32 err_1b = readl(&emif->emif_1b_ecc_err_cnt);
167 u32 int_status = readl(&emif->emif_irqstatus_raw_sys);
171 env = env_get("ecc_test");
173 ecc_test = simple_strtol(env, NULL, 0);
175 puts("ECC test Status:\n");
176 if (int_status & EMIF_INT_WR_ECC_ERR_SYS_MASK)
177 puts("\tECC test: DDR ECC write error interrupted\n");
179 if (int_status & EMIF_INT_TWOBIT_ECC_ERR_SYS_MASK)
181 panic("\tECC test: DDR ECC 2-bit error interrupted");
183 if (int_status & EMIF_INT_ONEBIT_ECC_ERR_SYS_MASK)
184 puts("\tECC test: DDR ECC 1-bit error interrupted\n");
187 printf("\tECC test: 1-bit ECC err count: 0x%x\n", err_1b);
190 static int ddr_memory_ecc_err(u32 addr, u32 ecc_err)
192 struct emif_reg_struct *emif = (struct emif_reg_struct *)EMIF1_BASE;
193 u32 ecc_ctrl = readl(&emif->emif_ecc_ctrl_reg);
194 u32 val1, val2, val3;
196 debug("Disabling D-Cache before ECC test\n");
198 invalidate_dcache_all();
200 puts("Testing DDR ECC:\n");
201 puts("\tECC test: Disabling DDR ECC ...\n");
202 writel(0, &emif->emif_ecc_ctrl_reg);
205 val2 = val1 ^ ecc_err;
209 #ifdef CONFIG_ARCH_KEYSTONE
210 ecc_ctrl = ECC_START_ADDR1 | (ECC_END_ADDR1 << 16);
211 writel(ecc_ctrl, EMIF1_BASE + KS2_DDR3_ECC_ADDR_RANGE1_OFFSET);
212 ddr3_enable_ecc(EMIF1_BASE, 1);
214 writel(ecc_ctrl, &emif->emif_ecc_ctrl_reg);
217 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",
218 addr, val1, val2, ecc_err, val3);
220 puts("\tECC test: Enabled DDR ECC ...\n");
223 printf("\tECC test: addr 0x%x, read data 0x%x\n", addr, val1);
225 ddr_check_ecc_status();
227 debug("Enabling D-cache back after ECC test\n");
233 static int is_addr_valid(u32 addr)
235 struct emif_reg_struct *emif = (struct emif_reg_struct *)EMIF1_BASE;
236 u32 start_addr, end_addr, range, ecc_ctrl;
238 #ifdef CONFIG_ARCH_KEYSTONE
239 ecc_ctrl = EMIF_ECC_REG_ECC_ADDR_RGN_1_EN_MASK;
240 range = ECC_START_ADDR1 | (ECC_END_ADDR1 << 16);
242 ecc_ctrl = readl(&emif->emif_ecc_ctrl_reg);
243 range = readl(&emif->emif_ecc_address_range_1);
246 /* Check in ecc address range 1 */
247 if (ecc_ctrl & EMIF_ECC_REG_ECC_ADDR_RGN_1_EN_MASK) {
248 start_addr = ((range & EMIF_ECC_REG_ECC_START_ADDR_MASK) << 16)
249 + CONFIG_SYS_SDRAM_BASE;
250 end_addr = (range & EMIF_ECC_REG_ECC_END_ADDR_MASK) + 0xFFFF +
251 CONFIG_SYS_SDRAM_BASE;
252 if ((addr >= start_addr) && (addr <= end_addr))
253 /* addr within ecc address range 1 */
257 /* Check in ecc address range 2 */
258 if (ecc_ctrl & EMIF_ECC_REG_ECC_ADDR_RGN_2_EN_MASK) {
259 range = readl(&emif->emif_ecc_address_range_2);
260 start_addr = ((range & EMIF_ECC_REG_ECC_START_ADDR_MASK) << 16)
261 + CONFIG_SYS_SDRAM_BASE;
262 end_addr = (range & EMIF_ECC_REG_ECC_END_ADDR_MASK) + 0xFFFF +
263 CONFIG_SYS_SDRAM_BASE;
264 if ((addr >= start_addr) && (addr <= end_addr))
265 /* addr within ecc address range 2 */
272 static int is_ecc_enabled(void)
274 struct emif_reg_struct *emif = (struct emif_reg_struct *)EMIF1_BASE;
275 u32 ecc_ctrl = readl(&emif->emif_ecc_ctrl_reg);
277 return (ecc_ctrl & EMIF_ECC_CTRL_REG_ECC_EN_MASK) &&
278 (ecc_ctrl & EMIF_ECC_REG_RMW_EN_MASK);
281 static int do_ddr_test(struct cmd_tbl *cmdtp,
282 int flag, int argc, char *const argv[])
284 u32 start_addr, end_addr, size, ecc_err;
286 if ((argc == 4) && (strncmp(argv[1], "ecc_err", 8) == 0)) {
287 if (!is_ecc_enabled()) {
288 puts("ECC not enabled. Please Enable ECC any try again\n");
289 return CMD_RET_FAILURE;
292 start_addr = simple_strtoul(argv[2], NULL, 16);
293 ecc_err = simple_strtoul(argv[3], NULL, 16);
295 if (!is_addr_valid(start_addr)) {
296 puts("Invalid address. Please enter ECC supported address!\n");
297 return CMD_RET_FAILURE;
300 ddr_memory_ecc_err(start_addr, ecc_err);
304 if (!(((argc == 4) && (strncmp(argv[1], "test", 5) == 0)) ||
305 ((argc == 5) && (strncmp(argv[1], "compare", 8) == 0))))
306 return cmd_usage(cmdtp);
308 start_addr = simple_strtoul(argv[2], NULL, 16);
309 end_addr = simple_strtoul(argv[3], NULL, 16);
311 if ((start_addr < CONFIG_SYS_SDRAM_BASE) ||
312 (start_addr > (CONFIG_SYS_SDRAM_BASE +
313 get_effective_memsize() - 1)) ||
314 (end_addr < CONFIG_SYS_SDRAM_BASE) ||
315 (end_addr > (CONFIG_SYS_SDRAM_BASE +
316 get_effective_memsize() - 1)) || (start_addr >= end_addr)) {
317 puts("Invalid start or end address!\n");
318 return cmd_usage(cmdtp);
321 puts("Please wait ...\n");
323 size = simple_strtoul(argv[4], NULL, 16);
324 ddr_memory_compare(start_addr, end_addr, size);
326 ddr_memory_test(start_addr, end_addr, 0);
332 U_BOOT_CMD(ddr, 5, 1, do_ddr_test,
334 "test <start_addr in hex> <end_addr in hex> - test DDR from start\n"
335 " address to end address\n"
336 "ddr compare <start_addr in hex> <end_addr in hex> <size in hex> -\n"
337 " compare DDR data of (size) bytes from start address to end\n"
339 "ddr ecc_err <addr in hex> <bit_err in hex> - generate bit errors\n"
340 " in DDR data at <addr>, the command will read a 32-bit data\n"
341 " from <addr>, and write (data ^ bit_err) back to <addr>\n"