highbank_mc_edac.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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_module.h"
  25. /* DDR Ctrlr Error Registers */
  26. #define HB_DDR_ECC_ERR_BASE 0x128
  27. #define MW_DDR_ECC_ERR_BASE 0x1b4
  28. #define HB_DDR_ECC_OPT 0x00
  29. #define HB_DDR_ECC_U_ERR_ADDR 0x08
  30. #define HB_DDR_ECC_U_ERR_STAT 0x0c
  31. #define HB_DDR_ECC_U_ERR_DATAL 0x10
  32. #define HB_DDR_ECC_U_ERR_DATAH 0x14
  33. #define HB_DDR_ECC_C_ERR_ADDR 0x18
  34. #define HB_DDR_ECC_C_ERR_STAT 0x1c
  35. #define HB_DDR_ECC_C_ERR_DATAL 0x20
  36. #define HB_DDR_ECC_C_ERR_DATAH 0x24
  37. #define HB_DDR_ECC_OPT_MODE_MASK 0x3
  38. #define HB_DDR_ECC_OPT_FWC 0x100
  39. #define HB_DDR_ECC_OPT_XOR_SHIFT 16
  40. /* DDR Ctrlr Interrupt Registers */
  41. #define HB_DDR_ECC_INT_BASE 0x180
  42. #define MW_DDR_ECC_INT_BASE 0x218
  43. #define HB_DDR_ECC_INT_STATUS 0x00
  44. #define HB_DDR_ECC_INT_ACK 0x04
  45. #define HB_DDR_ECC_INT_STAT_CE 0x8
  46. #define HB_DDR_ECC_INT_STAT_DOUBLE_CE 0x10
  47. #define HB_DDR_ECC_INT_STAT_UE 0x20
  48. #define HB_DDR_ECC_INT_STAT_DOUBLE_UE 0x40
  49. struct hb_mc_drvdata {
  50. void __iomem *mc_err_base;
  51. void __iomem *mc_int_base;
  52. };
  53. static irqreturn_t highbank_mc_err_handler(int irq, void *dev_id)
  54. {
  55. struct mem_ctl_info *mci = dev_id;
  56. struct hb_mc_drvdata *drvdata = mci->pvt_info;
  57. u32 status, err_addr;
  58. /* Read the interrupt status register */
  59. status = readl(drvdata->mc_int_base + HB_DDR_ECC_INT_STATUS);
  60. if (status & HB_DDR_ECC_INT_STAT_UE) {
  61. err_addr = readl(drvdata->mc_err_base + HB_DDR_ECC_U_ERR_ADDR);
  62. edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1,
  63. err_addr >> PAGE_SHIFT,
  64. err_addr & ~PAGE_MASK, 0,
  65. 0, 0, -1,
  66. mci->ctl_name, "");
  67. }
  68. if (status & HB_DDR_ECC_INT_STAT_CE) {
  69. u32 syndrome = readl(drvdata->mc_err_base + HB_DDR_ECC_C_ERR_STAT);
  70. syndrome = (syndrome >> 8) & 0xff;
  71. err_addr = readl(drvdata->mc_err_base + HB_DDR_ECC_C_ERR_ADDR);
  72. edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1,
  73. err_addr >> PAGE_SHIFT,
  74. err_addr & ~PAGE_MASK, syndrome,
  75. 0, 0, -1,
  76. mci->ctl_name, "");
  77. }
  78. /* clear the error, clears the interrupt */
  79. writel(status, drvdata->mc_int_base + HB_DDR_ECC_INT_ACK);
  80. return IRQ_HANDLED;
  81. }
  82. static void highbank_mc_err_inject(struct mem_ctl_info *mci, u8 synd)
  83. {
  84. struct hb_mc_drvdata *pdata = mci->pvt_info;
  85. u32 reg;
  86. reg = readl(pdata->mc_err_base + HB_DDR_ECC_OPT);
  87. reg &= HB_DDR_ECC_OPT_MODE_MASK;
  88. reg |= (synd << HB_DDR_ECC_OPT_XOR_SHIFT) | HB_DDR_ECC_OPT_FWC;
  89. writel(reg, pdata->mc_err_base + HB_DDR_ECC_OPT);
  90. }
  91. #define to_mci(k) container_of(k, struct mem_ctl_info, dev)
  92. static ssize_t highbank_mc_inject_ctrl(struct device *dev,
  93. struct device_attribute *attr, const char *buf, size_t count)
  94. {
  95. struct mem_ctl_info *mci = to_mci(dev);
  96. u8 synd;
  97. if (kstrtou8(buf, 16, &synd))
  98. return -EINVAL;
  99. highbank_mc_err_inject(mci, synd);
  100. return count;
  101. }
  102. static DEVICE_ATTR(inject_ctrl, S_IWUSR, NULL, highbank_mc_inject_ctrl);
  103. static struct attribute *highbank_dev_attrs[] = {
  104. &dev_attr_inject_ctrl.attr,
  105. NULL
  106. };
  107. ATTRIBUTE_GROUPS(highbank_dev);
  108. struct hb_mc_settings {
  109. int err_offset;
  110. int int_offset;
  111. };
  112. static struct hb_mc_settings hb_settings = {
  113. .err_offset = HB_DDR_ECC_ERR_BASE,
  114. .int_offset = HB_DDR_ECC_INT_BASE,
  115. };
  116. static struct hb_mc_settings mw_settings = {
  117. .err_offset = MW_DDR_ECC_ERR_BASE,
  118. .int_offset = MW_DDR_ECC_INT_BASE,
  119. };
  120. static const struct of_device_id hb_ddr_ctrl_of_match[] = {
  121. { .compatible = "calxeda,hb-ddr-ctrl", .data = &hb_settings },
  122. { .compatible = "calxeda,ecx-2000-ddr-ctrl", .data = &mw_settings },
  123. {},
  124. };
  125. MODULE_DEVICE_TABLE(of, hb_ddr_ctrl_of_match);
  126. static int highbank_mc_probe(struct platform_device *pdev)
  127. {
  128. const struct of_device_id *id;
  129. const struct hb_mc_settings *settings;
  130. struct edac_mc_layer layers[2];
  131. struct mem_ctl_info *mci;
  132. struct hb_mc_drvdata *drvdata;
  133. struct dimm_info *dimm;
  134. struct resource *r;
  135. void __iomem *base;
  136. u32 control;
  137. int irq;
  138. int res = 0;
  139. id = of_match_device(hb_ddr_ctrl_of_match, &pdev->dev);
  140. if (!id)
  141. return -ENODEV;
  142. layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
  143. layers[0].size = 1;
  144. layers[0].is_virt_csrow = true;
  145. layers[1].type = EDAC_MC_LAYER_CHANNEL;
  146. layers[1].size = 1;
  147. layers[1].is_virt_csrow = false;
  148. mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers,
  149. sizeof(struct hb_mc_drvdata));
  150. if (!mci)
  151. return -ENOMEM;
  152. mci->pdev = &pdev->dev;
  153. drvdata = mci->pvt_info;
  154. platform_set_drvdata(pdev, mci);
  155. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
  156. return -ENOMEM;
  157. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  158. if (!r) {
  159. dev_err(&pdev->dev, "Unable to get mem resource\n");
  160. res = -ENODEV;
  161. goto err;
  162. }
  163. if (!devm_request_mem_region(&pdev->dev, r->start,
  164. resource_size(r), dev_name(&pdev->dev))) {
  165. dev_err(&pdev->dev, "Error while requesting mem region\n");
  166. res = -EBUSY;
  167. goto err;
  168. }
  169. base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  170. if (!base) {
  171. dev_err(&pdev->dev, "Unable to map regs\n");
  172. res = -ENOMEM;
  173. goto err;
  174. }
  175. settings = id->data;
  176. drvdata->mc_err_base = base + settings->err_offset;
  177. drvdata->mc_int_base = base + settings->int_offset;
  178. control = readl(drvdata->mc_err_base + HB_DDR_ECC_OPT) & 0x3;
  179. if (!control || (control == 0x2)) {
  180. dev_err(&pdev->dev, "No ECC present, or ECC disabled\n");
  181. res = -ENODEV;
  182. goto err;
  183. }
  184. mci->mtype_cap = MEM_FLAG_DDR3;
  185. mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_SECDED;
  186. mci->edac_cap = EDAC_FLAG_SECDED;
  187. mci->mod_name = pdev->dev.driver->name;
  188. mci->ctl_name = id->compatible;
  189. mci->dev_name = dev_name(&pdev->dev);
  190. mci->scrub_mode = SCRUB_SW_SRC;
  191. /* Only a single 4GB DIMM is supported */
  192. dimm = *mci->dimms;
  193. dimm->nr_pages = (~0UL >> PAGE_SHIFT) + 1;
  194. dimm->grain = 8;
  195. dimm->dtype = DEV_X8;
  196. dimm->mtype = MEM_DDR3;
  197. dimm->edac_mode = EDAC_SECDED;
  198. res = edac_mc_add_mc_with_groups(mci, highbank_dev_groups);
  199. if (res < 0)
  200. goto err;
  201. irq = platform_get_irq(pdev, 0);
  202. res = devm_request_irq(&pdev->dev, irq, highbank_mc_err_handler,
  203. 0, dev_name(&pdev->dev), mci);
  204. if (res < 0) {
  205. dev_err(&pdev->dev, "Unable to request irq %d\n", irq);
  206. goto err2;
  207. }
  208. devres_close_group(&pdev->dev, NULL);
  209. return 0;
  210. err2:
  211. edac_mc_del_mc(&pdev->dev);
  212. err:
  213. devres_release_group(&pdev->dev, NULL);
  214. edac_mc_free(mci);
  215. return res;
  216. }
  217. static int highbank_mc_remove(struct platform_device *pdev)
  218. {
  219. struct mem_ctl_info *mci = platform_get_drvdata(pdev);
  220. edac_mc_del_mc(&pdev->dev);
  221. edac_mc_free(mci);
  222. return 0;
  223. }
  224. static struct platform_driver highbank_mc_edac_driver = {
  225. .probe = highbank_mc_probe,
  226. .remove = highbank_mc_remove,
  227. .driver = {
  228. .name = "hb_mc_edac",
  229. .of_match_table = hb_ddr_ctrl_of_match,
  230. },
  231. };
  232. module_platform_driver(highbank_mc_edac_driver);
  233. MODULE_LICENSE("GPL v2");
  234. MODULE_AUTHOR("Calxeda, Inc.");
  235. MODULE_DESCRIPTION("EDAC Driver for Calxeda Highbank");