coresight-dynamic-replicator.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/amba/bus.h>
  6. #include <linux/clk.h>
  7. #include <linux/coresight.h>
  8. #include <linux/device.h>
  9. #include <linux/err.h>
  10. #include <linux/init.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/of.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/slab.h>
  16. #include "coresight-priv.h"
  17. #define REPLICATOR_IDFILTER0 0x000
  18. #define REPLICATOR_IDFILTER1 0x004
  19. /**
  20. * struct replicator_state - specifics associated to a replicator component
  21. * @base: memory mapped base address for this component.
  22. * @dev: the device entity associated with this component
  23. * @atclk: optional clock for the core parts of the replicator.
  24. * @csdev: component vitals needed by the framework
  25. */
  26. struct replicator_state {
  27. void __iomem *base;
  28. struct device *dev;
  29. struct clk *atclk;
  30. struct coresight_device *csdev;
  31. };
  32. /*
  33. * replicator_reset : Reset the replicator configuration to sane values.
  34. */
  35. static void replicator_reset(struct replicator_state *drvdata)
  36. {
  37. CS_UNLOCK(drvdata->base);
  38. if (!coresight_claim_device_unlocked(drvdata->base)) {
  39. writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0);
  40. writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1);
  41. coresight_disclaim_device_unlocked(drvdata->base);
  42. }
  43. CS_LOCK(drvdata->base);
  44. }
  45. static int replicator_enable(struct coresight_device *csdev, int inport,
  46. int outport)
  47. {
  48. int rc = 0;
  49. u32 reg;
  50. struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent);
  51. switch (outport) {
  52. case 0:
  53. reg = REPLICATOR_IDFILTER0;
  54. break;
  55. case 1:
  56. reg = REPLICATOR_IDFILTER1;
  57. break;
  58. default:
  59. WARN_ON(1);
  60. return -EINVAL;
  61. }
  62. CS_UNLOCK(drvdata->base);
  63. if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) &&
  64. (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff))
  65. rc = coresight_claim_device_unlocked(drvdata->base);
  66. /* Ensure that the outport is enabled. */
  67. if (!rc) {
  68. writel_relaxed(0x00, drvdata->base + reg);
  69. dev_dbg(drvdata->dev, "REPLICATOR enabled\n");
  70. }
  71. CS_LOCK(drvdata->base);
  72. return rc;
  73. }
  74. static void replicator_disable(struct coresight_device *csdev, int inport,
  75. int outport)
  76. {
  77. u32 reg;
  78. struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent);
  79. switch (outport) {
  80. case 0:
  81. reg = REPLICATOR_IDFILTER0;
  82. break;
  83. case 1:
  84. reg = REPLICATOR_IDFILTER1;
  85. break;
  86. default:
  87. WARN_ON(1);
  88. return;
  89. }
  90. CS_UNLOCK(drvdata->base);
  91. /* disable the flow of ATB data through port */
  92. writel_relaxed(0xff, drvdata->base + reg);
  93. if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) &&
  94. (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff))
  95. coresight_disclaim_device_unlocked(drvdata->base);
  96. CS_LOCK(drvdata->base);
  97. dev_dbg(drvdata->dev, "REPLICATOR disabled\n");
  98. }
  99. static const struct coresight_ops_link replicator_link_ops = {
  100. .enable = replicator_enable,
  101. .disable = replicator_disable,
  102. };
  103. static const struct coresight_ops replicator_cs_ops = {
  104. .link_ops = &replicator_link_ops,
  105. };
  106. #define coresight_replicator_reg(name, offset) \
  107. coresight_simple_reg32(struct replicator_state, name, offset)
  108. coresight_replicator_reg(idfilter0, REPLICATOR_IDFILTER0);
  109. coresight_replicator_reg(idfilter1, REPLICATOR_IDFILTER1);
  110. static struct attribute *replicator_mgmt_attrs[] = {
  111. &dev_attr_idfilter0.attr,
  112. &dev_attr_idfilter1.attr,
  113. NULL,
  114. };
  115. static const struct attribute_group replicator_mgmt_group = {
  116. .attrs = replicator_mgmt_attrs,
  117. .name = "mgmt",
  118. };
  119. static const struct attribute_group *replicator_groups[] = {
  120. &replicator_mgmt_group,
  121. NULL,
  122. };
  123. static int replicator_probe(struct amba_device *adev, const struct amba_id *id)
  124. {
  125. int ret;
  126. struct device *dev = &adev->dev;
  127. struct resource *res = &adev->res;
  128. struct coresight_platform_data *pdata = NULL;
  129. struct replicator_state *drvdata;
  130. struct coresight_desc desc = { 0 };
  131. struct device_node *np = adev->dev.of_node;
  132. void __iomem *base;
  133. if (np) {
  134. pdata = of_get_coresight_platform_data(dev, np);
  135. if (IS_ERR(pdata))
  136. return PTR_ERR(pdata);
  137. adev->dev.platform_data = pdata;
  138. }
  139. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  140. if (!drvdata)
  141. return -ENOMEM;
  142. drvdata->dev = &adev->dev;
  143. drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
  144. if (!IS_ERR(drvdata->atclk)) {
  145. ret = clk_prepare_enable(drvdata->atclk);
  146. if (ret)
  147. return ret;
  148. }
  149. /* Validity for the resource is already checked by the AMBA core */
  150. base = devm_ioremap_resource(dev, res);
  151. if (IS_ERR(base))
  152. return PTR_ERR(base);
  153. drvdata->base = base;
  154. dev_set_drvdata(dev, drvdata);
  155. pm_runtime_put(&adev->dev);
  156. desc.type = CORESIGHT_DEV_TYPE_LINK;
  157. desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
  158. desc.ops = &replicator_cs_ops;
  159. desc.pdata = adev->dev.platform_data;
  160. desc.dev = &adev->dev;
  161. desc.groups = replicator_groups;
  162. drvdata->csdev = coresight_register(&desc);
  163. if (!IS_ERR(drvdata->csdev)) {
  164. replicator_reset(drvdata);
  165. return 0;
  166. }
  167. return PTR_ERR(drvdata->csdev);
  168. }
  169. #ifdef CONFIG_PM
  170. static int replicator_runtime_suspend(struct device *dev)
  171. {
  172. struct replicator_state *drvdata = dev_get_drvdata(dev);
  173. if (drvdata && !IS_ERR(drvdata->atclk))
  174. clk_disable_unprepare(drvdata->atclk);
  175. return 0;
  176. }
  177. static int replicator_runtime_resume(struct device *dev)
  178. {
  179. struct replicator_state *drvdata = dev_get_drvdata(dev);
  180. if (drvdata && !IS_ERR(drvdata->atclk))
  181. clk_prepare_enable(drvdata->atclk);
  182. return 0;
  183. }
  184. #endif
  185. static const struct dev_pm_ops replicator_dev_pm_ops = {
  186. SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
  187. replicator_runtime_resume,
  188. NULL)
  189. };
  190. static const struct amba_id replicator_ids[] = {
  191. {
  192. .id = 0x000bb909,
  193. .mask = 0x000fffff,
  194. },
  195. {
  196. /* Coresight SoC-600 */
  197. .id = 0x000bb9ec,
  198. .mask = 0x000fffff,
  199. },
  200. { 0, 0 },
  201. };
  202. static struct amba_driver replicator_driver = {
  203. .drv = {
  204. .name = "coresight-dynamic-replicator",
  205. .pm = &replicator_dev_pm_ops,
  206. .suppress_bind_attrs = true,
  207. },
  208. .probe = replicator_probe,
  209. .id_table = replicator_ids,
  210. };
  211. builtin_amba_driver(replicator_driver);