ti_mc_edac.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
  3. *
  4. * Texas Instruments DDR3 MC ECC error detection driver
  5. *
  6. * TODO: Need to investigate how to hook this to the edac core driver.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/io.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_device.h>
  25. #include <linux/module.h>
  26. /* DDR3 controller registers */
  27. #define DDR3_EOI 0x0A0
  28. #define DDR3_IRQ_STATUS_RAW_SYS 0x0A4
  29. #define DDR3_IRQ_STATUS_SYS 0x0AC
  30. #define DDR3_IRQ_ENABLE_SET_SYS 0x0B4
  31. #define DDR3_IRQ_ENABLE_CLR_SYS 0x0BC
  32. #define DDR3_ECC_CTRL 0x110
  33. #define DDR3_ONE_BIT_ECC_ERR_CNT 0x130
  34. #define TWO_BIT_ECC_ERR_ADDR_LOG 0x140
  35. #define DDR3_1B_ECC_ERR BIT(5)
  36. #define DDR3_2B_ECC_ERR BIT(4)
  37. #define DDR3_WR_ECC_ERR BIT(3)
  38. #define DDR3_SYS_ERR BIT(0)
  39. /* Bit 31 enables ECC and 28 enables RMW */
  40. #define ECC_ENABLED (BIT(31) | BIT(28))
  41. static void ti_mc_ddr3_ecc_check(void __iomem *reg)
  42. {
  43. u32 irq_status;
  44. irq_status = readl(reg + DDR3_IRQ_STATUS_SYS);
  45. if ((irq_status & DDR3_2B_ECC_ERR) ||
  46. (irq_status & DDR3_WR_ECC_ERR)) {
  47. /*
  48. * Do a kernel panic as this is double bit ECC error
  49. * or ECC write that are fatal
  50. */
  51. if (irq_status & DDR3_2B_ECC_ERR)
  52. panic("UC DDR3 ECC err, irq stats 0x%x, addr 0x%x..\n",
  53. irq_status,
  54. readl(reg + TWO_BIT_ECC_ERR_ADDR_LOG));
  55. else
  56. panic("UC DDR3 ECC err, irq stats 0x%x..\n",
  57. irq_status);
  58. }
  59. }
  60. static irqreturn_t ti_mc_ddr3_ecc_isr(int irq, void *reg_virt)
  61. {
  62. void __iomem *reg = (void __iomem *)reg_virt;
  63. ti_mc_ddr3_ecc_check(reg);
  64. /*
  65. * Other errors should be handled by hardware
  66. * So, nothing to do here. For now it never reaches here
  67. * as panic will be triggerred for ECC errors
  68. */
  69. return IRQ_HANDLED;
  70. }
  71. static const struct of_device_id ti_mc_ddr3_ecc_of_match[] = {
  72. {.compatible = "ti,keystone-ddr3-mc-edac", },
  73. {.compatible = "ti,dra7-ddr3-mc-edac", },
  74. {},
  75. };
  76. static int ti_mc_ddr3_ecc_probe(struct platform_device *pdev)
  77. {
  78. int error_irq = 0, ret = -ENODEV;
  79. struct device *dev = &pdev->dev;
  80. struct resource *res;
  81. void __iomem *reg;
  82. u32 val;
  83. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  84. reg = devm_ioremap_resource(dev, res);
  85. if (IS_ERR(reg)) {
  86. dev_err(dev, "DDR3 controller regs not defined\n");
  87. return PTR_ERR(reg);
  88. }
  89. /* Check if ECC is enabled. If not, just return */
  90. val = readl(reg + DDR3_ECC_CTRL);
  91. if (!(val & ECC_ENABLED)) {
  92. dev_info(dev, "ECC is not enabled, disable edac\n");
  93. return ret;
  94. }
  95. /* disable and clear unused ECC interrupts */
  96. writel(DDR3_1B_ECC_ERR | DDR3_SYS_ERR, reg + DDR3_IRQ_ENABLE_CLR_SYS);
  97. writel(DDR3_1B_ECC_ERR | DDR3_SYS_ERR, reg + DDR3_IRQ_STATUS_SYS);
  98. /* check if we already have unrecoverable errors */
  99. ti_mc_ddr3_ecc_check(reg);
  100. writel(DDR3_2B_ECC_ERR | DDR3_WR_ECC_ERR,
  101. reg + DDR3_IRQ_ENABLE_CLR_SYS);
  102. /* add DDR3 ECC error handler */
  103. error_irq = platform_get_irq(pdev, 0);
  104. if (!error_irq) {
  105. dev_err(dev, "DDR3 ECC irq number not defined\n");
  106. return ret;
  107. }
  108. ret = devm_request_irq(dev, error_irq, ti_mc_ddr3_ecc_isr, 0,
  109. "ddr3-ecc-err-irq", (void *)reg);
  110. if (ret) {
  111. dev_err(dev, "request_irq fail for DDR3 ECC error irq\n");
  112. return ret;
  113. }
  114. writel(DDR3_2B_ECC_ERR | DDR3_WR_ECC_ERR,
  115. reg + DDR3_IRQ_ENABLE_SET_SYS);
  116. return ret;
  117. }
  118. static struct platform_driver ti_mc_ddr3_ecc_driver = {
  119. .probe = ti_mc_ddr3_ecc_probe,
  120. .driver = {
  121. .name = "ti_mc_ddr3_ecc",
  122. .of_match_table = ti_mc_ddr3_ecc_of_match,
  123. },
  124. };
  125. static int __init ti_mc_ddr3_ecc_init(void)
  126. {
  127. int ret = 0;
  128. ret = platform_driver_register(&ti_mc_ddr3_ecc_driver);
  129. if (ret)
  130. pr_warn("TI DDR3 DDR3 ecc_init failed\n");
  131. return ret;
  132. }
  133. subsys_initcall(ti_mc_ddr3_ecc_init);
  134. MODULE_AUTHOR("Texas Instruments Inc.");
  135. MODULE_DESCRIPTION("EDAC Driver for Texas Instruments DDR3 MC");
  136. MODULE_LICENSE("GPL v2");