1297670Ssgalabov#include <sys/cdefs.h> 2297670Ssgalabov__FBSDID("$FreeBSD: stable/11/sys/mips/mediatek/mtk_ohci.c 308401 2016-11-07 08:36:06Z hselasky $"); 3297670Ssgalabov 4297670Ssgalabov/*- 5297670Ssgalabov * Copyright (c) 2015 Stanislav Galabov. All rights reserved. 6297670Ssgalabov * Copyright (c) 2010,2011 Aleksandr Rybalko. All rights reserved. 7297670Ssgalabov * Copyright (c) 2007-2008 Hans Petter Selasky. All rights reserved. 8297670Ssgalabov * 9297670Ssgalabov * Redistribution and use in source and binary forms, with or without 10297670Ssgalabov * modification, are permitted provided that the following conditions 11297670Ssgalabov * are met: 12297670Ssgalabov * 1. Redistributions of source code must retain the above copyright 13297670Ssgalabov * notice, this list of conditions and the following disclaimer. 14297670Ssgalabov * 2. Redistributions in binary form must reproduce the above copyright 15297670Ssgalabov * notice, this list of conditions and the following disclaimer in the 16297670Ssgalabov * documentation and/or other materials provided with the distribution. 17297670Ssgalabov * 18297670Ssgalabov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19297670Ssgalabov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20297670Ssgalabov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21297670Ssgalabov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22297670Ssgalabov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23297670Ssgalabov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24297670Ssgalabov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25297670Ssgalabov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26297670Ssgalabov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27297670Ssgalabov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28297670Ssgalabov * SUCH DAMAGE. 29297670Ssgalabov */ 30297670Ssgalabov 31297670Ssgalabov#include <sys/stdint.h> 32297670Ssgalabov#include <sys/stddef.h> 33297670Ssgalabov#include <sys/param.h> 34297670Ssgalabov#include <sys/queue.h> 35297670Ssgalabov#include <sys/types.h> 36297670Ssgalabov#include <sys/systm.h> 37297670Ssgalabov#include <sys/kernel.h> 38297670Ssgalabov#include <sys/bus.h> 39297670Ssgalabov#include <sys/module.h> 40297670Ssgalabov#include <sys/lock.h> 41297670Ssgalabov#include <sys/mutex.h> 42297670Ssgalabov#include <sys/condvar.h> 43297670Ssgalabov#include <sys/sysctl.h> 44297670Ssgalabov#include <sys/sx.h> 45297670Ssgalabov#include <sys/unistd.h> 46297670Ssgalabov#include <sys/callout.h> 47297670Ssgalabov#include <sys/malloc.h> 48297670Ssgalabov#include <sys/priv.h> 49297670Ssgalabov#include <sys/rman.h> 50297670Ssgalabov 51297670Ssgalabov#include <dev/usb/usb.h> 52297670Ssgalabov#include <dev/usb/usbdi.h> 53297670Ssgalabov 54297670Ssgalabov#include <dev/usb/usb_core.h> 55297670Ssgalabov#include <dev/usb/usb_busdma.h> 56297670Ssgalabov#include <dev/usb/usb_process.h> 57297670Ssgalabov#include <dev/usb/usb_util.h> 58297670Ssgalabov 59297670Ssgalabov#include <dev/usb/usb_controller.h> 60297670Ssgalabov#include <dev/usb/usb_bus.h> 61297670Ssgalabov 62297670Ssgalabov#include <dev/usb/controller/ohci.h> 63297670Ssgalabov 64297670Ssgalabov#include <dev/ofw/openfirm.h> 65297670Ssgalabov#include <dev/ofw/ofw_bus.h> 66297670Ssgalabov#include <dev/ofw/ofw_bus_subr.h> 67297670Ssgalabov 68297670Ssgalabov#define OHCI_HC_DEVSTR "MTK USB Controller" 69297670Ssgalabov 70297670Ssgalabovstatic device_probe_t ohci_fdt_probe; 71297670Ssgalabovstatic device_attach_t ohci_fdt_attach; 72297670Ssgalabovstatic device_detach_t ohci_fdt_detach; 73297670Ssgalabov 74297670Ssgalabovstatic int 75297670Ssgalabovohci_fdt_probe(device_t self) 76297670Ssgalabov{ 77297670Ssgalabov 78297670Ssgalabov if (!ofw_bus_status_okay(self)) 79297670Ssgalabov return (ENXIO); 80297670Ssgalabov 81298059Ssgalabov if (!ofw_bus_is_compatible(self, "generic-ohci")) 82297670Ssgalabov return (ENXIO); 83297670Ssgalabov 84297670Ssgalabov device_set_desc(self, OHCI_HC_DEVSTR); 85297670Ssgalabov 86297670Ssgalabov return (BUS_PROBE_DEFAULT); 87297670Ssgalabov} 88297670Ssgalabov 89297670Ssgalabovstatic int 90297670Ssgalabovohci_fdt_attach(device_t self) 91297670Ssgalabov{ 92297670Ssgalabov ohci_softc_t *sc = device_get_softc(self); 93297670Ssgalabov int err; 94297670Ssgalabov int rid; 95297670Ssgalabov 96297670Ssgalabov /* initialise some bus fields */ 97297670Ssgalabov sc->sc_bus.parent = self; 98297670Ssgalabov sc->sc_bus.devices = sc->sc_devices; 99297670Ssgalabov sc->sc_bus.devices_max = OHCI_MAX_DEVICES; 100297670Ssgalabov sc->sc_bus.dma_bits = 32; 101297670Ssgalabov 102297670Ssgalabov /* get all DMA memory */ 103297670Ssgalabov if (usb_bus_mem_alloc_all(&sc->sc_bus, 104297670Ssgalabov USB_GET_DMA_TAG(self), &ohci_iterate_hw_softc)) { 105297670Ssgalabov printf("No mem\n"); 106297670Ssgalabov return (ENOMEM); 107297670Ssgalabov } 108297670Ssgalabov 109297670Ssgalabov rid = 0; 110297670Ssgalabov sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, 111297670Ssgalabov RF_ACTIVE); 112297670Ssgalabov if (!sc->sc_io_res) { 113297670Ssgalabov device_printf(self, "Could not map memory\n"); 114297670Ssgalabov goto error; 115297670Ssgalabov } 116297670Ssgalabov sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 117297670Ssgalabov sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 118297670Ssgalabov sc->sc_io_size = rman_get_size(sc->sc_io_res); 119297670Ssgalabov 120297670Ssgalabov rid = 0; 121297670Ssgalabov sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 122297670Ssgalabov RF_SHAREABLE | RF_ACTIVE); 123297670Ssgalabov if (sc->sc_irq_res == NULL) { 124297670Ssgalabov device_printf(self, "Could not allocate irq\n"); 125297670Ssgalabov goto error; 126297670Ssgalabov } 127297670Ssgalabov 128297670Ssgalabov sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 129297670Ssgalabov if (!(sc->sc_bus.bdev)) { 130297670Ssgalabov device_printf(self, "Could not add USB device\n"); 131297670Ssgalabov goto error; 132297670Ssgalabov } 133297670Ssgalabov device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 134297670Ssgalabov device_set_desc(sc->sc_bus.bdev, OHCI_HC_DEVSTR); 135297670Ssgalabov 136297670Ssgalabov sprintf(sc->sc_vendor, "MediaTek"); 137297670Ssgalabov 138297670Ssgalabov err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 139297670Ssgalabov NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl); 140297670Ssgalabov if (err) { 141297670Ssgalabov device_printf(self, "Could not setup irq, %d\n", err); 142297670Ssgalabov sc->sc_intr_hdl = NULL; 143297670Ssgalabov goto error; 144297670Ssgalabov } 145297670Ssgalabov 146297670Ssgalabov err = ohci_init(sc); 147297670Ssgalabov if (!err) { 148297670Ssgalabov err = device_probe_and_attach(sc->sc_bus.bdev); 149297670Ssgalabov } 150297670Ssgalabov if (err) { 151297670Ssgalabov device_printf(self, "USB init failed err=%d\n", err); 152297670Ssgalabov goto error; 153297670Ssgalabov } 154297670Ssgalabov return (0); 155297670Ssgalabov 156297670Ssgalaboverror: 157297670Ssgalabov ohci_fdt_detach(self); 158297670Ssgalabov return (ENXIO); 159297670Ssgalabov} 160297670Ssgalabov 161297670Ssgalabovstatic int 162297670Ssgalabovohci_fdt_detach(device_t self) 163297670Ssgalabov{ 164297670Ssgalabov ohci_softc_t *sc = device_get_softc(self); 165297670Ssgalabov int err; 166297670Ssgalabov 167297670Ssgalabov /* during module unload there are lots of children leftover */ 168297670Ssgalabov device_delete_children(self); 169297670Ssgalabov 170297670Ssgalabov if (sc->sc_irq_res && sc->sc_intr_hdl) { 171297670Ssgalabov /* 172297670Ssgalabov * only call ohci_detach() after ohci_init() 173297670Ssgalabov */ 174297670Ssgalabov ohci_detach(sc); 175297670Ssgalabov 176297670Ssgalabov err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 177297670Ssgalabov if (err) 178297670Ssgalabov device_printf(self, "Could not tear down irq, %d\n", 179297670Ssgalabov err); 180297670Ssgalabov sc->sc_intr_hdl = NULL; 181297670Ssgalabov } 182297670Ssgalabov if (sc->sc_irq_res) { 183297670Ssgalabov bus_release_resource(self, SYS_RES_IRQ, 0, 184297670Ssgalabov sc->sc_irq_res); 185297670Ssgalabov sc->sc_irq_res = NULL; 186297670Ssgalabov } 187297670Ssgalabov if (sc->sc_io_res) { 188297670Ssgalabov bus_release_resource(self, SYS_RES_MEMORY, 0, 189297670Ssgalabov sc->sc_io_res); 190297670Ssgalabov sc->sc_io_res = NULL; 191297670Ssgalabov } 192297670Ssgalabov usb_bus_mem_free_all(&sc->sc_bus, &ohci_iterate_hw_softc); 193297670Ssgalabov 194297670Ssgalabov return (0); 195297670Ssgalabov} 196297670Ssgalabov 197297670Ssgalabovstatic device_method_t ohci_fdt_methods[] = { 198297670Ssgalabov /* Device interface */ 199297670Ssgalabov DEVMETHOD(device_probe, ohci_fdt_probe), 200297670Ssgalabov DEVMETHOD(device_attach, ohci_fdt_attach), 201297670Ssgalabov DEVMETHOD(device_detach, ohci_fdt_detach), 202297670Ssgalabov DEVMETHOD(device_suspend, bus_generic_suspend), 203297670Ssgalabov DEVMETHOD(device_resume, bus_generic_resume), 204297670Ssgalabov DEVMETHOD(device_shutdown, bus_generic_shutdown), 205297670Ssgalabov 206297670Ssgalabov DEVMETHOD_END 207297670Ssgalabov}; 208297670Ssgalabov 209297670Ssgalabovstatic driver_t ohci_fdt_driver = { 210297670Ssgalabov .name = "ohci", 211297670Ssgalabov .methods = ohci_fdt_methods, 212297670Ssgalabov .size = sizeof(ohci_softc_t), 213297670Ssgalabov}; 214297670Ssgalabov 215297670Ssgalabovstatic devclass_t ohci_fdt_devclass; 216297670Ssgalabov 217297670SsgalabovDRIVER_MODULE(ohci, simplebus, ohci_fdt_driver, ohci_fdt_devclass, 0, 0); 218