2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Allwinner module clocks
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <machine/bus.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/ofw/ofw_subr.h>
48 #include <dev/extres/clk/clk_mux.h>
49 #include <dev/extres/clk/clk_gate.h>
51 #include "clkdev_if.h"
53 #define SCLK_GATING (1 << 31)
54 #define CLK_SRC_SEL (0x3 << 24)
55 #define CLK_SRC_SEL_SHIFT 24
56 #define CLK_RATIO_N (0x3 << 16)
57 #define CLK_RATIO_N_SHIFT 16
58 #define CLK_RATIO_N_MAX 0x3
59 #define CLK_RATIO_M (0x1f << 0)
60 #define CLK_RATIO_M_SHIFT 0
61 #define CLK_RATIO_M_MAX 0x1f
63 static struct ofw_compat_data compat_data[] = {
64 { "allwinner,sun4i-a10-mod0-clk", 1 },
74 #define MODCLK_READ(sc, val) CLKDEV_READ_4((sc)->clkdev, (sc)->reg, (val))
75 #define MODCLK_WRITE(sc, val) CLKDEV_WRITE_4((sc)->clkdev, (sc)->reg, (val))
76 #define DEVICE_LOCK(sc) CLKDEV_DEVICE_LOCK((sc)->clkdev)
77 #define DEVICE_UNLOCK(sc) CLKDEV_DEVICE_UNLOCK((sc)->clkdev)
80 aw_modclk_init(struct clknode *clk, device_t dev)
82 struct aw_modclk_sc *sc;
85 sc = clknode_get_softc(clk);
88 MODCLK_READ(sc, &val);
91 index = (val & CLK_SRC_SEL) >> CLK_SRC_SEL_SHIFT;
93 clknode_init_parent_idx(clk, index);
98 aw_modclk_set_mux(struct clknode *clk, int index)
100 struct aw_modclk_sc *sc;
103 sc = clknode_get_softc(clk);
105 if (index < 0 || index >= sc->parent_cnt)
109 MODCLK_READ(sc, &val);
111 val |= (index << CLK_SRC_SEL_SHIFT);
112 MODCLK_WRITE(sc, val);
119 aw_modclk_set_gate(struct clknode *clk, bool enable)
121 struct aw_modclk_sc *sc;
124 sc = clknode_get_softc(clk);
127 MODCLK_READ(sc, &val);
132 MODCLK_WRITE(sc, val);
139 aw_modclk_recalc_freq(struct clknode *clk, uint64_t *freq)
141 struct aw_modclk_sc *sc;
144 sc = clknode_get_softc(clk);
147 MODCLK_READ(sc, &val);
150 n = 1 << ((val & CLK_RATIO_N) >> CLK_RATIO_N_SHIFT);
151 m = ((val & CLK_RATIO_M) >> CLK_RATIO_M_SHIFT) + 1;
153 *freq = *freq / n / m;
159 aw_modclk_set_freq(struct clknode *clk, uint64_t fin, uint64_t *fout,
160 int flags, int *stop)
162 struct aw_modclk_sc *sc;
163 uint32_t val, m, n, src, best_m, best_n, best_src;
165 int64_t best_diff, cur_diff;
168 sc = clknode_get_softc(clk);
170 best_diff = (int64_t)*fout;
173 for (src = 0; src < sc->parent_cnt; src++) {
174 error = clknode_set_parent_by_idx(clk, src);
177 error = clknode_get_freq(clknode_get_parent(clk), &fin);
181 for (n = 0; n <= CLK_RATIO_N_MAX; n++)
182 for (m = 0; m <= CLK_RATIO_M_MAX; m++) {
183 cur_freq = fin / (1 << n) / (m + 1);
184 cur_diff = (int64_t)*fout - cur_freq;
185 if (cur_diff >= 0 && cur_diff < best_diff) {
187 best_diff = cur_diff;
194 if (best_diff == (int64_t)*fout)
197 error = clknode_set_parent_by_idx(clk, best_src);
200 error = clknode_get_freq(clknode_get_parent(clk), &fin);
205 MODCLK_READ(sc, &val);
206 val &= ~(CLK_RATIO_N | CLK_RATIO_M);
207 val |= (best_n << CLK_RATIO_N_SHIFT);
208 val |= (best_m << CLK_RATIO_M_SHIFT);
209 MODCLK_WRITE(sc, val);
212 *fout = fin / (1 << best_n) / (best_m + 1);
218 static clknode_method_t aw_modclk_clknode_methods[] = {
219 /* Device interface */
220 CLKNODEMETHOD(clknode_init, aw_modclk_init),
221 CLKNODEMETHOD(clknode_set_gate, aw_modclk_set_gate),
222 CLKNODEMETHOD(clknode_set_mux, aw_modclk_set_mux),
223 CLKNODEMETHOD(clknode_recalc_freq, aw_modclk_recalc_freq),
224 CLKNODEMETHOD(clknode_set_freq, aw_modclk_set_freq),
227 DEFINE_CLASS_1(aw_modclk_clknode, aw_modclk_clknode_class,
228 aw_modclk_clknode_methods, sizeof(struct aw_modclk_sc), clknode_class);
231 aw_modclk_probe(device_t dev)
233 if (!ofw_bus_status_okay(dev))
236 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
239 device_set_desc(dev, "Allwinner Module Clock");
240 return (BUS_PROBE_DEFAULT);
244 aw_modclk_attach(device_t dev)
246 struct clknode_init_def def;
247 struct aw_modclk_sc *sc;
248 struct clkdom *clkdom;
254 int error, ncells, i;
256 node = ofw_bus_get_node(dev);
258 if (ofw_reg_to_paddr(node, 0, &paddr, &psize, NULL) != 0) {
259 device_printf(dev, "cannot parse 'reg' property\n");
263 error = ofw_bus_parse_xref_list_get_length(node, "clocks",
264 "#clock-cells", &ncells);
266 device_printf(dev, "cannot get clock count\n");
270 clkdom = clkdom_create(dev);
272 memset(&def, 0, sizeof(def));
273 error = clk_parse_ofw_clk_name(dev, node, &def.name);
275 device_printf(dev, "cannot parse clock name\n");
280 def.parent_names = malloc(sizeof(char *) * ncells, M_OFWPROP, M_WAITOK);
281 for (i = 0; i < ncells; i++) {
282 error = clk_get_by_ofw_index(dev, 0, i, &clk_parent);
284 device_printf(dev, "cannot get clock %d\n", i);
287 def.parent_names[i] = clk_get_name(clk_parent);
288 clk_release(clk_parent);
290 def.parent_cnt = ncells;
292 clk = clknode_create(clkdom, &aw_modclk_clknode_class, &def);
294 device_printf(dev, "cannot create clknode\n");
299 sc = clknode_get_softc(clk);
301 sc->clkdev = device_get_parent(dev);
302 sc->parent_cnt = def.parent_cnt;
304 clknode_register(clkdom, clk);
306 if (clkdom_finit(clkdom) != 0) {
307 device_printf(dev, "cannot finalize clkdom initialization\n");
321 static device_method_t aw_modclk_methods[] = {
322 /* Device interface */
323 DEVMETHOD(device_probe, aw_modclk_probe),
324 DEVMETHOD(device_attach, aw_modclk_attach),
329 static driver_t aw_modclk_driver = {
335 static devclass_t aw_modclk_devclass;
337 EARLY_DRIVER_MODULE(aw_modclk, simplebus, aw_modclk_driver,
338 aw_modclk_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);