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