altera_edac.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Copyright Altera Corporation (C) 2014-2015. 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 <linux/ctype.h>
  20. #include <linux/edac.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/kernel.h>
  23. #include <linux/mfd/syscon.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/regmap.h>
  27. #include <linux/types.h>
  28. #include <linux/uaccess.h>
  29. #include "altera_edac.h"
  30. #include "edac_core.h"
  31. #include "edac_module.h"
  32. #define EDAC_MOD_STR "altera_edac"
  33. #define EDAC_VERSION "1"
  34. static const struct altr_sdram_prv_data c5_data = {
  35. .ecc_ctrl_offset = CV_CTLCFG_OFST,
  36. .ecc_ctl_en_mask = CV_CTLCFG_ECC_AUTO_EN,
  37. .ecc_stat_offset = CV_DRAMSTS_OFST,
  38. .ecc_stat_ce_mask = CV_DRAMSTS_SBEERR,
  39. .ecc_stat_ue_mask = CV_DRAMSTS_DBEERR,
  40. .ecc_saddr_offset = CV_ERRADDR_OFST,
  41. .ecc_daddr_offset = CV_ERRADDR_OFST,
  42. .ecc_cecnt_offset = CV_SBECOUNT_OFST,
  43. .ecc_uecnt_offset = CV_DBECOUNT_OFST,
  44. .ecc_irq_en_offset = CV_DRAMINTR_OFST,
  45. .ecc_irq_en_mask = CV_DRAMINTR_INTREN,
  46. .ecc_irq_clr_offset = CV_DRAMINTR_OFST,
  47. .ecc_irq_clr_mask = (CV_DRAMINTR_INTRCLR | CV_DRAMINTR_INTREN),
  48. .ecc_cnt_rst_offset = CV_DRAMINTR_OFST,
  49. .ecc_cnt_rst_mask = CV_DRAMINTR_INTRCLR,
  50. .ce_ue_trgr_offset = CV_CTLCFG_OFST,
  51. .ce_set_mask = CV_CTLCFG_GEN_SB_ERR,
  52. .ue_set_mask = CV_CTLCFG_GEN_DB_ERR,
  53. };
  54. static const struct altr_sdram_prv_data a10_data = {
  55. .ecc_ctrl_offset = A10_ECCCTRL1_OFST,
  56. .ecc_ctl_en_mask = A10_ECCCTRL1_ECC_EN,
  57. .ecc_stat_offset = A10_INTSTAT_OFST,
  58. .ecc_stat_ce_mask = A10_INTSTAT_SBEERR,
  59. .ecc_stat_ue_mask = A10_INTSTAT_DBEERR,
  60. .ecc_saddr_offset = A10_SERRADDR_OFST,
  61. .ecc_daddr_offset = A10_DERRADDR_OFST,
  62. .ecc_irq_en_offset = A10_ERRINTEN_OFST,
  63. .ecc_irq_en_mask = A10_ECC_IRQ_EN_MASK,
  64. .ecc_irq_clr_offset = A10_INTSTAT_OFST,
  65. .ecc_irq_clr_mask = (A10_INTSTAT_SBEERR | A10_INTSTAT_DBEERR),
  66. .ecc_cnt_rst_offset = A10_ECCCTRL1_OFST,
  67. .ecc_cnt_rst_mask = A10_ECC_CNT_RESET_MASK,
  68. .ce_ue_trgr_offset = A10_DIAGINTTEST_OFST,
  69. .ce_set_mask = A10_DIAGINT_TSERRA_MASK,
  70. .ue_set_mask = A10_DIAGINT_TDERRA_MASK,
  71. };
  72. static irqreturn_t altr_sdram_mc_err_handler(int irq, void *dev_id)
  73. {
  74. struct mem_ctl_info *mci = dev_id;
  75. struct altr_sdram_mc_data *drvdata = mci->pvt_info;
  76. const struct altr_sdram_prv_data *priv = drvdata->data;
  77. u32 status, err_count = 1, err_addr;
  78. regmap_read(drvdata->mc_vbase, priv->ecc_stat_offset, &status);
  79. if (status & priv->ecc_stat_ue_mask) {
  80. regmap_read(drvdata->mc_vbase, priv->ecc_daddr_offset,
  81. &err_addr);
  82. if (priv->ecc_uecnt_offset)
  83. regmap_read(drvdata->mc_vbase, priv->ecc_uecnt_offset,
  84. &err_count);
  85. panic("\nEDAC: [%d Uncorrectable errors @ 0x%08X]\n",
  86. err_count, err_addr);
  87. }
  88. if (status & priv->ecc_stat_ce_mask) {
  89. regmap_read(drvdata->mc_vbase, priv->ecc_saddr_offset,
  90. &err_addr);
  91. if (priv->ecc_uecnt_offset)
  92. regmap_read(drvdata->mc_vbase, priv->ecc_cecnt_offset,
  93. &err_count);
  94. edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, err_count,
  95. err_addr >> PAGE_SHIFT,
  96. err_addr & ~PAGE_MASK, 0,
  97. 0, 0, -1, mci->ctl_name, "");
  98. /* Clear IRQ to resume */
  99. regmap_write(drvdata->mc_vbase, priv->ecc_irq_clr_offset,
  100. priv->ecc_irq_clr_mask);
  101. return IRQ_HANDLED;
  102. }
  103. return IRQ_NONE;
  104. }
  105. static ssize_t altr_sdr_mc_err_inject_write(struct file *file,
  106. const char __user *data,
  107. size_t count, loff_t *ppos)
  108. {
  109. struct mem_ctl_info *mci = file->private_data;
  110. struct altr_sdram_mc_data *drvdata = mci->pvt_info;
  111. const struct altr_sdram_prv_data *priv = drvdata->data;
  112. u32 *ptemp;
  113. dma_addr_t dma_handle;
  114. u32 reg, read_reg;
  115. ptemp = dma_alloc_coherent(mci->pdev, 16, &dma_handle, GFP_KERNEL);
  116. if (!ptemp) {
  117. dma_free_coherent(mci->pdev, 16, ptemp, dma_handle);
  118. edac_printk(KERN_ERR, EDAC_MC,
  119. "Inject: Buffer Allocation error\n");
  120. return -ENOMEM;
  121. }
  122. regmap_read(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
  123. &read_reg);
  124. read_reg &= ~(priv->ce_set_mask | priv->ue_set_mask);
  125. /* Error are injected by writing a word while the SBE or DBE
  126. * bit in the CTLCFG register is set. Reading the word will
  127. * trigger the SBE or DBE error and the corresponding IRQ.
  128. */
  129. if (count == 3) {
  130. edac_printk(KERN_ALERT, EDAC_MC,
  131. "Inject Double bit error\n");
  132. regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
  133. (read_reg | priv->ue_set_mask));
  134. } else {
  135. edac_printk(KERN_ALERT, EDAC_MC,
  136. "Inject Single bit error\n");
  137. regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset,
  138. (read_reg | priv->ce_set_mask));
  139. }
  140. ptemp[0] = 0x5A5A5A5A;
  141. ptemp[1] = 0xA5A5A5A5;
  142. /* Clear the error injection bits */
  143. regmap_write(drvdata->mc_vbase, priv->ce_ue_trgr_offset, read_reg);
  144. /* Ensure it has been written out */
  145. wmb();
  146. /*
  147. * To trigger the error, we need to read the data back
  148. * (the data was written with errors above).
  149. * The ACCESS_ONCE macros and printk are used to prevent the
  150. * the compiler optimizing these reads out.
  151. */
  152. reg = ACCESS_ONCE(ptemp[0]);
  153. read_reg = ACCESS_ONCE(ptemp[1]);
  154. /* Force Read */
  155. rmb();
  156. edac_printk(KERN_ALERT, EDAC_MC, "Read Data [0x%X, 0x%X]\n",
  157. reg, read_reg);
  158. dma_free_coherent(mci->pdev, 16, ptemp, dma_handle);
  159. return count;
  160. }
  161. static const struct file_operations altr_sdr_mc_debug_inject_fops = {
  162. .open = simple_open,
  163. .write = altr_sdr_mc_err_inject_write,
  164. .llseek = generic_file_llseek,
  165. };
  166. static void altr_sdr_mc_create_debugfs_nodes(struct mem_ctl_info *mci)
  167. {
  168. if (!IS_ENABLED(CONFIG_EDAC_DEBUG))
  169. return;
  170. if (!mci->debugfs)
  171. return;
  172. edac_debugfs_create_file("inject_ctrl", S_IWUSR, mci->debugfs, mci,
  173. &altr_sdr_mc_debug_inject_fops);
  174. }
  175. /* Get total memory size from Open Firmware DTB */
  176. static unsigned long get_total_mem(void)
  177. {
  178. struct device_node *np = NULL;
  179. const unsigned int *reg, *reg_end;
  180. int len, sw, aw;
  181. unsigned long start, size, total_mem = 0;
  182. for_each_node_by_type(np, "memory") {
  183. aw = of_n_addr_cells(np);
  184. sw = of_n_size_cells(np);
  185. reg = (const unsigned int *)of_get_property(np, "reg", &len);
  186. reg_end = reg + (len / sizeof(u32));
  187. total_mem = 0;
  188. do {
  189. start = of_read_number(reg, aw);
  190. reg += aw;
  191. size = of_read_number(reg, sw);
  192. reg += sw;
  193. total_mem += size;
  194. } while (reg < reg_end);
  195. }
  196. edac_dbg(0, "total_mem 0x%lx\n", total_mem);
  197. return total_mem;
  198. }
  199. static const struct of_device_id altr_sdram_ctrl_of_match[] = {
  200. { .compatible = "altr,sdram-edac", .data = (void *)&c5_data},
  201. { .compatible = "altr,sdram-edac-a10", .data = (void *)&a10_data},
  202. {},
  203. };
  204. MODULE_DEVICE_TABLE(of, altr_sdram_ctrl_of_match);
  205. static int a10_init(struct regmap *mc_vbase)
  206. {
  207. if (regmap_update_bits(mc_vbase, A10_INTMODE_OFST,
  208. A10_INTMODE_SB_INT, A10_INTMODE_SB_INT)) {
  209. edac_printk(KERN_ERR, EDAC_MC,
  210. "Error setting SB IRQ mode\n");
  211. return -ENODEV;
  212. }
  213. if (regmap_write(mc_vbase, A10_SERRCNTREG_OFST, 1)) {
  214. edac_printk(KERN_ERR, EDAC_MC,
  215. "Error setting trigger count\n");
  216. return -ENODEV;
  217. }
  218. return 0;
  219. }
  220. static int a10_unmask_irq(struct platform_device *pdev, u32 mask)
  221. {
  222. void __iomem *sm_base;
  223. int ret = 0;
  224. if (!request_mem_region(A10_SYMAN_INTMASK_CLR, sizeof(u32),
  225. dev_name(&pdev->dev))) {
  226. edac_printk(KERN_ERR, EDAC_MC,
  227. "Unable to request mem region\n");
  228. return -EBUSY;
  229. }
  230. sm_base = ioremap(A10_SYMAN_INTMASK_CLR, sizeof(u32));
  231. if (!sm_base) {
  232. edac_printk(KERN_ERR, EDAC_MC,
  233. "Unable to ioremap device\n");
  234. ret = -ENOMEM;
  235. goto release;
  236. }
  237. iowrite32(mask, sm_base);
  238. iounmap(sm_base);
  239. release:
  240. release_mem_region(A10_SYMAN_INTMASK_CLR, sizeof(u32));
  241. return ret;
  242. }
  243. static int altr_sdram_probe(struct platform_device *pdev)
  244. {
  245. const struct of_device_id *id;
  246. struct edac_mc_layer layers[2];
  247. struct mem_ctl_info *mci;
  248. struct altr_sdram_mc_data *drvdata;
  249. const struct altr_sdram_prv_data *priv;
  250. struct regmap *mc_vbase;
  251. struct dimm_info *dimm;
  252. u32 read_reg;
  253. int irq, irq2, res = 0;
  254. unsigned long mem_size, irqflags = 0;
  255. id = of_match_device(altr_sdram_ctrl_of_match, &pdev->dev);
  256. if (!id)
  257. return -ENODEV;
  258. /* Grab the register range from the sdr controller in device tree */
  259. mc_vbase = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  260. "altr,sdr-syscon");
  261. if (IS_ERR(mc_vbase)) {
  262. edac_printk(KERN_ERR, EDAC_MC,
  263. "regmap for altr,sdr-syscon lookup failed.\n");
  264. return -ENODEV;
  265. }
  266. /* Check specific dependencies for the module */
  267. priv = of_match_node(altr_sdram_ctrl_of_match,
  268. pdev->dev.of_node)->data;
  269. /* Validate the SDRAM controller has ECC enabled */
  270. if (regmap_read(mc_vbase, priv->ecc_ctrl_offset, &read_reg) ||
  271. ((read_reg & priv->ecc_ctl_en_mask) != priv->ecc_ctl_en_mask)) {
  272. edac_printk(KERN_ERR, EDAC_MC,
  273. "No ECC/ECC disabled [0x%08X]\n", read_reg);
  274. return -ENODEV;
  275. }
  276. /* Grab memory size from device tree. */
  277. mem_size = get_total_mem();
  278. if (!mem_size) {
  279. edac_printk(KERN_ERR, EDAC_MC, "Unable to calculate memory size\n");
  280. return -ENODEV;
  281. }
  282. /* Ensure the SDRAM Interrupt is disabled */
  283. if (regmap_update_bits(mc_vbase, priv->ecc_irq_en_offset,
  284. priv->ecc_irq_en_mask, 0)) {
  285. edac_printk(KERN_ERR, EDAC_MC,
  286. "Error disabling SDRAM ECC IRQ\n");
  287. return -ENODEV;
  288. }
  289. /* Toggle to clear the SDRAM Error count */
  290. if (regmap_update_bits(mc_vbase, priv->ecc_cnt_rst_offset,
  291. priv->ecc_cnt_rst_mask,
  292. priv->ecc_cnt_rst_mask)) {
  293. edac_printk(KERN_ERR, EDAC_MC,
  294. "Error clearing SDRAM ECC count\n");
  295. return -ENODEV;
  296. }
  297. if (regmap_update_bits(mc_vbase, priv->ecc_cnt_rst_offset,
  298. priv->ecc_cnt_rst_mask, 0)) {
  299. edac_printk(KERN_ERR, EDAC_MC,
  300. "Error clearing SDRAM ECC count\n");
  301. return -ENODEV;
  302. }
  303. irq = platform_get_irq(pdev, 0);
  304. if (irq < 0) {
  305. edac_printk(KERN_ERR, EDAC_MC,
  306. "No irq %d in DT\n", irq);
  307. return -ENODEV;
  308. }
  309. /* Arria10 has a 2nd IRQ */
  310. irq2 = platform_get_irq(pdev, 1);
  311. layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
  312. layers[0].size = 1;
  313. layers[0].is_virt_csrow = true;
  314. layers[1].type = EDAC_MC_LAYER_CHANNEL;
  315. layers[1].size = 1;
  316. layers[1].is_virt_csrow = false;
  317. mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers,
  318. sizeof(struct altr_sdram_mc_data));
  319. if (!mci)
  320. return -ENOMEM;
  321. mci->pdev = &pdev->dev;
  322. drvdata = mci->pvt_info;
  323. drvdata->mc_vbase = mc_vbase;
  324. drvdata->data = priv;
  325. platform_set_drvdata(pdev, mci);
  326. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
  327. edac_printk(KERN_ERR, EDAC_MC,
  328. "Unable to get managed device resource\n");
  329. res = -ENOMEM;
  330. goto free;
  331. }
  332. mci->mtype_cap = MEM_FLAG_DDR3;
  333. mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_SECDED;
  334. mci->edac_cap = EDAC_FLAG_SECDED;
  335. mci->mod_name = EDAC_MOD_STR;
  336. mci->mod_ver = EDAC_VERSION;
  337. mci->ctl_name = dev_name(&pdev->dev);
  338. mci->scrub_mode = SCRUB_SW_SRC;
  339. mci->dev_name = dev_name(&pdev->dev);
  340. dimm = *mci->dimms;
  341. dimm->nr_pages = ((mem_size - 1) >> PAGE_SHIFT) + 1;
  342. dimm->grain = 8;
  343. dimm->dtype = DEV_X8;
  344. dimm->mtype = MEM_DDR3;
  345. dimm->edac_mode = EDAC_SECDED;
  346. res = edac_mc_add_mc(mci);
  347. if (res < 0)
  348. goto err;
  349. /* Only the Arria10 has separate IRQs */
  350. if (irq2 > 0) {
  351. /* Arria10 specific initialization */
  352. res = a10_init(mc_vbase);
  353. if (res < 0)
  354. goto err2;
  355. res = devm_request_irq(&pdev->dev, irq2,
  356. altr_sdram_mc_err_handler,
  357. IRQF_SHARED, dev_name(&pdev->dev), mci);
  358. if (res < 0) {
  359. edac_mc_printk(mci, KERN_ERR,
  360. "Unable to request irq %d\n", irq2);
  361. res = -ENODEV;
  362. goto err2;
  363. }
  364. res = a10_unmask_irq(pdev, A10_DDR0_IRQ_MASK);
  365. if (res < 0)
  366. goto err2;
  367. irqflags = IRQF_SHARED;
  368. }
  369. res = devm_request_irq(&pdev->dev, irq, altr_sdram_mc_err_handler,
  370. irqflags, dev_name(&pdev->dev), mci);
  371. if (res < 0) {
  372. edac_mc_printk(mci, KERN_ERR,
  373. "Unable to request irq %d\n", irq);
  374. res = -ENODEV;
  375. goto err2;
  376. }
  377. /* Infrastructure ready - enable the IRQ */
  378. if (regmap_update_bits(drvdata->mc_vbase, priv->ecc_irq_en_offset,
  379. priv->ecc_irq_en_mask, priv->ecc_irq_en_mask)) {
  380. edac_mc_printk(mci, KERN_ERR,
  381. "Error enabling SDRAM ECC IRQ\n");
  382. res = -ENODEV;
  383. goto err2;
  384. }
  385. altr_sdr_mc_create_debugfs_nodes(mci);
  386. devres_close_group(&pdev->dev, NULL);
  387. return 0;
  388. err2:
  389. edac_mc_del_mc(&pdev->dev);
  390. err:
  391. devres_release_group(&pdev->dev, NULL);
  392. free:
  393. edac_mc_free(mci);
  394. edac_printk(KERN_ERR, EDAC_MC,
  395. "EDAC Probe Failed; Error %d\n", res);
  396. return res;
  397. }
  398. static int altr_sdram_remove(struct platform_device *pdev)
  399. {
  400. struct mem_ctl_info *mci = platform_get_drvdata(pdev);
  401. edac_mc_del_mc(&pdev->dev);
  402. edac_mc_free(mci);
  403. platform_set_drvdata(pdev, NULL);
  404. return 0;
  405. }
  406. /*
  407. * If you want to suspend, need to disable EDAC by removing it
  408. * from the device tree or defconfig.
  409. */
  410. #ifdef CONFIG_PM
  411. static int altr_sdram_prepare(struct device *dev)
  412. {
  413. pr_err("Suspend not allowed when EDAC is enabled.\n");
  414. return -EPERM;
  415. }
  416. static const struct dev_pm_ops altr_sdram_pm_ops = {
  417. .prepare = altr_sdram_prepare,
  418. };
  419. #endif
  420. static struct platform_driver altr_sdram_edac_driver = {
  421. .probe = altr_sdram_probe,
  422. .remove = altr_sdram_remove,
  423. .driver = {
  424. .name = "altr_sdram_edac",
  425. #ifdef CONFIG_PM
  426. .pm = &altr_sdram_pm_ops,
  427. #endif
  428. .of_match_table = altr_sdram_ctrl_of_match,
  429. },
  430. };
  431. module_platform_driver(altr_sdram_edac_driver);
  432. MODULE_LICENSE("GPL v2");
  433. MODULE_AUTHOR("Thor Thayer");
  434. MODULE_DESCRIPTION("EDAC Driver for Altera SDRAM Controller");