coresight-cti.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /* Copyright (c) 2013, 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/device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/io.h>
  18. #include <linux/err.h>
  19. #include <linux/slab.h>
  20. #include <linux/mutex.h>
  21. #include <linux/clk.h>
  22. #include <linux/of_coresight.h>
  23. #include <linux/coresight.h>
  24. #include <linux/coresight-cti.h>
  25. #include "coresight-priv.h"
  26. #define cti_writel(drvdata, val, off) __raw_writel((val), drvdata->base + off)
  27. #define cti_readl(drvdata, off) __raw_readl(drvdata->base + off)
  28. #define CTI_LOCK(drvdata) \
  29. do { \
  30. mb(); \
  31. cti_writel(drvdata, 0x0, CORESIGHT_LAR); \
  32. } while (0)
  33. #define CTI_UNLOCK(drvdata) \
  34. do { \
  35. cti_writel(drvdata, CORESIGHT_UNLOCK, CORESIGHT_LAR); \
  36. mb(); \
  37. } while (0)
  38. #define CTICONTROL (0x000)
  39. #define CTIINTACK (0x010)
  40. #define CTIAPPSET (0x014)
  41. #define CTIAPPCLEAR (0x018)
  42. #define CTIAPPPULSE (0x01C)
  43. #define CTIINEN(n) (0x020 + (n * 4))
  44. #define CTIOUTEN(n) (0x0A0 + (n * 4))
  45. #define CTITRIGINSTATUS (0x130)
  46. #define CTITRIGOUTSTATUS (0x134)
  47. #define CTICHINSTATUS (0x138)
  48. #define CTICHOUTSTATUS (0x13C)
  49. #define CTIGATE (0x140)
  50. #define ASICCTL (0x144)
  51. #define ITCHINACK (0xEDC)
  52. #define ITTRIGINACK (0xEE0)
  53. #define ITCHOUT (0xEE4)
  54. #define ITTRIGOUT (0xEE8)
  55. #define ITCHOUTACK (0xEEC)
  56. #define ITTRIGOUTACK (0xEF0)
  57. #define ITCHIN (0xEF4)
  58. #define ITTRIGIN (0xEF8)
  59. #define CTI_MAX_TRIGGERS (8)
  60. #define CTI_MAX_CHANNELS (4)
  61. #define to_cti_drvdata(c) container_of(c, struct cti_drvdata, cti)
  62. struct cti_drvdata {
  63. void __iomem *base;
  64. struct device *dev;
  65. struct coresight_device *csdev;
  66. struct clk *clk;
  67. struct mutex mutex;
  68. struct coresight_cti cti;
  69. int refcnt;
  70. };
  71. static LIST_HEAD(cti_list);
  72. static DEFINE_MUTEX(cti_lock);
  73. static int cti_verify_bounds(int trig, int ch)
  74. {
  75. if (trig >= CTI_MAX_TRIGGERS)
  76. return -EINVAL;
  77. if (ch >= CTI_MAX_CHANNELS)
  78. return -EINVAL;
  79. return 0;
  80. }
  81. static int cti_enable(struct cti_drvdata *drvdata)
  82. {
  83. int ret;
  84. ret = clk_prepare_enable(drvdata->clk);
  85. if (ret)
  86. return ret;
  87. CTI_UNLOCK(drvdata);
  88. cti_writel(drvdata, 0x1, CTICONTROL);
  89. CTI_LOCK(drvdata);
  90. return 0;
  91. }
  92. static int __cti_map_trigin(struct cti_drvdata *drvdata, int trig, int ch)
  93. {
  94. uint32_t ctien;
  95. int ret;
  96. if (drvdata->refcnt == 0) {
  97. ret = cti_enable(drvdata);
  98. if (ret)
  99. return ret;
  100. }
  101. CTI_UNLOCK(drvdata);
  102. ctien = cti_readl(drvdata, CTIINEN(trig));
  103. if (ctien & (0x1 << ch))
  104. goto out;
  105. cti_writel(drvdata, (ctien | 0x1 << ch), CTIINEN(trig));
  106. CTI_LOCK(drvdata);
  107. drvdata->refcnt++;
  108. return 0;
  109. out:
  110. CTI_LOCK(drvdata);
  111. return 0;
  112. }
  113. int coresight_cti_map_trigin(struct coresight_cti *cti, int trig, int ch)
  114. {
  115. struct cti_drvdata *drvdata;
  116. int ret;
  117. if (IS_ERR_OR_NULL(cti))
  118. return -EINVAL;
  119. ret = cti_verify_bounds(trig, ch);
  120. if (ret)
  121. return ret;
  122. drvdata = to_cti_drvdata(cti);
  123. mutex_lock(&drvdata->mutex);
  124. ret = __cti_map_trigin(drvdata, trig, ch);
  125. mutex_unlock(&drvdata->mutex);
  126. return ret;
  127. }
  128. EXPORT_SYMBOL(coresight_cti_map_trigin);
  129. static int __cti_map_trigout(struct cti_drvdata *drvdata, int trig, int ch)
  130. {
  131. uint32_t ctien;
  132. int ret;
  133. if (drvdata->refcnt == 0) {
  134. ret = cti_enable(drvdata);
  135. if (ret)
  136. return ret;
  137. }
  138. CTI_UNLOCK(drvdata);
  139. ctien = cti_readl(drvdata, CTIOUTEN(trig));
  140. if (ctien & (0x1 << ch))
  141. goto out;
  142. cti_writel(drvdata, (ctien | 0x1 << ch), CTIOUTEN(trig));
  143. CTI_LOCK(drvdata);
  144. drvdata->refcnt++;
  145. return 0;
  146. out:
  147. CTI_LOCK(drvdata);
  148. return 0;
  149. }
  150. int coresight_cti_map_trigout(struct coresight_cti *cti, int trig, int ch)
  151. {
  152. struct cti_drvdata *drvdata;
  153. int ret;
  154. if (IS_ERR_OR_NULL(cti))
  155. return -EINVAL;
  156. ret = cti_verify_bounds(trig, ch);
  157. if (ret)
  158. return ret;
  159. drvdata = to_cti_drvdata(cti);
  160. mutex_lock(&drvdata->mutex);
  161. ret = __cti_map_trigout(drvdata, trig, ch);
  162. mutex_unlock(&drvdata->mutex);
  163. return ret;
  164. }
  165. EXPORT_SYMBOL(coresight_cti_map_trigout);
  166. static void cti_disable(struct cti_drvdata *drvdata)
  167. {
  168. CTI_UNLOCK(drvdata);
  169. cti_writel(drvdata, 0x0, CTICONTROL);
  170. CTI_LOCK(drvdata);
  171. clk_disable_unprepare(drvdata->clk);
  172. }
  173. static void __cti_unmap_trigin(struct cti_drvdata *drvdata, int trig, int ch)
  174. {
  175. uint32_t ctien;
  176. CTI_UNLOCK(drvdata);
  177. ctien = cti_readl(drvdata, CTIINEN(trig));
  178. if (!(ctien & (0x1 << ch)))
  179. goto out;
  180. cti_writel(drvdata, (ctien & ~(0x1 << ch)), CTIINEN(trig));
  181. CTI_LOCK(drvdata);
  182. if (drvdata->refcnt == 1)
  183. cti_disable(drvdata);
  184. drvdata->refcnt--;
  185. return;
  186. out:
  187. CTI_LOCK(drvdata);
  188. return;
  189. }
  190. void coresight_cti_unmap_trigin(struct coresight_cti *cti, int trig, int ch)
  191. {
  192. struct cti_drvdata *drvdata;
  193. if (IS_ERR_OR_NULL(cti))
  194. return;
  195. if (cti_verify_bounds(trig, ch))
  196. return;
  197. drvdata = to_cti_drvdata(cti);
  198. mutex_lock(&drvdata->mutex);
  199. __cti_unmap_trigin(drvdata, trig, ch);
  200. mutex_unlock(&drvdata->mutex);
  201. }
  202. EXPORT_SYMBOL(coresight_cti_unmap_trigin);
  203. static void __cti_unmap_trigout(struct cti_drvdata *drvdata, int trig, int ch)
  204. {
  205. uint32_t ctien;
  206. CTI_UNLOCK(drvdata);
  207. ctien = cti_readl(drvdata, CTIOUTEN(trig));
  208. if (!(ctien & (0x1 << ch)))
  209. goto out;
  210. cti_writel(drvdata, (ctien & ~(0x1 << ch)), CTIOUTEN(trig));
  211. CTI_LOCK(drvdata);
  212. if (drvdata->refcnt == 1)
  213. cti_disable(drvdata);
  214. drvdata->refcnt--;
  215. return;
  216. out:
  217. CTI_LOCK(drvdata);
  218. return;
  219. }
  220. void coresight_cti_unmap_trigout(struct coresight_cti *cti, int trig, int ch)
  221. {
  222. struct cti_drvdata *drvdata;
  223. if (IS_ERR_OR_NULL(cti))
  224. return;
  225. if (cti_verify_bounds(trig, ch))
  226. return;
  227. drvdata = to_cti_drvdata(cti);
  228. mutex_lock(&drvdata->mutex);
  229. __cti_unmap_trigout(drvdata, trig, ch);
  230. mutex_unlock(&drvdata->mutex);
  231. }
  232. EXPORT_SYMBOL(coresight_cti_unmap_trigout);
  233. static void __cti_reset(struct cti_drvdata *drvdata)
  234. {
  235. int trig;
  236. if (!drvdata->refcnt)
  237. return;
  238. CTI_UNLOCK(drvdata);
  239. for (trig = 0; trig < CTI_MAX_TRIGGERS; trig++) {
  240. cti_writel(drvdata, 0, CTIINEN(trig));
  241. cti_writel(drvdata, 0, CTIOUTEN(trig));
  242. }
  243. CTI_LOCK(drvdata);
  244. cti_disable(drvdata);
  245. drvdata->refcnt = 0;
  246. }
  247. void coresight_cti_reset(struct coresight_cti *cti)
  248. {
  249. struct cti_drvdata *drvdata;
  250. if (IS_ERR_OR_NULL(cti))
  251. return;
  252. drvdata = to_cti_drvdata(cti);
  253. mutex_lock(&drvdata->mutex);
  254. __cti_reset(drvdata);
  255. mutex_unlock(&drvdata->mutex);
  256. }
  257. EXPORT_SYMBOL(coresight_cti_reset);
  258. struct coresight_cti *coresight_cti_get(const char *name)
  259. {
  260. struct coresight_cti *cti;
  261. mutex_lock(&cti_lock);
  262. list_for_each_entry(cti, &cti_list, link) {
  263. if (!strncmp(cti->name, name, strlen(cti->name) + 1)) {
  264. mutex_unlock(&cti_lock);
  265. return cti;
  266. }
  267. }
  268. mutex_unlock(&cti_lock);
  269. return ERR_PTR(-EINVAL);
  270. }
  271. EXPORT_SYMBOL(coresight_cti_get);
  272. void coresight_cti_put(struct coresight_cti *cti)
  273. {
  274. }
  275. EXPORT_SYMBOL(coresight_cti_put);
  276. static ssize_t cti_store_map_trigin(struct device *dev,
  277. struct device_attribute *attr,
  278. const char *buf, size_t size)
  279. {
  280. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  281. unsigned long val1, val2;
  282. int ret;
  283. if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
  284. return -EINVAL;
  285. ret = coresight_cti_map_trigin(&drvdata->cti, val1, val2);
  286. if (ret)
  287. return ret;
  288. return size;
  289. }
  290. static DEVICE_ATTR(map_trigin, S_IWUSR, NULL, cti_store_map_trigin);
  291. static ssize_t cti_store_map_trigout(struct device *dev,
  292. struct device_attribute *attr,
  293. const char *buf, size_t size)
  294. {
  295. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  296. unsigned long val1, val2;
  297. int ret;
  298. if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
  299. return -EINVAL;
  300. ret = coresight_cti_map_trigout(&drvdata->cti, val1, val2);
  301. if (ret)
  302. return ret;
  303. return size;
  304. }
  305. static DEVICE_ATTR(map_trigout, S_IWUSR, NULL, cti_store_map_trigout);
  306. static ssize_t cti_store_unmap_trigin(struct device *dev,
  307. struct device_attribute *attr,
  308. const char *buf, size_t size)
  309. {
  310. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  311. unsigned long val1, val2;
  312. if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
  313. return -EINVAL;
  314. coresight_cti_unmap_trigin(&drvdata->cti, val1, val2);
  315. return size;
  316. }
  317. static DEVICE_ATTR(unmap_trigin, S_IWUSR, NULL, cti_store_unmap_trigin);
  318. static ssize_t cti_store_unmap_trigout(struct device *dev,
  319. struct device_attribute *attr,
  320. const char *buf, size_t size)
  321. {
  322. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  323. unsigned long val1, val2;
  324. if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
  325. return -EINVAL;
  326. coresight_cti_unmap_trigout(&drvdata->cti, val1, val2);
  327. return size;
  328. }
  329. static DEVICE_ATTR(unmap_trigout, S_IWUSR, NULL, cti_store_unmap_trigout);
  330. static ssize_t cti_store_reset(struct device *dev,
  331. struct device_attribute *attr,
  332. const char *buf, size_t size)
  333. {
  334. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  335. unsigned long val;
  336. if (sscanf(buf, "%lx", &val) != 1)
  337. return -EINVAL;
  338. if (!val)
  339. return -EINVAL;
  340. coresight_cti_reset(&drvdata->cti);
  341. return size;
  342. }
  343. static DEVICE_ATTR(reset, S_IWUSR, NULL, cti_store_reset);
  344. static ssize_t cti_show_trigin(struct device *dev,
  345. struct device_attribute *attr, char *buf)
  346. {
  347. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  348. unsigned long trig, ch;
  349. uint32_t ctien;
  350. ssize_t size = 0;
  351. mutex_lock(&cti_lock);
  352. if (!drvdata->refcnt)
  353. goto err;
  354. for (trig = 0; trig < CTI_MAX_TRIGGERS; trig++) {
  355. ctien = cti_readl(drvdata, CTIINEN(trig));
  356. for (ch = 0; ch < CTI_MAX_CHANNELS; ch++) {
  357. if (ctien & (1 << ch)) {
  358. /* Ensure we do not write more than PAGE_SIZE
  359. * bytes of data including \n character and null
  360. * terminator
  361. */
  362. size += scnprintf(&buf[size], PAGE_SIZE - size -
  363. 1, " %#lx %#lx,", trig, ch);
  364. if (size >= PAGE_SIZE - 2) {
  365. dev_err(dev, "show buffer full\n");
  366. goto err;
  367. }
  368. }
  369. }
  370. }
  371. err:
  372. size += scnprintf(&buf[size], 2, "\n");
  373. mutex_unlock(&cti_lock);
  374. return size;
  375. }
  376. static DEVICE_ATTR(show_trigin, S_IRUGO, cti_show_trigin, NULL);
  377. static ssize_t cti_show_trigout(struct device *dev,
  378. struct device_attribute *attr, char *buf)
  379. {
  380. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  381. unsigned long trig, ch;
  382. uint32_t ctien;
  383. ssize_t size = 0;
  384. mutex_lock(&cti_lock);
  385. if (!drvdata->refcnt)
  386. goto err;
  387. for (trig = 0; trig < CTI_MAX_TRIGGERS; trig++) {
  388. ctien = cti_readl(drvdata, CTIOUTEN(trig));
  389. for (ch = 0; ch < CTI_MAX_CHANNELS; ch++) {
  390. if (ctien & (1 << ch)) {
  391. /* Ensure we do not write more than PAGE_SIZE
  392. * bytes of data including \n character and null
  393. * terminator
  394. */
  395. size += scnprintf(&buf[size], PAGE_SIZE - size -
  396. 1, " %#lx %#lx,", trig, ch);
  397. if (size >= PAGE_SIZE - 2) {
  398. dev_err(dev, "show buffer full\n");
  399. goto err;
  400. }
  401. }
  402. }
  403. }
  404. err:
  405. size += scnprintf(&buf[size], 2, "\n");
  406. mutex_unlock(&cti_lock);
  407. return size;
  408. }
  409. static DEVICE_ATTR(show_trigout, S_IRUGO, cti_show_trigout, NULL);
  410. static struct attribute *cti_attrs[] = {
  411. &dev_attr_map_trigin.attr,
  412. &dev_attr_map_trigout.attr,
  413. &dev_attr_unmap_trigin.attr,
  414. &dev_attr_unmap_trigout.attr,
  415. &dev_attr_reset.attr,
  416. &dev_attr_show_trigin.attr,
  417. &dev_attr_show_trigout.attr,
  418. NULL,
  419. };
  420. static struct attribute_group cti_attr_grp = {
  421. .attrs = cti_attrs,
  422. };
  423. static const struct attribute_group *cti_attr_grps[] = {
  424. &cti_attr_grp,
  425. NULL,
  426. };
  427. static int __devinit cti_probe(struct platform_device *pdev)
  428. {
  429. int ret;
  430. struct device *dev = &pdev->dev;
  431. struct coresight_platform_data *pdata;
  432. struct cti_drvdata *drvdata;
  433. struct resource *res;
  434. struct coresight_desc *desc;
  435. if (coresight_fuse_access_disabled())
  436. return -EPERM;
  437. if (pdev->dev.of_node) {
  438. pdata = of_get_coresight_platform_data(dev, pdev->dev.of_node);
  439. if (IS_ERR(pdata))
  440. return PTR_ERR(pdata);
  441. pdev->dev.platform_data = pdata;
  442. }
  443. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  444. if (!drvdata)
  445. return -ENOMEM;
  446. /* Store the driver data pointer for use in exported functions */
  447. drvdata->dev = &pdev->dev;
  448. platform_set_drvdata(pdev, drvdata);
  449. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cti-base");
  450. if (!res)
  451. return -ENODEV;
  452. drvdata->base = devm_ioremap(dev, res->start, resource_size(res));
  453. if (!drvdata->base)
  454. return -ENOMEM;
  455. mutex_init(&drvdata->mutex);
  456. drvdata->clk = devm_clk_get(dev, "core_clk");
  457. if (IS_ERR(drvdata->clk))
  458. return PTR_ERR(drvdata->clk);
  459. ret = clk_set_rate(drvdata->clk, CORESIGHT_CLK_RATE_TRACE);
  460. if (ret)
  461. return ret;
  462. mutex_lock(&cti_lock);
  463. drvdata->cti.name = ((struct coresight_platform_data *)
  464. (pdev->dev.platform_data))->name;
  465. list_add_tail(&drvdata->cti.link, &cti_list);
  466. mutex_unlock(&cti_lock);
  467. desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
  468. if (!desc)
  469. return -ENOMEM;
  470. desc->type = CORESIGHT_DEV_TYPE_NONE;
  471. desc->pdata = pdev->dev.platform_data;
  472. desc->dev = &pdev->dev;
  473. desc->groups = cti_attr_grps;
  474. desc->owner = THIS_MODULE;
  475. drvdata->csdev = coresight_register(desc);
  476. if (IS_ERR(drvdata->csdev))
  477. return PTR_ERR(drvdata->csdev);
  478. dev_info(dev, "CTI initialized\n");
  479. return 0;
  480. }
  481. static int __devexit cti_remove(struct platform_device *pdev)
  482. {
  483. struct cti_drvdata *drvdata = platform_get_drvdata(pdev);
  484. mutex_destroy(&drvdata->mutex);
  485. coresight_unregister(drvdata->csdev);
  486. return 0;
  487. }
  488. static struct of_device_id cti_match[] = {
  489. {.compatible = "arm,coresight-cti"},
  490. {}
  491. };
  492. static struct platform_driver cti_driver = {
  493. .probe = cti_probe,
  494. .remove = __devexit_p(cti_remove),
  495. .driver = {
  496. .name = "coresight-cti",
  497. .owner = THIS_MODULE,
  498. .of_match_table = cti_match,
  499. },
  500. };
  501. static int __init cti_init(void)
  502. {
  503. return platform_driver_register(&cti_driver);
  504. }
  505. module_init(cti_init);
  506. static void __exit cti_exit(void)
  507. {
  508. platform_driver_unregister(&cti_driver);
  509. }
  510. module_exit(cti_exit);
  511. MODULE_LICENSE("GPL v2");
  512. MODULE_DESCRIPTION("CoreSight CTI driver");