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, int inport, int outport)
59 struct tpda_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
61 spin_lock(&drvdata->spinlock);
62 if (atomic_read(&csdev->refcnt[inport]) == 0)
63 __tpda_enable(drvdata, inport);
65 atomic_inc(&csdev->refcnt[inport]);
66 spin_unlock(&drvdata->spinlock);
68 dev_dbg(drvdata->dev, "TPDA inport %d enabled.\n", inport);
72 static void __tpda_disable(struct tpda_drvdata *drvdata, int port)
76 CS_UNLOCK(drvdata->base);
78 val = readl_relaxed(drvdata->base + TPDA_Pn_CR(port));
79 val &= ~TPDA_Pn_CR_ENA;
80 writel_relaxed(val, drvdata->base + TPDA_Pn_CR(port));
82 CS_LOCK(drvdata->base);
85 static void tpda_disable(struct coresight_device *csdev, int inport,
88 struct tpda_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
90 spin_lock(&drvdata->spinlock);
91 if (atomic_dec_return(&csdev->refcnt[inport]) == 0)
92 __tpda_disable(drvdata, inport);
94 spin_unlock(&drvdata->spinlock);
96 dev_dbg(drvdata->dev, "TPDA inport %d disabled\n", inport);
99 static const struct coresight_ops_link tpda_link_ops = {
100 .enable = tpda_enable,
101 .disable = tpda_disable,
104 static const struct coresight_ops tpda_cs_ops = {
105 .link_ops = &tpda_link_ops,
108 static int tpda_init_default_data(struct tpda_drvdata *drvdata)
112 * TPDA must has a unique atid. This atid can uniquely
113 * identify the TPDM trace source connected to the TPDA.
114 * The TPDMs which are connected to same TPDA share the
115 * same trace-id. When TPDA does packetization, different
116 * port will have unique channel number for decoding.
118 atid = coresight_trace_id_get_system_id();
122 drvdata->atid = atid;
126 static int tpda_probe(struct amba_device *adev, const struct amba_id *id)
129 struct device *dev = &adev->dev;
130 struct coresight_platform_data *pdata;
131 struct tpda_drvdata *drvdata;
132 struct coresight_desc desc = { 0 };
135 pdata = coresight_get_platform_data(dev);
137 return PTR_ERR(pdata);
138 adev->dev.platform_data = pdata;
140 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
144 drvdata->dev = &adev->dev;
145 dev_set_drvdata(dev, drvdata);
147 base = devm_ioremap_resource(dev, &adev->res);
149 return PTR_ERR(base);
150 drvdata->base = base;
152 spin_lock_init(&drvdata->spinlock);
154 ret = tpda_init_default_data(drvdata);
158 desc.name = coresight_alloc_device_name(&tpda_devs, dev);
161 desc.type = CORESIGHT_DEV_TYPE_LINK;
162 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
163 desc.ops = &tpda_cs_ops;
164 desc.pdata = adev->dev.platform_data;
165 desc.dev = &adev->dev;
166 desc.access = CSDEV_ACCESS_IOMEM(base);
167 drvdata->csdev = coresight_register(&desc);
168 if (IS_ERR(drvdata->csdev))
169 return PTR_ERR(drvdata->csdev);
171 pm_runtime_put(&adev->dev);
173 dev_dbg(drvdata->dev, "TPDA initialized\n");
177 static void tpda_remove(struct amba_device *adev)
179 struct tpda_drvdata *drvdata = dev_get_drvdata(&adev->dev);
181 coresight_trace_id_put_system_id(drvdata->atid);
182 coresight_unregister(drvdata->csdev);
186 * Different TPDA has different periph id.
187 * The difference is 0-7 bits' value. So ignore 0-7 bits.
189 static struct amba_id tpda_ids[] = {
197 static struct amba_driver tpda_driver = {
199 .name = "coresight-tpda",
200 .owner = THIS_MODULE,
201 .suppress_bind_attrs = true,
204 .remove = tpda_remove,
205 .id_table = tpda_ids,
208 module_amba_driver(tpda_driver);
210 MODULE_LICENSE("GPL");
211 MODULE_DESCRIPTION("Trace, Profiling & Diagnostic Aggregator driver");