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/acpi.h>
20 #include <linux/amba/bus.h>
21 #include <linux/bitmap.h>
22 #include <linux/clk.h>
23 #include <linux/coresight.h>
24 #include <linux/coresight-stm.h>
25 #include <linux/err.h>
26 #include <linux/kernel.h>
27 #include <linux/moduleparam.h>
28 #include <linux/of_address.h>
29 #include <linux/perf_event.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/stm.h>
32 #include <linux/platform_device.h>
33
34 #include "coresight-priv.h"
35 #include "coresight-trace-id.h"
36
37 #define STMDMASTARTR 0xc04
38 #define STMDMASTOPR 0xc08
39 #define STMDMASTATR 0xc0c
40 #define STMDMACTLR 0xc10
41 #define STMDMAIDR 0xcfc
42 #define STMHEER 0xd00
43 #define STMHETER 0xd20
44 #define STMHEBSR 0xd60
45 #define STMHEMCR 0xd64
46 #define STMHEMASTR 0xdf4
47 #define STMHEFEAT1R 0xdf8
48 #define STMHEIDR 0xdfc
49 #define STMSPER 0xe00
50 #define STMSPTER 0xe20
51 #define STMPRIVMASKR 0xe40
52 #define STMSPSCR 0xe60
53 #define STMSPMSCR 0xe64
54 #define STMSPOVERRIDER 0xe68
55 #define STMSPMOVERRIDER 0xe6c
56 #define STMSPTRIGCSR 0xe70
57 #define STMTCSR 0xe80
58 #define STMTSSTIMR 0xe84
59 #define STMTSFREQR 0xe8c
60 #define STMSYNCR 0xe90
61 #define STMAUXCR 0xe94
62 #define STMSPFEAT1R 0xea0
63 #define STMSPFEAT2R 0xea4
64 #define STMSPFEAT3R 0xea8
65 #define STMITTRIGGER 0xee8
66 #define STMITATBDATA0 0xeec
67 #define STMITATBCTR2 0xef0
68 #define STMITATBID 0xef4
69 #define STMITATBCTR0 0xef8
70
71 #define STM_32_CHANNEL 32
72 #define BYTES_PER_CHANNEL 256
73 #define STM_TRACE_BUF_SIZE 4096
74 #define STM_SW_MASTER_END 127
75
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
81 enum stm_pkt_type {
82 STM_PKT_TYPE_DATA = 0x98,
83 STM_PKT_TYPE_FLAG = 0xE8,
84 STM_PKT_TYPE_TRIG = 0xF8,
85 };
86
87 #define stm_channel_addr(drvdata, ch) (drvdata->chs.base + \
88 (ch * BYTES_PER_CHANNEL))
89 #define stm_channel_off(type, opts) (type & ~opts)
90
91 static int boot_nr_channel;
92
93 /*
94 * Not really modular but using module_param is the easiest way to
95 * remain consistent with existing use cases for now.
96 */
97 module_param_named(
98 boot_nr_channel, boot_nr_channel, int, S_IRUGO
99 );
100
101 /*
102 * struct channel_space - central management entity for extended ports
103 * @base: memory mapped base address where channels start.
104 * @phys: physical base address of channel region.
105 * @guaraneed: is the channel delivery guaranteed.
106 */
107 struct channel_space {
108 void __iomem *base;
109 phys_addr_t phys;
110 unsigned long *guaranteed;
111 };
112
113 /**
114 * struct stm_drvdata - specifics associated to an STM component
115 * @base: memory mapped base address for this component.
116 * @atclk: optional clock for the core parts of the STM.
117 * @pclk: APB clock if present, otherwise NULL
118 * @csdev: component vitals needed by the framework.
119 * @spinlock: only one at a time pls.
120 * @chs: the channels accociated to this STM.
121 * @stm: structure associated to the generic STM interface.
122 * @traceid: value of the current ID for this component.
123 * @write_bytes: Maximus bytes this STM can write at a time.
124 * @stmsper: settings for register STMSPER.
125 * @stmspscr: settings for register STMSPSCR.
126 * @numsp: the total number of stimulus port support by this STM.
127 * @stmheer: settings for register STMHEER.
128 * @stmheter: settings for register STMHETER.
129 * @stmhebsr: settings for register STMHEBSR.
130 */
131 struct stm_drvdata {
132 void __iomem *base;
133 struct clk *atclk;
134 struct clk *pclk;
135 struct coresight_device *csdev;
136 spinlock_t spinlock;
137 struct channel_space chs;
138 struct stm_data stm;
139 u8 traceid;
140 u32 write_bytes;
141 u32 stmsper;
142 u32 stmspscr;
143 u32 numsp;
144 u32 stmheer;
145 u32 stmheter;
146 u32 stmhebsr;
147 };
148
stm_hwevent_enable_hw(struct stm_drvdata * drvdata)149 static void stm_hwevent_enable_hw(struct stm_drvdata *drvdata)
150 {
151 CS_UNLOCK(drvdata->base);
152
153 writel_relaxed(drvdata->stmhebsr, drvdata->base + STMHEBSR);
154 writel_relaxed(drvdata->stmheter, drvdata->base + STMHETER);
155 writel_relaxed(drvdata->stmheer, drvdata->base + STMHEER);
156 writel_relaxed(0x01 | /* Enable HW event tracing */
157 0x04, /* Error detection on event tracing */
158 drvdata->base + STMHEMCR);
159
160 CS_LOCK(drvdata->base);
161 }
162
stm_port_enable_hw(struct stm_drvdata * drvdata)163 static void stm_port_enable_hw(struct stm_drvdata *drvdata)
164 {
165 CS_UNLOCK(drvdata->base);
166 /* ATB trigger enable on direct writes to TRIG locations */
167 writel_relaxed(0x10,
168 drvdata->base + STMSPTRIGCSR);
169 writel_relaxed(drvdata->stmspscr, drvdata->base + STMSPSCR);
170 writel_relaxed(drvdata->stmsper, drvdata->base + STMSPER);
171
172 CS_LOCK(drvdata->base);
173 }
174
stm_enable_hw(struct stm_drvdata * drvdata)175 static void stm_enable_hw(struct stm_drvdata *drvdata)
176 {
177 if (drvdata->stmheer)
178 stm_hwevent_enable_hw(drvdata);
179
180 stm_port_enable_hw(drvdata);
181
182 CS_UNLOCK(drvdata->base);
183
184 /* 4096 byte between synchronisation packets */
185 writel_relaxed(0xFFF, drvdata->base + STMSYNCR);
186 writel_relaxed((drvdata->traceid << 16 | /* trace id */
187 0x02 | /* timestamp enable */
188 0x01), /* global STM enable */
189 drvdata->base + STMTCSR);
190
191 CS_LOCK(drvdata->base);
192 }
193
stm_enable(struct coresight_device * csdev,struct perf_event * event,enum cs_mode mode,__maybe_unused struct coresight_path * path)194 static int stm_enable(struct coresight_device *csdev, struct perf_event *event,
195 enum cs_mode mode,
196 __maybe_unused struct coresight_path *path)
197 {
198 struct stm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
199
200 if (mode != CS_MODE_SYSFS)
201 return -EINVAL;
202
203 if (!coresight_take_mode(csdev, mode)) {
204 /* Someone is already using the tracer */
205 return -EBUSY;
206 }
207
208 pm_runtime_get_sync(csdev->dev.parent);
209
210 spin_lock(&drvdata->spinlock);
211 stm_enable_hw(drvdata);
212 spin_unlock(&drvdata->spinlock);
213
214 dev_dbg(&csdev->dev, "STM tracing enabled\n");
215 return 0;
216 }
217
stm_hwevent_disable_hw(struct stm_drvdata * drvdata)218 static void stm_hwevent_disable_hw(struct stm_drvdata *drvdata)
219 {
220 CS_UNLOCK(drvdata->base);
221
222 writel_relaxed(0x0, drvdata->base + STMHEMCR);
223 writel_relaxed(0x0, drvdata->base + STMHEER);
224 writel_relaxed(0x0, drvdata->base + STMHETER);
225
226 CS_LOCK(drvdata->base);
227 }
228
stm_port_disable_hw(struct stm_drvdata * drvdata)229 static void stm_port_disable_hw(struct stm_drvdata *drvdata)
230 {
231 CS_UNLOCK(drvdata->base);
232
233 writel_relaxed(0x0, drvdata->base + STMSPER);
234 writel_relaxed(0x0, drvdata->base + STMSPTRIGCSR);
235
236 CS_LOCK(drvdata->base);
237 }
238
stm_disable_hw(struct stm_drvdata * drvdata)239 static void stm_disable_hw(struct stm_drvdata *drvdata)
240 {
241 u32 val;
242
243 CS_UNLOCK(drvdata->base);
244
245 val = readl_relaxed(drvdata->base + STMTCSR);
246 val &= ~0x1; /* clear global STM enable [0] */
247 writel_relaxed(val, drvdata->base + STMTCSR);
248
249 CS_LOCK(drvdata->base);
250
251 stm_port_disable_hw(drvdata);
252 if (drvdata->stmheer)
253 stm_hwevent_disable_hw(drvdata);
254 }
255
stm_disable(struct coresight_device * csdev,struct perf_event * event)256 static void stm_disable(struct coresight_device *csdev,
257 struct perf_event *event)
258 {
259 struct stm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
260 struct csdev_access *csa = &csdev->access;
261
262 /*
263 * For as long as the tracer isn't disabled another entity can't
264 * change its status. As such we can read the status here without
265 * fearing it will change under us.
266 */
267 if (coresight_get_mode(csdev) == CS_MODE_SYSFS) {
268 spin_lock(&drvdata->spinlock);
269 stm_disable_hw(drvdata);
270 spin_unlock(&drvdata->spinlock);
271
272 /* Wait until the engine has completely stopped */
273 coresight_timeout(csa, STMTCSR, STMTCSR_BUSY_BIT, 0);
274
275 pm_runtime_put(csdev->dev.parent);
276
277 coresight_set_mode(csdev, CS_MODE_DISABLED);
278 dev_dbg(&csdev->dev, "STM tracing disabled\n");
279 }
280 }
281
stm_trace_id(struct coresight_device * csdev,__maybe_unused enum cs_mode mode,__maybe_unused struct coresight_device * sink)282 static int stm_trace_id(struct coresight_device *csdev, __maybe_unused enum cs_mode mode,
283 __maybe_unused struct coresight_device *sink)
284 {
285 struct stm_drvdata *drvdata;
286
287 drvdata = dev_get_drvdata(csdev->dev.parent);
288
289 return drvdata->traceid;
290 }
291
292 static const struct coresight_ops_source stm_source_ops = {
293 .enable = stm_enable,
294 .disable = stm_disable,
295 };
296
297 static const struct coresight_ops stm_cs_ops = {
298 .trace_id = stm_trace_id,
299 .source_ops = &stm_source_ops,
300 };
301
stm_addr_unaligned(const void * addr,u8 write_bytes)302 static bool stm_addr_unaligned(const void *addr, u8 write_bytes)
303 {
304 return ((unsigned long)addr & (write_bytes - 1));
305 }
306
stm_send(void __iomem * addr,const void * data,u32 size,u8 write_bytes)307 static void stm_send(void __iomem *addr, const void *data,
308 u32 size, u8 write_bytes)
309 {
310 u8 paload[8];
311
312 if (stm_addr_unaligned(data, write_bytes)) {
313 memcpy(paload, data, size);
314 data = paload;
315 }
316
317 /* now we are 64bit/32bit aligned */
318 switch (size) {
319 #ifdef CONFIG_64BIT
320 case 8:
321 writeq_relaxed(*(u64 *)data, addr);
322 break;
323 #endif
324 case 4:
325 writel_relaxed(*(u32 *)data, addr);
326 break;
327 case 2:
328 writew_relaxed(*(u16 *)data, addr);
329 break;
330 case 1:
331 writeb_relaxed(*(u8 *)data, addr);
332 break;
333 default:
334 break;
335 }
336 }
337
stm_generic_link(struct stm_data * stm_data,unsigned int master,unsigned int channel)338 static int stm_generic_link(struct stm_data *stm_data,
339 unsigned int master, unsigned int channel)
340 {
341 struct stm_drvdata *drvdata = container_of(stm_data,
342 struct stm_drvdata, stm);
343 if (!drvdata->csdev)
344 return -EINVAL;
345
346 return coresight_enable_sysfs(drvdata->csdev);
347 }
348
stm_generic_unlink(struct stm_data * stm_data,unsigned int master,unsigned int channel)349 static void stm_generic_unlink(struct stm_data *stm_data,
350 unsigned int master, unsigned int channel)
351 {
352 struct stm_drvdata *drvdata = container_of(stm_data,
353 struct stm_drvdata, stm);
354 if (!drvdata->csdev)
355 return;
356
357 coresight_disable_sysfs(drvdata->csdev);
358 }
359
360 static phys_addr_t
stm_mmio_addr(struct stm_data * stm_data,unsigned int master,unsigned int channel,unsigned int nr_chans)361 stm_mmio_addr(struct stm_data *stm_data, unsigned int master,
362 unsigned int channel, unsigned int nr_chans)
363 {
364 struct stm_drvdata *drvdata = container_of(stm_data,
365 struct stm_drvdata, stm);
366 phys_addr_t addr;
367
368 addr = drvdata->chs.phys + channel * BYTES_PER_CHANNEL;
369
370 if (offset_in_page(addr) ||
371 offset_in_page(nr_chans * BYTES_PER_CHANNEL))
372 return 0;
373
374 return addr;
375 }
376
stm_generic_set_options(struct stm_data * stm_data,unsigned int master,unsigned int channel,unsigned int nr_chans,unsigned long options)377 static long stm_generic_set_options(struct stm_data *stm_data,
378 unsigned int master,
379 unsigned int channel,
380 unsigned int nr_chans,
381 unsigned long options)
382 {
383 struct stm_drvdata *drvdata = container_of(stm_data,
384 struct stm_drvdata, stm);
385 if (!coresight_get_mode(drvdata->csdev))
386 return -EINVAL;
387
388 if (channel >= drvdata->numsp)
389 return -EINVAL;
390
391 switch (options) {
392 case STM_OPTION_GUARANTEED:
393 set_bit(channel, drvdata->chs.guaranteed);
394 break;
395
396 case STM_OPTION_INVARIANT:
397 clear_bit(channel, drvdata->chs.guaranteed);
398 break;
399
400 default:
401 return -EINVAL;
402 }
403
404 return 0;
405 }
406
stm_generic_packet(struct stm_data * stm_data,unsigned int master,unsigned int channel,unsigned int packet,unsigned int flags,unsigned int size,const unsigned char * payload)407 static ssize_t notrace stm_generic_packet(struct stm_data *stm_data,
408 unsigned int master,
409 unsigned int channel,
410 unsigned int packet,
411 unsigned int flags,
412 unsigned int size,
413 const unsigned char *payload)
414 {
415 void __iomem *ch_addr;
416 struct stm_drvdata *drvdata = container_of(stm_data,
417 struct stm_drvdata, stm);
418 unsigned int stm_flags;
419
420 if (!coresight_get_mode(drvdata->csdev))
421 return -EACCES;
422
423 if (channel >= drvdata->numsp)
424 return -EINVAL;
425
426 ch_addr = stm_channel_addr(drvdata, channel);
427
428 stm_flags = (flags & STP_PACKET_TIMESTAMPED) ?
429 STM_FLAG_TIMESTAMPED : 0;
430 stm_flags |= test_bit(channel, drvdata->chs.guaranteed) ?
431 STM_FLAG_GUARANTEED : 0;
432
433 if (size > drvdata->write_bytes)
434 size = drvdata->write_bytes;
435 else
436 size = rounddown_pow_of_two(size);
437
438 switch (packet) {
439 case STP_PACKET_FLAG:
440 ch_addr += stm_channel_off(STM_PKT_TYPE_FLAG, stm_flags);
441
442 /*
443 * The generic STM core sets a size of '0' on flag packets.
444 * As such send a flag packet of size '1' and tell the
445 * core we did so.
446 */
447 stm_send(ch_addr, payload, 1, drvdata->write_bytes);
448 size = 1;
449 break;
450
451 case STP_PACKET_DATA:
452 stm_flags |= (flags & STP_PACKET_MARKED) ? STM_FLAG_MARKED : 0;
453 ch_addr += stm_channel_off(STM_PKT_TYPE_DATA, stm_flags);
454 stm_send(ch_addr, payload, size,
455 drvdata->write_bytes);
456 break;
457
458 default:
459 return -ENOTSUPP;
460 }
461
462 return size;
463 }
464
hwevent_enable_show(struct device * dev,struct device_attribute * attr,char * buf)465 static ssize_t hwevent_enable_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
467 {
468 struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
469 unsigned long val = drvdata->stmheer;
470
471 return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
472 }
473
hwevent_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)474 static ssize_t hwevent_enable_store(struct device *dev,
475 struct device_attribute *attr,
476 const char *buf, size_t size)
477 {
478 struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
479 unsigned long val;
480 int ret = 0;
481
482 ret = kstrtoul(buf, 16, &val);
483 if (ret)
484 return -EINVAL;
485
486 drvdata->stmheer = val;
487 /* HW event enable and trigger go hand in hand */
488 drvdata->stmheter = val;
489
490 return size;
491 }
492 static DEVICE_ATTR_RW(hwevent_enable);
493
hwevent_select_show(struct device * dev,struct device_attribute * attr,char * buf)494 static ssize_t hwevent_select_show(struct device *dev,
495 struct device_attribute *attr, char *buf)
496 {
497 struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
498 unsigned long val = drvdata->stmhebsr;
499
500 return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
501 }
502
hwevent_select_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)503 static ssize_t hwevent_select_store(struct device *dev,
504 struct device_attribute *attr,
505 const char *buf, size_t size)
506 {
507 struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
508 unsigned long val;
509 int ret = 0;
510
511 ret = kstrtoul(buf, 16, &val);
512 if (ret)
513 return -EINVAL;
514
515 drvdata->stmhebsr = val;
516
517 return size;
518 }
519 static DEVICE_ATTR_RW(hwevent_select);
520
port_select_show(struct device * dev,struct device_attribute * attr,char * buf)521 static ssize_t port_select_show(struct device *dev,
522 struct device_attribute *attr, char *buf)
523 {
524 struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
525 unsigned long val;
526
527 if (!coresight_get_mode(drvdata->csdev)) {
528 val = drvdata->stmspscr;
529 } else {
530 spin_lock(&drvdata->spinlock);
531 val = readl_relaxed(drvdata->base + STMSPSCR);
532 spin_unlock(&drvdata->spinlock);
533 }
534
535 return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
536 }
537
port_select_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)538 static ssize_t port_select_store(struct device *dev,
539 struct device_attribute *attr,
540 const char *buf, size_t size)
541 {
542 struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
543 unsigned long val, stmsper;
544 int ret = 0;
545
546 ret = kstrtoul(buf, 16, &val);
547 if (ret)
548 return ret;
549
550 spin_lock(&drvdata->spinlock);
551 drvdata->stmspscr = val;
552
553 if (coresight_get_mode(drvdata->csdev)) {
554 CS_UNLOCK(drvdata->base);
555 /* Process as per ARM's TRM recommendation */
556 stmsper = readl_relaxed(drvdata->base + STMSPER);
557 writel_relaxed(0x0, drvdata->base + STMSPER);
558 writel_relaxed(drvdata->stmspscr, drvdata->base + STMSPSCR);
559 writel_relaxed(stmsper, drvdata->base + STMSPER);
560 CS_LOCK(drvdata->base);
561 }
562 spin_unlock(&drvdata->spinlock);
563
564 return size;
565 }
566 static DEVICE_ATTR_RW(port_select);
567
port_enable_show(struct device * dev,struct device_attribute * attr,char * buf)568 static ssize_t port_enable_show(struct device *dev,
569 struct device_attribute *attr, char *buf)
570 {
571 struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
572 unsigned long val;
573
574 if (!coresight_get_mode(drvdata->csdev)) {
575 val = drvdata->stmsper;
576 } else {
577 spin_lock(&drvdata->spinlock);
578 val = readl_relaxed(drvdata->base + STMSPER);
579 spin_unlock(&drvdata->spinlock);
580 }
581
582 return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
583 }
584
port_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)585 static ssize_t port_enable_store(struct device *dev,
586 struct device_attribute *attr,
587 const char *buf, size_t size)
588 {
589 struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
590 unsigned long val;
591 int ret = 0;
592
593 ret = kstrtoul(buf, 16, &val);
594 if (ret)
595 return ret;
596
597 spin_lock(&drvdata->spinlock);
598 drvdata->stmsper = val;
599
600 if (coresight_get_mode(drvdata->csdev)) {
601 CS_UNLOCK(drvdata->base);
602 writel_relaxed(drvdata->stmsper, drvdata->base + STMSPER);
603 CS_LOCK(drvdata->base);
604 }
605 spin_unlock(&drvdata->spinlock);
606
607 return size;
608 }
609 static DEVICE_ATTR_RW(port_enable);
610
traceid_show(struct device * dev,struct device_attribute * attr,char * buf)611 static ssize_t traceid_show(struct device *dev,
612 struct device_attribute *attr, char *buf)
613 {
614 unsigned long val;
615 struct stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
616
617 val = drvdata->traceid;
618 return sprintf(buf, "%#lx\n", val);
619 }
620 static DEVICE_ATTR_RO(traceid);
621
622 static struct attribute *coresight_stm_attrs[] = {
623 &dev_attr_hwevent_enable.attr,
624 &dev_attr_hwevent_select.attr,
625 &dev_attr_port_enable.attr,
626 &dev_attr_port_select.attr,
627 &dev_attr_traceid.attr,
628 NULL,
629 };
630
631 static struct attribute *coresight_stm_mgmt_attrs[] = {
632 coresight_simple_reg32(tcsr, STMTCSR),
633 coresight_simple_reg32(tsfreqr, STMTSFREQR),
634 coresight_simple_reg32(syncr, STMSYNCR),
635 coresight_simple_reg32(sper, STMSPER),
636 coresight_simple_reg32(spter, STMSPTER),
637 coresight_simple_reg32(privmaskr, STMPRIVMASKR),
638 coresight_simple_reg32(spscr, STMSPSCR),
639 coresight_simple_reg32(spmscr, STMSPMSCR),
640 coresight_simple_reg32(spfeat1r, STMSPFEAT1R),
641 coresight_simple_reg32(spfeat2r, STMSPFEAT2R),
642 coresight_simple_reg32(spfeat3r, STMSPFEAT3R),
643 coresight_simple_reg32(devid, CORESIGHT_DEVID),
644 NULL,
645 };
646
647 static const struct attribute_group coresight_stm_group = {
648 .attrs = coresight_stm_attrs,
649 };
650
651 static const struct attribute_group coresight_stm_mgmt_group = {
652 .attrs = coresight_stm_mgmt_attrs,
653 .name = "mgmt",
654 };
655
656 static const struct attribute_group *coresight_stm_groups[] = {
657 &coresight_stm_group,
658 &coresight_stm_mgmt_group,
659 NULL,
660 };
661
662 #ifdef CONFIG_OF
of_stm_get_stimulus_area(struct device * dev,struct resource * res)663 static int of_stm_get_stimulus_area(struct device *dev, struct resource *res)
664 {
665 const char *name = NULL;
666 int index = 0, found = 0;
667 struct device_node *np = dev->of_node;
668
669 while (!of_property_read_string_index(np, "reg-names", index, &name)) {
670 if (strcmp("stm-stimulus-base", name)) {
671 index++;
672 continue;
673 }
674
675 /* We have a match and @index is where it's at */
676 found = 1;
677 break;
678 }
679
680 if (!found)
681 return -EINVAL;
682
683 return of_address_to_resource(np, index, res);
684 }
685 #else
of_stm_get_stimulus_area(struct device * dev,struct resource * res)686 static int of_stm_get_stimulus_area(struct device *dev,
687 struct resource *res)
688 {
689 return -ENOENT;
690 }
691 #endif
692
693 #ifdef CONFIG_ACPI
acpi_stm_get_stimulus_area(struct device * dev,struct resource * res)694 static int acpi_stm_get_stimulus_area(struct device *dev, struct resource *res)
695 {
696 int rc;
697 bool found_base = false;
698 struct resource_entry *rent;
699 LIST_HEAD(res_list);
700
701 struct acpi_device *adev = ACPI_COMPANION(dev);
702
703 rc = acpi_dev_get_resources(adev, &res_list, NULL, NULL);
704 if (rc < 0)
705 return rc;
706
707 /*
708 * The stimulus base for STM device must be listed as the second memory
709 * resource, followed by the programming base address as described in
710 * "Section 2.3 Resources" in ACPI for CoreSightTM 1.0 Platform Design
711 * document (DEN0067).
712 */
713 rc = -ENOENT;
714 list_for_each_entry(rent, &res_list, node) {
715 if (resource_type(rent->res) != IORESOURCE_MEM)
716 continue;
717 if (found_base) {
718 *res = *rent->res;
719 rc = 0;
720 break;
721 }
722
723 found_base = true;
724 }
725
726 acpi_dev_free_resource_list(&res_list);
727 return rc;
728 }
729 #else
acpi_stm_get_stimulus_area(struct device * dev,struct resource * res)730 static int acpi_stm_get_stimulus_area(struct device *dev,
731 struct resource *res)
732 {
733 return -ENOENT;
734 }
735 #endif
736
stm_get_stimulus_area(struct device * dev,struct resource * res)737 static int stm_get_stimulus_area(struct device *dev, struct resource *res)
738 {
739 struct fwnode_handle *fwnode = dev_fwnode(dev);
740
741 if (is_of_node(fwnode))
742 return of_stm_get_stimulus_area(dev, res);
743 else if (is_acpi_node(fwnode))
744 return acpi_stm_get_stimulus_area(dev, res);
745 return -ENOENT;
746 }
747
stm_fundamental_data_size(struct stm_drvdata * drvdata)748 static u32 stm_fundamental_data_size(struct stm_drvdata *drvdata)
749 {
750 u32 stmspfeat2r;
751
752 if (!IS_ENABLED(CONFIG_64BIT))
753 return 4;
754
755 stmspfeat2r = readl_relaxed(drvdata->base + STMSPFEAT2R);
756
757 /*
758 * bit[15:12] represents the fundamental data size
759 * 0 - 32-bit data
760 * 1 - 64-bit data
761 */
762 return BMVAL(stmspfeat2r, 12, 15) ? 8 : 4;
763 }
764
stm_num_stimulus_port(struct stm_drvdata * drvdata)765 static u32 stm_num_stimulus_port(struct stm_drvdata *drvdata)
766 {
767 u32 numsp;
768
769 numsp = readl_relaxed(drvdata->base + CORESIGHT_DEVID);
770 /*
771 * NUMPS in STMDEVID is 17 bit long and if equal to 0x0,
772 * 32 stimulus ports are supported.
773 */
774 numsp &= 0x1ffff;
775 if (!numsp)
776 numsp = STM_32_CHANNEL;
777 return numsp;
778 }
779
stm_init_default_data(struct stm_drvdata * drvdata)780 static void stm_init_default_data(struct stm_drvdata *drvdata)
781 {
782 /* Don't use port selection */
783 drvdata->stmspscr = 0x0;
784 /*
785 * Enable all channel regardless of their number. When port
786 * selection isn't used (see above) STMSPER applies to all
787 * 32 channel group available, hence setting all 32 bits to 1
788 */
789 drvdata->stmsper = ~0x0;
790
791 /* Set invariant transaction timing on all channels */
792 bitmap_clear(drvdata->chs.guaranteed, 0, drvdata->numsp);
793 }
794
stm_init_generic_data(struct stm_drvdata * drvdata,const char * name)795 static void stm_init_generic_data(struct stm_drvdata *drvdata,
796 const char *name)
797 {
798 drvdata->stm.name = name;
799
800 /*
801 * MasterIDs are assigned at HW design phase. As such the core is
802 * using a single master for interaction with this device.
803 */
804 drvdata->stm.sw_start = 1;
805 drvdata->stm.sw_end = 1;
806 drvdata->stm.hw_override = true;
807 drvdata->stm.sw_nchannels = drvdata->numsp;
808 drvdata->stm.sw_mmiosz = BYTES_PER_CHANNEL;
809 drvdata->stm.packet = stm_generic_packet;
810 drvdata->stm.mmio_addr = stm_mmio_addr;
811 drvdata->stm.link = stm_generic_link;
812 drvdata->stm.unlink = stm_generic_unlink;
813 drvdata->stm.set_options = stm_generic_set_options;
814 }
815
816 static const struct amba_id stm_ids[];
817
stm_csdev_name(struct coresight_device * csdev)818 static char *stm_csdev_name(struct coresight_device *csdev)
819 {
820 u32 stm_pid = coresight_get_pid(&csdev->access);
821 void *uci_data = coresight_get_uci_data_from_amba(stm_ids, stm_pid);
822
823 return uci_data ? (char *)uci_data : "STM";
824 }
825
__stm_probe(struct device * dev,struct resource * res)826 static int __stm_probe(struct device *dev, struct resource *res)
827 {
828 int ret, trace_id;
829 void __iomem *base;
830 struct coresight_platform_data *pdata = NULL;
831 struct stm_drvdata *drvdata;
832 struct resource ch_res;
833 struct coresight_desc desc = { 0 };
834
835 desc.name = coresight_alloc_device_name("stm", dev);
836 if (!desc.name)
837 return -ENOMEM;
838
839 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
840 if (!drvdata)
841 return -ENOMEM;
842
843 ret = coresight_get_enable_clocks(dev, &drvdata->pclk, &drvdata->atclk);
844 if (ret)
845 return ret;
846
847 dev_set_drvdata(dev, drvdata);
848
849 base = devm_ioremap_resource(dev, res);
850 if (IS_ERR(base))
851 return PTR_ERR(base);
852 drvdata->base = base;
853 desc.access = CSDEV_ACCESS_IOMEM(base);
854
855 ret = stm_get_stimulus_area(dev, &ch_res);
856 if (ret)
857 return ret;
858 drvdata->chs.phys = ch_res.start;
859
860 base = devm_ioremap_resource(dev, &ch_res);
861 if (IS_ERR(base))
862 return PTR_ERR(base);
863 drvdata->chs.base = base;
864
865 drvdata->write_bytes = stm_fundamental_data_size(drvdata);
866
867 if (boot_nr_channel)
868 drvdata->numsp = boot_nr_channel;
869 else
870 drvdata->numsp = stm_num_stimulus_port(drvdata);
871
872 drvdata->chs.guaranteed = devm_bitmap_zalloc(dev, drvdata->numsp,
873 GFP_KERNEL);
874 if (!drvdata->chs.guaranteed)
875 return -ENOMEM;
876
877 spin_lock_init(&drvdata->spinlock);
878
879 stm_init_default_data(drvdata);
880 stm_init_generic_data(drvdata, desc.name);
881
882 if (stm_register_device(dev, &drvdata->stm, THIS_MODULE)) {
883 dev_info(dev,
884 "%s : stm_register_device failed, probing deferred\n",
885 desc.name);
886 return -EPROBE_DEFER;
887 }
888
889 pdata = coresight_get_platform_data(dev);
890 if (IS_ERR(pdata)) {
891 ret = PTR_ERR(pdata);
892 goto stm_unregister;
893 }
894 dev->platform_data = pdata;
895
896 desc.type = CORESIGHT_DEV_TYPE_SOURCE;
897 desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE;
898 desc.ops = &stm_cs_ops;
899 desc.pdata = pdata;
900 desc.dev = dev;
901 desc.groups = coresight_stm_groups;
902 drvdata->csdev = coresight_register(&desc);
903 if (IS_ERR(drvdata->csdev)) {
904 ret = PTR_ERR(drvdata->csdev);
905 goto stm_unregister;
906 }
907
908 trace_id = coresight_trace_id_get_system_id();
909 if (trace_id < 0) {
910 ret = trace_id;
911 goto cs_unregister;
912 }
913 drvdata->traceid = (u8)trace_id;
914
915 dev_info(&drvdata->csdev->dev, "%s initialized\n",
916 stm_csdev_name(drvdata->csdev));
917 return 0;
918
919 cs_unregister:
920 coresight_unregister(drvdata->csdev);
921
922 stm_unregister:
923 stm_unregister_device(&drvdata->stm);
924 return ret;
925 }
926
stm_probe(struct amba_device * adev,const struct amba_id * id)927 static int stm_probe(struct amba_device *adev, const struct amba_id *id)
928 {
929 int ret;
930
931 ret = __stm_probe(&adev->dev, &adev->res);
932 if (!ret)
933 pm_runtime_put(&adev->dev);
934
935 return ret;
936 }
937
__stm_remove(struct device * dev)938 static void __stm_remove(struct device *dev)
939 {
940 struct stm_drvdata *drvdata = dev_get_drvdata(dev);
941
942 coresight_trace_id_put_system_id(drvdata->traceid);
943 coresight_unregister(drvdata->csdev);
944
945 stm_unregister_device(&drvdata->stm);
946 }
947
stm_remove(struct amba_device * adev)948 static void stm_remove(struct amba_device *adev)
949 {
950 __stm_remove(&adev->dev);
951 }
952
953 #ifdef CONFIG_PM
stm_runtime_suspend(struct device * dev)954 static int stm_runtime_suspend(struct device *dev)
955 {
956 struct stm_drvdata *drvdata = dev_get_drvdata(dev);
957
958 clk_disable_unprepare(drvdata->atclk);
959 clk_disable_unprepare(drvdata->pclk);
960
961 return 0;
962 }
963
stm_runtime_resume(struct device * dev)964 static int stm_runtime_resume(struct device *dev)
965 {
966 struct stm_drvdata *drvdata = dev_get_drvdata(dev);
967 int ret;
968
969 ret = clk_prepare_enable(drvdata->pclk);
970 if (ret)
971 return ret;
972
973 ret = clk_prepare_enable(drvdata->atclk);
974 if (ret)
975 clk_disable_unprepare(drvdata->pclk);
976
977 return ret;
978 }
979 #endif
980
981 static const struct dev_pm_ops stm_dev_pm_ops = {
982 SET_RUNTIME_PM_OPS(stm_runtime_suspend, stm_runtime_resume, NULL)
983 };
984
985 static const struct amba_id stm_ids[] = {
986 CS_AMBA_ID_DATA(0x000bb962, "STM32"),
987 CS_AMBA_ID_DATA(0x000bb963, "STM500"),
988 { 0, 0, NULL },
989 };
990
991 MODULE_DEVICE_TABLE(amba, stm_ids);
992
993 static struct amba_driver stm_driver = {
994 .drv = {
995 .name = "coresight-stm",
996 .pm = &stm_dev_pm_ops,
997 .suppress_bind_attrs = true,
998 },
999 .probe = stm_probe,
1000 .remove = stm_remove,
1001 .id_table = stm_ids,
1002 };
1003
stm_platform_probe(struct platform_device * pdev)1004 static int stm_platform_probe(struct platform_device *pdev)
1005 {
1006 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1007 int ret = 0;
1008
1009 pm_runtime_get_noresume(&pdev->dev);
1010 pm_runtime_set_active(&pdev->dev);
1011 pm_runtime_enable(&pdev->dev);
1012
1013 ret = __stm_probe(&pdev->dev, res);
1014 pm_runtime_put(&pdev->dev);
1015 if (ret)
1016 pm_runtime_disable(&pdev->dev);
1017
1018 return ret;
1019 }
1020
stm_platform_remove(struct platform_device * pdev)1021 static void stm_platform_remove(struct platform_device *pdev)
1022 {
1023 struct stm_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
1024
1025 if (WARN_ON(!drvdata))
1026 return;
1027
1028 __stm_remove(&pdev->dev);
1029 pm_runtime_disable(&pdev->dev);
1030 }
1031
1032 #ifdef CONFIG_ACPI
1033 static const struct acpi_device_id stm_acpi_ids[] = {
1034 {"ARMHC502", 0, 0, 0}, /* ARM CoreSight STM */
1035 {},
1036 };
1037 MODULE_DEVICE_TABLE(acpi, stm_acpi_ids);
1038 #endif
1039
1040 static struct platform_driver stm_platform_driver = {
1041 .probe = stm_platform_probe,
1042 .remove = stm_platform_remove,
1043 .driver = {
1044 .name = "coresight-stm-platform",
1045 .acpi_match_table = ACPI_PTR(stm_acpi_ids),
1046 .suppress_bind_attrs = true,
1047 .pm = &stm_dev_pm_ops,
1048 },
1049 };
1050
stm_init(void)1051 static int __init stm_init(void)
1052 {
1053 return coresight_init_driver("stm", &stm_driver, &stm_platform_driver, THIS_MODULE);
1054 }
1055
stm_exit(void)1056 static void __exit stm_exit(void)
1057 {
1058 coresight_remove_driver(&stm_driver, &stm_platform_driver);
1059 }
1060 module_init(stm_init);
1061 module_exit(stm_exit);
1062
1063 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
1064 MODULE_DESCRIPTION("Arm CoreSight System Trace Macrocell driver");
1065 MODULE_LICENSE("GPL v2");
1066