2 * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
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 AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, 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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
37 #include <machine/bus.h>
39 #include <dev/extres/clk/clk_div.h>
41 #include "clkdev_if.h"
43 #define WR4(_clk, off, val) \
44 CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
45 #define RD4(_clk, off, val) \
46 CLKDEV_READ_4(clknode_get_device(_clk), off, val)
47 #define MD4(_clk, off, clr, set ) \
48 CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set)
49 #define DEVICE_LOCK(_clk) \
50 CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))
51 #define DEVICE_UNLOCK(_clk) \
52 CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))
54 static int clknode_div_init(struct clknode *clk, device_t dev);
55 static int clknode_div_recalc(struct clknode *clk, uint64_t *req);
56 static int clknode_div_set_freq(struct clknode *clknode, uint64_t fin,
57 uint64_t *fout, int flag, int *stop);
59 struct clknode_div_sc {
61 struct resource *mem_res;
70 uint32_t divider; /* in natural form */
72 struct clk_div_table *div_table;
75 static clknode_method_t clknode_div_methods[] = {
76 /* Device interface */
77 CLKNODEMETHOD(clknode_init, clknode_div_init),
78 CLKNODEMETHOD(clknode_recalc_freq, clknode_div_recalc),
79 CLKNODEMETHOD(clknode_set_freq, clknode_div_set_freq),
82 DEFINE_CLASS_1(clknode_div, clknode_div_class, clknode_div_methods,
83 sizeof(struct clknode_div_sc), clknode_class);
86 clknode_div_table_get_divider(struct clknode_div_sc *sc, uint32_t divider)
88 struct clk_div_table *table;
90 if (!(sc->div_flags & CLK_DIV_WITH_TABLE))
93 for (table = sc->div_table; table->divider != 0; table++)
94 if (table->value == sc->divider)
95 return (table->divider);
101 clknode_div_table_get_value(struct clknode_div_sc *sc, uint32_t divider)
103 struct clk_div_table *table;
105 if (!(sc->div_flags & CLK_DIV_WITH_TABLE))
108 for (table = sc->div_table; table->divider != 0; table++)
109 if (table->divider == sc->divider)
110 return (table->value);
116 clknode_div_init(struct clknode *clk, device_t dev)
119 struct clknode_div_sc *sc;
120 uint32_t i_div, f_div;
123 sc = clknode_get_softc(clk);
126 rv = RD4(clk, sc->offset, ®);
131 i_div = (reg >> sc->i_shift) & sc->i_mask;
132 if (!(sc->div_flags & CLK_DIV_ZERO_BASED))
134 f_div = (reg >> sc->f_shift) & sc->f_mask;
135 sc->divider = i_div << sc->f_width | f_div;
137 sc->divider = clknode_div_table_get_divider(sc, sc->divider);
138 if (sc->divider == 0)
139 panic("%s: divider is zero!\n", clknode_get_name(clk));
141 clknode_init_parent_idx(clk, 0);
146 clknode_div_recalc(struct clknode *clk, uint64_t *freq)
148 struct clknode_div_sc *sc;
150 sc = clknode_get_softc(clk);
151 if (sc->divider == 0) {
152 printf("%s: %s divider is zero!\n", clknode_get_name(clk),
157 *freq = (*freq << sc->f_width) / sc->divider;
162 clknode_div_set_freq(struct clknode *clk, uint64_t fin, uint64_t *fout,
163 int flags, int *stop)
165 struct clknode_div_sc *sc;
166 uint64_t divider, _fin, _fout;
167 uint32_t reg, i_div, f_div, hw_i_div;
170 sc = clknode_get_softc(clk);
172 /* For fractional divider. */
173 _fin = fin << sc->f_width;
174 divider = (_fin + *fout / 2) / *fout;
175 _fout = _fin / divider;
178 if ((flags & CLK_SET_ROUND_UP) && (*fout < _fout))
180 else if ((flags & CLK_SET_ROUND_DOWN) && (*fout > _fout))
183 /* Break divider into integer and fractional parts. */
184 i_div = divider >> sc->f_width;
185 f_div = divider & sc->f_mask;
188 printf("%s: %s integer divider is zero!\n",
189 clknode_get_name(clk), __func__);
194 if (!(sc->div_flags & CLK_DIV_ZERO_BASED))
198 if (hw_i_div > sc->i_mask &&
199 ((sc->div_flags & CLK_DIV_WITH_TABLE) == 0)) {
200 /* XXX Or only return error? */
201 printf("%s: %s integer divider is too big: %u\n",
202 clknode_get_name(clk), __func__, hw_i_div);
203 hw_i_div = sc->i_mask;
208 if (!(sc->div_flags & CLK_DIV_ZERO_BASED))
210 divider = i_div << sc->f_width | f_div;
212 if ((flags & CLK_SET_DRYRUN) == 0) {
214 ((flags & (CLK_SET_ROUND_UP | CLK_SET_ROUND_DOWN)) == 0) &&
215 (*fout != (_fin / divider)))
218 divider = clknode_div_table_get_value(sc, divider);
223 rv = MD4(clk, sc->offset,
224 (sc->i_mask << sc->i_shift) | (sc->f_mask << sc->f_shift),
225 (i_div << sc->i_shift) | (f_div << sc->f_shift));
230 RD4(clk, sc->offset, ®);
233 sc->divider = divider;
236 *fout = _fin / divider;
241 clknode_div_register(struct clkdom *clkdom, struct clk_div_def *clkdef)
244 struct clknode_div_sc *sc;
246 clk = clknode_create(clkdom, &clknode_div_class, &clkdef->clkdef);
250 sc = clknode_get_softc(clk);
251 sc->offset = clkdef->offset;
252 sc->i_shift = clkdef->i_shift;
253 sc->i_width = clkdef->i_width;
254 sc->i_mask = (1 << clkdef->i_width) - 1;
255 sc->f_shift = clkdef->f_shift;
256 sc->f_width = clkdef->f_width;
257 sc->f_mask = (1 << clkdef->f_width) - 1;
258 sc->div_flags = clkdef->div_flags;
259 sc->div_table = clkdef->div_table;
261 clknode_register(clkdom, clk);