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 */
tpda_enable_pre_port(struct tpda_drvdata * drvdata)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 
tpda_enable_port(struct tpda_drvdata * drvdata,int port)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 
__tpda_enable(struct tpda_drvdata * drvdata,int port)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 
tpda_enable(struct coresight_device * csdev,struct coresight_connection * in,struct coresight_connection * out)57 static int tpda_enable(struct coresight_device *csdev,
58 		       struct coresight_connection *in,
59 		       struct coresight_connection *out)
60 {
61 	struct tpda_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
62 
63 	spin_lock(&drvdata->spinlock);
64 	if (atomic_read(&in->dest_refcnt) == 0)
65 		__tpda_enable(drvdata, in->dest_port);
66 
67 	atomic_inc(&in->dest_refcnt);
68 	spin_unlock(&drvdata->spinlock);
69 
70 	dev_dbg(drvdata->dev, "TPDA inport %d enabled.\n", in->dest_port);
71 	return 0;
72 }
73 
__tpda_disable(struct tpda_drvdata * drvdata,int port)74 static void __tpda_disable(struct tpda_drvdata *drvdata, int port)
75 {
76 	u32 val;
77 
78 	CS_UNLOCK(drvdata->base);
79 
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));
83 
84 	CS_LOCK(drvdata->base);
85 }
86 
tpda_disable(struct coresight_device * csdev,struct coresight_connection * in,struct coresight_connection * out)87 static void tpda_disable(struct coresight_device *csdev,
88 			 struct coresight_connection *in,
89 			 struct coresight_connection *out)
90 {
91 	struct tpda_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
92 
93 	spin_lock(&drvdata->spinlock);
94 	if (atomic_dec_return(&in->dest_refcnt) == 0)
95 		__tpda_disable(drvdata, in->dest_port);
96 
97 	spin_unlock(&drvdata->spinlock);
98 
99 	dev_dbg(drvdata->dev, "TPDA inport %d disabled\n", in->dest_port);
100 }
101 
102 static const struct coresight_ops_link tpda_link_ops = {
103 	.enable		= tpda_enable,
104 	.disable	= tpda_disable,
105 };
106 
107 static const struct coresight_ops tpda_cs_ops = {
108 	.link_ops	= &tpda_link_ops,
109 };
110 
tpda_init_default_data(struct tpda_drvdata * drvdata)111 static int tpda_init_default_data(struct tpda_drvdata *drvdata)
112 {
113 	int atid;
114 	/*
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.
120 	 */
121 	atid = coresight_trace_id_get_system_id();
122 	if (atid < 0)
123 		return atid;
124 
125 	drvdata->atid = atid;
126 	return 0;
127 }
128 
tpda_probe(struct amba_device * adev,const struct amba_id * id)129 static int tpda_probe(struct amba_device *adev, const struct amba_id *id)
130 {
131 	int ret;
132 	struct device *dev = &adev->dev;
133 	struct coresight_platform_data *pdata;
134 	struct tpda_drvdata *drvdata;
135 	struct coresight_desc desc = { 0 };
136 	void __iomem *base;
137 
138 	pdata = coresight_get_platform_data(dev);
139 	if (IS_ERR(pdata))
140 		return PTR_ERR(pdata);
141 	adev->dev.platform_data = pdata;
142 
143 	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
144 	if (!drvdata)
145 		return -ENOMEM;
146 
147 	drvdata->dev = &adev->dev;
148 	dev_set_drvdata(dev, drvdata);
149 
150 	base = devm_ioremap_resource(dev, &adev->res);
151 	if (IS_ERR(base))
152 		return PTR_ERR(base);
153 	drvdata->base = base;
154 
155 	spin_lock_init(&drvdata->spinlock);
156 
157 	ret = tpda_init_default_data(drvdata);
158 	if (ret)
159 		return ret;
160 
161 	desc.name = coresight_alloc_device_name(&tpda_devs, dev);
162 	if (!desc.name)
163 		return -ENOMEM;
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);
173 
174 	pm_runtime_put(&adev->dev);
175 
176 	dev_dbg(drvdata->dev, "TPDA initialized\n");
177 	return 0;
178 }
179 
tpda_remove(struct amba_device * adev)180 static void tpda_remove(struct amba_device *adev)
181 {
182 	struct tpda_drvdata *drvdata = dev_get_drvdata(&adev->dev);
183 
184 	coresight_trace_id_put_system_id(drvdata->atid);
185 	coresight_unregister(drvdata->csdev);
186 }
187 
188 /*
189  * Different TPDA has different periph id.
190  * The difference is 0-7 bits' value. So ignore 0-7 bits.
191  */
192 static struct amba_id tpda_ids[] = {
193 	{
194 		.id     = 0x000f0f00,
195 		.mask   = 0x000fff00,
196 	},
197 	{ 0, 0},
198 };
199 
200 static struct amba_driver tpda_driver = {
201 	.drv = {
202 		.name   = "coresight-tpda",
203 		.owner	= THIS_MODULE,
204 		.suppress_bind_attrs = true,
205 	},
206 	.probe          = tpda_probe,
207 	.remove		= tpda_remove,
208 	.id_table	= tpda_ids,
209 };
210 
211 module_amba_driver(tpda_driver);
212 
213 MODULE_LICENSE("GPL");
214 MODULE_DESCRIPTION("Trace, Profiling & Diagnostic Aggregator driver");
215