3 * Stefan Roese, DENX Software Engineering, sr@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm/ppc4xx.h>
10 #include <asm/processor.h>
12 #include <asm/cache.h>
14 #if defined(CONFIG_SDRAM_PPC4xx_IBM_DDR) || \
15 defined(CONFIG_SDRAM_PPC4xx_IBM_DDR2)
16 #if defined(CONFIG_DDR_ECC) || defined(CONFIG_SDRAM_ECC)
18 #if defined(CONFIG_405EX)
20 * Currently only 405EX uses 16bit data bus width as an alternative
21 * option to 32bit data width (SDRAM0_MCOPT1_WDTH)
23 #define SDRAM_DATA_ALT_WIDTH 2
25 #define SDRAM_DATA_ALT_WIDTH 8
28 #if defined(CONFIG_SYS_OCM_BASE)
29 #define CONFIG_FUNC_ISRAM_ADDR CONFIG_SYS_OCM_BASE
32 #if defined(CONFIG_SYS_ISRAM_BASE)
33 #define CONFIG_FUNC_ISRAM_ADDR CONFIG_SYS_ISRAM_BASE
36 #if !defined(CONFIG_FUNC_ISRAM_ADDR)
37 #error "No internal SRAM/OCM provided!"
40 #define force_inline inline __attribute__ ((always_inline))
42 static inline void machine_check_disable(void)
44 mtmsr(mfmsr() & ~MSR_ME);
47 static inline void machine_check_enable(void)
49 mtmsr(mfmsr() | MSR_ME);
53 * These helper functions need to be inlined, since they
54 * are called from the functions running from internal SRAM.
55 * SDRAM operation is forbidden at that time, so calling
56 * functions in SDRAM has to be avoided.
58 static force_inline void wait_ddr_idle(void)
63 mfsdram(SDRAM_MCSTAT, val);
64 } while ((val & SDRAM_MCSTAT_IDLE_MASK) == SDRAM_MCSTAT_IDLE_NOT);
67 static force_inline void recalibrate_ddr(void)
72 * Rewrite RQDC & RFDC to calibrate again. If this is not
73 * done, the SDRAM controller is working correctly after
74 * changing the MCOPT1_MCHK bits.
76 mfsdram(SDRAM_RQDC, val);
77 mtsdram(SDRAM_RQDC, val);
78 mfsdram(SDRAM_RFDC, val);
79 mtsdram(SDRAM_RFDC, val);
82 static force_inline void set_mcopt1_mchk(u32 bits)
87 mfsdram(SDRAM_MCOPT1, val);
88 mtsdram(SDRAM_MCOPT1, (val & ~SDRAM_MCOPT1_MCHK_MASK) | bits);
93 * The next 2 functions are copied to internal SRAM/OCM and run
94 * there. No function calls allowed here. No SDRAM acitivity should
97 static void inject_ecc_error(void *ptr, int par)
100 * Taken from PPC460EX/EXr/GT users manual (Rev 1.21)
101 * 22.2.17.13 ECC Diagnostics
103 * Items 1 ... 5 are already done by now, running from RAM
107 out_be32(ptr, 0x00000000);
110 /* 6. Set memory controller to no error checking */
111 set_mcopt1_mchk(SDRAM_MCOPT1_MCHK_NON);
113 /* 7. Modify one or two bits for error simulation */
115 out_be32(ptr, in_be32(ptr) ^ 0x00000001);
117 out_be32(ptr, in_be32(ptr) ^ 0x00000003);
119 /* 8. Wait for SDRAM idle */
121 set_mcopt1_mchk(SDRAM_MCOPT1_MCHK_CHK_REP);
123 /* Wait for SDRAM idle */
126 /* Continue with 9. in calling function... */
129 static void rewrite_ecc_parity(void *ptr, int par)
131 u32 current_address = (u32)ptr;
133 u32 address_increment;
137 * Fill ECC parity byte again. Otherwise further accesses to
138 * the failure address will result in exceptions.
141 /* Wait for SDRAM idle */
143 set_mcopt1_mchk(SDRAM_MCOPT1_MCHK_GEN);
145 /* ECC bit set method for non-cached memory */
146 mfsdram(SDRAM_MCOPT1, mcopt1);
147 if ((mcopt1 & SDRAM_MCOPT1_DMWD_MASK) == SDRAM_MCOPT1_DMWD_32)
148 address_increment = 4;
150 address_increment = SDRAM_DATA_ALT_WIDTH;
151 end_address = current_address + CONFIG_SYS_CACHELINE_SIZE;
153 while (current_address < end_address) {
154 *((unsigned long *)current_address) = 0;
155 current_address += address_increment;
158 set_mcopt1_mchk(SDRAM_MCOPT1_MCHK_CHK_REP);
160 /* Wait for SDRAM idle */
164 static int do_ecctest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
169 void (*sram_func)(u32 *, int);
173 return cmd_usage(cmdtp);
176 ptr = (u32 *)simple_strtoul(argv[1], NULL, 16);
177 error = simple_strtoul(argv[2], NULL, 16);
178 if ((error < 1) || (error > 2)) {
179 return cmd_usage(cmdtp);
182 printf("Using address %p for %d bit ECC error injection\n",
186 * Save value to restore it later on
188 old_val = in_be32(ptr);
191 * Copy ECC injection function into internal SRAM/OCM
193 sram_func = (void *)CONFIG_FUNC_ISRAM_ADDR;
194 memcpy((void *)CONFIG_FUNC_ISRAM_ADDR, inject_ecc_error, 0x10000);
197 * Disable interrupts and exceptions before calling this
198 * function in internal SRAM/OCM
200 disable_interrupts();
201 machine_check_disable();
205 * Jump to ECC simulation function in internal SRAM/OCM
207 (*sram_func)(ptr, error);
209 /* 10. Read the corresponding address */
213 * Read and print ECC status register/info:
214 * The faulting address is only known upon uncorrectable ECC
217 mfsdram(SDRAM_ECCES, val);
218 if (val & SDRAM_ECCES_CE)
219 printf("ECC: Correctable error\n");
220 if (val & SDRAM_ECCES_UE) {
221 printf("ECC: Uncorrectable error at 0x%02x%08x\n",
222 mfdcr(SDRAM_ERRADDULL), mfdcr(SDRAM_ERRADDLLL));
226 * Clear pending interrupts/exceptions
228 mtsdram(SDRAM_ECCES, 0xffffffff);
229 mtdcr(SDRAM_ERRSTATLL, 0xff000000);
230 set_mcsr(get_mcsr());
232 /* Now enable interrupts and exceptions again */
234 machine_check_enable();
238 * The ECC parity byte need to be re-written for the
239 * corresponding address. Otherwise future accesses to it
240 * will result in exceptions.
242 * Jump to ECC parity generation function
244 memcpy((void *)CONFIG_FUNC_ISRAM_ADDR, rewrite_ecc_parity, 0x10000);
245 (*sram_func)(ptr, 0);
248 * Restore value in corresponding address
250 out_be32(ptr, old_val);
256 ecctest, 3, 0, do_ecctest,
257 "Test ECC by single and double error bit injection",
261 #endif /* defined(CONFIG_DDR_ECC) || defined(CONFIG_SDRAM_ECC) */
262 #endif /* defined(CONFIG_SDRAM_PPC4xx_IBM_DDR)... */