sunxi_gates.c revision 1.2 1 1.2 aymeric /* $NetBSD: sunxi_gates.c,v 1.2 2018/09/09 07:21:18 aymeric Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2017 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.2 aymeric __KERNEL_RCSID(0, "$NetBSD: sunxi_gates.c,v 1.2 2018/09/09 07:21:18 aymeric 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 #include <sys/cpu.h>
35 1.1 jmcneill #include <sys/device.h>
36 1.1 jmcneill #include <sys/kmem.h>
37 1.1 jmcneill
38 1.1 jmcneill #include <dev/fdt/fdtvar.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <dev/clk/clk_backend.h>
41 1.1 jmcneill
42 1.1 jmcneill #define GATE_REG(index) (((index) / 32) * 4)
43 1.1 jmcneill #define GATE_MASK(index) __BIT((index) % 32)
44 1.1 jmcneill
45 1.1 jmcneill static const char * compatible[] = {
46 1.1 jmcneill "allwinner,sun4i-a10-gates-clk",
47 1.1 jmcneill NULL
48 1.1 jmcneill };
49 1.1 jmcneill
50 1.1 jmcneill struct sunxi_gate {
51 1.1 jmcneill struct clk base;
52 1.1 jmcneill u_int index;
53 1.1 jmcneill
54 1.1 jmcneill TAILQ_ENTRY(sunxi_gate) gates;
55 1.1 jmcneill };
56 1.1 jmcneill
57 1.1 jmcneill struct sunxi_gates_softc {
58 1.1 jmcneill device_t sc_dev;
59 1.1 jmcneill bus_space_tag_t sc_bst;
60 1.1 jmcneill bus_space_handle_t sc_bsh;
61 1.1 jmcneill int sc_phandle;
62 1.1 jmcneill
63 1.1 jmcneill struct clk_domain sc_clkdom;
64 1.1 jmcneill TAILQ_HEAD(, sunxi_gate) sc_gates;
65 1.1 jmcneill };
66 1.1 jmcneill
67 1.1 jmcneill #define GATE_READ(sc, reg) \
68 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
69 1.1 jmcneill #define GATE_WRITE(sc, reg, val) \
70 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
71 1.1 jmcneill
72 1.1 jmcneill static struct clk *
73 1.2 aymeric sunxi_gates_clock_decode(device_t dev, int cc_phandle, const void *data,
74 1.2 aymeric size_t len)
75 1.1 jmcneill {
76 1.1 jmcneill struct sunxi_gates_softc * const sc = device_private(dev);
77 1.1 jmcneill struct sunxi_gate *gate;
78 1.1 jmcneill
79 1.1 jmcneill if (len != 4)
80 1.1 jmcneill return NULL;
81 1.1 jmcneill
82 1.1 jmcneill const u_int index = be32dec(data);
83 1.1 jmcneill
84 1.1 jmcneill TAILQ_FOREACH(gate, &sc->sc_gates, gates)
85 1.1 jmcneill if (gate->index == index)
86 1.1 jmcneill return &gate->base;
87 1.1 jmcneill
88 1.1 jmcneill return NULL;
89 1.1 jmcneill }
90 1.1 jmcneill
91 1.1 jmcneill static const struct fdtbus_clock_controller_func sunxi_gates_fdtclock_funcs = {
92 1.1 jmcneill .decode = sunxi_gates_clock_decode,
93 1.1 jmcneill };
94 1.1 jmcneill
95 1.1 jmcneill static struct clk *
96 1.1 jmcneill sunxi_gates_clock_get(void *priv, const char *name)
97 1.1 jmcneill {
98 1.1 jmcneill struct sunxi_gates_softc * const sc = priv;
99 1.1 jmcneill struct sunxi_gate *gate;
100 1.1 jmcneill
101 1.1 jmcneill TAILQ_FOREACH(gate, &sc->sc_gates, gates)
102 1.1 jmcneill if (strcmp(gate->base.name, name) == 0)
103 1.1 jmcneill return &gate->base;
104 1.1 jmcneill
105 1.1 jmcneill return NULL;
106 1.1 jmcneill }
107 1.1 jmcneill
108 1.1 jmcneill static void
109 1.1 jmcneill sunxi_gates_clock_put(void *priv, struct clk *clk)
110 1.1 jmcneill {
111 1.1 jmcneill }
112 1.1 jmcneill
113 1.1 jmcneill static u_int
114 1.1 jmcneill sunxi_gates_clock_get_rate(void *priv, struct clk *clkp)
115 1.1 jmcneill {
116 1.1 jmcneill struct sunxi_gates_softc * const sc = priv;
117 1.1 jmcneill struct sunxi_gate *gate = (struct sunxi_gate *)clkp;
118 1.1 jmcneill struct clk *clkp_parent;
119 1.1 jmcneill
120 1.1 jmcneill clkp_parent = clk_get_parent(clkp);
121 1.1 jmcneill if (clkp_parent == NULL)
122 1.1 jmcneill return 0;
123 1.1 jmcneill
124 1.1 jmcneill const bus_size_t gate_reg = GATE_REG(gate->index);
125 1.1 jmcneill const uint32_t gate_mask = GATE_MASK(gate->index);
126 1.1 jmcneill
127 1.1 jmcneill if ((GATE_READ(sc, gate_reg) & gate_mask) == 0)
128 1.1 jmcneill return 0;
129 1.1 jmcneill
130 1.1 jmcneill return clk_get_rate(clkp_parent);
131 1.1 jmcneill }
132 1.1 jmcneill
133 1.1 jmcneill static int
134 1.1 jmcneill sunxi_gates_clock_enable(void *priv, struct clk *clkp)
135 1.1 jmcneill {
136 1.1 jmcneill struct sunxi_gates_softc * const sc = priv;
137 1.1 jmcneill struct sunxi_gate *gate = (struct sunxi_gate *)clkp;
138 1.1 jmcneill uint32_t val;
139 1.1 jmcneill
140 1.1 jmcneill const bus_size_t gate_reg = GATE_REG(gate->index);
141 1.1 jmcneill const uint32_t gate_mask = GATE_MASK(gate->index);
142 1.1 jmcneill
143 1.1 jmcneill val = GATE_READ(sc, gate_reg);
144 1.1 jmcneill val |= gate_mask;
145 1.1 jmcneill GATE_WRITE(sc, gate_reg, val);
146 1.1 jmcneill
147 1.1 jmcneill return 0;
148 1.1 jmcneill }
149 1.1 jmcneill
150 1.1 jmcneill static int
151 1.1 jmcneill sunxi_gates_clock_disable(void *priv, struct clk *clkp)
152 1.1 jmcneill {
153 1.1 jmcneill struct sunxi_gates_softc * const sc = priv;
154 1.1 jmcneill struct sunxi_gate *gate = (struct sunxi_gate *)clkp;
155 1.1 jmcneill uint32_t val;
156 1.1 jmcneill
157 1.1 jmcneill const bus_size_t gate_reg = GATE_REG(gate->index);
158 1.1 jmcneill const uint32_t gate_mask = GATE_MASK(gate->index);
159 1.1 jmcneill
160 1.1 jmcneill val = GATE_READ(sc, gate_reg);
161 1.1 jmcneill val &= ~gate_mask;
162 1.1 jmcneill GATE_WRITE(sc, gate_reg, val);
163 1.1 jmcneill
164 1.1 jmcneill return 0;
165 1.1 jmcneill }
166 1.1 jmcneill
167 1.1 jmcneill static struct clk *
168 1.1 jmcneill sunxi_gates_clock_get_parent(void *priv, struct clk *clkp)
169 1.1 jmcneill {
170 1.1 jmcneill struct sunxi_gates_softc * const sc = priv;
171 1.1 jmcneill
172 1.1 jmcneill return fdtbus_clock_get_index(sc->sc_phandle, 0);
173 1.1 jmcneill }
174 1.1 jmcneill
175 1.1 jmcneill static const struct clk_funcs sunxi_gates_clock_funcs = {
176 1.1 jmcneill .get = sunxi_gates_clock_get,
177 1.1 jmcneill .put = sunxi_gates_clock_put,
178 1.1 jmcneill .get_rate = sunxi_gates_clock_get_rate,
179 1.1 jmcneill .enable = sunxi_gates_clock_enable,
180 1.1 jmcneill .disable = sunxi_gates_clock_disable,
181 1.1 jmcneill .get_parent = sunxi_gates_clock_get_parent,
182 1.1 jmcneill };
183 1.1 jmcneill
184 1.1 jmcneill static void
185 1.1 jmcneill sunxi_gates_print(struct sunxi_gates_softc *sc)
186 1.1 jmcneill {
187 1.1 jmcneill struct sunxi_gate *gate;
188 1.1 jmcneill struct clk *clkp_parent;
189 1.1 jmcneill
190 1.1 jmcneill TAILQ_FOREACH(gate, &sc->sc_gates, gates) {
191 1.1 jmcneill clkp_parent = clk_get_parent(&gate->base);
192 1.1 jmcneill
193 1.1 jmcneill aprint_debug_dev(sc->sc_dev,
194 1.1 jmcneill "%3d %-12s %2s %-12s %-7s ",
195 1.1 jmcneill gate->index,
196 1.1 jmcneill gate->base.name,
197 1.1 jmcneill clkp_parent ? "<-" : "",
198 1.1 jmcneill clkp_parent ? clkp_parent->name : "",
199 1.1 jmcneill "gate");
200 1.1 jmcneill aprint_debug("%10d Hz\n", clk_get_rate(&gate->base));
201 1.1 jmcneill }
202 1.1 jmcneill }
203 1.1 jmcneill
204 1.1 jmcneill static int
205 1.1 jmcneill sunxi_gates_match(device_t parent, cfdata_t cf, void *aux)
206 1.1 jmcneill {
207 1.1 jmcneill struct fdt_attach_args * const faa = aux;
208 1.1 jmcneill
209 1.1 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
210 1.1 jmcneill }
211 1.1 jmcneill
212 1.1 jmcneill static void
213 1.1 jmcneill sunxi_gates_attach(device_t parent, device_t self, void *aux)
214 1.1 jmcneill {
215 1.1 jmcneill struct sunxi_gates_softc * const sc = device_private(self);
216 1.1 jmcneill struct fdt_attach_args * const faa = aux;
217 1.1 jmcneill const int phandle = faa->faa_phandle;
218 1.1 jmcneill struct sunxi_gate *gate;
219 1.1 jmcneill const u_int *indices;
220 1.1 jmcneill bus_addr_t addr;
221 1.1 jmcneill bus_size_t size;
222 1.1 jmcneill int len, i;
223 1.1 jmcneill
224 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
225 1.1 jmcneill aprint_error(": couldn't get registers\n");
226 1.1 jmcneill return;
227 1.1 jmcneill }
228 1.1 jmcneill
229 1.1 jmcneill sc->sc_dev = self;
230 1.1 jmcneill sc->sc_phandle = phandle;
231 1.1 jmcneill sc->sc_bst = faa->faa_bst;
232 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
233 1.1 jmcneill aprint_error(": couldn't map registers\n");
234 1.1 jmcneill return;
235 1.1 jmcneill }
236 1.1 jmcneill TAILQ_INIT(&sc->sc_gates);
237 1.1 jmcneill
238 1.1 jmcneill aprint_naive("\n");
239 1.1 jmcneill aprint_normal("\n");
240 1.1 jmcneill
241 1.1 jmcneill sc->sc_clkdom.funcs = &sunxi_gates_clock_funcs;
242 1.1 jmcneill sc->sc_clkdom.priv = sc;
243 1.1 jmcneill
244 1.1 jmcneill indices = fdtbus_get_prop(phandle, "clock-indices", &len);
245 1.1 jmcneill if (indices == NULL) {
246 1.1 jmcneill aprint_error_dev(self, "no clock-indices property\n");
247 1.1 jmcneill return;
248 1.1 jmcneill }
249 1.1 jmcneill
250 1.1 jmcneill for (i = 0;
251 1.1 jmcneill len >= sizeof(u_int);
252 1.1 jmcneill len -= sizeof(u_int), i++, indices++) {
253 1.1 jmcneill const u_int index = be32dec(indices);
254 1.1 jmcneill const char *name = fdtbus_get_string_index(phandle,
255 1.1 jmcneill "clock-output-names", i);
256 1.1 jmcneill
257 1.1 jmcneill if (name == NULL) {
258 1.1 jmcneill aprint_error_dev(self, "no name for clk index %d\n",
259 1.1 jmcneill index);
260 1.1 jmcneill continue;
261 1.1 jmcneill }
262 1.1 jmcneill
263 1.1 jmcneill gate = kmem_zalloc(sizeof(*gate), KM_SLEEP);
264 1.1 jmcneill gate->base.domain = &sc->sc_clkdom;
265 1.1 jmcneill gate->base.name = name;
266 1.1 jmcneill gate->index = index;
267 1.1 jmcneill
268 1.1 jmcneill TAILQ_INSERT_TAIL(&sc->sc_gates, gate, gates);
269 1.1 jmcneill }
270 1.1 jmcneill
271 1.1 jmcneill fdtbus_register_clock_controller(sc->sc_dev, phandle,
272 1.1 jmcneill &sunxi_gates_fdtclock_funcs);
273 1.1 jmcneill
274 1.1 jmcneill sunxi_gates_print(sc);
275 1.1 jmcneill }
276 1.1 jmcneill
277 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_gates, sizeof(struct sunxi_gates_softc),
278 1.1 jmcneill sunxi_gates_match, sunxi_gates_attach, NULL, NULL);
279