1 /*- 2 * Copyright (c) 2009, Nathan Whitehorn <nwhitehorn@FreeBSD.org> 3 * Copyright (c) 2013, Luiz Otavio O Souza <loos@FreeBSD.org> 4 * Copyright (c) 2013 The FreeBSD Foundation 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 unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/gpio.h> 35 #include <sys/kernel.h> 36 #include <sys/libkern.h> 37 #include <sys/lock.h> 38 #include <sys/module.h> 39 #include <sys/mutex.h> 40 41 #include <dev/fdt/fdt_common.h> 42 #include <dev/gpio/gpiobusvar.h> 43 #include <dev/ofw/ofw_bus.h> 44 #include <dev/ofw/openfirm.h> 45 46 #include <machine/resource.h> 47 48 #include "gpio_if.h" 49 #include "gpiobus_if.h" 50 51 struct ofw_gpiobus_devinfo { 52 struct gpiobus_ivar opd_dinfo; 53 struct ofw_bus_devinfo opd_obdinfo; 54 }; 55 56 static int ofw_gpiobus_parse_gpios(struct gpiobus_softc *, 57 struct gpiobus_ivar *, phandle_t); 58 static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t, 59 phandle_t); 60 static void ofw_gpiobus_destroy_devinfo(struct ofw_gpiobus_devinfo *); 61 62 device_t 63 ofw_gpiobus_add_fdt_child(device_t bus, phandle_t child) 64 { 65 struct ofw_gpiobus_devinfo *dinfo; 66 device_t childdev; 67 68 /* 69 * Set up the GPIO child and OFW bus layer devinfo and add it to bus. 70 */ 71 dinfo = ofw_gpiobus_setup_devinfo(bus, child); 72 if (dinfo == NULL) 73 return (NULL); 74 childdev = device_add_child(bus, NULL, -1); 75 if (childdev == NULL) { 76 device_printf(bus, "could not add child: %s\n", 77 dinfo->opd_obdinfo.obd_name); 78 ofw_gpiobus_destroy_devinfo(dinfo); 79 return (NULL); 80 } 81 device_set_ivars(childdev, dinfo); 82 83 return (childdev); 84 } 85 86 static int 87 ofw_gpiobus_parse_gpios(struct gpiobus_softc *sc, struct gpiobus_ivar *dinfo, 88 phandle_t child) 89 { 90 int i, len; 91 pcell_t *gpios; 92 phandle_t gpio; 93 94 /* Retrieve the gpios property. */ 95 if ((len = OF_getproplen(child, "gpios")) < 0) 96 return (EINVAL); 97 gpios = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 98 if (gpios == NULL) 99 return (ENOMEM); 100 if (OF_getencprop(child, "gpios", gpios, len) < 0) { 101 free(gpios, M_DEVBUF); 102 return (EINVAL); 103 } 104 105 /* 106 * Each 'gpios' entry must contain 4 pcells. 107 * The first one is the GPIO controller phandler. 108 * Then the last three are the GPIO pin, the GPIO pin direction and 109 * the GPIO pin flags. 110 */ 111 if ((len / sizeof(pcell_t)) % 4) { 112 free(gpios, M_DEVBUF); 113 return (EINVAL); 114 } 115 dinfo->npins = len / (sizeof(pcell_t) * 4); 116 dinfo->pins = malloc(sizeof(uint32_t) * dinfo->npins, M_DEVBUF, 117 M_NOWAIT | M_ZERO); 118 if (dinfo->pins == NULL) { 119 free(gpios, M_DEVBUF); 120 return (ENOMEM); 121 } 122 123 for (i = 0; i < dinfo->npins; i++) { 124 125 /* Verify if we're attaching to the correct gpio controller. */ 126 gpio = OF_xref_phandle(gpios[i * 4 + 0]); 127 if (!OF_hasprop(gpio, "gpio-controller") || 128 gpio != ofw_bus_get_node(sc->sc_dev)) { 129 free(dinfo->pins, M_DEVBUF); 130 free(gpios, M_DEVBUF); 131 return (EINVAL); 132 } 133 134 /* Get the GPIO pin number. */ 135 dinfo->pins[i] = gpios[i * 4 + 1]; 136 /* gpios[i * 4 + 2] - GPIO pin direction */ 137 /* gpios[i * 4 + 3] - GPIO pin flags */ 138 139 if (dinfo->pins[i] > sc->sc_npins) { 140 device_printf(sc->sc_busdev, 141 "invalid pin %d, max: %d\n", 142 dinfo->pins[i], sc->sc_npins - 1); 143 free(dinfo->pins, M_DEVBUF); 144 free(gpios, M_DEVBUF); 145 return (EINVAL); 146 } 147 148 /* 149 * Mark pin as mapped and give warning if it's already mapped. 150 */ 151 if (sc->sc_pins_mapped[dinfo->pins[i]]) { 152 device_printf(sc->sc_busdev, 153 "warning: pin %d is already mapped\n", 154 dinfo->pins[i]); 155 free(dinfo->pins, M_DEVBUF); 156 free(gpios, M_DEVBUF); 157 return (EINVAL); 158 } 159 sc->sc_pins_mapped[dinfo->pins[i]] = 1; 160 } 161 162 free(gpios, M_DEVBUF); 163 164 return (0); 165 } 166 167 static struct ofw_gpiobus_devinfo * 168 ofw_gpiobus_setup_devinfo(device_t dev, phandle_t node) 169 { 170 struct gpiobus_softc *sc; 171 struct ofw_gpiobus_devinfo *dinfo; 172 173 sc = device_get_softc(dev); 174 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO); 175 if (dinfo == NULL) 176 return (NULL); 177 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) { 178 free(dinfo, M_DEVBUF); 179 return (NULL); 180 } 181 182 /* Parse the gpios property for the child. */ 183 if (ofw_gpiobus_parse_gpios(sc, &dinfo->opd_dinfo, node) != 0) { 184 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo); 185 free(dinfo, M_DEVBUF); 186 return (NULL); 187 } 188 189 return (dinfo); 190 } 191 192 static void 193 ofw_gpiobus_destroy_devinfo(struct ofw_gpiobus_devinfo *dinfo) 194 { 195 196 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo); 197 free(dinfo, M_DEVBUF); 198 } 199 200 static int 201 ofw_gpiobus_probe(device_t dev) 202 { 203 204 if (ofw_bus_get_node(dev) == -1) 205 return (ENXIO); 206 device_set_desc(dev, "OFW GPIO bus"); 207 208 return (0); 209 } 210 211 static int 212 ofw_gpiobus_attach(device_t dev) 213 { 214 struct gpiobus_softc *sc; 215 phandle_t child; 216 217 sc = GPIOBUS_SOFTC(dev); 218 sc->sc_busdev = dev; 219 sc->sc_dev = device_get_parent(dev); 220 221 /* Read the pin max. value */ 222 if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0) 223 return (ENXIO); 224 225 KASSERT(sc->sc_npins != 0, ("GPIO device with no pins")); 226 227 /* 228 * Increase to get number of pins. 229 */ 230 sc->sc_npins++; 231 232 sc->sc_pins_mapped = malloc(sizeof(int) * sc->sc_npins, M_DEVBUF, 233 M_NOWAIT | M_ZERO); 234 235 if (!sc->sc_pins_mapped) 236 return (ENOMEM); 237 238 /* Init the bus lock. */ 239 GPIOBUS_LOCK_INIT(sc); 240 241 bus_generic_probe(dev); 242 bus_enumerate_hinted_children(dev); 243 244 /* 245 * Attach the children represented in the device tree. 246 */ 247 for (child = OF_child(ofw_bus_get_node(dev)); child != 0; 248 child = OF_peer(child)) 249 if (ofw_gpiobus_add_fdt_child(dev, child) == NULL) 250 continue; 251 252 return (bus_generic_attach(dev)); 253 } 254 255 static device_t 256 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit) 257 { 258 device_t child; 259 struct ofw_gpiobus_devinfo *devi; 260 261 child = device_add_child_ordered(dev, order, name, unit); 262 if (child == NULL) 263 return (child); 264 devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF, 265 M_NOWAIT | M_ZERO); 266 if (devi == NULL) { 267 device_delete_child(dev, child); 268 return (0); 269 } 270 271 /* 272 * NULL all the OFW-related parts of the ivars for non-OFW 273 * children. 274 */ 275 devi->opd_obdinfo.obd_node = -1; 276 devi->opd_obdinfo.obd_name = NULL; 277 devi->opd_obdinfo.obd_compat = NULL; 278 devi->opd_obdinfo.obd_type = NULL; 279 devi->opd_obdinfo.obd_model = NULL; 280 281 device_set_ivars(child, devi); 282 283 return (child); 284 } 285 286 static int 287 ofw_gpiobus_print_child(device_t dev, device_t child) 288 { 289 struct ofw_gpiobus_devinfo *devi; 290 int retval = 0; 291 292 devi = device_get_ivars(child); 293 retval += bus_print_child_header(dev, child); 294 retval += printf(" at pin(s) "); 295 gpiobus_print_pins(&devi->opd_dinfo); 296 retval += bus_print_child_footer(dev, child); 297 298 return (retval); 299 } 300 301 static const struct ofw_bus_devinfo * 302 ofw_gpiobus_get_devinfo(device_t bus, device_t dev) 303 { 304 struct ofw_gpiobus_devinfo *dinfo; 305 306 dinfo = device_get_ivars(dev); 307 308 return (&dinfo->opd_obdinfo); 309 } 310 311 static device_method_t ofw_gpiobus_methods[] = { 312 /* Device interface */ 313 DEVMETHOD(device_probe, ofw_gpiobus_probe), 314 DEVMETHOD(device_attach, ofw_gpiobus_attach), 315 316 /* Bus interface */ 317 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 318 DEVMETHOD(bus_print_child, ofw_gpiobus_print_child), 319 DEVMETHOD(bus_add_child, ofw_gpiobus_add_child), 320 321 /* ofw_bus interface */ 322 DEVMETHOD(ofw_bus_get_devinfo, ofw_gpiobus_get_devinfo), 323 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 324 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 325 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 326 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 327 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 328 329 DEVMETHOD_END 330 }; 331 332 static devclass_t ofwgpiobus_devclass; 333 334 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods, 335 sizeof(struct gpiobus_softc), gpiobus_driver); 336 DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass, 0, 0); 337 MODULE_VERSION(ofw_gpiobus, 1); 338 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1); 339