coresight-tmc.c 9.8 KB

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