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