• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2016 Maxime Ripard
4  * Maxime Ripard <maxime.ripard@free-electrons.com>
5  */
6 
7 #include <linux/clk-provider.h>
8 #include <linux/io.h>
9 
10 #include "ccu_gate.h"
11 #include "ccu_mp.h"
12 
ccu_mp_find_best(unsigned long parent,unsigned long rate,unsigned int max_m,unsigned int max_p,unsigned int * m,unsigned int * p)13 static void ccu_mp_find_best(unsigned long parent, unsigned long rate,
14 			     unsigned int max_m, unsigned int max_p,
15 			     unsigned int *m, unsigned int *p)
16 {
17 	unsigned long best_rate = 0;
18 	unsigned int best_m = 0, best_p = 0;
19 	unsigned int _m, _p;
20 
21 	for (_p = 1; _p <= max_p; _p <<= 1) {
22 		for (_m = 1; _m <= max_m; _m++) {
23 			unsigned long tmp_rate = parent / _p / _m;
24 
25 			if (tmp_rate > rate)
26 				continue;
27 
28 			if ((rate - tmp_rate) < (rate - best_rate)) {
29 				best_rate = tmp_rate;
30 				best_m = _m;
31 				best_p = _p;
32 			}
33 		}
34 	}
35 
36 	*m = best_m;
37 	*p = best_p;
38 }
39 
ccu_mp_find_best_with_parent_adj(struct clk_hw * hw,unsigned long * parent,unsigned long rate,unsigned int max_m,unsigned int max_p)40 static unsigned long ccu_mp_find_best_with_parent_adj(struct clk_hw *hw,
41 						      unsigned long *parent,
42 						      unsigned long rate,
43 						      unsigned int max_m,
44 						      unsigned int max_p)
45 {
46 	unsigned long parent_rate_saved;
47 	unsigned long parent_rate, now;
48 	unsigned long best_rate = 0;
49 	unsigned int _m, _p, div;
50 	unsigned long maxdiv;
51 
52 	parent_rate_saved = *parent;
53 
54 	/*
55 	 * The maximum divider we can use without overflowing
56 	 * unsigned long in rate * m * p below
57 	 */
58 	maxdiv = max_m * max_p;
59 	maxdiv = min(ULONG_MAX / rate, maxdiv);
60 
61 	for (_p = 1; _p <= max_p; _p <<= 1) {
62 		for (_m = 1; _m <= max_m; _m++) {
63 			div = _m * _p;
64 
65 			if (div > maxdiv)
66 				break;
67 
68 			if (rate * div == parent_rate_saved) {
69 				/*
70 				 * It's the most ideal case if the requested
71 				 * rate can be divided from parent clock without
72 				 * needing to change parent rate, so return the
73 				 * divider immediately.
74 				 */
75 				*parent = parent_rate_saved;
76 				return rate;
77 			}
78 
79 			parent_rate = clk_hw_round_rate(hw, rate * div);
80 			now = parent_rate / div;
81 
82 			if (now <= rate && now > best_rate) {
83 				best_rate = now;
84 				*parent = parent_rate;
85 
86 				if (now == rate)
87 					return rate;
88 			}
89 		}
90 	}
91 
92 	return best_rate;
93 }
94 
ccu_mp_round_rate(struct ccu_mux_internal * mux,struct clk_hw * hw,unsigned long * parent_rate,unsigned long rate,void * data)95 static unsigned long ccu_mp_round_rate(struct ccu_mux_internal *mux,
96 				       struct clk_hw *hw,
97 				       unsigned long *parent_rate,
98 				       unsigned long rate,
99 				       void *data)
100 {
101 	struct ccu_mp *cmp = data;
102 	unsigned int max_m, max_p;
103 	unsigned int m, p;
104 
105 	if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
106 		rate *= cmp->fixed_post_div;
107 
108 	max_m = cmp->m.max ?: 1 << cmp->m.width;
109 	max_p = cmp->p.max ?: 1 << ((1 << cmp->p.width) - 1);
110 	/*
111 	 * When mp-clk use the clk_round_rate, the clk shouldn't recal
112 	 * parent's rate unless this clk set CLK_SET_RATE_PARENT flag.
113 	 */
114 	if (!(clk_hw_get_flags(&cmp->common.hw) & CLK_SET_RATE_PARENT)) {
115 		ccu_mp_find_best(*parent_rate, rate, max_m, max_p, &m, &p);
116 		rate = *parent_rate / p / m;
117 	} else {
118 		rate = ccu_mp_find_best_with_parent_adj(hw, parent_rate, rate,
119 							max_m, max_p);
120 	}
121 
122 	if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
123 		rate /= cmp->fixed_post_div;
124 
125 	return rate;
126 }
127 
ccu_mp_disable(struct clk_hw * hw)128 static void ccu_mp_disable(struct clk_hw *hw)
129 {
130 	struct ccu_mp *cmp = hw_to_ccu_mp(hw);
131 
132 	return ccu_gate_helper_disable(&cmp->common, cmp->enable);
133 }
134 
ccu_mp_enable(struct clk_hw * hw)135 static int ccu_mp_enable(struct clk_hw *hw)
136 {
137 	struct ccu_mp *cmp = hw_to_ccu_mp(hw);
138 
139 	return ccu_gate_helper_enable(&cmp->common, cmp->enable);
140 }
141 
ccu_mp_is_enabled(struct clk_hw * hw)142 static int ccu_mp_is_enabled(struct clk_hw *hw)
143 {
144 	struct ccu_mp *cmp = hw_to_ccu_mp(hw);
145 
146 	return ccu_gate_helper_is_enabled(&cmp->common, cmp->enable);
147 }
148 
ccu_mp_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)149 static unsigned long ccu_mp_recalc_rate(struct clk_hw *hw,
150 					unsigned long parent_rate)
151 {
152 	struct ccu_mp *cmp = hw_to_ccu_mp(hw);
153 	unsigned long rate;
154 	unsigned int m, p;
155 	u32 reg;
156 
157 	/* Adjust parent_rate according to pre-dividers */
158 	parent_rate = ccu_mux_helper_apply_prediv(&cmp->common, &cmp->mux, -1,
159 						  parent_rate);
160 
161 	reg = readl(cmp->common.base + cmp->common.reg);
162 
163 	m = reg >> cmp->m.shift;
164 	m &= (1 << cmp->m.width) - 1;
165 	m += cmp->m.offset;
166 	if (!m)
167 		m++;
168 
169 	p = reg >> cmp->p.shift;
170 	p &= (1 << cmp->p.width) - 1;
171 
172 	if (unlikely(cmp->common.features & CCU_FEATURE_MP_NO_INDEX_MODE)) {
173 		p += cmp->p.offset;
174 		if (!p)
175 			p++;
176 		rate = (parent_rate / p) / m;
177 	} else {
178 		rate = (parent_rate >> p) / m;
179 	}
180 	if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
181 		rate /= cmp->fixed_post_div;
182 
183 	return rate;
184 }
185 
ccu_mp_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)186 static int ccu_mp_determine_rate(struct clk_hw *hw,
187 				 struct clk_rate_request *req)
188 {
189 	struct ccu_mp *cmp = hw_to_ccu_mp(hw);
190 
191 	return ccu_mux_helper_determine_rate(&cmp->common, &cmp->mux,
192 					     req, ccu_mp_round_rate, cmp);
193 }
194 
ccu_mp_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)195 static int ccu_mp_set_rate(struct clk_hw *hw, unsigned long rate,
196 			   unsigned long parent_rate)
197 {
198 	struct ccu_mp *cmp = hw_to_ccu_mp(hw);
199 	unsigned long flags;
200 	unsigned int max_m, max_p;
201 	unsigned int m, p;
202 	u32 reg;
203 
204 	/* Adjust parent_rate according to pre-dividers */
205 	parent_rate = ccu_mux_helper_apply_prediv(&cmp->common, &cmp->mux, -1,
206 						  parent_rate);
207 
208 	max_m = cmp->m.max ?: 1 << cmp->m.width;
209 
210 	if (unlikely(cmp->common.features & CCU_FEATURE_MP_NO_INDEX_MODE))
211 		max_p = cmp->p.max ?: 1 << cmp->p.width;
212 	else
213 		max_p = cmp->p.max ?: 1 << ((1 << cmp->p.width) - 1);
214 
215 
216 	/* Adjust target rate according to post-dividers */
217 	if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
218 		rate = rate * cmp->fixed_post_div;
219 
220 	ccu_mp_find_best(parent_rate, rate, max_m, max_p, &m, &p);
221 
222 	spin_lock_irqsave(cmp->common.lock, flags);
223 
224 	reg = readl(cmp->common.base + cmp->common.reg);
225 	reg &= ~GENMASK(cmp->m.width + cmp->m.shift - 1, cmp->m.shift);
226 	reg &= ~GENMASK(cmp->p.width + cmp->p.shift - 1, cmp->p.shift);
227 	reg |= (m - cmp->m.offset) << cmp->m.shift;
228 
229 	if (unlikely(cmp->common.features & CCU_FEATURE_MP_NO_INDEX_MODE))
230 		reg |= (p - cmp->p.offset) << cmp->p.shift;
231 	else
232 		reg |= ilog2(p) << cmp->p.shift;
233 
234 	writel(reg, cmp->common.base + cmp->common.reg);
235 
236 	spin_unlock_irqrestore(cmp->common.lock, flags);
237 
238 	return 0;
239 }
240 
ccu_mp_get_parent(struct clk_hw * hw)241 static u8 ccu_mp_get_parent(struct clk_hw *hw)
242 {
243 	struct ccu_mp *cmp = hw_to_ccu_mp(hw);
244 
245 	return ccu_mux_helper_get_parent(&cmp->common, &cmp->mux);
246 }
247 
ccu_mp_set_parent(struct clk_hw * hw,u8 index)248 static int ccu_mp_set_parent(struct clk_hw *hw, u8 index)
249 {
250 	struct ccu_mp *cmp = hw_to_ccu_mp(hw);
251 
252 	return ccu_mux_helper_set_parent(&cmp->common, &cmp->mux, index);
253 }
254 
255 const struct clk_ops ccu_mp_ops = {
256 	.disable	= ccu_mp_disable,
257 	.enable		= ccu_mp_enable,
258 	.is_enabled	= ccu_mp_is_enabled,
259 
260 	.get_parent	= ccu_mp_get_parent,
261 	.set_parent	= ccu_mp_set_parent,
262 
263 	.determine_rate	= ccu_mp_determine_rate,
264 	.recalc_rate	= ccu_mp_recalc_rate,
265 	.set_rate	= ccu_mp_set_rate,
266 };
267 EXPORT_SYMBOL_GPL(ccu_mp_ops);
268 
269 /*
270  * Support for MMC timing mode switching
271  *
272  * The MMC clocks on some SoCs support switching between old and
273  * new timing modes. A platform specific API is provided to query
274  * and set the timing mode on supported SoCs.
275  *
276  * In addition, a special class of ccu_mp_ops is provided, which
277  * takes in to account the timing mode switch. When the new timing
278  * mode is active, the clock output rate is halved. This new class
279  * is a wrapper around the generic ccu_mp_ops. When clock rates
280  * are passed through to ccu_mp_ops callbacks, they are doubled
281  * if the new timing mode bit is set, to account for the post
282  * divider. Conversely, when clock rates are passed back, they
283  * are halved if the mode bit is set.
284  */
285 
ccu_mp_mmc_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)286 static unsigned long ccu_mp_mmc_recalc_rate(struct clk_hw *hw,
287 					    unsigned long parent_rate)
288 {
289 	unsigned long rate = ccu_mp_recalc_rate(hw, parent_rate);
290 	struct ccu_common *cm = hw_to_ccu_common(hw);
291 	u32 val = readl(cm->base + cm->reg);
292 
293 	if (val & CCU_MMC_NEW_TIMING_MODE)
294 		return rate / 2;
295 	return rate;
296 }
297 
ccu_mp_mmc_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)298 static int ccu_mp_mmc_determine_rate(struct clk_hw *hw,
299 				     struct clk_rate_request *req)
300 {
301 	struct ccu_common *cm = hw_to_ccu_common(hw);
302 	u32 val = readl(cm->base + cm->reg);
303 	int ret;
304 
305 	/* adjust the requested clock rate */
306 	if (val & CCU_MMC_NEW_TIMING_MODE) {
307 		req->rate *= 2;
308 		req->min_rate *= 2;
309 		req->max_rate *= 2;
310 	}
311 
312 	ret = ccu_mp_determine_rate(hw, req);
313 
314 	/* re-adjust the requested clock rate back */
315 	if (val & CCU_MMC_NEW_TIMING_MODE) {
316 		req->rate /= 2;
317 		req->min_rate /= 2;
318 		req->max_rate /= 2;
319 	}
320 
321 	return ret;
322 }
323 
ccu_mp_mmc_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)324 static int ccu_mp_mmc_set_rate(struct clk_hw *hw, unsigned long rate,
325 			       unsigned long parent_rate)
326 {
327 	struct ccu_common *cm = hw_to_ccu_common(hw);
328 	u32 val = readl(cm->base + cm->reg);
329 
330 	if (val & CCU_MMC_NEW_TIMING_MODE)
331 		rate *= 2;
332 
333 	return ccu_mp_set_rate(hw, rate, parent_rate);
334 }
335 
336 const struct clk_ops ccu_mp_mmc_ops = {
337 	.disable	= ccu_mp_disable,
338 	.enable		= ccu_mp_enable,
339 	.is_enabled	= ccu_mp_is_enabled,
340 
341 	.get_parent	= ccu_mp_get_parent,
342 	.set_parent	= ccu_mp_set_parent,
343 
344 	.determine_rate	= ccu_mp_mmc_determine_rate,
345 	.recalc_rate	= ccu_mp_mmc_recalc_rate,
346 	.set_rate	= ccu_mp_mmc_set_rate,
347 };
348