1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
5 * Description: CoreSight Trace Port Interface Unit driver
8 #include <linux/acpi.h>
9 #include <linux/amba/bus.h>
10 #include <linux/atomic.h>
11 #include <linux/clk.h>
12 #include <linux/coresight.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/slab.h>
22 #include "coresight-priv.h"
24 #define TPIU_SUPP_PORTSZ 0x000
25 #define TPIU_CURR_PORTSZ 0x004
26 #define TPIU_SUPP_TRIGMODES 0x100
27 #define TPIU_TRIG_CNTRVAL 0x104
28 #define TPIU_TRIG_MULT 0x108
29 #define TPIU_SUPP_TESTPATM 0x200
30 #define TPIU_CURR_TESTPATM 0x204
31 #define TPIU_TEST_PATREPCNTR 0x208
32 #define TPIU_FFSR 0x300
33 #define TPIU_FFCR 0x304
34 #define TPIU_FSYNC_CNTR 0x308
35 #define TPIU_EXTCTL_INPORT 0x400
36 #define TPIU_EXTCTL_OUTPORT 0x404
37 #define TPIU_ITTRFLINACK 0xee4
38 #define TPIU_ITTRFLIN 0xee8
39 #define TPIU_ITATBDATA0 0xeec
40 #define TPIU_ITATBCTR2 0xef0
41 #define TPIU_ITATBCTR1 0xef4
42 #define TPIU_ITATBCTR0 0xef8
44 /** register definition **/
46 #define FFSR_FT_STOPPED_BIT 1
48 #define FFCR_FON_MAN_BIT 6
49 #define FFCR_FON_MAN BIT(6)
50 #define FFCR_STOP_FI BIT(12)
52 DEFINE_CORESIGHT_DEVLIST(tpiu_devs
, "tpiu");
55 * @base: memory mapped base address for this component.
56 * @atclk: optional clock for the core parts of the TPIU.
57 * @pclk: APB clock if present, otherwise NULL
58 * @csdev: component vitals needed by the framework.
64 struct coresight_device
*csdev
;
68 static void tpiu_enable_hw(struct csdev_access
*csa
)
72 /* TODO: fill this up */
77 static int tpiu_enable(struct coresight_device
*csdev
, enum cs_mode mode
,
80 struct tpiu_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
82 guard(spinlock
)(&drvdata
->spinlock
);
83 tpiu_enable_hw(&csdev
->access
);
85 dev_dbg(&csdev
->dev
, "TPIU enabled\n");
89 static void tpiu_disable_hw(struct csdev_access
*csa
)
93 /* Clear formatter and stop on flush */
94 csdev_access_relaxed_write32(csa
, FFCR_STOP_FI
, TPIU_FFCR
);
95 /* Generate manual flush */
96 csdev_access_relaxed_write32(csa
, FFCR_STOP_FI
| FFCR_FON_MAN
, TPIU_FFCR
);
97 /* Wait for flush to complete */
98 coresight_timeout(csa
, TPIU_FFCR
, FFCR_FON_MAN_BIT
, 0);
99 /* Wait for formatter to stop */
100 coresight_timeout(csa
, TPIU_FFSR
, FFSR_FT_STOPPED_BIT
, 1);
105 static int tpiu_disable(struct coresight_device
*csdev
)
107 struct tpiu_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
109 guard(spinlock
)(&drvdata
->spinlock
);
114 tpiu_disable_hw(&csdev
->access
);
116 dev_dbg(&csdev
->dev
, "TPIU disabled\n");
120 static const struct coresight_ops_sink tpiu_sink_ops
= {
121 .enable
= tpiu_enable
,
122 .disable
= tpiu_disable
,
125 static const struct coresight_ops tpiu_cs_ops
= {
126 .sink_ops
= &tpiu_sink_ops
,
129 static int __tpiu_probe(struct device
*dev
, struct resource
*res
)
133 struct coresight_platform_data
*pdata
= NULL
;
134 struct tpiu_drvdata
*drvdata
;
135 struct coresight_desc desc
= { 0 };
137 desc
.name
= coresight_alloc_device_name(&tpiu_devs
, dev
);
141 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
145 spin_lock_init(&drvdata
->spinlock
);
147 drvdata
->atclk
= devm_clk_get(dev
, "atclk"); /* optional */
148 if (!IS_ERR(drvdata
->atclk
)) {
149 ret
= clk_prepare_enable(drvdata
->atclk
);
154 drvdata
->pclk
= coresight_get_enable_apb_pclk(dev
);
155 if (IS_ERR(drvdata
->pclk
))
157 dev_set_drvdata(dev
, drvdata
);
159 /* Validity for the resource is already checked by the AMBA core */
160 base
= devm_ioremap_resource(dev
, res
);
162 return PTR_ERR(base
);
164 drvdata
->base
= base
;
165 desc
.access
= CSDEV_ACCESS_IOMEM(base
);
167 /* Disable tpiu to support older devices */
168 tpiu_disable_hw(&desc
.access
);
170 pdata
= coresight_get_platform_data(dev
);
172 return PTR_ERR(pdata
);
173 dev
->platform_data
= pdata
;
175 desc
.type
= CORESIGHT_DEV_TYPE_SINK
;
176 desc
.subtype
.sink_subtype
= CORESIGHT_DEV_SUBTYPE_SINK_PORT
;
177 desc
.ops
= &tpiu_cs_ops
;
180 drvdata
->csdev
= coresight_register(&desc
);
182 if (!IS_ERR(drvdata
->csdev
))
185 return PTR_ERR(drvdata
->csdev
);
188 static int tpiu_probe(struct amba_device
*adev
, const struct amba_id
*id
)
192 ret
= __tpiu_probe(&adev
->dev
, &adev
->res
);
194 pm_runtime_put(&adev
->dev
);
198 static void __tpiu_remove(struct device
*dev
)
200 struct tpiu_drvdata
*drvdata
= dev_get_drvdata(dev
);
202 coresight_unregister(drvdata
->csdev
);
205 static void tpiu_remove(struct amba_device
*adev
)
207 __tpiu_remove(&adev
->dev
);
211 static int tpiu_runtime_suspend(struct device
*dev
)
213 struct tpiu_drvdata
*drvdata
= dev_get_drvdata(dev
);
215 if (drvdata
&& !IS_ERR(drvdata
->atclk
))
216 clk_disable_unprepare(drvdata
->atclk
);
218 if (drvdata
&& !IS_ERR_OR_NULL(drvdata
->pclk
))
219 clk_disable_unprepare(drvdata
->pclk
);
223 static int tpiu_runtime_resume(struct device
*dev
)
225 struct tpiu_drvdata
*drvdata
= dev_get_drvdata(dev
);
227 if (drvdata
&& !IS_ERR(drvdata
->atclk
))
228 clk_prepare_enable(drvdata
->atclk
);
230 if (drvdata
&& !IS_ERR_OR_NULL(drvdata
->pclk
))
231 clk_prepare_enable(drvdata
->pclk
);
236 static const struct dev_pm_ops tpiu_dev_pm_ops
= {
237 SET_RUNTIME_PM_OPS(tpiu_runtime_suspend
, tpiu_runtime_resume
, NULL
)
240 static const struct amba_id tpiu_ids
[] = {
250 /* Coresight SoC-600 */
257 MODULE_DEVICE_TABLE(amba
, tpiu_ids
);
259 static struct amba_driver tpiu_driver
= {
261 .name
= "coresight-tpiu",
262 .pm
= &tpiu_dev_pm_ops
,
263 .suppress_bind_attrs
= true,
266 .remove
= tpiu_remove
,
267 .id_table
= tpiu_ids
,
270 static int tpiu_platform_probe(struct platform_device
*pdev
)
272 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
275 pm_runtime_get_noresume(&pdev
->dev
);
276 pm_runtime_set_active(&pdev
->dev
);
277 pm_runtime_enable(&pdev
->dev
);
279 ret
= __tpiu_probe(&pdev
->dev
, res
);
280 pm_runtime_put(&pdev
->dev
);
282 pm_runtime_disable(&pdev
->dev
);
287 static void tpiu_platform_remove(struct platform_device
*pdev
)
289 struct tpiu_drvdata
*drvdata
= dev_get_drvdata(&pdev
->dev
);
291 if (WARN_ON(!drvdata
))
294 __tpiu_remove(&pdev
->dev
);
295 pm_runtime_disable(&pdev
->dev
);
296 if (!IS_ERR_OR_NULL(drvdata
->pclk
))
297 clk_put(drvdata
->pclk
);
301 static const struct acpi_device_id tpiu_acpi_ids
[] = {
302 {"ARMHC979", 0, 0, 0}, /* ARM CoreSight TPIU */
305 MODULE_DEVICE_TABLE(acpi
, tpiu_acpi_ids
);
308 static struct platform_driver tpiu_platform_driver
= {
309 .probe
= tpiu_platform_probe
,
310 .remove_new
= tpiu_platform_remove
,
312 .name
= "coresight-tpiu-platform",
313 .acpi_match_table
= ACPI_PTR(tpiu_acpi_ids
),
314 .suppress_bind_attrs
= true,
315 .pm
= &tpiu_dev_pm_ops
,
319 static int __init
tpiu_init(void)
321 return coresight_init_driver("tpiu", &tpiu_driver
, &tpiu_platform_driver
);
324 static void __exit
tpiu_exit(void)
326 coresight_remove_driver(&tpiu_driver
, &tpiu_platform_driver
);
328 module_init(tpiu_init
);
329 module_exit(tpiu_exit
);
331 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
332 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
333 MODULE_DESCRIPTION("Arm CoreSight TPIU (Trace Port Interface Unit) driver");
334 MODULE_LICENSE("GPL v2");