]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/edac/altera_edac.c
EDAC: Get rid of mci->mod_ver
[mirror_ubuntu-bionic-kernel.git] / drivers / edac / altera_edac.c
CommitLineData
71bcada8 1/*
c3eea194 2 * Copyright Altera Corporation (C) 2014-2016. All rights reserved.
71bcada8
TT
3 * Copyright 2011-2012 Calxeda, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Adapted from the highbank_mc_edac driver.
18 */
19
c3eea194 20#include <asm/cacheflush.h>
71bcada8 21#include <linux/ctype.h>
1166fde9 22#include <linux/delay.h>
71bcada8 23#include <linux/edac.h>
c3eea194 24#include <linux/genalloc.h>
71bcada8 25#include <linux/interrupt.h>
13ab8448 26#include <linux/irqchip/chained_irq.h>
71bcada8
TT
27#include <linux/kernel.h>
28#include <linux/mfd/syscon.h>
588cb03e 29#include <linux/of_address.h>
13ab8448 30#include <linux/of_irq.h>
71bcada8
TT
31#include <linux/of_platform.h>
32#include <linux/platform_device.h>
33#include <linux/regmap.h>
34#include <linux/types.h>
35#include <linux/uaccess.h>
36
143f4a5a 37#include "altera_edac.h"
71bcada8
TT
38#include "edac_module.h"
39
40#define EDAC_MOD_STR "altera_edac"
c3eea194 41#define EDAC_DEVICE "Altera"
71bcada8 42
143f4a5a
TT
43static const struct altr_sdram_prv_data c5_data = {
44 .ecc_ctrl_offset = CV_CTLCFG_OFST,
45 .ecc_ctl_en_mask = CV_CTLCFG_ECC_AUTO_EN,
46 .ecc_stat_offset = CV_DRAMSTS_OFST,
47 .ecc_stat_ce_mask = CV_DRAMSTS_SBEERR,
48 .ecc_stat_ue_mask = CV_DRAMSTS_DBEERR,
49 .ecc_saddr_offset = CV_ERRADDR_OFST,
73bcc942 50 .ecc_daddr_offset = CV_ERRADDR_OFST,
143f4a5a
TT
51 .ecc_cecnt_offset = CV_SBECOUNT_OFST,
52 .ecc_uecnt_offset = CV_DBECOUNT_OFST,
53 .ecc_irq_en_offset = CV_DRAMINTR_OFST,
54 .ecc_irq_en_mask = CV_DRAMINTR_INTREN,
55 .ecc_irq_clr_offset = CV_DRAMINTR_OFST,
56 .ecc_irq_clr_mask = (CV_DRAMINTR_INTRCLR | CV_DRAMINTR_INTREN),
57 .ecc_cnt_rst_offset = CV_DRAMINTR_OFST,
58 .ecc_cnt_rst_mask = CV_DRAMINTR_INTRCLR,
143f4a5a
TT
59 .ce_ue_trgr_offset = CV_CTLCFG_OFST,
60 .ce_set_mask = CV_CTLCFG_GEN_SB_ERR,
61 .ue_set_mask = CV_CTLCFG_GEN_DB_ERR,
71bcada8
TT
62};
63
73bcc942
TT
64static const struct altr_sdram_prv_data a10_data = {
65 .ecc_ctrl_offset = A10_ECCCTRL1_OFST,
66 .ecc_ctl_en_mask = A10_ECCCTRL1_ECC_EN,
67 .ecc_stat_offset = A10_INTSTAT_OFST,
68 .ecc_stat_ce_mask = A10_INTSTAT_SBEERR,
69 .ecc_stat_ue_mask = A10_INTSTAT_DBEERR,
70 .ecc_saddr_offset = A10_SERRADDR_OFST,
71 .ecc_daddr_offset = A10_DERRADDR_OFST,
72 .ecc_irq_en_offset = A10_ERRINTEN_OFST,
73 .ecc_irq_en_mask = A10_ECC_IRQ_EN_MASK,
74 .ecc_irq_clr_offset = A10_INTSTAT_OFST,
75 .ecc_irq_clr_mask = (A10_INTSTAT_SBEERR | A10_INTSTAT_DBEERR),
76 .ecc_cnt_rst_offset = A10_ECCCTRL1_OFST,
77 .ecc_cnt_rst_mask = A10_ECC_CNT_RESET_MASK,
73bcc942
TT
78 .ce_ue_trgr_offset = A10_DIAGINTTEST_OFST,
79 .ce_set_mask = A10_DIAGINT_TSERRA_MASK,
80 .ue_set_mask = A10_DIAGINT_TDERRA_MASK,
73bcc942
TT
81};
82
c3eea194
TT
83/*********************** EDAC Memory Controller Functions ****************/
84
85/* The SDRAM controller uses the EDAC Memory Controller framework. */
86
71bcada8
TT
87static irqreturn_t altr_sdram_mc_err_handler(int irq, void *dev_id)
88{
89 struct mem_ctl_info *mci = dev_id;
90 struct altr_sdram_mc_data *drvdata = mci->pvt_info;
143f4a5a 91 const struct altr_sdram_prv_data *priv = drvdata->data;
73bcc942 92 u32 status, err_count = 1, err_addr;
71bcada8 93
143f4a5a 94 regmap_read(drvdata->mc_vbase, priv->ecc_stat_offset, &status);
71bcada8 95
143f4a5a 96 if (status & priv->ecc_stat_ue_mask) {
73bcc942
TT
97 regmap_read(drvdata->mc_vbase, priv->ecc_daddr_offset,
98 &err_addr);
99 if (priv->ecc_uecnt_offset)
100 regmap_read(drvdata->mc_vbase, priv->ecc_uecnt_offset,
101 &err_count);
71bcada8
TT
102 panic("\nEDAC: [%d Uncorrectable errors @ 0x%08X]\n",
103 err_count, err_addr);
104 }
143f4a5a 105 if (status & priv->ecc_stat_ce_mask) {
73bcc942
TT
106 regmap_read(drvdata->mc_vbase, priv->ecc_saddr_offset,
107 &err_addr);
108 if (priv->ecc_uecnt_offset)
109 regmap_read(drvdata->mc_vbase, priv->ecc_cecnt_offset,
110 &err_count);
71bcada8
TT
111 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, err_count,
112 err_addr >> PAGE_SHIFT,
113 err_addr & ~PAGE_MASK, 0,
114 0, 0, -1, mci->ctl_name, "");
73bcc942
TT
115 /* Clear IRQ to resume */
116 regmap_write(drvdata->mc_vbase, priv->ecc_irq_clr_offset,
117 priv->ecc_irq_clr_mask);
71bcada8 118
73bcc942
TT
119 return IRQ_HANDLED;
120 }
121 return IRQ_NONE;
71bcada8
TT
122}
123
71bcada8
TT
124static ssize_t altr_sdr_mc_err_inject_write(struct file *file,
125 const char __user *data,
126 size_t count, loff_t *ppos)
127{
128 struct mem_ctl_info *mci = file->private_data;
129 struct altr_sdram_mc_data *drvdata = mci->pvt_info;
143f4a5a 130 const struct altr_sdram_prv_data *priv = drvdata->data;
71bcada8
TT
131 u32 *ptemp;
132 dma_addr_t dma_handle;
133 u32 reg, read_reg;
134
135 ptemp = dma_alloc_coherent(mci->pdev, 16, &dma_handle, GFP_KERNEL);
136 if (!ptemp) {
137 dma_free_coherent(mci->pdev, 16, ptemp, dma_handle);
138 edac_printk(KERN_ERR, EDAC_MC,
139 "Inject: Buffer Allocation error\n");
140 return -ENOMEM;
141 }
142
143f4a5a
TT
143 regmap_read(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
144 &read_reg);
145 read_reg &= ~(priv->ce_set_mask | priv->ue_set_mask);
71bcada8
TT
146
147 /* Error are injected by writing a word while the SBE or DBE
148 * bit in the CTLCFG register is set. Reading the word will
149 * trigger the SBE or DBE error and the corresponding IRQ.
150 */
151 if (count == 3) {
152 edac_printk(KERN_ALERT, EDAC_MC,
153 "Inject Double bit error\n");
90e493d7 154 local_irq_disable();
143f4a5a
TT
155 regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
156 (read_reg | priv->ue_set_mask));
90e493d7 157 local_irq_enable();
71bcada8
TT
158 } else {
159 edac_printk(KERN_ALERT, EDAC_MC,
160 "Inject Single bit error\n");
90e493d7 161 local_irq_disable();
143f4a5a
TT
162 regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
163 (read_reg | priv->ce_set_mask));
90e493d7 164 local_irq_enable();
71bcada8
TT
165 }
166
167 ptemp[0] = 0x5A5A5A5A;
168 ptemp[1] = 0xA5A5A5A5;
169
170 /* Clear the error injection bits */
143f4a5a 171 regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset, read_reg);
71bcada8
TT
172 /* Ensure it has been written out */
173 wmb();
174
175 /*
176 * To trigger the error, we need to read the data back
177 * (the data was written with errors above).
178 * The ACCESS_ONCE macros and printk are used to prevent the
179 * the compiler optimizing these reads out.
180 */
181 reg = ACCESS_ONCE(ptemp[0]);
182 read_reg = ACCESS_ONCE(ptemp[1]);
183 /* Force Read */
184 rmb();
185
186 edac_printk(KERN_ALERT, EDAC_MC, "Read Data [0x%X, 0x%X]\n",
187 reg, read_reg);
188
189 dma_free_coherent(mci->pdev, 16, ptemp, dma_handle);
190
191 return count;
192}
193
194static const struct file_operations altr_sdr_mc_debug_inject_fops = {
195 .open = simple_open,
196 .write = altr_sdr_mc_err_inject_write,
197 .llseek = generic_file_llseek,
198};
199
200static void altr_sdr_mc_create_debugfs_nodes(struct mem_ctl_info *mci)
201{
bba3b31e
BP
202 if (!IS_ENABLED(CONFIG_EDAC_DEBUG))
203 return;
204
205 if (!mci->debugfs)
206 return;
207
b8978bad 208 edac_debugfs_create_file("altr_trigger", S_IWUSR, mci->debugfs, mci,
bba3b31e 209 &altr_sdr_mc_debug_inject_fops);
71bcada8 210}
71bcada8 211
f9ae487e
TT
212/* Get total memory size from Open Firmware DTB */
213static unsigned long get_total_mem(void)
71bcada8 214{
f9ae487e 215 struct device_node *np = NULL;
ff0abed4
CP
216 struct resource res;
217 int ret;
218 unsigned long total_mem = 0;
f9ae487e
TT
219
220 for_each_node_by_type(np, "memory") {
ff0abed4
CP
221 ret = of_address_to_resource(np, 0, &res);
222 if (ret)
223 continue;
224
225 total_mem += resource_size(&res);
f9ae487e
TT
226 }
227 edac_dbg(0, "total_mem 0x%lx\n", total_mem);
228 return total_mem;
71bcada8
TT
229}
230
143f4a5a 231static const struct of_device_id altr_sdram_ctrl_of_match[] = {
2c911f6c
AB
232 { .compatible = "altr,sdram-edac", .data = &c5_data},
233 { .compatible = "altr,sdram-edac-a10", .data = &a10_data},
143f4a5a
TT
234 {},
235};
236MODULE_DEVICE_TABLE(of, altr_sdram_ctrl_of_match);
237
73bcc942
TT
238static int a10_init(struct regmap *mc_vbase)
239{
240 if (regmap_update_bits(mc_vbase, A10_INTMODE_OFST,
241 A10_INTMODE_SB_INT, A10_INTMODE_SB_INT)) {
242 edac_printk(KERN_ERR, EDAC_MC,
243 "Error setting SB IRQ mode\n");
244 return -ENODEV;
245 }
246
247 if (regmap_write(mc_vbase, A10_SERRCNTREG_OFST, 1)) {
248 edac_printk(KERN_ERR, EDAC_MC,
249 "Error setting trigger count\n");
250 return -ENODEV;
251 }
252
253 return 0;
254}
255
256static int a10_unmask_irq(struct platform_device *pdev, u32 mask)
257{
258 void __iomem *sm_base;
259 int ret = 0;
260
261 if (!request_mem_region(A10_SYMAN_INTMASK_CLR, sizeof(u32),
262 dev_name(&pdev->dev))) {
263 edac_printk(KERN_ERR, EDAC_MC,
264 "Unable to request mem region\n");
265 return -EBUSY;
266 }
267
268 sm_base = ioremap(A10_SYMAN_INTMASK_CLR, sizeof(u32));
269 if (!sm_base) {
270 edac_printk(KERN_ERR, EDAC_MC,
271 "Unable to ioremap device\n");
272
273 ret = -ENOMEM;
274 goto release;
275 }
276
277 iowrite32(mask, sm_base);
278
279 iounmap(sm_base);
280
281release:
282 release_mem_region(A10_SYMAN_INTMASK_CLR, sizeof(u32));
283
284 return ret;
285}
286
71bcada8
TT
287static int altr_sdram_probe(struct platform_device *pdev)
288{
143f4a5a 289 const struct of_device_id *id;
71bcada8
TT
290 struct edac_mc_layer layers[2];
291 struct mem_ctl_info *mci;
292 struct altr_sdram_mc_data *drvdata;
143f4a5a 293 const struct altr_sdram_prv_data *priv;
71bcada8
TT
294 struct regmap *mc_vbase;
295 struct dimm_info *dimm;
143f4a5a 296 u32 read_reg;
73bcc942
TT
297 int irq, irq2, res = 0;
298 unsigned long mem_size, irqflags = 0;
143f4a5a
TT
299
300 id = of_match_device(altr_sdram_ctrl_of_match, &pdev->dev);
301 if (!id)
302 return -ENODEV;
71bcada8 303
71bcada8
TT
304 /* Grab the register range from the sdr controller in device tree */
305 mc_vbase = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
306 "altr,sdr-syscon");
307 if (IS_ERR(mc_vbase)) {
308 edac_printk(KERN_ERR, EDAC_MC,
309 "regmap for altr,sdr-syscon lookup failed.\n");
310 return -ENODEV;
311 }
312
143f4a5a
TT
313 /* Check specific dependencies for the module */
314 priv = of_match_node(altr_sdram_ctrl_of_match,
315 pdev->dev.of_node)->data;
316
317 /* Validate the SDRAM controller has ECC enabled */
318 if (regmap_read(mc_vbase, priv->ecc_ctrl_offset, &read_reg) ||
319 ((read_reg & priv->ecc_ctl_en_mask) != priv->ecc_ctl_en_mask)) {
71bcada8
TT
320 edac_printk(KERN_ERR, EDAC_MC,
321 "No ECC/ECC disabled [0x%08X]\n", read_reg);
322 return -ENODEV;
323 }
324
325 /* Grab memory size from device tree. */
f9ae487e 326 mem_size = get_total_mem();
71bcada8 327 if (!mem_size) {
f9ae487e 328 edac_printk(KERN_ERR, EDAC_MC, "Unable to calculate memory size\n");
71bcada8
TT
329 return -ENODEV;
330 }
331
143f4a5a
TT
332 /* Ensure the SDRAM Interrupt is disabled */
333 if (regmap_update_bits(mc_vbase, priv->ecc_irq_en_offset,
334 priv->ecc_irq_en_mask, 0)) {
335 edac_printk(KERN_ERR, EDAC_MC,
336 "Error disabling SDRAM ECC IRQ\n");
337 return -ENODEV;
338 }
339
340 /* Toggle to clear the SDRAM Error count */
341 if (regmap_update_bits(mc_vbase, priv->ecc_cnt_rst_offset,
342 priv->ecc_cnt_rst_mask,
343 priv->ecc_cnt_rst_mask)) {
344 edac_printk(KERN_ERR, EDAC_MC,
345 "Error clearing SDRAM ECC count\n");
346 return -ENODEV;
347 }
348
349 if (regmap_update_bits(mc_vbase, priv->ecc_cnt_rst_offset,
350 priv->ecc_cnt_rst_mask, 0)) {
71bcada8 351 edac_printk(KERN_ERR, EDAC_MC,
143f4a5a 352 "Error clearing SDRAM ECC count\n");
71bcada8
TT
353 return -ENODEV;
354 }
355
356 irq = platform_get_irq(pdev, 0);
357 if (irq < 0) {
358 edac_printk(KERN_ERR, EDAC_MC,
359 "No irq %d in DT\n", irq);
360 return -ENODEV;
361 }
362
73bcc942
TT
363 /* Arria10 has a 2nd IRQ */
364 irq2 = platform_get_irq(pdev, 1);
365
71bcada8
TT
366 layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
367 layers[0].size = 1;
368 layers[0].is_virt_csrow = true;
369 layers[1].type = EDAC_MC_LAYER_CHANNEL;
370 layers[1].size = 1;
371 layers[1].is_virt_csrow = false;
372 mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers,
373 sizeof(struct altr_sdram_mc_data));
374 if (!mci)
375 return -ENOMEM;
376
377 mci->pdev = &pdev->dev;
378 drvdata = mci->pvt_info;
379 drvdata->mc_vbase = mc_vbase;
143f4a5a 380 drvdata->data = priv;
71bcada8
TT
381 platform_set_drvdata(pdev, mci);
382
383 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
143f4a5a
TT
384 edac_printk(KERN_ERR, EDAC_MC,
385 "Unable to get managed device resource\n");
71bcada8
TT
386 res = -ENOMEM;
387 goto free;
388 }
389
390 mci->mtype_cap = MEM_FLAG_DDR3;
391 mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_SECDED;
392 mci->edac_cap = EDAC_FLAG_SECDED;
393 mci->mod_name = EDAC_MOD_STR;
71bcada8
TT
394 mci->ctl_name = dev_name(&pdev->dev);
395 mci->scrub_mode = SCRUB_SW_SRC;
396 mci->dev_name = dev_name(&pdev->dev);
397
398 dimm = *mci->dimms;
399 dimm->nr_pages = ((mem_size - 1) >> PAGE_SHIFT) + 1;
400 dimm->grain = 8;
401 dimm->dtype = DEV_X8;
402 dimm->mtype = MEM_DDR3;
403 dimm->edac_mode = EDAC_SECDED;
404
405 res = edac_mc_add_mc(mci);
406 if (res < 0)
407 goto err;
408
73bcc942
TT
409 /* Only the Arria10 has separate IRQs */
410 if (irq2 > 0) {
411 /* Arria10 specific initialization */
412 res = a10_init(mc_vbase);
413 if (res < 0)
414 goto err2;
415
416 res = devm_request_irq(&pdev->dev, irq2,
417 altr_sdram_mc_err_handler,
418 IRQF_SHARED, dev_name(&pdev->dev), mci);
419 if (res < 0) {
420 edac_mc_printk(mci, KERN_ERR,
421 "Unable to request irq %d\n", irq2);
422 res = -ENODEV;
423 goto err2;
424 }
425
426 res = a10_unmask_irq(pdev, A10_DDR0_IRQ_MASK);
427 if (res < 0)
428 goto err2;
429
430 irqflags = IRQF_SHARED;
431 }
432
71bcada8 433 res = devm_request_irq(&pdev->dev, irq, altr_sdram_mc_err_handler,
73bcc942 434 irqflags, dev_name(&pdev->dev), mci);
71bcada8
TT
435 if (res < 0) {
436 edac_mc_printk(mci, KERN_ERR,
437 "Unable to request irq %d\n", irq);
438 res = -ENODEV;
439 goto err2;
440 }
441
143f4a5a
TT
442 /* Infrastructure ready - enable the IRQ */
443 if (regmap_update_bits(drvdata->mc_vbase, priv->ecc_irq_en_offset,
444 priv->ecc_irq_en_mask, priv->ecc_irq_en_mask)) {
71bcada8
TT
445 edac_mc_printk(mci, KERN_ERR,
446 "Error enabling SDRAM ECC IRQ\n");
447 res = -ENODEV;
448 goto err2;
449 }
450
451 altr_sdr_mc_create_debugfs_nodes(mci);
452
453 devres_close_group(&pdev->dev, NULL);
454
455 return 0;
456
457err2:
458 edac_mc_del_mc(&pdev->dev);
459err:
460 devres_release_group(&pdev->dev, NULL);
461free:
462 edac_mc_free(mci);
463 edac_printk(KERN_ERR, EDAC_MC,
464 "EDAC Probe Failed; Error %d\n", res);
465
466 return res;
467}
468
469static int altr_sdram_remove(struct platform_device *pdev)
470{
471 struct mem_ctl_info *mci = platform_get_drvdata(pdev);
472
473 edac_mc_del_mc(&pdev->dev);
474 edac_mc_free(mci);
475 platform_set_drvdata(pdev, NULL);
476
477 return 0;
478}
479
6f2b6422
AT
480/*
481 * If you want to suspend, need to disable EDAC by removing it
482 * from the device tree or defconfig.
483 */
484#ifdef CONFIG_PM
485static int altr_sdram_prepare(struct device *dev)
486{
487 pr_err("Suspend not allowed when EDAC is enabled.\n");
488
489 return -EPERM;
490}
491
492static const struct dev_pm_ops altr_sdram_pm_ops = {
493 .prepare = altr_sdram_prepare,
494};
495#endif
496
71bcada8
TT
497static struct platform_driver altr_sdram_edac_driver = {
498 .probe = altr_sdram_probe,
499 .remove = altr_sdram_remove,
500 .driver = {
501 .name = "altr_sdram_edac",
6f2b6422
AT
502#ifdef CONFIG_PM
503 .pm = &altr_sdram_pm_ops,
504#endif
71bcada8
TT
505 .of_match_table = altr_sdram_ctrl_of_match,
506 },
507};
508
509module_platform_driver(altr_sdram_edac_driver);
510
c3eea194
TT
511/************************* EDAC Parent Probe *************************/
512
513static const struct of_device_id altr_edac_device_of_match[];
514
515static const struct of_device_id altr_edac_of_match[] = {
516 { .compatible = "altr,socfpga-ecc-manager" },
517 {},
518};
519MODULE_DEVICE_TABLE(of, altr_edac_of_match);
520
521static int altr_edac_probe(struct platform_device *pdev)
522{
523 of_platform_populate(pdev->dev.of_node, altr_edac_device_of_match,
524 NULL, &pdev->dev);
525 return 0;
526}
527
528static struct platform_driver altr_edac_driver = {
529 .probe = altr_edac_probe,
530 .driver = {
531 .name = "socfpga_ecc_manager",
532 .of_match_table = altr_edac_of_match,
533 },
534};
535module_platform_driver(altr_edac_driver);
536
537/************************* EDAC Device Functions *************************/
538
539/*
540 * EDAC Device Functions (shared between various IPs).
541 * The discrete memories use the EDAC Device framework. The probe
542 * and error handling functions are very similar between memories
543 * so they are shared. The memory allocation and freeing for EDAC
544 * trigger testing are different for each memory.
545 */
546
1cf70377
TT
547static const struct edac_device_prv_data ocramecc_data;
548static const struct edac_device_prv_data l2ecc_data;
549static const struct edac_device_prv_data a10_ocramecc_data;
550static const struct edac_device_prv_data a10_l2ecc_data;
c3eea194 551
c3eea194
TT
552static irqreturn_t altr_edac_device_handler(int irq, void *dev_id)
553{
554 irqreturn_t ret_value = IRQ_NONE;
555 struct edac_device_ctl_info *dci = dev_id;
556 struct altr_edac_device_dev *drvdata = dci->pvt_info;
557 const struct edac_device_prv_data *priv = drvdata->data;
558
559 if (irq == drvdata->sb_irq) {
560 if (priv->ce_clear_mask)
561 writel(priv->ce_clear_mask, drvdata->base);
562 edac_device_handle_ce(dci, 0, 0, drvdata->edac_dev_name);
563 ret_value = IRQ_HANDLED;
564 } else if (irq == drvdata->db_irq) {
565 if (priv->ue_clear_mask)
566 writel(priv->ue_clear_mask, drvdata->base);
567 edac_device_handle_ue(dci, 0, 0, drvdata->edac_dev_name);
568 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
569 ret_value = IRQ_HANDLED;
570 } else {
571 WARN_ON(1);
572 }
573
574 return ret_value;
575}
576
577static ssize_t altr_edac_device_trig(struct file *file,
578 const char __user *user_buf,
579 size_t count, loff_t *ppos)
580
581{
582 u32 *ptemp, i, error_mask;
583 int result = 0;
584 u8 trig_type;
585 unsigned long flags;
586 struct edac_device_ctl_info *edac_dci = file->private_data;
587 struct altr_edac_device_dev *drvdata = edac_dci->pvt_info;
588 const struct edac_device_prv_data *priv = drvdata->data;
589 void *generic_ptr = edac_dci->dev;
590
591 if (!user_buf || get_user(trig_type, user_buf))
592 return -EFAULT;
593
594 if (!priv->alloc_mem)
595 return -ENOMEM;
596
597 /*
598 * Note that generic_ptr is initialized to the device * but in
599 * some alloc_functions, this is overridden and returns data.
600 */
601 ptemp = priv->alloc_mem(priv->trig_alloc_sz, &generic_ptr);
602 if (!ptemp) {
603 edac_printk(KERN_ERR, EDAC_DEVICE,
604 "Inject: Buffer Allocation error\n");
605 return -ENOMEM;
606 }
607
608 if (trig_type == ALTR_UE_TRIGGER_CHAR)
609 error_mask = priv->ue_set_mask;
610 else
611 error_mask = priv->ce_set_mask;
612
613 edac_printk(KERN_ALERT, EDAC_DEVICE,
614 "Trigger Error Mask (0x%X)\n", error_mask);
615
616 local_irq_save(flags);
617 /* write ECC corrupted data out. */
618 for (i = 0; i < (priv->trig_alloc_sz / sizeof(*ptemp)); i++) {
619 /* Read data so we're in the correct state */
620 rmb();
621 if (ACCESS_ONCE(ptemp[i]))
622 result = -1;
623 /* Toggle Error bit (it is latched), leave ECC enabled */
811fce4f
TT
624 writel(error_mask, (drvdata->base + priv->set_err_ofst));
625 writel(priv->ecc_enable_mask, (drvdata->base +
626 priv->set_err_ofst));
c3eea194
TT
627 ptemp[i] = i;
628 }
629 /* Ensure it has been written out */
630 wmb();
631 local_irq_restore(flags);
632
633 if (result)
634 edac_printk(KERN_ERR, EDAC_DEVICE, "Mem Not Cleared\n");
635
636 /* Read out written data. ECC error caused here */
637 for (i = 0; i < ALTR_TRIGGER_READ_WRD_CNT; i++)
638 if (ACCESS_ONCE(ptemp[i]) != i)
639 edac_printk(KERN_ERR, EDAC_DEVICE,
640 "Read doesn't match written data\n");
641
642 if (priv->free_mem)
643 priv->free_mem(ptemp, priv->trig_alloc_sz, generic_ptr);
644
645 return count;
646}
647
648static const struct file_operations altr_edac_device_inject_fops = {
649 .open = simple_open,
650 .write = altr_edac_device_trig,
651 .llseek = generic_file_llseek,
652};
653
c7b4be8d
TT
654static ssize_t altr_edac_a10_device_trig(struct file *file,
655 const char __user *user_buf,
656 size_t count, loff_t *ppos);
657
658static const struct file_operations altr_edac_a10_device_inject_fops = {
659 .open = simple_open,
660 .write = altr_edac_a10_device_trig,
661 .llseek = generic_file_llseek,
662};
663
c3eea194
TT
664static void altr_create_edacdev_dbgfs(struct edac_device_ctl_info *edac_dci,
665 const struct edac_device_prv_data *priv)
666{
667 struct altr_edac_device_dev *drvdata = edac_dci->pvt_info;
668
669 if (!IS_ENABLED(CONFIG_EDAC_DEBUG))
670 return;
671
672 drvdata->debugfs_dir = edac_debugfs_create_dir(drvdata->edac_dev_name);
673 if (!drvdata->debugfs_dir)
674 return;
675
f399f34b 676 if (!edac_debugfs_create_file("altr_trigger", S_IWUSR,
c3eea194 677 drvdata->debugfs_dir, edac_dci,
e17ced2c 678 priv->inject_fops))
c3eea194
TT
679 debugfs_remove_recursive(drvdata->debugfs_dir);
680}
681
682static const struct of_device_id altr_edac_device_of_match[] = {
683#ifdef CONFIG_EDAC_ALTERA_L2C
2c911f6c 684 { .compatible = "altr,socfpga-l2-ecc", .data = &l2ecc_data },
c3eea194
TT
685#endif
686#ifdef CONFIG_EDAC_ALTERA_OCRAM
2c911f6c 687 { .compatible = "altr,socfpga-ocram-ecc", .data = &ocramecc_data },
c3eea194
TT
688#endif
689 {},
690};
691MODULE_DEVICE_TABLE(of, altr_edac_device_of_match);
692
693/*
694 * altr_edac_device_probe()
695 * This is a generic EDAC device driver that will support
696 * various Altera memory devices such as the L2 cache ECC and
697 * OCRAM ECC as well as the memories for other peripherals.
698 * Module specific initialization is done by passing the
699 * function index in the device tree.
700 */
701static int altr_edac_device_probe(struct platform_device *pdev)
702{
703 struct edac_device_ctl_info *dci;
704 struct altr_edac_device_dev *drvdata;
705 struct resource *r;
706 int res = 0;
707 struct device_node *np = pdev->dev.of_node;
708 char *ecc_name = (char *)np->name;
709 static int dev_instance;
710
711 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
712 edac_printk(KERN_ERR, EDAC_DEVICE,
713 "Unable to open devm\n");
714 return -ENOMEM;
715 }
716
717 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
718 if (!r) {
719 edac_printk(KERN_ERR, EDAC_DEVICE,
720 "Unable to get mem resource\n");
721 res = -ENODEV;
722 goto fail;
723 }
724
725 if (!devm_request_mem_region(&pdev->dev, r->start, resource_size(r),
726 dev_name(&pdev->dev))) {
727 edac_printk(KERN_ERR, EDAC_DEVICE,
728 "%s:Error requesting mem region\n", ecc_name);
729 res = -EBUSY;
730 goto fail;
731 }
732
733 dci = edac_device_alloc_ctl_info(sizeof(*drvdata), ecc_name,
734 1, ecc_name, 1, 0, NULL, 0,
735 dev_instance++);
736
737 if (!dci) {
738 edac_printk(KERN_ERR, EDAC_DEVICE,
739 "%s: Unable to allocate EDAC device\n", ecc_name);
740 res = -ENOMEM;
741 goto fail;
742 }
743
744 drvdata = dci->pvt_info;
745 dci->dev = &pdev->dev;
746 platform_set_drvdata(pdev, dci);
747 drvdata->edac_dev_name = ecc_name;
748
749 drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
750 if (!drvdata->base)
751 goto fail1;
752
753 /* Get driver specific data for this EDAC device */
754 drvdata->data = of_match_node(altr_edac_device_of_match, np)->data;
755
756 /* Check specific dependencies for the module */
757 if (drvdata->data->setup) {
328ca7ae 758 res = drvdata->data->setup(drvdata);
c3eea194
TT
759 if (res)
760 goto fail1;
761 }
762
763 drvdata->sb_irq = platform_get_irq(pdev, 0);
764 res = devm_request_irq(&pdev->dev, drvdata->sb_irq,
765 altr_edac_device_handler,
766 0, dev_name(&pdev->dev), dci);
767 if (res)
768 goto fail1;
769
770 drvdata->db_irq = platform_get_irq(pdev, 1);
771 res = devm_request_irq(&pdev->dev, drvdata->db_irq,
772 altr_edac_device_handler,
773 0, dev_name(&pdev->dev), dci);
774 if (res)
775 goto fail1;
776
777 dci->mod_name = "Altera ECC Manager";
778 dci->dev_name = drvdata->edac_dev_name;
779
780 res = edac_device_add_device(dci);
781 if (res)
782 goto fail1;
783
784 altr_create_edacdev_dbgfs(dci, drvdata->data);
785
786 devres_close_group(&pdev->dev, NULL);
787
788 return 0;
789
790fail1:
791 edac_device_free_ctl_info(dci);
792fail:
793 devres_release_group(&pdev->dev, NULL);
794 edac_printk(KERN_ERR, EDAC_DEVICE,
795 "%s:Error setting up EDAC device: %d\n", ecc_name, res);
796
797 return res;
798}
799
800static int altr_edac_device_remove(struct platform_device *pdev)
801{
802 struct edac_device_ctl_info *dci = platform_get_drvdata(pdev);
803 struct altr_edac_device_dev *drvdata = dci->pvt_info;
804
805 debugfs_remove_recursive(drvdata->debugfs_dir);
806 edac_device_del_device(&pdev->dev);
807 edac_device_free_ctl_info(dci);
808
809 return 0;
810}
811
812static struct platform_driver altr_edac_device_driver = {
813 .probe = altr_edac_device_probe,
814 .remove = altr_edac_device_remove,
815 .driver = {
816 .name = "altr_edac_device",
817 .of_match_table = altr_edac_device_of_match,
818 },
819};
820module_platform_driver(altr_edac_device_driver);
821
6b300fb9 822/******************* Arria10 Device ECC Shared Functions *****************/
c3eea194 823
1aa6eb5c
AB
824/*
825 * Test for memory's ECC dependencies upon entry because platform specific
826 * startup should have initialized the memory and enabled the ECC.
827 * Can't turn on ECC here because accessing un-initialized memory will
828 * cause CE/UE errors possibly causing an ABORT.
829 */
6b300fb9
TT
830static int __maybe_unused
831altr_check_ecc_deps(struct altr_edac_device_dev *device)
1aa6eb5c
AB
832{
833 void __iomem *base = device->base;
834 const struct edac_device_prv_data *prv = device->data;
835
836 if (readl(base + prv->ecc_en_ofst) & prv->ecc_enable_mask)
837 return 0;
838
839 edac_printk(KERN_ERR, EDAC_DEVICE,
840 "%s: No ECC present or ECC disabled.\n",
841 device->edac_dev_name);
842 return -ENODEV;
843}
c3eea194 844
6b300fb9
TT
845static irqreturn_t __maybe_unused altr_edac_a10_ecc_irq(int irq, void *dev_id)
846{
847 struct altr_edac_device_dev *dci = dev_id;
848 void __iomem *base = dci->base;
849
850 if (irq == dci->sb_irq) {
851 writel(ALTR_A10_ECC_SERRPENA,
852 base + ALTR_A10_ECC_INTSTAT_OFST);
853 edac_device_handle_ce(dci->edac_dev, 0, 0, dci->edac_dev_name);
854
855 return IRQ_HANDLED;
856 } else if (irq == dci->db_irq) {
857 writel(ALTR_A10_ECC_DERRPENA,
858 base + ALTR_A10_ECC_INTSTAT_OFST);
859 edac_device_handle_ue(dci->edac_dev, 0, 0, dci->edac_dev_name);
860 if (dci->data->panic)
861 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
862
863 return IRQ_HANDLED;
864 }
865
866 WARN_ON(1);
867
868 return IRQ_NONE;
869}
870
1166fde9
TT
871/******************* Arria10 Memory Buffer Functions *********************/
872
873static inline int a10_get_irq_mask(struct device_node *np)
874{
875 int irq;
876 const u32 *handle = of_get_property(np, "interrupts", NULL);
877
878 if (!handle)
879 return -ENODEV;
880 irq = be32_to_cpup(handle);
881 return irq;
882}
883
884static inline void ecc_set_bits(u32 bit_mask, void __iomem *ioaddr)
885{
886 u32 value = readl(ioaddr);
887
888 value |= bit_mask;
889 writel(value, ioaddr);
890}
891
892static inline void ecc_clear_bits(u32 bit_mask, void __iomem *ioaddr)
893{
894 u32 value = readl(ioaddr);
895
896 value &= ~bit_mask;
897 writel(value, ioaddr);
898}
899
900static inline int ecc_test_bits(u32 bit_mask, void __iomem *ioaddr)
901{
902 u32 value = readl(ioaddr);
903
904 return (value & bit_mask) ? 1 : 0;
905}
906
907/*
908 * This function uses the memory initialization block in the Arria10 ECC
909 * controller to initialize/clear the entire memory data and ECC data.
910 */
911static int __maybe_unused altr_init_memory_port(void __iomem *ioaddr, int port)
912{
913 int limit = ALTR_A10_ECC_INIT_WATCHDOG_10US;
914 u32 init_mask, stat_mask, clear_mask;
915 int ret = 0;
916
917 if (port) {
918 init_mask = ALTR_A10_ECC_INITB;
919 stat_mask = ALTR_A10_ECC_INITCOMPLETEB;
920 clear_mask = ALTR_A10_ECC_ERRPENB_MASK;
921 } else {
922 init_mask = ALTR_A10_ECC_INITA;
923 stat_mask = ALTR_A10_ECC_INITCOMPLETEA;
924 clear_mask = ALTR_A10_ECC_ERRPENA_MASK;
925 }
926
927 ecc_set_bits(init_mask, (ioaddr + ALTR_A10_ECC_CTRL_OFST));
928 while (limit--) {
929 if (ecc_test_bits(stat_mask,
930 (ioaddr + ALTR_A10_ECC_INITSTAT_OFST)))
931 break;
932 udelay(1);
933 }
934 if (limit < 0)
935 ret = -EBUSY;
936
937 /* Clear any pending ECC interrupts */
938 writel(clear_mask, (ioaddr + ALTR_A10_ECC_INTSTAT_OFST));
939
940 return ret;
941}
942
943static __init int __maybe_unused
944altr_init_a10_ecc_block(struct device_node *np, u32 irq_mask,
945 u32 ecc_ctrl_en_mask, bool dual_port)
946{
947 int ret = 0;
948 void __iomem *ecc_block_base;
949 struct regmap *ecc_mgr_map;
950 char *ecc_name;
951 struct device_node *np_eccmgr;
952
953 ecc_name = (char *)np->name;
954
955 /* Get the ECC Manager - parent of the device EDACs */
956 np_eccmgr = of_get_parent(np);
957 ecc_mgr_map = syscon_regmap_lookup_by_phandle(np_eccmgr,
958 "altr,sysmgr-syscon");
959 of_node_put(np_eccmgr);
960 if (IS_ERR(ecc_mgr_map)) {
961 edac_printk(KERN_ERR, EDAC_DEVICE,
962 "Unable to get syscon altr,sysmgr-syscon\n");
963 return -ENODEV;
964 }
965
966 /* Map the ECC Block */
967 ecc_block_base = of_iomap(np, 0);
968 if (!ecc_block_base) {
969 edac_printk(KERN_ERR, EDAC_DEVICE,
970 "Unable to map %s ECC block\n", ecc_name);
971 return -ENODEV;
972 }
973
974 /* Disable ECC */
975 regmap_write(ecc_mgr_map, A10_SYSMGR_ECC_INTMASK_SET_OFST, irq_mask);
976 writel(ALTR_A10_ECC_SERRINTEN,
977 (ecc_block_base + ALTR_A10_ECC_ERRINTENR_OFST));
978 ecc_clear_bits(ecc_ctrl_en_mask,
979 (ecc_block_base + ALTR_A10_ECC_CTRL_OFST));
980 /* Ensure all writes complete */
981 wmb();
982 /* Use HW initialization block to initialize memory for ECC */
983 ret = altr_init_memory_port(ecc_block_base, 0);
984 if (ret) {
985 edac_printk(KERN_ERR, EDAC_DEVICE,
986 "ECC: cannot init %s PORTA memory\n", ecc_name);
987 goto out;
988 }
989
990 if (dual_port) {
991 ret = altr_init_memory_port(ecc_block_base, 1);
992 if (ret) {
993 edac_printk(KERN_ERR, EDAC_DEVICE,
994 "ECC: cannot init %s PORTB memory\n",
995 ecc_name);
996 goto out;
997 }
998 }
999
1000 /* Interrupt mode set to every SBERR */
1001 regmap_write(ecc_mgr_map, ALTR_A10_ECC_INTMODE_OFST,
1002 ALTR_A10_ECC_INTMODE);
1003 /* Enable ECC */
1004 ecc_set_bits(ecc_ctrl_en_mask, (ecc_block_base +
1005 ALTR_A10_ECC_CTRL_OFST));
1006 writel(ALTR_A10_ECC_SERRINTEN,
1007 (ecc_block_base + ALTR_A10_ECC_ERRINTENS_OFST));
1008 regmap_write(ecc_mgr_map, A10_SYSMGR_ECC_INTMASK_CLR_OFST, irq_mask);
1009 /* Ensure all writes complete */
1010 wmb();
1011out:
1012 iounmap(ecc_block_base);
1013 return ret;
1014}
1015
25b223dd
TT
1016static int socfpga_is_a10(void)
1017{
1018 return of_machine_is_compatible("altr,socfpga-arria10");
1019}
1020
1166fde9
TT
1021static int validate_parent_available(struct device_node *np);
1022static const struct of_device_id altr_edac_a10_device_of_match[];
1023static int __init __maybe_unused altr_init_a10_ecc_device_type(char *compat)
1024{
1025 int irq;
25b223dd
TT
1026 struct device_node *child, *np;
1027
1028 if (!socfpga_is_a10())
1029 return -ENODEV;
1030
1031 np = of_find_compatible_node(NULL, NULL,
1032 "altr,socfpga-a10-ecc-manager");
1166fde9
TT
1033 if (!np) {
1034 edac_printk(KERN_ERR, EDAC_DEVICE, "ECC Manager not found\n");
1035 return -ENODEV;
1036 }
1037
1038 for_each_child_of_node(np, child) {
1039 const struct of_device_id *pdev_id;
1040 const struct edac_device_prv_data *prv;
1041
1042 if (!of_device_is_available(child))
1043 continue;
1044 if (!of_device_is_compatible(child, compat))
1045 continue;
1046
1047 if (validate_parent_available(child))
1048 continue;
1049
1050 irq = a10_get_irq_mask(child);
1051 if (irq < 0)
1052 continue;
1053
1054 /* Get matching node and check for valid result */
1055 pdev_id = of_match_node(altr_edac_a10_device_of_match, child);
1056 if (IS_ERR_OR_NULL(pdev_id))
1057 continue;
1058
1059 /* Validate private data pointer before dereferencing */
1060 prv = pdev_id->data;
1061 if (!prv)
1062 continue;
1063
1064 altr_init_a10_ecc_block(child, BIT(irq),
1065 prv->ecc_enable_mask, 0);
1066 }
1067
1068 of_node_put(np);
1069 return 0;
1070}
1071
6b300fb9
TT
1072/*********************** OCRAM EDAC Device Functions *********************/
1073
1074#ifdef CONFIG_EDAC_ALTERA_OCRAM
1075
c3eea194
TT
1076static void *ocram_alloc_mem(size_t size, void **other)
1077{
1078 struct device_node *np;
1079 struct gen_pool *gp;
1080 void *sram_addr;
1081
1082 np = of_find_compatible_node(NULL, NULL, "altr,socfpga-ocram-ecc");
1083 if (!np)
1084 return NULL;
1085
1086 gp = of_gen_pool_get(np, "iram", 0);
1087 of_node_put(np);
1088 if (!gp)
1089 return NULL;
1090
1091 sram_addr = (void *)gen_pool_alloc(gp, size);
1092 if (!sram_addr)
1093 return NULL;
1094
1095 memset(sram_addr, 0, size);
1096 /* Ensure data is written out */
1097 wmb();
1098
1099 /* Remember this handle for freeing later */
1100 *other = gp;
1101
1102 return sram_addr;
1103}
1104
1105static void ocram_free_mem(void *p, size_t size, void *other)
1106{
1107 gen_pool_free((struct gen_pool *)other, (u32)p, size);
1108}
1109
1cf70377 1110static const struct edac_device_prv_data ocramecc_data = {
aa1f06dc 1111 .setup = altr_check_ecc_deps,
c3eea194
TT
1112 .ce_clear_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_SERR),
1113 .ue_clear_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_DERR),
c3eea194
TT
1114 .alloc_mem = ocram_alloc_mem,
1115 .free_mem = ocram_free_mem,
1116 .ecc_enable_mask = ALTR_OCR_ECC_EN,
943ad917 1117 .ecc_en_ofst = ALTR_OCR_ECC_REG_OFFSET,
c3eea194
TT
1118 .ce_set_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_INJS),
1119 .ue_set_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_INJD),
811fce4f 1120 .set_err_ofst = ALTR_OCR_ECC_REG_OFFSET,
c3eea194 1121 .trig_alloc_sz = ALTR_TRIG_OCRAM_BYTE_SIZE,
e17ced2c 1122 .inject_fops = &altr_edac_device_inject_fops,
c3eea194
TT
1123};
1124
1cf70377 1125static const struct edac_device_prv_data a10_ocramecc_data = {
c7b4be8d
TT
1126 .setup = altr_check_ecc_deps,
1127 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1128 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
1129 .irq_status_mask = A10_SYSMGR_ECC_INTSTAT_OCRAM,
c7b4be8d
TT
1130 .ecc_enable_mask = ALTR_A10_OCRAM_ECC_EN_CTL,
1131 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1132 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1133 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1134 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1135 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1136 .inject_fops = &altr_edac_a10_device_inject_fops,
2b083d65
TT
1137 /*
1138 * OCRAM panic on uncorrectable error because sleep/resume
1139 * functions and FPGA contents are stored in OCRAM. Prefer
1140 * a kernel panic over executing/loading corrupted data.
1141 */
1142 .panic = true,
c7b4be8d
TT
1143};
1144
c3eea194
TT
1145#endif /* CONFIG_EDAC_ALTERA_OCRAM */
1146
1147/********************* L2 Cache EDAC Device Functions ********************/
1148
1149#ifdef CONFIG_EDAC_ALTERA_L2C
1150
1151static void *l2_alloc_mem(size_t size, void **other)
1152{
1153 struct device *dev = *other;
1154 void *ptemp = devm_kzalloc(dev, size, GFP_KERNEL);
1155
1156 if (!ptemp)
1157 return NULL;
1158
1159 /* Make sure everything is written out */
1160 wmb();
1161
1162 /*
1163 * Clean all cache levels up to LoC (includes L2)
1164 * This ensures the corrupted data is written into
1165 * L2 cache for readback test (which causes ECC error).
1166 */
1167 flush_cache_all();
1168
1169 return ptemp;
1170}
1171
1172static void l2_free_mem(void *p, size_t size, void *other)
1173{
1174 struct device *dev = other;
1175
1176 if (dev && p)
1177 devm_kfree(dev, p);
1178}
1179
1180/*
1181 * altr_l2_check_deps()
1182 * Test for L2 cache ECC dependencies upon entry because
1183 * platform specific startup should have initialized the L2
1184 * memory and enabled the ECC.
1185 * Bail if ECC is not enabled.
1186 * Note that L2 Cache Enable is forced at build time.
1187 */
328ca7ae 1188static int altr_l2_check_deps(struct altr_edac_device_dev *device)
c3eea194 1189{
328ca7ae 1190 void __iomem *base = device->base;
27439a1a
TT
1191 const struct edac_device_prv_data *prv = device->data;
1192
1193 if ((readl(base) & prv->ecc_enable_mask) ==
1194 prv->ecc_enable_mask)
c3eea194
TT
1195 return 0;
1196
1197 edac_printk(KERN_ERR, EDAC_DEVICE,
1198 "L2: No ECC present, or ECC disabled\n");
1199 return -ENODEV;
1200}
1201
13ab8448 1202static irqreturn_t altr_edac_a10_l2_irq(int irq, void *dev_id)
588cb03e 1203{
13ab8448
TT
1204 struct altr_edac_device_dev *dci = dev_id;
1205
1206 if (irq == dci->sb_irq) {
588cb03e
TT
1207 regmap_write(dci->edac->ecc_mgr_map,
1208 A10_SYSGMR_MPU_CLEAR_L2_ECC_OFST,
1209 A10_SYSGMR_MPU_CLEAR_L2_ECC_SB);
1210 edac_device_handle_ce(dci->edac_dev, 0, 0, dci->edac_dev_name);
13ab8448
TT
1211
1212 return IRQ_HANDLED;
1213 } else if (irq == dci->db_irq) {
588cb03e
TT
1214 regmap_write(dci->edac->ecc_mgr_map,
1215 A10_SYSGMR_MPU_CLEAR_L2_ECC_OFST,
1216 A10_SYSGMR_MPU_CLEAR_L2_ECC_MB);
1217 edac_device_handle_ue(dci->edac_dev, 0, 0, dci->edac_dev_name);
1218 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
13ab8448
TT
1219
1220 return IRQ_HANDLED;
588cb03e 1221 }
13ab8448
TT
1222
1223 WARN_ON(1);
1224
1225 return IRQ_NONE;
588cb03e
TT
1226}
1227
1cf70377 1228static const struct edac_device_prv_data l2ecc_data = {
c3eea194
TT
1229 .setup = altr_l2_check_deps,
1230 .ce_clear_mask = 0,
1231 .ue_clear_mask = 0,
c3eea194
TT
1232 .alloc_mem = l2_alloc_mem,
1233 .free_mem = l2_free_mem,
1234 .ecc_enable_mask = ALTR_L2_ECC_EN,
1235 .ce_set_mask = (ALTR_L2_ECC_EN | ALTR_L2_ECC_INJS),
1236 .ue_set_mask = (ALTR_L2_ECC_EN | ALTR_L2_ECC_INJD),
811fce4f 1237 .set_err_ofst = ALTR_L2_ECC_REG_OFFSET,
c3eea194 1238 .trig_alloc_sz = ALTR_TRIG_L2C_BYTE_SIZE,
e17ced2c 1239 .inject_fops = &altr_edac_device_inject_fops,
c3eea194
TT
1240};
1241
1cf70377 1242static const struct edac_device_prv_data a10_l2ecc_data = {
588cb03e
TT
1243 .setup = altr_l2_check_deps,
1244 .ce_clear_mask = ALTR_A10_L2_ECC_SERR_CLR,
1245 .ue_clear_mask = ALTR_A10_L2_ECC_MERR_CLR,
1246 .irq_status_mask = A10_SYSMGR_ECC_INTSTAT_L2,
588cb03e
TT
1247 .alloc_mem = l2_alloc_mem,
1248 .free_mem = l2_free_mem,
1249 .ecc_enable_mask = ALTR_A10_L2_ECC_EN_CTL,
1250 .ce_set_mask = ALTR_A10_L2_ECC_CE_INJ_MASK,
1251 .ue_set_mask = ALTR_A10_L2_ECC_UE_INJ_MASK,
1252 .set_err_ofst = ALTR_A10_L2_ECC_INJ_OFST,
1253 .ecc_irq_handler = altr_edac_a10_l2_irq,
1254 .trig_alloc_sz = ALTR_TRIG_L2C_BYTE_SIZE,
e17ced2c 1255 .inject_fops = &altr_edac_device_inject_fops,
588cb03e
TT
1256};
1257
c3eea194
TT
1258#endif /* CONFIG_EDAC_ALTERA_L2C */
1259
ab8c1e0f
TT
1260/********************* Ethernet Device Functions ********************/
1261
1262#ifdef CONFIG_EDAC_ALTERA_ETHERNET
1263
1264static const struct edac_device_prv_data a10_enetecc_data = {
1265 .setup = altr_check_ecc_deps,
1266 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1267 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
ab8c1e0f
TT
1268 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1269 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1270 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1271 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1272 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1273 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1274 .inject_fops = &altr_edac_a10_device_inject_fops,
1275};
1276
1277static int __init socfpga_init_ethernet_ecc(void)
1278{
1279 return altr_init_a10_ecc_device_type("altr,socfpga-eth-mac-ecc");
1280}
1281
1282early_initcall(socfpga_init_ethernet_ecc);
1283
1284#endif /* CONFIG_EDAC_ALTERA_ETHERNET */
1285
c6882fb2
TT
1286/********************** NAND Device Functions **********************/
1287
1288#ifdef CONFIG_EDAC_ALTERA_NAND
1289
1290static const struct edac_device_prv_data a10_nandecc_data = {
1291 .setup = altr_check_ecc_deps,
1292 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1293 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
c6882fb2
TT
1294 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1295 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1296 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1297 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1298 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1299 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1300 .inject_fops = &altr_edac_a10_device_inject_fops,
1301};
1302
1303static int __init socfpga_init_nand_ecc(void)
1304{
1305 return altr_init_a10_ecc_device_type("altr,socfpga-nand-ecc");
1306}
1307
1308early_initcall(socfpga_init_nand_ecc);
1309
1310#endif /* CONFIG_EDAC_ALTERA_NAND */
1311
e8263793
TT
1312/********************** DMA Device Functions **********************/
1313
1314#ifdef CONFIG_EDAC_ALTERA_DMA
1315
1316static const struct edac_device_prv_data a10_dmaecc_data = {
1317 .setup = altr_check_ecc_deps,
1318 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1319 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
e8263793
TT
1320 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1321 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1322 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1323 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1324 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1325 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1326 .inject_fops = &altr_edac_a10_device_inject_fops,
1327};
1328
1329static int __init socfpga_init_dma_ecc(void)
1330{
1331 return altr_init_a10_ecc_device_type("altr,socfpga-dma-ecc");
1332}
1333
1334early_initcall(socfpga_init_dma_ecc);
1335
1336#endif /* CONFIG_EDAC_ALTERA_DMA */
1337
c609581d
TT
1338/********************** USB Device Functions **********************/
1339
1340#ifdef CONFIG_EDAC_ALTERA_USB
1341
1342static const struct edac_device_prv_data a10_usbecc_data = {
1343 .setup = altr_check_ecc_deps,
1344 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1345 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
c609581d
TT
1346 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1347 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1348 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1349 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1350 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1351 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1352 .inject_fops = &altr_edac_a10_device_inject_fops,
1353};
1354
1355static int __init socfpga_init_usb_ecc(void)
1356{
1357 return altr_init_a10_ecc_device_type("altr,socfpga-usb-ecc");
1358}
1359
1360early_initcall(socfpga_init_usb_ecc);
1361
1362#endif /* CONFIG_EDAC_ALTERA_USB */
1363
485fe9e2
TT
1364/********************** QSPI Device Functions **********************/
1365
1366#ifdef CONFIG_EDAC_ALTERA_QSPI
1367
1368static const struct edac_device_prv_data a10_qspiecc_data = {
1369 .setup = altr_check_ecc_deps,
1370 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1371 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
485fe9e2
TT
1372 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1373 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1374 .ce_set_mask = ALTR_A10_ECC_TSERRA,
1375 .ue_set_mask = ALTR_A10_ECC_TDERRA,
1376 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1377 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1378 .inject_fops = &altr_edac_a10_device_inject_fops,
1379};
1380
1381static int __init socfpga_init_qspi_ecc(void)
1382{
1383 return altr_init_a10_ecc_device_type("altr,socfpga-qspi-ecc");
1384}
1385
1386early_initcall(socfpga_init_qspi_ecc);
1387
1388#endif /* CONFIG_EDAC_ALTERA_QSPI */
1389
91104984
TT
1390/********************* SDMMC Device Functions **********************/
1391
1392#ifdef CONFIG_EDAC_ALTERA_SDMMC
1393
1394static const struct edac_device_prv_data a10_sdmmceccb_data;
1395static int altr_portb_setup(struct altr_edac_device_dev *device)
1396{
1397 struct edac_device_ctl_info *dci;
1398 struct altr_edac_device_dev *altdev;
1399 char *ecc_name = "sdmmcb-ecc";
1400 int edac_idx, rc;
1401 struct device_node *np;
1402 const struct edac_device_prv_data *prv = &a10_sdmmceccb_data;
1403
1404 rc = altr_check_ecc_deps(device);
1405 if (rc)
1406 return rc;
1407
1408 np = of_find_compatible_node(NULL, NULL, "altr,socfpga-sdmmc-ecc");
1409 if (!np) {
1410 edac_printk(KERN_WARNING, EDAC_DEVICE, "SDMMC node not found\n");
1411 return -ENODEV;
1412 }
1413
1414 /* Create the PortB EDAC device */
1415 edac_idx = edac_device_alloc_index();
1416 dci = edac_device_alloc_ctl_info(sizeof(*altdev), ecc_name, 1,
1417 ecc_name, 1, 0, NULL, 0, edac_idx);
1418 if (!dci) {
1419 edac_printk(KERN_ERR, EDAC_DEVICE,
1420 "%s: Unable to allocate PortB EDAC device\n",
1421 ecc_name);
1422 return -ENOMEM;
1423 }
1424
1425 /* Initialize the PortB EDAC device structure from PortA structure */
1426 altdev = dci->pvt_info;
1427 *altdev = *device;
1428
1429 if (!devres_open_group(&altdev->ddev, altr_portb_setup, GFP_KERNEL))
1430 return -ENOMEM;
1431
1432 /* Update PortB specific values */
1433 altdev->edac_dev_name = ecc_name;
1434 altdev->edac_idx = edac_idx;
1435 altdev->edac_dev = dci;
1436 altdev->data = prv;
1437 dci->dev = &altdev->ddev;
1438 dci->ctl_name = "Altera ECC Manager";
1439 dci->mod_name = ecc_name;
1440 dci->dev_name = ecc_name;
1441
1442 /* Update the IRQs for PortB */
1443 altdev->sb_irq = irq_of_parse_and_map(np, 2);
1444 if (!altdev->sb_irq) {
1445 edac_printk(KERN_ERR, EDAC_DEVICE, "Error PortB SBIRQ alloc\n");
1446 rc = -ENODEV;
1447 goto err_release_group_1;
1448 }
1449 rc = devm_request_irq(&altdev->ddev, altdev->sb_irq,
1450 prv->ecc_irq_handler,
a29d64a4
TT
1451 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
1452 ecc_name, altdev);
91104984
TT
1453 if (rc) {
1454 edac_printk(KERN_ERR, EDAC_DEVICE, "PortB SBERR IRQ error\n");
1455 goto err_release_group_1;
1456 }
1457
1458 altdev->db_irq = irq_of_parse_and_map(np, 3);
1459 if (!altdev->db_irq) {
1460 edac_printk(KERN_ERR, EDAC_DEVICE, "Error PortB DBIRQ alloc\n");
1461 rc = -ENODEV;
1462 goto err_release_group_1;
1463 }
1464 rc = devm_request_irq(&altdev->ddev, altdev->db_irq,
1465 prv->ecc_irq_handler,
a29d64a4
TT
1466 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
1467 ecc_name, altdev);
91104984
TT
1468 if (rc) {
1469 edac_printk(KERN_ERR, EDAC_DEVICE, "PortB DBERR IRQ error\n");
1470 goto err_release_group_1;
1471 }
1472
1473 rc = edac_device_add_device(dci);
1474 if (rc) {
1475 edac_printk(KERN_ERR, EDAC_DEVICE,
1476 "edac_device_add_device portB failed\n");
1477 rc = -ENOMEM;
1478 goto err_release_group_1;
1479 }
1480 altr_create_edacdev_dbgfs(dci, prv);
1481
1482 list_add(&altdev->next, &altdev->edac->a10_ecc_devices);
1483
1484 devres_remove_group(&altdev->ddev, altr_portb_setup);
1485
1486 return 0;
1487
1488err_release_group_1:
1489 edac_device_free_ctl_info(dci);
1490 devres_release_group(&altdev->ddev, altr_portb_setup);
1491 edac_printk(KERN_ERR, EDAC_DEVICE,
1492 "%s:Error setting up EDAC device: %d\n", ecc_name, rc);
1493 return rc;
1494}
1495
1496static irqreturn_t altr_edac_a10_ecc_irq_portb(int irq, void *dev_id)
1497{
1498 struct altr_edac_device_dev *ad = dev_id;
1499 void __iomem *base = ad->base;
1500 const struct edac_device_prv_data *priv = ad->data;
1501
1502 if (irq == ad->sb_irq) {
1503 writel(priv->ce_clear_mask,
1504 base + ALTR_A10_ECC_INTSTAT_OFST);
1505 edac_device_handle_ce(ad->edac_dev, 0, 0, ad->edac_dev_name);
1506 return IRQ_HANDLED;
1507 } else if (irq == ad->db_irq) {
1508 writel(priv->ue_clear_mask,
1509 base + ALTR_A10_ECC_INTSTAT_OFST);
1510 edac_device_handle_ue(ad->edac_dev, 0, 0, ad->edac_dev_name);
1511 return IRQ_HANDLED;
1512 }
1513
1514 WARN_ONCE(1, "Unhandled IRQ%d on Port B.", irq);
1515
1516 return IRQ_NONE;
1517}
1518
1519static const struct edac_device_prv_data a10_sdmmcecca_data = {
1520 .setup = altr_portb_setup,
1521 .ce_clear_mask = ALTR_A10_ECC_SERRPENA,
1522 .ue_clear_mask = ALTR_A10_ECC_DERRPENA,
91104984
TT
1523 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1524 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1525 .ce_set_mask = ALTR_A10_ECC_SERRPENA,
1526 .ue_set_mask = ALTR_A10_ECC_DERRPENA,
1527 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1528 .ecc_irq_handler = altr_edac_a10_ecc_irq,
1529 .inject_fops = &altr_edac_a10_device_inject_fops,
1530};
1531
1532static const struct edac_device_prv_data a10_sdmmceccb_data = {
1533 .setup = altr_portb_setup,
1534 .ce_clear_mask = ALTR_A10_ECC_SERRPENB,
1535 .ue_clear_mask = ALTR_A10_ECC_DERRPENB,
91104984
TT
1536 .ecc_enable_mask = ALTR_A10_COMMON_ECC_EN_CTL,
1537 .ecc_en_ofst = ALTR_A10_ECC_CTRL_OFST,
1538 .ce_set_mask = ALTR_A10_ECC_TSERRB,
1539 .ue_set_mask = ALTR_A10_ECC_TDERRB,
1540 .set_err_ofst = ALTR_A10_ECC_INTTEST_OFST,
1541 .ecc_irq_handler = altr_edac_a10_ecc_irq_portb,
1542 .inject_fops = &altr_edac_a10_device_inject_fops,
1543};
1544
1545static int __init socfpga_init_sdmmc_ecc(void)
1546{
1547 int rc = -ENODEV;
25b223dd
TT
1548 struct device_node *child;
1549
1550 if (!socfpga_is_a10())
1551 return -ENODEV;
1552
1553 child = of_find_compatible_node(NULL, NULL, "altr,socfpga-sdmmc-ecc");
91104984
TT
1554 if (!child) {
1555 edac_printk(KERN_WARNING, EDAC_DEVICE, "SDMMC node not found\n");
1556 return -ENODEV;
1557 }
1558
1559 if (!of_device_is_available(child))
1560 goto exit;
1561
1562 if (validate_parent_available(child))
1563 goto exit;
1564
1565 rc = altr_init_a10_ecc_block(child, ALTR_A10_SDMMC_IRQ_MASK,
1566 a10_sdmmcecca_data.ecc_enable_mask, 1);
1567exit:
1568 of_node_put(child);
1569 return rc;
1570}
1571
1572early_initcall(socfpga_init_sdmmc_ecc);
1573
1574#endif /* CONFIG_EDAC_ALTERA_SDMMC */
1575
588cb03e 1576/********************* Arria10 EDAC Device Functions *************************/
ab564cb5
TT
1577static const struct of_device_id altr_edac_a10_device_of_match[] = {
1578#ifdef CONFIG_EDAC_ALTERA_L2C
1579 { .compatible = "altr,socfpga-a10-l2-ecc", .data = &a10_l2ecc_data },
1580#endif
1581#ifdef CONFIG_EDAC_ALTERA_OCRAM
1582 { .compatible = "altr,socfpga-a10-ocram-ecc",
1583 .data = &a10_ocramecc_data },
ab8c1e0f
TT
1584#endif
1585#ifdef CONFIG_EDAC_ALTERA_ETHERNET
1586 { .compatible = "altr,socfpga-eth-mac-ecc",
1587 .data = &a10_enetecc_data },
c6882fb2
TT
1588#endif
1589#ifdef CONFIG_EDAC_ALTERA_NAND
1590 { .compatible = "altr,socfpga-nand-ecc", .data = &a10_nandecc_data },
e8263793
TT
1591#endif
1592#ifdef CONFIG_EDAC_ALTERA_DMA
1593 { .compatible = "altr,socfpga-dma-ecc", .data = &a10_dmaecc_data },
c609581d
TT
1594#endif
1595#ifdef CONFIG_EDAC_ALTERA_USB
1596 { .compatible = "altr,socfpga-usb-ecc", .data = &a10_usbecc_data },
485fe9e2
TT
1597#endif
1598#ifdef CONFIG_EDAC_ALTERA_QSPI
1599 { .compatible = "altr,socfpga-qspi-ecc", .data = &a10_qspiecc_data },
91104984
TT
1600#endif
1601#ifdef CONFIG_EDAC_ALTERA_SDMMC
1602 { .compatible = "altr,socfpga-sdmmc-ecc", .data = &a10_sdmmcecca_data },
ab564cb5
TT
1603#endif
1604 {},
1605};
1606MODULE_DEVICE_TABLE(of, altr_edac_a10_device_of_match);
588cb03e
TT
1607
1608/*
1609 * The Arria10 EDAC Device Functions differ from the Cyclone5/Arria5
1610 * because 2 IRQs are shared among the all ECC peripherals. The ECC
1611 * manager manages the IRQs and the children.
1612 * Based on xgene_edac.c peripheral code.
1613 */
1614
c7b4be8d
TT
1615static ssize_t altr_edac_a10_device_trig(struct file *file,
1616 const char __user *user_buf,
1617 size_t count, loff_t *ppos)
1618{
1619 struct edac_device_ctl_info *edac_dci = file->private_data;
1620 struct altr_edac_device_dev *drvdata = edac_dci->pvt_info;
1621 const struct edac_device_prv_data *priv = drvdata->data;
1622 void __iomem *set_addr = (drvdata->base + priv->set_err_ofst);
1623 unsigned long flags;
1624 u8 trig_type;
1625
1626 if (!user_buf || get_user(trig_type, user_buf))
1627 return -EFAULT;
1628
1629 local_irq_save(flags);
1630 if (trig_type == ALTR_UE_TRIGGER_CHAR)
1631 writel(priv->ue_set_mask, set_addr);
1632 else
1633 writel(priv->ce_set_mask, set_addr);
1634 /* Ensure the interrupt test bits are set */
1635 wmb();
1636 local_irq_restore(flags);
1637
1638 return count;
1639}
1640
13ab8448 1641static void altr_edac_a10_irq_handler(struct irq_desc *desc)
588cb03e 1642{
13ab8448
TT
1643 int dberr, bit, sm_offset, irq_status;
1644 struct altr_arria10_edac *edac = irq_desc_get_handler_data(desc);
1645 struct irq_chip *chip = irq_desc_get_chip(desc);
1646 int irq = irq_desc_get_irq(desc);
1647
1648 dberr = (irq == edac->db_irq) ? 1 : 0;
1649 sm_offset = dberr ? A10_SYSMGR_ECC_INTSTAT_DERR_OFST :
1650 A10_SYSMGR_ECC_INTSTAT_SERR_OFST;
1651
1652 chained_irq_enter(chip, desc);
588cb03e
TT
1653
1654 regmap_read(edac->ecc_mgr_map, sm_offset, &irq_status);
1655
13ab8448
TT
1656 for_each_set_bit(bit, (unsigned long *)&irq_status, 32) {
1657 irq = irq_linear_revmap(edac->domain, dberr * 32 + bit);
1658 if (irq)
1659 generic_handle_irq(irq);
588cb03e
TT
1660 }
1661
13ab8448 1662 chained_irq_exit(chip, desc);
588cb03e
TT
1663}
1664
44ec9b30
TT
1665static int validate_parent_available(struct device_node *np)
1666{
1667 struct device_node *parent;
1668 int ret = 0;
1669
1670 /* Ensure parent device is enabled if parent node exists */
1671 parent = of_parse_phandle(np, "altr,ecc-parent", 0);
1672 if (parent && !of_device_is_available(parent))
1673 ret = -ENODEV;
1674
1675 of_node_put(parent);
1676 return ret;
1677}
1678
588cb03e
TT
1679static int altr_edac_a10_device_add(struct altr_arria10_edac *edac,
1680 struct device_node *np)
1681{
1682 struct edac_device_ctl_info *dci;
1683 struct altr_edac_device_dev *altdev;
1684 char *ecc_name = (char *)np->name;
1685 struct resource res;
1686 int edac_idx;
1687 int rc = 0;
1688 const struct edac_device_prv_data *prv;
1689 /* Get matching node and check for valid result */
1690 const struct of_device_id *pdev_id =
ab564cb5 1691 of_match_node(altr_edac_a10_device_of_match, np);
588cb03e
TT
1692 if (IS_ERR_OR_NULL(pdev_id))
1693 return -ENODEV;
1694
1695 /* Get driver specific data for this EDAC device */
1696 prv = pdev_id->data;
1697 if (IS_ERR_OR_NULL(prv))
1698 return -ENODEV;
1699
44ec9b30
TT
1700 if (validate_parent_available(np))
1701 return -ENODEV;
1702
588cb03e
TT
1703 if (!devres_open_group(edac->dev, altr_edac_a10_device_add, GFP_KERNEL))
1704 return -ENOMEM;
1705
1706 rc = of_address_to_resource(np, 0, &res);
1707 if (rc < 0) {
1708 edac_printk(KERN_ERR, EDAC_DEVICE,
1709 "%s: no resource address\n", ecc_name);
1710 goto err_release_group;
1711 }
1712
1713 edac_idx = edac_device_alloc_index();
1714 dci = edac_device_alloc_ctl_info(sizeof(*altdev), ecc_name,
1715 1, ecc_name, 1, 0, NULL, 0,
1716 edac_idx);
1717
1718 if (!dci) {
1719 edac_printk(KERN_ERR, EDAC_DEVICE,
1720 "%s: Unable to allocate EDAC device\n", ecc_name);
1721 rc = -ENOMEM;
1722 goto err_release_group;
1723 }
1724
1725 altdev = dci->pvt_info;
1726 dci->dev = edac->dev;
1727 altdev->edac_dev_name = ecc_name;
1728 altdev->edac_idx = edac_idx;
1729 altdev->edac = edac;
1730 altdev->edac_dev = dci;
1731 altdev->data = prv;
1732 altdev->ddev = *edac->dev;
1733 dci->dev = &altdev->ddev;
1734 dci->ctl_name = "Altera ECC Manager";
1735 dci->mod_name = ecc_name;
1736 dci->dev_name = ecc_name;
1737
1738 altdev->base = devm_ioremap_resource(edac->dev, &res);
1739 if (IS_ERR(altdev->base)) {
1740 rc = PTR_ERR(altdev->base);
1741 goto err_release_group1;
1742 }
1743
1744 /* Check specific dependencies for the module */
1745 if (altdev->data->setup) {
1746 rc = altdev->data->setup(altdev);
1747 if (rc)
1748 goto err_release_group1;
1749 }
1750
13ab8448
TT
1751 altdev->sb_irq = irq_of_parse_and_map(np, 0);
1752 if (!altdev->sb_irq) {
1753 edac_printk(KERN_ERR, EDAC_DEVICE, "Error allocating SBIRQ\n");
1754 rc = -ENODEV;
1755 goto err_release_group1;
1756 }
a29d64a4
TT
1757 rc = devm_request_irq(edac->dev, altdev->sb_irq, prv->ecc_irq_handler,
1758 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
1759 ecc_name, altdev);
13ab8448 1760 if (rc) {
3763569f 1761 edac_printk(KERN_ERR, EDAC_DEVICE, "No SBERR IRQ resource\n");
13ab8448
TT
1762 goto err_release_group1;
1763 }
1764
1765 altdev->db_irq = irq_of_parse_and_map(np, 1);
1766 if (!altdev->db_irq) {
1767 edac_printk(KERN_ERR, EDAC_DEVICE, "Error allocating DBIRQ\n");
1768 rc = -ENODEV;
1769 goto err_release_group1;
1770 }
a29d64a4
TT
1771 rc = devm_request_irq(edac->dev, altdev->db_irq, prv->ecc_irq_handler,
1772 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
1773 ecc_name, altdev);
13ab8448
TT
1774 if (rc) {
1775 edac_printk(KERN_ERR, EDAC_DEVICE, "No DBERR IRQ resource\n");
1776 goto err_release_group1;
1777 }
1778
588cb03e
TT
1779 rc = edac_device_add_device(dci);
1780 if (rc) {
1781 dev_err(edac->dev, "edac_device_add_device failed\n");
1782 rc = -ENOMEM;
1783 goto err_release_group1;
1784 }
1785
1786 altr_create_edacdev_dbgfs(dci, prv);
1787
1788 list_add(&altdev->next, &edac->a10_ecc_devices);
1789
1790 devres_remove_group(edac->dev, altr_edac_a10_device_add);
1791
1792 return 0;
1793
1794err_release_group1:
1795 edac_device_free_ctl_info(dci);
1796err_release_group:
588cb03e
TT
1797 devres_release_group(edac->dev, NULL);
1798 edac_printk(KERN_ERR, EDAC_DEVICE,
1799 "%s:Error setting up EDAC device: %d\n", ecc_name, rc);
1800
1801 return rc;
1802}
1803
13ab8448
TT
1804static void a10_eccmgr_irq_mask(struct irq_data *d)
1805{
1806 struct altr_arria10_edac *edac = irq_data_get_irq_chip_data(d);
1807
1808 regmap_write(edac->ecc_mgr_map, A10_SYSMGR_ECC_INTMASK_SET_OFST,
1809 BIT(d->hwirq));
1810}
1811
1812static void a10_eccmgr_irq_unmask(struct irq_data *d)
1813{
1814 struct altr_arria10_edac *edac = irq_data_get_irq_chip_data(d);
1815
1816 regmap_write(edac->ecc_mgr_map, A10_SYSMGR_ECC_INTMASK_CLR_OFST,
1817 BIT(d->hwirq));
1818}
1819
1820static int a10_eccmgr_irqdomain_map(struct irq_domain *d, unsigned int irq,
1821 irq_hw_number_t hwirq)
1822{
1823 struct altr_arria10_edac *edac = d->host_data;
1824
1825 irq_set_chip_and_handler(irq, &edac->irq_chip, handle_simple_irq);
1826 irq_set_chip_data(irq, edac);
1827 irq_set_noprobe(irq);
1828
1829 return 0;
1830}
1831
18caec20 1832static const struct irq_domain_ops a10_eccmgr_ic_ops = {
13ab8448
TT
1833 .map = a10_eccmgr_irqdomain_map,
1834 .xlate = irq_domain_xlate_twocell,
1835};
1836
588cb03e
TT
1837static int altr_edac_a10_probe(struct platform_device *pdev)
1838{
1839 struct altr_arria10_edac *edac;
1840 struct device_node *child;
588cb03e
TT
1841
1842 edac = devm_kzalloc(&pdev->dev, sizeof(*edac), GFP_KERNEL);
1843 if (!edac)
1844 return -ENOMEM;
1845
1846 edac->dev = &pdev->dev;
1847 platform_set_drvdata(pdev, edac);
1848 INIT_LIST_HEAD(&edac->a10_ecc_devices);
1849
1850 edac->ecc_mgr_map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
1851 "altr,sysmgr-syscon");
1852 if (IS_ERR(edac->ecc_mgr_map)) {
1853 edac_printk(KERN_ERR, EDAC_DEVICE,
1854 "Unable to get syscon altr,sysmgr-syscon\n");
1855 return PTR_ERR(edac->ecc_mgr_map);
1856 }
1857
13ab8448
TT
1858 edac->irq_chip.name = pdev->dev.of_node->name;
1859 edac->irq_chip.irq_mask = a10_eccmgr_irq_mask;
1860 edac->irq_chip.irq_unmask = a10_eccmgr_irq_unmask;
1861 edac->domain = irq_domain_add_linear(pdev->dev.of_node, 64,
1862 &a10_eccmgr_ic_ops, edac);
1863 if (!edac->domain) {
1864 dev_err(&pdev->dev, "Error adding IRQ domain\n");
1865 return -ENOMEM;
1866 }
1867
588cb03e 1868 edac->sb_irq = platform_get_irq(pdev, 0);
13ab8448
TT
1869 if (edac->sb_irq < 0) {
1870 dev_err(&pdev->dev, "No SBERR IRQ resource\n");
1871 return edac->sb_irq;
588cb03e
TT
1872 }
1873
13ab8448
TT
1874 irq_set_chained_handler_and_data(edac->sb_irq,
1875 altr_edac_a10_irq_handler,
1876 edac);
1877
588cb03e 1878 edac->db_irq = platform_get_irq(pdev, 1);
13ab8448
TT
1879 if (edac->db_irq < 0) {
1880 dev_err(&pdev->dev, "No DBERR IRQ resource\n");
1881 return edac->db_irq;
588cb03e 1882 }
13ab8448
TT
1883 irq_set_chained_handler_and_data(edac->db_irq,
1884 altr_edac_a10_irq_handler,
1885 edac);
588cb03e
TT
1886
1887 for_each_child_of_node(pdev->dev.of_node, child) {
1888 if (!of_device_is_available(child))
1889 continue;
c6882fb2
TT
1890
1891 if (of_device_is_compatible(child, "altr,socfpga-a10-l2-ecc") ||
1892 of_device_is_compatible(child, "altr,socfpga-a10-ocram-ecc") ||
1893 of_device_is_compatible(child, "altr,socfpga-eth-mac-ecc") ||
e8263793 1894 of_device_is_compatible(child, "altr,socfpga-nand-ecc") ||
c609581d 1895 of_device_is_compatible(child, "altr,socfpga-dma-ecc") ||
485fe9e2 1896 of_device_is_compatible(child, "altr,socfpga-usb-ecc") ||
91104984
TT
1897 of_device_is_compatible(child, "altr,socfpga-qspi-ecc") ||
1898 of_device_is_compatible(child, "altr,socfpga-sdmmc-ecc"))
c6882fb2 1899
c7b4be8d 1900 altr_edac_a10_device_add(edac, child);
c6882fb2
TT
1901
1902 else if (of_device_is_compatible(child, "altr,sdram-edac-a10"))
ab564cb5
TT
1903 of_platform_populate(pdev->dev.of_node,
1904 altr_sdram_ctrl_of_match,
1905 NULL, &pdev->dev);
588cb03e
TT
1906 }
1907
1908 return 0;
1909}
1910
1911static const struct of_device_id altr_edac_a10_of_match[] = {
1912 { .compatible = "altr,socfpga-a10-ecc-manager" },
1913 {},
1914};
1915MODULE_DEVICE_TABLE(of, altr_edac_a10_of_match);
1916
1917static struct platform_driver altr_edac_a10_driver = {
1918 .probe = altr_edac_a10_probe,
1919 .driver = {
1920 .name = "socfpga_a10_ecc_manager",
1921 .of_match_table = altr_edac_a10_of_match,
1922 },
1923};
1924module_platform_driver(altr_edac_a10_driver);
1925
71bcada8
TT
1926MODULE_LICENSE("GPL v2");
1927MODULE_AUTHOR("Thor Thayer");
c3eea194 1928MODULE_DESCRIPTION("EDAC Driver for Altera Memories");