rk_cru_composite.c revision 1.5.14.1 1 /* $NetBSD: rk_cru_composite.c,v 1.5.14.1 2021/05/31 22:15:11 cjep Exp $ */
2
3 /*-
4 * Copyright (c) 2018 Jared McNeill <jmcneill (at) 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.14.1 2021/05/31 22:15:11 cjep 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 = (composite->div_mask != 0)
87 ? __SHIFTOUT(val, composite->div_mask) + 1 : 1;
88
89 return prate / div;
90 }
91 }
92
93 static u_int
94 rk_cru_composite_get_frac_div(u_int n, u_int d)
95 {
96 u_int tmp;
97
98 while (d > 0) {
99 tmp = d;
100 d = n % d;
101 n = tmp;
102 }
103
104 return n;
105 }
106
107 static int
108 rk_cru_composite_set_rate_frac(struct rk_cru_softc *sc,
109 struct rk_cru_clk *clk, u_int rate)
110 {
111 struct rk_cru_composite *composite = &clk->u.composite;
112 struct clk *clk_parent;
113
114 clk_parent = clk_get_parent(&clk->base);
115 if (clk_parent == NULL)
116 return ENXIO;
117
118 const u_int prate = clk_get_rate(clk_parent);
119 const u_int v = rk_cru_composite_get_frac_div(prate, rate);
120 const u_int num = (prate / v) & 0xffff;
121 const u_int den = (rate / v) & 0xffff;
122 if (prate / num * den != rate)
123 return EINVAL;
124
125 CRU_WRITE(sc, composite->frac_reg, (den << 16) | num);
126
127 return 0;
128 }
129
130 int
131 rk_cru_composite_set_rate(struct rk_cru_softc *sc,
132 struct rk_cru_clk *clk, u_int rate)
133 {
134 struct rk_cru_composite *composite = &clk->u.composite;
135 u_int best_div, best_mux, best_diff;
136 struct rk_cru_clk *rclk_parent;
137 struct clk *clk_parent;
138
139 KASSERT(clk->type == RK_CRU_COMPOSITE);
140
141 if (composite->flags & RK_COMPOSITE_SET_RATE_PARENT) {
142 clk_parent = clk_get_parent(&clk->base);
143 if (clk_parent == NULL)
144 return ENXIO;
145 return clk_set_rate(clk_parent, rate);
146 }
147
148 if (composite->flags & RK_COMPOSITE_FRACDIV) {
149 return rk_cru_composite_set_rate_frac(sc, clk, rate);
150 }
151
152 best_div = 0;
153 best_mux = 0;
154 best_diff = INT_MAX;
155 for (u_int mux = 0; mux < composite->nparents; mux++) {
156 rclk_parent = rk_cru_clock_find(sc, composite->parents[mux]);
157 if (rclk_parent != NULL)
158 clk_parent = &rclk_parent->base;
159 else
160 clk_parent = fdtbus_clock_byname(composite->parents[mux]);
161 if (clk_parent == NULL)
162 continue;
163
164 const u_int prate = clk_get_rate(clk_parent);
165 if (prate == 0)
166 continue;
167
168 for (u_int div = 1; div <= __SHIFTOUT_MASK(composite->div_mask) + 1; div++) {
169 const u_int cur_rate = prate / div;
170 const int diff = (int)rate - (int)cur_rate;
171 if (composite->flags & RK_COMPOSITE_ROUND_DOWN) {
172 if (diff >= 0 && diff < best_diff) {
173 best_diff = diff;
174 best_mux = mux;
175 best_div = div;
176 }
177 } else {
178 if (abs(diff) < best_diff) {
179 best_diff = abs(diff);
180 best_mux = mux;
181 best_div = div;
182 }
183 }
184 }
185 }
186 if (best_diff == INT_MAX)
187 return ERANGE;
188
189 uint32_t write_mask = composite->div_mask << 16;
190 uint32_t write_val = __SHIFTIN(best_div - 1, composite->div_mask);
191 if (composite->mux_mask) {
192 write_mask |= composite->mux_mask << 16;
193 write_val |= __SHIFTIN(best_mux, composite->mux_mask);
194 }
195
196 CRU_WRITE(sc, composite->muxdiv_reg, write_mask | write_val);
197
198 return 0;
199 }
200
201 const char *
202 rk_cru_composite_get_parent(struct rk_cru_softc *sc,
203 struct rk_cru_clk *clk)
204 {
205 struct rk_cru_composite *composite = &clk->u.composite;
206 uint32_t val;
207 u_int mux;
208
209 KASSERT(clk->type == RK_CRU_COMPOSITE);
210
211 if (composite->mux_mask) {
212 val = CRU_READ(sc, composite->muxdiv_reg);
213 mux = __SHIFTOUT(val, composite->mux_mask);
214 } else {
215 mux = 0;
216 }
217
218 return composite->parents[mux];
219 }
220
221 int
222 rk_cru_composite_set_parent(struct rk_cru_softc *sc,
223 struct rk_cru_clk *clk, const char *parent)
224 {
225 struct rk_cru_composite *composite = &clk->u.composite;
226
227 KASSERT(clk->type == RK_CRU_COMPOSITE);
228
229 if (!composite->mux_mask)
230 return EINVAL;
231
232 for (u_int mux = 0; mux < composite->nparents; mux++) {
233 if (strcmp(composite->parents[mux], parent) == 0) {
234 const uint32_t write_mask = composite->mux_mask << 16;
235 const uint32_t write_val = __SHIFTIN(mux, composite->mux_mask);
236
237 CRU_WRITE(sc, composite->muxdiv_reg, write_mask | write_val);
238 return 0;
239 }
240 }
241
242 return EINVAL;
243 }
244