sunxi_gpio.c revision 1.1 1 1.1 jmcneill /* $NetBSD: sunxi_gpio.c,v 1.1 2017/07/02 13:36:46 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 "opt_soc.h"
30 1.1 jmcneill
31 1.1 jmcneill #include <sys/cdefs.h>
32 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_gpio.c,v 1.1 2017/07/02 13:36:46 jmcneill 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.1 jmcneill
43 1.1 jmcneill #include <dev/fdt/fdtvar.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <arm/sunxi/sunxi_gpio.h>
46 1.1 jmcneill
47 1.1 jmcneill #define SUNXI_GPIO_PORT(port) (0x20 * (port))
48 1.1 jmcneill #define SUNXI_GPIO_CFG(port, pin) (SUNXI_GPIO_PORT(port) + (0x4 * ((pin) / 8)))
49 1.1 jmcneill #define SUNXI_GPIO_CFG_PINMASK(pin) (0x7 << (((pin) % 8) * 4))
50 1.1 jmcneill #define SUNXI_GPIO_DATA(port) (SUNXI_GPIO_PORT(port) + 0x10)
51 1.1 jmcneill #define SUNXI_GPIO_DRV(port, pin) (SUNXI_GPIO_PORT(port) + 0x14 + (0x4 * ((pin) / 16)))
52 1.1 jmcneill #define SUNXI_GPIO_PULL(port, pin) (SUNXI_GPIO_PORT(port) + 0x1c + (0x4 * ((pin) / 16)))
53 1.1 jmcneill
54 1.1 jmcneill static const struct of_compat_data compat_data[] = {
55 1.1 jmcneill #ifdef SOC_SUN6I_A31
56 1.1 jmcneill { "allwinner,sun6i-a31-pinctrl", (uintptr_t)&sun6i_a31_padconf },
57 1.1 jmcneill #endif
58 1.1 jmcneill #ifdef SOC_SUN8I_H3
59 1.1 jmcneill { "allwinner,sun8i-h3-pinctrl", (uintptr_t)&sun8i_h3_padconf },
60 1.1 jmcneill { "allwunner,sun8i-h3-r-pinctrl", (uintptr_t)&sun8i_h3_r_padconf },
61 1.1 jmcneill #endif
62 1.1 jmcneill { NULL }
63 1.1 jmcneill };
64 1.1 jmcneill
65 1.1 jmcneill struct sunxi_gpio_softc {
66 1.1 jmcneill device_t sc_dev;
67 1.1 jmcneill bus_space_tag_t sc_bst;
68 1.1 jmcneill bus_space_handle_t sc_bsh;
69 1.1 jmcneill const struct sunxi_gpio_padconf *sc_padconf;
70 1.1 jmcneill };
71 1.1 jmcneill
72 1.1 jmcneill struct sunxi_gpio_pin {
73 1.1 jmcneill struct sunxi_gpio_softc *pin_sc;
74 1.1 jmcneill const struct sunxi_gpio_pins *pin_def;
75 1.1 jmcneill int pin_flags;
76 1.1 jmcneill bool pin_actlo;
77 1.1 jmcneill };
78 1.1 jmcneill
79 1.1 jmcneill #define GPIO_READ(sc, reg) \
80 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
81 1.1 jmcneill #define GPIO_WRITE(sc, reg, val) \
82 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
83 1.1 jmcneill
84 1.1 jmcneill static int sunxi_gpio_match(device_t, cfdata_t, void *);
85 1.1 jmcneill static void sunxi_gpio_attach(device_t, device_t, void *);
86 1.1 jmcneill
87 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_gpio, sizeof(struct sunxi_gpio_softc),
88 1.1 jmcneill sunxi_gpio_match, sunxi_gpio_attach, NULL, NULL);
89 1.1 jmcneill
90 1.1 jmcneill static const struct sunxi_gpio_pins *
91 1.1 jmcneill sunxi_gpio_lookup(struct sunxi_gpio_softc *sc, uint8_t port, uint8_t pin)
92 1.1 jmcneill {
93 1.1 jmcneill const struct sunxi_gpio_pins *pin_def;
94 1.1 jmcneill u_int n;
95 1.1 jmcneill
96 1.1 jmcneill for (n = 0; n < sc->sc_padconf->npins; n++) {
97 1.1 jmcneill pin_def = &sc->sc_padconf->pins[n];
98 1.1 jmcneill if (pin_def->port == port && pin_def->pin == pin)
99 1.1 jmcneill return pin_def;
100 1.1 jmcneill }
101 1.1 jmcneill
102 1.1 jmcneill return NULL;
103 1.1 jmcneill }
104 1.1 jmcneill
105 1.1 jmcneill static int
106 1.1 jmcneill sunxi_gpio_setfunc(struct sunxi_gpio_softc *sc,
107 1.1 jmcneill const struct sunxi_gpio_pins *pin_def, const char *func)
108 1.1 jmcneill {
109 1.1 jmcneill uint32_t cfg;
110 1.1 jmcneill u_int n;
111 1.1 jmcneill
112 1.1 jmcneill const bus_size_t cfg_reg = SUNXI_GPIO_CFG(pin_def->port, pin_def->pin);
113 1.1 jmcneill const uint32_t cfg_mask = SUNXI_GPIO_CFG_PINMASK(pin_def->pin);
114 1.1 jmcneill
115 1.1 jmcneill for (n = 0; n < SUNXI_GPIO_MAXFUNC; n++) {
116 1.1 jmcneill if (pin_def->functions[n] == NULL)
117 1.1 jmcneill continue;
118 1.1 jmcneill if (strcmp(pin_def->functions[n], func) == 0) {
119 1.1 jmcneill cfg = GPIO_READ(sc, cfg_reg);
120 1.1 jmcneill cfg &= ~cfg_mask;
121 1.1 jmcneill cfg |= __SHIFTIN(n, cfg_mask);
122 1.1 jmcneill GPIO_WRITE(sc, cfg_reg, cfg);
123 1.1 jmcneill return 0;
124 1.1 jmcneill }
125 1.1 jmcneill }
126 1.1 jmcneill
127 1.1 jmcneill /* Function not found */
128 1.1 jmcneill device_printf(sc->sc_dev, "function '%s' not supported on P%c%02d\n",
129 1.1 jmcneill func, pin_def->port + 'A', pin_def->pin);
130 1.1 jmcneill
131 1.1 jmcneill return ENXIO;
132 1.1 jmcneill }
133 1.1 jmcneill
134 1.1 jmcneill static int
135 1.1 jmcneill sunxi_gpio_ctl(struct sunxi_gpio_softc *sc, const struct sunxi_gpio_pins *pin_def,
136 1.1 jmcneill int flags)
137 1.1 jmcneill {
138 1.1 jmcneill if (flags & GPIO_PIN_INPUT)
139 1.1 jmcneill return sunxi_gpio_setfunc(sc, pin_def, "gpio_in");
140 1.1 jmcneill if (flags & GPIO_PIN_OUTPUT)
141 1.1 jmcneill return sunxi_gpio_setfunc(sc, pin_def, "gpio_out");
142 1.1 jmcneill
143 1.1 jmcneill return EINVAL;
144 1.1 jmcneill }
145 1.1 jmcneill
146 1.1 jmcneill static void *
147 1.1 jmcneill sunxi_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
148 1.1 jmcneill {
149 1.1 jmcneill struct sunxi_gpio_softc * const sc = device_private(dev);
150 1.1 jmcneill const struct sunxi_gpio_pins *pin_def;
151 1.1 jmcneill struct sunxi_gpio_pin *gpin;
152 1.1 jmcneill const u_int *gpio = data;
153 1.1 jmcneill int error;
154 1.1 jmcneill
155 1.1 jmcneill if (len != 16)
156 1.1 jmcneill return NULL;
157 1.1 jmcneill
158 1.1 jmcneill const uint8_t port = be32toh(gpio[1]) & 0xff;
159 1.1 jmcneill const uint8_t pin = be32toh(gpio[2]) & 0xff;
160 1.1 jmcneill const bool actlo = be32toh(gpio[3]) & 1;
161 1.1 jmcneill
162 1.1 jmcneill pin_def = sunxi_gpio_lookup(sc, port, pin);
163 1.1 jmcneill if (pin_def == NULL)
164 1.1 jmcneill return NULL;
165 1.1 jmcneill
166 1.1 jmcneill error = sunxi_gpio_ctl(sc, pin_def, flags);
167 1.1 jmcneill if (error != 0)
168 1.1 jmcneill return NULL;
169 1.1 jmcneill
170 1.1 jmcneill gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
171 1.1 jmcneill gpin->pin_sc = sc;
172 1.1 jmcneill gpin->pin_def = pin_def;
173 1.1 jmcneill gpin->pin_flags = flags;
174 1.1 jmcneill gpin->pin_actlo = actlo;
175 1.1 jmcneill
176 1.1 jmcneill return gpin;
177 1.1 jmcneill }
178 1.1 jmcneill
179 1.1 jmcneill static void
180 1.1 jmcneill sunxi_gpio_release(device_t dev, void *priv)
181 1.1 jmcneill {
182 1.1 jmcneill struct sunxi_gpio_pin *pin = priv;
183 1.1 jmcneill
184 1.1 jmcneill sunxi_gpio_ctl(pin->pin_sc, pin->pin_def, GPIO_PIN_INPUT);
185 1.1 jmcneill
186 1.1 jmcneill kmem_free(pin, sizeof(*pin));
187 1.1 jmcneill }
188 1.1 jmcneill
189 1.1 jmcneill static int
190 1.1 jmcneill sunxi_gpio_read(device_t dev, void *priv, bool raw)
191 1.1 jmcneill {
192 1.1 jmcneill struct sunxi_gpio_softc * const sc = device_private(dev);
193 1.1 jmcneill struct sunxi_gpio_pin *pin = priv;
194 1.1 jmcneill const struct sunxi_gpio_pins *pin_def = pin->pin_def;
195 1.1 jmcneill uint32_t data;
196 1.1 jmcneill int val;
197 1.1 jmcneill
198 1.1 jmcneill KASSERT(sc == pin->pin_sc);
199 1.1 jmcneill
200 1.1 jmcneill const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
201 1.1 jmcneill const uint32_t data_mask = __BIT(pin_def->pin);
202 1.1 jmcneill
203 1.1 jmcneill data = GPIO_READ(sc, data_reg);
204 1.1 jmcneill val = __SHIFTOUT(data, data_mask);
205 1.1 jmcneill if (!raw && pin->pin_actlo)
206 1.1 jmcneill val = !val;
207 1.1 jmcneill
208 1.1 jmcneill #ifdef SUNXI_GPIO_DEBUG
209 1.1 jmcneill device_printf(dev, "P%c%02d rd %08x (%d %d)\n",
210 1.1 jmcneill pin_def->port + 'A', pin_def->pin, data,
211 1.1 jmcneill __SHIFTOUT(val, data_mask), val);
212 1.1 jmcneill #endif
213 1.1 jmcneill
214 1.1 jmcneill return val;
215 1.1 jmcneill }
216 1.1 jmcneill
217 1.1 jmcneill static void
218 1.1 jmcneill sunxi_gpio_write(device_t dev, void *priv, int val, bool raw)
219 1.1 jmcneill {
220 1.1 jmcneill struct sunxi_gpio_softc * const sc = device_private(dev);
221 1.1 jmcneill struct sunxi_gpio_pin *pin = priv;
222 1.1 jmcneill const struct sunxi_gpio_pins *pin_def = pin->pin_def;
223 1.1 jmcneill uint32_t data;
224 1.1 jmcneill #ifdef SUNXI_GPIO_DEBUG
225 1.1 jmcneill uint32_t old_data;
226 1.1 jmcneill #endif
227 1.1 jmcneill
228 1.1 jmcneill KASSERT(sc == pin->pin_sc);
229 1.1 jmcneill
230 1.1 jmcneill const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
231 1.1 jmcneill const uint32_t data_mask = __BIT(pin_def->pin);
232 1.1 jmcneill
233 1.1 jmcneill if (!raw && pin->pin_actlo)
234 1.1 jmcneill val = !val;
235 1.1 jmcneill
236 1.1 jmcneill /* XXX locking */
237 1.1 jmcneill data = GPIO_READ(sc, data_reg);
238 1.1 jmcneill #ifdef SUNXI_GPIO_DEBUG
239 1.1 jmcneill old_data = data;
240 1.1 jmcneill #endif
241 1.1 jmcneill data &= ~data_mask;
242 1.1 jmcneill data |= __SHIFTIN(val, data_mask);
243 1.1 jmcneill GPIO_WRITE(sc, data_reg, data_mask);
244 1.1 jmcneill
245 1.1 jmcneill #ifdef SUNXI_GPIO_DEBUG
246 1.1 jmcneill device_printf(dev, "P%c%02d wr %08x -> %08x\n",
247 1.1 jmcneill pin_def->port + 'A', pin_def->pin, old_data, data);
248 1.1 jmcneill #endif
249 1.1 jmcneill }
250 1.1 jmcneill
251 1.1 jmcneill static struct fdtbus_gpio_controller_func sunxi_gpio_funcs = {
252 1.1 jmcneill .acquire = sunxi_gpio_acquire,
253 1.1 jmcneill .release = sunxi_gpio_release,
254 1.1 jmcneill .read = sunxi_gpio_read,
255 1.1 jmcneill .write = sunxi_gpio_write,
256 1.1 jmcneill };
257 1.1 jmcneill
258 1.1 jmcneill static int
259 1.1 jmcneill sunxi_gpio_match(device_t parent, cfdata_t cf, void *aux)
260 1.1 jmcneill {
261 1.1 jmcneill struct fdt_attach_args * const faa = aux;
262 1.1 jmcneill
263 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
264 1.1 jmcneill }
265 1.1 jmcneill
266 1.1 jmcneill static void
267 1.1 jmcneill sunxi_gpio_attach(device_t parent, device_t self, void *aux)
268 1.1 jmcneill {
269 1.1 jmcneill struct sunxi_gpio_softc * const sc = device_private(self);
270 1.1 jmcneill struct fdt_attach_args * const faa = aux;
271 1.1 jmcneill const int phandle = faa->faa_phandle;
272 1.1 jmcneill bus_addr_t addr;
273 1.1 jmcneill bus_size_t size;
274 1.1 jmcneill
275 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
276 1.1 jmcneill aprint_error(": couldn't get registers\n");
277 1.1 jmcneill return;
278 1.1 jmcneill }
279 1.1 jmcneill
280 1.1 jmcneill sc->sc_dev = self;
281 1.1 jmcneill sc->sc_bst = faa->faa_bst;
282 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
283 1.1 jmcneill aprint_error(": couldn't map registers\n");
284 1.1 jmcneill return;
285 1.1 jmcneill }
286 1.1 jmcneill sc->sc_padconf = (void *)of_search_compatible(phandle, compat_data)->data;
287 1.1 jmcneill
288 1.1 jmcneill aprint_naive("\n");
289 1.1 jmcneill aprint_normal(": PIO\n");
290 1.1 jmcneill
291 1.1 jmcneill fdtbus_register_gpio_controller(self, phandle, &sunxi_gpio_funcs);
292 1.1 jmcneill }
293