xref: /freebsd/sys/dev/clk/allwinner/aw_clk_np.c (revision be82b3a0)
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 
32be82b3a0SEmmanuel 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_np.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 / p
43e37e8677SEmmanuel Vadot  *
44e37e8677SEmmanuel Vadot  */
45e37e8677SEmmanuel Vadot 
46e37e8677SEmmanuel Vadot struct aw_clk_np_sc {
47e37e8677SEmmanuel Vadot 	uint32_t	offset;
48e37e8677SEmmanuel Vadot 
49e37e8677SEmmanuel Vadot 	struct aw_clk_factor	n;
50e37e8677SEmmanuel Vadot 	struct aw_clk_factor	p;
51e37e8677SEmmanuel Vadot 
52e37e8677SEmmanuel Vadot 	uint32_t	gate_shift;
53e37e8677SEmmanuel Vadot 	uint32_t	lock_shift;
54e37e8677SEmmanuel Vadot 	uint32_t	lock_retries;
55e37e8677SEmmanuel Vadot 
56e37e8677SEmmanuel Vadot 	uint32_t	flags;
57e37e8677SEmmanuel Vadot };
58e37e8677SEmmanuel Vadot 
59e37e8677SEmmanuel Vadot #define	WRITE4(_clk, off, val)						\
60e37e8677SEmmanuel Vadot 	CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
61e37e8677SEmmanuel Vadot #define	READ4(_clk, off, val)						\
62e37e8677SEmmanuel Vadot 	CLKDEV_READ_4(clknode_get_device(_clk), off, val)
63e37e8677SEmmanuel Vadot #define	DEVICE_LOCK(_clk)							\
64e37e8677SEmmanuel Vadot 	CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))
65e37e8677SEmmanuel Vadot #define	DEVICE_UNLOCK(_clk)						\
66e37e8677SEmmanuel Vadot 	CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))
67e37e8677SEmmanuel Vadot 
68e37e8677SEmmanuel Vadot static int
aw_clk_np_init(struct clknode * clk,device_t dev)69e37e8677SEmmanuel Vadot aw_clk_np_init(struct clknode *clk, device_t dev)
70e37e8677SEmmanuel Vadot {
71e37e8677SEmmanuel Vadot 
72e37e8677SEmmanuel Vadot 	clknode_init_parent_idx(clk, 0);
73e37e8677SEmmanuel Vadot 	return (0);
74e37e8677SEmmanuel Vadot }
75e37e8677SEmmanuel Vadot 
76e37e8677SEmmanuel Vadot static int
aw_clk_np_set_gate(struct clknode * clk,bool enable)77e37e8677SEmmanuel Vadot aw_clk_np_set_gate(struct clknode *clk, bool enable)
78e37e8677SEmmanuel Vadot {
79e37e8677SEmmanuel Vadot 	struct aw_clk_np_sc *sc;
80e37e8677SEmmanuel Vadot 	uint32_t val;
81e37e8677SEmmanuel Vadot 
82e37e8677SEmmanuel Vadot 	sc = clknode_get_softc(clk);
83e37e8677SEmmanuel Vadot 
84e37e8677SEmmanuel Vadot 	if ((sc->flags & AW_CLK_HAS_GATE) == 0)
85e37e8677SEmmanuel Vadot 		return (0);
86e37e8677SEmmanuel Vadot 
87e37e8677SEmmanuel Vadot 	DEVICE_LOCK(clk);
88e37e8677SEmmanuel Vadot 	READ4(clk, sc->offset, &val);
89e37e8677SEmmanuel Vadot 	if (enable)
90e37e8677SEmmanuel Vadot 		val |= (1 << sc->gate_shift);
91e37e8677SEmmanuel Vadot 	else
92e37e8677SEmmanuel Vadot 		val &= ~(1 << sc->gate_shift);
93e37e8677SEmmanuel Vadot 	WRITE4(clk, sc->offset, val);
94e37e8677SEmmanuel Vadot 	DEVICE_UNLOCK(clk);
95e37e8677SEmmanuel Vadot 
96e37e8677SEmmanuel Vadot 	return (0);
97e37e8677SEmmanuel Vadot }
98e37e8677SEmmanuel Vadot 
99e37e8677SEmmanuel Vadot static uint64_t
aw_clk_np_find_best(struct aw_clk_np_sc * sc,uint64_t fparent,uint64_t * fout,uint32_t * factor_n,uint32_t * factor_p)100e37e8677SEmmanuel Vadot aw_clk_np_find_best(struct aw_clk_np_sc *sc, uint64_t fparent, uint64_t *fout,
101e37e8677SEmmanuel Vadot     uint32_t *factor_n, uint32_t *factor_p)
102e37e8677SEmmanuel Vadot {
103e37e8677SEmmanuel Vadot 	uint64_t cur, best;
104e37e8677SEmmanuel Vadot 	uint32_t n, p, max_n, max_p, min_n, min_p;
105e37e8677SEmmanuel Vadot 
106e37e8677SEmmanuel Vadot 	*factor_n = *factor_p = 0;
107e37e8677SEmmanuel Vadot 
108e37e8677SEmmanuel Vadot 	max_n = aw_clk_factor_get_max(&sc->n);
109e37e8677SEmmanuel Vadot 	max_p = aw_clk_factor_get_max(&sc->p);
110e37e8677SEmmanuel Vadot 	min_n = aw_clk_factor_get_min(&sc->n);
111e37e8677SEmmanuel Vadot 	min_p = aw_clk_factor_get_min(&sc->p);
112e37e8677SEmmanuel Vadot 
113e37e8677SEmmanuel Vadot 	for (p = min_p; p <= max_p; ) {
114e37e8677SEmmanuel Vadot 		for (n = min_n; n <= max_n; ) {
115e37e8677SEmmanuel Vadot 			cur = fparent * n / p;
116e37e8677SEmmanuel Vadot 			if (abs(*fout - cur) < abs(*fout - best)) {
117e37e8677SEmmanuel Vadot 				best = cur;
118e37e8677SEmmanuel Vadot 				*factor_n = n;
119e37e8677SEmmanuel Vadot 				*factor_p = p;
120e37e8677SEmmanuel Vadot 			}
121e37e8677SEmmanuel Vadot 
122e37e8677SEmmanuel Vadot 			n++;
123e37e8677SEmmanuel Vadot 		}
124e37e8677SEmmanuel Vadot 		p++;
125e37e8677SEmmanuel Vadot 	}
126e37e8677SEmmanuel Vadot 
127e37e8677SEmmanuel Vadot 	return (best);
128e37e8677SEmmanuel Vadot }
129e37e8677SEmmanuel Vadot 
130e37e8677SEmmanuel Vadot static int
aw_clk_np_set_freq(struct clknode * clk,uint64_t fparent,uint64_t * fout,int flags,int * stop)131e37e8677SEmmanuel Vadot aw_clk_np_set_freq(struct clknode *clk, uint64_t fparent, uint64_t *fout,
132e37e8677SEmmanuel Vadot     int flags, int *stop)
133e37e8677SEmmanuel Vadot {
134e37e8677SEmmanuel Vadot 	struct aw_clk_np_sc *sc;
135e37e8677SEmmanuel Vadot 	uint64_t cur, best;
136e37e8677SEmmanuel Vadot 	uint32_t val, n, p, best_n, best_p;
137e37e8677SEmmanuel Vadot 	int retry;
138e37e8677SEmmanuel Vadot 
139e37e8677SEmmanuel Vadot 	sc = clknode_get_softc(clk);
140e37e8677SEmmanuel Vadot 
141e37e8677SEmmanuel Vadot 	best = cur = 0;
142e37e8677SEmmanuel Vadot 
143e37e8677SEmmanuel Vadot 	best = aw_clk_np_find_best(sc, fparent, fout,
144e37e8677SEmmanuel Vadot 	    &best_n, &best_p);
145e37e8677SEmmanuel Vadot 
146e37e8677SEmmanuel Vadot 	if ((flags & CLK_SET_DRYRUN) != 0) {
147e37e8677SEmmanuel Vadot 		*fout = best;
148e37e8677SEmmanuel Vadot 		*stop = 1;
149e37e8677SEmmanuel Vadot 		return (0);
150e37e8677SEmmanuel Vadot 	}
151e37e8677SEmmanuel Vadot 
152e37e8677SEmmanuel Vadot 	if ((best < *fout) &&
153e37e8677SEmmanuel Vadot 	  ((flags & CLK_SET_ROUND_DOWN) == 0)) {
154e37e8677SEmmanuel Vadot 		*stop = 1;
155e37e8677SEmmanuel Vadot 		return (ERANGE);
156e37e8677SEmmanuel Vadot 	}
157e37e8677SEmmanuel Vadot 	if ((best > *fout) &&
158e37e8677SEmmanuel Vadot 	  ((flags & CLK_SET_ROUND_UP) == 0)) {
159e37e8677SEmmanuel Vadot 		*stop = 1;
160e37e8677SEmmanuel Vadot 		return (ERANGE);
161e37e8677SEmmanuel Vadot 	}
162e37e8677SEmmanuel Vadot 
163e37e8677SEmmanuel Vadot 	DEVICE_LOCK(clk);
164e37e8677SEmmanuel Vadot 	READ4(clk, sc->offset, &val);
165e37e8677SEmmanuel Vadot 
166e37e8677SEmmanuel Vadot 	n = aw_clk_factor_get_value(&sc->n, best_n);
167e37e8677SEmmanuel Vadot 	p = aw_clk_factor_get_value(&sc->p, best_p);
168e37e8677SEmmanuel Vadot 	val &= ~sc->n.mask;
169e37e8677SEmmanuel Vadot 	val &= ~sc->p.mask;
170e37e8677SEmmanuel Vadot 	val |= n << sc->n.shift;
171e37e8677SEmmanuel Vadot 	val |= p << sc->p.shift;
172e37e8677SEmmanuel Vadot 
173e37e8677SEmmanuel Vadot 	WRITE4(clk, sc->offset, val);
174e37e8677SEmmanuel Vadot 	DEVICE_UNLOCK(clk);
175e37e8677SEmmanuel Vadot 
176e37e8677SEmmanuel Vadot 	if ((sc->flags & AW_CLK_HAS_LOCK) != 0) {
177e37e8677SEmmanuel Vadot 		for (retry = 0; retry < sc->lock_retries; retry++) {
178e37e8677SEmmanuel Vadot 			READ4(clk, sc->offset, &val);
179e37e8677SEmmanuel Vadot 			if ((val & (1 << sc->lock_shift)) != 0)
180e37e8677SEmmanuel Vadot 				break;
181e37e8677SEmmanuel Vadot 			DELAY(1000);
182e37e8677SEmmanuel Vadot 		}
183e37e8677SEmmanuel Vadot 	}
184e37e8677SEmmanuel Vadot 
185e37e8677SEmmanuel Vadot 	*fout = best;
186e37e8677SEmmanuel Vadot 	*stop = 1;
187e37e8677SEmmanuel Vadot 
188e37e8677SEmmanuel Vadot 	return (0);
189e37e8677SEmmanuel Vadot }
190e37e8677SEmmanuel Vadot 
191e37e8677SEmmanuel Vadot static int
aw_clk_np_recalc(struct clknode * clk,uint64_t * freq)192e37e8677SEmmanuel Vadot aw_clk_np_recalc(struct clknode *clk, uint64_t *freq)
193e37e8677SEmmanuel Vadot {
194e37e8677SEmmanuel Vadot 	struct aw_clk_np_sc *sc;
195e37e8677SEmmanuel Vadot 	uint32_t val, n, p;
196e37e8677SEmmanuel Vadot 
197e37e8677SEmmanuel Vadot 	sc = clknode_get_softc(clk);
198e37e8677SEmmanuel Vadot 
199e37e8677SEmmanuel Vadot 	DEVICE_LOCK(clk);
200e37e8677SEmmanuel Vadot 	READ4(clk, sc->offset, &val);
201e37e8677SEmmanuel Vadot 	DEVICE_UNLOCK(clk);
202e37e8677SEmmanuel Vadot 
203e37e8677SEmmanuel Vadot 	n = aw_clk_get_factor(val, &sc->n);
204e37e8677SEmmanuel Vadot 	p = aw_clk_get_factor(val, &sc->p);
205e37e8677SEmmanuel Vadot 
206e37e8677SEmmanuel Vadot 	*freq = *freq * n / p;
207e37e8677SEmmanuel Vadot 
208e37e8677SEmmanuel Vadot 	return (0);
209e37e8677SEmmanuel Vadot }
210e37e8677SEmmanuel Vadot 
211e37e8677SEmmanuel Vadot static clknode_method_t aw_np_clknode_methods[] = {
212e37e8677SEmmanuel Vadot 	/* Device interface */
213e37e8677SEmmanuel Vadot 	CLKNODEMETHOD(clknode_init,		aw_clk_np_init),
214e37e8677SEmmanuel Vadot 	CLKNODEMETHOD(clknode_set_gate,		aw_clk_np_set_gate),
215e37e8677SEmmanuel Vadot 	CLKNODEMETHOD(clknode_recalc_freq,	aw_clk_np_recalc),
216e37e8677SEmmanuel Vadot 	CLKNODEMETHOD(clknode_set_freq,		aw_clk_np_set_freq),
217e37e8677SEmmanuel Vadot 	CLKNODEMETHOD_END
218e37e8677SEmmanuel Vadot };
219e37e8677SEmmanuel Vadot 
220e37e8677SEmmanuel Vadot DEFINE_CLASS_1(aw_np_clknode, aw_np_clknode_class, aw_np_clknode_methods,
221e37e8677SEmmanuel Vadot     sizeof(struct aw_clk_np_sc), clknode_class);
222e37e8677SEmmanuel Vadot 
223e37e8677SEmmanuel Vadot int
aw_clk_np_register(struct clkdom * clkdom,struct aw_clk_np_def * clkdef)224e37e8677SEmmanuel Vadot aw_clk_np_register(struct clkdom *clkdom, struct aw_clk_np_def *clkdef)
225e37e8677SEmmanuel Vadot {
226e37e8677SEmmanuel Vadot 	struct clknode *clk;
227e37e8677SEmmanuel Vadot 	struct aw_clk_np_sc *sc;
228e37e8677SEmmanuel Vadot 
229e37e8677SEmmanuel Vadot 	clk = clknode_create(clkdom, &aw_np_clknode_class, &clkdef->clkdef);
230e37e8677SEmmanuel Vadot 	if (clk == NULL)
231e37e8677SEmmanuel Vadot 		return (1);
232e37e8677SEmmanuel Vadot 
233e37e8677SEmmanuel Vadot 	sc = clknode_get_softc(clk);
234e37e8677SEmmanuel Vadot 
235e37e8677SEmmanuel Vadot 	sc->offset = clkdef->offset;
236e37e8677SEmmanuel Vadot 
237e37e8677SEmmanuel Vadot 	sc->n.shift = clkdef->n.shift;
238e37e8677SEmmanuel Vadot 	sc->n.width = clkdef->n.width;
239e37e8677SEmmanuel Vadot 	sc->n.mask = ((1 << sc->n.width) - 1) << sc->n.shift;
240e37e8677SEmmanuel Vadot 	sc->n.value = clkdef->n.value;
241e37e8677SEmmanuel Vadot 	sc->n.flags = clkdef->n.flags;
242e37e8677SEmmanuel Vadot 
243e37e8677SEmmanuel Vadot 	sc->p.shift = clkdef->p.shift;
244e37e8677SEmmanuel Vadot 	sc->p.width = clkdef->p.width;
245e37e8677SEmmanuel Vadot 	sc->p.mask = ((1 << sc->p.width) - 1) << sc->p.shift;
246e37e8677SEmmanuel Vadot 	sc->p.value = clkdef->p.value;
247e37e8677SEmmanuel Vadot 	sc->p.flags = clkdef->p.flags;
248e37e8677SEmmanuel Vadot 
249e37e8677SEmmanuel Vadot 	sc->gate_shift = clkdef->gate_shift;
250e37e8677SEmmanuel Vadot 
251e37e8677SEmmanuel Vadot 	sc->lock_shift = clkdef->lock_shift;
252e37e8677SEmmanuel Vadot 	sc->lock_retries = clkdef->lock_retries;
253e37e8677SEmmanuel Vadot 
254e37e8677SEmmanuel Vadot 	sc->flags = clkdef->flags;
255e37e8677SEmmanuel Vadot 
256e37e8677SEmmanuel Vadot 	clknode_register(clkdom, clk);
257e37e8677SEmmanuel Vadot 
258e37e8677SEmmanuel Vadot 	return (0);
259e37e8677SEmmanuel Vadot }
260