imx_ccm.c revision 1.1.2.2 1 1.1.2.2 thorpej /* $NetBSD: imx_ccm.c,v 1.1.2.2 2021/01/03 16:34:52 thorpej Exp $ */
2 1.1.2.2 thorpej
3 1.1.2.2 thorpej /*-
4 1.1.2.2 thorpej * Copyright (c) 2020 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1.2.2 thorpej * All rights reserved.
6 1.1.2.2 thorpej *
7 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
9 1.1.2.2 thorpej * are met:
10 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
15 1.1.2.2 thorpej *
16 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.2.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.2.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.2.2 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.2.2 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1.2.2 thorpej * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1.2.2 thorpej * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1.2.2 thorpej * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1.2.2 thorpej * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.2.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.2.2 thorpej * SUCH DAMAGE.
27 1.1.2.2 thorpej */
28 1.1.2.2 thorpej
29 1.1.2.2 thorpej #include <sys/cdefs.h>
30 1.1.2.2 thorpej __KERNEL_RCSID(0, "$NetBSD: imx_ccm.c,v 1.1.2.2 2021/01/03 16:34:52 thorpej Exp $");
31 1.1.2.2 thorpej
32 1.1.2.2 thorpej #include <sys/param.h>
33 1.1.2.2 thorpej #include <sys/bus.h>
34 1.1.2.2 thorpej #include <sys/cpu.h>
35 1.1.2.2 thorpej #include <sys/device.h>
36 1.1.2.2 thorpej
37 1.1.2.2 thorpej #include <dev/fdt/fdtvar.h>
38 1.1.2.2 thorpej
39 1.1.2.2 thorpej #include <dev/clk/clk_backend.h>
40 1.1.2.2 thorpej
41 1.1.2.2 thorpej #include <arm/nxp/imx_ccm.h>
42 1.1.2.2 thorpej
43 1.1.2.2 thorpej static struct clk *
44 1.1.2.2 thorpej imx_ccm_clock_decode(device_t dev, int cc_phandle, const void *data, size_t len)
45 1.1.2.2 thorpej {
46 1.1.2.2 thorpej struct imx_ccm_softc * const sc = device_private(dev);
47 1.1.2.2 thorpej struct imx_ccm_clk *clk;
48 1.1.2.2 thorpej
49 1.1.2.2 thorpej if (len != 4)
50 1.1.2.2 thorpej return NULL;
51 1.1.2.2 thorpej
52 1.1.2.2 thorpej const u_int clock_id = be32dec(data);
53 1.1.2.2 thorpej
54 1.1.2.2 thorpej for (int i = 0; i < sc->sc_nclks; i++) {
55 1.1.2.2 thorpej clk = &sc->sc_clks[i];
56 1.1.2.2 thorpej if (clk->id == clock_id)
57 1.1.2.2 thorpej return &clk->base;
58 1.1.2.2 thorpej }
59 1.1.2.2 thorpej
60 1.1.2.2 thorpej return NULL;
61 1.1.2.2 thorpej }
62 1.1.2.2 thorpej
63 1.1.2.2 thorpej static const struct fdtbus_clock_controller_func imx_ccm_fdtclock_funcs = {
64 1.1.2.2 thorpej .decode = imx_ccm_clock_decode,
65 1.1.2.2 thorpej };
66 1.1.2.2 thorpej
67 1.1.2.2 thorpej static struct clk *
68 1.1.2.2 thorpej imx_ccm_clock_get(void *priv, const char *name)
69 1.1.2.2 thorpej {
70 1.1.2.2 thorpej struct imx_ccm_softc * const sc = priv;
71 1.1.2.2 thorpej struct imx_ccm_clk *clk;
72 1.1.2.2 thorpej
73 1.1.2.2 thorpej clk = imx_ccm_clock_find(sc, name);
74 1.1.2.2 thorpej if (clk == NULL)
75 1.1.2.2 thorpej return NULL;
76 1.1.2.2 thorpej
77 1.1.2.2 thorpej return &clk->base;
78 1.1.2.2 thorpej }
79 1.1.2.2 thorpej
80 1.1.2.2 thorpej static void
81 1.1.2.2 thorpej imx_ccm_clock_put(void *priv, struct clk *clk)
82 1.1.2.2 thorpej {
83 1.1.2.2 thorpej }
84 1.1.2.2 thorpej
85 1.1.2.2 thorpej static u_int
86 1.1.2.2 thorpej imx_ccm_clock_get_rate(void *priv, struct clk *clkp)
87 1.1.2.2 thorpej {
88 1.1.2.2 thorpej struct imx_ccm_softc * const sc = priv;
89 1.1.2.2 thorpej struct imx_ccm_clk *clk = (struct imx_ccm_clk *)clkp;
90 1.1.2.2 thorpej struct clk *clkp_parent;
91 1.1.2.2 thorpej
92 1.1.2.2 thorpej if (clk->get_rate)
93 1.1.2.2 thorpej return clk->get_rate(sc, clk);
94 1.1.2.2 thorpej
95 1.1.2.2 thorpej clkp_parent = clk_get_parent(clkp);
96 1.1.2.2 thorpej if (clkp_parent == NULL) {
97 1.1.2.2 thorpej aprint_debug("%s: no parent for %s\n", __func__, clk->base.name);
98 1.1.2.2 thorpej return 0;
99 1.1.2.2 thorpej }
100 1.1.2.2 thorpej
101 1.1.2.2 thorpej return clk_get_rate(clkp_parent);
102 1.1.2.2 thorpej }
103 1.1.2.2 thorpej
104 1.1.2.2 thorpej static int
105 1.1.2.2 thorpej imx_ccm_clock_set_rate(void *priv, struct clk *clkp, u_int rate)
106 1.1.2.2 thorpej {
107 1.1.2.2 thorpej struct imx_ccm_softc * const sc = priv;
108 1.1.2.2 thorpej struct imx_ccm_clk *clk = (struct imx_ccm_clk *)clkp;
109 1.1.2.2 thorpej struct clk *clkp_parent;
110 1.1.2.2 thorpej
111 1.1.2.2 thorpej if (clkp->flags & CLK_SET_RATE_PARENT) {
112 1.1.2.2 thorpej clkp_parent = clk_get_parent(clkp);
113 1.1.2.2 thorpej if (clkp_parent == NULL) {
114 1.1.2.2 thorpej aprint_error("%s: no parent for %s\n", __func__, clk->base.name);
115 1.1.2.2 thorpej return ENXIO;
116 1.1.2.2 thorpej }
117 1.1.2.2 thorpej return clk_set_rate(clkp_parent, rate);
118 1.1.2.2 thorpej }
119 1.1.2.2 thorpej
120 1.1.2.2 thorpej if (clk->set_rate)
121 1.1.2.2 thorpej return clk->set_rate(sc, clk, rate);
122 1.1.2.2 thorpej
123 1.1.2.2 thorpej return ENXIO;
124 1.1.2.2 thorpej }
125 1.1.2.2 thorpej
126 1.1.2.2 thorpej static u_int
127 1.1.2.2 thorpej imx_ccm_clock_round_rate(void *priv, struct clk *clkp, u_int rate)
128 1.1.2.2 thorpej {
129 1.1.2.2 thorpej struct imx_ccm_softc * const sc = priv;
130 1.1.2.2 thorpej struct imx_ccm_clk *clk = (struct imx_ccm_clk *)clkp;
131 1.1.2.2 thorpej struct clk *clkp_parent;
132 1.1.2.2 thorpej
133 1.1.2.2 thorpej if (clkp->flags & CLK_SET_RATE_PARENT) {
134 1.1.2.2 thorpej clkp_parent = clk_get_parent(clkp);
135 1.1.2.2 thorpej if (clkp_parent == NULL) {
136 1.1.2.2 thorpej aprint_error("%s: no parent for %s\n", __func__, clk->base.name);
137 1.1.2.2 thorpej return 0;
138 1.1.2.2 thorpej }
139 1.1.2.2 thorpej return clk_round_rate(clkp_parent, rate);
140 1.1.2.2 thorpej }
141 1.1.2.2 thorpej
142 1.1.2.2 thorpej if (clk->round_rate)
143 1.1.2.2 thorpej return clk->round_rate(sc, clk, rate);
144 1.1.2.2 thorpej
145 1.1.2.2 thorpej return 0;
146 1.1.2.2 thorpej }
147 1.1.2.2 thorpej
148 1.1.2.2 thorpej static int
149 1.1.2.2 thorpej imx_ccm_clock_enable(void *priv, struct clk *clkp)
150 1.1.2.2 thorpej {
151 1.1.2.2 thorpej struct imx_ccm_softc * const sc = priv;
152 1.1.2.2 thorpej struct imx_ccm_clk *clk = (struct imx_ccm_clk *)clkp;
153 1.1.2.2 thorpej struct clk *clkp_parent;
154 1.1.2.2 thorpej int error = 0;
155 1.1.2.2 thorpej
156 1.1.2.2 thorpej clkp_parent = clk_get_parent(clkp);
157 1.1.2.2 thorpej if (clkp_parent != NULL) {
158 1.1.2.2 thorpej error = clk_enable(clkp_parent);
159 1.1.2.2 thorpej if (error != 0)
160 1.1.2.2 thorpej return error;
161 1.1.2.2 thorpej }
162 1.1.2.2 thorpej
163 1.1.2.2 thorpej if (clk->enable)
164 1.1.2.2 thorpej error = clk->enable(sc, clk, 1);
165 1.1.2.2 thorpej
166 1.1.2.2 thorpej return error;
167 1.1.2.2 thorpej }
168 1.1.2.2 thorpej
169 1.1.2.2 thorpej static int
170 1.1.2.2 thorpej imx_ccm_clock_disable(void *priv, struct clk *clkp)
171 1.1.2.2 thorpej {
172 1.1.2.2 thorpej struct imx_ccm_softc * const sc = priv;
173 1.1.2.2 thorpej struct imx_ccm_clk *clk = (struct imx_ccm_clk *)clkp;
174 1.1.2.2 thorpej int error = EINVAL;
175 1.1.2.2 thorpej
176 1.1.2.2 thorpej if (clk->enable)
177 1.1.2.2 thorpej error = clk->enable(sc, clk, 0);
178 1.1.2.2 thorpej
179 1.1.2.2 thorpej return error;
180 1.1.2.2 thorpej }
181 1.1.2.2 thorpej
182 1.1.2.2 thorpej static int
183 1.1.2.2 thorpej imx_ccm_clock_set_parent(void *priv, struct clk *clkp,
184 1.1.2.2 thorpej struct clk *clkp_parent)
185 1.1.2.2 thorpej {
186 1.1.2.2 thorpej struct imx_ccm_softc * const sc = priv;
187 1.1.2.2 thorpej struct imx_ccm_clk *clk = (struct imx_ccm_clk *)clkp;
188 1.1.2.2 thorpej
189 1.1.2.2 thorpej if (clk->set_parent == NULL)
190 1.1.2.2 thorpej return EINVAL;
191 1.1.2.2 thorpej
192 1.1.2.2 thorpej return clk->set_parent(sc, clk, clkp_parent->name);
193 1.1.2.2 thorpej }
194 1.1.2.2 thorpej
195 1.1.2.2 thorpej static struct clk *
196 1.1.2.2 thorpej imx_ccm_clock_get_parent(void *priv, struct clk *clkp)
197 1.1.2.2 thorpej {
198 1.1.2.2 thorpej struct imx_ccm_softc * const sc = priv;
199 1.1.2.2 thorpej struct imx_ccm_clk *clk = (struct imx_ccm_clk *)clkp;
200 1.1.2.2 thorpej struct imx_ccm_clk *clk_parent;
201 1.1.2.2 thorpej const char *parent;
202 1.1.2.2 thorpej
203 1.1.2.2 thorpej if (clk->get_parent == NULL)
204 1.1.2.2 thorpej return NULL;
205 1.1.2.2 thorpej
206 1.1.2.2 thorpej parent = clk->get_parent(sc, clk);
207 1.1.2.2 thorpej if (parent == NULL)
208 1.1.2.2 thorpej return NULL;
209 1.1.2.2 thorpej
210 1.1.2.2 thorpej clk_parent = imx_ccm_clock_find(sc, parent);
211 1.1.2.2 thorpej if (clk_parent != NULL)
212 1.1.2.2 thorpej return &clk_parent->base;
213 1.1.2.2 thorpej
214 1.1.2.2 thorpej /* No parent in this domain, try FDT */
215 1.1.2.2 thorpej return fdtbus_clock_byname(parent);
216 1.1.2.2 thorpej }
217 1.1.2.2 thorpej
218 1.1.2.2 thorpej const struct clk_funcs imx_ccm_clock_funcs = {
219 1.1.2.2 thorpej .get = imx_ccm_clock_get,
220 1.1.2.2 thorpej .put = imx_ccm_clock_put,
221 1.1.2.2 thorpej .get_rate = imx_ccm_clock_get_rate,
222 1.1.2.2 thorpej .set_rate = imx_ccm_clock_set_rate,
223 1.1.2.2 thorpej .round_rate = imx_ccm_clock_round_rate,
224 1.1.2.2 thorpej .enable = imx_ccm_clock_enable,
225 1.1.2.2 thorpej .disable = imx_ccm_clock_disable,
226 1.1.2.2 thorpej .set_parent = imx_ccm_clock_set_parent,
227 1.1.2.2 thorpej .get_parent = imx_ccm_clock_get_parent,
228 1.1.2.2 thorpej };
229 1.1.2.2 thorpej
230 1.1.2.2 thorpej struct imx_ccm_clk *
231 1.1.2.2 thorpej imx_ccm_clock_find(struct imx_ccm_softc *sc, const char *name)
232 1.1.2.2 thorpej {
233 1.1.2.2 thorpej for (int i = 0; i < sc->sc_nclks; i++) {
234 1.1.2.2 thorpej if (sc->sc_clks[i].base.name == NULL)
235 1.1.2.2 thorpej continue;
236 1.1.2.2 thorpej if (strcmp(sc->sc_clks[i].base.name, name) == 0)
237 1.1.2.2 thorpej return &sc->sc_clks[i];
238 1.1.2.2 thorpej }
239 1.1.2.2 thorpej
240 1.1.2.2 thorpej return NULL;
241 1.1.2.2 thorpej }
242 1.1.2.2 thorpej
243 1.1.2.2 thorpej int
244 1.1.2.2 thorpej imx_ccm_attach(struct imx_ccm_softc *sc)
245 1.1.2.2 thorpej {
246 1.1.2.2 thorpej bus_addr_t addr;
247 1.1.2.2 thorpej bus_size_t size;
248 1.1.2.2 thorpej int i;
249 1.1.2.2 thorpej
250 1.1.2.2 thorpej if (fdtbus_get_reg(sc->sc_phandle, 0, &addr, &size) != 0) {
251 1.1.2.2 thorpej aprint_error(": couldn't get registers\n");
252 1.1.2.2 thorpej return ENXIO;
253 1.1.2.2 thorpej }
254 1.1.2.2 thorpej if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh[0]) != 0) {
255 1.1.2.2 thorpej aprint_error(": couldn't map registers\n");
256 1.1.2.2 thorpej return ENXIO;
257 1.1.2.2 thorpej }
258 1.1.2.2 thorpej
259 1.1.2.2 thorpej sc->sc_clkdom.name = device_xname(sc->sc_dev);
260 1.1.2.2 thorpej sc->sc_clkdom.funcs = &imx_ccm_clock_funcs;
261 1.1.2.2 thorpej sc->sc_clkdom.priv = sc;
262 1.1.2.2 thorpej for (i = 0; i < sc->sc_nclks; i++) {
263 1.1.2.2 thorpej sc->sc_clks[i].base.domain = &sc->sc_clkdom;
264 1.1.2.2 thorpej clk_attach(&sc->sc_clks[i].base);
265 1.1.2.2 thorpej }
266 1.1.2.2 thorpej
267 1.1.2.2 thorpej fdtbus_register_clock_controller(sc->sc_dev, sc->sc_phandle,
268 1.1.2.2 thorpej &imx_ccm_fdtclock_funcs);
269 1.1.2.2 thorpej
270 1.1.2.2 thorpej return 0;
271 1.1.2.2 thorpej }
272 1.1.2.2 thorpej
273 1.1.2.2 thorpej void
274 1.1.2.2 thorpej imx_ccm_print(struct imx_ccm_softc *sc)
275 1.1.2.2 thorpej {
276 1.1.2.2 thorpej struct imx_ccm_clk *clk;
277 1.1.2.2 thorpej struct clk *clkp_parent;
278 1.1.2.2 thorpej const char *type;
279 1.1.2.2 thorpej int i;
280 1.1.2.2 thorpej
281 1.1.2.2 thorpej for (i = 0; i < sc->sc_nclks; i++) {
282 1.1.2.2 thorpej clk = &sc->sc_clks[i];
283 1.1.2.2 thorpej if (clk->type == IMX_CCM_UNKNOWN)
284 1.1.2.2 thorpej continue;
285 1.1.2.2 thorpej
286 1.1.2.2 thorpej clkp_parent = clk_get_parent(&clk->base);
287 1.1.2.2 thorpej
288 1.1.2.2 thorpej switch (clk->type) {
289 1.1.2.2 thorpej case IMX_CCM_EXTCLK: type = "extclk"; break;
290 1.1.2.2 thorpej case IMX_CCM_GATE: type = "gate"; break;
291 1.1.2.2 thorpej case IMX_CCM_COMPOSITE: type = "comp"; break;
292 1.1.2.2 thorpej case IMX_CCM_PLL: type = "pll"; break;
293 1.1.2.2 thorpej case IMX_CCM_FIXED: type = "fixed"; break;
294 1.1.2.2 thorpej case IMX_CCM_FIXED_FACTOR: type = "fixed-factor"; break;
295 1.1.2.2 thorpej case IMX_CCM_MUX: type = "mux"; break;
296 1.1.2.2 thorpej case IMX_CCM_DIV: type = "div"; break;
297 1.1.2.2 thorpej default: type = "???"; break;
298 1.1.2.2 thorpej }
299 1.1.2.2 thorpej
300 1.1.2.2 thorpej aprint_debug_dev(sc->sc_dev,
301 1.1.2.2 thorpej "%3d %-14s %2s %-14s %-7s ",
302 1.1.2.2 thorpej clk->id,
303 1.1.2.2 thorpej clk->base.name,
304 1.1.2.2 thorpej clkp_parent ? "<-" : "",
305 1.1.2.2 thorpej clkp_parent ? clkp_parent->name : "",
306 1.1.2.2 thorpej type);
307 1.1.2.2 thorpej aprint_debug("%10d Hz\n", clk_get_rate(&clk->base));
308 1.1.2.2 thorpej }
309 1.1.2.2 thorpej }
310