xref: /freebsd/sys/dev/clk/allwinner/aw_clk_nmm.c (revision be82b3a0bf72ed3b5f01ac9fcd8dcd3802e3c742)
1e37e8677SEmmanuel Vadot /*-
2e37e8677SEmmanuel Vadot  * SPDX-License-Identifier: BSD-2-Clause
3e37e8677SEmmanuel Vadot  *
4e37e8677SEmmanuel Vadot  * Copyright (c) 2019 Emmanuel Vadot <manu@freebsd.org>
5e37e8677SEmmanuel Vadot  *
6e37e8677SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
7e37e8677SEmmanuel Vadot  * modification, are permitted provided that the following conditions
8e37e8677SEmmanuel Vadot  * are met:
9e37e8677SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
10e37e8677SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
11e37e8677SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
12e37e8677SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
13e37e8677SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
14e37e8677SEmmanuel Vadot  *
15e37e8677SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16e37e8677SEmmanuel Vadot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17e37e8677SEmmanuel Vadot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18e37e8677SEmmanuel Vadot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19e37e8677SEmmanuel Vadot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20e37e8677SEmmanuel Vadot  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21e37e8677SEmmanuel Vadot  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22e37e8677SEmmanuel Vadot  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23e37e8677SEmmanuel Vadot  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24e37e8677SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25e37e8677SEmmanuel Vadot  * SUCH DAMAGE.
26e37e8677SEmmanuel Vadot  */
27e37e8677SEmmanuel Vadot 
28e37e8677SEmmanuel Vadot #include <sys/param.h>
29e37e8677SEmmanuel Vadot #include <sys/systm.h>
30e37e8677SEmmanuel Vadot #include <sys/bus.h>
31e37e8677SEmmanuel Vadot 
32*be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
33e37e8677SEmmanuel Vadot 
34e37e8677SEmmanuel Vadot #include <dev/clk/allwinner/aw_clk.h>
35e37e8677SEmmanuel Vadot #include <dev/clk/allwinner/aw_clk_nmm.h>
36e37e8677SEmmanuel Vadot 
37e37e8677SEmmanuel Vadot #include "clkdev_if.h"
38e37e8677SEmmanuel Vadot 
39e37e8677SEmmanuel Vadot /*
40e37e8677SEmmanuel Vadot  * clknode for clocks matching the formula :
41e37e8677SEmmanuel Vadot  *
42e37e8677SEmmanuel Vadot  * clk = clkin * n / m0 / m1
43e37e8677SEmmanuel Vadot  *
44e37e8677SEmmanuel Vadot  */
45e37e8677SEmmanuel Vadot 
46e37e8677SEmmanuel Vadot struct aw_clk_nmm_sc {
47e37e8677SEmmanuel Vadot 	uint32_t	offset;
48e37e8677SEmmanuel Vadot 
49e37e8677SEmmanuel Vadot 	struct aw_clk_factor	n;
50e37e8677SEmmanuel Vadot 	struct aw_clk_factor	m0;
51e37e8677SEmmanuel Vadot 	struct aw_clk_factor	m1;
52e37e8677SEmmanuel Vadot 
53e37e8677SEmmanuel Vadot 	uint32_t	gate_shift;
54e37e8677SEmmanuel Vadot 	uint32_t	lock_shift;
55e37e8677SEmmanuel Vadot 	uint32_t	lock_retries;
56e37e8677SEmmanuel Vadot 
57e37e8677SEmmanuel Vadot 	uint32_t	flags;
58e37e8677SEmmanuel Vadot };
59e37e8677SEmmanuel Vadot 
60e37e8677SEmmanuel Vadot #define	WRITE4(_clk, off, val)						\
61e37e8677SEmmanuel Vadot 	CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
62e37e8677SEmmanuel Vadot #define	READ4(_clk, off, val)						\
63e37e8677SEmmanuel Vadot 	CLKDEV_READ_4(clknode_get_device(_clk), off, val)
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_nmm_init(struct clknode * clk,device_t dev)70e37e8677SEmmanuel Vadot aw_clk_nmm_init(struct clknode *clk, device_t dev)
71e37e8677SEmmanuel Vadot {
72e37e8677SEmmanuel Vadot 
73e37e8677SEmmanuel Vadot 	clknode_init_parent_idx(clk, 0);
74e37e8677SEmmanuel Vadot 	return (0);
75e37e8677SEmmanuel Vadot }
76e37e8677SEmmanuel Vadot 
77e37e8677SEmmanuel Vadot static int
aw_clk_nmm_set_gate(struct clknode * clk,bool enable)78e37e8677SEmmanuel Vadot aw_clk_nmm_set_gate(struct clknode *clk, bool enable)
79e37e8677SEmmanuel Vadot {
80e37e8677SEmmanuel Vadot 	struct aw_clk_nmm_sc *sc;
81e37e8677SEmmanuel Vadot 	uint32_t val;
82e37e8677SEmmanuel Vadot 
83e37e8677SEmmanuel Vadot 	sc = clknode_get_softc(clk);
84e37e8677SEmmanuel Vadot 
85e37e8677SEmmanuel Vadot 	if ((sc->flags & AW_CLK_HAS_GATE) == 0)
86e37e8677SEmmanuel Vadot 		return (0);
87e37e8677SEmmanuel Vadot 
88e37e8677SEmmanuel Vadot 	DEVICE_LOCK(clk);
89e37e8677SEmmanuel Vadot 	READ4(clk, sc->offset, &val);
90e37e8677SEmmanuel Vadot 	if (enable)
91e37e8677SEmmanuel Vadot 		val |= (1 << sc->gate_shift);
92e37e8677SEmmanuel Vadot 	else
93e37e8677SEmmanuel Vadot 		val &= ~(1 << sc->gate_shift);
94e37e8677SEmmanuel Vadot 	WRITE4(clk, sc->offset, val);
95e37e8677SEmmanuel Vadot 	DEVICE_UNLOCK(clk);
96e37e8677SEmmanuel Vadot 
97e37e8677SEmmanuel Vadot 	return (0);
98e37e8677SEmmanuel Vadot }
99e37e8677SEmmanuel Vadot 
100e37e8677SEmmanuel Vadot static uint64_t
aw_clk_nmm_find_best(struct aw_clk_nmm_sc * sc,uint64_t fparent,uint64_t * fout,uint32_t * factor_n,uint32_t * factor_m0,uint32_t * factor_m1)101e37e8677SEmmanuel Vadot aw_clk_nmm_find_best(struct aw_clk_nmm_sc *sc, uint64_t fparent, uint64_t *fout,
102e37e8677SEmmanuel Vadot   uint32_t *factor_n, uint32_t *factor_m0, uint32_t *factor_m1)
103e37e8677SEmmanuel Vadot {
104e37e8677SEmmanuel Vadot 	uint64_t cur, best;
105e37e8677SEmmanuel Vadot 	uint32_t n, m0, m1;
106e37e8677SEmmanuel Vadot 	uint32_t max_n, max_m0, max_m1;
107e37e8677SEmmanuel Vadot 	uint32_t min_n, min_m0, min_m1;
108e37e8677SEmmanuel Vadot 
109e37e8677SEmmanuel Vadot 	*factor_n = *factor_m0 = *factor_m1 = 0;
110e37e8677SEmmanuel Vadot 
111e37e8677SEmmanuel Vadot 	max_n = aw_clk_factor_get_max(&sc->n);
112e37e8677SEmmanuel Vadot 	min_n = aw_clk_factor_get_min(&sc->n);
113e37e8677SEmmanuel Vadot 	max_m0 = aw_clk_factor_get_max(&sc->m0);
114e37e8677SEmmanuel Vadot 	min_m0 = aw_clk_factor_get_min(&sc->m0);
115e37e8677SEmmanuel Vadot 	max_m1 = aw_clk_factor_get_max(&sc->m1);
116e37e8677SEmmanuel Vadot 	min_m1 = aw_clk_factor_get_min(&sc->m1);
117e37e8677SEmmanuel Vadot 
118e37e8677SEmmanuel Vadot 	for (m0 = min_m0; m0 <= max_m0; ) {
119e37e8677SEmmanuel Vadot 		for (m1 = min_m1; m1 <= max_m1; ) {
120e37e8677SEmmanuel Vadot 			for (n = min_n; n <= max_n; ) {
121e37e8677SEmmanuel Vadot 				cur = fparent * n / m0 / m1;
122e37e8677SEmmanuel Vadot 				if (abs(*fout - cur) < abs(*fout - best)) {
123e37e8677SEmmanuel Vadot 					best = cur;
124e37e8677SEmmanuel Vadot 					*factor_n = n;
125e37e8677SEmmanuel Vadot 					*factor_m0 = m0;
126e37e8677SEmmanuel Vadot 					*factor_m1 = m1;
127e37e8677SEmmanuel Vadot 				}
128e37e8677SEmmanuel Vadot 				n++;
129e37e8677SEmmanuel Vadot 			}
130e37e8677SEmmanuel Vadot 			m1++;
131e37e8677SEmmanuel Vadot 		}
132e37e8677SEmmanuel Vadot 		m0++;
133e37e8677SEmmanuel Vadot 	}
134e37e8677SEmmanuel Vadot 
135e37e8677SEmmanuel Vadot 	return (best);
136e37e8677SEmmanuel Vadot }
137e37e8677SEmmanuel Vadot 
138e37e8677SEmmanuel Vadot static int
aw_clk_nmm_set_freq(struct clknode * clk,uint64_t fparent,uint64_t * fout,int flags,int * stop)139e37e8677SEmmanuel Vadot aw_clk_nmm_set_freq(struct clknode *clk, uint64_t fparent, uint64_t *fout,
140e37e8677SEmmanuel Vadot     int flags, int *stop)
141e37e8677SEmmanuel Vadot {
142e37e8677SEmmanuel Vadot 	struct aw_clk_nmm_sc *sc;
143e37e8677SEmmanuel Vadot 	uint64_t cur, best;
144e37e8677SEmmanuel Vadot 	uint32_t val, n, m0, m1, best_n, best_m0, best_m1;
145e37e8677SEmmanuel Vadot 	int retry;
146e37e8677SEmmanuel Vadot 
147e37e8677SEmmanuel Vadot 	sc = clknode_get_softc(clk);
148e37e8677SEmmanuel Vadot 
149e37e8677SEmmanuel Vadot 	best = cur = 0;
150e37e8677SEmmanuel Vadot 
151e37e8677SEmmanuel Vadot 	best = aw_clk_nmm_find_best(sc, fparent, fout,
152e37e8677SEmmanuel Vadot 	    &best_n, &best_m0, &best_m1);
153e37e8677SEmmanuel Vadot 
154e37e8677SEmmanuel Vadot 	if ((flags & CLK_SET_DRYRUN) != 0) {
155e37e8677SEmmanuel Vadot 		*fout = best;
156e37e8677SEmmanuel Vadot 		*stop = 1;
157e37e8677SEmmanuel Vadot 		return (0);
158e37e8677SEmmanuel Vadot 	}
159e37e8677SEmmanuel Vadot 
160e37e8677SEmmanuel Vadot 	if ((best < *fout) &&
161e37e8677SEmmanuel Vadot 	  ((flags & CLK_SET_ROUND_DOWN) == 0)) {
162e37e8677SEmmanuel Vadot 		*stop = 1;
163e37e8677SEmmanuel Vadot 		return (ERANGE);
164e37e8677SEmmanuel Vadot 	}
165e37e8677SEmmanuel Vadot 	if ((best > *fout) &&
166e37e8677SEmmanuel Vadot 	  ((flags & CLK_SET_ROUND_UP) == 0)) {
167e37e8677SEmmanuel Vadot 		*stop = 1;
168e37e8677SEmmanuel Vadot 		return (ERANGE);
169e37e8677SEmmanuel Vadot 	}
170e37e8677SEmmanuel Vadot 
171e37e8677SEmmanuel Vadot 	DEVICE_LOCK(clk);
172e37e8677SEmmanuel Vadot 	READ4(clk, sc->offset, &val);
173e37e8677SEmmanuel Vadot 
174e37e8677SEmmanuel Vadot 	n = aw_clk_factor_get_value(&sc->n, best_n);
175e37e8677SEmmanuel Vadot 	m0 = aw_clk_factor_get_value(&sc->m0, best_m0);
176e37e8677SEmmanuel Vadot 	m1 = aw_clk_factor_get_value(&sc->m1, best_m1);
177e37e8677SEmmanuel Vadot 	val &= ~sc->n.mask;
178e37e8677SEmmanuel Vadot 	val &= ~sc->m0.mask;
179e37e8677SEmmanuel Vadot 	val &= ~sc->m1.mask;
180e37e8677SEmmanuel Vadot 	val |= n << sc->n.shift;
181e37e8677SEmmanuel Vadot 	val |= m0 << sc->m0.shift;
182e37e8677SEmmanuel Vadot 	val |= m1 << sc->m1.shift;
183e37e8677SEmmanuel Vadot 
184e37e8677SEmmanuel Vadot 	WRITE4(clk, sc->offset, val);
185e37e8677SEmmanuel Vadot 	DEVICE_UNLOCK(clk);
186e37e8677SEmmanuel Vadot 
187e37e8677SEmmanuel Vadot 	if ((sc->flags & AW_CLK_HAS_LOCK) != 0) {
188e37e8677SEmmanuel Vadot 		for (retry = 0; retry < sc->lock_retries; retry++) {
189e37e8677SEmmanuel Vadot 			READ4(clk, sc->offset, &val);
190e37e8677SEmmanuel Vadot 			if ((val & (1 << sc->lock_shift)) != 0)
191e37e8677SEmmanuel Vadot 				break;
192e37e8677SEmmanuel Vadot 			DELAY(1000);
193e37e8677SEmmanuel Vadot 		}
194e37e8677SEmmanuel Vadot 	}
195e37e8677SEmmanuel Vadot 
196e37e8677SEmmanuel Vadot 	*fout = best;
197e37e8677SEmmanuel Vadot 	*stop = 1;
198e37e8677SEmmanuel Vadot 
199e37e8677SEmmanuel Vadot 	return (0);
200e37e8677SEmmanuel Vadot }
201e37e8677SEmmanuel Vadot 
202e37e8677SEmmanuel Vadot static int
aw_clk_nmm_recalc(struct clknode * clk,uint64_t * freq)203e37e8677SEmmanuel Vadot aw_clk_nmm_recalc(struct clknode *clk, uint64_t *freq)
204e37e8677SEmmanuel Vadot {
205e37e8677SEmmanuel Vadot 	struct aw_clk_nmm_sc *sc;
206e37e8677SEmmanuel Vadot 	uint32_t val, n, m0, m1;
207e37e8677SEmmanuel Vadot 
208e37e8677SEmmanuel Vadot 	sc = clknode_get_softc(clk);
209e37e8677SEmmanuel Vadot 
210e37e8677SEmmanuel Vadot 	DEVICE_LOCK(clk);
211e37e8677SEmmanuel Vadot 	READ4(clk, sc->offset, &val);
212e37e8677SEmmanuel Vadot 	DEVICE_UNLOCK(clk);
213e37e8677SEmmanuel Vadot 
214e37e8677SEmmanuel Vadot 	n = aw_clk_get_factor(val, &sc->n);
215e37e8677SEmmanuel Vadot 	m0 = aw_clk_get_factor(val, &sc->m0);
216e37e8677SEmmanuel Vadot 	m1 = aw_clk_get_factor(val, &sc->m1);
217e37e8677SEmmanuel Vadot 
218e37e8677SEmmanuel Vadot 	*freq = *freq * n / m0 / m1;
219e37e8677SEmmanuel Vadot 
220e37e8677SEmmanuel Vadot 	return (0);
221e37e8677SEmmanuel Vadot }
222e37e8677SEmmanuel Vadot 
223e37e8677SEmmanuel Vadot static clknode_method_t aw_nmm_clknode_methods[] = {
224e37e8677SEmmanuel Vadot 	/* Device interface */
225e37e8677SEmmanuel Vadot 	CLKNODEMETHOD(clknode_init,		aw_clk_nmm_init),
226e37e8677SEmmanuel Vadot 	CLKNODEMETHOD(clknode_set_gate,		aw_clk_nmm_set_gate),
227e37e8677SEmmanuel Vadot 	CLKNODEMETHOD(clknode_recalc_freq,	aw_clk_nmm_recalc),
228e37e8677SEmmanuel Vadot 	CLKNODEMETHOD(clknode_set_freq,		aw_clk_nmm_set_freq),
229e37e8677SEmmanuel Vadot 	CLKNODEMETHOD_END
230e37e8677SEmmanuel Vadot };
231e37e8677SEmmanuel Vadot 
232e37e8677SEmmanuel Vadot DEFINE_CLASS_1(aw_nmm_clknode, aw_nmm_clknode_class, aw_nmm_clknode_methods,
233e37e8677SEmmanuel Vadot     sizeof(struct aw_clk_nmm_sc), clknode_class);
234e37e8677SEmmanuel Vadot 
235e37e8677SEmmanuel Vadot int
aw_clk_nmm_register(struct clkdom * clkdom,struct aw_clk_nmm_def * clkdef)236e37e8677SEmmanuel Vadot aw_clk_nmm_register(struct clkdom *clkdom, struct aw_clk_nmm_def *clkdef)
237e37e8677SEmmanuel Vadot {
238e37e8677SEmmanuel Vadot 	struct clknode *clk;
239e37e8677SEmmanuel Vadot 	struct aw_clk_nmm_sc *sc;
240e37e8677SEmmanuel Vadot 
241e37e8677SEmmanuel Vadot 	clk = clknode_create(clkdom, &aw_nmm_clknode_class, &clkdef->clkdef);
242e37e8677SEmmanuel Vadot 	if (clk == NULL)
243e37e8677SEmmanuel Vadot 		return (1);
244e37e8677SEmmanuel Vadot 
245e37e8677SEmmanuel Vadot 	sc = clknode_get_softc(clk);
246e37e8677SEmmanuel Vadot 
247e37e8677SEmmanuel Vadot 	sc->offset = clkdef->offset;
248e37e8677SEmmanuel Vadot 
249e37e8677SEmmanuel Vadot 	sc->n.shift = clkdef->n.shift;
250e37e8677SEmmanuel Vadot 	sc->n.width = clkdef->n.width;
251e37e8677SEmmanuel Vadot 	sc->n.mask = ((1 << sc->n.width) - 1) << sc->n.shift;
252e37e8677SEmmanuel Vadot 	sc->n.value = clkdef->n.value;
253e37e8677SEmmanuel Vadot 	sc->n.flags = clkdef->n.flags;
254e37e8677SEmmanuel Vadot 
255e37e8677SEmmanuel Vadot 	sc->m0.shift = clkdef->m0.shift;
256e37e8677SEmmanuel Vadot 	sc->m0.width = clkdef->m0.width;
257e37e8677SEmmanuel Vadot 	sc->m0.mask = ((1 << sc->m0.width) - 1) << sc->m0.shift;
258e37e8677SEmmanuel Vadot 	sc->m0.value = clkdef->m0.value;
259e37e8677SEmmanuel Vadot 	sc->m0.flags = clkdef->m0.flags;
260e37e8677SEmmanuel Vadot 
261e37e8677SEmmanuel Vadot 	sc->m1.shift = clkdef->m1.shift;
262e37e8677SEmmanuel Vadot 	sc->m1.width = clkdef->m1.width;
263e37e8677SEmmanuel Vadot 	sc->m1.mask = ((1 << sc->m1.width) - 1) << sc->m1.shift;
264e37e8677SEmmanuel Vadot 	sc->m1.value = clkdef->m1.value;
265e37e8677SEmmanuel Vadot 	sc->m1.flags = clkdef->m1.flags;
266e37e8677SEmmanuel Vadot 
267e37e8677SEmmanuel Vadot 	sc->gate_shift = clkdef->gate_shift;
268e37e8677SEmmanuel Vadot 
269e37e8677SEmmanuel Vadot 	sc->lock_shift = clkdef->lock_shift;
270e37e8677SEmmanuel Vadot 	sc->lock_retries = clkdef->lock_retries;
271e37e8677SEmmanuel Vadot 
272e37e8677SEmmanuel Vadot 	sc->flags = clkdef->flags;
273e37e8677SEmmanuel Vadot 
274e37e8677SEmmanuel Vadot 	clknode_register(clkdom, clk);
275e37e8677SEmmanuel Vadot 
276e37e8677SEmmanuel Vadot 	return (0);
277e37e8677SEmmanuel Vadot }
278