coresight-funnel.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
  4. *
  5. * Description: CoreSight Funnel driver
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/types.h>
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/fs.h>
  13. #include <linux/slab.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/coresight.h>
  16. #include <linux/amba/bus.h>
  17. #include <linux/clk.h>
  18. #include "coresight-priv.h"
  19. #define FUNNEL_FUNCTL 0x000
  20. #define FUNNEL_PRICTL 0x004
  21. #define FUNNEL_HOLDTIME_MASK 0xf00
  22. #define FUNNEL_HOLDTIME_SHFT 0x8
  23. #define FUNNEL_HOLDTIME (0x7 << FUNNEL_HOLDTIME_SHFT)
  24. #define FUNNEL_ENSx_MASK 0xff
  25. /**
  26. * struct funnel_drvdata - specifics associated to a funnel component
  27. * @base: memory mapped base address for this component.
  28. * @dev: the device entity associated to this component.
  29. * @atclk: optional clock for the core parts of the funnel.
  30. * @csdev: component vitals needed by the framework.
  31. * @priority: port selection order.
  32. */
  33. struct funnel_drvdata {
  34. void __iomem *base;
  35. struct device *dev;
  36. struct clk *atclk;
  37. struct coresight_device *csdev;
  38. unsigned long priority;
  39. };
  40. static int funnel_enable_hw(struct funnel_drvdata *drvdata, int port)
  41. {
  42. u32 functl;
  43. int rc = 0;
  44. CS_UNLOCK(drvdata->base);
  45. functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
  46. /* Claim the device only when we enable the first slave */
  47. if (!(functl & FUNNEL_ENSx_MASK)) {
  48. rc = coresight_claim_device_unlocked(drvdata->base);
  49. if (rc)
  50. goto done;
  51. }
  52. functl &= ~FUNNEL_HOLDTIME_MASK;
  53. functl |= FUNNEL_HOLDTIME;
  54. functl |= (1 << port);
  55. writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
  56. writel_relaxed(drvdata->priority, drvdata->base + FUNNEL_PRICTL);
  57. done:
  58. CS_LOCK(drvdata->base);
  59. return rc;
  60. }
  61. static int funnel_enable(struct coresight_device *csdev, int inport,
  62. int outport)
  63. {
  64. int rc;
  65. struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  66. rc = funnel_enable_hw(drvdata, inport);
  67. if (!rc)
  68. dev_dbg(drvdata->dev, "FUNNEL inport %d enabled\n", inport);
  69. return rc;
  70. }
  71. static void funnel_disable_hw(struct funnel_drvdata *drvdata, int inport)
  72. {
  73. u32 functl;
  74. CS_UNLOCK(drvdata->base);
  75. functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
  76. functl &= ~(1 << inport);
  77. writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
  78. /* Disclaim the device if none of the slaves are now active */
  79. if (!(functl & FUNNEL_ENSx_MASK))
  80. coresight_disclaim_device_unlocked(drvdata->base);
  81. CS_LOCK(drvdata->base);
  82. }
  83. static void funnel_disable(struct coresight_device *csdev, int inport,
  84. int outport)
  85. {
  86. struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  87. funnel_disable_hw(drvdata, inport);
  88. dev_dbg(drvdata->dev, "FUNNEL inport %d disabled\n", inport);
  89. }
  90. static const struct coresight_ops_link funnel_link_ops = {
  91. .enable = funnel_enable,
  92. .disable = funnel_disable,
  93. };
  94. static const struct coresight_ops funnel_cs_ops = {
  95. .link_ops = &funnel_link_ops,
  96. };
  97. static ssize_t priority_show(struct device *dev,
  98. struct device_attribute *attr, char *buf)
  99. {
  100. struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
  101. unsigned long val = drvdata->priority;
  102. return sprintf(buf, "%#lx\n", val);
  103. }
  104. static ssize_t priority_store(struct device *dev,
  105. struct device_attribute *attr,
  106. const char *buf, size_t size)
  107. {
  108. int ret;
  109. unsigned long val;
  110. struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
  111. ret = kstrtoul(buf, 16, &val);
  112. if (ret)
  113. return ret;
  114. drvdata->priority = val;
  115. return size;
  116. }
  117. static DEVICE_ATTR_RW(priority);
  118. static u32 get_funnel_ctrl_hw(struct funnel_drvdata *drvdata)
  119. {
  120. u32 functl;
  121. CS_UNLOCK(drvdata->base);
  122. functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
  123. CS_LOCK(drvdata->base);
  124. return functl;
  125. }
  126. static ssize_t funnel_ctrl_show(struct device *dev,
  127. struct device_attribute *attr, char *buf)
  128. {
  129. u32 val;
  130. struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
  131. pm_runtime_get_sync(drvdata->dev);
  132. val = get_funnel_ctrl_hw(drvdata);
  133. pm_runtime_put(drvdata->dev);
  134. return sprintf(buf, "%#x\n", val);
  135. }
  136. static DEVICE_ATTR_RO(funnel_ctrl);
  137. static struct attribute *coresight_funnel_attrs[] = {
  138. &dev_attr_funnel_ctrl.attr,
  139. &dev_attr_priority.attr,
  140. NULL,
  141. };
  142. ATTRIBUTE_GROUPS(coresight_funnel);
  143. static int funnel_probe(struct amba_device *adev, const struct amba_id *id)
  144. {
  145. int ret;
  146. void __iomem *base;
  147. struct device *dev = &adev->dev;
  148. struct coresight_platform_data *pdata = NULL;
  149. struct funnel_drvdata *drvdata;
  150. struct resource *res = &adev->res;
  151. struct coresight_desc desc = { 0 };
  152. struct device_node *np = adev->dev.of_node;
  153. if (np) {
  154. pdata = of_get_coresight_platform_data(dev, np);
  155. if (IS_ERR(pdata))
  156. return PTR_ERR(pdata);
  157. adev->dev.platform_data = pdata;
  158. }
  159. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  160. if (!drvdata)
  161. return -ENOMEM;
  162. drvdata->dev = &adev->dev;
  163. drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
  164. if (!IS_ERR(drvdata->atclk)) {
  165. ret = clk_prepare_enable(drvdata->atclk);
  166. if (ret)
  167. return ret;
  168. }
  169. dev_set_drvdata(dev, drvdata);
  170. /* Validity for the resource is already checked by the AMBA core */
  171. base = devm_ioremap_resource(dev, res);
  172. if (IS_ERR(base))
  173. return PTR_ERR(base);
  174. drvdata->base = base;
  175. pm_runtime_put(&adev->dev);
  176. desc.type = CORESIGHT_DEV_TYPE_LINK;
  177. desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
  178. desc.ops = &funnel_cs_ops;
  179. desc.pdata = pdata;
  180. desc.dev = dev;
  181. desc.groups = coresight_funnel_groups;
  182. drvdata->csdev = coresight_register(&desc);
  183. return PTR_ERR_OR_ZERO(drvdata->csdev);
  184. }
  185. #ifdef CONFIG_PM
  186. static int funnel_runtime_suspend(struct device *dev)
  187. {
  188. struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
  189. if (drvdata && !IS_ERR(drvdata->atclk))
  190. clk_disable_unprepare(drvdata->atclk);
  191. return 0;
  192. }
  193. static int funnel_runtime_resume(struct device *dev)
  194. {
  195. struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
  196. if (drvdata && !IS_ERR(drvdata->atclk))
  197. clk_prepare_enable(drvdata->atclk);
  198. return 0;
  199. }
  200. #endif
  201. static const struct dev_pm_ops funnel_dev_pm_ops = {
  202. SET_RUNTIME_PM_OPS(funnel_runtime_suspend, funnel_runtime_resume, NULL)
  203. };
  204. static const struct amba_id funnel_ids[] = {
  205. {
  206. .id = 0x000bb908,
  207. .mask = 0x000fffff,
  208. },
  209. {
  210. /* Coresight SoC-600 */
  211. .id = 0x000bb9eb,
  212. .mask = 0x000fffff,
  213. },
  214. { 0, 0},
  215. };
  216. static struct amba_driver funnel_driver = {
  217. .drv = {
  218. .name = "coresight-funnel",
  219. .owner = THIS_MODULE,
  220. .pm = &funnel_dev_pm_ops,
  221. .suppress_bind_attrs = true,
  222. },
  223. .probe = funnel_probe,
  224. .id_table = funnel_ids,
  225. };
  226. builtin_amba_driver(funnel_driver);