York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Freescale Memory Controller kernel module |
| 3 | * |
| 4 | * Support Power-based SoCs including MPC85xx, MPC86xx, MPC83xx and |
| 5 | * ARM-based Layerscape SoCs including LS2xxx. Originally split |
| 6 | * out from mpc85xx_edac EDAC driver. |
| 7 | * |
| 8 | * Parts Copyrighted (c) 2013 by Freescale Semiconductor, Inc. |
| 9 | * |
| 10 | * Author: Dave Jiang <djiang@mvista.com> |
| 11 | * |
| 12 | * 2006-2007 (c) MontaVista Software, Inc. This file is licensed under |
| 13 | * the terms of the GNU General Public License version 2. This program |
| 14 | * is licensed "as is" without any warranty of any kind, whether express |
| 15 | * or implied. |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 16 | */ |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/ctype.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/mod_devicetable.h> |
| 23 | #include <linux/edac.h> |
| 24 | #include <linux/smp.h> |
| 25 | #include <linux/gfp.h> |
| 26 | |
| 27 | #include <linux/of_platform.h> |
| 28 | #include <linux/of_device.h> |
| 29 | #include "edac_module.h" |
| 30 | #include "edac_core.h" |
| 31 | #include "fsl_ddr_edac.h" |
| 32 | |
| 33 | #define EDAC_MOD_STR "fsl_ddr_edac" |
| 34 | |
| 35 | static int edac_mc_idx; |
| 36 | |
| 37 | static u32 orig_ddr_err_disable; |
| 38 | static u32 orig_ddr_err_sbe; |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 39 | static bool little_endian; |
| 40 | |
| 41 | static inline u32 ddr_in32(void __iomem *addr) |
| 42 | { |
| 43 | return little_endian ? ioread32(addr) : ioread32be(addr); |
| 44 | } |
| 45 | |
| 46 | static inline void ddr_out32(void __iomem *addr, u32 value) |
| 47 | { |
| 48 | if (little_endian) |
| 49 | iowrite32(value, addr); |
| 50 | else |
| 51 | iowrite32be(value, addr); |
| 52 | } |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 53 | |
| 54 | /************************ MC SYSFS parts ***********************************/ |
| 55 | |
| 56 | #define to_mci(k) container_of(k, struct mem_ctl_info, dev) |
| 57 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 58 | static ssize_t fsl_mc_inject_data_hi_show(struct device *dev, |
| 59 | struct device_attribute *mattr, |
| 60 | char *data) |
| 61 | { |
| 62 | struct mem_ctl_info *mci = to_mci(dev); |
| 63 | struct fsl_mc_pdata *pdata = mci->pvt_info; |
| 64 | return sprintf(data, "0x%08x", |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 65 | ddr_in32(pdata->mc_vbase + FSL_MC_DATA_ERR_INJECT_HI)); |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static ssize_t fsl_mc_inject_data_lo_show(struct device *dev, |
| 69 | struct device_attribute *mattr, |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 70 | char *data) |
| 71 | { |
| 72 | struct mem_ctl_info *mci = to_mci(dev); |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 73 | struct fsl_mc_pdata *pdata = mci->pvt_info; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 74 | return sprintf(data, "0x%08x", |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 75 | ddr_in32(pdata->mc_vbase + FSL_MC_DATA_ERR_INJECT_LO)); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 76 | } |
| 77 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 78 | static ssize_t fsl_mc_inject_ctrl_show(struct device *dev, |
| 79 | struct device_attribute *mattr, |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 80 | char *data) |
| 81 | { |
| 82 | struct mem_ctl_info *mci = to_mci(dev); |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 83 | struct fsl_mc_pdata *pdata = mci->pvt_info; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 84 | return sprintf(data, "0x%08x", |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 85 | ddr_in32(pdata->mc_vbase + FSL_MC_ECC_ERR_INJECT)); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 86 | } |
| 87 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 88 | static ssize_t fsl_mc_inject_data_hi_store(struct device *dev, |
| 89 | struct device_attribute *mattr, |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 90 | const char *data, size_t count) |
| 91 | { |
| 92 | struct mem_ctl_info *mci = to_mci(dev); |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 93 | struct fsl_mc_pdata *pdata = mci->pvt_info; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 94 | if (isdigit(*data)) { |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 95 | ddr_out32(pdata->mc_vbase + FSL_MC_DATA_ERR_INJECT_HI, |
| 96 | simple_strtoul(data, NULL, 0)); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 97 | return count; |
| 98 | } |
| 99 | return 0; |
| 100 | } |
| 101 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 102 | static ssize_t fsl_mc_inject_data_lo_store(struct device *dev, |
| 103 | struct device_attribute *mattr, |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 104 | const char *data, size_t count) |
| 105 | { |
| 106 | struct mem_ctl_info *mci = to_mci(dev); |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 107 | struct fsl_mc_pdata *pdata = mci->pvt_info; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 108 | if (isdigit(*data)) { |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 109 | ddr_out32(pdata->mc_vbase + FSL_MC_DATA_ERR_INJECT_LO, |
| 110 | simple_strtoul(data, NULL, 0)); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 111 | return count; |
| 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 116 | static ssize_t fsl_mc_inject_ctrl_store(struct device *dev, |
| 117 | struct device_attribute *mattr, |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 118 | const char *data, size_t count) |
| 119 | { |
| 120 | struct mem_ctl_info *mci = to_mci(dev); |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 121 | struct fsl_mc_pdata *pdata = mci->pvt_info; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 122 | if (isdigit(*data)) { |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 123 | ddr_out32(pdata->mc_vbase + FSL_MC_ECC_ERR_INJECT, |
| 124 | simple_strtoul(data, NULL, 0)); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 125 | return count; |
| 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | DEVICE_ATTR(inject_data_hi, S_IRUGO | S_IWUSR, |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 131 | fsl_mc_inject_data_hi_show, fsl_mc_inject_data_hi_store); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 132 | DEVICE_ATTR(inject_data_lo, S_IRUGO | S_IWUSR, |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 133 | fsl_mc_inject_data_lo_show, fsl_mc_inject_data_lo_store); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 134 | DEVICE_ATTR(inject_ctrl, S_IRUGO | S_IWUSR, |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 135 | fsl_mc_inject_ctrl_show, fsl_mc_inject_ctrl_store); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 136 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 137 | static struct attribute *fsl_ddr_dev_attrs[] = { |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 138 | &dev_attr_inject_data_hi.attr, |
| 139 | &dev_attr_inject_data_lo.attr, |
| 140 | &dev_attr_inject_ctrl.attr, |
| 141 | NULL |
| 142 | }; |
| 143 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 144 | ATTRIBUTE_GROUPS(fsl_ddr_dev); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 145 | |
| 146 | /**************************** MC Err device ***************************/ |
| 147 | |
| 148 | /* |
| 149 | * Taken from table 8-55 in the MPC8641 User's Manual and/or 9-61 in the |
| 150 | * MPC8572 User's Manual. Each line represents a syndrome bit column as a |
| 151 | * 64-bit value, but split into an upper and lower 32-bit chunk. The labels |
| 152 | * below correspond to Freescale's manuals. |
| 153 | */ |
| 154 | static unsigned int ecc_table[16] = { |
| 155 | /* MSB LSB */ |
| 156 | /* [0:31] [32:63] */ |
| 157 | 0xf00fe11e, 0xc33c0ff7, /* Syndrome bit 7 */ |
| 158 | 0x00ff00ff, 0x00fff0ff, |
| 159 | 0x0f0f0f0f, 0x0f0fff00, |
| 160 | 0x11113333, 0x7777000f, |
| 161 | 0x22224444, 0x8888222f, |
| 162 | 0x44448888, 0xffff4441, |
| 163 | 0x8888ffff, 0x11118882, |
| 164 | 0xffff1111, 0x22221114, /* Syndrome bit 0 */ |
| 165 | }; |
| 166 | |
| 167 | /* |
| 168 | * Calculate the correct ECC value for a 64-bit value specified by high:low |
| 169 | */ |
| 170 | static u8 calculate_ecc(u32 high, u32 low) |
| 171 | { |
| 172 | u32 mask_low; |
| 173 | u32 mask_high; |
| 174 | int bit_cnt; |
| 175 | u8 ecc = 0; |
| 176 | int i; |
| 177 | int j; |
| 178 | |
| 179 | for (i = 0; i < 8; i++) { |
| 180 | mask_high = ecc_table[i * 2]; |
| 181 | mask_low = ecc_table[i * 2 + 1]; |
| 182 | bit_cnt = 0; |
| 183 | |
| 184 | for (j = 0; j < 32; j++) { |
| 185 | if ((mask_high >> j) & 1) |
| 186 | bit_cnt ^= (high >> j) & 1; |
| 187 | if ((mask_low >> j) & 1) |
| 188 | bit_cnt ^= (low >> j) & 1; |
| 189 | } |
| 190 | |
| 191 | ecc |= bit_cnt << i; |
| 192 | } |
| 193 | |
| 194 | return ecc; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Create the syndrome code which is generated if the data line specified by |
| 199 | * 'bit' failed. Eg generate an 8-bit codes seen in Table 8-55 in the MPC8641 |
| 200 | * User's Manual and 9-61 in the MPC8572 User's Manual. |
| 201 | */ |
| 202 | static u8 syndrome_from_bit(unsigned int bit) { |
| 203 | int i; |
| 204 | u8 syndrome = 0; |
| 205 | |
| 206 | /* |
| 207 | * Cycle through the upper or lower 32-bit portion of each value in |
| 208 | * ecc_table depending on if 'bit' is in the upper or lower half of |
| 209 | * 64-bit data. |
| 210 | */ |
| 211 | for (i = bit < 32; i < 16; i += 2) |
| 212 | syndrome |= ((ecc_table[i] >> (bit % 32)) & 1) << (i / 2); |
| 213 | |
| 214 | return syndrome; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Decode data and ecc syndrome to determine what went wrong |
| 219 | * Note: This can only decode single-bit errors |
| 220 | */ |
| 221 | static void sbe_ecc_decode(u32 cap_high, u32 cap_low, u32 cap_ecc, |
| 222 | int *bad_data_bit, int *bad_ecc_bit) |
| 223 | { |
| 224 | int i; |
| 225 | u8 syndrome; |
| 226 | |
| 227 | *bad_data_bit = -1; |
| 228 | *bad_ecc_bit = -1; |
| 229 | |
| 230 | /* |
| 231 | * Calculate the ECC of the captured data and XOR it with the captured |
| 232 | * ECC to find an ECC syndrome value we can search for |
| 233 | */ |
| 234 | syndrome = calculate_ecc(cap_high, cap_low) ^ cap_ecc; |
| 235 | |
| 236 | /* Check if a data line is stuck... */ |
| 237 | for (i = 0; i < 64; i++) { |
| 238 | if (syndrome == syndrome_from_bit(i)) { |
| 239 | *bad_data_bit = i; |
| 240 | return; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* If data is correct, check ECC bits for errors... */ |
| 245 | for (i = 0; i < 8; i++) { |
| 246 | if ((syndrome >> i) & 0x1) { |
| 247 | *bad_ecc_bit = i; |
| 248 | return; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | #define make64(high, low) (((u64)(high) << 32) | (low)) |
| 254 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 255 | static void fsl_mc_check(struct mem_ctl_info *mci) |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 256 | { |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 257 | struct fsl_mc_pdata *pdata = mci->pvt_info; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 258 | struct csrow_info *csrow; |
| 259 | u32 bus_width; |
| 260 | u32 err_detect; |
| 261 | u32 syndrome; |
| 262 | u64 err_addr; |
| 263 | u32 pfn; |
| 264 | int row_index; |
| 265 | u32 cap_high; |
| 266 | u32 cap_low; |
| 267 | int bad_data_bit; |
| 268 | int bad_ecc_bit; |
| 269 | |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 270 | err_detect = ddr_in32(pdata->mc_vbase + FSL_MC_ERR_DETECT); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 271 | if (!err_detect) |
| 272 | return; |
| 273 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 274 | fsl_mc_printk(mci, KERN_ERR, "Err Detect Register: %#8.8x\n", |
| 275 | err_detect); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 276 | |
| 277 | /* no more processing if not ECC bit errors */ |
| 278 | if (!(err_detect & (DDR_EDE_SBE | DDR_EDE_MBE))) { |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 279 | ddr_out32(pdata->mc_vbase + FSL_MC_ERR_DETECT, err_detect); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 280 | return; |
| 281 | } |
| 282 | |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 283 | syndrome = ddr_in32(pdata->mc_vbase + FSL_MC_CAPTURE_ECC); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 284 | |
| 285 | /* Mask off appropriate bits of syndrome based on bus width */ |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 286 | bus_width = (ddr_in32(pdata->mc_vbase + FSL_MC_DDR_SDRAM_CFG) & |
| 287 | DSC_DBW_MASK) ? 32 : 64; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 288 | if (bus_width == 64) |
| 289 | syndrome &= 0xff; |
| 290 | else |
| 291 | syndrome &= 0xffff; |
| 292 | |
| 293 | err_addr = make64( |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 294 | ddr_in32(pdata->mc_vbase + FSL_MC_CAPTURE_EXT_ADDRESS), |
| 295 | ddr_in32(pdata->mc_vbase + FSL_MC_CAPTURE_ADDRESS)); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 296 | pfn = err_addr >> PAGE_SHIFT; |
| 297 | |
| 298 | for (row_index = 0; row_index < mci->nr_csrows; row_index++) { |
| 299 | csrow = mci->csrows[row_index]; |
| 300 | if ((pfn >= csrow->first_page) && (pfn <= csrow->last_page)) |
| 301 | break; |
| 302 | } |
| 303 | |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 304 | cap_high = ddr_in32(pdata->mc_vbase + FSL_MC_CAPTURE_DATA_HI); |
| 305 | cap_low = ddr_in32(pdata->mc_vbase + FSL_MC_CAPTURE_DATA_LO); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 306 | |
| 307 | /* |
| 308 | * Analyze single-bit errors on 64-bit wide buses |
| 309 | * TODO: Add support for 32-bit wide buses |
| 310 | */ |
| 311 | if ((err_detect & DDR_EDE_SBE) && (bus_width == 64)) { |
| 312 | sbe_ecc_decode(cap_high, cap_low, syndrome, |
| 313 | &bad_data_bit, &bad_ecc_bit); |
| 314 | |
| 315 | if (bad_data_bit != -1) |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 316 | fsl_mc_printk(mci, KERN_ERR, |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 317 | "Faulty Data bit: %d\n", bad_data_bit); |
| 318 | if (bad_ecc_bit != -1) |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 319 | fsl_mc_printk(mci, KERN_ERR, |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 320 | "Faulty ECC bit: %d\n", bad_ecc_bit); |
| 321 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 322 | fsl_mc_printk(mci, KERN_ERR, |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 323 | "Expected Data / ECC:\t%#8.8x_%08x / %#2.2x\n", |
| 324 | cap_high ^ (1 << (bad_data_bit - 32)), |
| 325 | cap_low ^ (1 << bad_data_bit), |
| 326 | syndrome ^ (1 << bad_ecc_bit)); |
| 327 | } |
| 328 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 329 | fsl_mc_printk(mci, KERN_ERR, |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 330 | "Captured Data / ECC:\t%#8.8x_%08x / %#2.2x\n", |
| 331 | cap_high, cap_low, syndrome); |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 332 | fsl_mc_printk(mci, KERN_ERR, "Err addr: %#8.8llx\n", err_addr); |
| 333 | fsl_mc_printk(mci, KERN_ERR, "PFN: %#8.8x\n", pfn); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 334 | |
| 335 | /* we are out of range */ |
| 336 | if (row_index == mci->nr_csrows) |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 337 | fsl_mc_printk(mci, KERN_ERR, "PFN out of range!\n"); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 338 | |
| 339 | if (err_detect & DDR_EDE_SBE) |
| 340 | edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1, |
| 341 | pfn, err_addr & ~PAGE_MASK, syndrome, |
| 342 | row_index, 0, -1, |
| 343 | mci->ctl_name, ""); |
| 344 | |
| 345 | if (err_detect & DDR_EDE_MBE) |
| 346 | edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1, |
| 347 | pfn, err_addr & ~PAGE_MASK, syndrome, |
| 348 | row_index, 0, -1, |
| 349 | mci->ctl_name, ""); |
| 350 | |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 351 | ddr_out32(pdata->mc_vbase + FSL_MC_ERR_DETECT, err_detect); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 352 | } |
| 353 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 354 | static irqreturn_t fsl_mc_isr(int irq, void *dev_id) |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 355 | { |
| 356 | struct mem_ctl_info *mci = dev_id; |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 357 | struct fsl_mc_pdata *pdata = mci->pvt_info; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 358 | u32 err_detect; |
| 359 | |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 360 | err_detect = ddr_in32(pdata->mc_vbase + FSL_MC_ERR_DETECT); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 361 | if (!err_detect) |
| 362 | return IRQ_NONE; |
| 363 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 364 | fsl_mc_check(mci); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 365 | |
| 366 | return IRQ_HANDLED; |
| 367 | } |
| 368 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 369 | static void fsl_ddr_init_csrows(struct mem_ctl_info *mci) |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 370 | { |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 371 | struct fsl_mc_pdata *pdata = mci->pvt_info; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 372 | struct csrow_info *csrow; |
| 373 | struct dimm_info *dimm; |
| 374 | u32 sdram_ctl; |
| 375 | u32 sdtype; |
| 376 | enum mem_type mtype; |
| 377 | u32 cs_bnds; |
| 378 | int index; |
| 379 | |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 380 | sdram_ctl = ddr_in32(pdata->mc_vbase + FSL_MC_DDR_SDRAM_CFG); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 381 | |
| 382 | sdtype = sdram_ctl & DSC_SDTYPE_MASK; |
| 383 | if (sdram_ctl & DSC_RD_EN) { |
| 384 | switch (sdtype) { |
York Sun | 4e2c325 | 2016-08-09 14:55:42 -0700 | [diff] [blame] | 385 | case 0x02000000: |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 386 | mtype = MEM_RDDR; |
| 387 | break; |
York Sun | 4e2c325 | 2016-08-09 14:55:42 -0700 | [diff] [blame] | 388 | case 0x03000000: |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 389 | mtype = MEM_RDDR2; |
| 390 | break; |
York Sun | 4e2c325 | 2016-08-09 14:55:42 -0700 | [diff] [blame] | 391 | case 0x07000000: |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 392 | mtype = MEM_RDDR3; |
| 393 | break; |
York Sun | 4e2c325 | 2016-08-09 14:55:42 -0700 | [diff] [blame] | 394 | case 0x05000000: |
| 395 | mtype = MEM_RDDR4; |
| 396 | break; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 397 | default: |
| 398 | mtype = MEM_UNKNOWN; |
| 399 | break; |
| 400 | } |
| 401 | } else { |
| 402 | switch (sdtype) { |
York Sun | 4e2c325 | 2016-08-09 14:55:42 -0700 | [diff] [blame] | 403 | case 0x02000000: |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 404 | mtype = MEM_DDR; |
| 405 | break; |
York Sun | 4e2c325 | 2016-08-09 14:55:42 -0700 | [diff] [blame] | 406 | case 0x03000000: |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 407 | mtype = MEM_DDR2; |
| 408 | break; |
York Sun | 4e2c325 | 2016-08-09 14:55:42 -0700 | [diff] [blame] | 409 | case 0x07000000: |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 410 | mtype = MEM_DDR3; |
| 411 | break; |
York Sun | 4e2c325 | 2016-08-09 14:55:42 -0700 | [diff] [blame] | 412 | case 0x05000000: |
| 413 | mtype = MEM_DDR4; |
| 414 | break; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 415 | default: |
| 416 | mtype = MEM_UNKNOWN; |
| 417 | break; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | for (index = 0; index < mci->nr_csrows; index++) { |
| 422 | u32 start; |
| 423 | u32 end; |
| 424 | |
| 425 | csrow = mci->csrows[index]; |
| 426 | dimm = csrow->channels[0]->dimm; |
| 427 | |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 428 | cs_bnds = ddr_in32(pdata->mc_vbase + FSL_MC_CS_BNDS_0 + |
| 429 | (index * FSL_MC_CS_BNDS_OFS)); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 430 | |
| 431 | start = (cs_bnds & 0xffff0000) >> 16; |
| 432 | end = (cs_bnds & 0x0000ffff); |
| 433 | |
| 434 | if (start == end) |
| 435 | continue; /* not populated */ |
| 436 | |
| 437 | start <<= (24 - PAGE_SHIFT); |
| 438 | end <<= (24 - PAGE_SHIFT); |
| 439 | end |= (1 << (24 - PAGE_SHIFT)) - 1; |
| 440 | |
| 441 | csrow->first_page = start; |
| 442 | csrow->last_page = end; |
| 443 | |
| 444 | dimm->nr_pages = end + 1 - start; |
| 445 | dimm->grain = 8; |
| 446 | dimm->mtype = mtype; |
| 447 | dimm->dtype = DEV_UNKNOWN; |
| 448 | if (sdram_ctl & DSC_X32_EN) |
| 449 | dimm->dtype = DEV_X32; |
| 450 | dimm->edac_mode = EDAC_SECDED; |
| 451 | } |
| 452 | } |
| 453 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 454 | int fsl_mc_err_probe(struct platform_device *op) |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 455 | { |
| 456 | struct mem_ctl_info *mci; |
| 457 | struct edac_mc_layer layers[2]; |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 458 | struct fsl_mc_pdata *pdata; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 459 | struct resource r; |
| 460 | u32 sdram_ctl; |
| 461 | int res; |
| 462 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 463 | if (!devres_open_group(&op->dev, fsl_mc_err_probe, GFP_KERNEL)) |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 464 | return -ENOMEM; |
| 465 | |
| 466 | layers[0].type = EDAC_MC_LAYER_CHIP_SELECT; |
| 467 | layers[0].size = 4; |
| 468 | layers[0].is_virt_csrow = true; |
| 469 | layers[1].type = EDAC_MC_LAYER_CHANNEL; |
| 470 | layers[1].size = 1; |
| 471 | layers[1].is_virt_csrow = false; |
| 472 | mci = edac_mc_alloc(edac_mc_idx, ARRAY_SIZE(layers), layers, |
| 473 | sizeof(*pdata)); |
| 474 | if (!mci) { |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 475 | devres_release_group(&op->dev, fsl_mc_err_probe); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 476 | return -ENOMEM; |
| 477 | } |
| 478 | |
| 479 | pdata = mci->pvt_info; |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 480 | pdata->name = "fsl_mc_err"; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 481 | pdata->irq = NO_IRQ; |
| 482 | mci->pdev = &op->dev; |
| 483 | pdata->edac_idx = edac_mc_idx++; |
| 484 | dev_set_drvdata(mci->pdev, mci); |
| 485 | mci->ctl_name = pdata->name; |
| 486 | mci->dev_name = pdata->name; |
| 487 | |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 488 | /* |
| 489 | * Get the endianness of DDR controller registers. |
| 490 | * Default is big endian. |
| 491 | */ |
| 492 | little_endian = of_property_read_bool(op->dev.of_node, "little-endian"); |
| 493 | |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 494 | res = of_address_to_resource(op->dev.of_node, 0, &r); |
| 495 | if (res) { |
| 496 | pr_err("%s: Unable to get resource for MC err regs\n", |
| 497 | __func__); |
| 498 | goto err; |
| 499 | } |
| 500 | |
| 501 | if (!devm_request_mem_region(&op->dev, r.start, resource_size(&r), |
| 502 | pdata->name)) { |
| 503 | pr_err("%s: Error while requesting mem region\n", |
| 504 | __func__); |
| 505 | res = -EBUSY; |
| 506 | goto err; |
| 507 | } |
| 508 | |
| 509 | pdata->mc_vbase = devm_ioremap(&op->dev, r.start, resource_size(&r)); |
| 510 | if (!pdata->mc_vbase) { |
| 511 | pr_err("%s: Unable to setup MC err regs\n", __func__); |
| 512 | res = -ENOMEM; |
| 513 | goto err; |
| 514 | } |
| 515 | |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 516 | sdram_ctl = ddr_in32(pdata->mc_vbase + FSL_MC_DDR_SDRAM_CFG); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 517 | if (!(sdram_ctl & DSC_ECC_EN)) { |
| 518 | /* no ECC */ |
| 519 | pr_warn("%s: No ECC DIMMs discovered\n", __func__); |
| 520 | res = -ENODEV; |
| 521 | goto err; |
| 522 | } |
| 523 | |
| 524 | edac_dbg(3, "init mci\n"); |
York Sun | 4e2c325 | 2016-08-09 14:55:42 -0700 | [diff] [blame] | 525 | mci->mtype_cap = MEM_FLAG_DDR | MEM_FLAG_RDDR | |
| 526 | MEM_FLAG_DDR2 | MEM_FLAG_RDDR2 | |
| 527 | MEM_FLAG_DDR3 | MEM_FLAG_RDDR3 | |
| 528 | MEM_FLAG_DDR4 | MEM_FLAG_RDDR4; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 529 | mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_SECDED; |
| 530 | mci->edac_cap = EDAC_FLAG_SECDED; |
| 531 | mci->mod_name = EDAC_MOD_STR; |
| 532 | |
| 533 | if (edac_op_state == EDAC_OPSTATE_POLL) |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 534 | mci->edac_check = fsl_mc_check; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 535 | |
| 536 | mci->ctl_page_to_phys = NULL; |
| 537 | |
| 538 | mci->scrub_mode = SCRUB_SW_SRC; |
| 539 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 540 | fsl_ddr_init_csrows(mci); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 541 | |
| 542 | /* store the original error disable bits */ |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 543 | orig_ddr_err_disable = ddr_in32(pdata->mc_vbase + FSL_MC_ERR_DISABLE); |
| 544 | ddr_out32(pdata->mc_vbase + FSL_MC_ERR_DISABLE, 0); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 545 | |
| 546 | /* clear all error bits */ |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 547 | ddr_out32(pdata->mc_vbase + FSL_MC_ERR_DETECT, ~0); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 548 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 549 | if (edac_mc_add_mc_with_groups(mci, fsl_ddr_dev_groups)) { |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 550 | edac_dbg(3, "failed edac_mc_add_mc()\n"); |
| 551 | goto err; |
| 552 | } |
| 553 | |
| 554 | if (edac_op_state == EDAC_OPSTATE_INT) { |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 555 | ddr_out32(pdata->mc_vbase + FSL_MC_ERR_INT_EN, |
| 556 | DDR_EIE_MBEE | DDR_EIE_SBEE); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 557 | |
| 558 | /* store the original error management threshold */ |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 559 | orig_ddr_err_sbe = ddr_in32(pdata->mc_vbase + |
| 560 | FSL_MC_ERR_SBE) & 0xff0000; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 561 | |
| 562 | /* set threshold to 1 error per interrupt */ |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 563 | ddr_out32(pdata->mc_vbase + FSL_MC_ERR_SBE, 0x10000); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 564 | |
| 565 | /* register interrupts */ |
| 566 | pdata->irq = irq_of_parse_and_map(op->dev.of_node, 0); |
| 567 | res = devm_request_irq(&op->dev, pdata->irq, |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 568 | fsl_mc_isr, |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 569 | IRQF_SHARED, |
| 570 | "[EDAC] MC err", mci); |
| 571 | if (res < 0) { |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 572 | pr_err("%s: Unable to request irq %d for FSL DDR DRAM ERR\n", |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 573 | __func__, pdata->irq); |
| 574 | irq_dispose_mapping(pdata->irq); |
| 575 | res = -ENODEV; |
| 576 | goto err2; |
| 577 | } |
| 578 | |
| 579 | pr_info(EDAC_MOD_STR " acquired irq %d for MC\n", |
| 580 | pdata->irq); |
| 581 | } |
| 582 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 583 | devres_remove_group(&op->dev, fsl_mc_err_probe); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 584 | edac_dbg(3, "success\n"); |
| 585 | pr_info(EDAC_MOD_STR " MC err registered\n"); |
| 586 | |
| 587 | return 0; |
| 588 | |
| 589 | err2: |
| 590 | edac_mc_del_mc(&op->dev); |
| 591 | err: |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 592 | devres_release_group(&op->dev, fsl_mc_err_probe); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 593 | edac_mc_free(mci); |
| 594 | return res; |
| 595 | } |
| 596 | |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 597 | int fsl_mc_err_remove(struct platform_device *op) |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 598 | { |
| 599 | struct mem_ctl_info *mci = dev_get_drvdata(&op->dev); |
York Sun | d43a9fb | 2016-08-09 14:55:41 -0700 | [diff] [blame] | 600 | struct fsl_mc_pdata *pdata = mci->pvt_info; |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 601 | |
| 602 | edac_dbg(0, "\n"); |
| 603 | |
| 604 | if (edac_op_state == EDAC_OPSTATE_INT) { |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 605 | irq_dispose_mapping(pdata->irq); |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 606 | ddr_out32(pdata->mc_vbase + FSL_MC_ERR_INT_EN, 0); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 607 | } |
| 608 | |
York Sun | 339fdff | 2016-08-09 14:55:43 -0700 | [diff] [blame^] | 609 | ddr_out32(pdata->mc_vbase + FSL_MC_ERR_DISABLE, |
| 610 | orig_ddr_err_disable); |
| 611 | ddr_out32(pdata->mc_vbase + FSL_MC_ERR_SBE, orig_ddr_err_sbe); |
York Sun | ea2eb9a | 2016-08-11 13:15:18 -0700 | [diff] [blame] | 612 | |
| 613 | edac_mc_del_mc(&op->dev); |
| 614 | edac_mc_free(mci); |
| 615 | return 0; |
| 616 | } |