saf1761_otg_fdt.c revision 266831
1/* $FreeBSD: head/sys/dev/usb/controller/saf1761_otg_fdt.c 266831 2014-05-29 10:06:18Z hselasky $ */ 2/*- 3 * Copyright (c) 2014 Hans Petter Selasky <hselasky@FreeBSD.org> 4 * All rights reserved. 5 * 6 * This software was developed by SRI International and the University of 7 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 8 * ("CTSRD"), as part of the DARPA CRASH research programme. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#ifdef USB_GLOBAL_INCLUDE_FILE 33#include USB_GLOBAL_INCLUDE_FILE 34#else 35#include <sys/stdint.h> 36#include <sys/stddef.h> 37#include <sys/param.h> 38#include <sys/queue.h> 39#include <sys/types.h> 40#include <sys/systm.h> 41#include <sys/kernel.h> 42#include <sys/bus.h> 43#include <sys/module.h> 44#include <sys/lock.h> 45#include <sys/mutex.h> 46#include <sys/condvar.h> 47#include <sys/sysctl.h> 48#include <sys/sx.h> 49#include <sys/unistd.h> 50#include <sys/callout.h> 51#include <sys/malloc.h> 52#include <sys/priv.h> 53#include <sys/rman.h> 54 55#include <dev/fdt/fdt_common.h> 56 57#include <dev/ofw/openfirm.h> 58#include <dev/ofw/ofw_bus.h> 59#include <dev/ofw/ofw_bus_subr.h> 60 61#include <dev/usb/usb.h> 62#include <dev/usb/usbdi.h> 63 64#include <dev/usb/usb_core.h> 65#include <dev/usb/usb_busdma.h> 66#include <dev/usb/usb_process.h> 67#include <dev/usb/usb_transfer.h> 68#include <dev/usb/usb_device.h> 69#include <dev/usb/usb_hub.h> 70#include <dev/usb/usb_util.h> 71 72#include <dev/usb/usb_controller.h> 73#include <dev/usb/usb_bus.h> 74#endif /* USB_GLOBAL_INCLUDE_FILE */ 75 76#include <dev/usb/controller/saf1761_otg.h> 77#include <dev/usb/controller/saf1761_otg_reg.h> 78 79static device_probe_t saf1761_otg_fdt_probe; 80static device_attach_t saf1761_otg_fdt_attach; 81static device_detach_t saf1761_otg_fdt_detach; 82 83static device_method_t saf1761_otg_methods[] = { 84 /* Device interface */ 85 DEVMETHOD(device_probe, saf1761_otg_fdt_probe), 86 DEVMETHOD(device_attach, saf1761_otg_fdt_attach), 87 DEVMETHOD(device_detach, saf1761_otg_fdt_detach), 88 DEVMETHOD(device_suspend, bus_generic_suspend), 89 DEVMETHOD(device_resume, bus_generic_resume), 90 DEVMETHOD(device_shutdown, bus_generic_shutdown), 91 92 DEVMETHOD_END 93}; 94 95static driver_t saf1761_otg_driver = { 96 .name = "saf1761", 97 .methods = saf1761_otg_methods, 98 .size = sizeof(struct saf1761_otg_softc), 99}; 100 101static devclass_t saf1761_otg_devclass; 102 103DRIVER_MODULE(saf1761, simplebus, saf1761_otg_driver, saf1761_otg_devclass, 0, 0); 104MODULE_DEPEND(saf1761, usb, 1, 1, 1); 105 106static int 107saf1761_otg_fdt_probe(device_t dev) 108{ 109 if (!ofw_bus_status_okay(dev)) 110 return (ENXIO); 111 112 if (!ofw_bus_is_compatible(dev, "nxp,usb-isp1761")) 113 return (ENXIO); 114 115 device_set_desc(dev, "ISP1761/SAF1761 DCI USB 2.0 Device Controller"); 116 117 return (0); 118} 119 120static int 121saf1761_otg_fdt_attach(device_t dev) 122{ 123 struct saf1761_otg_softc *sc = device_get_softc(dev); 124 char param[24]; 125 int err; 126 int rid; 127 128 /* get configuration from FDT */ 129 130 /* get bus-width, if any */ 131 if (OF_getprop(ofw_bus_get_node(dev), "bus-width", 132 ¶m, sizeof(param)) > 0) { 133 param[sizeof(param) - 1] = 0; 134 if (strcmp(param, "32") == 0) 135 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH; 136 } else { 137 /* assume 32-bit data bus */ 138 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH; 139 } 140 141 /* get analog over-current setting */ 142 if (OF_getprop(ofw_bus_get_node(dev), "analog-oc", 143 ¶m, sizeof(param)) > 0) { 144 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_ANA_DIGI_OC; 145 } 146 147 /* get DACK polarity */ 148 if (OF_getprop(ofw_bus_get_node(dev), "dack-polarity", 149 ¶m, sizeof(param)) > 0) { 150 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DACK_POL; 151 } 152 153 /* get DREQ polarity */ 154 if (OF_getprop(ofw_bus_get_node(dev), "dreq-polarity", 155 ¶m, sizeof(param)) > 0) { 156 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DREQ_POL; 157 } 158 159 /* get IRQ polarity */ 160 if (OF_getprop(ofw_bus_get_node(dev), "int-polarity", 161 ¶m, sizeof(param)) > 0) { 162 sc->sc_interrupt_cfg |= SOTG_INTERRUPT_CFG_INTPOL; 163 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_INTR_POL; 164 } 165 166 /* get IRQ level triggering */ 167 if (OF_getprop(ofw_bus_get_node(dev), "int-level", 168 ¶m, sizeof(param)) > 0) { 169 sc->sc_interrupt_cfg |= SOTG_INTERRUPT_CFG_INTLVL; 170 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_INTR_LEVEL; 171 } 172 173 /* initialise some bus fields */ 174 sc->sc_bus.parent = dev; 175 sc->sc_bus.devices = sc->sc_devices; 176 sc->sc_bus.devices_max = SOTG_MAX_DEVICES; 177 178 /* get all DMA memory */ 179 if (usb_bus_mem_alloc_all(&sc->sc_bus, 180 USB_GET_DMA_TAG(dev), NULL)) { 181 return (ENOMEM); 182 } 183 rid = 0; 184 sc->sc_io_res = 185 bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 186 187 if (sc->sc_io_res == NULL) 188 goto error; 189 190 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 191 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 192 sc->sc_io_size = rman_get_size(sc->sc_io_res); 193 194 /* try to allocate the HC interrupt first */ 195 rid = 1; 196 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 197 RF_SHAREABLE | RF_ACTIVE); 198 if (sc->sc_irq_res == NULL) { 199 /* try to allocate a common IRQ second */ 200 rid = 0; 201 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 202 RF_SHAREABLE | RF_ACTIVE); 203 if (sc->sc_irq_res == NULL) 204 goto error; 205 } 206 207 sc->sc_bus.bdev = device_add_child(dev, "usbus", -1); 208 if (sc->sc_bus.bdev == NULL) 209 goto error; 210 211 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 212 213 err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_TTY | INTR_MPSAFE, 214 &saf1761_otg_filter_interrupt, &saf1761_otg_interrupt, sc, &sc->sc_intr_hdl); 215 if (err) { 216 sc->sc_intr_hdl = NULL; 217 goto error; 218 } 219 err = saf1761_otg_init(sc); 220 if (err) { 221 device_printf(dev, "Init failed\n"); 222 goto error; 223 } 224 err = device_probe_and_attach(sc->sc_bus.bdev); 225 if (err) { 226 device_printf(dev, "USB probe and attach failed\n"); 227 goto error; 228 } 229 return (0); 230 231error: 232 saf1761_otg_fdt_detach(dev); 233 return (ENXIO); 234} 235 236static int 237saf1761_otg_fdt_detach(device_t dev) 238{ 239 struct saf1761_otg_softc *sc = device_get_softc(dev); 240 device_t bdev; 241 int err; 242 243 if (sc->sc_bus.bdev) { 244 bdev = sc->sc_bus.bdev; 245 device_detach(bdev); 246 device_delete_child(dev, bdev); 247 } 248 /* during module unload there are lots of children leftover */ 249 device_delete_children(dev); 250 251 if (sc->sc_irq_res && sc->sc_intr_hdl) { 252 /* 253 * Only call uninit() after init() 254 */ 255 saf1761_otg_uninit(sc); 256 257 err = bus_teardown_intr(dev, sc->sc_irq_res, 258 sc->sc_intr_hdl); 259 sc->sc_intr_hdl = NULL; 260 } 261 if (sc->sc_irq_res) { 262 bus_release_resource(dev, SYS_RES_IRQ, 0, 263 sc->sc_irq_res); 264 sc->sc_irq_res = NULL; 265 } 266 if (sc->sc_io_res) { 267 bus_release_resource(dev, SYS_RES_MEMORY, 0, 268 sc->sc_io_res); 269 sc->sc_io_res = NULL; 270 } 271 usb_bus_mem_free_all(&sc->sc_bus, NULL); 272 273 return (0); 274} 275