blob: 3e86cf327ad08b3b30fca07be20e26a1eb14bb84 [file] [log] [blame]
Thor Thayer3dab6bd2018-04-27 13:37:17 -05001// SPDX-License-Identifier: GPL-2.0
Thor Thayer71bcada2014-09-03 10:27:54 -05002/*
Thor Thayer3dab6bd2018-04-27 13:37:17 -05003 * Copyright (C) 2017-2018, Intel Corporation. All rights reserved
Thor Thayerc3eea192016-02-10 13:26:21 -06004 * Copyright Altera Corporation (C) 2014-2016. All rights reserved.
Thor Thayer71bcada2014-09-03 10:27:54 -05005 * Copyright 2011-2012 Calxeda, Inc.
Thor Thayer71bcada2014-09-03 10:27:54 -05006 */
7
Thor Thayerc3eea192016-02-10 13:26:21 -06008#include <asm/cacheflush.h>
Thor Thayer71bcada2014-09-03 10:27:54 -05009#include <linux/ctype.h>
Thor Thayer1166fde92016-06-22 08:58:57 -050010#include <linux/delay.h>
Thor Thayer71bcada2014-09-03 10:27:54 -050011#include <linux/edac.h>
Thor Thayerfad9fab2019-04-02 17:40:56 +020012#include <linux/firmware/intel/stratix10-smc.h>
Thor Thayerc3eea192016-02-10 13:26:21 -060013#include <linux/genalloc.h>
Thor Thayer71bcada2014-09-03 10:27:54 -050014#include <linux/interrupt.h>
Thor Thayer13ab8442016-06-07 15:35:57 -050015#include <linux/irqchip/chained_irq.h>
Thor Thayer71bcada2014-09-03 10:27:54 -050016#include <linux/kernel.h>
17#include <linux/mfd/syscon.h>
Thor Thayere9918d72018-05-11 18:00:10 -050018#include <linux/notifier.h>
Thor Thayer588cb032016-03-21 11:01:44 -050019#include <linux/of_address.h>
Thor Thayer13ab8442016-06-07 15:35:57 -050020#include <linux/of_irq.h>
Thor Thayer71bcada2014-09-03 10:27:54 -050021#include <linux/of_platform.h>
22#include <linux/platform_device.h>
23#include <linux/regmap.h>
24#include <linux/types.h>
25#include <linux/uaccess.h>
26
Thor Thayer143f4a52015-06-04 09:28:46 -050027#include "altera_edac.h"
Thor Thayer71bcada2014-09-03 10:27:54 -050028#include "edac_module.h"
29
30#define EDAC_MOD_STR "altera_edac"
Thor Thayerc3eea192016-02-10 13:26:21 -060031#define EDAC_DEVICE "Altera"
Thor Thayer71bcada2014-09-03 10:27:54 -050032
Thor Thayer580b5cf2019-02-25 12:56:45 -060033#ifdef CONFIG_EDAC_ALTERA_SDRAM
Thor Thayer143f4a52015-06-04 09:28:46 -050034static const struct altr_sdram_prv_data c5_data = {
35 .ecc_ctrl_offset = CV_CTLCFG_OFST,
36 .ecc_ctl_en_mask = CV_CTLCFG_ECC_AUTO_EN,
37 .ecc_stat_offset = CV_DRAMSTS_OFST,
38 .ecc_stat_ce_mask = CV_DRAMSTS_SBEERR,
39 .ecc_stat_ue_mask = CV_DRAMSTS_DBEERR,
40 .ecc_saddr_offset = CV_ERRADDR_OFST,
Thor Thayer73bcc942015-06-04 09:28:47 -050041 .ecc_daddr_offset = CV_ERRADDR_OFST,
Thor Thayer143f4a52015-06-04 09:28:46 -050042 .ecc_cecnt_offset = CV_SBECOUNT_OFST,
43 .ecc_uecnt_offset = CV_DBECOUNT_OFST,
44 .ecc_irq_en_offset = CV_DRAMINTR_OFST,
45 .ecc_irq_en_mask = CV_DRAMINTR_INTREN,
46 .ecc_irq_clr_offset = CV_DRAMINTR_OFST,
47 .ecc_irq_clr_mask = (CV_DRAMINTR_INTRCLR | CV_DRAMINTR_INTREN),
48 .ecc_cnt_rst_offset = CV_DRAMINTR_OFST,
49 .ecc_cnt_rst_mask = CV_DRAMINTR_INTRCLR,
Thor Thayer143f4a52015-06-04 09:28:46 -050050 .ce_ue_trgr_offset = CV_CTLCFG_OFST,
51 .ce_set_mask = CV_CTLCFG_GEN_SB_ERR,
52 .ue_set_mask = CV_CTLCFG_GEN_DB_ERR,
Thor Thayer71bcada2014-09-03 10:27:54 -050053};
54
Thor Thayer73bcc942015-06-04 09:28:47 -050055static const struct altr_sdram_prv_data a10_data = {
56 .ecc_ctrl_offset = A10_ECCCTRL1_OFST,
57 .ecc_ctl_en_mask = A10_ECCCTRL1_ECC_EN,
58 .ecc_stat_offset = A10_INTSTAT_OFST,
59 .ecc_stat_ce_mask = A10_INTSTAT_SBEERR,
60 .ecc_stat_ue_mask = A10_INTSTAT_DBEERR,
61 .ecc_saddr_offset = A10_SERRADDR_OFST,
62 .ecc_daddr_offset = A10_DERRADDR_OFST,
63 .ecc_irq_en_offset = A10_ERRINTEN_OFST,
64 .ecc_irq_en_mask = A10_ECC_IRQ_EN_MASK,
65 .ecc_irq_clr_offset = A10_INTSTAT_OFST,
66 .ecc_irq_clr_mask = (A10_INTSTAT_SBEERR | A10_INTSTAT_DBEERR),
67 .ecc_cnt_rst_offset = A10_ECCCTRL1_OFST,
68 .ecc_cnt_rst_mask = A10_ECC_CNT_RESET_MASK,
Thor Thayer73bcc942015-06-04 09:28:47 -050069 .ce_ue_trgr_offset = A10_DIAGINTTEST_OFST,
70 .ce_set_mask = A10_DIAGINT_TSERRA_MASK,
71 .ue_set_mask = A10_DIAGINT_TDERRA_MASK,
Thor Thayer73bcc942015-06-04 09:28:47 -050072};
73
Thor Thayerc3eea192016-02-10 13:26:21 -060074/*********************** EDAC Memory Controller Functions ****************/
75
76/* The SDRAM controller uses the EDAC Memory Controller framework. */
77
Thor Thayer71bcada2014-09-03 10:27:54 -050078static irqreturn_t altr_sdram_mc_err_handler(int irq, void *dev_id)
79{
80 struct mem_ctl_info *mci = dev_id;
81 struct altr_sdram_mc_data *drvdata = mci->pvt_info;
Thor Thayer143f4a52015-06-04 09:28:46 -050082 const struct altr_sdram_prv_data *priv = drvdata->data;
Thor Thayer73bcc942015-06-04 09:28:47 -050083 u32 status, err_count = 1, err_addr;
Thor Thayer71bcada2014-09-03 10:27:54 -050084
Thor Thayer143f4a52015-06-04 09:28:46 -050085 regmap_read(drvdata->mc_vbase, priv->ecc_stat_offset, &status);
Thor Thayer71bcada2014-09-03 10:27:54 -050086
Thor Thayer143f4a52015-06-04 09:28:46 -050087 if (status & priv->ecc_stat_ue_mask) {
Thor Thayer73bcc942015-06-04 09:28:47 -050088 regmap_read(drvdata->mc_vbase, priv->ecc_daddr_offset,
89 &err_addr);
90 if (priv->ecc_uecnt_offset)
91 regmap_read(drvdata->mc_vbase, priv->ecc_uecnt_offset,
92 &err_count);
Thor Thayer71bcada2014-09-03 10:27:54 -050093 panic("\nEDAC: [%d Uncorrectable errors @ 0x%08X]\n",
94 err_count, err_addr);
95 }
Thor Thayer143f4a52015-06-04 09:28:46 -050096 if (status & priv->ecc_stat_ce_mask) {
Thor Thayer73bcc942015-06-04 09:28:47 -050097 regmap_read(drvdata->mc_vbase, priv->ecc_saddr_offset,
98 &err_addr);
99 if (priv->ecc_uecnt_offset)
100 regmap_read(drvdata->mc_vbase, priv->ecc_cecnt_offset,
101 &err_count);
Thor Thayer71bcada2014-09-03 10:27:54 -0500102 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, err_count,
103 err_addr >> PAGE_SHIFT,
104 err_addr & ~PAGE_MASK, 0,
105 0, 0, -1, mci->ctl_name, "");
Thor Thayer73bcc942015-06-04 09:28:47 -0500106 /* Clear IRQ to resume */
107 regmap_write(drvdata->mc_vbase, priv->ecc_irq_clr_offset,
108 priv->ecc_irq_clr_mask);
109
110 return IRQ_HANDLED;
Thor Thayer71bcada2014-09-03 10:27:54 -0500111 }
Thor Thayer73bcc942015-06-04 09:28:47 -0500112 return IRQ_NONE;
Thor Thayer71bcada2014-09-03 10:27:54 -0500113}
114
Thor Thayer71bcada2014-09-03 10:27:54 -0500115static ssize_t altr_sdr_mc_err_inject_write(struct file *file,
116 const char __user *data,
117 size_t count, loff_t *ppos)
118{
119 struct mem_ctl_info *mci = file->private_data;
120 struct altr_sdram_mc_data *drvdata = mci->pvt_info;
Thor Thayer143f4a52015-06-04 09:28:46 -0500121 const struct altr_sdram_prv_data *priv = drvdata->data;
Thor Thayer71bcada2014-09-03 10:27:54 -0500122 u32 *ptemp;
123 dma_addr_t dma_handle;
124 u32 reg, read_reg;
125
126 ptemp = dma_alloc_coherent(mci->pdev, 16, &dma_handle, GFP_KERNEL);
127 if (!ptemp) {
128 dma_free_coherent(mci->pdev, 16, ptemp, dma_handle);
129 edac_printk(KERN_ERR, EDAC_MC,
130 "Inject: Buffer Allocation error\n");
131 return -ENOMEM;
132 }
133
Thor Thayer143f4a52015-06-04 09:28:46 -0500134 regmap_read(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
135 &read_reg);
136 read_reg &= ~(priv->ce_set_mask | priv->ue_set_mask);
Thor Thayer71bcada2014-09-03 10:27:54 -0500137
138 /* Error are injected by writing a word while the SBE or DBE
139 * bit in the CTLCFG register is set. Reading the word will
140 * trigger the SBE or DBE error and the corresponding IRQ.
141 */
142 if (count == 3) {
143 edac_printk(KERN_ALERT, EDAC_MC,
144 "Inject Double bit error\n");
Thor Thayer90e493d2016-10-19 14:53:47 -0500145 local_irq_disable();
Thor Thayer143f4a52015-06-04 09:28:46 -0500146 regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
147 (read_reg | priv->ue_set_mask));
Thor Thayer90e493d2016-10-19 14:53:47 -0500148 local_irq_enable();
Thor Thayer71bcada2014-09-03 10:27:54 -0500149 } else {
150 edac_printk(KERN_ALERT, EDAC_MC,
151 "Inject Single bit error\n");
Thor Thayer90e493d2016-10-19 14:53:47 -0500152 local_irq_disable();
Thor Thayer143f4a52015-06-04 09:28:46 -0500153 regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
154 (read_reg | priv->ce_set_mask));
Thor Thayer90e493d2016-10-19 14:53:47 -0500155 local_irq_enable();
Thor Thayer71bcada2014-09-03 10:27:54 -0500156 }
157
158 ptemp[0] = 0x5A5A5A5A;
159 ptemp[1] = 0xA5A5A5A5;
160
161 /* Clear the error injection bits */
Thor Thayer143f4a52015-06-04 09:28:46 -0500162 regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset, read_reg);
Thor Thayer71bcada2014-09-03 10:27:54 -0500163 /* Ensure it has been written out */
164 wmb();
165
166 /*
167 * To trigger the error, we need to read the data back
168 * (the data was written with errors above).
Mark Rutland332efa62017-10-23 14:07:12 -0700169 * The READ_ONCE macros and printk are used to prevent the
Thor Thayer71bcada2014-09-03 10:27:54 -0500170 * the compiler optimizing these reads out.
171 */
Mark Rutland332efa62017-10-23 14:07:12 -0700172 reg = READ_ONCE(ptemp[0]);
173 read_reg = READ_ONCE(ptemp[1]);
Thor Thayer71bcada2014-09-03 10:27:54 -0500174 /* Force Read */
175 rmb();
176
177 edac_printk(KERN_ALERT, EDAC_MC, "Read Data [0x%X, 0x%X]\n",
178 reg, read_reg);
179
180 dma_free_coherent(mci->pdev, 16, ptemp, dma_handle);
181
182 return count;
183}
184
185static const struct file_operations altr_sdr_mc_debug_inject_fops = {
186 .open = simple_open,
187 .write = altr_sdr_mc_err_inject_write,
188 .llseek = generic_file_llseek,
189};
190
191static void altr_sdr_mc_create_debugfs_nodes(struct mem_ctl_info *mci)
192{
Borislav Petkovbba3b312015-09-22 12:27:29 +0200193 if (!IS_ENABLED(CONFIG_EDAC_DEBUG))
194 return;
195
196 if (!mci->debugfs)
197 return;
198
Thor Thayerb8978ba2016-08-19 11:04:26 -0500199 edac_debugfs_create_file("altr_trigger", S_IWUSR, mci->debugfs, mci,
Borislav Petkovbba3b312015-09-22 12:27:29 +0200200 &altr_sdr_mc_debug_inject_fops);
Thor Thayer71bcada2014-09-03 10:27:54 -0500201}
Thor Thayer71bcada2014-09-03 10:27:54 -0500202
Thor Thayerf9ae4872015-06-04 09:28:45 -0500203/* Get total memory size from Open Firmware DTB */
204static unsigned long get_total_mem(void)
Thor Thayer71bcada2014-09-03 10:27:54 -0500205{
Thor Thayerf9ae4872015-06-04 09:28:45 -0500206 struct device_node *np = NULL;
Chris Packhamff0abed2017-06-07 11:54:59 +1200207 struct resource res;
208 int ret;
209 unsigned long total_mem = 0;
Thor Thayer71bcada2014-09-03 10:27:54 -0500210
Thor Thayerf9ae4872015-06-04 09:28:45 -0500211 for_each_node_by_type(np, "memory") {
Chris Packhamff0abed2017-06-07 11:54:59 +1200212 ret = of_address_to_resource(np, 0, &res);
213 if (ret)
214 continue;
Thor Thayer71bcada2014-09-03 10:27:54 -0500215
Chris Packhamff0abed2017-06-07 11:54:59 +1200216 total_mem += resource_size(&res);
Thor Thayerf9ae4872015-06-04 09:28:45 -0500217 }
218 edac_dbg(0, "total_mem 0x%lx\n", total_mem);
219 return total_mem;
Thor Thayer71bcada2014-09-03 10:27:54 -0500220}
221
Thor Thayer143f4a52015-06-04 09:28:46 -0500222static const struct of_device_id altr_sdram_ctrl_of_match[] = {
Arnd Bergmann2c911f62016-04-16 22:13:55 +0200223 { .compatible = "altr,sdram-edac", .data = &c5_data},
224 { .compatible = "altr,sdram-edac-a10", .data = &a10_data},
Thor Thayer143f4a52015-06-04 09:28:46 -0500225 {},
226};
227MODULE_DEVICE_TABLE(of, altr_sdram_ctrl_of_match);
228
Thor Thayer73bcc942015-06-04 09:28:47 -0500229static int a10_init(struct regmap *mc_vbase)
230{
231 if (regmap_update_bits(mc_vbase, A10_INTMODE_OFST,
232 A10_INTMODE_SB_INT, A10_INTMODE_SB_INT)) {
233 edac_printk(KERN_ERR, EDAC_MC,
234 "Error setting SB IRQ mode\n");
235 return -ENODEV;
236 }
237
238 if (regmap_write(mc_vbase, A10_SERRCNTREG_OFST, 1)) {
239 edac_printk(KERN_ERR, EDAC_MC,
240 "Error setting trigger count\n");
241 return -ENODEV;
242 }
243
244 return 0;
245}
246
247static int a10_unmask_irq(struct platform_device *pdev, u32 mask)
248{
249 void __iomem *sm_base;
250 int ret = 0;
251
252 if (!request_mem_region(A10_SYMAN_INTMASK_CLR, sizeof(u32),
253 dev_name(&pdev->dev))) {
254 edac_printk(KERN_ERR, EDAC_MC,
255 "Unable to request mem region\n");
256 return -EBUSY;
257 }
258
259 sm_base = ioremap(A10_SYMAN_INTMASK_CLR, sizeof(u32));
260 if (!sm_base) {
261 edac_printk(KERN_ERR, EDAC_MC,
262 "Unable to ioremap device\n");
263
264 ret = -ENOMEM;
265 goto release;
266 }
267
268 iowrite32(mask, sm_base);
269
270 iounmap(sm_base);
271
272release:
273 release_mem_region(A10_SYMAN_INTMASK_CLR, sizeof(u32));
274
275 return ret;
276}
277
Thor Thayer08f08bf2018-09-25 08:49:00 -0500278static int socfpga_is_a10(void);
Thor Thayer71bcada2014-09-03 10:27:54 -0500279static int altr_sdram_probe(struct platform_device *pdev)
280{
Thor Thayer143f4a52015-06-04 09:28:46 -0500281 const struct of_device_id *id;
Thor Thayer71bcada2014-09-03 10:27:54 -0500282 struct edac_mc_layer layers[2];
283 struct mem_ctl_info *mci;
284 struct altr_sdram_mc_data *drvdata;
Thor Thayer143f4a52015-06-04 09:28:46 -0500285 const struct altr_sdram_prv_data *priv;
Thor Thayer71bcada2014-09-03 10:27:54 -0500286 struct regmap *mc_vbase;
287 struct dimm_info *dimm;
Thor Thayer143f4a52015-06-04 09:28:46 -0500288 u32 read_reg;
Thor Thayer73bcc942015-06-04 09:28:47 -0500289 int irq, irq2, res = 0;
290 unsigned long mem_size, irqflags = 0;
Thor Thayer71bcada2014-09-03 10:27:54 -0500291
Thor Thayer143f4a52015-06-04 09:28:46 -0500292 id = of_match_device(altr_sdram_ctrl_of_match, &pdev->dev);
293 if (!id)
294 return -ENODEV;
295
Thor Thayer71bcada2014-09-03 10:27:54 -0500296 /* Grab the register range from the sdr controller in device tree */
297 mc_vbase = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
298 "altr,sdr-syscon");
299 if (IS_ERR(mc_vbase)) {
300 edac_printk(KERN_ERR, EDAC_MC,
301 "regmap for altr,sdr-syscon lookup failed.\n");
302 return -ENODEV;
303 }
304
Thor Thayer143f4a52015-06-04 09:28:46 -0500305 /* Check specific dependencies for the module */
306 priv = of_match_node(altr_sdram_ctrl_of_match,
307 pdev->dev.of_node)->data;
308
309 /* Validate the SDRAM controller has ECC enabled */
310 if (regmap_read(mc_vbase, priv->ecc_ctrl_offset, &read_reg) ||
311 ((read_reg & priv->ecc_ctl_en_mask) != priv->ecc_ctl_en_mask)) {
Thor Thayer71bcada2014-09-03 10:27:54 -0500312 edac_printk(KERN_ERR, EDAC_MC,
313 "No ECC/ECC disabled [0x%08X]\n", read_reg);
314 return -ENODEV;
315 }
316
317 /* Grab memory size from device tree. */
Thor Thayerf9ae4872015-06-04 09:28:45 -0500318 mem_size = get_total_mem();
Thor Thayer71bcada2014-09-03 10:27:54 -0500319 if (!mem_size) {
Thor Thayerf9ae4872015-06-04 09:28:45 -0500320 edac_printk(KERN_ERR, EDAC_MC, "Unable to calculate memory size\n");
Thor Thayer71bcada2014-09-03 10:27:54 -0500321 return -ENODEV;
322 }
323
Thor Thayer143f4a52015-06-04 09:28:46 -0500324 /* Ensure the SDRAM Interrupt is disabled */
325 if (regmap_update_bits(mc_vbase, priv->ecc_irq_en_offset,
326 priv->ecc_irq_en_mask, 0)) {
Thor Thayer71bcada2014-09-03 10:27:54 -0500327 edac_printk(KERN_ERR, EDAC_MC,
Thor Thayer143f4a52015-06-04 09:28:46 -0500328 "Error disabling SDRAM ECC IRQ\n");
329 return -ENODEV;
330 }
331
332 /* Toggle to clear the SDRAM Error count */
333 if (regmap_update_bits(mc_vbase, priv->ecc_cnt_rst_offset,
334 priv->ecc_cnt_rst_mask,
335 priv->ecc_cnt_rst_mask)) {
336 edac_printk(KERN_ERR, EDAC_MC,
337 "Error clearing SDRAM ECC count\n");
338 return -ENODEV;
339 }
340
341 if (regmap_update_bits(mc_vbase, priv->ecc_cnt_rst_offset,
342 priv->ecc_cnt_rst_mask, 0)) {
343 edac_printk(KERN_ERR, EDAC_MC,
344 "Error clearing SDRAM ECC count\n");
Thor Thayer71bcada2014-09-03 10:27:54 -0500345 return -ENODEV;
346 }
347
348 irq = platform_get_irq(pdev, 0);
349 if (irq < 0) {
350 edac_printk(KERN_ERR, EDAC_MC,
351 "No irq %d in DT\n", irq);
352 return -ENODEV;
353 }
354
Thor Thayer73bcc942015-06-04 09:28:47 -0500355 /* Arria10 has a 2nd IRQ */
356 irq2 = platform_get_irq(pdev, 1);
357
Thor Thayer71bcada2014-09-03 10:27:54 -0500358 layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
359 layers[0].size = 1;
360 layers[0].is_virt_csrow = true;
361 layers[1].type = EDAC_MC_LAYER_CHANNEL;
362 layers[1].size = 1;
363 layers[1].is_virt_csrow = false;
364 mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers,
365 sizeof(struct altr_sdram_mc_data));
366 if (!mci)
367 return -ENOMEM;
368
369 mci->pdev = &pdev->dev;
370 drvdata = mci->pvt_info;
371 drvdata->mc_vbase = mc_vbase;
Thor Thayer143f4a52015-06-04 09:28:46 -0500372 drvdata->data = priv;
Thor Thayer71bcada2014-09-03 10:27:54 -0500373 platform_set_drvdata(pdev, mci);
374
375 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
Thor Thayer143f4a52015-06-04 09:28:46 -0500376 edac_printk(KERN_ERR, EDAC_MC,
377 "Unable to get managed device resource\n");
Thor Thayer71bcada2014-09-03 10:27:54 -0500378 res = -ENOMEM;
379 goto free;
380 }
381
382 mci->mtype_cap = MEM_FLAG_DDR3;
383 mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_SECDED;
384 mci->edac_cap = EDAC_FLAG_SECDED;
385 mci->mod_name = EDAC_MOD_STR;
Thor Thayer71bcada2014-09-03 10:27:54 -0500386 mci->ctl_name = dev_name(&pdev->dev);
387 mci->scrub_mode = SCRUB_SW_SRC;
388 mci->dev_name = dev_name(&pdev->dev);
389
390 dimm = *mci->dimms;
391 dimm->nr_pages = ((mem_size - 1) >> PAGE_SHIFT) + 1;
392 dimm->grain = 8;
393 dimm->dtype = DEV_X8;
394 dimm->mtype = MEM_DDR3;
395 dimm->edac_mode = EDAC_SECDED;
396
397 res = edac_mc_add_mc(mci);
398 if (res < 0)
399 goto err;
400
Thor Thayer73bcc942015-06-04 09:28:47 -0500401 /* Only the Arria10 has separate IRQs */
Thor Thayer08f08bf2018-09-25 08:49:00 -0500402 if (socfpga_is_a10()) {
Thor Thayer73bcc942015-06-04 09:28:47 -0500403 /* Arria10 specific initialization */
404 res = a10_init(mc_vbase);
405 if (res < 0)
406 goto err2;
407
408 res = devm_request_irq(&pdev->dev, irq2,
409 altr_sdram_mc_err_handler,
410 IRQF_SHARED, dev_name(&pdev->dev), mci);
411 if (res < 0) {
412 edac_mc_printk(mci, KERN_ERR,
413 "Unable to request irq %d\n", irq2);
414 res = -ENODEV;
415 goto err2;
416 }
417
418 res = a10_unmask_irq(pdev, A10_DDR0_IRQ_MASK);
419 if (res < 0)
420 goto err2;
421
422 irqflags = IRQF_SHARED;
423 }
424
Thor Thayer71bcada2014-09-03 10:27:54 -0500425 res = devm_request_irq(&pdev->dev, irq, altr_sdram_mc_err_handler,
Thor Thayer73bcc942015-06-04 09:28:47 -0500426 irqflags, dev_name(&pdev->dev), mci);
Thor Thayer71bcada2014-09-03 10:27:54 -0500427 if (res < 0) {
428 edac_mc_printk(mci, KERN_ERR,
429 "Unable to request irq %d\n", irq);
430 res = -ENODEV;
431 goto err2;
432 }
433
Thor Thayer143f4a52015-06-04 09:28:46 -0500434 /* Infrastructure ready - enable the IRQ */
435 if (regmap_update_bits(drvdata->mc_vbase, priv->ecc_irq_en_offset,
436 priv->ecc_irq_en_mask, priv->ecc_irq_en_mask)) {
Thor Thayer71bcada2014-09-03 10:27:54 -0500437 edac_mc_printk(mci, KERN_ERR,
438 "Error enabling SDRAM ECC IRQ\n");
439 res = -ENODEV;
440 goto err2;
441 }
442
443 altr_sdr_mc_create_debugfs_nodes(mci);
444
445 devres_close_group(&pdev->dev, NULL);
446
447 return 0;
448
449err2:
450 edac_mc_del_mc(&pdev->dev);
451err:
452 devres_release_group(&pdev->dev, NULL);
453free:
454 edac_mc_free(mci);
455 edac_printk(KERN_ERR, EDAC_MC,
456 "EDAC Probe Failed; Error %d\n", res);
457
458 return res;
459}
460
461static int altr_sdram_remove(struct platform_device *pdev)
462{
463 struct mem_ctl_info *mci = platform_get_drvdata(pdev);
464
465 edac_mc_del_mc(&pdev->dev);
466 edac_mc_free(mci);
467 platform_set_drvdata(pdev, NULL);
468
469 return 0;
470}
471
Thor Thayer580b5cf2019-02-25 12:56:45 -0600472/*
473 * If you want to suspend, need to disable EDAC by removing it
474 * from the device tree or defconfig.
475 */
476#ifdef CONFIG_PM
477static int altr_sdram_prepare(struct device *dev)
478{
479 pr_err("Suspend not allowed when EDAC is enabled.\n");
480
481 return -EPERM;
482}
483
484static const struct dev_pm_ops altr_sdram_pm_ops = {
485 .prepare = altr_sdram_prepare,
486};
487#endif
488
489static struct platform_driver altr_sdram_edac_driver = {
490 .probe = altr_sdram_probe,
491 .remove = altr_sdram_remove,
492 .driver = {
493 .name = "altr_sdram_edac",
494#ifdef CONFIG_PM
495 .pm = &altr_sdram_pm_ops,
496#endif
497 .of_match_table = altr_sdram_ctrl_of_match,
498 },
499};
500
501module_platform_driver(altr_sdram_edac_driver);
502
503#endif /* CONFIG_EDAC_ALTERA_SDRAM */
504
Thor Thayer3dab6bd2018-04-27 13:37:17 -0500505/**************** Stratix 10 EDAC Memory Controller Functions ************/
506
507/**
508 * s10_protected_reg_write
509 * Write to a protected SMC register.
510 * @context: Not used.
511 * @reg: Address of register
512 * @value: Value to write
513 * Return: INTEL_SIP_SMC_STATUS_OK (0) on success
514 * INTEL_SIP_SMC_REG_ERROR on error
515 * INTEL_SIP_SMC_RETURN_UNKNOWN_FUNCTION if not supported
516 */
517static int s10_protected_reg_write(void *context, unsigned int reg,
518 unsigned int val)
519{
520 struct arm_smccc_res result;
Thor Thayer08f08bf2018-09-25 08:49:00 -0500521 unsigned long offset = (unsigned long)context;
Thor Thayer3dab6bd2018-04-27 13:37:17 -0500522
Thor Thayer08f08bf2018-09-25 08:49:00 -0500523 arm_smccc_smc(INTEL_SIP_SMC_REG_WRITE, offset + reg, val, 0, 0,
Thor Thayer3dab6bd2018-04-27 13:37:17 -0500524 0, 0, 0, &result);
525
526 return (int)result.a0;
527}
528
529/**
530 * s10_protected_reg_read
531 * Read the status of a protected SMC register
532 * @context: Not used.
533 * @reg: Address of register
534 * @value: Value read.
535 * Return: INTEL_SIP_SMC_STATUS_OK (0) on success
536 * INTEL_SIP_SMC_REG_ERROR on error
537 * INTEL_SIP_SMC_RETURN_UNKNOWN_FUNCTION if not supported
538 */
539static int s10_protected_reg_read(void *context, unsigned int reg,
540 unsigned int *val)
541{
542 struct arm_smccc_res result;
Thor Thayer08f08bf2018-09-25 08:49:00 -0500543 unsigned long offset = (unsigned long)context;
Thor Thayer3dab6bd2018-04-27 13:37:17 -0500544
Thor Thayer08f08bf2018-09-25 08:49:00 -0500545 arm_smccc_smc(INTEL_SIP_SMC_REG_READ, offset + reg, 0, 0, 0,
Thor Thayer3dab6bd2018-04-27 13:37:17 -0500546 0, 0, 0, &result);
547
548 *val = (unsigned int)result.a1;
549
550 return (int)result.a0;
551}
552
Thor Thayer3dab6bd2018-04-27 13:37:17 -0500553static const struct regmap_config s10_sdram_regmap_cfg = {
554 .name = "s10_ddr",
555 .reg_bits = 32,
556 .reg_stride = 4,
557 .val_bits = 32,
Thor Thayer08f08bf2018-09-25 08:49:00 -0500558 .max_register = 0xffd12228,
Thor Thayer3dab6bd2018-04-27 13:37:17 -0500559 .reg_read = s10_protected_reg_read,
560 .reg_write = s10_protected_reg_write,
David Frey1c96a2f2018-09-01 09:50:41 -0700561 .use_single_read = true,
562 .use_single_write = true,
Meng Li56d9e7b2019-11-21 12:30:46 -0600563 .fast_io = true,
Thor Thayer3dab6bd2018-04-27 13:37:17 -0500564};
565
Thor Thayer3dab6bd2018-04-27 13:37:17 -0500566/************** </Stratix10 EDAC Memory Controller Functions> ***********/
567
Thor Thayerc3eea192016-02-10 13:26:21 -0600568/************************* EDAC Parent Probe *************************/
569
570static const struct of_device_id altr_edac_device_of_match[];
571
572static const struct of_device_id altr_edac_of_match[] = {
573 { .compatible = "altr,socfpga-ecc-manager" },
574 {},
575};
576MODULE_DEVICE_TABLE(of, altr_edac_of_match);
577
578static int altr_edac_probe(struct platform_device *pdev)
579{
580 of_platform_populate(pdev->dev.of_node, altr_edac_device_of_match,
581 NULL, &pdev->dev);
582 return 0;
583}
584
585static struct platform_driver altr_edac_driver = {
586 .probe = altr_edac_probe,
587 .driver = {
588 .name = "socfpga_ecc_manager",
589 .of_match_table = altr_edac_of_match,
590 },
591};
592module_platform_driver(altr_edac_driver);
593
594/************************* EDAC Device Functions *************************/
595
596/*
597 * EDAC Device Functions (shared between various IPs).
598 * The discrete memories use the EDAC Device framework. The probe
599 * and error handling functions are very similar between memories
600 * so they are shared. The memory allocation and freeing for EDAC
601 * trigger testing are different for each memory.
602 */
603
Thor Thayer1cf70372016-06-22 08:58:54 -0500604static const struct edac_device_prv_data ocramecc_data;
605static const struct edac_device_prv_data l2ecc_data;
606static const struct edac_device_prv_data a10_ocramecc_data;
607static const struct edac_device_prv_data a10_l2ecc_data;
Thor Thayerc3eea192016-02-10 13:26:21 -0600608
Thor Thayerc3eea192016-02-10 13:26:21 -0600609static irqreturn_t altr_edac_device_handler(int irq, void *dev_id)
610{
611 irqreturn_t ret_value = IRQ_NONE;
612 struct edac_device_ctl_info *dci = dev_id;
613 struct altr_edac_device_dev *drvdata = dci->pvt_info;
614 const struct edac_device_prv_data *priv = drvdata->data;
615
616 if (irq == drvdata->sb_irq) {
617 if (priv->ce_clear_mask)
618 writel(priv->ce_clear_mask, drvdata->base);
619 edac_device_handle_ce(dci, 0, 0, drvdata->edac_dev_name);
620 ret_value = IRQ_HANDLED;
621 } else if (irq == drvdata->db_irq) {
622 if (priv->ue_clear_mask)
623 writel(priv->ue_clear_mask, drvdata->base);
624 edac_device_handle_ue(dci, 0, 0, drvdata->edac_dev_name);
625 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
626 ret_value = IRQ_HANDLED;
627 } else {
628 WARN_ON(1);
629 }
630
631 return ret_value;
632}
633
634static ssize_t altr_edac_device_trig(struct file *file,
635 const char __user *user_buf,
636 size_t count, loff_t *ppos)
637
638{
639 u32 *ptemp, i, error_mask;
640 int result = 0;
641 u8 trig_type;
642 unsigned long flags;
643 struct edac_device_ctl_info *edac_dci = file->private_data;
644 struct altr_edac_device_dev *drvdata = edac_dci->pvt_info;
645 const struct edac_device_prv_data *priv = drvdata->data;
646 void *generic_ptr = edac_dci->dev;
647
648 if (!user_buf || get_user(trig_type, user_buf))
649 return -EFAULT;
650
651 if (!priv->alloc_mem)
652 return -ENOMEM;
653
654 /*
655 * Note that generic_ptr is initialized to the device * but in
656 * some alloc_functions, this is overridden and returns data.
657 */
658 ptemp = priv->alloc_mem(priv->trig_alloc_sz, &generic_ptr);
659 if (!ptemp) {
660 edac_printk(KERN_ERR, EDAC_DEVICE,
661 "Inject: Buffer Allocation error\n");
662 return -ENOMEM;
663 }
664
665 if (trig_type == ALTR_UE_TRIGGER_CHAR)
666 error_mask = priv->ue_set_mask;
667 else
668 error_mask = priv->ce_set_mask;
669
670 edac_printk(KERN_ALERT, EDAC_DEVICE,
671 "Trigger Error Mask (0x%X)\n", error_mask);
672
673 local_irq_save(flags);
674 /* write ECC corrupted data out. */
675 for (i = 0; i < (priv->trig_alloc_sz / sizeof(*ptemp)); i++) {
676 /* Read data so we're in the correct state */
677 rmb();
Mark Rutland332efa62017-10-23 14:07:12 -0700678 if (READ_ONCE(ptemp[i]))
Thor Thayerc3eea192016-02-10 13:26:21 -0600679 result = -1;
680 /* Toggle Error bit (it is latched), leave ECC enabled */
Thor Thayer811fce42016-03-21 11:01:42 -0500681 writel(error_mask, (drvdata->base + priv->set_err_ofst));
682 writel(priv->ecc_enable_mask, (drvdata->base +
683 priv->set_err_ofst));
Thor Thayerc3eea192016-02-10 13:26:21 -0600684 ptemp[i] = i;
685 }
686 /* Ensure it has been written out */
687 wmb();
688 local_irq_restore(flags);
689
690 if (result)
691 edac_printk(KERN_ERR, EDAC_DEVICE, "Mem Not Cleared\n");
692
693 /* Read out written data. ECC error caused here */
694 for (i = 0; i < ALTR_TRIGGER_READ_WRD_CNT; i++)
Mark Rutland332efa62017-10-23 14:07:12 -0700695 if (READ_ONCE(ptemp[i]) != i)
Thor Thayerc3eea192016-02-10 13:26:21 -0600696 edac_printk(KERN_ERR, EDAC_DEVICE,
697 "Read doesn't match written data\n");
698
699 if (priv->free_mem)
700 priv->free_mem(ptemp, priv->trig_alloc_sz, generic_ptr);
701
702 return count;
703}
704
705static const struct file_operations altr_edac_device_inject_fops = {
706 .open = simple_open,
707 .write = altr_edac_device_trig,
708 .llseek = generic_file_llseek,
709};
710
Thor Thayerc7b4be82016-04-06 20:22:54 -0500711static ssize_t altr_edac_a10_device_trig(struct file *file,
712 const char __user *user_buf,
713 size_t count, loff_t *ppos);
714
715static const struct file_operations altr_edac_a10_device_inject_fops = {
716 .open = simple_open,
717 .write = altr_edac_a10_device_trig,
718 .llseek = generic_file_llseek,
719};
720
Thor Thayer064acbd2018-09-25 08:49:01 -0500721static ssize_t altr_edac_a10_device_trig2(struct file *file,
722 const char __user *user_buf,
723 size_t count, loff_t *ppos);
724
725static const struct file_operations altr_edac_a10_device_inject2_fops = {
726 .open = simple_open,
727 .write = altr_edac_a10_device_trig2,
728 .llseek = generic_file_llseek,
729};
730
Thor Thayerc3eea192016-02-10 13:26:21 -0600731static void altr_create_edacdev_dbgfs(struct edac_device_ctl_info *edac_dci,
732 const struct edac_device_prv_data *priv)
733{
734 struct altr_edac_device_dev *drvdata = edac_dci->pvt_info;
735
736 if (!IS_ENABLED(CONFIG_EDAC_DEBUG))
737 return;
738
739 drvdata->debugfs_dir = edac_debugfs_create_dir(drvdata->edac_dev_name);
740 if (!drvdata->debugfs_dir)
741 return;
742
Thor Thayerf399f342016-08-19 11:04:25 -0500743 if (!edac_debugfs_create_file("altr_trigger", S_IWUSR,
Thor Thayerc3eea192016-02-10 13:26:21 -0600744 drvdata->debugfs_dir, edac_dci,
Thor Thayere17ced22016-03-31 13:48:01 -0500745 priv->inject_fops))
Thor Thayerc3eea192016-02-10 13:26:21 -0600746 debugfs_remove_recursive(drvdata->debugfs_dir);
747}
748
749static const struct of_device_id altr_edac_device_of_match[] = {
750#ifdef CONFIG_EDAC_ALTERA_L2C
Arnd Bergmann2c911f62016-04-16 22:13:55 +0200751 { .compatible = "altr,socfpga-l2-ecc", .data = &l2ecc_data },
Thor Thayerc3eea192016-02-10 13:26:21 -0600752#endif
753#ifdef CONFIG_EDAC_ALTERA_OCRAM
Arnd Bergmann2c911f62016-04-16 22:13:55 +0200754 { .compatible = "altr,socfpga-ocram-ecc", .data = &ocramecc_data },
Thor Thayerc3eea192016-02-10 13:26:21 -0600755#endif
756 {},
757};
758MODULE_DEVICE_TABLE(of, altr_edac_device_of_match);
759
760/*
761 * altr_edac_device_probe()
762 * This is a generic EDAC device driver that will support
763 * various Altera memory devices such as the L2 cache ECC and
764 * OCRAM ECC as well as the memories for other peripherals.
765 * Module specific initialization is done by passing the
766 * function index in the device tree.
767 */
768static int altr_edac_device_probe(struct platform_device *pdev)
769{
770 struct edac_device_ctl_info *dci;
771 struct altr_edac_device_dev *drvdata;
772 struct resource *r;
773 int res = 0;
774 struct device_node *np = pdev->dev.of_node;
775 char *ecc_name = (char *)np->name;
776 static int dev_instance;
777
778 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
779 edac_printk(KERN_ERR, EDAC_DEVICE,
780 "Unable to open devm\n");
781 return -ENOMEM;
782 }
783
784 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
785 if (!r) {
786 edac_printk(KERN_ERR, EDAC_DEVICE,
787 "Unable to get mem resource\n");
788 res = -ENODEV;
789 goto fail;
790 }
791
792 if (!devm_request_mem_region(&pdev->dev, r->start, resource_size(r),
793 dev_name(&pdev->dev))) {
794 edac_printk(KERN_ERR, EDAC_DEVICE,
795 "%s:Error requesting mem region\n", ecc_name);
796 res = -EBUSY;
797 goto fail;
798 }
799
800 dci = edac_device_alloc_ctl_info(sizeof(*drvdata), ecc_name,
801 1, ecc_name, 1, 0, NULL, 0,
802 dev_instance++);
803
804 if (!dci) {
805 edac_printk(KERN_ERR, EDAC_DEVICE,
806 "%s: Unable to allocate EDAC device\n", ecc_name);
807 res = -ENOMEM;
808 goto fail;
809 }
810
811 drvdata = dci->pvt_info;
812 dci->dev = &pdev->dev;
813 platform_set_drvdata(pdev, dci);
814 drvdata->edac_dev_name = ecc_name;
815
816 drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
Christophe JAILLET8b073d92017-08-16 07:05:06 +0200817 if (!drvdata->base) {
818 res = -ENOMEM;
Thor Thayerc3eea192016-02-10 13:26:21 -0600819 goto fail1;
Christophe JAILLET8b073d92017-08-16 07:05:06 +0200820 }
Thor Thayerc3eea192016-02-10 13:26:21 -0600821
822 /* Get driver specific data for this EDAC device */
823 drvdata->data = of_match_node(altr_edac_device_of_match, np)->data;
824
825 /* Check specific dependencies for the module */
826 if (drvdata->data->setup) {
Thor Thayer328ca7a2016-03-21 11:01:40 -0500827 res = drvdata->data->setup(drvdata);
Thor Thayerc3eea192016-02-10 13:26:21 -0600828 if (res)
829 goto fail1;
830 }
831
832 drvdata->sb_irq = platform_get_irq(pdev, 0);
833 res = devm_request_irq(&pdev->dev, drvdata->sb_irq,
834 altr_edac_device_handler,
835 0, dev_name(&pdev->dev), dci);
836 if (res)
837 goto fail1;
838
839 drvdata->db_irq = platform_get_irq(pdev, 1);
840 res = devm_request_irq(&pdev->dev, drvdata->db_irq,
841 altr_edac_device_handler,
842 0, dev_name(&pdev->dev), dci);
843 if (res)
844 goto fail1;
845
846 dci->mod_name = "Altera ECC Manager";
847 dci->dev_name = drvdata->edac_dev_name;
848
849 res = edac_device_add_device(dci);
850 if (res)
851 goto fail1;
852
853 altr_create_edacdev_dbgfs(dci, drvdata->data);
854
855 devres_close_group(&pdev->dev, NULL);
856
857 return 0;
858
859fail1:
860 edac_device_free_ctl_info(dci);
861fail:
862 devres_release_group(&pdev->dev, NULL);
863 edac_printk(KERN_ERR, EDAC_DEVICE,
864 "%s:Error setting up EDAC device: %d\n", ecc_name, res);
865
866 return res;
867}
868
869static int altr_edac_device_remove(struct platform_device *pdev)
870{
871 struct edac_device_ctl_info *dci = platform_get_drvdata(pdev);
872 struct altr_edac_device_dev *drvdata = dci->pvt_info;
873
874 debugfs_remove_recursive(drvdata->debugfs_dir);
875 edac_device_del_device(&pdev->dev);
876 edac_device_free_ctl_info(dci);
877
878 return 0;
879}
880
881static struct platform_driver altr_edac_device_driver = {
882 .probe = altr_edac_device_probe,
883 .remove = altr_edac_device_remove,
884 .driver = {
885 .name = "altr_edac_device",
886 .of_match_table = altr_edac_device_of_match,
887 },
888};
889module_platform_driver(altr_edac_device_driver);
890
Thor Thayer6b300fb2016-06-22 08:58:55 -0500891/******************* Arria10 Device ECC Shared Functions *****************/
Thor Thayerc3eea192016-02-10 13:26:21 -0600892
Arnd Bergmann1aa6eb52016-04-16 22:13:56 +0200893/*
894 * Test for memory's ECC dependencies upon entry because platform specific
895 * startup should have initialized the memory and enabled the ECC.
896 * Can't turn on ECC here because accessing un-initialized memory will
897 * cause CE/UE errors possibly causing an ABORT.
898 */
Thor Thayer6b300fb2016-06-22 08:58:55 -0500899static int __maybe_unused
900altr_check_ecc_deps(struct altr_edac_device_dev *device)
Arnd Bergmann1aa6eb52016-04-16 22:13:56 +0200901{
902 void __iomem *base = device->base;
903 const struct edac_device_prv_data *prv = device->data;
904
905 if (readl(base + prv->ecc_en_ofst) & prv->ecc_enable_mask)
906 return 0;
907
908 edac_printk(KERN_ERR, EDAC_DEVICE,
909 "%s: No ECC present or ECC disabled.\n",
910 device->edac_dev_name);
911 return -ENODEV;
912}
Thor Thayerc3eea192016-02-10 13:26:21 -0600913
Thor Thayer6b300fb2016-06-22 08:58:55 -0500914static irqreturn_t __maybe_unused altr_edac_a10_ecc_irq(int irq, void *dev_id)
915{
916 struct altr_edac_device_dev *dci = dev_id;
917 void __iomem *base = dci->base;
918
919 if (irq == dci->sb_irq) {
920 writel(ALTR_A10_ECC_SERRPENA,
921 base + ALTR_A10_ECC_INTSTAT_OFST);
922 edac_device_handle_ce(dci->edac_dev, 0, 0, dci->edac_dev_name);
923
924 return IRQ_HANDLED;
925 } else if (irq == dci->db_irq) {
926 writel(ALTR_A10_ECC_DERRPENA,
927 base + ALTR_A10_ECC_INTSTAT_OFST);
928 edac_device_handle_ue(dci->edac_dev, 0, 0, dci->edac_dev_name);
929 if (dci->data->panic)
930 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
931
932 return IRQ_HANDLED;
933 }
934
935 WARN_ON(1);
936
937 return IRQ_NONE;
938}
939
Thor Thayer1166fde92016-06-22 08:58:57 -0500940/******************* Arria10 Memory Buffer Functions *********************/
941
942static inline int a10_get_irq_mask(struct device_node *np)
943{
944 int irq;
945 const u32 *handle = of_get_property(np, "interrupts", NULL);
946
947 if (!handle)
948 return -ENODEV;
949 irq = be32_to_cpup(handle);
950 return irq;
951}
952
953static inline void ecc_set_bits(u32 bit_mask, void __iomem *ioaddr)
954{
955 u32 value = readl(ioaddr);
956
957 value |= bit_mask;
958 writel(value, ioaddr);
959}
960
961static inline void ecc_clear_bits(u32 bit_mask, void __iomem *ioaddr)
962{
963 u32 value = readl(ioaddr);
964
965 value &= ~bit_mask;
966 writel(value, ioaddr);
967}
968
969static inline int ecc_test_bits(u32 bit_mask, void __iomem *ioaddr)
970{
971 u32 value = readl(ioaddr);
972
973 return (value & bit_mask) ? 1 : 0;
974}
975
976/*
977 * This function uses the memory initialization block in the Arria10 ECC
978 * controller to initialize/clear the entire memory data and ECC data.
979 */
980static int __maybe_unused altr_init_memory_port(void __iomem *ioaddr, int port)
981{
982 int limit = ALTR_A10_ECC_INIT_WATCHDOG_10US;
983 u32 init_mask, stat_mask, clear_mask;
984 int ret = 0;
985
986 if (port) {
987 init_mask = ALTR_A10_ECC_INITB;
988 stat_mask = ALTR_A10_ECC_INITCOMPLETEB;
989 clear_mask = ALTR_A10_ECC_ERRPENB_MASK;
990 } else {
991 init_mask = ALTR_A10_ECC_INITA;
992 stat_mask = ALTR_A10_ECC_INITCOMPLETEA;
993 clear_mask = ALTR_A10_ECC_ERRPENA_MASK;
994 }
995
996 ecc_set_bits(init_mask, (ioaddr + ALTR_A10_ECC_CTRL_OFST));
997 while (limit--) {
998 if (ecc_test_bits(stat_mask,
999 (ioaddr + ALTR_A10_ECC_INITSTAT_OFST)))
1000 break;
1001 udelay(1);
1002 }
1003 if (limit < 0)
1004 ret = -EBUSY;
1005
1006 /* Clear any pending ECC interrupts */
1007 writel(clear_mask, (ioaddr + ALTR_A10_ECC_INTSTAT_OFST));
1008
1009 return ret;
1010}
1011
Thor Thayer064acbd2018-09-25 08:49:01 -05001012static int socfpga_is_a10(void)
1013{
1014 return of_machine_is_compatible("altr,socfpga-arria10");
1015}
1016
Thor Thayer1166fde92016-06-22 08:58:57 -05001017static __init int __maybe_unused
1018altr_init_a10_ecc_block(struct device_node *np, u32 irq_mask,
1019 u32 ecc_ctrl_en_mask, bool dual_port)
1020{
1021 int ret = 0;
1022 void __iomem *ecc_block_base;
1023 struct regmap *ecc_mgr_map;
1024 char *ecc_name;
1025 struct device_node *np_eccmgr;
1026
1027 ecc_name = (char *)np->name;
1028
1029 /* Get the ECC Manager - parent of the device EDACs */
1030 np_eccmgr = of_get_parent(np);
Thor Thayer064acbd2018-09-25 08:49:01 -05001031
1032 if (socfpga_is_a10()) {
1033 ecc_mgr_map = syscon_regmap_lookup_by_phandle(np_eccmgr,
1034 "altr,sysmgr-syscon");
1035 } else {
1036 struct device_node *sysmgr_np;
1037 struct resource res;
Arnd Bergmann8537bf102018-09-27 12:09:26 +02001038 uintptr_t base;
Thor Thayer064acbd2018-09-25 08:49:01 -05001039
1040 sysmgr_np = of_parse_phandle(np_eccmgr,
1041 "altr,sysmgr-syscon", 0);
1042 if (!sysmgr_np) {
1043 edac_printk(KERN_ERR, EDAC_DEVICE,
1044 "Unable to find altr,sysmgr-syscon\n");
1045 return -ENODEV;
1046 }
1047
Huang Zijiang7f736592019-02-14 14:39:07 +08001048 if (of_address_to_resource(sysmgr_np, 0, &res)) {
1049 of_node_put(sysmgr_np);
Thor Thayer064acbd2018-09-25 08:49:01 -05001050 return -ENOMEM;
Huang Zijiang7f736592019-02-14 14:39:07 +08001051 }
Thor Thayer064acbd2018-09-25 08:49:01 -05001052
1053 /* Need physical address for SMCC call */
Arnd Bergmann8537bf102018-09-27 12:09:26 +02001054 base = res.start;
Thor Thayer064acbd2018-09-25 08:49:01 -05001055
Arnd Bergmann8537bf102018-09-27 12:09:26 +02001056 ecc_mgr_map = regmap_init(NULL, NULL, (void *)base,
Thor Thayer064acbd2018-09-25 08:49:01 -05001057 &s10_sdram_regmap_cfg);
Huang Zijiang7f736592019-02-14 14:39:07 +08001058 of_node_put(sysmgr_np);
Thor Thayer064acbd2018-09-25 08:49:01 -05001059 }
Thor Thayer1166fde92016-06-22 08:58:57 -05001060 of_node_put(np_eccmgr);
1061 if (IS_ERR(ecc_mgr_map)) {
1062 edac_printk(KERN_ERR, EDAC_DEVICE,
1063 "Unable to get syscon altr,sysmgr-syscon\n");
1064 return -ENODEV;
1065 }
1066
1067 /* Map the ECC Block */
1068 ecc_block_base = of_iomap(np, 0);
1069 if (!ecc_block_base) {
1070 edac_printk(KERN_ERR, EDAC_DEVICE,
1071 "Unable to map %s ECC block\n", ecc_name);
1072 return -ENODEV;
1073 }
1074
1075 /* Disable ECC */
1076 regmap_write(ecc_mgr_map, A10_SYSMGR_ECC_INTMASK_SET_OFST, irq_mask);
1077 writel(ALTR_A10_ECC_SERRINTEN,
1078 (ecc_block_base + ALTR_A10_ECC_ERRINTENR_OFST));
1079 ecc_clear_bits(ecc_ctrl_en_mask,
1080 (ecc_block_base + ALTR_A10_ECC_CTRL_OFST));
1081 /* Ensure all writes complete */
1082 wmb();
1083 /* Use HW initialization block to initialize memory for ECC */
1084 ret = altr_init_memory_port(ecc_block_base, 0);
1085 if (ret) {
1086 edac_printk(KERN_ERR, EDAC_DEVICE,
1087 "ECC: cannot init %s PORTA memory\n", ecc_name);
1088 goto out;
1089 }
1090
1091 if (dual_port) {
1092 ret = altr_init_memory_port(ecc_block_base, 1);
1093 if (ret) {
1094 edac_printk(KERN_ERR, EDAC_DEVICE,
1095 "ECC: cannot init %s PORTB memory\n",
1096 ecc_name);
1097 goto out;
1098 }
1099 }
1100
1101 /* Interrupt mode set to every SBERR */
1102 regmap_write(ecc_mgr_map, ALTR_A10_ECC_INTMODE_OFST,
1103 ALTR_A10_ECC_INTMODE);
1104 /* Enable ECC */
1105 ecc_set_bits(ecc_ctrl_en_mask, (ecc_block_base +
1106 ALTR_A10_ECC_CTRL_OFST));
1107 writel(ALTR_A10_ECC_SERRINTEN,
1108 (ecc_block_base + ALTR_A10_ECC_ERRINTENS_OFST));
1109 regmap_write(ecc_mgr_map, A10_SYSMGR_ECC_INTMASK_CLR_OFST, irq_mask);
1110 /* Ensure all writes complete */
1111 wmb();
1112out:
1113 iounmap(ecc_block_base);
1114 return ret;
1115}
1116
1117static int validate_parent_available(struct device_node *np);
1118static const struct of_device_id altr_edac_a10_device_of_match[];
1119static int __init __maybe_unused altr_init_a10_ecc_device_type(char *compat)
1120{
1121 int irq;
Thor Thayer25b223d2017-04-05 13:01:02 -05001122 struct device_node *child, *np;
1123
Thor Thayer25b223d2017-04-05 13:01:02 -05001124 np = of_find_compatible_node(NULL, NULL,
1125 "altr,socfpga-a10-ecc-manager");
Thor Thayer1166fde92016-06-22 08:58:57 -05001126 if (!np) {
1127 edac_printk(KERN_ERR, EDAC_DEVICE, "ECC Manager not found\n");
1128 return -ENODEV;
1129 }
1130
1131 for_each_child_of_node(np, child) {
1132 const struct of_device_id *pdev_id;
1133 const struct edac_device_prv_data *prv;
1134
1135 if (!of_device_is_available(child))
1136 continue;
1137 if (!of_device_is_compatible(child, compat))
1138 continue;
1139
1140 if (validate_parent_available(child))
1141 continue;
1142
1143 irq = a10_get_irq_mask(child);
1144 if (irq < 0)
1145 continue;
1146
1147 /* Get matching node and check for valid result */
1148 pdev_id = of_match_node(altr_edac_a10_device_of_match, child);
1149 if (IS_ERR_OR_NULL(pdev_id))
1150 continue;
1151
1152 /* Validate private data pointer before dereferencing */
1153 prv = pdev_id->data;
1154 if (!prv)
1155 continue;
1156
1157 altr_init_a10_ecc_block(child, BIT(irq),
1158 prv->ecc_enable_mask, 0);
1159 }
1160
1161 of_node_put(np);
1162 return 0;
1163}
1164
Thor Thayer3123c5c2019-07-12 13:28:43 -05001165/*********************** SDRAM EDAC Device Functions *********************/
1166
1167#ifdef CONFIG_EDAC_ALTERA_SDRAM
1168
1169static const struct edac_device_prv_data s10_sdramecc_data = {
1170 .setup = altr_check_ecc_deps,
1171 .ce_clear_mask = ALTR_S10_ECC_SERRPENA,
1172 .ue_clear_mask = ALTR_S10_ECC_DERRPENA,
1173 .ecc_enable_mask = ALTR_S10_ECC_EN,
1174 .ecc_en_ofst = ALTR_S10_ECC_CTRL_SDRAM_OFST,
1175 .ce_set_mask = ALTR_S10_ECC_TSERRA,
1176 .ue_set_mask = ALTR_S10_ECC_TDERRA,
1177 .set_err_ofst = ALTR_S10_ECC_INTTEST_OFST,
1178 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1179 .inject_fops = &altr_edac_a10_device_inject_fops,
1180};
1181#endif /* CONFIG_EDAC_ALTERA_SDRAM */
1182
Thor Thayer6b300fb2016-06-22 08:58:55 -05001183/*********************** OCRAM EDAC Device Functions *********************/
1184
1185#ifdef CONFIG_EDAC_ALTERA_OCRAM
1186
Thor Thayerc3eea192016-02-10 13:26:21 -06001187static void *ocram_alloc_mem(size_t size, void **other)
1188{
1189 struct device_node *np;
1190 struct gen_pool *gp;
1191 void *sram_addr;
1192
1193 np = of_find_compatible_node(NULL, NULL, "altr,socfpga-ocram-ecc");
1194 if (!np)
1195 return NULL;
1196
1197 gp = of_gen_pool_get(np, "iram", 0);
1198 of_node_put(np);
1199 if (!gp)
1200 return NULL;
1201
1202 sram_addr = (void *)gen_pool_alloc(gp, size);
1203 if (!sram_addr)
1204 return NULL;
1205
1206 memset(sram_addr, 0, size);
1207 /* Ensure data is written out */
1208 wmb();
1209
1210 /* Remember this handle for freeing later */
1211 *other = gp;
1212
1213 return sram_addr;
1214}
1215
1216static void ocram_free_mem(void *p, size_t size, void *other)
1217{
Thor Thayer9ef20752018-05-14 12:04:01 -05001218 gen_pool_free((struct gen_pool *)other, (unsigned long)p, size);
Thor Thayerc3eea192016-02-10 13:26:21 -06001219}
1220
Thor Thayer1cf70372016-06-22 08:58:54 -05001221static const struct edac_device_prv_data ocramecc_data = {
Thor Thayeraa1f06d2016-03-31 13:48:03 -05001222 .setup = altr_check_ecc_deps,
Thor Thayerc3eea192016-02-10 13:26:21 -06001223 .ce_clear_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_SERR),
1224 .ue_clear_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_DERR),
Thor Thayerc3eea192016-02-10 13:26:21 -06001225 .alloc_mem = ocram_alloc_mem,
1226 .free_mem = ocram_free_mem,
1227 .ecc_enable_mask = ALTR_OCR_ECC_EN,
Thor Thayer943ad912016-03-31 13:48:02 -05001228 .ecc_en_ofst = ALTR_OCR_ECC_REG_OFFSET,
Thor Thayerc3eea192016-02-10 13:26:21 -06001229 .ce_set_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_INJS),
1230 .ue_set_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_INJD),
Thor Thayer811fce42016-03-21 11:01:42 -05001231 .set_err_ofst = ALTR_OCR_ECC_REG_OFFSET,
Thor Thayerc3eea192016-02-10 13:26:21 -06001232 .trig_alloc_sz = ALTR_TRIG_OCRAM_BYTE_SIZE,
Thor Thayere17ced22016-03-31 13:48:01 -05001233 .inject_fops = &altr_edac_device_inject_fops,
Thor Thayerc3eea192016-02-10 13:26:21 -06001234};
1235
Thor Thayer17e47dc2019-04-23 09:36:34 -05001236static int __maybe_unused
1237altr_check_ocram_deps_init(struct altr_edac_device_dev *device)
1238{
1239 void __iomem *base = device->base;
1240 int ret;
1241
1242 ret = altr_check_ecc_deps(device);
1243 if (ret)
1244 return ret;
1245
1246 /* Verify OCRAM has been initialized */
1247 if (!ecc_test_bits(ALTR_A10_ECC_INITCOMPLETEA,
1248 (base + ALTR_A10_ECC_INITSTAT_OFST)))
1249 return -ENODEV;
1250
1251 /* Enable IRQ on Single Bit Error */
1252 writel(ALTR_A10_ECC_SERRINTEN, (base + ALTR_A10_ECC_ERRINTENS_OFST));
1253 /* Ensure all writes complete */
1254 wmb();
1255
1256 return 0;
1257}
1258
Thor Thayer1cf70372016-06-22 08:58:54 -05001259static const struct edac_device_prv_data a10_ocramecc_data = {
Thor Thayer17e47dc2019-04-23 09:36:34 -05001260 .setup = altr_check_ocram_deps_init,
Thor Thayerc7b4be82016-04-06 20:22:54 -05001261 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1262 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
1263 .irq_status_mask = A10_SYSMGR_ECC_INTSTAT_OCRAM,
Thor Thayerc7b4be82016-04-06 20:22:54 -05001264 .ecc_enable_mask = ALTR_A10_OCRAM_ECC_EN_CTL,
1265 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1266 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1267 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1268 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1269 .ecc_irq_handler = altr_edac_a10_ecc_irq,
Thor Thayer17e47dc2019-04-23 09:36:34 -05001270 .inject_fops = &altr_edac_a10_device_inject2_fops,
Thor Thayer2b083d62016-06-22 08:58:53 -05001271 /*
1272 * OCRAM panic on uncorrectable error because sleep/resume
1273 * functions and FPGA contents are stored in OCRAM. Prefer
1274 * a kernel panic over executing/loading corrupted data.
1275 */
1276 .panic = true,
Thor Thayerc7b4be82016-04-06 20:22:54 -05001277};
1278
Thor Thayerc3eea192016-02-10 13:26:21 -06001279#endif /* CONFIG_EDAC_ALTERA_OCRAM */
1280
1281/********************* L2 Cache EDAC Device Functions ********************/
1282
1283#ifdef CONFIG_EDAC_ALTERA_L2C
1284
1285static void *l2_alloc_mem(size_t size, void **other)
1286{
1287 struct device *dev = *other;
1288 void *ptemp = devm_kzalloc(dev, size, GFP_KERNEL);
1289
1290 if (!ptemp)
1291 return NULL;
1292
1293 /* Make sure everything is written out */
1294 wmb();
1295
1296 /*
1297 * Clean all cache levels up to LoC (includes L2)
1298 * This ensures the corrupted data is written into
1299 * L2 cache for readback test (which causes ECC error).
1300 */
1301 flush_cache_all();
1302
1303 return ptemp;
1304}
1305
1306static void l2_free_mem(void *p, size_t size, void *other)
1307{
1308 struct device *dev = other;
1309
1310 if (dev && p)
1311 devm_kfree(dev, p);
1312}
1313
1314/*
1315 * altr_l2_check_deps()
1316 * Test for L2 cache ECC dependencies upon entry because
1317 * platform specific startup should have initialized the L2
1318 * memory and enabled the ECC.
1319 * Bail if ECC is not enabled.
1320 * Note that L2 Cache Enable is forced at build time.
1321 */
Thor Thayer328ca7a2016-03-21 11:01:40 -05001322static int altr_l2_check_deps(struct altr_edac_device_dev *device)
Thor Thayerc3eea192016-02-10 13:26:21 -06001323{
Thor Thayer328ca7a2016-03-21 11:01:40 -05001324 void __iomem *base = device->base;
Thor Thayer27439a12016-03-21 11:01:41 -05001325 const struct edac_device_prv_data *prv = device->data;
1326
1327 if ((readl(base) & prv->ecc_enable_mask) ==
1328 prv->ecc_enable_mask)
Thor Thayerc3eea192016-02-10 13:26:21 -06001329 return 0;
1330
1331 edac_printk(KERN_ERR, EDAC_DEVICE,
1332 "L2: No ECC present, or ECC disabled\n");
1333 return -ENODEV;
1334}
1335
Thor Thayer13ab8442016-06-07 15:35:57 -05001336static irqreturn_t altr_edac_a10_l2_irq(int irq, void *dev_id)
Thor Thayer588cb032016-03-21 11:01:44 -05001337{
Thor Thayer13ab8442016-06-07 15:35:57 -05001338 struct altr_edac_device_dev *dci = dev_id;
1339
1340 if (irq == dci->sb_irq) {
Thor Thayer588cb032016-03-21 11:01:44 -05001341 regmap_write(dci->edac->ecc_mgr_map,
1342 A10_SYSGMR_MPU_CLEAR_L2_ECC_OFST,
1343 A10_SYSGMR_MPU_CLEAR_L2_ECC_SB);
1344 edac_device_handle_ce(dci->edac_dev, 0, 0, dci->edac_dev_name);
Thor Thayer13ab8442016-06-07 15:35:57 -05001345
1346 return IRQ_HANDLED;
1347 } else if (irq == dci->db_irq) {
Thor Thayer588cb032016-03-21 11:01:44 -05001348 regmap_write(dci->edac->ecc_mgr_map,
1349 A10_SYSGMR_MPU_CLEAR_L2_ECC_OFST,
1350 A10_SYSGMR_MPU_CLEAR_L2_ECC_MB);
1351 edac_device_handle_ue(dci->edac_dev, 0, 0, dci->edac_dev_name);
1352 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
Thor Thayer13ab8442016-06-07 15:35:57 -05001353
1354 return IRQ_HANDLED;
Thor Thayer588cb032016-03-21 11:01:44 -05001355 }
Thor Thayer13ab8442016-06-07 15:35:57 -05001356
1357 WARN_ON(1);
1358
1359 return IRQ_NONE;
Thor Thayer588cb032016-03-21 11:01:44 -05001360}
1361
Thor Thayer1cf70372016-06-22 08:58:54 -05001362static const struct edac_device_prv_data l2ecc_data = {
Thor Thayerc3eea192016-02-10 13:26:21 -06001363 .setup = altr_l2_check_deps,
1364 .ce_clear_mask = 0,
1365 .ue_clear_mask = 0,
Thor Thayerc3eea192016-02-10 13:26:21 -06001366 .alloc_mem = l2_alloc_mem,
1367 .free_mem = l2_free_mem,
1368 .ecc_enable_mask = ALTR_L2_ECC_EN,
1369 .ce_set_mask = (ALTR_L2_ECC_EN | ALTR_L2_ECC_INJS),
1370 .ue_set_mask = (ALTR_L2_ECC_EN | ALTR_L2_ECC_INJD),
Thor Thayer811fce42016-03-21 11:01:42 -05001371 .set_err_ofst = ALTR_L2_ECC_REG_OFFSET,
Thor Thayerc3eea192016-02-10 13:26:21 -06001372 .trig_alloc_sz = ALTR_TRIG_L2C_BYTE_SIZE,
Thor Thayere17ced22016-03-31 13:48:01 -05001373 .inject_fops = &altr_edac_device_inject_fops,
Thor Thayerc3eea192016-02-10 13:26:21 -06001374};
1375
Thor Thayer1cf70372016-06-22 08:58:54 -05001376static const struct edac_device_prv_data a10_l2ecc_data = {
Thor Thayer588cb032016-03-21 11:01:44 -05001377 .setup = altr_l2_check_deps,
1378 .ce_clear_mask = ALTR_A10_L2_ECC_SERR_CLR,
1379 .ue_clear_mask = ALTR_A10_L2_ECC_MERR_CLR,
1380 .irq_status_mask = A10_SYSMGR_ECC_INTSTAT_L2,
Thor Thayer588cb032016-03-21 11:01:44 -05001381 .alloc_mem = l2_alloc_mem,
1382 .free_mem = l2_free_mem,
1383 .ecc_enable_mask = ALTR_A10_L2_ECC_EN_CTL,
1384 .ce_set_mask = ALTR_A10_L2_ECC_CE_INJ_MASK,
1385 .ue_set_mask = ALTR_A10_L2_ECC_UE_INJ_MASK,
1386 .set_err_ofst = ALTR_A10_L2_ECC_INJ_OFST,
1387 .ecc_irq_handler = altr_edac_a10_l2_irq,
1388 .trig_alloc_sz = ALTR_TRIG_L2C_BYTE_SIZE,
Thor Thayere17ced22016-03-31 13:48:01 -05001389 .inject_fops = &altr_edac_device_inject_fops,
Thor Thayer588cb032016-03-21 11:01:44 -05001390};
1391
Thor Thayerc3eea192016-02-10 13:26:21 -06001392#endif /* CONFIG_EDAC_ALTERA_L2C */
1393
Thor Thayerab8c1e02016-06-22 08:58:58 -05001394/********************* Ethernet Device Functions ********************/
1395
1396#ifdef CONFIG_EDAC_ALTERA_ETHERNET
1397
Thor Thayer788586e2019-03-26 16:29:31 -05001398static int __init socfpga_init_ethernet_ecc(struct altr_edac_device_dev *dev)
1399{
1400 int ret;
1401
1402 ret = altr_init_a10_ecc_device_type("altr,socfpga-eth-mac-ecc");
1403 if (ret)
1404 return ret;
1405
1406 return altr_check_ecc_deps(dev);
1407}
1408
Thor Thayerab8c1e02016-06-22 08:58:58 -05001409static const struct edac_device_prv_data a10_enetecc_data = {
Thor Thayer788586e2019-03-26 16:29:31 -05001410 .setup = socfpga_init_ethernet_ecc,
Thor Thayerab8c1e02016-06-22 08:58:58 -05001411 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1412 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
Thor Thayerab8c1e02016-06-22 08:58:58 -05001413 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1414 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1415 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1416 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1417 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1418 .ecc_irq_handler = altr_edac_a10_ecc_irq,
Thor Thayer064acbd2018-09-25 08:49:01 -05001419 .inject_fops = &altr_edac_a10_device_inject2_fops,
Thor Thayerab8c1e02016-06-22 08:58:58 -05001420};
1421
Thor Thayerab8c1e02016-06-22 08:58:58 -05001422#endif /* CONFIG_EDAC_ALTERA_ETHERNET */
1423
Thor Thayerc6882fb2016-07-14 11:06:43 -05001424/********************** NAND Device Functions **********************/
1425
1426#ifdef CONFIG_EDAC_ALTERA_NAND
1427
Thor Thayer788586e2019-03-26 16:29:31 -05001428static int __init socfpga_init_nand_ecc(struct altr_edac_device_dev *device)
1429{
1430 int ret;
1431
1432 ret = altr_init_a10_ecc_device_type("altr,socfpga-nand-ecc");
1433 if (ret)
1434 return ret;
1435
1436 return altr_check_ecc_deps(device);
1437}
1438
Thor Thayerc6882fb2016-07-14 11:06:43 -05001439static const struct edac_device_prv_data a10_nandecc_data = {
Thor Thayer788586e2019-03-26 16:29:31 -05001440 .setup = socfpga_init_nand_ecc,
Thor Thayerc6882fb2016-07-14 11:06:43 -05001441 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1442 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
Thor Thayerc6882fb2016-07-14 11:06:43 -05001443 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1444 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1445 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1446 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1447 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1448 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1449 .inject_fops = &altr_edac_a10_device_inject_fops,
1450};
1451
Thor Thayerc6882fb2016-07-14 11:06:43 -05001452#endif /* CONFIG_EDAC_ALTERA_NAND */
1453
Thor Thayere8263792016-07-28 10:03:57 +02001454/********************** DMA Device Functions **********************/
1455
1456#ifdef CONFIG_EDAC_ALTERA_DMA
1457
Thor Thayer788586e2019-03-26 16:29:31 -05001458static int __init socfpga_init_dma_ecc(struct altr_edac_device_dev *device)
1459{
1460 int ret;
1461
1462 ret = altr_init_a10_ecc_device_type("altr,socfpga-dma-ecc");
1463 if (ret)
1464 return ret;
1465
1466 return altr_check_ecc_deps(device);
1467}
1468
Thor Thayere8263792016-07-28 10:03:57 +02001469static const struct edac_device_prv_data a10_dmaecc_data = {
Thor Thayer788586e2019-03-26 16:29:31 -05001470 .setup = socfpga_init_dma_ecc,
Thor Thayere8263792016-07-28 10:03:57 +02001471 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1472 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
Thor Thayere8263792016-07-28 10:03:57 +02001473 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1474 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1475 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1476 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1477 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1478 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1479 .inject_fops = &altr_edac_a10_device_inject_fops,
1480};
1481
Thor Thayere8263792016-07-28 10:03:57 +02001482#endif /* CONFIG_EDAC_ALTERA_DMA */
1483
Thor Thayerc6095812016-07-14 11:06:45 -05001484/********************** USB Device Functions **********************/
1485
1486#ifdef CONFIG_EDAC_ALTERA_USB
1487
Thor Thayer788586e2019-03-26 16:29:31 -05001488static int __init socfpga_init_usb_ecc(struct altr_edac_device_dev *device)
1489{
1490 int ret;
1491
1492 ret = altr_init_a10_ecc_device_type("altr,socfpga-usb-ecc");
1493 if (ret)
1494 return ret;
1495
1496 return altr_check_ecc_deps(device);
1497}
1498
Thor Thayerc6095812016-07-14 11:06:45 -05001499static const struct edac_device_prv_data a10_usbecc_data = {
Thor Thayer788586e2019-03-26 16:29:31 -05001500 .setup = socfpga_init_usb_ecc,
Thor Thayerc6095812016-07-14 11:06:45 -05001501 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1502 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
Thor Thayerc6095812016-07-14 11:06:45 -05001503 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1504 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1505 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1506 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1507 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1508 .ecc_irq_handler = altr_edac_a10_ecc_irq,
Thor Thayer064acbd2018-09-25 08:49:01 -05001509 .inject_fops = &altr_edac_a10_device_inject2_fops,
Thor Thayerc6095812016-07-14 11:06:45 -05001510};
1511
Thor Thayerc6095812016-07-14 11:06:45 -05001512#endif /* CONFIG_EDAC_ALTERA_USB */
1513
Thor Thayer485fe9e2016-07-14 11:06:46 -05001514/********************** QSPI Device Functions **********************/
1515
1516#ifdef CONFIG_EDAC_ALTERA_QSPI
1517
Thor Thayer788586e2019-03-26 16:29:31 -05001518static int __init socfpga_init_qspi_ecc(struct altr_edac_device_dev *device)
1519{
1520 int ret;
1521
1522 ret = altr_init_a10_ecc_device_type("altr,socfpga-qspi-ecc");
1523 if (ret)
1524 return ret;
1525
1526 return altr_check_ecc_deps(device);
1527}
1528
Thor Thayer485fe9e2016-07-14 11:06:46 -05001529static const struct edac_device_prv_data a10_qspiecc_data = {
Thor Thayer788586e2019-03-26 16:29:31 -05001530 .setup = socfpga_init_qspi_ecc,
Thor Thayer485fe9e2016-07-14 11:06:46 -05001531 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1532 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
Thor Thayer485fe9e2016-07-14 11:06:46 -05001533 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1534 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1535 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1536 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1537 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1538 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1539 .inject_fops = &altr_edac_a10_device_inject_fops,
1540};
1541
Thor Thayer485fe9e2016-07-14 11:06:46 -05001542#endif /* CONFIG_EDAC_ALTERA_QSPI */
1543
Thor Thayer91104982016-08-09 09:40:52 -05001544/********************* SDMMC Device Functions **********************/
1545
1546#ifdef CONFIG_EDAC_ALTERA_SDMMC
1547
1548static const struct edac_device_prv_data a10_sdmmceccb_data;
1549static int altr_portb_setup(struct altr_edac_device_dev *device)
1550{
1551 struct edac_device_ctl_info *dci;
1552 struct altr_edac_device_dev *altdev;
1553 char *ecc_name = "sdmmcb-ecc";
1554 int edac_idx, rc;
1555 struct device_node *np;
1556 const struct edac_device_prv_data *prv = &a10_sdmmceccb_data;
1557
1558 rc = altr_check_ecc_deps(device);
1559 if (rc)
1560 return rc;
1561
1562 np = of_find_compatible_node(NULL, NULL, "altr,socfpga-sdmmc-ecc");
1563 if (!np) {
1564 edac_printk(KERN_WARNING, EDAC_DEVICE, "SDMMC node not found\n");
1565 return -ENODEV;
1566 }
1567
1568 /* Create the PortB EDAC device */
1569 edac_idx = edac_device_alloc_index();
1570 dci = edac_device_alloc_ctl_info(sizeof(*altdev), ecc_name, 1,
1571 ecc_name, 1, 0, NULL, 0, edac_idx);
1572 if (!dci) {
1573 edac_printk(KERN_ERR, EDAC_DEVICE,
1574 "%s: Unable to allocate PortB EDAC device\n",
1575 ecc_name);
1576 return -ENOMEM;
1577 }
1578
1579 /* Initialize the PortB EDAC device structure from PortA structure */
1580 altdev = dci->pvt_info;
1581 *altdev = *device;
1582
1583 if (!devres_open_group(&altdev->ddev, altr_portb_setup, GFP_KERNEL))
1584 return -ENOMEM;
1585
1586 /* Update PortB specific values */
1587 altdev->edac_dev_name = ecc_name;
1588 altdev->edac_idx = edac_idx;
1589 altdev->edac_dev = dci;
1590 altdev->data = prv;
1591 dci->dev = &altdev->ddev;
1592 dci->ctl_name = "Altera ECC Manager";
1593 dci->mod_name = ecc_name;
1594 dci->dev_name = ecc_name;
1595
Thor Thayera428b4d2019-04-23 09:36:36 -05001596 /* Update the PortB IRQs - A10 has 4, S10 has 2, Index accordingly */
1597#ifdef CONFIG_ARCH_STRATIX10
1598 altdev->sb_irq = irq_of_parse_and_map(np, 1);
1599#else
Thor Thayer91104982016-08-09 09:40:52 -05001600 altdev->sb_irq = irq_of_parse_and_map(np, 2);
Thor Thayera428b4d2019-04-23 09:36:36 -05001601#endif
Thor Thayer91104982016-08-09 09:40:52 -05001602 if (!altdev->sb_irq) {
1603 edac_printk(KERN_ERR, EDAC_DEVICE, "Error PortB SBIRQ alloc\n");
1604 rc = -ENODEV;
1605 goto err_release_group_1;
1606 }
1607 rc = devm_request_irq(&altdev->ddev, altdev->sb_irq,
1608 prv->ecc_irq_handler,
Thor Thayera29d64a2016-09-22 17:13:39 -05001609 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
1610 ecc_name, altdev);
Thor Thayer91104982016-08-09 09:40:52 -05001611 if (rc) {
1612 edac_printk(KERN_ERR, EDAC_DEVICE, "PortB SBERR IRQ error\n");
1613 goto err_release_group_1;
1614 }
1615
Thor Thayera428b4d2019-04-23 09:36:36 -05001616#ifdef CONFIG_ARCH_STRATIX10
1617 /* Use IRQ to determine SError origin instead of assigning IRQ */
1618 rc = of_property_read_u32_index(np, "interrupts", 1, &altdev->db_irq);
1619 if (rc) {
1620 edac_printk(KERN_ERR, EDAC_DEVICE,
1621 "Error PortB DBIRQ alloc\n");
1622 goto err_release_group_1;
1623 }
1624#else
Thor Thayer91104982016-08-09 09:40:52 -05001625 altdev->db_irq = irq_of_parse_and_map(np, 3);
1626 if (!altdev->db_irq) {
1627 edac_printk(KERN_ERR, EDAC_DEVICE, "Error PortB DBIRQ alloc\n");
1628 rc = -ENODEV;
1629 goto err_release_group_1;
1630 }
1631 rc = devm_request_irq(&altdev->ddev, altdev->db_irq,
1632 prv->ecc_irq_handler,
Thor Thayera29d64a2016-09-22 17:13:39 -05001633 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
1634 ecc_name, altdev);
Thor Thayer91104982016-08-09 09:40:52 -05001635 if (rc) {
1636 edac_printk(KERN_ERR, EDAC_DEVICE, "PortB DBERR IRQ error\n");
1637 goto err_release_group_1;
1638 }
Thor Thayera428b4d2019-04-23 09:36:36 -05001639#endif
Thor Thayer91104982016-08-09 09:40:52 -05001640
1641 rc = edac_device_add_device(dci);
1642 if (rc) {
1643 edac_printk(KERN_ERR, EDAC_DEVICE,
1644 "edac_device_add_device portB failed\n");
1645 rc = -ENOMEM;
1646 goto err_release_group_1;
1647 }
1648 altr_create_edacdev_dbgfs(dci, prv);
1649
1650 list_add(&altdev->next, &altdev->edac->a10_ecc_devices);
1651
1652 devres_remove_group(&altdev->ddev, altr_portb_setup);
1653
1654 return 0;
1655
1656err_release_group_1:
1657 edac_device_free_ctl_info(dci);
1658 devres_release_group(&altdev->ddev, altr_portb_setup);
1659 edac_printk(KERN_ERR, EDAC_DEVICE,
1660 "%s:Error setting up EDAC device: %d\n", ecc_name, rc);
1661 return rc;
1662}
1663
Thor Thayer788586e2019-03-26 16:29:31 -05001664static int __init socfpga_init_sdmmc_ecc(struct altr_edac_device_dev *device)
1665{
1666 int rc = -ENODEV;
1667 struct device_node *child;
1668
1669 child = of_find_compatible_node(NULL, NULL, "altr,socfpga-sdmmc-ecc");
1670 if (!child)
1671 return -ENODEV;
1672
1673 if (!of_device_is_available(child))
1674 goto exit;
1675
1676 if (validate_parent_available(child))
1677 goto exit;
1678
1679 /* Init portB */
1680 rc = altr_init_a10_ecc_block(child, ALTR_A10_SDMMC_IRQ_MASK,
1681 a10_sdmmceccb_data.ecc_enable_mask, 1);
1682 if (rc)
1683 goto exit;
1684
1685 /* Setup portB */
1686 return altr_portb_setup(device);
1687
1688exit:
1689 of_node_put(child);
1690 return rc;
1691}
1692
Thor Thayer91104982016-08-09 09:40:52 -05001693static irqreturn_t altr_edac_a10_ecc_irq_portb(int irq, void *dev_id)
1694{
1695 struct altr_edac_device_dev *ad = dev_id;
1696 void __iomem *base = ad->base;
1697 const struct edac_device_prv_data *priv = ad->data;
1698
1699 if (irq == ad->sb_irq) {
1700 writel(priv->ce_clear_mask,
1701 base + ALTR_A10_ECC_INTSTAT_OFST);
1702 edac_device_handle_ce(ad->edac_dev, 0, 0, ad->edac_dev_name);
1703 return IRQ_HANDLED;
1704 } else if (irq == ad->db_irq) {
1705 writel(priv->ue_clear_mask,
1706 base + ALTR_A10_ECC_INTSTAT_OFST);
1707 edac_device_handle_ue(ad->edac_dev, 0, 0, ad->edac_dev_name);
1708 return IRQ_HANDLED;
1709 }
1710
1711 WARN_ONCE(1, "Unhandled IRQ%d on Port B.", irq);
1712
1713 return IRQ_NONE;
1714}
1715
1716static const struct edac_device_prv_data a10_sdmmcecca_data = {
Thor Thayer788586e2019-03-26 16:29:31 -05001717 .setup = socfpga_init_sdmmc_ecc,
Thor Thayer91104982016-08-09 09:40:52 -05001718 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1719 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
Thor Thayer91104982016-08-09 09:40:52 -05001720 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1721 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1722 .ce_set_mask = ALTR_A10_ECC_SERRPENA,
1723 .ue_set_mask = ALTR_A10_ECC_DERRPENA,
1724 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1725 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1726 .inject_fops = &altr_edac_a10_device_inject_fops,
1727};
1728
1729static const struct edac_device_prv_data a10_sdmmceccb_data = {
Thor Thayer788586e2019-03-26 16:29:31 -05001730 .setup = socfpga_init_sdmmc_ecc,
Thor Thayer91104982016-08-09 09:40:52 -05001731 .ce_clear_mask = ALTR_A10_ECC_SERRPENB,
1732 .ue_clear_mask = ALTR_A10_ECC_DERRPENB,
Thor Thayer91104982016-08-09 09:40:52 -05001733 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1734 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1735 .ce_set_mask = ALTR_A10_ECC_TSERRB,
1736 .ue_set_mask = ALTR_A10_ECC_TDERRB,
1737 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1738 .ecc_irq_handler = altr_edac_a10_ecc_irq_portb,
1739 .inject_fops = &altr_edac_a10_device_inject_fops,
1740};
1741
Thor Thayer91104982016-08-09 09:40:52 -05001742#endif /* CONFIG_EDAC_ALTERA_SDMMC */
1743
Thor Thayer588cb032016-03-21 11:01:44 -05001744/********************* Arria10 EDAC Device Functions *************************/
Thor Thayerab564cb2016-05-25 11:29:41 -05001745static const struct of_device_id altr_edac_a10_device_of_match[] = {
1746#ifdef CONFIG_EDAC_ALTERA_L2C
1747 { .compatible = "altr,socfpga-a10-l2-ecc", .data = &a10_l2ecc_data },
1748#endif
1749#ifdef CONFIG_EDAC_ALTERA_OCRAM
1750 { .compatible = "altr,socfpga-a10-ocram-ecc",
1751 .data = &a10_ocramecc_data },
1752#endif
Thor Thayerab8c1e02016-06-22 08:58:58 -05001753#ifdef CONFIG_EDAC_ALTERA_ETHERNET
1754 { .compatible = "altr,socfpga-eth-mac-ecc",
1755 .data = &a10_enetecc_data },
1756#endif
Thor Thayerc6882fb2016-07-14 11:06:43 -05001757#ifdef CONFIG_EDAC_ALTERA_NAND
1758 { .compatible = "altr,socfpga-nand-ecc", .data = &a10_nandecc_data },
1759#endif
Thor Thayere8263792016-07-28 10:03:57 +02001760#ifdef CONFIG_EDAC_ALTERA_DMA
1761 { .compatible = "altr,socfpga-dma-ecc", .data = &a10_dmaecc_data },
1762#endif
Thor Thayerc6095812016-07-14 11:06:45 -05001763#ifdef CONFIG_EDAC_ALTERA_USB
1764 { .compatible = "altr,socfpga-usb-ecc", .data = &a10_usbecc_data },
1765#endif
Thor Thayer485fe9e2016-07-14 11:06:46 -05001766#ifdef CONFIG_EDAC_ALTERA_QSPI
1767 { .compatible = "altr,socfpga-qspi-ecc", .data = &a10_qspiecc_data },
1768#endif
Thor Thayer91104982016-08-09 09:40:52 -05001769#ifdef CONFIG_EDAC_ALTERA_SDMMC
1770 { .compatible = "altr,socfpga-sdmmc-ecc", .data = &a10_sdmmcecca_data },
1771#endif
Thor Thayer3123c5c2019-07-12 13:28:43 -05001772#ifdef CONFIG_EDAC_ALTERA_SDRAM
1773 { .compatible = "altr,sdram-edac-s10", .data = &s10_sdramecc_data },
1774#endif
Thor Thayerab564cb2016-05-25 11:29:41 -05001775 {},
1776};
1777MODULE_DEVICE_TABLE(of, altr_edac_a10_device_of_match);
Thor Thayer588cb032016-03-21 11:01:44 -05001778
1779/*
1780 * The Arria10 EDAC Device Functions differ from the Cyclone5/Arria5
1781 * because 2 IRQs are shared among the all ECC peripherals. The ECC
1782 * manager manages the IRQs and the children.
1783 * Based on xgene_edac.c peripheral code.
1784 */
1785
Thor Thayerc7b4be82016-04-06 20:22:54 -05001786static ssize_t altr_edac_a10_device_trig(struct file *file,
1787 const char __user *user_buf,
1788 size_t count, loff_t *ppos)
1789{
1790 struct edac_device_ctl_info *edac_dci = file->private_data;
1791 struct altr_edac_device_dev *drvdata = edac_dci->pvt_info;
1792 const struct edac_device_prv_data *priv = drvdata->data;
1793 void __iomem *set_addr = (drvdata->base + priv->set_err_ofst);
1794 unsigned long flags;
1795 u8 trig_type;
1796
1797 if (!user_buf || get_user(trig_type, user_buf))
1798 return -EFAULT;
1799
1800 local_irq_save(flags);
1801 if (trig_type == ALTR_UE_TRIGGER_CHAR)
1802 writel(priv->ue_set_mask, set_addr);
1803 else
1804 writel(priv->ce_set_mask, set_addr);
Thor Thayer064acbd2018-09-25 08:49:01 -05001805
1806 /* Ensure the interrupt test bits are set */
1807 wmb();
1808 local_irq_restore(flags);
1809
1810 return count;
1811}
1812
1813/*
1814 * The Stratix10 EDAC Error Injection Functions differ from Arria10
1815 * slightly. A few Arria10 peripherals can use this injection function.
1816 * Inject the error into the memory and then readback to trigger the IRQ.
1817 */
1818static ssize_t altr_edac_a10_device_trig2(struct file *file,
1819 const char __user *user_buf,
1820 size_t count, loff_t *ppos)
1821{
1822 struct edac_device_ctl_info *edac_dci = file->private_data;
1823 struct altr_edac_device_dev *drvdata = edac_dci->pvt_info;
1824 const struct edac_device_prv_data *priv = drvdata->data;
1825 void __iomem *set_addr = (drvdata->base + priv->set_err_ofst);
1826 unsigned long flags;
1827 u8 trig_type;
1828
1829 if (!user_buf || get_user(trig_type, user_buf))
1830 return -EFAULT;
1831
1832 local_irq_save(flags);
1833 if (trig_type == ALTR_UE_TRIGGER_CHAR) {
1834 writel(priv->ue_set_mask, set_addr);
1835 } else {
Thor Thayer436b0a52019-03-26 16:29:30 -05001836 /* Setup read/write of 4 bytes */
Thor Thayer064acbd2018-09-25 08:49:01 -05001837 writel(ECC_WORD_WRITE, drvdata->base + ECC_BLK_DBYTECTRL_OFST);
1838 /* Setup Address to 0 */
Thor Thayer436b0a52019-03-26 16:29:30 -05001839 writel(0, drvdata->base + ECC_BLK_ADDRESS_OFST);
1840 /* Setup accctrl to read & ecc & data override */
1841 writel(ECC_READ_EDOVR, drvdata->base + ECC_BLK_ACCCTRL_OFST);
Thor Thayer064acbd2018-09-25 08:49:01 -05001842 /* Kick it. */
1843 writel(ECC_XACT_KICK, drvdata->base + ECC_BLK_STARTACC_OFST);
1844 /* Setup write for single bit change */
Thor Thayer436b0a52019-03-26 16:29:30 -05001845 writel(readl(drvdata->base + ECC_BLK_RDATA0_OFST) ^ 0x1,
1846 drvdata->base + ECC_BLK_WDATA0_OFST);
1847 writel(readl(drvdata->base + ECC_BLK_RDATA1_OFST),
1848 drvdata->base + ECC_BLK_WDATA1_OFST);
1849 writel(readl(drvdata->base + ECC_BLK_RDATA2_OFST),
1850 drvdata->base + ECC_BLK_WDATA2_OFST);
1851 writel(readl(drvdata->base + ECC_BLK_RDATA3_OFST),
1852 drvdata->base + ECC_BLK_WDATA3_OFST);
1853
Thor Thayer064acbd2018-09-25 08:49:01 -05001854 /* Copy Read ECC to Write ECC */
1855 writel(readl(drvdata->base + ECC_BLK_RECC0_OFST),
1856 drvdata->base + ECC_BLK_WECC0_OFST);
1857 writel(readl(drvdata->base + ECC_BLK_RECC1_OFST),
1858 drvdata->base + ECC_BLK_WECC1_OFST);
1859 /* Setup accctrl to write & ecc override & data override */
1860 writel(ECC_WRITE_EDOVR, drvdata->base + ECC_BLK_ACCCTRL_OFST);
1861 /* Kick it. */
1862 writel(ECC_XACT_KICK, drvdata->base + ECC_BLK_STARTACC_OFST);
1863 /* Setup accctrl to read & ecc overwrite & data overwrite */
1864 writel(ECC_READ_EDOVR, drvdata->base + ECC_BLK_ACCCTRL_OFST);
1865 /* Kick it. */
1866 writel(ECC_XACT_KICK, drvdata->base + ECC_BLK_STARTACC_OFST);
1867 }
1868
Thor Thayerc7b4be82016-04-06 20:22:54 -05001869 /* Ensure the interrupt test bits are set */
1870 wmb();
1871 local_irq_restore(flags);
1872
1873 return count;
1874}
1875
Thor Thayer13ab8442016-06-07 15:35:57 -05001876static void altr_edac_a10_irq_handler(struct irq_desc *desc)
Thor Thayer588cb032016-03-21 11:01:44 -05001877{
Thor Thayer13ab8442016-06-07 15:35:57 -05001878 int dberr, bit, sm_offset, irq_status;
1879 struct altr_arria10_edac *edac = irq_desc_get_handler_data(desc);
1880 struct irq_chip *chip = irq_desc_get_chip(desc);
1881 int irq = irq_desc_get_irq(desc);
Dan Carpenter8faa1cf2019-06-24 16:47:17 +03001882 unsigned long bits;
Thor Thayer13ab8442016-06-07 15:35:57 -05001883
1884 dberr = (irq == edac->db_irq) ? 1 : 0;
1885 sm_offset = dberr ? A10_SYSMGR_ECC_INTSTAT_DERR_OFST :
1886 A10_SYSMGR_ECC_INTSTAT_SERR_OFST;
1887
1888 chained_irq_enter(chip, desc);
Thor Thayer588cb032016-03-21 11:01:44 -05001889
1890 regmap_read(edac->ecc_mgr_map, sm_offset, &irq_status);
1891
Dan Carpenter8faa1cf2019-06-24 16:47:17 +03001892 bits = irq_status;
1893 for_each_set_bit(bit, &bits, 32) {
Thor Thayer13ab8442016-06-07 15:35:57 -05001894 irq = irq_linear_revmap(edac->domain, dberr * 32 + bit);
1895 if (irq)
1896 generic_handle_irq(irq);
Thor Thayer588cb032016-03-21 11:01:44 -05001897 }
1898
Thor Thayer13ab8442016-06-07 15:35:57 -05001899 chained_irq_exit(chip, desc);
Thor Thayer588cb032016-03-21 11:01:44 -05001900}
1901
Thor Thayer44ec9b32016-06-22 08:58:52 -05001902static int validate_parent_available(struct device_node *np)
1903{
1904 struct device_node *parent;
1905 int ret = 0;
1906
Thor Thayer3123c5c2019-07-12 13:28:43 -05001907 /* SDRAM must be present for Linux (implied parent) */
1908 if (of_device_is_compatible(np, "altr,sdram-edac-s10"))
1909 return 0;
1910
Thor Thayer44ec9b32016-06-22 08:58:52 -05001911 /* Ensure parent device is enabled if parent node exists */
1912 parent = of_parse_phandle(np, "altr,ecc-parent", 0);
1913 if (parent && !of_device_is_available(parent))
1914 ret = -ENODEV;
1915
1916 of_node_put(parent);
1917 return ret;
1918}
1919
Thor Thayer3123c5c2019-07-12 13:28:43 -05001920static int get_s10_sdram_edac_resource(struct device_node *np,
1921 struct resource *res)
1922{
1923 struct device_node *parent;
1924 int ret;
1925
1926 parent = of_parse_phandle(np, "altr,sdr-syscon", 0);
1927 if (!parent)
1928 return -ENODEV;
1929
1930 ret = of_address_to_resource(parent, 0, res);
1931 of_node_put(parent);
1932
1933 return ret;
1934}
1935
Thor Thayer588cb032016-03-21 11:01:44 -05001936static int altr_edac_a10_device_add(struct altr_arria10_edac *edac,
1937 struct device_node *np)
1938{
1939 struct edac_device_ctl_info *dci;
1940 struct altr_edac_device_dev *altdev;
1941 char *ecc_name = (char *)np->name;
1942 struct resource res;
1943 int edac_idx;
1944 int rc = 0;
1945 const struct edac_device_prv_data *prv;
1946 /* Get matching node and check for valid result */
1947 const struct of_device_id *pdev_id =
Thor Thayerab564cb2016-05-25 11:29:41 -05001948 of_match_node(altr_edac_a10_device_of_match, np);
Thor Thayer588cb032016-03-21 11:01:44 -05001949 if (IS_ERR_OR_NULL(pdev_id))
1950 return -ENODEV;
1951
1952 /* Get driver specific data for this EDAC device */
1953 prv = pdev_id->data;
1954 if (IS_ERR_OR_NULL(prv))
1955 return -ENODEV;
1956
Thor Thayer44ec9b32016-06-22 08:58:52 -05001957 if (validate_parent_available(np))
1958 return -ENODEV;
1959
Thor Thayer588cb032016-03-21 11:01:44 -05001960 if (!devres_open_group(edac->dev, altr_edac_a10_device_add, GFP_KERNEL))
1961 return -ENOMEM;
1962
Thor Thayer3123c5c2019-07-12 13:28:43 -05001963 if (of_device_is_compatible(np, "altr,sdram-edac-s10"))
1964 rc = get_s10_sdram_edac_resource(np, &res);
1965 else
1966 rc = of_address_to_resource(np, 0, &res);
1967
Thor Thayer588cb032016-03-21 11:01:44 -05001968 if (rc < 0) {
1969 edac_printk(KERN_ERR, EDAC_DEVICE,
1970 "%s: no resource address\n", ecc_name);
1971 goto err_release_group;
1972 }
1973
1974 edac_idx = edac_device_alloc_index();
1975 dci = edac_device_alloc_ctl_info(sizeof(*altdev), ecc_name,
1976 1, ecc_name, 1, 0, NULL, 0,
1977 edac_idx);
1978
1979 if (!dci) {
1980 edac_printk(KERN_ERR, EDAC_DEVICE,
1981 "%s: Unable to allocate EDAC device\n", ecc_name);
1982 rc = -ENOMEM;
1983 goto err_release_group;
1984 }
1985
1986 altdev = dci->pvt_info;
1987 dci->dev = edac->dev;
1988 altdev->edac_dev_name = ecc_name;
1989 altdev->edac_idx = edac_idx;
1990 altdev->edac = edac;
1991 altdev->edac_dev = dci;
1992 altdev->data = prv;
1993 altdev->ddev = *edac->dev;
1994 dci->dev = &altdev->ddev;
1995 dci->ctl_name = "Altera ECC Manager";
1996 dci->mod_name = ecc_name;
1997 dci->dev_name = ecc_name;
1998
1999 altdev->base = devm_ioremap_resource(edac->dev, &res);
2000 if (IS_ERR(altdev->base)) {
2001 rc = PTR_ERR(altdev->base);
2002 goto err_release_group1;
2003 }
2004
2005 /* Check specific dependencies for the module */
2006 if (altdev->data->setup) {
2007 rc = altdev->data->setup(altdev);
2008 if (rc)
2009 goto err_release_group1;
2010 }
2011
Thor Thayer13ab8442016-06-07 15:35:57 -05002012 altdev->sb_irq = irq_of_parse_and_map(np, 0);
2013 if (!altdev->sb_irq) {
2014 edac_printk(KERN_ERR, EDAC_DEVICE, "Error allocating SBIRQ\n");
2015 rc = -ENODEV;
2016 goto err_release_group1;
2017 }
Thor Thayera29d64a2016-09-22 17:13:39 -05002018 rc = devm_request_irq(edac->dev, altdev->sb_irq, prv->ecc_irq_handler,
2019 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
2020 ecc_name, altdev);
Thor Thayer13ab8442016-06-07 15:35:57 -05002021 if (rc) {
Thor Thayer37635692016-09-22 17:13:38 -05002022 edac_printk(KERN_ERR, EDAC_DEVICE, "No SBERR IRQ resource\n");
Thor Thayer13ab8442016-06-07 15:35:57 -05002023 goto err_release_group1;
2024 }
2025
Thor Thayer1bd76ff2019-03-13 10:27:22 -05002026#ifdef CONFIG_ARCH_STRATIX10
2027 /* Use IRQ to determine SError origin instead of assigning IRQ */
2028 rc = of_property_read_u32_index(np, "interrupts", 0, &altdev->db_irq);
2029 if (rc) {
2030 edac_printk(KERN_ERR, EDAC_DEVICE,
2031 "Unable to parse DB IRQ index\n");
2032 goto err_release_group1;
2033 }
2034#else
Thor Thayer13ab8442016-06-07 15:35:57 -05002035 altdev->db_irq = irq_of_parse_and_map(np, 1);
2036 if (!altdev->db_irq) {
2037 edac_printk(KERN_ERR, EDAC_DEVICE, "Error allocating DBIRQ\n");
2038 rc = -ENODEV;
2039 goto err_release_group1;
2040 }
Thor Thayera29d64a2016-09-22 17:13:39 -05002041 rc = devm_request_irq(edac->dev, altdev->db_irq, prv->ecc_irq_handler,
2042 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
2043 ecc_name, altdev);
Thor Thayer13ab8442016-06-07 15:35:57 -05002044 if (rc) {
2045 edac_printk(KERN_ERR, EDAC_DEVICE, "No DBERR IRQ resource\n");
2046 goto err_release_group1;
2047 }
Thor Thayer1bd76ff2019-03-13 10:27:22 -05002048#endif
Thor Thayer13ab8442016-06-07 15:35:57 -05002049
Thor Thayer588cb032016-03-21 11:01:44 -05002050 rc = edac_device_add_device(dci);
2051 if (rc) {
2052 dev_err(edac->dev, "edac_device_add_device failed\n");
2053 rc = -ENOMEM;
2054 goto err_release_group1;
2055 }
2056
2057 altr_create_edacdev_dbgfs(dci, prv);
2058
2059 list_add(&altdev->next, &edac->a10_ecc_devices);
2060
2061 devres_remove_group(edac->dev, altr_edac_a10_device_add);
2062
2063 return 0;
2064
2065err_release_group1:
2066 edac_device_free_ctl_info(dci);
2067err_release_group:
Thor Thayer588cb032016-03-21 11:01:44 -05002068 devres_release_group(edac->dev, NULL);
2069 edac_printk(KERN_ERR, EDAC_DEVICE,
2070 "%s:Error setting up EDAC device: %d\n", ecc_name, rc);
2071
2072 return rc;
2073}
2074
Thor Thayer13ab8442016-06-07 15:35:57 -05002075static void a10_eccmgr_irq_mask(struct irq_data *d)
2076{
2077 struct altr_arria10_edac *edac = irq_data_get_irq_chip_data(d);
2078
2079 regmap_write(edac->ecc_mgr_map, A10_SYSMGR_ECC_INTMASK_SET_OFST,
2080 BIT(d->hwirq));
2081}
2082
2083static void a10_eccmgr_irq_unmask(struct irq_data *d)
2084{
2085 struct altr_arria10_edac *edac = irq_data_get_irq_chip_data(d);
2086
2087 regmap_write(edac->ecc_mgr_map, A10_SYSMGR_ECC_INTMASK_CLR_OFST,
2088 BIT(d->hwirq));
2089}
2090
2091static int a10_eccmgr_irqdomain_map(struct irq_domain *d, unsigned int irq,
2092 irq_hw_number_t hwirq)
2093{
2094 struct altr_arria10_edac *edac = d->host_data;
2095
2096 irq_set_chip_and_handler(irq, &edac->irq_chip, handle_simple_irq);
2097 irq_set_chip_data(irq, edac);
2098 irq_set_noprobe(irq);
2099
2100 return 0;
2101}
2102
Tobias Klauser18caec22017-05-24 15:35:05 +02002103static const struct irq_domain_ops a10_eccmgr_ic_ops = {
Thor Thayer13ab8442016-06-07 15:35:57 -05002104 .map = a10_eccmgr_irqdomain_map,
2105 .xlate = irq_domain_xlate_twocell,
2106};
2107
Thor Thayerd5fc9122018-09-25 08:48:58 -05002108/************** Stratix 10 EDAC Double Bit Error Handler ************/
2109#define to_a10edac(p, m) container_of(p, struct altr_arria10_edac, m)
2110
Thor Thayer1bd76ff2019-03-13 10:27:22 -05002111#ifdef CONFIG_ARCH_STRATIX10
2112/* panic routine issues reboot on non-zero panic_timeout */
2113extern int panic_timeout;
2114
Thor Thayerd5fc9122018-09-25 08:48:58 -05002115/*
2116 * The double bit error is handled through SError which is fatal. This is
2117 * called as a panic notifier to printout ECC error info as part of the panic.
2118 */
2119static int s10_edac_dberr_handler(struct notifier_block *this,
2120 unsigned long event, void *ptr)
2121{
2122 struct altr_arria10_edac *edac = to_a10edac(this, panic_notifier);
2123 int err_addr, dberror;
2124
2125 regmap_read(edac->ecc_mgr_map, S10_SYSMGR_ECC_INTSTAT_DERR_OFST,
2126 &dberror);
2127 regmap_write(edac->ecc_mgr_map, S10_SYSMGR_UE_VAL_OFST, dberror);
Thor Thayer1bd76ff2019-03-13 10:27:22 -05002128 if (dberror & S10_DBE_IRQ_MASK) {
2129 struct list_head *position;
2130 struct altr_edac_device_dev *ed;
2131 struct arm_smccc_res result;
2132
2133 /* Find the matching DBE in the list of devices */
2134 list_for_each(position, &edac->a10_ecc_devices) {
2135 ed = list_entry(position, struct altr_edac_device_dev,
2136 next);
2137 if (!(BIT(ed->db_irq) & dberror))
2138 continue;
2139
2140 writel(ALTR_A10_ECC_DERRPENA,
2141 ed->base + ALTR_A10_ECC_INTSTAT_OFST);
2142 err_addr = readl(ed->base + ALTR_S10_DERR_ADDRA_OFST);
2143 regmap_write(edac->ecc_mgr_map,
2144 S10_SYSMGR_UE_ADDR_OFST, err_addr);
2145 edac_printk(KERN_ERR, EDAC_DEVICE,
2146 "EDAC: [Fatal DBE on %s @ 0x%08X]\n",
2147 ed->edac_dev_name, err_addr);
2148 break;
2149 }
2150 /* Notify the System through SMC. Reboot delay = 1 second */
2151 panic_timeout = 1;
2152 arm_smccc_smc(INTEL_SIP_SMC_ECC_DBE, dberror, 0, 0, 0, 0,
2153 0, 0, &result);
Thor Thayerd5fc9122018-09-25 08:48:58 -05002154 }
2155
2156 return NOTIFY_DONE;
2157}
Thor Thayer1bd76ff2019-03-13 10:27:22 -05002158#endif
Thor Thayerd5fc9122018-09-25 08:48:58 -05002159
2160/****************** Arria 10 EDAC Probe Function *********************/
Thor Thayer588cb032016-03-21 11:01:44 -05002161static int altr_edac_a10_probe(struct platform_device *pdev)
2162{
2163 struct altr_arria10_edac *edac;
2164 struct device_node *child;
Thor Thayer588cb032016-03-21 11:01:44 -05002165
2166 edac = devm_kzalloc(&pdev->dev, sizeof(*edac), GFP_KERNEL);
2167 if (!edac)
2168 return -ENOMEM;
2169
2170 edac->dev = &pdev->dev;
2171 platform_set_drvdata(pdev, edac);
2172 INIT_LIST_HEAD(&edac->a10_ecc_devices);
2173
Thor Thayerd5fc9122018-09-25 08:48:58 -05002174 if (socfpga_is_a10()) {
2175 edac->ecc_mgr_map =
2176 syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
Thor Thayer588cb032016-03-21 11:01:44 -05002177 "altr,sysmgr-syscon");
Thor Thayerd5fc9122018-09-25 08:48:58 -05002178 } else {
2179 struct device_node *sysmgr_np;
2180 struct resource res;
Arnd Bergmann8537bf102018-09-27 12:09:26 +02002181 uintptr_t base;
Thor Thayerd5fc9122018-09-25 08:48:58 -05002182
2183 sysmgr_np = of_parse_phandle(pdev->dev.of_node,
2184 "altr,sysmgr-syscon", 0);
2185 if (!sysmgr_np) {
2186 edac_printk(KERN_ERR, EDAC_DEVICE,
2187 "Unable to find altr,sysmgr-syscon\n");
2188 return -ENODEV;
2189 }
2190
2191 if (of_address_to_resource(sysmgr_np, 0, &res))
2192 return -ENOMEM;
2193
2194 /* Need physical address for SMCC call */
Arnd Bergmann8537bf102018-09-27 12:09:26 +02002195 base = res.start;
Thor Thayerd5fc9122018-09-25 08:48:58 -05002196
Arnd Bergmann8537bf102018-09-27 12:09:26 +02002197 edac->ecc_mgr_map = devm_regmap_init(&pdev->dev, NULL,
2198 (void *)base,
Thor Thayerd5fc9122018-09-25 08:48:58 -05002199 &s10_sdram_regmap_cfg);
2200 }
2201
Thor Thayer588cb032016-03-21 11:01:44 -05002202 if (IS_ERR(edac->ecc_mgr_map)) {
2203 edac_printk(KERN_ERR, EDAC_DEVICE,
2204 "Unable to get syscon altr,sysmgr-syscon\n");
2205 return PTR_ERR(edac->ecc_mgr_map);
2206 }
2207
Thor Thayer13ab8442016-06-07 15:35:57 -05002208 edac->irq_chip.name = pdev->dev.of_node->name;
2209 edac->irq_chip.irq_mask = a10_eccmgr_irq_mask;
2210 edac->irq_chip.irq_unmask = a10_eccmgr_irq_unmask;
2211 edac->domain = irq_domain_add_linear(pdev->dev.of_node, 64,
2212 &a10_eccmgr_ic_ops, edac);
2213 if (!edac->domain) {
2214 dev_err(&pdev->dev, "Error adding IRQ domain\n");
2215 return -ENOMEM;
Thor Thayer588cb032016-03-21 11:01:44 -05002216 }
2217
Thor Thayer13ab8442016-06-07 15:35:57 -05002218 edac->sb_irq = platform_get_irq(pdev, 0);
2219 if (edac->sb_irq < 0) {
2220 dev_err(&pdev->dev, "No SBERR IRQ resource\n");
2221 return edac->sb_irq;
Thor Thayer588cb032016-03-21 11:01:44 -05002222 }
2223
Thor Thayer13ab8442016-06-07 15:35:57 -05002224 irq_set_chained_handler_and_data(edac->sb_irq,
2225 altr_edac_a10_irq_handler,
2226 edac);
2227
Thor Thayer1bd76ff2019-03-13 10:27:22 -05002228#ifdef CONFIG_ARCH_STRATIX10
2229 {
Thor Thayerd5fc9122018-09-25 08:48:58 -05002230 int dberror, err_addr;
2231
2232 edac->panic_notifier.notifier_call = s10_edac_dberr_handler;
2233 atomic_notifier_chain_register(&panic_notifier_list,
2234 &edac->panic_notifier);
2235
2236 /* Printout a message if uncorrectable error previously. */
2237 regmap_read(edac->ecc_mgr_map, S10_SYSMGR_UE_VAL_OFST,
2238 &dberror);
2239 if (dberror) {
2240 regmap_read(edac->ecc_mgr_map, S10_SYSMGR_UE_ADDR_OFST,
2241 &err_addr);
2242 edac_printk(KERN_ERR, EDAC_DEVICE,
2243 "Previous Boot UE detected[0x%X] @ 0x%X\n",
2244 dberror, err_addr);
2245 /* Reset the sticky registers */
2246 regmap_write(edac->ecc_mgr_map,
2247 S10_SYSMGR_UE_VAL_OFST, 0);
2248 regmap_write(edac->ecc_mgr_map,
2249 S10_SYSMGR_UE_ADDR_OFST, 0);
2250 }
Thor Thayer13ab8442016-06-07 15:35:57 -05002251 }
Thor Thayer1bd76ff2019-03-13 10:27:22 -05002252#else
2253 edac->db_irq = platform_get_irq(pdev, 1);
2254 if (edac->db_irq < 0) {
2255 dev_err(&pdev->dev, "No DBERR IRQ resource\n");
2256 return edac->db_irq;
2257 }
2258 irq_set_chained_handler_and_data(edac->db_irq,
2259 altr_edac_a10_irq_handler, edac);
2260#endif
Thor Thayer13ab8442016-06-07 15:35:57 -05002261
Thor Thayer588cb032016-03-21 11:01:44 -05002262 for_each_child_of_node(pdev->dev.of_node, child) {
2263 if (!of_device_is_available(child))
2264 continue;
Thor Thayerc6882fb2016-07-14 11:06:43 -05002265
Thor Thayer08a260d2019-11-07 14:01:29 -06002266 if (of_match_node(altr_edac_a10_device_of_match, child))
Thor Thayer588cb032016-03-21 11:01:44 -05002267 altr_edac_a10_device_add(edac, child);
Thor Thayerc6882fb2016-07-14 11:06:43 -05002268
Thor Thayer580b5cf2019-02-25 12:56:45 -06002269#ifdef CONFIG_EDAC_ALTERA_SDRAM
Thor Thayer3123c5c2019-07-12 13:28:43 -05002270 else if (of_device_is_compatible(child, "altr,sdram-edac-a10"))
Thor Thayerab564cb2016-05-25 11:29:41 -05002271 of_platform_populate(pdev->dev.of_node,
2272 altr_sdram_ctrl_of_match,
2273 NULL, &pdev->dev);
Thor Thayer580b5cf2019-02-25 12:56:45 -06002274#endif
Thor Thayer588cb032016-03-21 11:01:44 -05002275 }
2276
2277 return 0;
2278}
2279
2280static const struct of_device_id altr_edac_a10_of_match[] = {
2281 { .compatible = "altr,socfpga-a10-ecc-manager" },
Thor Thayerd5fc9122018-09-25 08:48:58 -05002282 { .compatible = "altr,socfpga-s10-ecc-manager" },
Thor Thayer588cb032016-03-21 11:01:44 -05002283 {},
2284};
2285MODULE_DEVICE_TABLE(of, altr_edac_a10_of_match);
2286
2287static struct platform_driver altr_edac_a10_driver = {
2288 .probe = altr_edac_a10_probe,
2289 .driver = {
2290 .name = "socfpga_a10_ecc_manager",
2291 .of_match_table = altr_edac_a10_of_match,
2292 },
2293};
2294module_platform_driver(altr_edac_a10_driver);
2295
Thor Thayer71bcada2014-09-03 10:27:54 -05002296MODULE_LICENSE("GPL v2");
2297MODULE_AUTHOR("Thor Thayer");
Thor Thayerc3eea192016-02-10 13:26:21 -06002298MODULE_DESCRIPTION("EDAC Driver for Altera Memories");