ti_dpll_clock.c revision 1.5 1 1.5 thorpej /* $NetBSD: ti_dpll_clock.c,v 1.5 2021/01/25 14:20:39 thorpej Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2019 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 <sys/cdefs.h>
30 1.5 thorpej __KERNEL_RCSID(0, "$NetBSD: ti_dpll_clock.c,v 1.5 2021/01/25 14:20:39 thorpej Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/systm.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/kmem.h>
36 1.1 jmcneill #include <sys/bus.h>
37 1.1 jmcneill
38 1.1 jmcneill #include <dev/clk/clk_backend.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <dev/fdt/fdtvar.h>
41 1.1 jmcneill
42 1.1 jmcneill #define DPLL_MULT __BITS(18,8)
43 1.1 jmcneill #define DPLL_DIV __BITS(6,0)
44 1.1 jmcneill
45 1.2 jmcneill #define AM3_ST_MN_BYPASS __BIT(8)
46 1.2 jmcneill #define AM3_ST_DPLL_CLK __BIT(0)
47 1.2 jmcneill
48 1.2 jmcneill #define AM3_DPLL_EN __BITS(2,0)
49 1.2 jmcneill #define AM3_DPLL_EN_NM_BYPASS 4
50 1.2 jmcneill #define AM3_DPLL_EN_LOCK 7
51 1.2 jmcneill
52 1.2 jmcneill #define OMAP3_ST_MPU_CLK __BIT(0)
53 1.2 jmcneill
54 1.2 jmcneill #define OMAP3_EN_MPU_DPLL __BITS(2,0)
55 1.2 jmcneill #define OMAP3_EN_MPU_DPLL_BYPASS 5
56 1.2 jmcneill #define OMAP3_EN_MPU_DPLL_LOCK 7
57 1.2 jmcneill
58 1.2 jmcneill #define OMAP3_CORE_DPLL_CLKOUT_DIV __BITS(31,27)
59 1.2 jmcneill #define OMAP3_CORE_DPLL_MULT __BITS(26,16)
60 1.2 jmcneill #define OMAP3_CORE_DPLL_DIV __BITS(14,8)
61 1.1 jmcneill
62 1.1 jmcneill static int ti_dpll_clock_match(device_t, cfdata_t, void *);
63 1.1 jmcneill static void ti_dpll_clock_attach(device_t, device_t, void *);
64 1.1 jmcneill
65 1.1 jmcneill static struct clk *ti_dpll_clock_decode(device_t, int, const void *, size_t);
66 1.1 jmcneill
67 1.1 jmcneill static const struct fdtbus_clock_controller_func ti_dpll_clock_fdt_funcs = {
68 1.1 jmcneill .decode = ti_dpll_clock_decode
69 1.1 jmcneill };
70 1.1 jmcneill
71 1.1 jmcneill static struct clk *ti_dpll_clock_get(void *, const char *);
72 1.1 jmcneill static void ti_dpll_clock_put(void *, struct clk *);
73 1.1 jmcneill static u_int ti_dpll_clock_get_rate(void *, struct clk *);
74 1.1 jmcneill static struct clk *ti_dpll_clock_get_parent(void *, struct clk *);
75 1.1 jmcneill
76 1.2 jmcneill static int am3_dpll_clock_set_rate(void *, struct clk *, u_int);
77 1.2 jmcneill
78 1.2 jmcneill static const struct clk_funcs am3_dpll_clock_clk_funcs = {
79 1.1 jmcneill .get = ti_dpll_clock_get,
80 1.1 jmcneill .put = ti_dpll_clock_put,
81 1.2 jmcneill .set_rate = am3_dpll_clock_set_rate,
82 1.1 jmcneill .get_rate = ti_dpll_clock_get_rate,
83 1.1 jmcneill .get_parent = ti_dpll_clock_get_parent,
84 1.1 jmcneill };
85 1.1 jmcneill
86 1.2 jmcneill static int omap3_dpll_clock_set_rate(void *, struct clk *, u_int);
87 1.2 jmcneill
88 1.2 jmcneill static const struct clk_funcs omap3_dpll_clock_clk_funcs = {
89 1.2 jmcneill .get = ti_dpll_clock_get,
90 1.2 jmcneill .put = ti_dpll_clock_put,
91 1.2 jmcneill .set_rate = omap3_dpll_clock_set_rate,
92 1.2 jmcneill .get_rate = ti_dpll_clock_get_rate,
93 1.2 jmcneill .get_parent = ti_dpll_clock_get_parent,
94 1.2 jmcneill };
95 1.2 jmcneill
96 1.2 jmcneill static u_int omap3_dpll_core_clock_get_rate(void *, struct clk *);
97 1.2 jmcneill
98 1.2 jmcneill static const struct clk_funcs omap3_dpll_core_clock_clk_funcs = {
99 1.2 jmcneill .get = ti_dpll_clock_get,
100 1.2 jmcneill .put = ti_dpll_clock_put,
101 1.2 jmcneill .get_rate = omap3_dpll_core_clock_get_rate,
102 1.2 jmcneill .get_parent = ti_dpll_clock_get_parent,
103 1.2 jmcneill };
104 1.2 jmcneill
105 1.4 thorpej static const struct device_compatible_entry compat_data[] = {
106 1.4 thorpej { .compat = "ti,am3-dpll-clock",
107 1.4 thorpej .data = &am3_dpll_clock_clk_funcs },
108 1.4 thorpej { .compat = "ti,omap3-dpll-clock",
109 1.4 thorpej .data = &omap3_dpll_clock_clk_funcs },
110 1.4 thorpej { .compat = "ti,omap3-dpll-core-clock",
111 1.4 thorpej .data = &omap3_dpll_core_clock_clk_funcs },
112 1.4 thorpej
113 1.5 thorpej { }
114 1.2 jmcneill };
115 1.2 jmcneill
116 1.1 jmcneill enum {
117 1.1 jmcneill REG_CONTROL,
118 1.1 jmcneill REG_IDLEST,
119 1.1 jmcneill REG_MULT_DIV1,
120 1.1 jmcneill NREG
121 1.1 jmcneill };
122 1.1 jmcneill
123 1.1 jmcneill struct ti_dpll_clock_softc {
124 1.1 jmcneill device_t sc_dev;
125 1.1 jmcneill int sc_phandle;
126 1.1 jmcneill bus_space_tag_t sc_bst;
127 1.1 jmcneill bus_space_handle_t sc_bsh[NREG];
128 1.1 jmcneill
129 1.1 jmcneill struct clk_domain sc_clkdom;
130 1.1 jmcneill struct clk sc_clk;
131 1.1 jmcneill };
132 1.1 jmcneill
133 1.1 jmcneill #define RD4(sc, space) \
134 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh[(space)], 0)
135 1.1 jmcneill #define WR4(sc, space, val) \
136 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh[(space)], 0, (val))
137 1.1 jmcneill
138 1.1 jmcneill CFATTACH_DECL_NEW(ti_dpll_clock, sizeof(struct ti_dpll_clock_softc),
139 1.1 jmcneill ti_dpll_clock_match, ti_dpll_clock_attach, NULL, NULL);
140 1.1 jmcneill
141 1.1 jmcneill static int
142 1.1 jmcneill ti_dpll_clock_match(device_t parent, cfdata_t cf, void *aux)
143 1.1 jmcneill {
144 1.1 jmcneill const struct fdt_attach_args *faa = aux;
145 1.1 jmcneill
146 1.2 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
147 1.1 jmcneill }
148 1.1 jmcneill
149 1.1 jmcneill static void
150 1.1 jmcneill ti_dpll_clock_attach(device_t parent, device_t self, void *aux)
151 1.1 jmcneill {
152 1.1 jmcneill struct ti_dpll_clock_softc * const sc = device_private(self);
153 1.1 jmcneill const struct fdt_attach_args *faa = aux;
154 1.1 jmcneill const int phandle = faa->faa_phandle;
155 1.2 jmcneill const struct clk_funcs *clkfuncs;
156 1.1 jmcneill bus_addr_t addr[NREG], base_addr;
157 1.1 jmcneill u_int n;
158 1.1 jmcneill
159 1.1 jmcneill const int prcm_phandle = OF_parent(OF_parent(phandle));
160 1.1 jmcneill if (fdtbus_get_reg(prcm_phandle, 0, &base_addr, NULL) != 0) {
161 1.1 jmcneill aprint_error(": couldn't get prcm registers\n");
162 1.1 jmcneill return;
163 1.1 jmcneill }
164 1.1 jmcneill
165 1.1 jmcneill for (n = 0; n < NREG; n++) {
166 1.1 jmcneill if (fdtbus_get_reg(phandle, n, &addr[n], NULL) != 0) {
167 1.1 jmcneill aprint_error(": couldn't get registers\n");
168 1.1 jmcneill return;
169 1.1 jmcneill }
170 1.1 jmcneill }
171 1.1 jmcneill
172 1.1 jmcneill sc->sc_dev = self;
173 1.1 jmcneill sc->sc_phandle = phandle;
174 1.1 jmcneill sc->sc_bst = faa->faa_bst;
175 1.1 jmcneill for (n = 0; n < NREG; n++) {
176 1.1 jmcneill if (bus_space_map(sc->sc_bst, base_addr + addr[n], 4, 0, &sc->sc_bsh[n]) != 0) {
177 1.1 jmcneill aprint_error(": couldn't map registers\n");
178 1.1 jmcneill return;
179 1.1 jmcneill }
180 1.1 jmcneill }
181 1.1 jmcneill
182 1.4 thorpej clkfuncs = of_search_compatible(phandle, compat_data)->data;
183 1.2 jmcneill
184 1.1 jmcneill sc->sc_clkdom.name = device_xname(self);
185 1.2 jmcneill sc->sc_clkdom.funcs = clkfuncs;
186 1.1 jmcneill sc->sc_clkdom.priv = sc;
187 1.1 jmcneill
188 1.1 jmcneill sc->sc_clk.domain = &sc->sc_clkdom;
189 1.1 jmcneill sc->sc_clk.name = kmem_asprintf("%s", faa->faa_name);
190 1.1 jmcneill clk_attach(&sc->sc_clk);
191 1.1 jmcneill
192 1.1 jmcneill aprint_naive("\n");
193 1.1 jmcneill aprint_normal(": TI DPLL clock (%s)\n", sc->sc_clk.name);
194 1.1 jmcneill
195 1.1 jmcneill fdtbus_register_clock_controller(self, phandle, &ti_dpll_clock_fdt_funcs);
196 1.1 jmcneill }
197 1.1 jmcneill
198 1.1 jmcneill static struct clk *
199 1.1 jmcneill ti_dpll_clock_decode(device_t dev, int cc_phandle, const void *data,
200 1.1 jmcneill size_t len)
201 1.1 jmcneill {
202 1.1 jmcneill struct ti_dpll_clock_softc * const sc = device_private(dev);
203 1.1 jmcneill
204 1.1 jmcneill return &sc->sc_clk;
205 1.1 jmcneill }
206 1.1 jmcneill
207 1.1 jmcneill static struct clk *
208 1.1 jmcneill ti_dpll_clock_get(void *priv, const char *name)
209 1.1 jmcneill {
210 1.1 jmcneill struct ti_dpll_clock_softc * const sc = priv;
211 1.1 jmcneill
212 1.1 jmcneill return &sc->sc_clk;
213 1.1 jmcneill }
214 1.1 jmcneill
215 1.1 jmcneill static void
216 1.1 jmcneill ti_dpll_clock_put(void *priv, struct clk *clk)
217 1.1 jmcneill {
218 1.1 jmcneill }
219 1.1 jmcneill
220 1.1 jmcneill static u_int
221 1.1 jmcneill ti_dpll_clock_get_rate(void *priv, struct clk *clk)
222 1.1 jmcneill {
223 1.1 jmcneill struct ti_dpll_clock_softc * const sc = priv;
224 1.1 jmcneill struct clk *clk_parent = clk_get_parent(clk);
225 1.1 jmcneill uint32_t val;
226 1.1 jmcneill uint64_t parent_rate;
227 1.1 jmcneill
228 1.1 jmcneill if (clk_parent == NULL)
229 1.1 jmcneill return 0;
230 1.1 jmcneill
231 1.1 jmcneill val = RD4(sc, REG_MULT_DIV1);
232 1.1 jmcneill const u_int mult = __SHIFTOUT(val, DPLL_MULT);
233 1.1 jmcneill const u_int div = __SHIFTOUT(val, DPLL_DIV) + 1;
234 1.1 jmcneill
235 1.1 jmcneill parent_rate = clk_get_rate(clk_parent);
236 1.1 jmcneill
237 1.1 jmcneill return (u_int)((mult * parent_rate) / div);
238 1.1 jmcneill }
239 1.1 jmcneill
240 1.2 jmcneill static struct clk *
241 1.2 jmcneill ti_dpll_clock_get_parent(void *priv, struct clk *clk)
242 1.2 jmcneill {
243 1.2 jmcneill struct ti_dpll_clock_softc * const sc = priv;
244 1.2 jmcneill
245 1.2 jmcneill /* XXX assume ref clk */
246 1.2 jmcneill return fdtbus_clock_get_index(sc->sc_phandle, 0);
247 1.2 jmcneill }
248 1.2 jmcneill
249 1.1 jmcneill static int
250 1.2 jmcneill am3_dpll_clock_set_rate(void *priv, struct clk *clk, u_int rate)
251 1.1 jmcneill {
252 1.1 jmcneill struct ti_dpll_clock_softc * const sc = priv;
253 1.1 jmcneill struct clk *clk_parent = clk_get_parent(clk);
254 1.1 jmcneill uint64_t parent_rate;
255 1.1 jmcneill uint32_t control, mult_div1;
256 1.1 jmcneill
257 1.1 jmcneill if (clk_parent == NULL)
258 1.1 jmcneill return ENXIO;
259 1.1 jmcneill
260 1.1 jmcneill parent_rate = clk_get_rate(clk_parent);
261 1.2 jmcneill if (parent_rate == 0)
262 1.2 jmcneill return EIO;
263 1.1 jmcneill
264 1.1 jmcneill const u_int div = (parent_rate / 1000000) - 1;
265 1.1 jmcneill const u_int mult = rate / (parent_rate / (div + 1));
266 1.1 jmcneill if (mult < 2 || mult > 2047)
267 1.1 jmcneill return EINVAL;
268 1.1 jmcneill
269 1.1 jmcneill control = RD4(sc, REG_CONTROL);
270 1.2 jmcneill control &= ~AM3_DPLL_EN;
271 1.2 jmcneill control |= __SHIFTIN(AM3_DPLL_EN_NM_BYPASS, AM3_DPLL_EN);
272 1.1 jmcneill WR4(sc, REG_CONTROL, control);
273 1.1 jmcneill
274 1.3 jmcneill while (RD4(sc, REG_IDLEST) != AM3_ST_MN_BYPASS)
275 1.1 jmcneill ;
276 1.1 jmcneill
277 1.1 jmcneill mult_div1 = __SHIFTIN(mult, DPLL_MULT);
278 1.1 jmcneill mult_div1 |= __SHIFTIN(div, DPLL_DIV);
279 1.1 jmcneill WR4(sc, REG_MULT_DIV1, mult_div1);
280 1.1 jmcneill
281 1.2 jmcneill control &= ~AM3_DPLL_EN;
282 1.2 jmcneill control |= __SHIFTIN(AM3_DPLL_EN_LOCK, AM3_DPLL_EN);
283 1.2 jmcneill WR4(sc, REG_CONTROL, control);
284 1.2 jmcneill
285 1.3 jmcneill while (RD4(sc, REG_IDLEST) != AM3_ST_DPLL_CLK)
286 1.2 jmcneill ;
287 1.2 jmcneill
288 1.2 jmcneill return 0;
289 1.2 jmcneill }
290 1.2 jmcneill
291 1.2 jmcneill static int
292 1.2 jmcneill omap3_dpll_clock_set_rate(void *priv, struct clk *clk, u_int rate)
293 1.2 jmcneill {
294 1.2 jmcneill struct ti_dpll_clock_softc * const sc = priv;
295 1.2 jmcneill struct clk *clk_parent = clk_get_parent(clk);
296 1.2 jmcneill uint64_t parent_rate;
297 1.2 jmcneill uint32_t control, mult_div1;
298 1.2 jmcneill
299 1.2 jmcneill if (clk_parent == NULL)
300 1.2 jmcneill return ENXIO;
301 1.2 jmcneill
302 1.2 jmcneill parent_rate = clk_get_rate(clk_parent);
303 1.2 jmcneill
304 1.2 jmcneill const u_int div = (parent_rate / 1000000) - 1;
305 1.2 jmcneill const u_int mult = rate / (parent_rate / (div + 1));
306 1.2 jmcneill if (mult < 2 || mult > 2047)
307 1.2 jmcneill return EINVAL;
308 1.2 jmcneill
309 1.2 jmcneill control = RD4(sc, REG_CONTROL);
310 1.2 jmcneill control &= ~OMAP3_EN_MPU_DPLL;
311 1.2 jmcneill control |= __SHIFTIN(OMAP3_EN_MPU_DPLL_BYPASS, OMAP3_EN_MPU_DPLL);
312 1.2 jmcneill WR4(sc, REG_CONTROL, control);
313 1.2 jmcneill
314 1.2 jmcneill delay(10);
315 1.2 jmcneill
316 1.2 jmcneill mult_div1 = __SHIFTIN(mult, DPLL_MULT);
317 1.2 jmcneill mult_div1 |= __SHIFTIN(div, DPLL_DIV);
318 1.2 jmcneill WR4(sc, REG_MULT_DIV1, mult_div1);
319 1.2 jmcneill
320 1.2 jmcneill control &= ~OMAP3_EN_MPU_DPLL;
321 1.2 jmcneill control |= __SHIFTIN(OMAP3_EN_MPU_DPLL_LOCK, OMAP3_EN_MPU_DPLL);
322 1.1 jmcneill WR4(sc, REG_CONTROL, control);
323 1.1 jmcneill
324 1.2 jmcneill while ((RD4(sc, REG_IDLEST) & OMAP3_ST_MPU_CLK) != 0)
325 1.1 jmcneill ;
326 1.1 jmcneill
327 1.1 jmcneill return 0;
328 1.1 jmcneill }
329 1.1 jmcneill
330 1.2 jmcneill static u_int
331 1.2 jmcneill omap3_dpll_core_clock_get_rate(void *priv, struct clk *clk)
332 1.1 jmcneill {
333 1.1 jmcneill struct ti_dpll_clock_softc * const sc = priv;
334 1.2 jmcneill struct clk *clk_parent = clk_get_parent(clk);
335 1.2 jmcneill uint32_t val;
336 1.2 jmcneill uint64_t parent_rate;
337 1.1 jmcneill
338 1.2 jmcneill if (clk_parent == NULL)
339 1.2 jmcneill return 0;
340 1.2 jmcneill
341 1.2 jmcneill val = RD4(sc, REG_MULT_DIV1);
342 1.2 jmcneill const u_int mult = __SHIFTOUT(val, OMAP3_CORE_DPLL_MULT);
343 1.2 jmcneill const u_int div = __SHIFTOUT(val, OMAP3_CORE_DPLL_DIV) + 1;
344 1.2 jmcneill const u_int postdiv = __SHIFTOUT(val, OMAP3_CORE_DPLL_CLKOUT_DIV);
345 1.2 jmcneill
346 1.2 jmcneill parent_rate = clk_get_rate(clk_parent);
347 1.2 jmcneill
348 1.2 jmcneill return (u_int)((mult * parent_rate) / div) / postdiv;
349 1.1 jmcneill }
350