coresight-tmc.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
  2. *
  3. * Description: CoreSight Trace Memory Controller driver
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/types.h>
  17. #include <linux/device.h>
  18. #include <linux/io.h>
  19. #include <linux/err.h>
  20. #include <linux/fs.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/slab.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/of.h>
  28. #include <linux/coresight.h>
  29. #include <linux/amba/bus.h>
  30. #include "coresight-priv.h"
  31. #include "coresight-tmc.h"
  32. void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
  33. {
  34. /* Ensure formatter, unformatter and hardware fifo are empty */
  35. if (coresight_timeout(drvdata->base,
  36. TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
  37. dev_err(drvdata->dev,
  38. "timeout while waiting for TMC to be Ready\n");
  39. }
  40. }
  41. void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
  42. {
  43. u32 ffcr;
  44. ffcr = readl_relaxed(drvdata->base + TMC_FFCR);
  45. ffcr |= TMC_FFCR_STOP_ON_FLUSH;
  46. writel_relaxed(ffcr, drvdata->base + TMC_FFCR);
  47. ffcr |= BIT(TMC_FFCR_FLUSHMAN_BIT);
  48. writel_relaxed(ffcr, drvdata->base + TMC_FFCR);
  49. /* Ensure flush completes */
  50. if (coresight_timeout(drvdata->base,
  51. TMC_FFCR, TMC_FFCR_FLUSHMAN_BIT, 0)) {
  52. dev_err(drvdata->dev,
  53. "timeout while waiting for completion of Manual Flush\n");
  54. }
  55. tmc_wait_for_tmcready(drvdata);
  56. }
  57. void tmc_enable_hw(struct tmc_drvdata *drvdata)
  58. {
  59. writel_relaxed(TMC_CTL_CAPT_EN, drvdata->base + TMC_CTL);
  60. }
  61. void tmc_disable_hw(struct tmc_drvdata *drvdata)
  62. {
  63. writel_relaxed(0x0, drvdata->base + TMC_CTL);
  64. }
  65. static int tmc_read_prepare(struct tmc_drvdata *drvdata)
  66. {
  67. int ret = 0;
  68. switch (drvdata->config_type) {
  69. case TMC_CONFIG_TYPE_ETB:
  70. case TMC_CONFIG_TYPE_ETF:
  71. ret = tmc_read_prepare_etb(drvdata);
  72. break;
  73. case TMC_CONFIG_TYPE_ETR:
  74. ret = tmc_read_prepare_etr(drvdata);
  75. break;
  76. default:
  77. ret = -EINVAL;
  78. }
  79. if (!ret)
  80. dev_info(drvdata->dev, "TMC read start\n");
  81. return ret;
  82. }
  83. static int tmc_read_unprepare(struct tmc_drvdata *drvdata)
  84. {
  85. int ret = 0;
  86. switch (drvdata->config_type) {
  87. case TMC_CONFIG_TYPE_ETB:
  88. case TMC_CONFIG_TYPE_ETF:
  89. ret = tmc_read_unprepare_etb(drvdata);
  90. break;
  91. case TMC_CONFIG_TYPE_ETR:
  92. ret = tmc_read_unprepare_etr(drvdata);
  93. break;
  94. default:
  95. ret = -EINVAL;
  96. }
  97. if (!ret)
  98. dev_info(drvdata->dev, "TMC read end\n");
  99. return ret;
  100. }
  101. static int tmc_open(struct inode *inode, struct file *file)
  102. {
  103. int ret;
  104. struct tmc_drvdata *drvdata = container_of(file->private_data,
  105. struct tmc_drvdata, miscdev);
  106. ret = tmc_read_prepare(drvdata);
  107. if (ret)
  108. return ret;
  109. nonseekable_open(inode, file);
  110. dev_dbg(drvdata->dev, "%s: successfully opened\n", __func__);
  111. return 0;
  112. }
  113. static ssize_t tmc_read(struct file *file, char __user *data, size_t len,
  114. loff_t *ppos)
  115. {
  116. struct tmc_drvdata *drvdata = container_of(file->private_data,
  117. struct tmc_drvdata, miscdev);
  118. char *bufp = drvdata->buf + *ppos;
  119. if (*ppos + len > drvdata->len)
  120. len = drvdata->len - *ppos;
  121. if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
  122. if (bufp == (char *)(drvdata->vaddr + drvdata->size))
  123. bufp = drvdata->vaddr;
  124. else if (bufp > (char *)(drvdata->vaddr + drvdata->size))
  125. bufp -= drvdata->size;
  126. if ((bufp + len) > (char *)(drvdata->vaddr + drvdata->size))
  127. len = (char *)(drvdata->vaddr + drvdata->size) - bufp;
  128. }
  129. if (copy_to_user(data, bufp, len)) {
  130. dev_dbg(drvdata->dev, "%s: copy_to_user failed\n", __func__);
  131. return -EFAULT;
  132. }
  133. *ppos += len;
  134. dev_dbg(drvdata->dev, "%s: %zu bytes copied, %d bytes left\n",
  135. __func__, len, (int)(drvdata->len - *ppos));
  136. return len;
  137. }
  138. static int tmc_release(struct inode *inode, struct file *file)
  139. {
  140. int ret;
  141. struct tmc_drvdata *drvdata = container_of(file->private_data,
  142. struct tmc_drvdata, miscdev);
  143. ret = tmc_read_unprepare(drvdata);
  144. if (ret)
  145. return ret;
  146. dev_dbg(drvdata->dev, "%s: released\n", __func__);
  147. return 0;
  148. }
  149. static const struct file_operations tmc_fops = {
  150. .owner = THIS_MODULE,
  151. .open = tmc_open,
  152. .read = tmc_read,
  153. .release = tmc_release,
  154. .llseek = no_llseek,
  155. };
  156. static enum tmc_mem_intf_width tmc_get_memwidth(u32 devid)
  157. {
  158. enum tmc_mem_intf_width memwidth;
  159. /*
  160. * Excerpt from the TRM:
  161. *
  162. * DEVID::MEMWIDTH[10:8]
  163. * 0x2 Memory interface databus is 32 bits wide.
  164. * 0x3 Memory interface databus is 64 bits wide.
  165. * 0x4 Memory interface databus is 128 bits wide.
  166. * 0x5 Memory interface databus is 256 bits wide.
  167. */
  168. switch (BMVAL(devid, 8, 10)) {
  169. case 0x2:
  170. memwidth = TMC_MEM_INTF_WIDTH_32BITS;
  171. break;
  172. case 0x3:
  173. memwidth = TMC_MEM_INTF_WIDTH_64BITS;
  174. break;
  175. case 0x4:
  176. memwidth = TMC_MEM_INTF_WIDTH_128BITS;
  177. break;
  178. case 0x5:
  179. memwidth = TMC_MEM_INTF_WIDTH_256BITS;
  180. break;
  181. default:
  182. memwidth = 0;
  183. }
  184. return memwidth;
  185. }
  186. #define coresight_tmc_simple_func(name, offset) \
  187. coresight_simple_func(struct tmc_drvdata, NULL, name, offset)
  188. coresight_tmc_simple_func(rsz, TMC_RSZ);
  189. coresight_tmc_simple_func(sts, TMC_STS);
  190. coresight_tmc_simple_func(rrp, TMC_RRP);
  191. coresight_tmc_simple_func(rwp, TMC_RWP);
  192. coresight_tmc_simple_func(trg, TMC_TRG);
  193. coresight_tmc_simple_func(ctl, TMC_CTL);
  194. coresight_tmc_simple_func(ffsr, TMC_FFSR);
  195. coresight_tmc_simple_func(ffcr, TMC_FFCR);
  196. coresight_tmc_simple_func(mode, TMC_MODE);
  197. coresight_tmc_simple_func(pscr, TMC_PSCR);
  198. coresight_tmc_simple_func(devid, CORESIGHT_DEVID);
  199. static struct attribute *coresight_tmc_mgmt_attrs[] = {
  200. &dev_attr_rsz.attr,
  201. &dev_attr_sts.attr,
  202. &dev_attr_rrp.attr,
  203. &dev_attr_rwp.attr,
  204. &dev_attr_trg.attr,
  205. &dev_attr_ctl.attr,
  206. &dev_attr_ffsr.attr,
  207. &dev_attr_ffcr.attr,
  208. &dev_attr_mode.attr,
  209. &dev_attr_pscr.attr,
  210. &dev_attr_devid.attr,
  211. NULL,
  212. };
  213. static ssize_t trigger_cntr_show(struct device *dev,
  214. struct device_attribute *attr, char *buf)
  215. {
  216. struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
  217. unsigned long val = drvdata->trigger_cntr;
  218. return sprintf(buf, "%#lx\n", val);
  219. }
  220. static ssize_t trigger_cntr_store(struct device *dev,
  221. struct device_attribute *attr,
  222. const char *buf, size_t size)
  223. {
  224. int ret;
  225. unsigned long val;
  226. struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
  227. ret = kstrtoul(buf, 16, &val);
  228. if (ret)
  229. return ret;
  230. drvdata->trigger_cntr = val;
  231. return size;
  232. }
  233. static DEVICE_ATTR_RW(trigger_cntr);
  234. static struct attribute *coresight_tmc_attrs[] = {
  235. &dev_attr_trigger_cntr.attr,
  236. NULL,
  237. };
  238. static const struct attribute_group coresight_tmc_group = {
  239. .attrs = coresight_tmc_attrs,
  240. };
  241. static const struct attribute_group coresight_tmc_mgmt_group = {
  242. .attrs = coresight_tmc_mgmt_attrs,
  243. .name = "mgmt",
  244. };
  245. const struct attribute_group *coresight_tmc_groups[] = {
  246. &coresight_tmc_group,
  247. &coresight_tmc_mgmt_group,
  248. NULL,
  249. };
  250. static int tmc_probe(struct amba_device *adev, const struct amba_id *id)
  251. {
  252. int ret = 0;
  253. u32 devid;
  254. void __iomem *base;
  255. struct device *dev = &adev->dev;
  256. struct coresight_platform_data *pdata = NULL;
  257. struct tmc_drvdata *drvdata;
  258. struct resource *res = &adev->res;
  259. struct coresight_desc desc = { 0 };
  260. struct device_node *np = adev->dev.of_node;
  261. if (np) {
  262. pdata = of_get_coresight_platform_data(dev, np);
  263. if (IS_ERR(pdata)) {
  264. ret = PTR_ERR(pdata);
  265. goto out;
  266. }
  267. adev->dev.platform_data = pdata;
  268. }
  269. ret = -ENOMEM;
  270. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  271. if (!drvdata)
  272. goto out;
  273. drvdata->dev = &adev->dev;
  274. dev_set_drvdata(dev, drvdata);
  275. /* Validity for the resource is already checked by the AMBA core */
  276. base = devm_ioremap_resource(dev, res);
  277. if (IS_ERR(base)) {
  278. ret = PTR_ERR(base);
  279. goto out;
  280. }
  281. drvdata->base = base;
  282. spin_lock_init(&drvdata->spinlock);
  283. devid = readl_relaxed(drvdata->base + CORESIGHT_DEVID);
  284. drvdata->config_type = BMVAL(devid, 6, 7);
  285. drvdata->memwidth = tmc_get_memwidth(devid);
  286. if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
  287. if (np)
  288. ret = of_property_read_u32(np,
  289. "arm,buffer-size",
  290. &drvdata->size);
  291. if (ret)
  292. drvdata->size = SZ_1M;
  293. } else {
  294. drvdata->size = readl_relaxed(drvdata->base + TMC_RSZ) * 4;
  295. }
  296. pm_runtime_put(&adev->dev);
  297. desc.pdata = pdata;
  298. desc.dev = dev;
  299. desc.groups = coresight_tmc_groups;
  300. if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
  301. desc.type = CORESIGHT_DEV_TYPE_SINK;
  302. desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
  303. desc.ops = &tmc_etb_cs_ops;
  304. } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
  305. desc.type = CORESIGHT_DEV_TYPE_SINK;
  306. desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
  307. desc.ops = &tmc_etr_cs_ops;
  308. } else {
  309. desc.type = CORESIGHT_DEV_TYPE_LINKSINK;
  310. desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_FIFO;
  311. desc.ops = &tmc_etf_cs_ops;
  312. }
  313. drvdata->csdev = coresight_register(&desc);
  314. if (IS_ERR(drvdata->csdev)) {
  315. ret = PTR_ERR(drvdata->csdev);
  316. goto out;
  317. }
  318. drvdata->miscdev.name = pdata->name;
  319. drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
  320. drvdata->miscdev.fops = &tmc_fops;
  321. ret = misc_register(&drvdata->miscdev);
  322. if (ret)
  323. coresight_unregister(drvdata->csdev);
  324. out:
  325. return ret;
  326. }
  327. static struct amba_id tmc_ids[] = {
  328. {
  329. .id = 0x0003b961,
  330. .mask = 0x0003ffff,
  331. },
  332. { 0, 0},
  333. };
  334. static struct amba_driver tmc_driver = {
  335. .drv = {
  336. .name = "coresight-tmc",
  337. .owner = THIS_MODULE,
  338. .suppress_bind_attrs = true,
  339. },
  340. .probe = tmc_probe,
  341. .id_table = tmc_ids,
  342. };
  343. builtin_amba_driver(tmc_driver);