]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/lpc/lpc_gpio.c
Merge ACPICA 20170831.
[FreeBSD/FreeBSD.git] / sys / arm / lpc / lpc_gpio.c
1 /*-
2  * Copyright (c) 2011 Jakub Wojciech Klama <jceel@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27
28 /*
29  * GPIO on LPC32x0 consist of 4 ports:
30  * - Port0 with 8 input/output pins
31  * - Port1 with 24 input/output pins
32  * - Port2 with 13 input/output pins
33  * - Port3 with:
34  *   - 26 input pins (GPI_00..GPI_09 + GPI_15..GPI_23 + GPI_25 + GPI_27..GPI_28)
35  *   - 24 output pins (GPO_00..GPO_23)
36  *   - 6 input/output pins (GPIO_00..GPIO_05)
37  *
38  * Pins are mapped to logical pin number as follows:
39  * [0..9] -> GPI_00..GPI_09             (port 3)
40  * [10..18] -> GPI_15..GPI_23           (port 3)
41  * [19] -> GPI_25                       (port 3)
42  * [20..21] -> GPI_27..GPI_28           (port 3)
43  * [22..45] -> GPO_00..GPO_23           (port 3)
44  * [46..51] -> GPIO_00..GPIO_05         (port 3)
45  * [52..64] -> P2.0..P2.12              (port 2)
46  * [65..88] -> P1.0..P1.23              (port 1)
47  * [89..96] -> P0.0..P0.7               (port 0)
48  *
49  */
50
51
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/bio.h>
58 #include <sys/bus.h>
59 #include <sys/conf.h>
60 #include <sys/endian.h>
61 #include <sys/kernel.h>
62 #include <sys/kthread.h>
63 #include <sys/lock.h>
64 #include <sys/malloc.h>
65 #include <sys/module.h>
66 #include <sys/mutex.h>
67 #include <sys/queue.h>
68 #include <sys/resource.h>
69 #include <sys/rman.h>
70 #include <sys/time.h>
71 #include <sys/timetc.h>
72 #include <sys/watchdog.h>
73 #include <sys/gpio.h>
74
75 #include <machine/bus.h>
76 #include <machine/resource.h>
77 #include <machine/intr.h>
78 #include <machine/fdt.h>
79
80 #include <dev/gpio/gpiobusvar.h>
81 #include <dev/ofw/ofw_bus.h>
82 #include <dev/ofw/ofw_bus_subr.h>
83
84 #include <arm/lpc/lpcreg.h>
85 #include <arm/lpc/lpcvar.h>
86
87 #include "gpio_if.h"
88
89 struct lpc_gpio_softc
90 {
91         device_t                lg_dev;
92         device_t                lg_busdev;
93         struct resource *       lg_res;
94         bus_space_tag_t         lg_bst;
95         bus_space_handle_t      lg_bsh;
96 };
97
98 struct lpc_gpio_pinmap
99 {
100         int                     lp_start_idx;
101         int                     lp_pin_count;
102         int                     lp_port;
103         int                     lp_start_bit;
104         int                     lp_flags;
105 };
106
107 static const struct lpc_gpio_pinmap lpc_gpio_pins[] = {
108         { 0,    10,     3,      0,      GPIO_PIN_INPUT },
109         { 10,   9,      3,      15,     GPIO_PIN_INPUT },
110         { 19,   1,      3,      25,     GPIO_PIN_INPUT },
111         { 20,   2,      3,      27,     GPIO_PIN_INPUT },
112         { 22,   24,     3,      0,      GPIO_PIN_OUTPUT },
113         /*
114          * -1 below is to mark special case for Port3 GPIO pins, as they
115          * have other bits in Port 3 registers as inputs and as outputs
116          */
117         { 46,   6,      3,      -1,     GPIO_PIN_INPUT | GPIO_PIN_OUTPUT },
118         { 52,   13,     2,      0,      GPIO_PIN_INPUT | GPIO_PIN_OUTPUT },
119         { 65,   24,     1,      0,      GPIO_PIN_INPUT | GPIO_PIN_OUTPUT },
120         { 89,   8,      0,      0,      GPIO_PIN_INPUT | GPIO_PIN_OUTPUT },
121         { -1,   -1,     -1,     -1,     -1 },
122 };
123
124 #define LPC_GPIO_NPINS                          \
125     (LPC_GPIO_P0_COUNT + LPC_GPIO_P1_COUNT +    \
126     LPC_GPIO_P2_COUNT + LPC_GPIO_P3_COUNT)
127
128 #define LPC_GPIO_PIN_IDX(_map, _idx)    \
129     (_idx - _map->lp_start_idx)
130
131 #define LPC_GPIO_PIN_BIT(_map, _idx)    \
132     (_map->lp_start_bit + LPC_GPIO_PIN_IDX(_map, _idx))
133
134 static int lpc_gpio_probe(device_t);
135 static int lpc_gpio_attach(device_t);
136 static int lpc_gpio_detach(device_t);
137
138 static device_t lpc_gpio_get_bus(device_t);
139 static int lpc_gpio_pin_max(device_t, int *);
140 static int lpc_gpio_pin_getcaps(device_t, uint32_t, uint32_t *);
141 static int lpc_gpio_pin_getflags(device_t, uint32_t, uint32_t *);
142 static int lpc_gpio_pin_setflags(device_t, uint32_t, uint32_t);
143 static int lpc_gpio_pin_getname(device_t, uint32_t, char *);
144 static int lpc_gpio_pin_get(device_t, uint32_t, uint32_t *);
145 static int lpc_gpio_pin_set(device_t, uint32_t, uint32_t);
146 static int lpc_gpio_pin_toggle(device_t, uint32_t);
147
148 static const struct lpc_gpio_pinmap *lpc_gpio_get_pinmap(int);
149
150 static struct lpc_gpio_softc *lpc_gpio_sc = NULL;
151
152 #define lpc_gpio_read_4(_sc, _reg) \
153     bus_space_read_4(_sc->lg_bst, _sc->lg_bsh, _reg)
154 #define lpc_gpio_write_4(_sc, _reg, _val) \
155     bus_space_write_4(_sc->lg_bst, _sc->lg_bsh, _reg, _val)
156 #define lpc_gpio_get_4(_sc, _test, _reg1, _reg2) \
157     lpc_gpio_read_4(_sc, ((_test) ? _reg1 : _reg2))
158 #define lpc_gpio_set_4(_sc, _test, _reg1, _reg2, _val) \
159     lpc_gpio_write_4(_sc, ((_test) ? _reg1 : _reg2), _val)
160
161 static int
162 lpc_gpio_probe(device_t dev)
163 {
164
165         if (!ofw_bus_status_okay(dev))
166                 return (ENXIO);
167
168         if (!ofw_bus_is_compatible(dev, "lpc,gpio"))
169                 return (ENXIO);
170
171         device_set_desc(dev, "LPC32x0 GPIO");
172         return (BUS_PROBE_DEFAULT);
173 }
174
175 static int
176 lpc_gpio_attach(device_t dev)
177 {
178         struct lpc_gpio_softc *sc = device_get_softc(dev);
179         int rid;
180
181         sc->lg_dev = dev;
182
183         rid = 0;
184         sc->lg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
185             RF_ACTIVE);
186         if (!sc->lg_res) {
187                 device_printf(dev, "cannot allocate memory window\n");
188                 return (ENXIO);
189         }
190
191         sc->lg_bst = rman_get_bustag(sc->lg_res);
192         sc->lg_bsh = rman_get_bushandle(sc->lg_res);
193
194         lpc_gpio_sc = sc;
195
196         sc->lg_busdev = gpiobus_attach_bus(dev);
197         if (sc->lg_busdev == NULL) {
198                 bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->lg_res);
199                 return (ENXIO);
200         }
201
202         return (0);
203 }
204
205 static int
206 lpc_gpio_detach(device_t dev)
207 {
208         return (EBUSY);
209 }
210
211 static device_t
212 lpc_gpio_get_bus(device_t dev)
213 {
214         struct lpc_gpio_softc *sc;
215
216         sc = device_get_softc(dev);
217
218         return (sc->lg_busdev);
219 }
220
221 static int
222 lpc_gpio_pin_max(device_t dev, int *npins)
223 {
224         *npins = LPC_GPIO_NPINS - 1;
225         return (0);
226 }
227
228 static int
229 lpc_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
230 {
231         const struct lpc_gpio_pinmap *map;
232
233         if (pin > LPC_GPIO_NPINS)
234                 return (ENODEV);
235
236         map = lpc_gpio_get_pinmap(pin);
237
238         *caps = map->lp_flags;
239         return (0);
240 }
241
242 static int
243 lpc_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
244 {
245         struct lpc_gpio_softc *sc = device_get_softc(dev);
246         const struct lpc_gpio_pinmap *map;
247         uint32_t state;
248         int dir;
249
250         if (pin > LPC_GPIO_NPINS)
251                 return (ENODEV);
252
253         map = lpc_gpio_get_pinmap(pin);
254
255         /* Check whether it's bidirectional pin */
256         if ((map->lp_flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) != 
257             (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) {
258                 *flags = map->lp_flags;
259                 return (0);
260         }
261
262         switch (map->lp_port) {
263         case 0:
264                 state = lpc_gpio_read_4(sc, LPC_GPIO_P0_DIR_STATE);
265                 dir = (state & (1 << LPC_GPIO_PIN_BIT(map, pin)));
266                 break;
267         case 1:
268                 state = lpc_gpio_read_4(sc, LPC_GPIO_P1_DIR_STATE);
269                 dir = (state & (1 << LPC_GPIO_PIN_BIT(map, pin)));
270                 break;
271         case 2:
272                 state = lpc_gpio_read_4(sc, LPC_GPIO_P2_DIR_STATE);
273                 dir = (state & (1 << LPC_GPIO_PIN_BIT(map, pin)));
274                 break;
275         case 3:
276                 state = lpc_gpio_read_4(sc, LPC_GPIO_P2_DIR_STATE);
277                 dir = (state & (1 << (25 + LPC_GPIO_PIN_IDX(map, pin))));
278                 break;
279         default:
280                 panic("unknown GPIO port");
281         }
282
283         *flags = dir ? GPIO_PIN_OUTPUT : GPIO_PIN_INPUT;
284
285         return (0);
286 }
287
288 static int
289 lpc_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
290 {
291         struct lpc_gpio_softc *sc = device_get_softc(dev);
292         const struct lpc_gpio_pinmap *map;
293         uint32_t dir, state;
294
295         if (pin > LPC_GPIO_NPINS)
296                 return (ENODEV);
297
298         map = lpc_gpio_get_pinmap(pin);
299
300         /* Check whether it's bidirectional pin */
301         if ((map->lp_flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) != 
302             (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT))
303                 return (ENOTSUP);
304         
305         if (flags & GPIO_PIN_INPUT)
306                 dir = 0;
307
308         if (flags & GPIO_PIN_OUTPUT)
309                 dir = 1;
310
311         switch (map->lp_port) {
312         case 0:
313                 state = (1 << LPC_GPIO_PIN_IDX(map, pin));
314                 lpc_gpio_set_4(sc, dir, LPC_GPIO_P0_DIR_SET, 
315                     LPC_GPIO_P0_DIR_CLR, state);
316                 break;
317         case 1:
318                 state = (1 << LPC_GPIO_PIN_IDX(map, pin));
319                 lpc_gpio_set_4(sc, dir, LPC_GPIO_P1_DIR_SET, 
320                     LPC_GPIO_P0_DIR_CLR, state);
321                 break;
322         case 2:
323                 state = (1 << LPC_GPIO_PIN_IDX(map, pin));
324                 lpc_gpio_set_4(sc, dir, LPC_GPIO_P2_DIR_SET, 
325                     LPC_GPIO_P0_DIR_CLR, state);
326                 break;
327         case 3:
328                 state = (1 << (25 + (pin - map->lp_start_idx)));
329                 lpc_gpio_set_4(sc, dir, LPC_GPIO_P2_DIR_SET, 
330                     LPC_GPIO_P0_DIR_CLR, state);
331                 break;
332         }
333
334         return (0);
335 }
336
337 static int
338 lpc_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
339 {
340         const struct lpc_gpio_pinmap *map;
341         int idx;
342
343         map = lpc_gpio_get_pinmap(pin);
344         idx = LPC_GPIO_PIN_IDX(map, pin);
345
346         switch (map->lp_port) {
347         case 0:
348         case 1:
349         case 2:
350                 snprintf(name, GPIOMAXNAME - 1, "P%d.%d", map->lp_port, 
351                     map->lp_start_bit + LPC_GPIO_PIN_IDX(map, pin));
352                 break;
353         case 3:
354                 if (map->lp_start_bit == -1) {
355                         snprintf(name, GPIOMAXNAME - 1, "GPIO_%02d", idx);
356                         break;
357                 }
358
359                 snprintf(name, GPIOMAXNAME - 1, "GP%c_%02d",
360                     (map->lp_flags & GPIO_PIN_INPUT) ? 'I' : 'O',
361                     map->lp_start_bit + idx);
362                 break;
363         }
364
365         return (0);
366 }
367
368 static int
369 lpc_gpio_pin_get(device_t dev, uint32_t pin, uint32_t *value)
370 {
371         struct lpc_gpio_softc *sc = device_get_softc(dev);
372         const struct lpc_gpio_pinmap *map;
373         uint32_t state, flags;
374         int dir;
375
376         map = lpc_gpio_get_pinmap(pin);
377
378         if (lpc_gpio_pin_getflags(dev, pin, &flags))
379                 return (ENXIO);
380
381         if (flags & GPIO_PIN_OUTPUT)
382                 dir = 1;
383
384         if (flags & GPIO_PIN_INPUT)
385                 dir = 0;
386
387         switch (map->lp_port) {
388         case 0:
389                 state = lpc_gpio_get_4(sc, dir, LPC_GPIO_P0_OUTP_STATE,
390                     LPC_GPIO_P0_INP_STATE);
391                 *value = !!(state & (1 << LPC_GPIO_PIN_BIT(map, pin)));
392         case 1:
393                 state = lpc_gpio_get_4(sc, dir, LPC_GPIO_P1_OUTP_STATE,
394                     LPC_GPIO_P1_INP_STATE);
395                 *value = !!(state & (1 << LPC_GPIO_PIN_BIT(map, pin)));
396         case 2:
397                 state = lpc_gpio_read_4(sc, LPC_GPIO_P2_INP_STATE);
398                 *value = !!(state & (1 << LPC_GPIO_PIN_BIT(map, pin)));
399         case 3:
400                 state = lpc_gpio_get_4(sc, dir, LPC_GPIO_P3_OUTP_STATE,
401                     LPC_GPIO_P3_INP_STATE);
402                 if (map->lp_start_bit == -1) {
403                         if (dir)
404                                 *value = !!(state & (1 << (25 + 
405                                     LPC_GPIO_PIN_IDX(map, pin))));
406                         else
407                                 *value = !!(state & (1 << (10 + 
408                                     LPC_GPIO_PIN_IDX(map, pin))));
409                 }
410
411                 *value = !!(state & (1 << LPC_GPIO_PIN_BIT(map, pin)));
412         }
413
414         return (0);
415 }
416
417 static int
418 lpc_gpio_pin_set(device_t dev, uint32_t pin, uint32_t value)
419 {
420         struct lpc_gpio_softc *sc = device_get_softc(dev);
421         const struct lpc_gpio_pinmap *map;
422         uint32_t state, flags;
423
424         map = lpc_gpio_get_pinmap(pin);
425
426         if (lpc_gpio_pin_getflags(dev, pin, &flags))
427                 return (ENXIO);
428
429         if ((flags & GPIO_PIN_OUTPUT) == 0)
430                 return (EINVAL);
431
432         state = (1 << LPC_GPIO_PIN_BIT(map, pin));
433
434         switch (map->lp_port) {
435         case 0:
436                 lpc_gpio_set_4(sc, value, LPC_GPIO_P0_OUTP_SET,
437                     LPC_GPIO_P0_OUTP_CLR, state);
438                 break;
439         case 1:
440                 lpc_gpio_set_4(sc, value, LPC_GPIO_P1_OUTP_SET,
441                     LPC_GPIO_P1_OUTP_CLR, state);
442                 break;
443         case 2:
444                 lpc_gpio_set_4(sc, value, LPC_GPIO_P2_OUTP_SET,
445                     LPC_GPIO_P2_OUTP_CLR, state);
446                 break;
447         case 3:
448                 if (map->lp_start_bit == -1)
449                         state = (1 << (25 + LPC_GPIO_PIN_IDX(map, pin)));
450                 
451                 lpc_gpio_set_4(sc, value, LPC_GPIO_P3_OUTP_SET,
452                     LPC_GPIO_P3_OUTP_CLR, state);
453                 break;
454         }
455
456         return (0);
457 }
458
459 static int
460 lpc_gpio_pin_toggle(device_t dev, uint32_t pin)
461 {
462         const struct lpc_gpio_pinmap *map;
463         uint32_t flags;
464
465         map = lpc_gpio_get_pinmap(pin);
466
467         if (lpc_gpio_pin_getflags(dev, pin, &flags))
468                 return (ENXIO);
469
470         if ((flags & GPIO_PIN_OUTPUT) == 0)
471                 return (EINVAL);
472         
473         panic("not implemented yet");
474
475         return (0);
476
477 }
478
479 static const struct lpc_gpio_pinmap *
480 lpc_gpio_get_pinmap(int pin)
481 {
482         const struct lpc_gpio_pinmap *map;
483
484         for (map = &lpc_gpio_pins[0]; map->lp_start_idx != -1; map++) {
485                 if (pin >= map->lp_start_idx &&
486                     pin < map->lp_start_idx + map->lp_pin_count)
487                         return map;
488         }
489
490         panic("pin number %d out of range", pin);
491 }
492
493 int
494 lpc_gpio_set_flags(device_t dev, int pin, int flags)
495 {
496         if (lpc_gpio_sc == NULL)
497                 return (ENXIO);
498
499         return lpc_gpio_pin_setflags(lpc_gpio_sc->lg_dev, pin, flags);
500 }
501
502 int
503 lpc_gpio_set_state(device_t dev, int pin, int state)
504 {
505         if (lpc_gpio_sc == NULL)
506                 return (ENXIO);
507
508         return lpc_gpio_pin_set(lpc_gpio_sc->lg_dev, pin, state); 
509 }
510
511 int
512 lpc_gpio_get_state(device_t dev, int pin, int *state)
513 {
514         if (lpc_gpio_sc == NULL)
515                 return (ENXIO);
516
517         return lpc_gpio_pin_get(lpc_gpio_sc->lg_dev, pin, state);
518 }
519
520 void
521 lpc_gpio_init(void)
522 {
523         bus_space_tag_t bst;
524         bus_space_handle_t bsh;
525
526         bst = fdtbus_bs_tag;
527
528         /* Preset SPI devices CS pins to one */
529         bus_space_map(bst, LPC_GPIO_PHYS_BASE, LPC_GPIO_SIZE, 0, &bsh);
530         bus_space_write_4(bst, bsh, LPC_GPIO_P3_OUTP_SET,
531             1 << (SSD1289_CS_PIN - LPC_GPIO_GPO_00(0)) |
532             1 << (SSD1289_DC_PIN - LPC_GPIO_GPO_00(0)) |
533             1 << (ADS7846_CS_PIN - LPC_GPIO_GPO_00(0)));        
534         bus_space_unmap(bst, bsh, LPC_GPIO_SIZE);
535 }
536
537 static device_method_t lpc_gpio_methods[] = {
538         /* Device interface */
539         DEVMETHOD(device_probe,         lpc_gpio_probe),
540         DEVMETHOD(device_attach,        lpc_gpio_attach),
541         DEVMETHOD(device_detach,        lpc_gpio_detach),
542
543         /* GPIO interface */
544         DEVMETHOD(gpio_get_bus,         lpc_gpio_get_bus),
545         DEVMETHOD(gpio_pin_max,         lpc_gpio_pin_max),
546         DEVMETHOD(gpio_pin_getcaps,     lpc_gpio_pin_getcaps),
547         DEVMETHOD(gpio_pin_getflags,    lpc_gpio_pin_getflags),
548         DEVMETHOD(gpio_pin_setflags,    lpc_gpio_pin_setflags),
549         DEVMETHOD(gpio_pin_getname,     lpc_gpio_pin_getname),
550         DEVMETHOD(gpio_pin_set,         lpc_gpio_pin_set),
551         DEVMETHOD(gpio_pin_get,         lpc_gpio_pin_get),
552         DEVMETHOD(gpio_pin_toggle,      lpc_gpio_pin_toggle),
553
554         { 0, 0 }
555 };
556
557 static devclass_t lpc_gpio_devclass;
558
559 static driver_t lpc_gpio_driver = {
560         "lpcgpio",
561         lpc_gpio_methods,
562         sizeof(struct lpc_gpio_softc),
563 };
564
565 extern devclass_t gpiobus_devclass, gpioc_devclass;
566 extern driver_t gpiobus_driver, gpioc_driver;
567
568 DRIVER_MODULE(lpcgpio, simplebus, lpc_gpio_driver, lpc_gpio_devclass, 0, 0);
569 DRIVER_MODULE(gpiobus, lpcgpio, gpiobus_driver, gpiobus_devclass, 0, 0);
570 DRIVER_MODULE(gpioc, lpcgpio, gpioc_driver, gpioc_devclass, 0, 0);
571 MODULE_VERSION(lpcgpio, 1);