xref: /linux/drivers/hwtracing/coresight/coresight-tpda.c (revision 69bfec7548f4c1595bac0e3ddfc0458a5af31f4c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
4  */
5 
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>
11 #include <linux/fs.h>
12 #include <linux/io.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 
18 #include "coresight-priv.h"
19 #include "coresight-tpda.h"
20 #include "coresight-trace-id.h"
21 
22 DEFINE_CORESIGHT_DEVLIST(tpda_devs, "tpda");
23 
24 /* Settings pre enabling port control register */
25 static void tpda_enable_pre_port(struct tpda_drvdata *drvdata)
26 {
27 	u32 val;
28 
29 	val = readl_relaxed(drvdata->base + TPDA_CR);
30 	val &= ~TPDA_CR_ATID;
31 	val |= FIELD_PREP(TPDA_CR_ATID, drvdata->atid);
32 	writel_relaxed(val, drvdata->base + TPDA_CR);
33 }
34 
35 static void tpda_enable_port(struct tpda_drvdata *drvdata, int port)
36 {
37 	u32 val;
38 
39 	val = readl_relaxed(drvdata->base + TPDA_Pn_CR(port));
40 	/* Enable the port */
41 	val |= TPDA_Pn_CR_ENA;
42 	writel_relaxed(val, drvdata->base + TPDA_Pn_CR(port));
43 }
44 
45 static void __tpda_enable(struct tpda_drvdata *drvdata, int port)
46 {
47 	CS_UNLOCK(drvdata->base);
48 
49 	if (!drvdata->csdev->enable)
50 		tpda_enable_pre_port(drvdata);
51 
52 	tpda_enable_port(drvdata, port);
53 
54 	CS_LOCK(drvdata->base);
55 }
56 
57 static int tpda_enable(struct coresight_device *csdev, int inport, int outport)
58 {
59 	struct tpda_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
60 
61 	spin_lock(&drvdata->spinlock);
62 	if (atomic_read(&csdev->refcnt[inport]) == 0)
63 		__tpda_enable(drvdata, inport);
64 
65 	atomic_inc(&csdev->refcnt[inport]);
66 	spin_unlock(&drvdata->spinlock);
67 
68 	dev_dbg(drvdata->dev, "TPDA inport %d enabled.\n", inport);
69 	return 0;
70 }
71 
72 static void __tpda_disable(struct tpda_drvdata *drvdata, int port)
73 {
74 	u32 val;
75 
76 	CS_UNLOCK(drvdata->base);
77 
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));
81 
82 	CS_LOCK(drvdata->base);
83 }
84 
85 static void tpda_disable(struct coresight_device *csdev, int inport,
86 			   int outport)
87 {
88 	struct tpda_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
89 
90 	spin_lock(&drvdata->spinlock);
91 	if (atomic_dec_return(&csdev->refcnt[inport]) == 0)
92 		__tpda_disable(drvdata, inport);
93 
94 	spin_unlock(&drvdata->spinlock);
95 
96 	dev_dbg(drvdata->dev, "TPDA inport %d disabled\n", inport);
97 }
98 
99 static const struct coresight_ops_link tpda_link_ops = {
100 	.enable		= tpda_enable,
101 	.disable	= tpda_disable,
102 };
103 
104 static const struct coresight_ops tpda_cs_ops = {
105 	.link_ops	= &tpda_link_ops,
106 };
107 
108 static int tpda_init_default_data(struct tpda_drvdata *drvdata)
109 {
110 	int atid;
111 	/*
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.
117 	 */
118 	atid = coresight_trace_id_get_system_id();
119 	if (atid < 0)
120 		return atid;
121 
122 	drvdata->atid = atid;
123 	return 0;
124 }
125 
126 static int tpda_probe(struct amba_device *adev, const struct amba_id *id)
127 {
128 	int ret;
129 	struct device *dev = &adev->dev;
130 	struct coresight_platform_data *pdata;
131 	struct tpda_drvdata *drvdata;
132 	struct coresight_desc desc = { 0 };
133 	void __iomem *base;
134 
135 	pdata = coresight_get_platform_data(dev);
136 	if (IS_ERR(pdata))
137 		return PTR_ERR(pdata);
138 	adev->dev.platform_data = pdata;
139 
140 	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
141 	if (!drvdata)
142 		return -ENOMEM;
143 
144 	drvdata->dev = &adev->dev;
145 	dev_set_drvdata(dev, drvdata);
146 
147 	base = devm_ioremap_resource(dev, &adev->res);
148 	if (IS_ERR(base))
149 		return PTR_ERR(base);
150 	drvdata->base = base;
151 
152 	spin_lock_init(&drvdata->spinlock);
153 
154 	ret = tpda_init_default_data(drvdata);
155 	if (ret)
156 		return ret;
157 
158 	desc.name = coresight_alloc_device_name(&tpda_devs, dev);
159 	if (!desc.name)
160 		return -ENOMEM;
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);
170 
171 	pm_runtime_put(&adev->dev);
172 
173 	dev_dbg(drvdata->dev, "TPDA initialized\n");
174 	return 0;
175 }
176 
177 static void tpda_remove(struct amba_device *adev)
178 {
179 	struct tpda_drvdata *drvdata = dev_get_drvdata(&adev->dev);
180 
181 	coresight_trace_id_put_system_id(drvdata->atid);
182 	coresight_unregister(drvdata->csdev);
183 }
184 
185 /*
186  * Different TPDA has different periph id.
187  * The difference is 0-7 bits' value. So ignore 0-7 bits.
188  */
189 static struct amba_id tpda_ids[] = {
190 	{
191 		.id     = 0x000f0f00,
192 		.mask   = 0x000fff00,
193 	},
194 	{ 0, 0},
195 };
196 
197 static struct amba_driver tpda_driver = {
198 	.drv = {
199 		.name   = "coresight-tpda",
200 		.owner	= THIS_MODULE,
201 		.suppress_bind_attrs = true,
202 	},
203 	.probe          = tpda_probe,
204 	.remove		= tpda_remove,
205 	.id_table	= tpda_ids,
206 };
207 
208 module_amba_driver(tpda_driver);
209 
210 MODULE_LICENSE("GPL");
211 MODULE_DESCRIPTION("Trace, Profiling & Diagnostic Aggregator driver");
212