rk_cru.c revision 1.4 1 1.4 jmcneill /* $NetBSD: rk_cru.c,v 1.4 2018/06/30 17:54:07 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include "opt_soc.h"
30 1.1 jmcneill #include "opt_fdt_arm.h"
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/cdefs.h>
33 1.4 jmcneill __KERNEL_RCSID(0, "$NetBSD: rk_cru.c,v 1.4 2018/06/30 17:54:07 jmcneill Exp $");
34 1.1 jmcneill
35 1.1 jmcneill #include <sys/param.h>
36 1.1 jmcneill #include <sys/bus.h>
37 1.1 jmcneill #include <sys/cpu.h>
38 1.1 jmcneill #include <sys/device.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <dev/fdt/fdtvar.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <dev/clk/clk_backend.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <arm/rockchip/rk_cru.h>
45 1.1 jmcneill
46 1.1 jmcneill #define CRU_SOFTRST_CON0 0x0300
47 1.1 jmcneill
48 1.1 jmcneill static void *
49 1.1 jmcneill rk_cru_reset_acquire(device_t dev, const void *data, size_t len)
50 1.1 jmcneill {
51 1.1 jmcneill if (len != 4)
52 1.1 jmcneill return NULL;
53 1.1 jmcneill
54 1.1 jmcneill return (void *)(uintptr_t)be32dec(data);
55 1.1 jmcneill }
56 1.1 jmcneill
57 1.1 jmcneill static void
58 1.1 jmcneill rk_cru_reset_release(device_t dev, void *priv)
59 1.1 jmcneill {
60 1.1 jmcneill }
61 1.1 jmcneill
62 1.1 jmcneill static int
63 1.1 jmcneill rk_cru_reset_assert(device_t dev, void *priv)
64 1.1 jmcneill {
65 1.1 jmcneill struct rk_cru_softc * const sc = device_private(dev);
66 1.1 jmcneill const uintptr_t reset_id = (uintptr_t)priv;
67 1.2 jmcneill const bus_size_t reg = CRU_SOFTRST_CON0 + (reset_id / 16) * 4;
68 1.2 jmcneill const u_int shift = reset_id % 16;
69 1.1 jmcneill
70 1.2 jmcneill CRU_WRITE(sc, reg, (1 << (shift + 16)) | (1 << shift));
71 1.1 jmcneill
72 1.1 jmcneill return 0;
73 1.1 jmcneill }
74 1.1 jmcneill
75 1.1 jmcneill static int
76 1.1 jmcneill rk_cru_reset_deassert(device_t dev, void *priv)
77 1.1 jmcneill {
78 1.1 jmcneill struct rk_cru_softc * const sc = device_private(dev);
79 1.1 jmcneill const uintptr_t reset_id = (uintptr_t)priv;
80 1.2 jmcneill const bus_size_t reg = CRU_SOFTRST_CON0 + (reset_id / 16) * 4;
81 1.2 jmcneill const u_int shift = reset_id % 16;
82 1.1 jmcneill
83 1.2 jmcneill CRU_WRITE(sc, reg, (1 << (shift + 16)) | (0 << shift));
84 1.1 jmcneill
85 1.1 jmcneill return 0;
86 1.1 jmcneill }
87 1.1 jmcneill
88 1.1 jmcneill static const struct fdtbus_reset_controller_func rk_cru_fdtreset_funcs = {
89 1.1 jmcneill .acquire = rk_cru_reset_acquire,
90 1.1 jmcneill .release = rk_cru_reset_release,
91 1.1 jmcneill .reset_assert = rk_cru_reset_assert,
92 1.1 jmcneill .reset_deassert = rk_cru_reset_deassert,
93 1.1 jmcneill };
94 1.1 jmcneill
95 1.1 jmcneill static struct clk *
96 1.1 jmcneill rk_cru_clock_decode(device_t dev, const void *data, size_t len)
97 1.1 jmcneill {
98 1.1 jmcneill struct rk_cru_softc * const sc = device_private(dev);
99 1.1 jmcneill struct rk_cru_clk *clk;
100 1.1 jmcneill
101 1.1 jmcneill if (len != 4)
102 1.1 jmcneill return NULL;
103 1.1 jmcneill
104 1.1 jmcneill const u_int clock_id = be32dec(data);
105 1.1 jmcneill
106 1.1 jmcneill for (int i = 0; i < sc->sc_nclks; i++) {
107 1.1 jmcneill clk = &sc->sc_clks[i];
108 1.1 jmcneill if (clk->id == clock_id)
109 1.1 jmcneill return &clk->base;
110 1.1 jmcneill }
111 1.1 jmcneill
112 1.1 jmcneill return NULL;
113 1.1 jmcneill }
114 1.1 jmcneill
115 1.1 jmcneill static const struct fdtbus_clock_controller_func rk_cru_fdtclock_funcs = {
116 1.1 jmcneill .decode = rk_cru_clock_decode,
117 1.1 jmcneill };
118 1.1 jmcneill
119 1.1 jmcneill static struct clk *
120 1.1 jmcneill rk_cru_clock_get(void *priv, const char *name)
121 1.1 jmcneill {
122 1.1 jmcneill struct rk_cru_softc * const sc = priv;
123 1.1 jmcneill struct rk_cru_clk *clk;
124 1.1 jmcneill
125 1.1 jmcneill clk = rk_cru_clock_find(sc, name);
126 1.1 jmcneill if (clk == NULL)
127 1.1 jmcneill return NULL;
128 1.1 jmcneill
129 1.1 jmcneill return &clk->base;
130 1.1 jmcneill }
131 1.1 jmcneill
132 1.1 jmcneill static void
133 1.1 jmcneill rk_cru_clock_put(void *priv, struct clk *clk)
134 1.1 jmcneill {
135 1.1 jmcneill }
136 1.1 jmcneill
137 1.1 jmcneill static u_int
138 1.1 jmcneill rk_cru_clock_get_rate(void *priv, struct clk *clkp)
139 1.1 jmcneill {
140 1.1 jmcneill struct rk_cru_softc * const sc = priv;
141 1.1 jmcneill struct rk_cru_clk *clk = (struct rk_cru_clk *)clkp;
142 1.1 jmcneill struct clk *clkp_parent;
143 1.1 jmcneill
144 1.1 jmcneill if (clk->get_rate)
145 1.1 jmcneill return clk->get_rate(sc, clk);
146 1.1 jmcneill
147 1.1 jmcneill clkp_parent = clk_get_parent(clkp);
148 1.1 jmcneill if (clkp_parent == NULL) {
149 1.1 jmcneill aprint_error("%s: no parent for %s\n", __func__, clk->base.name);
150 1.1 jmcneill return 0;
151 1.1 jmcneill }
152 1.1 jmcneill
153 1.1 jmcneill return clk_get_rate(clkp_parent);
154 1.1 jmcneill }
155 1.1 jmcneill
156 1.1 jmcneill static int
157 1.1 jmcneill rk_cru_clock_set_rate(void *priv, struct clk *clkp, u_int rate)
158 1.1 jmcneill {
159 1.1 jmcneill struct rk_cru_softc * const sc = priv;
160 1.1 jmcneill struct rk_cru_clk *clk = (struct rk_cru_clk *)clkp;
161 1.1 jmcneill struct clk *clkp_parent;
162 1.1 jmcneill
163 1.1 jmcneill if (clkp->flags & CLK_SET_RATE_PARENT) {
164 1.1 jmcneill clkp_parent = clk_get_parent(clkp);
165 1.1 jmcneill if (clkp_parent == NULL) {
166 1.1 jmcneill aprint_error("%s: no parent for %s\n", __func__, clk->base.name);
167 1.1 jmcneill return ENXIO;
168 1.1 jmcneill }
169 1.1 jmcneill return clk_set_rate(clkp_parent, rate);
170 1.1 jmcneill }
171 1.1 jmcneill
172 1.1 jmcneill if (clk->set_rate)
173 1.1 jmcneill return clk->set_rate(sc, clk, rate);
174 1.1 jmcneill
175 1.1 jmcneill return ENXIO;
176 1.1 jmcneill }
177 1.1 jmcneill
178 1.1 jmcneill static u_int
179 1.1 jmcneill rk_cru_clock_round_rate(void *priv, struct clk *clkp, u_int rate)
180 1.1 jmcneill {
181 1.1 jmcneill struct rk_cru_softc * const sc = priv;
182 1.1 jmcneill struct rk_cru_clk *clk = (struct rk_cru_clk *)clkp;
183 1.1 jmcneill struct clk *clkp_parent;
184 1.1 jmcneill
185 1.1 jmcneill if (clkp->flags & CLK_SET_RATE_PARENT) {
186 1.1 jmcneill clkp_parent = clk_get_parent(clkp);
187 1.1 jmcneill if (clkp_parent == NULL) {
188 1.1 jmcneill aprint_error("%s: no parent for %s\n", __func__, clk->base.name);
189 1.1 jmcneill return 0;
190 1.1 jmcneill }
191 1.1 jmcneill return clk_round_rate(clkp_parent, rate);
192 1.1 jmcneill }
193 1.1 jmcneill
194 1.1 jmcneill if (clk->round_rate)
195 1.1 jmcneill return clk->round_rate(sc, clk, rate);
196 1.1 jmcneill
197 1.1 jmcneill return 0;
198 1.1 jmcneill }
199 1.1 jmcneill
200 1.1 jmcneill static int
201 1.1 jmcneill rk_cru_clock_enable(void *priv, struct clk *clkp)
202 1.1 jmcneill {
203 1.1 jmcneill struct rk_cru_softc * const sc = priv;
204 1.1 jmcneill struct rk_cru_clk *clk = (struct rk_cru_clk *)clkp;
205 1.1 jmcneill struct clk *clkp_parent;
206 1.1 jmcneill int error = 0;
207 1.1 jmcneill
208 1.1 jmcneill clkp_parent = clk_get_parent(clkp);
209 1.1 jmcneill if (clkp_parent != NULL) {
210 1.1 jmcneill error = clk_enable(clkp_parent);
211 1.1 jmcneill if (error != 0)
212 1.1 jmcneill return error;
213 1.1 jmcneill }
214 1.1 jmcneill
215 1.1 jmcneill if (clk->enable)
216 1.1 jmcneill error = clk->enable(sc, clk, 1);
217 1.1 jmcneill
218 1.1 jmcneill return error;
219 1.1 jmcneill }
220 1.1 jmcneill
221 1.1 jmcneill static int
222 1.1 jmcneill rk_cru_clock_disable(void *priv, struct clk *clkp)
223 1.1 jmcneill {
224 1.1 jmcneill struct rk_cru_softc * const sc = priv;
225 1.1 jmcneill struct rk_cru_clk *clk = (struct rk_cru_clk *)clkp;
226 1.1 jmcneill int error = EINVAL;
227 1.1 jmcneill
228 1.1 jmcneill if (clk->enable)
229 1.1 jmcneill error = clk->enable(sc, clk, 0);
230 1.1 jmcneill
231 1.1 jmcneill return error;
232 1.1 jmcneill }
233 1.1 jmcneill
234 1.1 jmcneill static int
235 1.1 jmcneill rk_cru_clock_set_parent(void *priv, struct clk *clkp,
236 1.1 jmcneill struct clk *clkp_parent)
237 1.1 jmcneill {
238 1.1 jmcneill struct rk_cru_softc * const sc = priv;
239 1.1 jmcneill struct rk_cru_clk *clk = (struct rk_cru_clk *)clkp;
240 1.1 jmcneill
241 1.1 jmcneill if (clk->set_parent == NULL)
242 1.1 jmcneill return EINVAL;
243 1.1 jmcneill
244 1.1 jmcneill return clk->set_parent(sc, clk, clkp_parent->name);
245 1.1 jmcneill }
246 1.1 jmcneill
247 1.1 jmcneill static struct clk *
248 1.1 jmcneill rk_cru_clock_get_parent(void *priv, struct clk *clkp)
249 1.1 jmcneill {
250 1.1 jmcneill struct rk_cru_softc * const sc = priv;
251 1.1 jmcneill struct rk_cru_clk *clk = (struct rk_cru_clk *)clkp;
252 1.1 jmcneill struct rk_cru_clk *clk_parent;
253 1.1 jmcneill const char *parent;
254 1.1 jmcneill
255 1.1 jmcneill if (clk->get_parent == NULL)
256 1.1 jmcneill return NULL;
257 1.1 jmcneill
258 1.1 jmcneill parent = clk->get_parent(sc, clk);
259 1.1 jmcneill if (parent == NULL)
260 1.1 jmcneill return NULL;
261 1.1 jmcneill
262 1.1 jmcneill clk_parent = rk_cru_clock_find(sc, parent);
263 1.1 jmcneill if (clk_parent != NULL)
264 1.1 jmcneill return &clk_parent->base;
265 1.1 jmcneill
266 1.1 jmcneill /* No parent in this domain, try FDT */
267 1.1 jmcneill return fdtbus_clock_byname(parent);
268 1.1 jmcneill }
269 1.1 jmcneill
270 1.1 jmcneill static const struct clk_funcs rk_cru_clock_funcs = {
271 1.1 jmcneill .get = rk_cru_clock_get,
272 1.1 jmcneill .put = rk_cru_clock_put,
273 1.1 jmcneill .get_rate = rk_cru_clock_get_rate,
274 1.1 jmcneill .set_rate = rk_cru_clock_set_rate,
275 1.1 jmcneill .round_rate = rk_cru_clock_round_rate,
276 1.1 jmcneill .enable = rk_cru_clock_enable,
277 1.1 jmcneill .disable = rk_cru_clock_disable,
278 1.1 jmcneill .set_parent = rk_cru_clock_set_parent,
279 1.1 jmcneill .get_parent = rk_cru_clock_get_parent,
280 1.1 jmcneill };
281 1.1 jmcneill
282 1.1 jmcneill struct rk_cru_clk *
283 1.1 jmcneill rk_cru_clock_find(struct rk_cru_softc *sc, const char *name)
284 1.1 jmcneill {
285 1.1 jmcneill for (int i = 0; i < sc->sc_nclks; i++) {
286 1.1 jmcneill if (sc->sc_clks[i].base.name == NULL)
287 1.1 jmcneill continue;
288 1.1 jmcneill if (strcmp(sc->sc_clks[i].base.name, name) == 0)
289 1.1 jmcneill return &sc->sc_clks[i];
290 1.1 jmcneill }
291 1.1 jmcneill
292 1.1 jmcneill return NULL;
293 1.1 jmcneill }
294 1.1 jmcneill
295 1.1 jmcneill int
296 1.1 jmcneill rk_cru_attach(struct rk_cru_softc *sc)
297 1.1 jmcneill {
298 1.1 jmcneill bus_addr_t addr;
299 1.1 jmcneill bus_size_t size;
300 1.1 jmcneill int i;
301 1.1 jmcneill
302 1.1 jmcneill if (of_hasprop(sc->sc_phandle, "rockchip,grf")) {
303 1.4 jmcneill sc->sc_grf = fdtbus_syscon_acquire(sc->sc_phandle, "rockchip,grf");
304 1.4 jmcneill if (sc->sc_grf == NULL) {
305 1.4 jmcneill aprint_error(": couldn't get grf syscon\n");
306 1.1 jmcneill return ENXIO;
307 1.1 jmcneill }
308 1.1 jmcneill }
309 1.1 jmcneill
310 1.1 jmcneill if (fdtbus_get_reg(sc->sc_phandle, 0, &addr, &size) != 0) {
311 1.1 jmcneill aprint_error(": couldn't get registers\n");
312 1.1 jmcneill return ENXIO;
313 1.1 jmcneill }
314 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
315 1.1 jmcneill aprint_error(": couldn't map registers\n");
316 1.1 jmcneill return ENXIO;
317 1.1 jmcneill }
318 1.1 jmcneill
319 1.1 jmcneill sc->sc_clkdom.name = device_xname(sc->sc_dev);
320 1.1 jmcneill sc->sc_clkdom.funcs = &rk_cru_clock_funcs;
321 1.1 jmcneill sc->sc_clkdom.priv = sc;
322 1.1 jmcneill for (i = 0; i < sc->sc_nclks; i++) {
323 1.1 jmcneill sc->sc_clks[i].base.domain = &sc->sc_clkdom;
324 1.1 jmcneill clk_attach(&sc->sc_clks[i].base);
325 1.1 jmcneill }
326 1.1 jmcneill
327 1.1 jmcneill fdtbus_register_clock_controller(sc->sc_dev, sc->sc_phandle,
328 1.1 jmcneill &rk_cru_fdtclock_funcs);
329 1.1 jmcneill
330 1.1 jmcneill fdtbus_register_reset_controller(sc->sc_dev, sc->sc_phandle,
331 1.1 jmcneill &rk_cru_fdtreset_funcs);
332 1.1 jmcneill
333 1.1 jmcneill return 0;
334 1.1 jmcneill }
335 1.1 jmcneill
336 1.1 jmcneill void
337 1.1 jmcneill rk_cru_print(struct rk_cru_softc *sc)
338 1.1 jmcneill {
339 1.1 jmcneill struct rk_cru_clk *clk;
340 1.1 jmcneill struct clk *clkp_parent;
341 1.1 jmcneill const char *type;
342 1.1 jmcneill int i;
343 1.1 jmcneill
344 1.1 jmcneill for (i = 0; i < sc->sc_nclks; i++) {
345 1.1 jmcneill clk = &sc->sc_clks[i];
346 1.1 jmcneill if (clk->type == RK_CRU_UNKNOWN)
347 1.1 jmcneill continue;
348 1.1 jmcneill
349 1.1 jmcneill clkp_parent = clk_get_parent(&clk->base);
350 1.1 jmcneill
351 1.1 jmcneill switch (clk->type) {
352 1.1 jmcneill case RK_CRU_PLL: type = "pll"; break;
353 1.1 jmcneill case RK_CRU_ARM: type = "arm"; break;
354 1.1 jmcneill case RK_CRU_COMPOSITE: type = "comp"; break;
355 1.1 jmcneill case RK_CRU_GATE: type = "gate"; break;
356 1.1 jmcneill case RK_CRU_MUX: type = "mux"; break;
357 1.1 jmcneill default: type = "???"; break;
358 1.1 jmcneill }
359 1.1 jmcneill
360 1.3 jmcneill aprint_debug_dev(sc->sc_dev,
361 1.1 jmcneill "%3d %-14s %2s %-14s %-7s ",
362 1.1 jmcneill clk->id,
363 1.1 jmcneill clk->base.name,
364 1.1 jmcneill clkp_parent ? "<-" : "",
365 1.1 jmcneill clkp_parent ? clkp_parent->name : "",
366 1.1 jmcneill type);
367 1.3 jmcneill aprint_debug("%10d Hz\n", clk_get_rate(&clk->base));
368 1.1 jmcneill }
369 1.1 jmcneill }
370