altera_edac.c 52 KB

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