blob: acb3006df6315db90450a74d1f79afc3aa6bdd60 [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 Thayerc3eea192016-02-10 13:26:21 -060012#include <linux/genalloc.h>
Thor Thayer71bcada2014-09-03 10:27:54 -050013#include <linux/interrupt.h>
Thor Thayer13ab8442016-06-07 15:35:57 -050014#include <linux/irqchip/chained_irq.h>
Thor Thayer71bcada2014-09-03 10:27:54 -050015#include <linux/kernel.h>
16#include <linux/mfd/syscon.h>
Thor Thayere9918d72018-05-11 18:00:10 -050017#include <linux/notifier.h>
Thor Thayer588cb032016-03-21 11:01:44 -050018#include <linux/of_address.h>
Thor Thayer13ab8442016-06-07 15:35:57 -050019#include <linux/of_irq.h>
Thor Thayer71bcada2014-09-03 10:27:54 -050020#include <linux/of_platform.h>
21#include <linux/platform_device.h>
22#include <linux/regmap.h>
23#include <linux/types.h>
24#include <linux/uaccess.h>
25
Thor Thayer143f4a52015-06-04 09:28:46 -050026#include "altera_edac.h"
Thor Thayer71bcada2014-09-03 10:27:54 -050027#include "edac_module.h"
28
29#define EDAC_MOD_STR "altera_edac"
Thor Thayerc3eea192016-02-10 13:26:21 -060030#define EDAC_DEVICE "Altera"
Thor Thayer71bcada2014-09-03 10:27:54 -050031
Thor Thayer580b5cf2019-02-25 12:56:45 -060032#ifdef CONFIG_EDAC_ALTERA_SDRAM
Thor Thayer143f4a52015-06-04 09:28:46 -050033static const struct altr_sdram_prv_data c5_data = {
34 .ecc_ctrl_offset = CV_CTLCFG_OFST,
35 .ecc_ctl_en_mask = CV_CTLCFG_ECC_AUTO_EN,
36 .ecc_stat_offset = CV_DRAMSTS_OFST,
37 .ecc_stat_ce_mask = CV_DRAMSTS_SBEERR,
38 .ecc_stat_ue_mask = CV_DRAMSTS_DBEERR,
39 .ecc_saddr_offset = CV_ERRADDR_OFST,
Thor Thayer73bcc942015-06-04 09:28:47 -050040 .ecc_daddr_offset = CV_ERRADDR_OFST,
Thor Thayer143f4a52015-06-04 09:28:46 -050041 .ecc_cecnt_offset = CV_SBECOUNT_OFST,
42 .ecc_uecnt_offset = CV_DBECOUNT_OFST,
43 .ecc_irq_en_offset = CV_DRAMINTR_OFST,
44 .ecc_irq_en_mask = CV_DRAMINTR_INTREN,
45 .ecc_irq_clr_offset = CV_DRAMINTR_OFST,
46 .ecc_irq_clr_mask = (CV_DRAMINTR_INTRCLR | CV_DRAMINTR_INTREN),
47 .ecc_cnt_rst_offset = CV_DRAMINTR_OFST,
48 .ecc_cnt_rst_mask = CV_DRAMINTR_INTRCLR,
Thor Thayer143f4a52015-06-04 09:28:46 -050049 .ce_ue_trgr_offset = CV_CTLCFG_OFST,
50 .ce_set_mask = CV_CTLCFG_GEN_SB_ERR,
51 .ue_set_mask = CV_CTLCFG_GEN_DB_ERR,
Thor Thayer71bcada2014-09-03 10:27:54 -050052};
53
Thor Thayer73bcc942015-06-04 09:28:47 -050054static const struct altr_sdram_prv_data a10_data = {
55 .ecc_ctrl_offset = A10_ECCCTRL1_OFST,
56 .ecc_ctl_en_mask = A10_ECCCTRL1_ECC_EN,
57 .ecc_stat_offset = A10_INTSTAT_OFST,
58 .ecc_stat_ce_mask = A10_INTSTAT_SBEERR,
59 .ecc_stat_ue_mask = A10_INTSTAT_DBEERR,
60 .ecc_saddr_offset = A10_SERRADDR_OFST,
61 .ecc_daddr_offset = A10_DERRADDR_OFST,
62 .ecc_irq_en_offset = A10_ERRINTEN_OFST,
63 .ecc_irq_en_mask = A10_ECC_IRQ_EN_MASK,
64 .ecc_irq_clr_offset = A10_INTSTAT_OFST,
65 .ecc_irq_clr_mask = (A10_INTSTAT_SBEERR | A10_INTSTAT_DBEERR),
66 .ecc_cnt_rst_offset = A10_ECCCTRL1_OFST,
67 .ecc_cnt_rst_mask = A10_ECC_CNT_RESET_MASK,
Thor Thayer73bcc942015-06-04 09:28:47 -050068 .ce_ue_trgr_offset = A10_DIAGINTTEST_OFST,
69 .ce_set_mask = A10_DIAGINT_TSERRA_MASK,
70 .ue_set_mask = A10_DIAGINT_TDERRA_MASK,
Thor Thayer73bcc942015-06-04 09:28:47 -050071};
72
Thor Thayerc3eea192016-02-10 13:26:21 -060073/*********************** EDAC Memory Controller Functions ****************/
74
75/* The SDRAM controller uses the EDAC Memory Controller framework. */
76
Thor Thayer71bcada2014-09-03 10:27:54 -050077static irqreturn_t altr_sdram_mc_err_handler(int irq, void *dev_id)
78{
79 struct mem_ctl_info *mci = dev_id;
80 struct altr_sdram_mc_data *drvdata = mci->pvt_info;
Thor Thayer143f4a52015-06-04 09:28:46 -050081 const struct altr_sdram_prv_data *priv = drvdata->data;
Thor Thayer73bcc942015-06-04 09:28:47 -050082 u32 status, err_count = 1, err_addr;
Thor Thayer71bcada2014-09-03 10:27:54 -050083
Thor Thayer143f4a52015-06-04 09:28:46 -050084 regmap_read(drvdata->mc_vbase, priv->ecc_stat_offset, &status);
Thor Thayer71bcada2014-09-03 10:27:54 -050085
Thor Thayer143f4a52015-06-04 09:28:46 -050086 if (status & priv->ecc_stat_ue_mask) {
Thor Thayer73bcc942015-06-04 09:28:47 -050087 regmap_read(drvdata->mc_vbase, priv->ecc_daddr_offset,
88 &err_addr);
89 if (priv->ecc_uecnt_offset)
90 regmap_read(drvdata->mc_vbase, priv->ecc_uecnt_offset,
91 &err_count);
Thor Thayer71bcada2014-09-03 10:27:54 -050092 panic("\nEDAC: [%d Uncorrectable errors @ 0x%08X]\n",
93 err_count, err_addr);
94 }
Thor Thayer143f4a52015-06-04 09:28:46 -050095 if (status & priv->ecc_stat_ce_mask) {
Thor Thayer73bcc942015-06-04 09:28:47 -050096 regmap_read(drvdata->mc_vbase, priv->ecc_saddr_offset,
97 &err_addr);
98 if (priv->ecc_uecnt_offset)
99 regmap_read(drvdata->mc_vbase, priv->ecc_cecnt_offset,
100 &err_count);
Thor Thayer71bcada2014-09-03 10:27:54 -0500101 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, err_count,
102 err_addr >> PAGE_SHIFT,
103 err_addr & ~PAGE_MASK, 0,
104 0, 0, -1, mci->ctl_name, "");
Thor Thayer73bcc942015-06-04 09:28:47 -0500105 /* Clear IRQ to resume */
106 regmap_write(drvdata->mc_vbase, priv->ecc_irq_clr_offset,
107 priv->ecc_irq_clr_mask);
108
109 return IRQ_HANDLED;
Thor Thayer71bcada2014-09-03 10:27:54 -0500110 }
Thor Thayer73bcc942015-06-04 09:28:47 -0500111 return IRQ_NONE;
Thor Thayer71bcada2014-09-03 10:27:54 -0500112}
113
Thor Thayer71bcada2014-09-03 10:27:54 -0500114static ssize_t altr_sdr_mc_err_inject_write(struct file *file,
115 const char __user *data,
116 size_t count, loff_t *ppos)
117{
118 struct mem_ctl_info *mci = file->private_data;
119 struct altr_sdram_mc_data *drvdata = mci->pvt_info;
Thor Thayer143f4a52015-06-04 09:28:46 -0500120 const struct altr_sdram_prv_data *priv = drvdata->data;
Thor Thayer71bcada2014-09-03 10:27:54 -0500121 u32 *ptemp;
122 dma_addr_t dma_handle;
123 u32 reg, read_reg;
124
125 ptemp = dma_alloc_coherent(mci->pdev, 16, &dma_handle, GFP_KERNEL);
126 if (!ptemp) {
127 dma_free_coherent(mci->pdev, 16, ptemp, dma_handle);
128 edac_printk(KERN_ERR, EDAC_MC,
129 "Inject: Buffer Allocation error\n");
130 return -ENOMEM;
131 }
132
Thor Thayer143f4a52015-06-04 09:28:46 -0500133 regmap_read(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
134 &read_reg);
135 read_reg &= ~(priv->ce_set_mask | priv->ue_set_mask);
Thor Thayer71bcada2014-09-03 10:27:54 -0500136
137 /* Error are injected by writing a word while the SBE or DBE
138 * bit in the CTLCFG register is set. Reading the word will
139 * trigger the SBE or DBE error and the corresponding IRQ.
140 */
141 if (count == 3) {
142 edac_printk(KERN_ALERT, EDAC_MC,
143 "Inject Double bit error\n");
Thor Thayer90e493d2016-10-19 14:53:47 -0500144 local_irq_disable();
Thor Thayer143f4a52015-06-04 09:28:46 -0500145 regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
146 (read_reg | priv->ue_set_mask));
Thor Thayer90e493d2016-10-19 14:53:47 -0500147 local_irq_enable();
Thor Thayer71bcada2014-09-03 10:27:54 -0500148 } else {
149 edac_printk(KERN_ALERT, EDAC_MC,
150 "Inject Single bit error\n");
Thor Thayer90e493d2016-10-19 14:53:47 -0500151 local_irq_disable();
Thor Thayer143f4a52015-06-04 09:28:46 -0500152 regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
153 (read_reg | priv->ce_set_mask));
Thor Thayer90e493d2016-10-19 14:53:47 -0500154 local_irq_enable();
Thor Thayer71bcada2014-09-03 10:27:54 -0500155 }
156
157 ptemp[0] = 0x5A5A5A5A;
158 ptemp[1] = 0xA5A5A5A5;
159
160 /* Clear the error injection bits */
Thor Thayer143f4a52015-06-04 09:28:46 -0500161 regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset, read_reg);
Thor Thayer71bcada2014-09-03 10:27:54 -0500162 /* Ensure it has been written out */
163 wmb();
164
165 /*
166 * To trigger the error, we need to read the data back
167 * (the data was written with errors above).
Mark Rutland332efa62017-10-23 14:07:12 -0700168 * The READ_ONCE macros and printk are used to prevent the
Thor Thayer71bcada2014-09-03 10:27:54 -0500169 * the compiler optimizing these reads out.
170 */
Mark Rutland332efa62017-10-23 14:07:12 -0700171 reg = READ_ONCE(ptemp[0]);
172 read_reg = READ_ONCE(ptemp[1]);
Thor Thayer71bcada2014-09-03 10:27:54 -0500173 /* Force Read */
174 rmb();
175
176 edac_printk(KERN_ALERT, EDAC_MC, "Read Data [0x%X, 0x%X]\n",
177 reg, read_reg);
178
179 dma_free_coherent(mci->pdev, 16, ptemp, dma_handle);
180
181 return count;
182}
183
184static const struct file_operations altr_sdr_mc_debug_inject_fops = {
185 .open = simple_open,
186 .write = altr_sdr_mc_err_inject_write,
187 .llseek = generic_file_llseek,
188};
189
190static void altr_sdr_mc_create_debugfs_nodes(struct mem_ctl_info *mci)
191{
Borislav Petkovbba3b312015-09-22 12:27:29 +0200192 if (!IS_ENABLED(CONFIG_EDAC_DEBUG))
193 return;
194
195 if (!mci->debugfs)
196 return;
197
Thor Thayerb8978ba2016-08-19 11:04:26 -0500198 edac_debugfs_create_file("altr_trigger", S_IWUSR, mci->debugfs, mci,
Borislav Petkovbba3b312015-09-22 12:27:29 +0200199 &altr_sdr_mc_debug_inject_fops);
Thor Thayer71bcada2014-09-03 10:27:54 -0500200}
Thor Thayer71bcada2014-09-03 10:27:54 -0500201
Thor Thayerf9ae4872015-06-04 09:28:45 -0500202/* Get total memory size from Open Firmware DTB */
203static unsigned long get_total_mem(void)
Thor Thayer71bcada2014-09-03 10:27:54 -0500204{
Thor Thayerf9ae4872015-06-04 09:28:45 -0500205 struct device_node *np = NULL;
Chris Packhamff0abed2017-06-07 11:54:59 +1200206 struct resource res;
207 int ret;
208 unsigned long total_mem = 0;
Thor Thayer71bcada2014-09-03 10:27:54 -0500209
Thor Thayerf9ae4872015-06-04 09:28:45 -0500210 for_each_node_by_type(np, "memory") {
Chris Packhamff0abed2017-06-07 11:54:59 +1200211 ret = of_address_to_resource(np, 0, &res);
212 if (ret)
213 continue;
Thor Thayer71bcada2014-09-03 10:27:54 -0500214
Chris Packhamff0abed2017-06-07 11:54:59 +1200215 total_mem += resource_size(&res);
Thor Thayerf9ae4872015-06-04 09:28:45 -0500216 }
217 edac_dbg(0, "total_mem 0x%lx\n", total_mem);
218 return total_mem;
Thor Thayer71bcada2014-09-03 10:27:54 -0500219}
220
Thor Thayer143f4a52015-06-04 09:28:46 -0500221static const struct of_device_id altr_sdram_ctrl_of_match[] = {
Arnd Bergmann2c911f62016-04-16 22:13:55 +0200222 { .compatible = "altr,sdram-edac", .data = &c5_data},
223 { .compatible = "altr,sdram-edac-a10", .data = &a10_data},
Thor Thayer08f08bf2018-09-25 08:49:00 -0500224 { .compatible = "altr,sdram-edac-s10", .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,
Thor Thayer3dab6bd2018-04-27 13:37:17 -0500563};
564
Thor Thayer3dab6bd2018-04-27 13:37:17 -0500565/************** </Stratix10 EDAC Memory Controller Functions> ***********/
566
Thor Thayerc3eea192016-02-10 13:26:21 -0600567/************************* EDAC Parent Probe *************************/
568
569static const struct of_device_id altr_edac_device_of_match[];
570
571static const struct of_device_id altr_edac_of_match[] = {
572 { .compatible = "altr,socfpga-ecc-manager" },
573 {},
574};
575MODULE_DEVICE_TABLE(of, altr_edac_of_match);
576
577static int altr_edac_probe(struct platform_device *pdev)
578{
579 of_platform_populate(pdev->dev.of_node, altr_edac_device_of_match,
580 NULL, &pdev->dev);
581 return 0;
582}
583
584static struct platform_driver altr_edac_driver = {
585 .probe = altr_edac_probe,
586 .driver = {
587 .name = "socfpga_ecc_manager",
588 .of_match_table = altr_edac_of_match,
589 },
590};
591module_platform_driver(altr_edac_driver);
592
593/************************* EDAC Device Functions *************************/
594
595/*
596 * EDAC Device Functions (shared between various IPs).
597 * The discrete memories use the EDAC Device framework. The probe
598 * and error handling functions are very similar between memories
599 * so they are shared. The memory allocation and freeing for EDAC
600 * trigger testing are different for each memory.
601 */
602
Thor Thayer1cf70372016-06-22 08:58:54 -0500603static const struct edac_device_prv_data ocramecc_data;
604static const struct edac_device_prv_data l2ecc_data;
605static const struct edac_device_prv_data a10_ocramecc_data;
606static const struct edac_device_prv_data a10_l2ecc_data;
Thor Thayerc3eea192016-02-10 13:26:21 -0600607
Thor Thayerc3eea192016-02-10 13:26:21 -0600608static irqreturn_t altr_edac_device_handler(int irq, void *dev_id)
609{
610 irqreturn_t ret_value = IRQ_NONE;
611 struct edac_device_ctl_info *dci = dev_id;
612 struct altr_edac_device_dev *drvdata = dci->pvt_info;
613 const struct edac_device_prv_data *priv = drvdata->data;
614
615 if (irq == drvdata->sb_irq) {
616 if (priv->ce_clear_mask)
617 writel(priv->ce_clear_mask, drvdata->base);
618 edac_device_handle_ce(dci, 0, 0, drvdata->edac_dev_name);
619 ret_value = IRQ_HANDLED;
620 } else if (irq == drvdata->db_irq) {
621 if (priv->ue_clear_mask)
622 writel(priv->ue_clear_mask, drvdata->base);
623 edac_device_handle_ue(dci, 0, 0, drvdata->edac_dev_name);
624 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
625 ret_value = IRQ_HANDLED;
626 } else {
627 WARN_ON(1);
628 }
629
630 return ret_value;
631}
632
633static ssize_t altr_edac_device_trig(struct file *file,
634 const char __user *user_buf,
635 size_t count, loff_t *ppos)
636
637{
638 u32 *ptemp, i, error_mask;
639 int result = 0;
640 u8 trig_type;
641 unsigned long flags;
642 struct edac_device_ctl_info *edac_dci = file->private_data;
643 struct altr_edac_device_dev *drvdata = edac_dci->pvt_info;
644 const struct edac_device_prv_data *priv = drvdata->data;
645 void *generic_ptr = edac_dci->dev;
646
647 if (!user_buf || get_user(trig_type, user_buf))
648 return -EFAULT;
649
650 if (!priv->alloc_mem)
651 return -ENOMEM;
652
653 /*
654 * Note that generic_ptr is initialized to the device * but in
655 * some alloc_functions, this is overridden and returns data.
656 */
657 ptemp = priv->alloc_mem(priv->trig_alloc_sz, &generic_ptr);
658 if (!ptemp) {
659 edac_printk(KERN_ERR, EDAC_DEVICE,
660 "Inject: Buffer Allocation error\n");
661 return -ENOMEM;
662 }
663
664 if (trig_type == ALTR_UE_TRIGGER_CHAR)
665 error_mask = priv->ue_set_mask;
666 else
667 error_mask = priv->ce_set_mask;
668
669 edac_printk(KERN_ALERT, EDAC_DEVICE,
670 "Trigger Error Mask (0x%X)\n", error_mask);
671
672 local_irq_save(flags);
673 /* write ECC corrupted data out. */
674 for (i = 0; i < (priv->trig_alloc_sz / sizeof(*ptemp)); i++) {
675 /* Read data so we're in the correct state */
676 rmb();
Mark Rutland332efa62017-10-23 14:07:12 -0700677 if (READ_ONCE(ptemp[i]))
Thor Thayerc3eea192016-02-10 13:26:21 -0600678 result = -1;
679 /* Toggle Error bit (it is latched), leave ECC enabled */
Thor Thayer811fce42016-03-21 11:01:42 -0500680 writel(error_mask, (drvdata->base + priv->set_err_ofst));
681 writel(priv->ecc_enable_mask, (drvdata->base +
682 priv->set_err_ofst));
Thor Thayerc3eea192016-02-10 13:26:21 -0600683 ptemp[i] = i;
684 }
685 /* Ensure it has been written out */
686 wmb();
687 local_irq_restore(flags);
688
689 if (result)
690 edac_printk(KERN_ERR, EDAC_DEVICE, "Mem Not Cleared\n");
691
692 /* Read out written data. ECC error caused here */
693 for (i = 0; i < ALTR_TRIGGER_READ_WRD_CNT; i++)
Mark Rutland332efa62017-10-23 14:07:12 -0700694 if (READ_ONCE(ptemp[i]) != i)
Thor Thayerc3eea192016-02-10 13:26:21 -0600695 edac_printk(KERN_ERR, EDAC_DEVICE,
696 "Read doesn't match written data\n");
697
698 if (priv->free_mem)
699 priv->free_mem(ptemp, priv->trig_alloc_sz, generic_ptr);
700
701 return count;
702}
703
704static const struct file_operations altr_edac_device_inject_fops = {
705 .open = simple_open,
706 .write = altr_edac_device_trig,
707 .llseek = generic_file_llseek,
708};
709
Thor Thayerc7b4be82016-04-06 20:22:54 -0500710static ssize_t altr_edac_a10_device_trig(struct file *file,
711 const char __user *user_buf,
712 size_t count, loff_t *ppos);
713
714static const struct file_operations altr_edac_a10_device_inject_fops = {
715 .open = simple_open,
716 .write = altr_edac_a10_device_trig,
717 .llseek = generic_file_llseek,
718};
719
Thor Thayer064acbd2018-09-25 08:49:01 -0500720static ssize_t altr_edac_a10_device_trig2(struct file *file,
721 const char __user *user_buf,
722 size_t count, loff_t *ppos);
723
724static const struct file_operations altr_edac_a10_device_inject2_fops = {
725 .open = simple_open,
726 .write = altr_edac_a10_device_trig2,
727 .llseek = generic_file_llseek,
728};
729
Thor Thayerc3eea192016-02-10 13:26:21 -0600730static void altr_create_edacdev_dbgfs(struct edac_device_ctl_info *edac_dci,
731 const struct edac_device_prv_data *priv)
732{
733 struct altr_edac_device_dev *drvdata = edac_dci->pvt_info;
734
735 if (!IS_ENABLED(CONFIG_EDAC_DEBUG))
736 return;
737
738 drvdata->debugfs_dir = edac_debugfs_create_dir(drvdata->edac_dev_name);
739 if (!drvdata->debugfs_dir)
740 return;
741
Thor Thayerf399f342016-08-19 11:04:25 -0500742 if (!edac_debugfs_create_file("altr_trigger", S_IWUSR,
Thor Thayerc3eea192016-02-10 13:26:21 -0600743 drvdata->debugfs_dir, edac_dci,
Thor Thayere17ced22016-03-31 13:48:01 -0500744 priv->inject_fops))
Thor Thayerc3eea192016-02-10 13:26:21 -0600745 debugfs_remove_recursive(drvdata->debugfs_dir);
746}
747
748static const struct of_device_id altr_edac_device_of_match[] = {
749#ifdef CONFIG_EDAC_ALTERA_L2C
Arnd Bergmann2c911f62016-04-16 22:13:55 +0200750 { .compatible = "altr,socfpga-l2-ecc", .data = &l2ecc_data },
Thor Thayerc3eea192016-02-10 13:26:21 -0600751#endif
752#ifdef CONFIG_EDAC_ALTERA_OCRAM
Arnd Bergmann2c911f62016-04-16 22:13:55 +0200753 { .compatible = "altr,socfpga-ocram-ecc", .data = &ocramecc_data },
Thor Thayerc3eea192016-02-10 13:26:21 -0600754#endif
755 {},
756};
757MODULE_DEVICE_TABLE(of, altr_edac_device_of_match);
758
759/*
760 * altr_edac_device_probe()
761 * This is a generic EDAC device driver that will support
762 * various Altera memory devices such as the L2 cache ECC and
763 * OCRAM ECC as well as the memories for other peripherals.
764 * Module specific initialization is done by passing the
765 * function index in the device tree.
766 */
767static int altr_edac_device_probe(struct platform_device *pdev)
768{
769 struct edac_device_ctl_info *dci;
770 struct altr_edac_device_dev *drvdata;
771 struct resource *r;
772 int res = 0;
773 struct device_node *np = pdev->dev.of_node;
774 char *ecc_name = (char *)np->name;
775 static int dev_instance;
776
777 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
778 edac_printk(KERN_ERR, EDAC_DEVICE,
779 "Unable to open devm\n");
780 return -ENOMEM;
781 }
782
783 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
784 if (!r) {
785 edac_printk(KERN_ERR, EDAC_DEVICE,
786 "Unable to get mem resource\n");
787 res = -ENODEV;
788 goto fail;
789 }
790
791 if (!devm_request_mem_region(&pdev->dev, r->start, resource_size(r),
792 dev_name(&pdev->dev))) {
793 edac_printk(KERN_ERR, EDAC_DEVICE,
794 "%s:Error requesting mem region\n", ecc_name);
795 res = -EBUSY;
796 goto fail;
797 }
798
799 dci = edac_device_alloc_ctl_info(sizeof(*drvdata), ecc_name,
800 1, ecc_name, 1, 0, NULL, 0,
801 dev_instance++);
802
803 if (!dci) {
804 edac_printk(KERN_ERR, EDAC_DEVICE,
805 "%s: Unable to allocate EDAC device\n", ecc_name);
806 res = -ENOMEM;
807 goto fail;
808 }
809
810 drvdata = dci->pvt_info;
811 dci->dev = &pdev->dev;
812 platform_set_drvdata(pdev, dci);
813 drvdata->edac_dev_name = ecc_name;
814
815 drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
Christophe JAILLET8b073d92017-08-16 07:05:06 +0200816 if (!drvdata->base) {
817 res = -ENOMEM;
Thor Thayerc3eea192016-02-10 13:26:21 -0600818 goto fail1;
Christophe JAILLET8b073d92017-08-16 07:05:06 +0200819 }
Thor Thayerc3eea192016-02-10 13:26:21 -0600820
821 /* Get driver specific data for this EDAC device */
822 drvdata->data = of_match_node(altr_edac_device_of_match, np)->data;
823
824 /* Check specific dependencies for the module */
825 if (drvdata->data->setup) {
Thor Thayer328ca7a2016-03-21 11:01:40 -0500826 res = drvdata->data->setup(drvdata);
Thor Thayerc3eea192016-02-10 13:26:21 -0600827 if (res)
828 goto fail1;
829 }
830
831 drvdata->sb_irq = platform_get_irq(pdev, 0);
832 res = devm_request_irq(&pdev->dev, drvdata->sb_irq,
833 altr_edac_device_handler,
834 0, dev_name(&pdev->dev), dci);
835 if (res)
836 goto fail1;
837
838 drvdata->db_irq = platform_get_irq(pdev, 1);
839 res = devm_request_irq(&pdev->dev, drvdata->db_irq,
840 altr_edac_device_handler,
841 0, dev_name(&pdev->dev), dci);
842 if (res)
843 goto fail1;
844
845 dci->mod_name = "Altera ECC Manager";
846 dci->dev_name = drvdata->edac_dev_name;
847
848 res = edac_device_add_device(dci);
849 if (res)
850 goto fail1;
851
852 altr_create_edacdev_dbgfs(dci, drvdata->data);
853
854 devres_close_group(&pdev->dev, NULL);
855
856 return 0;
857
858fail1:
859 edac_device_free_ctl_info(dci);
860fail:
861 devres_release_group(&pdev->dev, NULL);
862 edac_printk(KERN_ERR, EDAC_DEVICE,
863 "%s:Error setting up EDAC device: %d\n", ecc_name, res);
864
865 return res;
866}
867
868static int altr_edac_device_remove(struct platform_device *pdev)
869{
870 struct edac_device_ctl_info *dci = platform_get_drvdata(pdev);
871 struct altr_edac_device_dev *drvdata = dci->pvt_info;
872
873 debugfs_remove_recursive(drvdata->debugfs_dir);
874 edac_device_del_device(&pdev->dev);
875 edac_device_free_ctl_info(dci);
876
877 return 0;
878}
879
880static struct platform_driver altr_edac_device_driver = {
881 .probe = altr_edac_device_probe,
882 .remove = altr_edac_device_remove,
883 .driver = {
884 .name = "altr_edac_device",
885 .of_match_table = altr_edac_device_of_match,
886 },
887};
888module_platform_driver(altr_edac_device_driver);
889
Thor Thayer6b300fb2016-06-22 08:58:55 -0500890/******************* Arria10 Device ECC Shared Functions *****************/
Thor Thayerc3eea192016-02-10 13:26:21 -0600891
Arnd Bergmann1aa6eb52016-04-16 22:13:56 +0200892/*
893 * Test for memory's ECC dependencies upon entry because platform specific
894 * startup should have initialized the memory and enabled the ECC.
895 * Can't turn on ECC here because accessing un-initialized memory will
896 * cause CE/UE errors possibly causing an ABORT.
897 */
Thor Thayer6b300fb2016-06-22 08:58:55 -0500898static int __maybe_unused
899altr_check_ecc_deps(struct altr_edac_device_dev *device)
Arnd Bergmann1aa6eb52016-04-16 22:13:56 +0200900{
901 void __iomem *base = device->base;
902 const struct edac_device_prv_data *prv = device->data;
903
904 if (readl(base + prv->ecc_en_ofst) & prv->ecc_enable_mask)
905 return 0;
906
907 edac_printk(KERN_ERR, EDAC_DEVICE,
908 "%s: No ECC present or ECC disabled.\n",
909 device->edac_dev_name);
910 return -ENODEV;
911}
Thor Thayerc3eea192016-02-10 13:26:21 -0600912
Thor Thayer6b300fb2016-06-22 08:58:55 -0500913static irqreturn_t __maybe_unused altr_edac_a10_ecc_irq(int irq, void *dev_id)
914{
915 struct altr_edac_device_dev *dci = dev_id;
916 void __iomem *base = dci->base;
917
918 if (irq == dci->sb_irq) {
919 writel(ALTR_A10_ECC_SERRPENA,
920 base + ALTR_A10_ECC_INTSTAT_OFST);
921 edac_device_handle_ce(dci->edac_dev, 0, 0, dci->edac_dev_name);
922
923 return IRQ_HANDLED;
924 } else if (irq == dci->db_irq) {
925 writel(ALTR_A10_ECC_DERRPENA,
926 base + ALTR_A10_ECC_INTSTAT_OFST);
927 edac_device_handle_ue(dci->edac_dev, 0, 0, dci->edac_dev_name);
928 if (dci->data->panic)
929 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
930
931 return IRQ_HANDLED;
932 }
933
934 WARN_ON(1);
935
936 return IRQ_NONE;
937}
938
Thor Thayer1166fde92016-06-22 08:58:57 -0500939/******************* Arria10 Memory Buffer Functions *********************/
940
941static inline int a10_get_irq_mask(struct device_node *np)
942{
943 int irq;
944 const u32 *handle = of_get_property(np, "interrupts", NULL);
945
946 if (!handle)
947 return -ENODEV;
948 irq = be32_to_cpup(handle);
949 return irq;
950}
951
952static inline void ecc_set_bits(u32 bit_mask, void __iomem *ioaddr)
953{
954 u32 value = readl(ioaddr);
955
956 value |= bit_mask;
957 writel(value, ioaddr);
958}
959
960static inline void ecc_clear_bits(u32 bit_mask, void __iomem *ioaddr)
961{
962 u32 value = readl(ioaddr);
963
964 value &= ~bit_mask;
965 writel(value, ioaddr);
966}
967
968static inline int ecc_test_bits(u32 bit_mask, void __iomem *ioaddr)
969{
970 u32 value = readl(ioaddr);
971
972 return (value & bit_mask) ? 1 : 0;
973}
974
975/*
976 * This function uses the memory initialization block in the Arria10 ECC
977 * controller to initialize/clear the entire memory data and ECC data.
978 */
979static int __maybe_unused altr_init_memory_port(void __iomem *ioaddr, int port)
980{
981 int limit = ALTR_A10_ECC_INIT_WATCHDOG_10US;
982 u32 init_mask, stat_mask, clear_mask;
983 int ret = 0;
984
985 if (port) {
986 init_mask = ALTR_A10_ECC_INITB;
987 stat_mask = ALTR_A10_ECC_INITCOMPLETEB;
988 clear_mask = ALTR_A10_ECC_ERRPENB_MASK;
989 } else {
990 init_mask = ALTR_A10_ECC_INITA;
991 stat_mask = ALTR_A10_ECC_INITCOMPLETEA;
992 clear_mask = ALTR_A10_ECC_ERRPENA_MASK;
993 }
994
995 ecc_set_bits(init_mask, (ioaddr + ALTR_A10_ECC_CTRL_OFST));
996 while (limit--) {
997 if (ecc_test_bits(stat_mask,
998 (ioaddr + ALTR_A10_ECC_INITSTAT_OFST)))
999 break;
1000 udelay(1);
1001 }
1002 if (limit < 0)
1003 ret = -EBUSY;
1004
1005 /* Clear any pending ECC interrupts */
1006 writel(clear_mask, (ioaddr + ALTR_A10_ECC_INTSTAT_OFST));
1007
1008 return ret;
1009}
1010
Thor Thayer064acbd2018-09-25 08:49:01 -05001011static int socfpga_is_a10(void)
1012{
1013 return of_machine_is_compatible("altr,socfpga-arria10");
1014}
1015
1016static int socfpga_is_s10(void)
1017{
1018 return of_machine_is_compatible("altr,socfpga-stratix10");
1019}
1020
Thor Thayer1166fde92016-06-22 08:58:57 -05001021static __init int __maybe_unused
1022altr_init_a10_ecc_block(struct device_node *np, u32 irq_mask,
1023 u32 ecc_ctrl_en_mask, bool dual_port)
1024{
1025 int ret = 0;
1026 void __iomem *ecc_block_base;
1027 struct regmap *ecc_mgr_map;
1028 char *ecc_name;
1029 struct device_node *np_eccmgr;
1030
1031 ecc_name = (char *)np->name;
1032
1033 /* Get the ECC Manager - parent of the device EDACs */
1034 np_eccmgr = of_get_parent(np);
Thor Thayer064acbd2018-09-25 08:49:01 -05001035
1036 if (socfpga_is_a10()) {
1037 ecc_mgr_map = syscon_regmap_lookup_by_phandle(np_eccmgr,
1038 "altr,sysmgr-syscon");
1039 } else {
1040 struct device_node *sysmgr_np;
1041 struct resource res;
Arnd Bergmann8537bf102018-09-27 12:09:26 +02001042 uintptr_t base;
Thor Thayer064acbd2018-09-25 08:49:01 -05001043
1044 sysmgr_np = of_parse_phandle(np_eccmgr,
1045 "altr,sysmgr-syscon", 0);
1046 if (!sysmgr_np) {
1047 edac_printk(KERN_ERR, EDAC_DEVICE,
1048 "Unable to find altr,sysmgr-syscon\n");
1049 return -ENODEV;
1050 }
1051
Huang Zijiang7f736592019-02-14 14:39:07 +08001052 if (of_address_to_resource(sysmgr_np, 0, &res)) {
1053 of_node_put(sysmgr_np);
Thor Thayer064acbd2018-09-25 08:49:01 -05001054 return -ENOMEM;
Huang Zijiang7f736592019-02-14 14:39:07 +08001055 }
Thor Thayer064acbd2018-09-25 08:49:01 -05001056
1057 /* Need physical address for SMCC call */
Arnd Bergmann8537bf102018-09-27 12:09:26 +02001058 base = res.start;
Thor Thayer064acbd2018-09-25 08:49:01 -05001059
Arnd Bergmann8537bf102018-09-27 12:09:26 +02001060 ecc_mgr_map = regmap_init(NULL, NULL, (void *)base,
Thor Thayer064acbd2018-09-25 08:49:01 -05001061 &s10_sdram_regmap_cfg);
Huang Zijiang7f736592019-02-14 14:39:07 +08001062 of_node_put(sysmgr_np);
Thor Thayer064acbd2018-09-25 08:49:01 -05001063 }
Thor Thayer1166fde92016-06-22 08:58:57 -05001064 of_node_put(np_eccmgr);
1065 if (IS_ERR(ecc_mgr_map)) {
1066 edac_printk(KERN_ERR, EDAC_DEVICE,
1067 "Unable to get syscon altr,sysmgr-syscon\n");
1068 return -ENODEV;
1069 }
1070
1071 /* Map the ECC Block */
1072 ecc_block_base = of_iomap(np, 0);
1073 if (!ecc_block_base) {
1074 edac_printk(KERN_ERR, EDAC_DEVICE,
1075 "Unable to map %s ECC block\n", ecc_name);
1076 return -ENODEV;
1077 }
1078
1079 /* Disable ECC */
1080 regmap_write(ecc_mgr_map, A10_SYSMGR_ECC_INTMASK_SET_OFST, irq_mask);
1081 writel(ALTR_A10_ECC_SERRINTEN,
1082 (ecc_block_base + ALTR_A10_ECC_ERRINTENR_OFST));
1083 ecc_clear_bits(ecc_ctrl_en_mask,
1084 (ecc_block_base + ALTR_A10_ECC_CTRL_OFST));
1085 /* Ensure all writes complete */
1086 wmb();
1087 /* Use HW initialization block to initialize memory for ECC */
1088 ret = altr_init_memory_port(ecc_block_base, 0);
1089 if (ret) {
1090 edac_printk(KERN_ERR, EDAC_DEVICE,
1091 "ECC: cannot init %s PORTA memory\n", ecc_name);
1092 goto out;
1093 }
1094
1095 if (dual_port) {
1096 ret = altr_init_memory_port(ecc_block_base, 1);
1097 if (ret) {
1098 edac_printk(KERN_ERR, EDAC_DEVICE,
1099 "ECC: cannot init %s PORTB memory\n",
1100 ecc_name);
1101 goto out;
1102 }
1103 }
1104
1105 /* Interrupt mode set to every SBERR */
1106 regmap_write(ecc_mgr_map, ALTR_A10_ECC_INTMODE_OFST,
1107 ALTR_A10_ECC_INTMODE);
1108 /* Enable ECC */
1109 ecc_set_bits(ecc_ctrl_en_mask, (ecc_block_base +
1110 ALTR_A10_ECC_CTRL_OFST));
1111 writel(ALTR_A10_ECC_SERRINTEN,
1112 (ecc_block_base + ALTR_A10_ECC_ERRINTENS_OFST));
1113 regmap_write(ecc_mgr_map, A10_SYSMGR_ECC_INTMASK_CLR_OFST, irq_mask);
1114 /* Ensure all writes complete */
1115 wmb();
1116out:
1117 iounmap(ecc_block_base);
1118 return ret;
1119}
1120
1121static int validate_parent_available(struct device_node *np);
1122static const struct of_device_id altr_edac_a10_device_of_match[];
1123static int __init __maybe_unused altr_init_a10_ecc_device_type(char *compat)
1124{
1125 int irq;
Thor Thayer25b223d2017-04-05 13:01:02 -05001126 struct device_node *child, *np;
1127
Thor Thayer064acbd2018-09-25 08:49:01 -05001128 if (!socfpga_is_a10() && !socfpga_is_s10())
Thor Thayer25b223d2017-04-05 13:01:02 -05001129 return -ENODEV;
1130
1131 np = of_find_compatible_node(NULL, NULL,
1132 "altr,socfpga-a10-ecc-manager");
Thor Thayer1166fde92016-06-22 08:58:57 -05001133 if (!np) {
1134 edac_printk(KERN_ERR, EDAC_DEVICE, "ECC Manager not found\n");
1135 return -ENODEV;
1136 }
1137
1138 for_each_child_of_node(np, child) {
1139 const struct of_device_id *pdev_id;
1140 const struct edac_device_prv_data *prv;
1141
1142 if (!of_device_is_available(child))
1143 continue;
1144 if (!of_device_is_compatible(child, compat))
1145 continue;
1146
1147 if (validate_parent_available(child))
1148 continue;
1149
1150 irq = a10_get_irq_mask(child);
1151 if (irq < 0)
1152 continue;
1153
1154 /* Get matching node and check for valid result */
1155 pdev_id = of_match_node(altr_edac_a10_device_of_match, child);
1156 if (IS_ERR_OR_NULL(pdev_id))
1157 continue;
1158
1159 /* Validate private data pointer before dereferencing */
1160 prv = pdev_id->data;
1161 if (!prv)
1162 continue;
1163
1164 altr_init_a10_ecc_block(child, BIT(irq),
1165 prv->ecc_enable_mask, 0);
1166 }
1167
1168 of_node_put(np);
1169 return 0;
1170}
1171
Thor Thayer6b300fb2016-06-22 08:58:55 -05001172/*********************** OCRAM EDAC Device Functions *********************/
1173
1174#ifdef CONFIG_EDAC_ALTERA_OCRAM
1175
Thor Thayerc3eea192016-02-10 13:26:21 -06001176static void *ocram_alloc_mem(size_t size, void **other)
1177{
1178 struct device_node *np;
1179 struct gen_pool *gp;
1180 void *sram_addr;
1181
1182 np = of_find_compatible_node(NULL, NULL, "altr,socfpga-ocram-ecc");
1183 if (!np)
1184 return NULL;
1185
1186 gp = of_gen_pool_get(np, "iram", 0);
1187 of_node_put(np);
1188 if (!gp)
1189 return NULL;
1190
1191 sram_addr = (void *)gen_pool_alloc(gp, size);
1192 if (!sram_addr)
1193 return NULL;
1194
1195 memset(sram_addr, 0, size);
1196 /* Ensure data is written out */
1197 wmb();
1198
1199 /* Remember this handle for freeing later */
1200 *other = gp;
1201
1202 return sram_addr;
1203}
1204
1205static void ocram_free_mem(void *p, size_t size, void *other)
1206{
Thor Thayer9ef20752018-05-14 12:04:01 -05001207 gen_pool_free((struct gen_pool *)other, (unsigned long)p, size);
Thor Thayerc3eea192016-02-10 13:26:21 -06001208}
1209
Thor Thayer1cf70372016-06-22 08:58:54 -05001210static const struct edac_device_prv_data ocramecc_data = {
Thor Thayeraa1f06d2016-03-31 13:48:03 -05001211 .setup = altr_check_ecc_deps,
Thor Thayerc3eea192016-02-10 13:26:21 -06001212 .ce_clear_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_SERR),
1213 .ue_clear_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_DERR),
Thor Thayerc3eea192016-02-10 13:26:21 -06001214 .alloc_mem = ocram_alloc_mem,
1215 .free_mem = ocram_free_mem,
1216 .ecc_enable_mask = ALTR_OCR_ECC_EN,
Thor Thayer943ad912016-03-31 13:48:02 -05001217 .ecc_en_ofst = ALTR_OCR_ECC_REG_OFFSET,
Thor Thayerc3eea192016-02-10 13:26:21 -06001218 .ce_set_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_INJS),
1219 .ue_set_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_INJD),
Thor Thayer811fce42016-03-21 11:01:42 -05001220 .set_err_ofst = ALTR_OCR_ECC_REG_OFFSET,
Thor Thayerc3eea192016-02-10 13:26:21 -06001221 .trig_alloc_sz = ALTR_TRIG_OCRAM_BYTE_SIZE,
Thor Thayere17ced22016-03-31 13:48:01 -05001222 .inject_fops = &altr_edac_device_inject_fops,
Thor Thayerc3eea192016-02-10 13:26:21 -06001223};
1224
Thor Thayer1cf70372016-06-22 08:58:54 -05001225static const struct edac_device_prv_data a10_ocramecc_data = {
Thor Thayerc7b4be82016-04-06 20:22:54 -05001226 .setup = altr_check_ecc_deps,
1227 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1228 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
1229 .irq_status_mask = A10_SYSMGR_ECC_INTSTAT_OCRAM,
Thor Thayerc7b4be82016-04-06 20:22:54 -05001230 .ecc_enable_mask = ALTR_A10_OCRAM_ECC_EN_CTL,
1231 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1232 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1233 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1234 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1235 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1236 .inject_fops = &altr_edac_a10_device_inject_fops,
Thor Thayer2b083d62016-06-22 08:58:53 -05001237 /*
1238 * OCRAM panic on uncorrectable error because sleep/resume
1239 * functions and FPGA contents are stored in OCRAM. Prefer
1240 * a kernel panic over executing/loading corrupted data.
1241 */
1242 .panic = true,
Thor Thayerc7b4be82016-04-06 20:22:54 -05001243};
1244
Thor Thayerc3eea192016-02-10 13:26:21 -06001245#endif /* CONFIG_EDAC_ALTERA_OCRAM */
1246
1247/********************* L2 Cache EDAC Device Functions ********************/
1248
1249#ifdef CONFIG_EDAC_ALTERA_L2C
1250
1251static void *l2_alloc_mem(size_t size, void **other)
1252{
1253 struct device *dev = *other;
1254 void *ptemp = devm_kzalloc(dev, size, GFP_KERNEL);
1255
1256 if (!ptemp)
1257 return NULL;
1258
1259 /* Make sure everything is written out */
1260 wmb();
1261
1262 /*
1263 * Clean all cache levels up to LoC (includes L2)
1264 * This ensures the corrupted data is written into
1265 * L2 cache for readback test (which causes ECC error).
1266 */
1267 flush_cache_all();
1268
1269 return ptemp;
1270}
1271
1272static void l2_free_mem(void *p, size_t size, void *other)
1273{
1274 struct device *dev = other;
1275
1276 if (dev && p)
1277 devm_kfree(dev, p);
1278}
1279
1280/*
1281 * altr_l2_check_deps()
1282 * Test for L2 cache ECC dependencies upon entry because
1283 * platform specific startup should have initialized the L2
1284 * memory and enabled the ECC.
1285 * Bail if ECC is not enabled.
1286 * Note that L2 Cache Enable is forced at build time.
1287 */
Thor Thayer328ca7a2016-03-21 11:01:40 -05001288static int altr_l2_check_deps(struct altr_edac_device_dev *device)
Thor Thayerc3eea192016-02-10 13:26:21 -06001289{
Thor Thayer328ca7a2016-03-21 11:01:40 -05001290 void __iomem *base = device->base;
Thor Thayer27439a12016-03-21 11:01:41 -05001291 const struct edac_device_prv_data *prv = device->data;
1292
1293 if ((readl(base) & prv->ecc_enable_mask) ==
1294 prv->ecc_enable_mask)
Thor Thayerc3eea192016-02-10 13:26:21 -06001295 return 0;
1296
1297 edac_printk(KERN_ERR, EDAC_DEVICE,
1298 "L2: No ECC present, or ECC disabled\n");
1299 return -ENODEV;
1300}
1301
Thor Thayer13ab8442016-06-07 15:35:57 -05001302static irqreturn_t altr_edac_a10_l2_irq(int irq, void *dev_id)
Thor Thayer588cb032016-03-21 11:01:44 -05001303{
Thor Thayer13ab8442016-06-07 15:35:57 -05001304 struct altr_edac_device_dev *dci = dev_id;
1305
1306 if (irq == dci->sb_irq) {
Thor Thayer588cb032016-03-21 11:01:44 -05001307 regmap_write(dci->edac->ecc_mgr_map,
1308 A10_SYSGMR_MPU_CLEAR_L2_ECC_OFST,
1309 A10_SYSGMR_MPU_CLEAR_L2_ECC_SB);
1310 edac_device_handle_ce(dci->edac_dev, 0, 0, dci->edac_dev_name);
Thor Thayer13ab8442016-06-07 15:35:57 -05001311
1312 return IRQ_HANDLED;
1313 } else if (irq == dci->db_irq) {
Thor Thayer588cb032016-03-21 11:01:44 -05001314 regmap_write(dci->edac->ecc_mgr_map,
1315 A10_SYSGMR_MPU_CLEAR_L2_ECC_OFST,
1316 A10_SYSGMR_MPU_CLEAR_L2_ECC_MB);
1317 edac_device_handle_ue(dci->edac_dev, 0, 0, dci->edac_dev_name);
1318 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
Thor Thayer13ab8442016-06-07 15:35:57 -05001319
1320 return IRQ_HANDLED;
Thor Thayer588cb032016-03-21 11:01:44 -05001321 }
Thor Thayer13ab8442016-06-07 15:35:57 -05001322
1323 WARN_ON(1);
1324
1325 return IRQ_NONE;
Thor Thayer588cb032016-03-21 11:01:44 -05001326}
1327
Thor Thayer1cf70372016-06-22 08:58:54 -05001328static const struct edac_device_prv_data l2ecc_data = {
Thor Thayerc3eea192016-02-10 13:26:21 -06001329 .setup = altr_l2_check_deps,
1330 .ce_clear_mask = 0,
1331 .ue_clear_mask = 0,
Thor Thayerc3eea192016-02-10 13:26:21 -06001332 .alloc_mem = l2_alloc_mem,
1333 .free_mem = l2_free_mem,
1334 .ecc_enable_mask = ALTR_L2_ECC_EN,
1335 .ce_set_mask = (ALTR_L2_ECC_EN | ALTR_L2_ECC_INJS),
1336 .ue_set_mask = (ALTR_L2_ECC_EN | ALTR_L2_ECC_INJD),
Thor Thayer811fce42016-03-21 11:01:42 -05001337 .set_err_ofst = ALTR_L2_ECC_REG_OFFSET,
Thor Thayerc3eea192016-02-10 13:26:21 -06001338 .trig_alloc_sz = ALTR_TRIG_L2C_BYTE_SIZE,
Thor Thayere17ced22016-03-31 13:48:01 -05001339 .inject_fops = &altr_edac_device_inject_fops,
Thor Thayerc3eea192016-02-10 13:26:21 -06001340};
1341
Thor Thayer1cf70372016-06-22 08:58:54 -05001342static const struct edac_device_prv_data a10_l2ecc_data = {
Thor Thayer588cb032016-03-21 11:01:44 -05001343 .setup = altr_l2_check_deps,
1344 .ce_clear_mask = ALTR_A10_L2_ECC_SERR_CLR,
1345 .ue_clear_mask = ALTR_A10_L2_ECC_MERR_CLR,
1346 .irq_status_mask = A10_SYSMGR_ECC_INTSTAT_L2,
Thor Thayer588cb032016-03-21 11:01:44 -05001347 .alloc_mem = l2_alloc_mem,
1348 .free_mem = l2_free_mem,
1349 .ecc_enable_mask = ALTR_A10_L2_ECC_EN_CTL,
1350 .ce_set_mask = ALTR_A10_L2_ECC_CE_INJ_MASK,
1351 .ue_set_mask = ALTR_A10_L2_ECC_UE_INJ_MASK,
1352 .set_err_ofst = ALTR_A10_L2_ECC_INJ_OFST,
1353 .ecc_irq_handler = altr_edac_a10_l2_irq,
1354 .trig_alloc_sz = ALTR_TRIG_L2C_BYTE_SIZE,
Thor Thayere17ced22016-03-31 13:48:01 -05001355 .inject_fops = &altr_edac_device_inject_fops,
Thor Thayer588cb032016-03-21 11:01:44 -05001356};
1357
Thor Thayerc3eea192016-02-10 13:26:21 -06001358#endif /* CONFIG_EDAC_ALTERA_L2C */
1359
Thor Thayerab8c1e02016-06-22 08:58:58 -05001360/********************* Ethernet Device Functions ********************/
1361
1362#ifdef CONFIG_EDAC_ALTERA_ETHERNET
1363
1364static const struct edac_device_prv_data a10_enetecc_data = {
1365 .setup = altr_check_ecc_deps,
1366 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1367 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
Thor Thayerab8c1e02016-06-22 08:58:58 -05001368 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1369 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1370 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1371 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1372 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1373 .ecc_irq_handler = altr_edac_a10_ecc_irq,
Thor Thayer064acbd2018-09-25 08:49:01 -05001374 .inject_fops = &altr_edac_a10_device_inject2_fops,
Thor Thayerab8c1e02016-06-22 08:58:58 -05001375};
1376
1377static int __init socfpga_init_ethernet_ecc(void)
1378{
1379 return altr_init_a10_ecc_device_type("altr,socfpga-eth-mac-ecc");
1380}
1381
1382early_initcall(socfpga_init_ethernet_ecc);
1383
1384#endif /* CONFIG_EDAC_ALTERA_ETHERNET */
1385
Thor Thayerc6882fb2016-07-14 11:06:43 -05001386/********************** NAND Device Functions **********************/
1387
1388#ifdef CONFIG_EDAC_ALTERA_NAND
1389
1390static const struct edac_device_prv_data a10_nandecc_data = {
1391 .setup = altr_check_ecc_deps,
1392 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1393 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
Thor Thayerc6882fb2016-07-14 11:06:43 -05001394 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1395 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1396 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1397 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1398 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1399 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1400 .inject_fops = &altr_edac_a10_device_inject_fops,
1401};
1402
1403static int __init socfpga_init_nand_ecc(void)
1404{
1405 return altr_init_a10_ecc_device_type("altr,socfpga-nand-ecc");
1406}
1407
1408early_initcall(socfpga_init_nand_ecc);
1409
1410#endif /* CONFIG_EDAC_ALTERA_NAND */
1411
Thor Thayere8263792016-07-28 10:03:57 +02001412/********************** DMA Device Functions **********************/
1413
1414#ifdef CONFIG_EDAC_ALTERA_DMA
1415
1416static const struct edac_device_prv_data a10_dmaecc_data = {
1417 .setup = altr_check_ecc_deps,
1418 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1419 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
Thor Thayere8263792016-07-28 10:03:57 +02001420 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1421 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1422 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1423 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1424 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1425 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1426 .inject_fops = &altr_edac_a10_device_inject_fops,
1427};
1428
1429static int __init socfpga_init_dma_ecc(void)
1430{
1431 return altr_init_a10_ecc_device_type("altr,socfpga-dma-ecc");
1432}
1433
1434early_initcall(socfpga_init_dma_ecc);
1435
1436#endif /* CONFIG_EDAC_ALTERA_DMA */
1437
Thor Thayerc6095812016-07-14 11:06:45 -05001438/********************** USB Device Functions **********************/
1439
1440#ifdef CONFIG_EDAC_ALTERA_USB
1441
1442static const struct edac_device_prv_data a10_usbecc_data = {
1443 .setup = altr_check_ecc_deps,
1444 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1445 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
Thor Thayerc6095812016-07-14 11:06:45 -05001446 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1447 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1448 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1449 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1450 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1451 .ecc_irq_handler = altr_edac_a10_ecc_irq,
Thor Thayer064acbd2018-09-25 08:49:01 -05001452 .inject_fops = &altr_edac_a10_device_inject2_fops,
Thor Thayerc6095812016-07-14 11:06:45 -05001453};
1454
1455static int __init socfpga_init_usb_ecc(void)
1456{
1457 return altr_init_a10_ecc_device_type("altr,socfpga-usb-ecc");
1458}
1459
1460early_initcall(socfpga_init_usb_ecc);
1461
1462#endif /* CONFIG_EDAC_ALTERA_USB */
1463
Thor Thayer485fe9e2016-07-14 11:06:46 -05001464/********************** QSPI Device Functions **********************/
1465
1466#ifdef CONFIG_EDAC_ALTERA_QSPI
1467
1468static const struct edac_device_prv_data a10_qspiecc_data = {
1469 .setup = altr_check_ecc_deps,
1470 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1471 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
Thor Thayer485fe9e2016-07-14 11:06:46 -05001472 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1473 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1474 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1475 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1476 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1477 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1478 .inject_fops = &altr_edac_a10_device_inject_fops,
1479};
1480
1481static int __init socfpga_init_qspi_ecc(void)
1482{
1483 return altr_init_a10_ecc_device_type("altr,socfpga-qspi-ecc");
1484}
1485
1486early_initcall(socfpga_init_qspi_ecc);
1487
1488#endif /* CONFIG_EDAC_ALTERA_QSPI */
1489
Thor Thayer91104982016-08-09 09:40:52 -05001490/********************* SDMMC Device Functions **********************/
1491
1492#ifdef CONFIG_EDAC_ALTERA_SDMMC
1493
1494static const struct edac_device_prv_data a10_sdmmceccb_data;
1495static int altr_portb_setup(struct altr_edac_device_dev *device)
1496{
1497 struct edac_device_ctl_info *dci;
1498 struct altr_edac_device_dev *altdev;
1499 char *ecc_name = "sdmmcb-ecc";
1500 int edac_idx, rc;
1501 struct device_node *np;
1502 const struct edac_device_prv_data *prv = &a10_sdmmceccb_data;
1503
1504 rc = altr_check_ecc_deps(device);
1505 if (rc)
1506 return rc;
1507
1508 np = of_find_compatible_node(NULL, NULL, "altr,socfpga-sdmmc-ecc");
1509 if (!np) {
1510 edac_printk(KERN_WARNING, EDAC_DEVICE, "SDMMC node not found\n");
1511 return -ENODEV;
1512 }
1513
1514 /* Create the PortB EDAC device */
1515 edac_idx = edac_device_alloc_index();
1516 dci = edac_device_alloc_ctl_info(sizeof(*altdev), ecc_name, 1,
1517 ecc_name, 1, 0, NULL, 0, edac_idx);
1518 if (!dci) {
1519 edac_printk(KERN_ERR, EDAC_DEVICE,
1520 "%s: Unable to allocate PortB EDAC device\n",
1521 ecc_name);
1522 return -ENOMEM;
1523 }
1524
1525 /* Initialize the PortB EDAC device structure from PortA structure */
1526 altdev = dci->pvt_info;
1527 *altdev = *device;
1528
1529 if (!devres_open_group(&altdev->ddev, altr_portb_setup, GFP_KERNEL))
1530 return -ENOMEM;
1531
1532 /* Update PortB specific values */
1533 altdev->edac_dev_name = ecc_name;
1534 altdev->edac_idx = edac_idx;
1535 altdev->edac_dev = dci;
1536 altdev->data = prv;
1537 dci->dev = &altdev->ddev;
1538 dci->ctl_name = "Altera ECC Manager";
1539 dci->mod_name = ecc_name;
1540 dci->dev_name = ecc_name;
1541
1542 /* Update the IRQs for PortB */
1543 altdev->sb_irq = irq_of_parse_and_map(np, 2);
1544 if (!altdev->sb_irq) {
1545 edac_printk(KERN_ERR, EDAC_DEVICE, "Error PortB SBIRQ alloc\n");
1546 rc = -ENODEV;
1547 goto err_release_group_1;
1548 }
1549 rc = devm_request_irq(&altdev->ddev, altdev->sb_irq,
1550 prv->ecc_irq_handler,
Thor Thayera29d64a2016-09-22 17:13:39 -05001551 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
1552 ecc_name, altdev);
Thor Thayer91104982016-08-09 09:40:52 -05001553 if (rc) {
1554 edac_printk(KERN_ERR, EDAC_DEVICE, "PortB SBERR IRQ error\n");
1555 goto err_release_group_1;
1556 }
1557
1558 altdev->db_irq = irq_of_parse_and_map(np, 3);
1559 if (!altdev->db_irq) {
1560 edac_printk(KERN_ERR, EDAC_DEVICE, "Error PortB DBIRQ alloc\n");
1561 rc = -ENODEV;
1562 goto err_release_group_1;
1563 }
1564 rc = devm_request_irq(&altdev->ddev, altdev->db_irq,
1565 prv->ecc_irq_handler,
Thor Thayera29d64a2016-09-22 17:13:39 -05001566 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
1567 ecc_name, altdev);
Thor Thayer91104982016-08-09 09:40:52 -05001568 if (rc) {
1569 edac_printk(KERN_ERR, EDAC_DEVICE, "PortB DBERR IRQ error\n");
1570 goto err_release_group_1;
1571 }
1572
1573 rc = edac_device_add_device(dci);
1574 if (rc) {
1575 edac_printk(KERN_ERR, EDAC_DEVICE,
1576 "edac_device_add_device portB failed\n");
1577 rc = -ENOMEM;
1578 goto err_release_group_1;
1579 }
1580 altr_create_edacdev_dbgfs(dci, prv);
1581
1582 list_add(&altdev->next, &altdev->edac->a10_ecc_devices);
1583
1584 devres_remove_group(&altdev->ddev, altr_portb_setup);
1585
1586 return 0;
1587
1588err_release_group_1:
1589 edac_device_free_ctl_info(dci);
1590 devres_release_group(&altdev->ddev, altr_portb_setup);
1591 edac_printk(KERN_ERR, EDAC_DEVICE,
1592 "%s:Error setting up EDAC device: %d\n", ecc_name, rc);
1593 return rc;
1594}
1595
1596static irqreturn_t altr_edac_a10_ecc_irq_portb(int irq, void *dev_id)
1597{
1598 struct altr_edac_device_dev *ad = dev_id;
1599 void __iomem *base = ad->base;
1600 const struct edac_device_prv_data *priv = ad->data;
1601
1602 if (irq == ad->sb_irq) {
1603 writel(priv->ce_clear_mask,
1604 base + ALTR_A10_ECC_INTSTAT_OFST);
1605 edac_device_handle_ce(ad->edac_dev, 0, 0, ad->edac_dev_name);
1606 return IRQ_HANDLED;
1607 } else if (irq == ad->db_irq) {
1608 writel(priv->ue_clear_mask,
1609 base + ALTR_A10_ECC_INTSTAT_OFST);
1610 edac_device_handle_ue(ad->edac_dev, 0, 0, ad->edac_dev_name);
1611 return IRQ_HANDLED;
1612 }
1613
1614 WARN_ONCE(1, "Unhandled IRQ%d on Port B.", irq);
1615
1616 return IRQ_NONE;
1617}
1618
1619static const struct edac_device_prv_data a10_sdmmcecca_data = {
1620 .setup = altr_portb_setup,
1621 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1622 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
Thor Thayer91104982016-08-09 09:40:52 -05001623 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1624 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1625 .ce_set_mask = ALTR_A10_ECC_SERRPENA,
1626 .ue_set_mask = ALTR_A10_ECC_DERRPENA,
1627 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1628 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1629 .inject_fops = &altr_edac_a10_device_inject_fops,
1630};
1631
1632static const struct edac_device_prv_data a10_sdmmceccb_data = {
1633 .setup = altr_portb_setup,
1634 .ce_clear_mask = ALTR_A10_ECC_SERRPENB,
1635 .ue_clear_mask = ALTR_A10_ECC_DERRPENB,
Thor Thayer91104982016-08-09 09:40:52 -05001636 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1637 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1638 .ce_set_mask = ALTR_A10_ECC_TSERRB,
1639 .ue_set_mask = ALTR_A10_ECC_TDERRB,
1640 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1641 .ecc_irq_handler = altr_edac_a10_ecc_irq_portb,
1642 .inject_fops = &altr_edac_a10_device_inject_fops,
1643};
1644
1645static int __init socfpga_init_sdmmc_ecc(void)
1646{
1647 int rc = -ENODEV;
Thor Thayer25b223d2017-04-05 13:01:02 -05001648 struct device_node *child;
1649
Thor Thayer064acbd2018-09-25 08:49:01 -05001650 if (!socfpga_is_a10() && !socfpga_is_s10())
Thor Thayer25b223d2017-04-05 13:01:02 -05001651 return -ENODEV;
1652
1653 child = of_find_compatible_node(NULL, NULL, "altr,socfpga-sdmmc-ecc");
Thor Thayer91104982016-08-09 09:40:52 -05001654 if (!child) {
1655 edac_printk(KERN_WARNING, EDAC_DEVICE, "SDMMC node not found\n");
1656 return -ENODEV;
1657 }
1658
1659 if (!of_device_is_available(child))
1660 goto exit;
1661
1662 if (validate_parent_available(child))
1663 goto exit;
1664
1665 rc = altr_init_a10_ecc_block(child, ALTR_A10_SDMMC_IRQ_MASK,
1666 a10_sdmmcecca_data.ecc_enable_mask, 1);
1667exit:
1668 of_node_put(child);
1669 return rc;
1670}
1671
1672early_initcall(socfpga_init_sdmmc_ecc);
1673
1674#endif /* CONFIG_EDAC_ALTERA_SDMMC */
1675
Thor Thayer588cb032016-03-21 11:01:44 -05001676/********************* Arria10 EDAC Device Functions *************************/
Thor Thayerab564cb2016-05-25 11:29:41 -05001677static const struct of_device_id altr_edac_a10_device_of_match[] = {
1678#ifdef CONFIG_EDAC_ALTERA_L2C
1679 { .compatible = "altr,socfpga-a10-l2-ecc", .data = &a10_l2ecc_data },
1680#endif
1681#ifdef CONFIG_EDAC_ALTERA_OCRAM
1682 { .compatible = "altr,socfpga-a10-ocram-ecc",
1683 .data = &a10_ocramecc_data },
1684#endif
Thor Thayerab8c1e02016-06-22 08:58:58 -05001685#ifdef CONFIG_EDAC_ALTERA_ETHERNET
1686 { .compatible = "altr,socfpga-eth-mac-ecc",
1687 .data = &a10_enetecc_data },
1688#endif
Thor Thayerc6882fb2016-07-14 11:06:43 -05001689#ifdef CONFIG_EDAC_ALTERA_NAND
1690 { .compatible = "altr,socfpga-nand-ecc", .data = &a10_nandecc_data },
1691#endif
Thor Thayere8263792016-07-28 10:03:57 +02001692#ifdef CONFIG_EDAC_ALTERA_DMA
1693 { .compatible = "altr,socfpga-dma-ecc", .data = &a10_dmaecc_data },
1694#endif
Thor Thayerc6095812016-07-14 11:06:45 -05001695#ifdef CONFIG_EDAC_ALTERA_USB
1696 { .compatible = "altr,socfpga-usb-ecc", .data = &a10_usbecc_data },
1697#endif
Thor Thayer485fe9e2016-07-14 11:06:46 -05001698#ifdef CONFIG_EDAC_ALTERA_QSPI
1699 { .compatible = "altr,socfpga-qspi-ecc", .data = &a10_qspiecc_data },
1700#endif
Thor Thayer91104982016-08-09 09:40:52 -05001701#ifdef CONFIG_EDAC_ALTERA_SDMMC
1702 { .compatible = "altr,socfpga-sdmmc-ecc", .data = &a10_sdmmcecca_data },
1703#endif
Thor Thayerab564cb2016-05-25 11:29:41 -05001704 {},
1705};
1706MODULE_DEVICE_TABLE(of, altr_edac_a10_device_of_match);
Thor Thayer588cb032016-03-21 11:01:44 -05001707
1708/*
1709 * The Arria10 EDAC Device Functions differ from the Cyclone5/Arria5
1710 * because 2 IRQs are shared among the all ECC peripherals. The ECC
1711 * manager manages the IRQs and the children.
1712 * Based on xgene_edac.c peripheral code.
1713 */
1714
Thor Thayerc7b4be82016-04-06 20:22:54 -05001715static ssize_t altr_edac_a10_device_trig(struct file *file,
1716 const char __user *user_buf,
1717 size_t count, loff_t *ppos)
1718{
1719 struct edac_device_ctl_info *edac_dci = file->private_data;
1720 struct altr_edac_device_dev *drvdata = edac_dci->pvt_info;
1721 const struct edac_device_prv_data *priv = drvdata->data;
1722 void __iomem *set_addr = (drvdata->base + priv->set_err_ofst);
1723 unsigned long flags;
1724 u8 trig_type;
1725
1726 if (!user_buf || get_user(trig_type, user_buf))
1727 return -EFAULT;
1728
1729 local_irq_save(flags);
1730 if (trig_type == ALTR_UE_TRIGGER_CHAR)
1731 writel(priv->ue_set_mask, set_addr);
1732 else
1733 writel(priv->ce_set_mask, set_addr);
Thor Thayer064acbd2018-09-25 08:49:01 -05001734
1735 /* Ensure the interrupt test bits are set */
1736 wmb();
1737 local_irq_restore(flags);
1738
1739 return count;
1740}
1741
1742/*
1743 * The Stratix10 EDAC Error Injection Functions differ from Arria10
1744 * slightly. A few Arria10 peripherals can use this injection function.
1745 * Inject the error into the memory and then readback to trigger the IRQ.
1746 */
1747static ssize_t altr_edac_a10_device_trig2(struct file *file,
1748 const char __user *user_buf,
1749 size_t count, loff_t *ppos)
1750{
1751 struct edac_device_ctl_info *edac_dci = file->private_data;
1752 struct altr_edac_device_dev *drvdata = edac_dci->pvt_info;
1753 const struct edac_device_prv_data *priv = drvdata->data;
1754 void __iomem *set_addr = (drvdata->base + priv->set_err_ofst);
1755 unsigned long flags;
1756 u8 trig_type;
1757
1758 if (!user_buf || get_user(trig_type, user_buf))
1759 return -EFAULT;
1760
1761 local_irq_save(flags);
1762 if (trig_type == ALTR_UE_TRIGGER_CHAR) {
1763 writel(priv->ue_set_mask, set_addr);
1764 } else {
Thor Thayer436b0a52019-03-26 16:29:30 -05001765 /* Setup read/write of 4 bytes */
Thor Thayer064acbd2018-09-25 08:49:01 -05001766 writel(ECC_WORD_WRITE, drvdata->base + ECC_BLK_DBYTECTRL_OFST);
1767 /* Setup Address to 0 */
Thor Thayer436b0a52019-03-26 16:29:30 -05001768 writel(0, drvdata->base + ECC_BLK_ADDRESS_OFST);
1769 /* Setup accctrl to read & ecc & data override */
1770 writel(ECC_READ_EDOVR, drvdata->base + ECC_BLK_ACCCTRL_OFST);
Thor Thayer064acbd2018-09-25 08:49:01 -05001771 /* Kick it. */
1772 writel(ECC_XACT_KICK, drvdata->base + ECC_BLK_STARTACC_OFST);
1773 /* Setup write for single bit change */
Thor Thayer436b0a52019-03-26 16:29:30 -05001774 writel(readl(drvdata->base + ECC_BLK_RDATA0_OFST) ^ 0x1,
1775 drvdata->base + ECC_BLK_WDATA0_OFST);
1776 writel(readl(drvdata->base + ECC_BLK_RDATA1_OFST),
1777 drvdata->base + ECC_BLK_WDATA1_OFST);
1778 writel(readl(drvdata->base + ECC_BLK_RDATA2_OFST),
1779 drvdata->base + ECC_BLK_WDATA2_OFST);
1780 writel(readl(drvdata->base + ECC_BLK_RDATA3_OFST),
1781 drvdata->base + ECC_BLK_WDATA3_OFST);
1782
Thor Thayer064acbd2018-09-25 08:49:01 -05001783 /* Copy Read ECC to Write ECC */
1784 writel(readl(drvdata->base + ECC_BLK_RECC0_OFST),
1785 drvdata->base + ECC_BLK_WECC0_OFST);
1786 writel(readl(drvdata->base + ECC_BLK_RECC1_OFST),
1787 drvdata->base + ECC_BLK_WECC1_OFST);
1788 /* Setup accctrl to write & ecc override & data override */
1789 writel(ECC_WRITE_EDOVR, drvdata->base + ECC_BLK_ACCCTRL_OFST);
1790 /* Kick it. */
1791 writel(ECC_XACT_KICK, drvdata->base + ECC_BLK_STARTACC_OFST);
1792 /* Setup accctrl to read & ecc overwrite & data overwrite */
1793 writel(ECC_READ_EDOVR, drvdata->base + ECC_BLK_ACCCTRL_OFST);
1794 /* Kick it. */
1795 writel(ECC_XACT_KICK, drvdata->base + ECC_BLK_STARTACC_OFST);
1796 }
1797
Thor Thayerc7b4be82016-04-06 20:22:54 -05001798 /* Ensure the interrupt test bits are set */
1799 wmb();
1800 local_irq_restore(flags);
1801
1802 return count;
1803}
1804
Thor Thayer13ab8442016-06-07 15:35:57 -05001805static void altr_edac_a10_irq_handler(struct irq_desc *desc)
Thor Thayer588cb032016-03-21 11:01:44 -05001806{
Thor Thayer13ab8442016-06-07 15:35:57 -05001807 int dberr, bit, sm_offset, irq_status;
1808 struct altr_arria10_edac *edac = irq_desc_get_handler_data(desc);
1809 struct irq_chip *chip = irq_desc_get_chip(desc);
1810 int irq = irq_desc_get_irq(desc);
1811
1812 dberr = (irq == edac->db_irq) ? 1 : 0;
1813 sm_offset = dberr ? A10_SYSMGR_ECC_INTSTAT_DERR_OFST :
1814 A10_SYSMGR_ECC_INTSTAT_SERR_OFST;
1815
1816 chained_irq_enter(chip, desc);
Thor Thayer588cb032016-03-21 11:01:44 -05001817
1818 regmap_read(edac->ecc_mgr_map, sm_offset, &irq_status);
1819
Thor Thayer13ab8442016-06-07 15:35:57 -05001820 for_each_set_bit(bit, (unsigned long *)&irq_status, 32) {
1821 irq = irq_linear_revmap(edac->domain, dberr * 32 + bit);
1822 if (irq)
1823 generic_handle_irq(irq);
Thor Thayer588cb032016-03-21 11:01:44 -05001824 }
1825
Thor Thayer13ab8442016-06-07 15:35:57 -05001826 chained_irq_exit(chip, desc);
Thor Thayer588cb032016-03-21 11:01:44 -05001827}
1828
Thor Thayer44ec9b32016-06-22 08:58:52 -05001829static int validate_parent_available(struct device_node *np)
1830{
1831 struct device_node *parent;
1832 int ret = 0;
1833
1834 /* Ensure parent device is enabled if parent node exists */
1835 parent = of_parse_phandle(np, "altr,ecc-parent", 0);
1836 if (parent && !of_device_is_available(parent))
1837 ret = -ENODEV;
1838
1839 of_node_put(parent);
1840 return ret;
1841}
1842
Thor Thayer588cb032016-03-21 11:01:44 -05001843static int altr_edac_a10_device_add(struct altr_arria10_edac *edac,
1844 struct device_node *np)
1845{
1846 struct edac_device_ctl_info *dci;
1847 struct altr_edac_device_dev *altdev;
1848 char *ecc_name = (char *)np->name;
1849 struct resource res;
1850 int edac_idx;
1851 int rc = 0;
1852 const struct edac_device_prv_data *prv;
1853 /* Get matching node and check for valid result */
1854 const struct of_device_id *pdev_id =
Thor Thayerab564cb2016-05-25 11:29:41 -05001855 of_match_node(altr_edac_a10_device_of_match, np);
Thor Thayer588cb032016-03-21 11:01:44 -05001856 if (IS_ERR_OR_NULL(pdev_id))
1857 return -ENODEV;
1858
1859 /* Get driver specific data for this EDAC device */
1860 prv = pdev_id->data;
1861 if (IS_ERR_OR_NULL(prv))
1862 return -ENODEV;
1863
Thor Thayer44ec9b32016-06-22 08:58:52 -05001864 if (validate_parent_available(np))
1865 return -ENODEV;
1866
Thor Thayer588cb032016-03-21 11:01:44 -05001867 if (!devres_open_group(edac->dev, altr_edac_a10_device_add, GFP_KERNEL))
1868 return -ENOMEM;
1869
1870 rc = of_address_to_resource(np, 0, &res);
1871 if (rc < 0) {
1872 edac_printk(KERN_ERR, EDAC_DEVICE,
1873 "%s: no resource address\n", ecc_name);
1874 goto err_release_group;
1875 }
1876
1877 edac_idx = edac_device_alloc_index();
1878 dci = edac_device_alloc_ctl_info(sizeof(*altdev), ecc_name,
1879 1, ecc_name, 1, 0, NULL, 0,
1880 edac_idx);
1881
1882 if (!dci) {
1883 edac_printk(KERN_ERR, EDAC_DEVICE,
1884 "%s: Unable to allocate EDAC device\n", ecc_name);
1885 rc = -ENOMEM;
1886 goto err_release_group;
1887 }
1888
1889 altdev = dci->pvt_info;
1890 dci->dev = edac->dev;
1891 altdev->edac_dev_name = ecc_name;
1892 altdev->edac_idx = edac_idx;
1893 altdev->edac = edac;
1894 altdev->edac_dev = dci;
1895 altdev->data = prv;
1896 altdev->ddev = *edac->dev;
1897 dci->dev = &altdev->ddev;
1898 dci->ctl_name = "Altera ECC Manager";
1899 dci->mod_name = ecc_name;
1900 dci->dev_name = ecc_name;
1901
1902 altdev->base = devm_ioremap_resource(edac->dev, &res);
1903 if (IS_ERR(altdev->base)) {
1904 rc = PTR_ERR(altdev->base);
1905 goto err_release_group1;
1906 }
1907
1908 /* Check specific dependencies for the module */
1909 if (altdev->data->setup) {
1910 rc = altdev->data->setup(altdev);
1911 if (rc)
1912 goto err_release_group1;
1913 }
1914
Thor Thayer13ab8442016-06-07 15:35:57 -05001915 altdev->sb_irq = irq_of_parse_and_map(np, 0);
1916 if (!altdev->sb_irq) {
1917 edac_printk(KERN_ERR, EDAC_DEVICE, "Error allocating SBIRQ\n");
1918 rc = -ENODEV;
1919 goto err_release_group1;
1920 }
Thor Thayera29d64a2016-09-22 17:13:39 -05001921 rc = devm_request_irq(edac->dev, altdev->sb_irq, prv->ecc_irq_handler,
1922 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
1923 ecc_name, altdev);
Thor Thayer13ab8442016-06-07 15:35:57 -05001924 if (rc) {
Thor Thayer37635692016-09-22 17:13:38 -05001925 edac_printk(KERN_ERR, EDAC_DEVICE, "No SBERR IRQ resource\n");
Thor Thayer13ab8442016-06-07 15:35:57 -05001926 goto err_release_group1;
1927 }
1928
Thor Thayer1bd76ff2019-03-13 10:27:22 -05001929#ifdef CONFIG_ARCH_STRATIX10
1930 /* Use IRQ to determine SError origin instead of assigning IRQ */
1931 rc = of_property_read_u32_index(np, "interrupts", 0, &altdev->db_irq);
1932 if (rc) {
1933 edac_printk(KERN_ERR, EDAC_DEVICE,
1934 "Unable to parse DB IRQ index\n");
1935 goto err_release_group1;
1936 }
1937#else
Thor Thayer13ab8442016-06-07 15:35:57 -05001938 altdev->db_irq = irq_of_parse_and_map(np, 1);
1939 if (!altdev->db_irq) {
1940 edac_printk(KERN_ERR, EDAC_DEVICE, "Error allocating DBIRQ\n");
1941 rc = -ENODEV;
1942 goto err_release_group1;
1943 }
Thor Thayera29d64a2016-09-22 17:13:39 -05001944 rc = devm_request_irq(edac->dev, altdev->db_irq, prv->ecc_irq_handler,
1945 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
1946 ecc_name, altdev);
Thor Thayer13ab8442016-06-07 15:35:57 -05001947 if (rc) {
1948 edac_printk(KERN_ERR, EDAC_DEVICE, "No DBERR IRQ resource\n");
1949 goto err_release_group1;
1950 }
Thor Thayer1bd76ff2019-03-13 10:27:22 -05001951#endif
Thor Thayer13ab8442016-06-07 15:35:57 -05001952
Thor Thayer588cb032016-03-21 11:01:44 -05001953 rc = edac_device_add_device(dci);
1954 if (rc) {
1955 dev_err(edac->dev, "edac_device_add_device failed\n");
1956 rc = -ENOMEM;
1957 goto err_release_group1;
1958 }
1959
1960 altr_create_edacdev_dbgfs(dci, prv);
1961
1962 list_add(&altdev->next, &edac->a10_ecc_devices);
1963
1964 devres_remove_group(edac->dev, altr_edac_a10_device_add);
1965
1966 return 0;
1967
1968err_release_group1:
1969 edac_device_free_ctl_info(dci);
1970err_release_group:
Thor Thayer588cb032016-03-21 11:01:44 -05001971 devres_release_group(edac->dev, NULL);
1972 edac_printk(KERN_ERR, EDAC_DEVICE,
1973 "%s:Error setting up EDAC device: %d\n", ecc_name, rc);
1974
1975 return rc;
1976}
1977
Thor Thayer13ab8442016-06-07 15:35:57 -05001978static void a10_eccmgr_irq_mask(struct irq_data *d)
1979{
1980 struct altr_arria10_edac *edac = irq_data_get_irq_chip_data(d);
1981
1982 regmap_write(edac->ecc_mgr_map, A10_SYSMGR_ECC_INTMASK_SET_OFST,
1983 BIT(d->hwirq));
1984}
1985
1986static void a10_eccmgr_irq_unmask(struct irq_data *d)
1987{
1988 struct altr_arria10_edac *edac = irq_data_get_irq_chip_data(d);
1989
1990 regmap_write(edac->ecc_mgr_map, A10_SYSMGR_ECC_INTMASK_CLR_OFST,
1991 BIT(d->hwirq));
1992}
1993
1994static int a10_eccmgr_irqdomain_map(struct irq_domain *d, unsigned int irq,
1995 irq_hw_number_t hwirq)
1996{
1997 struct altr_arria10_edac *edac = d->host_data;
1998
1999 irq_set_chip_and_handler(irq, &edac->irq_chip, handle_simple_irq);
2000 irq_set_chip_data(irq, edac);
2001 irq_set_noprobe(irq);
2002
2003 return 0;
2004}
2005
Tobias Klauser18caec22017-05-24 15:35:05 +02002006static const struct irq_domain_ops a10_eccmgr_ic_ops = {
Thor Thayer13ab8442016-06-07 15:35:57 -05002007 .map = a10_eccmgr_irqdomain_map,
2008 .xlate = irq_domain_xlate_twocell,
2009};
2010
Thor Thayerd5fc9122018-09-25 08:48:58 -05002011/************** Stratix 10 EDAC Double Bit Error Handler ************/
2012#define to_a10edac(p, m) container_of(p, struct altr_arria10_edac, m)
2013
Thor Thayer1bd76ff2019-03-13 10:27:22 -05002014#ifdef CONFIG_ARCH_STRATIX10
2015/* panic routine issues reboot on non-zero panic_timeout */
2016extern int panic_timeout;
2017
Thor Thayerd5fc9122018-09-25 08:48:58 -05002018/*
2019 * The double bit error is handled through SError which is fatal. This is
2020 * called as a panic notifier to printout ECC error info as part of the panic.
2021 */
2022static int s10_edac_dberr_handler(struct notifier_block *this,
2023 unsigned long event, void *ptr)
2024{
2025 struct altr_arria10_edac *edac = to_a10edac(this, panic_notifier);
2026 int err_addr, dberror;
2027
2028 regmap_read(edac->ecc_mgr_map, S10_SYSMGR_ECC_INTSTAT_DERR_OFST,
2029 &dberror);
2030 regmap_write(edac->ecc_mgr_map, S10_SYSMGR_UE_VAL_OFST, dberror);
Thor Thayer1bd76ff2019-03-13 10:27:22 -05002031 if (dberror & S10_DBE_IRQ_MASK) {
2032 struct list_head *position;
2033 struct altr_edac_device_dev *ed;
2034 struct arm_smccc_res result;
2035
2036 /* Find the matching DBE in the list of devices */
2037 list_for_each(position, &edac->a10_ecc_devices) {
2038 ed = list_entry(position, struct altr_edac_device_dev,
2039 next);
2040 if (!(BIT(ed->db_irq) & dberror))
2041 continue;
2042
2043 writel(ALTR_A10_ECC_DERRPENA,
2044 ed->base + ALTR_A10_ECC_INTSTAT_OFST);
2045 err_addr = readl(ed->base + ALTR_S10_DERR_ADDRA_OFST);
2046 regmap_write(edac->ecc_mgr_map,
2047 S10_SYSMGR_UE_ADDR_OFST, err_addr);
2048 edac_printk(KERN_ERR, EDAC_DEVICE,
2049 "EDAC: [Fatal DBE on %s @ 0x%08X]\n",
2050 ed->edac_dev_name, err_addr);
2051 break;
2052 }
2053 /* Notify the System through SMC. Reboot delay = 1 second */
2054 panic_timeout = 1;
2055 arm_smccc_smc(INTEL_SIP_SMC_ECC_DBE, dberror, 0, 0, 0, 0,
2056 0, 0, &result);
Thor Thayerd5fc9122018-09-25 08:48:58 -05002057 }
2058
2059 return NOTIFY_DONE;
2060}
Thor Thayer1bd76ff2019-03-13 10:27:22 -05002061#endif
Thor Thayerd5fc9122018-09-25 08:48:58 -05002062
2063/****************** Arria 10 EDAC Probe Function *********************/
Thor Thayer588cb032016-03-21 11:01:44 -05002064static int altr_edac_a10_probe(struct platform_device *pdev)
2065{
2066 struct altr_arria10_edac *edac;
2067 struct device_node *child;
Thor Thayer588cb032016-03-21 11:01:44 -05002068
2069 edac = devm_kzalloc(&pdev->dev, sizeof(*edac), GFP_KERNEL);
2070 if (!edac)
2071 return -ENOMEM;
2072
2073 edac->dev = &pdev->dev;
2074 platform_set_drvdata(pdev, edac);
2075 INIT_LIST_HEAD(&edac->a10_ecc_devices);
2076
Thor Thayerd5fc9122018-09-25 08:48:58 -05002077 if (socfpga_is_a10()) {
2078 edac->ecc_mgr_map =
2079 syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
Thor Thayer588cb032016-03-21 11:01:44 -05002080 "altr,sysmgr-syscon");
Thor Thayerd5fc9122018-09-25 08:48:58 -05002081 } else {
2082 struct device_node *sysmgr_np;
2083 struct resource res;
Arnd Bergmann8537bf102018-09-27 12:09:26 +02002084 uintptr_t base;
Thor Thayerd5fc9122018-09-25 08:48:58 -05002085
2086 sysmgr_np = of_parse_phandle(pdev->dev.of_node,
2087 "altr,sysmgr-syscon", 0);
2088 if (!sysmgr_np) {
2089 edac_printk(KERN_ERR, EDAC_DEVICE,
2090 "Unable to find altr,sysmgr-syscon\n");
2091 return -ENODEV;
2092 }
2093
2094 if (of_address_to_resource(sysmgr_np, 0, &res))
2095 return -ENOMEM;
2096
2097 /* Need physical address for SMCC call */
Arnd Bergmann8537bf102018-09-27 12:09:26 +02002098 base = res.start;
Thor Thayerd5fc9122018-09-25 08:48:58 -05002099
Arnd Bergmann8537bf102018-09-27 12:09:26 +02002100 edac->ecc_mgr_map = devm_regmap_init(&pdev->dev, NULL,
2101 (void *)base,
Thor Thayerd5fc9122018-09-25 08:48:58 -05002102 &s10_sdram_regmap_cfg);
2103 }
2104
Thor Thayer588cb032016-03-21 11:01:44 -05002105 if (IS_ERR(edac->ecc_mgr_map)) {
2106 edac_printk(KERN_ERR, EDAC_DEVICE,
2107 "Unable to get syscon altr,sysmgr-syscon\n");
2108 return PTR_ERR(edac->ecc_mgr_map);
2109 }
2110
Thor Thayer13ab8442016-06-07 15:35:57 -05002111 edac->irq_chip.name = pdev->dev.of_node->name;
2112 edac->irq_chip.irq_mask = a10_eccmgr_irq_mask;
2113 edac->irq_chip.irq_unmask = a10_eccmgr_irq_unmask;
2114 edac->domain = irq_domain_add_linear(pdev->dev.of_node, 64,
2115 &a10_eccmgr_ic_ops, edac);
2116 if (!edac->domain) {
2117 dev_err(&pdev->dev, "Error adding IRQ domain\n");
2118 return -ENOMEM;
Thor Thayer588cb032016-03-21 11:01:44 -05002119 }
2120
Thor Thayer13ab8442016-06-07 15:35:57 -05002121 edac->sb_irq = platform_get_irq(pdev, 0);
2122 if (edac->sb_irq < 0) {
2123 dev_err(&pdev->dev, "No SBERR IRQ resource\n");
2124 return edac->sb_irq;
Thor Thayer588cb032016-03-21 11:01:44 -05002125 }
2126
Thor Thayer13ab8442016-06-07 15:35:57 -05002127 irq_set_chained_handler_and_data(edac->sb_irq,
2128 altr_edac_a10_irq_handler,
2129 edac);
2130
Thor Thayer1bd76ff2019-03-13 10:27:22 -05002131#ifdef CONFIG_ARCH_STRATIX10
2132 {
Thor Thayerd5fc9122018-09-25 08:48:58 -05002133 int dberror, err_addr;
2134
2135 edac->panic_notifier.notifier_call = s10_edac_dberr_handler;
2136 atomic_notifier_chain_register(&panic_notifier_list,
2137 &edac->panic_notifier);
2138
2139 /* Printout a message if uncorrectable error previously. */
2140 regmap_read(edac->ecc_mgr_map, S10_SYSMGR_UE_VAL_OFST,
2141 &dberror);
2142 if (dberror) {
2143 regmap_read(edac->ecc_mgr_map, S10_SYSMGR_UE_ADDR_OFST,
2144 &err_addr);
2145 edac_printk(KERN_ERR, EDAC_DEVICE,
2146 "Previous Boot UE detected[0x%X] @ 0x%X\n",
2147 dberror, err_addr);
2148 /* Reset the sticky registers */
2149 regmap_write(edac->ecc_mgr_map,
2150 S10_SYSMGR_UE_VAL_OFST, 0);
2151 regmap_write(edac->ecc_mgr_map,
2152 S10_SYSMGR_UE_ADDR_OFST, 0);
2153 }
Thor Thayer13ab8442016-06-07 15:35:57 -05002154 }
Thor Thayer1bd76ff2019-03-13 10:27:22 -05002155#else
2156 edac->db_irq = platform_get_irq(pdev, 1);
2157 if (edac->db_irq < 0) {
2158 dev_err(&pdev->dev, "No DBERR IRQ resource\n");
2159 return edac->db_irq;
2160 }
2161 irq_set_chained_handler_and_data(edac->db_irq,
2162 altr_edac_a10_irq_handler, edac);
2163#endif
Thor Thayer13ab8442016-06-07 15:35:57 -05002164
Thor Thayer588cb032016-03-21 11:01:44 -05002165 for_each_child_of_node(pdev->dev.of_node, child) {
2166 if (!of_device_is_available(child))
2167 continue;
Thor Thayerc6882fb2016-07-14 11:06:43 -05002168
2169 if (of_device_is_compatible(child, "altr,socfpga-a10-l2-ecc") ||
2170 of_device_is_compatible(child, "altr,socfpga-a10-ocram-ecc") ||
2171 of_device_is_compatible(child, "altr,socfpga-eth-mac-ecc") ||
Thor Thayere8263792016-07-28 10:03:57 +02002172 of_device_is_compatible(child, "altr,socfpga-nand-ecc") ||
Thor Thayerc6095812016-07-14 11:06:45 -05002173 of_device_is_compatible(child, "altr,socfpga-dma-ecc") ||
Thor Thayer485fe9e2016-07-14 11:06:46 -05002174 of_device_is_compatible(child, "altr,socfpga-usb-ecc") ||
Thor Thayer91104982016-08-09 09:40:52 -05002175 of_device_is_compatible(child, "altr,socfpga-qspi-ecc") ||
2176 of_device_is_compatible(child, "altr,socfpga-sdmmc-ecc"))
Thor Thayerc6882fb2016-07-14 11:06:43 -05002177
Thor Thayer588cb032016-03-21 11:01:44 -05002178 altr_edac_a10_device_add(edac, child);
Thor Thayerc6882fb2016-07-14 11:06:43 -05002179
Thor Thayer580b5cf2019-02-25 12:56:45 -06002180#ifdef CONFIG_EDAC_ALTERA_SDRAM
Thor Thayerd5fc9122018-09-25 08:48:58 -05002181 else if ((of_device_is_compatible(child, "altr,sdram-edac-a10")) ||
2182 (of_device_is_compatible(child, "altr,sdram-edac-s10")))
Thor Thayerab564cb2016-05-25 11:29:41 -05002183 of_platform_populate(pdev->dev.of_node,
2184 altr_sdram_ctrl_of_match,
2185 NULL, &pdev->dev);
Thor Thayer580b5cf2019-02-25 12:56:45 -06002186#endif
Thor Thayer588cb032016-03-21 11:01:44 -05002187 }
2188
2189 return 0;
2190}
2191
2192static const struct of_device_id altr_edac_a10_of_match[] = {
2193 { .compatible = "altr,socfpga-a10-ecc-manager" },
Thor Thayerd5fc9122018-09-25 08:48:58 -05002194 { .compatible = "altr,socfpga-s10-ecc-manager" },
Thor Thayer588cb032016-03-21 11:01:44 -05002195 {},
2196};
2197MODULE_DEVICE_TABLE(of, altr_edac_a10_of_match);
2198
2199static struct platform_driver altr_edac_a10_driver = {
2200 .probe = altr_edac_a10_probe,
2201 .driver = {
2202 .name = "socfpga_a10_ecc_manager",
2203 .of_match_table = altr_edac_a10_of_match,
2204 },
2205};
2206module_platform_driver(altr_edac_a10_driver);
2207
Thor Thayer71bcada2014-09-03 10:27:54 -05002208MODULE_LICENSE("GPL v2");
2209MODULE_AUTHOR("Thor Thayer");
Thor Thayerc3eea192016-02-10 13:26:21 -06002210MODULE_DESCRIPTION("EDAC Driver for Altera Memories");