1 /*- 2 * Copyright (c) 2012 Ganbold Tsagaankhuu <ganbold@freebsd.org> 3 * Copyright (c) 2016 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * This software was developed by Andrew Turner under 7 * sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 /* 32 * Generic EHCI driver based on the Allwinner A10 EHCI driver 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include "opt_bus.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/bus.h> 43 #include <sys/rman.h> 44 #include <sys/condvar.h> 45 #include <sys/kernel.h> 46 #include <sys/module.h> 47 48 #include <machine/bus.h> 49 50 #include <dev/usb/usb.h> 51 #include <dev/usb/usbdi.h> 52 53 #include <dev/usb/usb_core.h> 54 #include <dev/usb/usb_busdma.h> 55 #include <dev/usb/usb_process.h> 56 #include <dev/usb/usb_util.h> 57 58 #include <dev/usb/usb_controller.h> 59 #include <dev/usb/usb_bus.h> 60 #include <dev/usb/controller/ehci.h> 61 #include <dev/usb/controller/ehcireg.h> 62 63 #include "generic_ehci.h" 64 65 int 66 generic_ehci_attach(device_t self) 67 { 68 ehci_softc_t *sc = device_get_softc(self); 69 int err; 70 int rid; 71 72 /* initialise some bus fields */ 73 sc->sc_bus.parent = self; 74 sc->sc_bus.devices = sc->sc_devices; 75 sc->sc_bus.devices_max = EHCI_MAX_DEVICES; 76 sc->sc_bus.dma_bits = 32; 77 78 /* get all DMA memory */ 79 if (usb_bus_mem_alloc_all(&sc->sc_bus, 80 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) { 81 return (ENOMEM); 82 } 83 84 sc->sc_bus.usbrev = USB_REV_2_0; 85 86 rid = 0; 87 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, 88 RF_ACTIVE); 89 if (!sc->sc_io_res) { 90 device_printf(self, "Could not map memory\n"); 91 goto error; 92 } 93 94 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 95 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 96 sc->sc_io_size = rman_get_size(sc->sc_io_res); 97 98 rid = 0; 99 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 100 RF_SHAREABLE | RF_ACTIVE); 101 if (sc->sc_irq_res == NULL) { 102 device_printf(self, "Could not allocate irq\n"); 103 goto error; 104 } 105 sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 106 if (!sc->sc_bus.bdev) { 107 device_printf(self, "Could not add USB device\n"); 108 goto error; 109 } 110 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 111 112 strlcpy(sc->sc_vendor, "Generic", sizeof(sc->sc_vendor)); 113 114 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 115 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl); 116 if (err) { 117 device_printf(self, "Could not setup irq, %d\n", err); 118 sc->sc_intr_hdl = NULL; 119 goto error; 120 } 121 122 sc->sc_flags |= EHCI_SCFLG_DONTRESET; 123 124 err = ehci_init(sc); 125 if (!err) 126 err = device_probe_and_attach(sc->sc_bus.bdev); 127 if (err) 128 goto error; 129 130 return (0); 131 132 error: 133 generic_ehci_detach(self); 134 return (ENXIO); 135 } 136 137 int 138 generic_ehci_detach(device_t self) 139 { 140 ehci_softc_t *sc = device_get_softc(self); 141 int err; 142 143 /* during module unload there are lots of children leftover */ 144 device_delete_children(self); 145 146 if (sc->sc_irq_res && sc->sc_intr_hdl) { 147 /* 148 * only call ehci_detach() after ehci_init() 149 */ 150 ehci_detach(sc); 151 152 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 153 154 if (err) 155 /* XXX or should we panic? */ 156 device_printf(self, "Could not tear down irq, %d\n", 157 err); 158 sc->sc_intr_hdl = NULL; 159 } 160 161 if (sc->sc_irq_res) { 162 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); 163 sc->sc_irq_res = NULL; 164 } 165 if (sc->sc_io_res) { 166 bus_release_resource(self, SYS_RES_MEMORY, 0, 167 sc->sc_io_res); 168 sc->sc_io_res = NULL; 169 } 170 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc); 171 172 return (0); 173 } 174 175 static device_method_t ehci_methods[] = { 176 /* Device interface */ 177 DEVMETHOD(device_attach, generic_ehci_attach), 178 DEVMETHOD(device_detach, generic_ehci_detach), 179 DEVMETHOD(device_suspend, bus_generic_suspend), 180 DEVMETHOD(device_resume, bus_generic_resume), 181 DEVMETHOD(device_shutdown, bus_generic_shutdown), 182 183 DEVMETHOD_END 184 }; 185 186 driver_t generic_ehci_driver = { 187 .name = "ehci", 188 .methods = ehci_methods, 189 .size = sizeof(ehci_softc_t), 190 }; 191