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