pti.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Intel(R) Trace Hub PTI output driver
  3. *
  4. * Copyright (C) 2014-2015 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. };
  34. /* map PTI widths to MODE settings of PTI_CTL register */
  35. static const unsigned int pti_mode[] = {
  36. 0, 4, 8, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0,
  37. };
  38. static int pti_width_mode(unsigned int width)
  39. {
  40. int i;
  41. for (i = 0; i < ARRAY_SIZE(pti_mode); i++)
  42. if (pti_mode[i] == width)
  43. return i;
  44. return -EINVAL;
  45. }
  46. static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
  47. char *buf)
  48. {
  49. struct pti_device *pti = dev_get_drvdata(dev);
  50. return scnprintf(buf, PAGE_SIZE, "%d\n", pti_mode[pti->mode]);
  51. }
  52. static ssize_t mode_store(struct device *dev, struct device_attribute *attr,
  53. const char *buf, size_t size)
  54. {
  55. struct pti_device *pti = dev_get_drvdata(dev);
  56. unsigned long val;
  57. int ret;
  58. ret = kstrtoul(buf, 10, &val);
  59. if (ret)
  60. return ret;
  61. ret = pti_width_mode(val);
  62. if (ret < 0)
  63. return ret;
  64. pti->mode = ret;
  65. return size;
  66. }
  67. static DEVICE_ATTR_RW(mode);
  68. static ssize_t
  69. freerunning_clock_show(struct device *dev, struct device_attribute *attr,
  70. char *buf)
  71. {
  72. struct pti_device *pti = dev_get_drvdata(dev);
  73. return scnprintf(buf, PAGE_SIZE, "%d\n", pti->freeclk);
  74. }
  75. static ssize_t
  76. freerunning_clock_store(struct device *dev, struct device_attribute *attr,
  77. const char *buf, size_t size)
  78. {
  79. struct pti_device *pti = dev_get_drvdata(dev);
  80. unsigned long val;
  81. int ret;
  82. ret = kstrtoul(buf, 10, &val);
  83. if (ret)
  84. return ret;
  85. pti->freeclk = !!val;
  86. return size;
  87. }
  88. static DEVICE_ATTR_RW(freerunning_clock);
  89. static ssize_t
  90. clock_divider_show(struct device *dev, struct device_attribute *attr,
  91. char *buf)
  92. {
  93. struct pti_device *pti = dev_get_drvdata(dev);
  94. return scnprintf(buf, PAGE_SIZE, "%d\n", 1u << pti->clkdiv);
  95. }
  96. static ssize_t
  97. clock_divider_store(struct device *dev, struct device_attribute *attr,
  98. const char *buf, size_t size)
  99. {
  100. struct pti_device *pti = dev_get_drvdata(dev);
  101. unsigned long val;
  102. int ret;
  103. ret = kstrtoul(buf, 10, &val);
  104. if (ret)
  105. return ret;
  106. if (!is_power_of_2(val) || val > 8 || !val)
  107. return -EINVAL;
  108. pti->clkdiv = val;
  109. return size;
  110. }
  111. static DEVICE_ATTR_RW(clock_divider);
  112. static struct attribute *pti_output_attrs[] = {
  113. &dev_attr_mode.attr,
  114. &dev_attr_freerunning_clock.attr,
  115. &dev_attr_clock_divider.attr,
  116. NULL,
  117. };
  118. static struct attribute_group pti_output_group = {
  119. .attrs = pti_output_attrs,
  120. };
  121. static int intel_th_pti_activate(struct intel_th_device *thdev)
  122. {
  123. struct pti_device *pti = dev_get_drvdata(&thdev->dev);
  124. u32 ctl = PTI_EN;
  125. if (pti->patgen)
  126. ctl |= pti->patgen << __ffs(PTI_PATGENMODE);
  127. if (pti->freeclk)
  128. ctl |= PTI_FCEN;
  129. ctl |= pti->mode << __ffs(PTI_MODE);
  130. ctl |= pti->clkdiv << __ffs(PTI_CLKDIV);
  131. iowrite32(ctl, pti->base + REG_PTI_CTL);
  132. intel_th_trace_enable(thdev);
  133. return 0;
  134. }
  135. static void intel_th_pti_deactivate(struct intel_th_device *thdev)
  136. {
  137. struct pti_device *pti = dev_get_drvdata(&thdev->dev);
  138. intel_th_trace_disable(thdev);
  139. iowrite32(0, pti->base + REG_PTI_CTL);
  140. }
  141. static void read_hw_config(struct pti_device *pti)
  142. {
  143. u32 ctl = ioread32(pti->base + REG_PTI_CTL);
  144. pti->mode = (ctl & PTI_MODE) >> __ffs(PTI_MODE);
  145. pti->clkdiv = (ctl & PTI_CLKDIV) >> __ffs(PTI_CLKDIV);
  146. pti->freeclk = !!(ctl & PTI_FCEN);
  147. if (!pti_mode[pti->mode])
  148. pti->mode = pti_width_mode(4);
  149. if (!pti->clkdiv)
  150. pti->clkdiv = 1;
  151. }
  152. static int intel_th_pti_probe(struct intel_th_device *thdev)
  153. {
  154. struct device *dev = &thdev->dev;
  155. struct resource *res;
  156. struct pti_device *pti;
  157. void __iomem *base;
  158. int ret;
  159. res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
  160. if (!res)
  161. return -ENODEV;
  162. base = devm_ioremap(dev, res->start, resource_size(res));
  163. if (!base)
  164. return -ENOMEM;
  165. pti = devm_kzalloc(dev, sizeof(*pti), GFP_KERNEL);
  166. if (!pti)
  167. return -ENOMEM;
  168. pti->thdev = thdev;
  169. pti->base = base;
  170. read_hw_config(pti);
  171. ret = sysfs_create_group(&dev->kobj, &pti_output_group);
  172. if (ret)
  173. return ret;
  174. dev_set_drvdata(dev, pti);
  175. return 0;
  176. }
  177. static void intel_th_pti_remove(struct intel_th_device *thdev)
  178. {
  179. }
  180. static struct intel_th_driver intel_th_pti_driver = {
  181. .probe = intel_th_pti_probe,
  182. .remove = intel_th_pti_remove,
  183. .activate = intel_th_pti_activate,
  184. .deactivate = intel_th_pti_deactivate,
  185. .driver = {
  186. .name = "pti",
  187. .owner = THIS_MODULE,
  188. },
  189. };
  190. module_driver(intel_th_pti_driver,
  191. intel_th_driver_register,
  192. intel_th_driver_unregister);
  193. MODULE_LICENSE("GPL v2");
  194. MODULE_DESCRIPTION("Intel(R) Trace Hub PTI output driver");
  195. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");