rk_gpio.c revision 1.7.6.1 1 1.7.6.1 perseant /* $NetBSD: rk_gpio.c,v 1.7.6.1 2025/08/02 05:55:29 perseant Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2018 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.7.6.1 perseant __KERNEL_RCSID(0, "$NetBSD: rk_gpio.c,v 1.7.6.1 2025/08/02 05:55:29 perseant 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/device.h>
35 1.1 jmcneill #include <sys/intr.h>
36 1.1 jmcneill #include <sys/systm.h>
37 1.1 jmcneill #include <sys/mutex.h>
38 1.1 jmcneill #include <sys/kmem.h>
39 1.1 jmcneill #include <sys/gpio.h>
40 1.1 jmcneill #include <sys/bitops.h>
41 1.1 jmcneill #include <sys/lwp.h>
42 1.1 jmcneill
43 1.1 jmcneill #include <dev/fdt/fdtvar.h>
44 1.1 jmcneill #include <dev/gpio/gpiovar.h>
45 1.1 jmcneill
46 1.1 jmcneill #define GPIO_SWPORTA_DR_REG 0x0000
47 1.1 jmcneill #define GPIO_SWPORTA_DDR_REG 0x0004
48 1.1 jmcneill #define GPIO_INTEN_REG 0x0030
49 1.1 jmcneill #define GPIO_INTMASK_REG 0x0034
50 1.1 jmcneill #define GPIO_INTTYPE_LEVEL_REG 0x0038
51 1.1 jmcneill #define GPIO_INT_POLARITY_REG 0x003c
52 1.1 jmcneill #define GPIO_INT_STATUS_REG 0x0040
53 1.1 jmcneill #define GPIO_INT_RAWSTATUS_REG 0x0044
54 1.1 jmcneill #define GPIO_DEBOUNCE_REG 0x0048
55 1.1 jmcneill #define GPIO_PORTA_EOI_REG 0x004c
56 1.1 jmcneill #define GPIO_EXT_PORTA_REG 0x0050
57 1.1 jmcneill #define GPIO_LS_SYNC_REG 0x0060
58 1.7 tnn #define GPIO_VER_ID_REG 0x0078
59 1.7 tnn #define GPIO_VER_ID_GPIOV2 0x0101157c
60 1.7 tnn
61 1.7 tnn /*
62 1.7 tnn * In "version 2" GPIO controllers, half of each register is used by the
63 1.7 tnn * write_enable mask, so the 32 pins are spread over two registers.
64 1.7 tnn *
65 1.7 tnn * pins 0 - 15 go into the GPIO_SWPORT_*_L register
66 1.7 tnn * pins 16 - 31 go into the GPIO_SWPORT_*_H register
67 1.7 tnn */
68 1.7 tnn #define GPIOV2_SWPORT_DR_BASE 0x0000
69 1.7 tnn #define GPIOV2_SWPORT_DR_REG(pin) \
70 1.7 tnn (GPIOV2_SWPORT_DR_BASE + GPIOV2_REG_OFFSET(pin))
71 1.7 tnn #define GPIOV2_SWPORT_DDR_BASE 0x0008
72 1.7 tnn #define GPIOV2_SWPORT_DDR_REG(pin) \
73 1.7 tnn (GPIOV2_SWPORT_DDR_BASE + GPIOV2_REG_OFFSET(pin))
74 1.7 tnn #define GPIOV2_EXT_PORT_REG 0x0070
75 1.7 tnn #define GPIOV2_REG_OFFSET(pin) (((pin) >> 4) << 2)
76 1.7 tnn #define GPIOV2_DATA_MASK(pin) (__BIT((pin) & 0xF))
77 1.7 tnn #define GPIOV2_WRITE_MASK(pin) (__BIT(((pin) & 0xF) | 0x10))
78 1.1 jmcneill
79 1.3 thorpej static const struct device_compatible_entry compat_data[] = {
80 1.3 thorpej { .compat = "rockchip,gpio-bank" },
81 1.3 thorpej DEVICE_COMPAT_EOL
82 1.1 jmcneill };
83 1.1 jmcneill
84 1.7.6.1 perseant struct rk_gpio_eint {
85 1.7.6.1 perseant int (*eint_func)(void *);
86 1.7.6.1 perseant void *eint_arg;
87 1.7.6.1 perseant bool eint_mpsafe;
88 1.7.6.1 perseant int eint_num;
89 1.7.6.1 perseant };
90 1.7.6.1 perseant
91 1.1 jmcneill struct rk_gpio_softc {
92 1.1 jmcneill device_t sc_dev;
93 1.1 jmcneill bus_space_tag_t sc_bst;
94 1.1 jmcneill bus_space_handle_t sc_bsh;
95 1.1 jmcneill kmutex_t sc_lock;
96 1.1 jmcneill
97 1.1 jmcneill struct gpio_chipset_tag sc_gp;
98 1.1 jmcneill gpio_pin_t sc_pins[32];
99 1.1 jmcneill device_t sc_gpiodev;
100 1.7.6.1 perseant
101 1.7.6.1 perseant void *sc_ih;
102 1.7.6.1 perseant struct rk_gpio_eint sc_eint[32];
103 1.1 jmcneill };
104 1.1 jmcneill
105 1.1 jmcneill struct rk_gpio_pin {
106 1.1 jmcneill struct rk_gpio_softc *pin_sc;
107 1.1 jmcneill u_int pin_nr;
108 1.1 jmcneill int pin_flags;
109 1.1 jmcneill bool pin_actlo;
110 1.1 jmcneill };
111 1.1 jmcneill
112 1.1 jmcneill #define RD4(sc, reg) \
113 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
114 1.1 jmcneill #define WR4(sc, reg, val) \
115 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
116 1.1 jmcneill
117 1.1 jmcneill static int rk_gpio_match(device_t, cfdata_t, void *);
118 1.1 jmcneill static void rk_gpio_attach(device_t, device_t, void *);
119 1.1 jmcneill
120 1.1 jmcneill CFATTACH_DECL_NEW(rk_gpio, sizeof(struct rk_gpio_softc),
121 1.1 jmcneill rk_gpio_match, rk_gpio_attach, NULL, NULL);
122 1.1 jmcneill
123 1.1 jmcneill static void *
124 1.1 jmcneill rk_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
125 1.1 jmcneill {
126 1.1 jmcneill struct rk_gpio_softc * const sc = device_private(dev);
127 1.1 jmcneill struct rk_gpio_pin *gpin;
128 1.1 jmcneill const u_int *gpio = data;
129 1.1 jmcneill
130 1.1 jmcneill if (len != 12)
131 1.1 jmcneill return NULL;
132 1.1 jmcneill
133 1.1 jmcneill const uint8_t pin = be32toh(gpio[1]) & 0xff;
134 1.1 jmcneill const bool actlo = be32toh(gpio[2]) & 1;
135 1.1 jmcneill
136 1.1 jmcneill if (pin >= __arraycount(sc->sc_pins))
137 1.1 jmcneill return NULL;
138 1.1 jmcneill
139 1.6 tnn sc->sc_gp.gp_pin_ctl(sc, pin, flags);
140 1.1 jmcneill
141 1.1 jmcneill gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
142 1.1 jmcneill gpin->pin_sc = sc;
143 1.1 jmcneill gpin->pin_nr = pin;
144 1.1 jmcneill gpin->pin_flags = flags;
145 1.1 jmcneill gpin->pin_actlo = actlo;
146 1.1 jmcneill
147 1.1 jmcneill return gpin;
148 1.1 jmcneill }
149 1.1 jmcneill
150 1.1 jmcneill static void
151 1.1 jmcneill rk_gpio_release(device_t dev, void *priv)
152 1.1 jmcneill {
153 1.1 jmcneill struct rk_gpio_softc * const sc = device_private(dev);
154 1.1 jmcneill struct rk_gpio_pin *pin = priv;
155 1.1 jmcneill
156 1.6 tnn KASSERT(sc == pin->pin_sc);
157 1.6 tnn
158 1.6 tnn sc->sc_gp.gp_pin_ctl(sc, pin->pin_nr, GPIO_PIN_INPUT);
159 1.1 jmcneill
160 1.1 jmcneill kmem_free(pin, sizeof(*pin));
161 1.1 jmcneill }
162 1.1 jmcneill
163 1.1 jmcneill static int
164 1.1 jmcneill rk_gpio_read(device_t dev, void *priv, bool raw)
165 1.1 jmcneill {
166 1.1 jmcneill struct rk_gpio_softc * const sc = device_private(dev);
167 1.1 jmcneill struct rk_gpio_pin *pin = priv;
168 1.1 jmcneill int val;
169 1.1 jmcneill
170 1.1 jmcneill KASSERT(sc == pin->pin_sc);
171 1.1 jmcneill
172 1.6 tnn val = sc->sc_gp.gp_pin_read(sc, pin->pin_nr);
173 1.1 jmcneill if (!raw && pin->pin_actlo)
174 1.1 jmcneill val = !val;
175 1.1 jmcneill
176 1.1 jmcneill return val;
177 1.1 jmcneill }
178 1.1 jmcneill
179 1.1 jmcneill static void
180 1.1 jmcneill rk_gpio_write(device_t dev, void *priv, int val, bool raw)
181 1.1 jmcneill {
182 1.1 jmcneill struct rk_gpio_softc * const sc = device_private(dev);
183 1.1 jmcneill struct rk_gpio_pin *pin = priv;
184 1.1 jmcneill
185 1.1 jmcneill KASSERT(sc == pin->pin_sc);
186 1.1 jmcneill
187 1.1 jmcneill if (!raw && pin->pin_actlo)
188 1.1 jmcneill val = !val;
189 1.1 jmcneill
190 1.6 tnn sc->sc_gp.gp_pin_write(sc, pin->pin_nr, val);
191 1.1 jmcneill }
192 1.1 jmcneill
193 1.1 jmcneill static struct fdtbus_gpio_controller_func rk_gpio_funcs = {
194 1.1 jmcneill .acquire = rk_gpio_acquire,
195 1.1 jmcneill .release = rk_gpio_release,
196 1.1 jmcneill .read = rk_gpio_read,
197 1.1 jmcneill .write = rk_gpio_write,
198 1.1 jmcneill };
199 1.1 jmcneill
200 1.1 jmcneill static int
201 1.1 jmcneill rk_gpio_pin_read(void *priv, int pin)
202 1.1 jmcneill {
203 1.1 jmcneill struct rk_gpio_softc * const sc = priv;
204 1.1 jmcneill uint32_t data;
205 1.1 jmcneill int val;
206 1.1 jmcneill
207 1.1 jmcneill KASSERT(pin < __arraycount(sc->sc_pins));
208 1.1 jmcneill
209 1.1 jmcneill const uint32_t data_mask = __BIT(pin);
210 1.1 jmcneill
211 1.1 jmcneill /* No lock required for reads */
212 1.2 jmcneill data = RD4(sc, GPIO_EXT_PORTA_REG);
213 1.1 jmcneill val = __SHIFTOUT(data, data_mask);
214 1.1 jmcneill
215 1.1 jmcneill return val;
216 1.1 jmcneill }
217 1.1 jmcneill
218 1.1 jmcneill static void
219 1.1 jmcneill rk_gpio_pin_write(void *priv, int pin, int val)
220 1.1 jmcneill {
221 1.1 jmcneill struct rk_gpio_softc * const sc = priv;
222 1.1 jmcneill uint32_t data;
223 1.1 jmcneill
224 1.1 jmcneill KASSERT(pin < __arraycount(sc->sc_pins));
225 1.1 jmcneill
226 1.1 jmcneill const uint32_t data_mask = __BIT(pin);
227 1.1 jmcneill
228 1.1 jmcneill mutex_enter(&sc->sc_lock);
229 1.1 jmcneill data = RD4(sc, GPIO_SWPORTA_DR_REG);
230 1.1 jmcneill if (val)
231 1.1 jmcneill data |= data_mask;
232 1.1 jmcneill else
233 1.1 jmcneill data &= ~data_mask;
234 1.1 jmcneill WR4(sc, GPIO_SWPORTA_DR_REG, data);
235 1.1 jmcneill mutex_exit(&sc->sc_lock);
236 1.1 jmcneill }
237 1.1 jmcneill
238 1.1 jmcneill static void
239 1.1 jmcneill rk_gpio_pin_ctl(void *priv, int pin, int flags)
240 1.1 jmcneill {
241 1.1 jmcneill struct rk_gpio_softc * const sc = priv;
242 1.6 tnn uint32_t ddr;
243 1.1 jmcneill
244 1.1 jmcneill KASSERT(pin < __arraycount(sc->sc_pins));
245 1.1 jmcneill
246 1.1 jmcneill mutex_enter(&sc->sc_lock);
247 1.6 tnn ddr = RD4(sc, GPIO_SWPORTA_DDR_REG);
248 1.6 tnn if (flags & GPIO_PIN_INPUT)
249 1.6 tnn ddr &= ~__BIT(pin);
250 1.6 tnn else if (flags & GPIO_PIN_OUTPUT)
251 1.6 tnn ddr |= __BIT(pin);
252 1.6 tnn WR4(sc, GPIO_SWPORTA_DDR_REG, ddr);
253 1.1 jmcneill mutex_exit(&sc->sc_lock);
254 1.1 jmcneill }
255 1.1 jmcneill
256 1.7 tnn static int
257 1.7 tnn rk_gpio_v2_pin_read(void *priv, int pin)
258 1.7 tnn {
259 1.7 tnn struct rk_gpio_softc * const sc = priv;
260 1.7 tnn uint32_t data;
261 1.7 tnn int val;
262 1.7 tnn
263 1.7 tnn KASSERT(pin < __arraycount(sc->sc_pins));
264 1.7 tnn
265 1.7 tnn const uint32_t data_mask = __BIT(pin);
266 1.7 tnn
267 1.7 tnn /* No lock required for reads */
268 1.7 tnn data = RD4(sc, GPIOV2_EXT_PORT_REG);
269 1.7 tnn val = __SHIFTOUT(data, data_mask);
270 1.7 tnn
271 1.7 tnn return val;
272 1.7 tnn }
273 1.7 tnn
274 1.7 tnn static void
275 1.7 tnn rk_gpio_v2_pin_write(void *priv, int pin, int val)
276 1.7 tnn {
277 1.7 tnn struct rk_gpio_softc * const sc = priv;
278 1.7 tnn uint32_t data;
279 1.7 tnn
280 1.7 tnn KASSERT(pin < __arraycount(sc->sc_pins));
281 1.7 tnn
282 1.7 tnn const uint32_t write_mask = GPIOV2_WRITE_MASK(pin);
283 1.7 tnn
284 1.7 tnn /* No lock required for writes on v2 controllers */
285 1.7 tnn data = val ? GPIOV2_DATA_MASK(pin) : 0;
286 1.7 tnn WR4(sc, GPIOV2_SWPORT_DR_REG(pin), write_mask | data);
287 1.7 tnn }
288 1.7 tnn
289 1.7 tnn static void
290 1.7 tnn rk_gpio_v2_pin_ctl(void *priv, int pin, int flags)
291 1.7 tnn {
292 1.7 tnn struct rk_gpio_softc * const sc = priv;
293 1.7 tnn uint32_t ddr;
294 1.7 tnn
295 1.7 tnn KASSERT(pin < __arraycount(sc->sc_pins));
296 1.7 tnn
297 1.7 tnn /* No lock required for writes on v2 controllers */
298 1.7 tnn ddr = (flags & GPIO_PIN_OUTPUT) ? GPIOV2_DATA_MASK(pin) : 0;
299 1.7 tnn WR4(sc, GPIOV2_SWPORT_DDR_REG(pin), GPIOV2_WRITE_MASK(pin) | ddr);
300 1.7 tnn }
301 1.7 tnn
302 1.7.6.1 perseant static int
303 1.7.6.1 perseant rk_gpio_intr(void *priv)
304 1.7.6.1 perseant {
305 1.7.6.1 perseant struct rk_gpio_softc * const sc = priv;
306 1.7.6.1 perseant struct rk_gpio_eint *eint;
307 1.7.6.1 perseant uint32_t status, bit;
308 1.7.6.1 perseant int ret = 0;
309 1.7.6.1 perseant
310 1.7.6.1 perseant status = RD4(sc, GPIO_INT_STATUS_REG);
311 1.7.6.1 perseant if (status == 0)
312 1.7.6.1 perseant return ret;
313 1.7.6.1 perseant
314 1.7.6.1 perseant WR4(sc, GPIO_PORTA_EOI_REG, status);
315 1.7.6.1 perseant
316 1.7.6.1 perseant while ((bit = ffs32(status)) != 0) {
317 1.7.6.1 perseant status &= ~__BIT(bit - 1);
318 1.7.6.1 perseant eint = &sc->sc_eint[bit - 1];
319 1.7.6.1 perseant if (eint == NULL || eint->eint_func == NULL)
320 1.7.6.1 perseant continue;
321 1.7.6.1 perseant if (!eint->eint_mpsafe)
322 1.7.6.1 perseant KERNEL_LOCK(1, curlwp);
323 1.7.6.1 perseant ret |= eint->eint_func(eint->eint_arg);
324 1.7.6.1 perseant if (!eint->eint_mpsafe)
325 1.7.6.1 perseant KERNEL_UNLOCK_ONE(curlwp);
326 1.7.6.1 perseant }
327 1.7.6.1 perseant
328 1.7.6.1 perseant return ret;
329 1.7.6.1 perseant }
330 1.7.6.1 perseant
331 1.7.6.1 perseant static void *
332 1.7.6.1 perseant rk_intr_enable(struct rk_gpio_softc *sc, u_int pin, uint32_t level,
333 1.7.6.1 perseant uint32_t polarity, bool mpsafe, int (*func)(void *), void *arg)
334 1.7.6.1 perseant {
335 1.7.6.1 perseant uint32_t val;
336 1.7.6.1 perseant struct rk_gpio_eint *eint;
337 1.7.6.1 perseant
338 1.7.6.1 perseant mutex_enter(&sc->sc_lock);
339 1.7.6.1 perseant if (sc->sc_eint[pin].eint_func != NULL) {
340 1.7.6.1 perseant mutex_exit(&sc->sc_lock);
341 1.7.6.1 perseant return NULL; /* in use */
342 1.7.6.1 perseant }
343 1.7.6.1 perseant
344 1.7.6.1 perseant eint = &sc->sc_eint[pin];
345 1.7.6.1 perseant
346 1.7.6.1 perseant eint->eint_func = func;
347 1.7.6.1 perseant eint->eint_arg = arg;
348 1.7.6.1 perseant eint->eint_mpsafe = mpsafe;
349 1.7.6.1 perseant eint->eint_num = pin;
350 1.7.6.1 perseant
351 1.7.6.1 perseant val = RD4(sc, GPIO_INTTYPE_LEVEL_REG);
352 1.7.6.1 perseant if (level)
353 1.7.6.1 perseant val |= 1 << pin;
354 1.7.6.1 perseant else
355 1.7.6.1 perseant val &= ~(1 << pin);
356 1.7.6.1 perseant WR4(sc, GPIO_INTTYPE_LEVEL_REG, val);
357 1.7.6.1 perseant
358 1.7.6.1 perseant val = RD4(sc, GPIO_INT_POLARITY_REG);
359 1.7.6.1 perseant if (polarity)
360 1.7.6.1 perseant val |= 1 << pin;
361 1.7.6.1 perseant else
362 1.7.6.1 perseant val &= ~(1 << pin);
363 1.7.6.1 perseant WR4(sc, GPIO_INT_POLARITY_REG, val);
364 1.7.6.1 perseant
365 1.7.6.1 perseant val = RD4(sc, GPIO_INTEN_REG);
366 1.7.6.1 perseant val |= 1 << pin;
367 1.7.6.1 perseant WR4(sc, GPIO_INTEN_REG, val);
368 1.7.6.1 perseant #if 0
369 1.7.6.1 perseant /* Configure eint mode */
370 1.7.6.1 perseant val = R4(sc, SUNXI_GPIO_INT_CFG, pin);
371 1.7.6.1 perseant val &= ~SUNXI_GPIO_INT_MODEMASK(eint->eint_num);
372 1.7.6.1 perseant val |= __SHIFTIN(mode, SUNXI_GPIO_INT_MODEMASK(eint->eint_num));
373 1.7.6.1 perseant GPIO_WRITE(sc, SUNXI_GPIO_INT_CFG(eint->eint_bank, eint->eint_num), val);
374 1.7.6.1 perseant
375 1.7.6.1 perseant val = SUNXI_GPIO_INT_DEBOUNCE_CLK_SEL;
376 1.7.6.1 perseant GPIO_WRITE(sc, SUNXI_GPIO_INT_DEBOUNCE(eint->eint_bank), val);
377 1.7.6.1 perseant
378 1.7.6.1 perseant /* Enable eint */
379 1.7.6.1 perseant val = GPIO_READ(sc, SUNXI_GPIO_INT_CTL(eint->eint_bank));
380 1.7.6.1 perseant val |= __BIT(eint->eint_num);
381 1.7.6.1 perseant GPIO_WRITE(sc, SUNXI_GPIO_INT_CTL(eint->eint_bank), val);
382 1.7.6.1 perseant #endif
383 1.7.6.1 perseant mutex_exit(&sc->sc_lock);
384 1.7.6.1 perseant
385 1.7.6.1 perseant return eint;
386 1.7.6.1 perseant }
387 1.7.6.1 perseant
388 1.7.6.1 perseant static void
389 1.7.6.1 perseant rk_intr_disable(struct rk_gpio_softc *sc, struct rk_gpio_eint *eint)
390 1.7.6.1 perseant {
391 1.7.6.1 perseant uint32_t val;
392 1.7.6.1 perseant
393 1.7.6.1 perseant KASSERT(eint != NULL && eint->eint_func != NULL);
394 1.7.6.1 perseant
395 1.7.6.1 perseant mutex_enter(&sc->sc_lock);
396 1.7.6.1 perseant
397 1.7.6.1 perseant /* Disable eint */
398 1.7.6.1 perseant val = RD4(sc, GPIO_INTEN_REG);
399 1.7.6.1 perseant val &= ~__BIT(eint->eint_num);
400 1.7.6.1 perseant WR4(sc, GPIO_INTEN_REG, val);
401 1.7.6.1 perseant WR4(sc, GPIO_INT_STATUS_REG, __BIT(eint->eint_num));
402 1.7.6.1 perseant
403 1.7.6.1 perseant sc->sc_eint[eint->eint_num].eint_func = NULL;
404 1.7.6.1 perseant
405 1.7.6.1 perseant mutex_exit(&sc->sc_lock);
406 1.7.6.1 perseant }
407 1.7.6.1 perseant
408 1.7.6.1 perseant static void *
409 1.7.6.1 perseant rk_fdt_intr_establish(device_t dev, u_int *specifier, int ipl, int flags,
410 1.7.6.1 perseant int (*func)(void *), void *arg, const char *xname)
411 1.7.6.1 perseant {
412 1.7.6.1 perseant struct rk_gpio_softc * const sc = device_private(dev);
413 1.7.6.1 perseant bool mpsafe = (flags & GPIO_INTR_MPSAFE) != 0;
414 1.7.6.1 perseant uint32_t level, polarity;
415 1.7.6.1 perseant
416 1.7.6.1 perseant const uint32_t pin = be32toh(specifier[0]);
417 1.7.6.1 perseant const uint32_t type = be32toh(specifier[1]) & 0xf;
418 1.7.6.1 perseant
419 1.7.6.1 perseant switch (type) {
420 1.7.6.1 perseant case FDT_INTR_TYPE_POS_EDGE:
421 1.7.6.1 perseant level = 1;
422 1.7.6.1 perseant polarity = 1;
423 1.7.6.1 perseant break;
424 1.7.6.1 perseant case FDT_INTR_TYPE_NEG_EDGE:
425 1.7.6.1 perseant level = 1;
426 1.7.6.1 perseant polarity = 0;
427 1.7.6.1 perseant break;
428 1.7.6.1 perseant case FDT_INTR_TYPE_HIGH_LEVEL:
429 1.7.6.1 perseant level = 0;
430 1.7.6.1 perseant polarity = 1;
431 1.7.6.1 perseant break;
432 1.7.6.1 perseant case FDT_INTR_TYPE_LOW_LEVEL:
433 1.7.6.1 perseant level = 0;
434 1.7.6.1 perseant polarity = 0;
435 1.7.6.1 perseant break;
436 1.7.6.1 perseant default:
437 1.7.6.1 perseant aprint_error_dev(dev, "%s: unsupported irq type 0x%x\n",
438 1.7.6.1 perseant __func__, type);
439 1.7.6.1 perseant return NULL;
440 1.7.6.1 perseant }
441 1.7.6.1 perseant
442 1.7.6.1 perseant return rk_intr_enable(sc, pin, level, polarity, mpsafe, func, arg);
443 1.7.6.1 perseant }
444 1.7.6.1 perseant
445 1.7.6.1 perseant static void
446 1.7.6.1 perseant rk_fdt_intr_disestablish(device_t dev, void *ih)
447 1.7.6.1 perseant {
448 1.7.6.1 perseant struct rk_gpio_softc * const sc = device_private(dev);
449 1.7.6.1 perseant struct rk_gpio_eint * const eint = ih;
450 1.7.6.1 perseant
451 1.7.6.1 perseant rk_intr_disable(sc, eint);
452 1.7.6.1 perseant }
453 1.7.6.1 perseant
454 1.7.6.1 perseant static bool
455 1.7.6.1 perseant rk_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
456 1.7.6.1 perseant {
457 1.7.6.1 perseant
458 1.7.6.1 perseant if (!specifier)
459 1.7.6.1 perseant return false;
460 1.7.6.1 perseant const u_int pin = be32toh(specifier[0]);
461 1.7.6.1 perseant
462 1.7.6.1 perseant if (pin < 0 || pin >= 32)
463 1.7.6.1 perseant return false;
464 1.7.6.1 perseant
465 1.7.6.1 perseant snprintf(buf, buflen, "GPIO %d", pin);
466 1.7.6.1 perseant
467 1.7.6.1 perseant return true;
468 1.7.6.1 perseant }
469 1.7.6.1 perseant
470 1.7.6.1 perseant static void
471 1.7.6.1 perseant rk_fdt_intr_mask(device_t dev, void *ih)
472 1.7.6.1 perseant {
473 1.7.6.1 perseant struct rk_gpio_softc * const sc = device_private(dev);
474 1.7.6.1 perseant void * const gpio = device_private(sc->sc_gpiodev);
475 1.7.6.1 perseant
476 1.7.6.1 perseant gpio_intr_mask(gpio, ih);
477 1.7.6.1 perseant }
478 1.7.6.1 perseant
479 1.7.6.1 perseant static void
480 1.7.6.1 perseant rk_fdt_intr_unmask(device_t dev, void *ih)
481 1.7.6.1 perseant {
482 1.7.6.1 perseant struct rk_gpio_softc * const sc = device_private(dev);
483 1.7.6.1 perseant void * const gpio = device_private(sc->sc_gpiodev);
484 1.7.6.1 perseant
485 1.7.6.1 perseant gpio_intr_unmask(gpio, ih);
486 1.7.6.1 perseant }
487 1.7.6.1 perseant
488 1.7.6.1 perseant
489 1.7.6.1 perseant static struct fdtbus_interrupt_controller_func rk_gpio_intrfuncs = {
490 1.7.6.1 perseant .establish = rk_fdt_intr_establish,
491 1.7.6.1 perseant .disestablish = rk_fdt_intr_disestablish,
492 1.7.6.1 perseant .intrstr = rk_fdt_intrstr,
493 1.7.6.1 perseant .mask = rk_fdt_intr_mask,
494 1.7.6.1 perseant .unmask = rk_fdt_intr_unmask
495 1.7.6.1 perseant };
496 1.7.6.1 perseant
497 1.7.6.1 perseant static void *
498 1.7.6.1 perseant rk_gpio_intr_establish(void *vsc, int pin, int ipl, int irqmode,
499 1.7.6.1 perseant int (*func)(void *), void *arg)
500 1.7.6.1 perseant {
501 1.7.6.1 perseant struct rk_gpio_softc * const sc = vsc;
502 1.7.6.1 perseant bool mpsafe = (irqmode & GPIO_INTR_MPSAFE) != 0;
503 1.7.6.1 perseant int type = irqmode & GPIO_INTR_MODE_MASK;
504 1.7.6.1 perseant uint32_t level, polarity;
505 1.7.6.1 perseant
506 1.7.6.1 perseant switch (type) {
507 1.7.6.1 perseant case GPIO_INTR_POS_EDGE:
508 1.7.6.1 perseant level = 1;
509 1.7.6.1 perseant polarity = 1;
510 1.7.6.1 perseant break;
511 1.7.6.1 perseant case GPIO_INTR_NEG_EDGE:
512 1.7.6.1 perseant level = 1;
513 1.7.6.1 perseant polarity = 0;
514 1.7.6.1 perseant break;
515 1.7.6.1 perseant case GPIO_INTR_HIGH_LEVEL:
516 1.7.6.1 perseant level = 0;
517 1.7.6.1 perseant polarity = 1;
518 1.7.6.1 perseant break;
519 1.7.6.1 perseant case GPIO_INTR_LOW_LEVEL:
520 1.7.6.1 perseant level = 0;
521 1.7.6.1 perseant polarity = 0;
522 1.7.6.1 perseant break;
523 1.7.6.1 perseant default:
524 1.7.6.1 perseant aprint_error_dev(sc->sc_dev, "%s: unsupported irq type 0x%x\n",
525 1.7.6.1 perseant __func__, type);
526 1.7.6.1 perseant return NULL;
527 1.7.6.1 perseant }
528 1.7.6.1 perseant
529 1.7.6.1 perseant return rk_intr_enable(sc, pin, level, polarity, mpsafe, func, arg);
530 1.7.6.1 perseant }
531 1.7.6.1 perseant
532 1.7.6.1 perseant static void
533 1.7.6.1 perseant rk_gpio_intr_disestablish(void *vsc, void *ih)
534 1.7.6.1 perseant {
535 1.7.6.1 perseant struct rk_gpio_softc * const sc = vsc;
536 1.7.6.1 perseant struct rk_gpio_eint * const eint = ih;
537 1.7.6.1 perseant
538 1.7.6.1 perseant rk_intr_disable(sc, eint);
539 1.7.6.1 perseant }
540 1.7.6.1 perseant
541 1.7.6.1 perseant static bool
542 1.7.6.1 perseant rk_gpio_intrstr(void *vsc, int pin, int irqmode, char *buf, size_t buflen)
543 1.7.6.1 perseant {
544 1.7.6.1 perseant
545 1.7.6.1 perseant if (pin < 0 || pin >= 32)
546 1.7.6.1 perseant return false;
547 1.7.6.1 perseant
548 1.7.6.1 perseant snprintf(buf, buflen, "GPIO %d", pin);
549 1.7.6.1 perseant
550 1.7.6.1 perseant return true;
551 1.7.6.1 perseant }
552 1.7.6.1 perseant
553 1.7.6.1 perseant static void
554 1.7.6.1 perseant rk_gpio_intr_mask(void *priv, void *ih)
555 1.7.6.1 perseant {
556 1.7.6.1 perseant struct rk_gpio_softc * const sc = priv;
557 1.7.6.1 perseant struct rk_gpio_eint * const eint = ih;
558 1.7.6.1 perseant uint32_t val;
559 1.7.6.1 perseant
560 1.7.6.1 perseant val = RD4(sc, GPIO_INTMASK_REG);
561 1.7.6.1 perseant val |= 1 << eint->eint_num;
562 1.7.6.1 perseant WR4(sc, GPIO_INTEN_REG, val);
563 1.7.6.1 perseant }
564 1.7.6.1 perseant
565 1.7.6.1 perseant static void
566 1.7.6.1 perseant rk_gpio_intr_unmask(void *priv, void *ih)
567 1.7.6.1 perseant {
568 1.7.6.1 perseant struct rk_gpio_softc * const sc = priv;
569 1.7.6.1 perseant struct rk_gpio_eint * const eint = ih;
570 1.7.6.1 perseant uint32_t val;
571 1.7.6.1 perseant
572 1.7.6.1 perseant val = RD4(sc, GPIO_INTMASK_REG);
573 1.7.6.1 perseant val &= ~(1 << eint->eint_num);
574 1.7.6.1 perseant WR4(sc, GPIO_INTEN_REG, val);
575 1.7.6.1 perseant }
576 1.7.6.1 perseant
577 1.1 jmcneill static void
578 1.1 jmcneill rk_gpio_attach_ports(struct rk_gpio_softc *sc)
579 1.1 jmcneill {
580 1.1 jmcneill struct gpiobus_attach_args gba;
581 1.1 jmcneill u_int pin;
582 1.1 jmcneill
583 1.1 jmcneill for (pin = 0; pin < __arraycount(sc->sc_pins); pin++) {
584 1.1 jmcneill sc->sc_pins[pin].pin_num = pin;
585 1.1 jmcneill sc->sc_pins[pin].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
586 1.1 jmcneill sc->sc_pins[pin].pin_state = rk_gpio_pin_read(sc, pin);
587 1.1 jmcneill }
588 1.1 jmcneill
589 1.1 jmcneill memset(&gba, 0, sizeof(gba));
590 1.7 tnn gba.gba_gc = &sc->sc_gp;
591 1.1 jmcneill gba.gba_pins = sc->sc_pins;
592 1.1 jmcneill gba.gba_npins = __arraycount(sc->sc_pins);
593 1.5 thorpej sc->sc_gpiodev = config_found(sc->sc_dev, &gba, NULL, CFARGS_NONE);
594 1.1 jmcneill }
595 1.1 jmcneill
596 1.1 jmcneill static int
597 1.1 jmcneill rk_gpio_match(device_t parent, cfdata_t cf, void *aux)
598 1.1 jmcneill {
599 1.1 jmcneill struct fdt_attach_args * const faa = aux;
600 1.1 jmcneill
601 1.3 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
602 1.1 jmcneill }
603 1.1 jmcneill
604 1.1 jmcneill static void
605 1.1 jmcneill rk_gpio_attach(device_t parent, device_t self, void *aux)
606 1.1 jmcneill {
607 1.1 jmcneill struct rk_gpio_softc * const sc = device_private(self);
608 1.7 tnn struct gpio_chipset_tag * const gp = &sc->sc_gp;
609 1.1 jmcneill struct fdt_attach_args * const faa = aux;
610 1.1 jmcneill const int phandle = faa->faa_phandle;
611 1.7.6.1 perseant char intrstr[128];
612 1.1 jmcneill struct clk *clk;
613 1.1 jmcneill bus_addr_t addr;
614 1.1 jmcneill bus_size_t size;
615 1.7 tnn uint32_t ver_id;
616 1.7 tnn int ver;
617 1.1 jmcneill
618 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
619 1.1 jmcneill aprint_error(": couldn't get registers\n");
620 1.1 jmcneill return;
621 1.1 jmcneill }
622 1.1 jmcneill
623 1.1 jmcneill if ((clk = fdtbus_clock_get_index(phandle, 0)) == NULL || clk_enable(clk) != 0) {
624 1.1 jmcneill aprint_error(": couldn't enable clock\n");
625 1.1 jmcneill return;
626 1.1 jmcneill }
627 1.1 jmcneill
628 1.1 jmcneill sc->sc_dev = self;
629 1.1 jmcneill sc->sc_bst = faa->faa_bst;
630 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
631 1.1 jmcneill aprint_error(": couldn't map registers\n");
632 1.1 jmcneill return;
633 1.1 jmcneill }
634 1.7 tnn
635 1.7 tnn gp->gp_cookie = sc;
636 1.7 tnn ver_id = RD4(sc, GPIO_VER_ID_REG);
637 1.7 tnn switch (ver_id) {
638 1.7 tnn case 0: /* VER_ID not implemented in v1 but reads back as 0 */
639 1.7 tnn ver = 1;
640 1.7 tnn gp->gp_pin_read = rk_gpio_pin_read;
641 1.7 tnn gp->gp_pin_write = rk_gpio_pin_write;
642 1.7 tnn gp->gp_pin_ctl = rk_gpio_pin_ctl;
643 1.7.6.1 perseant gp->gp_intr_establish = rk_gpio_intr_establish;
644 1.7.6.1 perseant gp->gp_intr_disestablish = rk_gpio_intr_disestablish;
645 1.7.6.1 perseant gp->gp_intr_str = rk_gpio_intrstr;
646 1.7.6.1 perseant gp->gp_intr_mask = rk_gpio_intr_mask;
647 1.7.6.1 perseant gp->gp_intr_unmask = rk_gpio_intr_unmask;
648 1.7 tnn break;
649 1.7 tnn case GPIO_VER_ID_GPIOV2:
650 1.7 tnn ver = 2;
651 1.7 tnn gp->gp_pin_read = rk_gpio_v2_pin_read;
652 1.7 tnn gp->gp_pin_write = rk_gpio_v2_pin_write;
653 1.7 tnn gp->gp_pin_ctl = rk_gpio_v2_pin_ctl;
654 1.7.6.1 perseant /* XXX */
655 1.7 tnn break;
656 1.7 tnn default:
657 1.7 tnn aprint_error(": unknown version 0x%08" PRIx32 "\n", ver_id);
658 1.7 tnn return;
659 1.7 tnn }
660 1.7 tnn
661 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
662 1.1 jmcneill
663 1.1 jmcneill aprint_naive("\n");
664 1.7 tnn aprint_normal(": GPIO v%d (%s)\n", ver, fdtbus_get_string(phandle, "name"));
665 1.1 jmcneill
666 1.1 jmcneill fdtbus_register_gpio_controller(self, phandle, &rk_gpio_funcs);
667 1.1 jmcneill
668 1.1 jmcneill rk_gpio_attach_ports(sc);
669 1.7.6.1 perseant
670 1.7.6.1 perseant WR4(sc, GPIO_INTEN_REG, 0);
671 1.7.6.1 perseant
672 1.7.6.1 perseant if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
673 1.7.6.1 perseant aprint_error_dev(self, "failed to decode interrupt\n");
674 1.7.6.1 perseant return;
675 1.7.6.1 perseant }
676 1.7.6.1 perseant sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM,
677 1.7.6.1 perseant FDT_INTR_MPSAFE, rk_gpio_intr, sc, device_xname(self));
678 1.7.6.1 perseant if (sc->sc_ih == NULL) {
679 1.7.6.1 perseant aprint_error_dev(self, "failed to establish interrupt on %s\n",
680 1.7.6.1 perseant intrstr);
681 1.7.6.1 perseant return;
682 1.7.6.1 perseant }
683 1.7.6.1 perseant aprint_normal_dev(self, "interrupting on %s\n", intrstr);
684 1.7.6.1 perseant fdtbus_register_interrupt_controller(self, phandle,
685 1.7.6.1 perseant &rk_gpio_intrfuncs);
686 1.1 jmcneill }
687