xref: /freebsd/sys/dev/clk/allwinner/aw_clk_frac.c (revision be82b3a0)
1e37e8677SEmmanuel Vadot /*-
2e37e8677SEmmanuel Vadot  * Copyright (c) 2019 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 
30be82b3a0SEmmanuel 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_frac.h>
34e37e8677SEmmanuel Vadot 
35e37e8677SEmmanuel Vadot #include "clkdev_if.h"
36e37e8677SEmmanuel Vadot 
37e37e8677SEmmanuel Vadot /* #define	dprintf(format, arg...)	printf("%s:(%s)" format, __func__, clknode_get_name(clk), arg) */
38e37e8677SEmmanuel Vadot #define	dprintf(format, arg...)
39e37e8677SEmmanuel Vadot 
40e37e8677SEmmanuel Vadot /*
41e37e8677SEmmanuel Vadot  * clknode for clocks matching the formula :
42e37e8677SEmmanuel Vadot  *
43e37e8677SEmmanuel Vadot  * clk = (24Mhz * n) / m in integer mode
44e37e8677SEmmanuel Vadot  * clk = frac_out1 or frac_out2 in fractional mode
45e37e8677SEmmanuel Vadot  *
46e37e8677SEmmanuel Vadot  */
47e37e8677SEmmanuel Vadot 
48e37e8677SEmmanuel Vadot struct aw_clk_frac_sc {
49e37e8677SEmmanuel Vadot 	uint32_t	offset;
50e37e8677SEmmanuel Vadot 
51e37e8677SEmmanuel Vadot 	struct aw_clk_factor	m;
52e37e8677SEmmanuel Vadot 	struct aw_clk_factor	n;
53e37e8677SEmmanuel Vadot 	struct aw_clk_frac	frac;
54e37e8677SEmmanuel Vadot 
55e37e8677SEmmanuel Vadot 	uint64_t		min_freq;
56e37e8677SEmmanuel Vadot 	uint64_t		max_freq;
57e37e8677SEmmanuel Vadot 
58e37e8677SEmmanuel Vadot 	uint32_t	mux_shift;
59e37e8677SEmmanuel Vadot 	uint32_t	mux_mask;
60e37e8677SEmmanuel Vadot 	uint32_t	gate_shift;
61e37e8677SEmmanuel Vadot 	uint32_t	lock_shift;
62e37e8677SEmmanuel Vadot 	uint32_t	lock_retries;
63e37e8677SEmmanuel Vadot 
64e37e8677SEmmanuel Vadot 	uint32_t	flags;
65e37e8677SEmmanuel Vadot };
66e37e8677SEmmanuel Vadot 
67e37e8677SEmmanuel Vadot #define	WRITE4(_clk, off, val)						\
68e37e8677SEmmanuel Vadot 	CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
69e37e8677SEmmanuel Vadot #define	READ4(_clk, off, val)						\
70e37e8677SEmmanuel Vadot 	CLKDEV_READ_4(clknode_get_device(_clk), off, val)
71e37e8677SEmmanuel Vadot #define	DEVICE_LOCK(_clk)							\
72e37e8677SEmmanuel Vadot 	CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))
73e37e8677SEmmanuel Vadot #define	DEVICE_UNLOCK(_clk)						\
74e37e8677SEmmanuel Vadot 	CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))
75e37e8677SEmmanuel Vadot 
76e37e8677SEmmanuel Vadot static int
aw_clk_frac_init(struct clknode * clk,device_t dev)77e37e8677SEmmanuel Vadot aw_clk_frac_init(struct clknode *clk, device_t dev)
78e37e8677SEmmanuel Vadot {
79e37e8677SEmmanuel Vadot 	struct aw_clk_frac_sc *sc;
80e37e8677SEmmanuel Vadot 	uint32_t val, idx;
81e37e8677SEmmanuel Vadot 
82e37e8677SEmmanuel Vadot 	sc = clknode_get_softc(clk);
83e37e8677SEmmanuel Vadot 
84e37e8677SEmmanuel Vadot 	idx = 0;
85e37e8677SEmmanuel Vadot 	if ((sc->flags & AW_CLK_HAS_MUX) != 0) {
86e37e8677SEmmanuel Vadot 		DEVICE_LOCK(clk);
87e37e8677SEmmanuel Vadot 		READ4(clk, sc->offset, &val);
88e37e8677SEmmanuel Vadot 		DEVICE_UNLOCK(clk);
89e37e8677SEmmanuel Vadot 
90e37e8677SEmmanuel Vadot 		idx = (val & sc->mux_mask) >> sc->mux_shift;
91e37e8677SEmmanuel Vadot 	}
92e37e8677SEmmanuel Vadot 
93e37e8677SEmmanuel Vadot 	dprintf("init parent idx %d\n", idx);
94e37e8677SEmmanuel Vadot 	clknode_init_parent_idx(clk, idx);
95e37e8677SEmmanuel Vadot 	return (0);
96e37e8677SEmmanuel Vadot }
97e37e8677SEmmanuel Vadot 
98e37e8677SEmmanuel Vadot static int
aw_clk_frac_set_gate(struct clknode * clk,bool enable)99e37e8677SEmmanuel Vadot aw_clk_frac_set_gate(struct clknode *clk, bool enable)
100e37e8677SEmmanuel Vadot {
101e37e8677SEmmanuel Vadot 	struct aw_clk_frac_sc *sc;
102e37e8677SEmmanuel Vadot 	uint32_t val;
103e37e8677SEmmanuel Vadot 
104e37e8677SEmmanuel Vadot 	sc = clknode_get_softc(clk);
105e37e8677SEmmanuel Vadot 
106e37e8677SEmmanuel Vadot 	if ((sc->flags & AW_CLK_HAS_GATE) == 0)
107e37e8677SEmmanuel Vadot 		return (0);
108e37e8677SEmmanuel Vadot 
109e37e8677SEmmanuel Vadot 	dprintf("%sabling gate\n", enable ? "En" : "Dis");
110e37e8677SEmmanuel Vadot 	DEVICE_LOCK(clk);
111e37e8677SEmmanuel Vadot 	READ4(clk, sc->offset, &val);
112e37e8677SEmmanuel Vadot 	if (enable)
113e37e8677SEmmanuel Vadot 		val |= (1 << sc->gate_shift);
114e37e8677SEmmanuel Vadot 	else
115e37e8677SEmmanuel Vadot 		val &= ~(1 << sc->gate_shift);
116e37e8677SEmmanuel Vadot 	WRITE4(clk, sc->offset, val);
117e37e8677SEmmanuel Vadot 	DEVICE_UNLOCK(clk);
118e37e8677SEmmanuel Vadot 
119e37e8677SEmmanuel Vadot 	return (0);
120e37e8677SEmmanuel Vadot }
121e37e8677SEmmanuel Vadot 
122e37e8677SEmmanuel Vadot static int
aw_clk_frac_set_mux(struct clknode * clk,int index)123e37e8677SEmmanuel Vadot aw_clk_frac_set_mux(struct clknode *clk, int index)
124e37e8677SEmmanuel Vadot {
125e37e8677SEmmanuel Vadot 	struct aw_clk_frac_sc *sc;
126e37e8677SEmmanuel Vadot 	uint32_t val;
127e37e8677SEmmanuel Vadot 
128e37e8677SEmmanuel Vadot 	sc = clknode_get_softc(clk);
129e37e8677SEmmanuel Vadot 
130e37e8677SEmmanuel Vadot 	if ((sc->flags & AW_CLK_HAS_MUX) == 0)
131e37e8677SEmmanuel Vadot 		return (0);
132e37e8677SEmmanuel Vadot 
133e37e8677SEmmanuel Vadot 	dprintf("Set mux to %d\n", index);
134e37e8677SEmmanuel Vadot 	DEVICE_LOCK(clk);
135e37e8677SEmmanuel Vadot 	READ4(clk, sc->offset, &val);
136e37e8677SEmmanuel Vadot 	val &= ~sc->mux_mask;
137e37e8677SEmmanuel Vadot 	val |= index << sc->mux_shift;
138e37e8677SEmmanuel Vadot 	WRITE4(clk, sc->offset, val);
139e37e8677SEmmanuel Vadot 	DEVICE_UNLOCK(clk);
140e37e8677SEmmanuel Vadot 
141e37e8677SEmmanuel Vadot 	return (0);
142e37e8677SEmmanuel Vadot }
143e37e8677SEmmanuel Vadot 
144e37e8677SEmmanuel Vadot static uint64_t
aw_clk_frac_find_best(struct aw_clk_frac_sc * sc,uint64_t fparent,uint64_t fout,uint32_t * factor_n,uint32_t * factor_m)145e37e8677SEmmanuel Vadot aw_clk_frac_find_best(struct aw_clk_frac_sc *sc, uint64_t fparent, uint64_t fout,
146e37e8677SEmmanuel Vadot     uint32_t *factor_n, uint32_t *factor_m)
147e37e8677SEmmanuel Vadot {
148e37e8677SEmmanuel Vadot 	uint64_t cur, best;
149e37e8677SEmmanuel Vadot 	uint32_t m, n, max_m, max_n, min_m, min_n;
150e37e8677SEmmanuel Vadot 
151e37e8677SEmmanuel Vadot 	*factor_n = *factor_m = 0;
152e37e8677SEmmanuel Vadot 	best = cur = 0;
153e37e8677SEmmanuel Vadot 
154e37e8677SEmmanuel Vadot 	max_m = aw_clk_factor_get_max(&sc->m);
155e37e8677SEmmanuel Vadot 	max_n = aw_clk_factor_get_max(&sc->n);
156e37e8677SEmmanuel Vadot 	min_m = aw_clk_factor_get_min(&sc->m);
157e37e8677SEmmanuel Vadot 	min_n = sc->min_freq / fparent;
158e37e8677SEmmanuel Vadot 
159e37e8677SEmmanuel Vadot 	for (n = min_n; n <= max_n; n++) {
160e37e8677SEmmanuel Vadot 		for (m = min_m; m <= max_m; m++) {
161e37e8677SEmmanuel Vadot 			cur = fparent * n / m;
162e37e8677SEmmanuel Vadot 			if (cur < sc->min_freq) {
163e37e8677SEmmanuel Vadot 				continue;
164e37e8677SEmmanuel Vadot 			}
165e37e8677SEmmanuel Vadot 			if (cur > sc->max_freq) {
166e37e8677SEmmanuel Vadot 				continue;
167e37e8677SEmmanuel Vadot 			}
168e37e8677SEmmanuel Vadot 			if (cur == fout) {
169e37e8677SEmmanuel Vadot 				*factor_n = n;
170e37e8677SEmmanuel Vadot 				*factor_m = m;
171e37e8677SEmmanuel Vadot 				return (cur);
172e37e8677SEmmanuel Vadot 			}
173e37e8677SEmmanuel Vadot 			if (abs((fout - cur)) < abs((fout - best))) {
174e37e8677SEmmanuel Vadot 				best = cur;
175e37e8677SEmmanuel Vadot 				*factor_n = n;
176e37e8677SEmmanuel Vadot 				*factor_m = m;
177e37e8677SEmmanuel Vadot 			}
178e37e8677SEmmanuel Vadot 		}
179e37e8677SEmmanuel Vadot 	}
180e37e8677SEmmanuel Vadot 
181e37e8677SEmmanuel Vadot 	return (best);
182e37e8677SEmmanuel Vadot }
183e37e8677SEmmanuel Vadot 
184e37e8677SEmmanuel Vadot static int
aw_clk_frac_set_freq(struct clknode * clk,uint64_t fparent,uint64_t * fout,int flags,int * stop)185e37e8677SEmmanuel Vadot aw_clk_frac_set_freq(struct clknode *clk, uint64_t fparent, uint64_t *fout,
186e37e8677SEmmanuel Vadot     int flags, int *stop)
187e37e8677SEmmanuel Vadot {
188e37e8677SEmmanuel Vadot 	struct aw_clk_frac_sc *sc;
189e37e8677SEmmanuel Vadot 	uint64_t cur, best, best_frac;
190e37e8677SEmmanuel Vadot 	uint32_t val, m, n, best_m, best_n;
191e37e8677SEmmanuel Vadot 	int retry, multiple, max_mult, best_mult;
192e37e8677SEmmanuel Vadot 
193e37e8677SEmmanuel Vadot 	sc = clknode_get_softc(clk);
194e37e8677SEmmanuel Vadot 
195e37e8677SEmmanuel Vadot 	best = best_frac = cur = 0;
196e37e8677SEmmanuel Vadot 	best_mult = 0;
197e37e8677SEmmanuel Vadot 	max_mult = 1;
198e37e8677SEmmanuel Vadot 
199e37e8677SEmmanuel Vadot 	dprintf("Trying to find freq %ju with parent %ju\n", *fout, fparent);
200e37e8677SEmmanuel Vadot 	if ((flags & CLK_SET_ROUND_MULTIPLE) != 0)
201e37e8677SEmmanuel Vadot 		max_mult = 10;
202e37e8677SEmmanuel Vadot 	for (multiple = 1; multiple <= max_mult; multiple++) {
203e37e8677SEmmanuel Vadot 		/* First test the fractional frequencies */
204e37e8677SEmmanuel Vadot 		dprintf("Testing with multiple %d\n", multiple);
205e37e8677SEmmanuel Vadot 		if (*fout * multiple == sc->frac.freq0) {
206e37e8677SEmmanuel Vadot 			best = best_frac = sc->frac.freq0;
207e37e8677SEmmanuel Vadot 			best_mult = multiple;
208e37e8677SEmmanuel Vadot 			dprintf("Found with using frac.freq0 and multiple %d\n", multiple);
209e37e8677SEmmanuel Vadot 			break;
210e37e8677SEmmanuel Vadot 		}
211e37e8677SEmmanuel Vadot 		else if (*fout * multiple == sc->frac.freq1) {
212e37e8677SEmmanuel Vadot 			best = best_frac = sc->frac.freq1;
213e37e8677SEmmanuel Vadot 			best_mult = multiple;
214e37e8677SEmmanuel Vadot 			dprintf("Found with using frac.freq1 and multiple %d\n", multiple);
215e37e8677SEmmanuel Vadot 			break;
216e37e8677SEmmanuel Vadot 		}
217e37e8677SEmmanuel Vadot 		else {
218e37e8677SEmmanuel Vadot 			cur = aw_clk_frac_find_best(sc, fparent, *fout * multiple,
219e37e8677SEmmanuel Vadot 			  &n, &m);
220e37e8677SEmmanuel Vadot 			dprintf("Got %ju with n=%d, m=%d\n", cur, n, m);
221e37e8677SEmmanuel Vadot 			if (cur == (*fout * multiple)) {
222e37e8677SEmmanuel Vadot 				best = cur;
223e37e8677SEmmanuel Vadot 				best_mult = multiple;
224e37e8677SEmmanuel Vadot 				best_n = n;
225e37e8677SEmmanuel Vadot 				best_m = m;
226e37e8677SEmmanuel Vadot 				dprintf("This is the one: n=%d m=%d mult=%d\n", best_n, best_m, best_mult);
227e37e8677SEmmanuel Vadot 				break;
228e37e8677SEmmanuel Vadot 			}
229e37e8677SEmmanuel Vadot 			if (abs(((*fout * multiple) - cur)) < abs(((*fout * multiple) - best))) {
230e37e8677SEmmanuel Vadot 				best = cur;
231e37e8677SEmmanuel Vadot 				best_mult = multiple;
232e37e8677SEmmanuel Vadot 				best_n = n;
233e37e8677SEmmanuel Vadot 				best_m = m;
234e37e8677SEmmanuel Vadot 				dprintf("This is the best for now: n=%d m=%d mult=%d\n", best_n, best_m, best_mult);
235e37e8677SEmmanuel Vadot 			}
236e37e8677SEmmanuel Vadot 		}
237e37e8677SEmmanuel Vadot 	}
238e37e8677SEmmanuel Vadot 
239e37e8677SEmmanuel Vadot 	if (best < sc->min_freq ||
240e37e8677SEmmanuel Vadot 	    best > sc->max_freq) {
241e37e8677SEmmanuel Vadot 		printf("%s: Cannot set %ju for %s (min=%ju max=%ju)\n",
242e37e8677SEmmanuel Vadot 		    __func__, best, clknode_get_name(clk),
243e37e8677SEmmanuel Vadot 		    sc->min_freq, sc->max_freq);
244e37e8677SEmmanuel Vadot 		return (ERANGE);
245e37e8677SEmmanuel Vadot 	}
246e37e8677SEmmanuel Vadot 	if ((flags & CLK_SET_DRYRUN) != 0) {
247e37e8677SEmmanuel Vadot 		*fout = best;
248e37e8677SEmmanuel Vadot 		*stop = 1;
249e37e8677SEmmanuel Vadot 		return (0);
250e37e8677SEmmanuel Vadot 	}
251e37e8677SEmmanuel Vadot 
252e37e8677SEmmanuel Vadot 	if ((best < (*fout * best_mult)) &&
253e37e8677SEmmanuel Vadot 	  ((flags & CLK_SET_ROUND_DOWN) == 0)) {
254e37e8677SEmmanuel Vadot 		*stop = 1;
255e37e8677SEmmanuel Vadot 		return (ERANGE);
256e37e8677SEmmanuel Vadot 	}
257e37e8677SEmmanuel Vadot 	if ((best > *fout * best_mult) &&
258e37e8677SEmmanuel Vadot 	  ((flags & CLK_SET_ROUND_UP) == 0)) {
259e37e8677SEmmanuel Vadot 		*stop = 1;
260e37e8677SEmmanuel Vadot 		return (ERANGE);
261e37e8677SEmmanuel Vadot 	}
262e37e8677SEmmanuel Vadot 
263e37e8677SEmmanuel Vadot 	DEVICE_LOCK(clk);
264e37e8677SEmmanuel Vadot 	READ4(clk, sc->offset, &val);
265e37e8677SEmmanuel Vadot 	/* Disable clock during freq changes */
266e37e8677SEmmanuel Vadot 	val &= ~(1 << sc->gate_shift);
267e37e8677SEmmanuel Vadot 	WRITE4(clk, sc->offset, val);
268e37e8677SEmmanuel Vadot 
269e37e8677SEmmanuel Vadot 	if (best_frac != 0) {
270e37e8677SEmmanuel Vadot 		val &= ~sc->frac.mode_sel;
271e37e8677SEmmanuel Vadot 		/* M should be 0 per the manual */
272e37e8677SEmmanuel Vadot 		val &= ~sc->m.mask;
273e37e8677SEmmanuel Vadot 		if (best_frac == sc->frac.freq0)
274e37e8677SEmmanuel Vadot 			val &= ~sc->frac.freq_sel;
275e37e8677SEmmanuel Vadot 		else
276e37e8677SEmmanuel Vadot 			val |= sc->frac.freq_sel;
277e37e8677SEmmanuel Vadot 	} else {
278e37e8677SEmmanuel Vadot 		val |= sc->frac.mode_sel; /* Select integer mode */
279e37e8677SEmmanuel Vadot 		n = aw_clk_factor_get_value(&sc->n, best_n);
280e37e8677SEmmanuel Vadot 		m = aw_clk_factor_get_value(&sc->m, best_m);
281e37e8677SEmmanuel Vadot 		val &= ~sc->n.mask;
282e37e8677SEmmanuel Vadot 		val &= ~sc->m.mask;
283e37e8677SEmmanuel Vadot 		val |= n << sc->n.shift;
284e37e8677SEmmanuel Vadot 		val |= m << sc->m.shift;
285e37e8677SEmmanuel Vadot 	}
286e37e8677SEmmanuel Vadot 
287e37e8677SEmmanuel Vadot 	/* Write the clock changes */
288e37e8677SEmmanuel Vadot 	WRITE4(clk, sc->offset, val);
289e37e8677SEmmanuel Vadot 
290e37e8677SEmmanuel Vadot 	/* Enable clock now that we've change it */
291e37e8677SEmmanuel Vadot 	val |= 1 << sc->gate_shift;
292e37e8677SEmmanuel Vadot 	WRITE4(clk, sc->offset, val);
293e37e8677SEmmanuel Vadot 	DEVICE_UNLOCK(clk);
294e37e8677SEmmanuel Vadot 
295e37e8677SEmmanuel Vadot 	for (retry = 0; retry < sc->lock_retries; retry++) {
296e37e8677SEmmanuel Vadot 		READ4(clk, sc->offset, &val);
297e37e8677SEmmanuel Vadot 		if ((val & (1 << sc->lock_shift)) != 0)
298e37e8677SEmmanuel Vadot 			break;
299e37e8677SEmmanuel Vadot 		DELAY(1000);
300e37e8677SEmmanuel Vadot 	}
301e37e8677SEmmanuel Vadot 
302e37e8677SEmmanuel Vadot 	*fout = best;
303e37e8677SEmmanuel Vadot 	*stop = 1;
304e37e8677SEmmanuel Vadot 
305e37e8677SEmmanuel Vadot 	return (0);
306e37e8677SEmmanuel Vadot }
307e37e8677SEmmanuel Vadot 
308e37e8677SEmmanuel Vadot static int
aw_clk_frac_recalc(struct clknode * clk,uint64_t * freq)309e37e8677SEmmanuel Vadot aw_clk_frac_recalc(struct clknode *clk, uint64_t *freq)
310e37e8677SEmmanuel Vadot {
311e37e8677SEmmanuel Vadot 	struct aw_clk_frac_sc *sc;
312e37e8677SEmmanuel Vadot 	uint32_t val, m, n;
313e37e8677SEmmanuel Vadot 
314e37e8677SEmmanuel Vadot 	sc = clknode_get_softc(clk);
315e37e8677SEmmanuel Vadot 
316e37e8677SEmmanuel Vadot 	DEVICE_LOCK(clk);
317e37e8677SEmmanuel Vadot 	READ4(clk, sc->offset, &val);
318e37e8677SEmmanuel Vadot 	DEVICE_UNLOCK(clk);
319e37e8677SEmmanuel Vadot 
320e37e8677SEmmanuel Vadot 	if ((val & sc->frac.mode_sel) == 0) {
321e37e8677SEmmanuel Vadot 		if (val & sc->frac.freq_sel)
322e37e8677SEmmanuel Vadot 			*freq = sc->frac.freq1;
323e37e8677SEmmanuel Vadot 		else
324e37e8677SEmmanuel Vadot 			*freq = sc->frac.freq0;
325e37e8677SEmmanuel Vadot 	} else {
326e37e8677SEmmanuel Vadot 		m = aw_clk_get_factor(val, &sc->m);
327e37e8677SEmmanuel Vadot 		n = aw_clk_get_factor(val, &sc->n);
328e37e8677SEmmanuel Vadot 		*freq = *freq * n / m;
329e37e8677SEmmanuel Vadot 	}
330e37e8677SEmmanuel Vadot 
331e37e8677SEmmanuel Vadot 	return (0);
332e37e8677SEmmanuel Vadot }
333e37e8677SEmmanuel Vadot 
334e37e8677SEmmanuel Vadot static clknode_method_t aw_frac_clknode_methods[] = {
335e37e8677SEmmanuel Vadot 	/* Device interface */
336e37e8677SEmmanuel Vadot 	CLKNODEMETHOD(clknode_init,		aw_clk_frac_init),
337e37e8677SEmmanuel Vadot 	CLKNODEMETHOD(clknode_set_gate,		aw_clk_frac_set_gate),
338e37e8677SEmmanuel Vadot 	CLKNODEMETHOD(clknode_set_mux,		aw_clk_frac_set_mux),
339e37e8677SEmmanuel Vadot 	CLKNODEMETHOD(clknode_recalc_freq,	aw_clk_frac_recalc),
340e37e8677SEmmanuel Vadot 	CLKNODEMETHOD(clknode_set_freq,		aw_clk_frac_set_freq),
341e37e8677SEmmanuel Vadot 	CLKNODEMETHOD_END
342e37e8677SEmmanuel Vadot };
343e37e8677SEmmanuel Vadot 
344e37e8677SEmmanuel Vadot DEFINE_CLASS_1(aw_frac_clknode, aw_frac_clknode_class, aw_frac_clknode_methods,
345e37e8677SEmmanuel Vadot     sizeof(struct aw_clk_frac_sc), clknode_class);
346e37e8677SEmmanuel Vadot 
347e37e8677SEmmanuel Vadot int
aw_clk_frac_register(struct clkdom * clkdom,struct aw_clk_frac_def * clkdef)348e37e8677SEmmanuel Vadot aw_clk_frac_register(struct clkdom *clkdom, struct aw_clk_frac_def *clkdef)
349e37e8677SEmmanuel Vadot {
350e37e8677SEmmanuel Vadot 	struct clknode *clk;
351e37e8677SEmmanuel Vadot 	struct aw_clk_frac_sc *sc;
352e37e8677SEmmanuel Vadot 
353e37e8677SEmmanuel Vadot 	clk = clknode_create(clkdom, &aw_frac_clknode_class, &clkdef->clkdef);
354e37e8677SEmmanuel Vadot 	if (clk == NULL)
355e37e8677SEmmanuel Vadot 		return (1);
356e37e8677SEmmanuel Vadot 
357e37e8677SEmmanuel Vadot 	sc = clknode_get_softc(clk);
358e37e8677SEmmanuel Vadot 
359e37e8677SEmmanuel Vadot 	sc->offset = clkdef->offset;
360e37e8677SEmmanuel Vadot 
361e37e8677SEmmanuel Vadot 	sc->m.shift = clkdef->m.shift;
362e37e8677SEmmanuel Vadot 	sc->m.width = clkdef->m.width;
363e37e8677SEmmanuel Vadot 	sc->m.mask = ((1 << sc->m.width) - 1) << sc->m.shift;
364e37e8677SEmmanuel Vadot 	sc->m.value = clkdef->m.value;
365e37e8677SEmmanuel Vadot 	sc->m.flags = clkdef->m.flags;
366e37e8677SEmmanuel Vadot 
367e37e8677SEmmanuel Vadot 	sc->n.shift = clkdef->n.shift;
368e37e8677SEmmanuel Vadot 	sc->n.width = clkdef->n.width;
369e37e8677SEmmanuel Vadot 	sc->n.mask = ((1 << sc->n.width) - 1) << sc->n.shift;
370e37e8677SEmmanuel Vadot 	sc->n.value = clkdef->n.value;
371e37e8677SEmmanuel Vadot 	sc->n.flags = clkdef->n.flags;
372e37e8677SEmmanuel Vadot 
373e37e8677SEmmanuel Vadot 	sc->frac.freq0 = clkdef->frac.freq0;
374e37e8677SEmmanuel Vadot 	sc->frac.freq1 = clkdef->frac.freq1;
375e37e8677SEmmanuel Vadot 	sc->frac.mode_sel = 1 << clkdef->frac.mode_sel;
376e37e8677SEmmanuel Vadot 	sc->frac.freq_sel = 1 << clkdef->frac.freq_sel;
377e37e8677SEmmanuel Vadot 
378e37e8677SEmmanuel Vadot 	sc->min_freq = clkdef->min_freq;
379e37e8677SEmmanuel Vadot 	sc->max_freq = clkdef->max_freq;
380e37e8677SEmmanuel Vadot 
381e37e8677SEmmanuel Vadot 	sc->mux_shift = clkdef->mux_shift;
382e37e8677SEmmanuel Vadot 	sc->mux_mask = ((1 << clkdef->mux_width) - 1) << sc->mux_shift;
383e37e8677SEmmanuel Vadot 
384e37e8677SEmmanuel Vadot 	sc->gate_shift = clkdef->gate_shift;
385e37e8677SEmmanuel Vadot 
386e37e8677SEmmanuel Vadot 	sc->lock_shift = clkdef->lock_shift;
387e37e8677SEmmanuel Vadot 	sc->lock_retries = clkdef->lock_retries;
388e37e8677SEmmanuel Vadot 
389e37e8677SEmmanuel Vadot 	sc->flags = clkdef->flags;
390e37e8677SEmmanuel Vadot 
391e37e8677SEmmanuel Vadot 	clknode_register(clkdom, clk);
392e37e8677SEmmanuel Vadot 
393e37e8677SEmmanuel Vadot 	return (0);
394e37e8677SEmmanuel Vadot }
395