]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/allwinner/a10_gpio.c
Need to handle the !FDT case still too... I thought in r270025 we
[FreeBSD/FreeBSD.git] / sys / arm / allwinner / a10_gpio.c
1 /*-
2  * Copyright (c) 2013 Ganbold Tsagaankhuu <ganbold@freebsd.org>
3  * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
4  * Copyright (c) 2012 Luiz Otavio O Souza.
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 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/rman.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/gpio.h>
42
43 #include <machine/bus.h>
44 #include <machine/cpu.h>
45 #include <machine/cpufunc.h>
46 #include <machine/resource.h>
47 #include <machine/intr.h>
48
49 #include <dev/fdt/fdt_common.h>
50 #include <dev/gpio/gpiobusvar.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
53
54 #include "gpio_if.h"
55 #include "a10_gpio.h"
56
57 /*
58  * A10 have 9 banks of gpio.
59  * 32 pins per bank:
60  * PA0 - PA17 | PB0 - PB23 | PC0 - PC24
61  * PD0 - PD27 | PE0 - PE31 | PF0 - PF5
62  * PG0 - PG9 | PH0 - PH27 | PI0 - PI12
63  */
64
65 #define A10_GPIO_PINS           288
66 #define A10_GPIO_DEFAULT_CAPS   (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |     \
67     GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN)
68
69 #define A10_GPIO_NONE           0
70 #define A10_GPIO_PULLUP         1
71 #define A10_GPIO_PULLDOWN       2
72
73 #define A10_GPIO_INPUT          0
74 #define A10_GPIO_OUTPUT         1
75
76 struct a10_gpio_softc {
77         device_t                sc_dev;
78         device_t                sc_busdev;
79         struct mtx              sc_mtx;
80         struct resource *       sc_mem_res;
81         struct resource *       sc_irq_res;
82         bus_space_tag_t         sc_bst;
83         bus_space_handle_t      sc_bsh;
84         void *                  sc_intrhand;
85         int                     sc_gpio_npins;
86         struct gpio_pin         sc_gpio_pins[A10_GPIO_PINS];
87 };
88
89 #define A10_GPIO_LOCK(_sc)              mtx_lock(&_sc->sc_mtx)
90 #define A10_GPIO_UNLOCK(_sc)            mtx_unlock(&_sc->sc_mtx)
91 #define A10_GPIO_LOCK_ASSERT(_sc)       mtx_assert(&_sc->sc_mtx, MA_OWNED)
92
93 #define A10_GPIO_GP_CFG(_bank, _pin)    0x00 + ((_bank) * 0x24) + ((_pin)<<2)
94 #define A10_GPIO_GP_DAT(_bank)          0x10 + ((_bank) * 0x24)
95 #define A10_GPIO_GP_DRV(_bank, _pin)    0x14 + ((_bank) * 0x24) + ((_pin)<<2)
96 #define A10_GPIO_GP_PUL(_bank, _pin)    0x1c + ((_bank) * 0x24) + ((_pin)<<2)
97
98 #define A10_GPIO_GP_INT_CFG0            0x200
99 #define A10_GPIO_GP_INT_CFG1            0x204
100 #define A10_GPIO_GP_INT_CFG2            0x208
101 #define A10_GPIO_GP_INT_CFG3            0x20c
102
103 #define A10_GPIO_GP_INT_CTL             0x210
104 #define A10_GPIO_GP_INT_STA             0x214
105 #define A10_GPIO_GP_INT_DEB             0x218
106
107 static struct a10_gpio_softc *a10_gpio_sc;
108
109 #define A10_GPIO_WRITE(_sc, _off, _val)         \
110     bus_space_write_4(_sc->sc_bst, _sc->sc_bsh, _off, _val)
111 #define A10_GPIO_READ(_sc, _off)                \
112     bus_space_read_4(_sc->sc_bst, _sc->sc_bsh, _off)
113
114 static uint32_t
115 a10_gpio_get_function(struct a10_gpio_softc *sc, uint32_t pin)
116 {
117         uint32_t bank, func, offset;
118
119         bank = pin / 32;
120         pin = pin - 32 * bank;
121         func = pin >> 3;
122         offset = ((pin & 0x07) << 2);
123
124         A10_GPIO_LOCK(sc);
125         func = (A10_GPIO_READ(sc, A10_GPIO_GP_CFG(bank, func)) >> offset) & 7;
126         A10_GPIO_UNLOCK(sc);
127
128         return (func);
129 }
130
131 static uint32_t
132 a10_gpio_func_flag(uint32_t nfunc)
133 {
134
135         switch (nfunc) {
136         case A10_GPIO_INPUT:
137                 return (GPIO_PIN_INPUT);
138         case A10_GPIO_OUTPUT:
139                 return (GPIO_PIN_OUTPUT);
140         }
141         return (0);
142 }
143
144 static void
145 a10_gpio_set_function(struct a10_gpio_softc *sc, uint32_t pin, uint32_t f)
146 {
147         uint32_t bank, func, data, offset;
148
149         /* Must be called with lock held. */
150         A10_GPIO_LOCK_ASSERT(sc);
151
152         bank = pin / 32;
153         pin = pin - 32 * bank;
154         func = pin >> 3;
155         offset = ((pin & 0x07) << 2);
156
157         data = A10_GPIO_READ(sc, A10_GPIO_GP_CFG(bank, func));
158         data &= ~(7 << offset);
159         data |= (f << offset);
160         A10_GPIO_WRITE(sc, A10_GPIO_GP_CFG(bank, func), data);
161 }
162
163 static void
164 a10_gpio_set_pud(struct a10_gpio_softc *sc, uint32_t pin, uint32_t state)
165 {
166         uint32_t bank, offset, pull, val;
167
168         /* Must be called with lock held. */
169         A10_GPIO_LOCK_ASSERT(sc);
170
171         bank = pin / 32;
172         pin = pin - 32 * bank;
173         pull = pin >> 4;
174         offset = ((pin & 0x0f) << 1);
175
176         val = A10_GPIO_READ(sc, A10_GPIO_GP_PUL(bank, pull));
177         val &= ~(0x03 << offset);
178         val |= (state << offset);
179         A10_GPIO_WRITE(sc, A10_GPIO_GP_PUL(bank, pull), val);
180 }
181
182 static void
183 a10_gpio_pin_configure(struct a10_gpio_softc *sc, struct gpio_pin *pin,
184     unsigned int flags)
185 {
186
187         A10_GPIO_LOCK(sc);
188
189         /*
190          * Manage input/output.
191          */
192         if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
193                 pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
194                 if (flags & GPIO_PIN_OUTPUT) {
195                         pin->gp_flags |= GPIO_PIN_OUTPUT;
196                         a10_gpio_set_function(sc, pin->gp_pin,
197                             A10_GPIO_OUTPUT);
198                 } else {
199                         pin->gp_flags |= GPIO_PIN_INPUT;
200                         a10_gpio_set_function(sc, pin->gp_pin,
201                             A10_GPIO_INPUT);
202                 }
203         }
204
205         /* Manage Pull-up/pull-down. */
206         pin->gp_flags &= ~(GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN);
207         if (flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) {
208                 if (flags & GPIO_PIN_PULLUP) {
209                         pin->gp_flags |= GPIO_PIN_PULLUP;
210                         a10_gpio_set_pud(sc, pin->gp_pin, A10_GPIO_PULLUP);
211                 } else {
212                         pin->gp_flags |= GPIO_PIN_PULLDOWN;
213                         a10_gpio_set_pud(sc, pin->gp_pin, A10_GPIO_PULLDOWN);
214                 }
215         } else 
216                 a10_gpio_set_pud(sc, pin->gp_pin, A10_GPIO_NONE);
217
218         A10_GPIO_UNLOCK(sc);
219 }
220
221 static device_t
222 a10_gpio_get_bus(device_t dev)
223 {
224         struct a10_gpio_softc *sc;
225
226         sc = device_get_softc(dev);
227
228         return (sc->sc_busdev);
229 }
230
231 static int
232 a10_gpio_pin_max(device_t dev, int *maxpin)
233 {
234
235         *maxpin = A10_GPIO_PINS - 1;
236         return (0);
237 }
238
239 static int
240 a10_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
241 {
242         struct a10_gpio_softc *sc = device_get_softc(dev);
243         int i;
244
245         for (i = 0; i < sc->sc_gpio_npins; i++) {
246                 if (sc->sc_gpio_pins[i].gp_pin == pin)
247                         break;
248         }
249
250         if (i >= sc->sc_gpio_npins)
251                 return (EINVAL);
252
253         A10_GPIO_LOCK(sc);
254         *caps = sc->sc_gpio_pins[i].gp_caps;
255         A10_GPIO_UNLOCK(sc);
256
257         return (0);
258 }
259
260 static int
261 a10_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
262 {
263         struct a10_gpio_softc *sc = device_get_softc(dev);
264         int i;
265
266         for (i = 0; i < sc->sc_gpio_npins; i++) {
267                 if (sc->sc_gpio_pins[i].gp_pin == pin)
268                         break;
269         }
270
271         if (i >= sc->sc_gpio_npins)
272                 return (EINVAL);
273
274         A10_GPIO_LOCK(sc);
275         *flags = sc->sc_gpio_pins[i].gp_flags;
276         A10_GPIO_UNLOCK(sc);
277
278         return (0);
279 }
280
281 static int
282 a10_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
283 {
284         struct a10_gpio_softc *sc = device_get_softc(dev);
285         int i;
286
287         for (i = 0; i < sc->sc_gpio_npins; i++) {
288                 if (sc->sc_gpio_pins[i].gp_pin == pin)
289                         break;
290         }
291
292         if (i >= sc->sc_gpio_npins)
293                 return (EINVAL);
294
295         A10_GPIO_LOCK(sc);
296         memcpy(name, sc->sc_gpio_pins[i].gp_name, GPIOMAXNAME);
297         A10_GPIO_UNLOCK(sc);
298
299         return (0);
300 }
301
302 static int
303 a10_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
304 {
305         struct a10_gpio_softc *sc = device_get_softc(dev);
306         int i;
307
308         for (i = 0; i < sc->sc_gpio_npins; i++) {
309                 if (sc->sc_gpio_pins[i].gp_pin == pin)
310                         break;
311         }
312
313         if (i >= sc->sc_gpio_npins)
314                 return (EINVAL);
315
316         a10_gpio_pin_configure(sc, &sc->sc_gpio_pins[i], flags);
317
318         return (0);
319 }
320
321 static int
322 a10_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
323 {
324         struct a10_gpio_softc *sc = device_get_softc(dev);
325         uint32_t bank, offset, data;
326         int i;
327
328         for (i = 0; i < sc->sc_gpio_npins; i++) {
329                 if (sc->sc_gpio_pins[i].gp_pin == pin)
330                         break;
331         }
332
333         if (i >= sc->sc_gpio_npins)
334                 return (EINVAL);
335
336         bank = pin / 32;
337         pin = pin - 32 * bank;
338         offset = pin & 0x1f;
339
340         A10_GPIO_LOCK(sc);
341         data = A10_GPIO_READ(sc, A10_GPIO_GP_DAT(bank));
342         if (value)
343                 data |= (1 << offset);
344         else
345                 data &= ~(1 << offset);
346         A10_GPIO_WRITE(sc, A10_GPIO_GP_DAT(bank), data);
347         A10_GPIO_UNLOCK(sc);
348
349         return (0);
350 }
351
352 static int
353 a10_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
354 {
355         struct a10_gpio_softc *sc = device_get_softc(dev);
356         uint32_t bank, offset, reg_data;
357         int i;
358
359         for (i = 0; i < sc->sc_gpio_npins; i++) {
360                 if (sc->sc_gpio_pins[i].gp_pin == pin)
361                         break;
362         }
363
364         if (i >= sc->sc_gpio_npins)
365                 return (EINVAL);
366
367         bank = pin / 32;
368         pin = pin - 32 * bank;
369         offset = pin & 0x1f;
370
371         A10_GPIO_LOCK(sc);
372         reg_data = A10_GPIO_READ(sc, A10_GPIO_GP_DAT(bank));
373         A10_GPIO_UNLOCK(sc);
374         *val = (reg_data & (1 << offset)) ? 1 : 0;
375
376         return (0);
377 }
378
379 static int
380 a10_gpio_pin_toggle(device_t dev, uint32_t pin)
381 {
382         struct a10_gpio_softc *sc = device_get_softc(dev);
383         uint32_t bank, data, offset;
384         int i;
385
386         for (i = 0; i < sc->sc_gpio_npins; i++) {
387                 if (sc->sc_gpio_pins[i].gp_pin == pin)
388                         break;
389         }
390
391         if (i >= sc->sc_gpio_npins)
392                 return (EINVAL);
393
394         bank = pin / 32;
395         pin = pin - 32 * bank;
396         offset = pin & 0x1f;
397
398         A10_GPIO_LOCK(sc);
399         data = A10_GPIO_READ(sc, A10_GPIO_GP_DAT(bank));
400         if (data & (1 << offset))
401                 data &= ~(1 << offset);
402         else
403                 data |= (1 << offset);
404         A10_GPIO_WRITE(sc, A10_GPIO_GP_DAT(bank), data);
405         A10_GPIO_UNLOCK(sc);
406
407         return (0);
408 }
409
410 static int
411 a10_gpio_probe(device_t dev)
412 {
413
414         if (!ofw_bus_status_okay(dev))
415                 return (ENXIO);
416
417         if (!ofw_bus_is_compatible(dev, "allwinner,sun4i-gpio"))
418                 return (ENXIO);
419
420         device_set_desc(dev, "Allwinner GPIO controller");
421         return (BUS_PROBE_DEFAULT);
422 }
423
424 static int
425 a10_gpio_attach(device_t dev)
426 {
427         struct a10_gpio_softc *sc = device_get_softc(dev);
428         uint32_t func;
429         int i, rid;
430         phandle_t gpio;
431
432         sc->sc_dev = dev;
433
434         mtx_init(&sc->sc_mtx, "a10 gpio", "gpio", MTX_DEF);
435
436         rid = 0;
437         sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
438             RF_ACTIVE);
439         if (!sc->sc_mem_res) {
440                 device_printf(dev, "cannot allocate memory window\n");
441                 goto fail;
442         }
443
444         sc->sc_bst = rman_get_bustag(sc->sc_mem_res);
445         sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);
446
447         rid = 0;
448         sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
449             RF_ACTIVE);
450         if (!sc->sc_irq_res) {
451                 device_printf(dev, "cannot allocate interrupt\n");
452                 goto fail;
453         }
454
455         /* Find our node. */
456         gpio = ofw_bus_get_node(sc->sc_dev);
457
458         if (!OF_hasprop(gpio, "gpio-controller"))
459                 /* Node is not a GPIO controller. */
460                 goto fail;
461
462         /* Initialize the software controlled pins. */
463         for (i = 0; i < A10_GPIO_PINS; i++) {
464                 snprintf(sc->sc_gpio_pins[i].gp_name, GPIOMAXNAME,
465                     "pin %d", i);
466                 func = a10_gpio_get_function(sc, i);
467                 sc->sc_gpio_pins[i].gp_pin = i;
468                 sc->sc_gpio_pins[i].gp_caps = A10_GPIO_DEFAULT_CAPS;
469                 sc->sc_gpio_pins[i].gp_flags = a10_gpio_func_flag(func);
470         }
471         sc->sc_gpio_npins = i;
472         a10_gpio_sc = sc;
473         sc->sc_busdev = gpiobus_attach_bus(dev);
474         if (sc->sc_busdev == NULL)
475                 goto fail;
476
477         return (0);
478
479 fail:
480         if (sc->sc_irq_res)
481                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
482         if (sc->sc_mem_res)
483                 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
484         mtx_destroy(&sc->sc_mtx);
485
486         return (ENXIO);
487 }
488
489 static int
490 a10_gpio_detach(device_t dev)
491 {
492
493         return (EBUSY);
494 }
495
496 static device_method_t a10_gpio_methods[] = {
497         /* Device interface */
498         DEVMETHOD(device_probe,         a10_gpio_probe),
499         DEVMETHOD(device_attach,        a10_gpio_attach),
500         DEVMETHOD(device_detach,        a10_gpio_detach),
501
502         /* GPIO protocol */
503         DEVMETHOD(gpio_get_bus,         a10_gpio_get_bus),
504         DEVMETHOD(gpio_pin_max,         a10_gpio_pin_max),
505         DEVMETHOD(gpio_pin_getname,     a10_gpio_pin_getname),
506         DEVMETHOD(gpio_pin_getflags,    a10_gpio_pin_getflags),
507         DEVMETHOD(gpio_pin_getcaps,     a10_gpio_pin_getcaps),
508         DEVMETHOD(gpio_pin_setflags,    a10_gpio_pin_setflags),
509         DEVMETHOD(gpio_pin_get,         a10_gpio_pin_get),
510         DEVMETHOD(gpio_pin_set,         a10_gpio_pin_set),
511         DEVMETHOD(gpio_pin_toggle,      a10_gpio_pin_toggle),
512
513         DEVMETHOD_END
514 };
515
516 static devclass_t a10_gpio_devclass;
517
518 static driver_t a10_gpio_driver = {
519         "gpio",
520         a10_gpio_methods,
521         sizeof(struct a10_gpio_softc),
522 };
523
524 DRIVER_MODULE(a10_gpio, simplebus, a10_gpio_driver, a10_gpio_devclass, 0, 0);
525
526 int
527 a10_emac_gpio_config(uint32_t pin)
528 {
529         struct a10_gpio_softc *sc = a10_gpio_sc;
530
531         if (sc == NULL)
532                 return (ENXIO);
533
534         /* Configure pin mux settings for MII. */
535         A10_GPIO_LOCK(sc);
536         a10_gpio_set_function(sc, pin, A10_GPIO_PULLDOWN);
537         A10_GPIO_UNLOCK(sc);
538
539         return (0);
540 }