blob: 46b00e15e44288e5467d6b9bfb3d82a4e89af986 [file] [log] [blame]
York Sunea2eb9a2016-08-11 13:15:18 -07001/*
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
36static int edac_mc_idx;
37
38static u32 orig_ddr_err_disable;
39static 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 Sund43a9fb2016-08-09 14:55:41 -070045static 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
56static ssize_t fsl_mc_inject_data_lo_show(struct device *dev,
57 struct device_attribute *mattr,
York Sunea2eb9a2016-08-11 13:15:18 -070058 char *data)
59{
60 struct mem_ctl_info *mci = to_mci(dev);
York Sund43a9fb2016-08-09 14:55:41 -070061 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -070062 return sprintf(data, "0x%08x",
63 in_be32(pdata->mc_vbase +
York Sund43a9fb2016-08-09 14:55:41 -070064 FSL_MC_DATA_ERR_INJECT_LO));
York Sunea2eb9a2016-08-11 13:15:18 -070065}
66
York Sund43a9fb2016-08-09 14:55:41 -070067static ssize_t fsl_mc_inject_ctrl_show(struct device *dev,
68 struct device_attribute *mattr,
York Sunea2eb9a2016-08-11 13:15:18 -070069 char *data)
70{
71 struct mem_ctl_info *mci = to_mci(dev);
York Sund43a9fb2016-08-09 14:55:41 -070072 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -070073 return sprintf(data, "0x%08x",
York Sund43a9fb2016-08-09 14:55:41 -070074 in_be32(pdata->mc_vbase + FSL_MC_ECC_ERR_INJECT));
York Sunea2eb9a2016-08-11 13:15:18 -070075}
76
York Sund43a9fb2016-08-09 14:55:41 -070077static ssize_t fsl_mc_inject_data_hi_store(struct device *dev,
78 struct device_attribute *mattr,
York Sunea2eb9a2016-08-11 13:15:18 -070079 const char *data, size_t count)
80{
81 struct mem_ctl_info *mci = to_mci(dev);
York Sund43a9fb2016-08-09 14:55:41 -070082 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -070083 if (isdigit(*data)) {
York Sund43a9fb2016-08-09 14:55:41 -070084 out_be32(pdata->mc_vbase + FSL_MC_DATA_ERR_INJECT_HI,
York Sunea2eb9a2016-08-11 13:15:18 -070085 simple_strtoul(data, NULL, 0));
86 return count;
87 }
88 return 0;
89}
90
York Sund43a9fb2016-08-09 14:55:41 -070091static ssize_t fsl_mc_inject_data_lo_store(struct device *dev,
92 struct device_attribute *mattr,
York Sunea2eb9a2016-08-11 13:15:18 -070093 const char *data, size_t count)
94{
95 struct mem_ctl_info *mci = to_mci(dev);
York Sund43a9fb2016-08-09 14:55:41 -070096 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -070097 if (isdigit(*data)) {
York Sund43a9fb2016-08-09 14:55:41 -070098 out_be32(pdata->mc_vbase + FSL_MC_DATA_ERR_INJECT_LO,
York Sunea2eb9a2016-08-11 13:15:18 -070099 simple_strtoul(data, NULL, 0));
100 return count;
101 }
102 return 0;
103}
104
York Sund43a9fb2016-08-09 14:55:41 -0700105static ssize_t fsl_mc_inject_ctrl_store(struct device *dev,
106 struct device_attribute *mattr,
York Sunea2eb9a2016-08-11 13:15:18 -0700107 const char *data, size_t count)
108{
109 struct mem_ctl_info *mci = to_mci(dev);
York Sund43a9fb2016-08-09 14:55:41 -0700110 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -0700111 if (isdigit(*data)) {
York Sund43a9fb2016-08-09 14:55:41 -0700112 out_be32(pdata->mc_vbase + FSL_MC_ECC_ERR_INJECT,
York Sunea2eb9a2016-08-11 13:15:18 -0700113 simple_strtoul(data, NULL, 0));
114 return count;
115 }
116 return 0;
117}
118
119DEVICE_ATTR(inject_data_hi, S_IRUGO | S_IWUSR,
York Sund43a9fb2016-08-09 14:55:41 -0700120 fsl_mc_inject_data_hi_show, fsl_mc_inject_data_hi_store);
York Sunea2eb9a2016-08-11 13:15:18 -0700121DEVICE_ATTR(inject_data_lo, S_IRUGO | S_IWUSR,
York Sund43a9fb2016-08-09 14:55:41 -0700122 fsl_mc_inject_data_lo_show, fsl_mc_inject_data_lo_store);
York Sunea2eb9a2016-08-11 13:15:18 -0700123DEVICE_ATTR(inject_ctrl, S_IRUGO | S_IWUSR,
York Sund43a9fb2016-08-09 14:55:41 -0700124 fsl_mc_inject_ctrl_show, fsl_mc_inject_ctrl_store);
York Sunea2eb9a2016-08-11 13:15:18 -0700125
York Sund43a9fb2016-08-09 14:55:41 -0700126static struct attribute *fsl_ddr_dev_attrs[] = {
York Sunea2eb9a2016-08-11 13:15:18 -0700127 &dev_attr_inject_data_hi.attr,
128 &dev_attr_inject_data_lo.attr,
129 &dev_attr_inject_ctrl.attr,
130 NULL
131};
132
York Sund43a9fb2016-08-09 14:55:41 -0700133ATTRIBUTE_GROUPS(fsl_ddr_dev);
York Sunea2eb9a2016-08-11 13:15:18 -0700134
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 */
143static 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 */
159static 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 */
191static 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 */
210static 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 Sund43a9fb2016-08-09 14:55:41 -0700244static void fsl_mc_check(struct mem_ctl_info *mci)
York Sunea2eb9a2016-08-11 13:15:18 -0700245{
York Sund43a9fb2016-08-09 14:55:41 -0700246 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -0700247 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 Sund43a9fb2016-08-09 14:55:41 -0700259 err_detect = in_be32(pdata->mc_vbase + FSL_MC_ERR_DETECT);
York Sunea2eb9a2016-08-11 13:15:18 -0700260 if (!err_detect)
261 return;
262
York Sund43a9fb2016-08-09 14:55:41 -0700263 fsl_mc_printk(mci, KERN_ERR, "Err Detect Register: %#8.8x\n",
264 err_detect);
York Sunea2eb9a2016-08-11 13:15:18 -0700265
266 /* no more processing if not ECC bit errors */
267 if (!(err_detect & (DDR_EDE_SBE | DDR_EDE_MBE))) {
York Sund43a9fb2016-08-09 14:55:41 -0700268 out_be32(pdata->mc_vbase + FSL_MC_ERR_DETECT, err_detect);
York Sunea2eb9a2016-08-11 13:15:18 -0700269 return;
270 }
271
York Sund43a9fb2016-08-09 14:55:41 -0700272 syndrome = in_be32(pdata->mc_vbase + FSL_MC_CAPTURE_ECC);
York Sunea2eb9a2016-08-11 13:15:18 -0700273
274 /* Mask off appropriate bits of syndrome based on bus width */
York Sund43a9fb2016-08-09 14:55:41 -0700275 bus_width = (in_be32(pdata->mc_vbase + FSL_MC_DDR_SDRAM_CFG) &
York Sunea2eb9a2016-08-11 13:15:18 -0700276 DSC_DBW_MASK) ? 32 : 64;
277 if (bus_width == 64)
278 syndrome &= 0xff;
279 else
280 syndrome &= 0xffff;
281
282 err_addr = make64(
York Sund43a9fb2016-08-09 14:55:41 -0700283 in_be32(pdata->mc_vbase + FSL_MC_CAPTURE_EXT_ADDRESS),
284 in_be32(pdata->mc_vbase + FSL_MC_CAPTURE_ADDRESS));
York Sunea2eb9a2016-08-11 13:15:18 -0700285 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 Sund43a9fb2016-08-09 14:55:41 -0700293 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 Sunea2eb9a2016-08-11 13:15:18 -0700295
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 Sund43a9fb2016-08-09 14:55:41 -0700305 fsl_mc_printk(mci, KERN_ERR,
York Sunea2eb9a2016-08-11 13:15:18 -0700306 "Faulty Data bit: %d\n", bad_data_bit);
307 if (bad_ecc_bit != -1)
York Sund43a9fb2016-08-09 14:55:41 -0700308 fsl_mc_printk(mci, KERN_ERR,
York Sunea2eb9a2016-08-11 13:15:18 -0700309 "Faulty ECC bit: %d\n", bad_ecc_bit);
310
York Sund43a9fb2016-08-09 14:55:41 -0700311 fsl_mc_printk(mci, KERN_ERR,
York Sunea2eb9a2016-08-11 13:15:18 -0700312 "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 Sund43a9fb2016-08-09 14:55:41 -0700318 fsl_mc_printk(mci, KERN_ERR,
York Sunea2eb9a2016-08-11 13:15:18 -0700319 "Captured Data / ECC:\t%#8.8x_%08x / %#2.2x\n",
320 cap_high, cap_low, syndrome);
York Sund43a9fb2016-08-09 14:55:41 -0700321 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 Sunea2eb9a2016-08-11 13:15:18 -0700323
324 /* we are out of range */
325 if (row_index == mci->nr_csrows)
York Sund43a9fb2016-08-09 14:55:41 -0700326 fsl_mc_printk(mci, KERN_ERR, "PFN out of range!\n");
York Sunea2eb9a2016-08-11 13:15:18 -0700327
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 Sund43a9fb2016-08-09 14:55:41 -0700340 out_be32(pdata->mc_vbase + FSL_MC_ERR_DETECT, err_detect);
York Sunea2eb9a2016-08-11 13:15:18 -0700341}
342
York Sund43a9fb2016-08-09 14:55:41 -0700343static irqreturn_t fsl_mc_isr(int irq, void *dev_id)
York Sunea2eb9a2016-08-11 13:15:18 -0700344{
345 struct mem_ctl_info *mci = dev_id;
York Sund43a9fb2016-08-09 14:55:41 -0700346 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -0700347 u32 err_detect;
348
York Sund43a9fb2016-08-09 14:55:41 -0700349 err_detect = in_be32(pdata->mc_vbase + FSL_MC_ERR_DETECT);
York Sunea2eb9a2016-08-11 13:15:18 -0700350 if (!err_detect)
351 return IRQ_NONE;
352
York Sund43a9fb2016-08-09 14:55:41 -0700353 fsl_mc_check(mci);
York Sunea2eb9a2016-08-11 13:15:18 -0700354
355 return IRQ_HANDLED;
356}
357
York Sund43a9fb2016-08-09 14:55:41 -0700358static void fsl_ddr_init_csrows(struct mem_ctl_info *mci)
York Sunea2eb9a2016-08-11 13:15:18 -0700359{
York Sund43a9fb2016-08-09 14:55:41 -0700360 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -0700361 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 Sund43a9fb2016-08-09 14:55:41 -0700369 sdram_ctl = in_be32(pdata->mc_vbase + FSL_MC_DDR_SDRAM_CFG);
York Sunea2eb9a2016-08-11 13:15:18 -0700370
371 sdtype = sdram_ctl & DSC_SDTYPE_MASK;
372 if (sdram_ctl & DSC_RD_EN) {
373 switch (sdtype) {
York Sun4e2c3252016-08-09 14:55:42 -0700374 case 0x02000000:
York Sunea2eb9a2016-08-11 13:15:18 -0700375 mtype = MEM_RDDR;
376 break;
York Sun4e2c3252016-08-09 14:55:42 -0700377 case 0x03000000:
York Sunea2eb9a2016-08-11 13:15:18 -0700378 mtype = MEM_RDDR2;
379 break;
York Sun4e2c3252016-08-09 14:55:42 -0700380 case 0x07000000:
York Sunea2eb9a2016-08-11 13:15:18 -0700381 mtype = MEM_RDDR3;
382 break;
York Sun4e2c3252016-08-09 14:55:42 -0700383 case 0x05000000:
384 mtype = MEM_RDDR4;
385 break;
York Sunea2eb9a2016-08-11 13:15:18 -0700386 default:
387 mtype = MEM_UNKNOWN;
388 break;
389 }
390 } else {
391 switch (sdtype) {
York Sun4e2c3252016-08-09 14:55:42 -0700392 case 0x02000000:
York Sunea2eb9a2016-08-11 13:15:18 -0700393 mtype = MEM_DDR;
394 break;
York Sun4e2c3252016-08-09 14:55:42 -0700395 case 0x03000000:
York Sunea2eb9a2016-08-11 13:15:18 -0700396 mtype = MEM_DDR2;
397 break;
York Sun4e2c3252016-08-09 14:55:42 -0700398 case 0x07000000:
York Sunea2eb9a2016-08-11 13:15:18 -0700399 mtype = MEM_DDR3;
400 break;
York Sun4e2c3252016-08-09 14:55:42 -0700401 case 0x05000000:
402 mtype = MEM_DDR4;
403 break;
York Sunea2eb9a2016-08-11 13:15:18 -0700404 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 Sund43a9fb2016-08-09 14:55:41 -0700417 cs_bnds = in_be32(pdata->mc_vbase + FSL_MC_CS_BNDS_0 +
418 (index * FSL_MC_CS_BNDS_OFS));
York Sunea2eb9a2016-08-11 13:15:18 -0700419
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 Sund43a9fb2016-08-09 14:55:41 -0700443int fsl_mc_err_probe(struct platform_device *op)
York Sunea2eb9a2016-08-11 13:15:18 -0700444{
445 struct mem_ctl_info *mci;
446 struct edac_mc_layer layers[2];
York Sund43a9fb2016-08-09 14:55:41 -0700447 struct fsl_mc_pdata *pdata;
York Sunea2eb9a2016-08-11 13:15:18 -0700448 struct resource r;
449 u32 sdram_ctl;
450 int res;
451
York Sund43a9fb2016-08-09 14:55:41 -0700452 if (!devres_open_group(&op->dev, fsl_mc_err_probe, GFP_KERNEL))
York Sunea2eb9a2016-08-11 13:15:18 -0700453 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 Sund43a9fb2016-08-09 14:55:41 -0700464 devres_release_group(&op->dev, fsl_mc_err_probe);
York Sunea2eb9a2016-08-11 13:15:18 -0700465 return -ENOMEM;
466 }
467
468 pdata = mci->pvt_info;
York Sund43a9fb2016-08-09 14:55:41 -0700469 pdata->name = "fsl_mc_err";
York Sunea2eb9a2016-08-11 13:15:18 -0700470 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 Sund43a9fb2016-08-09 14:55:41 -0700499 sdram_ctl = in_be32(pdata->mc_vbase + FSL_MC_DDR_SDRAM_CFG);
York Sunea2eb9a2016-08-11 13:15:18 -0700500 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 Sun4e2c3252016-08-09 14:55:42 -0700508 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 Sunea2eb9a2016-08-11 13:15:18 -0700512 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 Sund43a9fb2016-08-09 14:55:41 -0700517 mci->edac_check = fsl_mc_check;
York Sunea2eb9a2016-08-11 13:15:18 -0700518
519 mci->ctl_page_to_phys = NULL;
520
521 mci->scrub_mode = SCRUB_SW_SRC;
522
York Sund43a9fb2016-08-09 14:55:41 -0700523 fsl_ddr_init_csrows(mci);
York Sunea2eb9a2016-08-11 13:15:18 -0700524
525 /* store the original error disable bits */
526 orig_ddr_err_disable =
York Sund43a9fb2016-08-09 14:55:41 -0700527 in_be32(pdata->mc_vbase + FSL_MC_ERR_DISABLE);
528 out_be32(pdata->mc_vbase + FSL_MC_ERR_DISABLE, 0);
York Sunea2eb9a2016-08-11 13:15:18 -0700529
530 /* clear all error bits */
York Sund43a9fb2016-08-09 14:55:41 -0700531 out_be32(pdata->mc_vbase + FSL_MC_ERR_DETECT, ~0);
York Sunea2eb9a2016-08-11 13:15:18 -0700532
York Sund43a9fb2016-08-09 14:55:41 -0700533 if (edac_mc_add_mc_with_groups(mci, fsl_ddr_dev_groups)) {
York Sunea2eb9a2016-08-11 13:15:18 -0700534 edac_dbg(3, "failed edac_mc_add_mc()\n");
535 goto err;
536 }
537
538 if (edac_op_state == EDAC_OPSTATE_INT) {
York Sund43a9fb2016-08-09 14:55:41 -0700539 out_be32(pdata->mc_vbase + FSL_MC_ERR_INT_EN,
York Sunea2eb9a2016-08-11 13:15:18 -0700540 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 Sund43a9fb2016-08-09 14:55:41 -0700544 FSL_MC_ERR_SBE) & 0xff0000;
York Sunea2eb9a2016-08-11 13:15:18 -0700545
546 /* set threshold to 1 error per interrupt */
York Sund43a9fb2016-08-09 14:55:41 -0700547 out_be32(pdata->mc_vbase + FSL_MC_ERR_SBE, 0x10000);
York Sunea2eb9a2016-08-11 13:15:18 -0700548
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 Sund43a9fb2016-08-09 14:55:41 -0700552 fsl_mc_isr,
York Sunea2eb9a2016-08-11 13:15:18 -0700553 IRQF_SHARED,
554 "[EDAC] MC err", mci);
555 if (res < 0) {
York Sund43a9fb2016-08-09 14:55:41 -0700556 pr_err("%s: Unable to request irq %d for FSL DDR DRAM ERR\n",
York Sunea2eb9a2016-08-11 13:15:18 -0700557 __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 Sund43a9fb2016-08-09 14:55:41 -0700567 devres_remove_group(&op->dev, fsl_mc_err_probe);
York Sunea2eb9a2016-08-11 13:15:18 -0700568 edac_dbg(3, "success\n");
569 pr_info(EDAC_MOD_STR " MC err registered\n");
570
571 return 0;
572
573err2:
574 edac_mc_del_mc(&op->dev);
575err:
York Sund43a9fb2016-08-09 14:55:41 -0700576 devres_release_group(&op->dev, fsl_mc_err_probe);
York Sunea2eb9a2016-08-11 13:15:18 -0700577 edac_mc_free(mci);
578 return res;
579}
580
York Sund43a9fb2016-08-09 14:55:41 -0700581int fsl_mc_err_remove(struct platform_device *op)
York Sunea2eb9a2016-08-11 13:15:18 -0700582{
583 struct mem_ctl_info *mci = dev_get_drvdata(&op->dev);
York Sund43a9fb2016-08-09 14:55:41 -0700584 struct fsl_mc_pdata *pdata = mci->pvt_info;
York Sunea2eb9a2016-08-11 13:15:18 -0700585
586 edac_dbg(0, "\n");
587
588 if (edac_op_state == EDAC_OPSTATE_INT) {
York Sund43a9fb2016-08-09 14:55:41 -0700589 out_be32(pdata->mc_vbase + FSL_MC_ERR_INT_EN, 0);
York Sunea2eb9a2016-08-11 13:15:18 -0700590 irq_dispose_mapping(pdata->irq);
591 }
592
York Sund43a9fb2016-08-09 14:55:41 -0700593 out_be32(pdata->mc_vbase + FSL_MC_ERR_DISABLE,
York Sunea2eb9a2016-08-11 13:15:18 -0700594 orig_ddr_err_disable);
York Sund43a9fb2016-08-09 14:55:41 -0700595 out_be32(pdata->mc_vbase + FSL_MC_ERR_SBE, orig_ddr_err_sbe);
York Sunea2eb9a2016-08-11 13:15:18 -0700596
597 edac_mc_del_mc(&op->dev);
598 edac_mc_free(mci);
599 return 0;
600}