coresight-funnel.c 6.9 KB

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