]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/rockchip/rk_gpio.c
Upgrade Unbound to 1.7.0. More to follow.
[FreeBSD/FreeBSD.git] / sys / arm64 / rockchip / rk_gpio.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
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 AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, 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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/rman.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/gpio.h>
43
44 #include <machine/bus.h>
45 #include <machine/resource.h>
46 #include <machine/intr.h>
47
48 #include <dev/gpio/gpiobusvar.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 #include <dev/extres/clk/clk.h>
52
53 #include "opt_soc.h"
54
55 #include "gpio_if.h"
56
57 #define RK_GPIO_SWPORTA_DR      0x00    /* Data register */
58 #define RK_GPIO_SWPORTA_DDR     0x04    /* Data direction register */
59
60 #define RK_GPIO_INTEN           0x30    /* Interrupt enable register */
61 #define RK_GPIO_INTMASK         0x34    /* Interrupt mask register */
62 #define RK_GPIO_INTTYPE_LEVEL   0x38    /* Interrupt level register */
63 #define RK_GPIO_INT_POLARITY    0x3C    /* Interrupt polarity register */
64 #define RK_GPIO_INT_STATUS      0x40    /* Interrupt status register */
65 #define RK_GPIO_INT_RAWSTATUS   0x44    /* Raw Interrupt status register */
66
67 #define RK_GPIO_DEBOUNCE        0x48    /* Debounce enable register */
68
69 #define RK_GPIO_PORTA_EOI       0x4C    /* Clear interrupt register */
70 #define RK_GPIO_EXT_PORTA       0x50    /* External port register */
71
72 #define RK_GPIO_LS_SYNC         0x60    /* Level sensitive syncronization enable register */
73
74 struct rk_gpio_softc {
75         device_t                sc_dev;
76         device_t                sc_busdev;
77         struct mtx              sc_mtx;
78         struct resource         *sc_res[2];
79         bus_space_tag_t         sc_bst;
80         bus_space_handle_t      sc_bsh;
81         clk_t                   clk;
82 };
83
84 static struct ofw_compat_data compat_data[] = {
85         {"rockchip,gpio-bank", 1},
86         {NULL,             0}
87 };
88
89 static struct resource_spec rk_gpio_spec[] = {
90         { SYS_RES_MEMORY,       0,      RF_ACTIVE },
91         { SYS_RES_IRQ,          0,      RF_ACTIVE },
92         { -1, 0 }
93 };
94
95 static int rk_gpio_detach(device_t dev);
96
97 #define RK_GPIO_LOCK(_sc)               mtx_lock_spin(&(_sc)->sc_mtx)
98 #define RK_GPIO_UNLOCK(_sc)             mtx_unlock_spin(&(_sc)->sc_mtx)
99 #define RK_GPIO_LOCK_ASSERT(_sc)        mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
100
101 #define RK_GPIO_WRITE(_sc, _off, _val)          \
102     bus_space_write_4(_sc->sc_bst, _sc->sc_bsh, _off, _val)
103 #define RK_GPIO_READ(_sc, _off)         \
104     bus_space_read_4(_sc->sc_bst, _sc->sc_bsh, _off)
105
106 static int
107 rk_gpio_probe(device_t dev)
108 {
109
110         if (!ofw_bus_status_okay(dev))
111                 return (ENXIO);
112
113         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
114                 return (ENXIO);
115
116         device_set_desc(dev, "RockChip GPIO Bank controller");
117         return (BUS_PROBE_DEFAULT);
118 }
119
120 static int
121 rk_gpio_attach(device_t dev)
122 {
123         struct rk_gpio_softc *sc;
124         phandle_t node;
125         int err;
126
127         sc = device_get_softc(dev);
128         sc->sc_dev = dev;
129
130         node = ofw_bus_get_node(sc->sc_dev);
131         if (!OF_hasprop(node, "gpio-controller"))
132                 return (ENXIO);
133
134         mtx_init(&sc->sc_mtx, "rk gpio", "gpio", MTX_SPIN);
135
136         if (bus_alloc_resources(dev, rk_gpio_spec, sc->sc_res)) {
137                 device_printf(dev, "could not allocate resources\n");
138                 bus_release_resources(dev, rk_gpio_spec, sc->sc_res);
139                 mtx_destroy(&sc->sc_mtx);
140                 return (ENXIO);
141         }
142
143         sc->sc_bst = rman_get_bustag(sc->sc_res[0]);
144         sc->sc_bsh = rman_get_bushandle(sc->sc_res[0]);
145
146         if (clk_get_by_ofw_index(dev, 0, 0, &sc->clk) != 0) {
147                 device_printf(dev, "Cannot get clock\n");
148                 rk_gpio_detach(dev);
149                 return (ENXIO);
150         }
151         err = clk_enable(sc->clk);
152         if (err != 0) {
153                 device_printf(dev, "Could not enable clock %s\n",
154                     clk_get_name(sc->clk));
155                 rk_gpio_detach(dev);
156                 return (ENXIO);
157         }
158
159         sc->sc_busdev = gpiobus_attach_bus(dev);
160         if (sc->sc_busdev == NULL) {
161                 rk_gpio_detach(dev);
162                 return (ENXIO);
163         }
164
165         return (0);
166 }
167
168 static int
169 rk_gpio_detach(device_t dev)
170 {
171         struct rk_gpio_softc *sc;
172
173         sc = device_get_softc(dev);
174
175         if (sc->sc_busdev)
176                 gpiobus_detach_bus(dev);
177         bus_release_resources(dev, rk_gpio_spec, sc->sc_res);
178         mtx_destroy(&sc->sc_mtx);
179         clk_disable(sc->clk);
180
181         return(0);
182 }
183
184 static device_t
185 rk_gpio_get_bus(device_t dev)
186 {
187         struct rk_gpio_softc *sc;
188
189         sc = device_get_softc(dev);
190
191         return (sc->sc_busdev);
192 }
193
194 static int
195 rk_gpio_pin_max(device_t dev, int *maxpin)
196 {
197
198         /* Each bank have always 32 pins */
199         *maxpin = 32;
200         return (0);
201 }
202
203 static int
204 rk_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
205 {
206         struct rk_gpio_softc *sc;
207
208         sc = device_get_softc(dev);
209
210         if (pin >= 32)
211                 return (EINVAL);
212
213         RK_GPIO_LOCK(sc);
214         snprintf(name, GPIOMAXNAME, "gpio%d", pin);
215         RK_GPIO_UNLOCK(sc);
216
217         return (0);
218 }
219
220 static int
221 rk_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
222 {
223         struct rk_gpio_softc *sc;
224         uint32_t reg;
225
226         sc = device_get_softc(dev);
227
228         RK_GPIO_LOCK(sc);
229         reg = RK_GPIO_READ(sc, RK_GPIO_SWPORTA_DDR);
230         RK_GPIO_UNLOCK(sc);
231
232         if (reg & (1 << pin))
233                 *flags = GPIO_PIN_OUTPUT;
234         else
235                 *flags = GPIO_PIN_INPUT;
236
237         return (0);
238 }
239
240 static int
241 rk_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
242 {
243
244         /* Caps are managed by the pinctrl device */
245         *caps = 0;
246         return (0);
247 }
248
249 static int
250 rk_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
251 {
252         struct rk_gpio_softc *sc;
253         uint32_t reg;
254
255         sc = device_get_softc(dev);
256
257         RK_GPIO_LOCK(sc);
258
259         reg = RK_GPIO_READ(sc, RK_GPIO_SWPORTA_DDR);
260         if (flags & GPIO_PIN_INPUT)
261                 reg &= ~(1 << pin);
262         else if (flags & GPIO_PIN_OUTPUT)
263                 reg |= (1 << pin);
264
265         RK_GPIO_WRITE(sc, RK_GPIO_SWPORTA_DDR, reg);
266         RK_GPIO_UNLOCK(sc);
267
268         return (0);
269 }
270
271 static int
272 rk_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
273 {
274         struct rk_gpio_softc *sc;
275         uint32_t reg;
276
277         sc = device_get_softc(dev);
278
279         RK_GPIO_LOCK(sc);
280         reg = RK_GPIO_READ(sc, RK_GPIO_SWPORTA_DR);
281         RK_GPIO_UNLOCK(sc);
282
283         *val = reg & (1 << pin) ? 1 : 0;
284
285         return (0);
286 }
287
288 static int
289 rk_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
290 {
291         struct rk_gpio_softc *sc;
292         uint32_t reg;
293
294         sc = device_get_softc(dev);
295
296         RK_GPIO_LOCK(sc);
297         reg = RK_GPIO_READ(sc, RK_GPIO_SWPORTA_DR);
298         if (value)
299                 reg |= (1 << pin);
300         else
301                 reg &= ~(1 << pin);
302         RK_GPIO_WRITE(sc, RK_GPIO_SWPORTA_DR, reg);
303         RK_GPIO_UNLOCK(sc);
304
305         return (0);
306 }
307
308 static int
309 rk_gpio_pin_toggle(device_t dev, uint32_t pin)
310 {
311         struct rk_gpio_softc *sc;
312         uint32_t reg;
313
314         sc = device_get_softc(dev);
315
316         RK_GPIO_LOCK(sc);
317         reg = RK_GPIO_READ(sc, RK_GPIO_SWPORTA_DR);
318         if (reg & (1 << pin))
319                 reg &= ~(1 << pin);
320         else
321                 reg |= (1 << pin);
322         RK_GPIO_WRITE(sc, RK_GPIO_SWPORTA_DR, reg);
323         RK_GPIO_UNLOCK(sc);
324
325         return (0);
326 }
327
328 static int
329 rk_gpio_pin_access_32(device_t dev, uint32_t first_pin, uint32_t clear_pins,
330     uint32_t change_pins, uint32_t *orig_pins)
331 {
332         struct rk_gpio_softc *sc;
333         uint32_t reg;
334
335         sc = device_get_softc(dev);
336
337         RK_GPIO_LOCK(sc);
338         reg = RK_GPIO_READ(sc, RK_GPIO_SWPORTA_DR);
339         if (orig_pins)
340                 *orig_pins = reg;
341
342         if ((clear_pins | change_pins) != 0) {
343                 reg = (reg & ~clear_pins) ^ change_pins;
344                 RK_GPIO_WRITE(sc, RK_GPIO_SWPORTA_DR, reg);
345         }
346         RK_GPIO_UNLOCK(sc);
347
348         return (0);
349 }
350
351 static int
352 rk_gpio_pin_config_32(device_t dev, uint32_t first_pin, uint32_t num_pins,
353     uint32_t *pin_flags)
354 {
355         struct rk_gpio_softc *sc;
356         uint32_t reg, set, mask, flags;
357         int i;
358
359         sc = device_get_softc(dev);
360
361         if (first_pin != 0 || num_pins > 32)
362                 return (EINVAL);
363
364         set = 0;
365         mask = 0;
366         for (i = 0; i < num_pins; i++) {
367                 mask = (mask << 1) | 1;
368                 flags = pin_flags[i];
369                 if (flags & GPIO_PIN_INPUT) {
370                         set &= ~(1 << i);
371                 } else if (flags & GPIO_PIN_OUTPUT) {
372                         set |= (1 << i);
373                 }
374         }
375
376         RK_GPIO_LOCK(sc);
377         reg = RK_GPIO_READ(sc, RK_GPIO_SWPORTA_DDR);
378         reg &= ~mask;
379         reg |= set;
380         RK_GPIO_WRITE(sc, RK_GPIO_SWPORTA_DDR, reg);
381         RK_GPIO_UNLOCK(sc);
382
383         return (0);
384 }
385
386 static int
387 rk_gpio_map_gpios(device_t bus, phandle_t dev, phandle_t gparent, int gcells,
388     pcell_t *gpios, uint32_t *pin, uint32_t *flags)
389 {
390
391         /* The gpios are mapped as <gpio-phandle pin flags> */
392         *pin = gpios[1];
393         *flags = gpios[2];
394         return (0);
395 }
396
397 static device_method_t rk_gpio_methods[] = {
398         /* Device interface */
399         DEVMETHOD(device_probe,         rk_gpio_probe),
400         DEVMETHOD(device_attach,        rk_gpio_attach),
401         DEVMETHOD(device_detach,        rk_gpio_detach),
402
403         /* GPIO protocol */
404         DEVMETHOD(gpio_get_bus,         rk_gpio_get_bus),
405         DEVMETHOD(gpio_pin_max,         rk_gpio_pin_max),
406         DEVMETHOD(gpio_pin_getname,     rk_gpio_pin_getname),
407         DEVMETHOD(gpio_pin_getflags,    rk_gpio_pin_getflags),
408         DEVMETHOD(gpio_pin_getcaps,     rk_gpio_pin_getcaps),
409         DEVMETHOD(gpio_pin_setflags,    rk_gpio_pin_setflags),
410         DEVMETHOD(gpio_pin_get,         rk_gpio_pin_get),
411         DEVMETHOD(gpio_pin_set,         rk_gpio_pin_set),
412         DEVMETHOD(gpio_pin_toggle,      rk_gpio_pin_toggle),
413         DEVMETHOD(gpio_pin_access_32,   rk_gpio_pin_access_32),
414         DEVMETHOD(gpio_pin_config_32,   rk_gpio_pin_config_32),
415         DEVMETHOD(gpio_map_gpios,       rk_gpio_map_gpios),
416
417         DEVMETHOD_END
418 };
419
420 static driver_t rk_gpio_driver = {
421         "gpio",
422         rk_gpio_methods,
423         sizeof(struct rk_gpio_softc),
424 };
425
426 static devclass_t rk_gpio_devclass;
427
428 EARLY_DRIVER_MODULE(rk_gpio, simplebus, rk_gpio_driver,
429     rk_gpio_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);