1 1.10 thorpej /* $NetBSD: argpio.c,v 1.10 2021/08/07 16:18:58 thorpej Exp $ */ 2 1.1 gdamore 3 1.1 gdamore /*- 4 1.1 gdamore * Copyright (c) 2006 Garrett D'Amore 5 1.1 gdamore * All rights reserved. 6 1.1 gdamore * 7 1.1 gdamore * Written by Garrett D'Amore. 8 1.1 gdamore * 9 1.1 gdamore * Redistribution and use in source and binary forms, with or without 10 1.1 gdamore * modification, are permitted provided that the following conditions 11 1.1 gdamore * are met: 12 1.1 gdamore * 1. Redistributions of source code must retain the above copyright 13 1.1 gdamore * notice, this list of conditions and the following disclaimer. 14 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 gdamore * notice, this list of conditions and the following disclaimer in the 16 1.1 gdamore * documentation and/or other materials provided with the distribution. 17 1.1 gdamore * 3. The name of the author may not be used to endorse 18 1.1 gdamore * or promote products derived from this software without specific 19 1.1 gdamore * prior written permission. 20 1.1 gdamore * 21 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 22 1.1 gdamore * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 1.1 gdamore * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 1.1 gdamore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 25 1.1 gdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 1.1 gdamore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27 1.1 gdamore * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 1.1 gdamore * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 1.1 gdamore * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 1.1 gdamore * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 1.1 gdamore * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 1.1 gdamore */ 33 1.1 gdamore 34 1.1 gdamore #include <sys/cdefs.h> 35 1.10 thorpej __KERNEL_RCSID(0, "$NetBSD: argpio.c,v 1.10 2021/08/07 16:18:58 thorpej Exp $"); 36 1.1 gdamore 37 1.1 gdamore #include <sys/param.h> 38 1.5 matt #include <sys/bus.h> 39 1.1 gdamore #include <sys/conf.h> 40 1.5 matt #include <sys/device.h> 41 1.5 matt #include <sys/gpio.h> 42 1.5 matt #include <sys/intr.h> 43 1.1 gdamore #include <sys/kernel.h> 44 1.1 gdamore #include <sys/types.h> 45 1.1 gdamore 46 1.1 gdamore #include <mips/atheros/include/arbusvar.h> 47 1.1 gdamore 48 1.1 gdamore #include <dev/gpio/gpiovar.h> 49 1.1 gdamore #include <dev/sysmon/sysmonvar.h> 50 1.1 gdamore #include <dev/sysmon/sysmon_taskq.h> 51 1.1 gdamore 52 1.1 gdamore #include <mips/atheros/dev/argpioreg.h> 53 1.1 gdamore 54 1.1 gdamore /* 55 1.1 gdamore * General Plan: 56 1.1 gdamore * 57 1.1 gdamore * Register GPIOs for all pins that are _not_ associated with the reset 58 1.8 nia * pin. (Possibly also not the system LED.) 59 1.1 gdamore */ 60 1.1 gdamore 61 1.1 gdamore struct argpio_softc { 62 1.5 matt device_t sc_dev; 63 1.1 gdamore struct gpio_chipset_tag sc_gc; 64 1.1 gdamore gpio_pin_t sc_pins[ARGPIO_NPINS]; 65 1.1 gdamore int sc_npins; 66 1.1 gdamore bus_space_tag_t sc_st; 67 1.1 gdamore bus_space_handle_t sc_sh; 68 1.1 gdamore bus_size_t sc_size; 69 1.1 gdamore int sc_caps; 70 1.1 gdamore struct sysmon_pswitch sc_resetbtn; 71 1.1 gdamore void *sc_ih; 72 1.1 gdamore int sc_rstpin; 73 1.3 gdamore int sc_ledpin; 74 1.1 gdamore }; 75 1.1 gdamore 76 1.5 matt static int argpio_match(device_t, cfdata_t, void *); 77 1.5 matt static void argpio_attach(device_t, device_t, void *); 78 1.1 gdamore static int argpio_intr(void *); 79 1.1 gdamore static void argpio_reset_pressed(void *); 80 1.1 gdamore static void argpio_ctl(void *, int, int); 81 1.1 gdamore static void argpio_write(void *, int, int); 82 1.1 gdamore static int argpio_read(void *, int); 83 1.1 gdamore 84 1.5 matt CFATTACH_DECL_NEW(argpio, sizeof (struct argpio_softc), argpio_match, 85 1.1 gdamore argpio_attach, NULL, NULL); 86 1.1 gdamore 87 1.1 gdamore #define INPUT(pin) (1 << (pin)) /* input bit */ 88 1.1 gdamore #define INTR(pin) (1 << ((pin) + 8)) /* interrupt bit */ 89 1.1 gdamore #define SERIAL(pin) (1 << ((pin) + 16)) /* serial mux bit */ 90 1.1 gdamore 91 1.1 gdamore #define GETREG(sc, o) bus_space_read_4(sc->sc_st, sc->sc_sh, o) 92 1.1 gdamore #define PUTREG(sc, o, v) bus_space_write_4(sc->sc_st, sc->sc_sh, o, v) 93 1.1 gdamore #define FLUSH(sc) bus_space_barrier(sc->sc_st, sc->sc_sh, \ 94 1.7 jdolecek 0, 12, BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE) 95 1.1 gdamore 96 1.1 gdamore int 97 1.5 matt argpio_match(device_t parent, cfdata_t match, void *aux) 98 1.1 gdamore { 99 1.1 gdamore struct arbus_attach_args *aa = aux; 100 1.1 gdamore 101 1.1 gdamore return ((strcmp(aa->aa_name, "argpio") == 0) ? 1 : 0); 102 1.1 gdamore } 103 1.1 gdamore 104 1.1 gdamore void 105 1.5 matt argpio_attach(device_t parent, device_t self, void *aux) 106 1.1 gdamore { 107 1.5 matt struct argpio_softc *sc = device_private(self); 108 1.1 gdamore struct arbus_attach_args *aa = aux; 109 1.1 gdamore struct gpiobus_attach_args gba; 110 1.3 gdamore prop_number_t pn; 111 1.3 gdamore int i; 112 1.1 gdamore uint32_t reg; 113 1.1 gdamore 114 1.5 matt sc->sc_dev = self; 115 1.1 gdamore sc->sc_st = aa->aa_bst; 116 1.1 gdamore sc->sc_npins = ARGPIO_NPINS; 117 1.1 gdamore sc->sc_size = aa->aa_size; 118 1.3 gdamore sc->sc_ledpin = -1; 119 1.3 gdamore sc->sc_rstpin = -1; 120 1.1 gdamore 121 1.1 gdamore if (bus_space_map(sc->sc_st, aa->aa_addr, sc->sc_size, 0, 122 1.1 gdamore &sc->sc_sh) != 0) { 123 1.1 gdamore printf(": unable to map registers!\n"); 124 1.1 gdamore return; 125 1.1 gdamore } 126 1.1 gdamore 127 1.1 gdamore sc->sc_gc.gp_cookie = sc; 128 1.1 gdamore sc->sc_gc.gp_pin_read = argpio_read; 129 1.1 gdamore sc->sc_gc.gp_pin_write = argpio_write; 130 1.1 gdamore sc->sc_gc.gp_pin_ctl = argpio_ctl; 131 1.1 gdamore 132 1.1 gdamore aprint_normal(": Atheros AR531X GPIO"); 133 1.5 matt pn = prop_dictionary_get(device_properties(sc->sc_dev), "reset-pin"); 134 1.3 gdamore if (pn != NULL) { 135 1.3 gdamore KASSERT(prop_object_type(pn) == PROP_TYPE_NUMBER); 136 1.3 gdamore sc->sc_rstpin = (int)prop_number_integer_value(pn); 137 1.3 gdamore aprint_normal(", reset button pin %d", sc->sc_rstpin); 138 1.1 gdamore } 139 1.5 matt pn = prop_dictionary_get(device_properties(sc->sc_dev), "sysled-pin"); 140 1.3 gdamore if (pn != NULL) { 141 1.3 gdamore KASSERT(prop_object_type(pn) == PROP_TYPE_NUMBER); 142 1.3 gdamore sc->sc_ledpin = (int)prop_number_integer_value(pn); 143 1.3 gdamore aprint_normal(", system led pin %d", sc->sc_ledpin); 144 1.1 gdamore } 145 1.3 gdamore 146 1.5 matt aprint_normal("\n"); 147 1.1 gdamore 148 1.3 gdamore if (sc->sc_ledpin) { 149 1.3 gdamore sc->sc_ih = arbus_intr_establish(aa->aa_cirq, aa->aa_mirq, 150 1.3 gdamore argpio_intr, sc); 151 1.1 gdamore if (sc->sc_ih == NULL) { 152 1.5 matt aprint_error_dev(sc->sc_dev, 153 1.5 matt "couldn't establish interrupt\n"); 154 1.1 gdamore } 155 1.1 gdamore } 156 1.1 gdamore 157 1.1 gdamore if (sc->sc_ih) { 158 1.1 gdamore sysmon_task_queue_init(); 159 1.1 gdamore 160 1.5 matt sc->sc_resetbtn.smpsw_name = device_xname(sc->sc_dev); 161 1.1 gdamore sc->sc_resetbtn.smpsw_type = PSWITCH_TYPE_RESET; 162 1.6 dyoung if (sysmon_pswitch_register(&sc->sc_resetbtn) != 0) { 163 1.6 dyoung aprint_normal_dev(sc->sc_dev, 164 1.6 dyoung "unable to register reset button\n"); 165 1.6 dyoung } 166 1.1 gdamore } 167 1.1 gdamore 168 1.1 gdamore reg = GETREG(sc, GPIO_CR); 169 1.1 gdamore 170 1.1 gdamore for (i = 0; i < sc->sc_npins; i++) { 171 1.1 gdamore gpio_pin_t *pp; 172 1.1 gdamore 173 1.1 gdamore pp = &sc->sc_pins[i]; 174 1.1 gdamore 175 1.3 gdamore if (i == sc->sc_rstpin) { 176 1.1 gdamore /* configure as interrupt for reset */ 177 1.1 gdamore pp->pin_caps = GPIO_PIN_INPUT; 178 1.1 gdamore reg &= ~SERIAL(i); 179 1.1 gdamore reg |= INPUT(i); 180 1.1 gdamore /* only if we were able to set up the handler, tho' */ 181 1.1 gdamore if (sc->sc_ih != NULL) 182 1.1 gdamore reg |= INTR(i); 183 1.1 gdamore 184 1.3 gdamore } else if (i == sc->sc_ledpin) { 185 1.1 gdamore /* configure as output for LED */ 186 1.1 gdamore pp->pin_caps = GPIO_PIN_OUTPUT; 187 1.1 gdamore reg &= ~SERIAL(i); 188 1.1 gdamore reg &= ~INPUT(i); 189 1.1 gdamore reg &= ~INTR(i); 190 1.1 gdamore 191 1.1 gdamore } else { 192 1.1 gdamore if (reg & SERIAL(i)) { 193 1.1 gdamore /* pin multiplexed with serial bit */ 194 1.1 gdamore pp->pin_caps = 0; 195 1.1 gdamore } else { 196 1.1 gdamore pp->pin_caps = GPIO_PIN_INPUT | 197 1.1 gdamore GPIO_PIN_OUTPUT; 198 1.1 gdamore } 199 1.1 gdamore } 200 1.1 gdamore } 201 1.1 gdamore 202 1.1 gdamore PUTREG(sc, GPIO_CR, reg); 203 1.1 gdamore FLUSH(sc); 204 1.1 gdamore 205 1.1 gdamore gba.gba_gc = &sc->sc_gc; 206 1.1 gdamore gba.gba_pins = sc->sc_pins; 207 1.1 gdamore gba.gba_npins = sc->sc_npins; 208 1.10 thorpej config_found(sc->sc_dev, &gba, gpiobus_print, CFARGS_NONE); 209 1.1 gdamore } 210 1.1 gdamore 211 1.1 gdamore void 212 1.1 gdamore argpio_ctl(void *arg, int pin, int flags) 213 1.1 gdamore { 214 1.1 gdamore struct argpio_softc *sc = arg; 215 1.1 gdamore uint32_t reg; 216 1.1 gdamore 217 1.1 gdamore reg = GETREG(sc, GPIO_CR); 218 1.1 gdamore if (reg & (SERIAL(pin) | INTR(pin))) { 219 1.1 gdamore printf("pin %d cannot be changed!\n", pin); 220 1.1 gdamore /* don't allow changes to these bits */ 221 1.1 gdamore return; 222 1.1 gdamore } 223 1.1 gdamore if (flags & GPIO_PIN_INPUT) { 224 1.1 gdamore reg |= INPUT(pin); 225 1.1 gdamore } else { 226 1.1 gdamore reg &= ~INPUT(pin); 227 1.1 gdamore } 228 1.1 gdamore 229 1.1 gdamore PUTREG(sc, GPIO_CR, reg); 230 1.1 gdamore FLUSH(sc); 231 1.1 gdamore } 232 1.1 gdamore 233 1.1 gdamore void 234 1.1 gdamore argpio_write(void *arg, int pin, int value) 235 1.1 gdamore { 236 1.1 gdamore struct argpio_softc *sc = arg; 237 1.1 gdamore uint32_t reg; 238 1.1 gdamore 239 1.1 gdamore reg = GETREG(sc, GPIO_DO); 240 1.1 gdamore if (value) 241 1.1 gdamore reg &= ~(1 << pin); 242 1.1 gdamore else 243 1.1 gdamore reg |= (1 << pin); 244 1.1 gdamore PUTREG(sc, GPIO_DO, reg); 245 1.1 gdamore FLUSH(sc); 246 1.1 gdamore } 247 1.1 gdamore 248 1.1 gdamore int 249 1.1 gdamore argpio_read(void *arg, int pin) 250 1.1 gdamore { 251 1.1 gdamore struct argpio_softc *sc = arg; 252 1.1 gdamore 253 1.1 gdamore return ((GETREG(sc, GPIO_DI) & (1 << pin)) ? 254 1.1 gdamore GPIO_PIN_HIGH : GPIO_PIN_LOW); 255 1.1 gdamore } 256 1.1 gdamore 257 1.1 gdamore void 258 1.1 gdamore argpio_reset_pressed(void *arg) 259 1.1 gdamore { 260 1.1 gdamore struct argpio_softc *sc = arg; 261 1.1 gdamore int x; 262 1.1 gdamore 263 1.1 gdamore sysmon_pswitch_event(&sc->sc_resetbtn, PSWITCH_EVENT_PRESSED); 264 1.1 gdamore 265 1.1 gdamore /* reenable the interrupt */ 266 1.1 gdamore x = splhigh(); 267 1.1 gdamore PUTREG(sc, GPIO_CR, 268 1.1 gdamore GETREG(sc, GPIO_CR) | INTR(sc->sc_rstpin)); 269 1.1 gdamore splx(x); 270 1.1 gdamore } 271 1.1 gdamore 272 1.1 gdamore int 273 1.1 gdamore argpio_intr(void *arg) 274 1.1 gdamore { 275 1.1 gdamore struct argpio_softc *sc = arg; 276 1.1 gdamore 277 1.1 gdamore if (sc->sc_rstpin < 0) 278 1.1 gdamore return 0; 279 1.1 gdamore 280 1.1 gdamore /* this is an edge triggered interrupt, so disable it for now */ 281 1.1 gdamore PUTREG(sc, GPIO_CR, GETREG(sc, GPIO_CR) & ~INTR(sc->sc_rstpin)); 282 1.1 gdamore 283 1.1 gdamore /* no other interrupt on this, so we have to claim it */ 284 1.1 gdamore sysmon_task_queue_sched(0, argpio_reset_pressed, sc); 285 1.1 gdamore 286 1.1 gdamore return 1; 287 1.1 gdamore } 288