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 static bool coresight_device_is_tpdm(struct coresight_device *csdev)
26 return (csdev->type == CORESIGHT_DEV_TYPE_SOURCE) &&
27 (csdev->subtype.source_subtype ==
28 CORESIGHT_DEV_SUBTYPE_SOURCE_TPDM);
32 * Read the DSB element size from the TPDM device
34 * The dsb element size read from the devicetree if available.
35 * 0 - Otherwise, with a warning once.
37 static int tpdm_read_dsb_element_size(struct coresight_device *csdev)
42 rc = fwnode_property_read_u8(dev_fwnode(csdev->dev.parent),
43 "qcom,dsb-element-size", &size);
45 dev_warn_once(&csdev->dev,
46 "Failed to read TPDM DSB Element size: %d\n", rc);
52 * Search and read element data size from the TPDM node in
53 * the devicetree. Each input port of TPDA is connected to
54 * a TPDM. Different TPDM supports different types of dataset,
55 * and some may support more than one type of dataset.
56 * Parameter "inport" is used to pass in the input port number
57 * of TPDA, and it is set to -1 in the recursize call.
59 static int tpda_get_element_size(struct coresight_device *csdev,
62 int dsb_size = -ENOENT;
64 struct coresight_device *in;
66 for (i = 0; i < csdev->pdata->nr_inconns; i++) {
67 in = csdev->pdata->in_conns[i]->src_dev;
71 /* Ignore the paths that do not match port */
73 csdev->pdata->in_conns[i]->dest_port != inport)
76 if (coresight_device_is_tpdm(in)) {
77 size = tpdm_read_dsb_element_size(in);
79 /* Recurse down the path */
80 size = tpda_get_element_size(in, -1);
87 /* Found a size, save it. */
90 /* Found duplicate TPDMs */
98 /* Settings pre enabling port control register */
99 static void tpda_enable_pre_port(struct tpda_drvdata *drvdata)
103 val = readl_relaxed(drvdata->base + TPDA_CR);
104 val &= ~TPDA_CR_ATID;
105 val |= FIELD_PREP(TPDA_CR_ATID, drvdata->atid);
106 writel_relaxed(val, drvdata->base + TPDA_CR);
109 static int tpda_enable_port(struct tpda_drvdata *drvdata, int port)
114 val = readl_relaxed(drvdata->base + TPDA_Pn_CR(port));
116 * Configure aggregator port n DSB data set element size
117 * Set the bit to 0 if the size is 32
118 * Set the bit to 1 if the size is 64
120 size = tpda_get_element_size(drvdata->csdev, port);
123 val &= ~TPDA_Pn_CR_DSBSIZE;
126 val |= TPDA_Pn_CR_DSBSIZE;
131 dev_warn_once(&drvdata->csdev->dev,
132 "Detected multiple TPDMs on port %d", -EEXIST);
138 /* Enable the port */
139 val |= TPDA_Pn_CR_ENA;
140 writel_relaxed(val, drvdata->base + TPDA_Pn_CR(port));
145 static int __tpda_enable(struct tpda_drvdata *drvdata, int port)
149 CS_UNLOCK(drvdata->base);
151 if (!drvdata->csdev->enable)
152 tpda_enable_pre_port(drvdata);
154 ret = tpda_enable_port(drvdata, port);
155 CS_LOCK(drvdata->base);
160 static int tpda_enable(struct coresight_device *csdev,
161 struct coresight_connection *in,
162 struct coresight_connection *out)
164 struct tpda_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
167 spin_lock(&drvdata->spinlock);
168 if (atomic_read(&in->dest_refcnt) == 0) {
169 ret = __tpda_enable(drvdata, in->dest_port);
171 atomic_inc(&in->dest_refcnt);
172 dev_dbg(drvdata->dev, "TPDA inport %d enabled.\n", in->dest_port);
176 spin_unlock(&drvdata->spinlock);
180 static void __tpda_disable(struct tpda_drvdata *drvdata, int port)
184 CS_UNLOCK(drvdata->base);
186 val = readl_relaxed(drvdata->base + TPDA_Pn_CR(port));
187 val &= ~TPDA_Pn_CR_ENA;
188 writel_relaxed(val, drvdata->base + TPDA_Pn_CR(port));
190 CS_LOCK(drvdata->base);
193 static void tpda_disable(struct coresight_device *csdev,
194 struct coresight_connection *in,
195 struct coresight_connection *out)
197 struct tpda_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
199 spin_lock(&drvdata->spinlock);
200 if (atomic_dec_return(&in->dest_refcnt) == 0)
201 __tpda_disable(drvdata, in->dest_port);
203 spin_unlock(&drvdata->spinlock);
205 dev_dbg(drvdata->dev, "TPDA inport %d disabled\n", in->dest_port);
208 static const struct coresight_ops_link tpda_link_ops = {
209 .enable = tpda_enable,
210 .disable = tpda_disable,
213 static const struct coresight_ops tpda_cs_ops = {
214 .link_ops = &tpda_link_ops,
217 static int tpda_init_default_data(struct tpda_drvdata *drvdata)
221 * TPDA must has a unique atid. This atid can uniquely
222 * identify the TPDM trace source connected to the TPDA.
223 * The TPDMs which are connected to same TPDA share the
224 * same trace-id. When TPDA does packetization, different
225 * port will have unique channel number for decoding.
227 atid = coresight_trace_id_get_system_id();
231 drvdata->atid = atid;
235 static int tpda_probe(struct amba_device *adev, const struct amba_id *id)
238 struct device *dev = &adev->dev;
239 struct coresight_platform_data *pdata;
240 struct tpda_drvdata *drvdata;
241 struct coresight_desc desc = { 0 };
244 pdata = coresight_get_platform_data(dev);
246 return PTR_ERR(pdata);
247 adev->dev.platform_data = pdata;
249 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
253 drvdata->dev = &adev->dev;
254 dev_set_drvdata(dev, drvdata);
256 base = devm_ioremap_resource(dev, &adev->res);
258 return PTR_ERR(base);
259 drvdata->base = base;
261 spin_lock_init(&drvdata->spinlock);
263 ret = tpda_init_default_data(drvdata);
267 desc.name = coresight_alloc_device_name(&tpda_devs, dev);
270 desc.type = CORESIGHT_DEV_TYPE_LINK;
271 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
272 desc.ops = &tpda_cs_ops;
273 desc.pdata = adev->dev.platform_data;
274 desc.dev = &adev->dev;
275 desc.access = CSDEV_ACCESS_IOMEM(base);
276 drvdata->csdev = coresight_register(&desc);
277 if (IS_ERR(drvdata->csdev))
278 return PTR_ERR(drvdata->csdev);
280 pm_runtime_put(&adev->dev);
282 dev_dbg(drvdata->dev, "TPDA initialized\n");
286 static void tpda_remove(struct amba_device *adev)
288 struct tpda_drvdata *drvdata = dev_get_drvdata(&adev->dev);
290 coresight_trace_id_put_system_id(drvdata->atid);
291 coresight_unregister(drvdata->csdev);
295 * Different TPDA has different periph id.
296 * The difference is 0-7 bits' value. So ignore 0-7 bits.
298 static struct amba_id tpda_ids[] = {
306 static struct amba_driver tpda_driver = {
308 .name = "coresight-tpda",
309 .owner = THIS_MODULE,
310 .suppress_bind_attrs = true,
313 .remove = tpda_remove,
314 .id_table = tpda_ids,
317 module_amba_driver(tpda_driver);
319 MODULE_LICENSE("GPL");
320 MODULE_DESCRIPTION("Trace, Profiling & Diagnostic Aggregator driver");