pl061.c revision 1.4 1 1.4 thorpej /* $NetBSD: pl061.c,v 1.4 2021/08/07 16:19:12 thorpej Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*
4 1.1 jmcneill * Copyright (c) 2018 Jonathan A. Kollasch
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 COPYRIGHT HOLDERS AND CONTRIBUTORS
17 1.1 jmcneill * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 1.1 jmcneill * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 1.1 jmcneill * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 1.1 jmcneill * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 1.1 jmcneill * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 1.1 jmcneill * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 1.1 jmcneill * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 1.1 jmcneill * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.4 thorpej __KERNEL_RCSID(0, "$NetBSD: pl061.c,v 1.4 2021/08/07 16:19:12 thorpej 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/kernel.h>
38 1.1 jmcneill #include <sys/kmem.h>
39 1.1 jmcneill #include <sys/gpio.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <dev/gpio/gpiovar.h>
42 1.1 jmcneill #include "gpio.h"
43 1.1 jmcneill
44 1.1 jmcneill #include <dev/ic/pl061reg.h>
45 1.1 jmcneill #include <dev/ic/pl061var.h>
46 1.1 jmcneill
47 1.1 jmcneill void
48 1.1 jmcneill plgpio_attach(struct plgpio_softc *sc)
49 1.1 jmcneill {
50 1.1 jmcneill struct gpiobus_attach_args gba;
51 1.1 jmcneill u_int pin;
52 1.1 jmcneill
53 1.1 jmcneill sc->sc_gc.gp_cookie = sc;
54 1.1 jmcneill sc->sc_gc.gp_pin_read = plgpio_pin_read;
55 1.1 jmcneill sc->sc_gc.gp_pin_write = plgpio_pin_write;
56 1.1 jmcneill sc->sc_gc.gp_pin_ctl = plgpio_pin_ctl;
57 1.1 jmcneill
58 1.2 jmcneill const uint32_t cnf = PLGPIO_READ(sc, PL061_GPIOAFSEL_REG) |
59 1.2 jmcneill sc->sc_reserved_mask;
60 1.1 jmcneill
61 1.1 jmcneill for (pin = 0; pin < 8; pin++) {
62 1.1 jmcneill sc->sc_pins[pin].pin_num = pin;
63 1.1 jmcneill /* skip pins in hardware control mode */
64 1.1 jmcneill if ((cnf & __BIT(pin)) != 0)
65 1.1 jmcneill continue;
66 1.1 jmcneill sc->sc_pins[pin].pin_caps =
67 1.1 jmcneill GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
68 1.1 jmcneill GPIO_PIN_TRISTATE;
69 1.1 jmcneill sc->sc_pins[pin].pin_state =
70 1.1 jmcneill plgpio_pin_read(sc, pin);
71 1.1 jmcneill }
72 1.1 jmcneill
73 1.1 jmcneill memset(&gba, 0, sizeof(gba));
74 1.1 jmcneill gba.gba_gc = &sc->sc_gc;
75 1.1 jmcneill gba.gba_pins = sc->sc_pins;
76 1.1 jmcneill gba.gba_npins = 8;
77 1.1 jmcneill
78 1.1 jmcneill #if NGPIO > 0
79 1.4 thorpej config_found(sc->sc_dev, &gba, gpiobus_print, CFARGS_NONE);
80 1.1 jmcneill #endif
81 1.1 jmcneill }
82 1.1 jmcneill
83 1.1 jmcneill int
84 1.1 jmcneill plgpio_pin_read(void *priv, int pin)
85 1.1 jmcneill {
86 1.1 jmcneill struct plgpio_softc * const sc = priv;
87 1.1 jmcneill
88 1.1 jmcneill const uint32_t v = PLGPIO_READ(sc, PL061_GPIODATA_REG(1<<pin));
89 1.1 jmcneill
90 1.1 jmcneill return (v >> pin) & 1;
91 1.1 jmcneill }
92 1.1 jmcneill
93 1.1 jmcneill void
94 1.1 jmcneill plgpio_pin_write(void *priv, int pin, int val)
95 1.1 jmcneill {
96 1.1 jmcneill struct plgpio_softc * const sc = priv;
97 1.1 jmcneill
98 1.1 jmcneill PLGPIO_WRITE(sc, PL061_GPIODATA_REG(1 << pin), val << pin);
99 1.1 jmcneill }
100 1.1 jmcneill
101 1.1 jmcneill void
102 1.1 jmcneill plgpio_pin_ctl(void *priv, int pin, int flags)
103 1.1 jmcneill {
104 1.1 jmcneill struct plgpio_softc * const sc = priv;
105 1.1 jmcneill uint32_t v;
106 1.1 jmcneill
107 1.1 jmcneill if (flags & GPIO_PIN_INPUT) {
108 1.1 jmcneill v = PLGPIO_READ(sc, PL061_GPIODIR_REG);
109 1.1 jmcneill v &= ~(1 << pin);
110 1.1 jmcneill PLGPIO_WRITE(sc, PL061_GPIODIR_REG, v);
111 1.1 jmcneill } else if (flags & GPIO_PIN_OUTPUT) {
112 1.1 jmcneill v = PLGPIO_READ(sc, PL061_GPIODIR_REG);
113 1.1 jmcneill v |= (1 << pin);
114 1.1 jmcneill PLGPIO_WRITE(sc, PL061_GPIODIR_REG, v);
115 1.1 jmcneill }
116 1.1 jmcneill }
117