pti.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Intel(R) Trace Hub PTI output driver
  3. *
  4. * Copyright (C) 2014-2016 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/types.h>
  17. #include <linux/module.h>
  18. #include <linux/device.h>
  19. #include <linux/sizes.h>
  20. #include <linux/printk.h>
  21. #include <linux/slab.h>
  22. #include <linux/mm.h>
  23. #include <linux/io.h>
  24. #include "intel_th.h"
  25. #include "pti.h"
  26. struct pti_device {
  27. void __iomem *base;
  28. struct intel_th_device *thdev;
  29. unsigned int mode;
  30. unsigned int freeclk;
  31. unsigned int clkdiv;
  32. unsigned int patgen;
  33. unsigned int lpp_dest_mask;
  34. unsigned int lpp_dest;
  35. };
  36. /* map PTI widths to MODE settings of PTI_CTL register */
  37. static const unsigned int pti_mode[] = {
  38. 0, 4, 8, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0,
  39. };
  40. static int pti_width_mode(unsigned int width)
  41. {
  42. int i;
  43. for (i = 0; i < ARRAY_SIZE(pti_mode); i++)
  44. if (pti_mode[i] == width)
  45. return i;
  46. return -EINVAL;
  47. }
  48. static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
  49. char *buf)
  50. {
  51. struct pti_device *pti = dev_get_drvdata(dev);
  52. return scnprintf(buf, PAGE_SIZE, "%d\n", pti_mode[pti->mode]);
  53. }
  54. static ssize_t mode_store(struct device *dev, struct device_attribute *attr,
  55. const char *buf, size_t size)
  56. {
  57. struct pti_device *pti = dev_get_drvdata(dev);
  58. unsigned long val;
  59. int ret;
  60. ret = kstrtoul(buf, 10, &val);
  61. if (ret)
  62. return ret;
  63. ret = pti_width_mode(val);
  64. if (ret < 0)
  65. return ret;
  66. pti->mode = ret;
  67. return size;
  68. }
  69. static DEVICE_ATTR_RW(mode);
  70. static ssize_t
  71. freerunning_clock_show(struct device *dev, struct device_attribute *attr,
  72. char *buf)
  73. {
  74. struct pti_device *pti = dev_get_drvdata(dev);
  75. return scnprintf(buf, PAGE_SIZE, "%d\n", pti->freeclk);
  76. }
  77. static ssize_t
  78. freerunning_clock_store(struct device *dev, struct device_attribute *attr,
  79. const char *buf, size_t size)
  80. {
  81. struct pti_device *pti = dev_get_drvdata(dev);
  82. unsigned long val;
  83. int ret;
  84. ret = kstrtoul(buf, 10, &val);
  85. if (ret)
  86. return ret;
  87. pti->freeclk = !!val;
  88. return size;
  89. }
  90. static DEVICE_ATTR_RW(freerunning_clock);
  91. static ssize_t
  92. clock_divider_show(struct device *dev, struct device_attribute *attr,
  93. char *buf)
  94. {
  95. struct pti_device *pti = dev_get_drvdata(dev);
  96. return scnprintf(buf, PAGE_SIZE, "%d\n", 1u << pti->clkdiv);
  97. }
  98. static ssize_t
  99. clock_divider_store(struct device *dev, struct device_attribute *attr,
  100. const char *buf, size_t size)
  101. {
  102. struct pti_device *pti = dev_get_drvdata(dev);
  103. unsigned long val;
  104. int ret;
  105. ret = kstrtoul(buf, 10, &val);
  106. if (ret)
  107. return ret;
  108. if (!is_power_of_2(val) || val > 8 || !val)
  109. return -EINVAL;
  110. pti->clkdiv = val;
  111. return size;
  112. }
  113. static DEVICE_ATTR_RW(clock_divider);
  114. static struct attribute *pti_output_attrs[] = {
  115. &dev_attr_mode.attr,
  116. &dev_attr_freerunning_clock.attr,
  117. &dev_attr_clock_divider.attr,
  118. NULL,
  119. };
  120. static struct attribute_group pti_output_group = {
  121. .attrs = pti_output_attrs,
  122. };
  123. static int intel_th_pti_activate(struct intel_th_device *thdev)
  124. {
  125. struct pti_device *pti = dev_get_drvdata(&thdev->dev);
  126. u32 ctl = PTI_EN;
  127. if (pti->patgen)
  128. ctl |= pti->patgen << __ffs(PTI_PATGENMODE);
  129. if (pti->freeclk)
  130. ctl |= PTI_FCEN;
  131. ctl |= pti->mode << __ffs(PTI_MODE);
  132. ctl |= pti->clkdiv << __ffs(PTI_CLKDIV);
  133. ctl |= pti->lpp_dest << __ffs(LPP_DEST);
  134. iowrite32(ctl, pti->base + REG_PTI_CTL);
  135. intel_th_trace_enable(thdev);
  136. return 0;
  137. }
  138. static void intel_th_pti_deactivate(struct intel_th_device *thdev)
  139. {
  140. struct pti_device *pti = dev_get_drvdata(&thdev->dev);
  141. intel_th_trace_disable(thdev);
  142. iowrite32(0, pti->base + REG_PTI_CTL);
  143. }
  144. static void read_hw_config(struct pti_device *pti)
  145. {
  146. u32 ctl = ioread32(pti->base + REG_PTI_CTL);
  147. pti->mode = (ctl & PTI_MODE) >> __ffs(PTI_MODE);
  148. pti->clkdiv = (ctl & PTI_CLKDIV) >> __ffs(PTI_CLKDIV);
  149. pti->freeclk = !!(ctl & PTI_FCEN);
  150. if (!pti_mode[pti->mode])
  151. pti->mode = pti_width_mode(4);
  152. if (!pti->clkdiv)
  153. pti->clkdiv = 1;
  154. if (pti->thdev->output.type == GTH_LPP) {
  155. if (ctl & LPP_PTIPRESENT)
  156. pti->lpp_dest_mask |= LPP_DEST_PTI;
  157. if (ctl & LPP_BSSBPRESENT)
  158. pti->lpp_dest_mask |= LPP_DEST_EXI;
  159. if (ctl & LPP_DEST)
  160. pti->lpp_dest = 1;
  161. }
  162. }
  163. static int intel_th_pti_probe(struct intel_th_device *thdev)
  164. {
  165. struct device *dev = &thdev->dev;
  166. struct resource *res;
  167. struct pti_device *pti;
  168. void __iomem *base;
  169. res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
  170. if (!res)
  171. return -ENODEV;
  172. base = devm_ioremap(dev, res->start, resource_size(res));
  173. if (!base)
  174. return -ENOMEM;
  175. pti = devm_kzalloc(dev, sizeof(*pti), GFP_KERNEL);
  176. if (!pti)
  177. return -ENOMEM;
  178. pti->thdev = thdev;
  179. pti->base = base;
  180. read_hw_config(pti);
  181. dev_set_drvdata(dev, pti);
  182. return 0;
  183. }
  184. static void intel_th_pti_remove(struct intel_th_device *thdev)
  185. {
  186. }
  187. static struct intel_th_driver intel_th_pti_driver = {
  188. .probe = intel_th_pti_probe,
  189. .remove = intel_th_pti_remove,
  190. .activate = intel_th_pti_activate,
  191. .deactivate = intel_th_pti_deactivate,
  192. .attr_group = &pti_output_group,
  193. .driver = {
  194. .name = "pti",
  195. .owner = THIS_MODULE,
  196. },
  197. };
  198. static const char * const lpp_dest_str[] = { "pti", "exi" };
  199. static ssize_t lpp_dest_show(struct device *dev, struct device_attribute *attr,
  200. char *buf)
  201. {
  202. struct pti_device *pti = dev_get_drvdata(dev);
  203. ssize_t ret = 0;
  204. int i;
  205. for (i = ARRAY_SIZE(lpp_dest_str) - 1; i >= 0; i--) {
  206. const char *fmt = pti->lpp_dest == i ? "[%s] " : "%s ";
  207. if (!(pti->lpp_dest_mask & BIT(i)))
  208. continue;
  209. ret += scnprintf(buf + ret, PAGE_SIZE - ret,
  210. fmt, lpp_dest_str[i]);
  211. }
  212. if (ret)
  213. buf[ret - 1] = '\n';
  214. return ret;
  215. }
  216. static ssize_t lpp_dest_store(struct device *dev, struct device_attribute *attr,
  217. const char *buf, size_t size)
  218. {
  219. struct pti_device *pti = dev_get_drvdata(dev);
  220. ssize_t ret = -EINVAL;
  221. int i;
  222. for (i = 0; i < ARRAY_SIZE(lpp_dest_str); i++)
  223. if (sysfs_streq(buf, lpp_dest_str[i]))
  224. break;
  225. if (i < ARRAY_SIZE(lpp_dest_str) && pti->lpp_dest_mask & BIT(i)) {
  226. pti->lpp_dest = i;
  227. ret = size;
  228. }
  229. return ret;
  230. }
  231. static DEVICE_ATTR_RW(lpp_dest);
  232. static struct attribute *lpp_output_attrs[] = {
  233. &dev_attr_mode.attr,
  234. &dev_attr_freerunning_clock.attr,
  235. &dev_attr_clock_divider.attr,
  236. &dev_attr_lpp_dest.attr,
  237. NULL,
  238. };
  239. static struct attribute_group lpp_output_group = {
  240. .attrs = lpp_output_attrs,
  241. };
  242. static struct intel_th_driver intel_th_lpp_driver = {
  243. .probe = intel_th_pti_probe,
  244. .remove = intel_th_pti_remove,
  245. .activate = intel_th_pti_activate,
  246. .deactivate = intel_th_pti_deactivate,
  247. .attr_group = &lpp_output_group,
  248. .driver = {
  249. .name = "lpp",
  250. .owner = THIS_MODULE,
  251. },
  252. };
  253. static int __init intel_th_pti_lpp_init(void)
  254. {
  255. int err;
  256. err = intel_th_driver_register(&intel_th_pti_driver);
  257. if (err)
  258. return err;
  259. err = intel_th_driver_register(&intel_th_lpp_driver);
  260. if (err) {
  261. intel_th_driver_unregister(&intel_th_pti_driver);
  262. return err;
  263. }
  264. return 0;
  265. }
  266. module_init(intel_th_pti_lpp_init);
  267. static void __exit intel_th_pti_lpp_exit(void)
  268. {
  269. intel_th_driver_unregister(&intel_th_pti_driver);
  270. intel_th_driver_unregister(&intel_th_lpp_driver);
  271. }
  272. module_exit(intel_th_pti_lpp_exit);
  273. MODULE_LICENSE("GPL v2");
  274. MODULE_DESCRIPTION("Intel(R) Trace Hub PTI/LPP output driver");
  275. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");