Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michael Zaidman | 4228f8b | 2010-04-07 18:30:08 +0300 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2010 |
| 4 | * Eastman Kodak Company, <www.kodak.com> |
| 5 | * Michael Zaidman, <michael.zaidman@kodak.com> |
| 6 | * |
| 7 | * The code is based on the cpu/mpc83xx/ecc.c written by |
| 8 | * Dave Liu <daveliu@freescale.com> |
Michael Zaidman | 4228f8b | 2010-04-07 18:30:08 +0300 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 12 | #include <cpu_func.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 13 | #include <irq_func.h> |
Michael Zaidman | 4228f8b | 2010-04-07 18:30:08 +0300 | [diff] [blame] | 14 | #include <mpc83xx.h> |
| 15 | #include <watchdog.h> |
| 16 | #include <asm/io.h> |
| 17 | #include <post.h> |
| 18 | |
| 19 | #if CONFIG_POST & CONFIG_SYS_POST_ECC |
| 20 | /* |
| 21 | * We use the RAW I/O accessors where possible in order to |
| 22 | * achieve performance goal, since the test's execution time |
| 23 | * affects the board start up time. |
| 24 | */ |
| 25 | static inline void ecc_clear(ddr83xx_t *ddr) |
| 26 | { |
| 27 | /* Clear capture registers */ |
| 28 | __raw_writel(0, &ddr->capture_address); |
| 29 | __raw_writel(0, &ddr->capture_data_hi); |
| 30 | __raw_writel(0, &ddr->capture_data_lo); |
| 31 | __raw_writel(0, &ddr->capture_ecc); |
| 32 | __raw_writel(0, &ddr->capture_attributes); |
| 33 | |
| 34 | /* Clear SBEC and set SBET to 1 */ |
| 35 | out_be32(&ddr->err_sbe, 1 << ECC_ERROR_MAN_SBET_SHIFT); |
| 36 | |
| 37 | /* Clear Error Detect register */ |
| 38 | out_be32(&ddr->err_detect, ECC_ERROR_DETECT_MME |\ |
| 39 | ECC_ERROR_DETECT_MBE |\ |
| 40 | ECC_ERROR_DETECT_SBE |\ |
| 41 | ECC_ERROR_DETECT_MSE); |
| 42 | |
| 43 | isync(); |
| 44 | } |
| 45 | |
| 46 | int ecc_post_test(int flags) |
| 47 | { |
| 48 | int ret = 0; |
| 49 | int int_state; |
| 50 | int errbit; |
| 51 | u32 pattern[2], writeback[2], retval[2]; |
| 52 | ddr83xx_t *ddr = &((immap_t *)CONFIG_SYS_IMMR)->ddr; |
| 53 | volatile u64 *addr = (u64 *)CONFIG_SYS_POST_ECC_START_ADDR; |
| 54 | |
| 55 | /* The pattern is written into memory to generate error */ |
| 56 | pattern[0] = 0xfedcba98UL; |
| 57 | pattern[1] = 0x76543210UL; |
| 58 | |
| 59 | /* After injecting error, re-initialize the memory with the value */ |
| 60 | writeback[0] = ~pattern[0]; |
| 61 | writeback[1] = ~pattern[1]; |
| 62 | |
| 63 | /* Check if ECC is enabled */ |
| 64 | if (__raw_readl(&ddr->err_disable) & ECC_ERROR_ENABLE) { |
| 65 | debug("DDR's ECC is not enabled, skipping the ECC POST.\n"); |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | int_state = disable_interrupts(); |
| 70 | icache_enable(); |
| 71 | |
| 72 | #ifdef CONFIG_DDR_32BIT |
| 73 | /* It seems like no one really uses the CONFIG_DDR_32BIT mode */ |
| 74 | #error "Add ECC POST support for CONFIG_DDR_32BIT here!" |
| 75 | #else |
| 76 | for (addr = (u64*)CONFIG_SYS_POST_ECC_START_ADDR, errbit=0; |
| 77 | addr < (u64*)CONFIG_SYS_POST_ECC_STOP_ADDR; addr++, errbit++ ) { |
| 78 | |
| 79 | WATCHDOG_RESET(); |
| 80 | |
| 81 | ecc_clear(ddr); |
| 82 | |
| 83 | /* Enable error injection */ |
| 84 | setbits_be32(&ddr->ecc_err_inject, ECC_ERR_INJECT_EIEN); |
| 85 | sync(); |
| 86 | isync(); |
| 87 | |
| 88 | /* Set bit to be injected */ |
| 89 | if (errbit < 32) { |
| 90 | __raw_writel(1 << errbit, &ddr->data_err_inject_lo); |
| 91 | __raw_writel(0, &ddr->data_err_inject_hi); |
| 92 | } else { |
| 93 | __raw_writel(0, &ddr->data_err_inject_lo); |
| 94 | __raw_writel(1<<(errbit-32), &ddr->data_err_inject_hi); |
| 95 | } |
| 96 | sync(); |
| 97 | isync(); |
| 98 | |
| 99 | /* Write memory location injecting SBE */ |
| 100 | ppcDWstore((u32*)addr, pattern); |
| 101 | sync(); |
| 102 | |
| 103 | /* Disable error injection */ |
| 104 | clrbits_be32(&ddr->ecc_err_inject, ECC_ERR_INJECT_EIEN); |
| 105 | sync(); |
| 106 | isync(); |
| 107 | |
| 108 | /* Data read should generate SBE */ |
| 109 | ppcDWload((u32*)addr, retval); |
| 110 | sync(); |
| 111 | |
| 112 | if (!(__raw_readl(&ddr->err_detect) & ECC_ERROR_DETECT_SBE) || |
| 113 | (__raw_readl(&ddr->data_err_inject_hi) != |
| 114 | (__raw_readl(&ddr->capture_data_hi) ^ pattern[0])) || |
| 115 | (__raw_readl(&ddr->data_err_inject_lo) != |
| 116 | (__raw_readl(&ddr->capture_data_lo) ^ pattern[1]))) { |
| 117 | |
| 118 | post_log("ECC failed to detect SBE error at %08x, " |
| 119 | "SBE injection mask %08x-%08x, wrote " |
| 120 | "%08x-%08x, read %08x-%08x\n", addr, |
| 121 | ddr->data_err_inject_hi, |
| 122 | ddr->data_err_inject_lo, |
| 123 | pattern[0], pattern[1], |
| 124 | retval[0], retval[1]); |
| 125 | |
| 126 | printf("ERR_DETECT Reg: %08x\n", ddr->err_detect); |
| 127 | printf("ECC CAPTURE_DATA Reg: %08x-%08x\n", |
| 128 | ddr->capture_data_hi, ddr->capture_data_lo); |
| 129 | ret = 1; |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | /* Re-initialize the ECC memory */ |
| 134 | ppcDWstore((u32*)addr, writeback); |
| 135 | sync(); |
| 136 | isync(); |
| 137 | |
| 138 | errbit %= 63; |
| 139 | } |
| 140 | #endif /* !CONFIG_DDR_32BIT */ |
| 141 | |
| 142 | ecc_clear(ddr); |
| 143 | |
| 144 | icache_disable(); |
| 145 | |
| 146 | if (int_state) |
| 147 | enable_interrupts(); |
| 148 | |
| 149 | return ret; |
| 150 | } |
| 151 | #endif |