1 /*- 2 * Copyright (c) 2015 Justin Hibbits 3 * Copyright (c) 2013 Thomas Skibo 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/conf.h> 36 #include <sys/bus.h> 37 #include <sys/kernel.h> 38 #include <sys/module.h> 39 #include <sys/lock.h> 40 #include <sys/mutex.h> 41 #include <sys/resource.h> 42 #include <sys/rman.h> 43 #include <sys/gpio.h> 44 45 #include <machine/bus.h> 46 #include <machine/resource.h> 47 #include <machine/stdarg.h> 48 49 #include <dev/fdt/fdt_common.h> 50 #include <dev/gpio/gpiobusvar.h> 51 #include <dev/ofw/ofw_bus.h> 52 #include <dev/ofw/ofw_bus_subr.h> 53 54 #include "gpio_if.h" 55 56 #define MAXPIN (7) 57 58 #define VALID_PIN(u) ((u) >= 0 && (u) <= MAXPIN) 59 60 #define GPIO_LOCK(sc) mtx_lock(&(sc)->sc_mtx) 61 #define GPIO_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx) 62 #define GPIO_LOCK_INIT(sc) \ 63 mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev), \ 64 "gpio", MTX_DEF) 65 #define GPIO_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 66 67 struct mpc85xx_gpio_softc { 68 device_t dev; 69 device_t busdev; 70 struct mtx sc_mtx; 71 struct resource *out_res; /* Memory resource */ 72 struct resource *in_res; 73 }; 74 75 static device_t 76 mpc85xx_gpio_get_bus(device_t dev) 77 { 78 struct mpc85xx_gpio_softc *sc; 79 80 sc = device_get_softc(dev); 81 82 return (sc->busdev); 83 } 84 85 static int 86 mpc85xx_gpio_pin_max(device_t dev, int *maxpin) 87 { 88 89 *maxpin = MAXPIN; 90 return (0); 91 } 92 93 /* Get a specific pin's capabilities. */ 94 static int 95 mpc85xx_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps) 96 { 97 98 if (!VALID_PIN(pin)) 99 return (EINVAL); 100 101 *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT); 102 103 return (0); 104 } 105 106 /* Get a specific pin's name. */ 107 static int 108 mpc85xx_gpio_pin_getname(device_t dev, uint32_t pin, char *name) 109 { 110 111 if (!VALID_PIN(pin)) 112 return (EINVAL); 113 114 snprintf(name, GPIOMAXNAME, "GPIO%d", pin); 115 name[GPIOMAXNAME-1] = '\0'; 116 117 return (0); 118 } 119 120 /* Set a specific output pin's value. */ 121 static int 122 mpc85xx_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value) 123 { 124 struct mpc85xx_gpio_softc *sc = device_get_softc(dev); 125 uint32_t outvals; 126 uint8_t pinbit; 127 128 if (!VALID_PIN(pin) || value > 1) 129 return (EINVAL); 130 131 GPIO_LOCK(sc); 132 pinbit = 31 - pin; 133 134 outvals = bus_read_4(sc->out_res, 0); 135 outvals &= ~(1 << pinbit); 136 outvals |= (value << pinbit); 137 bus_write_4(sc->out_res, 0, outvals); 138 139 GPIO_UNLOCK(sc); 140 141 return (0); 142 } 143 144 /* Get a specific pin's input value. */ 145 static int 146 mpc85xx_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value) 147 { 148 struct mpc85xx_gpio_softc *sc = device_get_softc(dev); 149 150 if (!VALID_PIN(pin)) 151 return (EINVAL); 152 153 *value = (bus_read_4(sc->in_res, 0) >> (31 - pin)) & 1; 154 155 return (0); 156 } 157 158 /* Toggle a pin's output value. */ 159 static int 160 mpc85xx_gpio_pin_toggle(device_t dev, uint32_t pin) 161 { 162 struct mpc85xx_gpio_softc *sc = device_get_softc(dev); 163 uint32_t val; 164 165 if (!VALID_PIN(pin)) 166 return (EINVAL); 167 168 GPIO_LOCK(sc); 169 170 val = bus_read_4(sc->out_res, 0); 171 val ^= (1 << (31 - pin)); 172 bus_write_4(sc->out_res, 0, val); 173 174 GPIO_UNLOCK(sc); 175 176 return (0); 177 } 178 179 static int 180 mpc85xx_gpio_probe(device_t dev) 181 { 182 uint16_t vers; 183 uint32_t svr; 184 185 if (!ofw_bus_status_okay(dev)) 186 return (ENXIO); 187 188 if (!ofw_bus_is_compatible(dev, "gpio")) 189 return (ENXIO); 190 191 vers = mfpvr() >> 16; 192 switch (vers) { 193 case FSL_E500v1: 194 case FSL_E500v2: 195 case FSL_E500mc: 196 break; 197 default: 198 return (ENXIO); 199 } 200 201 svr = mfspr(SPR_SVR); 202 switch (SVR_VER(svr)) { 203 case SVR_MPC8533: 204 case SVR_MPC8533E: 205 break; 206 default: 207 return (ENXIO); 208 } 209 210 device_set_desc(dev, "MPC85xx GPIO driver"); 211 return (0); 212 } 213 214 static int mpc85xx_gpio_detach(device_t dev); 215 216 static int 217 mpc85xx_gpio_attach(device_t dev) 218 { 219 struct mpc85xx_gpio_softc *sc = device_get_softc(dev); 220 int rid; 221 222 sc->dev = dev; 223 224 GPIO_LOCK_INIT(sc); 225 226 /* Allocate memory. */ 227 rid = 0; 228 sc->out_res = bus_alloc_resource_any(dev, 229 SYS_RES_MEMORY, &rid, RF_ACTIVE); 230 if (sc->out_res == NULL) { 231 device_printf(dev, "Can't allocate memory for device output port"); 232 mpc85xx_gpio_detach(dev); 233 return (ENOMEM); 234 } 235 236 rid = 1; 237 sc->in_res = bus_alloc_resource_any(dev, 238 SYS_RES_MEMORY, &rid, RF_ACTIVE); 239 if (sc->in_res == NULL) { 240 device_printf(dev, "Can't allocate memory for device input port"); 241 mpc85xx_gpio_detach(dev); 242 return (ENOMEM); 243 } 244 245 sc->busdev = gpiobus_attach_bus(dev); 246 if (sc->busdev == NULL) { 247 mpc85xx_gpio_detach(dev); 248 return (ENOMEM); 249 } 250 251 OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev); 252 253 return (0); 254 } 255 256 static int 257 mpc85xx_gpio_detach(device_t dev) 258 { 259 struct mpc85xx_gpio_softc *sc = device_get_softc(dev); 260 261 gpiobus_detach_bus(dev); 262 263 if (sc->out_res != NULL) { 264 /* Release output port resource. */ 265 bus_release_resource(dev, SYS_RES_MEMORY, 266 rman_get_rid(sc->out_res), sc->out_res); 267 } 268 269 if (sc->in_res != NULL) { 270 /* Release input port resource. */ 271 bus_release_resource(dev, SYS_RES_MEMORY, 272 rman_get_rid(sc->in_res), sc->in_res); 273 } 274 275 GPIO_LOCK_DESTROY(sc); 276 277 return (0); 278 } 279 280 static device_method_t mpc85xx_gpio_methods[] = { 281 /* device_if */ 282 DEVMETHOD(device_probe, mpc85xx_gpio_probe), 283 DEVMETHOD(device_attach, mpc85xx_gpio_attach), 284 DEVMETHOD(device_detach, mpc85xx_gpio_detach), 285 286 /* GPIO protocol */ 287 DEVMETHOD(gpio_get_bus, mpc85xx_gpio_get_bus), 288 DEVMETHOD(gpio_pin_max, mpc85xx_gpio_pin_max), 289 DEVMETHOD(gpio_pin_getname, mpc85xx_gpio_pin_getname), 290 DEVMETHOD(gpio_pin_getcaps, mpc85xx_gpio_pin_getcaps), 291 DEVMETHOD(gpio_pin_get, mpc85xx_gpio_pin_get), 292 DEVMETHOD(gpio_pin_set, mpc85xx_gpio_pin_set), 293 DEVMETHOD(gpio_pin_toggle, mpc85xx_gpio_pin_toggle), 294 295 DEVMETHOD_END 296 }; 297 298 static driver_t mpc85xx_gpio_driver = { 299 "gpio", 300 mpc85xx_gpio_methods, 301 sizeof(struct mpc85xx_gpio_softc), 302 }; 303 static devclass_t mpc85xx_gpio_devclass; 304 305 EARLY_DRIVER_MODULE(mpc85xx_gpio, simplebus, mpc85xx_gpio_driver, 306 mpc85xx_gpio_devclass, NULL, NULL, 307 BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE); 308