1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2017-2018, Intel Corporation. All rights reserved
4 * Copyright Altera Corporation (C) 2014-2016. All rights reserved.
5 * Copyright 2011-2012 Calxeda, Inc.
8 #include <asm/cacheflush.h>
9 #include <linux/ctype.h>
10 #include <linux/delay.h>
11 #include <linux/edac.h>
12 #include <linux/firmware/intel/stratix10-smc.h>
13 #include <linux/genalloc.h>
14 #include <linux/interrupt.h>
15 #include <linux/irqchip/chained_irq.h>
16 #include <linux/kernel.h>
17 #include <linux/mfd/altera-sysmgr.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/notifier.h>
20 #include <linux/of_address.h>
21 #include <linux/of_irq.h>
22 #include <linux/of_platform.h>
23 #include <linux/panic_notifier.h>
24 #include <linux/platform_device.h>
25 #include <linux/property.h>
26 #include <linux/regmap.h>
27 #include <linux/types.h>
28 #include <linux/uaccess.h>
30 #include "altera_edac.h"
31 #include "edac_module.h"
33 #define EDAC_MOD_STR "altera_edac"
34 #define EDAC_DEVICE "Altera"
36 #ifdef CONFIG_EDAC_ALTERA_SDRAM
37 static const struct altr_sdram_prv_data c5_data
= {
38 .ecc_ctrl_offset
= CV_CTLCFG_OFST
,
39 .ecc_ctl_en_mask
= CV_CTLCFG_ECC_AUTO_EN
,
40 .ecc_stat_offset
= CV_DRAMSTS_OFST
,
41 .ecc_stat_ce_mask
= CV_DRAMSTS_SBEERR
,
42 .ecc_stat_ue_mask
= CV_DRAMSTS_DBEERR
,
43 .ecc_saddr_offset
= CV_ERRADDR_OFST
,
44 .ecc_daddr_offset
= CV_ERRADDR_OFST
,
45 .ecc_cecnt_offset
= CV_SBECOUNT_OFST
,
46 .ecc_uecnt_offset
= CV_DBECOUNT_OFST
,
47 .ecc_irq_en_offset
= CV_DRAMINTR_OFST
,
48 .ecc_irq_en_mask
= CV_DRAMINTR_INTREN
,
49 .ecc_irq_clr_offset
= CV_DRAMINTR_OFST
,
50 .ecc_irq_clr_mask
= (CV_DRAMINTR_INTRCLR
| CV_DRAMINTR_INTREN
),
51 .ecc_cnt_rst_offset
= CV_DRAMINTR_OFST
,
52 .ecc_cnt_rst_mask
= CV_DRAMINTR_INTRCLR
,
53 .ce_ue_trgr_offset
= CV_CTLCFG_OFST
,
54 .ce_set_mask
= CV_CTLCFG_GEN_SB_ERR
,
55 .ue_set_mask
= CV_CTLCFG_GEN_DB_ERR
,
58 static const struct altr_sdram_prv_data a10_data
= {
59 .ecc_ctrl_offset
= A10_ECCCTRL1_OFST
,
60 .ecc_ctl_en_mask
= A10_ECCCTRL1_ECC_EN
,
61 .ecc_stat_offset
= A10_INTSTAT_OFST
,
62 .ecc_stat_ce_mask
= A10_INTSTAT_SBEERR
,
63 .ecc_stat_ue_mask
= A10_INTSTAT_DBEERR
,
64 .ecc_saddr_offset
= A10_SERRADDR_OFST
,
65 .ecc_daddr_offset
= A10_DERRADDR_OFST
,
66 .ecc_irq_en_offset
= A10_ERRINTEN_OFST
,
67 .ecc_irq_en_mask
= A10_ECC_IRQ_EN_MASK
,
68 .ecc_irq_clr_offset
= A10_INTSTAT_OFST
,
69 .ecc_irq_clr_mask
= (A10_INTSTAT_SBEERR
| A10_INTSTAT_DBEERR
),
70 .ecc_cnt_rst_offset
= A10_ECCCTRL1_OFST
,
71 .ecc_cnt_rst_mask
= A10_ECC_CNT_RESET_MASK
,
72 .ce_ue_trgr_offset
= A10_DIAGINTTEST_OFST
,
73 .ce_set_mask
= A10_DIAGINT_TSERRA_MASK
,
74 .ue_set_mask
= A10_DIAGINT_TDERRA_MASK
,
77 /*********************** EDAC Memory Controller Functions ****************/
79 /* The SDRAM controller uses the EDAC Memory Controller framework. */
81 static irqreturn_t
altr_sdram_mc_err_handler(int irq
, void *dev_id
)
83 struct mem_ctl_info
*mci
= dev_id
;
84 struct altr_sdram_mc_data
*drvdata
= mci
->pvt_info
;
85 const struct altr_sdram_prv_data
*priv
= drvdata
->data
;
86 u32 status
, err_count
= 1, err_addr
;
88 regmap_read(drvdata
->mc_vbase
, priv
->ecc_stat_offset
, &status
);
90 if (status
& priv
->ecc_stat_ue_mask
) {
91 regmap_read(drvdata
->mc_vbase
, priv
->ecc_daddr_offset
,
93 if (priv
->ecc_uecnt_offset
)
94 regmap_read(drvdata
->mc_vbase
, priv
->ecc_uecnt_offset
,
96 panic("\nEDAC: [%d Uncorrectable errors @ 0x%08X]\n",
99 if (status
& priv
->ecc_stat_ce_mask
) {
100 regmap_read(drvdata
->mc_vbase
, priv
->ecc_saddr_offset
,
102 if (priv
->ecc_uecnt_offset
)
103 regmap_read(drvdata
->mc_vbase
, priv
->ecc_cecnt_offset
,
105 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED
, mci
, err_count
,
106 err_addr
>> PAGE_SHIFT
,
107 err_addr
& ~PAGE_MASK
, 0,
108 0, 0, -1, mci
->ctl_name
, "");
109 /* Clear IRQ to resume */
110 regmap_write(drvdata
->mc_vbase
, priv
->ecc_irq_clr_offset
,
111 priv
->ecc_irq_clr_mask
);
118 static ssize_t
altr_sdr_mc_err_inject_write(struct file
*file
,
119 const char __user
*data
,
120 size_t count
, loff_t
*ppos
)
122 struct mem_ctl_info
*mci
= file
->private_data
;
123 struct altr_sdram_mc_data
*drvdata
= mci
->pvt_info
;
124 const struct altr_sdram_prv_data
*priv
= drvdata
->data
;
126 dma_addr_t dma_handle
;
129 ptemp
= dma_alloc_coherent(mci
->pdev
, 16, &dma_handle
, GFP_KERNEL
);
131 dma_free_coherent(mci
->pdev
, 16, ptemp
, dma_handle
);
132 edac_printk(KERN_ERR
, EDAC_MC
,
133 "Inject: Buffer Allocation error\n");
137 regmap_read(drvdata
->mc_vbase
, priv
->ce_ue_trgr_offset
,
139 read_reg
&= ~(priv
->ce_set_mask
| priv
->ue_set_mask
);
141 /* Error are injected by writing a word while the SBE or DBE
142 * bit in the CTLCFG register is set. Reading the word will
143 * trigger the SBE or DBE error and the corresponding IRQ.
146 edac_printk(KERN_ALERT
, EDAC_MC
,
147 "Inject Double bit error\n");
149 regmap_write(drvdata
->mc_vbase
, priv
->ce_ue_trgr_offset
,
150 (read_reg
| priv
->ue_set_mask
));
153 edac_printk(KERN_ALERT
, EDAC_MC
,
154 "Inject Single bit error\n");
156 regmap_write(drvdata
->mc_vbase
, priv
->ce_ue_trgr_offset
,
157 (read_reg
| priv
->ce_set_mask
));
161 ptemp
[0] = 0x5A5A5A5A;
162 ptemp
[1] = 0xA5A5A5A5;
164 /* Clear the error injection bits */
165 regmap_write(drvdata
->mc_vbase
, priv
->ce_ue_trgr_offset
, read_reg
);
166 /* Ensure it has been written out */
170 * To trigger the error, we need to read the data back
171 * (the data was written with errors above).
172 * The READ_ONCE macros and printk are used to prevent the
173 * the compiler optimizing these reads out.
175 reg
= READ_ONCE(ptemp
[0]);
176 read_reg
= READ_ONCE(ptemp
[1]);
180 edac_printk(KERN_ALERT
, EDAC_MC
, "Read Data [0x%X, 0x%X]\n",
183 dma_free_coherent(mci
->pdev
, 16, ptemp
, dma_handle
);
188 static const struct file_operations altr_sdr_mc_debug_inject_fops
= {
190 .write
= altr_sdr_mc_err_inject_write
,
191 .llseek
= generic_file_llseek
,
194 static void altr_sdr_mc_create_debugfs_nodes(struct mem_ctl_info
*mci
)
196 if (!IS_ENABLED(CONFIG_EDAC_DEBUG
))
202 edac_debugfs_create_file("altr_trigger", S_IWUSR
, mci
->debugfs
, mci
,
203 &altr_sdr_mc_debug_inject_fops
);
206 /* Get total memory size from Open Firmware DTB */
207 static unsigned long get_total_mem(void)
209 struct device_node
*np
= NULL
;
212 unsigned long total_mem
= 0;
214 for_each_node_by_type(np
, "memory") {
215 ret
= of_address_to_resource(np
, 0, &res
);
219 total_mem
+= resource_size(&res
);
221 edac_dbg(0, "total_mem 0x%lx\n", total_mem
);
225 static const struct of_device_id altr_sdram_ctrl_of_match
[] = {
226 { .compatible
= "altr,sdram-edac", .data
= &c5_data
},
227 { .compatible
= "altr,sdram-edac-a10", .data
= &a10_data
},
230 MODULE_DEVICE_TABLE(of
, altr_sdram_ctrl_of_match
);
232 static int a10_init(struct regmap
*mc_vbase
)
234 if (regmap_update_bits(mc_vbase
, A10_INTMODE_OFST
,
235 A10_INTMODE_SB_INT
, A10_INTMODE_SB_INT
)) {
236 edac_printk(KERN_ERR
, EDAC_MC
,
237 "Error setting SB IRQ mode\n");
241 if (regmap_write(mc_vbase
, A10_SERRCNTREG_OFST
, 1)) {
242 edac_printk(KERN_ERR
, EDAC_MC
,
243 "Error setting trigger count\n");
250 static int a10_unmask_irq(struct platform_device
*pdev
, u32 mask
)
252 void __iomem
*sm_base
;
255 if (!request_mem_region(A10_SYMAN_INTMASK_CLR
, sizeof(u32
),
256 dev_name(&pdev
->dev
))) {
257 edac_printk(KERN_ERR
, EDAC_MC
,
258 "Unable to request mem region\n");
262 sm_base
= ioremap(A10_SYMAN_INTMASK_CLR
, sizeof(u32
));
264 edac_printk(KERN_ERR
, EDAC_MC
,
265 "Unable to ioremap device\n");
271 iowrite32(mask
, sm_base
);
276 release_mem_region(A10_SYMAN_INTMASK_CLR
, sizeof(u32
));
281 static int altr_sdram_probe(struct platform_device
*pdev
)
283 struct edac_mc_layer layers
[2];
284 struct mem_ctl_info
*mci
;
285 struct altr_sdram_mc_data
*drvdata
;
286 const struct altr_sdram_prv_data
*priv
;
287 struct regmap
*mc_vbase
;
288 struct dimm_info
*dimm
;
290 int irq
, irq2
, res
= 0;
291 unsigned long mem_size
, irqflags
= 0;
293 /* Grab the register range from the sdr controller in device tree */
294 mc_vbase
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
,
296 if (IS_ERR(mc_vbase
)) {
297 edac_printk(KERN_ERR
, EDAC_MC
,
298 "regmap for altr,sdr-syscon lookup failed.\n");
302 /* Check specific dependencies for the module */
303 priv
= device_get_match_data(&pdev
->dev
);
305 /* Validate the SDRAM controller has ECC enabled */
306 if (regmap_read(mc_vbase
, priv
->ecc_ctrl_offset
, &read_reg
) ||
307 ((read_reg
& priv
->ecc_ctl_en_mask
) != priv
->ecc_ctl_en_mask
)) {
308 edac_printk(KERN_ERR
, EDAC_MC
,
309 "No ECC/ECC disabled [0x%08X]\n", read_reg
);
313 /* Grab memory size from device tree. */
314 mem_size
= get_total_mem();
316 edac_printk(KERN_ERR
, EDAC_MC
, "Unable to calculate memory size\n");
320 /* Ensure the SDRAM Interrupt is disabled */
321 if (regmap_update_bits(mc_vbase
, priv
->ecc_irq_en_offset
,
322 priv
->ecc_irq_en_mask
, 0)) {
323 edac_printk(KERN_ERR
, EDAC_MC
,
324 "Error disabling SDRAM ECC IRQ\n");
328 /* Toggle to clear the SDRAM Error count */
329 if (regmap_update_bits(mc_vbase
, priv
->ecc_cnt_rst_offset
,
330 priv
->ecc_cnt_rst_mask
,
331 priv
->ecc_cnt_rst_mask
)) {
332 edac_printk(KERN_ERR
, EDAC_MC
,
333 "Error clearing SDRAM ECC count\n");
337 if (regmap_update_bits(mc_vbase
, priv
->ecc_cnt_rst_offset
,
338 priv
->ecc_cnt_rst_mask
, 0)) {
339 edac_printk(KERN_ERR
, EDAC_MC
,
340 "Error clearing SDRAM ECC count\n");
344 irq
= platform_get_irq(pdev
, 0);
346 edac_printk(KERN_ERR
, EDAC_MC
,
347 "No irq %d in DT\n", irq
);
351 /* Arria10 has a 2nd IRQ */
352 irq2
= platform_get_irq(pdev
, 1);
354 layers
[0].type
= EDAC_MC_LAYER_CHIP_SELECT
;
356 layers
[0].is_virt_csrow
= true;
357 layers
[1].type
= EDAC_MC_LAYER_CHANNEL
;
359 layers
[1].is_virt_csrow
= false;
360 mci
= edac_mc_alloc(0, ARRAY_SIZE(layers
), layers
,
361 sizeof(struct altr_sdram_mc_data
));
365 mci
->pdev
= &pdev
->dev
;
366 drvdata
= mci
->pvt_info
;
367 drvdata
->mc_vbase
= mc_vbase
;
368 drvdata
->data
= priv
;
369 platform_set_drvdata(pdev
, mci
);
371 if (!devres_open_group(&pdev
->dev
, NULL
, GFP_KERNEL
)) {
372 edac_printk(KERN_ERR
, EDAC_MC
,
373 "Unable to get managed device resource\n");
378 mci
->mtype_cap
= MEM_FLAG_DDR3
;
379 mci
->edac_ctl_cap
= EDAC_FLAG_NONE
| EDAC_FLAG_SECDED
;
380 mci
->edac_cap
= EDAC_FLAG_SECDED
;
381 mci
->mod_name
= EDAC_MOD_STR
;
382 mci
->ctl_name
= dev_name(&pdev
->dev
);
383 mci
->scrub_mode
= SCRUB_SW_SRC
;
384 mci
->dev_name
= dev_name(&pdev
->dev
);
387 dimm
->nr_pages
= ((mem_size
- 1) >> PAGE_SHIFT
) + 1;
389 dimm
->dtype
= DEV_X8
;
390 dimm
->mtype
= MEM_DDR3
;
391 dimm
->edac_mode
= EDAC_SECDED
;
393 res
= edac_mc_add_mc(mci
);
397 /* Only the Arria10 has separate IRQs */
398 if (of_machine_is_compatible("altr,socfpga-arria10")) {
399 /* Arria10 specific initialization */
400 res
= a10_init(mc_vbase
);
404 res
= devm_request_irq(&pdev
->dev
, irq2
,
405 altr_sdram_mc_err_handler
,
406 IRQF_SHARED
, dev_name(&pdev
->dev
), mci
);
408 edac_mc_printk(mci
, KERN_ERR
,
409 "Unable to request irq %d\n", irq2
);
414 res
= a10_unmask_irq(pdev
, A10_DDR0_IRQ_MASK
);
418 irqflags
= IRQF_SHARED
;
421 res
= devm_request_irq(&pdev
->dev
, irq
, altr_sdram_mc_err_handler
,
422 irqflags
, dev_name(&pdev
->dev
), mci
);
424 edac_mc_printk(mci
, KERN_ERR
,
425 "Unable to request irq %d\n", irq
);
430 /* Infrastructure ready - enable the IRQ */
431 if (regmap_update_bits(drvdata
->mc_vbase
, priv
->ecc_irq_en_offset
,
432 priv
->ecc_irq_en_mask
, priv
->ecc_irq_en_mask
)) {
433 edac_mc_printk(mci
, KERN_ERR
,
434 "Error enabling SDRAM ECC IRQ\n");
439 altr_sdr_mc_create_debugfs_nodes(mci
);
441 devres_close_group(&pdev
->dev
, NULL
);
446 edac_mc_del_mc(&pdev
->dev
);
448 devres_release_group(&pdev
->dev
, NULL
);
451 edac_printk(KERN_ERR
, EDAC_MC
,
452 "EDAC Probe Failed; Error %d\n", res
);
457 static void altr_sdram_remove(struct platform_device
*pdev
)
459 struct mem_ctl_info
*mci
= platform_get_drvdata(pdev
);
461 edac_mc_del_mc(&pdev
->dev
);
463 platform_set_drvdata(pdev
, NULL
);
467 * If you want to suspend, need to disable EDAC by removing it
468 * from the device tree or defconfig.
471 static int altr_sdram_prepare(struct device
*dev
)
473 pr_err("Suspend not allowed when EDAC is enabled.\n");
478 static const struct dev_pm_ops altr_sdram_pm_ops
= {
479 .prepare
= altr_sdram_prepare
,
483 static struct platform_driver altr_sdram_edac_driver
= {
484 .probe
= altr_sdram_probe
,
485 .remove
= altr_sdram_remove
,
487 .name
= "altr_sdram_edac",
489 .pm
= &altr_sdram_pm_ops
,
491 .of_match_table
= altr_sdram_ctrl_of_match
,
495 module_platform_driver(altr_sdram_edac_driver
);
497 #endif /* CONFIG_EDAC_ALTERA_SDRAM */
499 /************************* EDAC Parent Probe *************************/
501 static const struct of_device_id altr_edac_device_of_match
[];
503 static const struct of_device_id altr_edac_of_match
[] = {
504 { .compatible
= "altr,socfpga-ecc-manager" },
507 MODULE_DEVICE_TABLE(of
, altr_edac_of_match
);
509 static int altr_edac_probe(struct platform_device
*pdev
)
511 of_platform_populate(pdev
->dev
.of_node
, altr_edac_device_of_match
,
516 static struct platform_driver altr_edac_driver
= {
517 .probe
= altr_edac_probe
,
519 .name
= "socfpga_ecc_manager",
520 .of_match_table
= altr_edac_of_match
,
523 module_platform_driver(altr_edac_driver
);
525 /************************* EDAC Device Functions *************************/
528 * EDAC Device Functions (shared between various IPs).
529 * The discrete memories use the EDAC Device framework. The probe
530 * and error handling functions are very similar between memories
531 * so they are shared. The memory allocation and freeing for EDAC
532 * trigger testing are different for each memory.
535 #ifdef CONFIG_EDAC_ALTERA_OCRAM
536 static const struct edac_device_prv_data ocramecc_data
;
538 #ifdef CONFIG_EDAC_ALTERA_L2C
539 static const struct edac_device_prv_data l2ecc_data
;
541 #ifdef CONFIG_EDAC_ALTERA_OCRAM
542 static const struct edac_device_prv_data a10_ocramecc_data
;
544 #ifdef CONFIG_EDAC_ALTERA_L2C
545 static const struct edac_device_prv_data a10_l2ecc_data
;
548 static irqreturn_t
altr_edac_device_handler(int irq
, void *dev_id
)
550 irqreturn_t ret_value
= IRQ_NONE
;
551 struct edac_device_ctl_info
*dci
= dev_id
;
552 struct altr_edac_device_dev
*drvdata
= dci
->pvt_info
;
553 const struct edac_device_prv_data
*priv
= drvdata
->data
;
555 if (irq
== drvdata
->sb_irq
) {
556 if (priv
->ce_clear_mask
)
557 writel(priv
->ce_clear_mask
, drvdata
->base
);
558 edac_device_handle_ce(dci
, 0, 0, drvdata
->edac_dev_name
);
559 ret_value
= IRQ_HANDLED
;
560 } else if (irq
== drvdata
->db_irq
) {
561 if (priv
->ue_clear_mask
)
562 writel(priv
->ue_clear_mask
, drvdata
->base
);
563 edac_device_handle_ue(dci
, 0, 0, drvdata
->edac_dev_name
);
564 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
565 ret_value
= IRQ_HANDLED
;
573 static ssize_t __maybe_unused
574 altr_edac_device_trig(struct file
*file
, const char __user
*user_buf
,
575 size_t count
, loff_t
*ppos
)
578 u32
*ptemp
, i
, error_mask
;
582 struct edac_device_ctl_info
*edac_dci
= file
->private_data
;
583 struct altr_edac_device_dev
*drvdata
= edac_dci
->pvt_info
;
584 const struct edac_device_prv_data
*priv
= drvdata
->data
;
585 void *generic_ptr
= edac_dci
->dev
;
587 if (!user_buf
|| get_user(trig_type
, user_buf
))
590 if (!priv
->alloc_mem
)
594 * Note that generic_ptr is initialized to the device * but in
595 * some alloc_functions, this is overridden and returns data.
597 ptemp
= priv
->alloc_mem(priv
->trig_alloc_sz
, &generic_ptr
);
599 edac_printk(KERN_ERR
, EDAC_DEVICE
,
600 "Inject: Buffer Allocation error\n");
604 if (trig_type
== ALTR_UE_TRIGGER_CHAR
)
605 error_mask
= priv
->ue_set_mask
;
607 error_mask
= priv
->ce_set_mask
;
609 edac_printk(KERN_ALERT
, EDAC_DEVICE
,
610 "Trigger Error Mask (0x%X)\n", error_mask
);
612 local_irq_save(flags
);
613 /* write ECC corrupted data out. */
614 for (i
= 0; i
< (priv
->trig_alloc_sz
/ sizeof(*ptemp
)); i
++) {
615 /* Read data so we're in the correct state */
617 if (READ_ONCE(ptemp
[i
]))
619 /* Toggle Error bit (it is latched), leave ECC enabled */
620 writel(error_mask
, (drvdata
->base
+ priv
->set_err_ofst
));
621 writel(priv
->ecc_enable_mask
, (drvdata
->base
+
622 priv
->set_err_ofst
));
625 /* Ensure it has been written out */
627 local_irq_restore(flags
);
630 edac_printk(KERN_ERR
, EDAC_DEVICE
, "Mem Not Cleared\n");
632 /* Read out written data. ECC error caused here */
633 for (i
= 0; i
< ALTR_TRIGGER_READ_WRD_CNT
; i
++)
634 if (READ_ONCE(ptemp
[i
]) != i
)
635 edac_printk(KERN_ERR
, EDAC_DEVICE
,
636 "Read doesn't match written data\n");
639 priv
->free_mem(ptemp
, priv
->trig_alloc_sz
, generic_ptr
);
644 static const struct file_operations altr_edac_device_inject_fops __maybe_unused
= {
646 .write
= altr_edac_device_trig
,
647 .llseek
= generic_file_llseek
,
650 static ssize_t __maybe_unused
651 altr_edac_a10_device_trig(struct file
*file
, const char __user
*user_buf
,
652 size_t count
, loff_t
*ppos
);
654 static const struct file_operations altr_edac_a10_device_inject_fops __maybe_unused
= {
656 .write
= altr_edac_a10_device_trig
,
657 .llseek
= generic_file_llseek
,
660 static ssize_t __maybe_unused
661 altr_edac_a10_device_trig2(struct file
*file
, const char __user
*user_buf
,
662 size_t count
, loff_t
*ppos
);
664 static const struct file_operations altr_edac_a10_device_inject2_fops __maybe_unused
= {
666 .write
= altr_edac_a10_device_trig2
,
667 .llseek
= generic_file_llseek
,
670 static void altr_create_edacdev_dbgfs(struct edac_device_ctl_info
*edac_dci
,
671 const struct edac_device_prv_data
*priv
)
673 struct altr_edac_device_dev
*drvdata
= edac_dci
->pvt_info
;
675 if (!IS_ENABLED(CONFIG_EDAC_DEBUG
))
678 drvdata
->debugfs_dir
= edac_debugfs_create_dir(drvdata
->edac_dev_name
);
679 if (!drvdata
->debugfs_dir
)
682 if (!edac_debugfs_create_file("altr_trigger", S_IWUSR
,
683 drvdata
->debugfs_dir
, edac_dci
,
685 debugfs_remove_recursive(drvdata
->debugfs_dir
);
688 static const struct of_device_id altr_edac_device_of_match
[] = {
689 #ifdef CONFIG_EDAC_ALTERA_L2C
690 { .compatible
= "altr,socfpga-l2-ecc", .data
= &l2ecc_data
},
692 #ifdef CONFIG_EDAC_ALTERA_OCRAM
693 { .compatible
= "altr,socfpga-ocram-ecc", .data
= &ocramecc_data
},
697 MODULE_DEVICE_TABLE(of
, altr_edac_device_of_match
);
700 * altr_edac_device_probe()
701 * This is a generic EDAC device driver that will support
702 * various Altera memory devices such as the L2 cache ECC and
703 * OCRAM ECC as well as the memories for other peripherals.
704 * Module specific initialization is done by passing the
705 * function index in the device tree.
707 static int altr_edac_device_probe(struct platform_device
*pdev
)
709 struct edac_device_ctl_info
*dci
;
710 struct altr_edac_device_dev
*drvdata
;
713 struct device_node
*np
= pdev
->dev
.of_node
;
714 char *ecc_name
= (char *)np
->name
;
715 static int dev_instance
;
717 if (!devres_open_group(&pdev
->dev
, NULL
, GFP_KERNEL
)) {
718 edac_printk(KERN_ERR
, EDAC_DEVICE
,
719 "Unable to open devm\n");
723 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
725 edac_printk(KERN_ERR
, EDAC_DEVICE
,
726 "Unable to get mem resource\n");
731 if (!devm_request_mem_region(&pdev
->dev
, r
->start
, resource_size(r
),
732 dev_name(&pdev
->dev
))) {
733 edac_printk(KERN_ERR
, EDAC_DEVICE
,
734 "%s:Error requesting mem region\n", ecc_name
);
739 dci
= edac_device_alloc_ctl_info(sizeof(*drvdata
), ecc_name
,
740 1, ecc_name
, 1, 0, dev_instance
++);
743 edac_printk(KERN_ERR
, EDAC_DEVICE
,
744 "%s: Unable to allocate EDAC device\n", ecc_name
);
749 drvdata
= dci
->pvt_info
;
750 dci
->dev
= &pdev
->dev
;
751 platform_set_drvdata(pdev
, dci
);
752 drvdata
->edac_dev_name
= ecc_name
;
754 drvdata
->base
= devm_ioremap(&pdev
->dev
, r
->start
, resource_size(r
));
755 if (!drvdata
->base
) {
760 /* Get driver specific data for this EDAC device */
761 drvdata
->data
= of_match_node(altr_edac_device_of_match
, np
)->data
;
763 /* Check specific dependencies for the module */
764 if (drvdata
->data
->setup
) {
765 res
= drvdata
->data
->setup(drvdata
);
770 drvdata
->sb_irq
= platform_get_irq(pdev
, 0);
771 res
= devm_request_irq(&pdev
->dev
, drvdata
->sb_irq
,
772 altr_edac_device_handler
,
773 0, dev_name(&pdev
->dev
), dci
);
777 drvdata
->db_irq
= platform_get_irq(pdev
, 1);
778 res
= devm_request_irq(&pdev
->dev
, drvdata
->db_irq
,
779 altr_edac_device_handler
,
780 0, dev_name(&pdev
->dev
), dci
);
784 dci
->mod_name
= "Altera ECC Manager";
785 dci
->dev_name
= drvdata
->edac_dev_name
;
787 res
= edac_device_add_device(dci
);
791 altr_create_edacdev_dbgfs(dci
, drvdata
->data
);
793 devres_close_group(&pdev
->dev
, NULL
);
798 edac_device_free_ctl_info(dci
);
800 devres_release_group(&pdev
->dev
, NULL
);
801 edac_printk(KERN_ERR
, EDAC_DEVICE
,
802 "%s:Error setting up EDAC device: %d\n", ecc_name
, res
);
807 static void altr_edac_device_remove(struct platform_device
*pdev
)
809 struct edac_device_ctl_info
*dci
= platform_get_drvdata(pdev
);
810 struct altr_edac_device_dev
*drvdata
= dci
->pvt_info
;
812 debugfs_remove_recursive(drvdata
->debugfs_dir
);
813 edac_device_del_device(&pdev
->dev
);
814 edac_device_free_ctl_info(dci
);
817 static struct platform_driver altr_edac_device_driver
= {
818 .probe
= altr_edac_device_probe
,
819 .remove
= altr_edac_device_remove
,
821 .name
= "altr_edac_device",
822 .of_match_table
= altr_edac_device_of_match
,
825 module_platform_driver(altr_edac_device_driver
);
827 /******************* Arria10 Device ECC Shared Functions *****************/
830 * Test for memory's ECC dependencies upon entry because platform specific
831 * startup should have initialized the memory and enabled the ECC.
832 * Can't turn on ECC here because accessing un-initialized memory will
833 * cause CE/UE errors possibly causing an ABORT.
835 static int __maybe_unused
836 altr_check_ecc_deps(struct altr_edac_device_dev
*device
)
838 void __iomem
*base
= device
->base
;
839 const struct edac_device_prv_data
*prv
= device
->data
;
841 if (readl(base
+ prv
->ecc_en_ofst
) & prv
->ecc_enable_mask
)
844 edac_printk(KERN_ERR
, EDAC_DEVICE
,
845 "%s: No ECC present or ECC disabled.\n",
846 device
->edac_dev_name
);
850 static irqreturn_t __maybe_unused
altr_edac_a10_ecc_irq(int irq
, void *dev_id
)
852 struct altr_edac_device_dev
*dci
= dev_id
;
853 void __iomem
*base
= dci
->base
;
855 if (irq
== dci
->sb_irq
) {
856 writel(ALTR_A10_ECC_SERRPENA
,
857 base
+ ALTR_A10_ECC_INTSTAT_OFST
);
858 edac_device_handle_ce(dci
->edac_dev
, 0, 0, dci
->edac_dev_name
);
861 } else if (irq
== dci
->db_irq
) {
862 writel(ALTR_A10_ECC_DERRPENA
,
863 base
+ ALTR_A10_ECC_INTSTAT_OFST
);
864 edac_device_handle_ue(dci
->edac_dev
, 0, 0, dci
->edac_dev_name
);
865 if (dci
->data
->panic
)
866 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
876 /******************* Arria10 Memory Buffer Functions *********************/
878 static inline int a10_get_irq_mask(struct device_node
*np
)
881 const u32
*handle
= of_get_property(np
, "interrupts", NULL
);
885 irq
= be32_to_cpup(handle
);
889 static inline void ecc_set_bits(u32 bit_mask
, void __iomem
*ioaddr
)
891 u32 value
= readl(ioaddr
);
894 writel(value
, ioaddr
);
897 static inline void ecc_clear_bits(u32 bit_mask
, void __iomem
*ioaddr
)
899 u32 value
= readl(ioaddr
);
902 writel(value
, ioaddr
);
905 static inline int ecc_test_bits(u32 bit_mask
, void __iomem
*ioaddr
)
907 u32 value
= readl(ioaddr
);
909 return (value
& bit_mask
) ? 1 : 0;
913 * This function uses the memory initialization block in the Arria10 ECC
914 * controller to initialize/clear the entire memory data and ECC data.
916 static int __maybe_unused
altr_init_memory_port(void __iomem
*ioaddr
, int port
)
918 int limit
= ALTR_A10_ECC_INIT_WATCHDOG_10US
;
919 u32 init_mask
, stat_mask
, clear_mask
;
923 init_mask
= ALTR_A10_ECC_INITB
;
924 stat_mask
= ALTR_A10_ECC_INITCOMPLETEB
;
925 clear_mask
= ALTR_A10_ECC_ERRPENB_MASK
;
927 init_mask
= ALTR_A10_ECC_INITA
;
928 stat_mask
= ALTR_A10_ECC_INITCOMPLETEA
;
929 clear_mask
= ALTR_A10_ECC_ERRPENA_MASK
;
932 ecc_set_bits(init_mask
, (ioaddr
+ ALTR_A10_ECC_CTRL_OFST
));
934 if (ecc_test_bits(stat_mask
,
935 (ioaddr
+ ALTR_A10_ECC_INITSTAT_OFST
)))
942 /* Clear any pending ECC interrupts */
943 writel(clear_mask
, (ioaddr
+ ALTR_A10_ECC_INTSTAT_OFST
));
948 static __init
int __maybe_unused
949 altr_init_a10_ecc_block(struct device_node
*np
, u32 irq_mask
,
950 u32 ecc_ctrl_en_mask
, bool dual_port
)
953 void __iomem
*ecc_block_base
;
954 struct regmap
*ecc_mgr_map
;
956 struct device_node
*np_eccmgr
;
958 ecc_name
= (char *)np
->name
;
960 /* Get the ECC Manager - parent of the device EDACs */
961 np_eccmgr
= of_get_parent(np
);
964 altr_sysmgr_regmap_lookup_by_phandle(np_eccmgr
,
965 "altr,sysmgr-syscon");
967 of_node_put(np_eccmgr
);
968 if (IS_ERR(ecc_mgr_map
)) {
969 edac_printk(KERN_ERR
, EDAC_DEVICE
,
970 "Unable to get syscon altr,sysmgr-syscon\n");
974 /* Map the ECC Block */
975 ecc_block_base
= of_iomap(np
, 0);
976 if (!ecc_block_base
) {
977 edac_printk(KERN_ERR
, EDAC_DEVICE
,
978 "Unable to map %s ECC block\n", ecc_name
);
983 regmap_write(ecc_mgr_map
, A10_SYSMGR_ECC_INTMASK_SET_OFST
, irq_mask
);
984 writel(ALTR_A10_ECC_SERRINTEN
,
985 (ecc_block_base
+ ALTR_A10_ECC_ERRINTENR_OFST
));
986 ecc_clear_bits(ecc_ctrl_en_mask
,
987 (ecc_block_base
+ ALTR_A10_ECC_CTRL_OFST
));
988 /* Ensure all writes complete */
990 /* Use HW initialization block to initialize memory for ECC */
991 ret
= altr_init_memory_port(ecc_block_base
, 0);
993 edac_printk(KERN_ERR
, EDAC_DEVICE
,
994 "ECC: cannot init %s PORTA memory\n", ecc_name
);
999 ret
= altr_init_memory_port(ecc_block_base
, 1);
1001 edac_printk(KERN_ERR
, EDAC_DEVICE
,
1002 "ECC: cannot init %s PORTB memory\n",
1008 /* Interrupt mode set to every SBERR */
1009 regmap_write(ecc_mgr_map
, ALTR_A10_ECC_INTMODE_OFST
,
1010 ALTR_A10_ECC_INTMODE
);
1012 ecc_set_bits(ecc_ctrl_en_mask
, (ecc_block_base
+
1013 ALTR_A10_ECC_CTRL_OFST
));
1014 writel(ALTR_A10_ECC_SERRINTEN
,
1015 (ecc_block_base
+ ALTR_A10_ECC_ERRINTENS_OFST
));
1016 regmap_write(ecc_mgr_map
, A10_SYSMGR_ECC_INTMASK_CLR_OFST
, irq_mask
);
1017 /* Ensure all writes complete */
1020 iounmap(ecc_block_base
);
1024 static int validate_parent_available(struct device_node
*np
);
1025 static const struct of_device_id altr_edac_a10_device_of_match
[];
1026 static int __init __maybe_unused
altr_init_a10_ecc_device_type(char *compat
)
1029 struct device_node
*child
, *np
;
1031 np
= of_find_compatible_node(NULL
, NULL
,
1032 "altr,socfpga-a10-ecc-manager");
1034 edac_printk(KERN_ERR
, EDAC_DEVICE
, "ECC Manager not found\n");
1038 for_each_child_of_node(np
, child
) {
1039 const struct of_device_id
*pdev_id
;
1040 const struct edac_device_prv_data
*prv
;
1042 if (!of_device_is_available(child
))
1044 if (!of_device_is_compatible(child
, compat
))
1047 if (validate_parent_available(child
))
1050 irq
= a10_get_irq_mask(child
);
1054 /* Get matching node and check for valid result */
1055 pdev_id
= of_match_node(altr_edac_a10_device_of_match
, child
);
1056 if (IS_ERR_OR_NULL(pdev_id
))
1059 /* Validate private data pointer before dereferencing */
1060 prv
= pdev_id
->data
;
1064 altr_init_a10_ecc_block(child
, BIT(irq
),
1065 prv
->ecc_enable_mask
, 0);
1072 /*********************** SDRAM EDAC Device Functions *********************/
1074 #ifdef CONFIG_EDAC_ALTERA_SDRAM
1077 * A legacy U-Boot bug only enabled memory mapped access to the ECC Enable
1078 * register if ECC is enabled. Linux checks the ECC Enable register to
1079 * determine ECC status.
1080 * Use an SMC call (which always works) to determine ECC enablement.
1082 static int altr_s10_sdram_check_ecc_deps(struct altr_edac_device_dev
*device
)
1084 const struct edac_device_prv_data
*prv
= device
->data
;
1085 unsigned long sdram_ecc_addr
;
1086 struct arm_smccc_res result
;
1087 struct device_node
*np
;
1088 phys_addr_t sdram_addr
;
1092 np
= of_find_compatible_node(NULL
, NULL
, "altr,sdr-ctl");
1096 sdram_addr
= of_translate_address(np
, of_get_address(np
, 0,
1099 sdram_ecc_addr
= (unsigned long)sdram_addr
+ prv
->ecc_en_ofst
;
1100 arm_smccc_smc(INTEL_SIP_SMC_REG_READ
, sdram_ecc_addr
,
1101 0, 0, 0, 0, 0, 0, &result
);
1102 read_reg
= (unsigned int)result
.a1
;
1103 ret
= (int)result
.a0
;
1104 if (!ret
&& (read_reg
& prv
->ecc_enable_mask
))
1108 edac_printk(KERN_ERR
, EDAC_DEVICE
,
1109 "%s: No ECC present or ECC disabled.\n",
1110 device
->edac_dev_name
);
1114 static const struct edac_device_prv_data s10_sdramecc_data
= {
1115 .setup
= altr_s10_sdram_check_ecc_deps
,
1116 .ce_clear_mask
= ALTR_S10_ECC_SERRPENA
,
1117 .ue_clear_mask
= ALTR_S10_ECC_DERRPENA
,
1118 .ecc_enable_mask
= ALTR_S10_ECC_EN
,
1119 .ecc_en_ofst
= ALTR_S10_ECC_CTRL_SDRAM_OFST
,
1120 .ce_set_mask
= ALTR_S10_ECC_TSERRA
,
1121 .ue_set_mask
= ALTR_S10_ECC_TDERRA
,
1122 .set_err_ofst
= ALTR_S10_ECC_INTTEST_OFST
,
1123 .ecc_irq_handler
= altr_edac_a10_ecc_irq
,
1124 .inject_fops
= &altr_edac_a10_device_inject_fops
,
1126 #endif /* CONFIG_EDAC_ALTERA_SDRAM */
1128 /*********************** OCRAM EDAC Device Functions *********************/
1130 #ifdef CONFIG_EDAC_ALTERA_OCRAM
1132 static void *ocram_alloc_mem(size_t size
, void **other
)
1134 struct device_node
*np
;
1135 struct gen_pool
*gp
;
1138 np
= of_find_compatible_node(NULL
, NULL
, "altr,socfpga-ocram-ecc");
1142 gp
= of_gen_pool_get(np
, "iram", 0);
1147 sram_addr
= (void *)gen_pool_alloc(gp
, size
);
1151 memset(sram_addr
, 0, size
);
1152 /* Ensure data is written out */
1155 /* Remember this handle for freeing later */
1161 static void ocram_free_mem(void *p
, size_t size
, void *other
)
1163 gen_pool_free((struct gen_pool
*)other
, (unsigned long)p
, size
);
1166 static const struct edac_device_prv_data ocramecc_data
= {
1167 .setup
= altr_check_ecc_deps
,
1168 .ce_clear_mask
= (ALTR_OCR_ECC_EN
| ALTR_OCR_ECC_SERR
),
1169 .ue_clear_mask
= (ALTR_OCR_ECC_EN
| ALTR_OCR_ECC_DERR
),
1170 .alloc_mem
= ocram_alloc_mem
,
1171 .free_mem
= ocram_free_mem
,
1172 .ecc_enable_mask
= ALTR_OCR_ECC_EN
,
1173 .ecc_en_ofst
= ALTR_OCR_ECC_REG_OFFSET
,
1174 .ce_set_mask
= (ALTR_OCR_ECC_EN
| ALTR_OCR_ECC_INJS
),
1175 .ue_set_mask
= (ALTR_OCR_ECC_EN
| ALTR_OCR_ECC_INJD
),
1176 .set_err_ofst
= ALTR_OCR_ECC_REG_OFFSET
,
1177 .trig_alloc_sz
= ALTR_TRIG_OCRAM_BYTE_SIZE
,
1178 .inject_fops
= &altr_edac_device_inject_fops
,
1181 static int __maybe_unused
1182 altr_check_ocram_deps_init(struct altr_edac_device_dev
*device
)
1184 void __iomem
*base
= device
->base
;
1187 ret
= altr_check_ecc_deps(device
);
1191 /* Verify OCRAM has been initialized */
1192 if (!ecc_test_bits(ALTR_A10_ECC_INITCOMPLETEA
,
1193 (base
+ ALTR_A10_ECC_INITSTAT_OFST
)))
1196 /* Enable IRQ on Single Bit Error */
1197 writel(ALTR_A10_ECC_SERRINTEN
, (base
+ ALTR_A10_ECC_ERRINTENS_OFST
));
1198 /* Ensure all writes complete */
1204 static const struct edac_device_prv_data a10_ocramecc_data
= {
1205 .setup
= altr_check_ocram_deps_init
,
1206 .ce_clear_mask
= ALTR_A10_ECC_SERRPENA
,
1207 .ue_clear_mask
= ALTR_A10_ECC_DERRPENA
,
1208 .irq_status_mask
= A10_SYSMGR_ECC_INTSTAT_OCRAM
,
1209 .ecc_enable_mask
= ALTR_A10_OCRAM_ECC_EN_CTL
,
1210 .ecc_en_ofst
= ALTR_A10_ECC_CTRL_OFST
,
1211 .ce_set_mask
= ALTR_A10_ECC_TSERRA
,
1212 .ue_set_mask
= ALTR_A10_ECC_TDERRA
,
1213 .set_err_ofst
= ALTR_A10_ECC_INTTEST_OFST
,
1214 .ecc_irq_handler
= altr_edac_a10_ecc_irq
,
1215 .inject_fops
= &altr_edac_a10_device_inject2_fops
,
1217 * OCRAM panic on uncorrectable error because sleep/resume
1218 * functions and FPGA contents are stored in OCRAM. Prefer
1219 * a kernel panic over executing/loading corrupted data.
1224 #endif /* CONFIG_EDAC_ALTERA_OCRAM */
1226 /********************* L2 Cache EDAC Device Functions ********************/
1228 #ifdef CONFIG_EDAC_ALTERA_L2C
1230 static void *l2_alloc_mem(size_t size
, void **other
)
1232 struct device
*dev
= *other
;
1233 void *ptemp
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
1238 /* Make sure everything is written out */
1242 * Clean all cache levels up to LoC (includes L2)
1243 * This ensures the corrupted data is written into
1244 * L2 cache for readback test (which causes ECC error).
1251 static void l2_free_mem(void *p
, size_t size
, void *other
)
1253 struct device
*dev
= other
;
1260 * altr_l2_check_deps()
1261 * Test for L2 cache ECC dependencies upon entry because
1262 * platform specific startup should have initialized the L2
1263 * memory and enabled the ECC.
1264 * Bail if ECC is not enabled.
1265 * Note that L2 Cache Enable is forced at build time.
1267 static int altr_l2_check_deps(struct altr_edac_device_dev
*device
)
1269 void __iomem
*base
= device
->base
;
1270 const struct edac_device_prv_data
*prv
= device
->data
;
1272 if ((readl(base
) & prv
->ecc_enable_mask
) ==
1273 prv
->ecc_enable_mask
)
1276 edac_printk(KERN_ERR
, EDAC_DEVICE
,
1277 "L2: No ECC present, or ECC disabled\n");
1281 static irqreturn_t
altr_edac_a10_l2_irq(int irq
, void *dev_id
)
1283 struct altr_edac_device_dev
*dci
= dev_id
;
1285 if (irq
== dci
->sb_irq
) {
1286 regmap_write(dci
->edac
->ecc_mgr_map
,
1287 A10_SYSGMR_MPU_CLEAR_L2_ECC_OFST
,
1288 A10_SYSGMR_MPU_CLEAR_L2_ECC_SB
);
1289 edac_device_handle_ce(dci
->edac_dev
, 0, 0, dci
->edac_dev_name
);
1292 } else if (irq
== dci
->db_irq
) {
1293 regmap_write(dci
->edac
->ecc_mgr_map
,
1294 A10_SYSGMR_MPU_CLEAR_L2_ECC_OFST
,
1295 A10_SYSGMR_MPU_CLEAR_L2_ECC_MB
);
1296 edac_device_handle_ue(dci
->edac_dev
, 0, 0, dci
->edac_dev_name
);
1297 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
1307 static const struct edac_device_prv_data l2ecc_data
= {
1308 .setup
= altr_l2_check_deps
,
1311 .alloc_mem
= l2_alloc_mem
,
1312 .free_mem
= l2_free_mem
,
1313 .ecc_enable_mask
= ALTR_L2_ECC_EN
,
1314 .ce_set_mask
= (ALTR_L2_ECC_EN
| ALTR_L2_ECC_INJS
),
1315 .ue_set_mask
= (ALTR_L2_ECC_EN
| ALTR_L2_ECC_INJD
),
1316 .set_err_ofst
= ALTR_L2_ECC_REG_OFFSET
,
1317 .trig_alloc_sz
= ALTR_TRIG_L2C_BYTE_SIZE
,
1318 .inject_fops
= &altr_edac_device_inject_fops
,
1321 static const struct edac_device_prv_data a10_l2ecc_data
= {
1322 .setup
= altr_l2_check_deps
,
1323 .ce_clear_mask
= ALTR_A10_L2_ECC_SERR_CLR
,
1324 .ue_clear_mask
= ALTR_A10_L2_ECC_MERR_CLR
,
1325 .irq_status_mask
= A10_SYSMGR_ECC_INTSTAT_L2
,
1326 .alloc_mem
= l2_alloc_mem
,
1327 .free_mem
= l2_free_mem
,
1328 .ecc_enable_mask
= ALTR_A10_L2_ECC_EN_CTL
,
1329 .ce_set_mask
= ALTR_A10_L2_ECC_CE_INJ_MASK
,
1330 .ue_set_mask
= ALTR_A10_L2_ECC_UE_INJ_MASK
,
1331 .set_err_ofst
= ALTR_A10_L2_ECC_INJ_OFST
,
1332 .ecc_irq_handler
= altr_edac_a10_l2_irq
,
1333 .trig_alloc_sz
= ALTR_TRIG_L2C_BYTE_SIZE
,
1334 .inject_fops
= &altr_edac_device_inject_fops
,
1337 #endif /* CONFIG_EDAC_ALTERA_L2C */
1339 /********************* Ethernet Device Functions ********************/
1341 #ifdef CONFIG_EDAC_ALTERA_ETHERNET
1343 static int __init
socfpga_init_ethernet_ecc(struct altr_edac_device_dev
*dev
)
1347 ret
= altr_init_a10_ecc_device_type("altr,socfpga-eth-mac-ecc");
1351 return altr_check_ecc_deps(dev
);
1354 static const struct edac_device_prv_data a10_enetecc_data
= {
1355 .setup
= socfpga_init_ethernet_ecc
,
1356 .ce_clear_mask
= ALTR_A10_ECC_SERRPENA
,
1357 .ue_clear_mask
= ALTR_A10_ECC_DERRPENA
,
1358 .ecc_enable_mask
= ALTR_A10_COMMON_ECC_EN_CTL
,
1359 .ecc_en_ofst
= ALTR_A10_ECC_CTRL_OFST
,
1360 .ce_set_mask
= ALTR_A10_ECC_TSERRA
,
1361 .ue_set_mask
= ALTR_A10_ECC_TDERRA
,
1362 .set_err_ofst
= ALTR_A10_ECC_INTTEST_OFST
,
1363 .ecc_irq_handler
= altr_edac_a10_ecc_irq
,
1364 .inject_fops
= &altr_edac_a10_device_inject2_fops
,
1367 #endif /* CONFIG_EDAC_ALTERA_ETHERNET */
1369 /********************** NAND Device Functions **********************/
1371 #ifdef CONFIG_EDAC_ALTERA_NAND
1373 static int __init
socfpga_init_nand_ecc(struct altr_edac_device_dev
*device
)
1377 ret
= altr_init_a10_ecc_device_type("altr,socfpga-nand-ecc");
1381 return altr_check_ecc_deps(device
);
1384 static const struct edac_device_prv_data a10_nandecc_data
= {
1385 .setup
= socfpga_init_nand_ecc
,
1386 .ce_clear_mask
= ALTR_A10_ECC_SERRPENA
,
1387 .ue_clear_mask
= ALTR_A10_ECC_DERRPENA
,
1388 .ecc_enable_mask
= ALTR_A10_COMMON_ECC_EN_CTL
,
1389 .ecc_en_ofst
= ALTR_A10_ECC_CTRL_OFST
,
1390 .ce_set_mask
= ALTR_A10_ECC_TSERRA
,
1391 .ue_set_mask
= ALTR_A10_ECC_TDERRA
,
1392 .set_err_ofst
= ALTR_A10_ECC_INTTEST_OFST
,
1393 .ecc_irq_handler
= altr_edac_a10_ecc_irq
,
1394 .inject_fops
= &altr_edac_a10_device_inject_fops
,
1397 #endif /* CONFIG_EDAC_ALTERA_NAND */
1399 /********************** DMA Device Functions **********************/
1401 #ifdef CONFIG_EDAC_ALTERA_DMA
1403 static int __init
socfpga_init_dma_ecc(struct altr_edac_device_dev
*device
)
1407 ret
= altr_init_a10_ecc_device_type("altr,socfpga-dma-ecc");
1411 return altr_check_ecc_deps(device
);
1414 static const struct edac_device_prv_data a10_dmaecc_data
= {
1415 .setup
= socfpga_init_dma_ecc
,
1416 .ce_clear_mask
= ALTR_A10_ECC_SERRPENA
,
1417 .ue_clear_mask
= ALTR_A10_ECC_DERRPENA
,
1418 .ecc_enable_mask
= ALTR_A10_COMMON_ECC_EN_CTL
,
1419 .ecc_en_ofst
= ALTR_A10_ECC_CTRL_OFST
,
1420 .ce_set_mask
= ALTR_A10_ECC_TSERRA
,
1421 .ue_set_mask
= ALTR_A10_ECC_TDERRA
,
1422 .set_err_ofst
= ALTR_A10_ECC_INTTEST_OFST
,
1423 .ecc_irq_handler
= altr_edac_a10_ecc_irq
,
1424 .inject_fops
= &altr_edac_a10_device_inject_fops
,
1427 #endif /* CONFIG_EDAC_ALTERA_DMA */
1429 /********************** USB Device Functions **********************/
1431 #ifdef CONFIG_EDAC_ALTERA_USB
1433 static int __init
socfpga_init_usb_ecc(struct altr_edac_device_dev
*device
)
1437 ret
= altr_init_a10_ecc_device_type("altr,socfpga-usb-ecc");
1441 return altr_check_ecc_deps(device
);
1444 static const struct edac_device_prv_data a10_usbecc_data
= {
1445 .setup
= socfpga_init_usb_ecc
,
1446 .ce_clear_mask
= ALTR_A10_ECC_SERRPENA
,
1447 .ue_clear_mask
= ALTR_A10_ECC_DERRPENA
,
1448 .ecc_enable_mask
= ALTR_A10_COMMON_ECC_EN_CTL
,
1449 .ecc_en_ofst
= ALTR_A10_ECC_CTRL_OFST
,
1450 .ce_set_mask
= ALTR_A10_ECC_TSERRA
,
1451 .ue_set_mask
= ALTR_A10_ECC_TDERRA
,
1452 .set_err_ofst
= ALTR_A10_ECC_INTTEST_OFST
,
1453 .ecc_irq_handler
= altr_edac_a10_ecc_irq
,
1454 .inject_fops
= &altr_edac_a10_device_inject2_fops
,
1457 #endif /* CONFIG_EDAC_ALTERA_USB */
1459 /********************** QSPI Device Functions **********************/
1461 #ifdef CONFIG_EDAC_ALTERA_QSPI
1463 static int __init
socfpga_init_qspi_ecc(struct altr_edac_device_dev
*device
)
1467 ret
= altr_init_a10_ecc_device_type("altr,socfpga-qspi-ecc");
1471 return altr_check_ecc_deps(device
);
1474 static const struct edac_device_prv_data a10_qspiecc_data
= {
1475 .setup
= socfpga_init_qspi_ecc
,
1476 .ce_clear_mask
= ALTR_A10_ECC_SERRPENA
,
1477 .ue_clear_mask
= ALTR_A10_ECC_DERRPENA
,
1478 .ecc_enable_mask
= ALTR_A10_COMMON_ECC_EN_CTL
,
1479 .ecc_en_ofst
= ALTR_A10_ECC_CTRL_OFST
,
1480 .ce_set_mask
= ALTR_A10_ECC_TSERRA
,
1481 .ue_set_mask
= ALTR_A10_ECC_TDERRA
,
1482 .set_err_ofst
= ALTR_A10_ECC_INTTEST_OFST
,
1483 .ecc_irq_handler
= altr_edac_a10_ecc_irq
,
1484 .inject_fops
= &altr_edac_a10_device_inject_fops
,
1487 #endif /* CONFIG_EDAC_ALTERA_QSPI */
1489 /********************* SDMMC Device Functions **********************/
1491 #ifdef CONFIG_EDAC_ALTERA_SDMMC
1493 static const struct edac_device_prv_data a10_sdmmceccb_data
;
1494 static int altr_portb_setup(struct altr_edac_device_dev
*device
)
1496 struct edac_device_ctl_info
*dci
;
1497 struct altr_edac_device_dev
*altdev
;
1498 char *ecc_name
= "sdmmcb-ecc";
1500 struct device_node
*np
;
1501 const struct edac_device_prv_data
*prv
= &a10_sdmmceccb_data
;
1503 rc
= altr_check_ecc_deps(device
);
1507 np
= of_find_compatible_node(NULL
, NULL
, "altr,socfpga-sdmmc-ecc");
1509 edac_printk(KERN_WARNING
, EDAC_DEVICE
, "SDMMC node not found\n");
1513 /* Create the PortB EDAC device */
1514 edac_idx
= edac_device_alloc_index();
1515 dci
= edac_device_alloc_ctl_info(sizeof(*altdev
), ecc_name
, 1,
1516 ecc_name
, 1, 0, edac_idx
);
1518 edac_printk(KERN_ERR
, EDAC_DEVICE
,
1519 "%s: Unable to allocate PortB EDAC device\n",
1524 /* Initialize the PortB EDAC device structure from PortA structure */
1525 altdev
= dci
->pvt_info
;
1528 if (!devres_open_group(&altdev
->ddev
, altr_portb_setup
, GFP_KERNEL
))
1531 /* Update PortB specific values */
1532 altdev
->edac_dev_name
= ecc_name
;
1533 altdev
->edac_idx
= edac_idx
;
1534 altdev
->edac_dev
= dci
;
1536 dci
->dev
= &altdev
->ddev
;
1537 dci
->ctl_name
= "Altera ECC Manager";
1538 dci
->mod_name
= ecc_name
;
1539 dci
->dev_name
= ecc_name
;
1542 * Update the PortB IRQs - A10 has 4, S10 has 2, Index accordingly
1544 * FIXME: Instead of ifdefs with different architectures the driver
1545 * should properly use compatibles.
1548 altdev
->sb_irq
= irq_of_parse_and_map(np
, 1);
1550 altdev
->sb_irq
= irq_of_parse_and_map(np
, 2);
1552 if (!altdev
->sb_irq
) {
1553 edac_printk(KERN_ERR
, EDAC_DEVICE
, "Error PortB SBIRQ alloc\n");
1555 goto err_release_group_1
;
1557 rc
= devm_request_irq(&altdev
->ddev
, altdev
->sb_irq
,
1558 prv
->ecc_irq_handler
,
1559 IRQF_ONESHOT
| IRQF_TRIGGER_HIGH
,
1562 edac_printk(KERN_ERR
, EDAC_DEVICE
, "PortB SBERR IRQ error\n");
1563 goto err_release_group_1
;
1567 /* Use IRQ to determine SError origin instead of assigning IRQ */
1568 rc
= of_property_read_u32_index(np
, "interrupts", 1, &altdev
->db_irq
);
1570 edac_printk(KERN_ERR
, EDAC_DEVICE
,
1571 "Error PortB DBIRQ alloc\n");
1572 goto err_release_group_1
;
1575 altdev
->db_irq
= irq_of_parse_and_map(np
, 3);
1576 if (!altdev
->db_irq
) {
1577 edac_printk(KERN_ERR
, EDAC_DEVICE
, "Error PortB DBIRQ alloc\n");
1579 goto err_release_group_1
;
1581 rc
= devm_request_irq(&altdev
->ddev
, altdev
->db_irq
,
1582 prv
->ecc_irq_handler
,
1583 IRQF_ONESHOT
| IRQF_TRIGGER_HIGH
,
1586 edac_printk(KERN_ERR
, EDAC_DEVICE
, "PortB DBERR IRQ error\n");
1587 goto err_release_group_1
;
1591 rc
= edac_device_add_device(dci
);
1593 edac_printk(KERN_ERR
, EDAC_DEVICE
,
1594 "edac_device_add_device portB failed\n");
1596 goto err_release_group_1
;
1598 altr_create_edacdev_dbgfs(dci
, prv
);
1600 list_add(&altdev
->next
, &altdev
->edac
->a10_ecc_devices
);
1602 devres_remove_group(&altdev
->ddev
, altr_portb_setup
);
1606 err_release_group_1
:
1607 edac_device_free_ctl_info(dci
);
1608 devres_release_group(&altdev
->ddev
, altr_portb_setup
);
1609 edac_printk(KERN_ERR
, EDAC_DEVICE
,
1610 "%s:Error setting up EDAC device: %d\n", ecc_name
, rc
);
1614 static int __init
socfpga_init_sdmmc_ecc(struct altr_edac_device_dev
*device
)
1617 struct device_node
*child
;
1619 child
= of_find_compatible_node(NULL
, NULL
, "altr,socfpga-sdmmc-ecc");
1623 if (!of_device_is_available(child
))
1626 if (validate_parent_available(child
))
1630 rc
= altr_init_a10_ecc_block(child
, ALTR_A10_SDMMC_IRQ_MASK
,
1631 a10_sdmmceccb_data
.ecc_enable_mask
, 1);
1636 return altr_portb_setup(device
);
1643 static irqreturn_t
altr_edac_a10_ecc_irq_portb(int irq
, void *dev_id
)
1645 struct altr_edac_device_dev
*ad
= dev_id
;
1646 void __iomem
*base
= ad
->base
;
1647 const struct edac_device_prv_data
*priv
= ad
->data
;
1649 if (irq
== ad
->sb_irq
) {
1650 writel(priv
->ce_clear_mask
,
1651 base
+ ALTR_A10_ECC_INTSTAT_OFST
);
1652 edac_device_handle_ce(ad
->edac_dev
, 0, 0, ad
->edac_dev_name
);
1654 } else if (irq
== ad
->db_irq
) {
1655 writel(priv
->ue_clear_mask
,
1656 base
+ ALTR_A10_ECC_INTSTAT_OFST
);
1657 edac_device_handle_ue(ad
->edac_dev
, 0, 0, ad
->edac_dev_name
);
1661 WARN_ONCE(1, "Unhandled IRQ%d on Port B.", irq
);
1666 static const struct edac_device_prv_data a10_sdmmcecca_data
= {
1667 .setup
= socfpga_init_sdmmc_ecc
,
1668 .ce_clear_mask
= ALTR_A10_ECC_SERRPENA
,
1669 .ue_clear_mask
= ALTR_A10_ECC_DERRPENA
,
1670 .ecc_enable_mask
= ALTR_A10_COMMON_ECC_EN_CTL
,
1671 .ecc_en_ofst
= ALTR_A10_ECC_CTRL_OFST
,
1672 .ce_set_mask
= ALTR_A10_ECC_SERRPENA
,
1673 .ue_set_mask
= ALTR_A10_ECC_DERRPENA
,
1674 .set_err_ofst
= ALTR_A10_ECC_INTTEST_OFST
,
1675 .ecc_irq_handler
= altr_edac_a10_ecc_irq
,
1676 .inject_fops
= &altr_edac_a10_device_inject_fops
,
1679 static const struct edac_device_prv_data a10_sdmmceccb_data
= {
1680 .setup
= socfpga_init_sdmmc_ecc
,
1681 .ce_clear_mask
= ALTR_A10_ECC_SERRPENB
,
1682 .ue_clear_mask
= ALTR_A10_ECC_DERRPENB
,
1683 .ecc_enable_mask
= ALTR_A10_COMMON_ECC_EN_CTL
,
1684 .ecc_en_ofst
= ALTR_A10_ECC_CTRL_OFST
,
1685 .ce_set_mask
= ALTR_A10_ECC_TSERRB
,
1686 .ue_set_mask
= ALTR_A10_ECC_TDERRB
,
1687 .set_err_ofst
= ALTR_A10_ECC_INTTEST_OFST
,
1688 .ecc_irq_handler
= altr_edac_a10_ecc_irq_portb
,
1689 .inject_fops
= &altr_edac_a10_device_inject_fops
,
1692 #endif /* CONFIG_EDAC_ALTERA_SDMMC */
1694 /********************* Arria10 EDAC Device Functions *************************/
1695 static const struct of_device_id altr_edac_a10_device_of_match
[] = {
1696 #ifdef CONFIG_EDAC_ALTERA_L2C
1697 { .compatible
= "altr,socfpga-a10-l2-ecc", .data
= &a10_l2ecc_data
},
1699 #ifdef CONFIG_EDAC_ALTERA_OCRAM
1700 { .compatible
= "altr,socfpga-a10-ocram-ecc",
1701 .data
= &a10_ocramecc_data
},
1703 #ifdef CONFIG_EDAC_ALTERA_ETHERNET
1704 { .compatible
= "altr,socfpga-eth-mac-ecc",
1705 .data
= &a10_enetecc_data
},
1707 #ifdef CONFIG_EDAC_ALTERA_NAND
1708 { .compatible
= "altr,socfpga-nand-ecc", .data
= &a10_nandecc_data
},
1710 #ifdef CONFIG_EDAC_ALTERA_DMA
1711 { .compatible
= "altr,socfpga-dma-ecc", .data
= &a10_dmaecc_data
},
1713 #ifdef CONFIG_EDAC_ALTERA_USB
1714 { .compatible
= "altr,socfpga-usb-ecc", .data
= &a10_usbecc_data
},
1716 #ifdef CONFIG_EDAC_ALTERA_QSPI
1717 { .compatible
= "altr,socfpga-qspi-ecc", .data
= &a10_qspiecc_data
},
1719 #ifdef CONFIG_EDAC_ALTERA_SDMMC
1720 { .compatible
= "altr,socfpga-sdmmc-ecc", .data
= &a10_sdmmcecca_data
},
1722 #ifdef CONFIG_EDAC_ALTERA_SDRAM
1723 { .compatible
= "altr,sdram-edac-s10", .data
= &s10_sdramecc_data
},
1727 MODULE_DEVICE_TABLE(of
, altr_edac_a10_device_of_match
);
1730 * The Arria10 EDAC Device Functions differ from the Cyclone5/Arria5
1731 * because 2 IRQs are shared among the all ECC peripherals. The ECC
1732 * manager manages the IRQs and the children.
1733 * Based on xgene_edac.c peripheral code.
1736 static ssize_t __maybe_unused
1737 altr_edac_a10_device_trig(struct file
*file
, const char __user
*user_buf
,
1738 size_t count
, loff_t
*ppos
)
1740 struct edac_device_ctl_info
*edac_dci
= file
->private_data
;
1741 struct altr_edac_device_dev
*drvdata
= edac_dci
->pvt_info
;
1742 const struct edac_device_prv_data
*priv
= drvdata
->data
;
1743 void __iomem
*set_addr
= (drvdata
->base
+ priv
->set_err_ofst
);
1744 unsigned long flags
;
1747 if (!user_buf
|| get_user(trig_type
, user_buf
))
1750 local_irq_save(flags
);
1751 if (trig_type
== ALTR_UE_TRIGGER_CHAR
)
1752 writel(priv
->ue_set_mask
, set_addr
);
1754 writel(priv
->ce_set_mask
, set_addr
);
1756 /* Ensure the interrupt test bits are set */
1758 local_irq_restore(flags
);
1764 * The Stratix10 EDAC Error Injection Functions differ from Arria10
1765 * slightly. A few Arria10 peripherals can use this injection function.
1766 * Inject the error into the memory and then readback to trigger the IRQ.
1768 static ssize_t __maybe_unused
1769 altr_edac_a10_device_trig2(struct file
*file
, const char __user
*user_buf
,
1770 size_t count
, loff_t
*ppos
)
1772 struct edac_device_ctl_info
*edac_dci
= file
->private_data
;
1773 struct altr_edac_device_dev
*drvdata
= edac_dci
->pvt_info
;
1774 const struct edac_device_prv_data
*priv
= drvdata
->data
;
1775 void __iomem
*set_addr
= (drvdata
->base
+ priv
->set_err_ofst
);
1776 unsigned long flags
;
1779 if (!user_buf
|| get_user(trig_type
, user_buf
))
1782 local_irq_save(flags
);
1783 if (trig_type
== ALTR_UE_TRIGGER_CHAR
) {
1784 writel(priv
->ue_set_mask
, set_addr
);
1786 /* Setup read/write of 4 bytes */
1787 writel(ECC_WORD_WRITE
, drvdata
->base
+ ECC_BLK_DBYTECTRL_OFST
);
1788 /* Setup Address to 0 */
1789 writel(0, drvdata
->base
+ ECC_BLK_ADDRESS_OFST
);
1790 /* Setup accctrl to read & ecc & data override */
1791 writel(ECC_READ_EDOVR
, drvdata
->base
+ ECC_BLK_ACCCTRL_OFST
);
1793 writel(ECC_XACT_KICK
, drvdata
->base
+ ECC_BLK_STARTACC_OFST
);
1794 /* Setup write for single bit change */
1795 writel(readl(drvdata
->base
+ ECC_BLK_RDATA0_OFST
) ^ 0x1,
1796 drvdata
->base
+ ECC_BLK_WDATA0_OFST
);
1797 writel(readl(drvdata
->base
+ ECC_BLK_RDATA1_OFST
),
1798 drvdata
->base
+ ECC_BLK_WDATA1_OFST
);
1799 writel(readl(drvdata
->base
+ ECC_BLK_RDATA2_OFST
),
1800 drvdata
->base
+ ECC_BLK_WDATA2_OFST
);
1801 writel(readl(drvdata
->base
+ ECC_BLK_RDATA3_OFST
),
1802 drvdata
->base
+ ECC_BLK_WDATA3_OFST
);
1804 /* Copy Read ECC to Write ECC */
1805 writel(readl(drvdata
->base
+ ECC_BLK_RECC0_OFST
),
1806 drvdata
->base
+ ECC_BLK_WECC0_OFST
);
1807 writel(readl(drvdata
->base
+ ECC_BLK_RECC1_OFST
),
1808 drvdata
->base
+ ECC_BLK_WECC1_OFST
);
1809 /* Setup accctrl to write & ecc override & data override */
1810 writel(ECC_WRITE_EDOVR
, drvdata
->base
+ ECC_BLK_ACCCTRL_OFST
);
1812 writel(ECC_XACT_KICK
, drvdata
->base
+ ECC_BLK_STARTACC_OFST
);
1813 /* Setup accctrl to read & ecc overwrite & data overwrite */
1814 writel(ECC_READ_EDOVR
, drvdata
->base
+ ECC_BLK_ACCCTRL_OFST
);
1816 writel(ECC_XACT_KICK
, drvdata
->base
+ ECC_BLK_STARTACC_OFST
);
1819 /* Ensure the interrupt test bits are set */
1821 local_irq_restore(flags
);
1826 static void altr_edac_a10_irq_handler(struct irq_desc
*desc
)
1828 int dberr
, bit
, sm_offset
, irq_status
;
1829 struct altr_arria10_edac
*edac
= irq_desc_get_handler_data(desc
);
1830 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
1831 int irq
= irq_desc_get_irq(desc
);
1834 dberr
= (irq
== edac
->db_irq
) ? 1 : 0;
1835 sm_offset
= dberr
? A10_SYSMGR_ECC_INTSTAT_DERR_OFST
:
1836 A10_SYSMGR_ECC_INTSTAT_SERR_OFST
;
1838 chained_irq_enter(chip
, desc
);
1840 regmap_read(edac
->ecc_mgr_map
, sm_offset
, &irq_status
);
1843 for_each_set_bit(bit
, &bits
, 32)
1844 generic_handle_domain_irq(edac
->domain
, dberr
* 32 + bit
);
1846 chained_irq_exit(chip
, desc
);
1849 static int validate_parent_available(struct device_node
*np
)
1851 struct device_node
*parent
;
1854 /* SDRAM must be present for Linux (implied parent) */
1855 if (of_device_is_compatible(np
, "altr,sdram-edac-s10"))
1858 /* Ensure parent device is enabled if parent node exists */
1859 parent
= of_parse_phandle(np
, "altr,ecc-parent", 0);
1860 if (parent
&& !of_device_is_available(parent
))
1863 of_node_put(parent
);
1867 static int get_s10_sdram_edac_resource(struct device_node
*np
,
1868 struct resource
*res
)
1870 struct device_node
*parent
;
1873 parent
= of_parse_phandle(np
, "altr,sdr-syscon", 0);
1877 ret
= of_address_to_resource(parent
, 0, res
);
1878 of_node_put(parent
);
1883 static int altr_edac_a10_device_add(struct altr_arria10_edac
*edac
,
1884 struct device_node
*np
)
1886 struct edac_device_ctl_info
*dci
;
1887 struct altr_edac_device_dev
*altdev
;
1888 char *ecc_name
= (char *)np
->name
;
1889 struct resource res
;
1892 const struct edac_device_prv_data
*prv
;
1893 /* Get matching node and check for valid result */
1894 const struct of_device_id
*pdev_id
=
1895 of_match_node(altr_edac_a10_device_of_match
, np
);
1896 if (IS_ERR_OR_NULL(pdev_id
))
1899 /* Get driver specific data for this EDAC device */
1900 prv
= pdev_id
->data
;
1901 if (IS_ERR_OR_NULL(prv
))
1904 if (validate_parent_available(np
))
1907 if (!devres_open_group(edac
->dev
, altr_edac_a10_device_add
, GFP_KERNEL
))
1910 if (of_device_is_compatible(np
, "altr,sdram-edac-s10"))
1911 rc
= get_s10_sdram_edac_resource(np
, &res
);
1913 rc
= of_address_to_resource(np
, 0, &res
);
1916 edac_printk(KERN_ERR
, EDAC_DEVICE
,
1917 "%s: no resource address\n", ecc_name
);
1918 goto err_release_group
;
1921 edac_idx
= edac_device_alloc_index();
1922 dci
= edac_device_alloc_ctl_info(sizeof(*altdev
), ecc_name
,
1923 1, ecc_name
, 1, 0, edac_idx
);
1926 edac_printk(KERN_ERR
, EDAC_DEVICE
,
1927 "%s: Unable to allocate EDAC device\n", ecc_name
);
1929 goto err_release_group
;
1932 altdev
= dci
->pvt_info
;
1933 dci
->dev
= edac
->dev
;
1934 altdev
->edac_dev_name
= ecc_name
;
1935 altdev
->edac_idx
= edac_idx
;
1936 altdev
->edac
= edac
;
1937 altdev
->edac_dev
= dci
;
1939 altdev
->ddev
= *edac
->dev
;
1940 dci
->dev
= &altdev
->ddev
;
1941 dci
->ctl_name
= "Altera ECC Manager";
1942 dci
->mod_name
= ecc_name
;
1943 dci
->dev_name
= ecc_name
;
1945 altdev
->base
= devm_ioremap_resource(edac
->dev
, &res
);
1946 if (IS_ERR(altdev
->base
)) {
1947 rc
= PTR_ERR(altdev
->base
);
1948 goto err_release_group1
;
1951 /* Check specific dependencies for the module */
1952 if (altdev
->data
->setup
) {
1953 rc
= altdev
->data
->setup(altdev
);
1955 goto err_release_group1
;
1958 altdev
->sb_irq
= irq_of_parse_and_map(np
, 0);
1959 if (!altdev
->sb_irq
) {
1960 edac_printk(KERN_ERR
, EDAC_DEVICE
, "Error allocating SBIRQ\n");
1962 goto err_release_group1
;
1964 rc
= devm_request_irq(edac
->dev
, altdev
->sb_irq
, prv
->ecc_irq_handler
,
1965 IRQF_ONESHOT
| IRQF_TRIGGER_HIGH
,
1968 edac_printk(KERN_ERR
, EDAC_DEVICE
, "No SBERR IRQ resource\n");
1969 goto err_release_group1
;
1973 /* Use IRQ to determine SError origin instead of assigning IRQ */
1974 rc
= of_property_read_u32_index(np
, "interrupts", 0, &altdev
->db_irq
);
1976 edac_printk(KERN_ERR
, EDAC_DEVICE
,
1977 "Unable to parse DB IRQ index\n");
1978 goto err_release_group1
;
1981 altdev
->db_irq
= irq_of_parse_and_map(np
, 1);
1982 if (!altdev
->db_irq
) {
1983 edac_printk(KERN_ERR
, EDAC_DEVICE
, "Error allocating DBIRQ\n");
1985 goto err_release_group1
;
1987 rc
= devm_request_irq(edac
->dev
, altdev
->db_irq
, prv
->ecc_irq_handler
,
1988 IRQF_ONESHOT
| IRQF_TRIGGER_HIGH
,
1991 edac_printk(KERN_ERR
, EDAC_DEVICE
, "No DBERR IRQ resource\n");
1992 goto err_release_group1
;
1996 rc
= edac_device_add_device(dci
);
1998 dev_err(edac
->dev
, "edac_device_add_device failed\n");
2000 goto err_release_group1
;
2003 altr_create_edacdev_dbgfs(dci
, prv
);
2005 list_add(&altdev
->next
, &edac
->a10_ecc_devices
);
2007 devres_remove_group(edac
->dev
, altr_edac_a10_device_add
);
2012 edac_device_free_ctl_info(dci
);
2014 devres_release_group(edac
->dev
, NULL
);
2015 edac_printk(KERN_ERR
, EDAC_DEVICE
,
2016 "%s:Error setting up EDAC device: %d\n", ecc_name
, rc
);
2021 static void a10_eccmgr_irq_mask(struct irq_data
*d
)
2023 struct altr_arria10_edac
*edac
= irq_data_get_irq_chip_data(d
);
2025 regmap_write(edac
->ecc_mgr_map
, A10_SYSMGR_ECC_INTMASK_SET_OFST
,
2029 static void a10_eccmgr_irq_unmask(struct irq_data
*d
)
2031 struct altr_arria10_edac
*edac
= irq_data_get_irq_chip_data(d
);
2033 regmap_write(edac
->ecc_mgr_map
, A10_SYSMGR_ECC_INTMASK_CLR_OFST
,
2037 static int a10_eccmgr_irqdomain_map(struct irq_domain
*d
, unsigned int irq
,
2038 irq_hw_number_t hwirq
)
2040 struct altr_arria10_edac
*edac
= d
->host_data
;
2042 irq_set_chip_and_handler(irq
, &edac
->irq_chip
, handle_simple_irq
);
2043 irq_set_chip_data(irq
, edac
);
2044 irq_set_noprobe(irq
);
2049 static const struct irq_domain_ops a10_eccmgr_ic_ops
= {
2050 .map
= a10_eccmgr_irqdomain_map
,
2051 .xlate
= irq_domain_xlate_twocell
,
2054 /************** Stratix 10 EDAC Double Bit Error Handler ************/
2055 #define to_a10edac(p, m) container_of(p, struct altr_arria10_edac, m)
2058 /* panic routine issues reboot on non-zero panic_timeout */
2059 extern int panic_timeout
;
2062 * The double bit error is handled through SError which is fatal. This is
2063 * called as a panic notifier to printout ECC error info as part of the panic.
2065 static int s10_edac_dberr_handler(struct notifier_block
*this,
2066 unsigned long event
, void *ptr
)
2068 struct altr_arria10_edac
*edac
= to_a10edac(this, panic_notifier
);
2069 int err_addr
, dberror
;
2071 regmap_read(edac
->ecc_mgr_map
, S10_SYSMGR_ECC_INTSTAT_DERR_OFST
,
2073 regmap_write(edac
->ecc_mgr_map
, S10_SYSMGR_UE_VAL_OFST
, dberror
);
2074 if (dberror
& S10_DBE_IRQ_MASK
) {
2075 struct list_head
*position
;
2076 struct altr_edac_device_dev
*ed
;
2077 struct arm_smccc_res result
;
2079 /* Find the matching DBE in the list of devices */
2080 list_for_each(position
, &edac
->a10_ecc_devices
) {
2081 ed
= list_entry(position
, struct altr_edac_device_dev
,
2083 if (!(BIT(ed
->db_irq
) & dberror
))
2086 writel(ALTR_A10_ECC_DERRPENA
,
2087 ed
->base
+ ALTR_A10_ECC_INTSTAT_OFST
);
2088 err_addr
= readl(ed
->base
+ ALTR_S10_DERR_ADDRA_OFST
);
2089 regmap_write(edac
->ecc_mgr_map
,
2090 S10_SYSMGR_UE_ADDR_OFST
, err_addr
);
2091 edac_printk(KERN_ERR
, EDAC_DEVICE
,
2092 "EDAC: [Fatal DBE on %s @ 0x%08X]\n",
2093 ed
->edac_dev_name
, err_addr
);
2096 /* Notify the System through SMC. Reboot delay = 1 second */
2098 arm_smccc_smc(INTEL_SIP_SMC_ECC_DBE
, dberror
, 0, 0, 0, 0,
2106 /****************** Arria 10 EDAC Probe Function *********************/
2107 static int altr_edac_a10_probe(struct platform_device
*pdev
)
2109 struct altr_arria10_edac
*edac
;
2110 struct device_node
*child
;
2112 edac
= devm_kzalloc(&pdev
->dev
, sizeof(*edac
), GFP_KERNEL
);
2116 edac
->dev
= &pdev
->dev
;
2117 platform_set_drvdata(pdev
, edac
);
2118 INIT_LIST_HEAD(&edac
->a10_ecc_devices
);
2121 altr_sysmgr_regmap_lookup_by_phandle(pdev
->dev
.of_node
,
2122 "altr,sysmgr-syscon");
2124 if (IS_ERR(edac
->ecc_mgr_map
)) {
2125 edac_printk(KERN_ERR
, EDAC_DEVICE
,
2126 "Unable to get syscon altr,sysmgr-syscon\n");
2127 return PTR_ERR(edac
->ecc_mgr_map
);
2130 edac
->irq_chip
.name
= pdev
->dev
.of_node
->name
;
2131 edac
->irq_chip
.irq_mask
= a10_eccmgr_irq_mask
;
2132 edac
->irq_chip
.irq_unmask
= a10_eccmgr_irq_unmask
;
2133 edac
->domain
= irq_domain_add_linear(pdev
->dev
.of_node
, 64,
2134 &a10_eccmgr_ic_ops
, edac
);
2135 if (!edac
->domain
) {
2136 dev_err(&pdev
->dev
, "Error adding IRQ domain\n");
2140 edac
->sb_irq
= platform_get_irq(pdev
, 0);
2141 if (edac
->sb_irq
< 0)
2142 return edac
->sb_irq
;
2144 irq_set_chained_handler_and_data(edac
->sb_irq
,
2145 altr_edac_a10_irq_handler
,
2150 int dberror
, err_addr
;
2152 edac
->panic_notifier
.notifier_call
= s10_edac_dberr_handler
;
2153 atomic_notifier_chain_register(&panic_notifier_list
,
2154 &edac
->panic_notifier
);
2156 /* Printout a message if uncorrectable error previously. */
2157 regmap_read(edac
->ecc_mgr_map
, S10_SYSMGR_UE_VAL_OFST
,
2160 regmap_read(edac
->ecc_mgr_map
, S10_SYSMGR_UE_ADDR_OFST
,
2162 edac_printk(KERN_ERR
, EDAC_DEVICE
,
2163 "Previous Boot UE detected[0x%X] @ 0x%X\n",
2165 /* Reset the sticky registers */
2166 regmap_write(edac
->ecc_mgr_map
,
2167 S10_SYSMGR_UE_VAL_OFST
, 0);
2168 regmap_write(edac
->ecc_mgr_map
,
2169 S10_SYSMGR_UE_ADDR_OFST
, 0);
2173 edac
->db_irq
= platform_get_irq(pdev
, 1);
2174 if (edac
->db_irq
< 0)
2175 return edac
->db_irq
;
2177 irq_set_chained_handler_and_data(edac
->db_irq
,
2178 altr_edac_a10_irq_handler
, edac
);
2181 for_each_child_of_node(pdev
->dev
.of_node
, child
) {
2182 if (!of_device_is_available(child
))
2185 if (of_match_node(altr_edac_a10_device_of_match
, child
))
2186 altr_edac_a10_device_add(edac
, child
);
2188 #ifdef CONFIG_EDAC_ALTERA_SDRAM
2189 else if (of_device_is_compatible(child
, "altr,sdram-edac-a10"))
2190 of_platform_populate(pdev
->dev
.of_node
,
2191 altr_sdram_ctrl_of_match
,
2199 static const struct of_device_id altr_edac_a10_of_match
[] = {
2200 { .compatible
= "altr,socfpga-a10-ecc-manager" },
2201 { .compatible
= "altr,socfpga-s10-ecc-manager" },
2204 MODULE_DEVICE_TABLE(of
, altr_edac_a10_of_match
);
2206 static struct platform_driver altr_edac_a10_driver
= {
2207 .probe
= altr_edac_a10_probe
,
2209 .name
= "socfpga_a10_ecc_manager",
2210 .of_match_table
= altr_edac_a10_of_match
,
2213 module_platform_driver(altr_edac_a10_driver
);
2215 MODULE_AUTHOR("Thor Thayer");
2216 MODULE_DESCRIPTION("EDAC Driver for Altera Memories");