coresight-tmc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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: %d 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 trigger_cntr_show(struct device *dev,
  478. struct device_attribute *attr, char *buf)
  479. {
  480. struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
  481. unsigned long val = drvdata->trigger_cntr;
  482. return sprintf(buf, "%#lx\n", val);
  483. }
  484. static ssize_t trigger_cntr_store(struct device *dev,
  485. struct device_attribute *attr,
  486. const char *buf, size_t size)
  487. {
  488. int ret;
  489. unsigned long val;
  490. struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
  491. ret = kstrtoul(buf, 16, &val);
  492. if (ret)
  493. return ret;
  494. drvdata->trigger_cntr = val;
  495. return size;
  496. }
  497. static DEVICE_ATTR_RW(trigger_cntr);
  498. static struct attribute *coresight_etb_attrs[] = {
  499. &dev_attr_trigger_cntr.attr,
  500. NULL,
  501. };
  502. ATTRIBUTE_GROUPS(coresight_etb);
  503. static struct attribute *coresight_etr_attrs[] = {
  504. &dev_attr_trigger_cntr.attr,
  505. NULL,
  506. };
  507. ATTRIBUTE_GROUPS(coresight_etr);
  508. static struct attribute *coresight_etf_attrs[] = {
  509. &dev_attr_trigger_cntr.attr,
  510. NULL,
  511. };
  512. ATTRIBUTE_GROUPS(coresight_etf);
  513. static int tmc_probe(struct amba_device *adev, const struct amba_id *id)
  514. {
  515. int ret = 0;
  516. u32 devid;
  517. void __iomem *base;
  518. struct device *dev = &adev->dev;
  519. struct coresight_platform_data *pdata = NULL;
  520. struct tmc_drvdata *drvdata;
  521. struct resource *res = &adev->res;
  522. struct coresight_desc *desc;
  523. struct device_node *np = adev->dev.of_node;
  524. if (np) {
  525. pdata = of_get_coresight_platform_data(dev, np);
  526. if (IS_ERR(pdata))
  527. return PTR_ERR(pdata);
  528. adev->dev.platform_data = pdata;
  529. }
  530. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  531. if (!drvdata)
  532. return -ENOMEM;
  533. drvdata->dev = &adev->dev;
  534. dev_set_drvdata(dev, drvdata);
  535. /* Validity for the resource is already checked by the AMBA core */
  536. base = devm_ioremap_resource(dev, res);
  537. if (IS_ERR(base))
  538. return PTR_ERR(base);
  539. drvdata->base = base;
  540. spin_lock_init(&drvdata->spinlock);
  541. drvdata->clk = adev->pclk;
  542. ret = clk_prepare_enable(drvdata->clk);
  543. if (ret)
  544. return ret;
  545. devid = readl_relaxed(drvdata->base + CORESIGHT_DEVID);
  546. drvdata->config_type = BMVAL(devid, 6, 7);
  547. if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
  548. if (np)
  549. ret = of_property_read_u32(np,
  550. "arm,buffer-size",
  551. &drvdata->size);
  552. if (ret)
  553. drvdata->size = SZ_1M;
  554. } else {
  555. drvdata->size = readl_relaxed(drvdata->base + TMC_RSZ) * 4;
  556. }
  557. clk_disable_unprepare(drvdata->clk);
  558. if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
  559. drvdata->vaddr = dma_alloc_coherent(dev, drvdata->size,
  560. &drvdata->paddr, GFP_KERNEL);
  561. if (!drvdata->vaddr)
  562. return -ENOMEM;
  563. memset(drvdata->vaddr, 0, drvdata->size);
  564. drvdata->buf = drvdata->vaddr;
  565. } else {
  566. drvdata->buf = devm_kzalloc(dev, drvdata->size, GFP_KERNEL);
  567. if (!drvdata->buf)
  568. return -ENOMEM;
  569. }
  570. desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
  571. if (!desc) {
  572. ret = -ENOMEM;
  573. goto err_devm_kzalloc;
  574. }
  575. desc->pdata = pdata;
  576. desc->dev = dev;
  577. desc->subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
  578. if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
  579. desc->type = CORESIGHT_DEV_TYPE_SINK;
  580. desc->ops = &tmc_etb_cs_ops;
  581. desc->groups = coresight_etb_groups;
  582. } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
  583. desc->type = CORESIGHT_DEV_TYPE_SINK;
  584. desc->ops = &tmc_etr_cs_ops;
  585. desc->groups = coresight_etr_groups;
  586. } else {
  587. desc->type = CORESIGHT_DEV_TYPE_LINKSINK;
  588. desc->subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_FIFO;
  589. desc->ops = &tmc_etf_cs_ops;
  590. desc->groups = coresight_etf_groups;
  591. }
  592. drvdata->csdev = coresight_register(desc);
  593. if (IS_ERR(drvdata->csdev)) {
  594. ret = PTR_ERR(drvdata->csdev);
  595. goto err_devm_kzalloc;
  596. }
  597. drvdata->miscdev.name = pdata->name;
  598. drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
  599. drvdata->miscdev.fops = &tmc_fops;
  600. ret = misc_register(&drvdata->miscdev);
  601. if (ret)
  602. goto err_misc_register;
  603. dev_info(dev, "TMC initialized\n");
  604. return 0;
  605. err_misc_register:
  606. coresight_unregister(drvdata->csdev);
  607. err_devm_kzalloc:
  608. if (drvdata->config_type == TMC_CONFIG_TYPE_ETR)
  609. dma_free_coherent(dev, drvdata->size,
  610. &drvdata->paddr, GFP_KERNEL);
  611. return ret;
  612. }
  613. static int tmc_remove(struct amba_device *adev)
  614. {
  615. struct tmc_drvdata *drvdata = amba_get_drvdata(adev);
  616. misc_deregister(&drvdata->miscdev);
  617. coresight_unregister(drvdata->csdev);
  618. if (drvdata->config_type == TMC_CONFIG_TYPE_ETR)
  619. dma_free_coherent(drvdata->dev, drvdata->size,
  620. &drvdata->paddr, GFP_KERNEL);
  621. return 0;
  622. }
  623. static struct amba_id tmc_ids[] = {
  624. {
  625. .id = 0x0003b961,
  626. .mask = 0x0003ffff,
  627. },
  628. { 0, 0},
  629. };
  630. static struct amba_driver tmc_driver = {
  631. .drv = {
  632. .name = "coresight-tmc",
  633. .owner = THIS_MODULE,
  634. },
  635. .probe = tmc_probe,
  636. .remove = tmc_remove,
  637. .id_table = tmc_ids,
  638. };
  639. static int __init tmc_init(void)
  640. {
  641. return amba_driver_register(&tmc_driver);
  642. }
  643. module_init(tmc_init);
  644. static void __exit tmc_exit(void)
  645. {
  646. amba_driver_unregister(&tmc_driver);
  647. }
  648. module_exit(tmc_exit);
  649. MODULE_LICENSE("GPL v2");
  650. MODULE_DESCRIPTION("CoreSight Trace Memory Controller driver");