2 * Copyright Altera Corporation (C) 2014-2016. All rights reserved.
3 * Copyright 2011-2012 Calxeda, Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
17 * Adapted from the highbank_mc_edac driver.
20 #include <asm/cacheflush.h>
21 #include <linux/ctype.h>
22 #include <linux/edac.h>
23 #include <linux/genalloc.h>
24 #include <linux/interrupt.h>
25 #include <linux/kernel.h>
26 #include <linux/mfd/syscon.h>
27 #include <linux/of_address.h>
28 #include <linux/of_platform.h>
29 #include <linux/platform_device.h>
30 #include <linux/regmap.h>
31 #include <linux/types.h>
32 #include <linux/uaccess.h>
34 #include "altera_edac.h"
35 #include "edac_core.h"
36 #include "edac_module.h"
38 #define EDAC_MOD_STR "altera_edac"
39 #define EDAC_VERSION "1"
40 #define EDAC_DEVICE "Altera"
42 static const struct altr_sdram_prv_data c5_data
= {
43 .ecc_ctrl_offset
= CV_CTLCFG_OFST
,
44 .ecc_ctl_en_mask
= CV_CTLCFG_ECC_AUTO_EN
,
45 .ecc_stat_offset
= CV_DRAMSTS_OFST
,
46 .ecc_stat_ce_mask
= CV_DRAMSTS_SBEERR
,
47 .ecc_stat_ue_mask
= CV_DRAMSTS_DBEERR
,
48 .ecc_saddr_offset
= CV_ERRADDR_OFST
,
49 .ecc_daddr_offset
= CV_ERRADDR_OFST
,
50 .ecc_cecnt_offset
= CV_SBECOUNT_OFST
,
51 .ecc_uecnt_offset
= CV_DBECOUNT_OFST
,
52 .ecc_irq_en_offset
= CV_DRAMINTR_OFST
,
53 .ecc_irq_en_mask
= CV_DRAMINTR_INTREN
,
54 .ecc_irq_clr_offset
= CV_DRAMINTR_OFST
,
55 .ecc_irq_clr_mask
= (CV_DRAMINTR_INTRCLR
| CV_DRAMINTR_INTREN
),
56 .ecc_cnt_rst_offset
= CV_DRAMINTR_OFST
,
57 .ecc_cnt_rst_mask
= CV_DRAMINTR_INTRCLR
,
58 .ce_ue_trgr_offset
= CV_CTLCFG_OFST
,
59 .ce_set_mask
= CV_CTLCFG_GEN_SB_ERR
,
60 .ue_set_mask
= CV_CTLCFG_GEN_DB_ERR
,
63 static const struct altr_sdram_prv_data a10_data
= {
64 .ecc_ctrl_offset
= A10_ECCCTRL1_OFST
,
65 .ecc_ctl_en_mask
= A10_ECCCTRL1_ECC_EN
,
66 .ecc_stat_offset
= A10_INTSTAT_OFST
,
67 .ecc_stat_ce_mask
= A10_INTSTAT_SBEERR
,
68 .ecc_stat_ue_mask
= A10_INTSTAT_DBEERR
,
69 .ecc_saddr_offset
= A10_SERRADDR_OFST
,
70 .ecc_daddr_offset
= A10_DERRADDR_OFST
,
71 .ecc_irq_en_offset
= A10_ERRINTEN_OFST
,
72 .ecc_irq_en_mask
= A10_ECC_IRQ_EN_MASK
,
73 .ecc_irq_clr_offset
= A10_INTSTAT_OFST
,
74 .ecc_irq_clr_mask
= (A10_INTSTAT_SBEERR
| A10_INTSTAT_DBEERR
),
75 .ecc_cnt_rst_offset
= A10_ECCCTRL1_OFST
,
76 .ecc_cnt_rst_mask
= A10_ECC_CNT_RESET_MASK
,
77 .ce_ue_trgr_offset
= A10_DIAGINTTEST_OFST
,
78 .ce_set_mask
= A10_DIAGINT_TSERRA_MASK
,
79 .ue_set_mask
= A10_DIAGINT_TDERRA_MASK
,
82 /*********************** EDAC Memory Controller Functions ****************/
84 /* The SDRAM controller uses the EDAC Memory Controller framework. */
86 static irqreturn_t
altr_sdram_mc_err_handler(int irq
, void *dev_id
)
88 struct mem_ctl_info
*mci
= dev_id
;
89 struct altr_sdram_mc_data
*drvdata
= mci
->pvt_info
;
90 const struct altr_sdram_prv_data
*priv
= drvdata
->data
;
91 u32 status
, err_count
= 1, err_addr
;
93 regmap_read(drvdata
->mc_vbase
, priv
->ecc_stat_offset
, &status
);
95 if (status
& priv
->ecc_stat_ue_mask
) {
96 regmap_read(drvdata
->mc_vbase
, priv
->ecc_daddr_offset
,
98 if (priv
->ecc_uecnt_offset
)
99 regmap_read(drvdata
->mc_vbase
, priv
->ecc_uecnt_offset
,
101 panic("\nEDAC: [%d Uncorrectable errors @ 0x%08X]\n",
102 err_count
, err_addr
);
104 if (status
& priv
->ecc_stat_ce_mask
) {
105 regmap_read(drvdata
->mc_vbase
, priv
->ecc_saddr_offset
,
107 if (priv
->ecc_uecnt_offset
)
108 regmap_read(drvdata
->mc_vbase
, priv
->ecc_cecnt_offset
,
110 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED
, mci
, err_count
,
111 err_addr
>> PAGE_SHIFT
,
112 err_addr
& ~PAGE_MASK
, 0,
113 0, 0, -1, mci
->ctl_name
, "");
114 /* Clear IRQ to resume */
115 regmap_write(drvdata
->mc_vbase
, priv
->ecc_irq_clr_offset
,
116 priv
->ecc_irq_clr_mask
);
123 static ssize_t
altr_sdr_mc_err_inject_write(struct file
*file
,
124 const char __user
*data
,
125 size_t count
, loff_t
*ppos
)
127 struct mem_ctl_info
*mci
= file
->private_data
;
128 struct altr_sdram_mc_data
*drvdata
= mci
->pvt_info
;
129 const struct altr_sdram_prv_data
*priv
= drvdata
->data
;
131 dma_addr_t dma_handle
;
134 ptemp
= dma_alloc_coherent(mci
->pdev
, 16, &dma_handle
, GFP_KERNEL
);
136 dma_free_coherent(mci
->pdev
, 16, ptemp
, dma_handle
);
137 edac_printk(KERN_ERR
, EDAC_MC
,
138 "Inject: Buffer Allocation error\n");
142 regmap_read(drvdata
->mc_vbase
, priv
->ce_ue_trgr_offset
,
144 read_reg
&= ~(priv
->ce_set_mask
| priv
->ue_set_mask
);
146 /* Error are injected by writing a word while the SBE or DBE
147 * bit in the CTLCFG register is set. Reading the word will
148 * trigger the SBE or DBE error and the corresponding IRQ.
151 edac_printk(KERN_ALERT
, EDAC_MC
,
152 "Inject Double bit error\n");
153 regmap_write(drvdata
->mc_vbase
, priv
->ce_ue_trgr_offset
,
154 (read_reg
| priv
->ue_set_mask
));
156 edac_printk(KERN_ALERT
, EDAC_MC
,
157 "Inject Single bit error\n");
158 regmap_write(drvdata
->mc_vbase
, priv
->ce_ue_trgr_offset
,
159 (read_reg
| priv
->ce_set_mask
));
162 ptemp
[0] = 0x5A5A5A5A;
163 ptemp
[1] = 0xA5A5A5A5;
165 /* Clear the error injection bits */
166 regmap_write(drvdata
->mc_vbase
, priv
->ce_ue_trgr_offset
, read_reg
);
167 /* Ensure it has been written out */
171 * To trigger the error, we need to read the data back
172 * (the data was written with errors above).
173 * The ACCESS_ONCE macros and printk are used to prevent the
174 * the compiler optimizing these reads out.
176 reg
= ACCESS_ONCE(ptemp
[0]);
177 read_reg
= ACCESS_ONCE(ptemp
[1]);
181 edac_printk(KERN_ALERT
, EDAC_MC
, "Read Data [0x%X, 0x%X]\n",
184 dma_free_coherent(mci
->pdev
, 16, ptemp
, dma_handle
);
189 static const struct file_operations altr_sdr_mc_debug_inject_fops
= {
191 .write
= altr_sdr_mc_err_inject_write
,
192 .llseek
= generic_file_llseek
,
195 static void altr_sdr_mc_create_debugfs_nodes(struct mem_ctl_info
*mci
)
197 if (!IS_ENABLED(CONFIG_EDAC_DEBUG
))
203 edac_debugfs_create_file("inject_ctrl", S_IWUSR
, mci
->debugfs
, mci
,
204 &altr_sdr_mc_debug_inject_fops
);
207 /* Get total memory size from Open Firmware DTB */
208 static unsigned long get_total_mem(void)
210 struct device_node
*np
= NULL
;
211 const unsigned int *reg
, *reg_end
;
213 unsigned long start
, size
, total_mem
= 0;
215 for_each_node_by_type(np
, "memory") {
216 aw
= of_n_addr_cells(np
);
217 sw
= of_n_size_cells(np
);
218 reg
= (const unsigned int *)of_get_property(np
, "reg", &len
);
219 reg_end
= reg
+ (len
/ sizeof(u32
));
223 start
= of_read_number(reg
, aw
);
225 size
= of_read_number(reg
, sw
);
228 } while (reg
< reg_end
);
230 edac_dbg(0, "total_mem 0x%lx\n", total_mem
);
234 static const struct of_device_id altr_sdram_ctrl_of_match
[] = {
235 { .compatible
= "altr,sdram-edac", .data
= &c5_data
},
236 { .compatible
= "altr,sdram-edac-a10", .data
= &a10_data
},
239 MODULE_DEVICE_TABLE(of
, altr_sdram_ctrl_of_match
);
241 static int a10_init(struct regmap
*mc_vbase
)
243 if (regmap_update_bits(mc_vbase
, A10_INTMODE_OFST
,
244 A10_INTMODE_SB_INT
, A10_INTMODE_SB_INT
)) {
245 edac_printk(KERN_ERR
, EDAC_MC
,
246 "Error setting SB IRQ mode\n");
250 if (regmap_write(mc_vbase
, A10_SERRCNTREG_OFST
, 1)) {
251 edac_printk(KERN_ERR
, EDAC_MC
,
252 "Error setting trigger count\n");
259 static int a10_unmask_irq(struct platform_device
*pdev
, u32 mask
)
261 void __iomem
*sm_base
;
264 if (!request_mem_region(A10_SYMAN_INTMASK_CLR
, sizeof(u32
),
265 dev_name(&pdev
->dev
))) {
266 edac_printk(KERN_ERR
, EDAC_MC
,
267 "Unable to request mem region\n");
271 sm_base
= ioremap(A10_SYMAN_INTMASK_CLR
, sizeof(u32
));
273 edac_printk(KERN_ERR
, EDAC_MC
,
274 "Unable to ioremap device\n");
280 iowrite32(mask
, sm_base
);
285 release_mem_region(A10_SYMAN_INTMASK_CLR
, sizeof(u32
));
290 static int altr_sdram_probe(struct platform_device
*pdev
)
292 const struct of_device_id
*id
;
293 struct edac_mc_layer layers
[2];
294 struct mem_ctl_info
*mci
;
295 struct altr_sdram_mc_data
*drvdata
;
296 const struct altr_sdram_prv_data
*priv
;
297 struct regmap
*mc_vbase
;
298 struct dimm_info
*dimm
;
300 int irq
, irq2
, res
= 0;
301 unsigned long mem_size
, irqflags
= 0;
303 id
= of_match_device(altr_sdram_ctrl_of_match
, &pdev
->dev
);
307 /* Grab the register range from the sdr controller in device tree */
308 mc_vbase
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
,
310 if (IS_ERR(mc_vbase
)) {
311 edac_printk(KERN_ERR
, EDAC_MC
,
312 "regmap for altr,sdr-syscon lookup failed.\n");
316 /* Check specific dependencies for the module */
317 priv
= of_match_node(altr_sdram_ctrl_of_match
,
318 pdev
->dev
.of_node
)->data
;
320 /* Validate the SDRAM controller has ECC enabled */
321 if (regmap_read(mc_vbase
, priv
->ecc_ctrl_offset
, &read_reg
) ||
322 ((read_reg
& priv
->ecc_ctl_en_mask
) != priv
->ecc_ctl_en_mask
)) {
323 edac_printk(KERN_ERR
, EDAC_MC
,
324 "No ECC/ECC disabled [0x%08X]\n", read_reg
);
328 /* Grab memory size from device tree. */
329 mem_size
= get_total_mem();
331 edac_printk(KERN_ERR
, EDAC_MC
, "Unable to calculate memory size\n");
335 /* Ensure the SDRAM Interrupt is disabled */
336 if (regmap_update_bits(mc_vbase
, priv
->ecc_irq_en_offset
,
337 priv
->ecc_irq_en_mask
, 0)) {
338 edac_printk(KERN_ERR
, EDAC_MC
,
339 "Error disabling SDRAM ECC IRQ\n");
343 /* Toggle to clear the SDRAM Error count */
344 if (regmap_update_bits(mc_vbase
, priv
->ecc_cnt_rst_offset
,
345 priv
->ecc_cnt_rst_mask
,
346 priv
->ecc_cnt_rst_mask
)) {
347 edac_printk(KERN_ERR
, EDAC_MC
,
348 "Error clearing SDRAM ECC count\n");
352 if (regmap_update_bits(mc_vbase
, priv
->ecc_cnt_rst_offset
,
353 priv
->ecc_cnt_rst_mask
, 0)) {
354 edac_printk(KERN_ERR
, EDAC_MC
,
355 "Error clearing SDRAM ECC count\n");
359 irq
= platform_get_irq(pdev
, 0);
361 edac_printk(KERN_ERR
, EDAC_MC
,
362 "No irq %d in DT\n", irq
);
366 /* Arria10 has a 2nd IRQ */
367 irq2
= platform_get_irq(pdev
, 1);
369 layers
[0].type
= EDAC_MC_LAYER_CHIP_SELECT
;
371 layers
[0].is_virt_csrow
= true;
372 layers
[1].type
= EDAC_MC_LAYER_CHANNEL
;
374 layers
[1].is_virt_csrow
= false;
375 mci
= edac_mc_alloc(0, ARRAY_SIZE(layers
), layers
,
376 sizeof(struct altr_sdram_mc_data
));
380 mci
->pdev
= &pdev
->dev
;
381 drvdata
= mci
->pvt_info
;
382 drvdata
->mc_vbase
= mc_vbase
;
383 drvdata
->data
= priv
;
384 platform_set_drvdata(pdev
, mci
);
386 if (!devres_open_group(&pdev
->dev
, NULL
, GFP_KERNEL
)) {
387 edac_printk(KERN_ERR
, EDAC_MC
,
388 "Unable to get managed device resource\n");
393 mci
->mtype_cap
= MEM_FLAG_DDR3
;
394 mci
->edac_ctl_cap
= EDAC_FLAG_NONE
| EDAC_FLAG_SECDED
;
395 mci
->edac_cap
= EDAC_FLAG_SECDED
;
396 mci
->mod_name
= EDAC_MOD_STR
;
397 mci
->mod_ver
= EDAC_VERSION
;
398 mci
->ctl_name
= dev_name(&pdev
->dev
);
399 mci
->scrub_mode
= SCRUB_SW_SRC
;
400 mci
->dev_name
= dev_name(&pdev
->dev
);
403 dimm
->nr_pages
= ((mem_size
- 1) >> PAGE_SHIFT
) + 1;
405 dimm
->dtype
= DEV_X8
;
406 dimm
->mtype
= MEM_DDR3
;
407 dimm
->edac_mode
= EDAC_SECDED
;
409 res
= edac_mc_add_mc(mci
);
413 /* Only the Arria10 has separate IRQs */
415 /* Arria10 specific initialization */
416 res
= a10_init(mc_vbase
);
420 res
= devm_request_irq(&pdev
->dev
, irq2
,
421 altr_sdram_mc_err_handler
,
422 IRQF_SHARED
, dev_name(&pdev
->dev
), mci
);
424 edac_mc_printk(mci
, KERN_ERR
,
425 "Unable to request irq %d\n", irq2
);
430 res
= a10_unmask_irq(pdev
, A10_DDR0_IRQ_MASK
);
434 irqflags
= IRQF_SHARED
;
437 res
= devm_request_irq(&pdev
->dev
, irq
, altr_sdram_mc_err_handler
,
438 irqflags
, dev_name(&pdev
->dev
), mci
);
440 edac_mc_printk(mci
, KERN_ERR
,
441 "Unable to request irq %d\n", irq
);
446 /* Infrastructure ready - enable the IRQ */
447 if (regmap_update_bits(drvdata
->mc_vbase
, priv
->ecc_irq_en_offset
,
448 priv
->ecc_irq_en_mask
, priv
->ecc_irq_en_mask
)) {
449 edac_mc_printk(mci
, KERN_ERR
,
450 "Error enabling SDRAM ECC IRQ\n");
455 altr_sdr_mc_create_debugfs_nodes(mci
);
457 devres_close_group(&pdev
->dev
, NULL
);
462 edac_mc_del_mc(&pdev
->dev
);
464 devres_release_group(&pdev
->dev
, NULL
);
467 edac_printk(KERN_ERR
, EDAC_MC
,
468 "EDAC Probe Failed; Error %d\n", res
);
473 static int altr_sdram_remove(struct platform_device
*pdev
)
475 struct mem_ctl_info
*mci
= platform_get_drvdata(pdev
);
477 edac_mc_del_mc(&pdev
->dev
);
479 platform_set_drvdata(pdev
, NULL
);
485 * If you want to suspend, need to disable EDAC by removing it
486 * from the device tree or defconfig.
489 static int altr_sdram_prepare(struct device
*dev
)
491 pr_err("Suspend not allowed when EDAC is enabled.\n");
496 static const struct dev_pm_ops altr_sdram_pm_ops
= {
497 .prepare
= altr_sdram_prepare
,
501 static struct platform_driver altr_sdram_edac_driver
= {
502 .probe
= altr_sdram_probe
,
503 .remove
= altr_sdram_remove
,
505 .name
= "altr_sdram_edac",
507 .pm
= &altr_sdram_pm_ops
,
509 .of_match_table
= altr_sdram_ctrl_of_match
,
513 module_platform_driver(altr_sdram_edac_driver
);
515 /************************* EDAC Parent Probe *************************/
517 static const struct of_device_id altr_edac_device_of_match
[];
519 static const struct of_device_id altr_edac_of_match
[] = {
520 { .compatible
= "altr,socfpga-ecc-manager" },
523 MODULE_DEVICE_TABLE(of
, altr_edac_of_match
);
525 static int altr_edac_probe(struct platform_device
*pdev
)
527 of_platform_populate(pdev
->dev
.of_node
, altr_edac_device_of_match
,
532 static struct platform_driver altr_edac_driver
= {
533 .probe
= altr_edac_probe
,
535 .name
= "socfpga_ecc_manager",
536 .of_match_table
= altr_edac_of_match
,
539 module_platform_driver(altr_edac_driver
);
541 /************************* EDAC Device Functions *************************/
544 * EDAC Device Functions (shared between various IPs).
545 * The discrete memories use the EDAC Device framework. The probe
546 * and error handling functions are very similar between memories
547 * so they are shared. The memory allocation and freeing for EDAC
548 * trigger testing are different for each memory.
551 const struct edac_device_prv_data ocramecc_data
;
552 const struct edac_device_prv_data l2ecc_data
;
553 const struct edac_device_prv_data a10_ocramecc_data
;
554 const struct edac_device_prv_data a10_l2ecc_data
;
556 static irqreturn_t
altr_edac_device_handler(int irq
, void *dev_id
)
558 irqreturn_t ret_value
= IRQ_NONE
;
559 struct edac_device_ctl_info
*dci
= dev_id
;
560 struct altr_edac_device_dev
*drvdata
= dci
->pvt_info
;
561 const struct edac_device_prv_data
*priv
= drvdata
->data
;
563 if (irq
== drvdata
->sb_irq
) {
564 if (priv
->ce_clear_mask
)
565 writel(priv
->ce_clear_mask
, drvdata
->base
);
566 edac_device_handle_ce(dci
, 0, 0, drvdata
->edac_dev_name
);
567 ret_value
= IRQ_HANDLED
;
568 } else if (irq
== drvdata
->db_irq
) {
569 if (priv
->ue_clear_mask
)
570 writel(priv
->ue_clear_mask
, drvdata
->base
);
571 edac_device_handle_ue(dci
, 0, 0, drvdata
->edac_dev_name
);
572 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
573 ret_value
= IRQ_HANDLED
;
581 static ssize_t
altr_edac_device_trig(struct file
*file
,
582 const char __user
*user_buf
,
583 size_t count
, loff_t
*ppos
)
586 u32
*ptemp
, i
, error_mask
;
590 struct edac_device_ctl_info
*edac_dci
= file
->private_data
;
591 struct altr_edac_device_dev
*drvdata
= edac_dci
->pvt_info
;
592 const struct edac_device_prv_data
*priv
= drvdata
->data
;
593 void *generic_ptr
= edac_dci
->dev
;
595 if (!user_buf
|| get_user(trig_type
, user_buf
))
598 if (!priv
->alloc_mem
)
602 * Note that generic_ptr is initialized to the device * but in
603 * some alloc_functions, this is overridden and returns data.
605 ptemp
= priv
->alloc_mem(priv
->trig_alloc_sz
, &generic_ptr
);
607 edac_printk(KERN_ERR
, EDAC_DEVICE
,
608 "Inject: Buffer Allocation error\n");
612 if (trig_type
== ALTR_UE_TRIGGER_CHAR
)
613 error_mask
= priv
->ue_set_mask
;
615 error_mask
= priv
->ce_set_mask
;
617 edac_printk(KERN_ALERT
, EDAC_DEVICE
,
618 "Trigger Error Mask (0x%X)\n", error_mask
);
620 local_irq_save(flags
);
621 /* write ECC corrupted data out. */
622 for (i
= 0; i
< (priv
->trig_alloc_sz
/ sizeof(*ptemp
)); i
++) {
623 /* Read data so we're in the correct state */
625 if (ACCESS_ONCE(ptemp
[i
]))
627 /* Toggle Error bit (it is latched), leave ECC enabled */
628 writel(error_mask
, (drvdata
->base
+ priv
->set_err_ofst
));
629 writel(priv
->ecc_enable_mask
, (drvdata
->base
+
630 priv
->set_err_ofst
));
633 /* Ensure it has been written out */
635 local_irq_restore(flags
);
638 edac_printk(KERN_ERR
, EDAC_DEVICE
, "Mem Not Cleared\n");
640 /* Read out written data. ECC error caused here */
641 for (i
= 0; i
< ALTR_TRIGGER_READ_WRD_CNT
; i
++)
642 if (ACCESS_ONCE(ptemp
[i
]) != i
)
643 edac_printk(KERN_ERR
, EDAC_DEVICE
,
644 "Read doesn't match written data\n");
647 priv
->free_mem(ptemp
, priv
->trig_alloc_sz
, generic_ptr
);
652 static const struct file_operations altr_edac_device_inject_fops
= {
654 .write
= altr_edac_device_trig
,
655 .llseek
= generic_file_llseek
,
658 static ssize_t
altr_edac_a10_device_trig(struct file
*file
,
659 const char __user
*user_buf
,
660 size_t count
, loff_t
*ppos
);
662 static const struct file_operations altr_edac_a10_device_inject_fops
= {
664 .write
= altr_edac_a10_device_trig
,
665 .llseek
= generic_file_llseek
,
668 static void altr_create_edacdev_dbgfs(struct edac_device_ctl_info
*edac_dci
,
669 const struct edac_device_prv_data
*priv
)
671 struct altr_edac_device_dev
*drvdata
= edac_dci
->pvt_info
;
673 if (!IS_ENABLED(CONFIG_EDAC_DEBUG
))
676 drvdata
->debugfs_dir
= edac_debugfs_create_dir(drvdata
->edac_dev_name
);
677 if (!drvdata
->debugfs_dir
)
680 if (!edac_debugfs_create_file(priv
->dbgfs_name
, S_IWUSR
,
681 drvdata
->debugfs_dir
, edac_dci
,
683 debugfs_remove_recursive(drvdata
->debugfs_dir
);
686 static const struct of_device_id altr_edac_device_of_match
[] = {
687 #ifdef CONFIG_EDAC_ALTERA_L2C
688 { .compatible
= "altr,socfpga-l2-ecc", .data
= &l2ecc_data
},
689 { .compatible
= "altr,socfpga-a10-l2-ecc", .data
= &a10_l2ecc_data
},
691 #ifdef CONFIG_EDAC_ALTERA_OCRAM
692 { .compatible
= "altr,socfpga-ocram-ecc", .data
= &ocramecc_data
},
693 { .compatible
= "altr,socfpga-a10-ocram-ecc", .data
= &a10_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, NULL
, 0,
744 edac_printk(KERN_ERR
, EDAC_DEVICE
,
745 "%s: Unable to allocate EDAC device\n", ecc_name
);
750 drvdata
= dci
->pvt_info
;
751 dci
->dev
= &pdev
->dev
;
752 platform_set_drvdata(pdev
, dci
);
753 drvdata
->edac_dev_name
= ecc_name
;
755 drvdata
->base
= devm_ioremap(&pdev
->dev
, r
->start
, resource_size(r
));
759 /* Get driver specific data for this EDAC device */
760 drvdata
->data
= of_match_node(altr_edac_device_of_match
, np
)->data
;
762 /* Check specific dependencies for the module */
763 if (drvdata
->data
->setup
) {
764 res
= drvdata
->data
->setup(drvdata
);
769 drvdata
->sb_irq
= platform_get_irq(pdev
, 0);
770 res
= devm_request_irq(&pdev
->dev
, drvdata
->sb_irq
,
771 altr_edac_device_handler
,
772 0, dev_name(&pdev
->dev
), dci
);
776 drvdata
->db_irq
= platform_get_irq(pdev
, 1);
777 res
= devm_request_irq(&pdev
->dev
, drvdata
->db_irq
,
778 altr_edac_device_handler
,
779 0, dev_name(&pdev
->dev
), dci
);
783 dci
->mod_name
= "Altera ECC Manager";
784 dci
->dev_name
= drvdata
->edac_dev_name
;
786 res
= edac_device_add_device(dci
);
790 altr_create_edacdev_dbgfs(dci
, drvdata
->data
);
792 devres_close_group(&pdev
->dev
, NULL
);
797 edac_device_free_ctl_info(dci
);
799 devres_release_group(&pdev
->dev
, NULL
);
800 edac_printk(KERN_ERR
, EDAC_DEVICE
,
801 "%s:Error setting up EDAC device: %d\n", ecc_name
, res
);
806 static int altr_edac_device_remove(struct platform_device
*pdev
)
808 struct edac_device_ctl_info
*dci
= platform_get_drvdata(pdev
);
809 struct altr_edac_device_dev
*drvdata
= dci
->pvt_info
;
811 debugfs_remove_recursive(drvdata
->debugfs_dir
);
812 edac_device_del_device(&pdev
->dev
);
813 edac_device_free_ctl_info(dci
);
818 static struct platform_driver altr_edac_device_driver
= {
819 .probe
= altr_edac_device_probe
,
820 .remove
= altr_edac_device_remove
,
822 .name
= "altr_edac_device",
823 .of_match_table
= altr_edac_device_of_match
,
826 module_platform_driver(altr_edac_device_driver
);
828 /*********************** OCRAM EDAC Device Functions *********************/
830 #ifdef CONFIG_EDAC_ALTERA_OCRAM
832 * Test for memory's ECC dependencies upon entry because platform specific
833 * startup should have initialized the memory and enabled the ECC.
834 * Can't turn on ECC here because accessing un-initialized memory will
835 * cause CE/UE errors possibly causing an ABORT.
837 static int altr_check_ecc_deps(struct altr_edac_device_dev
*device
)
839 void __iomem
*base
= device
->base
;
840 const struct edac_device_prv_data
*prv
= device
->data
;
842 if (readl(base
+ prv
->ecc_en_ofst
) & prv
->ecc_enable_mask
)
845 edac_printk(KERN_ERR
, EDAC_DEVICE
,
846 "%s: No ECC present or ECC disabled.\n",
847 device
->edac_dev_name
);
851 static void *ocram_alloc_mem(size_t size
, void **other
)
853 struct device_node
*np
;
857 np
= of_find_compatible_node(NULL
, NULL
, "altr,socfpga-ocram-ecc");
861 gp
= of_gen_pool_get(np
, "iram", 0);
866 sram_addr
= (void *)gen_pool_alloc(gp
, size
);
870 memset(sram_addr
, 0, size
);
871 /* Ensure data is written out */
874 /* Remember this handle for freeing later */
880 static void ocram_free_mem(void *p
, size_t size
, void *other
)
882 gen_pool_free((struct gen_pool
*)other
, (u32
)p
, size
);
885 static irqreturn_t
altr_edac_a10_ecc_irq(struct altr_edac_device_dev
*dci
,
888 void __iomem
*base
= dci
->base
;
891 writel(ALTR_A10_ECC_SERRPENA
,
892 base
+ ALTR_A10_ECC_INTSTAT_OFST
);
893 edac_device_handle_ce(dci
->edac_dev
, 0, 0, dci
->edac_dev_name
);
895 writel(ALTR_A10_ECC_DERRPENA
,
896 base
+ ALTR_A10_ECC_INTSTAT_OFST
);
897 edac_device_handle_ue(dci
->edac_dev
, 0, 0, dci
->edac_dev_name
);
898 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
903 const struct edac_device_prv_data ocramecc_data
= {
904 .setup
= altr_check_ecc_deps
,
905 .ce_clear_mask
= (ALTR_OCR_ECC_EN
| ALTR_OCR_ECC_SERR
),
906 .ue_clear_mask
= (ALTR_OCR_ECC_EN
| ALTR_OCR_ECC_DERR
),
907 .dbgfs_name
= "altr_ocram_trigger",
908 .alloc_mem
= ocram_alloc_mem
,
909 .free_mem
= ocram_free_mem
,
910 .ecc_enable_mask
= ALTR_OCR_ECC_EN
,
911 .ecc_en_ofst
= ALTR_OCR_ECC_REG_OFFSET
,
912 .ce_set_mask
= (ALTR_OCR_ECC_EN
| ALTR_OCR_ECC_INJS
),
913 .ue_set_mask
= (ALTR_OCR_ECC_EN
| ALTR_OCR_ECC_INJD
),
914 .set_err_ofst
= ALTR_OCR_ECC_REG_OFFSET
,
915 .trig_alloc_sz
= ALTR_TRIG_OCRAM_BYTE_SIZE
,
916 .inject_fops
= &altr_edac_device_inject_fops
,
919 const struct edac_device_prv_data a10_ocramecc_data
= {
920 .setup
= altr_check_ecc_deps
,
921 .ce_clear_mask
= ALTR_A10_ECC_SERRPENA
,
922 .ue_clear_mask
= ALTR_A10_ECC_DERRPENA
,
923 .irq_status_mask
= A10_SYSMGR_ECC_INTSTAT_OCRAM
,
924 .dbgfs_name
= "altr_ocram_trigger",
925 .ecc_enable_mask
= ALTR_A10_OCRAM_ECC_EN_CTL
,
926 .ecc_en_ofst
= ALTR_A10_ECC_CTRL_OFST
,
927 .ce_set_mask
= ALTR_A10_ECC_TSERRA
,
928 .ue_set_mask
= ALTR_A10_ECC_TDERRA
,
929 .set_err_ofst
= ALTR_A10_ECC_INTTEST_OFST
,
930 .ecc_irq_handler
= altr_edac_a10_ecc_irq
,
931 .inject_fops
= &altr_edac_a10_device_inject_fops
,
934 #endif /* CONFIG_EDAC_ALTERA_OCRAM */
936 /********************* L2 Cache EDAC Device Functions ********************/
938 #ifdef CONFIG_EDAC_ALTERA_L2C
940 static void *l2_alloc_mem(size_t size
, void **other
)
942 struct device
*dev
= *other
;
943 void *ptemp
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
948 /* Make sure everything is written out */
952 * Clean all cache levels up to LoC (includes L2)
953 * This ensures the corrupted data is written into
954 * L2 cache for readback test (which causes ECC error).
961 static void l2_free_mem(void *p
, size_t size
, void *other
)
963 struct device
*dev
= other
;
970 * altr_l2_check_deps()
971 * Test for L2 cache ECC dependencies upon entry because
972 * platform specific startup should have initialized the L2
973 * memory and enabled the ECC.
974 * Bail if ECC is not enabled.
975 * Note that L2 Cache Enable is forced at build time.
977 static int altr_l2_check_deps(struct altr_edac_device_dev
*device
)
979 void __iomem
*base
= device
->base
;
980 const struct edac_device_prv_data
*prv
= device
->data
;
982 if ((readl(base
) & prv
->ecc_enable_mask
) ==
983 prv
->ecc_enable_mask
)
986 edac_printk(KERN_ERR
, EDAC_DEVICE
,
987 "L2: No ECC present, or ECC disabled\n");
991 static irqreturn_t
altr_edac_a10_l2_irq(struct altr_edac_device_dev
*dci
,
995 regmap_write(dci
->edac
->ecc_mgr_map
,
996 A10_SYSGMR_MPU_CLEAR_L2_ECC_OFST
,
997 A10_SYSGMR_MPU_CLEAR_L2_ECC_SB
);
998 edac_device_handle_ce(dci
->edac_dev
, 0, 0, dci
->edac_dev_name
);
1000 regmap_write(dci
->edac
->ecc_mgr_map
,
1001 A10_SYSGMR_MPU_CLEAR_L2_ECC_OFST
,
1002 A10_SYSGMR_MPU_CLEAR_L2_ECC_MB
);
1003 edac_device_handle_ue(dci
->edac_dev
, 0, 0, dci
->edac_dev_name
);
1004 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
1009 const struct edac_device_prv_data l2ecc_data
= {
1010 .setup
= altr_l2_check_deps
,
1013 .dbgfs_name
= "altr_l2_trigger",
1014 .alloc_mem
= l2_alloc_mem
,
1015 .free_mem
= l2_free_mem
,
1016 .ecc_enable_mask
= ALTR_L2_ECC_EN
,
1017 .ce_set_mask
= (ALTR_L2_ECC_EN
| ALTR_L2_ECC_INJS
),
1018 .ue_set_mask
= (ALTR_L2_ECC_EN
| ALTR_L2_ECC_INJD
),
1019 .set_err_ofst
= ALTR_L2_ECC_REG_OFFSET
,
1020 .trig_alloc_sz
= ALTR_TRIG_L2C_BYTE_SIZE
,
1021 .inject_fops
= &altr_edac_device_inject_fops
,
1024 const struct edac_device_prv_data a10_l2ecc_data
= {
1025 .setup
= altr_l2_check_deps
,
1026 .ce_clear_mask
= ALTR_A10_L2_ECC_SERR_CLR
,
1027 .ue_clear_mask
= ALTR_A10_L2_ECC_MERR_CLR
,
1028 .irq_status_mask
= A10_SYSMGR_ECC_INTSTAT_L2
,
1029 .dbgfs_name
= "altr_l2_trigger",
1030 .alloc_mem
= l2_alloc_mem
,
1031 .free_mem
= l2_free_mem
,
1032 .ecc_enable_mask
= ALTR_A10_L2_ECC_EN_CTL
,
1033 .ce_set_mask
= ALTR_A10_L2_ECC_CE_INJ_MASK
,
1034 .ue_set_mask
= ALTR_A10_L2_ECC_UE_INJ_MASK
,
1035 .set_err_ofst
= ALTR_A10_L2_ECC_INJ_OFST
,
1036 .ecc_irq_handler
= altr_edac_a10_l2_irq
,
1037 .trig_alloc_sz
= ALTR_TRIG_L2C_BYTE_SIZE
,
1038 .inject_fops
= &altr_edac_device_inject_fops
,
1041 #endif /* CONFIG_EDAC_ALTERA_L2C */
1043 /********************* Arria10 EDAC Device Functions *************************/
1046 * The Arria10 EDAC Device Functions differ from the Cyclone5/Arria5
1047 * because 2 IRQs are shared among the all ECC peripherals. The ECC
1048 * manager manages the IRQs and the children.
1049 * Based on xgene_edac.c peripheral code.
1052 static ssize_t
altr_edac_a10_device_trig(struct file
*file
,
1053 const char __user
*user_buf
,
1054 size_t count
, loff_t
*ppos
)
1056 struct edac_device_ctl_info
*edac_dci
= file
->private_data
;
1057 struct altr_edac_device_dev
*drvdata
= edac_dci
->pvt_info
;
1058 const struct edac_device_prv_data
*priv
= drvdata
->data
;
1059 void __iomem
*set_addr
= (drvdata
->base
+ priv
->set_err_ofst
);
1060 unsigned long flags
;
1063 if (!user_buf
|| get_user(trig_type
, user_buf
))
1066 local_irq_save(flags
);
1067 if (trig_type
== ALTR_UE_TRIGGER_CHAR
)
1068 writel(priv
->ue_set_mask
, set_addr
);
1070 writel(priv
->ce_set_mask
, set_addr
);
1071 /* Ensure the interrupt test bits are set */
1073 local_irq_restore(flags
);
1078 static irqreturn_t
altr_edac_a10_irq_handler(int irq
, void *dev_id
)
1080 irqreturn_t rc
= IRQ_NONE
;
1081 struct altr_arria10_edac
*edac
= dev_id
;
1082 struct altr_edac_device_dev
*dci
;
1084 bool sberr
= (irq
== edac
->sb_irq
) ? 1 : 0;
1085 int sm_offset
= sberr
? A10_SYSMGR_ECC_INTSTAT_SERR_OFST
:
1086 A10_SYSMGR_ECC_INTSTAT_DERR_OFST
;
1088 regmap_read(edac
->ecc_mgr_map
, sm_offset
, &irq_status
);
1090 if ((irq
!= edac
->sb_irq
) && (irq
!= edac
->db_irq
)) {
1093 list_for_each_entry(dci
, &edac
->a10_ecc_devices
, next
) {
1094 if (irq_status
& dci
->data
->irq_status_mask
)
1095 rc
= dci
->data
->ecc_irq_handler(dci
, sberr
);
1102 static int altr_edac_a10_device_add(struct altr_arria10_edac
*edac
,
1103 struct device_node
*np
)
1105 struct edac_device_ctl_info
*dci
;
1106 struct altr_edac_device_dev
*altdev
;
1107 char *ecc_name
= (char *)np
->name
;
1108 struct resource res
;
1111 const struct edac_device_prv_data
*prv
;
1112 /* Get matching node and check for valid result */
1113 const struct of_device_id
*pdev_id
=
1114 of_match_node(altr_edac_device_of_match
, np
);
1115 if (IS_ERR_OR_NULL(pdev_id
))
1118 /* Get driver specific data for this EDAC device */
1119 prv
= pdev_id
->data
;
1120 if (IS_ERR_OR_NULL(prv
))
1123 if (!devres_open_group(edac
->dev
, altr_edac_a10_device_add
, GFP_KERNEL
))
1126 rc
= of_address_to_resource(np
, 0, &res
);
1128 edac_printk(KERN_ERR
, EDAC_DEVICE
,
1129 "%s: no resource address\n", ecc_name
);
1130 goto err_release_group
;
1133 edac_idx
= edac_device_alloc_index();
1134 dci
= edac_device_alloc_ctl_info(sizeof(*altdev
), ecc_name
,
1135 1, ecc_name
, 1, 0, NULL
, 0,
1139 edac_printk(KERN_ERR
, EDAC_DEVICE
,
1140 "%s: Unable to allocate EDAC device\n", ecc_name
);
1142 goto err_release_group
;
1145 altdev
= dci
->pvt_info
;
1146 dci
->dev
= edac
->dev
;
1147 altdev
->edac_dev_name
= ecc_name
;
1148 altdev
->edac_idx
= edac_idx
;
1149 altdev
->edac
= edac
;
1150 altdev
->edac_dev
= dci
;
1152 altdev
->ddev
= *edac
->dev
;
1153 dci
->dev
= &altdev
->ddev
;
1154 dci
->ctl_name
= "Altera ECC Manager";
1155 dci
->mod_name
= ecc_name
;
1156 dci
->dev_name
= ecc_name
;
1158 altdev
->base
= devm_ioremap_resource(edac
->dev
, &res
);
1159 if (IS_ERR(altdev
->base
)) {
1160 rc
= PTR_ERR(altdev
->base
);
1161 goto err_release_group1
;
1164 /* Check specific dependencies for the module */
1165 if (altdev
->data
->setup
) {
1166 rc
= altdev
->data
->setup(altdev
);
1168 goto err_release_group1
;
1171 rc
= edac_device_add_device(dci
);
1173 dev_err(edac
->dev
, "edac_device_add_device failed\n");
1175 goto err_release_group1
;
1178 altr_create_edacdev_dbgfs(dci
, prv
);
1180 list_add(&altdev
->next
, &edac
->a10_ecc_devices
);
1182 devres_remove_group(edac
->dev
, altr_edac_a10_device_add
);
1187 edac_device_free_ctl_info(dci
);
1189 edac_printk(KERN_ALERT
, EDAC_DEVICE
, "%s: %d\n", __func__
, __LINE__
);
1190 devres_release_group(edac
->dev
, NULL
);
1191 edac_printk(KERN_ERR
, EDAC_DEVICE
,
1192 "%s:Error setting up EDAC device: %d\n", ecc_name
, rc
);
1197 static int altr_edac_a10_probe(struct platform_device
*pdev
)
1199 struct altr_arria10_edac
*edac
;
1200 struct device_node
*child
;
1203 edac
= devm_kzalloc(&pdev
->dev
, sizeof(*edac
), GFP_KERNEL
);
1207 edac
->dev
= &pdev
->dev
;
1208 platform_set_drvdata(pdev
, edac
);
1209 INIT_LIST_HEAD(&edac
->a10_ecc_devices
);
1211 edac
->ecc_mgr_map
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
,
1212 "altr,sysmgr-syscon");
1213 if (IS_ERR(edac
->ecc_mgr_map
)) {
1214 edac_printk(KERN_ERR
, EDAC_DEVICE
,
1215 "Unable to get syscon altr,sysmgr-syscon\n");
1216 return PTR_ERR(edac
->ecc_mgr_map
);
1219 edac
->sb_irq
= platform_get_irq(pdev
, 0);
1220 rc
= devm_request_irq(&pdev
->dev
, edac
->sb_irq
,
1221 altr_edac_a10_irq_handler
,
1222 IRQF_SHARED
, dev_name(&pdev
->dev
), edac
);
1224 edac_printk(KERN_ERR
, EDAC_DEVICE
, "No SBERR IRQ resource\n");
1228 edac
->db_irq
= platform_get_irq(pdev
, 1);
1229 rc
= devm_request_irq(&pdev
->dev
, edac
->db_irq
,
1230 altr_edac_a10_irq_handler
,
1231 IRQF_SHARED
, dev_name(&pdev
->dev
), edac
);
1233 edac_printk(KERN_ERR
, EDAC_DEVICE
, "No DBERR IRQ resource\n");
1237 for_each_child_of_node(pdev
->dev
.of_node
, child
) {
1238 if (!of_device_is_available(child
))
1240 if (of_device_is_compatible(child
, "altr,socfpga-a10-l2-ecc"))
1241 altr_edac_a10_device_add(edac
, child
);
1242 else if (of_device_is_compatible(child
,
1243 "altr,socfpga-a10-ocram-ecc"))
1244 altr_edac_a10_device_add(edac
, child
);
1250 static const struct of_device_id altr_edac_a10_of_match
[] = {
1251 { .compatible
= "altr,socfpga-a10-ecc-manager" },
1254 MODULE_DEVICE_TABLE(of
, altr_edac_a10_of_match
);
1256 static struct platform_driver altr_edac_a10_driver
= {
1257 .probe
= altr_edac_a10_probe
,
1259 .name
= "socfpga_a10_ecc_manager",
1260 .of_match_table
= altr_edac_a10_of_match
,
1263 module_platform_driver(altr_edac_a10_driver
);
1265 MODULE_LICENSE("GPL v2");
1266 MODULE_AUTHOR("Thor Thayer");
1267 MODULE_DESCRIPTION("EDAC Driver for Altera Memories");