highbank_mc_edac.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright 2011-2012 Calxeda, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/ctype.h>
  19. #include <linux/edac.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/uaccess.h>
  24. #include "edac_core.h"
  25. #include "edac_module.h"
  26. /* DDR Ctrlr Error Registers */
  27. #define HB_DDR_ECC_OPT 0x128
  28. #define HB_DDR_ECC_U_ERR_ADDR 0x130
  29. #define HB_DDR_ECC_U_ERR_STAT 0x134
  30. #define HB_DDR_ECC_U_ERR_DATAL 0x138
  31. #define HB_DDR_ECC_U_ERR_DATAH 0x13c
  32. #define HB_DDR_ECC_C_ERR_ADDR 0x140
  33. #define HB_DDR_ECC_C_ERR_STAT 0x144
  34. #define HB_DDR_ECC_C_ERR_DATAL 0x148
  35. #define HB_DDR_ECC_C_ERR_DATAH 0x14c
  36. #define HB_DDR_ECC_INT_STATUS 0x180
  37. #define HB_DDR_ECC_INT_ACK 0x184
  38. #define HB_DDR_ECC_U_ERR_ID 0x424
  39. #define HB_DDR_ECC_C_ERR_ID 0x428
  40. #define HB_DDR_ECC_INT_STAT_CE 0x8
  41. #define HB_DDR_ECC_INT_STAT_DOUBLE_CE 0x10
  42. #define HB_DDR_ECC_INT_STAT_UE 0x20
  43. #define HB_DDR_ECC_INT_STAT_DOUBLE_UE 0x40
  44. #define HB_DDR_ECC_OPT_MODE_MASK 0x3
  45. #define HB_DDR_ECC_OPT_FWC 0x100
  46. #define HB_DDR_ECC_OPT_XOR_SHIFT 16
  47. struct hb_mc_drvdata {
  48. void __iomem *mc_vbase;
  49. };
  50. static irqreturn_t highbank_mc_err_handler(int irq, void *dev_id)
  51. {
  52. struct mem_ctl_info *mci = dev_id;
  53. struct hb_mc_drvdata *drvdata = mci->pvt_info;
  54. u32 status, err_addr;
  55. /* Read the interrupt status register */
  56. status = readl(drvdata->mc_vbase + HB_DDR_ECC_INT_STATUS);
  57. if (status & HB_DDR_ECC_INT_STAT_UE) {
  58. err_addr = readl(drvdata->mc_vbase + HB_DDR_ECC_U_ERR_ADDR);
  59. edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1,
  60. err_addr >> PAGE_SHIFT,
  61. err_addr & ~PAGE_MASK, 0,
  62. 0, 0, -1,
  63. mci->ctl_name, "");
  64. }
  65. if (status & HB_DDR_ECC_INT_STAT_CE) {
  66. u32 syndrome = readl(drvdata->mc_vbase + HB_DDR_ECC_C_ERR_STAT);
  67. syndrome = (syndrome >> 8) & 0xff;
  68. err_addr = readl(drvdata->mc_vbase + HB_DDR_ECC_C_ERR_ADDR);
  69. edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1,
  70. err_addr >> PAGE_SHIFT,
  71. err_addr & ~PAGE_MASK, syndrome,
  72. 0, 0, -1,
  73. mci->ctl_name, "");
  74. }
  75. /* clear the error, clears the interrupt */
  76. writel(status, drvdata->mc_vbase + HB_DDR_ECC_INT_ACK);
  77. return IRQ_HANDLED;
  78. }
  79. #ifdef CONFIG_EDAC_DEBUG
  80. static ssize_t highbank_mc_err_inject_write(struct file *file,
  81. const char __user *data,
  82. size_t count, loff_t *ppos)
  83. {
  84. struct mem_ctl_info *mci = file->private_data;
  85. struct hb_mc_drvdata *pdata = mci->pvt_info;
  86. char buf[32];
  87. size_t buf_size;
  88. u32 reg;
  89. u8 synd;
  90. buf_size = min(count, (sizeof(buf)-1));
  91. if (copy_from_user(buf, data, buf_size))
  92. return -EFAULT;
  93. buf[buf_size] = 0;
  94. if (!kstrtou8(buf, 16, &synd)) {
  95. reg = readl(pdata->mc_vbase + HB_DDR_ECC_OPT);
  96. reg &= HB_DDR_ECC_OPT_MODE_MASK;
  97. reg |= (synd << HB_DDR_ECC_OPT_XOR_SHIFT) | HB_DDR_ECC_OPT_FWC;
  98. writel(reg, pdata->mc_vbase + HB_DDR_ECC_OPT);
  99. }
  100. return count;
  101. }
  102. static const struct file_operations highbank_mc_debug_inject_fops = {
  103. .open = simple_open,
  104. .write = highbank_mc_err_inject_write,
  105. .llseek = generic_file_llseek,
  106. };
  107. static void highbank_mc_create_debugfs_nodes(struct mem_ctl_info *mci)
  108. {
  109. if (mci->debugfs)
  110. debugfs_create_file("inject_ctrl", S_IWUSR, mci->debugfs, mci,
  111. &highbank_mc_debug_inject_fops);
  112. ;
  113. }
  114. #else
  115. static void highbank_mc_create_debugfs_nodes(struct mem_ctl_info *mci)
  116. {}
  117. #endif
  118. static int highbank_mc_probe(struct platform_device *pdev)
  119. {
  120. struct edac_mc_layer layers[2];
  121. struct mem_ctl_info *mci;
  122. struct hb_mc_drvdata *drvdata;
  123. struct dimm_info *dimm;
  124. struct resource *r;
  125. u32 control;
  126. int irq;
  127. int res = 0;
  128. layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
  129. layers[0].size = 1;
  130. layers[0].is_virt_csrow = true;
  131. layers[1].type = EDAC_MC_LAYER_CHANNEL;
  132. layers[1].size = 1;
  133. layers[1].is_virt_csrow = false;
  134. mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers,
  135. sizeof(struct hb_mc_drvdata));
  136. if (!mci)
  137. return -ENOMEM;
  138. mci->pdev = &pdev->dev;
  139. drvdata = mci->pvt_info;
  140. platform_set_drvdata(pdev, mci);
  141. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
  142. return -ENOMEM;
  143. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  144. if (!r) {
  145. dev_err(&pdev->dev, "Unable to get mem resource\n");
  146. res = -ENODEV;
  147. goto err;
  148. }
  149. if (!devm_request_mem_region(&pdev->dev, r->start,
  150. resource_size(r), dev_name(&pdev->dev))) {
  151. dev_err(&pdev->dev, "Error while requesting mem region\n");
  152. res = -EBUSY;
  153. goto err;
  154. }
  155. drvdata->mc_vbase = devm_ioremap(&pdev->dev,
  156. r->start, resource_size(r));
  157. if (!drvdata->mc_vbase) {
  158. dev_err(&pdev->dev, "Unable to map regs\n");
  159. res = -ENOMEM;
  160. goto err;
  161. }
  162. control = readl(drvdata->mc_vbase + HB_DDR_ECC_OPT) & 0x3;
  163. if (!control || (control == 0x2)) {
  164. dev_err(&pdev->dev, "No ECC present, or ECC disabled\n");
  165. res = -ENODEV;
  166. goto err;
  167. }
  168. irq = platform_get_irq(pdev, 0);
  169. res = devm_request_irq(&pdev->dev, irq, highbank_mc_err_handler,
  170. 0, dev_name(&pdev->dev), mci);
  171. if (res < 0) {
  172. dev_err(&pdev->dev, "Unable to request irq %d\n", irq);
  173. goto err;
  174. }
  175. mci->mtype_cap = MEM_FLAG_DDR3;
  176. mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_SECDED;
  177. mci->edac_cap = EDAC_FLAG_SECDED;
  178. mci->mod_name = dev_name(&pdev->dev);
  179. mci->mod_ver = "1";
  180. mci->ctl_name = dev_name(&pdev->dev);
  181. mci->scrub_mode = SCRUB_SW_SRC;
  182. /* Only a single 4GB DIMM is supported */
  183. dimm = *mci->dimms;
  184. dimm->nr_pages = (~0UL >> PAGE_SHIFT) + 1;
  185. dimm->grain = 8;
  186. dimm->dtype = DEV_X8;
  187. dimm->mtype = MEM_DDR3;
  188. dimm->edac_mode = EDAC_SECDED;
  189. res = edac_mc_add_mc(mci);
  190. if (res < 0)
  191. goto err;
  192. highbank_mc_create_debugfs_nodes(mci);
  193. devres_close_group(&pdev->dev, NULL);
  194. return 0;
  195. err:
  196. devres_release_group(&pdev->dev, NULL);
  197. edac_mc_free(mci);
  198. return res;
  199. }
  200. static int highbank_mc_remove(struct platform_device *pdev)
  201. {
  202. struct mem_ctl_info *mci = platform_get_drvdata(pdev);
  203. edac_mc_del_mc(&pdev->dev);
  204. edac_mc_free(mci);
  205. return 0;
  206. }
  207. static const struct of_device_id hb_ddr_ctrl_of_match[] = {
  208. { .compatible = "calxeda,hb-ddr-ctrl", },
  209. {},
  210. };
  211. MODULE_DEVICE_TABLE(of, hb_ddr_ctrl_of_match);
  212. static struct platform_driver highbank_mc_edac_driver = {
  213. .probe = highbank_mc_probe,
  214. .remove = highbank_mc_remove,
  215. .driver = {
  216. .name = "hb_mc_edac",
  217. .of_match_table = hb_ddr_ctrl_of_match,
  218. },
  219. };
  220. module_platform_driver(highbank_mc_edac_driver);
  221. MODULE_LICENSE("GPL v2");
  222. MODULE_AUTHOR("Calxeda, Inc.");
  223. MODULE_DESCRIPTION("EDAC Driver for Calxeda Highbank");