1be82b3a0SEmmanuel Vadot /*-
2be82b3a0SEmmanuel Vadot * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
3be82b3a0SEmmanuel Vadot * All rights reserved.
4be82b3a0SEmmanuel Vadot *
5be82b3a0SEmmanuel Vadot * Redistribution and use in source and binary forms, with or without
6be82b3a0SEmmanuel Vadot * modification, are permitted provided that the following conditions
7be82b3a0SEmmanuel Vadot * are met:
8be82b3a0SEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright
9be82b3a0SEmmanuel Vadot * notice, this list of conditions and the following disclaimer.
10be82b3a0SEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright
11be82b3a0SEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the
12be82b3a0SEmmanuel Vadot * documentation and/or other materials provided with the distribution.
13be82b3a0SEmmanuel Vadot *
14be82b3a0SEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15be82b3a0SEmmanuel Vadot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16be82b3a0SEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17be82b3a0SEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18be82b3a0SEmmanuel Vadot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19be82b3a0SEmmanuel Vadot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20be82b3a0SEmmanuel Vadot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21be82b3a0SEmmanuel Vadot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22be82b3a0SEmmanuel Vadot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23be82b3a0SEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24be82b3a0SEmmanuel Vadot * SUCH DAMAGE.
25be82b3a0SEmmanuel Vadot */
26be82b3a0SEmmanuel Vadot
27be82b3a0SEmmanuel Vadot #include <sys/param.h>
28be82b3a0SEmmanuel Vadot #include <sys/conf.h>
29be82b3a0SEmmanuel Vadot #include <sys/bus.h>
30be82b3a0SEmmanuel Vadot #include <sys/kernel.h>
31be82b3a0SEmmanuel Vadot #include <sys/systm.h>
32be82b3a0SEmmanuel Vadot
33be82b3a0SEmmanuel Vadot #include <machine/bus.h>
34be82b3a0SEmmanuel Vadot
35be82b3a0SEmmanuel Vadot #include <dev/clk/clk_div.h>
36be82b3a0SEmmanuel Vadot
37be82b3a0SEmmanuel Vadot #include "clkdev_if.h"
38be82b3a0SEmmanuel Vadot
39be82b3a0SEmmanuel Vadot #define WR4(_clk, off, val) \
40be82b3a0SEmmanuel Vadot CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
41be82b3a0SEmmanuel Vadot #define RD4(_clk, off, val) \
42be82b3a0SEmmanuel Vadot CLKDEV_READ_4(clknode_get_device(_clk), off, val)
43be82b3a0SEmmanuel Vadot #define MD4(_clk, off, clr, set ) \
44be82b3a0SEmmanuel Vadot CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set)
45be82b3a0SEmmanuel Vadot #define DEVICE_LOCK(_clk) \
46be82b3a0SEmmanuel Vadot CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))
47be82b3a0SEmmanuel Vadot #define DEVICE_UNLOCK(_clk) \
48be82b3a0SEmmanuel Vadot CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))
49be82b3a0SEmmanuel Vadot
50be82b3a0SEmmanuel Vadot static int clknode_div_init(struct clknode *clk, device_t dev);
51be82b3a0SEmmanuel Vadot static int clknode_div_recalc(struct clknode *clk, uint64_t *req);
52be82b3a0SEmmanuel Vadot static int clknode_div_set_freq(struct clknode *clknode, uint64_t fin,
53be82b3a0SEmmanuel Vadot uint64_t *fout, int flag, int *stop);
54be82b3a0SEmmanuel Vadot
55be82b3a0SEmmanuel Vadot struct clknode_div_sc {
56be82b3a0SEmmanuel Vadot struct mtx *mtx;
57be82b3a0SEmmanuel Vadot struct resource *mem_res;
58be82b3a0SEmmanuel Vadot uint32_t offset;
59be82b3a0SEmmanuel Vadot uint32_t i_shift;
60be82b3a0SEmmanuel Vadot uint32_t i_mask;
61be82b3a0SEmmanuel Vadot uint32_t i_width;
62be82b3a0SEmmanuel Vadot uint32_t f_shift;
63be82b3a0SEmmanuel Vadot uint32_t f_mask;
64be82b3a0SEmmanuel Vadot uint32_t f_width;
65be82b3a0SEmmanuel Vadot int div_flags;
66be82b3a0SEmmanuel Vadot uint32_t divider; /* in natural form */
67be82b3a0SEmmanuel Vadot
68be82b3a0SEmmanuel Vadot struct clk_div_table *div_table;
69be82b3a0SEmmanuel Vadot };
70be82b3a0SEmmanuel Vadot
71be82b3a0SEmmanuel Vadot static clknode_method_t clknode_div_methods[] = {
72be82b3a0SEmmanuel Vadot /* Device interface */
73be82b3a0SEmmanuel Vadot CLKNODEMETHOD(clknode_init, clknode_div_init),
74be82b3a0SEmmanuel Vadot CLKNODEMETHOD(clknode_recalc_freq, clknode_div_recalc),
75be82b3a0SEmmanuel Vadot CLKNODEMETHOD(clknode_set_freq, clknode_div_set_freq),
76be82b3a0SEmmanuel Vadot CLKNODEMETHOD_END
77be82b3a0SEmmanuel Vadot };
78be82b3a0SEmmanuel Vadot DEFINE_CLASS_1(clknode_div, clknode_div_class, clknode_div_methods,
79be82b3a0SEmmanuel Vadot sizeof(struct clknode_div_sc), clknode_class);
80be82b3a0SEmmanuel Vadot
81be82b3a0SEmmanuel Vadot static uint32_t
clknode_div_table_get_divider(struct clknode_div_sc * sc,uint32_t divider)82be82b3a0SEmmanuel Vadot clknode_div_table_get_divider(struct clknode_div_sc *sc, uint32_t divider)
83be82b3a0SEmmanuel Vadot {
84be82b3a0SEmmanuel Vadot struct clk_div_table *table;
85be82b3a0SEmmanuel Vadot
86be82b3a0SEmmanuel Vadot if (!(sc->div_flags & CLK_DIV_WITH_TABLE))
87be82b3a0SEmmanuel Vadot return (divider);
88be82b3a0SEmmanuel Vadot
89be82b3a0SEmmanuel Vadot for (table = sc->div_table; table->divider != 0; table++)
90be82b3a0SEmmanuel Vadot if (table->value == sc->divider)
91be82b3a0SEmmanuel Vadot return (table->divider);
92be82b3a0SEmmanuel Vadot
93be82b3a0SEmmanuel Vadot return (0);
94be82b3a0SEmmanuel Vadot }
95be82b3a0SEmmanuel Vadot
96be82b3a0SEmmanuel Vadot static int
clknode_div_table_get_value(struct clknode_div_sc * sc,uint32_t * divider)97be82b3a0SEmmanuel Vadot clknode_div_table_get_value(struct clknode_div_sc *sc, uint32_t *divider)
98be82b3a0SEmmanuel Vadot {
99be82b3a0SEmmanuel Vadot struct clk_div_table *table;
100be82b3a0SEmmanuel Vadot
101be82b3a0SEmmanuel Vadot if (!(sc->div_flags & CLK_DIV_WITH_TABLE))
102be82b3a0SEmmanuel Vadot return (0);
103be82b3a0SEmmanuel Vadot
104be82b3a0SEmmanuel Vadot for (table = sc->div_table; table->divider != 0; table++)
105be82b3a0SEmmanuel Vadot if (table->divider == *divider) {
106be82b3a0SEmmanuel Vadot *divider = table->value;
107be82b3a0SEmmanuel Vadot return (0);
108be82b3a0SEmmanuel Vadot }
109be82b3a0SEmmanuel Vadot
110be82b3a0SEmmanuel Vadot return (ENOENT);
111be82b3a0SEmmanuel Vadot }
112be82b3a0SEmmanuel Vadot
113be82b3a0SEmmanuel Vadot static int
clknode_div_init(struct clknode * clk,device_t dev)114be82b3a0SEmmanuel Vadot clknode_div_init(struct clknode *clk, device_t dev)
115be82b3a0SEmmanuel Vadot {
116be82b3a0SEmmanuel Vadot uint32_t reg;
117be82b3a0SEmmanuel Vadot struct clknode_div_sc *sc;
118be82b3a0SEmmanuel Vadot uint32_t i_div, f_div;
119be82b3a0SEmmanuel Vadot int rv;
120be82b3a0SEmmanuel Vadot
121be82b3a0SEmmanuel Vadot sc = clknode_get_softc(clk);
122be82b3a0SEmmanuel Vadot
123be82b3a0SEmmanuel Vadot DEVICE_LOCK(clk);
124be82b3a0SEmmanuel Vadot rv = RD4(clk, sc->offset, ®);
125be82b3a0SEmmanuel Vadot DEVICE_UNLOCK(clk);
126be82b3a0SEmmanuel Vadot if (rv != 0)
127be82b3a0SEmmanuel Vadot return (rv);
128be82b3a0SEmmanuel Vadot
129be82b3a0SEmmanuel Vadot i_div = (reg >> sc->i_shift) & sc->i_mask;
130be82b3a0SEmmanuel Vadot if (!(sc->div_flags & CLK_DIV_WITH_TABLE) &&
131be82b3a0SEmmanuel Vadot !(sc->div_flags & CLK_DIV_ZERO_BASED))
132be82b3a0SEmmanuel Vadot i_div++;
133be82b3a0SEmmanuel Vadot f_div = (reg >> sc->f_shift) & sc->f_mask;
134be82b3a0SEmmanuel Vadot sc->divider = i_div << sc->f_width | f_div;
135be82b3a0SEmmanuel Vadot
136be82b3a0SEmmanuel Vadot sc->divider = clknode_div_table_get_divider(sc, sc->divider);
137be82b3a0SEmmanuel Vadot if (sc->divider == 0)
138be82b3a0SEmmanuel Vadot panic("%s: divider is zero!\n", clknode_get_name(clk));
139be82b3a0SEmmanuel Vadot
140be82b3a0SEmmanuel Vadot clknode_init_parent_idx(clk, 0);
141be82b3a0SEmmanuel Vadot return(0);
142be82b3a0SEmmanuel Vadot }
143be82b3a0SEmmanuel Vadot
144be82b3a0SEmmanuel Vadot static int
clknode_div_recalc(struct clknode * clk,uint64_t * freq)145be82b3a0SEmmanuel Vadot clknode_div_recalc(struct clknode *clk, uint64_t *freq)
146be82b3a0SEmmanuel Vadot {
147be82b3a0SEmmanuel Vadot struct clknode_div_sc *sc;
148be82b3a0SEmmanuel Vadot
149be82b3a0SEmmanuel Vadot sc = clknode_get_softc(clk);
150be82b3a0SEmmanuel Vadot if (sc->divider == 0) {
151be82b3a0SEmmanuel Vadot printf("%s: %s divider is zero!\n", clknode_get_name(clk),
152be82b3a0SEmmanuel Vadot __func__);
153be82b3a0SEmmanuel Vadot *freq = 0;
154be82b3a0SEmmanuel Vadot return(EINVAL);
155be82b3a0SEmmanuel Vadot }
156be82b3a0SEmmanuel Vadot *freq = (*freq << sc->f_width) / sc->divider;
157be82b3a0SEmmanuel Vadot return (0);
158be82b3a0SEmmanuel Vadot }
159be82b3a0SEmmanuel Vadot
160be82b3a0SEmmanuel Vadot static int
clknode_div_set_freq(struct clknode * clk,uint64_t fin,uint64_t * fout,int flags,int * stop)161be82b3a0SEmmanuel Vadot clknode_div_set_freq(struct clknode *clk, uint64_t fin, uint64_t *fout,
162be82b3a0SEmmanuel Vadot int flags, int *stop)
163be82b3a0SEmmanuel Vadot {
164be82b3a0SEmmanuel Vadot struct clknode_div_sc *sc;
165be82b3a0SEmmanuel Vadot uint64_t divider, _fin, _fout;
166be82b3a0SEmmanuel Vadot uint32_t reg, i_div, f_div, hw_i_div;
167be82b3a0SEmmanuel Vadot int rv;
168be82b3a0SEmmanuel Vadot
169be82b3a0SEmmanuel Vadot sc = clknode_get_softc(clk);
170be82b3a0SEmmanuel Vadot
171be82b3a0SEmmanuel Vadot /* For fractional divider. */
172be82b3a0SEmmanuel Vadot _fin = fin << sc->f_width;
173be82b3a0SEmmanuel Vadot divider = (_fin + *fout / 2) / *fout;
174be82b3a0SEmmanuel Vadot _fout = _fin / divider;
175be82b3a0SEmmanuel Vadot
176be82b3a0SEmmanuel Vadot /* Rounding. */
177be82b3a0SEmmanuel Vadot if ((flags & CLK_SET_ROUND_UP) && (*fout < _fout))
178be82b3a0SEmmanuel Vadot divider--;
179be82b3a0SEmmanuel Vadot else if ((flags & CLK_SET_ROUND_DOWN) && (*fout > _fout))
180be82b3a0SEmmanuel Vadot divider++;
181be82b3a0SEmmanuel Vadot
182be82b3a0SEmmanuel Vadot /* Break divider into integer and fractional parts. */
183be82b3a0SEmmanuel Vadot i_div = divider >> sc->f_width;
184be82b3a0SEmmanuel Vadot f_div = divider & sc->f_mask;
185be82b3a0SEmmanuel Vadot
186be82b3a0SEmmanuel Vadot if (i_div == 0) {
187be82b3a0SEmmanuel Vadot printf("%s: %s integer divider is zero!\n",
188be82b3a0SEmmanuel Vadot clknode_get_name(clk), __func__);
189be82b3a0SEmmanuel Vadot return(EINVAL);
190be82b3a0SEmmanuel Vadot }
191be82b3a0SEmmanuel Vadot
192be82b3a0SEmmanuel Vadot *stop = 1;
193be82b3a0SEmmanuel Vadot hw_i_div = i_div;
194be82b3a0SEmmanuel Vadot if (sc->div_flags & CLK_DIV_WITH_TABLE) {
195be82b3a0SEmmanuel Vadot if (clknode_div_table_get_value(sc, &hw_i_div) != 0)
196be82b3a0SEmmanuel Vadot return (ERANGE);
197be82b3a0SEmmanuel Vadot } else {
198be82b3a0SEmmanuel Vadot if (!(sc->div_flags & CLK_DIV_ZERO_BASED))
199be82b3a0SEmmanuel Vadot hw_i_div--;
200be82b3a0SEmmanuel Vadot
201be82b3a0SEmmanuel Vadot if (i_div > sc->i_mask) {
202be82b3a0SEmmanuel Vadot /* XXX Pass to parent or return error? */
203be82b3a0SEmmanuel Vadot printf("%s: %s integer divider is too big: %u\n",
204be82b3a0SEmmanuel Vadot clknode_get_name(clk), __func__, i_div);
205be82b3a0SEmmanuel Vadot hw_i_div = sc->i_mask;
206be82b3a0SEmmanuel Vadot *stop = 0;
207be82b3a0SEmmanuel Vadot }
208be82b3a0SEmmanuel Vadot i_div = hw_i_div;
209be82b3a0SEmmanuel Vadot if (!(sc->div_flags & CLK_DIV_ZERO_BASED))
210be82b3a0SEmmanuel Vadot i_div++;
211be82b3a0SEmmanuel Vadot }
212be82b3a0SEmmanuel Vadot
213be82b3a0SEmmanuel Vadot divider = i_div << sc->f_width | f_div;
214be82b3a0SEmmanuel Vadot
215be82b3a0SEmmanuel Vadot if ((flags & CLK_SET_DRYRUN) == 0) {
216be82b3a0SEmmanuel Vadot if ((*stop != 0) &&
217be82b3a0SEmmanuel Vadot ((flags & (CLK_SET_ROUND_UP | CLK_SET_ROUND_DOWN)) == 0) &&
218be82b3a0SEmmanuel Vadot (*fout != (_fin / divider)))
219be82b3a0SEmmanuel Vadot return (ERANGE);
220be82b3a0SEmmanuel Vadot
221be82b3a0SEmmanuel Vadot DEVICE_LOCK(clk);
222be82b3a0SEmmanuel Vadot rv = MD4(clk, sc->offset,
223be82b3a0SEmmanuel Vadot (sc->i_mask << sc->i_shift) | (sc->f_mask << sc->f_shift),
224be82b3a0SEmmanuel Vadot (hw_i_div << sc->i_shift) | (f_div << sc->f_shift));
225be82b3a0SEmmanuel Vadot if (rv != 0) {
226be82b3a0SEmmanuel Vadot DEVICE_UNLOCK(clk);
227be82b3a0SEmmanuel Vadot return (rv);
228be82b3a0SEmmanuel Vadot }
229be82b3a0SEmmanuel Vadot RD4(clk, sc->offset, ®);
230be82b3a0SEmmanuel Vadot DEVICE_UNLOCK(clk);
231be82b3a0SEmmanuel Vadot
232be82b3a0SEmmanuel Vadot sc->divider = divider;
233be82b3a0SEmmanuel Vadot }
234be82b3a0SEmmanuel Vadot
235be82b3a0SEmmanuel Vadot *fout = _fin / divider;
236be82b3a0SEmmanuel Vadot return (0);
237be82b3a0SEmmanuel Vadot }
238be82b3a0SEmmanuel Vadot
239be82b3a0SEmmanuel Vadot int
clknode_div_register(struct clkdom * clkdom,struct clk_div_def * clkdef)240be82b3a0SEmmanuel Vadot clknode_div_register(struct clkdom *clkdom, struct clk_div_def *clkdef)
241be82b3a0SEmmanuel Vadot {
242be82b3a0SEmmanuel Vadot struct clknode *clk;
243be82b3a0SEmmanuel Vadot struct clknode_div_sc *sc;
244be82b3a0SEmmanuel Vadot
245be82b3a0SEmmanuel Vadot clk = clknode_create(clkdom, &clknode_div_class, &clkdef->clkdef);
246be82b3a0SEmmanuel Vadot if (clk == NULL)
247be82b3a0SEmmanuel Vadot return (1);
248be82b3a0SEmmanuel Vadot
249be82b3a0SEmmanuel Vadot sc = clknode_get_softc(clk);
250be82b3a0SEmmanuel Vadot sc->offset = clkdef->offset;
251be82b3a0SEmmanuel Vadot sc->i_shift = clkdef->i_shift;
252be82b3a0SEmmanuel Vadot sc->i_width = clkdef->i_width;
253be82b3a0SEmmanuel Vadot sc->i_mask = (1 << clkdef->i_width) - 1;
254be82b3a0SEmmanuel Vadot sc->f_shift = clkdef->f_shift;
255be82b3a0SEmmanuel Vadot sc->f_width = clkdef->f_width;
256be82b3a0SEmmanuel Vadot sc->f_mask = (1 << clkdef->f_width) - 1;
257be82b3a0SEmmanuel Vadot sc->div_flags = clkdef->div_flags;
258be82b3a0SEmmanuel Vadot sc->div_table = clkdef->div_table;
259be82b3a0SEmmanuel Vadot
260be82b3a0SEmmanuel Vadot clknode_register(clkdom, clk);
261be82b3a0SEmmanuel Vadot return (0);
262be82b3a0SEmmanuel Vadot }
263