sunxi_gpio.c revision 1.3 1 /* $NetBSD: sunxi_gpio.c,v 1.3 2017/07/02 18:19:26 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2017 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 "opt_soc.h"
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: sunxi_gpio.c,v 1.3 2017/07/02 18:19:26 jmcneill Exp $");
33
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/device.h>
37 #include <sys/intr.h>
38 #include <sys/systm.h>
39 #include <sys/mutex.h>
40 #include <sys/kmem.h>
41 #include <sys/gpio.h>
42
43 #include <dev/fdt/fdtvar.h>
44
45 #include <arm/sunxi/sunxi_gpio.h>
46
47 #define SUNXI_GPIO_PORT(port) (0x20 * (port))
48 #define SUNXI_GPIO_CFG(port, pin) (SUNXI_GPIO_PORT(port) + (0x4 * ((pin) / 8)))
49 #define SUNXI_GPIO_CFG_PINMASK(pin) (0x7 << (((pin) % 8) * 4))
50 #define SUNXI_GPIO_DATA(port) (SUNXI_GPIO_PORT(port) + 0x10)
51 #define SUNXI_GPIO_DRV(port, pin) (SUNXI_GPIO_PORT(port) + 0x14 + (0x4 * ((pin) / 16)))
52 #define SUNXI_GPIO_DRV_PINMASK(pin) (0x3 << (((pin) % 16) * 4))
53 #define SUNXI_GPIO_PULL(port, pin) (SUNXI_GPIO_PORT(port) + 0x1c + (0x4 * ((pin) / 16)))
54 #define SUNXI_GPIO_PULL_DISABLE 0
55 #define SUNXI_GPIO_PULL_UP 1
56 #define SUNXI_GPIO_PULL_DOWN 2
57 #define SUNXI_GPIO_PULL_PINMASK(pin) (0x3 << (((pin) % 16) * 4))
58
59 static const struct of_compat_data compat_data[] = {
60 #ifdef SOC_SUN6I_A31
61 { "allwinner,sun6i-a31-pinctrl", (uintptr_t)&sun6i_a31_padconf },
62 { "allwinner,sun6i-a31-r-pinctrl", (uintptr_t)&sun6i_a31_r_padconf },
63 #endif
64 #ifdef SOC_SUN8I_H3
65 { "allwinner,sun8i-h3-pinctrl", (uintptr_t)&sun8i_h3_padconf },
66 { "allwinner,sun8i-h3-r-pinctrl", (uintptr_t)&sun8i_h3_r_padconf },
67 #endif
68 { NULL }
69 };
70
71 struct sunxi_gpio_softc {
72 device_t sc_dev;
73 bus_space_tag_t sc_bst;
74 bus_space_handle_t sc_bsh;
75 const struct sunxi_gpio_padconf *sc_padconf;
76 };
77
78 struct sunxi_gpio_pin {
79 struct sunxi_gpio_softc *pin_sc;
80 const struct sunxi_gpio_pins *pin_def;
81 int pin_flags;
82 bool pin_actlo;
83 };
84
85 #define GPIO_READ(sc, reg) \
86 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
87 #define GPIO_WRITE(sc, reg, val) \
88 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
89
90 static int sunxi_gpio_match(device_t, cfdata_t, void *);
91 static void sunxi_gpio_attach(device_t, device_t, void *);
92
93 CFATTACH_DECL_NEW(sunxi_gpio, sizeof(struct sunxi_gpio_softc),
94 sunxi_gpio_match, sunxi_gpio_attach, NULL, NULL);
95
96 static const struct sunxi_gpio_pins *
97 sunxi_gpio_lookup(struct sunxi_gpio_softc *sc, uint8_t port, uint8_t pin)
98 {
99 const struct sunxi_gpio_pins *pin_def;
100 u_int n;
101
102 for (n = 0; n < sc->sc_padconf->npins; n++) {
103 pin_def = &sc->sc_padconf->pins[n];
104 if (pin_def->port == port && pin_def->pin == pin)
105 return pin_def;
106 }
107
108 return NULL;
109 }
110
111 static const struct sunxi_gpio_pins *
112 sunxi_gpio_lookup_byname(struct sunxi_gpio_softc *sc, const char *name)
113 {
114 const struct sunxi_gpio_pins *pin_def;
115 u_int n;
116
117 for (n = 0; n < sc->sc_padconf->npins; n++) {
118 pin_def = &sc->sc_padconf->pins[n];
119 if (strcmp(pin_def->name, name) == 0)
120 return pin_def;
121 }
122
123 return NULL;
124 }
125
126 static int
127 sunxi_gpio_setfunc(struct sunxi_gpio_softc *sc,
128 const struct sunxi_gpio_pins *pin_def, const char *func)
129 {
130 uint32_t cfg;
131 u_int n;
132
133 const bus_size_t cfg_reg = SUNXI_GPIO_CFG(pin_def->port, pin_def->pin);
134 const uint32_t cfg_mask = SUNXI_GPIO_CFG_PINMASK(pin_def->pin);
135
136 for (n = 0; n < SUNXI_GPIO_MAXFUNC; n++) {
137 if (pin_def->functions[n] == NULL)
138 continue;
139 if (strcmp(pin_def->functions[n], func) == 0) {
140 cfg = GPIO_READ(sc, cfg_reg);
141 cfg &= ~cfg_mask;
142 cfg |= __SHIFTIN(n, cfg_mask);
143 GPIO_WRITE(sc, cfg_reg, cfg);
144 return 0;
145 }
146 }
147
148 /* Function not found */
149 device_printf(sc->sc_dev, "function '%s' not supported on P%c%02d\n",
150 func, pin_def->port + 'A', pin_def->pin);
151
152 return ENXIO;
153 }
154
155 static int
156 sunxi_gpio_setpull(struct sunxi_gpio_softc *sc,
157 const struct sunxi_gpio_pins *pin_def, int flags)
158 {
159 uint32_t pull;
160
161 const bus_size_t pull_reg = SUNXI_GPIO_PULL(pin_def->port, pin_def->pin);
162 const uint32_t pull_mask = SUNXI_GPIO_PULL_PINMASK(pin_def->pin);
163
164 pull = GPIO_READ(sc, pull_reg);
165 pull &= ~pull_mask;
166 if (flags & GPIO_PIN_PULLUP)
167 pull |= __SHIFTIN(SUNXI_GPIO_PULL_UP, pull_mask);
168 else if (flags & GPIO_PIN_PULLDOWN)
169 pull |= __SHIFTIN(SUNXI_GPIO_PULL_DOWN, pull_mask);
170 else
171 pull |= __SHIFTIN(SUNXI_GPIO_PULL_DISABLE, pull_mask);
172 GPIO_WRITE(sc, pull_reg, pull);
173
174 return 0;
175 }
176
177 static int
178 sunxi_gpio_setdrv(struct sunxi_gpio_softc *sc,
179 const struct sunxi_gpio_pins *pin_def, int drive_strength)
180 {
181 uint32_t drv;
182
183 if (drive_strength < 10 || drive_strength > 40)
184 return EINVAL;
185
186 const bus_size_t drv_reg = SUNXI_GPIO_DRV(pin_def->port, pin_def->pin);
187 const uint32_t drv_mask = SUNXI_GPIO_DRV_PINMASK(pin_def->pin);
188
189 drv = GPIO_READ(sc, drv_reg);
190 drv &= ~drv_mask;
191 drv |= __SHIFTIN((drive_strength / 10) - 1, drv_mask);
192 GPIO_WRITE(sc, drv_reg, drv);
193
194 return 0;
195 }
196
197 static int
198 sunxi_gpio_ctl(struct sunxi_gpio_softc *sc, const struct sunxi_gpio_pins *pin_def,
199 int flags)
200 {
201 if (flags & GPIO_PIN_INPUT)
202 return sunxi_gpio_setfunc(sc, pin_def, "gpio_in");
203 if (flags & GPIO_PIN_OUTPUT)
204 return sunxi_gpio_setfunc(sc, pin_def, "gpio_out");
205
206 return EINVAL;
207 }
208
209 static void *
210 sunxi_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
211 {
212 struct sunxi_gpio_softc * const sc = device_private(dev);
213 const struct sunxi_gpio_pins *pin_def;
214 struct sunxi_gpio_pin *gpin;
215 const u_int *gpio = data;
216 int error;
217
218 if (len != 16)
219 return NULL;
220
221 const uint8_t port = be32toh(gpio[1]) & 0xff;
222 const uint8_t pin = be32toh(gpio[2]) & 0xff;
223 const bool actlo = be32toh(gpio[3]) & 1;
224
225 pin_def = sunxi_gpio_lookup(sc, port, pin);
226 if (pin_def == NULL)
227 return NULL;
228
229 error = sunxi_gpio_ctl(sc, pin_def, flags);
230 if (error != 0)
231 return NULL;
232
233 gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
234 gpin->pin_sc = sc;
235 gpin->pin_def = pin_def;
236 gpin->pin_flags = flags;
237 gpin->pin_actlo = actlo;
238
239 return gpin;
240 }
241
242 static void
243 sunxi_gpio_release(device_t dev, void *priv)
244 {
245 struct sunxi_gpio_pin *pin = priv;
246
247 sunxi_gpio_ctl(pin->pin_sc, pin->pin_def, GPIO_PIN_INPUT);
248
249 kmem_free(pin, sizeof(*pin));
250 }
251
252 static int
253 sunxi_gpio_read(device_t dev, void *priv, bool raw)
254 {
255 struct sunxi_gpio_softc * const sc = device_private(dev);
256 struct sunxi_gpio_pin *pin = priv;
257 const struct sunxi_gpio_pins *pin_def = pin->pin_def;
258 uint32_t data;
259 int val;
260
261 KASSERT(sc == pin->pin_sc);
262
263 const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
264 const uint32_t data_mask = __BIT(pin_def->pin);
265
266 data = GPIO_READ(sc, data_reg);
267 val = __SHIFTOUT(data, data_mask);
268 if (!raw && pin->pin_actlo)
269 val = !val;
270
271 #ifdef SUNXI_GPIO_DEBUG
272 device_printf(dev, "P%c%02d rd %08x (%d %d)\n",
273 pin_def->port + 'A', pin_def->pin, data,
274 __SHIFTOUT(val, data_mask), val);
275 #endif
276
277 return val;
278 }
279
280 static void
281 sunxi_gpio_write(device_t dev, void *priv, int val, bool raw)
282 {
283 struct sunxi_gpio_softc * const sc = device_private(dev);
284 struct sunxi_gpio_pin *pin = priv;
285 const struct sunxi_gpio_pins *pin_def = pin->pin_def;
286 uint32_t data;
287 #ifdef SUNXI_GPIO_DEBUG
288 uint32_t old_data;
289 #endif
290
291 KASSERT(sc == pin->pin_sc);
292
293 const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
294 const uint32_t data_mask = __BIT(pin_def->pin);
295
296 if (!raw && pin->pin_actlo)
297 val = !val;
298
299 /* XXX locking */
300 data = GPIO_READ(sc, data_reg);
301 #ifdef SUNXI_GPIO_DEBUG
302 old_data = data;
303 #endif
304 data &= ~data_mask;
305 data |= __SHIFTIN(val, data_mask);
306 GPIO_WRITE(sc, data_reg, data_mask);
307
308 #ifdef SUNXI_GPIO_DEBUG
309 device_printf(dev, "P%c%02d wr %08x -> %08x\n",
310 pin_def->port + 'A', pin_def->pin, old_data, data);
311 #endif
312 }
313
314 static struct fdtbus_gpio_controller_func sunxi_gpio_funcs = {
315 .acquire = sunxi_gpio_acquire,
316 .release = sunxi_gpio_release,
317 .read = sunxi_gpio_read,
318 .write = sunxi_gpio_write,
319 };
320
321 static int
322 sunxi_pinctrl_set_config(device_t dev, const void *data, size_t len)
323 {
324 struct sunxi_gpio_softc * const sc = device_private(dev);
325 const struct sunxi_gpio_pins *pin_def;
326 u_int drive_strength;
327
328 if (len != 4)
329 return -1;
330
331 const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
332
333 /*
334 * Required: pins, function
335 * Optional: bias-disable, bias-pull-up, bias-pull-down, drive-strength
336 */
337
338 const char *function = fdtbus_get_string(phandle, "function");
339 if (function == NULL)
340 return -1;
341 int pins_len = OF_getproplen(phandle, "pins");
342 if (pins_len <= 0)
343 return -1;
344 const char *pins = fdtbus_get_string(phandle, "pins");
345
346 for (pins = fdtbus_get_string(phandle, "pins");
347 pins_len > 0;
348 pins_len -= strlen(pins) + 1, pins += strlen(pins) + 1) {
349 pin_def = sunxi_gpio_lookup_byname(sc, pins);
350 if (pin_def == NULL) {
351 aprint_error_dev(dev, "unknown pin name '%s'\n", pins);
352 continue;
353 }
354 if (sunxi_gpio_setfunc(sc, pin_def, function) != 0)
355 continue;
356
357 if (of_hasprop(phandle, "bias-disable"))
358 sunxi_gpio_setpull(sc, pin_def, 0);
359 else if (of_hasprop(phandle, "bias-pull-up"))
360 sunxi_gpio_setpull(sc, pin_def, GPIO_PIN_PULLUP);
361 else if (of_hasprop(phandle, "bias-pull-down"))
362 sunxi_gpio_setpull(sc, pin_def, GPIO_PIN_PULLDOWN);
363
364 if (of_getprop_uint32(phandle, "drive-strength", &drive_strength) == 0)
365 sunxi_gpio_setdrv(sc, pin_def, drive_strength);
366 }
367
368 return 0;
369 }
370
371 static struct fdtbus_pinctrl_controller_func sunxi_pinctrl_funcs = {
372 .set_config = sunxi_pinctrl_set_config,
373 };
374
375 static int
376 sunxi_gpio_match(device_t parent, cfdata_t cf, void *aux)
377 {
378 struct fdt_attach_args * const faa = aux;
379
380 return of_match_compat_data(faa->faa_phandle, compat_data);
381 }
382
383 static void
384 sunxi_gpio_attach(device_t parent, device_t self, void *aux)
385 {
386 struct sunxi_gpio_softc * const sc = device_private(self);
387 struct fdt_attach_args * const faa = aux;
388 const int phandle = faa->faa_phandle;
389 bus_addr_t addr;
390 bus_size_t size;
391 int child;
392
393 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
394 aprint_error(": couldn't get registers\n");
395 return;
396 }
397
398 sc->sc_dev = self;
399 sc->sc_bst = faa->faa_bst;
400 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
401 aprint_error(": couldn't map registers\n");
402 return;
403 }
404 sc->sc_padconf = (void *)of_search_compatible(phandle, compat_data)->data;
405
406 aprint_naive("\n");
407 aprint_normal(": PIO\n");
408
409 fdtbus_register_gpio_controller(self, phandle, &sunxi_gpio_funcs);
410
411 for (child = OF_child(phandle); child; child = OF_peer(child)) {
412 if (!of_hasprop(child, "function") || !of_hasprop(child, "pins"))
413 continue;
414 fdtbus_register_pinctrl_config(self, child, &sunxi_pinctrl_funcs);
415 }
416
417 fdtbus_pinctrl_configure();
418 }
419