xref: /OK3568_Linux_fs/kernel/drivers/net/ipa/ipa_clock.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4*4882a593Smuzhiyun  * Copyright (C) 2018-2020 Linaro Ltd.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/refcount.h>
8*4882a593Smuzhiyun #include <linux/mutex.h>
9*4882a593Smuzhiyun #include <linux/clk.h>
10*4882a593Smuzhiyun #include <linux/device.h>
11*4882a593Smuzhiyun #include <linux/interconnect.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include "ipa.h"
14*4882a593Smuzhiyun #include "ipa_clock.h"
15*4882a593Smuzhiyun #include "ipa_modem.h"
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /**
18*4882a593Smuzhiyun  * DOC: IPA Clocking
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * The "IPA Clock" manages both the IPA core clock and the interconnects
21*4882a593Smuzhiyun  * (buses) the IPA depends on as a single logical entity.  A reference count
22*4882a593Smuzhiyun  * is incremented by "get" operations and decremented by "put" operations.
23*4882a593Smuzhiyun  * Transitions of that count from 0 to 1 result in the clock and interconnects
24*4882a593Smuzhiyun  * being enabled, and transitions of the count from 1 to 0 cause them to be
25*4882a593Smuzhiyun  * disabled.  We currently operate the core clock at a fixed clock rate, and
26*4882a593Smuzhiyun  * all buses at a fixed average and peak bandwidth.  As more advanced IPA
27*4882a593Smuzhiyun  * features are enabled, we can make better use of clock and bus scaling.
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * An IPA clock reference must be held for any access to IPA hardware.
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define	IPA_CORE_CLOCK_RATE		(75UL * 1000 * 1000)	/* Hz */
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /* Interconnect path bandwidths (each times 1000 bytes per second) */
35*4882a593Smuzhiyun #define IPA_MEMORY_AVG			(80 * 1000)	/* 80 MBps */
36*4882a593Smuzhiyun #define IPA_MEMORY_PEAK			(600 * 1000)
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define IPA_IMEM_AVG			(80 * 1000)
39*4882a593Smuzhiyun #define IPA_IMEM_PEAK			(350 * 1000)
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #define IPA_CONFIG_AVG			(40 * 1000)
42*4882a593Smuzhiyun #define IPA_CONFIG_PEAK			(40 * 1000)
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /**
45*4882a593Smuzhiyun  * struct ipa_clock - IPA clocking information
46*4882a593Smuzhiyun  * @count:		Clocking reference count
47*4882a593Smuzhiyun  * @mutex:		Protects clock enable/disable
48*4882a593Smuzhiyun  * @core:		IPA core clock
49*4882a593Smuzhiyun  * @memory_path:	Memory interconnect
50*4882a593Smuzhiyun  * @imem_path:		Internal memory interconnect
51*4882a593Smuzhiyun  * @config_path:	Configuration space interconnect
52*4882a593Smuzhiyun  */
53*4882a593Smuzhiyun struct ipa_clock {
54*4882a593Smuzhiyun 	refcount_t count;
55*4882a593Smuzhiyun 	struct mutex mutex; /* protects clock enable/disable */
56*4882a593Smuzhiyun 	struct clk *core;
57*4882a593Smuzhiyun 	struct icc_path *memory_path;
58*4882a593Smuzhiyun 	struct icc_path *imem_path;
59*4882a593Smuzhiyun 	struct icc_path *config_path;
60*4882a593Smuzhiyun };
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun static struct icc_path *
ipa_interconnect_init_one(struct device * dev,const char * name)63*4882a593Smuzhiyun ipa_interconnect_init_one(struct device *dev, const char *name)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	struct icc_path *path;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	path = of_icc_get(dev, name);
68*4882a593Smuzhiyun 	if (IS_ERR(path))
69*4882a593Smuzhiyun 		dev_err(dev, "error %ld getting %s interconnect\n",
70*4882a593Smuzhiyun 			PTR_ERR(path), name);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	return path;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun /* Initialize interconnects required for IPA operation */
ipa_interconnect_init(struct ipa_clock * clock,struct device * dev)76*4882a593Smuzhiyun static int ipa_interconnect_init(struct ipa_clock *clock, struct device *dev)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	struct icc_path *path;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	path = ipa_interconnect_init_one(dev, "memory");
81*4882a593Smuzhiyun 	if (IS_ERR(path))
82*4882a593Smuzhiyun 		goto err_return;
83*4882a593Smuzhiyun 	clock->memory_path = path;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	path = ipa_interconnect_init_one(dev, "imem");
86*4882a593Smuzhiyun 	if (IS_ERR(path))
87*4882a593Smuzhiyun 		goto err_memory_path_put;
88*4882a593Smuzhiyun 	clock->imem_path = path;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	path = ipa_interconnect_init_one(dev, "config");
91*4882a593Smuzhiyun 	if (IS_ERR(path))
92*4882a593Smuzhiyun 		goto err_imem_path_put;
93*4882a593Smuzhiyun 	clock->config_path = path;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	return 0;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun err_imem_path_put:
98*4882a593Smuzhiyun 	icc_put(clock->imem_path);
99*4882a593Smuzhiyun err_memory_path_put:
100*4882a593Smuzhiyun 	icc_put(clock->memory_path);
101*4882a593Smuzhiyun err_return:
102*4882a593Smuzhiyun 	return PTR_ERR(path);
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /* Inverse of ipa_interconnect_init() */
ipa_interconnect_exit(struct ipa_clock * clock)106*4882a593Smuzhiyun static void ipa_interconnect_exit(struct ipa_clock *clock)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	icc_put(clock->config_path);
109*4882a593Smuzhiyun 	icc_put(clock->imem_path);
110*4882a593Smuzhiyun 	icc_put(clock->memory_path);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun /* Currently we only use one bandwidth level, so just "enable" interconnects */
ipa_interconnect_enable(struct ipa * ipa)114*4882a593Smuzhiyun static int ipa_interconnect_enable(struct ipa *ipa)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	struct ipa_clock *clock = ipa->clock;
117*4882a593Smuzhiyun 	int ret;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	ret = icc_set_bw(clock->memory_path, IPA_MEMORY_AVG, IPA_MEMORY_PEAK);
120*4882a593Smuzhiyun 	if (ret)
121*4882a593Smuzhiyun 		return ret;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	ret = icc_set_bw(clock->imem_path, IPA_IMEM_AVG, IPA_IMEM_PEAK);
124*4882a593Smuzhiyun 	if (ret)
125*4882a593Smuzhiyun 		goto err_memory_path_disable;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	ret = icc_set_bw(clock->config_path, IPA_CONFIG_AVG, IPA_CONFIG_PEAK);
128*4882a593Smuzhiyun 	if (ret)
129*4882a593Smuzhiyun 		goto err_imem_path_disable;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	return 0;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun err_imem_path_disable:
134*4882a593Smuzhiyun 	(void)icc_set_bw(clock->imem_path, 0, 0);
135*4882a593Smuzhiyun err_memory_path_disable:
136*4882a593Smuzhiyun 	(void)icc_set_bw(clock->memory_path, 0, 0);
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	return ret;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun /* To disable an interconnect, we just its bandwidth to 0 */
ipa_interconnect_disable(struct ipa * ipa)142*4882a593Smuzhiyun static int ipa_interconnect_disable(struct ipa *ipa)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	struct ipa_clock *clock = ipa->clock;
145*4882a593Smuzhiyun 	int ret;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	ret = icc_set_bw(clock->memory_path, 0, 0);
148*4882a593Smuzhiyun 	if (ret)
149*4882a593Smuzhiyun 		return ret;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	ret = icc_set_bw(clock->imem_path, 0, 0);
152*4882a593Smuzhiyun 	if (ret)
153*4882a593Smuzhiyun 		goto err_memory_path_reenable;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	ret = icc_set_bw(clock->config_path, 0, 0);
156*4882a593Smuzhiyun 	if (ret)
157*4882a593Smuzhiyun 		goto err_imem_path_reenable;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	return 0;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun err_imem_path_reenable:
162*4882a593Smuzhiyun 	(void)icc_set_bw(clock->imem_path, IPA_IMEM_AVG, IPA_IMEM_PEAK);
163*4882a593Smuzhiyun err_memory_path_reenable:
164*4882a593Smuzhiyun 	(void)icc_set_bw(clock->memory_path, IPA_MEMORY_AVG, IPA_MEMORY_PEAK);
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	return ret;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun /* Turn on IPA clocks, including interconnects */
ipa_clock_enable(struct ipa * ipa)170*4882a593Smuzhiyun static int ipa_clock_enable(struct ipa *ipa)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	int ret;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	ret = ipa_interconnect_enable(ipa);
175*4882a593Smuzhiyun 	if (ret)
176*4882a593Smuzhiyun 		return ret;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	ret = clk_prepare_enable(ipa->clock->core);
179*4882a593Smuzhiyun 	if (ret)
180*4882a593Smuzhiyun 		ipa_interconnect_disable(ipa);
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	return ret;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun /* Inverse of ipa_clock_enable() */
ipa_clock_disable(struct ipa * ipa)186*4882a593Smuzhiyun static void ipa_clock_disable(struct ipa *ipa)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun 	clk_disable_unprepare(ipa->clock->core);
189*4882a593Smuzhiyun 	(void)ipa_interconnect_disable(ipa);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun /* Get an IPA clock reference, but only if the reference count is
193*4882a593Smuzhiyun  * already non-zero.  Returns true if the additional reference was
194*4882a593Smuzhiyun  * added successfully, or false otherwise.
195*4882a593Smuzhiyun  */
ipa_clock_get_additional(struct ipa * ipa)196*4882a593Smuzhiyun bool ipa_clock_get_additional(struct ipa *ipa)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	return refcount_inc_not_zero(&ipa->clock->count);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun /* Get an IPA clock reference.  If the reference count is non-zero, it is
202*4882a593Smuzhiyun  * incremented and return is immediate.  Otherwise it is checked again
203*4882a593Smuzhiyun  * under protection of the mutex, and if appropriate the IPA clock
204*4882a593Smuzhiyun  * is enabled.
205*4882a593Smuzhiyun  *
206*4882a593Smuzhiyun  * Incrementing the reference count is intentionally deferred until
207*4882a593Smuzhiyun  * after the clock is running and endpoints are resumed.
208*4882a593Smuzhiyun  */
ipa_clock_get(struct ipa * ipa)209*4882a593Smuzhiyun void ipa_clock_get(struct ipa *ipa)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun 	struct ipa_clock *clock = ipa->clock;
212*4882a593Smuzhiyun 	int ret;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	/* If the clock is running, just bump the reference count */
215*4882a593Smuzhiyun 	if (ipa_clock_get_additional(ipa))
216*4882a593Smuzhiyun 		return;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	/* Otherwise get the mutex and check again */
219*4882a593Smuzhiyun 	mutex_lock(&clock->mutex);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	/* A reference might have been added before we got the mutex. */
222*4882a593Smuzhiyun 	if (ipa_clock_get_additional(ipa))
223*4882a593Smuzhiyun 		goto out_mutex_unlock;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	ret = ipa_clock_enable(ipa);
226*4882a593Smuzhiyun 	if (ret) {
227*4882a593Smuzhiyun 		dev_err(&ipa->pdev->dev, "error %d enabling IPA clock\n", ret);
228*4882a593Smuzhiyun 		goto out_mutex_unlock;
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	refcount_set(&clock->count, 1);
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun out_mutex_unlock:
234*4882a593Smuzhiyun 	mutex_unlock(&clock->mutex);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun /* Attempt to remove an IPA clock reference.  If this represents the
238*4882a593Smuzhiyun  * last reference, disable the IPA clock under protection of the mutex.
239*4882a593Smuzhiyun  */
ipa_clock_put(struct ipa * ipa)240*4882a593Smuzhiyun void ipa_clock_put(struct ipa *ipa)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun 	struct ipa_clock *clock = ipa->clock;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	/* If this is not the last reference there's nothing more to do */
245*4882a593Smuzhiyun 	if (!refcount_dec_and_mutex_lock(&clock->count, &clock->mutex))
246*4882a593Smuzhiyun 		return;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	ipa_clock_disable(ipa);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	mutex_unlock(&clock->mutex);
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun /* Return the current IPA core clock rate */
ipa_clock_rate(struct ipa * ipa)254*4882a593Smuzhiyun u32 ipa_clock_rate(struct ipa *ipa)
255*4882a593Smuzhiyun {
256*4882a593Smuzhiyun 	return ipa->clock ? (u32)clk_get_rate(ipa->clock->core) : 0;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun /* Initialize IPA clocking */
ipa_clock_init(struct device * dev)260*4882a593Smuzhiyun struct ipa_clock *ipa_clock_init(struct device *dev)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun 	struct ipa_clock *clock;
263*4882a593Smuzhiyun 	struct clk *clk;
264*4882a593Smuzhiyun 	int ret;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	clk = clk_get(dev, "core");
267*4882a593Smuzhiyun 	if (IS_ERR(clk)) {
268*4882a593Smuzhiyun 		dev_err(dev, "error %ld getting core clock\n", PTR_ERR(clk));
269*4882a593Smuzhiyun 		return ERR_CAST(clk);
270*4882a593Smuzhiyun 	}
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	ret = clk_set_rate(clk, IPA_CORE_CLOCK_RATE);
273*4882a593Smuzhiyun 	if (ret) {
274*4882a593Smuzhiyun 		dev_err(dev, "error %d setting core clock rate to %lu\n",
275*4882a593Smuzhiyun 			ret, IPA_CORE_CLOCK_RATE);
276*4882a593Smuzhiyun 		goto err_clk_put;
277*4882a593Smuzhiyun 	}
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	clock = kzalloc(sizeof(*clock), GFP_KERNEL);
280*4882a593Smuzhiyun 	if (!clock) {
281*4882a593Smuzhiyun 		ret = -ENOMEM;
282*4882a593Smuzhiyun 		goto err_clk_put;
283*4882a593Smuzhiyun 	}
284*4882a593Smuzhiyun 	clock->core = clk;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	ret = ipa_interconnect_init(clock, dev);
287*4882a593Smuzhiyun 	if (ret)
288*4882a593Smuzhiyun 		goto err_kfree;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	mutex_init(&clock->mutex);
291*4882a593Smuzhiyun 	refcount_set(&clock->count, 0);
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	return clock;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun err_kfree:
296*4882a593Smuzhiyun 	kfree(clock);
297*4882a593Smuzhiyun err_clk_put:
298*4882a593Smuzhiyun 	clk_put(clk);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	return ERR_PTR(ret);
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun /* Inverse of ipa_clock_init() */
ipa_clock_exit(struct ipa_clock * clock)304*4882a593Smuzhiyun void ipa_clock_exit(struct ipa_clock *clock)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun 	struct clk *clk = clock->core;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	WARN_ON(refcount_read(&clock->count) != 0);
309*4882a593Smuzhiyun 	mutex_destroy(&clock->mutex);
310*4882a593Smuzhiyun 	ipa_interconnect_exit(clock);
311*4882a593Smuzhiyun 	kfree(clock);
312*4882a593Smuzhiyun 	clk_put(clk);
313*4882a593Smuzhiyun }
314