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