sunxi_gpio.c revision 1.39 1 1.39 skrll /* $NetBSD: sunxi_gpio.c,v 1.39 2024/08/13 07:20:23 skrll 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 "opt_soc.h"
30 1.1 jmcneill
31 1.1 jmcneill #include <sys/cdefs.h>
32 1.39 skrll __KERNEL_RCSID(0, "$NetBSD: sunxi_gpio.c,v 1.39 2024/08/13 07:20:23 skrll Exp $");
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/param.h>
35 1.1 jmcneill #include <sys/bus.h>
36 1.1 jmcneill #include <sys/device.h>
37 1.1 jmcneill #include <sys/intr.h>
38 1.1 jmcneill #include <sys/systm.h>
39 1.1 jmcneill #include <sys/mutex.h>
40 1.1 jmcneill #include <sys/kmem.h>
41 1.1 jmcneill #include <sys/gpio.h>
42 1.12 jmcneill #include <sys/bitops.h>
43 1.13 jmcneill #include <sys/lwp.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/fdt/fdtvar.h>
46 1.5 jmcneill #include <dev/gpio/gpiovar.h>
47 1.1 jmcneill
48 1.1 jmcneill #include <arm/sunxi/sunxi_gpio.h>
49 1.1 jmcneill
50 1.15 jmcneill #define SUNXI_GPIO_MAX_EINT_BANK 5
51 1.12 jmcneill #define SUNXI_GPIO_MAX_EINT 32
52 1.12 jmcneill
53 1.24 jmcneill #define SUNXI_GPIO_MAX_BANK 26
54 1.24 jmcneill
55 1.4 jmcneill #define SUNXI_GPIO_PORT(port) (0x24 * (port))
56 1.4 jmcneill #define SUNXI_GPIO_CFG(port, pin) (SUNXI_GPIO_PORT(port) + 0x00 + (0x4 * ((pin) / 8)))
57 1.28 skrll #define SUNXI_GPIO_CFG_PINMASK(pin) (0x7U << (((pin) % 8) * 4))
58 1.1 jmcneill #define SUNXI_GPIO_DATA(port) (SUNXI_GPIO_PORT(port) + 0x10)
59 1.1 jmcneill #define SUNXI_GPIO_DRV(port, pin) (SUNXI_GPIO_PORT(port) + 0x14 + (0x4 * ((pin) / 16)))
60 1.28 skrll #define SUNXI_GPIO_DRV_PINMASK(pin) (0x3U << (((pin) % 16) * 2))
61 1.1 jmcneill #define SUNXI_GPIO_PULL(port, pin) (SUNXI_GPIO_PORT(port) + 0x1c + (0x4 * ((pin) / 16)))
62 1.2 jmcneill #define SUNXI_GPIO_PULL_DISABLE 0
63 1.2 jmcneill #define SUNXI_GPIO_PULL_UP 1
64 1.2 jmcneill #define SUNXI_GPIO_PULL_DOWN 2
65 1.28 skrll #define SUNXI_GPIO_PULL_PINMASK(pin) (0x3U << (((pin) % 16) * 2))
66 1.15 jmcneill #define SUNXI_GPIO_INT_CFG(bank, eint) (0x200 + (0x20 * (bank)) + (0x4 * ((eint) / 8)))
67 1.28 skrll #define SUNXI_GPIO_INT_MODEMASK(eint) (0xfU << (((eint) % 8) * 4))
68 1.12 jmcneill #define SUNXI_GPIO_INT_MODE_POS_EDGE 0x0
69 1.12 jmcneill #define SUNXI_GPIO_INT_MODE_NEG_EDGE 0x1
70 1.12 jmcneill #define SUNXI_GPIO_INT_MODE_HIGH_LEVEL 0x2
71 1.12 jmcneill #define SUNXI_GPIO_INT_MODE_LOW_LEVEL 0x3
72 1.12 jmcneill #define SUNXI_GPIO_INT_MODE_DOUBLE_EDGE 0x4
73 1.15 jmcneill #define SUNXI_GPIO_INT_CTL(bank) (0x210 + 0x20 * (bank))
74 1.15 jmcneill #define SUNXI_GPIO_INT_STATUS(bank) (0x214 + 0x20 * (bank))
75 1.26 tnn #define SUNXI_GPIO_INT_DEBOUNCE(bank) (0x218 + 0x20 * (bank))
76 1.26 tnn #define SUNXI_GPIO_INT_DEBOUNCE_CLK_PRESCALE __BITS(6,4)
77 1.26 tnn #define SUNXI_GPIO_INT_DEBOUNCE_CLK_SEL __BIT(0)
78 1.24 jmcneill #define SUNXI_GPIO_GRP_CONFIG(bank) (0x300 + 0x4 * (bank))
79 1.24 jmcneill #define SUNXI_GPIO_GRP_IO_BIAS_CONFIGMASK 0xf
80 1.1 jmcneill
81 1.31 thorpej static const struct device_compatible_entry compat_data[] = {
82 1.14 jmcneill #ifdef SOC_SUN4I_A10
83 1.31 thorpej { .compat = "allwinner,sun4i-a10-pinctrl",
84 1.31 thorpej .data = &sun4i_a10_padconf },
85 1.14 jmcneill #endif
86 1.11 jmcneill #ifdef SOC_SUN5I_A13
87 1.31 thorpej { .compat = "allwinner,sun5i-a13-pinctrl",
88 1.31 thorpej .data = &sun5i_a13_padconf },
89 1.32 skrll { .compat = "nextthing,gr8-pinctrl",
90 1.31 thorpej .data = &sun5i_a13_padconf },
91 1.11 jmcneill #endif
92 1.1 jmcneill #ifdef SOC_SUN6I_A31
93 1.31 thorpej { .compat = "allwinner,sun6i-a31-pinctrl",
94 1.31 thorpej .data = &sun6i_a31_padconf },
95 1.31 thorpej { .compat = "allwinner,sun6i-a31-r-pinctrl",
96 1.31 thorpej .data = &sun6i_a31_r_padconf },
97 1.1 jmcneill #endif
98 1.14 jmcneill #ifdef SOC_SUN7I_A20
99 1.31 thorpej { .compat = "allwinner,sun7i-a20-pinctrl",
100 1.31 thorpej .data = &sun7i_a20_padconf },
101 1.14 jmcneill #endif
102 1.6 jmcneill #ifdef SOC_SUN8I_A83T
103 1.31 thorpej { .compat = "allwinner,sun8i-a83t-pinctrl",
104 1.31 thorpej .data = &sun8i_a83t_padconf },
105 1.31 thorpej { .compat = "allwinner,sun8i-a83t-r-pinctrl",
106 1.31 thorpej .data = &sun8i_a83t_r_padconf },
107 1.6 jmcneill #endif
108 1.1 jmcneill #ifdef SOC_SUN8I_H3
109 1.31 thorpej { .compat = "allwinner,sun8i-h3-pinctrl",
110 1.31 thorpej .data = &sun8i_h3_padconf },
111 1.31 thorpej { .compat = "allwinner,sun8i-h3-r-pinctrl",
112 1.31 thorpej .data = &sun8i_h3_r_padconf },
113 1.1 jmcneill #endif
114 1.38 skrll #ifdef SOC_SUN8I_V3S
115 1.38 skrll { .compat = "allwinner,sun8i-v3s-pinctrl",
116 1.38 skrll .data = &sun8i_v3s_padconf },
117 1.38 skrll #endif
118 1.15 jmcneill #ifdef SOC_SUN9I_A80
119 1.31 thorpej { .compat = "allwinner,sun9i-a80-pinctrl",
120 1.31 thorpej .data = &sun9i_a80_padconf },
121 1.31 thorpej { .compat = "allwinner,sun9i-a80-r-pinctrl",
122 1.31 thorpej .data = &sun9i_a80_r_padconf },
123 1.15 jmcneill #endif
124 1.39 skrll #ifdef SOC_SUN20I_D1
125 1.39 skrll { .compat = "allwinner,sun20i-d1-pinctrl",
126 1.39 skrll .data = &sun20i_d1_padconf },
127 1.39 skrll #endif
128 1.9 jmcneill #ifdef SOC_SUN50I_A64
129 1.31 thorpej { .compat = "allwinner,sun50i-a64-pinctrl",
130 1.31 thorpej .data = &sun50i_a64_padconf },
131 1.31 thorpej { .compat = "allwinner,sun50i-a64-r-pinctrl",
132 1.31 thorpej .data = &sun50i_a64_r_padconf },
133 1.9 jmcneill #endif
134 1.16 jmcneill #ifdef SOC_SUN50I_H5
135 1.31 thorpej { .compat = "allwinner,sun50i-h5-pinctrl",
136 1.31 thorpej .data = &sun8i_h3_padconf },
137 1.31 thorpej { .compat = "allwinner,sun50i-h5-r-pinctrl",
138 1.31 thorpej .data = &sun8i_h3_r_padconf },
139 1.16 jmcneill #endif
140 1.19 jmcneill #ifdef SOC_SUN50I_H6
141 1.31 thorpej { .compat = "allwinner,sun50i-h6-pinctrl",
142 1.31 thorpej .data = &sun50i_h6_padconf },
143 1.31 thorpej { .compat = "allwinner,sun50i-h6-r-pinctrl",
144 1.31 thorpej .data = &sun50i_h6_r_padconf },
145 1.19 jmcneill #endif
146 1.34 thorpej DEVICE_COMPAT_EOL
147 1.1 jmcneill };
148 1.1 jmcneill
149 1.12 jmcneill struct sunxi_gpio_eint {
150 1.12 jmcneill int (*eint_func)(void *);
151 1.12 jmcneill void *eint_arg;
152 1.25 tnn bool eint_mpsafe;
153 1.15 jmcneill int eint_bank;
154 1.12 jmcneill int eint_num;
155 1.12 jmcneill };
156 1.12 jmcneill
157 1.1 jmcneill struct sunxi_gpio_softc {
158 1.1 jmcneill device_t sc_dev;
159 1.1 jmcneill bus_space_tag_t sc_bst;
160 1.1 jmcneill bus_space_handle_t sc_bsh;
161 1.24 jmcneill int sc_phandle;
162 1.1 jmcneill const struct sunxi_gpio_padconf *sc_padconf;
163 1.5 jmcneill kmutex_t sc_lock;
164 1.5 jmcneill
165 1.5 jmcneill struct gpio_chipset_tag sc_gp;
166 1.5 jmcneill gpio_pin_t *sc_pins;
167 1.5 jmcneill device_t sc_gpiodev;
168 1.12 jmcneill
169 1.24 jmcneill struct fdtbus_regulator *sc_pin_supply[SUNXI_GPIO_MAX_BANK];
170 1.24 jmcneill
171 1.15 jmcneill u_int sc_eint_bank_max;
172 1.15 jmcneill
173 1.12 jmcneill void *sc_ih;
174 1.15 jmcneill struct sunxi_gpio_eint sc_eint[SUNXI_GPIO_MAX_EINT_BANK][SUNXI_GPIO_MAX_EINT];
175 1.1 jmcneill };
176 1.1 jmcneill
177 1.1 jmcneill struct sunxi_gpio_pin {
178 1.1 jmcneill struct sunxi_gpio_softc *pin_sc;
179 1.1 jmcneill const struct sunxi_gpio_pins *pin_def;
180 1.1 jmcneill int pin_flags;
181 1.1 jmcneill bool pin_actlo;
182 1.1 jmcneill };
183 1.1 jmcneill
184 1.1 jmcneill #define GPIO_READ(sc, reg) \
185 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
186 1.1 jmcneill #define GPIO_WRITE(sc, reg, val) \
187 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
188 1.1 jmcneill
189 1.1 jmcneill static int sunxi_gpio_match(device_t, cfdata_t, void *);
190 1.1 jmcneill static void sunxi_gpio_attach(device_t, device_t, void *);
191 1.1 jmcneill
192 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_gpio, sizeof(struct sunxi_gpio_softc),
193 1.1 jmcneill sunxi_gpio_match, sunxi_gpio_attach, NULL, NULL);
194 1.1 jmcneill
195 1.1 jmcneill static const struct sunxi_gpio_pins *
196 1.1 jmcneill sunxi_gpio_lookup(struct sunxi_gpio_softc *sc, uint8_t port, uint8_t pin)
197 1.1 jmcneill {
198 1.1 jmcneill const struct sunxi_gpio_pins *pin_def;
199 1.1 jmcneill u_int n;
200 1.1 jmcneill
201 1.1 jmcneill for (n = 0; n < sc->sc_padconf->npins; n++) {
202 1.1 jmcneill pin_def = &sc->sc_padconf->pins[n];
203 1.1 jmcneill if (pin_def->port == port && pin_def->pin == pin)
204 1.1 jmcneill return pin_def;
205 1.1 jmcneill }
206 1.1 jmcneill
207 1.1 jmcneill return NULL;
208 1.1 jmcneill }
209 1.1 jmcneill
210 1.2 jmcneill static const struct sunxi_gpio_pins *
211 1.2 jmcneill sunxi_gpio_lookup_byname(struct sunxi_gpio_softc *sc, const char *name)
212 1.2 jmcneill {
213 1.2 jmcneill const struct sunxi_gpio_pins *pin_def;
214 1.2 jmcneill u_int n;
215 1.2 jmcneill
216 1.2 jmcneill for (n = 0; n < sc->sc_padconf->npins; n++) {
217 1.2 jmcneill pin_def = &sc->sc_padconf->pins[n];
218 1.2 jmcneill if (strcmp(pin_def->name, name) == 0)
219 1.2 jmcneill return pin_def;
220 1.2 jmcneill }
221 1.2 jmcneill
222 1.2 jmcneill return NULL;
223 1.2 jmcneill }
224 1.2 jmcneill
225 1.1 jmcneill static int
226 1.1 jmcneill sunxi_gpio_setfunc(struct sunxi_gpio_softc *sc,
227 1.1 jmcneill const struct sunxi_gpio_pins *pin_def, const char *func)
228 1.1 jmcneill {
229 1.1 jmcneill uint32_t cfg;
230 1.1 jmcneill u_int n;
231 1.1 jmcneill
232 1.5 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
233 1.5 jmcneill
234 1.1 jmcneill const bus_size_t cfg_reg = SUNXI_GPIO_CFG(pin_def->port, pin_def->pin);
235 1.1 jmcneill const uint32_t cfg_mask = SUNXI_GPIO_CFG_PINMASK(pin_def->pin);
236 1.1 jmcneill
237 1.1 jmcneill for (n = 0; n < SUNXI_GPIO_MAXFUNC; n++) {
238 1.1 jmcneill if (pin_def->functions[n] == NULL)
239 1.1 jmcneill continue;
240 1.1 jmcneill if (strcmp(pin_def->functions[n], func) == 0) {
241 1.1 jmcneill cfg = GPIO_READ(sc, cfg_reg);
242 1.1 jmcneill cfg &= ~cfg_mask;
243 1.1 jmcneill cfg |= __SHIFTIN(n, cfg_mask);
244 1.4 jmcneill #ifdef SUNXI_GPIO_DEBUG
245 1.4 jmcneill device_printf(sc->sc_dev, "P%c%02d cfg %08x -> %08x\n",
246 1.4 jmcneill pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, cfg_reg), cfg);
247 1.4 jmcneill #endif
248 1.1 jmcneill GPIO_WRITE(sc, cfg_reg, cfg);
249 1.1 jmcneill return 0;
250 1.1 jmcneill }
251 1.1 jmcneill }
252 1.1 jmcneill
253 1.1 jmcneill /* Function not found */
254 1.1 jmcneill device_printf(sc->sc_dev, "function '%s' not supported on P%c%02d\n",
255 1.1 jmcneill func, pin_def->port + 'A', pin_def->pin);
256 1.1 jmcneill
257 1.1 jmcneill return ENXIO;
258 1.1 jmcneill }
259 1.1 jmcneill
260 1.1 jmcneill static int
261 1.2 jmcneill sunxi_gpio_setpull(struct sunxi_gpio_softc *sc,
262 1.2 jmcneill const struct sunxi_gpio_pins *pin_def, int flags)
263 1.2 jmcneill {
264 1.2 jmcneill uint32_t pull;
265 1.2 jmcneill
266 1.5 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
267 1.5 jmcneill
268 1.2 jmcneill const bus_size_t pull_reg = SUNXI_GPIO_PULL(pin_def->port, pin_def->pin);
269 1.2 jmcneill const uint32_t pull_mask = SUNXI_GPIO_PULL_PINMASK(pin_def->pin);
270 1.2 jmcneill
271 1.2 jmcneill pull = GPIO_READ(sc, pull_reg);
272 1.2 jmcneill pull &= ~pull_mask;
273 1.2 jmcneill if (flags & GPIO_PIN_PULLUP)
274 1.2 jmcneill pull |= __SHIFTIN(SUNXI_GPIO_PULL_UP, pull_mask);
275 1.2 jmcneill else if (flags & GPIO_PIN_PULLDOWN)
276 1.2 jmcneill pull |= __SHIFTIN(SUNXI_GPIO_PULL_DOWN, pull_mask);
277 1.2 jmcneill else
278 1.2 jmcneill pull |= __SHIFTIN(SUNXI_GPIO_PULL_DISABLE, pull_mask);
279 1.4 jmcneill #ifdef SUNXI_GPIO_DEBUG
280 1.4 jmcneill device_printf(sc->sc_dev, "P%c%02d pull %08x -> %08x\n",
281 1.4 jmcneill pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, pull_reg), pull);
282 1.4 jmcneill #endif
283 1.2 jmcneill GPIO_WRITE(sc, pull_reg, pull);
284 1.2 jmcneill
285 1.2 jmcneill return 0;
286 1.2 jmcneill }
287 1.2 jmcneill
288 1.2 jmcneill static int
289 1.2 jmcneill sunxi_gpio_setdrv(struct sunxi_gpio_softc *sc,
290 1.2 jmcneill const struct sunxi_gpio_pins *pin_def, int drive_strength)
291 1.2 jmcneill {
292 1.2 jmcneill uint32_t drv;
293 1.2 jmcneill
294 1.5 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
295 1.5 jmcneill
296 1.2 jmcneill if (drive_strength < 10 || drive_strength > 40)
297 1.2 jmcneill return EINVAL;
298 1.2 jmcneill
299 1.2 jmcneill const bus_size_t drv_reg = SUNXI_GPIO_DRV(pin_def->port, pin_def->pin);
300 1.2 jmcneill const uint32_t drv_mask = SUNXI_GPIO_DRV_PINMASK(pin_def->pin);
301 1.2 jmcneill
302 1.2 jmcneill drv = GPIO_READ(sc, drv_reg);
303 1.2 jmcneill drv &= ~drv_mask;
304 1.2 jmcneill drv |= __SHIFTIN((drive_strength / 10) - 1, drv_mask);
305 1.4 jmcneill #ifdef SUNXI_GPIO_DEBUG
306 1.4 jmcneill device_printf(sc->sc_dev, "P%c%02d drv %08x -> %08x\n",
307 1.4 jmcneill pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, drv_reg), drv);
308 1.4 jmcneill #endif
309 1.2 jmcneill GPIO_WRITE(sc, drv_reg, drv);
310 1.2 jmcneill
311 1.2 jmcneill return 0;
312 1.2 jmcneill }
313 1.2 jmcneill
314 1.2 jmcneill static int
315 1.1 jmcneill sunxi_gpio_ctl(struct sunxi_gpio_softc *sc, const struct sunxi_gpio_pins *pin_def,
316 1.1 jmcneill int flags)
317 1.1 jmcneill {
318 1.5 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
319 1.5 jmcneill
320 1.1 jmcneill if (flags & GPIO_PIN_INPUT)
321 1.1 jmcneill return sunxi_gpio_setfunc(sc, pin_def, "gpio_in");
322 1.1 jmcneill if (flags & GPIO_PIN_OUTPUT)
323 1.1 jmcneill return sunxi_gpio_setfunc(sc, pin_def, "gpio_out");
324 1.1 jmcneill
325 1.1 jmcneill return EINVAL;
326 1.1 jmcneill }
327 1.1 jmcneill
328 1.1 jmcneill static void *
329 1.1 jmcneill sunxi_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
330 1.1 jmcneill {
331 1.1 jmcneill struct sunxi_gpio_softc * const sc = device_private(dev);
332 1.1 jmcneill const struct sunxi_gpio_pins *pin_def;
333 1.1 jmcneill struct sunxi_gpio_pin *gpin;
334 1.1 jmcneill const u_int *gpio = data;
335 1.1 jmcneill int error;
336 1.1 jmcneill
337 1.1 jmcneill if (len != 16)
338 1.1 jmcneill return NULL;
339 1.1 jmcneill
340 1.1 jmcneill const uint8_t port = be32toh(gpio[1]) & 0xff;
341 1.1 jmcneill const uint8_t pin = be32toh(gpio[2]) & 0xff;
342 1.1 jmcneill const bool actlo = be32toh(gpio[3]) & 1;
343 1.1 jmcneill
344 1.1 jmcneill pin_def = sunxi_gpio_lookup(sc, port, pin);
345 1.1 jmcneill if (pin_def == NULL)
346 1.1 jmcneill return NULL;
347 1.1 jmcneill
348 1.5 jmcneill mutex_enter(&sc->sc_lock);
349 1.1 jmcneill error = sunxi_gpio_ctl(sc, pin_def, flags);
350 1.5 jmcneill mutex_exit(&sc->sc_lock);
351 1.5 jmcneill
352 1.1 jmcneill if (error != 0)
353 1.1 jmcneill return NULL;
354 1.1 jmcneill
355 1.1 jmcneill gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
356 1.1 jmcneill gpin->pin_sc = sc;
357 1.1 jmcneill gpin->pin_def = pin_def;
358 1.1 jmcneill gpin->pin_flags = flags;
359 1.1 jmcneill gpin->pin_actlo = actlo;
360 1.1 jmcneill
361 1.1 jmcneill return gpin;
362 1.1 jmcneill }
363 1.1 jmcneill
364 1.1 jmcneill static void
365 1.1 jmcneill sunxi_gpio_release(device_t dev, void *priv)
366 1.1 jmcneill {
367 1.21 jmcneill struct sunxi_gpio_softc * const sc = device_private(dev);
368 1.1 jmcneill struct sunxi_gpio_pin *pin = priv;
369 1.1 jmcneill
370 1.21 jmcneill mutex_enter(&sc->sc_lock);
371 1.1 jmcneill sunxi_gpio_ctl(pin->pin_sc, pin->pin_def, GPIO_PIN_INPUT);
372 1.21 jmcneill mutex_exit(&sc->sc_lock);
373 1.1 jmcneill
374 1.1 jmcneill kmem_free(pin, sizeof(*pin));
375 1.18 skrll }
376 1.1 jmcneill
377 1.1 jmcneill static int
378 1.1 jmcneill sunxi_gpio_read(device_t dev, void *priv, bool raw)
379 1.1 jmcneill {
380 1.1 jmcneill struct sunxi_gpio_softc * const sc = device_private(dev);
381 1.1 jmcneill struct sunxi_gpio_pin *pin = priv;
382 1.1 jmcneill const struct sunxi_gpio_pins *pin_def = pin->pin_def;
383 1.1 jmcneill uint32_t data;
384 1.1 jmcneill int val;
385 1.1 jmcneill
386 1.1 jmcneill KASSERT(sc == pin->pin_sc);
387 1.1 jmcneill
388 1.1 jmcneill const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
389 1.1 jmcneill const uint32_t data_mask = __BIT(pin_def->pin);
390 1.1 jmcneill
391 1.5 jmcneill /* No lock required for reads */
392 1.1 jmcneill data = GPIO_READ(sc, data_reg);
393 1.1 jmcneill val = __SHIFTOUT(data, data_mask);
394 1.1 jmcneill if (!raw && pin->pin_actlo)
395 1.1 jmcneill val = !val;
396 1.1 jmcneill
397 1.1 jmcneill #ifdef SUNXI_GPIO_DEBUG
398 1.1 jmcneill device_printf(dev, "P%c%02d rd %08x (%d %d)\n",
399 1.1 jmcneill pin_def->port + 'A', pin_def->pin, data,
400 1.1 jmcneill __SHIFTOUT(val, data_mask), val);
401 1.1 jmcneill #endif
402 1.1 jmcneill
403 1.1 jmcneill return val;
404 1.1 jmcneill }
405 1.1 jmcneill
406 1.1 jmcneill static void
407 1.1 jmcneill sunxi_gpio_write(device_t dev, void *priv, int val, bool raw)
408 1.1 jmcneill {
409 1.1 jmcneill struct sunxi_gpio_softc * const sc = device_private(dev);
410 1.1 jmcneill struct sunxi_gpio_pin *pin = priv;
411 1.1 jmcneill const struct sunxi_gpio_pins *pin_def = pin->pin_def;
412 1.1 jmcneill uint32_t data;
413 1.1 jmcneill
414 1.1 jmcneill KASSERT(sc == pin->pin_sc);
415 1.1 jmcneill
416 1.1 jmcneill const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
417 1.1 jmcneill const uint32_t data_mask = __BIT(pin_def->pin);
418 1.1 jmcneill
419 1.1 jmcneill if (!raw && pin->pin_actlo)
420 1.1 jmcneill val = !val;
421 1.1 jmcneill
422 1.5 jmcneill mutex_enter(&sc->sc_lock);
423 1.1 jmcneill data = GPIO_READ(sc, data_reg);
424 1.1 jmcneill data &= ~data_mask;
425 1.1 jmcneill data |= __SHIFTIN(val, data_mask);
426 1.1 jmcneill #ifdef SUNXI_GPIO_DEBUG
427 1.1 jmcneill device_printf(dev, "P%c%02d wr %08x -> %08x\n",
428 1.4 jmcneill pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, data_reg), data);
429 1.1 jmcneill #endif
430 1.7 jmcneill GPIO_WRITE(sc, data_reg, data);
431 1.5 jmcneill mutex_exit(&sc->sc_lock);
432 1.1 jmcneill }
433 1.1 jmcneill
434 1.1 jmcneill static struct fdtbus_gpio_controller_func sunxi_gpio_funcs = {
435 1.1 jmcneill .acquire = sunxi_gpio_acquire,
436 1.1 jmcneill .release = sunxi_gpio_release,
437 1.1 jmcneill .read = sunxi_gpio_read,
438 1.1 jmcneill .write = sunxi_gpio_write,
439 1.1 jmcneill };
440 1.1 jmcneill
441 1.12 jmcneill static int
442 1.12 jmcneill sunxi_gpio_intr(void *priv)
443 1.12 jmcneill {
444 1.12 jmcneill struct sunxi_gpio_softc * const sc = priv;
445 1.12 jmcneill struct sunxi_gpio_eint *eint;
446 1.12 jmcneill uint32_t status, bit;
447 1.15 jmcneill u_int bank;
448 1.12 jmcneill int ret = 0;
449 1.12 jmcneill
450 1.15 jmcneill for (bank = 0; bank <= sc->sc_eint_bank_max; bank++) {
451 1.15 jmcneill status = GPIO_READ(sc, SUNXI_GPIO_INT_STATUS(bank));
452 1.15 jmcneill if (status == 0)
453 1.15 jmcneill continue;
454 1.15 jmcneill GPIO_WRITE(sc, SUNXI_GPIO_INT_STATUS(bank), status);
455 1.12 jmcneill
456 1.15 jmcneill while ((bit = ffs32(status)) != 0) {
457 1.15 jmcneill status &= ~__BIT(bit - 1);
458 1.15 jmcneill eint = &sc->sc_eint[bank][bit - 1];
459 1.15 jmcneill if (eint->eint_func == NULL)
460 1.15 jmcneill continue;
461 1.25 tnn if (!eint->eint_mpsafe)
462 1.15 jmcneill KERNEL_LOCK(1, curlwp);
463 1.15 jmcneill ret |= eint->eint_func(eint->eint_arg);
464 1.25 tnn if (!eint->eint_mpsafe)
465 1.15 jmcneill KERNEL_UNLOCK_ONE(curlwp);
466 1.15 jmcneill }
467 1.12 jmcneill }
468 1.12 jmcneill
469 1.12 jmcneill return ret;
470 1.12 jmcneill }
471 1.12 jmcneill
472 1.12 jmcneill static void *
473 1.25 tnn sunxi_intr_enable(struct sunxi_gpio_softc *sc,
474 1.25 tnn const struct sunxi_gpio_pins *pin_def, u_int mode, bool mpsafe,
475 1.12 jmcneill int (*func)(void *), void *arg)
476 1.12 jmcneill {
477 1.25 tnn uint32_t val;
478 1.12 jmcneill struct sunxi_gpio_eint *eint;
479 1.32 skrll
480 1.12 jmcneill if (pin_def->functions[pin_def->eint_func] == NULL ||
481 1.20 bouyer strcmp(pin_def->functions[pin_def->eint_func], "irq") != 0)
482 1.12 jmcneill return NULL;
483 1.12 jmcneill
484 1.12 jmcneill KASSERT(pin_def->eint_num < SUNXI_GPIO_MAX_EINT);
485 1.12 jmcneill
486 1.12 jmcneill mutex_enter(&sc->sc_lock);
487 1.12 jmcneill
488 1.15 jmcneill eint = &sc->sc_eint[pin_def->eint_bank][pin_def->eint_num];
489 1.12 jmcneill if (eint->eint_func != NULL) {
490 1.12 jmcneill mutex_exit(&sc->sc_lock);
491 1.12 jmcneill return NULL; /* in use */
492 1.12 jmcneill }
493 1.12 jmcneill
494 1.12 jmcneill /* Set function */
495 1.20 bouyer if (sunxi_gpio_setfunc(sc, pin_def, "irq") != 0) {
496 1.12 jmcneill mutex_exit(&sc->sc_lock);
497 1.12 jmcneill return NULL;
498 1.12 jmcneill }
499 1.12 jmcneill
500 1.12 jmcneill eint->eint_func = func;
501 1.12 jmcneill eint->eint_arg = arg;
502 1.25 tnn eint->eint_mpsafe = mpsafe;
503 1.15 jmcneill eint->eint_bank = pin_def->eint_bank;
504 1.12 jmcneill eint->eint_num = pin_def->eint_num;
505 1.12 jmcneill
506 1.12 jmcneill /* Configure eint mode */
507 1.15 jmcneill val = GPIO_READ(sc, SUNXI_GPIO_INT_CFG(eint->eint_bank, eint->eint_num));
508 1.12 jmcneill val &= ~SUNXI_GPIO_INT_MODEMASK(eint->eint_num);
509 1.12 jmcneill val |= __SHIFTIN(mode, SUNXI_GPIO_INT_MODEMASK(eint->eint_num));
510 1.15 jmcneill GPIO_WRITE(sc, SUNXI_GPIO_INT_CFG(eint->eint_bank, eint->eint_num), val);
511 1.12 jmcneill
512 1.26 tnn val = SUNXI_GPIO_INT_DEBOUNCE_CLK_SEL;
513 1.26 tnn GPIO_WRITE(sc, SUNXI_GPIO_INT_DEBOUNCE(eint->eint_bank), val);
514 1.26 tnn
515 1.12 jmcneill /* Enable eint */
516 1.15 jmcneill val = GPIO_READ(sc, SUNXI_GPIO_INT_CTL(eint->eint_bank));
517 1.12 jmcneill val |= __BIT(eint->eint_num);
518 1.15 jmcneill GPIO_WRITE(sc, SUNXI_GPIO_INT_CTL(eint->eint_bank), val);
519 1.12 jmcneill
520 1.12 jmcneill mutex_exit(&sc->sc_lock);
521 1.12 jmcneill
522 1.12 jmcneill return eint;
523 1.12 jmcneill }
524 1.12 jmcneill
525 1.12 jmcneill static void
526 1.25 tnn sunxi_intr_disable(struct sunxi_gpio_softc *sc, struct sunxi_gpio_eint *eint)
527 1.12 jmcneill {
528 1.12 jmcneill uint32_t val;
529 1.12 jmcneill
530 1.12 jmcneill KASSERT(eint->eint_func != NULL);
531 1.12 jmcneill
532 1.12 jmcneill mutex_enter(&sc->sc_lock);
533 1.12 jmcneill
534 1.12 jmcneill /* Disable eint */
535 1.15 jmcneill val = GPIO_READ(sc, SUNXI_GPIO_INT_CTL(eint->eint_bank));
536 1.12 jmcneill val &= ~__BIT(eint->eint_num);
537 1.15 jmcneill GPIO_WRITE(sc, SUNXI_GPIO_INT_CTL(eint->eint_bank), val);
538 1.15 jmcneill GPIO_WRITE(sc, SUNXI_GPIO_INT_STATUS(eint->eint_bank), __BIT(eint->eint_num));
539 1.12 jmcneill
540 1.12 jmcneill eint->eint_func = NULL;
541 1.12 jmcneill eint->eint_arg = NULL;
542 1.25 tnn eint->eint_mpsafe = false;
543 1.12 jmcneill
544 1.12 jmcneill mutex_exit(&sc->sc_lock);
545 1.12 jmcneill }
546 1.12 jmcneill
547 1.25 tnn static void *
548 1.25 tnn sunxi_fdt_intr_establish(device_t dev, u_int *specifier, int ipl, int flags,
549 1.29 jmcneill int (*func)(void *), void *arg, const char *xname)
550 1.25 tnn {
551 1.25 tnn struct sunxi_gpio_softc * const sc = device_private(dev);
552 1.25 tnn bool mpsafe = (flags & FDT_INTR_MPSAFE) != 0;
553 1.25 tnn const struct sunxi_gpio_pins *pin_def;
554 1.25 tnn u_int mode;
555 1.25 tnn
556 1.25 tnn if (ipl != IPL_VM) {
557 1.25 tnn aprint_error_dev(dev, "%s: wrong IPL %d (expected %d)\n",
558 1.25 tnn __func__, ipl, IPL_VM);
559 1.25 tnn return NULL;
560 1.25 tnn }
561 1.25 tnn
562 1.25 tnn /* 1st cell is the bank */
563 1.25 tnn /* 2nd cell is the pin */
564 1.25 tnn /* 3rd cell is flags */
565 1.25 tnn const u_int port = be32toh(specifier[0]);
566 1.25 tnn const u_int pin = be32toh(specifier[1]);
567 1.25 tnn const u_int type = be32toh(specifier[2]) & 0xf;
568 1.25 tnn
569 1.25 tnn switch (type) {
570 1.25 tnn case FDT_INTR_TYPE_POS_EDGE:
571 1.25 tnn mode = SUNXI_GPIO_INT_MODE_POS_EDGE;
572 1.25 tnn break;
573 1.25 tnn case FDT_INTR_TYPE_NEG_EDGE:
574 1.25 tnn mode = SUNXI_GPIO_INT_MODE_NEG_EDGE;
575 1.25 tnn break;
576 1.25 tnn case FDT_INTR_TYPE_DOUBLE_EDGE:
577 1.25 tnn mode = SUNXI_GPIO_INT_MODE_DOUBLE_EDGE;
578 1.25 tnn break;
579 1.25 tnn case FDT_INTR_TYPE_HIGH_LEVEL:
580 1.25 tnn mode = SUNXI_GPIO_INT_MODE_HIGH_LEVEL;
581 1.25 tnn break;
582 1.25 tnn case FDT_INTR_TYPE_LOW_LEVEL:
583 1.25 tnn mode = SUNXI_GPIO_INT_MODE_LOW_LEVEL;
584 1.25 tnn break;
585 1.25 tnn default:
586 1.25 tnn aprint_error_dev(dev, "%s: unsupported irq type 0x%x\n",
587 1.25 tnn __func__, type);
588 1.25 tnn return NULL;
589 1.25 tnn }
590 1.25 tnn
591 1.25 tnn pin_def = sunxi_gpio_lookup(sc, port, pin);
592 1.25 tnn if (pin_def == NULL)
593 1.25 tnn return NULL;
594 1.25 tnn
595 1.25 tnn return sunxi_intr_enable(sc, pin_def, mode, mpsafe, func, arg);
596 1.25 tnn }
597 1.25 tnn
598 1.25 tnn static void
599 1.25 tnn sunxi_fdt_intr_disestablish(device_t dev, void *ih)
600 1.25 tnn {
601 1.25 tnn struct sunxi_gpio_softc * const sc = device_private(dev);
602 1.25 tnn struct sunxi_gpio_eint * const eint = ih;
603 1.25 tnn
604 1.25 tnn sunxi_intr_disable(sc, eint);
605 1.25 tnn }
606 1.25 tnn
607 1.12 jmcneill static bool
608 1.25 tnn sunxi_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
609 1.12 jmcneill {
610 1.12 jmcneill struct sunxi_gpio_softc * const sc = device_private(dev);
611 1.12 jmcneill const struct sunxi_gpio_pins *pin_def;
612 1.12 jmcneill
613 1.12 jmcneill /* 1st cell is the bank */
614 1.12 jmcneill /* 2nd cell is the pin */
615 1.12 jmcneill /* 3rd cell is flags */
616 1.12 jmcneill if (!specifier)
617 1.12 jmcneill return false;
618 1.12 jmcneill const u_int port = be32toh(specifier[0]);
619 1.12 jmcneill const u_int pin = be32toh(specifier[1]);
620 1.12 jmcneill
621 1.12 jmcneill pin_def = sunxi_gpio_lookup(sc, port, pin);
622 1.12 jmcneill if (pin_def == NULL)
623 1.12 jmcneill return false;
624 1.12 jmcneill
625 1.12 jmcneill snprintf(buf, buflen, "GPIO %s", pin_def->name);
626 1.12 jmcneill
627 1.12 jmcneill return true;
628 1.12 jmcneill }
629 1.12 jmcneill
630 1.12 jmcneill static struct fdtbus_interrupt_controller_func sunxi_gpio_intrfuncs = {
631 1.25 tnn .establish = sunxi_fdt_intr_establish,
632 1.25 tnn .disestablish = sunxi_fdt_intr_disestablish,
633 1.25 tnn .intrstr = sunxi_fdt_intrstr,
634 1.12 jmcneill };
635 1.12 jmcneill
636 1.25 tnn static void *
637 1.25 tnn sunxi_gpio_intr_establish(void *vsc, int pin, int ipl, int irqmode,
638 1.25 tnn int (*func)(void *), void *arg)
639 1.25 tnn {
640 1.25 tnn struct sunxi_gpio_softc * const sc = vsc;
641 1.25 tnn bool mpsafe = (irqmode & GPIO_INTR_MPSAFE) != 0;
642 1.25 tnn int type = irqmode & GPIO_INTR_MODE_MASK;
643 1.25 tnn const struct sunxi_gpio_pins *pin_def;
644 1.25 tnn u_int mode;
645 1.25 tnn
646 1.25 tnn switch (type) {
647 1.25 tnn case GPIO_INTR_POS_EDGE:
648 1.25 tnn mode = SUNXI_GPIO_INT_MODE_POS_EDGE;
649 1.25 tnn break;
650 1.25 tnn case GPIO_INTR_NEG_EDGE:
651 1.25 tnn mode = SUNXI_GPIO_INT_MODE_NEG_EDGE;
652 1.25 tnn break;
653 1.25 tnn case GPIO_INTR_DOUBLE_EDGE:
654 1.25 tnn mode = SUNXI_GPIO_INT_MODE_DOUBLE_EDGE;
655 1.25 tnn break;
656 1.25 tnn case GPIO_INTR_HIGH_LEVEL:
657 1.25 tnn mode = SUNXI_GPIO_INT_MODE_HIGH_LEVEL;
658 1.25 tnn break;
659 1.25 tnn case GPIO_INTR_LOW_LEVEL:
660 1.25 tnn mode = SUNXI_GPIO_INT_MODE_LOW_LEVEL;
661 1.25 tnn break;
662 1.25 tnn default:
663 1.25 tnn aprint_error_dev(sc->sc_dev, "%s: unsupported irq type 0x%x\n",
664 1.25 tnn __func__, type);
665 1.25 tnn return NULL;
666 1.25 tnn }
667 1.25 tnn
668 1.25 tnn if (pin < 0 || pin >= sc->sc_padconf->npins)
669 1.25 tnn return NULL;
670 1.25 tnn pin_def = &sc->sc_padconf->pins[pin];
671 1.25 tnn
672 1.25 tnn return sunxi_intr_enable(sc, pin_def, mode, mpsafe, func, arg);
673 1.25 tnn }
674 1.25 tnn
675 1.25 tnn static void
676 1.25 tnn sunxi_gpio_intr_disestablish(void *vsc, void *ih)
677 1.25 tnn {
678 1.25 tnn struct sunxi_gpio_softc * const sc = vsc;
679 1.25 tnn struct sunxi_gpio_eint * const eint = ih;
680 1.25 tnn
681 1.25 tnn sunxi_intr_disable(sc, eint);
682 1.25 tnn }
683 1.25 tnn
684 1.25 tnn static bool
685 1.25 tnn sunxi_gpio_intrstr(void *vsc, int pin, int irqmode, char *buf, size_t buflen)
686 1.25 tnn {
687 1.25 tnn struct sunxi_gpio_softc * const sc = vsc;
688 1.25 tnn const struct sunxi_gpio_pins *pin_def;
689 1.25 tnn
690 1.25 tnn if (pin < 0 || pin >= sc->sc_padconf->npins)
691 1.25 tnn return NULL;
692 1.25 tnn pin_def = &sc->sc_padconf->pins[pin];
693 1.25 tnn
694 1.25 tnn snprintf(buf, buflen, "GPIO %s", pin_def->name);
695 1.25 tnn
696 1.25 tnn return true;
697 1.25 tnn }
698 1.25 tnn
699 1.10 jmcneill static const char *
700 1.10 jmcneill sunxi_pinctrl_parse_function(int phandle)
701 1.10 jmcneill {
702 1.10 jmcneill const char *function;
703 1.10 jmcneill
704 1.22 thorpej function = fdtbus_pinctrl_parse_function(phandle);
705 1.10 jmcneill if (function != NULL)
706 1.10 jmcneill return function;
707 1.10 jmcneill
708 1.10 jmcneill return fdtbus_get_string(phandle, "allwinner,function");
709 1.10 jmcneill }
710 1.10 jmcneill
711 1.10 jmcneill static const char *
712 1.10 jmcneill sunxi_pinctrl_parse_pins(int phandle, int *pins_len)
713 1.10 jmcneill {
714 1.22 thorpej const char *pins;
715 1.10 jmcneill int len;
716 1.10 jmcneill
717 1.22 thorpej pins = fdtbus_pinctrl_parse_pins(phandle, pins_len);
718 1.22 thorpej if (pins != NULL)
719 1.22 thorpej return pins;
720 1.10 jmcneill
721 1.10 jmcneill len = OF_getproplen(phandle, "allwinner,pins");
722 1.10 jmcneill if (len > 0) {
723 1.10 jmcneill *pins_len = len;
724 1.24 jmcneill return fdtbus_get_prop(phandle, "allwinner,pins", pins_len);
725 1.10 jmcneill }
726 1.10 jmcneill
727 1.10 jmcneill return NULL;
728 1.10 jmcneill }
729 1.10 jmcneill
730 1.10 jmcneill static int
731 1.10 jmcneill sunxi_pinctrl_parse_bias(int phandle)
732 1.10 jmcneill {
733 1.10 jmcneill u_int pull;
734 1.22 thorpej int bias;
735 1.22 thorpej
736 1.22 thorpej bias = fdtbus_pinctrl_parse_bias(phandle, NULL);
737 1.22 thorpej if (bias != -1)
738 1.22 thorpej return bias;
739 1.10 jmcneill
740 1.22 thorpej if (of_getprop_uint32(phandle, "allwinner,pull", &pull) == 0) {
741 1.10 jmcneill switch (pull) {
742 1.10 jmcneill case 0:
743 1.10 jmcneill bias = 0;
744 1.10 jmcneill break;
745 1.10 jmcneill case 1:
746 1.10 jmcneill bias = GPIO_PIN_PULLUP;
747 1.10 jmcneill break;
748 1.10 jmcneill case 2:
749 1.10 jmcneill bias = GPIO_PIN_PULLDOWN;
750 1.10 jmcneill break;
751 1.10 jmcneill }
752 1.10 jmcneill }
753 1.10 jmcneill
754 1.10 jmcneill return bias;
755 1.10 jmcneill }
756 1.10 jmcneill
757 1.10 jmcneill static int
758 1.10 jmcneill sunxi_pinctrl_parse_drive_strength(int phandle)
759 1.10 jmcneill {
760 1.10 jmcneill int val;
761 1.10 jmcneill
762 1.22 thorpej val = fdtbus_pinctrl_parse_drive_strength(phandle);
763 1.22 thorpej if (val != -1)
764 1.10 jmcneill return val;
765 1.10 jmcneill
766 1.10 jmcneill if (of_getprop_uint32(phandle, "allwinner,drive", &val) == 0)
767 1.10 jmcneill return (val + 1) * 10;
768 1.10 jmcneill
769 1.10 jmcneill return -1;
770 1.10 jmcneill }
771 1.10 jmcneill
772 1.24 jmcneill static void
773 1.24 jmcneill sunxi_pinctrl_enable_regulator(struct sunxi_gpio_softc *sc,
774 1.24 jmcneill const struct sunxi_gpio_pins *pin_def)
775 1.24 jmcneill {
776 1.24 jmcneill char supply_prop[16];
777 1.24 jmcneill uint32_t val;
778 1.24 jmcneill u_int uvol;
779 1.24 jmcneill int error;
780 1.24 jmcneill
781 1.24 jmcneill const char c = tolower(pin_def->name[1]);
782 1.24 jmcneill if (c < 'a' || c > 'z')
783 1.24 jmcneill return;
784 1.24 jmcneill const int index = c - 'a';
785 1.24 jmcneill
786 1.24 jmcneill if (sc->sc_pin_supply[index] != NULL) {
787 1.24 jmcneill /* Already enabled */
788 1.24 jmcneill return;
789 1.24 jmcneill }
790 1.24 jmcneill
791 1.24 jmcneill snprintf(supply_prop, sizeof(supply_prop), "vcc-p%c-supply", c);
792 1.24 jmcneill sc->sc_pin_supply[index] = fdtbus_regulator_acquire(sc->sc_phandle, supply_prop);
793 1.24 jmcneill if (sc->sc_pin_supply[index] == NULL)
794 1.24 jmcneill return;
795 1.24 jmcneill
796 1.24 jmcneill aprint_debug_dev(sc->sc_dev, "enable \"%s\"\n", supply_prop);
797 1.24 jmcneill error = fdtbus_regulator_enable(sc->sc_pin_supply[index]);
798 1.24 jmcneill if (error != 0)
799 1.24 jmcneill aprint_error_dev(sc->sc_dev, "failed to enable %s: %d\n", supply_prop, error);
800 1.24 jmcneill
801 1.24 jmcneill if (sc->sc_padconf->has_io_bias_config) {
802 1.24 jmcneill error = fdtbus_regulator_get_voltage(sc->sc_pin_supply[index], &uvol);
803 1.24 jmcneill if (error != 0) {
804 1.24 jmcneill aprint_error_dev(sc->sc_dev, "failed to get %s voltage: %d\n",
805 1.24 jmcneill supply_prop, error);
806 1.24 jmcneill uvol = 0;
807 1.24 jmcneill }
808 1.24 jmcneill if (uvol != 0) {
809 1.24 jmcneill if (uvol <= 1800000)
810 1.24 jmcneill val = 0x0; /* 1.8V */
811 1.24 jmcneill else if (uvol <= 2500000)
812 1.24 jmcneill val = 0x6; /* 2.5V */
813 1.24 jmcneill else if (uvol <= 2800000)
814 1.24 jmcneill val = 0x9; /* 2.8V */
815 1.24 jmcneill else if (uvol <= 3000000)
816 1.24 jmcneill val = 0xa; /* 3.0V */
817 1.24 jmcneill else
818 1.24 jmcneill val = 0xd; /* 3.3V */
819 1.24 jmcneill
820 1.24 jmcneill aprint_debug_dev(sc->sc_dev, "set io bias config for port %d to 0x%x\n",
821 1.24 jmcneill pin_def->port, val);
822 1.24 jmcneill val = GPIO_READ(sc, SUNXI_GPIO_GRP_CONFIG(pin_def->port));
823 1.24 jmcneill val &= ~SUNXI_GPIO_GRP_IO_BIAS_CONFIGMASK;
824 1.24 jmcneill val |= __SHIFTIN(val, SUNXI_GPIO_GRP_IO_BIAS_CONFIGMASK);
825 1.24 jmcneill GPIO_WRITE(sc, SUNXI_GPIO_GRP_CONFIG(pin_def->port), val);
826 1.24 jmcneill }
827 1.24 jmcneill }
828 1.24 jmcneill }
829 1.24 jmcneill
830 1.1 jmcneill static int
831 1.2 jmcneill sunxi_pinctrl_set_config(device_t dev, const void *data, size_t len)
832 1.2 jmcneill {
833 1.2 jmcneill struct sunxi_gpio_softc * const sc = device_private(dev);
834 1.2 jmcneill const struct sunxi_gpio_pins *pin_def;
835 1.10 jmcneill int pins_len;
836 1.2 jmcneill
837 1.2 jmcneill if (len != 4)
838 1.2 jmcneill return -1;
839 1.2 jmcneill
840 1.2 jmcneill const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
841 1.2 jmcneill
842 1.2 jmcneill /*
843 1.2 jmcneill * Required: pins, function
844 1.10 jmcneill * Optional: bias, drive strength
845 1.2 jmcneill */
846 1.2 jmcneill
847 1.10 jmcneill const char *function = sunxi_pinctrl_parse_function(phandle);
848 1.2 jmcneill if (function == NULL)
849 1.2 jmcneill return -1;
850 1.10 jmcneill const char *pins = sunxi_pinctrl_parse_pins(phandle, &pins_len);
851 1.10 jmcneill if (pins == NULL)
852 1.2 jmcneill return -1;
853 1.10 jmcneill
854 1.10 jmcneill const int bias = sunxi_pinctrl_parse_bias(phandle);
855 1.10 jmcneill const int drive_strength = sunxi_pinctrl_parse_drive_strength(phandle);
856 1.2 jmcneill
857 1.5 jmcneill mutex_enter(&sc->sc_lock);
858 1.5 jmcneill
859 1.10 jmcneill for (; pins_len > 0;
860 1.10 jmcneill pins_len -= strlen(pins) + 1, pins += strlen(pins) + 1) {
861 1.2 jmcneill pin_def = sunxi_gpio_lookup_byname(sc, pins);
862 1.2 jmcneill if (pin_def == NULL) {
863 1.2 jmcneill aprint_error_dev(dev, "unknown pin name '%s'\n", pins);
864 1.2 jmcneill continue;
865 1.2 jmcneill }
866 1.2 jmcneill if (sunxi_gpio_setfunc(sc, pin_def, function) != 0)
867 1.2 jmcneill continue;
868 1.2 jmcneill
869 1.10 jmcneill if (bias != -1)
870 1.10 jmcneill sunxi_gpio_setpull(sc, pin_def, bias);
871 1.2 jmcneill
872 1.10 jmcneill if (drive_strength != -1)
873 1.2 jmcneill sunxi_gpio_setdrv(sc, pin_def, drive_strength);
874 1.24 jmcneill
875 1.24 jmcneill sunxi_pinctrl_enable_regulator(sc, pin_def);
876 1.2 jmcneill }
877 1.2 jmcneill
878 1.5 jmcneill mutex_exit(&sc->sc_lock);
879 1.5 jmcneill
880 1.2 jmcneill return 0;
881 1.2 jmcneill }
882 1.2 jmcneill
883 1.2 jmcneill static struct fdtbus_pinctrl_controller_func sunxi_pinctrl_funcs = {
884 1.2 jmcneill .set_config = sunxi_pinctrl_set_config,
885 1.2 jmcneill };
886 1.2 jmcneill
887 1.2 jmcneill static int
888 1.5 jmcneill sunxi_gpio_pin_read(void *priv, int pin)
889 1.5 jmcneill {
890 1.5 jmcneill struct sunxi_gpio_softc * const sc = priv;
891 1.5 jmcneill const struct sunxi_gpio_pins *pin_def = &sc->sc_padconf->pins[pin];
892 1.5 jmcneill uint32_t data;
893 1.5 jmcneill int val;
894 1.5 jmcneill
895 1.5 jmcneill KASSERT(pin < sc->sc_padconf->npins);
896 1.5 jmcneill
897 1.5 jmcneill const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
898 1.5 jmcneill const uint32_t data_mask = __BIT(pin_def->pin);
899 1.5 jmcneill
900 1.5 jmcneill /* No lock required for reads */
901 1.5 jmcneill data = GPIO_READ(sc, data_reg);
902 1.5 jmcneill val = __SHIFTOUT(data, data_mask);
903 1.5 jmcneill
904 1.5 jmcneill return val;
905 1.5 jmcneill }
906 1.5 jmcneill
907 1.5 jmcneill static void
908 1.5 jmcneill sunxi_gpio_pin_write(void *priv, int pin, int val)
909 1.5 jmcneill {
910 1.5 jmcneill struct sunxi_gpio_softc * const sc = priv;
911 1.5 jmcneill const struct sunxi_gpio_pins *pin_def = &sc->sc_padconf->pins[pin];
912 1.5 jmcneill uint32_t data;
913 1.5 jmcneill
914 1.5 jmcneill KASSERT(pin < sc->sc_padconf->npins);
915 1.5 jmcneill
916 1.5 jmcneill const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
917 1.5 jmcneill const uint32_t data_mask = __BIT(pin_def->pin);
918 1.5 jmcneill
919 1.5 jmcneill mutex_enter(&sc->sc_lock);
920 1.5 jmcneill data = GPIO_READ(sc, data_reg);
921 1.5 jmcneill if (val)
922 1.5 jmcneill data |= data_mask;
923 1.5 jmcneill else
924 1.5 jmcneill data &= ~data_mask;
925 1.5 jmcneill GPIO_WRITE(sc, data_reg, data);
926 1.5 jmcneill mutex_exit(&sc->sc_lock);
927 1.5 jmcneill }
928 1.5 jmcneill
929 1.5 jmcneill static void
930 1.5 jmcneill sunxi_gpio_pin_ctl(void *priv, int pin, int flags)
931 1.5 jmcneill {
932 1.5 jmcneill struct sunxi_gpio_softc * const sc = priv;
933 1.5 jmcneill const struct sunxi_gpio_pins *pin_def = &sc->sc_padconf->pins[pin];
934 1.5 jmcneill
935 1.5 jmcneill KASSERT(pin < sc->sc_padconf->npins);
936 1.5 jmcneill
937 1.5 jmcneill mutex_enter(&sc->sc_lock);
938 1.5 jmcneill sunxi_gpio_ctl(sc, pin_def, flags);
939 1.5 jmcneill sunxi_gpio_setpull(sc, pin_def, flags);
940 1.5 jmcneill mutex_exit(&sc->sc_lock);
941 1.5 jmcneill }
942 1.5 jmcneill
943 1.5 jmcneill static void
944 1.5 jmcneill sunxi_gpio_attach_ports(struct sunxi_gpio_softc *sc)
945 1.5 jmcneill {
946 1.5 jmcneill const struct sunxi_gpio_pins *pin_def;
947 1.5 jmcneill struct gpio_chipset_tag *gp = &sc->sc_gp;
948 1.5 jmcneill struct gpiobus_attach_args gba;
949 1.5 jmcneill u_int pin;
950 1.5 jmcneill
951 1.5 jmcneill gp->gp_cookie = sc;
952 1.5 jmcneill gp->gp_pin_read = sunxi_gpio_pin_read;
953 1.5 jmcneill gp->gp_pin_write = sunxi_gpio_pin_write;
954 1.5 jmcneill gp->gp_pin_ctl = sunxi_gpio_pin_ctl;
955 1.25 tnn gp->gp_intr_establish = sunxi_gpio_intr_establish;
956 1.25 tnn gp->gp_intr_disestablish = sunxi_gpio_intr_disestablish;
957 1.25 tnn gp->gp_intr_str = sunxi_gpio_intrstr;
958 1.5 jmcneill
959 1.5 jmcneill const u_int npins = sc->sc_padconf->npins;
960 1.5 jmcneill sc->sc_pins = kmem_zalloc(sizeof(*sc->sc_pins) * npins, KM_SLEEP);
961 1.5 jmcneill
962 1.5 jmcneill for (pin = 0; pin < sc->sc_padconf->npins; pin++) {
963 1.5 jmcneill pin_def = &sc->sc_padconf->pins[pin];
964 1.5 jmcneill sc->sc_pins[pin].pin_num = pin;
965 1.5 jmcneill sc->sc_pins[pin].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
966 1.5 jmcneill GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN;
967 1.25 tnn if (pin_def->functions[pin_def->eint_func] != NULL &&
968 1.25 tnn strcmp(pin_def->functions[pin_def->eint_func], "irq") == 0) {
969 1.25 tnn sc->sc_pins[pin].pin_intrcaps =
970 1.25 tnn GPIO_INTR_POS_EDGE | GPIO_INTR_NEG_EDGE |
971 1.25 tnn GPIO_INTR_HIGH_LEVEL | GPIO_INTR_LOW_LEVEL |
972 1.25 tnn GPIO_INTR_DOUBLE_EDGE | GPIO_INTR_MPSAFE;
973 1.25 tnn }
974 1.5 jmcneill sc->sc_pins[pin].pin_state = sunxi_gpio_pin_read(sc, pin);
975 1.5 jmcneill strlcpy(sc->sc_pins[pin].pin_defname, pin_def->name,
976 1.5 jmcneill sizeof(sc->sc_pins[pin].pin_defname));
977 1.5 jmcneill }
978 1.5 jmcneill
979 1.5 jmcneill memset(&gba, 0, sizeof(gba));
980 1.5 jmcneill gba.gba_gc = gp;
981 1.5 jmcneill gba.gba_pins = sc->sc_pins;
982 1.5 jmcneill gba.gba_npins = npins;
983 1.37 thorpej sc->sc_gpiodev = config_found(sc->sc_dev, &gba, NULL, CFARGS_NONE);
984 1.5 jmcneill }
985 1.5 jmcneill
986 1.5 jmcneill static int
987 1.1 jmcneill sunxi_gpio_match(device_t parent, cfdata_t cf, void *aux)
988 1.1 jmcneill {
989 1.1 jmcneill struct fdt_attach_args * const faa = aux;
990 1.1 jmcneill
991 1.35 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
992 1.1 jmcneill }
993 1.1 jmcneill
994 1.1 jmcneill static void
995 1.1 jmcneill sunxi_gpio_attach(device_t parent, device_t self, void *aux)
996 1.1 jmcneill {
997 1.1 jmcneill struct sunxi_gpio_softc * const sc = device_private(self);
998 1.1 jmcneill struct fdt_attach_args * const faa = aux;
999 1.1 jmcneill const int phandle = faa->faa_phandle;
1000 1.12 jmcneill char intrstr[128];
1001 1.8 jmcneill struct fdtbus_reset *rst;
1002 1.8 jmcneill struct clk *clk;
1003 1.1 jmcneill bus_addr_t addr;
1004 1.1 jmcneill bus_size_t size;
1005 1.2 jmcneill int child;
1006 1.1 jmcneill
1007 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1008 1.1 jmcneill aprint_error(": couldn't get registers\n");
1009 1.1 jmcneill return;
1010 1.1 jmcneill }
1011 1.1 jmcneill
1012 1.8 jmcneill if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL)
1013 1.8 jmcneill if (clk_enable(clk) != 0) {
1014 1.8 jmcneill aprint_error(": couldn't enable clock\n");
1015 1.8 jmcneill return;
1016 1.8 jmcneill }
1017 1.8 jmcneill
1018 1.8 jmcneill if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL)
1019 1.8 jmcneill if (fdtbus_reset_deassert(rst) != 0) {
1020 1.8 jmcneill aprint_error(": couldn't de-assert reset\n");
1021 1.8 jmcneill return;
1022 1.8 jmcneill }
1023 1.8 jmcneill
1024 1.1 jmcneill sc->sc_dev = self;
1025 1.24 jmcneill sc->sc_phandle = phandle;
1026 1.1 jmcneill sc->sc_bst = faa->faa_bst;
1027 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
1028 1.1 jmcneill aprint_error(": couldn't map registers\n");
1029 1.1 jmcneill return;
1030 1.1 jmcneill }
1031 1.5 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
1032 1.35 thorpej sc->sc_padconf = of_compatible_lookup(phandle, compat_data)->data;
1033 1.1 jmcneill
1034 1.1 jmcneill aprint_naive("\n");
1035 1.1 jmcneill aprint_normal(": PIO\n");
1036 1.1 jmcneill
1037 1.1 jmcneill fdtbus_register_gpio_controller(self, phandle, &sunxi_gpio_funcs);
1038 1.2 jmcneill
1039 1.2 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
1040 1.24 jmcneill bool is_valid =
1041 1.24 jmcneill (of_hasprop(child, "function") && of_hasprop(child, "pins")) ||
1042 1.24 jmcneill (of_hasprop(child, "allwinner,function") && of_hasprop(child, "allwinner,pins"));
1043 1.24 jmcneill if (!is_valid)
1044 1.2 jmcneill continue;
1045 1.2 jmcneill fdtbus_register_pinctrl_config(self, child, &sunxi_pinctrl_funcs);
1046 1.2 jmcneill }
1047 1.2 jmcneill
1048 1.5 jmcneill sunxi_gpio_attach_ports(sc);
1049 1.12 jmcneill
1050 1.12 jmcneill /* Disable all external interrupts */
1051 1.15 jmcneill for (int i = 0; i < sc->sc_padconf->npins; i++) {
1052 1.15 jmcneill const struct sunxi_gpio_pins *pin_def = &sc->sc_padconf->pins[i];
1053 1.15 jmcneill if (pin_def->eint_func == 0)
1054 1.15 jmcneill continue;
1055 1.15 jmcneill GPIO_WRITE(sc, SUNXI_GPIO_INT_CTL(pin_def->eint_bank), __BIT(pin_def->eint_num));
1056 1.15 jmcneill GPIO_WRITE(sc, SUNXI_GPIO_INT_STATUS(pin_def->eint_bank), __BIT(pin_def->eint_num));
1057 1.15 jmcneill
1058 1.15 jmcneill if (sc->sc_eint_bank_max < pin_def->eint_bank)
1059 1.15 jmcneill sc->sc_eint_bank_max = pin_def->eint_bank;
1060 1.15 jmcneill }
1061 1.15 jmcneill KASSERT(sc->sc_eint_bank_max < SUNXI_GPIO_MAX_EINT_BANK);
1062 1.12 jmcneill
1063 1.12 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
1064 1.12 jmcneill aprint_error_dev(self, "failed to decode interrupt\n");
1065 1.12 jmcneill return;
1066 1.12 jmcneill }
1067 1.30 jmcneill sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM,
1068 1.30 jmcneill FDT_INTR_MPSAFE, sunxi_gpio_intr, sc, device_xname(self));
1069 1.12 jmcneill if (sc->sc_ih == NULL) {
1070 1.12 jmcneill aprint_error_dev(self, "failed to establish interrupt on %s\n",
1071 1.12 jmcneill intrstr);
1072 1.12 jmcneill return;
1073 1.12 jmcneill }
1074 1.12 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1075 1.12 jmcneill fdtbus_register_interrupt_controller(self, phandle,
1076 1.12 jmcneill &sunxi_gpio_intrfuncs);
1077 1.1 jmcneill }
1078