1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
4 * Description: CoreSight Trace Memory Controller driver
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/types.h>
10 #include <linux/device.h>
12 #include <linux/err.h>
14 #include <linux/miscdevice.h>
15 #include <linux/property.h>
16 #include <linux/uaccess.h>
17 #include <linux/slab.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/spinlock.h>
20 #include <linux/pm_runtime.h>
22 #include <linux/coresight.h>
23 #include <linux/amba/bus.h>
25 #include "coresight-priv.h"
26 #include "coresight-tmc.h"
28 void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
30 /* Ensure formatter, unformatter and hardware fifo are empty */
31 if (coresight_timeout(drvdata->base,
32 TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
34 "timeout while waiting for TMC to be Ready\n");
38 void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
42 ffcr = readl_relaxed(drvdata->base + TMC_FFCR);
43 ffcr |= TMC_FFCR_STOP_ON_FLUSH;
44 writel_relaxed(ffcr, drvdata->base + TMC_FFCR);
45 ffcr |= BIT(TMC_FFCR_FLUSHMAN_BIT);
46 writel_relaxed(ffcr, drvdata->base + TMC_FFCR);
47 /* Ensure flush completes */
48 if (coresight_timeout(drvdata->base,
49 TMC_FFCR, TMC_FFCR_FLUSHMAN_BIT, 0)) {
51 "timeout while waiting for completion of Manual Flush\n");
54 tmc_wait_for_tmcready(drvdata);
57 void tmc_enable_hw(struct tmc_drvdata *drvdata)
59 writel_relaxed(TMC_CTL_CAPT_EN, drvdata->base + TMC_CTL);
62 void tmc_disable_hw(struct tmc_drvdata *drvdata)
64 writel_relaxed(0x0, drvdata->base + TMC_CTL);
67 static int tmc_read_prepare(struct tmc_drvdata *drvdata)
71 switch (drvdata->config_type) {
72 case TMC_CONFIG_TYPE_ETB:
73 case TMC_CONFIG_TYPE_ETF:
74 ret = tmc_read_prepare_etb(drvdata);
76 case TMC_CONFIG_TYPE_ETR:
77 ret = tmc_read_prepare_etr(drvdata);
84 dev_dbg(drvdata->dev, "TMC read start\n");
89 static int tmc_read_unprepare(struct tmc_drvdata *drvdata)
93 switch (drvdata->config_type) {
94 case TMC_CONFIG_TYPE_ETB:
95 case TMC_CONFIG_TYPE_ETF:
96 ret = tmc_read_unprepare_etb(drvdata);
98 case TMC_CONFIG_TYPE_ETR:
99 ret = tmc_read_unprepare_etr(drvdata);
106 dev_dbg(drvdata->dev, "TMC read end\n");
111 static int tmc_open(struct inode *inode, struct file *file)
114 struct tmc_drvdata *drvdata = container_of(file->private_data,
115 struct tmc_drvdata, miscdev);
117 ret = tmc_read_prepare(drvdata);
121 nonseekable_open(inode, file);
123 dev_dbg(drvdata->dev, "%s: successfully opened\n", __func__);
127 static inline ssize_t tmc_get_sysfs_trace(struct tmc_drvdata *drvdata,
128 loff_t pos, size_t len, char **bufpp)
130 switch (drvdata->config_type) {
131 case TMC_CONFIG_TYPE_ETB:
132 case TMC_CONFIG_TYPE_ETF:
133 return tmc_etb_get_sysfs_trace(drvdata, pos, len, bufpp);
134 case TMC_CONFIG_TYPE_ETR:
135 return tmc_etr_get_sysfs_trace(drvdata, pos, len, bufpp);
141 static ssize_t tmc_read(struct file *file, char __user *data, size_t len,
146 struct tmc_drvdata *drvdata = container_of(file->private_data,
147 struct tmc_drvdata, miscdev);
148 actual = tmc_get_sysfs_trace(drvdata, *ppos, len, &bufp);
152 if (copy_to_user(data, bufp, actual)) {
153 dev_dbg(drvdata->dev, "%s: copy_to_user failed\n", __func__);
158 dev_dbg(drvdata->dev, "%zu bytes copied\n", actual);
163 static int tmc_release(struct inode *inode, struct file *file)
166 struct tmc_drvdata *drvdata = container_of(file->private_data,
167 struct tmc_drvdata, miscdev);
169 ret = tmc_read_unprepare(drvdata);
173 dev_dbg(drvdata->dev, "%s: released\n", __func__);
177 static const struct file_operations tmc_fops = {
178 .owner = THIS_MODULE,
181 .release = tmc_release,
185 static enum tmc_mem_intf_width tmc_get_memwidth(u32 devid)
187 enum tmc_mem_intf_width memwidth;
190 * Excerpt from the TRM:
192 * DEVID::MEMWIDTH[10:8]
193 * 0x2 Memory interface databus is 32 bits wide.
194 * 0x3 Memory interface databus is 64 bits wide.
195 * 0x4 Memory interface databus is 128 bits wide.
196 * 0x5 Memory interface databus is 256 bits wide.
198 switch (BMVAL(devid, 8, 10)) {
200 memwidth = TMC_MEM_INTF_WIDTH_32BITS;
203 memwidth = TMC_MEM_INTF_WIDTH_64BITS;
206 memwidth = TMC_MEM_INTF_WIDTH_128BITS;
209 memwidth = TMC_MEM_INTF_WIDTH_256BITS;
218 #define coresight_tmc_reg(name, offset) \
219 coresight_simple_reg32(struct tmc_drvdata, name, offset)
220 #define coresight_tmc_reg64(name, lo_off, hi_off) \
221 coresight_simple_reg64(struct tmc_drvdata, name, lo_off, hi_off)
223 coresight_tmc_reg(rsz, TMC_RSZ);
224 coresight_tmc_reg(sts, TMC_STS);
225 coresight_tmc_reg(trg, TMC_TRG);
226 coresight_tmc_reg(ctl, TMC_CTL);
227 coresight_tmc_reg(ffsr, TMC_FFSR);
228 coresight_tmc_reg(ffcr, TMC_FFCR);
229 coresight_tmc_reg(mode, TMC_MODE);
230 coresight_tmc_reg(pscr, TMC_PSCR);
231 coresight_tmc_reg(axictl, TMC_AXICTL);
232 coresight_tmc_reg(devid, CORESIGHT_DEVID);
233 coresight_tmc_reg64(rrp, TMC_RRP, TMC_RRPHI);
234 coresight_tmc_reg64(rwp, TMC_RWP, TMC_RWPHI);
235 coresight_tmc_reg64(dba, TMC_DBALO, TMC_DBAHI);
237 static struct attribute *coresight_tmc_mgmt_attrs[] = {
248 &dev_attr_devid.attr,
250 &dev_attr_axictl.attr,
254 static ssize_t trigger_cntr_show(struct device *dev,
255 struct device_attribute *attr, char *buf)
257 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
258 unsigned long val = drvdata->trigger_cntr;
260 return sprintf(buf, "%#lx\n", val);
263 static ssize_t trigger_cntr_store(struct device *dev,
264 struct device_attribute *attr,
265 const char *buf, size_t size)
269 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
271 ret = kstrtoul(buf, 16, &val);
275 drvdata->trigger_cntr = val;
278 static DEVICE_ATTR_RW(trigger_cntr);
280 static ssize_t buffer_size_show(struct device *dev,
281 struct device_attribute *attr, char *buf)
283 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
285 return sprintf(buf, "%#x\n", drvdata->size);
288 static ssize_t buffer_size_store(struct device *dev,
289 struct device_attribute *attr,
290 const char *buf, size_t size)
294 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
296 /* Only permitted for TMC-ETRs */
297 if (drvdata->config_type != TMC_CONFIG_TYPE_ETR)
300 ret = kstrtoul(buf, 0, &val);
303 /* The buffer size should be page aligned */
304 if (val & (PAGE_SIZE - 1))
310 static DEVICE_ATTR_RW(buffer_size);
312 static struct attribute *coresight_tmc_attrs[] = {
313 &dev_attr_trigger_cntr.attr,
314 &dev_attr_buffer_size.attr,
318 static const struct attribute_group coresight_tmc_group = {
319 .attrs = coresight_tmc_attrs,
322 static const struct attribute_group coresight_tmc_mgmt_group = {
323 .attrs = coresight_tmc_mgmt_attrs,
327 const struct attribute_group *coresight_tmc_groups[] = {
328 &coresight_tmc_group,
329 &coresight_tmc_mgmt_group,
333 static inline bool tmc_etr_can_use_sg(struct tmc_drvdata *drvdata)
335 return fwnode_property_present(drvdata->dev->fwnode,
336 "arm,scatter-gather");
339 /* Detect and initialise the capabilities of a TMC ETR */
340 static int tmc_etr_setup_caps(struct tmc_drvdata *drvdata,
341 u32 devid, void *dev_caps)
347 /* Set the unadvertised capabilities */
348 tmc_etr_init_caps(drvdata, (u32)(unsigned long)dev_caps);
350 if (!(devid & TMC_DEVID_NOSCAT) && tmc_etr_can_use_sg(drvdata))
351 tmc_etr_set_cap(drvdata, TMC_ETR_SG);
353 /* Check if the AXI address width is available */
354 if (devid & TMC_DEVID_AXIAW_VALID)
355 dma_mask = ((devid >> TMC_DEVID_AXIAW_SHIFT) &
356 TMC_DEVID_AXIAW_MASK);
359 * Unless specified in the device configuration, ETR uses a 40-bit
360 * AXI master in place of the embedded SRAM of ETB/ETF.
368 dev_info(drvdata->dev, "Detected dma mask %dbits\n", dma_mask);
374 rc = dma_set_mask_and_coherent(drvdata->dev, DMA_BIT_MASK(dma_mask));
376 dev_err(drvdata->dev, "Failed to setup DMA mask: %d\n", rc);
380 static int tmc_probe(struct amba_device *adev, const struct amba_id *id)
385 struct device *dev = &adev->dev;
386 struct coresight_platform_data *pdata = NULL;
387 struct tmc_drvdata *drvdata;
388 struct resource *res = &adev->res;
389 struct coresight_desc desc = { 0 };
390 struct device_node *np = adev->dev.of_node;
393 pdata = of_get_coresight_platform_data(dev, np);
395 ret = PTR_ERR(pdata);
398 adev->dev.platform_data = pdata;
402 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
406 drvdata->dev = &adev->dev;
407 dev_set_drvdata(dev, drvdata);
409 /* Validity for the resource is already checked by the AMBA core */
410 base = devm_ioremap_resource(dev, res);
416 drvdata->base = base;
418 spin_lock_init(&drvdata->spinlock);
420 devid = readl_relaxed(drvdata->base + CORESIGHT_DEVID);
421 drvdata->config_type = BMVAL(devid, 6, 7);
422 drvdata->memwidth = tmc_get_memwidth(devid);
424 if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
426 ret = of_property_read_u32(np,
430 drvdata->size = SZ_1M;
432 drvdata->size = readl_relaxed(drvdata->base + TMC_RSZ) * 4;
435 pm_runtime_put(&adev->dev);
439 desc.groups = coresight_tmc_groups;
441 switch (drvdata->config_type) {
442 case TMC_CONFIG_TYPE_ETB:
443 desc.type = CORESIGHT_DEV_TYPE_SINK;
444 desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
445 desc.ops = &tmc_etb_cs_ops;
447 case TMC_CONFIG_TYPE_ETR:
448 desc.type = CORESIGHT_DEV_TYPE_SINK;
449 desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
450 desc.ops = &tmc_etr_cs_ops;
451 ret = tmc_etr_setup_caps(drvdata, devid,
452 coresight_get_uci_data(id));
456 case TMC_CONFIG_TYPE_ETF:
457 desc.type = CORESIGHT_DEV_TYPE_LINKSINK;
458 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_FIFO;
459 desc.ops = &tmc_etf_cs_ops;
462 pr_err("%s: Unsupported TMC config\n", pdata->name);
467 drvdata->csdev = coresight_register(&desc);
468 if (IS_ERR(drvdata->csdev)) {
469 ret = PTR_ERR(drvdata->csdev);
473 drvdata->miscdev.name = pdata->name;
474 drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
475 drvdata->miscdev.fops = &tmc_fops;
476 ret = misc_register(&drvdata->miscdev);
478 coresight_unregister(drvdata->csdev);
483 static const struct amba_id tmc_ids[] = {
484 CS_AMBA_ID(0x000bb961),
485 /* Coresight SoC 600 TMC-ETR/ETS */
486 CS_AMBA_ID_DATA(0x000bb9e8, (unsigned long)CORESIGHT_SOC_600_ETR_CAPS),
487 /* Coresight SoC 600 TMC-ETB */
488 CS_AMBA_ID(0x000bb9e9),
489 /* Coresight SoC 600 TMC-ETF */
490 CS_AMBA_ID(0x000bb9ea),
494 static struct amba_driver tmc_driver = {
496 .name = "coresight-tmc",
497 .owner = THIS_MODULE,
498 .suppress_bind_attrs = true,
503 builtin_amba_driver(tmc_driver);