altera_edac.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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/edac.h>
  22. #include <linux/genalloc.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mfd/syscon.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/regmap.h>
  29. #include <linux/types.h>
  30. #include <linux/uaccess.h>
  31. #include "altera_edac.h"
  32. #include "edac_core.h"
  33. #include "edac_module.h"
  34. #define EDAC_MOD_STR "altera_edac"
  35. #define EDAC_VERSION "1"
  36. #define EDAC_DEVICE "Altera"
  37. static const struct altr_sdram_prv_data c5_data = {
  38. .ecc_ctrl_offset = CV_CTLCFG_OFST,
  39. .ecc_ctl_en_mask = CV_CTLCFG_ECC_AUTO_EN,
  40. .ecc_stat_offset = CV_DRAMSTS_OFST,
  41. .ecc_stat_ce_mask = CV_DRAMSTS_SBEERR,
  42. .ecc_stat_ue_mask = CV_DRAMSTS_DBEERR,
  43. .ecc_saddr_offset = CV_ERRADDR_OFST,
  44. .ecc_daddr_offset = CV_ERRADDR_OFST,
  45. .ecc_cecnt_offset = CV_SBECOUNT_OFST,
  46. .ecc_uecnt_offset = CV_DBECOUNT_OFST,
  47. .ecc_irq_en_offset = CV_DRAMINTR_OFST,
  48. .ecc_irq_en_mask = CV_DRAMINTR_INTREN,
  49. .ecc_irq_clr_offset = CV_DRAMINTR_OFST,
  50. .ecc_irq_clr_mask = (CV_DRAMINTR_INTRCLR | CV_DRAMINTR_INTREN),
  51. .ecc_cnt_rst_offset = CV_DRAMINTR_OFST,
  52. .ecc_cnt_rst_mask = CV_DRAMINTR_INTRCLR,
  53. .ce_ue_trgr_offset = CV_CTLCFG_OFST,
  54. .ce_set_mask = CV_CTLCFG_GEN_SB_ERR,
  55. .ue_set_mask = CV_CTLCFG_GEN_DB_ERR,
  56. };
  57. static const struct altr_sdram_prv_data a10_data = {
  58. .ecc_ctrl_offset = A10_ECCCTRL1_OFST,
  59. .ecc_ctl_en_mask = A10_ECCCTRL1_ECC_EN,
  60. .ecc_stat_offset = A10_INTSTAT_OFST,
  61. .ecc_stat_ce_mask = A10_INTSTAT_SBEERR,
  62. .ecc_stat_ue_mask = A10_INTSTAT_DBEERR,
  63. .ecc_saddr_offset = A10_SERRADDR_OFST,
  64. .ecc_daddr_offset = A10_DERRADDR_OFST,
  65. .ecc_irq_en_offset = A10_ERRINTEN_OFST,
  66. .ecc_irq_en_mask = A10_ECC_IRQ_EN_MASK,
  67. .ecc_irq_clr_offset = A10_INTSTAT_OFST,
  68. .ecc_irq_clr_mask = (A10_INTSTAT_SBEERR | A10_INTSTAT_DBEERR),
  69. .ecc_cnt_rst_offset = A10_ECCCTRL1_OFST,
  70. .ecc_cnt_rst_mask = A10_ECC_CNT_RESET_MASK,
  71. .ce_ue_trgr_offset = A10_DIAGINTTEST_OFST,
  72. .ce_set_mask = A10_DIAGINT_TSERRA_MASK,
  73. .ue_set_mask = A10_DIAGINT_TDERRA_MASK,
  74. };
  75. /************************** EDAC Device Defines **************************/
  76. /* OCRAM ECC Management Group Defines */
  77. #define ALTR_MAN_GRP_OCRAM_ECC_OFFSET 0x04
  78. #define ALTR_OCR_ECC_EN BIT(0)
  79. #define ALTR_OCR_ECC_INJS BIT(1)
  80. #define ALTR_OCR_ECC_INJD BIT(2)
  81. #define ALTR_OCR_ECC_SERR BIT(3)
  82. #define ALTR_OCR_ECC_DERR BIT(4)
  83. /* L2 ECC Management Group Defines */
  84. #define ALTR_MAN_GRP_L2_ECC_OFFSET 0x00
  85. #define ALTR_L2_ECC_EN BIT(0)
  86. #define ALTR_L2_ECC_INJS BIT(1)
  87. #define ALTR_L2_ECC_INJD BIT(2)
  88. #define ALTR_UE_TRIGGER_CHAR 'U' /* Trigger for UE */
  89. #define ALTR_TRIGGER_READ_WRD_CNT 32 /* Line size x 4 */
  90. #define ALTR_TRIG_OCRAM_BYTE_SIZE 128 /* Line size x 4 */
  91. #define ALTR_TRIG_L2C_BYTE_SIZE 4096 /* Full Page */
  92. /*********************** EDAC Memory Controller Functions ****************/
  93. /* The SDRAM controller uses the EDAC Memory Controller framework. */
  94. static irqreturn_t altr_sdram_mc_err_handler(int irq, void *dev_id)
  95. {
  96. struct mem_ctl_info *mci = dev_id;
  97. struct altr_sdram_mc_data *drvdata = mci->pvt_info;
  98. const struct altr_sdram_prv_data *priv = drvdata->data;
  99. u32 status, err_count = 1, err_addr;
  100. regmap_read(drvdata->mc_vbase, priv->ecc_stat_offset, &status);
  101. if (status & priv->ecc_stat_ue_mask) {
  102. regmap_read(drvdata->mc_vbase, priv->ecc_daddr_offset,
  103. &err_addr);
  104. if (priv->ecc_uecnt_offset)
  105. regmap_read(drvdata->mc_vbase, priv->ecc_uecnt_offset,
  106. &err_count);
  107. panic("\nEDAC: [%d Uncorrectable errors @ 0x%08X]\n",
  108. err_count, err_addr);
  109. }
  110. if (status & priv->ecc_stat_ce_mask) {
  111. regmap_read(drvdata->mc_vbase, priv->ecc_saddr_offset,
  112. &err_addr);
  113. if (priv->ecc_uecnt_offset)
  114. regmap_read(drvdata->mc_vbase, priv->ecc_cecnt_offset,
  115. &err_count);
  116. edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, err_count,
  117. err_addr >> PAGE_SHIFT,
  118. err_addr & ~PAGE_MASK, 0,
  119. 0, 0, -1, mci->ctl_name, "");
  120. /* Clear IRQ to resume */
  121. regmap_write(drvdata->mc_vbase, priv->ecc_irq_clr_offset,
  122. priv->ecc_irq_clr_mask);
  123. return IRQ_HANDLED;
  124. }
  125. return IRQ_NONE;
  126. }
  127. static ssize_t altr_sdr_mc_err_inject_write(struct file *file,
  128. const char __user *data,
  129. size_t count, loff_t *ppos)
  130. {
  131. struct mem_ctl_info *mci = file->private_data;
  132. struct altr_sdram_mc_data *drvdata = mci->pvt_info;
  133. const struct altr_sdram_prv_data *priv = drvdata->data;
  134. u32 *ptemp;
  135. dma_addr_t dma_handle;
  136. u32 reg, read_reg;
  137. ptemp = dma_alloc_coherent(mci->pdev, 16, &dma_handle, GFP_KERNEL);
  138. if (!ptemp) {
  139. dma_free_coherent(mci->pdev, 16, ptemp, dma_handle);
  140. edac_printk(KERN_ERR, EDAC_MC,
  141. "Inject: Buffer Allocation error\n");
  142. return -ENOMEM;
  143. }
  144. regmap_read(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
  145. &read_reg);
  146. read_reg &= ~(priv->ce_set_mask | priv->ue_set_mask);
  147. /* Error are injected by writing a word while the SBE or DBE
  148. * bit in the CTLCFG register is set. Reading the word will
  149. * trigger the SBE or DBE error and the corresponding IRQ.
  150. */
  151. if (count == 3) {
  152. edac_printk(KERN_ALERT, EDAC_MC,
  153. "Inject Double bit error\n");
  154. regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
  155. (read_reg | priv->ue_set_mask));
  156. } else {
  157. edac_printk(KERN_ALERT, EDAC_MC,
  158. "Inject Single bit error\n");
  159. regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
  160. (read_reg | priv->ce_set_mask));
  161. }
  162. ptemp[0] = 0x5A5A5A5A;
  163. ptemp[1] = 0xA5A5A5A5;
  164. /* Clear the error injection bits */
  165. regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset, read_reg);
  166. /* Ensure it has been written out */
  167. wmb();
  168. /*
  169. * To trigger the error, we need to read the data back
  170. * (the data was written with errors above).
  171. * The ACCESS_ONCE macros and printk are used to prevent the
  172. * the compiler optimizing these reads out.
  173. */
  174. reg = ACCESS_ONCE(ptemp[0]);
  175. read_reg = ACCESS_ONCE(ptemp[1]);
  176. /* Force Read */
  177. rmb();
  178. edac_printk(KERN_ALERT, EDAC_MC, "Read Data [0x%X, 0x%X]\n",
  179. reg, read_reg);
  180. dma_free_coherent(mci->pdev, 16, ptemp, dma_handle);
  181. return count;
  182. }
  183. static const struct file_operations altr_sdr_mc_debug_inject_fops = {
  184. .open = simple_open,
  185. .write = altr_sdr_mc_err_inject_write,
  186. .llseek = generic_file_llseek,
  187. };
  188. static void altr_sdr_mc_create_debugfs_nodes(struct mem_ctl_info *mci)
  189. {
  190. if (!IS_ENABLED(CONFIG_EDAC_DEBUG))
  191. return;
  192. if (!mci->debugfs)
  193. return;
  194. edac_debugfs_create_file("inject_ctrl", S_IWUSR, mci->debugfs, mci,
  195. &altr_sdr_mc_debug_inject_fops);
  196. }
  197. /* Get total memory size from Open Firmware DTB */
  198. static unsigned long get_total_mem(void)
  199. {
  200. struct device_node *np = NULL;
  201. const unsigned int *reg, *reg_end;
  202. int len, sw, aw;
  203. unsigned long start, size, total_mem = 0;
  204. for_each_node_by_type(np, "memory") {
  205. aw = of_n_addr_cells(np);
  206. sw = of_n_size_cells(np);
  207. reg = (const unsigned int *)of_get_property(np, "reg", &len);
  208. reg_end = reg + (len / sizeof(u32));
  209. total_mem = 0;
  210. do {
  211. start = of_read_number(reg, aw);
  212. reg += aw;
  213. size = of_read_number(reg, sw);
  214. reg += sw;
  215. total_mem += size;
  216. } while (reg < reg_end);
  217. }
  218. edac_dbg(0, "total_mem 0x%lx\n", total_mem);
  219. return total_mem;
  220. }
  221. static const struct of_device_id altr_sdram_ctrl_of_match[] = {
  222. { .compatible = "altr,sdram-edac", .data = (void *)&c5_data},
  223. { .compatible = "altr,sdram-edac-a10", .data = (void *)&a10_data},
  224. {},
  225. };
  226. MODULE_DEVICE_TABLE(of, altr_sdram_ctrl_of_match);
  227. static int a10_init(struct regmap *mc_vbase)
  228. {
  229. if (regmap_update_bits(mc_vbase, A10_INTMODE_OFST,
  230. A10_INTMODE_SB_INT, A10_INTMODE_SB_INT)) {
  231. edac_printk(KERN_ERR, EDAC_MC,
  232. "Error setting SB IRQ mode\n");
  233. return -ENODEV;
  234. }
  235. if (regmap_write(mc_vbase, A10_SERRCNTREG_OFST, 1)) {
  236. edac_printk(KERN_ERR, EDAC_MC,
  237. "Error setting trigger count\n");
  238. return -ENODEV;
  239. }
  240. return 0;
  241. }
  242. static int a10_unmask_irq(struct platform_device *pdev, u32 mask)
  243. {
  244. void __iomem *sm_base;
  245. int ret = 0;
  246. if (!request_mem_region(A10_SYMAN_INTMASK_CLR, sizeof(u32),
  247. dev_name(&pdev->dev))) {
  248. edac_printk(KERN_ERR, EDAC_MC,
  249. "Unable to request mem region\n");
  250. return -EBUSY;
  251. }
  252. sm_base = ioremap(A10_SYMAN_INTMASK_CLR, sizeof(u32));
  253. if (!sm_base) {
  254. edac_printk(KERN_ERR, EDAC_MC,
  255. "Unable to ioremap device\n");
  256. ret = -ENOMEM;
  257. goto release;
  258. }
  259. iowrite32(mask, sm_base);
  260. iounmap(sm_base);
  261. release:
  262. release_mem_region(A10_SYMAN_INTMASK_CLR, sizeof(u32));
  263. return ret;
  264. }
  265. static int altr_sdram_probe(struct platform_device *pdev)
  266. {
  267. const struct of_device_id *id;
  268. struct edac_mc_layer layers[2];
  269. struct mem_ctl_info *mci;
  270. struct altr_sdram_mc_data *drvdata;
  271. const struct altr_sdram_prv_data *priv;
  272. struct regmap *mc_vbase;
  273. struct dimm_info *dimm;
  274. u32 read_reg;
  275. int irq, irq2, res = 0;
  276. unsigned long mem_size, irqflags = 0;
  277. id = of_match_device(altr_sdram_ctrl_of_match, &pdev->dev);
  278. if (!id)
  279. return -ENODEV;
  280. /* Grab the register range from the sdr controller in device tree */
  281. mc_vbase = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  282. "altr,sdr-syscon");
  283. if (IS_ERR(mc_vbase)) {
  284. edac_printk(KERN_ERR, EDAC_MC,
  285. "regmap for altr,sdr-syscon lookup failed.\n");
  286. return -ENODEV;
  287. }
  288. /* Check specific dependencies for the module */
  289. priv = of_match_node(altr_sdram_ctrl_of_match,
  290. pdev->dev.of_node)->data;
  291. /* Validate the SDRAM controller has ECC enabled */
  292. if (regmap_read(mc_vbase, priv->ecc_ctrl_offset, &read_reg) ||
  293. ((read_reg & priv->ecc_ctl_en_mask) != priv->ecc_ctl_en_mask)) {
  294. edac_printk(KERN_ERR, EDAC_MC,
  295. "No ECC/ECC disabled [0x%08X]\n", read_reg);
  296. return -ENODEV;
  297. }
  298. /* Grab memory size from device tree. */
  299. mem_size = get_total_mem();
  300. if (!mem_size) {
  301. edac_printk(KERN_ERR, EDAC_MC, "Unable to calculate memory size\n");
  302. return -ENODEV;
  303. }
  304. /* Ensure the SDRAM Interrupt is disabled */
  305. if (regmap_update_bits(mc_vbase, priv->ecc_irq_en_offset,
  306. priv->ecc_irq_en_mask, 0)) {
  307. edac_printk(KERN_ERR, EDAC_MC,
  308. "Error disabling SDRAM ECC IRQ\n");
  309. return -ENODEV;
  310. }
  311. /* Toggle to clear the SDRAM Error count */
  312. if (regmap_update_bits(mc_vbase, priv->ecc_cnt_rst_offset,
  313. priv->ecc_cnt_rst_mask,
  314. priv->ecc_cnt_rst_mask)) {
  315. edac_printk(KERN_ERR, EDAC_MC,
  316. "Error clearing SDRAM ECC count\n");
  317. return -ENODEV;
  318. }
  319. if (regmap_update_bits(mc_vbase, priv->ecc_cnt_rst_offset,
  320. priv->ecc_cnt_rst_mask, 0)) {
  321. edac_printk(KERN_ERR, EDAC_MC,
  322. "Error clearing SDRAM ECC count\n");
  323. return -ENODEV;
  324. }
  325. irq = platform_get_irq(pdev, 0);
  326. if (irq < 0) {
  327. edac_printk(KERN_ERR, EDAC_MC,
  328. "No irq %d in DT\n", irq);
  329. return -ENODEV;
  330. }
  331. /* Arria10 has a 2nd IRQ */
  332. irq2 = platform_get_irq(pdev, 1);
  333. layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
  334. layers[0].size = 1;
  335. layers[0].is_virt_csrow = true;
  336. layers[1].type = EDAC_MC_LAYER_CHANNEL;
  337. layers[1].size = 1;
  338. layers[1].is_virt_csrow = false;
  339. mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers,
  340. sizeof(struct altr_sdram_mc_data));
  341. if (!mci)
  342. return -ENOMEM;
  343. mci->pdev = &pdev->dev;
  344. drvdata = mci->pvt_info;
  345. drvdata->mc_vbase = mc_vbase;
  346. drvdata->data = priv;
  347. platform_set_drvdata(pdev, mci);
  348. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
  349. edac_printk(KERN_ERR, EDAC_MC,
  350. "Unable to get managed device resource\n");
  351. res = -ENOMEM;
  352. goto free;
  353. }
  354. mci->mtype_cap = MEM_FLAG_DDR3;
  355. mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_SECDED;
  356. mci->edac_cap = EDAC_FLAG_SECDED;
  357. mci->mod_name = EDAC_MOD_STR;
  358. mci->mod_ver = EDAC_VERSION;
  359. mci->ctl_name = dev_name(&pdev->dev);
  360. mci->scrub_mode = SCRUB_SW_SRC;
  361. mci->dev_name = dev_name(&pdev->dev);
  362. dimm = *mci->dimms;
  363. dimm->nr_pages = ((mem_size - 1) >> PAGE_SHIFT) + 1;
  364. dimm->grain = 8;
  365. dimm->dtype = DEV_X8;
  366. dimm->mtype = MEM_DDR3;
  367. dimm->edac_mode = EDAC_SECDED;
  368. res = edac_mc_add_mc(mci);
  369. if (res < 0)
  370. goto err;
  371. /* Only the Arria10 has separate IRQs */
  372. if (irq2 > 0) {
  373. /* Arria10 specific initialization */
  374. res = a10_init(mc_vbase);
  375. if (res < 0)
  376. goto err2;
  377. res = devm_request_irq(&pdev->dev, irq2,
  378. altr_sdram_mc_err_handler,
  379. IRQF_SHARED, dev_name(&pdev->dev), mci);
  380. if (res < 0) {
  381. edac_mc_printk(mci, KERN_ERR,
  382. "Unable to request irq %d\n", irq2);
  383. res = -ENODEV;
  384. goto err2;
  385. }
  386. res = a10_unmask_irq(pdev, A10_DDR0_IRQ_MASK);
  387. if (res < 0)
  388. goto err2;
  389. irqflags = IRQF_SHARED;
  390. }
  391. res = devm_request_irq(&pdev->dev, irq, altr_sdram_mc_err_handler,
  392. irqflags, dev_name(&pdev->dev), mci);
  393. if (res < 0) {
  394. edac_mc_printk(mci, KERN_ERR,
  395. "Unable to request irq %d\n", irq);
  396. res = -ENODEV;
  397. goto err2;
  398. }
  399. /* Infrastructure ready - enable the IRQ */
  400. if (regmap_update_bits(drvdata->mc_vbase, priv->ecc_irq_en_offset,
  401. priv->ecc_irq_en_mask, priv->ecc_irq_en_mask)) {
  402. edac_mc_printk(mci, KERN_ERR,
  403. "Error enabling SDRAM ECC IRQ\n");
  404. res = -ENODEV;
  405. goto err2;
  406. }
  407. altr_sdr_mc_create_debugfs_nodes(mci);
  408. devres_close_group(&pdev->dev, NULL);
  409. return 0;
  410. err2:
  411. edac_mc_del_mc(&pdev->dev);
  412. err:
  413. devres_release_group(&pdev->dev, NULL);
  414. free:
  415. edac_mc_free(mci);
  416. edac_printk(KERN_ERR, EDAC_MC,
  417. "EDAC Probe Failed; Error %d\n", res);
  418. return res;
  419. }
  420. static int altr_sdram_remove(struct platform_device *pdev)
  421. {
  422. struct mem_ctl_info *mci = platform_get_drvdata(pdev);
  423. edac_mc_del_mc(&pdev->dev);
  424. edac_mc_free(mci);
  425. platform_set_drvdata(pdev, NULL);
  426. return 0;
  427. }
  428. /*
  429. * If you want to suspend, need to disable EDAC by removing it
  430. * from the device tree or defconfig.
  431. */
  432. #ifdef CONFIG_PM
  433. static int altr_sdram_prepare(struct device *dev)
  434. {
  435. pr_err("Suspend not allowed when EDAC is enabled.\n");
  436. return -EPERM;
  437. }
  438. static const struct dev_pm_ops altr_sdram_pm_ops = {
  439. .prepare = altr_sdram_prepare,
  440. };
  441. #endif
  442. static struct platform_driver altr_sdram_edac_driver = {
  443. .probe = altr_sdram_probe,
  444. .remove = altr_sdram_remove,
  445. .driver = {
  446. .name = "altr_sdram_edac",
  447. #ifdef CONFIG_PM
  448. .pm = &altr_sdram_pm_ops,
  449. #endif
  450. .of_match_table = altr_sdram_ctrl_of_match,
  451. },
  452. };
  453. module_platform_driver(altr_sdram_edac_driver);
  454. /************************* EDAC Parent Probe *************************/
  455. static const struct of_device_id altr_edac_device_of_match[];
  456. static const struct of_device_id altr_edac_of_match[] = {
  457. { .compatible = "altr,socfpga-ecc-manager" },
  458. {},
  459. };
  460. MODULE_DEVICE_TABLE(of, altr_edac_of_match);
  461. static int altr_edac_probe(struct platform_device *pdev)
  462. {
  463. of_platform_populate(pdev->dev.of_node, altr_edac_device_of_match,
  464. NULL, &pdev->dev);
  465. return 0;
  466. }
  467. static struct platform_driver altr_edac_driver = {
  468. .probe = altr_edac_probe,
  469. .driver = {
  470. .name = "socfpga_ecc_manager",
  471. .of_match_table = altr_edac_of_match,
  472. },
  473. };
  474. module_platform_driver(altr_edac_driver);
  475. /************************* EDAC Device Functions *************************/
  476. /*
  477. * EDAC Device Functions (shared between various IPs).
  478. * The discrete memories use the EDAC Device framework. The probe
  479. * and error handling functions are very similar between memories
  480. * so they are shared. The memory allocation and freeing for EDAC
  481. * trigger testing are different for each memory.
  482. */
  483. const struct edac_device_prv_data ocramecc_data;
  484. const struct edac_device_prv_data l2ecc_data;
  485. struct edac_device_prv_data {
  486. int (*setup)(struct platform_device *pdev, void __iomem *base);
  487. int ce_clear_mask;
  488. int ue_clear_mask;
  489. char dbgfs_name[20];
  490. void * (*alloc_mem)(size_t size, void **other);
  491. void (*free_mem)(void *p, size_t size, void *other);
  492. int ecc_enable_mask;
  493. int ce_set_mask;
  494. int ue_set_mask;
  495. int trig_alloc_sz;
  496. };
  497. struct altr_edac_device_dev {
  498. void __iomem *base;
  499. int sb_irq;
  500. int db_irq;
  501. const struct edac_device_prv_data *data;
  502. struct dentry *debugfs_dir;
  503. char *edac_dev_name;
  504. };
  505. static irqreturn_t altr_edac_device_handler(int irq, void *dev_id)
  506. {
  507. irqreturn_t ret_value = IRQ_NONE;
  508. struct edac_device_ctl_info *dci = dev_id;
  509. struct altr_edac_device_dev *drvdata = dci->pvt_info;
  510. const struct edac_device_prv_data *priv = drvdata->data;
  511. if (irq == drvdata->sb_irq) {
  512. if (priv->ce_clear_mask)
  513. writel(priv->ce_clear_mask, drvdata->base);
  514. edac_device_handle_ce(dci, 0, 0, drvdata->edac_dev_name);
  515. ret_value = IRQ_HANDLED;
  516. } else if (irq == drvdata->db_irq) {
  517. if (priv->ue_clear_mask)
  518. writel(priv->ue_clear_mask, drvdata->base);
  519. edac_device_handle_ue(dci, 0, 0, drvdata->edac_dev_name);
  520. panic("\nEDAC:ECC_DEVICE[Uncorrectable errors]\n");
  521. ret_value = IRQ_HANDLED;
  522. } else {
  523. WARN_ON(1);
  524. }
  525. return ret_value;
  526. }
  527. static ssize_t altr_edac_device_trig(struct file *file,
  528. const char __user *user_buf,
  529. size_t count, loff_t *ppos)
  530. {
  531. u32 *ptemp, i, error_mask;
  532. int result = 0;
  533. u8 trig_type;
  534. unsigned long flags;
  535. struct edac_device_ctl_info *edac_dci = file->private_data;
  536. struct altr_edac_device_dev *drvdata = edac_dci->pvt_info;
  537. const struct edac_device_prv_data *priv = drvdata->data;
  538. void *generic_ptr = edac_dci->dev;
  539. if (!user_buf || get_user(trig_type, user_buf))
  540. return -EFAULT;
  541. if (!priv->alloc_mem)
  542. return -ENOMEM;
  543. /*
  544. * Note that generic_ptr is initialized to the device * but in
  545. * some alloc_functions, this is overridden and returns data.
  546. */
  547. ptemp = priv->alloc_mem(priv->trig_alloc_sz, &generic_ptr);
  548. if (!ptemp) {
  549. edac_printk(KERN_ERR, EDAC_DEVICE,
  550. "Inject: Buffer Allocation error\n");
  551. return -ENOMEM;
  552. }
  553. if (trig_type == ALTR_UE_TRIGGER_CHAR)
  554. error_mask = priv->ue_set_mask;
  555. else
  556. error_mask = priv->ce_set_mask;
  557. edac_printk(KERN_ALERT, EDAC_DEVICE,
  558. "Trigger Error Mask (0x%X)\n", error_mask);
  559. local_irq_save(flags);
  560. /* write ECC corrupted data out. */
  561. for (i = 0; i < (priv->trig_alloc_sz / sizeof(*ptemp)); i++) {
  562. /* Read data so we're in the correct state */
  563. rmb();
  564. if (ACCESS_ONCE(ptemp[i]))
  565. result = -1;
  566. /* Toggle Error bit (it is latched), leave ECC enabled */
  567. writel(error_mask, drvdata->base);
  568. writel(priv->ecc_enable_mask, drvdata->base);
  569. ptemp[i] = i;
  570. }
  571. /* Ensure it has been written out */
  572. wmb();
  573. local_irq_restore(flags);
  574. if (result)
  575. edac_printk(KERN_ERR, EDAC_DEVICE, "Mem Not Cleared\n");
  576. /* Read out written data. ECC error caused here */
  577. for (i = 0; i < ALTR_TRIGGER_READ_WRD_CNT; i++)
  578. if (ACCESS_ONCE(ptemp[i]) != i)
  579. edac_printk(KERN_ERR, EDAC_DEVICE,
  580. "Read doesn't match written data\n");
  581. if (priv->free_mem)
  582. priv->free_mem(ptemp, priv->trig_alloc_sz, generic_ptr);
  583. return count;
  584. }
  585. static const struct file_operations altr_edac_device_inject_fops = {
  586. .open = simple_open,
  587. .write = altr_edac_device_trig,
  588. .llseek = generic_file_llseek,
  589. };
  590. static void altr_create_edacdev_dbgfs(struct edac_device_ctl_info *edac_dci,
  591. const struct edac_device_prv_data *priv)
  592. {
  593. struct altr_edac_device_dev *drvdata = edac_dci->pvt_info;
  594. if (!IS_ENABLED(CONFIG_EDAC_DEBUG))
  595. return;
  596. drvdata->debugfs_dir = edac_debugfs_create_dir(drvdata->edac_dev_name);
  597. if (!drvdata->debugfs_dir)
  598. return;
  599. if (!edac_debugfs_create_file(priv->dbgfs_name, S_IWUSR,
  600. drvdata->debugfs_dir, edac_dci,
  601. &altr_edac_device_inject_fops))
  602. debugfs_remove_recursive(drvdata->debugfs_dir);
  603. }
  604. static const struct of_device_id altr_edac_device_of_match[] = {
  605. #ifdef CONFIG_EDAC_ALTERA_L2C
  606. { .compatible = "altr,socfpga-l2-ecc", .data = (void *)&l2ecc_data },
  607. #endif
  608. #ifdef CONFIG_EDAC_ALTERA_OCRAM
  609. { .compatible = "altr,socfpga-ocram-ecc",
  610. .data = (void *)&ocramecc_data },
  611. #endif
  612. {},
  613. };
  614. MODULE_DEVICE_TABLE(of, altr_edac_device_of_match);
  615. /*
  616. * altr_edac_device_probe()
  617. * This is a generic EDAC device driver that will support
  618. * various Altera memory devices such as the L2 cache ECC and
  619. * OCRAM ECC as well as the memories for other peripherals.
  620. * Module specific initialization is done by passing the
  621. * function index in the device tree.
  622. */
  623. static int altr_edac_device_probe(struct platform_device *pdev)
  624. {
  625. struct edac_device_ctl_info *dci;
  626. struct altr_edac_device_dev *drvdata;
  627. struct resource *r;
  628. int res = 0;
  629. struct device_node *np = pdev->dev.of_node;
  630. char *ecc_name = (char *)np->name;
  631. static int dev_instance;
  632. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
  633. edac_printk(KERN_ERR, EDAC_DEVICE,
  634. "Unable to open devm\n");
  635. return -ENOMEM;
  636. }
  637. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  638. if (!r) {
  639. edac_printk(KERN_ERR, EDAC_DEVICE,
  640. "Unable to get mem resource\n");
  641. res = -ENODEV;
  642. goto fail;
  643. }
  644. if (!devm_request_mem_region(&pdev->dev, r->start, resource_size(r),
  645. dev_name(&pdev->dev))) {
  646. edac_printk(KERN_ERR, EDAC_DEVICE,
  647. "%s:Error requesting mem region\n", ecc_name);
  648. res = -EBUSY;
  649. goto fail;
  650. }
  651. dci = edac_device_alloc_ctl_info(sizeof(*drvdata), ecc_name,
  652. 1, ecc_name, 1, 0, NULL, 0,
  653. dev_instance++);
  654. if (!dci) {
  655. edac_printk(KERN_ERR, EDAC_DEVICE,
  656. "%s: Unable to allocate EDAC device\n", ecc_name);
  657. res = -ENOMEM;
  658. goto fail;
  659. }
  660. drvdata = dci->pvt_info;
  661. dci->dev = &pdev->dev;
  662. platform_set_drvdata(pdev, dci);
  663. drvdata->edac_dev_name = ecc_name;
  664. drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  665. if (!drvdata->base)
  666. goto fail1;
  667. /* Get driver specific data for this EDAC device */
  668. drvdata->data = of_match_node(altr_edac_device_of_match, np)->data;
  669. /* Check specific dependencies for the module */
  670. if (drvdata->data->setup) {
  671. res = drvdata->data->setup(pdev, drvdata->base);
  672. if (res)
  673. goto fail1;
  674. }
  675. drvdata->sb_irq = platform_get_irq(pdev, 0);
  676. res = devm_request_irq(&pdev->dev, drvdata->sb_irq,
  677. altr_edac_device_handler,
  678. 0, dev_name(&pdev->dev), dci);
  679. if (res)
  680. goto fail1;
  681. drvdata->db_irq = platform_get_irq(pdev, 1);
  682. res = devm_request_irq(&pdev->dev, drvdata->db_irq,
  683. altr_edac_device_handler,
  684. 0, dev_name(&pdev->dev), dci);
  685. if (res)
  686. goto fail1;
  687. dci->mod_name = "Altera ECC Manager";
  688. dci->dev_name = drvdata->edac_dev_name;
  689. res = edac_device_add_device(dci);
  690. if (res)
  691. goto fail1;
  692. altr_create_edacdev_dbgfs(dci, drvdata->data);
  693. devres_close_group(&pdev->dev, NULL);
  694. return 0;
  695. fail1:
  696. edac_device_free_ctl_info(dci);
  697. fail:
  698. devres_release_group(&pdev->dev, NULL);
  699. edac_printk(KERN_ERR, EDAC_DEVICE,
  700. "%s:Error setting up EDAC device: %d\n", ecc_name, res);
  701. return res;
  702. }
  703. static int altr_edac_device_remove(struct platform_device *pdev)
  704. {
  705. struct edac_device_ctl_info *dci = platform_get_drvdata(pdev);
  706. struct altr_edac_device_dev *drvdata = dci->pvt_info;
  707. debugfs_remove_recursive(drvdata->debugfs_dir);
  708. edac_device_del_device(&pdev->dev);
  709. edac_device_free_ctl_info(dci);
  710. return 0;
  711. }
  712. static struct platform_driver altr_edac_device_driver = {
  713. .probe = altr_edac_device_probe,
  714. .remove = altr_edac_device_remove,
  715. .driver = {
  716. .name = "altr_edac_device",
  717. .of_match_table = altr_edac_device_of_match,
  718. },
  719. };
  720. module_platform_driver(altr_edac_device_driver);
  721. /*********************** OCRAM EDAC Device Functions *********************/
  722. #ifdef CONFIG_EDAC_ALTERA_OCRAM
  723. static void *ocram_alloc_mem(size_t size, void **other)
  724. {
  725. struct device_node *np;
  726. struct gen_pool *gp;
  727. void *sram_addr;
  728. np = of_find_compatible_node(NULL, NULL, "altr,socfpga-ocram-ecc");
  729. if (!np)
  730. return NULL;
  731. gp = of_gen_pool_get(np, "iram", 0);
  732. of_node_put(np);
  733. if (!gp)
  734. return NULL;
  735. sram_addr = (void *)gen_pool_alloc(gp, size);
  736. if (!sram_addr)
  737. return NULL;
  738. memset(sram_addr, 0, size);
  739. /* Ensure data is written out */
  740. wmb();
  741. /* Remember this handle for freeing later */
  742. *other = gp;
  743. return sram_addr;
  744. }
  745. static void ocram_free_mem(void *p, size_t size, void *other)
  746. {
  747. gen_pool_free((struct gen_pool *)other, (u32)p, size);
  748. }
  749. /*
  750. * altr_ocram_check_deps()
  751. * Test for OCRAM cache ECC dependencies upon entry because
  752. * platform specific startup should have initialized the
  753. * On-Chip RAM memory and enabled the ECC.
  754. * Can't turn on ECC here because accessing un-initialized
  755. * memory will cause CE/UE errors possibly causing an ABORT.
  756. */
  757. static int altr_ocram_check_deps(struct platform_device *pdev,
  758. void __iomem *base)
  759. {
  760. if (readl(base) & ALTR_OCR_ECC_EN)
  761. return 0;
  762. edac_printk(KERN_ERR, EDAC_DEVICE,
  763. "OCRAM: No ECC present or ECC disabled.\n");
  764. return -ENODEV;
  765. }
  766. const struct edac_device_prv_data ocramecc_data = {
  767. .setup = altr_ocram_check_deps,
  768. .ce_clear_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_SERR),
  769. .ue_clear_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_DERR),
  770. .dbgfs_name = "altr_ocram_trigger",
  771. .alloc_mem = ocram_alloc_mem,
  772. .free_mem = ocram_free_mem,
  773. .ecc_enable_mask = ALTR_OCR_ECC_EN,
  774. .ce_set_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_INJS),
  775. .ue_set_mask = (ALTR_OCR_ECC_EN | ALTR_OCR_ECC_INJD),
  776. .trig_alloc_sz = ALTR_TRIG_OCRAM_BYTE_SIZE,
  777. };
  778. #endif /* CONFIG_EDAC_ALTERA_OCRAM */
  779. /********************* L2 Cache EDAC Device Functions ********************/
  780. #ifdef CONFIG_EDAC_ALTERA_L2C
  781. static void *l2_alloc_mem(size_t size, void **other)
  782. {
  783. struct device *dev = *other;
  784. void *ptemp = devm_kzalloc(dev, size, GFP_KERNEL);
  785. if (!ptemp)
  786. return NULL;
  787. /* Make sure everything is written out */
  788. wmb();
  789. /*
  790. * Clean all cache levels up to LoC (includes L2)
  791. * This ensures the corrupted data is written into
  792. * L2 cache for readback test (which causes ECC error).
  793. */
  794. flush_cache_all();
  795. return ptemp;
  796. }
  797. static void l2_free_mem(void *p, size_t size, void *other)
  798. {
  799. struct device *dev = other;
  800. if (dev && p)
  801. devm_kfree(dev, p);
  802. }
  803. /*
  804. * altr_l2_check_deps()
  805. * Test for L2 cache ECC dependencies upon entry because
  806. * platform specific startup should have initialized the L2
  807. * memory and enabled the ECC.
  808. * Bail if ECC is not enabled.
  809. * Note that L2 Cache Enable is forced at build time.
  810. */
  811. static int altr_l2_check_deps(struct platform_device *pdev,
  812. void __iomem *base)
  813. {
  814. if (readl(base) & ALTR_L2_ECC_EN)
  815. return 0;
  816. edac_printk(KERN_ERR, EDAC_DEVICE,
  817. "L2: No ECC present, or ECC disabled\n");
  818. return -ENODEV;
  819. }
  820. const struct edac_device_prv_data l2ecc_data = {
  821. .setup = altr_l2_check_deps,
  822. .ce_clear_mask = 0,
  823. .ue_clear_mask = 0,
  824. .dbgfs_name = "altr_l2_trigger",
  825. .alloc_mem = l2_alloc_mem,
  826. .free_mem = l2_free_mem,
  827. .ecc_enable_mask = ALTR_L2_ECC_EN,
  828. .ce_set_mask = (ALTR_L2_ECC_EN | ALTR_L2_ECC_INJS),
  829. .ue_set_mask = (ALTR_L2_ECC_EN | ALTR_L2_ECC_INJD),
  830. .trig_alloc_sz = ALTR_TRIG_L2C_BYTE_SIZE,
  831. };
  832. #endif /* CONFIG_EDAC_ALTERA_L2C */
  833. MODULE_LICENSE("GPL v2");
  834. MODULE_AUTHOR("Thor Thayer");
  835. MODULE_DESCRIPTION("EDAC Driver for Altera Memories");