bt1-axi.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
  4. *
  5. * Authors:
  6. * Serge Semin <Sergey.Semin@baikalelectronics.ru>
  7. *
  8. * Baikal-T1 AXI-bus driver
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/bitfield.h>
  14. #include <linux/device.h>
  15. #include <linux/atomic.h>
  16. #include <linux/regmap.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/nmi.h>
  22. #include <linux/of.h>
  23. #include <linux/clk.h>
  24. #include <linux/reset.h>
  25. #include <linux/sysfs.h>
  26. #define BT1_AXI_WERRL 0x110
  27. #define BT1_AXI_WERRH 0x114
  28. #define BT1_AXI_WERRH_TYPE BIT(23)
  29. #define BT1_AXI_WERRH_ADDR_FLD 24
  30. #define BT1_AXI_WERRH_ADDR_MASK GENMASK(31, BT1_AXI_WERRH_ADDR_FLD)
  31. /*
  32. * struct bt1_axi - Baikal-T1 AXI-bus private data
  33. * @dev: Pointer to the device structure.
  34. * @qos_regs: AXI Interconnect QoS tuning registers.
  35. * @sys_regs: Baikal-T1 System Controller registers map.
  36. * @irq: Errors IRQ number.
  37. * @aclk: AXI reference clock.
  38. * @arst: AXI Interconnect reset line.
  39. * @count: Number of errors detected.
  40. */
  41. struct bt1_axi {
  42. struct device *dev;
  43. void __iomem *qos_regs;
  44. struct regmap *sys_regs;
  45. int irq;
  46. struct clk *aclk;
  47. struct reset_control *arst;
  48. atomic_t count;
  49. };
  50. static irqreturn_t bt1_axi_isr(int irq, void *data)
  51. {
  52. struct bt1_axi *axi = data;
  53. u32 low = 0, high = 0;
  54. regmap_read(axi->sys_regs, BT1_AXI_WERRL, &low);
  55. regmap_read(axi->sys_regs, BT1_AXI_WERRH, &high);
  56. dev_crit_ratelimited(axi->dev,
  57. "AXI-bus fault %d: %s at 0x%x%08x\n",
  58. atomic_inc_return(&axi->count),
  59. high & BT1_AXI_WERRH_TYPE ? "no slave" : "slave protocol error",
  60. high, low);
  61. /*
  62. * Print backtrace on each CPU. This might be pointless if the fault
  63. * has happened on the same CPU as the IRQ handler is executed or
  64. * the other core proceeded further execution despite the error.
  65. * But if it's not, by looking at the trace we would get straight to
  66. * the cause of the problem.
  67. */
  68. trigger_all_cpu_backtrace();
  69. return IRQ_HANDLED;
  70. }
  71. static void bt1_axi_clear_data(void *data)
  72. {
  73. struct bt1_axi *axi = data;
  74. struct platform_device *pdev = to_platform_device(axi->dev);
  75. platform_set_drvdata(pdev, NULL);
  76. }
  77. static struct bt1_axi *bt1_axi_create_data(struct platform_device *pdev)
  78. {
  79. struct device *dev = &pdev->dev;
  80. struct bt1_axi *axi;
  81. int ret;
  82. axi = devm_kzalloc(dev, sizeof(*axi), GFP_KERNEL);
  83. if (!axi)
  84. return ERR_PTR(-ENOMEM);
  85. ret = devm_add_action(dev, bt1_axi_clear_data, axi);
  86. if (ret) {
  87. dev_err(dev, "Can't add AXI EHB data clear action\n");
  88. return ERR_PTR(ret);
  89. }
  90. axi->dev = dev;
  91. atomic_set(&axi->count, 0);
  92. platform_set_drvdata(pdev, axi);
  93. return axi;
  94. }
  95. static int bt1_axi_request_regs(struct bt1_axi *axi)
  96. {
  97. struct platform_device *pdev = to_platform_device(axi->dev);
  98. struct device *dev = axi->dev;
  99. axi->sys_regs = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
  100. if (IS_ERR(axi->sys_regs)) {
  101. dev_err(dev, "Couldn't find syscon registers\n");
  102. return PTR_ERR(axi->sys_regs);
  103. }
  104. axi->qos_regs = devm_platform_ioremap_resource_byname(pdev, "qos");
  105. if (IS_ERR(axi->qos_regs))
  106. dev_err(dev, "Couldn't map AXI-bus QoS registers\n");
  107. return PTR_ERR_OR_ZERO(axi->qos_regs);
  108. }
  109. static int bt1_axi_request_rst(struct bt1_axi *axi)
  110. {
  111. int ret;
  112. axi->arst = devm_reset_control_get_optional_exclusive(axi->dev, "arst");
  113. if (IS_ERR(axi->arst))
  114. return dev_err_probe(axi->dev, PTR_ERR(axi->arst),
  115. "Couldn't get reset control line\n");
  116. ret = reset_control_deassert(axi->arst);
  117. if (ret)
  118. dev_err(axi->dev, "Failed to deassert the reset line\n");
  119. return ret;
  120. }
  121. static int bt1_axi_request_clk(struct bt1_axi *axi)
  122. {
  123. axi->aclk = devm_clk_get_enabled(axi->dev, "aclk");
  124. if (IS_ERR(axi->aclk))
  125. return dev_err_probe(axi->dev, PTR_ERR(axi->aclk),
  126. "Couldn't get AXI Interconnect clock\n");
  127. return 0;
  128. }
  129. static int bt1_axi_request_irq(struct bt1_axi *axi)
  130. {
  131. struct platform_device *pdev = to_platform_device(axi->dev);
  132. int ret;
  133. axi->irq = platform_get_irq(pdev, 0);
  134. if (axi->irq < 0)
  135. return axi->irq;
  136. ret = devm_request_irq(axi->dev, axi->irq, bt1_axi_isr, IRQF_SHARED,
  137. "bt1-axi", axi);
  138. if (ret)
  139. dev_err(axi->dev, "Couldn't request AXI EHB IRQ\n");
  140. return ret;
  141. }
  142. static ssize_t count_show(struct device *dev,
  143. struct device_attribute *attr, char *buf)
  144. {
  145. struct bt1_axi *axi = dev_get_drvdata(dev);
  146. return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&axi->count));
  147. }
  148. static DEVICE_ATTR_RO(count);
  149. static ssize_t inject_error_show(struct device *dev,
  150. struct device_attribute *attr, char *buf)
  151. {
  152. return scnprintf(buf, PAGE_SIZE, "Error injection: bus unaligned\n");
  153. }
  154. static ssize_t inject_error_store(struct device *dev,
  155. struct device_attribute *attr,
  156. const char *data, size_t count)
  157. {
  158. struct bt1_axi *axi = dev_get_drvdata(dev);
  159. /*
  160. * Performing unaligned read from the memory will cause the CM2 bus
  161. * error while unaligned writing - the AXI bus write error handled
  162. * by this driver.
  163. */
  164. if (sysfs_streq(data, "bus"))
  165. readb(axi->qos_regs);
  166. else if (sysfs_streq(data, "unaligned"))
  167. writeb(0, axi->qos_regs);
  168. else
  169. return -EINVAL;
  170. return count;
  171. }
  172. static DEVICE_ATTR_RW(inject_error);
  173. static struct attribute *bt1_axi_sysfs_attrs[] = {
  174. &dev_attr_count.attr,
  175. &dev_attr_inject_error.attr,
  176. NULL
  177. };
  178. ATTRIBUTE_GROUPS(bt1_axi_sysfs);
  179. static void bt1_axi_remove_sysfs(void *data)
  180. {
  181. struct bt1_axi *axi = data;
  182. device_remove_groups(axi->dev, bt1_axi_sysfs_groups);
  183. }
  184. static int bt1_axi_init_sysfs(struct bt1_axi *axi)
  185. {
  186. int ret;
  187. ret = device_add_groups(axi->dev, bt1_axi_sysfs_groups);
  188. if (ret) {
  189. dev_err(axi->dev, "Failed to add sysfs files group\n");
  190. return ret;
  191. }
  192. ret = devm_add_action_or_reset(axi->dev, bt1_axi_remove_sysfs, axi);
  193. if (ret)
  194. dev_err(axi->dev, "Can't add AXI EHB sysfs remove action\n");
  195. return ret;
  196. }
  197. static int bt1_axi_probe(struct platform_device *pdev)
  198. {
  199. struct bt1_axi *axi;
  200. int ret;
  201. axi = bt1_axi_create_data(pdev);
  202. if (IS_ERR(axi))
  203. return PTR_ERR(axi);
  204. ret = bt1_axi_request_regs(axi);
  205. if (ret)
  206. return ret;
  207. ret = bt1_axi_request_rst(axi);
  208. if (ret)
  209. return ret;
  210. ret = bt1_axi_request_clk(axi);
  211. if (ret)
  212. return ret;
  213. ret = bt1_axi_request_irq(axi);
  214. if (ret)
  215. return ret;
  216. ret = bt1_axi_init_sysfs(axi);
  217. if (ret)
  218. return ret;
  219. return 0;
  220. }
  221. static const struct of_device_id bt1_axi_of_match[] = {
  222. { .compatible = "baikal,bt1-axi" },
  223. { }
  224. };
  225. MODULE_DEVICE_TABLE(of, bt1_axi_of_match);
  226. static struct platform_driver bt1_axi_driver = {
  227. .probe = bt1_axi_probe,
  228. .driver = {
  229. .name = "bt1-axi",
  230. .of_match_table = bt1_axi_of_match
  231. }
  232. };
  233. module_platform_driver(bt1_axi_driver);
  234. MODULE_AUTHOR("Serge Semin <Sergey.Semin@baikalelectronics.ru>");
  235. MODULE_DESCRIPTION("Baikal-T1 AXI-bus driver");