1 // SPDX-License-Identifier: GPL-2.0-only
3 * Freescale Memory Controller kernel module
5 * Support Power-based SoCs including MPC85xx, MPC86xx, MPC83xx and
6 * ARM-based Layerscape SoCs including LS2xxx and LS1021A. Originally
7 * split out from mpc85xx_edac EDAC driver.
9 * Parts Copyrighted (c) 2013 by Freescale Semiconductor, Inc.
11 * Author: Dave Jiang <djiang@mvista.com>
13 * 2006-2007 (c) MontaVista Software, Inc.
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/ctype.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/edac.h>
22 #include <linux/smp.h>
23 #include <linux/gfp.h>
26 #include <linux/of_address.h>
27 #include "edac_module.h"
28 #include "fsl_ddr_edac.h"
30 #define EDAC_MOD_STR "fsl_ddr_edac"
32 static int edac_mc_idx
;
34 static inline void __iomem
*ddr_reg_addr(struct fsl_mc_pdata
*pdata
, unsigned int off
)
36 if (pdata
->flag
== TYPE_IMX9
&& off
>= FSL_MC_DATA_ERR_INJECT_HI
&& off
<= FSL_MC_ERR_SBE
)
37 return pdata
->inject_vbase
+ off
- FSL_MC_DATA_ERR_INJECT_HI
38 + IMX9_MC_DATA_ERR_INJECT_OFF
;
40 if (pdata
->flag
== TYPE_IMX9
&& off
>= IMX9_MC_ERR_EN
)
41 return pdata
->inject_vbase
+ off
- IMX9_MC_ERR_EN
;
43 return pdata
->mc_vbase
+ off
;
46 static inline u32
ddr_in32(struct fsl_mc_pdata
*pdata
, unsigned int off
)
48 void __iomem
*addr
= ddr_reg_addr(pdata
, off
);
50 return pdata
->little_endian
? ioread32(addr
) : ioread32be(addr
);
53 static inline void ddr_out32(struct fsl_mc_pdata
*pdata
, unsigned int off
, u32 value
)
55 void __iomem
*addr
= ddr_reg_addr(pdata
, off
);
57 if (pdata
->little_endian
)
58 iowrite32(value
, addr
);
60 iowrite32be(value
, addr
);
63 #ifdef CONFIG_EDAC_DEBUG
64 /************************ MC SYSFS parts ***********************************/
66 #define to_mci(k) container_of(k, struct mem_ctl_info, dev)
68 static ssize_t
fsl_mc_inject_data_hi_show(struct device
*dev
,
69 struct device_attribute
*mattr
,
72 struct mem_ctl_info
*mci
= to_mci(dev
);
73 struct fsl_mc_pdata
*pdata
= mci
->pvt_info
;
74 return sprintf(data
, "0x%08x",
75 ddr_in32(pdata
, FSL_MC_DATA_ERR_INJECT_HI
));
78 static ssize_t
fsl_mc_inject_data_lo_show(struct device
*dev
,
79 struct device_attribute
*mattr
,
82 struct mem_ctl_info
*mci
= to_mci(dev
);
83 struct fsl_mc_pdata
*pdata
= mci
->pvt_info
;
84 return sprintf(data
, "0x%08x",
85 ddr_in32(pdata
, FSL_MC_DATA_ERR_INJECT_LO
));
88 static ssize_t
fsl_mc_inject_ctrl_show(struct device
*dev
,
89 struct device_attribute
*mattr
,
92 struct mem_ctl_info
*mci
= to_mci(dev
);
93 struct fsl_mc_pdata
*pdata
= mci
->pvt_info
;
94 return sprintf(data
, "0x%08x",
95 ddr_in32(pdata
, FSL_MC_ECC_ERR_INJECT
));
98 static ssize_t
fsl_mc_inject_data_hi_store(struct device
*dev
,
99 struct device_attribute
*mattr
,
100 const char *data
, size_t count
)
102 struct mem_ctl_info
*mci
= to_mci(dev
);
103 struct fsl_mc_pdata
*pdata
= mci
->pvt_info
;
107 if (isdigit(*data
)) {
108 rc
= kstrtoul(data
, 0, &val
);
112 ddr_out32(pdata
, FSL_MC_DATA_ERR_INJECT_HI
, val
);
118 static ssize_t
fsl_mc_inject_data_lo_store(struct device
*dev
,
119 struct device_attribute
*mattr
,
120 const char *data
, size_t count
)
122 struct mem_ctl_info
*mci
= to_mci(dev
);
123 struct fsl_mc_pdata
*pdata
= mci
->pvt_info
;
127 if (isdigit(*data
)) {
128 rc
= kstrtoul(data
, 0, &val
);
132 ddr_out32(pdata
, FSL_MC_DATA_ERR_INJECT_LO
, val
);
138 static ssize_t
fsl_mc_inject_ctrl_store(struct device
*dev
,
139 struct device_attribute
*mattr
,
140 const char *data
, size_t count
)
142 struct mem_ctl_info
*mci
= to_mci(dev
);
143 struct fsl_mc_pdata
*pdata
= mci
->pvt_info
;
147 if (isdigit(*data
)) {
148 rc
= kstrtoul(data
, 0, &val
);
152 ddr_out32(pdata
, FSL_MC_ECC_ERR_INJECT
, val
);
158 static DEVICE_ATTR(inject_data_hi
, S_IRUGO
| S_IWUSR
,
159 fsl_mc_inject_data_hi_show
, fsl_mc_inject_data_hi_store
);
160 static DEVICE_ATTR(inject_data_lo
, S_IRUGO
| S_IWUSR
,
161 fsl_mc_inject_data_lo_show
, fsl_mc_inject_data_lo_store
);
162 static DEVICE_ATTR(inject_ctrl
, S_IRUGO
| S_IWUSR
,
163 fsl_mc_inject_ctrl_show
, fsl_mc_inject_ctrl_store
);
164 #endif /* CONFIG_EDAC_DEBUG */
166 static struct attribute
*fsl_ddr_dev_attrs
[] = {
167 #ifdef CONFIG_EDAC_DEBUG
168 &dev_attr_inject_data_hi
.attr
,
169 &dev_attr_inject_data_lo
.attr
,
170 &dev_attr_inject_ctrl
.attr
,
175 ATTRIBUTE_GROUPS(fsl_ddr_dev
);
177 /**************************** MC Err device ***************************/
180 * Taken from table 8-55 in the MPC8641 User's Manual and/or 9-61 in the
181 * MPC8572 User's Manual. Each line represents a syndrome bit column as a
182 * 64-bit value, but split into an upper and lower 32-bit chunk. The labels
183 * below correspond to Freescale's manuals.
185 static unsigned int ecc_table
[16] = {
188 0xf00fe11e, 0xc33c0ff7, /* Syndrome bit 7 */
189 0x00ff00ff, 0x00fff0ff,
190 0x0f0f0f0f, 0x0f0fff00,
191 0x11113333, 0x7777000f,
192 0x22224444, 0x8888222f,
193 0x44448888, 0xffff4441,
194 0x8888ffff, 0x11118882,
195 0xffff1111, 0x22221114, /* Syndrome bit 0 */
199 * Calculate the correct ECC value for a 64-bit value specified by high:low
201 static u8
calculate_ecc(u32 high
, u32 low
)
210 for (i
= 0; i
< 8; i
++) {
211 mask_high
= ecc_table
[i
* 2];
212 mask_low
= ecc_table
[i
* 2 + 1];
215 for (j
= 0; j
< 32; j
++) {
216 if ((mask_high
>> j
) & 1)
217 bit_cnt
^= (high
>> j
) & 1;
218 if ((mask_low
>> j
) & 1)
219 bit_cnt
^= (low
>> j
) & 1;
229 * Create the syndrome code which is generated if the data line specified by
230 * 'bit' failed. Eg generate an 8-bit codes seen in Table 8-55 in the MPC8641
231 * User's Manual and 9-61 in the MPC8572 User's Manual.
233 static u8
syndrome_from_bit(unsigned int bit
) {
238 * Cycle through the upper or lower 32-bit portion of each value in
239 * ecc_table depending on if 'bit' is in the upper or lower half of
242 for (i
= bit
< 32; i
< 16; i
+= 2)
243 syndrome
|= ((ecc_table
[i
] >> (bit
% 32)) & 1) << (i
/ 2);
249 * Decode data and ecc syndrome to determine what went wrong
250 * Note: This can only decode single-bit errors
252 static void sbe_ecc_decode(u32 cap_high
, u32 cap_low
, u32 cap_ecc
,
253 int *bad_data_bit
, int *bad_ecc_bit
)
262 * Calculate the ECC of the captured data and XOR it with the captured
263 * ECC to find an ECC syndrome value we can search for
265 syndrome
= calculate_ecc(cap_high
, cap_low
) ^ cap_ecc
;
267 /* Check if a data line is stuck... */
268 for (i
= 0; i
< 64; i
++) {
269 if (syndrome
== syndrome_from_bit(i
)) {
275 /* If data is correct, check ECC bits for errors... */
276 for (i
= 0; i
< 8; i
++) {
277 if ((syndrome
>> i
) & 0x1) {
284 #define make64(high, low) (((u64)(high) << 32) | (low))
286 static void fsl_mc_check(struct mem_ctl_info
*mci
)
288 struct fsl_mc_pdata
*pdata
= mci
->pvt_info
;
289 struct csrow_info
*csrow
;
301 err_detect
= ddr_in32(pdata
, FSL_MC_ERR_DETECT
);
305 fsl_mc_printk(mci
, KERN_ERR
, "Err Detect Register: %#8.8x\n",
308 /* no more processing if not ECC bit errors */
309 if (!(err_detect
& (DDR_EDE_SBE
| DDR_EDE_MBE
))) {
310 ddr_out32(pdata
, FSL_MC_ERR_DETECT
, err_detect
);
314 syndrome
= ddr_in32(pdata
, FSL_MC_CAPTURE_ECC
);
316 /* Mask off appropriate bits of syndrome based on bus width */
317 bus_width
= (ddr_in32(pdata
, FSL_MC_DDR_SDRAM_CFG
) &
318 DSC_DBW_MASK
) ? 32 : 64;
325 ddr_in32(pdata
, FSL_MC_CAPTURE_EXT_ADDRESS
),
326 ddr_in32(pdata
, FSL_MC_CAPTURE_ADDRESS
));
327 pfn
= err_addr
>> PAGE_SHIFT
;
329 for (row_index
= 0; row_index
< mci
->nr_csrows
; row_index
++) {
330 csrow
= mci
->csrows
[row_index
];
331 if ((pfn
>= csrow
->first_page
) && (pfn
<= csrow
->last_page
))
335 cap_high
= ddr_in32(pdata
, FSL_MC_CAPTURE_DATA_HI
);
336 cap_low
= ddr_in32(pdata
, FSL_MC_CAPTURE_DATA_LO
);
339 * Analyze single-bit errors on 64-bit wide buses
340 * TODO: Add support for 32-bit wide buses
342 if ((err_detect
& DDR_EDE_SBE
) && (bus_width
== 64)) {
343 u64 cap
= (u64
)cap_high
<< 32 | cap_low
;
346 sbe_ecc_decode(cap_high
, cap_low
, syndrome
,
347 &bad_data_bit
, &bad_ecc_bit
);
349 if (bad_data_bit
>= 0) {
350 fsl_mc_printk(mci
, KERN_ERR
, "Faulty Data bit: %d\n", bad_data_bit
);
351 cap
^= 1ULL << bad_data_bit
;
354 if (bad_ecc_bit
>= 0) {
355 fsl_mc_printk(mci
, KERN_ERR
, "Faulty ECC bit: %d\n", bad_ecc_bit
);
356 s
^= 1 << bad_ecc_bit
;
359 fsl_mc_printk(mci
, KERN_ERR
,
360 "Expected Data / ECC:\t%#8.8x_%08x / %#2.2x\n",
361 upper_32_bits(cap
), lower_32_bits(cap
), s
);
364 fsl_mc_printk(mci
, KERN_ERR
,
365 "Captured Data / ECC:\t%#8.8x_%08x / %#2.2x\n",
366 cap_high
, cap_low
, syndrome
);
367 fsl_mc_printk(mci
, KERN_ERR
, "Err addr: %#8.8llx\n", err_addr
);
368 fsl_mc_printk(mci
, KERN_ERR
, "PFN: %#8.8x\n", pfn
);
370 /* we are out of range */
371 if (row_index
== mci
->nr_csrows
)
372 fsl_mc_printk(mci
, KERN_ERR
, "PFN out of range!\n");
374 if (err_detect
& DDR_EDE_SBE
)
375 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED
, mci
, 1,
376 pfn
, err_addr
& ~PAGE_MASK
, syndrome
,
380 if (err_detect
& DDR_EDE_MBE
)
381 edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED
, mci
, 1,
382 pfn
, err_addr
& ~PAGE_MASK
, syndrome
,
386 ddr_out32(pdata
, FSL_MC_ERR_DETECT
, err_detect
);
389 static irqreturn_t
fsl_mc_isr(int irq
, void *dev_id
)
391 struct mem_ctl_info
*mci
= dev_id
;
392 struct fsl_mc_pdata
*pdata
= mci
->pvt_info
;
395 err_detect
= ddr_in32(pdata
, FSL_MC_ERR_DETECT
);
404 static void fsl_ddr_init_csrows(struct mem_ctl_info
*mci
)
406 struct fsl_mc_pdata
*pdata
= mci
->pvt_info
;
407 struct csrow_info
*csrow
;
408 struct dimm_info
*dimm
;
415 sdram_ctl
= ddr_in32(pdata
, FSL_MC_DDR_SDRAM_CFG
);
417 sdtype
= sdram_ctl
& DSC_SDTYPE_MASK
;
418 if (sdram_ctl
& DSC_RD_EN
) {
459 for (index
= 0; index
< mci
->nr_csrows
; index
++) {
463 csrow
= mci
->csrows
[index
];
464 dimm
= csrow
->channels
[0]->dimm
;
466 cs_bnds
= ddr_in32(pdata
, FSL_MC_CS_BNDS_0
+
467 (index
* FSL_MC_CS_BNDS_OFS
));
469 start
= (cs_bnds
& 0xffff0000) >> 16;
470 end
= (cs_bnds
& 0x0000ffff);
473 continue; /* not populated */
475 start
<<= (24 - PAGE_SHIFT
);
476 end
<<= (24 - PAGE_SHIFT
);
477 end
|= (1 << (24 - PAGE_SHIFT
)) - 1;
479 csrow
->first_page
= start
;
480 csrow
->last_page
= end
;
482 dimm
->nr_pages
= end
+ 1 - start
;
485 dimm
->dtype
= DEV_UNKNOWN
;
486 if (pdata
->flag
== TYPE_IMX9
)
487 dimm
->dtype
= DEV_X16
;
488 else if (sdram_ctl
& DSC_X32_EN
)
489 dimm
->dtype
= DEV_X32
;
490 dimm
->edac_mode
= EDAC_SECDED
;
494 int fsl_mc_err_probe(struct platform_device
*op
)
496 struct mem_ctl_info
*mci
;
497 struct edac_mc_layer layers
[2];
498 struct fsl_mc_pdata
*pdata
;
504 if (!devres_open_group(&op
->dev
, fsl_mc_err_probe
, GFP_KERNEL
))
507 layers
[0].type
= EDAC_MC_LAYER_CHIP_SELECT
;
509 layers
[0].is_virt_csrow
= true;
510 layers
[1].type
= EDAC_MC_LAYER_CHANNEL
;
512 layers
[1].is_virt_csrow
= false;
513 mci
= edac_mc_alloc(edac_mc_idx
, ARRAY_SIZE(layers
), layers
,
516 devres_release_group(&op
->dev
, fsl_mc_err_probe
);
520 pdata
= mci
->pvt_info
;
521 pdata
->name
= "fsl_mc_err";
522 mci
->pdev
= &op
->dev
;
523 pdata
->edac_idx
= edac_mc_idx
++;
524 dev_set_drvdata(mci
->pdev
, mci
);
525 mci
->ctl_name
= pdata
->name
;
526 mci
->dev_name
= pdata
->name
;
528 pdata
->flag
= (unsigned long)device_get_match_data(&op
->dev
);
531 * Get the endianness of DDR controller registers.
532 * Default is big endian.
534 pdata
->little_endian
= of_property_read_bool(op
->dev
.of_node
, "little-endian");
536 res
= of_address_to_resource(op
->dev
.of_node
, 0, &r
);
538 pr_err("%s: Unable to get resource for MC err regs\n",
543 if (!devm_request_mem_region(&op
->dev
, r
.start
, resource_size(&r
),
545 pr_err("%s: Error while requesting mem region\n",
551 pdata
->mc_vbase
= devm_ioremap(&op
->dev
, r
.start
, resource_size(&r
));
552 if (!pdata
->mc_vbase
) {
553 pr_err("%s: Unable to setup MC err regs\n", __func__
);
558 if (pdata
->flag
== TYPE_IMX9
) {
559 pdata
->inject_vbase
= devm_platform_ioremap_resource_byname(op
, "inject");
560 if (IS_ERR(pdata
->inject_vbase
)) {
566 if (pdata
->flag
== TYPE_IMX9
) {
567 sdram_ctl
= ddr_in32(pdata
, IMX9_MC_ERR_EN
);
568 ecc_en_mask
= ERR_ECC_EN
| ERR_INLINE_ECC
;
570 sdram_ctl
= ddr_in32(pdata
, FSL_MC_DDR_SDRAM_CFG
);
571 ecc_en_mask
= DSC_ECC_EN
;
574 if ((sdram_ctl
& ecc_en_mask
) != ecc_en_mask
) {
576 pr_warn("%s: No ECC DIMMs discovered\n", __func__
);
581 edac_dbg(3, "init mci\n");
582 mci
->mtype_cap
= MEM_FLAG_DDR
| MEM_FLAG_RDDR
|
583 MEM_FLAG_DDR2
| MEM_FLAG_RDDR2
|
584 MEM_FLAG_DDR3
| MEM_FLAG_RDDR3
|
585 MEM_FLAG_DDR4
| MEM_FLAG_RDDR4
|
587 mci
->edac_ctl_cap
= EDAC_FLAG_NONE
| EDAC_FLAG_SECDED
;
588 mci
->edac_cap
= EDAC_FLAG_SECDED
;
589 mci
->mod_name
= EDAC_MOD_STR
;
591 if (edac_op_state
== EDAC_OPSTATE_POLL
)
592 mci
->edac_check
= fsl_mc_check
;
594 mci
->ctl_page_to_phys
= NULL
;
596 mci
->scrub_mode
= SCRUB_SW_SRC
;
598 fsl_ddr_init_csrows(mci
);
600 /* store the original error disable bits */
601 pdata
->orig_ddr_err_disable
= ddr_in32(pdata
, FSL_MC_ERR_DISABLE
);
602 ddr_out32(pdata
, FSL_MC_ERR_DISABLE
, 0);
604 /* clear all error bits */
605 ddr_out32(pdata
, FSL_MC_ERR_DETECT
, ~0);
607 res
= edac_mc_add_mc_with_groups(mci
, fsl_ddr_dev_groups
);
609 edac_dbg(3, "failed edac_mc_add_mc()\n");
613 if (edac_op_state
== EDAC_OPSTATE_INT
) {
614 ddr_out32(pdata
, FSL_MC_ERR_INT_EN
,
615 DDR_EIE_MBEE
| DDR_EIE_SBEE
);
617 /* store the original error management threshold */
618 pdata
->orig_ddr_err_sbe
= ddr_in32(pdata
,
619 FSL_MC_ERR_SBE
) & 0xff0000;
621 /* set threshold to 1 error per interrupt */
622 ddr_out32(pdata
, FSL_MC_ERR_SBE
, 0x10000);
624 /* register interrupts */
625 pdata
->irq
= platform_get_irq(op
, 0);
626 res
= devm_request_irq(&op
->dev
, pdata
->irq
,
629 "[EDAC] MC err", mci
);
631 pr_err("%s: Unable to request irq %d for FSL DDR DRAM ERR\n",
632 __func__
, pdata
->irq
);
637 pr_info(EDAC_MOD_STR
" acquired irq %d for MC\n",
641 devres_remove_group(&op
->dev
, fsl_mc_err_probe
);
642 edac_dbg(3, "success\n");
643 pr_info(EDAC_MOD_STR
" MC err registered\n");
648 edac_mc_del_mc(&op
->dev
);
650 devres_release_group(&op
->dev
, fsl_mc_err_probe
);
655 void fsl_mc_err_remove(struct platform_device
*op
)
657 struct mem_ctl_info
*mci
= dev_get_drvdata(&op
->dev
);
658 struct fsl_mc_pdata
*pdata
= mci
->pvt_info
;
662 if (edac_op_state
== EDAC_OPSTATE_INT
) {
663 ddr_out32(pdata
, FSL_MC_ERR_INT_EN
, 0);
666 ddr_out32(pdata
, FSL_MC_ERR_DISABLE
,
667 pdata
->orig_ddr_err_disable
);
668 ddr_out32(pdata
, FSL_MC_ERR_SBE
, pdata
->orig_ddr_err_sbe
);
671 edac_mc_del_mc(&op
->dev
);