altera_edac.c 53 KB

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