coresight-stm.c 23 KB

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