1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2018 Emmanuel Vadot <manu@freebsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: stable/12/sys/arm64/rockchip/clk/rk_clk_composite.c 364938 2020-08-28 20:25:03Z gonzo $
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: stable/12/sys/arm64/rockchip/clk/rk_clk_composite.c 364938 2020-08-28 20:25:03Z gonzo $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37
38 #include <dev/extres/clk/clk.h>
39 #include <dev/extres/syscon/syscon.h>
40
41 #include <arm64/rockchip/clk/rk_clk_composite.h>
42
43 #include "clkdev_if.h"
44 #include "syscon_if.h"
45
46 struct rk_clk_composite_sc {
47 uint32_t muxdiv_offset;
48 uint32_t mux_shift;
49 uint32_t mux_width;
50 uint32_t mux_mask;
51
52 uint32_t div_shift;
53 uint32_t div_width;
54 uint32_t div_mask;
55
56 uint32_t gate_offset;
57 uint32_t gate_shift;
58
59 uint32_t flags;
60
61 struct syscon *grf;
62 };
63
64 #define WRITE4(_clk, off, val) \
65 rk_clk_composite_write_4(_clk, off, val)
66 #define READ4(_clk, off, val) \
67 rk_clk_composite_read_4(_clk, off, val)
68 #define DEVICE_LOCK(_clk) \
69 CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))
70 #define DEVICE_UNLOCK(_clk) \
71 CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))
72
73 #define RK_CLK_COMPOSITE_MASK_SHIFT 16
74
75 #if 0
76 #define dprintf(format, arg...) \
77 printf("%s:(%s)" format, __func__, clknode_get_name(clk), arg)
78 #else
79 #define dprintf(format, arg...)
80 #endif
81
82 static void
rk_clk_composite_read_4(struct clknode * clk,bus_addr_t addr,uint32_t * val)83 rk_clk_composite_read_4(struct clknode *clk, bus_addr_t addr, uint32_t *val)
84 {
85 struct rk_clk_composite_sc *sc;
86
87 sc = clknode_get_softc(clk);
88 if (sc->grf)
89 *val = SYSCON_READ_4(sc->grf, addr);
90 else
91 CLKDEV_READ_4(clknode_get_device(clk), addr, val);
92 }
93
94 static void
rk_clk_composite_write_4(struct clknode * clk,bus_addr_t addr,uint32_t val)95 rk_clk_composite_write_4(struct clknode *clk, bus_addr_t addr, uint32_t val)
96 {
97 struct rk_clk_composite_sc *sc;
98
99 sc = clknode_get_softc(clk);
100 if (sc->grf)
101 SYSCON_WRITE_4(sc->grf, addr, val | (0xffff << 16));
102 else
103 CLKDEV_WRITE_4(clknode_get_device(clk), addr, val);
104 }
105
106 static struct syscon *
rk_clk_composite_get_grf(struct clknode * clk)107 rk_clk_composite_get_grf(struct clknode *clk)
108 {
109 device_t dev;
110 phandle_t node;
111 struct syscon *grf;
112
113 grf = NULL;
114 dev = clknode_get_device(clk);
115 node = ofw_bus_get_node(dev);
116 if (OF_hasprop(node, "rockchip,grf") &&
117 syscon_get_by_ofw_property(dev, node,
118 "rockchip,grf", &grf) != 0) {
119 return (NULL);
120 }
121
122 return (grf);
123 }
124
125 static int
rk_clk_composite_init(struct clknode * clk,device_t dev)126 rk_clk_composite_init(struct clknode *clk, device_t dev)
127 {
128 struct rk_clk_composite_sc *sc;
129 uint32_t val, idx;
130
131 sc = clknode_get_softc(clk);
132 if ((sc->flags & RK_CLK_COMPOSITE_GRF) != 0) {
133 sc->grf = rk_clk_composite_get_grf(clk);
134 if (sc->grf == NULL)
135 panic("clock %s has GRF flag set but no syscon is available",
136 clknode_get_name(clk));
137 }
138
139 idx = 0;
140 if ((sc->flags & RK_CLK_COMPOSITE_HAVE_MUX) != 0) {
141 DEVICE_LOCK(clk);
142 READ4(clk, sc->muxdiv_offset, &val);
143 DEVICE_UNLOCK(clk);
144
145 idx = (val & sc->mux_mask) >> sc->mux_shift;
146 }
147
148 clknode_init_parent_idx(clk, idx);
149
150 return (0);
151 }
152
153 static int
rk_clk_composite_set_gate(struct clknode * clk,bool enable)154 rk_clk_composite_set_gate(struct clknode *clk, bool enable)
155 {
156 struct rk_clk_composite_sc *sc;
157 uint32_t val = 0;
158
159 sc = clknode_get_softc(clk);
160
161 if ((sc->flags & RK_CLK_COMPOSITE_HAVE_GATE) == 0)
162 return (0);
163
164 dprintf("%sabling gate\n", enable ? "En" : "Dis");
165 if (!enable)
166 val |= 1 << sc->gate_shift;
167 dprintf("sc->gate_shift: %x\n", sc->gate_shift);
168 val |= (1 << sc->gate_shift) << RK_CLK_COMPOSITE_MASK_SHIFT;
169 dprintf("Write: gate_offset=%x, val=%x\n", sc->gate_offset, val);
170 DEVICE_LOCK(clk);
171 WRITE4(clk, sc->gate_offset, val);
172 DEVICE_UNLOCK(clk);
173
174 return (0);
175 }
176
177 static int
rk_clk_composite_set_mux(struct clknode * clk,int index)178 rk_clk_composite_set_mux(struct clknode *clk, int index)
179 {
180 struct rk_clk_composite_sc *sc;
181 uint32_t val = 0;
182
183 sc = clknode_get_softc(clk);
184
185 if ((sc->flags & RK_CLK_COMPOSITE_HAVE_MUX) == 0)
186 return (0);
187
188 dprintf("Set mux to %d\n", index);
189 DEVICE_LOCK(clk);
190 val |= (index << sc->mux_shift);
191 val |= sc->mux_mask << RK_CLK_COMPOSITE_MASK_SHIFT;
192 dprintf("Write: muxdiv_offset=%x, val=%x\n", sc->muxdiv_offset, val);
193 WRITE4(clk, sc->muxdiv_offset, val);
194 DEVICE_UNLOCK(clk);
195
196 return (0);
197 }
198
199 static int
rk_clk_composite_recalc(struct clknode * clk,uint64_t * freq)200 rk_clk_composite_recalc(struct clknode *clk, uint64_t *freq)
201 {
202 struct rk_clk_composite_sc *sc;
203 uint32_t reg, div;
204
205 sc = clknode_get_softc(clk);
206
207 DEVICE_LOCK(clk);
208
209 READ4(clk, sc->muxdiv_offset, ®);
210 dprintf("Read: muxdiv_offset=%x, val=%x\n", sc->muxdiv_offset, reg);
211
212 DEVICE_UNLOCK(clk);
213
214 div = ((reg & sc->div_mask) >> sc->div_shift);
215 if (sc->flags & RK_CLK_COMPOSITE_DIV_EXP)
216 div = 1 << div;
217 else
218 div += 1;
219 dprintf("parent_freq=%ju, div=%u\n", *freq, div);
220 *freq = *freq / div;
221 dprintf("Final freq=%ju\n", *freq);
222 return (0);
223 }
224
225 static uint32_t
rk_clk_composite_find_best(struct rk_clk_composite_sc * sc,uint64_t fparent,uint64_t freq,uint32_t * reg)226 rk_clk_composite_find_best(struct rk_clk_composite_sc *sc, uint64_t fparent,
227 uint64_t freq, uint32_t *reg)
228 {
229 uint64_t best, cur;
230 uint32_t best_div, best_div_reg;
231 uint32_t div, div_reg;
232
233 best = 0;
234 best_div = 0;
235 best_div_reg = 0;
236
237 for (div_reg = 0; div_reg <= ((sc->div_mask >> sc->div_shift) + 1);
238 div_reg++) {
239 if (sc->flags == RK_CLK_COMPOSITE_DIV_EXP)
240 div = 1 << div_reg;
241 else
242 div = div_reg + 1;
243 cur = fparent / div;
244 if ((freq - cur) < (freq - best)) {
245 best = cur;
246 best_div = div;
247 best_div_reg = div_reg;
248 break;
249 }
250 }
251 *reg = div_reg;
252 return (best_div);
253 }
254
255 static int
rk_clk_composite_set_freq(struct clknode * clk,uint64_t fparent,uint64_t * fout,int flags,int * stop)256 rk_clk_composite_set_freq(struct clknode *clk, uint64_t fparent, uint64_t *fout,
257 int flags, int *stop)
258 {
259 struct rk_clk_composite_sc *sc;
260 struct clknode *p_clk;
261 const char **p_names;
262 uint64_t best, cur;
263 uint32_t div, div_reg, best_div, best_div_reg, val;
264 int p_idx, best_parent;
265
266 sc = clknode_get_softc(clk);
267 dprintf("Finding best parent/div for target freq of %ju\n", *fout);
268 p_names = clknode_get_parent_names(clk);
269 for (best_div = 0, best = 0, p_idx = 0;
270 p_idx != clknode_get_parents_num(clk); p_idx++) {
271 p_clk = clknode_find_by_name(p_names[p_idx]);
272 clknode_get_freq(p_clk, &fparent);
273 dprintf("Testing with parent %s (%d) at freq %ju\n",
274 clknode_get_name(p_clk), p_idx, fparent);
275 div = rk_clk_composite_find_best(sc, fparent, *fout, &div_reg);
276 cur = fparent / div;
277 if ((*fout - cur) < (*fout - best)) {
278 best = cur;
279 best_div = div;
280 best_div_reg = div_reg;
281 best_parent = p_idx;
282 dprintf("Best parent so far %s (%d) with best freq at "
283 "%ju\n", clknode_get_name(p_clk), p_idx, best);
284 }
285 }
286
287 *stop = 1;
288 if (best_div == 0)
289 return (ERANGE);
290
291 if ((best < *fout) && ((flags & CLK_SET_ROUND_DOWN) == 0))
292 return (ERANGE);
293
294 if ((best > *fout) && ((flags & CLK_SET_ROUND_UP) == 0)) {
295 return (ERANGE);
296 }
297
298 if ((flags & CLK_SET_DRYRUN) != 0) {
299 *fout = best;
300 return (0);
301 }
302
303 p_idx = clknode_get_parent_idx(clk);
304 if (p_idx != best_parent) {
305 dprintf("Switching parent index from %d to %d\n", p_idx,
306 best_parent);
307 clknode_set_parent_by_idx(clk, best_parent);
308 }
309
310 dprintf("Setting divider to %d (reg: %d)\n", best_div, best_div_reg);
311 dprintf(" div_mask: 0x%X, div_shift: %d\n", sc->div_mask,
312 sc->div_shift);
313
314 DEVICE_LOCK(clk);
315 val = best_div_reg << sc->div_shift;
316 val |= sc->div_mask << RK_CLK_COMPOSITE_MASK_SHIFT;
317 dprintf("Write: muxdiv_offset=%x, val=%x\n", sc->muxdiv_offset, val);
318 WRITE4(clk, sc->muxdiv_offset, val);
319 DEVICE_UNLOCK(clk);
320
321 *fout = best;
322 return (0);
323 }
324
325 static clknode_method_t rk_clk_composite_clknode_methods[] = {
326 /* Device interface */
327 CLKNODEMETHOD(clknode_init, rk_clk_composite_init),
328 CLKNODEMETHOD(clknode_set_gate, rk_clk_composite_set_gate),
329 CLKNODEMETHOD(clknode_set_mux, rk_clk_composite_set_mux),
330 CLKNODEMETHOD(clknode_recalc_freq, rk_clk_composite_recalc),
331 CLKNODEMETHOD(clknode_set_freq, rk_clk_composite_set_freq),
332 CLKNODEMETHOD_END
333 };
334
335 DEFINE_CLASS_1(rk_clk_composite_clknode, rk_clk_composite_clknode_class,
336 rk_clk_composite_clknode_methods, sizeof(struct rk_clk_composite_sc),
337 clknode_class);
338
339 int
rk_clk_composite_register(struct clkdom * clkdom,struct rk_clk_composite_def * clkdef)340 rk_clk_composite_register(struct clkdom *clkdom,
341 struct rk_clk_composite_def *clkdef)
342 {
343 struct clknode *clk;
344 struct rk_clk_composite_sc *sc;
345
346 clk = clknode_create(clkdom, &rk_clk_composite_clknode_class,
347 &clkdef->clkdef);
348 if (clk == NULL)
349 return (1);
350
351 sc = clknode_get_softc(clk);
352
353 sc->muxdiv_offset = clkdef->muxdiv_offset;
354
355 sc->mux_shift = clkdef->mux_shift;
356 sc->mux_width = clkdef->mux_width;
357 sc->mux_mask = ((1 << clkdef->mux_width) - 1) << sc->mux_shift;
358
359 sc->div_shift = clkdef->div_shift;
360 sc->div_width = clkdef->div_width;
361 sc->div_mask = ((1 << clkdef->div_width) - 1) << sc->div_shift;
362
363 sc->gate_offset = clkdef->gate_offset;
364 sc->gate_shift = clkdef->gate_shift;
365
366 sc->flags = clkdef->flags;
367
368 clknode_register(clkdom, clk);
369
370 return (0);
371 }
372