coresight-hwevent.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* Copyright (c) 2013, 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/device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/io.h>
  18. #include <linux/err.h>
  19. #include <linux/slab.h>
  20. #include <linux/mutex.h>
  21. #include <linux/clk.h>
  22. #include <linux/of_coresight.h>
  23. #include <linux/coresight.h>
  24. #include <linux/of.h>
  25. #include "coresight-priv.h"
  26. struct hwevent_mux {
  27. phys_addr_t start;
  28. phys_addr_t end;
  29. };
  30. struct hwevent_drvdata {
  31. struct device *dev;
  32. struct coresight_device *csdev;
  33. struct clk *clk;
  34. struct mutex mutex;
  35. int nr_hclk;
  36. struct clk **hclk;
  37. int nr_hmux;
  38. struct hwevent_mux *hmux;
  39. bool enable;
  40. };
  41. static int hwevent_enable(struct hwevent_drvdata *drvdata)
  42. {
  43. int ret, i;
  44. mutex_lock(&drvdata->mutex);
  45. if (drvdata->enable)
  46. goto out;
  47. ret = clk_prepare_enable(drvdata->clk);
  48. if (ret)
  49. goto err0;
  50. for (i = 0; i < drvdata->nr_hclk; i++) {
  51. ret = clk_prepare_enable(drvdata->hclk[i]);
  52. if (ret)
  53. goto err1;
  54. }
  55. drvdata->enable = true;
  56. dev_info(drvdata->dev, "Hardware Event driver enabled\n");
  57. out:
  58. mutex_unlock(&drvdata->mutex);
  59. return 0;
  60. err1:
  61. clk_disable_unprepare(drvdata->clk);
  62. for (i--; i >= 0; i--)
  63. clk_disable_unprepare(drvdata->hclk[i]);
  64. err0:
  65. mutex_unlock(&drvdata->mutex);
  66. return ret;
  67. }
  68. static void hwevent_disable(struct hwevent_drvdata *drvdata)
  69. {
  70. int i;
  71. mutex_lock(&drvdata->mutex);
  72. if (!drvdata->enable)
  73. goto out;
  74. drvdata->enable = false;
  75. clk_disable_unprepare(drvdata->clk);
  76. for (i = 0; i < drvdata->nr_hclk; i++)
  77. clk_disable_unprepare(drvdata->hclk[i]);
  78. dev_info(drvdata->dev, "Hardware Event driver disabled\n");
  79. out:
  80. mutex_unlock(&drvdata->mutex);
  81. }
  82. static ssize_t hwevent_show_enable(struct device *dev,
  83. struct device_attribute *attr, char *buf)
  84. {
  85. struct hwevent_drvdata *drvdata = dev_get_drvdata(dev->parent);
  86. unsigned long val = drvdata->enable;
  87. return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
  88. }
  89. static ssize_t hwevent_store_enable(struct device *dev,
  90. struct device_attribute *attr,
  91. const char *buf, size_t size)
  92. {
  93. struct hwevent_drvdata *drvdata = dev_get_drvdata(dev->parent);
  94. unsigned long val;
  95. int ret = 0;
  96. if (sscanf(buf, "%lx", &val) != 1)
  97. return -EINVAL;
  98. if (val)
  99. ret = hwevent_enable(drvdata);
  100. else
  101. hwevent_disable(drvdata);
  102. if (ret)
  103. return ret;
  104. return size;
  105. }
  106. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, hwevent_show_enable,
  107. hwevent_store_enable);
  108. static ssize_t hwevent_store_setreg(struct device *dev,
  109. struct device_attribute *attr,
  110. const char *buf, size_t size)
  111. {
  112. struct hwevent_drvdata *drvdata = dev_get_drvdata(dev->parent);
  113. void *hwereg;
  114. unsigned long long addr;
  115. unsigned long val;
  116. int ret, i;
  117. if (sscanf(buf, "%llx %lx", &addr, &val) != 2)
  118. return -EINVAL;
  119. mutex_lock(&drvdata->mutex);
  120. if (!drvdata->enable) {
  121. dev_err(dev, "Hardware Event driver not enabled\n");
  122. ret = -EINVAL;
  123. goto err;
  124. }
  125. for (i = 0; i < drvdata->nr_hmux; i++) {
  126. if ((addr >= drvdata->hmux[i].start) &&
  127. (addr < drvdata->hmux[i].end)) {
  128. hwereg = devm_ioremap(dev,
  129. drvdata->hmux[i].start,
  130. drvdata->hmux[i].end -
  131. drvdata->hmux[i].start);
  132. if (!hwereg) {
  133. dev_err(dev, "unable to map address 0x%llx\n",
  134. addr);
  135. ret = -ENOMEM;
  136. goto err;
  137. }
  138. writel_relaxed(val, hwereg + addr -
  139. drvdata->hmux[i].start);
  140. /* Ensure writes to hwevent control registers
  141. are completed before unmapping the address
  142. */
  143. mb();
  144. devm_iounmap(dev, hwereg);
  145. break;
  146. }
  147. }
  148. if (i == drvdata->nr_hmux) {
  149. ret = coresight_csr_hwctrl_set(addr, val);
  150. if (ret) {
  151. dev_err(dev, "invalid mux control register address\n");
  152. ret = -EINVAL;
  153. goto err;
  154. }
  155. }
  156. mutex_unlock(&drvdata->mutex);
  157. return size;
  158. err:
  159. mutex_unlock(&drvdata->mutex);
  160. return ret;
  161. }
  162. static DEVICE_ATTR(setreg, S_IWUSR, NULL, hwevent_store_setreg);
  163. static struct attribute *hwevent_attrs[] = {
  164. &dev_attr_enable.attr,
  165. &dev_attr_setreg.attr,
  166. NULL,
  167. };
  168. static struct attribute_group hwevent_attr_grp = {
  169. .attrs = hwevent_attrs,
  170. };
  171. static const struct attribute_group *hwevent_attr_grps[] = {
  172. &hwevent_attr_grp,
  173. NULL,
  174. };
  175. static int __devinit hwevent_probe(struct platform_device *pdev)
  176. {
  177. struct device *dev = &pdev->dev;
  178. struct hwevent_drvdata *drvdata;
  179. struct coresight_desc *desc;
  180. struct coresight_platform_data *pdata;
  181. struct resource *res;
  182. int ret, i;
  183. const char *hmux_name, *hclk_name;
  184. if (coresight_fuse_access_disabled())
  185. return -EPERM;
  186. if (pdev->dev.of_node) {
  187. pdata = of_get_coresight_platform_data(dev, pdev->dev.of_node);
  188. if (IS_ERR(pdata))
  189. return PTR_ERR(pdata);
  190. pdev->dev.platform_data = pdata;
  191. }
  192. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  193. if (!drvdata)
  194. return -ENOMEM;
  195. drvdata->dev = &pdev->dev;
  196. platform_set_drvdata(pdev, drvdata);
  197. if (pdev->dev.of_node)
  198. drvdata->nr_hmux = of_property_count_strings(pdev->dev.of_node,
  199. "reg-names");
  200. if (drvdata->nr_hmux > 0) {
  201. drvdata->hmux = devm_kzalloc(dev, drvdata->nr_hmux *
  202. sizeof(*drvdata->hmux),
  203. GFP_KERNEL);
  204. if (!drvdata->hmux)
  205. return -ENOMEM;
  206. for (i = 0; i < drvdata->nr_hmux; i++) {
  207. ret = of_property_read_string_index(pdev->dev.of_node,
  208. "reg-names", i,
  209. &hmux_name);
  210. if (ret)
  211. return ret;
  212. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  213. hmux_name);
  214. if (!res)
  215. return -ENODEV;
  216. drvdata->hmux[i].start = res->start;
  217. drvdata->hmux[i].end = res->end;
  218. }
  219. } else if (drvdata->nr_hmux < 0) {
  220. return drvdata->nr_hmux;
  221. } else {
  222. /* return error if reg-names in dt node is empty string */
  223. return -ENODEV;
  224. }
  225. mutex_init(&drvdata->mutex);
  226. drvdata->clk = devm_clk_get(dev, "core_clk");
  227. if (IS_ERR(drvdata->clk))
  228. return PTR_ERR(drvdata->clk);
  229. ret = clk_set_rate(drvdata->clk, CORESIGHT_CLK_RATE_TRACE);
  230. if (ret)
  231. return ret;
  232. if (pdev->dev.of_node)
  233. drvdata->nr_hclk = of_property_count_strings(pdev->dev.of_node,
  234. "qcom,hwevent-clks");
  235. if (drvdata->nr_hclk > 0) {
  236. drvdata->hclk = devm_kzalloc(dev, drvdata->nr_hclk *
  237. sizeof(*drvdata->hclk),
  238. GFP_KERNEL);
  239. if (!drvdata->hclk)
  240. return -ENOMEM;
  241. for (i = 0; i < drvdata->nr_hclk; i++) {
  242. ret = of_property_read_string_index(pdev->dev.of_node,
  243. "qcom,hwevent-clks",
  244. i, &hclk_name);
  245. if (ret)
  246. return ret;
  247. drvdata->hclk[i] = devm_clk_get(dev, hclk_name);
  248. if (IS_ERR(drvdata->hclk[i]))
  249. return PTR_ERR(drvdata->hclk[i]);
  250. }
  251. }
  252. desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
  253. if (!desc)
  254. return -ENOMEM;
  255. desc->type = CORESIGHT_DEV_TYPE_NONE;
  256. desc->pdata = pdev->dev.platform_data;
  257. desc->dev = &pdev->dev;
  258. desc->groups = hwevent_attr_grps;
  259. desc->owner = THIS_MODULE;
  260. drvdata->csdev = coresight_register(desc);
  261. if (IS_ERR(drvdata->csdev))
  262. return PTR_ERR(drvdata->csdev);
  263. dev_info(dev, "Hardware Event driver initialized\n");
  264. return 0;
  265. }
  266. static int __devexit hwevent_remove(struct platform_device *pdev)
  267. {
  268. struct hwevent_drvdata *drvdata = platform_get_drvdata(pdev);
  269. mutex_destroy(&drvdata->mutex);
  270. coresight_unregister(drvdata->csdev);
  271. return 0;
  272. }
  273. static struct of_device_id hwevent_match[] = {
  274. {.compatible = "qcom,coresight-hwevent"},
  275. {}
  276. };
  277. static struct platform_driver hwevent_driver = {
  278. .probe = hwevent_probe,
  279. .remove = __devexit_p(hwevent_remove),
  280. .driver = {
  281. .name = "coresight-hwevent",
  282. .owner = THIS_MODULE,
  283. .of_match_table = hwevent_match,
  284. },
  285. };
  286. static int __init hwevent_init(void)
  287. {
  288. return platform_driver_register(&hwevent_driver);
  289. }
  290. module_init(hwevent_init);
  291. static void __exit hwevent_exit(void)
  292. {
  293. platform_driver_unregister(&hwevent_driver);
  294. }
  295. module_exit(hwevent_exit);
  296. MODULE_LICENSE("GPL v2");
  297. MODULE_DESCRIPTION("CoreSight Hardware Event driver");