coresight-stm.c 23 KB

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