1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
6 #include <linux/amba/bus.h>
7 #include <linux/bitfield.h>
8 #include <linux/coresight.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
18 #include "coresight-priv.h"
19 #include "coresight-tpda.h"
20 #include "coresight-trace-id.h"
22 DEFINE_CORESIGHT_DEVLIST(tpda_devs, "tpda");
24 /* Settings pre enabling port control register */
25 static void tpda_enable_pre_port(struct tpda_drvdata *drvdata)
29 val = readl_relaxed(drvdata->base + TPDA_CR);
31 val |= FIELD_PREP(TPDA_CR_ATID, drvdata->atid);
32 writel_relaxed(val, drvdata->base + TPDA_CR);
35 static void tpda_enable_port(struct tpda_drvdata *drvdata, int port)
39 val = readl_relaxed(drvdata->base + TPDA_Pn_CR(port));
41 val |= TPDA_Pn_CR_ENA;
42 writel_relaxed(val, drvdata->base + TPDA_Pn_CR(port));
45 static void __tpda_enable(struct tpda_drvdata *drvdata, int port)
47 CS_UNLOCK(drvdata->base);
49 if (!drvdata->csdev->enable)
50 tpda_enable_pre_port(drvdata);
52 tpda_enable_port(drvdata, port);
54 CS_LOCK(drvdata->base);
57 static int tpda_enable(struct coresight_device *csdev,
58 struct coresight_connection *in,
59 struct coresight_connection *out)
61 struct tpda_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
63 spin_lock(&drvdata->spinlock);
64 if (atomic_read(&in->dest_refcnt) == 0)
65 __tpda_enable(drvdata, in->dest_port);
67 atomic_inc(&in->dest_refcnt);
68 spin_unlock(&drvdata->spinlock);
70 dev_dbg(drvdata->dev, "TPDA inport %d enabled.\n", in->dest_port);
74 static void __tpda_disable(struct tpda_drvdata *drvdata, int port)
78 CS_UNLOCK(drvdata->base);
80 val = readl_relaxed(drvdata->base + TPDA_Pn_CR(port));
81 val &= ~TPDA_Pn_CR_ENA;
82 writel_relaxed(val, drvdata->base + TPDA_Pn_CR(port));
84 CS_LOCK(drvdata->base);
87 static void tpda_disable(struct coresight_device *csdev,
88 struct coresight_connection *in,
89 struct coresight_connection *out)
91 struct tpda_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
93 spin_lock(&drvdata->spinlock);
94 if (atomic_dec_return(&in->dest_refcnt) == 0)
95 __tpda_disable(drvdata, in->dest_port);
97 spin_unlock(&drvdata->spinlock);
99 dev_dbg(drvdata->dev, "TPDA inport %d disabled\n", in->dest_port);
102 static const struct coresight_ops_link tpda_link_ops = {
103 .enable = tpda_enable,
104 .disable = tpda_disable,
107 static const struct coresight_ops tpda_cs_ops = {
108 .link_ops = &tpda_link_ops,
111 static int tpda_init_default_data(struct tpda_drvdata *drvdata)
115 * TPDA must has a unique atid. This atid can uniquely
116 * identify the TPDM trace source connected to the TPDA.
117 * The TPDMs which are connected to same TPDA share the
118 * same trace-id. When TPDA does packetization, different
119 * port will have unique channel number for decoding.
121 atid = coresight_trace_id_get_system_id();
125 drvdata->atid = atid;
129 static int tpda_probe(struct amba_device *adev, const struct amba_id *id)
132 struct device *dev = &adev->dev;
133 struct coresight_platform_data *pdata;
134 struct tpda_drvdata *drvdata;
135 struct coresight_desc desc = { 0 };
138 pdata = coresight_get_platform_data(dev);
140 return PTR_ERR(pdata);
141 adev->dev.platform_data = pdata;
143 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
147 drvdata->dev = &adev->dev;
148 dev_set_drvdata(dev, drvdata);
150 base = devm_ioremap_resource(dev, &adev->res);
152 return PTR_ERR(base);
153 drvdata->base = base;
155 spin_lock_init(&drvdata->spinlock);
157 ret = tpda_init_default_data(drvdata);
161 desc.name = coresight_alloc_device_name(&tpda_devs, dev);
164 desc.type = CORESIGHT_DEV_TYPE_LINK;
165 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
166 desc.ops = &tpda_cs_ops;
167 desc.pdata = adev->dev.platform_data;
168 desc.dev = &adev->dev;
169 desc.access = CSDEV_ACCESS_IOMEM(base);
170 drvdata->csdev = coresight_register(&desc);
171 if (IS_ERR(drvdata->csdev))
172 return PTR_ERR(drvdata->csdev);
174 pm_runtime_put(&adev->dev);
176 dev_dbg(drvdata->dev, "TPDA initialized\n");
180 static void tpda_remove(struct amba_device *adev)
182 struct tpda_drvdata *drvdata = dev_get_drvdata(&adev->dev);
184 coresight_trace_id_put_system_id(drvdata->atid);
185 coresight_unregister(drvdata->csdev);
189 * Different TPDA has different periph id.
190 * The difference is 0-7 bits' value. So ignore 0-7 bits.
192 static struct amba_id tpda_ids[] = {
200 static struct amba_driver tpda_driver = {
202 .name = "coresight-tpda",
203 .owner = THIS_MODULE,
204 .suppress_bind_attrs = true,
207 .remove = tpda_remove,
208 .id_table = tpda_ids,
211 module_amba_driver(tpda_driver);
213 MODULE_LICENSE("GPL");
214 MODULE_DESCRIPTION("Trace, Profiling & Diagnostic Aggregator driver");