altera_edac.c 52 KB

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