rk_cru_composite.c revision 1.3 1 /* $NetBSD: rk_cru_composite.c,v 1.3 2018/06/19 01:24:17 jmcneill 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.3 2018/06/19 01:24:17 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 const uint32_t val = CRU_READ(sc, composite->muxdiv_reg);
79 const u_int div = __SHIFTOUT(val, composite->div_mask) + 1;
80
81 return prate / div;
82 }
83
84 int
85 rk_cru_composite_set_rate(struct rk_cru_softc *sc,
86 struct rk_cru_clk *clk, u_int rate)
87 {
88 struct rk_cru_composite *composite = &clk->u.composite;
89 u_int best_div, best_mux, best_diff;
90 struct rk_cru_clk *rclk_parent;
91 struct clk *clk_parent;
92
93 KASSERT(clk->type == RK_CRU_COMPOSITE);
94
95 best_div = 0;
96 best_mux = 0;
97 best_diff = INT_MAX;
98 for (u_int mux = 0; mux < composite->nparents; mux++) {
99 rclk_parent = rk_cru_clock_find(sc, composite->parents[mux]);
100 if (rclk_parent != NULL)
101 clk_parent = &rclk_parent->base;
102 else
103 clk_parent = fdtbus_clock_byname(composite->parents[mux]);
104 if (clk_parent == NULL)
105 continue;
106
107 const u_int prate = clk_get_rate(clk_parent);
108 if (prate == 0)
109 continue;
110
111 for (u_int div = 1; div <= __SHIFTOUT_MASK(composite->div_mask) + 1; div++) {
112 const u_int cur_rate = prate / div;
113 const int diff = (int)rate - (int)cur_rate;
114 if (composite->flags & RK_COMPOSITE_ROUND_DOWN) {
115 if (diff >= 0 && diff < best_diff) {
116 best_diff = diff;
117 best_mux = mux;
118 best_div = div;
119 }
120 } else {
121 if (abs(diff) < best_diff) {
122 best_diff = abs(diff);
123 best_mux = mux;
124 best_div = div;
125 }
126 }
127 }
128 }
129 if (best_diff == INT_MAX)
130 return ERANGE;
131
132 uint32_t write_mask = composite->div_mask << 16;
133 uint32_t write_val = __SHIFTIN(best_div - 1, composite->div_mask);
134 if (composite->mux_mask) {
135 write_mask |= composite->mux_mask << 16;
136 write_val |= __SHIFTIN(best_mux, composite->mux_mask);
137 }
138
139 CRU_WRITE(sc, composite->muxdiv_reg, write_mask | write_val);
140
141 return 0;
142 }
143
144 const char *
145 rk_cru_composite_get_parent(struct rk_cru_softc *sc,
146 struct rk_cru_clk *clk)
147 {
148 struct rk_cru_composite *composite = &clk->u.composite;
149 uint32_t val;
150 u_int mux;
151
152 KASSERT(clk->type == RK_CRU_COMPOSITE);
153
154 if (composite->mux_mask) {
155 val = CRU_READ(sc, composite->muxdiv_reg);
156 mux = __SHIFTOUT(val, composite->mux_mask);
157 } else {
158 mux = 0;
159 }
160
161 return composite->parents[mux];
162 }
163
164 int
165 rk_cru_composite_set_parent(struct rk_cru_softc *sc,
166 struct rk_cru_clk *clk, const char *parent)
167 {
168 struct rk_cru_composite *composite = &clk->u.composite;
169
170 KASSERT(clk->type == RK_CRU_COMPOSITE);
171
172 if (!composite->mux_mask)
173 return EINVAL;
174
175 for (u_int mux = 0; mux < composite->nparents; mux++) {
176 if (strcmp(composite->parents[mux], parent) == 0) {
177 const uint32_t write_mask = composite->mux_mask << 16;
178 const uint32_t write_val = __SHIFTIN(mux, composite->mux_mask);
179
180 CRU_WRITE(sc, composite->muxdiv_reg, write_mask | write_val);
181 return 0;
182 }
183 }
184
185 return EINVAL;
186 }
187