xref: /netbsd-src/sys/arch/arm/rockchip/rk_cru_composite.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /* $NetBSD: rk_cru_composite.c,v 1.5 2019/11/16 13:23:13 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: rk_cru_composite.c,v 1.5 2019/11/16 13:23:13 jmcneill Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 
35 #include <dev/clk/clk_backend.h>
36 
37 #include <arm/rockchip/rk_cru.h>
38 
39 #include <dev/fdt/fdtvar.h>
40 
41 int
42 rk_cru_composite_enable(struct rk_cru_softc *sc, struct rk_cru_clk *clk,
43     int enable)
44 {
45 	struct rk_cru_composite *composite = &clk->u.composite;
46 
47 	KASSERT(clk->type == RK_CRU_COMPOSITE);
48 
49 	if (composite->gate_mask == 0)
50 		return enable ? 0 : ENXIO;
51 
52 	const uint32_t write_mask = composite->gate_mask << 16;
53 	const uint32_t write_val = enable ? 0 : composite->gate_mask;
54 
55 	CRU_WRITE(sc, composite->gate_reg, write_mask | write_val);
56 
57 	return 0;
58 }
59 
60 u_int
61 rk_cru_composite_get_rate(struct rk_cru_softc *sc,
62     struct rk_cru_clk *clk)
63 {
64 	struct rk_cru_composite *composite = &clk->u.composite;
65 	struct clk *clkp, *clkp_parent;
66 
67 	KASSERT(clk->type == RK_CRU_COMPOSITE);
68 
69 	clkp = &clk->base;
70 	clkp_parent = clk_get_parent(clkp);
71 	if (clkp_parent == NULL)
72 		return 0;
73 
74 	const u_int prate = clk_get_rate(clkp_parent);
75 	if (prate == 0)
76 		return 0;
77 
78 	if (composite->flags & RK_COMPOSITE_FRACDIV) {
79 		const uint32_t val = CRU_READ(sc, composite->frac_reg);
80 		const u_int num = (val >> 16) & 0xffff;
81 		const u_int den = val & 0xffff;
82 
83 		return (u_int)((uint64_t)prate * num / den);
84 	} else {
85 		const uint32_t val = CRU_READ(sc, composite->muxdiv_reg);
86 		const u_int div = __SHIFTOUT(val, composite->div_mask) + 1;
87 
88 		return prate / div;
89 	}
90 }
91 
92 static u_int
93 rk_cru_composite_get_frac_div(u_int n, u_int d)
94 {
95 	u_int tmp;
96 
97 	while (d > 0) {
98 		tmp = d;
99 		d = n % d;
100 		n = tmp;
101 	}
102 
103 	return n;
104 }
105 
106 static int
107 rk_cru_composite_set_rate_frac(struct rk_cru_softc *sc,
108     struct rk_cru_clk *clk, u_int rate)
109 {
110 	struct rk_cru_composite *composite = &clk->u.composite;
111 	struct clk *clk_parent;
112 
113 	clk_parent = clk_get_parent(&clk->base);
114 	if (clk_parent == NULL)
115 		return ENXIO;
116 
117 	const u_int prate = clk_get_rate(clk_parent);
118 	const u_int v = rk_cru_composite_get_frac_div(prate, rate);
119 	const u_int num = (prate / v) & 0xffff;
120 	const u_int den = (rate / v) & 0xffff;
121 	if (prate / num * den != rate)
122 		return EINVAL;
123 
124 	CRU_WRITE(sc, composite->frac_reg, (den << 16) | num);
125 
126 	return 0;
127 }
128 
129 int
130 rk_cru_composite_set_rate(struct rk_cru_softc *sc,
131     struct rk_cru_clk *clk, u_int rate)
132 {
133 	struct rk_cru_composite *composite = &clk->u.composite;
134 	u_int best_div, best_mux, best_diff;
135 	struct rk_cru_clk *rclk_parent;
136 	struct clk *clk_parent;
137 
138 	KASSERT(clk->type == RK_CRU_COMPOSITE);
139 
140 	if (composite->flags & RK_COMPOSITE_SET_RATE_PARENT) {
141 		clk_parent = clk_get_parent(&clk->base);
142 		if (clk_parent == NULL)
143 			return ENXIO;
144 		return clk_set_rate(clk_parent, rate);
145 	}
146 
147 	if (composite->flags & RK_COMPOSITE_FRACDIV) {
148 		return rk_cru_composite_set_rate_frac(sc, clk, rate);
149 	}
150 
151 	best_div = 0;
152 	best_mux = 0;
153 	best_diff = INT_MAX;
154 	for (u_int mux = 0; mux < composite->nparents; mux++) {
155 		rclk_parent = rk_cru_clock_find(sc, composite->parents[mux]);
156 		if (rclk_parent != NULL)
157 			clk_parent = &rclk_parent->base;
158 		else
159 			clk_parent = fdtbus_clock_byname(composite->parents[mux]);
160 		if (clk_parent == NULL)
161 			continue;
162 
163 		const u_int prate = clk_get_rate(clk_parent);
164 		if (prate == 0)
165 			continue;
166 
167 		for (u_int div = 1; div <= __SHIFTOUT_MASK(composite->div_mask) + 1; div++) {
168 			const u_int cur_rate = prate / div;
169 			const int diff = (int)rate - (int)cur_rate;
170 			if (composite->flags & RK_COMPOSITE_ROUND_DOWN) {
171 				if (diff >= 0 && diff < best_diff) {
172 					best_diff = diff;
173 					best_mux = mux;
174 					best_div = div;
175 				}
176 			} else {
177 				if (abs(diff) < best_diff) {
178 					best_diff = abs(diff);
179 					best_mux = mux;
180 					best_div = div;
181 				}
182 			}
183 		}
184 	}
185 	if (best_diff == INT_MAX)
186 		return ERANGE;
187 
188 	uint32_t write_mask = composite->div_mask << 16;
189 	uint32_t write_val = __SHIFTIN(best_div - 1, composite->div_mask);
190 	if (composite->mux_mask) {
191 		write_mask |= composite->mux_mask << 16;
192 		write_val |= __SHIFTIN(best_mux, composite->mux_mask);
193 	}
194 
195 	CRU_WRITE(sc, composite->muxdiv_reg, write_mask | write_val);
196 
197 	return 0;
198 }
199 
200 const char *
201 rk_cru_composite_get_parent(struct rk_cru_softc *sc,
202     struct rk_cru_clk *clk)
203 {
204 	struct rk_cru_composite *composite = &clk->u.composite;
205 	uint32_t val;
206 	u_int mux;
207 
208 	KASSERT(clk->type == RK_CRU_COMPOSITE);
209 
210 	if (composite->mux_mask) {
211 		val = CRU_READ(sc, composite->muxdiv_reg);
212 		mux = __SHIFTOUT(val, composite->mux_mask);
213 	} else {
214 		mux = 0;
215 	}
216 
217 	return composite->parents[mux];
218 }
219 
220 int
221 rk_cru_composite_set_parent(struct rk_cru_softc *sc,
222     struct rk_cru_clk *clk, const char *parent)
223 {
224 	struct rk_cru_composite *composite = &clk->u.composite;
225 
226 	KASSERT(clk->type == RK_CRU_COMPOSITE);
227 
228 	if (!composite->mux_mask)
229 		return EINVAL;
230 
231 	for (u_int mux = 0; mux < composite->nparents; mux++) {
232 		if (strcmp(composite->parents[mux], parent) == 0) {
233 			const uint32_t write_mask = composite->mux_mask << 16;
234 			const uint32_t write_val = __SHIFTIN(mux, composite->mux_mask);
235 
236 			CRU_WRITE(sc, composite->muxdiv_reg, write_mask | write_val);
237 			return 0;
238 		}
239 	}
240 
241 	return EINVAL;
242 }
243