1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/amba/bus.h>
7 #include <linux/clk.h>
8 #include <linux/coresight.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/init.h>
12 #include <linux/io.h>
13 #include <linux/kernel.h>
14 #include <linux/of.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/slab.h>
17 
18 #include "coresight-priv.h"
19 
20 #define REPLICATOR_IDFILTER0		0x000
21 #define REPLICATOR_IDFILTER1		0x004
22 
23 /**
24  * struct replicator_state - specifics associated to a replicator component
25  * @base:	memory mapped base address for this component.
26  * @dev:	the device entity associated with this component
27  * @atclk:	optional clock for the core parts of the replicator.
28  * @csdev:	component vitals needed by the framework
29  */
30 struct replicator_state {
31 	void __iomem		*base;
32 	struct device		*dev;
33 	struct clk		*atclk;
34 	struct coresight_device	*csdev;
35 };
36 
replicator_enable(struct coresight_device * csdev,int inport,int outport)37 static int replicator_enable(struct coresight_device *csdev, int inport,
38 			      int outport)
39 {
40 	struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent);
41 
42 	CS_UNLOCK(drvdata->base);
43 
44 	/*
45 	 * Ensure that the other port is disabled
46 	 * 0x00 - passing through the replicator unimpeded
47 	 * 0xff - disable (or impede) the flow of ATB data
48 	 */
49 	if (outport == 0) {
50 		writel_relaxed(0x00, drvdata->base + REPLICATOR_IDFILTER0);
51 		writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1);
52 	} else {
53 		writel_relaxed(0x00, drvdata->base + REPLICATOR_IDFILTER1);
54 		writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0);
55 	}
56 
57 	CS_LOCK(drvdata->base);
58 
59 	dev_info(drvdata->dev, "REPLICATOR enabled\n");
60 	return 0;
61 }
62 
replicator_disable(struct coresight_device * csdev,int inport,int outport)63 static void replicator_disable(struct coresight_device *csdev, int inport,
64 				int outport)
65 {
66 	struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent);
67 
68 	CS_UNLOCK(drvdata->base);
69 
70 	/* disable the flow of ATB data through port */
71 	if (outport == 0)
72 		writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0);
73 	else
74 		writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1);
75 
76 	CS_LOCK(drvdata->base);
77 
78 	dev_info(drvdata->dev, "REPLICATOR disabled\n");
79 }
80 
81 static const struct coresight_ops_link replicator_link_ops = {
82 	.enable		= replicator_enable,
83 	.disable	= replicator_disable,
84 };
85 
86 static const struct coresight_ops replicator_cs_ops = {
87 	.link_ops	= &replicator_link_ops,
88 };
89 
90 #define coresight_replicator_reg(name, offset) \
91 	coresight_simple_reg32(struct replicator_state, name, offset)
92 
93 coresight_replicator_reg(idfilter0, REPLICATOR_IDFILTER0);
94 coresight_replicator_reg(idfilter1, REPLICATOR_IDFILTER1);
95 
96 static struct attribute *replicator_mgmt_attrs[] = {
97 	&dev_attr_idfilter0.attr,
98 	&dev_attr_idfilter1.attr,
99 	NULL,
100 };
101 
102 static const struct attribute_group replicator_mgmt_group = {
103 	.attrs = replicator_mgmt_attrs,
104 	.name = "mgmt",
105 };
106 
107 static const struct attribute_group *replicator_groups[] = {
108 	&replicator_mgmt_group,
109 	NULL,
110 };
111 
replicator_probe(struct amba_device * adev,const struct amba_id * id)112 static int replicator_probe(struct amba_device *adev, const struct amba_id *id)
113 {
114 	int ret;
115 	struct device *dev = &adev->dev;
116 	struct resource *res = &adev->res;
117 	struct coresight_platform_data *pdata = NULL;
118 	struct replicator_state *drvdata;
119 	struct coresight_desc desc = { 0 };
120 	struct device_node *np = adev->dev.of_node;
121 	void __iomem *base;
122 
123 	if (np) {
124 		pdata = of_get_coresight_platform_data(dev, np);
125 		if (IS_ERR(pdata))
126 			return PTR_ERR(pdata);
127 		adev->dev.platform_data = pdata;
128 	}
129 
130 	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
131 	if (!drvdata)
132 		return -ENOMEM;
133 
134 	drvdata->dev = &adev->dev;
135 	drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
136 	if (!IS_ERR(drvdata->atclk)) {
137 		ret = clk_prepare_enable(drvdata->atclk);
138 		if (ret)
139 			return ret;
140 	}
141 
142 	/* Validity for the resource is already checked by the AMBA core */
143 	base = devm_ioremap_resource(dev, res);
144 	if (IS_ERR(base))
145 		return PTR_ERR(base);
146 
147 	drvdata->base = base;
148 	dev_set_drvdata(dev, drvdata);
149 	pm_runtime_put(&adev->dev);
150 
151 	desc.type = CORESIGHT_DEV_TYPE_LINK;
152 	desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
153 	desc.ops = &replicator_cs_ops;
154 	desc.pdata = adev->dev.platform_data;
155 	desc.dev = &adev->dev;
156 	desc.groups = replicator_groups;
157 	drvdata->csdev = coresight_register(&desc);
158 
159 	return PTR_ERR_OR_ZERO(drvdata->csdev);
160 }
161 
162 #ifdef CONFIG_PM
replicator_runtime_suspend(struct device * dev)163 static int replicator_runtime_suspend(struct device *dev)
164 {
165 	struct replicator_state *drvdata = dev_get_drvdata(dev);
166 
167 	if (drvdata && !IS_ERR(drvdata->atclk))
168 		clk_disable_unprepare(drvdata->atclk);
169 
170 	return 0;
171 }
172 
replicator_runtime_resume(struct device * dev)173 static int replicator_runtime_resume(struct device *dev)
174 {
175 	struct replicator_state *drvdata = dev_get_drvdata(dev);
176 
177 	if (drvdata && !IS_ERR(drvdata->atclk))
178 		clk_prepare_enable(drvdata->atclk);
179 
180 	return 0;
181 }
182 #endif
183 
184 static const struct dev_pm_ops replicator_dev_pm_ops = {
185 	SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
186 			   replicator_runtime_resume,
187 			   NULL)
188 };
189 
190 static const struct amba_id replicator_ids[] = {
191 	{
192 		.id     = 0x000bb909,
193 		.mask   = 0x000fffff,
194 	},
195 	{
196 		/* Coresight SoC-600 */
197 		.id     = 0x000bb9ec,
198 		.mask   = 0x000fffff,
199 	},
200 	{ 0, 0 },
201 };
202 
203 static struct amba_driver replicator_driver = {
204 	.drv = {
205 		.name	= "coresight-dynamic-replicator",
206 		.pm	= &replicator_dev_pm_ops,
207 		.suppress_bind_attrs = true,
208 	},
209 	.probe		= replicator_probe,
210 	.id_table	= replicator_ids,
211 };
212 builtin_amba_driver(replicator_driver);
213