altera_edac.c 52 KB

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