1835d722bSMike Leach // SPDX-License-Identifier: GPL-2.0
2835d722bSMike Leach /*
3835d722bSMike Leach * Copyright (c) 2018 Linaro Limited, All rights reserved.
4835d722bSMike Leach * Author: Mike Leach <mike.leach@linaro.org>
5835d722bSMike Leach */
6835d722bSMike Leach
7298754c5SStephen Boyd #include <linux/amba/bus.h>
8298754c5SStephen Boyd #include <linux/atomic.h>
9298754c5SStephen Boyd #include <linux/bits.h>
10298754c5SStephen Boyd #include <linux/coresight.h>
116a0953ceSMike Leach #include <linux/cpu_pm.h>
12e9b88058SMike Leach #include <linux/cpuhotplug.h>
13298754c5SStephen Boyd #include <linux/device.h>
14298754c5SStephen Boyd #include <linux/io.h>
15298754c5SStephen Boyd #include <linux/kernel.h>
16298754c5SStephen Boyd #include <linux/list.h>
17298754c5SStephen Boyd #include <linux/mutex.h>
18298754c5SStephen Boyd #include <linux/pm_runtime.h>
19177af828SMike Leach #include <linux/property.h>
20298754c5SStephen Boyd #include <linux/spinlock.h>
21298754c5SStephen Boyd
22298754c5SStephen Boyd #include "coresight-priv.h"
23835d722bSMike Leach #include "coresight-cti.h"
24835d722bSMike Leach
2504e8429cSJames Clark /*
26835d722bSMike Leach * CTI devices can be associated with a PE, or be connected to CoreSight
27835d722bSMike Leach * hardware. We have a list of all CTIs irrespective of CPU bound or
28835d722bSMike Leach * otherwise.
29835d722bSMike Leach *
30835d722bSMike Leach * We assume that the non-CPU CTIs are always powered as we do with sinks etc.
31835d722bSMike Leach *
32835d722bSMike Leach * We leave the client to figure out if all the CTIs are interconnected with
33835d722bSMike Leach * the same CTM, in general this is the case but does not always have to be.
34835d722bSMike Leach */
35835d722bSMike Leach
36835d722bSMike Leach /* net of CTI devices connected via CTM */
37ebd9b678SJason Yan static LIST_HEAD(ect_net);
38835d722bSMike Leach
39835d722bSMike Leach /* protect the list */
40835d722bSMike Leach static DEFINE_MUTEX(ect_mutex);
41835d722bSMike Leach
42835d722bSMike Leach #define csdev_to_cti_drvdata(csdev) \
43835d722bSMike Leach dev_get_drvdata(csdev->dev.parent)
44835d722bSMike Leach
45e9b88058SMike Leach /* power management handling */
46e9b88058SMike Leach static int nr_cti_cpu;
47e9b88058SMike Leach
48e9b88058SMike Leach /* quick lookup list for CPU bound CTIs when power handling */
49e9b88058SMike Leach static struct cti_drvdata *cti_cpu_drvdata[NR_CPUS];
50e9b88058SMike Leach
51835d722bSMike Leach /*
52835d722bSMike Leach * CTI naming. CTI bound to cores will have the name cti_cpu<N> where
53835d722bSMike Leach * N is the CPU ID. System CTIs will have the name cti_sys<I> where I
54835d722bSMike Leach * is an index allocated by order of discovery.
55835d722bSMike Leach *
56835d722bSMike Leach * CTI device name list - for CTI not bound to cores.
57835d722bSMike Leach */
58835d722bSMike Leach DEFINE_CORESIGHT_DEVLIST(cti_sys_devs, "cti_sys");
59835d722bSMike Leach
60835d722bSMike Leach /* write set of regs to hardware - call with spinlock claimed */
cti_write_all_hw_regs(struct cti_drvdata * drvdata)61835d722bSMike Leach void cti_write_all_hw_regs(struct cti_drvdata *drvdata)
62835d722bSMike Leach {
63835d722bSMike Leach struct cti_config *config = &drvdata->config;
64835d722bSMike Leach int i;
65835d722bSMike Leach
66835d722bSMike Leach CS_UNLOCK(drvdata->base);
67835d722bSMike Leach
68835d722bSMike Leach /* disable CTI before writing registers */
69835d722bSMike Leach writel_relaxed(0, drvdata->base + CTICONTROL);
70835d722bSMike Leach
71835d722bSMike Leach /* write the CTI trigger registers */
72835d722bSMike Leach for (i = 0; i < config->nr_trig_max; i++) {
73835d722bSMike Leach writel_relaxed(config->ctiinen[i], drvdata->base + CTIINEN(i));
74835d722bSMike Leach writel_relaxed(config->ctiouten[i],
75835d722bSMike Leach drvdata->base + CTIOUTEN(i));
76835d722bSMike Leach }
77835d722bSMike Leach
78835d722bSMike Leach /* other regs */
79835d722bSMike Leach writel_relaxed(config->ctigate, drvdata->base + CTIGATE);
80835d722bSMike Leach writel_relaxed(config->asicctl, drvdata->base + ASICCTL);
81835d722bSMike Leach writel_relaxed(config->ctiappset, drvdata->base + CTIAPPSET);
82835d722bSMike Leach
83835d722bSMike Leach /* re-enable CTI */
84835d722bSMike Leach writel_relaxed(1, drvdata->base + CTICONTROL);
85835d722bSMike Leach
86835d722bSMike Leach CS_LOCK(drvdata->base);
87835d722bSMike Leach }
88835d722bSMike Leach
89835d722bSMike Leach /* write regs to hardware and enable */
cti_enable_hw(struct cti_drvdata * drvdata)90835d722bSMike Leach static int cti_enable_hw(struct cti_drvdata *drvdata)
91835d722bSMike Leach {
92835d722bSMike Leach struct cti_config *config = &drvdata->config;
93984f37efSTingwei Zhang unsigned long flags;
94835d722bSMike Leach int rc = 0;
95835d722bSMike Leach
96e3044065SYeoreum Yun raw_spin_lock_irqsave(&drvdata->spinlock, flags);
97835d722bSMike Leach
98835d722bSMike Leach /* no need to do anything if enabled or unpowered*/
99835d722bSMike Leach if (config->hw_enabled || !config->hw_powered)
100835d722bSMike Leach goto cti_state_unchanged;
101835d722bSMike Leach
102835d722bSMike Leach /* claim the device */
1038ce00296SSuzuki K Poulose rc = coresight_claim_device(drvdata->csdev);
104835d722bSMike Leach if (rc)
105835d722bSMike Leach goto cti_err_not_enabled;
106835d722bSMike Leach
107835d722bSMike Leach cti_write_all_hw_regs(drvdata);
108835d722bSMike Leach
109835d722bSMike Leach config->hw_enabled = true;
110479043b7SJames Clark drvdata->config.enable_req_count++;
111e3044065SYeoreum Yun raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
112835d722bSMike Leach return rc;
113835d722bSMike Leach
114835d722bSMike Leach cti_state_unchanged:
115479043b7SJames Clark drvdata->config.enable_req_count++;
116835d722bSMike Leach
117835d722bSMike Leach /* cannot enable due to error */
118835d722bSMike Leach cti_err_not_enabled:
119e3044065SYeoreum Yun raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
120835d722bSMike Leach return rc;
121835d722bSMike Leach }
122835d722bSMike Leach
123e9b88058SMike Leach /* re-enable CTI on CPU when using CPU hotplug */
cti_cpuhp_enable_hw(struct cti_drvdata * drvdata)124e9b88058SMike Leach static void cti_cpuhp_enable_hw(struct cti_drvdata *drvdata)
125e9b88058SMike Leach {
126e9b88058SMike Leach struct cti_config *config = &drvdata->config;
127e9b88058SMike Leach
128e3044065SYeoreum Yun raw_spin_lock(&drvdata->spinlock);
129e9b88058SMike Leach config->hw_powered = true;
130e9b88058SMike Leach
131e9b88058SMike Leach /* no need to do anything if no enable request */
132479043b7SJames Clark if (!drvdata->config.enable_req_count)
133e9b88058SMike Leach goto cti_hp_not_enabled;
134e9b88058SMike Leach
135e9b88058SMike Leach /* try to claim the device */
1368ce00296SSuzuki K Poulose if (coresight_claim_device(drvdata->csdev))
137e9b88058SMike Leach goto cti_hp_not_enabled;
138e9b88058SMike Leach
139e9b88058SMike Leach cti_write_all_hw_regs(drvdata);
140e9b88058SMike Leach config->hw_enabled = true;
141e3044065SYeoreum Yun raw_spin_unlock(&drvdata->spinlock);
142e9b88058SMike Leach return;
143e9b88058SMike Leach
144e9b88058SMike Leach /* did not re-enable due to no claim / no request */
145e9b88058SMike Leach cti_hp_not_enabled:
146e3044065SYeoreum Yun raw_spin_unlock(&drvdata->spinlock);
147e9b88058SMike Leach }
148e9b88058SMike Leach
149835d722bSMike Leach /* disable hardware */
cti_disable_hw(struct cti_drvdata * drvdata)150835d722bSMike Leach static int cti_disable_hw(struct cti_drvdata *drvdata)
151835d722bSMike Leach {
152835d722bSMike Leach struct cti_config *config = &drvdata->config;
1538ce00296SSuzuki K Poulose struct coresight_device *csdev = drvdata->csdev;
1543244fb6dSJames Clark int ret = 0;
155835d722bSMike Leach
156e3044065SYeoreum Yun raw_spin_lock(&drvdata->spinlock);
157835d722bSMike Leach
1583244fb6dSJames Clark /* don't allow negative refcounts, return an error */
159479043b7SJames Clark if (!drvdata->config.enable_req_count) {
1603244fb6dSJames Clark ret = -EINVAL;
1613244fb6dSJames Clark goto cti_not_disabled;
1623244fb6dSJames Clark }
1633244fb6dSJames Clark
164835d722bSMike Leach /* check refcount - disable on 0 */
165479043b7SJames Clark if (--drvdata->config.enable_req_count > 0)
166835d722bSMike Leach goto cti_not_disabled;
167835d722bSMike Leach
168835d722bSMike Leach /* no need to do anything if disabled or cpu unpowered */
169835d722bSMike Leach if (!config->hw_enabled || !config->hw_powered)
170835d722bSMike Leach goto cti_not_disabled;
171835d722bSMike Leach
172835d722bSMike Leach CS_UNLOCK(drvdata->base);
173835d722bSMike Leach
174835d722bSMike Leach /* disable CTI */
175835d722bSMike Leach writel_relaxed(0, drvdata->base + CTICONTROL);
176835d722bSMike Leach config->hw_enabled = false;
177835d722bSMike Leach
1788ce00296SSuzuki K Poulose coresight_disclaim_device_unlocked(csdev);
179835d722bSMike Leach CS_LOCK(drvdata->base);
180e3044065SYeoreum Yun raw_spin_unlock(&drvdata->spinlock);
1813244fb6dSJames Clark return ret;
182835d722bSMike Leach
183835d722bSMike Leach /* not disabled this call */
184835d722bSMike Leach cti_not_disabled:
185e3044065SYeoreum Yun raw_spin_unlock(&drvdata->spinlock);
1863244fb6dSJames Clark return ret;
187835d722bSMike Leach }
188835d722bSMike Leach
cti_write_single_reg(struct cti_drvdata * drvdata,int offset,u32 value)189b5213376SMike Leach void cti_write_single_reg(struct cti_drvdata *drvdata, int offset, u32 value)
190b5213376SMike Leach {
191b5213376SMike Leach CS_UNLOCK(drvdata->base);
192b5213376SMike Leach writel_relaxed(value, drvdata->base + offset);
193b5213376SMike Leach CS_LOCK(drvdata->base);
194b5213376SMike Leach }
195b5213376SMike Leach
cti_write_intack(struct device * dev,u32 ackval)196b5213376SMike Leach void cti_write_intack(struct device *dev, u32 ackval)
197b5213376SMike Leach {
198b5213376SMike Leach struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
199b5213376SMike Leach struct cti_config *config = &drvdata->config;
200b5213376SMike Leach
201e3044065SYeoreum Yun raw_spin_lock(&drvdata->spinlock);
202b5213376SMike Leach /* write if enabled */
203b5213376SMike Leach if (cti_active(config))
204b5213376SMike Leach cti_write_single_reg(drvdata, CTIINTACK, ackval);
205e3044065SYeoreum Yun raw_spin_unlock(&drvdata->spinlock);
206b5213376SMike Leach }
207b5213376SMike Leach
208835d722bSMike Leach /*
209835d722bSMike Leach * Look at the HW DEVID register for some of the HW settings.
210835d722bSMike Leach * DEVID[15:8] - max number of in / out triggers.
211835d722bSMike Leach */
212835d722bSMike Leach #define CTI_DEVID_MAXTRIGS(devid_val) ((int) BMVAL(devid_val, 8, 15))
213835d722bSMike Leach
214835d722bSMike Leach /* DEVID[19:16] - number of CTM channels */
215835d722bSMike Leach #define CTI_DEVID_CTMCHANNELS(devid_val) ((int) BMVAL(devid_val, 16, 19))
216835d722bSMike Leach
cti_set_default_config(struct device * dev,struct cti_drvdata * drvdata)217835d722bSMike Leach static void cti_set_default_config(struct device *dev,
218835d722bSMike Leach struct cti_drvdata *drvdata)
219835d722bSMike Leach {
220835d722bSMike Leach struct cti_config *config = &drvdata->config;
221835d722bSMike Leach u32 devid;
222835d722bSMike Leach
223835d722bSMike Leach devid = readl_relaxed(drvdata->base + CORESIGHT_DEVID);
224835d722bSMike Leach config->nr_trig_max = CTI_DEVID_MAXTRIGS(devid);
225835d722bSMike Leach
226835d722bSMike Leach /*
227835d722bSMike Leach * no current hardware should exceed this, but protect the driver
228835d722bSMike Leach * in case of fault / out of spec hw
229835d722bSMike Leach */
230835d722bSMike Leach if (config->nr_trig_max > CTIINOUTEN_MAX) {
231835d722bSMike Leach dev_warn_once(dev,
232835d722bSMike Leach "Limiting HW MaxTrig value(%d) to driver max(%d)\n",
233835d722bSMike Leach config->nr_trig_max, CTIINOUTEN_MAX);
234835d722bSMike Leach config->nr_trig_max = CTIINOUTEN_MAX;
235835d722bSMike Leach }
236835d722bSMike Leach
237835d722bSMike Leach config->nr_ctm_channels = CTI_DEVID_CTMCHANNELS(devid);
238835d722bSMike Leach
239835d722bSMike Leach /* Most regs default to 0 as zalloc'ed except...*/
240835d722bSMike Leach config->trig_filter_enable = true;
241835d722bSMike Leach config->ctigate = GENMASK(config->nr_ctm_channels - 1, 0);
242479043b7SJames Clark config->enable_req_count = 0;
243835d722bSMike Leach }
244835d722bSMike Leach
245835d722bSMike Leach /*
246835d722bSMike Leach * Add a connection entry to the list of connections for this
247835d722bSMike Leach * CTI device.
248835d722bSMike Leach */
cti_add_connection_entry(struct device * dev,struct cti_drvdata * drvdata,struct cti_trig_con * tc,struct coresight_device * csdev,const char * assoc_dev_name)249835d722bSMike Leach int cti_add_connection_entry(struct device *dev, struct cti_drvdata *drvdata,
250835d722bSMike Leach struct cti_trig_con *tc,
251835d722bSMike Leach struct coresight_device *csdev,
252835d722bSMike Leach const char *assoc_dev_name)
253835d722bSMike Leach {
254835d722bSMike Leach struct cti_device *cti_dev = &drvdata->ctidev;
255835d722bSMike Leach
256835d722bSMike Leach tc->con_dev = csdev;
257835d722bSMike Leach /*
258835d722bSMike Leach * Prefer actual associated CS device dev name to supplied value -
259835d722bSMike Leach * which is likely to be node name / other conn name.
260835d722bSMike Leach */
261835d722bSMike Leach if (csdev)
262835d722bSMike Leach tc->con_dev_name = dev_name(&csdev->dev);
263835d722bSMike Leach else if (assoc_dev_name != NULL) {
264835d722bSMike Leach tc->con_dev_name = devm_kstrdup(dev,
265835d722bSMike Leach assoc_dev_name, GFP_KERNEL);
266835d722bSMike Leach if (!tc->con_dev_name)
267835d722bSMike Leach return -ENOMEM;
268835d722bSMike Leach }
269835d722bSMike Leach list_add_tail(&tc->node, &cti_dev->trig_cons);
270835d722bSMike Leach cti_dev->nr_trig_con++;
271835d722bSMike Leach
272835d722bSMike Leach /* add connection usage bit info to overall info */
273835d722bSMike Leach drvdata->config.trig_in_use |= tc->con_in->used_mask;
274835d722bSMike Leach drvdata->config.trig_out_use |= tc->con_out->used_mask;
275835d722bSMike Leach
276835d722bSMike Leach return 0;
277835d722bSMike Leach }
278835d722bSMike Leach
279835d722bSMike Leach /* create a trigger connection with appropriately sized signal groups */
cti_allocate_trig_con(struct device * dev,int in_sigs,int out_sigs)280835d722bSMike Leach struct cti_trig_con *cti_allocate_trig_con(struct device *dev, int in_sigs,
281835d722bSMike Leach int out_sigs)
282835d722bSMike Leach {
283835d722bSMike Leach struct cti_trig_con *tc = NULL;
284835d722bSMike Leach struct cti_trig_grp *in = NULL, *out = NULL;
285835d722bSMike Leach
286835d722bSMike Leach tc = devm_kzalloc(dev, sizeof(struct cti_trig_con), GFP_KERNEL);
287835d722bSMike Leach if (!tc)
288835d722bSMike Leach return tc;
289835d722bSMike Leach
290835d722bSMike Leach in = devm_kzalloc(dev,
291835d722bSMike Leach offsetof(struct cti_trig_grp, sig_types[in_sigs]),
292835d722bSMike Leach GFP_KERNEL);
293835d722bSMike Leach if (!in)
294835d722bSMike Leach return NULL;
295835d722bSMike Leach
296835d722bSMike Leach out = devm_kzalloc(dev,
297835d722bSMike Leach offsetof(struct cti_trig_grp, sig_types[out_sigs]),
298835d722bSMike Leach GFP_KERNEL);
299835d722bSMike Leach if (!out)
300835d722bSMike Leach return NULL;
301835d722bSMike Leach
302835d722bSMike Leach tc->con_in = in;
303835d722bSMike Leach tc->con_out = out;
304835d722bSMike Leach tc->con_in->nr_sigs = in_sigs;
305835d722bSMike Leach tc->con_out->nr_sigs = out_sigs;
306835d722bSMike Leach return tc;
307835d722bSMike Leach }
308835d722bSMike Leach
309835d722bSMike Leach /*
310835d722bSMike Leach * Add a default connection if nothing else is specified.
311835d722bSMike Leach * single connection based on max in/out info, no assoc device
312835d722bSMike Leach */
cti_add_default_connection(struct device * dev,struct cti_drvdata * drvdata)313835d722bSMike Leach int cti_add_default_connection(struct device *dev, struct cti_drvdata *drvdata)
314835d722bSMike Leach {
315835d722bSMike Leach int ret = 0;
316835d722bSMike Leach int n_trigs = drvdata->config.nr_trig_max;
317835d722bSMike Leach u32 n_trig_mask = GENMASK(n_trigs - 1, 0);
318835d722bSMike Leach struct cti_trig_con *tc = NULL;
319835d722bSMike Leach
320835d722bSMike Leach /*
321835d722bSMike Leach * Assume max trigs for in and out,
322835d722bSMike Leach * all used, default sig types allocated
323835d722bSMike Leach */
324835d722bSMike Leach tc = cti_allocate_trig_con(dev, n_trigs, n_trigs);
325835d722bSMike Leach if (!tc)
326835d722bSMike Leach return -ENOMEM;
327835d722bSMike Leach
328835d722bSMike Leach tc->con_in->used_mask = n_trig_mask;
329835d722bSMike Leach tc->con_out->used_mask = n_trig_mask;
330835d722bSMike Leach ret = cti_add_connection_entry(dev, drvdata, tc, NULL, "default");
331835d722bSMike Leach return ret;
332835d722bSMike Leach }
333835d722bSMike Leach
3341bf82857SMike Leach /** cti channel api **/
3351bf82857SMike Leach /* attach/detach channel from trigger - write through if enabled. */
cti_channel_trig_op(struct device * dev,enum cti_chan_op op,enum cti_trig_dir direction,u32 channel_idx,u32 trigger_idx)3361bf82857SMike Leach int cti_channel_trig_op(struct device *dev, enum cti_chan_op op,
3371bf82857SMike Leach enum cti_trig_dir direction, u32 channel_idx,
3381bf82857SMike Leach u32 trigger_idx)
3391bf82857SMike Leach {
3401bf82857SMike Leach struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
3411bf82857SMike Leach struct cti_config *config = &drvdata->config;
3421bf82857SMike Leach u32 trig_bitmask;
3431bf82857SMike Leach u32 chan_bitmask;
3441bf82857SMike Leach u32 reg_value;
3451bf82857SMike Leach int reg_offset;
3461bf82857SMike Leach
3471bf82857SMike Leach /* ensure indexes in range */
3481bf82857SMike Leach if ((channel_idx >= config->nr_ctm_channels) ||
3491bf82857SMike Leach (trigger_idx >= config->nr_trig_max))
3501bf82857SMike Leach return -EINVAL;
3511bf82857SMike Leach
3521bf82857SMike Leach trig_bitmask = BIT(trigger_idx);
3531bf82857SMike Leach
3541bf82857SMike Leach /* ensure registered triggers and not out filtered */
3551bf82857SMike Leach if (direction == CTI_TRIG_IN) {
3561bf82857SMike Leach if (!(trig_bitmask & config->trig_in_use))
3571bf82857SMike Leach return -EINVAL;
3581bf82857SMike Leach } else {
3591bf82857SMike Leach if (!(trig_bitmask & config->trig_out_use))
3601bf82857SMike Leach return -EINVAL;
3611bf82857SMike Leach
3621bf82857SMike Leach if ((config->trig_filter_enable) &&
3631bf82857SMike Leach (config->trig_out_filter & trig_bitmask))
3641bf82857SMike Leach return -EINVAL;
3651bf82857SMike Leach }
3661bf82857SMike Leach
3671bf82857SMike Leach /* update the local register values */
3681bf82857SMike Leach chan_bitmask = BIT(channel_idx);
3691bf82857SMike Leach reg_offset = (direction == CTI_TRIG_IN ? CTIINEN(trigger_idx) :
3701bf82857SMike Leach CTIOUTEN(trigger_idx));
3711bf82857SMike Leach
372e3044065SYeoreum Yun raw_spin_lock(&drvdata->spinlock);
3731bf82857SMike Leach
3741bf82857SMike Leach /* read - modify write - the trigger / channel enable value */
3751bf82857SMike Leach reg_value = direction == CTI_TRIG_IN ? config->ctiinen[trigger_idx] :
3761bf82857SMike Leach config->ctiouten[trigger_idx];
3771bf82857SMike Leach if (op == CTI_CHAN_ATTACH)
3781bf82857SMike Leach reg_value |= chan_bitmask;
3791bf82857SMike Leach else
3801bf82857SMike Leach reg_value &= ~chan_bitmask;
3811bf82857SMike Leach
3821bf82857SMike Leach /* write local copy */
3831bf82857SMike Leach if (direction == CTI_TRIG_IN)
3841bf82857SMike Leach config->ctiinen[trigger_idx] = reg_value;
3851bf82857SMike Leach else
3861bf82857SMike Leach config->ctiouten[trigger_idx] = reg_value;
3871bf82857SMike Leach
3881bf82857SMike Leach /* write through if enabled */
3891bf82857SMike Leach if (cti_active(config))
3901bf82857SMike Leach cti_write_single_reg(drvdata, reg_offset, reg_value);
391e3044065SYeoreum Yun raw_spin_unlock(&drvdata->spinlock);
3921bf82857SMike Leach return 0;
3931bf82857SMike Leach }
3941bf82857SMike Leach
cti_channel_gate_op(struct device * dev,enum cti_chan_gate_op op,u32 channel_idx)3951bf82857SMike Leach int cti_channel_gate_op(struct device *dev, enum cti_chan_gate_op op,
3961bf82857SMike Leach u32 channel_idx)
3971bf82857SMike Leach {
3981bf82857SMike Leach struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
3991bf82857SMike Leach struct cti_config *config = &drvdata->config;
4001bf82857SMike Leach u32 chan_bitmask;
4011bf82857SMike Leach u32 reg_value;
4021bf82857SMike Leach int err = 0;
4031bf82857SMike Leach
4041bf82857SMike Leach if (channel_idx >= config->nr_ctm_channels)
4051bf82857SMike Leach return -EINVAL;
4061bf82857SMike Leach
4071bf82857SMike Leach chan_bitmask = BIT(channel_idx);
4081bf82857SMike Leach
409e3044065SYeoreum Yun raw_spin_lock(&drvdata->spinlock);
4101bf82857SMike Leach reg_value = config->ctigate;
4111bf82857SMike Leach switch (op) {
4121bf82857SMike Leach case CTI_GATE_CHAN_ENABLE:
4131bf82857SMike Leach reg_value |= chan_bitmask;
4141bf82857SMike Leach break;
4151bf82857SMike Leach
4161bf82857SMike Leach case CTI_GATE_CHAN_DISABLE:
4171bf82857SMike Leach reg_value &= ~chan_bitmask;
4181bf82857SMike Leach break;
4191bf82857SMike Leach
4201bf82857SMike Leach default:
4211bf82857SMike Leach err = -EINVAL;
4221bf82857SMike Leach break;
4231bf82857SMike Leach }
4241bf82857SMike Leach if (err == 0) {
4251bf82857SMike Leach config->ctigate = reg_value;
4261bf82857SMike Leach if (cti_active(config))
4271bf82857SMike Leach cti_write_single_reg(drvdata, CTIGATE, reg_value);
4281bf82857SMike Leach }
429e3044065SYeoreum Yun raw_spin_unlock(&drvdata->spinlock);
4301bf82857SMike Leach return err;
4311bf82857SMike Leach }
4321bf82857SMike Leach
cti_channel_setop(struct device * dev,enum cti_chan_set_op op,u32 channel_idx)4331bf82857SMike Leach int cti_channel_setop(struct device *dev, enum cti_chan_set_op op,
4341bf82857SMike Leach u32 channel_idx)
4351bf82857SMike Leach {
4361bf82857SMike Leach struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
4371bf82857SMike Leach struct cti_config *config = &drvdata->config;
4381bf82857SMike Leach u32 chan_bitmask;
4391bf82857SMike Leach u32 reg_value;
4401bf82857SMike Leach u32 reg_offset;
4411bf82857SMike Leach int err = 0;
4421bf82857SMike Leach
4431bf82857SMike Leach if (channel_idx >= config->nr_ctm_channels)
4441bf82857SMike Leach return -EINVAL;
4451bf82857SMike Leach
4461bf82857SMike Leach chan_bitmask = BIT(channel_idx);
4471bf82857SMike Leach
448e3044065SYeoreum Yun raw_spin_lock(&drvdata->spinlock);
4491bf82857SMike Leach reg_value = config->ctiappset;
4501bf82857SMike Leach switch (op) {
4511bf82857SMike Leach case CTI_CHAN_SET:
4521bf82857SMike Leach config->ctiappset |= chan_bitmask;
4531bf82857SMike Leach reg_value = config->ctiappset;
4541bf82857SMike Leach reg_offset = CTIAPPSET;
4551bf82857SMike Leach break;
4561bf82857SMike Leach
4571bf82857SMike Leach case CTI_CHAN_CLR:
4581bf82857SMike Leach config->ctiappset &= ~chan_bitmask;
4591bf82857SMike Leach reg_value = chan_bitmask;
4601bf82857SMike Leach reg_offset = CTIAPPCLEAR;
4611bf82857SMike Leach break;
4621bf82857SMike Leach
4631bf82857SMike Leach case CTI_CHAN_PULSE:
4641bf82857SMike Leach config->ctiappset &= ~chan_bitmask;
4651bf82857SMike Leach reg_value = chan_bitmask;
4661bf82857SMike Leach reg_offset = CTIAPPPULSE;
4671bf82857SMike Leach break;
4681bf82857SMike Leach
4691bf82857SMike Leach default:
4701bf82857SMike Leach err = -EINVAL;
4711bf82857SMike Leach break;
4721bf82857SMike Leach }
4731bf82857SMike Leach
4741bf82857SMike Leach if ((err == 0) && cti_active(config))
4751bf82857SMike Leach cti_write_single_reg(drvdata, reg_offset, reg_value);
476e3044065SYeoreum Yun raw_spin_unlock(&drvdata->spinlock);
4771bf82857SMike Leach
4781bf82857SMike Leach return err;
4791bf82857SMike Leach }
4801bf82857SMike Leach
cti_add_sysfs_link(struct cti_drvdata * drvdata,struct cti_trig_con * tc)48173274abbSMike Leach static bool cti_add_sysfs_link(struct cti_drvdata *drvdata,
48273274abbSMike Leach struct cti_trig_con *tc)
48373274abbSMike Leach {
48473274abbSMike Leach struct coresight_sysfs_link link_info;
48573274abbSMike Leach int link_err = 0;
48673274abbSMike Leach
48773274abbSMike Leach link_info.orig = drvdata->csdev;
48873274abbSMike Leach link_info.orig_name = tc->con_dev_name;
48973274abbSMike Leach link_info.target = tc->con_dev;
49073274abbSMike Leach link_info.target_name = dev_name(&drvdata->csdev->dev);
49173274abbSMike Leach
49273274abbSMike Leach link_err = coresight_add_sysfs_link(&link_info);
49373274abbSMike Leach if (link_err)
49473274abbSMike Leach dev_warn(&drvdata->csdev->dev,
49573274abbSMike Leach "Failed to set CTI sysfs link %s<=>%s\n",
49673274abbSMike Leach link_info.orig_name, link_info.target_name);
49773274abbSMike Leach return !link_err;
49873274abbSMike Leach }
49973274abbSMike Leach
cti_remove_sysfs_link(struct cti_drvdata * drvdata,struct cti_trig_con * tc)5001cce921bSMike Leach static void cti_remove_sysfs_link(struct cti_drvdata *drvdata,
5011cce921bSMike Leach struct cti_trig_con *tc)
50273274abbSMike Leach {
50373274abbSMike Leach struct coresight_sysfs_link link_info;
50473274abbSMike Leach
5051cce921bSMike Leach link_info.orig = drvdata->csdev;
50673274abbSMike Leach link_info.orig_name = tc->con_dev_name;
50773274abbSMike Leach link_info.target = tc->con_dev;
5081cce921bSMike Leach link_info.target_name = dev_name(&drvdata->csdev->dev);
50973274abbSMike Leach coresight_remove_sysfs_link(&link_info);
51073274abbSMike Leach }
51173274abbSMike Leach
512177af828SMike Leach /*
513177af828SMike Leach * Look for a matching connection device name in the list of connections.
514177af828SMike Leach * If found then swap in the csdev name, set trig con association pointer
515177af828SMike Leach * and return found.
516177af828SMike Leach */
517177af828SMike Leach static bool
cti_match_fixup_csdev(struct cti_device * ctidev,const char * node_name,struct coresight_device * csdev)518177af828SMike Leach cti_match_fixup_csdev(struct cti_device *ctidev, const char *node_name,
519177af828SMike Leach struct coresight_device *csdev)
520177af828SMike Leach {
521177af828SMike Leach struct cti_trig_con *tc;
52273274abbSMike Leach struct cti_drvdata *drvdata = container_of(ctidev, struct cti_drvdata,
52373274abbSMike Leach ctidev);
524177af828SMike Leach
525177af828SMike Leach list_for_each_entry(tc, &ctidev->trig_cons, node) {
526177af828SMike Leach if (tc->con_dev_name) {
527177af828SMike Leach if (!strcmp(node_name, tc->con_dev_name)) {
528177af828SMike Leach /* match: so swap in csdev name & dev */
529177af828SMike Leach tc->con_dev_name = dev_name(&csdev->dev);
530177af828SMike Leach tc->con_dev = csdev;
53173274abbSMike Leach /* try to set sysfs link */
53273274abbSMike Leach if (cti_add_sysfs_link(drvdata, tc))
533177af828SMike Leach return true;
53473274abbSMike Leach /* link failed - remove CTI reference */
53573274abbSMike Leach tc->con_dev = NULL;
53673274abbSMike Leach break;
537177af828SMike Leach }
538177af828SMike Leach }
539177af828SMike Leach }
540177af828SMike Leach return false;
541177af828SMike Leach }
542177af828SMike Leach
543177af828SMike Leach /*
544177af828SMike Leach * Search the cti list to add an associated CTI into the supplied CS device
545177af828SMike Leach * This will set the association if CTI declared before the CS device.
54623722fb4SSudeep Holla * (called from coresight_register() without coresight_mutex locked).
547177af828SMike Leach */
cti_add_assoc_to_csdev(struct coresight_device * csdev)5487b0fc5d2STingwei Zhang static void cti_add_assoc_to_csdev(struct coresight_device *csdev)
549177af828SMike Leach {
550177af828SMike Leach struct cti_drvdata *ect_item;
551177af828SMike Leach struct cti_device *ctidev;
552177af828SMike Leach const char *node_name = NULL;
553177af828SMike Leach
554177af828SMike Leach /* protect the list */
555177af828SMike Leach mutex_lock(&ect_mutex);
556177af828SMike Leach
557177af828SMike Leach /* exit if current is an ECT device.*/
5581b5b1646SJames Clark if ((csdev->type == CORESIGHT_DEV_TYPE_HELPER &&
5591b5b1646SJames Clark csdev->subtype.helper_subtype ==
5601b5b1646SJames Clark CORESIGHT_DEV_SUBTYPE_HELPER_ECT_CTI) ||
5611b5b1646SJames Clark list_empty(&ect_net))
562177af828SMike Leach goto cti_add_done;
563177af828SMike Leach
564177af828SMike Leach /* if we didn't find the csdev previously we used the fwnode name */
565177af828SMike Leach node_name = cti_plat_get_node_name(dev_fwnode(csdev->dev.parent));
566177af828SMike Leach if (!node_name)
567177af828SMike Leach goto cti_add_done;
568177af828SMike Leach
569177af828SMike Leach /* for each CTI in list... */
570177af828SMike Leach list_for_each_entry(ect_item, &ect_net, node) {
571177af828SMike Leach ctidev = &ect_item->ctidev;
572177af828SMike Leach if (cti_match_fixup_csdev(ctidev, node_name, csdev)) {
573177af828SMike Leach /*
574177af828SMike Leach * if we found a matching csdev then update the ECT
575177af828SMike Leach * association pointer for the device with this CTI.
576177af828SMike Leach */
5771b5b1646SJames Clark coresight_add_helper(csdev, ect_item->csdev);
578177af828SMike Leach break;
579177af828SMike Leach }
580177af828SMike Leach }
581177af828SMike Leach cti_add_done:
582177af828SMike Leach mutex_unlock(&ect_mutex);
583177af828SMike Leach }
584177af828SMike Leach
585177af828SMike Leach /*
586177af828SMike Leach * Removing the associated devices is easier.
587177af828SMike Leach */
cti_remove_assoc_from_csdev(struct coresight_device * csdev)5887b0fc5d2STingwei Zhang static void cti_remove_assoc_from_csdev(struct coresight_device *csdev)
589177af828SMike Leach {
590177af828SMike Leach struct cti_drvdata *ctidrv;
591177af828SMike Leach struct cti_trig_con *tc;
592177af828SMike Leach struct cti_device *ctidev;
5931b5b1646SJames Clark union coresight_dev_subtype cti_subtype = {
5941b5b1646SJames Clark .helper_subtype = CORESIGHT_DEV_SUBTYPE_HELPER_ECT_CTI
5951b5b1646SJames Clark };
5961b5b1646SJames Clark struct coresight_device *cti_csdev = coresight_find_output_type(
5971b5b1646SJames Clark csdev->pdata, CORESIGHT_DEV_TYPE_HELPER, cti_subtype);
5981b5b1646SJames Clark
5991b5b1646SJames Clark if (!cti_csdev)
6001b5b1646SJames Clark return;
601177af828SMike Leach
602177af828SMike Leach mutex_lock(&ect_mutex);
6031b5b1646SJames Clark ctidrv = csdev_to_cti_drvdata(cti_csdev);
604177af828SMike Leach ctidev = &ctidrv->ctidev;
605177af828SMike Leach list_for_each_entry(tc, &ctidev->trig_cons, node) {
606cab280bfSMike Leach if (tc->con_dev == csdev) {
6071cce921bSMike Leach cti_remove_sysfs_link(ctidrv, tc);
608177af828SMike Leach tc->con_dev = NULL;
609177af828SMike Leach break;
610177af828SMike Leach }
611177af828SMike Leach }
612177af828SMike Leach mutex_unlock(&ect_mutex);
613177af828SMike Leach }
6147b0fc5d2STingwei Zhang
6157b0fc5d2STingwei Zhang /*
6167b0fc5d2STingwei Zhang * Operations to add and remove associated CTI.
6177b0fc5d2STingwei Zhang * Register to coresight core driver as call back function.
6187b0fc5d2STingwei Zhang */
6197b0fc5d2STingwei Zhang static struct cti_assoc_op cti_assoc_ops = {
6207b0fc5d2STingwei Zhang .add = cti_add_assoc_to_csdev,
6217b0fc5d2STingwei Zhang .remove = cti_remove_assoc_from_csdev
6227b0fc5d2STingwei Zhang };
623177af828SMike Leach
624177af828SMike Leach /*
625177af828SMike Leach * Update the cross references where the associated device was found
626177af828SMike Leach * while we were building the connection info. This will occur if the
627177af828SMike Leach * assoc device was registered before the CTI.
628177af828SMike Leach */
cti_update_conn_xrefs(struct cti_drvdata * drvdata)629177af828SMike Leach static void cti_update_conn_xrefs(struct cti_drvdata *drvdata)
630177af828SMike Leach {
631177af828SMike Leach struct cti_trig_con *tc;
632177af828SMike Leach struct cti_device *ctidev = &drvdata->ctidev;
633177af828SMike Leach
634177af828SMike Leach list_for_each_entry(tc, &ctidev->trig_cons, node) {
63573274abbSMike Leach if (tc->con_dev) {
63673274abbSMike Leach /* if we can set the sysfs link */
63773274abbSMike Leach if (cti_add_sysfs_link(drvdata, tc))
63873274abbSMike Leach /* set the CTI/csdev association */
6391b5b1646SJames Clark coresight_add_helper(tc->con_dev,
640177af828SMike Leach drvdata->csdev);
64173274abbSMike Leach else
64273274abbSMike Leach /* otherwise remove reference from CTI */
64373274abbSMike Leach tc->con_dev = NULL;
64473274abbSMike Leach }
645177af828SMike Leach }
646177af828SMike Leach }
647177af828SMike Leach
cti_remove_conn_xrefs(struct cti_drvdata * drvdata)648177af828SMike Leach static void cti_remove_conn_xrefs(struct cti_drvdata *drvdata)
649177af828SMike Leach {
650177af828SMike Leach struct cti_trig_con *tc;
651177af828SMike Leach struct cti_device *ctidev = &drvdata->ctidev;
652177af828SMike Leach
653177af828SMike Leach list_for_each_entry(tc, &ctidev->trig_cons, node) {
654177af828SMike Leach if (tc->con_dev) {
6551cce921bSMike Leach cti_remove_sysfs_link(drvdata, tc);
65673274abbSMike Leach tc->con_dev = NULL;
657177af828SMike Leach }
658177af828SMike Leach }
659177af828SMike Leach }
660177af828SMike Leach
6616a0953ceSMike Leach /** cti PM callbacks **/
cti_cpu_pm_notify(struct notifier_block * nb,unsigned long cmd,void * v)6626a0953ceSMike Leach static int cti_cpu_pm_notify(struct notifier_block *nb, unsigned long cmd,
6636a0953ceSMike Leach void *v)
6646a0953ceSMike Leach {
6656a0953ceSMike Leach struct cti_drvdata *drvdata;
6668ce00296SSuzuki K Poulose struct coresight_device *csdev;
6676a0953ceSMike Leach unsigned int cpu = smp_processor_id();
6686a0953ceSMike Leach int notify_res = NOTIFY_OK;
6696a0953ceSMike Leach
6706a0953ceSMike Leach if (!cti_cpu_drvdata[cpu])
6716a0953ceSMike Leach return NOTIFY_OK;
6726a0953ceSMike Leach
6736a0953ceSMike Leach drvdata = cti_cpu_drvdata[cpu];
6748ce00296SSuzuki K Poulose csdev = drvdata->csdev;
6756a0953ceSMike Leach
6766a0953ceSMike Leach if (WARN_ON_ONCE(drvdata->ctidev.cpu != cpu))
6776a0953ceSMike Leach return NOTIFY_BAD;
6786a0953ceSMike Leach
679e3044065SYeoreum Yun raw_spin_lock(&drvdata->spinlock);
6806a0953ceSMike Leach
6816a0953ceSMike Leach switch (cmd) {
6826a0953ceSMike Leach case CPU_PM_ENTER:
6836a0953ceSMike Leach /* CTI regs all static - we have a copy & nothing to save */
6846a0953ceSMike Leach drvdata->config.hw_powered = false;
6856a0953ceSMike Leach if (drvdata->config.hw_enabled)
6868ce00296SSuzuki K Poulose coresight_disclaim_device(csdev);
6876a0953ceSMike Leach break;
6886a0953ceSMike Leach
6896a0953ceSMike Leach case CPU_PM_ENTER_FAILED:
6906a0953ceSMike Leach drvdata->config.hw_powered = true;
6916a0953ceSMike Leach if (drvdata->config.hw_enabled) {
6928ce00296SSuzuki K Poulose if (coresight_claim_device(csdev))
6936a0953ceSMike Leach drvdata->config.hw_enabled = false;
6946a0953ceSMike Leach }
6956a0953ceSMike Leach break;
6966a0953ceSMike Leach
6976a0953ceSMike Leach case CPU_PM_EXIT:
6986a0953ceSMike Leach /* write hardware registers to re-enable. */
6996a0953ceSMike Leach drvdata->config.hw_powered = true;
7006a0953ceSMike Leach drvdata->config.hw_enabled = false;
7016a0953ceSMike Leach
7026a0953ceSMike Leach /* check enable reference count to enable HW */
703479043b7SJames Clark if (drvdata->config.enable_req_count) {
7046a0953ceSMike Leach /* check we can claim the device as we re-power */
7058ce00296SSuzuki K Poulose if (coresight_claim_device(csdev))
7066a0953ceSMike Leach goto cti_notify_exit;
7076a0953ceSMike Leach
7086a0953ceSMike Leach drvdata->config.hw_enabled = true;
7096a0953ceSMike Leach cti_write_all_hw_regs(drvdata);
7106a0953ceSMike Leach }
7116a0953ceSMike Leach break;
7126a0953ceSMike Leach
7136a0953ceSMike Leach default:
7146a0953ceSMike Leach notify_res = NOTIFY_DONE;
7156a0953ceSMike Leach break;
7166a0953ceSMike Leach }
7176a0953ceSMike Leach
7186a0953ceSMike Leach cti_notify_exit:
719e3044065SYeoreum Yun raw_spin_unlock(&drvdata->spinlock);
7206a0953ceSMike Leach return notify_res;
7216a0953ceSMike Leach }
7226a0953ceSMike Leach
7236a0953ceSMike Leach static struct notifier_block cti_cpu_pm_nb = {
7246a0953ceSMike Leach .notifier_call = cti_cpu_pm_notify,
7256a0953ceSMike Leach };
7266a0953ceSMike Leach
727e9b88058SMike Leach /* CPU HP handlers */
cti_starting_cpu(unsigned int cpu)728e9b88058SMike Leach static int cti_starting_cpu(unsigned int cpu)
729e9b88058SMike Leach {
730e9b88058SMike Leach struct cti_drvdata *drvdata = cti_cpu_drvdata[cpu];
731e9b88058SMike Leach
732e9b88058SMike Leach if (!drvdata)
733e9b88058SMike Leach return 0;
734e9b88058SMike Leach
735e9b88058SMike Leach cti_cpuhp_enable_hw(drvdata);
736e9b88058SMike Leach return 0;
737e9b88058SMike Leach }
738e9b88058SMike Leach
cti_dying_cpu(unsigned int cpu)739e9b88058SMike Leach static int cti_dying_cpu(unsigned int cpu)
740e9b88058SMike Leach {
741e9b88058SMike Leach struct cti_drvdata *drvdata = cti_cpu_drvdata[cpu];
742e9b88058SMike Leach
743e9b88058SMike Leach if (!drvdata)
744e9b88058SMike Leach return 0;
745e9b88058SMike Leach
746e3044065SYeoreum Yun raw_spin_lock(&drvdata->spinlock);
747e9b88058SMike Leach drvdata->config.hw_powered = false;
7480dee2826STingwei Zhang if (drvdata->config.hw_enabled)
7498ce00296SSuzuki K Poulose coresight_disclaim_device(drvdata->csdev);
750e3044065SYeoreum Yun raw_spin_unlock(&drvdata->spinlock);
751e9b88058SMike Leach return 0;
752e9b88058SMike Leach }
753e9b88058SMike Leach
cti_pm_setup(struct cti_drvdata * drvdata)7546740de94SDan Carpenter static int cti_pm_setup(struct cti_drvdata *drvdata)
7556740de94SDan Carpenter {
7566740de94SDan Carpenter int ret;
7576740de94SDan Carpenter
7586740de94SDan Carpenter if (drvdata->ctidev.cpu == -1)
7596740de94SDan Carpenter return 0;
7606740de94SDan Carpenter
7616740de94SDan Carpenter if (nr_cti_cpu)
7626740de94SDan Carpenter goto done;
7636740de94SDan Carpenter
7646740de94SDan Carpenter cpus_read_lock();
7656740de94SDan Carpenter ret = cpuhp_setup_state_nocalls_cpuslocked(
7666740de94SDan Carpenter CPUHP_AP_ARM_CORESIGHT_CTI_STARTING,
7676740de94SDan Carpenter "arm/coresight_cti:starting",
7686740de94SDan Carpenter cti_starting_cpu, cti_dying_cpu);
7696740de94SDan Carpenter if (ret) {
7706740de94SDan Carpenter cpus_read_unlock();
7716740de94SDan Carpenter return ret;
7726740de94SDan Carpenter }
7736740de94SDan Carpenter
7746740de94SDan Carpenter ret = cpu_pm_register_notifier(&cti_cpu_pm_nb);
7756740de94SDan Carpenter cpus_read_unlock();
7766740de94SDan Carpenter if (ret) {
7776740de94SDan Carpenter cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_CTI_STARTING);
7786740de94SDan Carpenter return ret;
7796740de94SDan Carpenter }
7806740de94SDan Carpenter
7816740de94SDan Carpenter done:
7826740de94SDan Carpenter nr_cti_cpu++;
7836740de94SDan Carpenter cti_cpu_drvdata[drvdata->ctidev.cpu] = drvdata;
7846740de94SDan Carpenter
7856740de94SDan Carpenter return 0;
7866740de94SDan Carpenter }
7876740de94SDan Carpenter
788e9b88058SMike Leach /* release PM registrations */
cti_pm_release(struct cti_drvdata * drvdata)789e9b88058SMike Leach static void cti_pm_release(struct cti_drvdata *drvdata)
790e9b88058SMike Leach {
7916740de94SDan Carpenter if (drvdata->ctidev.cpu == -1)
7926740de94SDan Carpenter return;
7936740de94SDan Carpenter
7946740de94SDan Carpenter cti_cpu_drvdata[drvdata->ctidev.cpu] = NULL;
795e9b88058SMike Leach if (--nr_cti_cpu == 0) {
7966a0953ceSMike Leach cpu_pm_unregister_notifier(&cti_cpu_pm_nb);
7976740de94SDan Carpenter cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_CTI_STARTING);
798e9b88058SMike Leach }
799e9b88058SMike Leach }
800e9b88058SMike Leach
801835d722bSMike Leach /** cti ect operations **/
cti_enable(struct coresight_device * csdev,enum cs_mode mode,void * data)8021b5b1646SJames Clark int cti_enable(struct coresight_device *csdev, enum cs_mode mode, void *data)
803835d722bSMike Leach {
804835d722bSMike Leach struct cti_drvdata *drvdata = csdev_to_cti_drvdata(csdev);
805835d722bSMike Leach
806835d722bSMike Leach return cti_enable_hw(drvdata);
807835d722bSMike Leach }
808835d722bSMike Leach
cti_disable(struct coresight_device * csdev,void * data)8091b5b1646SJames Clark int cti_disable(struct coresight_device *csdev, void *data)
810835d722bSMike Leach {
811835d722bSMike Leach struct cti_drvdata *drvdata = csdev_to_cti_drvdata(csdev);
812835d722bSMike Leach
813835d722bSMike Leach return cti_disable_hw(drvdata);
814835d722bSMike Leach }
815835d722bSMike Leach
8161b5b1646SJames Clark static const struct coresight_ops_helper cti_ops_ect = {
817835d722bSMike Leach .enable = cti_enable,
818835d722bSMike Leach .disable = cti_disable,
819835d722bSMike Leach };
820835d722bSMike Leach
821ebd9b678SJason Yan static const struct coresight_ops cti_ops = {
8221b5b1646SJames Clark .helper_ops = &cti_ops_ect,
823835d722bSMike Leach };
824835d722bSMike Leach
825835d722bSMike Leach /*
826835d722bSMike Leach * Free up CTI specific resources
827835d722bSMike Leach * called by dev->release, need to call down to underlying csdev release.
828835d722bSMike Leach */
cti_device_release(struct device * dev)829835d722bSMike Leach static void cti_device_release(struct device *dev)
830835d722bSMike Leach {
831835d722bSMike Leach struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
832835d722bSMike Leach struct cti_drvdata *ect_item, *ect_tmp;
833835d722bSMike Leach
834835d722bSMike Leach mutex_lock(&ect_mutex);
835e9b88058SMike Leach cti_pm_release(drvdata);
836835d722bSMike Leach
837835d722bSMike Leach /* remove from the list */
838835d722bSMike Leach list_for_each_entry_safe(ect_item, ect_tmp, &ect_net, node) {
839835d722bSMike Leach if (ect_item == drvdata) {
840835d722bSMike Leach list_del(&ect_item->node);
841835d722bSMike Leach break;
842835d722bSMike Leach }
843835d722bSMike Leach }
844835d722bSMike Leach mutex_unlock(&ect_mutex);
845835d722bSMike Leach
846835d722bSMike Leach if (drvdata->csdev_release)
847835d722bSMike Leach drvdata->csdev_release(dev);
848835d722bSMike Leach }
cti_remove(struct amba_device * adev)8493fd269e7SUwe Kleine-König static void cti_remove(struct amba_device *adev)
8503c3fd1a1STingwei Zhang {
8513c3fd1a1STingwei Zhang struct cti_drvdata *drvdata = dev_get_drvdata(&adev->dev);
8523c3fd1a1STingwei Zhang
8533c3fd1a1STingwei Zhang mutex_lock(&ect_mutex);
8543c3fd1a1STingwei Zhang cti_remove_conn_xrefs(drvdata);
8553c3fd1a1STingwei Zhang mutex_unlock(&ect_mutex);
8563c3fd1a1STingwei Zhang
8573c3fd1a1STingwei Zhang coresight_unregister(drvdata->csdev);
8583c3fd1a1STingwei Zhang }
859835d722bSMike Leach
cti_probe(struct amba_device * adev,const struct amba_id * id)860835d722bSMike Leach static int cti_probe(struct amba_device *adev, const struct amba_id *id)
861835d722bSMike Leach {
862835d722bSMike Leach int ret = 0;
863835d722bSMike Leach void __iomem *base;
864835d722bSMike Leach struct device *dev = &adev->dev;
865835d722bSMike Leach struct cti_drvdata *drvdata = NULL;
866835d722bSMike Leach struct coresight_desc cti_desc;
867835d722bSMike Leach struct coresight_platform_data *pdata = NULL;
868835d722bSMike Leach struct resource *res = &adev->res;
869835d722bSMike Leach
870835d722bSMike Leach /* driver data*/
871835d722bSMike Leach drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
8726740de94SDan Carpenter if (!drvdata)
8736740de94SDan Carpenter return -ENOMEM;
874835d722bSMike Leach
875835d722bSMike Leach /* Validity for the resource is already checked by the AMBA core */
876835d722bSMike Leach base = devm_ioremap_resource(dev, res);
8776740de94SDan Carpenter if (IS_ERR(base))
8786740de94SDan Carpenter return PTR_ERR(base);
8796740de94SDan Carpenter
880835d722bSMike Leach drvdata->base = base;
8816e736c60SSuzuki K Poulose cti_desc.access = CSDEV_ACCESS_IOMEM(base);
882835d722bSMike Leach
883835d722bSMike Leach dev_set_drvdata(dev, drvdata);
884835d722bSMike Leach
885835d722bSMike Leach /* default CTI device info */
886835d722bSMike Leach drvdata->ctidev.cpu = -1;
887835d722bSMike Leach drvdata->ctidev.nr_trig_con = 0;
888835d722bSMike Leach drvdata->ctidev.ctm_id = 0;
889835d722bSMike Leach INIT_LIST_HEAD(&drvdata->ctidev.trig_cons);
890835d722bSMike Leach
891e3044065SYeoreum Yun raw_spin_lock_init(&drvdata->spinlock);
892835d722bSMike Leach
893835d722bSMike Leach /* initialise CTI driver config values */
894835d722bSMike Leach cti_set_default_config(dev, drvdata);
895835d722bSMike Leach
896835d722bSMike Leach pdata = coresight_cti_get_platform_data(dev);
897835d722bSMike Leach if (IS_ERR(pdata)) {
898835d722bSMike Leach dev_err(dev, "coresight_cti_get_platform_data err\n");
8996740de94SDan Carpenter return PTR_ERR(pdata);
900835d722bSMike Leach }
901835d722bSMike Leach
902835d722bSMike Leach /* default to powered - could change on PM notifications */
903835d722bSMike Leach drvdata->config.hw_powered = true;
904835d722bSMike Leach
905835d722bSMike Leach /* set up device name - will depend if cpu bound or otherwise */
906835d722bSMike Leach if (drvdata->ctidev.cpu >= 0)
907835d722bSMike Leach cti_desc.name = devm_kasprintf(dev, GFP_KERNEL, "cti_cpu%d",
908835d722bSMike Leach drvdata->ctidev.cpu);
909835d722bSMike Leach else
910835d722bSMike Leach cti_desc.name = coresight_alloc_device_name(&cti_sys_devs, dev);
9116740de94SDan Carpenter if (!cti_desc.name)
9126740de94SDan Carpenter return -ENOMEM;
913835d722bSMike Leach
914e9b88058SMike Leach /* setup CPU power management handling for CPU bound CTI devices. */
9156740de94SDan Carpenter ret = cti_pm_setup(drvdata);
916e9b88058SMike Leach if (ret)
9176740de94SDan Carpenter return ret;
918e9b88058SMike Leach
9193c5597e3SMike Leach /* create dynamic attributes for connections */
9203c5597e3SMike Leach ret = cti_create_cons_sysfs(dev, drvdata);
9213c5597e3SMike Leach if (ret) {
9223c5597e3SMike Leach dev_err(dev, "%s: create dynamic sysfs entries failed\n",
9233c5597e3SMike Leach cti_desc.name);
9246740de94SDan Carpenter goto pm_release;
9253c5597e3SMike Leach }
9263c5597e3SMike Leach
927835d722bSMike Leach /* set up coresight component description */
928835d722bSMike Leach cti_desc.pdata = pdata;
9291b5b1646SJames Clark cti_desc.type = CORESIGHT_DEV_TYPE_HELPER;
9301b5b1646SJames Clark cti_desc.subtype.helper_subtype = CORESIGHT_DEV_SUBTYPE_HELPER_ECT_CTI;
931835d722bSMike Leach cti_desc.ops = &cti_ops;
9323c5597e3SMike Leach cti_desc.groups = drvdata->ctidev.con_groups;
933835d722bSMike Leach cti_desc.dev = dev;
934*7cd63686SJames Clark
935*7cd63686SJames Clark coresight_clear_self_claim_tag(&cti_desc.access);
936835d722bSMike Leach drvdata->csdev = coresight_register(&cti_desc);
937835d722bSMike Leach if (IS_ERR(drvdata->csdev)) {
938835d722bSMike Leach ret = PTR_ERR(drvdata->csdev);
9396740de94SDan Carpenter goto pm_release;
940835d722bSMike Leach }
941835d722bSMike Leach
942835d722bSMike Leach /* add to list of CTI devices */
943835d722bSMike Leach mutex_lock(&ect_mutex);
944835d722bSMike Leach list_add(&drvdata->node, &ect_net);
945177af828SMike Leach /* set any cross references */
946177af828SMike Leach cti_update_conn_xrefs(drvdata);
947835d722bSMike Leach mutex_unlock(&ect_mutex);
948835d722bSMike Leach
949835d722bSMike Leach /* set up release chain */
950835d722bSMike Leach drvdata->csdev_release = drvdata->csdev->dev.release;
951835d722bSMike Leach drvdata->csdev->dev.release = cti_device_release;
952835d722bSMike Leach
953835d722bSMike Leach /* all done - dec pm refcount */
954835d722bSMike Leach pm_runtime_put(&adev->dev);
955835d722bSMike Leach dev_info(&drvdata->csdev->dev, "CTI initialized\n");
956835d722bSMike Leach return 0;
957835d722bSMike Leach
9586740de94SDan Carpenter pm_release:
959e9b88058SMike Leach cti_pm_release(drvdata);
960835d722bSMike Leach return ret;
961835d722bSMike Leach }
962835d722bSMike Leach
963835d722bSMike Leach static struct amba_cs_uci_id uci_id_cti[] = {
964835d722bSMike Leach {
965835d722bSMike Leach /* CTI UCI data */
966835d722bSMike Leach .devarch = 0x47701a14, /* CTI v2 */
967835d722bSMike Leach .devarch_mask = 0xfff0ffff,
968835d722bSMike Leach .devtype = 0x00000014, /* maj(0x4-debug) min(0x1-ECT) */
969835d722bSMike Leach }
970835d722bSMike Leach };
971835d722bSMike Leach
972835d722bSMike Leach static const struct amba_id cti_ids[] = {
973835d722bSMike Leach CS_AMBA_ID(0x000bb906), /* Coresight CTI (SoC 400), C-A72, C-A57 */
974835d722bSMike Leach CS_AMBA_ID(0x000bb922), /* CTI - C-A8 */
975835d722bSMike Leach CS_AMBA_ID(0x000bb9a8), /* CTI - C-A53 */
976835d722bSMike Leach CS_AMBA_ID(0x000bb9aa), /* CTI - C-A73 */
977835d722bSMike Leach CS_AMBA_UCI_ID(0x000bb9da, uci_id_cti), /* CTI - C-A35 */
978835d722bSMike Leach CS_AMBA_UCI_ID(0x000bb9ed, uci_id_cti), /* Coresight CTI (SoC 600) */
9798a519235SJames Clark { 0, 0, NULL },
980835d722bSMike Leach };
981835d722bSMike Leach
9823c3fd1a1STingwei Zhang MODULE_DEVICE_TABLE(amba, cti_ids);
9833c3fd1a1STingwei Zhang
984835d722bSMike Leach static struct amba_driver cti_driver = {
985835d722bSMike Leach .drv = {
986835d722bSMike Leach .name = "coresight-cti",
987835d722bSMike Leach .suppress_bind_attrs = true,
988835d722bSMike Leach },
989835d722bSMike Leach .probe = cti_probe,
9903c3fd1a1STingwei Zhang .remove = cti_remove,
991835d722bSMike Leach .id_table = cti_ids,
992835d722bSMike Leach };
9937b0fc5d2STingwei Zhang
cti_init(void)9947b0fc5d2STingwei Zhang static int __init cti_init(void)
9957b0fc5d2STingwei Zhang {
9967b0fc5d2STingwei Zhang int ret;
9977b0fc5d2STingwei Zhang
9987b0fc5d2STingwei Zhang ret = amba_driver_register(&cti_driver);
9997b0fc5d2STingwei Zhang if (ret)
10007b0fc5d2STingwei Zhang pr_info("Error registering cti driver\n");
10017b0fc5d2STingwei Zhang coresight_set_cti_ops(&cti_assoc_ops);
10027b0fc5d2STingwei Zhang return ret;
10037b0fc5d2STingwei Zhang }
10047b0fc5d2STingwei Zhang
cti_exit(void)10057b0fc5d2STingwei Zhang static void __exit cti_exit(void)
10067b0fc5d2STingwei Zhang {
10077b0fc5d2STingwei Zhang coresight_remove_cti_ops();
10087b0fc5d2STingwei Zhang amba_driver_unregister(&cti_driver);
10097b0fc5d2STingwei Zhang }
10107b0fc5d2STingwei Zhang
10117b0fc5d2STingwei Zhang module_init(cti_init);
10127b0fc5d2STingwei Zhang module_exit(cti_exit);
10133c3fd1a1STingwei Zhang
10143c3fd1a1STingwei Zhang MODULE_AUTHOR("Mike Leach <mike.leach@linaro.org>");
10153c3fd1a1STingwei Zhang MODULE_DESCRIPTION("Arm CoreSight CTI Driver");
10163c3fd1a1STingwei Zhang MODULE_LICENSE("GPL v2");
1017