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