1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
5 * Description: CoreSight System Trace Macrocell driver
7 * Initial implementation by Pratik Patel
8 * (C) 2014-2015 Pratik Patel <pratikp@codeaurora.org>
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>
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>
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>
32 #include "coresight-priv.h"
34 #define STMDMASTARTR 0xc04
35 #define STMDMASTOPR 0xc08
36 #define STMDMASTATR 0xc0c
37 #define STMDMACTLR 0xc10
38 #define STMDMAIDR 0xcfc
40 #define STMHETER 0xd20
41 #define STMHEBSR 0xd60
42 #define STMHEMCR 0xd64
43 #define STMHEMASTR 0xdf4
44 #define STMHEFEAT1R 0xdf8
45 #define STMHEIDR 0xdfc
47 #define STMSPTER 0xe20
48 #define STMPRIVMASKR 0xe40
49 #define STMSPSCR 0xe60
50 #define STMSPMSCR 0xe64
51 #define STMSPOVERRIDER 0xe68
52 #define STMSPMOVERRIDER 0xe6c
53 #define STMSPTRIGCSR 0xe70
55 #define STMTSSTIMR 0xe84
56 #define STMTSFREQR 0xe8c
57 #define STMSYNCR 0xe90
58 #define STMAUXCR 0xe94
59 #define STMSPFEAT1R 0xea0
60 #define STMSPFEAT2R 0xea4
61 #define STMSPFEAT3R 0xea8
62 #define STMITTRIGGER 0xee8
63 #define STMITATBDATA0 0xeec
64 #define STMITATBCTR2 0xef0
65 #define STMITATBID 0xef4
66 #define STMITATBCTR0 0xef8
68 #define STM_32_CHANNEL 32
69 #define BYTES_PER_CHANNEL 256
70 #define STM_TRACE_BUF_SIZE 4096
71 #define STM_SW_MASTER_END 127
73 /* Register bit definition */
74 #define STMTCSR_BUSY_BIT 23
75 /* Reserve the first 10 channels for kernel usage */
76 #define STM_CHANNEL_OFFSET 0
79 STM_PKT_TYPE_DATA
= 0x98,
80 STM_PKT_TYPE_FLAG
= 0xE8,
81 STM_PKT_TYPE_TRIG
= 0xF8,
84 #define stm_channel_addr(drvdata, ch) (drvdata->chs.base + \
85 (ch * BYTES_PER_CHANNEL))
86 #define stm_channel_off(type, opts) (type & ~opts)
88 static int boot_nr_channel
;
91 * Not really modular but using module_param is the easiest way to
92 * remain consistent with existing use cases for now.
95 boot_nr_channel
, boot_nr_channel
, int, S_IRUGO
99 * struct channel_space - central management entity for extended ports
100 * @base: memory mapped base address where channels start.
101 * @phys: physical base address of channel region.
102 * @guaraneed: is the channel delivery guaranteed.
104 struct channel_space
{
107 unsigned long *guaranteed
;
111 * struct stm_drvdata - specifics associated to an STM component
112 * @base: memory mapped base address for this component.
113 * @dev: the device entity associated to this component.
114 * @atclk: optional clock for the core parts of the STM.
115 * @csdev: component vitals needed by the framework.
116 * @spinlock: only one at a time pls.
117 * @chs: the channels accociated to this STM.
118 * @stm: structure associated to the generic STM interface.
119 * @mode: this tracer's mode, i.e sysFS, or disabled.
120 * @traceid: value of the current ID for this component.
121 * @write_bytes: Maximus bytes this STM can write at a time.
122 * @stmsper: settings for register STMSPER.
123 * @stmspscr: settings for register STMSPSCR.
124 * @numsp: the total number of stimulus port support by this STM.
125 * @stmheer: settings for register STMHEER.
126 * @stmheter: settings for register STMHETER.
127 * @stmhebsr: settings for register STMHEBSR.
133 struct coresight_device
*csdev
;
135 struct channel_space chs
;
148 static void stm_hwevent_enable_hw(struct stm_drvdata
*drvdata
)
150 CS_UNLOCK(drvdata
->base
);
152 writel_relaxed(drvdata
->stmhebsr
, drvdata
->base
+ STMHEBSR
);
153 writel_relaxed(drvdata
->stmheter
, drvdata
->base
+ STMHETER
);
154 writel_relaxed(drvdata
->stmheer
, drvdata
->base
+ STMHEER
);
155 writel_relaxed(0x01 | /* Enable HW event tracing */
156 0x04, /* Error detection on event tracing */
157 drvdata
->base
+ STMHEMCR
);
159 CS_LOCK(drvdata
->base
);
162 static void stm_port_enable_hw(struct stm_drvdata
*drvdata
)
164 CS_UNLOCK(drvdata
->base
);
165 /* ATB trigger enable on direct writes to TRIG locations */
167 drvdata
->base
+ STMSPTRIGCSR
);
168 writel_relaxed(drvdata
->stmspscr
, drvdata
->base
+ STMSPSCR
);
169 writel_relaxed(drvdata
->stmsper
, drvdata
->base
+ STMSPER
);
171 CS_LOCK(drvdata
->base
);
174 static void stm_enable_hw(struct stm_drvdata
*drvdata
)
176 if (drvdata
->stmheer
)
177 stm_hwevent_enable_hw(drvdata
);
179 stm_port_enable_hw(drvdata
);
181 CS_UNLOCK(drvdata
->base
);
183 /* 4096 byte between synchronisation packets */
184 writel_relaxed(0xFFF, drvdata
->base
+ STMSYNCR
);
185 writel_relaxed((drvdata
->traceid
<< 16 | /* trace id */
186 0x02 | /* timestamp enable */
187 0x01), /* global STM enable */
188 drvdata
->base
+ STMTCSR
);
190 CS_LOCK(drvdata
->base
);
193 static int stm_enable(struct coresight_device
*csdev
,
194 struct perf_event
*event
, u32 mode
)
197 struct stm_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
199 if (mode
!= CS_MODE_SYSFS
)
202 val
= local_cmpxchg(&drvdata
->mode
, CS_MODE_DISABLED
, mode
);
204 /* Someone is already using the tracer */
208 pm_runtime_get_sync(drvdata
->dev
);
210 spin_lock(&drvdata
->spinlock
);
211 stm_enable_hw(drvdata
);
212 spin_unlock(&drvdata
->spinlock
);
214 dev_info(drvdata
->dev
, "STM tracing enabled\n");
218 static void stm_hwevent_disable_hw(struct stm_drvdata
*drvdata
)
220 CS_UNLOCK(drvdata
->base
);
222 writel_relaxed(0x0, drvdata
->base
+ STMHEMCR
);
223 writel_relaxed(0x0, drvdata
->base
+ STMHEER
);
224 writel_relaxed(0x0, drvdata
->base
+ STMHETER
);
226 CS_LOCK(drvdata
->base
);
229 static void stm_port_disable_hw(struct stm_drvdata
*drvdata
)
231 CS_UNLOCK(drvdata
->base
);
233 writel_relaxed(0x0, drvdata
->base
+ STMSPER
);
234 writel_relaxed(0x0, drvdata
->base
+ STMSPTRIGCSR
);
236 CS_LOCK(drvdata
->base
);
239 static void stm_disable_hw(struct stm_drvdata
*drvdata
)
243 CS_UNLOCK(drvdata
->base
);
245 val
= readl_relaxed(drvdata
->base
+ STMTCSR
);
246 val
&= ~0x1; /* clear global STM enable [0] */
247 writel_relaxed(val
, drvdata
->base
+ STMTCSR
);
249 CS_LOCK(drvdata
->base
);
251 stm_port_disable_hw(drvdata
);
252 if (drvdata
->stmheer
)
253 stm_hwevent_disable_hw(drvdata
);
256 static void stm_disable(struct coresight_device
*csdev
,
257 struct perf_event
*event
)
259 struct stm_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
262 * For as long as the tracer isn't disabled another entity can't
263 * change its status. As such we can read the status here without
264 * fearing it will change under us.
266 if (local_read(&drvdata
->mode
) == CS_MODE_SYSFS
) {
267 spin_lock(&drvdata
->spinlock
);
268 stm_disable_hw(drvdata
);
269 spin_unlock(&drvdata
->spinlock
);
271 /* Wait until the engine has completely stopped */
272 coresight_timeout(drvdata
->base
, STMTCSR
, STMTCSR_BUSY_BIT
, 0);
274 pm_runtime_put(drvdata
->dev
);
276 local_set(&drvdata
->mode
, CS_MODE_DISABLED
);
277 dev_info(drvdata
->dev
, "STM tracing disabled\n");
281 static int stm_trace_id(struct coresight_device
*csdev
)
283 struct stm_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
285 return drvdata
->traceid
;
288 static const struct coresight_ops_source stm_source_ops
= {
289 .trace_id
= stm_trace_id
,
290 .enable
= stm_enable
,
291 .disable
= stm_disable
,
294 static const struct coresight_ops stm_cs_ops
= {
295 .source_ops
= &stm_source_ops
,
298 static inline bool stm_addr_unaligned(const void *addr
, u8 write_bytes
)
300 return ((unsigned long)addr
& (write_bytes
- 1));
303 static void stm_send(void __iomem
*addr
, const void *data
,
304 u32 size
, u8 write_bytes
)
308 if (stm_addr_unaligned(data
, write_bytes
)) {
309 memcpy(paload
, data
, size
);
313 /* now we are 64bit/32bit aligned */
317 writeq_relaxed(*(u64
*)data
, addr
);
321 writel_relaxed(*(u32
*)data
, addr
);
324 writew_relaxed(*(u16
*)data
, addr
);
327 writeb_relaxed(*(u8
*)data
, addr
);
334 static int stm_generic_link(struct stm_data
*stm_data
,
335 unsigned int master
, unsigned int channel
)
337 struct stm_drvdata
*drvdata
= container_of(stm_data
,
338 struct stm_drvdata
, stm
);
339 if (!drvdata
|| !drvdata
->csdev
)
342 return coresight_enable(drvdata
->csdev
);
345 static void stm_generic_unlink(struct stm_data
*stm_data
,
346 unsigned int master
, unsigned int channel
)
348 struct stm_drvdata
*drvdata
= container_of(stm_data
,
349 struct stm_drvdata
, stm
);
350 if (!drvdata
|| !drvdata
->csdev
)
353 coresight_disable(drvdata
->csdev
);
357 stm_mmio_addr(struct stm_data
*stm_data
, unsigned int master
,
358 unsigned int channel
, unsigned int nr_chans
)
360 struct stm_drvdata
*drvdata
= container_of(stm_data
,
361 struct stm_drvdata
, stm
);
364 addr
= drvdata
->chs
.phys
+ channel
* BYTES_PER_CHANNEL
;
366 if (offset_in_page(addr
) ||
367 offset_in_page(nr_chans
* BYTES_PER_CHANNEL
))
373 static long stm_generic_set_options(struct stm_data
*stm_data
,
375 unsigned int channel
,
376 unsigned int nr_chans
,
377 unsigned long options
)
379 struct stm_drvdata
*drvdata
= container_of(stm_data
,
380 struct stm_drvdata
, stm
);
381 if (!(drvdata
&& local_read(&drvdata
->mode
)))
384 if (channel
>= drvdata
->numsp
)
388 case STM_OPTION_GUARANTEED
:
389 set_bit(channel
, drvdata
->chs
.guaranteed
);
392 case STM_OPTION_INVARIANT
:
393 clear_bit(channel
, drvdata
->chs
.guaranteed
);
403 static ssize_t notrace
stm_generic_packet(struct stm_data
*stm_data
,
405 unsigned int channel
,
409 const unsigned char *payload
)
411 void __iomem
*ch_addr
;
412 struct stm_drvdata
*drvdata
= container_of(stm_data
,
413 struct stm_drvdata
, stm
);
415 if (!(drvdata
&& local_read(&drvdata
->mode
)))
418 if (channel
>= drvdata
->numsp
)
421 ch_addr
= stm_channel_addr(drvdata
, channel
);
423 flags
= (flags
== STP_PACKET_TIMESTAMPED
) ? STM_FLAG_TIMESTAMPED
: 0;
424 flags
|= test_bit(channel
, drvdata
->chs
.guaranteed
) ?
425 STM_FLAG_GUARANTEED
: 0;
427 if (size
> drvdata
->write_bytes
)
428 size
= drvdata
->write_bytes
;
430 size
= rounddown_pow_of_two(size
);
433 case STP_PACKET_FLAG
:
434 ch_addr
+= stm_channel_off(STM_PKT_TYPE_FLAG
, flags
);
437 * The generic STM core sets a size of '0' on flag packets.
438 * As such send a flag packet of size '1' and tell the
441 stm_send(ch_addr
, payload
, 1, drvdata
->write_bytes
);
445 case STP_PACKET_DATA
:
446 ch_addr
+= stm_channel_off(STM_PKT_TYPE_DATA
, flags
);
447 stm_send(ch_addr
, payload
, size
,
448 drvdata
->write_bytes
);
458 static ssize_t
hwevent_enable_show(struct device
*dev
,
459 struct device_attribute
*attr
, char *buf
)
461 struct stm_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
462 unsigned long val
= drvdata
->stmheer
;
464 return scnprintf(buf
, PAGE_SIZE
, "%#lx\n", val
);
467 static ssize_t
hwevent_enable_store(struct device
*dev
,
468 struct device_attribute
*attr
,
469 const char *buf
, size_t size
)
471 struct stm_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
475 ret
= kstrtoul(buf
, 16, &val
);
479 drvdata
->stmheer
= val
;
480 /* HW event enable and trigger go hand in hand */
481 drvdata
->stmheter
= val
;
485 static DEVICE_ATTR_RW(hwevent_enable
);
487 static ssize_t
hwevent_select_show(struct device
*dev
,
488 struct device_attribute
*attr
, char *buf
)
490 struct stm_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
491 unsigned long val
= drvdata
->stmhebsr
;
493 return scnprintf(buf
, PAGE_SIZE
, "%#lx\n", val
);
496 static ssize_t
hwevent_select_store(struct device
*dev
,
497 struct device_attribute
*attr
,
498 const char *buf
, size_t size
)
500 struct stm_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
504 ret
= kstrtoul(buf
, 16, &val
);
508 drvdata
->stmhebsr
= val
;
512 static DEVICE_ATTR_RW(hwevent_select
);
514 static ssize_t
port_select_show(struct device
*dev
,
515 struct device_attribute
*attr
, char *buf
)
517 struct stm_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
520 if (!local_read(&drvdata
->mode
)) {
521 val
= drvdata
->stmspscr
;
523 spin_lock(&drvdata
->spinlock
);
524 val
= readl_relaxed(drvdata
->base
+ STMSPSCR
);
525 spin_unlock(&drvdata
->spinlock
);
528 return scnprintf(buf
, PAGE_SIZE
, "%#lx\n", val
);
531 static ssize_t
port_select_store(struct device
*dev
,
532 struct device_attribute
*attr
,
533 const char *buf
, size_t size
)
535 struct stm_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
536 unsigned long val
, stmsper
;
539 ret
= kstrtoul(buf
, 16, &val
);
543 spin_lock(&drvdata
->spinlock
);
544 drvdata
->stmspscr
= val
;
546 if (local_read(&drvdata
->mode
)) {
547 CS_UNLOCK(drvdata
->base
);
548 /* Process as per ARM's TRM recommendation */
549 stmsper
= readl_relaxed(drvdata
->base
+ STMSPER
);
550 writel_relaxed(0x0, drvdata
->base
+ STMSPER
);
551 writel_relaxed(drvdata
->stmspscr
, drvdata
->base
+ STMSPSCR
);
552 writel_relaxed(stmsper
, drvdata
->base
+ STMSPER
);
553 CS_LOCK(drvdata
->base
);
555 spin_unlock(&drvdata
->spinlock
);
559 static DEVICE_ATTR_RW(port_select
);
561 static ssize_t
port_enable_show(struct device
*dev
,
562 struct device_attribute
*attr
, char *buf
)
564 struct stm_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
567 if (!local_read(&drvdata
->mode
)) {
568 val
= drvdata
->stmsper
;
570 spin_lock(&drvdata
->spinlock
);
571 val
= readl_relaxed(drvdata
->base
+ STMSPER
);
572 spin_unlock(&drvdata
->spinlock
);
575 return scnprintf(buf
, PAGE_SIZE
, "%#lx\n", val
);
578 static ssize_t
port_enable_store(struct device
*dev
,
579 struct device_attribute
*attr
,
580 const char *buf
, size_t size
)
582 struct stm_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
586 ret
= kstrtoul(buf
, 16, &val
);
590 spin_lock(&drvdata
->spinlock
);
591 drvdata
->stmsper
= val
;
593 if (local_read(&drvdata
->mode
)) {
594 CS_UNLOCK(drvdata
->base
);
595 writel_relaxed(drvdata
->stmsper
, drvdata
->base
+ STMSPER
);
596 CS_LOCK(drvdata
->base
);
598 spin_unlock(&drvdata
->spinlock
);
602 static DEVICE_ATTR_RW(port_enable
);
604 static ssize_t
traceid_show(struct device
*dev
,
605 struct device_attribute
*attr
, char *buf
)
608 struct stm_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
610 val
= drvdata
->traceid
;
611 return sprintf(buf
, "%#lx\n", val
);
614 static ssize_t
traceid_store(struct device
*dev
,
615 struct device_attribute
*attr
,
616 const char *buf
, size_t size
)
620 struct stm_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
622 ret
= kstrtoul(buf
, 16, &val
);
626 /* traceid field is 7bit wide on STM32 */
627 drvdata
->traceid
= val
& 0x7f;
630 static DEVICE_ATTR_RW(traceid
);
632 #define coresight_stm_reg(name, offset) \
633 coresight_simple_reg32(struct stm_drvdata, name, offset)
635 coresight_stm_reg(tcsr
, STMTCSR
);
636 coresight_stm_reg(tsfreqr
, STMTSFREQR
);
637 coresight_stm_reg(syncr
, STMSYNCR
);
638 coresight_stm_reg(sper
, STMSPER
);
639 coresight_stm_reg(spter
, STMSPTER
);
640 coresight_stm_reg(privmaskr
, STMPRIVMASKR
);
641 coresight_stm_reg(spscr
, STMSPSCR
);
642 coresight_stm_reg(spmscr
, STMSPMSCR
);
643 coresight_stm_reg(spfeat1r
, STMSPFEAT1R
);
644 coresight_stm_reg(spfeat2r
, STMSPFEAT2R
);
645 coresight_stm_reg(spfeat3r
, STMSPFEAT3R
);
646 coresight_stm_reg(devid
, CORESIGHT_DEVID
);
648 static struct attribute
*coresight_stm_attrs
[] = {
649 &dev_attr_hwevent_enable
.attr
,
650 &dev_attr_hwevent_select
.attr
,
651 &dev_attr_port_enable
.attr
,
652 &dev_attr_port_select
.attr
,
653 &dev_attr_traceid
.attr
,
657 static struct attribute
*coresight_stm_mgmt_attrs
[] = {
659 &dev_attr_tsfreqr
.attr
,
660 &dev_attr_syncr
.attr
,
662 &dev_attr_spter
.attr
,
663 &dev_attr_privmaskr
.attr
,
664 &dev_attr_spscr
.attr
,
665 &dev_attr_spmscr
.attr
,
666 &dev_attr_spfeat1r
.attr
,
667 &dev_attr_spfeat2r
.attr
,
668 &dev_attr_spfeat3r
.attr
,
669 &dev_attr_devid
.attr
,
673 static const struct attribute_group coresight_stm_group
= {
674 .attrs
= coresight_stm_attrs
,
677 static const struct attribute_group coresight_stm_mgmt_group
= {
678 .attrs
= coresight_stm_mgmt_attrs
,
682 static const struct attribute_group
*coresight_stm_groups
[] = {
683 &coresight_stm_group
,
684 &coresight_stm_mgmt_group
,
688 static int stm_get_resource_byname(struct device_node
*np
,
689 char *ch_base
, struct resource
*res
)
691 const char *name
= NULL
;
692 int index
= 0, found
= 0;
694 while (!of_property_read_string_index(np
, "reg-names", index
, &name
)) {
695 if (strcmp(ch_base
, name
)) {
700 /* We have a match and @index is where it's at */
708 return of_address_to_resource(np
, index
, res
);
711 static u32
stm_fundamental_data_size(struct stm_drvdata
*drvdata
)
715 if (!IS_ENABLED(CONFIG_64BIT
))
718 stmspfeat2r
= readl_relaxed(drvdata
->base
+ STMSPFEAT2R
);
721 * bit[15:12] represents the fundamental data size
725 return BMVAL(stmspfeat2r
, 12, 15) ? 8 : 4;
728 static u32
stm_num_stimulus_port(struct stm_drvdata
*drvdata
)
732 numsp
= readl_relaxed(drvdata
->base
+ CORESIGHT_DEVID
);
734 * NUMPS in STMDEVID is 17 bit long and if equal to 0x0,
735 * 32 stimulus ports are supported.
739 numsp
= STM_32_CHANNEL
;
743 static void stm_init_default_data(struct stm_drvdata
*drvdata
)
745 /* Don't use port selection */
746 drvdata
->stmspscr
= 0x0;
748 * Enable all channel regardless of their number. When port
749 * selection isn't used (see above) STMSPER applies to all
750 * 32 channel group available, hence setting all 32 bits to 1
752 drvdata
->stmsper
= ~0x0;
755 * The trace ID value for *ETM* tracers start at CPU_ID * 2 + 0x10 and
756 * anything equal to or higher than 0x70 is reserved. Since 0x00 is
757 * also reserved the STM trace ID needs to be higher than 0x00 and
760 drvdata
->traceid
= 0x1;
762 /* Set invariant transaction timing on all channels */
763 bitmap_clear(drvdata
->chs
.guaranteed
, 0, drvdata
->numsp
);
766 static void stm_init_generic_data(struct stm_drvdata
*drvdata
)
768 drvdata
->stm
.name
= dev_name(drvdata
->dev
);
771 * MasterIDs are assigned at HW design phase. As such the core is
772 * using a single master for interaction with this device.
774 drvdata
->stm
.sw_start
= 1;
775 drvdata
->stm
.sw_end
= 1;
776 drvdata
->stm
.hw_override
= true;
777 drvdata
->stm
.sw_nchannels
= drvdata
->numsp
;
778 drvdata
->stm
.sw_mmiosz
= BYTES_PER_CHANNEL
;
779 drvdata
->stm
.packet
= stm_generic_packet
;
780 drvdata
->stm
.mmio_addr
= stm_mmio_addr
;
781 drvdata
->stm
.link
= stm_generic_link
;
782 drvdata
->stm
.unlink
= stm_generic_unlink
;
783 drvdata
->stm
.set_options
= stm_generic_set_options
;
786 static int stm_probe(struct amba_device
*adev
, const struct amba_id
*id
)
790 unsigned long *guaranteed
;
791 struct device
*dev
= &adev
->dev
;
792 struct coresight_platform_data
*pdata
= NULL
;
793 struct stm_drvdata
*drvdata
;
794 struct resource
*res
= &adev
->res
;
795 struct resource ch_res
;
796 size_t res_size
, bitmap_size
;
797 struct coresight_desc desc
= { 0 };
798 struct device_node
*np
= adev
->dev
.of_node
;
801 pdata
= of_get_coresight_platform_data(dev
, np
);
803 return PTR_ERR(pdata
);
804 adev
->dev
.platform_data
= pdata
;
806 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
810 drvdata
->dev
= &adev
->dev
;
811 drvdata
->atclk
= devm_clk_get(&adev
->dev
, "atclk"); /* optional */
812 if (!IS_ERR(drvdata
->atclk
)) {
813 ret
= clk_prepare_enable(drvdata
->atclk
);
817 dev_set_drvdata(dev
, drvdata
);
819 base
= devm_ioremap_resource(dev
, res
);
821 return PTR_ERR(base
);
822 drvdata
->base
= base
;
824 ret
= stm_get_resource_byname(np
, "stm-stimulus-base", &ch_res
);
827 drvdata
->chs
.phys
= ch_res
.start
;
829 base
= devm_ioremap_resource(dev
, &ch_res
);
831 return PTR_ERR(base
);
832 drvdata
->chs
.base
= base
;
834 drvdata
->write_bytes
= stm_fundamental_data_size(drvdata
);
836 if (boot_nr_channel
) {
837 drvdata
->numsp
= boot_nr_channel
;
838 res_size
= min((resource_size_t
)(boot_nr_channel
*
839 BYTES_PER_CHANNEL
), resource_size(res
));
841 drvdata
->numsp
= stm_num_stimulus_port(drvdata
);
842 res_size
= min((resource_size_t
)(drvdata
->numsp
*
843 BYTES_PER_CHANNEL
), resource_size(res
));
845 bitmap_size
= BITS_TO_LONGS(drvdata
->numsp
) * sizeof(long);
847 guaranteed
= devm_kzalloc(dev
, bitmap_size
, GFP_KERNEL
);
850 drvdata
->chs
.guaranteed
= guaranteed
;
852 spin_lock_init(&drvdata
->spinlock
);
854 stm_init_default_data(drvdata
);
855 stm_init_generic_data(drvdata
);
857 if (stm_register_device(dev
, &drvdata
->stm
, THIS_MODULE
)) {
859 "stm_register_device failed, probing deffered\n");
860 return -EPROBE_DEFER
;
863 desc
.type
= CORESIGHT_DEV_TYPE_SOURCE
;
864 desc
.subtype
.source_subtype
= CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE
;
865 desc
.ops
= &stm_cs_ops
;
868 desc
.groups
= coresight_stm_groups
;
869 drvdata
->csdev
= coresight_register(&desc
);
870 if (IS_ERR(drvdata
->csdev
)) {
871 ret
= PTR_ERR(drvdata
->csdev
);
875 pm_runtime_put(&adev
->dev
);
877 dev_info(dev
, "%s initialized\n", (char *)id
->data
);
881 stm_unregister_device(&drvdata
->stm
);
886 static int stm_runtime_suspend(struct device
*dev
)
888 struct stm_drvdata
*drvdata
= dev_get_drvdata(dev
);
890 if (drvdata
&& !IS_ERR(drvdata
->atclk
))
891 clk_disable_unprepare(drvdata
->atclk
);
896 static int stm_runtime_resume(struct device
*dev
)
898 struct stm_drvdata
*drvdata
= dev_get_drvdata(dev
);
900 if (drvdata
&& !IS_ERR(drvdata
->atclk
))
901 clk_prepare_enable(drvdata
->atclk
);
907 static const struct dev_pm_ops stm_dev_pm_ops
= {
908 SET_RUNTIME_PM_OPS(stm_runtime_suspend
, stm_runtime_resume
, NULL
)
911 static const struct amba_id stm_ids
[] = {
925 static struct amba_driver stm_driver
= {
927 .name
= "coresight-stm",
928 .owner
= THIS_MODULE
,
929 .pm
= &stm_dev_pm_ops
,
930 .suppress_bind_attrs
= true,
936 builtin_amba_driver(stm_driver
);