coresight-funnel.c 6.5 KB

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