coresight-stm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
  2. *
  3. * Description: CoreSight System Trace Macrocell driver
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * Initial implementation by Pratik Patel
  15. * (C) 2014-2015 Pratik Patel <pratikp@codeaurora.org>
  16. *
  17. * Serious refactoring, code cleanup and upgrading to the Coresight upstream
  18. * framework by Mathieu Poirier
  19. * (C) 2015-2016 Mathieu Poirier <mathieu.poirier@linaro.org>
  20. *
  21. * Guaranteed timing and support for various packet type coming from the
  22. * generic STM API by Chunyan Zhang
  23. * (C) 2015-2016 Chunyan Zhang <zhang.chunyan@linaro.org>
  24. */
  25. #include <asm/local.h>
  26. #include <linux/amba/bus.h>
  27. #include <linux/bitmap.h>
  28. #include <linux/clk.h>
  29. #include <linux/coresight.h>
  30. #include <linux/coresight-stm.h>
  31. #include <linux/err.h>
  32. #include <linux/kernel.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/of_address.h>
  35. #include <linux/perf_event.h>
  36. #include <linux/pm_runtime.h>
  37. #include <linux/stm.h>
  38. #include "coresight-priv.h"
  39. #define STMDMASTARTR 0xc04
  40. #define STMDMASTOPR 0xc08
  41. #define STMDMASTATR 0xc0c
  42. #define STMDMACTLR 0xc10
  43. #define STMDMAIDR 0xcfc
  44. #define STMHEER 0xd00
  45. #define STMHETER 0xd20
  46. #define STMHEBSR 0xd60
  47. #define STMHEMCR 0xd64
  48. #define STMHEMASTR 0xdf4
  49. #define STMHEFEAT1R 0xdf8
  50. #define STMHEIDR 0xdfc
  51. #define STMSPER 0xe00
  52. #define STMSPTER 0xe20
  53. #define STMPRIVMASKR 0xe40
  54. #define STMSPSCR 0xe60
  55. #define STMSPMSCR 0xe64
  56. #define STMSPOVERRIDER 0xe68
  57. #define STMSPMOVERRIDER 0xe6c
  58. #define STMSPTRIGCSR 0xe70
  59. #define STMTCSR 0xe80
  60. #define STMTSSTIMR 0xe84
  61. #define STMTSFREQR 0xe8c
  62. #define STMSYNCR 0xe90
  63. #define STMAUXCR 0xe94
  64. #define STMSPFEAT1R 0xea0
  65. #define STMSPFEAT2R 0xea4
  66. #define STMSPFEAT3R 0xea8
  67. #define STMITTRIGGER 0xee8
  68. #define STMITATBDATA0 0xeec
  69. #define STMITATBCTR2 0xef0
  70. #define STMITATBID 0xef4
  71. #define STMITATBCTR0 0xef8
  72. #define STM_32_CHANNEL 32
  73. #define BYTES_PER_CHANNEL 256
  74. #define STM_TRACE_BUF_SIZE 4096
  75. #define STM_SW_MASTER_END 127
  76. /* Register bit definition */
  77. #define STMTCSR_BUSY_BIT 23
  78. /* Reserve the first 10 channels for kernel usage */
  79. #define STM_CHANNEL_OFFSET 0
  80. enum stm_pkt_type {
  81. STM_PKT_TYPE_DATA = 0x98,
  82. STM_PKT_TYPE_FLAG = 0xE8,
  83. STM_PKT_TYPE_TRIG = 0xF8,
  84. };
  85. #define stm_channel_addr(drvdata, ch) (drvdata->chs.base + \
  86. (ch * BYTES_PER_CHANNEL))
  87. #define stm_channel_off(type, opts) (type & ~opts)
  88. static int boot_nr_channel;
  89. /*
  90. * Not really modular but using module_param is the easiest way to
  91. * remain consistent with existing use cases for now.
  92. */
  93. module_param_named(
  94. boot_nr_channel, boot_nr_channel, int, S_IRUGO
  95. );
  96. /**
  97. * struct channel_space - central management entity for extended ports
  98. * @base: memory mapped base address where channels start.
  99. * @guaraneed: is the channel delivery guaranteed.
  100. */
  101. struct channel_space {
  102. void __iomem *base;
  103. unsigned long *guaranteed;
  104. };
  105. /**
  106. * struct stm_drvdata - specifics associated to an STM component
  107. * @base: memory mapped base address for this component.
  108. * @dev: the device entity associated to this component.
  109. * @atclk: optional clock for the core parts of the STM.
  110. * @csdev: component vitals needed by the framework.
  111. * @spinlock: only one at a time pls.
  112. * @chs: the channels accociated to this STM.
  113. * @stm: structure associated to the generic STM interface.
  114. * @mode: this tracer's mode, i.e sysFS, or disabled.
  115. * @traceid: value of the current ID for this component.
  116. * @write_bytes: Maximus bytes this STM can write at a time.
  117. * @stmsper: settings for register STMSPER.
  118. * @stmspscr: settings for register STMSPSCR.
  119. * @numsp: the total number of stimulus port support by this STM.
  120. * @stmheer: settings for register STMHEER.
  121. * @stmheter: settings for register STMHETER.
  122. * @stmhebsr: settings for register STMHEBSR.
  123. */
  124. struct stm_drvdata {
  125. void __iomem *base;
  126. struct device *dev;
  127. struct clk *atclk;
  128. struct coresight_device *csdev;
  129. spinlock_t spinlock;
  130. struct channel_space chs;
  131. struct stm_data stm;
  132. local_t mode;
  133. u8 traceid;
  134. u32 write_bytes;
  135. u32 stmsper;
  136. u32 stmspscr;
  137. u32 numsp;
  138. u32 stmheer;
  139. u32 stmheter;
  140. u32 stmhebsr;
  141. };
  142. static void stm_hwevent_enable_hw(struct stm_drvdata *drvdata)
  143. {
  144. CS_UNLOCK(drvdata->base);
  145. writel_relaxed(drvdata->stmhebsr, drvdata->base + STMHEBSR);
  146. writel_relaxed(drvdata->stmheter, drvdata->base + STMHETER);
  147. writel_relaxed(drvdata->stmheer, drvdata->base + STMHEER);
  148. writel_relaxed(0x01 | /* Enable HW event tracing */
  149. 0x04, /* Error detection on event tracing */
  150. drvdata->base + STMHEMCR);
  151. CS_LOCK(drvdata->base);
  152. }
  153. static void stm_port_enable_hw(struct stm_drvdata *drvdata)
  154. {
  155. CS_UNLOCK(drvdata->base);
  156. /* ATB trigger enable on direct writes to TRIG locations */
  157. writel_relaxed(0x10,
  158. drvdata->base + STMSPTRIGCSR);
  159. writel_relaxed(drvdata->stmspscr, drvdata->base + STMSPSCR);
  160. writel_relaxed(drvdata->stmsper, drvdata->base + STMSPER);
  161. CS_LOCK(drvdata->base);
  162. }
  163. static void stm_enable_hw(struct stm_drvdata *drvdata)
  164. {
  165. if (drvdata->stmheer)
  166. stm_hwevent_enable_hw(drvdata);
  167. stm_port_enable_hw(drvdata);
  168. CS_UNLOCK(drvdata->base);
  169. /* 4096 byte between synchronisation packets */
  170. writel_relaxed(0xFFF, drvdata->base + STMSYNCR);
  171. writel_relaxed((drvdata->traceid << 16 | /* trace id */
  172. 0x02 | /* timestamp enable */
  173. 0x01), /* global STM enable */
  174. drvdata->base + STMTCSR);
  175. CS_LOCK(drvdata->base);
  176. }
  177. static int stm_enable(struct coresight_device *csdev,
  178. struct perf_event_attr *attr, u32 mode)
  179. {
  180. u32 val;
  181. struct stm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  182. if (mode != CS_MODE_SYSFS)
  183. return -EINVAL;
  184. val = local_cmpxchg(&drvdata->mode, CS_MODE_DISABLED, mode);
  185. /* Someone is already using the tracer */
  186. if (val)
  187. return -EBUSY;
  188. pm_runtime_get_sync(drvdata->dev);
  189. spin_lock(&drvdata->spinlock);
  190. stm_enable_hw(drvdata);
  191. spin_unlock(&drvdata->spinlock);
  192. dev_info(drvdata->dev, "STM tracing enabled\n");
  193. return 0;
  194. }
  195. static void stm_hwevent_disable_hw(struct stm_drvdata *drvdata)
  196. {
  197. CS_UNLOCK(drvdata->base);
  198. writel_relaxed(0x0, drvdata->base + STMHEMCR);
  199. writel_relaxed(0x0, drvdata->base + STMHEER);
  200. writel_relaxed(0x0, drvdata->base + STMHETER);
  201. CS_LOCK(drvdata->base);
  202. }
  203. static void stm_port_disable_hw(struct stm_drvdata *drvdata)
  204. {
  205. CS_UNLOCK(drvdata->base);
  206. writel_relaxed(0x0, drvdata->base + STMSPER);
  207. writel_relaxed(0x0, drvdata->base + STMSPTRIGCSR);
  208. CS_LOCK(drvdata->base);
  209. }
  210. static void stm_disable_hw(struct stm_drvdata *drvdata)
  211. {
  212. u32 val;
  213. CS_UNLOCK(drvdata->base);
  214. val = readl_relaxed(drvdata->base + STMTCSR);
  215. val &= ~0x1; /* clear global STM enable [0] */
  216. writel_relaxed(val, drvdata->base + STMTCSR);
  217. CS_LOCK(drvdata->base);
  218. stm_port_disable_hw(drvdata);
  219. if (drvdata->stmheer)
  220. stm_hwevent_disable_hw(drvdata);
  221. }
  222. static void stm_disable(struct coresight_device *csdev)
  223. {
  224. struct stm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  225. /*
  226. * For as long as the tracer isn't disabled another entity can't
  227. * change its status. As such we can read the status here without
  228. * fearing it will change under us.
  229. */
  230. if (local_read(&drvdata->mode) == CS_MODE_SYSFS) {
  231. spin_lock(&drvdata->spinlock);
  232. stm_disable_hw(drvdata);
  233. spin_unlock(&drvdata->spinlock);
  234. /* Wait until the engine has completely stopped */
  235. coresight_timeout(drvdata, STMTCSR, STMTCSR_BUSY_BIT, 0);
  236. pm_runtime_put(drvdata->dev);
  237. local_set(&drvdata->mode, CS_MODE_DISABLED);
  238. dev_info(drvdata->dev, "STM tracing disabled\n");
  239. }
  240. }
  241. static int stm_trace_id(struct coresight_device *csdev)
  242. {
  243. struct stm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  244. return drvdata->traceid;
  245. }
  246. static const struct coresight_ops_source stm_source_ops = {
  247. .trace_id = stm_trace_id,
  248. .enable = stm_enable,
  249. .disable = stm_disable,
  250. };
  251. static const struct coresight_ops stm_cs_ops = {
  252. .source_ops = &stm_source_ops,
  253. };
  254. static inline bool stm_addr_unaligned(const void *addr, u8 write_bytes)
  255. {
  256. return ((unsigned long)addr & (write_bytes - 1));
  257. }
  258. static void stm_send(void *addr, const void *data, u32 size, u8 write_bytes)
  259. {
  260. u8 paload[8];
  261. if (stm_addr_unaligned(data, write_bytes)) {
  262. memcpy(paload, data, size);
  263. data = paload;
  264. }
  265. /* now we are 64bit/32bit aligned */
  266. switch (size) {
  267. #ifdef CONFIG_64BIT
  268. case 8:
  269. writeq_relaxed(*(u64 *)data, addr);
  270. break;
  271. #endif
  272. case 4:
  273. writel_relaxed(*(u32 *)data, addr);
  274. break;
  275. case 2:
  276. writew_relaxed(*(u16 *)data, addr);
  277. break;
  278. case 1:
  279. writeb_relaxed(*(u8 *)data, addr);
  280. break;
  281. default:
  282. break;
  283. }
  284. }
  285. static int stm_generic_link(struct stm_data *stm_data,
  286. unsigned int master, unsigned int channel)
  287. {
  288. struct stm_drvdata *drvdata = container_of(stm_data,
  289. struct stm_drvdata, stm);
  290. if (!drvdata || !drvdata->csdev)
  291. return -EINVAL;
  292. return coresight_enable(drvdata->csdev);
  293. }
  294. static void stm_generic_unlink(struct stm_data *stm_data,
  295. unsigned int master, unsigned int channel)
  296. {
  297. struct stm_drvdata *drvdata = container_of(stm_data,
  298. struct stm_drvdata, stm);
  299. if (!drvdata || !drvdata->csdev)
  300. return;
  301. stm_disable(drvdata->csdev);
  302. }
  303. static long stm_generic_set_options(struct stm_data *stm_data,
  304. unsigned int master,
  305. unsigned int channel,
  306. unsigned int nr_chans,
  307. unsigned long options)
  308. {
  309. struct stm_drvdata *drvdata = container_of(stm_data,
  310. struct stm_drvdata, stm);
  311. if (!(drvdata && local_read(&drvdata->mode)))
  312. return -EINVAL;
  313. if (channel >= drvdata->numsp)
  314. return -EINVAL;
  315. switch (options) {
  316. case STM_OPTION_GUARANTEED:
  317. set_bit(channel, drvdata->chs.guaranteed);
  318. break;
  319. case STM_OPTION_INVARIANT:
  320. clear_bit(channel, drvdata->chs.guaranteed);
  321. break;
  322. default:
  323. return -EINVAL;
  324. }
  325. return 0;
  326. }
  327. static ssize_t stm_generic_packet(struct stm_data *stm_data,
  328. unsigned int master,
  329. unsigned int channel,
  330. unsigned int packet,
  331. unsigned int flags,
  332. unsigned int size,
  333. const unsigned char *payload)
  334. {
  335. unsigned long ch_addr;
  336. struct stm_drvdata *drvdata = container_of(stm_data,
  337. struct stm_drvdata, stm);
  338. if (!(drvdata && local_read(&drvdata->mode)))
  339. return 0;
  340. if (channel >= drvdata->numsp)
  341. return 0;
  342. ch_addr = (unsigned long)stm_channel_addr(drvdata, channel);
  343. flags = (flags == STP_PACKET_TIMESTAMPED) ? STM_FLAG_TIMESTAMPED : 0;
  344. flags |= test_bit(channel, drvdata->chs.guaranteed) ?
  345. STM_FLAG_GUARANTEED : 0;
  346. if (size > drvdata->write_bytes)
  347. size = drvdata->write_bytes;
  348. else
  349. size = rounddown_pow_of_two(size);
  350. switch (packet) {
  351. case STP_PACKET_FLAG:
  352. ch_addr |= stm_channel_off(STM_PKT_TYPE_FLAG, flags);
  353. /*
  354. * The generic STM core sets a size of '0' on flag packets.
  355. * As such send a flag packet of size '1' and tell the
  356. * core we did so.
  357. */
  358. stm_send((void *)ch_addr, payload, 1, drvdata->write_bytes);
  359. size = 1;
  360. break;
  361. case STP_PACKET_DATA:
  362. ch_addr |= stm_channel_off(STM_PKT_TYPE_DATA, flags);
  363. stm_send((void *)ch_addr, payload, size,
  364. drvdata->write_bytes);
  365. break;
  366. default:
  367. return -ENOTSUPP;
  368. }
  369. return size;
  370. }
  371. static ssize_t hwevent_enable_show(struct device *dev,
  372. struct device_attribute *attr, char *buf)
  373. {
  374. struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  375. unsigned long val = drvdata->stmheer;
  376. return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
  377. }
  378. static ssize_t hwevent_enable_store(struct device *dev,
  379. struct device_attribute *attr,
  380. const char *buf, size_t size)
  381. {
  382. struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  383. unsigned long val;
  384. int ret = 0;
  385. ret = kstrtoul(buf, 16, &val);
  386. if (ret)
  387. return -EINVAL;
  388. drvdata->stmheer = val;
  389. /* HW event enable and trigger go hand in hand */
  390. drvdata->stmheter = val;
  391. return size;
  392. }
  393. static DEVICE_ATTR_RW(hwevent_enable);
  394. static ssize_t hwevent_select_show(struct device *dev,
  395. struct device_attribute *attr, char *buf)
  396. {
  397. struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  398. unsigned long val = drvdata->stmhebsr;
  399. return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
  400. }
  401. static ssize_t hwevent_select_store(struct device *dev,
  402. struct device_attribute *attr,
  403. const char *buf, size_t size)
  404. {
  405. struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  406. unsigned long val;
  407. int ret = 0;
  408. ret = kstrtoul(buf, 16, &val);
  409. if (ret)
  410. return -EINVAL;
  411. drvdata->stmhebsr = val;
  412. return size;
  413. }
  414. static DEVICE_ATTR_RW(hwevent_select);
  415. static ssize_t port_select_show(struct device *dev,
  416. struct device_attribute *attr, char *buf)
  417. {
  418. struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  419. unsigned long val;
  420. if (!local_read(&drvdata->mode)) {
  421. val = drvdata->stmspscr;
  422. } else {
  423. spin_lock(&drvdata->spinlock);
  424. val = readl_relaxed(drvdata->base + STMSPSCR);
  425. spin_unlock(&drvdata->spinlock);
  426. }
  427. return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
  428. }
  429. static ssize_t port_select_store(struct device *dev,
  430. struct device_attribute *attr,
  431. const char *buf, size_t size)
  432. {
  433. struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  434. unsigned long val, stmsper;
  435. int ret = 0;
  436. ret = kstrtoul(buf, 16, &val);
  437. if (ret)
  438. return ret;
  439. spin_lock(&drvdata->spinlock);
  440. drvdata->stmspscr = val;
  441. if (local_read(&drvdata->mode)) {
  442. CS_UNLOCK(drvdata->base);
  443. /* Process as per ARM's TRM recommendation */
  444. stmsper = readl_relaxed(drvdata->base + STMSPER);
  445. writel_relaxed(0x0, drvdata->base + STMSPER);
  446. writel_relaxed(drvdata->stmspscr, drvdata->base + STMSPSCR);
  447. writel_relaxed(stmsper, drvdata->base + STMSPER);
  448. CS_LOCK(drvdata->base);
  449. }
  450. spin_unlock(&drvdata->spinlock);
  451. return size;
  452. }
  453. static DEVICE_ATTR_RW(port_select);
  454. static ssize_t port_enable_show(struct device *dev,
  455. struct device_attribute *attr, char *buf)
  456. {
  457. struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  458. unsigned long val;
  459. if (!local_read(&drvdata->mode)) {
  460. val = drvdata->stmsper;
  461. } else {
  462. spin_lock(&drvdata->spinlock);
  463. val = readl_relaxed(drvdata->base + STMSPER);
  464. spin_unlock(&drvdata->spinlock);
  465. }
  466. return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
  467. }
  468. static ssize_t port_enable_store(struct device *dev,
  469. struct device_attribute *attr,
  470. const char *buf, size_t size)
  471. {
  472. struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  473. unsigned long val;
  474. int ret = 0;
  475. ret = kstrtoul(buf, 16, &val);
  476. if (ret)
  477. return ret;
  478. spin_lock(&drvdata->spinlock);
  479. drvdata->stmsper = val;
  480. if (local_read(&drvdata->mode)) {
  481. CS_UNLOCK(drvdata->base);
  482. writel_relaxed(drvdata->stmsper, drvdata->base + STMSPER);
  483. CS_LOCK(drvdata->base);
  484. }
  485. spin_unlock(&drvdata->spinlock);
  486. return size;
  487. }
  488. static DEVICE_ATTR_RW(port_enable);
  489. static ssize_t traceid_show(struct device *dev,
  490. struct device_attribute *attr, char *buf)
  491. {
  492. unsigned long val;
  493. struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  494. val = drvdata->traceid;
  495. return sprintf(buf, "%#lx\n", val);
  496. }
  497. static ssize_t traceid_store(struct device *dev,
  498. struct device_attribute *attr,
  499. const char *buf, size_t size)
  500. {
  501. int ret;
  502. unsigned long val;
  503. struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  504. ret = kstrtoul(buf, 16, &val);
  505. if (ret)
  506. return ret;
  507. /* traceid field is 7bit wide on STM32 */
  508. drvdata->traceid = val & 0x7f;
  509. return size;
  510. }
  511. static DEVICE_ATTR_RW(traceid);
  512. #define coresight_stm_simple_func(name, offset) \
  513. coresight_simple_func(struct stm_drvdata, name, offset)
  514. coresight_stm_simple_func(tcsr, STMTCSR);
  515. coresight_stm_simple_func(tsfreqr, STMTSFREQR);
  516. coresight_stm_simple_func(syncr, STMSYNCR);
  517. coresight_stm_simple_func(sper, STMSPER);
  518. coresight_stm_simple_func(spter, STMSPTER);
  519. coresight_stm_simple_func(privmaskr, STMPRIVMASKR);
  520. coresight_stm_simple_func(spscr, STMSPSCR);
  521. coresight_stm_simple_func(spmscr, STMSPMSCR);
  522. coresight_stm_simple_func(spfeat1r, STMSPFEAT1R);
  523. coresight_stm_simple_func(spfeat2r, STMSPFEAT2R);
  524. coresight_stm_simple_func(spfeat3r, STMSPFEAT3R);
  525. coresight_stm_simple_func(devid, CORESIGHT_DEVID);
  526. static struct attribute *coresight_stm_attrs[] = {
  527. &dev_attr_hwevent_enable.attr,
  528. &dev_attr_hwevent_select.attr,
  529. &dev_attr_port_enable.attr,
  530. &dev_attr_port_select.attr,
  531. &dev_attr_traceid.attr,
  532. NULL,
  533. };
  534. static struct attribute *coresight_stm_mgmt_attrs[] = {
  535. &dev_attr_tcsr.attr,
  536. &dev_attr_tsfreqr.attr,
  537. &dev_attr_syncr.attr,
  538. &dev_attr_sper.attr,
  539. &dev_attr_spter.attr,
  540. &dev_attr_privmaskr.attr,
  541. &dev_attr_spscr.attr,
  542. &dev_attr_spmscr.attr,
  543. &dev_attr_spfeat1r.attr,
  544. &dev_attr_spfeat2r.attr,
  545. &dev_attr_spfeat3r.attr,
  546. &dev_attr_devid.attr,
  547. NULL,
  548. };
  549. static const struct attribute_group coresight_stm_group = {
  550. .attrs = coresight_stm_attrs,
  551. };
  552. static const struct attribute_group coresight_stm_mgmt_group = {
  553. .attrs = coresight_stm_mgmt_attrs,
  554. .name = "mgmt",
  555. };
  556. static const struct attribute_group *coresight_stm_groups[] = {
  557. &coresight_stm_group,
  558. &coresight_stm_mgmt_group,
  559. NULL,
  560. };
  561. static int stm_get_resource_byname(struct device_node *np,
  562. char *ch_base, struct resource *res)
  563. {
  564. const char *name = NULL;
  565. int index = 0, found = 0;
  566. while (!of_property_read_string_index(np, "reg-names", index, &name)) {
  567. if (strcmp(ch_base, name)) {
  568. index++;
  569. continue;
  570. }
  571. /* We have a match and @index is where it's at */
  572. found = 1;
  573. break;
  574. }
  575. if (!found)
  576. return -EINVAL;
  577. return of_address_to_resource(np, index, res);
  578. }
  579. static u32 stm_fundamental_data_size(struct stm_drvdata *drvdata)
  580. {
  581. u32 stmspfeat2r;
  582. if (!IS_ENABLED(CONFIG_64BIT))
  583. return 4;
  584. stmspfeat2r = readl_relaxed(drvdata->base + STMSPFEAT2R);
  585. /*
  586. * bit[15:12] represents the fundamental data size
  587. * 0 - 32-bit data
  588. * 1 - 64-bit data
  589. */
  590. return BMVAL(stmspfeat2r, 12, 15) ? 8 : 4;
  591. }
  592. static u32 stm_num_stimulus_port(struct stm_drvdata *drvdata)
  593. {
  594. u32 numsp;
  595. numsp = readl_relaxed(drvdata->base + CORESIGHT_DEVID);
  596. /*
  597. * NUMPS in STMDEVID is 17 bit long and if equal to 0x0,
  598. * 32 stimulus ports are supported.
  599. */
  600. numsp &= 0x1ffff;
  601. if (!numsp)
  602. numsp = STM_32_CHANNEL;
  603. return numsp;
  604. }
  605. static void stm_init_default_data(struct stm_drvdata *drvdata)
  606. {
  607. /* Don't use port selection */
  608. drvdata->stmspscr = 0x0;
  609. /*
  610. * Enable all channel regardless of their number. When port
  611. * selection isn't used (see above) STMSPER applies to all
  612. * 32 channel group available, hence setting all 32 bits to 1
  613. */
  614. drvdata->stmsper = ~0x0;
  615. /*
  616. * The trace ID value for *ETM* tracers start at CPU_ID * 2 + 0x10 and
  617. * anything equal to or higher than 0x70 is reserved. Since 0x00 is
  618. * also reserved the STM trace ID needs to be higher than 0x00 and
  619. * lowner than 0x10.
  620. */
  621. drvdata->traceid = 0x1;
  622. /* Set invariant transaction timing on all channels */
  623. bitmap_clear(drvdata->chs.guaranteed, 0, drvdata->numsp);
  624. }
  625. static void stm_init_generic_data(struct stm_drvdata *drvdata)
  626. {
  627. drvdata->stm.name = dev_name(drvdata->dev);
  628. /*
  629. * MasterIDs are assigned at HW design phase. As such the core is
  630. * using a single master for interaction with this device.
  631. */
  632. drvdata->stm.sw_start = 1;
  633. drvdata->stm.sw_end = 1;
  634. drvdata->stm.hw_override = true;
  635. drvdata->stm.sw_nchannels = drvdata->numsp;
  636. drvdata->stm.packet = stm_generic_packet;
  637. drvdata->stm.link = stm_generic_link;
  638. drvdata->stm.unlink = stm_generic_unlink;
  639. drvdata->stm.set_options = stm_generic_set_options;
  640. }
  641. static int stm_probe(struct amba_device *adev, const struct amba_id *id)
  642. {
  643. int ret;
  644. void __iomem *base;
  645. unsigned long *guaranteed;
  646. struct device *dev = &adev->dev;
  647. struct coresight_platform_data *pdata = NULL;
  648. struct stm_drvdata *drvdata;
  649. struct resource *res = &adev->res;
  650. struct resource ch_res;
  651. size_t res_size, bitmap_size;
  652. struct coresight_desc *desc;
  653. struct device_node *np = adev->dev.of_node;
  654. if (np) {
  655. pdata = of_get_coresight_platform_data(dev, np);
  656. if (IS_ERR(pdata))
  657. return PTR_ERR(pdata);
  658. adev->dev.platform_data = pdata;
  659. }
  660. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  661. if (!drvdata)
  662. return -ENOMEM;
  663. drvdata->dev = &adev->dev;
  664. drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
  665. if (!IS_ERR(drvdata->atclk)) {
  666. ret = clk_prepare_enable(drvdata->atclk);
  667. if (ret)
  668. return ret;
  669. }
  670. dev_set_drvdata(dev, drvdata);
  671. base = devm_ioremap_resource(dev, res);
  672. if (IS_ERR(base))
  673. return PTR_ERR(base);
  674. drvdata->base = base;
  675. ret = stm_get_resource_byname(np, "stm-stimulus-base", &ch_res);
  676. if (ret)
  677. return ret;
  678. base = devm_ioremap_resource(dev, &ch_res);
  679. if (IS_ERR(base))
  680. return PTR_ERR(base);
  681. drvdata->chs.base = base;
  682. drvdata->write_bytes = stm_fundamental_data_size(drvdata);
  683. if (boot_nr_channel) {
  684. drvdata->numsp = boot_nr_channel;
  685. res_size = min((resource_size_t)(boot_nr_channel *
  686. BYTES_PER_CHANNEL), resource_size(res));
  687. } else {
  688. drvdata->numsp = stm_num_stimulus_port(drvdata);
  689. res_size = min((resource_size_t)(drvdata->numsp *
  690. BYTES_PER_CHANNEL), resource_size(res));
  691. }
  692. bitmap_size = BITS_TO_LONGS(drvdata->numsp) * sizeof(long);
  693. guaranteed = devm_kzalloc(dev, bitmap_size, GFP_KERNEL);
  694. if (!guaranteed)
  695. return -ENOMEM;
  696. drvdata->chs.guaranteed = guaranteed;
  697. spin_lock_init(&drvdata->spinlock);
  698. stm_init_default_data(drvdata);
  699. stm_init_generic_data(drvdata);
  700. if (stm_register_device(dev, &drvdata->stm, THIS_MODULE)) {
  701. dev_info(dev,
  702. "stm_register_device failed, probing deffered\n");
  703. return -EPROBE_DEFER;
  704. }
  705. desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
  706. if (!desc) {
  707. ret = -ENOMEM;
  708. goto stm_unregister;
  709. }
  710. desc->type = CORESIGHT_DEV_TYPE_SOURCE;
  711. desc->subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE;
  712. desc->ops = &stm_cs_ops;
  713. desc->pdata = pdata;
  714. desc->dev = dev;
  715. desc->groups = coresight_stm_groups;
  716. drvdata->csdev = coresight_register(desc);
  717. if (IS_ERR(drvdata->csdev)) {
  718. ret = PTR_ERR(drvdata->csdev);
  719. goto stm_unregister;
  720. }
  721. pm_runtime_put(&adev->dev);
  722. dev_info(dev, "%s initialized\n", (char *)id->data);
  723. return 0;
  724. stm_unregister:
  725. stm_unregister_device(&drvdata->stm);
  726. return ret;
  727. }
  728. #ifdef CONFIG_PM
  729. static int stm_runtime_suspend(struct device *dev)
  730. {
  731. struct stm_drvdata *drvdata = dev_get_drvdata(dev);
  732. if (drvdata && !IS_ERR(drvdata->atclk))
  733. clk_disable_unprepare(drvdata->atclk);
  734. return 0;
  735. }
  736. static int stm_runtime_resume(struct device *dev)
  737. {
  738. struct stm_drvdata *drvdata = dev_get_drvdata(dev);
  739. if (drvdata && !IS_ERR(drvdata->atclk))
  740. clk_prepare_enable(drvdata->atclk);
  741. return 0;
  742. }
  743. #endif
  744. static const struct dev_pm_ops stm_dev_pm_ops = {
  745. SET_RUNTIME_PM_OPS(stm_runtime_suspend, stm_runtime_resume, NULL)
  746. };
  747. static struct amba_id stm_ids[] = {
  748. {
  749. .id = 0x0003b962,
  750. .mask = 0x0003ffff,
  751. .data = "STM32",
  752. },
  753. { 0, 0},
  754. };
  755. static struct amba_driver stm_driver = {
  756. .drv = {
  757. .name = "coresight-stm",
  758. .owner = THIS_MODULE,
  759. .pm = &stm_dev_pm_ops,
  760. .suppress_bind_attrs = true,
  761. },
  762. .probe = stm_probe,
  763. .id_table = stm_ids,
  764. };
  765. builtin_amba_driver(stm_driver);