1 /*-
2 * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/conf.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33
34 #include <machine/bus.h>
35
36 #include <dev/extres/clk/clk_div.h>
37
38 #include "clkdev_if.h"
39
40 #define WR4(_clk, off, val) \
41 CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
42 #define RD4(_clk, off, val) \
43 CLKDEV_READ_4(clknode_get_device(_clk), off, val)
44 #define MD4(_clk, off, clr, set ) \
45 CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set)
46 #define DEVICE_LOCK(_clk) \
47 CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))
48 #define DEVICE_UNLOCK(_clk) \
49 CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))
50
51 static int clknode_div_init(struct clknode *clk, device_t dev);
52 static int clknode_div_recalc(struct clknode *clk, uint64_t *req);
53 static int clknode_div_set_freq(struct clknode *clknode, uint64_t fin,
54 uint64_t *fout, int flag, int *stop);
55
56 struct clknode_div_sc {
57 struct mtx *mtx;
58 struct resource *mem_res;
59 uint32_t offset;
60 uint32_t i_shift;
61 uint32_t i_mask;
62 uint32_t i_width;
63 uint32_t f_shift;
64 uint32_t f_mask;
65 uint32_t f_width;
66 int div_flags;
67 uint32_t divider; /* in natural form */
68
69 struct clk_div_table *div_table;
70 };
71
72 static clknode_method_t clknode_div_methods[] = {
73 /* Device interface */
74 CLKNODEMETHOD(clknode_init, clknode_div_init),
75 CLKNODEMETHOD(clknode_recalc_freq, clknode_div_recalc),
76 CLKNODEMETHOD(clknode_set_freq, clknode_div_set_freq),
77 CLKNODEMETHOD_END
78 };
79 DEFINE_CLASS_1(clknode_div, clknode_div_class, clknode_div_methods,
80 sizeof(struct clknode_div_sc), clknode_class);
81
82 static uint32_t
clknode_div_table_get_divider(struct clknode_div_sc * sc,uint32_t divider)83 clknode_div_table_get_divider(struct clknode_div_sc *sc, uint32_t divider)
84 {
85 struct clk_div_table *table;
86
87 if (!(sc->div_flags & CLK_DIV_WITH_TABLE))
88 return (divider);
89
90 for (table = sc->div_table; table->divider != 0; table++)
91 if (table->value == sc->divider)
92 return (table->divider);
93
94 return (0);
95 }
96
97 static int
clknode_div_table_get_value(struct clknode_div_sc * sc,uint32_t * divider)98 clknode_div_table_get_value(struct clknode_div_sc *sc, uint32_t *divider)
99 {
100 struct clk_div_table *table;
101
102 if (!(sc->div_flags & CLK_DIV_WITH_TABLE))
103 return (0);
104
105 for (table = sc->div_table; table->divider != 0; table++)
106 if (table->divider == *divider) {
107 *divider = table->value;
108 return (0);
109 }
110
111 return (ENOENT);
112 }
113
114 static int
clknode_div_init(struct clknode * clk,device_t dev)115 clknode_div_init(struct clknode *clk, device_t dev)
116 {
117 uint32_t reg;
118 struct clknode_div_sc *sc;
119 uint32_t i_div, f_div;
120 int rv;
121
122 sc = clknode_get_softc(clk);
123
124 DEVICE_LOCK(clk);
125 rv = RD4(clk, sc->offset, ®);
126 DEVICE_UNLOCK(clk);
127 if (rv != 0)
128 return (rv);
129
130 i_div = (reg >> sc->i_shift) & sc->i_mask;
131 if (!(sc->div_flags & CLK_DIV_WITH_TABLE) &&
132 !(sc->div_flags & CLK_DIV_ZERO_BASED))
133 i_div++;
134 f_div = (reg >> sc->f_shift) & sc->f_mask;
135 sc->divider = i_div << sc->f_width | f_div;
136
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));
140
141 clknode_init_parent_idx(clk, 0);
142 return(0);
143 }
144
145 static int
clknode_div_recalc(struct clknode * clk,uint64_t * freq)146 clknode_div_recalc(struct clknode *clk, uint64_t *freq)
147 {
148 struct clknode_div_sc *sc;
149
150 sc = clknode_get_softc(clk);
151 if (sc->divider == 0) {
152 printf("%s: %s divider is zero!\n", clknode_get_name(clk),
153 __func__);
154 *freq = 0;
155 return(EINVAL);
156 }
157 *freq = (*freq << sc->f_width) / sc->divider;
158 return (0);
159 }
160
161 static int
clknode_div_set_freq(struct clknode * clk,uint64_t fin,uint64_t * fout,int flags,int * stop)162 clknode_div_set_freq(struct clknode *clk, uint64_t fin, uint64_t *fout,
163 int flags, int *stop)
164 {
165 struct clknode_div_sc *sc;
166 uint64_t divider, _fin, _fout;
167 uint32_t reg, i_div, f_div, hw_i_div;
168 int rv;
169
170 sc = clknode_get_softc(clk);
171
172 /* For fractional divider. */
173 _fin = fin << sc->f_width;
174 divider = (_fin + *fout / 2) / *fout;
175 _fout = _fin / divider;
176
177 /* Rounding. */
178 if ((flags & CLK_SET_ROUND_UP) && (*fout < _fout))
179 divider--;
180 else if ((flags & CLK_SET_ROUND_DOWN) && (*fout > _fout))
181 divider++;
182
183 /* Break divider into integer and fractional parts. */
184 i_div = divider >> sc->f_width;
185 f_div = divider & sc->f_mask;
186
187 if (i_div == 0) {
188 printf("%s: %s integer divider is zero!\n",
189 clknode_get_name(clk), __func__);
190 return(EINVAL);
191 }
192
193 *stop = 1;
194 hw_i_div = i_div;
195 if (sc->div_flags & CLK_DIV_WITH_TABLE) {
196 if (clknode_div_table_get_value(sc, &hw_i_div) != 0)
197 return (ERANGE);
198 } else {
199 if (!(sc->div_flags & CLK_DIV_ZERO_BASED))
200 hw_i_div--;
201
202 if (i_div > sc->i_mask) {
203 /* XXX Pass to parent or return error? */
204 printf("%s: %s integer divider is too big: %u\n",
205 clknode_get_name(clk), __func__, i_div);
206 hw_i_div = sc->i_mask;
207 *stop = 0;
208 }
209 i_div = hw_i_div;
210 if (!(sc->div_flags & CLK_DIV_ZERO_BASED))
211 i_div++;
212 }
213
214 divider = i_div << sc->f_width | f_div;
215
216 if ((flags & CLK_SET_DRYRUN) == 0) {
217 if ((*stop != 0) &&
218 ((flags & (CLK_SET_ROUND_UP | CLK_SET_ROUND_DOWN)) == 0) &&
219 (*fout != (_fin / divider)))
220 return (ERANGE);
221
222 DEVICE_LOCK(clk);
223 rv = MD4(clk, sc->offset,
224 (sc->i_mask << sc->i_shift) | (sc->f_mask << sc->f_shift),
225 (hw_i_div << sc->i_shift) | (f_div << sc->f_shift));
226 if (rv != 0) {
227 DEVICE_UNLOCK(clk);
228 return (rv);
229 }
230 RD4(clk, sc->offset, ®);
231 DEVICE_UNLOCK(clk);
232
233 sc->divider = divider;
234 }
235
236 *fout = _fin / divider;
237 return (0);
238 }
239
240 int
clknode_div_register(struct clkdom * clkdom,struct clk_div_def * clkdef)241 clknode_div_register(struct clkdom *clkdom, struct clk_div_def *clkdef)
242 {
243 struct clknode *clk;
244 struct clknode_div_sc *sc;
245
246 clk = clknode_create(clkdom, &clknode_div_class, &clkdef->clkdef);
247 if (clk == NULL)
248 return (1);
249
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;
260
261 clknode_register(clkdom, clk);
262 return (0);
263 }
264