1 /* $OpenBSD: skgpio.c,v 1.4 2020/08/26 03:29:06 visa Exp $ */ 2 3 /* 4 * Copyright (c) 2014 Matt Dainty <matt@bodgit-n-scarper.com> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN 15 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 16 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* 20 * Soekris net6501 GPIO and LEDs as implemented by the onboard Xilinx FPGA 21 */ 22 23 #include <sys/param.h> 24 #include <sys/systm.h> 25 #include <sys/device.h> 26 #include <sys/gpio.h> 27 28 #include <machine/bus.h> 29 30 #include <dev/isa/isavar.h> 31 32 #include <dev/gpio/gpiovar.h> 33 34 #define SKGPIO_BASE 0x680 /* Base address of FPGA I/O */ 35 #define SKGPIO_IOSIZE 32 /* I/O region size */ 36 37 #define SKGPIO_NPINS 16 /* Number of Pins */ 38 #define SKGPIO_GPIO_INPUT 0x000 /* Current state of pins */ 39 #define SKGPIO_GPIO_OUTPUT 0x004 /* Set state of output pins */ 40 #define SKGPIO_GPIO_RESET 0x008 /* Reset output pins */ 41 #define SKGPIO_GPIO_SET 0x00c /* Set output pins */ 42 #define SKGPIO_GPIO_DIR 0x010 /* Direction, set for output */ 43 44 #define SKGPIO_NLEDS 2 /* Number of LEDs */ 45 #define SKGPIO_LED_ERROR 0x01c /* Offset to error LED */ 46 #define SKGPIO_LED_READY 0x01d /* Offset to ready LED */ 47 48 const u_int skgpio_led_offset[SKGPIO_NLEDS] = { 49 SKGPIO_LED_ERROR, SKGPIO_LED_READY 50 }; 51 52 struct skgpio_softc { 53 struct device sc_dev; 54 55 bus_space_tag_t sc_iot; 56 bus_space_handle_t sc_ioh; 57 58 struct gpio_chipset_tag sc_gpio_gc; 59 gpio_pin_t sc_gpio_pins[SKGPIO_NPINS]; 60 61 /* Fake GPIO device for the LEDs */ 62 struct gpio_chipset_tag sc_led_gc; 63 gpio_pin_t sc_led_pins[SKGPIO_NLEDS]; 64 }; 65 66 int skgpio_match(struct device *, void *, void *); 67 void skgpio_attach(struct device *, struct device *, void *); 68 int skgpio_gpio_read(void *, int); 69 void skgpio_gpio_write(void *, int, int); 70 void skgpio_gpio_ctl(void *, int, int); 71 int skgpio_led_read(void *, int); 72 void skgpio_led_write(void *, int, int); 73 void skgpio_led_ctl(void *, int, int); 74 75 struct cfattach skgpio_ca = { 76 sizeof(struct skgpio_softc), skgpio_match, skgpio_attach 77 }; 78 79 struct cfdriver skgpio_cd = { 80 NULL, "skgpio", DV_DULL 81 }; 82 83 int 84 skgpio_match(struct device *parent, void *match, void *aux) 85 { 86 struct isa_attach_args *ia = aux; 87 bus_space_handle_t ioh; 88 89 if (hw_vendor == NULL || hw_prod == NULL || 90 strcmp(hw_vendor, "Soekris Engineering") != 0 || 91 strcmp(hw_prod, "net6501") != 0) 92 return (0); 93 94 if (ia->ia_iobase != SKGPIO_BASE || bus_space_map(ia->ia_iot, 95 ia->ia_iobase, SKGPIO_IOSIZE, 0, &ioh) != 0) 96 return (0); 97 98 bus_space_unmap(ia->ia_iot, ioh, SKGPIO_IOSIZE); 99 ia->ia_iosize = SKGPIO_IOSIZE; 100 ia->ipa_nio = 1; 101 ia->ipa_nmem = 0; 102 ia->ipa_nirq = 0; 103 ia->ipa_ndrq = 0; 104 105 return (1); 106 } 107 108 void 109 skgpio_attach(struct device *parent, struct device *self, void *aux) 110 { 111 struct skgpio_softc *sc = (void *)self; 112 struct isa_attach_args *ia = aux; 113 struct gpiobus_attach_args gba1, gba2; 114 u_int data; 115 int i; 116 117 if (bus_space_map(ia->ia_iot, ia->ia_iobase, ia->ia_iosize, 0, 118 &sc->sc_ioh) != 0) { 119 printf(": can't map i/o space\n"); 120 return; 121 } 122 123 printf("\n"); 124 125 sc->sc_iot = ia->ia_iot; 126 127 data = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SKGPIO_GPIO_DIR); 128 129 for (i = 0; i < SKGPIO_NPINS; i++) { 130 sc->sc_gpio_pins[i].pin_num = i; 131 sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT | 132 GPIO_PIN_OUTPUT; 133 sc->sc_gpio_pins[i].pin_flags = (data & (1 << i)) ? 134 GPIO_PIN_OUTPUT : GPIO_PIN_INPUT; 135 sc->sc_gpio_pins[i].pin_state = skgpio_gpio_read(sc, i); 136 } 137 138 sc->sc_gpio_gc.gp_cookie = sc; 139 sc->sc_gpio_gc.gp_pin_read = skgpio_gpio_read; 140 sc->sc_gpio_gc.gp_pin_write = skgpio_gpio_write; 141 sc->sc_gpio_gc.gp_pin_ctl = skgpio_gpio_ctl; 142 143 gba1.gba_name = "gpio"; 144 gba1.gba_gc = &sc->sc_gpio_gc; 145 gba1.gba_pins = sc->sc_gpio_pins; 146 gba1.gba_npins = SKGPIO_NPINS; 147 148 (void)config_found(&sc->sc_dev, &gba1, gpiobus_print); 149 150 for (i = 0; i < SKGPIO_NLEDS; i++) { 151 sc->sc_led_pins[i].pin_num = i; 152 sc->sc_led_pins[i].pin_caps = GPIO_PIN_OUTPUT; 153 sc->sc_led_pins[i].pin_flags = GPIO_PIN_OUTPUT; 154 sc->sc_led_pins[i].pin_state = skgpio_led_read(sc, i); 155 } 156 157 sc->sc_led_gc.gp_cookie = sc; 158 sc->sc_led_gc.gp_pin_read = skgpio_led_read; 159 sc->sc_led_gc.gp_pin_write = skgpio_led_write; 160 sc->sc_led_gc.gp_pin_ctl = skgpio_led_ctl; 161 162 gba2.gba_name = "gpio"; 163 gba2.gba_gc = &sc->sc_led_gc; 164 gba2.gba_pins = sc->sc_led_pins; 165 gba2.gba_npins = SKGPIO_NLEDS; 166 167 (void)config_found(&sc->sc_dev, &gba2, gpiobus_print); 168 } 169 170 int 171 skgpio_gpio_read(void *arg, int pin) 172 { 173 struct skgpio_softc *sc = arg; 174 u_int16_t data; 175 176 data = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SKGPIO_GPIO_INPUT); 177 178 return (data & (1 << pin)) ? GPIO_PIN_HIGH : GPIO_PIN_LOW; 179 } 180 181 void 182 skgpio_gpio_write(void *arg, int pin, int value) 183 { 184 struct skgpio_softc *sc = arg; 185 u_int16_t data; 186 187 data = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SKGPIO_GPIO_INPUT); 188 189 if (value == GPIO_PIN_LOW) 190 data &= ~(1 << pin); 191 else if (value == GPIO_PIN_HIGH) 192 data |= (1 << pin); 193 194 bus_space_write_2(sc->sc_iot, sc->sc_ioh, SKGPIO_GPIO_OUTPUT, data); 195 } 196 197 void 198 skgpio_gpio_ctl(void *arg, int pin, int flags) 199 { 200 struct skgpio_softc *sc = arg; 201 u_int16_t data; 202 203 data = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SKGPIO_GPIO_DIR); 204 205 if (flags & GPIO_PIN_INPUT) 206 data &= ~(1 << pin); 207 if (flags & GPIO_PIN_OUTPUT) 208 data |= (1 << pin); 209 210 bus_space_write_2(sc->sc_iot, sc->sc_ioh, SKGPIO_GPIO_DIR, data); 211 } 212 213 int 214 skgpio_led_read(void *arg, int pin) 215 { 216 struct skgpio_softc *sc = arg; 217 u_int8_t value; 218 219 value = bus_space_read_1(sc->sc_iot, sc->sc_ioh, 220 skgpio_led_offset[pin]); 221 222 return (value & 0x1) ? GPIO_PIN_HIGH : GPIO_PIN_LOW; 223 } 224 225 void 226 skgpio_led_write(void *arg, int pin, int value) 227 { 228 struct skgpio_softc *sc = arg; 229 230 bus_space_write_1(sc->sc_iot, sc->sc_ioh, skgpio_led_offset[pin], 231 value); 232 } 233 234 void 235 skgpio_led_ctl(void *arg, int pin, int flags) 236 { 237 } 238