coresight-tmc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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. #define TMC_RSZ 0x004
  32. #define TMC_STS 0x00c
  33. #define TMC_RRD 0x010
  34. #define TMC_RRP 0x014
  35. #define TMC_RWP 0x018
  36. #define TMC_TRG 0x01c
  37. #define TMC_CTL 0x020
  38. #define TMC_RWD 0x024
  39. #define TMC_MODE 0x028
  40. #define TMC_LBUFLEVEL 0x02c
  41. #define TMC_CBUFLEVEL 0x030
  42. #define TMC_BUFWM 0x034
  43. #define TMC_RRPHI 0x038
  44. #define TMC_RWPHI 0x03c
  45. #define TMC_AXICTL 0x110
  46. #define TMC_DBALO 0x118
  47. #define TMC_DBAHI 0x11c
  48. #define TMC_FFSR 0x300
  49. #define TMC_FFCR 0x304
  50. #define TMC_PSCR 0x308
  51. #define TMC_ITMISCOP0 0xee0
  52. #define TMC_ITTRFLIN 0xee8
  53. #define TMC_ITATBDATA0 0xeec
  54. #define TMC_ITATBCTR2 0xef0
  55. #define TMC_ITATBCTR1 0xef4
  56. #define TMC_ITATBCTR0 0xef8
  57. /* register description */
  58. /* TMC_CTL - 0x020 */
  59. #define TMC_CTL_CAPT_EN BIT(0)
  60. /* TMC_STS - 0x00C */
  61. #define TMC_STS_TRIGGERED BIT(1)
  62. /* TMC_AXICTL - 0x110 */
  63. #define TMC_AXICTL_PROT_CTL_B0 BIT(0)
  64. #define TMC_AXICTL_PROT_CTL_B1 BIT(1)
  65. #define TMC_AXICTL_SCT_GAT_MODE BIT(7)
  66. #define TMC_AXICTL_WR_BURST_LEN 0xF00
  67. /* TMC_FFCR - 0x304 */
  68. #define TMC_FFCR_EN_FMT BIT(0)
  69. #define TMC_FFCR_EN_TI BIT(1)
  70. #define TMC_FFCR_FON_FLIN BIT(4)
  71. #define TMC_FFCR_FON_TRIG_EVT BIT(5)
  72. #define TMC_FFCR_FLUSHMAN BIT(6)
  73. #define TMC_FFCR_TRIGON_TRIGIN BIT(8)
  74. #define TMC_FFCR_STOP_ON_FLUSH BIT(12)
  75. #define TMC_STS_TRIGGERED_BIT 2
  76. #define TMC_FFCR_FLUSHMAN_BIT 6
  77. enum tmc_config_type {
  78. TMC_CONFIG_TYPE_ETB,
  79. TMC_CONFIG_TYPE_ETR,
  80. TMC_CONFIG_TYPE_ETF,
  81. };
  82. enum tmc_mode {
  83. TMC_MODE_CIRCULAR_BUFFER,
  84. TMC_MODE_SOFTWARE_FIFO,
  85. TMC_MODE_HARDWARE_FIFO,
  86. };
  87. enum tmc_mem_intf_width {
  88. TMC_MEM_INTF_WIDTH_32BITS = 0x2,
  89. TMC_MEM_INTF_WIDTH_64BITS = 0x3,
  90. TMC_MEM_INTF_WIDTH_128BITS = 0x4,
  91. TMC_MEM_INTF_WIDTH_256BITS = 0x5,
  92. };
  93. /**
  94. * struct tmc_drvdata - specifics associated to an TMC component
  95. * @base: memory mapped base address for this component.
  96. * @dev: the device entity associated to this component.
  97. * @csdev: component vitals needed by the framework.
  98. * @miscdev: specifics to handle "/dev/xyz.tmc" entry.
  99. * @spinlock: only one at a time pls.
  100. * @read_count: manages preparation of buffer for reading.
  101. * @buf: area of memory where trace data get sent.
  102. * @paddr: DMA start location in RAM.
  103. * @vaddr: virtual representation of @paddr.
  104. * @size: @buf size.
  105. * @enable: this TMC is being used.
  106. * @config_type: TMC variant, must be of type @tmc_config_type.
  107. * @trigger_cntr: amount of words to store after a trigger.
  108. */
  109. struct tmc_drvdata {
  110. void __iomem *base;
  111. struct device *dev;
  112. struct coresight_device *csdev;
  113. struct miscdevice miscdev;
  114. spinlock_t spinlock;
  115. int read_count;
  116. bool reading;
  117. char *buf;
  118. dma_addr_t paddr;
  119. void *vaddr;
  120. u32 size;
  121. bool enable;
  122. enum tmc_config_type config_type;
  123. u32 trigger_cntr;
  124. };
  125. static void tmc_wait_for_ready(struct tmc_drvdata *drvdata)
  126. {
  127. /* Ensure formatter, unformatter and hardware fifo are empty */
  128. if (coresight_timeout(drvdata->base,
  129. TMC_STS, TMC_STS_TRIGGERED_BIT, 1)) {
  130. dev_err(drvdata->dev,
  131. "timeout observed when probing at offset %#x\n",
  132. TMC_STS);
  133. }
  134. }
  135. static void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
  136. {
  137. u32 ffcr;
  138. ffcr = readl_relaxed(drvdata->base + TMC_FFCR);
  139. ffcr |= TMC_FFCR_STOP_ON_FLUSH;
  140. writel_relaxed(ffcr, drvdata->base + TMC_FFCR);
  141. ffcr |= TMC_FFCR_FLUSHMAN;
  142. writel_relaxed(ffcr, drvdata->base + TMC_FFCR);
  143. /* Ensure flush completes */
  144. if (coresight_timeout(drvdata->base,
  145. TMC_FFCR, TMC_FFCR_FLUSHMAN_BIT, 0)) {
  146. dev_err(drvdata->dev,
  147. "timeout observed when probing at offset %#x\n",
  148. TMC_FFCR);
  149. }
  150. tmc_wait_for_ready(drvdata);
  151. }
  152. static void tmc_enable_hw(struct tmc_drvdata *drvdata)
  153. {
  154. writel_relaxed(TMC_CTL_CAPT_EN, drvdata->base + TMC_CTL);
  155. }
  156. static void tmc_disable_hw(struct tmc_drvdata *drvdata)
  157. {
  158. writel_relaxed(0x0, drvdata->base + TMC_CTL);
  159. }
  160. static void tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
  161. {
  162. /* Zero out the memory to help with debug */
  163. memset(drvdata->buf, 0, drvdata->size);
  164. CS_UNLOCK(drvdata->base);
  165. writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
  166. writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI |
  167. TMC_FFCR_FON_FLIN | TMC_FFCR_FON_TRIG_EVT |
  168. TMC_FFCR_TRIGON_TRIGIN,
  169. drvdata->base + TMC_FFCR);
  170. writel_relaxed(drvdata->trigger_cntr, drvdata->base + TMC_TRG);
  171. tmc_enable_hw(drvdata);
  172. CS_LOCK(drvdata->base);
  173. }
  174. static void tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
  175. {
  176. u32 axictl;
  177. /* Zero out the memory to help with debug */
  178. memset(drvdata->vaddr, 0, drvdata->size);
  179. CS_UNLOCK(drvdata->base);
  180. writel_relaxed(drvdata->size / 4, drvdata->base + TMC_RSZ);
  181. writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
  182. axictl = readl_relaxed(drvdata->base + TMC_AXICTL);
  183. axictl |= TMC_AXICTL_WR_BURST_LEN;
  184. writel_relaxed(axictl, drvdata->base + TMC_AXICTL);
  185. axictl &= ~TMC_AXICTL_SCT_GAT_MODE;
  186. writel_relaxed(axictl, drvdata->base + TMC_AXICTL);
  187. axictl = (axictl &
  188. ~(TMC_AXICTL_PROT_CTL_B0 | TMC_AXICTL_PROT_CTL_B1)) |
  189. TMC_AXICTL_PROT_CTL_B1;
  190. writel_relaxed(axictl, drvdata->base + TMC_AXICTL);
  191. writel_relaxed(drvdata->paddr, drvdata->base + TMC_DBALO);
  192. writel_relaxed(0x0, drvdata->base + TMC_DBAHI);
  193. writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI |
  194. TMC_FFCR_FON_FLIN | TMC_FFCR_FON_TRIG_EVT |
  195. TMC_FFCR_TRIGON_TRIGIN,
  196. drvdata->base + TMC_FFCR);
  197. writel_relaxed(drvdata->trigger_cntr, drvdata->base + TMC_TRG);
  198. tmc_enable_hw(drvdata);
  199. CS_LOCK(drvdata->base);
  200. }
  201. static void tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
  202. {
  203. CS_UNLOCK(drvdata->base);
  204. writel_relaxed(TMC_MODE_HARDWARE_FIFO, drvdata->base + TMC_MODE);
  205. writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI,
  206. drvdata->base + TMC_FFCR);
  207. writel_relaxed(0x0, drvdata->base + TMC_BUFWM);
  208. tmc_enable_hw(drvdata);
  209. CS_LOCK(drvdata->base);
  210. }
  211. static int tmc_enable(struct tmc_drvdata *drvdata, enum tmc_mode mode)
  212. {
  213. unsigned long flags;
  214. spin_lock_irqsave(&drvdata->spinlock, flags);
  215. if (drvdata->reading) {
  216. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  217. return -EBUSY;
  218. }
  219. if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
  220. tmc_etb_enable_hw(drvdata);
  221. } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
  222. tmc_etr_enable_hw(drvdata);
  223. } else {
  224. if (mode == TMC_MODE_CIRCULAR_BUFFER)
  225. tmc_etb_enable_hw(drvdata);
  226. else
  227. tmc_etf_enable_hw(drvdata);
  228. }
  229. drvdata->enable = true;
  230. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  231. dev_info(drvdata->dev, "TMC enabled\n");
  232. return 0;
  233. }
  234. static int tmc_enable_sink(struct coresight_device *csdev, u32 mode)
  235. {
  236. struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  237. return tmc_enable(drvdata, TMC_MODE_CIRCULAR_BUFFER);
  238. }
  239. static int tmc_enable_link(struct coresight_device *csdev, int inport,
  240. int outport)
  241. {
  242. struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  243. return tmc_enable(drvdata, TMC_MODE_HARDWARE_FIFO);
  244. }
  245. static void tmc_etb_dump_hw(struct tmc_drvdata *drvdata)
  246. {
  247. enum tmc_mem_intf_width memwidth;
  248. u8 memwords;
  249. char *bufp;
  250. u32 read_data;
  251. int i;
  252. memwidth = BMVAL(readl_relaxed(drvdata->base + CORESIGHT_DEVID), 8, 10);
  253. if (memwidth == TMC_MEM_INTF_WIDTH_32BITS)
  254. memwords = 1;
  255. else if (memwidth == TMC_MEM_INTF_WIDTH_64BITS)
  256. memwords = 2;
  257. else if (memwidth == TMC_MEM_INTF_WIDTH_128BITS)
  258. memwords = 4;
  259. else
  260. memwords = 8;
  261. bufp = drvdata->buf;
  262. while (1) {
  263. for (i = 0; i < memwords; i++) {
  264. read_data = readl_relaxed(drvdata->base + TMC_RRD);
  265. if (read_data == 0xFFFFFFFF)
  266. return;
  267. memcpy(bufp, &read_data, 4);
  268. bufp += 4;
  269. }
  270. }
  271. }
  272. static void tmc_etb_disable_hw(struct tmc_drvdata *drvdata)
  273. {
  274. CS_UNLOCK(drvdata->base);
  275. tmc_flush_and_stop(drvdata);
  276. tmc_etb_dump_hw(drvdata);
  277. tmc_disable_hw(drvdata);
  278. CS_LOCK(drvdata->base);
  279. }
  280. static void tmc_etr_dump_hw(struct tmc_drvdata *drvdata)
  281. {
  282. u32 rwp, val;
  283. rwp = readl_relaxed(drvdata->base + TMC_RWP);
  284. val = readl_relaxed(drvdata->base + TMC_STS);
  285. /* How much memory do we still have */
  286. if (val & BIT(0))
  287. drvdata->buf = drvdata->vaddr + rwp - drvdata->paddr;
  288. else
  289. drvdata->buf = drvdata->vaddr;
  290. }
  291. static void tmc_etr_disable_hw(struct tmc_drvdata *drvdata)
  292. {
  293. CS_UNLOCK(drvdata->base);
  294. tmc_flush_and_stop(drvdata);
  295. tmc_etr_dump_hw(drvdata);
  296. tmc_disable_hw(drvdata);
  297. CS_LOCK(drvdata->base);
  298. }
  299. static void tmc_etf_disable_hw(struct tmc_drvdata *drvdata)
  300. {
  301. CS_UNLOCK(drvdata->base);
  302. tmc_flush_and_stop(drvdata);
  303. tmc_disable_hw(drvdata);
  304. CS_LOCK(drvdata->base);
  305. }
  306. static void tmc_disable(struct tmc_drvdata *drvdata, enum tmc_mode mode)
  307. {
  308. unsigned long flags;
  309. spin_lock_irqsave(&drvdata->spinlock, flags);
  310. if (drvdata->reading)
  311. goto out;
  312. if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
  313. tmc_etb_disable_hw(drvdata);
  314. } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
  315. tmc_etr_disable_hw(drvdata);
  316. } else {
  317. if (mode == TMC_MODE_CIRCULAR_BUFFER)
  318. tmc_etb_disable_hw(drvdata);
  319. else
  320. tmc_etf_disable_hw(drvdata);
  321. }
  322. out:
  323. drvdata->enable = false;
  324. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  325. dev_info(drvdata->dev, "TMC disabled\n");
  326. }
  327. static void tmc_disable_sink(struct coresight_device *csdev)
  328. {
  329. struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  330. tmc_disable(drvdata, TMC_MODE_CIRCULAR_BUFFER);
  331. }
  332. static void tmc_disable_link(struct coresight_device *csdev, int inport,
  333. int outport)
  334. {
  335. struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  336. tmc_disable(drvdata, TMC_MODE_HARDWARE_FIFO);
  337. }
  338. static const struct coresight_ops_sink tmc_sink_ops = {
  339. .enable = tmc_enable_sink,
  340. .disable = tmc_disable_sink,
  341. };
  342. static const struct coresight_ops_link tmc_link_ops = {
  343. .enable = tmc_enable_link,
  344. .disable = tmc_disable_link,
  345. };
  346. static const struct coresight_ops tmc_etb_cs_ops = {
  347. .sink_ops = &tmc_sink_ops,
  348. };
  349. static const struct coresight_ops tmc_etr_cs_ops = {
  350. .sink_ops = &tmc_sink_ops,
  351. };
  352. static const struct coresight_ops tmc_etf_cs_ops = {
  353. .sink_ops = &tmc_sink_ops,
  354. .link_ops = &tmc_link_ops,
  355. };
  356. static int tmc_read_prepare(struct tmc_drvdata *drvdata)
  357. {
  358. int ret;
  359. unsigned long flags;
  360. enum tmc_mode mode;
  361. spin_lock_irqsave(&drvdata->spinlock, flags);
  362. if (!drvdata->enable)
  363. goto out;
  364. if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
  365. tmc_etb_disable_hw(drvdata);
  366. } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
  367. tmc_etr_disable_hw(drvdata);
  368. } else {
  369. mode = readl_relaxed(drvdata->base + TMC_MODE);
  370. if (mode == TMC_MODE_CIRCULAR_BUFFER) {
  371. tmc_etb_disable_hw(drvdata);
  372. } else {
  373. ret = -ENODEV;
  374. goto err;
  375. }
  376. }
  377. out:
  378. drvdata->reading = true;
  379. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  380. dev_info(drvdata->dev, "TMC read start\n");
  381. return 0;
  382. err:
  383. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  384. return ret;
  385. }
  386. static void tmc_read_unprepare(struct tmc_drvdata *drvdata)
  387. {
  388. unsigned long flags;
  389. enum tmc_mode mode;
  390. spin_lock_irqsave(&drvdata->spinlock, flags);
  391. if (!drvdata->enable)
  392. goto out;
  393. if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
  394. tmc_etb_enable_hw(drvdata);
  395. } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
  396. tmc_etr_enable_hw(drvdata);
  397. } else {
  398. mode = readl_relaxed(drvdata->base + TMC_MODE);
  399. if (mode == TMC_MODE_CIRCULAR_BUFFER)
  400. tmc_etb_enable_hw(drvdata);
  401. }
  402. out:
  403. drvdata->reading = false;
  404. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  405. dev_info(drvdata->dev, "TMC read end\n");
  406. }
  407. static int tmc_open(struct inode *inode, struct file *file)
  408. {
  409. struct tmc_drvdata *drvdata = container_of(file->private_data,
  410. struct tmc_drvdata, miscdev);
  411. int ret = 0;
  412. if (drvdata->read_count++)
  413. goto out;
  414. ret = tmc_read_prepare(drvdata);
  415. if (ret)
  416. return ret;
  417. out:
  418. nonseekable_open(inode, file);
  419. dev_dbg(drvdata->dev, "%s: successfully opened\n", __func__);
  420. return 0;
  421. }
  422. static ssize_t tmc_read(struct file *file, char __user *data, size_t len,
  423. loff_t *ppos)
  424. {
  425. struct tmc_drvdata *drvdata = container_of(file->private_data,
  426. struct tmc_drvdata, miscdev);
  427. char *bufp = drvdata->buf + *ppos;
  428. if (*ppos + len > drvdata->size)
  429. len = drvdata->size - *ppos;
  430. if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
  431. if (bufp == (char *)(drvdata->vaddr + drvdata->size))
  432. bufp = drvdata->vaddr;
  433. else if (bufp > (char *)(drvdata->vaddr + drvdata->size))
  434. bufp -= drvdata->size;
  435. if ((bufp + len) > (char *)(drvdata->vaddr + drvdata->size))
  436. len = (char *)(drvdata->vaddr + drvdata->size) - bufp;
  437. }
  438. if (copy_to_user(data, bufp, len)) {
  439. dev_dbg(drvdata->dev, "%s: copy_to_user failed\n", __func__);
  440. return -EFAULT;
  441. }
  442. *ppos += len;
  443. dev_dbg(drvdata->dev, "%s: %zu bytes copied, %d bytes left\n",
  444. __func__, len, (int)(drvdata->size - *ppos));
  445. return len;
  446. }
  447. static int tmc_release(struct inode *inode, struct file *file)
  448. {
  449. struct tmc_drvdata *drvdata = container_of(file->private_data,
  450. struct tmc_drvdata, miscdev);
  451. if (--drvdata->read_count) {
  452. if (drvdata->read_count < 0) {
  453. dev_err(drvdata->dev, "mismatched close\n");
  454. drvdata->read_count = 0;
  455. }
  456. goto out;
  457. }
  458. tmc_read_unprepare(drvdata);
  459. out:
  460. dev_dbg(drvdata->dev, "%s: released\n", __func__);
  461. return 0;
  462. }
  463. static const struct file_operations tmc_fops = {
  464. .owner = THIS_MODULE,
  465. .open = tmc_open,
  466. .read = tmc_read,
  467. .release = tmc_release,
  468. .llseek = no_llseek,
  469. };
  470. static ssize_t status_show(struct device *dev,
  471. struct device_attribute *attr, char *buf)
  472. {
  473. unsigned long flags;
  474. u32 tmc_rsz, tmc_sts, tmc_rrp, tmc_rwp, tmc_trg;
  475. u32 tmc_ctl, tmc_ffsr, tmc_ffcr, tmc_mode, tmc_pscr;
  476. u32 devid;
  477. struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
  478. pm_runtime_get_sync(drvdata->dev);
  479. spin_lock_irqsave(&drvdata->spinlock, flags);
  480. CS_UNLOCK(drvdata->base);
  481. tmc_rsz = readl_relaxed(drvdata->base + TMC_RSZ);
  482. tmc_sts = readl_relaxed(drvdata->base + TMC_STS);
  483. tmc_rrp = readl_relaxed(drvdata->base + TMC_RRP);
  484. tmc_rwp = readl_relaxed(drvdata->base + TMC_RWP);
  485. tmc_trg = readl_relaxed(drvdata->base + TMC_TRG);
  486. tmc_ctl = readl_relaxed(drvdata->base + TMC_CTL);
  487. tmc_ffsr = readl_relaxed(drvdata->base + TMC_FFSR);
  488. tmc_ffcr = readl_relaxed(drvdata->base + TMC_FFCR);
  489. tmc_mode = readl_relaxed(drvdata->base + TMC_MODE);
  490. tmc_pscr = readl_relaxed(drvdata->base + TMC_PSCR);
  491. devid = readl_relaxed(drvdata->base + CORESIGHT_DEVID);
  492. CS_LOCK(drvdata->base);
  493. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  494. pm_runtime_put(drvdata->dev);
  495. return sprintf(buf,
  496. "Depth:\t\t0x%x\n"
  497. "Status:\t\t0x%x\n"
  498. "RAM read ptr:\t0x%x\n"
  499. "RAM wrt ptr:\t0x%x\n"
  500. "Trigger cnt:\t0x%x\n"
  501. "Control:\t0x%x\n"
  502. "Flush status:\t0x%x\n"
  503. "Flush ctrl:\t0x%x\n"
  504. "Mode:\t\t0x%x\n"
  505. "PSRC:\t\t0x%x\n"
  506. "DEVID:\t\t0x%x\n",
  507. tmc_rsz, tmc_sts, tmc_rrp, tmc_rwp, tmc_trg,
  508. tmc_ctl, tmc_ffsr, tmc_ffcr, tmc_mode, tmc_pscr, devid);
  509. return -EINVAL;
  510. }
  511. static DEVICE_ATTR_RO(status);
  512. static ssize_t trigger_cntr_show(struct device *dev,
  513. struct device_attribute *attr, char *buf)
  514. {
  515. struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
  516. unsigned long val = drvdata->trigger_cntr;
  517. return sprintf(buf, "%#lx\n", val);
  518. }
  519. static ssize_t trigger_cntr_store(struct device *dev,
  520. struct device_attribute *attr,
  521. const char *buf, size_t size)
  522. {
  523. int ret;
  524. unsigned long val;
  525. struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
  526. ret = kstrtoul(buf, 16, &val);
  527. if (ret)
  528. return ret;
  529. drvdata->trigger_cntr = val;
  530. return size;
  531. }
  532. static DEVICE_ATTR_RW(trigger_cntr);
  533. static struct attribute *coresight_etb_attrs[] = {
  534. &dev_attr_trigger_cntr.attr,
  535. &dev_attr_status.attr,
  536. NULL,
  537. };
  538. ATTRIBUTE_GROUPS(coresight_etb);
  539. static struct attribute *coresight_etr_attrs[] = {
  540. &dev_attr_trigger_cntr.attr,
  541. &dev_attr_status.attr,
  542. NULL,
  543. };
  544. ATTRIBUTE_GROUPS(coresight_etr);
  545. static struct attribute *coresight_etf_attrs[] = {
  546. &dev_attr_trigger_cntr.attr,
  547. &dev_attr_status.attr,
  548. NULL,
  549. };
  550. ATTRIBUTE_GROUPS(coresight_etf);
  551. static int tmc_probe(struct amba_device *adev, const struct amba_id *id)
  552. {
  553. int ret = 0;
  554. u32 devid;
  555. void __iomem *base;
  556. struct device *dev = &adev->dev;
  557. struct coresight_platform_data *pdata = NULL;
  558. struct tmc_drvdata *drvdata;
  559. struct resource *res = &adev->res;
  560. struct coresight_desc *desc;
  561. struct device_node *np = adev->dev.of_node;
  562. if (np) {
  563. pdata = of_get_coresight_platform_data(dev, np);
  564. if (IS_ERR(pdata))
  565. return PTR_ERR(pdata);
  566. adev->dev.platform_data = pdata;
  567. }
  568. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  569. if (!drvdata)
  570. return -ENOMEM;
  571. drvdata->dev = &adev->dev;
  572. dev_set_drvdata(dev, drvdata);
  573. /* Validity for the resource is already checked by the AMBA core */
  574. base = devm_ioremap_resource(dev, res);
  575. if (IS_ERR(base))
  576. return PTR_ERR(base);
  577. drvdata->base = base;
  578. spin_lock_init(&drvdata->spinlock);
  579. devid = readl_relaxed(drvdata->base + CORESIGHT_DEVID);
  580. drvdata->config_type = BMVAL(devid, 6, 7);
  581. if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
  582. if (np)
  583. ret = of_property_read_u32(np,
  584. "arm,buffer-size",
  585. &drvdata->size);
  586. if (ret)
  587. drvdata->size = SZ_1M;
  588. } else {
  589. drvdata->size = readl_relaxed(drvdata->base + TMC_RSZ) * 4;
  590. }
  591. pm_runtime_put(&adev->dev);
  592. if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
  593. drvdata->vaddr = dma_alloc_coherent(dev, drvdata->size,
  594. &drvdata->paddr, GFP_KERNEL);
  595. if (!drvdata->vaddr)
  596. return -ENOMEM;
  597. memset(drvdata->vaddr, 0, drvdata->size);
  598. drvdata->buf = drvdata->vaddr;
  599. } else {
  600. drvdata->buf = devm_kzalloc(dev, drvdata->size, GFP_KERNEL);
  601. if (!drvdata->buf)
  602. return -ENOMEM;
  603. }
  604. desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
  605. if (!desc) {
  606. ret = -ENOMEM;
  607. goto err_devm_kzalloc;
  608. }
  609. desc->pdata = pdata;
  610. desc->dev = dev;
  611. desc->subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
  612. if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
  613. desc->type = CORESIGHT_DEV_TYPE_SINK;
  614. desc->ops = &tmc_etb_cs_ops;
  615. desc->groups = coresight_etb_groups;
  616. } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
  617. desc->type = CORESIGHT_DEV_TYPE_SINK;
  618. desc->ops = &tmc_etr_cs_ops;
  619. desc->groups = coresight_etr_groups;
  620. } else {
  621. desc->type = CORESIGHT_DEV_TYPE_LINKSINK;
  622. desc->subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_FIFO;
  623. desc->ops = &tmc_etf_cs_ops;
  624. desc->groups = coresight_etf_groups;
  625. }
  626. drvdata->csdev = coresight_register(desc);
  627. if (IS_ERR(drvdata->csdev)) {
  628. ret = PTR_ERR(drvdata->csdev);
  629. goto err_devm_kzalloc;
  630. }
  631. drvdata->miscdev.name = pdata->name;
  632. drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
  633. drvdata->miscdev.fops = &tmc_fops;
  634. ret = misc_register(&drvdata->miscdev);
  635. if (ret)
  636. goto err_misc_register;
  637. dev_info(dev, "TMC initialized\n");
  638. return 0;
  639. err_misc_register:
  640. coresight_unregister(drvdata->csdev);
  641. err_devm_kzalloc:
  642. if (drvdata->config_type == TMC_CONFIG_TYPE_ETR)
  643. dma_free_coherent(dev, drvdata->size,
  644. drvdata->vaddr, drvdata->paddr);
  645. return ret;
  646. }
  647. static struct amba_id tmc_ids[] = {
  648. {
  649. .id = 0x0003b961,
  650. .mask = 0x0003ffff,
  651. },
  652. { 0, 0},
  653. };
  654. static struct amba_driver tmc_driver = {
  655. .drv = {
  656. .name = "coresight-tmc",
  657. .owner = THIS_MODULE,
  658. .suppress_bind_attrs = true,
  659. },
  660. .probe = tmc_probe,
  661. .id_table = tmc_ids,
  662. };
  663. builtin_amba_driver(tmc_driver);