blob: e20a045cbf209b3c73121aa53ca55a216515537e [file] [log] [blame]
Thor Thayer71bcada2014-09-03 10:27:54 -05001/*
Thor Thayerc3eea192016-02-10 13:26:21 -06002 * Copyright Altera Corporation (C) 2014-2016. All rights reserved.
Thor Thayer71bcada2014-09-03 10:27:54 -05003 * 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
Thor Thayerc3eea192016-02-10 13:26:21 -060020#include <asm/cacheflush.h>
Thor Thayer71bcada2014-09-03 10:27:54 -050021#include <linux/ctype.h>
22#include <linux/edac.h>
Thor Thayerc3eea192016-02-10 13:26:21 -060023#include <linux/genalloc.h>
Thor Thayer71bcada2014-09-03 10:27:54 -050024#include <linux/interrupt.h>
25#include <linux/kernel.h>
26#include <linux/mfd/syscon.h>
27#include <linux/of_platform.h>
28#include <linux/platform_device.h>
29#include <linux/regmap.h>
30#include <linux/types.h>
31#include <linux/uaccess.h>
32
Thor Thayer143f4a52015-06-04 09:28:46 -050033#include "altera_edac.h"
Thor Thayer71bcada2014-09-03 10:27:54 -050034#include "edac_core.h"
35#include "edac_module.h"
36
37#define EDAC_MOD_STR "altera_edac"
38#define EDAC_VERSION "1"
Thor Thayerc3eea192016-02-10 13:26:21 -060039#define EDAC_DEVICE "Altera"
Thor Thayer71bcada2014-09-03 10:27:54 -050040
Thor Thayer143f4a52015-06-04 09:28:46 -050041static const struct altr_sdram_prv_data c5_data = {
42 .ecc_ctrl_offset = CV_CTLCFG_OFST,
43 .ecc_ctl_en_mask = CV_CTLCFG_ECC_AUTO_EN,
44 .ecc_stat_offset = CV_DRAMSTS_OFST,
45 .ecc_stat_ce_mask = CV_DRAMSTS_SBEERR,
46 .ecc_stat_ue_mask = CV_DRAMSTS_DBEERR,
47 .ecc_saddr_offset = CV_ERRADDR_OFST,
Thor Thayer73bcc942015-06-04 09:28:47 -050048 .ecc_daddr_offset = CV_ERRADDR_OFST,
Thor Thayer143f4a52015-06-04 09:28:46 -050049 .ecc_cecnt_offset = CV_SBECOUNT_OFST,
50 .ecc_uecnt_offset = CV_DBECOUNT_OFST,
51 .ecc_irq_en_offset = CV_DRAMINTR_OFST,
52 .ecc_irq_en_mask = CV_DRAMINTR_INTREN,
53 .ecc_irq_clr_offset = CV_DRAMINTR_OFST,
54 .ecc_irq_clr_mask = (CV_DRAMINTR_INTRCLR | CV_DRAMINTR_INTREN),
55 .ecc_cnt_rst_offset = CV_DRAMINTR_OFST,
56 .ecc_cnt_rst_mask = CV_DRAMINTR_INTRCLR,
Thor Thayer143f4a52015-06-04 09:28:46 -050057 .ce_ue_trgr_offset = CV_CTLCFG_OFST,
58 .ce_set_mask = CV_CTLCFG_GEN_SB_ERR,
59 .ue_set_mask = CV_CTLCFG_GEN_DB_ERR,
Thor Thayer71bcada2014-09-03 10:27:54 -050060};
61
Thor Thayer73bcc942015-06-04 09:28:47 -050062static const struct altr_sdram_prv_data a10_data = {
63 .ecc_ctrl_offset = A10_ECCCTRL1_OFST,
64 .ecc_ctl_en_mask = A10_ECCCTRL1_ECC_EN,
65 .ecc_stat_offset = A10_INTSTAT_OFST,
66 .ecc_stat_ce_mask = A10_INTSTAT_SBEERR,
67 .ecc_stat_ue_mask = A10_INTSTAT_DBEERR,
68 .ecc_saddr_offset = A10_SERRADDR_OFST,
69 .ecc_daddr_offset = A10_DERRADDR_OFST,
70 .ecc_irq_en_offset = A10_ERRINTEN_OFST,
71 .ecc_irq_en_mask = A10_ECC_IRQ_EN_MASK,
72 .ecc_irq_clr_offset = A10_INTSTAT_OFST,
73 .ecc_irq_clr_mask = (A10_INTSTAT_SBEERR | A10_INTSTAT_DBEERR),
74 .ecc_cnt_rst_offset = A10_ECCCTRL1_OFST,
75 .ecc_cnt_rst_mask = A10_ECC_CNT_RESET_MASK,
Thor Thayer73bcc942015-06-04 09:28:47 -050076 .ce_ue_trgr_offset = A10_DIAGINTTEST_OFST,
77 .ce_set_mask = A10_DIAGINT_TSERRA_MASK,
78 .ue_set_mask = A10_DIAGINT_TDERRA_MASK,
Thor Thayer73bcc942015-06-04 09:28:47 -050079};
80
Thor Thayerc3eea192016-02-10 13:26:21 -060081/*********************** EDAC Memory Controller Functions ****************/
82
83/* The SDRAM controller uses the EDAC Memory Controller framework. */
84
Thor Thayer71bcada2014-09-03 10:27:54 -050085static irqreturn_t altr_sdram_mc_err_handler(int irq, void *dev_id)
86{
87 struct mem_ctl_info *mci = dev_id;
88 struct altr_sdram_mc_data *drvdata = mci->pvt_info;
Thor Thayer143f4a52015-06-04 09:28:46 -050089 const struct altr_sdram_prv_data *priv = drvdata->data;
Thor Thayer73bcc942015-06-04 09:28:47 -050090 u32 status, err_count = 1, err_addr;
Thor Thayer71bcada2014-09-03 10:27:54 -050091
Thor Thayer143f4a52015-06-04 09:28:46 -050092 regmap_read(drvdata->mc_vbase, priv->ecc_stat_offset, &status);
Thor Thayer71bcada2014-09-03 10:27:54 -050093
Thor Thayer143f4a52015-06-04 09:28:46 -050094 if (status & priv->ecc_stat_ue_mask) {
Thor Thayer73bcc942015-06-04 09:28:47 -050095 regmap_read(drvdata->mc_vbase, priv->ecc_daddr_offset,
96 &err_addr);
97 if (priv->ecc_uecnt_offset)
98 regmap_read(drvdata->mc_vbase, priv->ecc_uecnt_offset,
99 &err_count);
Thor Thayer71bcada2014-09-03 10:27:54 -0500100 panic("\nEDAC: [%d Uncorrectable errors @ 0x%08X]\n",
101 err_count, err_addr);
102 }
Thor Thayer143f4a52015-06-04 09:28:46 -0500103 if (status & priv->ecc_stat_ce_mask) {
Thor Thayer73bcc942015-06-04 09:28:47 -0500104 regmap_read(drvdata->mc_vbase, priv->ecc_saddr_offset,
105 &err_addr);
106 if (priv->ecc_uecnt_offset)
107 regmap_read(drvdata->mc_vbase, priv->ecc_cecnt_offset,
108 &err_count);
Thor Thayer71bcada2014-09-03 10:27:54 -0500109 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, err_count,
110 err_addr >> PAGE_SHIFT,
111 err_addr & ~PAGE_MASK, 0,
112 0, 0, -1, mci->ctl_name, "");
Thor Thayer73bcc942015-06-04 09:28:47 -0500113 /* Clear IRQ to resume */
114 regmap_write(drvdata->mc_vbase, priv->ecc_irq_clr_offset,
115 priv->ecc_irq_clr_mask);
116
117 return IRQ_HANDLED;
Thor Thayer71bcada2014-09-03 10:27:54 -0500118 }
Thor Thayer73bcc942015-06-04 09:28:47 -0500119 return IRQ_NONE;
Thor Thayer71bcada2014-09-03 10:27:54 -0500120}
121
Thor Thayer71bcada2014-09-03 10:27:54 -0500122static ssize_t altr_sdr_mc_err_inject_write(struct file *file,
123 const char __user *data,
124 size_t count, loff_t *ppos)
125{
126 struct mem_ctl_info *mci = file->private_data;
127 struct altr_sdram_mc_data *drvdata = mci->pvt_info;
Thor Thayer143f4a52015-06-04 09:28:46 -0500128 const struct altr_sdram_prv_data *priv = drvdata->data;
Thor Thayer71bcada2014-09-03 10:27:54 -0500129 u32 *ptemp;
130 dma_addr_t dma_handle;
131 u32 reg, read_reg;
132
133 ptemp = dma_alloc_coherent(mci->pdev, 16, &dma_handle, GFP_KERNEL);
134 if (!ptemp) {
135 dma_free_coherent(mci->pdev, 16, ptemp, dma_handle);
136 edac_printk(KERN_ERR, EDAC_MC,
137 "Inject: Buffer Allocation error\n");
138 return -ENOMEM;
139 }
140
Thor Thayer143f4a52015-06-04 09:28:46 -0500141 regmap_read(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
142 &read_reg);
143 read_reg &= ~(priv->ce_set_mask | priv->ue_set_mask);
Thor Thayer71bcada2014-09-03 10:27:54 -0500144
145 /* Error are injected by writing a word while the SBE or DBE
146 * bit in the CTLCFG register is set. Reading the word will
147 * trigger the SBE or DBE error and the corresponding IRQ.
148 */
149 if (count == 3) {
150 edac_printk(KERN_ALERT, EDAC_MC,
151 "Inject Double bit error\n");
Thor Thayer143f4a52015-06-04 09:28:46 -0500152 regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
153 (read_reg | priv->ue_set_mask));
Thor Thayer71bcada2014-09-03 10:27:54 -0500154 } else {
155 edac_printk(KERN_ALERT, EDAC_MC,
156 "Inject Single bit error\n");
Thor Thayer143f4a52015-06-04 09:28:46 -0500157 regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
158 (read_reg | priv->ce_set_mask));
Thor Thayer71bcada2014-09-03 10:27:54 -0500159 }
160
161 ptemp[0] = 0x5A5A5A5A;
162 ptemp[1] = 0xA5A5A5A5;
163
164 /* Clear the error injection bits */
Thor Thayer143f4a52015-06-04 09:28:46 -0500165 regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset, read_reg);
Thor Thayer71bcada2014-09-03 10:27:54 -0500166 /* Ensure it has been written out */
167 wmb();
168
169 /*
170 * To trigger the error, we need to read the data back
171 * (the data was written with errors above).
172 * The ACCESS_ONCE macros and printk are used to prevent the
173 * the compiler optimizing these reads out.
174 */
175 reg = ACCESS_ONCE(ptemp[0]);
176 read_reg = ACCESS_ONCE(ptemp[1]);
177 /* Force Read */
178 rmb();
179
180 edac_printk(KERN_ALERT, EDAC_MC, "Read Data [0x%X, 0x%X]\n",
181 reg, read_reg);
182
183 dma_free_coherent(mci->pdev, 16, ptemp, dma_handle);
184
185 return count;
186}
187
188static const struct file_operations altr_sdr_mc_debug_inject_fops = {
189 .open = simple_open,
190 .write = altr_sdr_mc_err_inject_write,
191 .llseek = generic_file_llseek,
192};
193
194static void altr_sdr_mc_create_debugfs_nodes(struct mem_ctl_info *mci)
195{
Borislav Petkovbba3b312015-09-22 12:27:29 +0200196 if (!IS_ENABLED(CONFIG_EDAC_DEBUG))
197 return;
198
199 if (!mci->debugfs)
200 return;
201
202 edac_debugfs_create_file("inject_ctrl", S_IWUSR, mci->debugfs, mci,
203 &altr_sdr_mc_debug_inject_fops);
Thor Thayer71bcada2014-09-03 10:27:54 -0500204}
Thor Thayer71bcada2014-09-03 10:27:54 -0500205
Thor Thayerf9ae4872015-06-04 09:28:45 -0500206/* Get total memory size from Open Firmware DTB */
207static unsigned long get_total_mem(void)
Thor Thayer71bcada2014-09-03 10:27:54 -0500208{
Thor Thayerf9ae4872015-06-04 09:28:45 -0500209 struct device_node *np = NULL;
210 const unsigned int *reg, *reg_end;
211 int len, sw, aw;
212 unsigned long start, size, total_mem = 0;
Thor Thayer71bcada2014-09-03 10:27:54 -0500213
Thor Thayerf9ae4872015-06-04 09:28:45 -0500214 for_each_node_by_type(np, "memory") {
215 aw = of_n_addr_cells(np);
216 sw = of_n_size_cells(np);
217 reg = (const unsigned int *)of_get_property(np, "reg", &len);
218 reg_end = reg + (len / sizeof(u32));
Thor Thayer71bcada2014-09-03 10:27:54 -0500219
Thor Thayerf9ae4872015-06-04 09:28:45 -0500220 total_mem = 0;
221 do {
222 start = of_read_number(reg, aw);
223 reg += aw;
224 size = of_read_number(reg, sw);
225 reg += sw;
226 total_mem += size;
227 } while (reg < reg_end);
228 }
229 edac_dbg(0, "total_mem 0x%lx\n", total_mem);
230 return total_mem;
Thor Thayer71bcada2014-09-03 10:27:54 -0500231}
232
Thor Thayer143f4a52015-06-04 09:28:46 -0500233static const struct of_device_id altr_sdram_ctrl_of_match[] = {
234 { .compatible = "altr,sdram-edac", .data = (void *)&c5_data},
Thor Thayer73bcc942015-06-04 09:28:47 -0500235 { .compatible = "altr,sdram-edac-a10", .data = (void *)&a10_data},
Thor Thayer143f4a52015-06-04 09:28:46 -0500236 {},
237};
238MODULE_DEVICE_TABLE(of, altr_sdram_ctrl_of_match);
239
Thor Thayer73bcc942015-06-04 09:28:47 -0500240static int a10_init(struct regmap *mc_vbase)
241{
242 if (regmap_update_bits(mc_vbase, A10_INTMODE_OFST,
243 A10_INTMODE_SB_INT, A10_INTMODE_SB_INT)) {
244 edac_printk(KERN_ERR, EDAC_MC,
245 "Error setting SB IRQ mode\n");
246 return -ENODEV;
247 }
248
249 if (regmap_write(mc_vbase, A10_SERRCNTREG_OFST, 1)) {
250 edac_printk(KERN_ERR, EDAC_MC,
251 "Error setting trigger count\n");
252 return -ENODEV;
253 }
254
255 return 0;
256}
257
258static int a10_unmask_irq(struct platform_device *pdev, u32 mask)
259{
260 void __iomem *sm_base;
261 int ret = 0;
262
263 if (!request_mem_region(A10_SYMAN_INTMASK_CLR, sizeof(u32),
264 dev_name(&pdev->dev))) {
265 edac_printk(KERN_ERR, EDAC_MC,
266 "Unable to request mem region\n");
267 return -EBUSY;
268 }
269
270 sm_base = ioremap(A10_SYMAN_INTMASK_CLR, sizeof(u32));
271 if (!sm_base) {
272 edac_printk(KERN_ERR, EDAC_MC,
273 "Unable to ioremap device\n");
274
275 ret = -ENOMEM;
276 goto release;
277 }
278
279 iowrite32(mask, sm_base);
280
281 iounmap(sm_base);
282
283release:
284 release_mem_region(A10_SYMAN_INTMASK_CLR, sizeof(u32));
285
286 return ret;
287}
288
Thor Thayer71bcada2014-09-03 10:27:54 -0500289static int altr_sdram_probe(struct platform_device *pdev)
290{
Thor Thayer143f4a52015-06-04 09:28:46 -0500291 const struct of_device_id *id;
Thor Thayer71bcada2014-09-03 10:27:54 -0500292 struct edac_mc_layer layers[2];
293 struct mem_ctl_info *mci;
294 struct altr_sdram_mc_data *drvdata;
Thor Thayer143f4a52015-06-04 09:28:46 -0500295 const struct altr_sdram_prv_data *priv;
Thor Thayer71bcada2014-09-03 10:27:54 -0500296 struct regmap *mc_vbase;
297 struct dimm_info *dimm;
Thor Thayer143f4a52015-06-04 09:28:46 -0500298 u32 read_reg;
Thor Thayer73bcc942015-06-04 09:28:47 -0500299 int irq, irq2, res = 0;
300 unsigned long mem_size, irqflags = 0;
Thor Thayer71bcada2014-09-03 10:27:54 -0500301
Thor Thayer143f4a52015-06-04 09:28:46 -0500302 id = of_match_device(altr_sdram_ctrl_of_match, &pdev->dev);
303 if (!id)
304 return -ENODEV;
305
Thor Thayer71bcada2014-09-03 10:27:54 -0500306 /* Grab the register range from the sdr controller in device tree */
307 mc_vbase = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
308 "altr,sdr-syscon");
309 if (IS_ERR(mc_vbase)) {
310 edac_printk(KERN_ERR, EDAC_MC,
311 "regmap for altr,sdr-syscon lookup failed.\n");
312 return -ENODEV;
313 }
314
Thor Thayer143f4a52015-06-04 09:28:46 -0500315 /* Check specific dependencies for the module */
316 priv = of_match_node(altr_sdram_ctrl_of_match,
317 pdev->dev.of_node)->data;
318
319 /* Validate the SDRAM controller has ECC enabled */
320 if (regmap_read(mc_vbase, priv->ecc_ctrl_offset, &read_reg) ||
321 ((read_reg & priv->ecc_ctl_en_mask) != priv->ecc_ctl_en_mask)) {
Thor Thayer71bcada2014-09-03 10:27:54 -0500322 edac_printk(KERN_ERR, EDAC_MC,
323 "No ECC/ECC disabled [0x%08X]\n", read_reg);
324 return -ENODEV;
325 }
326
327 /* Grab memory size from device tree. */
Thor Thayerf9ae4872015-06-04 09:28:45 -0500328 mem_size = get_total_mem();
Thor Thayer71bcada2014-09-03 10:27:54 -0500329 if (!mem_size) {
Thor Thayerf9ae4872015-06-04 09:28:45 -0500330 edac_printk(KERN_ERR, EDAC_MC, "Unable to calculate memory size\n");
Thor Thayer71bcada2014-09-03 10:27:54 -0500331 return -ENODEV;
332 }
333
Thor Thayer143f4a52015-06-04 09:28:46 -0500334 /* Ensure the SDRAM Interrupt is disabled */
335 if (regmap_update_bits(mc_vbase, priv->ecc_irq_en_offset,
336 priv->ecc_irq_en_mask, 0)) {
Thor Thayer71bcada2014-09-03 10:27:54 -0500337 edac_printk(KERN_ERR, EDAC_MC,
Thor Thayer143f4a52015-06-04 09:28:46 -0500338 "Error disabling SDRAM ECC IRQ\n");
339 return -ENODEV;
340 }
341
342 /* Toggle to clear the SDRAM Error count */
343 if (regmap_update_bits(mc_vbase, priv->ecc_cnt_rst_offset,
344 priv->ecc_cnt_rst_mask,
345 priv->ecc_cnt_rst_mask)) {
346 edac_printk(KERN_ERR, EDAC_MC,
347 "Error clearing SDRAM ECC count\n");
348 return -ENODEV;
349 }
350
351 if (regmap_update_bits(mc_vbase, priv->ecc_cnt_rst_offset,
352 priv->ecc_cnt_rst_mask, 0)) {
353 edac_printk(KERN_ERR, EDAC_MC,
354 "Error clearing SDRAM ECC count\n");
Thor Thayer71bcada2014-09-03 10:27:54 -0500355 return -ENODEV;
356 }
357
358 irq = platform_get_irq(pdev, 0);
359 if (irq < 0) {
360 edac_printk(KERN_ERR, EDAC_MC,
361 "No irq %d in DT\n", irq);
362 return -ENODEV;
363 }
364
Thor Thayer73bcc942015-06-04 09:28:47 -0500365 /* Arria10 has a 2nd IRQ */
366 irq2 = platform_get_irq(pdev, 1);
367
Thor Thayer71bcada2014-09-03 10:27:54 -0500368 layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
369 layers[0].size = 1;
370 layers[0].is_virt_csrow = true;
371 layers[1].type = EDAC_MC_LAYER_CHANNEL;
372 layers[1].size = 1;
373 layers[1].is_virt_csrow = false;
374 mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers,
375 sizeof(struct altr_sdram_mc_data));
376 if (!mci)
377 return -ENOMEM;
378
379 mci->pdev = &pdev->dev;
380 drvdata = mci->pvt_info;
381 drvdata->mc_vbase = mc_vbase;
Thor Thayer143f4a52015-06-04 09:28:46 -0500382 drvdata->data = priv;
Thor Thayer71bcada2014-09-03 10:27:54 -0500383 platform_set_drvdata(pdev, mci);
384
385 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
Thor Thayer143f4a52015-06-04 09:28:46 -0500386 edac_printk(KERN_ERR, EDAC_MC,
387 "Unable to get managed device resource\n");
Thor Thayer71bcada2014-09-03 10:27:54 -0500388 res = -ENOMEM;
389 goto free;
390 }
391
392 mci->mtype_cap = MEM_FLAG_DDR3;
393 mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_SECDED;
394 mci->edac_cap = EDAC_FLAG_SECDED;
395 mci->mod_name = EDAC_MOD_STR;
396 mci->mod_ver = EDAC_VERSION;
397 mci->ctl_name = dev_name(&pdev->dev);
398 mci->scrub_mode = SCRUB_SW_SRC;
399 mci->dev_name = dev_name(&pdev->dev);
400
401 dimm = *mci->dimms;
402 dimm->nr_pages = ((mem_size - 1) >> PAGE_SHIFT) + 1;
403 dimm->grain = 8;
404 dimm->dtype = DEV_X8;
405 dimm->mtype = MEM_DDR3;
406 dimm->edac_mode = EDAC_SECDED;
407
408 res = edac_mc_add_mc(mci);
409 if (res < 0)
410 goto err;
411
Thor Thayer73bcc942015-06-04 09:28:47 -0500412 /* Only the Arria10 has separate IRQs */
413 if (irq2 > 0) {
414 /* Arria10 specific initialization */
415 res = a10_init(mc_vbase);
416 if (res < 0)
417 goto err2;
418
419 res = devm_request_irq(&pdev->dev, irq2,
420 altr_sdram_mc_err_handler,
421 IRQF_SHARED, dev_name(&pdev->dev), mci);
422 if (res < 0) {
423 edac_mc_printk(mci, KERN_ERR,
424 "Unable to request irq %d\n", irq2);
425 res = -ENODEV;
426 goto err2;
427 }
428
429 res = a10_unmask_irq(pdev, A10_DDR0_IRQ_MASK);
430 if (res < 0)
431 goto err2;
432
433 irqflags = IRQF_SHARED;
434 }
435
Thor Thayer71bcada2014-09-03 10:27:54 -0500436 res = devm_request_irq(&pdev->dev, irq, altr_sdram_mc_err_handler,
Thor Thayer73bcc942015-06-04 09:28:47 -0500437 irqflags, dev_name(&pdev->dev), mci);
Thor Thayer71bcada2014-09-03 10:27:54 -0500438 if (res < 0) {
439 edac_mc_printk(mci, KERN_ERR,
440 "Unable to request irq %d\n", irq);
441 res = -ENODEV;
442 goto err2;
443 }
444
Thor Thayer143f4a52015-06-04 09:28:46 -0500445 /* Infrastructure ready - enable the IRQ */
446 if (regmap_update_bits(drvdata->mc_vbase, priv->ecc_irq_en_offset,
447 priv->ecc_irq_en_mask, priv->ecc_irq_en_mask)) {
Thor Thayer71bcada2014-09-03 10:27:54 -0500448 edac_mc_printk(mci, KERN_ERR,
449 "Error enabling SDRAM ECC IRQ\n");
450 res = -ENODEV;
451 goto err2;
452 }
453
454 altr_sdr_mc_create_debugfs_nodes(mci);
455
456 devres_close_group(&pdev->dev, NULL);
457
458 return 0;
459
460err2:
461 edac_mc_del_mc(&pdev->dev);
462err:
463 devres_release_group(&pdev->dev, NULL);
464free:
465 edac_mc_free(mci);
466 edac_printk(KERN_ERR, EDAC_MC,
467 "EDAC Probe Failed; Error %d\n", res);
468
469 return res;
470}
471
472static int altr_sdram_remove(struct platform_device *pdev)
473{
474 struct mem_ctl_info *mci = platform_get_drvdata(pdev);
475
476 edac_mc_del_mc(&pdev->dev);
477 edac_mc_free(mci);
478 platform_set_drvdata(pdev, NULL);
479
480 return 0;
481}
482
Alan Tull6f2b6422015-06-05 08:49:15 -0500483/*
484 * If you want to suspend, need to disable EDAC by removing it
485 * from the device tree or defconfig.
486 */
487#ifdef CONFIG_PM
488static int altr_sdram_prepare(struct device *dev)
489{
490 pr_err("Suspend not allowed when EDAC is enabled.\n");
491
492 return -EPERM;
493}
494
495static const struct dev_pm_ops altr_sdram_pm_ops = {
496 .prepare = altr_sdram_prepare,
497};
498#endif
499
Thor Thayer71bcada2014-09-03 10:27:54 -0500500static struct platform_driver altr_sdram_edac_driver = {
501 .probe = altr_sdram_probe,
502 .remove = altr_sdram_remove,
503 .driver = {
504 .name = "altr_sdram_edac",
Alan Tull6f2b6422015-06-05 08:49:15 -0500505#ifdef CONFIG_PM
506 .pm = &altr_sdram_pm_ops,
507#endif
Thor Thayer71bcada2014-09-03 10:27:54 -0500508 .of_match_table = altr_sdram_ctrl_of_match,
509 },
510};
511
512module_platform_driver(altr_sdram_edac_driver);
513
Thor Thayerc3eea192016-02-10 13:26:21 -0600514/************************* EDAC Parent Probe *************************/
515
516static const struct of_device_id altr_edac_device_of_match[];
517
518static const struct of_device_id altr_edac_of_match[] = {
519 { .compatible = "altr,socfpga-ecc-manager" },
520 {},
521};
522MODULE_DEVICE_TABLE(of, altr_edac_of_match);
523
524static int altr_edac_probe(struct platform_device *pdev)
525{
526 of_platform_populate(pdev->dev.of_node, altr_edac_device_of_match,
527 NULL, &pdev->dev);
528 return 0;
529}
530
531static struct platform_driver altr_edac_driver = {
532 .probe = altr_edac_probe,
533 .driver = {
534 .name = "socfpga_ecc_manager",
535 .of_match_table = altr_edac_of_match,
536 },
537};
538module_platform_driver(altr_edac_driver);
539
540/************************* EDAC Device Functions *************************/
541
542/*
543 * EDAC Device Functions (shared between various IPs).
544 * The discrete memories use the EDAC Device framework. The probe
545 * and error handling functions are very similar between memories
546 * so they are shared. The memory allocation and freeing for EDAC
547 * trigger testing are different for each memory.
548 */
549
550const struct edac_device_prv_data ocramecc_data;
551const struct edac_device_prv_data l2ecc_data;
552
Thor Thayerc3eea192016-02-10 13:26:21 -0600553static irqreturn_t altr_edac_device_handler(int irq, void *dev_id)
554{
555 irqreturn_t ret_value = IRQ_NONE;
556 struct edac_device_ctl_info *dci = dev_id;
557 struct altr_edac_device_dev *drvdata = dci->pvt_info;
558 const struct edac_device_prv_data *priv = drvdata->data;
559
560 if (irq == drvdata->sb_irq) {
561 if (priv->ce_clear_mask)
562 writel(priv->ce_clear_mask, drvdata->base);
563 edac_device_handle_ce(dci, 0, 0, drvdata->edac_dev_name);
564 ret_value = IRQ_HANDLED;
565 } else if (irq == drvdata->db_irq) {
566 if (priv->ue_clear_mask)
567 writel(priv->ue_clear_mask, drvdata->base);
568 edac_device_handle_ue(dci, 0, 0, drvdata->edac_dev_name);
569 panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
570 ret_value = IRQ_HANDLED;
571 } else {
572 WARN_ON(1);
573 }
574
575 return ret_value;
576}
577
578static ssize_t altr_edac_device_trig(struct file *file,
579 const char __user *user_buf,
580 size_t count, loff_t *ppos)
581
582{
583 u32 *ptemp, i, error_mask;
584 int result = 0;
585 u8 trig_type;
586 unsigned long flags;
587 struct edac_device_ctl_info *edac_dci = file->private_data;
588 struct altr_edac_device_dev *drvdata = edac_dci->pvt_info;
589 const struct edac_device_prv_data *priv = drvdata->data;
590 void *generic_ptr = edac_dci->dev;
591
592 if (!user_buf || get_user(trig_type, user_buf))
593 return -EFAULT;
594
595 if (!priv->alloc_mem)
596 return -ENOMEM;
597
598 /*
599 * Note that generic_ptr is initialized to the device * but in
600 * some alloc_functions, this is overridden and returns data.
601 */
602 ptemp = priv->alloc_mem(priv->trig_alloc_sz, &generic_ptr);
603 if (!ptemp) {
604 edac_printk(KERN_ERR, EDAC_DEVICE,
605 "Inject: Buffer Allocation error\n");
606 return -ENOMEM;
607 }
608
609 if (trig_type == ALTR_UE_TRIGGER_CHAR)
610 error_mask = priv->ue_set_mask;
611 else
612 error_mask = priv->ce_set_mask;
613
614 edac_printk(KERN_ALERT, EDAC_DEVICE,
615 "Trigger Error Mask (0x%X)\n", error_mask);
616
617 local_irq_save(flags);
618 /* write ECC corrupted data out. */
619 for (i = 0; i < (priv->trig_alloc_sz / sizeof(*ptemp)); i++) {
620 /* Read data so we're in the correct state */
621 rmb();
622 if (ACCESS_ONCE(ptemp[i]))
623 result = -1;
624 /* Toggle Error bit (it is latched), leave ECC enabled */
625 writel(error_mask, drvdata->base);
626 writel(priv->ecc_enable_mask, drvdata->base);
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
654static void altr_create_edacdev_dbgfs(struct edac_device_ctl_info *edac_dci,
655 const struct edac_device_prv_data *priv)
656{
657 struct altr_edac_device_dev *drvdata = edac_dci->pvt_info;
658
659 if (!IS_ENABLED(CONFIG_EDAC_DEBUG))
660 return;
661
662 drvdata->debugfs_dir = edac_debugfs_create_dir(drvdata->edac_dev_name);
663 if (!drvdata->debugfs_dir)
664 return;
665
666 if (!edac_debugfs_create_file(priv->dbgfs_name, S_IWUSR,
667 drvdata->debugfs_dir, edac_dci,
668 &altr_edac_device_inject_fops))
669 debugfs_remove_recursive(drvdata->debugfs_dir);
670}
671
672static const struct of_device_id altr_edac_device_of_match[] = {
673#ifdef CONFIG_EDAC_ALTERA_L2C
674 { .compatible = "altr,socfpga-l2-ecc", .data = (void *)&l2ecc_data },
675#endif
676#ifdef CONFIG_EDAC_ALTERA_OCRAM
677 { .compatible = "altr,socfpga-ocram-ecc",
678 .data = (void *)&ocramecc_data },
679#endif
680 {},
681};
682MODULE_DEVICE_TABLE(of, altr_edac_device_of_match);
683
684/*
685 * altr_edac_device_probe()
686 * This is a generic EDAC device driver that will support
687 * various Altera memory devices such as the L2 cache ECC and
688 * OCRAM ECC as well as the memories for other peripherals.
689 * Module specific initialization is done by passing the
690 * function index in the device tree.
691 */
692static int altr_edac_device_probe(struct platform_device *pdev)
693{
694 struct edac_device_ctl_info *dci;
695 struct altr_edac_device_dev *drvdata;
696 struct resource *r;
697 int res = 0;
698 struct device_node *np = pdev->dev.of_node;
699 char *ecc_name = (char *)np->name;
700 static int dev_instance;
701
702 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
703 edac_printk(KERN_ERR, EDAC_DEVICE,
704 "Unable to open devm\n");
705 return -ENOMEM;
706 }
707
708 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
709 if (!r) {
710 edac_printk(KERN_ERR, EDAC_DEVICE,
711 "Unable to get mem resource\n");
712 res = -ENODEV;
713 goto fail;
714 }
715
716 if (!devm_request_mem_region(&pdev->dev, r->start, resource_size(r),
717 dev_name(&pdev->dev))) {
718 edac_printk(KERN_ERR, EDAC_DEVICE,
719 "%s:Error requesting mem region\n", ecc_name);
720 res = -EBUSY;
721 goto fail;
722 }
723
724 dci = edac_device_alloc_ctl_info(sizeof(*drvdata), ecc_name,
725 1, ecc_name, 1, 0, NULL, 0,
726 dev_instance++);
727
728 if (!dci) {
729 edac_printk(KERN_ERR, EDAC_DEVICE,
730 "%s: Unable to allocate EDAC device\n", ecc_name);
731 res = -ENOMEM;
732 goto fail;
733 }
734
735 drvdata = dci->pvt_info;
736 dci->dev = &pdev->dev;
737 platform_set_drvdata(pdev, dci);
738 drvdata->edac_dev_name = ecc_name;
739
740 drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
741 if (!drvdata->base)
742 goto fail1;
743
744 /* Get driver specific data for this EDAC device */
745 drvdata->data = of_match_node(altr_edac_device_of_match, np)->data;
746
747 /* Check specific dependencies for the module */
748 if (drvdata->data->setup) {
Thor Thayer328ca7a2016-03-21 11:01:40 -0500749 res = drvdata->data->setup(drvdata);
Thor Thayerc3eea192016-02-10 13:26:21 -0600750 if (res)
751 goto fail1;
752 }
753
754 drvdata->sb_irq = platform_get_irq(pdev, 0);
755 res = devm_request_irq(&pdev->dev, drvdata->sb_irq,
756 altr_edac_device_handler,
757 0, dev_name(&pdev->dev), dci);
758 if (res)
759 goto fail1;
760
761 drvdata->db_irq = platform_get_irq(pdev, 1);
762 res = devm_request_irq(&pdev->dev, drvdata->db_irq,
763 altr_edac_device_handler,
764 0, dev_name(&pdev->dev), dci);
765 if (res)
766 goto fail1;
767
768 dci->mod_name = "Altera ECC Manager";
769 dci->dev_name = drvdata->edac_dev_name;
770
771 res = edac_device_add_device(dci);
772 if (res)
773 goto fail1;
774
775 altr_create_edacdev_dbgfs(dci, drvdata->data);
776
777 devres_close_group(&pdev->dev, NULL);
778
779 return 0;
780
781fail1:
782 edac_device_free_ctl_info(dci);
783fail:
784 devres_release_group(&pdev->dev, NULL);
785 edac_printk(KERN_ERR, EDAC_DEVICE,
786 "%s:Error setting up EDAC device: %d\n", ecc_name, res);
787
788 return res;
789}
790
791static int altr_edac_device_remove(struct platform_device *pdev)
792{
793 struct edac_device_ctl_info *dci = platform_get_drvdata(pdev);
794 struct altr_edac_device_dev *drvdata = dci->pvt_info;
795
796 debugfs_remove_recursive(drvdata->debugfs_dir);
797 edac_device_del_device(&pdev->dev);
798 edac_device_free_ctl_info(dci);
799
800 return 0;
801}
802
803static struct platform_driver altr_edac_device_driver = {
804 .probe = altr_edac_device_probe,
805 .remove = altr_edac_device_remove,
806 .driver = {
807 .name = "altr_edac_device",
808 .of_match_table = altr_edac_device_of_match,
809 },
810};
811module_platform_driver(altr_edac_device_driver);
812
813/*********************** OCRAM EDAC Device Functions *********************/
814
815#ifdef CONFIG_EDAC_ALTERA_OCRAM
816
817static void *ocram_alloc_mem(size_t size, void **other)
818{
819 struct device_node *np;
820 struct gen_pool *gp;
821 void *sram_addr;
822
823 np = of_find_compatible_node(NULL, NULL, "altr,socfpga-ocram-ecc");
824 if (!np)
825 return NULL;
826
827 gp = of_gen_pool_get(np, "iram", 0);
828 of_node_put(np);
829 if (!gp)
830 return NULL;
831
832 sram_addr = (void *)gen_pool_alloc(gp, size);
833 if (!sram_addr)
834 return NULL;
835
836 memset(sram_addr, 0, size);
837 /* Ensure data is written out */
838 wmb();
839
840 /* Remember this handle for freeing later */
841 *other = gp;
842
843 return sram_addr;
844}
845
846static void ocram_free_mem(void *p, size_t size, void *other)
847{
848 gen_pool_free((struct gen_pool *)other, (u32)p, size);
849}
850
851/*
852 * altr_ocram_check_deps()
853 * Test for OCRAM cache ECC dependencies upon entry because
854 * platform specific startup should have initialized the
855 * On-Chip RAM memory and enabled the ECC.
856 * Can't turn on ECC here because accessing un-initialized
857 * memory will cause CE/UE errors possibly causing an ABORT.
858 */
Thor Thayer328ca7a2016-03-21 11:01:40 -0500859static int altr_ocram_check_deps(struct altr_edac_device_dev *device)
Thor Thayerc3eea192016-02-10 13:26:21 -0600860{
Thor Thayer328ca7a2016-03-21 11:01:40 -0500861 void __iomem *base = device->base;
Thor Thayerc3eea192016-02-10 13:26:21 -0600862 if (readl(base) & ALTR_OCR_ECC_EN)
863 return 0;
864
865 edac_printk(KERN_ERR, EDAC_DEVICE,
866 "OCRAM: No ECC present or ECC disabled.\n");
867 return -ENODEV;
868}
869
870const struct edac_device_prv_data ocramecc_data = {
871 .setup = altr_ocram_check_deps,
872 .ce_clear_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_SERR),
873 .ue_clear_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_DERR),
874 .dbgfs_name = "altr_ocram_trigger",
875 .alloc_mem = ocram_alloc_mem,
876 .free_mem = ocram_free_mem,
877 .ecc_enable_mask = ALTR_OCR_ECC_EN,
878 .ce_set_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_INJS),
879 .ue_set_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_INJD),
880 .trig_alloc_sz = ALTR_TRIG_OCRAM_BYTE_SIZE,
881};
882
883#endif /* CONFIG_EDAC_ALTERA_OCRAM */
884
885/********************* L2 Cache EDAC Device Functions ********************/
886
887#ifdef CONFIG_EDAC_ALTERA_L2C
888
889static void *l2_alloc_mem(size_t size, void **other)
890{
891 struct device *dev = *other;
892 void *ptemp = devm_kzalloc(dev, size, GFP_KERNEL);
893
894 if (!ptemp)
895 return NULL;
896
897 /* Make sure everything is written out */
898 wmb();
899
900 /*
901 * Clean all cache levels up to LoC (includes L2)
902 * This ensures the corrupted data is written into
903 * L2 cache for readback test (which causes ECC error).
904 */
905 flush_cache_all();
906
907 return ptemp;
908}
909
910static void l2_free_mem(void *p, size_t size, void *other)
911{
912 struct device *dev = other;
913
914 if (dev && p)
915 devm_kfree(dev, p);
916}
917
918/*
919 * altr_l2_check_deps()
920 * Test for L2 cache ECC dependencies upon entry because
921 * platform specific startup should have initialized the L2
922 * memory and enabled the ECC.
923 * Bail if ECC is not enabled.
924 * Note that L2 Cache Enable is forced at build time.
925 */
Thor Thayer328ca7a2016-03-21 11:01:40 -0500926static int altr_l2_check_deps(struct altr_edac_device_dev *device)
Thor Thayerc3eea192016-02-10 13:26:21 -0600927{
Thor Thayer328ca7a2016-03-21 11:01:40 -0500928 void __iomem *base = device->base;
Thor Thayerc3eea192016-02-10 13:26:21 -0600929 if (readl(base) & ALTR_L2_ECC_EN)
930 return 0;
931
932 edac_printk(KERN_ERR, EDAC_DEVICE,
933 "L2: No ECC present, or ECC disabled\n");
934 return -ENODEV;
935}
936
937const struct edac_device_prv_data l2ecc_data = {
938 .setup = altr_l2_check_deps,
939 .ce_clear_mask = 0,
940 .ue_clear_mask = 0,
941 .dbgfs_name = "altr_l2_trigger",
942 .alloc_mem = l2_alloc_mem,
943 .free_mem = l2_free_mem,
944 .ecc_enable_mask = ALTR_L2_ECC_EN,
945 .ce_set_mask = (ALTR_L2_ECC_EN | ALTR_L2_ECC_INJS),
946 .ue_set_mask = (ALTR_L2_ECC_EN | ALTR_L2_ECC_INJD),
947 .trig_alloc_sz = ALTR_TRIG_L2C_BYTE_SIZE,
948};
949
950#endif /* CONFIG_EDAC_ALTERA_L2C */
951
Thor Thayer71bcada2014-09-03 10:27:54 -0500952MODULE_LICENSE("GPL v2");
953MODULE_AUTHOR("Thor Thayer");
Thor Thayerc3eea192016-02-10 13:26:21 -0600954MODULE_DESCRIPTION("EDAC Driver for Altera Memories");