mtk_ehci.c revision 308401
1#include <sys/cdefs.h> 2__FBSDID("$FreeBSD: stable/11/sys/mips/mediatek/mtk_ehci.c 308401 2016-11-07 08:36:06Z hselasky $"); 3 4/*- 5 * Copyright (c) 2015 Stanislav Galabov. All rights reserved. 6 * Copyright (c) 2010,2011 Aleksandr Rybalko. All rights reserved. 7 * Copyright (c) 2007-2008 Hans Petter Selasky. All rights reserved. 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#include <sys/stdint.h> 32#include <sys/stddef.h> 33#include <sys/param.h> 34#include <sys/queue.h> 35#include <sys/types.h> 36#include <sys/systm.h> 37#include <sys/kernel.h> 38#include <sys/bus.h> 39#include <sys/module.h> 40#include <sys/lock.h> 41#include <sys/mutex.h> 42#include <sys/condvar.h> 43#include <sys/sysctl.h> 44#include <sys/sx.h> 45#include <sys/unistd.h> 46#include <sys/callout.h> 47#include <sys/malloc.h> 48#include <sys/priv.h> 49#include <sys/rman.h> 50 51#include <dev/usb/usb.h> 52#include <dev/usb/usbdi.h> 53 54#include <dev/usb/usb_core.h> 55#include <dev/usb/usb_busdma.h> 56#include <dev/usb/usb_process.h> 57#include <dev/usb/usb_util.h> 58 59#include <dev/usb/usb_controller.h> 60#include <dev/usb/usb_bus.h> 61 62#include <dev/usb/controller/ehci.h> 63 64#include <dev/ofw/openfirm.h> 65#include <dev/ofw/ofw_bus.h> 66#include <dev/ofw/ofw_bus_subr.h> 67 68#define EHCI_HC_DEVSTR "MTK USB 2.0 Controller" 69 70static device_probe_t ehci_fdt_probe; 71static device_attach_t ehci_fdt_attach; 72static device_detach_t ehci_fdt_detach; 73 74static int 75ehci_fdt_probe(device_t self) 76{ 77 78 if (!ofw_bus_status_okay(self)) 79 return (ENXIO); 80 81 if (!ofw_bus_is_compatible(self, "generic-ehci")) 82 return (ENXIO); 83 84 device_set_desc(self, EHCI_HC_DEVSTR); 85 86 return (BUS_PROBE_DEFAULT); 87} 88 89static int 90ehci_fdt_attach(device_t self) 91{ 92 ehci_softc_t *sc = device_get_softc(self); 93 int err; 94 int rid; 95 96 /* initialise some bus fields */ 97 sc->sc_bus.parent = self; 98 sc->sc_bus.devices = sc->sc_devices; 99 sc->sc_bus.devices_max = EHCI_MAX_DEVICES; 100 sc->sc_bus.dma_bits = 32; 101 102 /* get all DMA memory */ 103 if (usb_bus_mem_alloc_all(&sc->sc_bus, 104 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) { 105 printf("No mem\n"); 106 return (ENOMEM); 107 } 108 109 rid = 0; 110 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, 111 RF_ACTIVE); 112 if (!sc->sc_io_res) { 113 device_printf(self, "Could not map memory\n"); 114 goto error; 115 } 116 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 117 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 118 sc->sc_io_size = rman_get_size(sc->sc_io_res); 119 120 rid = 0; 121 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 122 RF_SHAREABLE | RF_ACTIVE); 123 if (sc->sc_irq_res == NULL) { 124 device_printf(self, "Could not allocate irq\n"); 125 goto error; 126 } 127 128 sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 129 if (!(sc->sc_bus.bdev)) { 130 device_printf(self, "Could not add USB device\n"); 131 goto error; 132 } 133 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 134 device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR); 135 136 sprintf(sc->sc_vendor, "MediaTek"); 137 138 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 139 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl); 140 if (err) { 141 device_printf(self, "Could not setup irq, %d\n", err); 142 sc->sc_intr_hdl = NULL; 143 goto error; 144 } 145 146 err = ehci_init(sc); 147 if (!err) { 148 err = device_probe_and_attach(sc->sc_bus.bdev); 149 } 150 if (err) { 151 device_printf(self, "USB init failed err=%d\n", err); 152 goto error; 153 } 154 return (0); 155 156error: 157 ehci_fdt_detach(self); 158 return (ENXIO); 159} 160 161static int 162ehci_fdt_detach(device_t self) 163{ 164 ehci_softc_t *sc = device_get_softc(self); 165 int err; 166 167 /* during module unload there are lots of children leftover */ 168 device_delete_children(self); 169 170 if (sc->sc_irq_res && sc->sc_intr_hdl) { 171 /* 172 * only call ehci_detach() after ehci_init() 173 */ 174 ehci_detach(sc); 175 176 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 177 if (err) 178 device_printf(self, "Could not tear down irq, %d\n", 179 err); 180 sc->sc_intr_hdl = NULL; 181 } 182 if (sc->sc_irq_res) { 183 bus_release_resource(self, SYS_RES_IRQ, 0, 184 sc->sc_irq_res); 185 sc->sc_irq_res = NULL; 186 } 187 if (sc->sc_io_res) { 188 bus_release_resource(self, SYS_RES_MEMORY, 0, 189 sc->sc_io_res); 190 sc->sc_io_res = NULL; 191 } 192 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc); 193 194 return (0); 195} 196 197static device_method_t ehci_fdt_methods[] = { 198 /* Device interface */ 199 DEVMETHOD(device_probe, ehci_fdt_probe), 200 DEVMETHOD(device_attach, ehci_fdt_attach), 201 DEVMETHOD(device_detach, ehci_fdt_detach), 202 DEVMETHOD(device_suspend, bus_generic_suspend), 203 DEVMETHOD(device_resume, bus_generic_resume), 204 DEVMETHOD(device_shutdown, bus_generic_shutdown), 205 206 DEVMETHOD_END 207}; 208 209static driver_t ehci_fdt_driver = { 210 .name = "ehci", 211 .methods = ehci_fdt_methods, 212 .size = sizeof(ehci_softc_t), 213}; 214 215static devclass_t ehci_fdt_devclass; 216 217DRIVER_MODULE(ehci, simplebus, ehci_fdt_driver, ehci_fdt_devclass, 0, 0); 218