xref: /linux/drivers/hwtracing/coresight/coresight-funnel.c (revision c26f4fbd58375bd6ef74f95eb73d61762ad97c59)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
4  *
5  * Description: CoreSight Funnel driver
6  */
7 
8 #include <linux/acpi.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/types.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/fs.h>
15 #include <linux/slab.h>
16 #include <linux/of.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/coresight.h>
20 #include <linux/amba/bus.h>
21 #include <linux/clk.h>
22 
23 #include "coresight-priv.h"
24 
25 #define FUNNEL_FUNCTL		0x000
26 #define FUNNEL_PRICTL		0x004
27 
28 #define FUNNEL_HOLDTIME_MASK	0xf00
29 #define FUNNEL_HOLDTIME_SHFT	0x8
30 #define FUNNEL_HOLDTIME		(0x7 << FUNNEL_HOLDTIME_SHFT)
31 #define FUNNEL_ENSx_MASK	0xff
32 
33 DEFINE_CORESIGHT_DEVLIST(funnel_devs, "funnel");
34 
35 /**
36  * struct funnel_drvdata - specifics associated to a funnel component
37  * @base:	memory mapped base address for this component.
38  * @atclk:	optional clock for the core parts of the funnel.
39  * @pclk:	APB clock if present, otherwise NULL
40  * @csdev:	component vitals needed by the framework.
41  * @priority:	port selection order.
42  * @spinlock:	serialize enable/disable operations.
43  */
44 struct funnel_drvdata {
45 	void __iomem		*base;
46 	struct clk		*atclk;
47 	struct clk		*pclk;
48 	struct coresight_device	*csdev;
49 	unsigned long		priority;
50 	raw_spinlock_t		spinlock;
51 };
52 
dynamic_funnel_enable_hw(struct funnel_drvdata * drvdata,int port)53 static int dynamic_funnel_enable_hw(struct funnel_drvdata *drvdata, int port)
54 {
55 	u32 functl;
56 	int rc = 0;
57 	struct coresight_device *csdev = drvdata->csdev;
58 
59 	CS_UNLOCK(drvdata->base);
60 
61 	functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
62 	/* Claim the device only when we enable the first slave */
63 	if (!(functl & FUNNEL_ENSx_MASK)) {
64 		rc = coresight_claim_device_unlocked(csdev);
65 		if (rc)
66 			goto done;
67 	}
68 
69 	functl &= ~FUNNEL_HOLDTIME_MASK;
70 	functl |= FUNNEL_HOLDTIME;
71 	functl |= (1 << port);
72 	writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
73 	writel_relaxed(drvdata->priority, drvdata->base + FUNNEL_PRICTL);
74 done:
75 	CS_LOCK(drvdata->base);
76 	return rc;
77 }
78 
funnel_enable(struct coresight_device * csdev,struct coresight_connection * in,struct coresight_connection * out)79 static int funnel_enable(struct coresight_device *csdev,
80 			 struct coresight_connection *in,
81 			 struct coresight_connection *out)
82 {
83 	int rc = 0;
84 	struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
85 	unsigned long flags;
86 	bool first_enable = false;
87 
88 	raw_spin_lock_irqsave(&drvdata->spinlock, flags);
89 	if (in->dest_refcnt == 0) {
90 		if (drvdata->base)
91 			rc = dynamic_funnel_enable_hw(drvdata, in->dest_port);
92 		if (!rc)
93 			first_enable = true;
94 	}
95 	if (!rc)
96 		in->dest_refcnt++;
97 	raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
98 
99 	if (first_enable)
100 		dev_dbg(&csdev->dev, "FUNNEL inport %d enabled\n",
101 			in->dest_port);
102 	return rc;
103 }
104 
dynamic_funnel_disable_hw(struct funnel_drvdata * drvdata,int inport)105 static void dynamic_funnel_disable_hw(struct funnel_drvdata *drvdata,
106 				      int inport)
107 {
108 	u32 functl;
109 	struct coresight_device *csdev = drvdata->csdev;
110 
111 	CS_UNLOCK(drvdata->base);
112 
113 	functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
114 	functl &= ~(1 << inport);
115 	writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
116 
117 	/* Disclaim the device if none of the slaves are now active */
118 	if (!(functl & FUNNEL_ENSx_MASK))
119 		coresight_disclaim_device_unlocked(csdev);
120 
121 	CS_LOCK(drvdata->base);
122 }
123 
funnel_disable(struct coresight_device * csdev,struct coresight_connection * in,struct coresight_connection * out)124 static void funnel_disable(struct coresight_device *csdev,
125 			   struct coresight_connection *in,
126 			   struct coresight_connection *out)
127 {
128 	struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
129 	unsigned long flags;
130 	bool last_disable = false;
131 
132 	raw_spin_lock_irqsave(&drvdata->spinlock, flags);
133 	if (--in->dest_refcnt == 0) {
134 		if (drvdata->base)
135 			dynamic_funnel_disable_hw(drvdata, in->dest_port);
136 		last_disable = true;
137 	}
138 	raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
139 
140 	if (last_disable)
141 		dev_dbg(&csdev->dev, "FUNNEL inport %d disabled\n",
142 			in->dest_port);
143 }
144 
145 static const struct coresight_ops_link funnel_link_ops = {
146 	.enable		= funnel_enable,
147 	.disable	= funnel_disable,
148 };
149 
150 static const struct coresight_ops funnel_cs_ops = {
151 	.link_ops	= &funnel_link_ops,
152 };
153 
priority_show(struct device * dev,struct device_attribute * attr,char * buf)154 static ssize_t priority_show(struct device *dev,
155 			     struct device_attribute *attr, char *buf)
156 {
157 	struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
158 	unsigned long val = drvdata->priority;
159 
160 	return sprintf(buf, "%#lx\n", val);
161 }
162 
priority_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)163 static ssize_t priority_store(struct device *dev,
164 			      struct device_attribute *attr,
165 			      const char *buf, size_t size)
166 {
167 	int ret;
168 	unsigned long val;
169 	struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
170 
171 	ret = kstrtoul(buf, 16, &val);
172 	if (ret)
173 		return ret;
174 
175 	drvdata->priority = val;
176 	return size;
177 }
178 static DEVICE_ATTR_RW(priority);
179 
get_funnel_ctrl_hw(struct funnel_drvdata * drvdata)180 static u32 get_funnel_ctrl_hw(struct funnel_drvdata *drvdata)
181 {
182 	u32 functl;
183 
184 	CS_UNLOCK(drvdata->base);
185 	functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
186 	CS_LOCK(drvdata->base);
187 
188 	return functl;
189 }
190 
funnel_ctrl_show(struct device * dev,struct device_attribute * attr,char * buf)191 static ssize_t funnel_ctrl_show(struct device *dev,
192 			     struct device_attribute *attr, char *buf)
193 {
194 	u32 val;
195 	struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
196 
197 	pm_runtime_get_sync(dev->parent);
198 
199 	val = get_funnel_ctrl_hw(drvdata);
200 
201 	pm_runtime_put(dev->parent);
202 
203 	return sprintf(buf, "%#x\n", val);
204 }
205 static DEVICE_ATTR_RO(funnel_ctrl);
206 
207 static struct attribute *coresight_funnel_attrs[] = {
208 	&dev_attr_funnel_ctrl.attr,
209 	&dev_attr_priority.attr,
210 	NULL,
211 };
212 ATTRIBUTE_GROUPS(coresight_funnel);
213 
funnel_probe(struct device * dev,struct resource * res)214 static int funnel_probe(struct device *dev, struct resource *res)
215 {
216 	int ret;
217 	void __iomem *base;
218 	struct coresight_platform_data *pdata = NULL;
219 	struct funnel_drvdata *drvdata;
220 	struct coresight_desc desc = { 0 };
221 
222 	if (is_of_node(dev_fwnode(dev)) &&
223 	    of_device_is_compatible(dev->of_node, "arm,coresight-funnel"))
224 		dev_warn_once(dev, "Uses OBSOLETE CoreSight funnel binding\n");
225 
226 	desc.name = coresight_alloc_device_name(&funnel_devs, dev);
227 	if (!desc.name)
228 		return -ENOMEM;
229 
230 	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
231 	if (!drvdata)
232 		return -ENOMEM;
233 
234 	drvdata->atclk = devm_clk_get(dev, "atclk"); /* optional */
235 	if (!IS_ERR(drvdata->atclk)) {
236 		ret = clk_prepare_enable(drvdata->atclk);
237 		if (ret)
238 			return ret;
239 	}
240 
241 	drvdata->pclk = coresight_get_enable_apb_pclk(dev);
242 	if (IS_ERR(drvdata->pclk))
243 		return -ENODEV;
244 
245 	/*
246 	 * Map the device base for dynamic-funnel, which has been
247 	 * validated by AMBA core.
248 	 */
249 	if (res) {
250 		base = devm_ioremap_resource(dev, res);
251 		if (IS_ERR(base)) {
252 			ret = PTR_ERR(base);
253 			goto out_disable_clk;
254 		}
255 		drvdata->base = base;
256 		desc.groups = coresight_funnel_groups;
257 		desc.access = CSDEV_ACCESS_IOMEM(base);
258 		coresight_clear_self_claim_tag(&desc.access);
259 	}
260 
261 	dev_set_drvdata(dev, drvdata);
262 
263 	pdata = coresight_get_platform_data(dev);
264 	if (IS_ERR(pdata)) {
265 		ret = PTR_ERR(pdata);
266 		goto out_disable_clk;
267 	}
268 	dev->platform_data = pdata;
269 
270 	raw_spin_lock_init(&drvdata->spinlock);
271 	desc.type = CORESIGHT_DEV_TYPE_LINK;
272 	desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
273 	desc.ops = &funnel_cs_ops;
274 	desc.pdata = pdata;
275 	desc.dev = dev;
276 	drvdata->csdev = coresight_register(&desc);
277 	if (IS_ERR(drvdata->csdev)) {
278 		ret = PTR_ERR(drvdata->csdev);
279 		goto out_disable_clk;
280 	}
281 
282 	ret = 0;
283 
284 out_disable_clk:
285 	if (ret && !IS_ERR_OR_NULL(drvdata->atclk))
286 		clk_disable_unprepare(drvdata->atclk);
287 	if (ret && !IS_ERR_OR_NULL(drvdata->pclk))
288 		clk_disable_unprepare(drvdata->pclk);
289 	return ret;
290 }
291 
funnel_remove(struct device * dev)292 static int funnel_remove(struct device *dev)
293 {
294 	struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
295 
296 	coresight_unregister(drvdata->csdev);
297 
298 	return 0;
299 }
300 
301 #ifdef CONFIG_PM
funnel_runtime_suspend(struct device * dev)302 static int funnel_runtime_suspend(struct device *dev)
303 {
304 	struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
305 
306 	if (drvdata && !IS_ERR(drvdata->atclk))
307 		clk_disable_unprepare(drvdata->atclk);
308 
309 	if (drvdata && !IS_ERR_OR_NULL(drvdata->pclk))
310 		clk_disable_unprepare(drvdata->pclk);
311 
312 	return 0;
313 }
314 
funnel_runtime_resume(struct device * dev)315 static int funnel_runtime_resume(struct device *dev)
316 {
317 	struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
318 
319 	if (drvdata && !IS_ERR(drvdata->atclk))
320 		clk_prepare_enable(drvdata->atclk);
321 
322 	if (drvdata && !IS_ERR_OR_NULL(drvdata->pclk))
323 		clk_prepare_enable(drvdata->pclk);
324 	return 0;
325 }
326 #endif
327 
328 static const struct dev_pm_ops funnel_dev_pm_ops = {
329 	SET_RUNTIME_PM_OPS(funnel_runtime_suspend, funnel_runtime_resume, NULL)
330 };
331 
funnel_platform_probe(struct platform_device * pdev)332 static int funnel_platform_probe(struct platform_device *pdev)
333 {
334 	struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
335 	int ret;
336 
337 	pm_runtime_get_noresume(&pdev->dev);
338 	pm_runtime_set_active(&pdev->dev);
339 	pm_runtime_enable(&pdev->dev);
340 
341 	ret = funnel_probe(&pdev->dev, res);
342 	pm_runtime_put(&pdev->dev);
343 	if (ret)
344 		pm_runtime_disable(&pdev->dev);
345 
346 	return ret;
347 }
348 
funnel_platform_remove(struct platform_device * pdev)349 static void funnel_platform_remove(struct platform_device *pdev)
350 {
351 	struct funnel_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
352 
353 	if (WARN_ON(!drvdata))
354 		return;
355 
356 	funnel_remove(&pdev->dev);
357 	pm_runtime_disable(&pdev->dev);
358 	if (!IS_ERR_OR_NULL(drvdata->pclk))
359 		clk_put(drvdata->pclk);
360 }
361 
362 static const struct of_device_id funnel_match[] = {
363 	{.compatible = "arm,coresight-static-funnel"},
364 	{}
365 };
366 
367 MODULE_DEVICE_TABLE(of, funnel_match);
368 
369 #ifdef CONFIG_ACPI
370 static const struct acpi_device_id funnel_acpi_ids[] = {
371 	{"ARMHC9FE", 0, 0, 0}, /* ARM Coresight Static Funnel */
372 	{"ARMHC9FF", 0, 0, 0}, /* ARM CoreSight Dynamic Funnel */
373 	{},
374 };
375 
376 MODULE_DEVICE_TABLE(acpi, funnel_acpi_ids);
377 #endif
378 
379 static struct platform_driver funnel_driver = {
380 	.probe		= funnel_platform_probe,
381 	.remove		= funnel_platform_remove,
382 	.driver		= {
383 		.name   = "coresight-funnel",
384 		/* THIS_MODULE is taken care of by platform_driver_register() */
385 		.of_match_table = funnel_match,
386 		.acpi_match_table = ACPI_PTR(funnel_acpi_ids),
387 		.pm	= &funnel_dev_pm_ops,
388 		.suppress_bind_attrs = true,
389 	},
390 };
391 
dynamic_funnel_probe(struct amba_device * adev,const struct amba_id * id)392 static int dynamic_funnel_probe(struct amba_device *adev,
393 				const struct amba_id *id)
394 {
395 	int ret;
396 
397 	ret = funnel_probe(&adev->dev, &adev->res);
398 	if (!ret)
399 		pm_runtime_put(&adev->dev);
400 
401 	return ret;
402 }
403 
dynamic_funnel_remove(struct amba_device * adev)404 static void dynamic_funnel_remove(struct amba_device *adev)
405 {
406 	funnel_remove(&adev->dev);
407 }
408 
409 static const struct amba_id dynamic_funnel_ids[] = {
410 	{
411 		.id     = 0x000bb908,
412 		.mask   = 0x000fffff,
413 	},
414 	{
415 		/* Coresight SoC-600 */
416 		.id     = 0x000bb9eb,
417 		.mask   = 0x000fffff,
418 	},
419 	{ 0, 0, NULL },
420 };
421 
422 MODULE_DEVICE_TABLE(amba, dynamic_funnel_ids);
423 
424 static struct amba_driver dynamic_funnel_driver = {
425 	.drv = {
426 		.name	= "coresight-dynamic-funnel",
427 		.pm	= &funnel_dev_pm_ops,
428 		.suppress_bind_attrs = true,
429 	},
430 	.probe		= dynamic_funnel_probe,
431 	.remove		= dynamic_funnel_remove,
432 	.id_table	= dynamic_funnel_ids,
433 };
434 
funnel_init(void)435 static int __init funnel_init(void)
436 {
437 	return coresight_init_driver("funnel", &dynamic_funnel_driver, &funnel_driver,
438 				     THIS_MODULE);
439 }
440 
funnel_exit(void)441 static void __exit funnel_exit(void)
442 {
443 	coresight_remove_driver(&dynamic_funnel_driver, &funnel_driver);
444 }
445 
446 module_init(funnel_init);
447 module_exit(funnel_exit);
448 
449 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
450 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
451 MODULE_DESCRIPTION("Arm CoreSight Funnel Driver");
452 MODULE_LICENSE("GPL v2");
453