pti.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
  159. if (!res)
  160. return -ENODEV;
  161. base = devm_ioremap(dev, res->start, resource_size(res));
  162. if (!base)
  163. return -ENOMEM;
  164. pti = devm_kzalloc(dev, sizeof(*pti), GFP_KERNEL);
  165. if (!pti)
  166. return -ENOMEM;
  167. pti->thdev = thdev;
  168. pti->base = base;
  169. read_hw_config(pti);
  170. dev_set_drvdata(dev, pti);
  171. return 0;
  172. }
  173. static void intel_th_pti_remove(struct intel_th_device *thdev)
  174. {
  175. }
  176. static struct intel_th_driver intel_th_pti_driver = {
  177. .probe = intel_th_pti_probe,
  178. .remove = intel_th_pti_remove,
  179. .activate = intel_th_pti_activate,
  180. .deactivate = intel_th_pti_deactivate,
  181. .attr_group = &pti_output_group,
  182. .driver = {
  183. .name = "pti",
  184. .owner = THIS_MODULE,
  185. },
  186. };
  187. module_driver(intel_th_pti_driver,
  188. intel_th_driver_register,
  189. intel_th_driver_unregister);
  190. MODULE_LICENSE("GPL v2");
  191. MODULE_DESCRIPTION("Intel(R) Trace Hub PTI output driver");
  192. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");