coresight-csr.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* Copyright (c) 2012-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/of.h>
  21. #include <linux/of_coresight.h>
  22. #include <linux/coresight.h>
  23. #include "coresight-priv.h"
  24. #define csr_writel(drvdata, val, off) __raw_writel((val), drvdata->base + off)
  25. #define csr_readl(drvdata, off) __raw_readl(drvdata->base + off)
  26. #define CSR_LOCK(drvdata) \
  27. do { \
  28. mb(); \
  29. csr_writel(drvdata, 0x0, CORESIGHT_LAR); \
  30. } while (0)
  31. #define CSR_UNLOCK(drvdata) \
  32. do { \
  33. csr_writel(drvdata, CORESIGHT_UNLOCK, CORESIGHT_LAR); \
  34. mb(); \
  35. } while (0)
  36. #define CSR_SWDBGPWRCTRL (0x000)
  37. #define CSR_SWDBGPWRACK (0x004)
  38. #define CSR_SWSPADREG0 (0x008)
  39. #define CSR_SWSPADREG1 (0x00C)
  40. #define CSR_STMTRANSCTRL (0x010)
  41. #define CSR_STMAWIDCTRL (0x014)
  42. #define CSR_STMCHNOFST0 (0x018)
  43. #define CSR_STMCHNOFST1 (0x01C)
  44. #define CSR_STMEXTHWCTRL0 (0x020)
  45. #define CSR_STMEXTHWCTRL1 (0x024)
  46. #define CSR_STMEXTHWCTRL2 (0x028)
  47. #define CSR_STMEXTHWCTRL3 (0x02C)
  48. #define CSR_USBBAMCTRL (0x030)
  49. #define CSR_USBFLSHCTRL (0x034)
  50. #define CSR_TIMESTAMPCTRL (0x038)
  51. #define CSR_AOTIMEVAL0 (0x03C)
  52. #define CSR_AOTIMEVAL1 (0x040)
  53. #define CSR_QDSSTIMEVAL0 (0x044)
  54. #define CSR_QDSSTIMEVAL1 (0x048)
  55. #define CSR_QDSSTIMELOAD0 (0x04C)
  56. #define CSR_QDSSTIMELOAD1 (0x050)
  57. #define CSR_DAPMSAVAL (0x054)
  58. #define CSR_QDSSCLKVOTE (0x058)
  59. #define CSR_QDSSCLKIPI (0x05C)
  60. #define CSR_QDSSPWRREQIGNORE (0x060)
  61. #define CSR_QDSSSPARE (0x064)
  62. #define CSR_IPCAT (0x068)
  63. #define CSR_BYTECNTVAL (0x06C)
  64. #define BLKSIZE_256 0
  65. #define BLKSIZE_512 1
  66. #define BLKSIZE_1024 2
  67. #define BLKSIZE_2048 3
  68. struct csr_drvdata {
  69. void __iomem *base;
  70. phys_addr_t pbase;
  71. struct device *dev;
  72. struct coresight_device *csdev;
  73. uint32_t blksize;
  74. };
  75. static struct csr_drvdata *csrdrvdata;
  76. void msm_qdss_csr_enable_bam_to_usb(void)
  77. {
  78. struct csr_drvdata *drvdata = csrdrvdata;
  79. uint32_t usbbamctrl, usbflshctrl;
  80. CSR_UNLOCK(drvdata);
  81. usbbamctrl = csr_readl(drvdata, CSR_USBBAMCTRL);
  82. usbbamctrl = (usbbamctrl & ~0x3) | drvdata->blksize;
  83. csr_writel(drvdata, usbbamctrl, CSR_USBBAMCTRL);
  84. usbflshctrl = csr_readl(drvdata, CSR_USBFLSHCTRL);
  85. usbflshctrl = (usbflshctrl & ~0x3FFFC) | (0xFFFF << 2);
  86. csr_writel(drvdata, usbflshctrl, CSR_USBFLSHCTRL);
  87. usbflshctrl |= 0x2;
  88. csr_writel(drvdata, usbflshctrl, CSR_USBFLSHCTRL);
  89. usbbamctrl |= 0x4;
  90. csr_writel(drvdata, usbbamctrl, CSR_USBBAMCTRL);
  91. CSR_LOCK(drvdata);
  92. }
  93. EXPORT_SYMBOL(msm_qdss_csr_enable_bam_to_usb);
  94. void msm_qdss_csr_disable_bam_to_usb(void)
  95. {
  96. struct csr_drvdata *drvdata = csrdrvdata;
  97. uint32_t usbbamctrl;
  98. CSR_UNLOCK(drvdata);
  99. usbbamctrl = csr_readl(drvdata, CSR_USBBAMCTRL);
  100. usbbamctrl &= (~0x4);
  101. csr_writel(drvdata, usbbamctrl, CSR_USBBAMCTRL);
  102. CSR_LOCK(drvdata);
  103. }
  104. EXPORT_SYMBOL(msm_qdss_csr_disable_bam_to_usb);
  105. void msm_qdss_csr_disable_flush(void)
  106. {
  107. struct csr_drvdata *drvdata = csrdrvdata;
  108. uint32_t usbflshctrl;
  109. CSR_UNLOCK(drvdata);
  110. usbflshctrl = csr_readl(drvdata, CSR_USBFLSHCTRL);
  111. usbflshctrl &= ~0x2;
  112. csr_writel(drvdata, usbflshctrl, CSR_USBFLSHCTRL);
  113. CSR_LOCK(drvdata);
  114. }
  115. EXPORT_SYMBOL(msm_qdss_csr_disable_flush);
  116. int coresight_csr_hwctrl_set(uint64_t addr, uint32_t val)
  117. {
  118. struct csr_drvdata *drvdata = csrdrvdata;
  119. int ret = 0;
  120. CSR_UNLOCK(drvdata);
  121. if (addr == (drvdata->pbase + CSR_STMEXTHWCTRL0))
  122. csr_writel(drvdata, val, CSR_STMEXTHWCTRL0);
  123. else if (addr == (drvdata->pbase + CSR_STMEXTHWCTRL1))
  124. csr_writel(drvdata, val, CSR_STMEXTHWCTRL1);
  125. else if (addr == (drvdata->pbase + CSR_STMEXTHWCTRL2))
  126. csr_writel(drvdata, val, CSR_STMEXTHWCTRL2);
  127. else if (addr == (drvdata->pbase + CSR_STMEXTHWCTRL3))
  128. csr_writel(drvdata, val, CSR_STMEXTHWCTRL3);
  129. else
  130. ret = -EINVAL;
  131. CSR_LOCK(drvdata);
  132. return ret;
  133. }
  134. EXPORT_SYMBOL(coresight_csr_hwctrl_set);
  135. void coresight_csr_set_byte_cntr(uint32_t count)
  136. {
  137. struct csr_drvdata *drvdata = csrdrvdata;
  138. CSR_UNLOCK(drvdata);
  139. csr_writel(drvdata, count, CSR_BYTECNTVAL);
  140. mb();
  141. CSR_LOCK(drvdata);
  142. }
  143. EXPORT_SYMBOL(coresight_csr_set_byte_cntr);
  144. static int __devinit csr_probe(struct platform_device *pdev)
  145. {
  146. int ret;
  147. struct device *dev = &pdev->dev;
  148. struct coresight_platform_data *pdata;
  149. struct csr_drvdata *drvdata;
  150. struct resource *res;
  151. struct coresight_desc *desc;
  152. if (coresight_fuse_access_disabled())
  153. return -EPERM;
  154. if (pdev->dev.of_node) {
  155. pdata = of_get_coresight_platform_data(dev, pdev->dev.of_node);
  156. if (IS_ERR(pdata))
  157. return PTR_ERR(pdata);
  158. pdev->dev.platform_data = pdata;
  159. }
  160. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  161. if (!drvdata)
  162. return -ENOMEM;
  163. /* Store the driver data pointer for use in exported functions */
  164. csrdrvdata = drvdata;
  165. drvdata->dev = &pdev->dev;
  166. platform_set_drvdata(pdev, drvdata);
  167. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csr-base");
  168. if (!res)
  169. return -ENODEV;
  170. drvdata->pbase = res->start;
  171. drvdata->base = devm_ioremap(dev, res->start, resource_size(res));
  172. if (!drvdata->base)
  173. return -ENOMEM;
  174. if (pdev->dev.of_node) {
  175. ret = of_property_read_u32(pdev->dev.of_node, "qcom,blk-size",
  176. &drvdata->blksize);
  177. if (ret)
  178. drvdata->blksize = BLKSIZE_256;
  179. }
  180. desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
  181. if (!desc)
  182. return -ENOMEM;
  183. desc->type = CORESIGHT_DEV_TYPE_NONE;
  184. desc->pdata = pdev->dev.platform_data;
  185. desc->dev = &pdev->dev;
  186. desc->owner = THIS_MODULE;
  187. drvdata->csdev = coresight_register(desc);
  188. if (IS_ERR(drvdata->csdev))
  189. return PTR_ERR(drvdata->csdev);
  190. dev_info(dev, "CSR initialized\n");
  191. return 0;
  192. }
  193. static int __devexit csr_remove(struct platform_device *pdev)
  194. {
  195. struct csr_drvdata *drvdata = platform_get_drvdata(pdev);
  196. coresight_unregister(drvdata->csdev);
  197. return 0;
  198. }
  199. static struct of_device_id csr_match[] = {
  200. {.compatible = "qcom,coresight-csr"},
  201. {}
  202. };
  203. static struct platform_driver csr_driver = {
  204. .probe = csr_probe,
  205. .remove = __devexit_p(csr_remove),
  206. .driver = {
  207. .name = "coresight-csr",
  208. .owner = THIS_MODULE,
  209. .of_match_table = csr_match,
  210. },
  211. };
  212. static int __init csr_init(void)
  213. {
  214. return platform_driver_register(&csr_driver);
  215. }
  216. module_init(csr_init);
  217. static void __exit csr_exit(void)
  218. {
  219. platform_driver_unregister(&csr_driver);
  220. }
  221. module_exit(csr_exit);
  222. MODULE_LICENSE("GPL v2");
  223. MODULE_DESCRIPTION("CoreSight CSR driver");