1 /* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 and
5  * only version 2 as published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17 #include <linux/io.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/clk.h>
21 #include <linux/coresight.h>
22 #include <linux/amba/bus.h>
23 
24 #include "coresight-priv.h"
25 
26 #define TPIU_SUPP_PORTSZ	0x000
27 #define TPIU_CURR_PORTSZ	0x004
28 #define TPIU_SUPP_TRIGMODES	0x100
29 #define TPIU_TRIG_CNTRVAL	0x104
30 #define TPIU_TRIG_MULT		0x108
31 #define TPIU_SUPP_TESTPATM	0x200
32 #define TPIU_CURR_TESTPATM	0x204
33 #define TPIU_TEST_PATREPCNTR	0x208
34 #define TPIU_FFSR		0x300
35 #define TPIU_FFCR		0x304
36 #define TPIU_FSYNC_CNTR		0x308
37 #define TPIU_EXTCTL_INPORT	0x400
38 #define TPIU_EXTCTL_OUTPORT	0x404
39 #define TPIU_ITTRFLINACK	0xee4
40 #define TPIU_ITTRFLIN		0xee8
41 #define TPIU_ITATBDATA0		0xeec
42 #define TPIU_ITATBCTR2		0xef0
43 #define TPIU_ITATBCTR1		0xef4
44 #define TPIU_ITATBCTR0		0xef8
45 
46 /** register definition **/
47 /* FFCR - 0x304 */
48 #define FFCR_FON_MAN		BIT(6)
49 
50 /**
51  * @base:	memory mapped base address for this component.
52  * @dev:	the device entity associated to this component.
53  * @csdev:	component vitals needed by the framework.
54  * @clk:	the clock this component is associated to.
55  */
56 struct tpiu_drvdata {
57 	void __iomem		*base;
58 	struct device		*dev;
59 	struct coresight_device	*csdev;
60 	struct clk		*clk;
61 };
62 
63 static void tpiu_enable_hw(struct tpiu_drvdata *drvdata)
64 {
65 	CS_UNLOCK(drvdata->base);
66 
67 	/* TODO: fill this up */
68 
69 	CS_LOCK(drvdata->base);
70 }
71 
72 static int tpiu_enable(struct coresight_device *csdev)
73 {
74 	struct tpiu_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
75 	int ret;
76 
77 	ret = clk_prepare_enable(drvdata->clk);
78 	if (ret)
79 		return ret;
80 
81 	tpiu_enable_hw(drvdata);
82 
83 	dev_info(drvdata->dev, "TPIU enabled\n");
84 	return 0;
85 }
86 
87 static void tpiu_disable_hw(struct tpiu_drvdata *drvdata)
88 {
89 	CS_UNLOCK(drvdata->base);
90 
91 	/* Clear formatter controle reg. */
92 	writel_relaxed(0x0, drvdata->base + TPIU_FFCR);
93 	/* Generate manual flush */
94 	writel_relaxed(FFCR_FON_MAN, drvdata->base + TPIU_FFCR);
95 
96 	CS_LOCK(drvdata->base);
97 }
98 
99 static void tpiu_disable(struct coresight_device *csdev)
100 {
101 	struct tpiu_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
102 
103 	tpiu_disable_hw(drvdata);
104 
105 	clk_disable_unprepare(drvdata->clk);
106 
107 	dev_info(drvdata->dev, "TPIU disabled\n");
108 }
109 
110 static const struct coresight_ops_sink tpiu_sink_ops = {
111 	.enable		= tpiu_enable,
112 	.disable	= tpiu_disable,
113 };
114 
115 static const struct coresight_ops tpiu_cs_ops = {
116 	.sink_ops	= &tpiu_sink_ops,
117 };
118 
119 static int tpiu_probe(struct amba_device *adev, const struct amba_id *id)
120 {
121 	int ret;
122 	void __iomem *base;
123 	struct device *dev = &adev->dev;
124 	struct coresight_platform_data *pdata = NULL;
125 	struct tpiu_drvdata *drvdata;
126 	struct resource *res = &adev->res;
127 	struct coresight_desc *desc;
128 	struct device_node *np = adev->dev.of_node;
129 
130 	if (np) {
131 		pdata = of_get_coresight_platform_data(dev, np);
132 		if (IS_ERR(pdata))
133 			return PTR_ERR(pdata);
134 		adev->dev.platform_data = pdata;
135 	}
136 
137 	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
138 	if (!drvdata)
139 		return -ENOMEM;
140 
141 	drvdata->dev = &adev->dev;
142 	dev_set_drvdata(dev, drvdata);
143 
144 	/* Validity for the resource is already checked by the AMBA core */
145 	base = devm_ioremap_resource(dev, res);
146 	if (IS_ERR(base))
147 		return PTR_ERR(base);
148 
149 	drvdata->base = base;
150 
151 	drvdata->clk = adev->pclk;
152 	ret = clk_prepare_enable(drvdata->clk);
153 	if (ret)
154 		return ret;
155 
156 	/* Disable tpiu to support older devices */
157 	tpiu_disable_hw(drvdata);
158 
159 	clk_disable_unprepare(drvdata->clk);
160 
161 	desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
162 	if (!desc)
163 		return -ENOMEM;
164 
165 	desc->type = CORESIGHT_DEV_TYPE_SINK;
166 	desc->subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_PORT;
167 	desc->ops = &tpiu_cs_ops;
168 	desc->pdata = pdata;
169 	desc->dev = dev;
170 	drvdata->csdev = coresight_register(desc);
171 	if (IS_ERR(drvdata->csdev))
172 		return PTR_ERR(drvdata->csdev);
173 
174 	dev_info(dev, "TPIU initialized\n");
175 	return 0;
176 }
177 
178 static int tpiu_remove(struct amba_device *adev)
179 {
180 	struct tpiu_drvdata *drvdata = amba_get_drvdata(adev);
181 
182 	coresight_unregister(drvdata->csdev);
183 	return 0;
184 }
185 
186 static struct amba_id tpiu_ids[] = {
187 	{
188 		.id	= 0x0003b912,
189 		.mask	= 0x0003ffff,
190 	},
191 	{ 0, 0},
192 };
193 
194 static struct amba_driver tpiu_driver = {
195 	.drv = {
196 		.name	= "coresight-tpiu",
197 		.owner	= THIS_MODULE,
198 	},
199 	.probe		= tpiu_probe,
200 	.remove		= tpiu_remove,
201 	.id_table	= tpiu_ids,
202 };
203 
204 module_amba_driver(tpiu_driver);
205 
206 MODULE_LICENSE("GPL v2");
207 MODULE_DESCRIPTION("CoreSight Trace Port Interface Unit driver");
208