coresight-tmc.c 20 KB

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