bt1-axi.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
  4. *
  5. * Authors:
  6. * Serge Semin <[email protected]>
  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 void bt1_axi_disable_clk(void *data)
  122. {
  123. struct bt1_axi *axi = data;
  124. clk_disable_unprepare(axi->aclk);
  125. }
  126. static int bt1_axi_request_clk(struct bt1_axi *axi)
  127. {
  128. int ret;
  129. axi->aclk = devm_clk_get(axi->dev, "aclk");
  130. if (IS_ERR(axi->aclk))
  131. return dev_err_probe(axi->dev, PTR_ERR(axi->aclk),
  132. "Couldn't get AXI Interconnect clock\n");
  133. ret = clk_prepare_enable(axi->aclk);
  134. if (ret) {
  135. dev_err(axi->dev, "Couldn't enable the AXI clock\n");
  136. return ret;
  137. }
  138. ret = devm_add_action_or_reset(axi->dev, bt1_axi_disable_clk, axi);
  139. if (ret)
  140. dev_err(axi->dev, "Can't add AXI clock disable action\n");
  141. return ret;
  142. }
  143. static int bt1_axi_request_irq(struct bt1_axi *axi)
  144. {
  145. struct platform_device *pdev = to_platform_device(axi->dev);
  146. int ret;
  147. axi->irq = platform_get_irq(pdev, 0);
  148. if (axi->irq < 0)
  149. return axi->irq;
  150. ret = devm_request_irq(axi->dev, axi->irq, bt1_axi_isr, IRQF_SHARED,
  151. "bt1-axi", axi);
  152. if (ret)
  153. dev_err(axi->dev, "Couldn't request AXI EHB IRQ\n");
  154. return ret;
  155. }
  156. static ssize_t count_show(struct device *dev,
  157. struct device_attribute *attr, char *buf)
  158. {
  159. struct bt1_axi *axi = dev_get_drvdata(dev);
  160. return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&axi->count));
  161. }
  162. static DEVICE_ATTR_RO(count);
  163. static ssize_t inject_error_show(struct device *dev,
  164. struct device_attribute *attr, char *buf)
  165. {
  166. return scnprintf(buf, PAGE_SIZE, "Error injection: bus unaligned\n");
  167. }
  168. static ssize_t inject_error_store(struct device *dev,
  169. struct device_attribute *attr,
  170. const char *data, size_t count)
  171. {
  172. struct bt1_axi *axi = dev_get_drvdata(dev);
  173. /*
  174. * Performing unaligned read from the memory will cause the CM2 bus
  175. * error while unaligned writing - the AXI bus write error handled
  176. * by this driver.
  177. */
  178. if (sysfs_streq(data, "bus"))
  179. readb(axi->qos_regs);
  180. else if (sysfs_streq(data, "unaligned"))
  181. writeb(0, axi->qos_regs);
  182. else
  183. return -EINVAL;
  184. return count;
  185. }
  186. static DEVICE_ATTR_RW(inject_error);
  187. static struct attribute *bt1_axi_sysfs_attrs[] = {
  188. &dev_attr_count.attr,
  189. &dev_attr_inject_error.attr,
  190. NULL
  191. };
  192. ATTRIBUTE_GROUPS(bt1_axi_sysfs);
  193. static void bt1_axi_remove_sysfs(void *data)
  194. {
  195. struct bt1_axi *axi = data;
  196. device_remove_groups(axi->dev, bt1_axi_sysfs_groups);
  197. }
  198. static int bt1_axi_init_sysfs(struct bt1_axi *axi)
  199. {
  200. int ret;
  201. ret = device_add_groups(axi->dev, bt1_axi_sysfs_groups);
  202. if (ret) {
  203. dev_err(axi->dev, "Failed to add sysfs files group\n");
  204. return ret;
  205. }
  206. ret = devm_add_action_or_reset(axi->dev, bt1_axi_remove_sysfs, axi);
  207. if (ret)
  208. dev_err(axi->dev, "Can't add AXI EHB sysfs remove action\n");
  209. return ret;
  210. }
  211. static int bt1_axi_probe(struct platform_device *pdev)
  212. {
  213. struct bt1_axi *axi;
  214. int ret;
  215. axi = bt1_axi_create_data(pdev);
  216. if (IS_ERR(axi))
  217. return PTR_ERR(axi);
  218. ret = bt1_axi_request_regs(axi);
  219. if (ret)
  220. return ret;
  221. ret = bt1_axi_request_rst(axi);
  222. if (ret)
  223. return ret;
  224. ret = bt1_axi_request_clk(axi);
  225. if (ret)
  226. return ret;
  227. ret = bt1_axi_request_irq(axi);
  228. if (ret)
  229. return ret;
  230. ret = bt1_axi_init_sysfs(axi);
  231. if (ret)
  232. return ret;
  233. return 0;
  234. }
  235. static const struct of_device_id bt1_axi_of_match[] = {
  236. { .compatible = "baikal,bt1-axi" },
  237. { }
  238. };
  239. MODULE_DEVICE_TABLE(of, bt1_axi_of_match);
  240. static struct platform_driver bt1_axi_driver = {
  241. .probe = bt1_axi_probe,
  242. .driver = {
  243. .name = "bt1-axi",
  244. .of_match_table = bt1_axi_of_match
  245. }
  246. };
  247. module_platform_driver(bt1_axi_driver);
  248. MODULE_AUTHOR("Serge Semin <[email protected]>");
  249. MODULE_DESCRIPTION("Baikal-T1 AXI-bus driver");
  250. MODULE_LICENSE("GPL v2");