coresight-tmc.c 20 KB

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