altera_edac.c 65 KB

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