zynq_gpio.c revision 1.2 1 /* $NetBSD: zynq_gpio.c,v 1.2 2022/10/27 22:35:31 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2022 Jared McNeill <jmcneill (at) invisible.ca>
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 ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * 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 __KERNEL_RCSID(0, "$NetBSD: zynq_gpio.c,v 1.2 2022/10/27 22:35:31 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bitops.h>
34 #include <sys/bus.h>
35 #include <sys/device.h>
36 #include <sys/gpio.h>
37 #include <sys/intr.h>
38 #include <sys/kmem.h>
39 #include <sys/lwp.h>
40 #include <sys/mutex.h>
41 #include <sys/systm.h>
42
43 #include <dev/fdt/fdtvar.h>
44 #include <dev/gpio/gpiovar.h>
45
46 #define ZYNQ_GPIO_NPINS (4 * 32)
47
48 #define MASK_DATA_REG(pin) (0x000 + 0x4 * ((pin) / 16))
49 #define MASK_DATA_SET(pin, val) ((((pin) % 16) << 16) | ((val) << ((pin) % 16)))
50 #define DATA_RO_REG(pin) (0x060 + 0x4 * ((pin) / 32))
51 #define DATA_RO_BIT(pin) __BIT((pin) % 32)
52 #define DIRM_REG(pin) (0x204 + 0x40 * ((pin) / 32))
53 #define DIRM_BIT(pin) __BIT((pin) % 32)
54 #define OEN_REG(pin) (0x208 + 0x40 * ((pin) / 32))
55 #define OEN_BIT(pin) __BIT((pin) % 32)
56
57 static const struct device_compatible_entry compat_data[] = {
58 { .compat = "xlnx,zynq-gpio-1.0" },
59 DEVICE_COMPAT_EOL
60 };
61
62 struct zynq_gpio_softc {
63 device_t sc_dev;
64 bus_space_tag_t sc_bst;
65 bus_space_handle_t sc_bsh;
66 kmutex_t sc_lock;
67 struct gpio_chipset_tag sc_gp;
68 gpio_pin_t sc_pins[ZYNQ_GPIO_NPINS];
69 device_t sc_gpiodev;
70 };
71
72 struct zynq_gpio_pin {
73 struct zynq_gpio_softc *pin_sc;
74 u_int pin_nr;
75 int pin_flags;
76 bool pin_actlo;
77 };
78
79 #define RD4(sc, reg) \
80 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
81 #define WR4(sc, reg, val) \
82 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
83
84 static int zynq_gpio_match(device_t, cfdata_t, void *);
85 static void zynq_gpio_attach(device_t, device_t, void *);
86
87 static int zynq_gpio_pin_read(void *, int);
88 static void zynq_gpio_pin_write(void *, int, int);
89
90 CFATTACH_DECL_NEW(zynqgpio, sizeof(struct zynq_gpio_softc),
91 zynq_gpio_match, zynq_gpio_attach, NULL, NULL);
92
93 static int
94 zynq_gpio_ctl(struct zynq_gpio_softc *sc, u_int pin, int flags)
95 {
96 uint32_t val;
97
98 KASSERT(mutex_owned(&sc->sc_lock));
99
100 val = RD4(sc, OEN_REG(pin));
101 if ((flags & GPIO_PIN_INPUT) != 0) {
102 val &= ~OEN_BIT(pin);
103 } else if ((flags & GPIO_PIN_OUTPUT) != 0) {
104 val |= OEN_BIT(pin);
105 }
106 WR4(sc, OEN_REG(pin), val);
107
108 return 0;
109 }
110
111 static void *
112 zynq_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
113 {
114 struct zynq_gpio_softc * const sc = device_private(dev);
115 struct zynq_gpio_pin *gpin;
116 const u_int *gpio = data;
117 int error;
118
119 if (len != 12)
120 return NULL;
121
122 const uint8_t pin = be32toh(gpio[1]) & 0xff;
123 const bool actlo = be32toh(gpio[2]) & 1;
124
125 if (pin >= __arraycount(sc->sc_pins))
126 return NULL;
127
128 mutex_enter(&sc->sc_lock);
129 error = zynq_gpio_ctl(sc, pin, flags);
130 mutex_exit(&sc->sc_lock);
131
132 if (error != 0)
133 return NULL;
134
135 gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
136 gpin->pin_sc = sc;
137 gpin->pin_nr = pin;
138 gpin->pin_flags = flags;
139 gpin->pin_actlo = actlo;
140
141 return gpin;
142 }
143
144 static void
145 zynq_gpio_release(device_t dev, void *priv)
146 {
147 struct zynq_gpio_softc * const sc = device_private(dev);
148 struct zynq_gpio_pin *pin = priv;
149
150 mutex_enter(&sc->sc_lock);
151 zynq_gpio_ctl(pin->pin_sc, pin->pin_nr, GPIO_PIN_INPUT);
152 mutex_exit(&sc->sc_lock);
153
154 kmem_free(pin, sizeof(*pin));
155 }
156
157 static int
158 zynq_gpio_read(device_t dev, void *priv, bool raw)
159 {
160 struct zynq_gpio_softc * const sc = device_private(dev);
161 struct zynq_gpio_pin *pin = priv;
162 int val;
163
164 KASSERT(sc == pin->pin_sc);
165
166 val = zynq_gpio_pin_read(sc, pin->pin_nr);
167 if (!raw && pin->pin_actlo)
168 val = !val;
169
170 return val;
171 }
172
173 static void
174 zynq_gpio_write(device_t dev, void *priv, int val, bool raw)
175 {
176 struct zynq_gpio_softc * const sc = device_private(dev);
177 struct zynq_gpio_pin *pin = priv;
178
179 KASSERT(sc == pin->pin_sc);
180
181 if (!raw && pin->pin_actlo)
182 val = !val;
183
184 zynq_gpio_pin_write(sc, pin->pin_nr, val);
185 }
186
187 static struct fdtbus_gpio_controller_func zynq_gpio_funcs = {
188 .acquire = zynq_gpio_acquire,
189 .release = zynq_gpio_release,
190 .read = zynq_gpio_read,
191 .write = zynq_gpio_write,
192 };
193
194 static int
195 zynq_gpio_pin_read(void *priv, int pin)
196 {
197 struct zynq_gpio_softc * const sc = priv;
198 uint32_t data;
199 int val;
200
201 KASSERT(pin < __arraycount(sc->sc_pins));
202
203 data = RD4(sc, DATA_RO_REG(pin));
204 val = __SHIFTOUT(data, DATA_RO_BIT(pin));
205
206 return val;
207 }
208
209 static void
210 zynq_gpio_pin_write(void *priv, int pin, int val)
211 {
212 struct zynq_gpio_softc * const sc = priv;
213
214 KASSERT(pin < __arraycount(sc->sc_pins));
215
216 WR4(sc, MASK_DATA_REG(pin), MASK_DATA_SET(pin, val));
217 }
218
219 static void
220 zynq_gpio_pin_ctl(void *priv, int pin, int flags)
221 {
222 struct zynq_gpio_softc * const sc = priv;
223
224 KASSERT(pin < __arraycount(sc->sc_pins));
225
226 mutex_enter(&sc->sc_lock);
227 zynq_gpio_ctl(sc, pin, flags);
228 mutex_exit(&sc->sc_lock);
229 }
230
231 static void
232 zynq_gpio_attach_ports(struct zynq_gpio_softc *sc)
233 {
234 struct gpio_chipset_tag *gp = &sc->sc_gp;
235 struct gpiobus_attach_args gba;
236 u_int pin;
237
238 gp->gp_cookie = sc;
239 gp->gp_pin_read = zynq_gpio_pin_read;
240 gp->gp_pin_write = zynq_gpio_pin_write;
241 gp->gp_pin_ctl = zynq_gpio_pin_ctl;
242
243 for (pin = 0; pin < __arraycount(sc->sc_pins); pin++) {
244 sc->sc_pins[pin].pin_num = pin;
245 sc->sc_pins[pin].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
246 sc->sc_pins[pin].pin_state = zynq_gpio_pin_read(sc, pin);
247 }
248
249 memset(&gba, 0, sizeof(gba));
250 gba.gba_gc = gp;
251 gba.gba_pins = sc->sc_pins;
252 gba.gba_npins = __arraycount(sc->sc_pins);
253 sc->sc_gpiodev = config_found(sc->sc_dev, &gba, NULL, CFARGS_NONE);
254 }
255
256 static int
257 zynq_gpio_match(device_t parent, cfdata_t cf, void *aux)
258 {
259 struct fdt_attach_args * const faa = aux;
260
261 return of_compatible_match(faa->faa_phandle, compat_data);
262 }
263
264 static void
265 zynq_gpio_attach(device_t parent, device_t self, void *aux)
266 {
267 struct zynq_gpio_softc * const sc = device_private(self);
268 struct fdt_attach_args * const faa = aux;
269 const int phandle = faa->faa_phandle;
270 bus_addr_t addr;
271 bus_size_t size;
272
273 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
274 aprint_error(": couldn't get registers\n");
275 return;
276 }
277
278 sc->sc_dev = self;
279 sc->sc_bst = faa->faa_bst;
280 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
281 aprint_error(": couldn't map registers\n");
282 return;
283 }
284 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
285
286 aprint_naive("\n");
287 aprint_normal(": XGPIOPS\n");
288
289 fdtbus_register_gpio_controller(self, phandle, &zynq_gpio_funcs);
290
291 zynq_gpio_attach_ports(sc);
292 }
293