1e37e8677SEmmanuel Vadot /*-
2e37e8677SEmmanuel Vadot * Copyright (c) 2017 Emmanuel Vadot <manu@freebsd.org>
3e37e8677SEmmanuel Vadot *
4e37e8677SEmmanuel Vadot * Redistribution and use in source and binary forms, with or without
5e37e8677SEmmanuel Vadot * modification, are permitted provided that the following conditions
6e37e8677SEmmanuel Vadot * are met:
7e37e8677SEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright
8e37e8677SEmmanuel Vadot * notice, this list of conditions and the following disclaimer.
9e37e8677SEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright
10e37e8677SEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the
11e37e8677SEmmanuel Vadot * documentation and/or other materials provided with the distribution.
12e37e8677SEmmanuel Vadot *
13e37e8677SEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14e37e8677SEmmanuel Vadot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15e37e8677SEmmanuel Vadot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16e37e8677SEmmanuel Vadot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17e37e8677SEmmanuel Vadot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18e37e8677SEmmanuel Vadot * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19e37e8677SEmmanuel Vadot * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20e37e8677SEmmanuel Vadot * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21e37e8677SEmmanuel Vadot * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22e37e8677SEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23e37e8677SEmmanuel Vadot * SUCH DAMAGE.
24e37e8677SEmmanuel Vadot */
25e37e8677SEmmanuel Vadot
26e37e8677SEmmanuel Vadot #include <sys/param.h>
27e37e8677SEmmanuel Vadot #include <sys/systm.h>
28e37e8677SEmmanuel Vadot #include <sys/bus.h>
29e37e8677SEmmanuel Vadot
30*be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
31e37e8677SEmmanuel Vadot
32e37e8677SEmmanuel Vadot #include <dev/clk/allwinner/aw_clk.h>
33e37e8677SEmmanuel Vadot #include <dev/clk/allwinner/aw_clk_prediv_mux.h>
34e37e8677SEmmanuel Vadot
35e37e8677SEmmanuel Vadot #include "clkdev_if.h"
36e37e8677SEmmanuel Vadot
37e37e8677SEmmanuel Vadot /*
38e37e8677SEmmanuel Vadot * clknode for clocks matching the formula :
39e37e8677SEmmanuel Vadot *
40e37e8677SEmmanuel Vadot * clk = clkin / prediv / div
41e37e8677SEmmanuel Vadot *
42e37e8677SEmmanuel Vadot * and where prediv is conditional
43e37e8677SEmmanuel Vadot *
44e37e8677SEmmanuel Vadot */
45e37e8677SEmmanuel Vadot
46e37e8677SEmmanuel Vadot struct aw_clk_prediv_mux_sc {
47e37e8677SEmmanuel Vadot uint32_t offset;
48e37e8677SEmmanuel Vadot
49e37e8677SEmmanuel Vadot uint32_t mux_shift;
50e37e8677SEmmanuel Vadot uint32_t mux_mask;
51e37e8677SEmmanuel Vadot
52e37e8677SEmmanuel Vadot struct aw_clk_factor div;
53e37e8677SEmmanuel Vadot struct aw_clk_factor prediv;
54e37e8677SEmmanuel Vadot
55e37e8677SEmmanuel Vadot uint32_t flags;
56e37e8677SEmmanuel Vadot };
57e37e8677SEmmanuel Vadot
58e37e8677SEmmanuel Vadot #define WRITE4(_clk, off, val) \
59e37e8677SEmmanuel Vadot CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
60e37e8677SEmmanuel Vadot #define READ4(_clk, off, val) \
61e37e8677SEmmanuel Vadot CLKDEV_READ_4(clknode_get_device(_clk), off, val)
62e37e8677SEmmanuel Vadot #define MODIFY4(_clk, off, clr, set ) \
63e37e8677SEmmanuel Vadot CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set)
64e37e8677SEmmanuel Vadot #define DEVICE_LOCK(_clk) \
65e37e8677SEmmanuel Vadot CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))
66e37e8677SEmmanuel Vadot #define DEVICE_UNLOCK(_clk) \
67e37e8677SEmmanuel Vadot CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))
68e37e8677SEmmanuel Vadot
69e37e8677SEmmanuel Vadot static int
aw_clk_prediv_mux_init(struct clknode * clk,device_t dev)70e37e8677SEmmanuel Vadot aw_clk_prediv_mux_init(struct clknode *clk, device_t dev)
71e37e8677SEmmanuel Vadot {
72e37e8677SEmmanuel Vadot struct aw_clk_prediv_mux_sc *sc;
73e37e8677SEmmanuel Vadot uint32_t val;
74e37e8677SEmmanuel Vadot
75e37e8677SEmmanuel Vadot sc = clknode_get_softc(clk);
76e37e8677SEmmanuel Vadot
77e37e8677SEmmanuel Vadot DEVICE_LOCK(clk);
78e37e8677SEmmanuel Vadot READ4(clk, sc->offset, &val);
79e37e8677SEmmanuel Vadot DEVICE_UNLOCK(clk);
80e37e8677SEmmanuel Vadot
81e37e8677SEmmanuel Vadot /* Init the current parent */
82e37e8677SEmmanuel Vadot val = (val & sc->mux_mask) >> sc->mux_shift;
83e37e8677SEmmanuel Vadot clknode_init_parent_idx(clk, val);
84e37e8677SEmmanuel Vadot
85e37e8677SEmmanuel Vadot return (0);
86e37e8677SEmmanuel Vadot }
87e37e8677SEmmanuel Vadot
88e37e8677SEmmanuel Vadot static int
aw_clk_prediv_mux_set_mux(struct clknode * clk,int index)89e37e8677SEmmanuel Vadot aw_clk_prediv_mux_set_mux(struct clknode *clk, int index)
90e37e8677SEmmanuel Vadot {
91e37e8677SEmmanuel Vadot struct aw_clk_prediv_mux_sc *sc;
92e37e8677SEmmanuel Vadot uint32_t val;
93e37e8677SEmmanuel Vadot
94e37e8677SEmmanuel Vadot sc = clknode_get_softc(clk);
95e37e8677SEmmanuel Vadot
96e37e8677SEmmanuel Vadot DEVICE_LOCK(clk);
97e37e8677SEmmanuel Vadot READ4(clk, sc->offset, &val);
98e37e8677SEmmanuel Vadot val &= ~sc->mux_mask;
99e37e8677SEmmanuel Vadot val |= index << sc->mux_shift;
100e37e8677SEmmanuel Vadot WRITE4(clk, sc->offset, val);
101e37e8677SEmmanuel Vadot DEVICE_UNLOCK(clk);
102e37e8677SEmmanuel Vadot
103e37e8677SEmmanuel Vadot return (0);
104e37e8677SEmmanuel Vadot }
105e37e8677SEmmanuel Vadot
106e37e8677SEmmanuel Vadot static int
aw_clk_prediv_mux_recalc(struct clknode * clk,uint64_t * freq)107e37e8677SEmmanuel Vadot aw_clk_prediv_mux_recalc(struct clknode *clk, uint64_t *freq)
108e37e8677SEmmanuel Vadot {
109e37e8677SEmmanuel Vadot struct aw_clk_prediv_mux_sc *sc;
110e37e8677SEmmanuel Vadot uint32_t val, div, prediv;
111e37e8677SEmmanuel Vadot
112e37e8677SEmmanuel Vadot sc = clknode_get_softc(clk);
113e37e8677SEmmanuel Vadot
114e37e8677SEmmanuel Vadot DEVICE_LOCK(clk);
115e37e8677SEmmanuel Vadot READ4(clk, sc->offset, &val);
116e37e8677SEmmanuel Vadot DEVICE_UNLOCK(clk);
117e37e8677SEmmanuel Vadot
118e37e8677SEmmanuel Vadot div = aw_clk_get_factor(val, &sc->div);
119e37e8677SEmmanuel Vadot prediv = aw_clk_get_factor(val, &sc->prediv);
120e37e8677SEmmanuel Vadot
121e37e8677SEmmanuel Vadot *freq = *freq / prediv / div;
122e37e8677SEmmanuel Vadot return (0);
123e37e8677SEmmanuel Vadot }
124e37e8677SEmmanuel Vadot
125e37e8677SEmmanuel Vadot static clknode_method_t aw_prediv_mux_clknode_methods[] = {
126e37e8677SEmmanuel Vadot /* Device interface */
127e37e8677SEmmanuel Vadot CLKNODEMETHOD(clknode_init, aw_clk_prediv_mux_init),
128e37e8677SEmmanuel Vadot CLKNODEMETHOD(clknode_set_mux, aw_clk_prediv_mux_set_mux),
129e37e8677SEmmanuel Vadot CLKNODEMETHOD(clknode_recalc_freq, aw_clk_prediv_mux_recalc),
130e37e8677SEmmanuel Vadot CLKNODEMETHOD_END
131e37e8677SEmmanuel Vadot };
132e37e8677SEmmanuel Vadot
133e37e8677SEmmanuel Vadot DEFINE_CLASS_1(aw_prediv_mux_clknode, aw_prediv_mux_clknode_class,
134e37e8677SEmmanuel Vadot aw_prediv_mux_clknode_methods, sizeof(struct aw_clk_prediv_mux_sc),
135e37e8677SEmmanuel Vadot clknode_class);
136e37e8677SEmmanuel Vadot
137e37e8677SEmmanuel Vadot int
aw_clk_prediv_mux_register(struct clkdom * clkdom,struct aw_clk_prediv_mux_def * clkdef)138e37e8677SEmmanuel Vadot aw_clk_prediv_mux_register(struct clkdom *clkdom, struct aw_clk_prediv_mux_def *clkdef)
139e37e8677SEmmanuel Vadot {
140e37e8677SEmmanuel Vadot struct clknode *clk;
141e37e8677SEmmanuel Vadot struct aw_clk_prediv_mux_sc *sc;
142e37e8677SEmmanuel Vadot
143e37e8677SEmmanuel Vadot clk = clknode_create(clkdom, &aw_prediv_mux_clknode_class, &clkdef->clkdef);
144e37e8677SEmmanuel Vadot if (clk == NULL)
145e37e8677SEmmanuel Vadot return (1);
146e37e8677SEmmanuel Vadot
147e37e8677SEmmanuel Vadot sc = clknode_get_softc(clk);
148e37e8677SEmmanuel Vadot
149e37e8677SEmmanuel Vadot sc->offset = clkdef->offset;
150e37e8677SEmmanuel Vadot
151e37e8677SEmmanuel Vadot sc->mux_shift = clkdef->mux_shift;
152e37e8677SEmmanuel Vadot sc->mux_mask = ((1 << clkdef->mux_width) - 1) << sc->mux_shift;
153e37e8677SEmmanuel Vadot
154e37e8677SEmmanuel Vadot sc->div.shift = clkdef->div.shift;
155e37e8677SEmmanuel Vadot sc->div.mask = ((1 << clkdef->div.width) - 1) << sc->div.shift;
156e37e8677SEmmanuel Vadot sc->div.value = clkdef->div.value;
157e37e8677SEmmanuel Vadot sc->div.cond_shift = clkdef->div.cond_shift;
158e37e8677SEmmanuel Vadot sc->div.cond_mask = ((1 << clkdef->div.cond_width) - 1) << sc->div.shift;
159e37e8677SEmmanuel Vadot sc->div.cond_value = clkdef->div.cond_value;
160e37e8677SEmmanuel Vadot sc->div.flags = clkdef->div.flags;
161e37e8677SEmmanuel Vadot
162e37e8677SEmmanuel Vadot sc->prediv.shift = clkdef->prediv.shift;
163e37e8677SEmmanuel Vadot sc->prediv.mask = ((1 << clkdef->prediv.width) - 1) << sc->prediv.shift;
164e37e8677SEmmanuel Vadot sc->prediv.value = clkdef->prediv.value;
165e37e8677SEmmanuel Vadot sc->prediv.cond_shift = clkdef->prediv.cond_shift;
166e37e8677SEmmanuel Vadot if (clkdef->prediv.cond_width != 0)
167e37e8677SEmmanuel Vadot sc->prediv.cond_mask = ((1 << clkdef->prediv.cond_width) - 1) << sc->prediv.shift;
168e37e8677SEmmanuel Vadot else
169e37e8677SEmmanuel Vadot sc->prediv.cond_mask = clkdef->prediv.cond_mask;
170e37e8677SEmmanuel Vadot sc->prediv.cond_value = clkdef->prediv.cond_value;
171e37e8677SEmmanuel Vadot sc->prediv.flags = clkdef->prediv.flags;
172e37e8677SEmmanuel Vadot
173e37e8677SEmmanuel Vadot sc->flags = clkdef->flags;
174e37e8677SEmmanuel Vadot
175e37e8677SEmmanuel Vadot clknode_register(clkdom, clk);
176e37e8677SEmmanuel Vadot
177e37e8677SEmmanuel Vadot return (0);
178e37e8677SEmmanuel Vadot }
179