saf1761_otg_fdt.c revision 266215
1/* $FreeBSD: head/sys/dev/usb/controller/saf1761_dci_fdt.c 266215 2014-05-16 10:35:21Z 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_dci.h> 77#include <dev/usb/controller/saf1761_dci_reg.h> 78 79static device_probe_t saf1761_dci_fdt_probe; 80static device_attach_t saf1761_dci_fdt_attach; 81static device_detach_t saf1761_dci_fdt_detach; 82 83static device_method_t saf1761_dci_methods[] = { 84 /* Device interface */ 85 DEVMETHOD(device_probe, saf1761_dci_fdt_probe), 86 DEVMETHOD(device_attach, saf1761_dci_fdt_attach), 87 DEVMETHOD(device_detach, saf1761_dci_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_dci_driver = { 96 .name = "saf1761", 97 .methods = saf1761_dci_methods, 98 .size = sizeof(struct saf1761_dci_softc), 99}; 100 101static devclass_t saf1761_dci_devclass; 102 103DRIVER_MODULE(saf1761, simplebus, saf1761_dci_driver, saf1761_dci_devclass, 0, 0); 104MODULE_DEPEND(saf1761, usb, 1, 1, 1); 105 106static int 107saf1761_dci_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_dci_fdt_attach(device_t dev) 122{ 123 struct saf1761_dci_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 /* initialise some bus fields */ 160 sc->sc_bus.parent = dev; 161 sc->sc_bus.devices = sc->sc_devices; 162 sc->sc_bus.devices_max = SOTG_MAX_DEVICES; 163 164 /* get all DMA memory */ 165 if (usb_bus_mem_alloc_all(&sc->sc_bus, 166 USB_GET_DMA_TAG(dev), NULL)) { 167 return (ENOMEM); 168 } 169 rid = 0; 170 sc->sc_io_res = 171 bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 172 173 if (!sc->sc_io_res) { 174 goto error; 175 } 176 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 177 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 178 sc->sc_io_size = rman_get_size(sc->sc_io_res); 179 180 rid = 0; 181 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 182 RF_SHAREABLE | RF_ACTIVE); 183 if (sc->sc_irq_res == NULL) { 184 goto error; 185 } 186 sc->sc_bus.bdev = device_add_child(dev, "usbus", -1); 187 if (!(sc->sc_bus.bdev)) { 188 goto error; 189 } 190 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 191 192 err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 193 NULL, (driver_intr_t *)saf1761_dci_interrupt, sc, &sc->sc_intr_hdl); 194 if (err) { 195 sc->sc_intr_hdl = NULL; 196 goto error; 197 } 198 err = saf1761_dci_init(sc); 199 if (err) { 200 device_printf(dev, "Init failed\n"); 201 goto error; 202 } 203 err = device_probe_and_attach(sc->sc_bus.bdev); 204 if (err) { 205 device_printf(dev, "USB probe and attach failed\n"); 206 goto error; 207 } 208 return (0); 209 210error: 211 saf1761_dci_fdt_detach(dev); 212 return (ENXIO); 213} 214 215static int 216saf1761_dci_fdt_detach(device_t dev) 217{ 218 struct saf1761_dci_softc *sc = device_get_softc(dev); 219 device_t bdev; 220 int err; 221 222 if (sc->sc_bus.bdev) { 223 bdev = sc->sc_bus.bdev; 224 device_detach(bdev); 225 device_delete_child(dev, bdev); 226 } 227 /* during module unload there are lots of children leftover */ 228 device_delete_children(dev); 229 230 if (sc->sc_irq_res && sc->sc_intr_hdl) { 231 /* 232 * Only call uninit() after init() 233 */ 234 saf1761_dci_uninit(sc); 235 236 err = bus_teardown_intr(dev, sc->sc_irq_res, 237 sc->sc_intr_hdl); 238 sc->sc_intr_hdl = NULL; 239 } 240 if (sc->sc_irq_res) { 241 bus_release_resource(dev, SYS_RES_IRQ, 0, 242 sc->sc_irq_res); 243 sc->sc_irq_res = NULL; 244 } 245 if (sc->sc_io_res) { 246 bus_release_resource(dev, SYS_RES_IOPORT, 0, 247 sc->sc_io_res); 248 sc->sc_io_res = NULL; 249 } 250 usb_bus_mem_free_all(&sc->sc_bus, NULL); 251 252 return (0); 253} 254