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 "opt_bus.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/bus.h> 40 #include <sys/rman.h> 41 #include <sys/condvar.h> 42 #include <sys/kernel.h> 43 #include <sys/module.h> 44 45 #include <machine/bus.h> 46 47 #include <dev/usb/usb.h> 48 #include <dev/usb/usbdi.h> 49 50 #include <dev/usb/usb_core.h> 51 #include <dev/usb/usb_busdma.h> 52 #include <dev/usb/usb_process.h> 53 #include <dev/usb/usb_util.h> 54 55 #include <dev/usb/usb_controller.h> 56 #include <dev/usb/usb_bus.h> 57 #include <dev/usb/controller/ehci.h> 58 #include <dev/usb/controller/ehcireg.h> 59 60 #include "generic_ehci.h" 61 62 int 63 generic_ehci_attach(device_t self) 64 { 65 ehci_softc_t *sc = device_get_softc(self); 66 int err; 67 int rid; 68 69 /* initialise some bus fields */ 70 sc->sc_bus.parent = self; 71 sc->sc_bus.devices = sc->sc_devices; 72 sc->sc_bus.devices_max = EHCI_MAX_DEVICES; 73 sc->sc_bus.dma_bits = 32; 74 75 /* get all DMA memory */ 76 if (usb_bus_mem_alloc_all(&sc->sc_bus, 77 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) { 78 return (ENOMEM); 79 } 80 81 sc->sc_bus.usbrev = USB_REV_2_0; 82 83 rid = 0; 84 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, 85 RF_ACTIVE); 86 if (!sc->sc_io_res) { 87 device_printf(self, "Could not map memory\n"); 88 goto error; 89 } 90 91 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 92 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 93 sc->sc_io_size = rman_get_size(sc->sc_io_res); 94 95 rid = 0; 96 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 97 RF_SHAREABLE | RF_ACTIVE); 98 if (sc->sc_irq_res == NULL) { 99 device_printf(self, "Could not allocate irq\n"); 100 goto error; 101 } 102 sc->sc_bus.bdev = device_add_child(self, "usbus", DEVICE_UNIT_ANY); 103 if (!sc->sc_bus.bdev) { 104 device_printf(self, "Could not add USB device\n"); 105 goto error; 106 } 107 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 108 109 strlcpy(sc->sc_vendor, "Generic", sizeof(sc->sc_vendor)); 110 111 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 112 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl); 113 if (err) { 114 device_printf(self, "Could not setup irq, %d\n", err); 115 sc->sc_intr_hdl = NULL; 116 goto error; 117 } 118 119 sc->sc_flags |= EHCI_SCFLG_DONTRESET; 120 121 err = ehci_init(sc); 122 if (!err) 123 err = device_probe_and_attach(sc->sc_bus.bdev); 124 if (err) 125 goto error; 126 127 return (0); 128 129 error: 130 generic_ehci_detach(self); 131 return (ENXIO); 132 } 133 134 int 135 generic_ehci_detach(device_t self) 136 { 137 ehci_softc_t *sc = device_get_softc(self); 138 int err; 139 140 /* during module unload there are lots of children leftover */ 141 err = bus_generic_detach(self); 142 if (err != 0) 143 return (err); 144 145 if (sc->sc_irq_res && sc->sc_intr_hdl) { 146 /* 147 * only call ehci_detach() after ehci_init() 148 */ 149 ehci_detach(sc); 150 151 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 152 153 if (err) 154 /* XXX or should we panic? */ 155 device_printf(self, "Could not tear down irq, %d\n", 156 err); 157 sc->sc_intr_hdl = NULL; 158 } 159 160 if (sc->sc_irq_res) { 161 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); 162 sc->sc_irq_res = NULL; 163 } 164 if (sc->sc_io_res) { 165 bus_release_resource(self, SYS_RES_MEMORY, 0, 166 sc->sc_io_res); 167 sc->sc_io_res = NULL; 168 } 169 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc); 170 171 return (0); 172 } 173 174 static device_method_t ehci_methods[] = { 175 /* Device interface */ 176 DEVMETHOD(device_attach, generic_ehci_attach), 177 DEVMETHOD(device_detach, generic_ehci_detach), 178 DEVMETHOD(device_suspend, bus_generic_suspend), 179 DEVMETHOD(device_resume, bus_generic_resume), 180 DEVMETHOD(device_shutdown, bus_generic_shutdown), 181 182 DEVMETHOD_END 183 }; 184 185 driver_t generic_ehci_driver = { 186 .name = "ehci", 187 .methods = ehci_methods, 188 .size = sizeof(ehci_softc_t), 189 }; 190