1296827Swma/*- 2296827Swma * Copyright (c) 2015 Semihalf. 3296827Swma * Copyright (c) 2015 Stormshield. 4296827Swma * All rights reserved. 5296827Swma * 6296827Swma * Redistribution and use in source and binary forms, with or without 7296827Swma * modification, are permitted provided that the following conditions 8296827Swma * are met: 9296827Swma * 1. Redistributions of source code must retain the above copyright 10296827Swma * notice, this list of conditions and the following disclaimer. 11296827Swma * 2. Redistributions in binary form must reproduce the above copyright 12296827Swma * notice, this list of conditions and the following disclaimer in the 13296827Swma * documentation and/or other materials provided with the distribution. 14296827Swma * 15296827Swma * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16296827Swma * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17296827Swma * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18296827Swma * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19296827Swma * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20296827Swma * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21296827Swma * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22296827Swma * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23296827Swma * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24296827Swma * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25296827Swma * SUCH DAMAGE. 26296827Swma */ 27296827Swma 28296827Swma#include <sys/cdefs.h> 29296827Swma__FBSDID("$FreeBSD: stable/11/sys/dev/usb/controller/xhci_mv.c 308401 2016-11-07 08:36:06Z hselasky $"); 30296827Swma 31296827Swma#include "opt_bus.h" 32296827Swma 33296827Swma#include <sys/stdint.h> 34296827Swma#include <sys/stddef.h> 35296827Swma#include <sys/param.h> 36296827Swma#include <sys/queue.h> 37296827Swma#include <sys/types.h> 38296827Swma#include <sys/systm.h> 39296827Swma#include <sys/kernel.h> 40296827Swma#include <sys/bus.h> 41296827Swma#include <sys/module.h> 42296827Swma#include <sys/lock.h> 43296827Swma#include <sys/mutex.h> 44296827Swma#include <sys/condvar.h> 45296827Swma#include <sys/sysctl.h> 46296827Swma#include <sys/sx.h> 47296827Swma#include <sys/unistd.h> 48296827Swma#include <sys/priv.h> 49296827Swma#include <sys/rman.h> 50296827Swma 51296827Swma#include <dev/ofw/ofw_bus.h> 52296827Swma#include <dev/ofw/ofw_bus_subr.h> 53296827Swma 54296827Swma#include <dev/usb/usb.h> 55296827Swma#include <dev/usb/usbdi.h> 56296827Swma 57296827Swma#include <dev/usb/usb_core.h> 58296827Swma#include <dev/usb/usb_busdma.h> 59296827Swma#include <dev/usb/usb_process.h> 60296827Swma#include <dev/usb/usb_util.h> 61296827Swma 62296827Swma#include <dev/usb/usb_controller.h> 63296827Swma#include <dev/usb/usb_bus.h> 64296827Swma#include <dev/usb/controller/xhci.h> 65296827Swma#include <dev/usb/controller/xhcireg.h> 66296827Swma 67296827Swma#define XHCI_HC_DEVSTR "Marvell Integrated USB 3.0 controller" 68296827Swma#define XHCI_HC_VENDOR "Marvell" 69296827Swma 70296827Swma#define IS_DMA_32B 1 71296827Swma 72296827Swmastatic device_attach_t xhci_attach; 73296827Swmastatic device_detach_t xhci_detach; 74296827Swma 75296827Swmastatic struct ofw_compat_data compat_data[] = { 76296827Swma {"marvell,armada-380-xhci", true}, 77296827Swma {NULL, false} 78296827Swma}; 79296827Swma 80296827Swmastatic int 81296827Swmaxhci_probe(device_t dev) 82296827Swma{ 83296827Swma 84296827Swma if (!ofw_bus_status_okay(dev)) 85296827Swma return (ENXIO); 86296827Swma 87296827Swma if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data) 88296827Swma return (ENXIO); 89296827Swma 90296827Swma device_set_desc(dev, XHCI_HC_DEVSTR); 91296827Swma 92296827Swma return (BUS_PROBE_DEFAULT); 93296827Swma} 94296827Swma 95296827Swmastatic int 96296827Swmaxhci_attach(device_t dev) 97296827Swma{ 98296827Swma struct xhci_softc *sc = device_get_softc(dev); 99296827Swma int err = 0, rid = 0; 100296827Swma 101296827Swma sc->sc_bus.parent = dev; 102296827Swma sc->sc_bus.devices = sc->sc_devices; 103296827Swma sc->sc_bus.devices_max = XHCI_MAX_DEVICES; 104296827Swma 105296827Swma sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 106296827Swma RF_ACTIVE); 107296827Swma if (sc->sc_io_res == NULL) { 108296827Swma device_printf(dev, "Failed to map memory\n"); 109296827Swma xhci_detach(dev); 110296827Swma return (ENXIO); 111296827Swma } 112296827Swma 113296827Swma sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 114296827Swma sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 115296827Swma sc->sc_io_size = rman_get_size(sc->sc_io_res); 116296827Swma 117296827Swma sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 118296827Swma RF_SHAREABLE | RF_ACTIVE); 119296827Swma if (sc->sc_irq_res == NULL) { 120296827Swma device_printf(dev, "Failed to allocate IRQ\n"); 121296827Swma xhci_detach(dev); 122296827Swma return (ENXIO); 123296827Swma } 124296827Swma 125296827Swma sc->sc_bus.bdev = device_add_child(dev, "usbus", -1); 126296827Swma if (sc->sc_bus.bdev == NULL) { 127296827Swma device_printf(dev, "Failed to add USB device\n"); 128296827Swma xhci_detach(dev); 129296827Swma return (ENXIO); 130296827Swma } 131296827Swma 132296827Swma device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 133296827Swma 134296827Swma sprintf(sc->sc_vendor, XHCI_HC_VENDOR); 135296827Swma device_set_desc(sc->sc_bus.bdev, XHCI_HC_DEVSTR); 136296827Swma 137296827Swma err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 138296827Swma NULL, (driver_intr_t *)xhci_interrupt, sc, &sc->sc_intr_hdl); 139296827Swma if (err != 0) { 140296827Swma device_printf(dev, "Failed to setup error IRQ, %d\n", err); 141296827Swma sc->sc_intr_hdl = NULL; 142296827Swma xhci_detach(dev); 143296827Swma return (err); 144296827Swma } 145296827Swma 146296827Swma err = xhci_init(sc, dev, IS_DMA_32B); 147296827Swma if (err != 0) { 148296827Swma device_printf(dev, "Failed to init XHCI, with error %d\n", err); 149296827Swma xhci_detach(dev); 150296827Swma return (ENXIO); 151296827Swma } 152296827Swma 153296827Swma err = xhci_start_controller(sc); 154296827Swma if (err != 0) { 155296827Swma device_printf(dev, "Failed to start XHCI controller, with error %d\n", err); 156296827Swma xhci_detach(dev); 157296827Swma return (ENXIO); 158296827Swma } 159296827Swma 160296827Swma err = device_probe_and_attach(sc->sc_bus.bdev); 161296827Swma if (err != 0) { 162296827Swma device_printf(dev, "Failed to initialize USB, with error %d\n", err); 163296827Swma xhci_detach(dev); 164296827Swma return (ENXIO); 165296827Swma } 166296827Swma 167296827Swma return (0); 168296827Swma} 169296827Swma 170296827Swmastatic int 171296827Swmaxhci_detach(device_t dev) 172296827Swma{ 173296827Swma struct xhci_softc *sc = device_get_softc(dev); 174296827Swma int err; 175296827Swma 176296827Swma /* during module unload there are lots of children leftover */ 177296827Swma device_delete_children(dev); 178296827Swma 179296827Swma if (sc->sc_irq_res != NULL && sc->sc_intr_hdl != NULL) { 180296827Swma err = bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hdl); 181296827Swma if (err != 0) 182296827Swma device_printf(dev, "Could not tear down irq, %d\n", 183296827Swma err); 184296827Swma sc->sc_intr_hdl = NULL; 185296827Swma } 186296827Swma 187296827Swma if (sc->sc_irq_res != NULL) { 188296827Swma bus_release_resource(dev, SYS_RES_IRQ, 189296827Swma rman_get_rid(sc->sc_irq_res), sc->sc_irq_res); 190296827Swma sc->sc_irq_res = NULL; 191296827Swma } 192296827Swma 193296827Swma if (sc->sc_io_res != NULL) { 194296827Swma bus_release_resource(dev, SYS_RES_MEMORY, 195296827Swma rman_get_rid(sc->sc_io_res), sc->sc_io_res); 196296827Swma sc->sc_io_res = NULL; 197296827Swma } 198296827Swma 199296827Swma xhci_uninit(sc); 200296827Swma 201296827Swma return (0); 202296827Swma} 203296827Swma 204296827Swmastatic device_method_t xhci_methods[] = { 205296827Swma /* Device interface */ 206296827Swma DEVMETHOD(device_probe, xhci_probe), 207296827Swma DEVMETHOD(device_attach, xhci_attach), 208296827Swma DEVMETHOD(device_detach, xhci_detach), 209296827Swma DEVMETHOD(device_suspend, bus_generic_suspend), 210296827Swma DEVMETHOD(device_resume, bus_generic_resume), 211296827Swma DEVMETHOD(device_shutdown, bus_generic_shutdown), 212296827Swma 213296827Swma DEVMETHOD_END 214296827Swma}; 215296827Swma 216296827Swmastatic driver_t xhci_driver = { 217296827Swma "xhci", 218296827Swma xhci_methods, 219296827Swma sizeof(struct xhci_softc), 220296827Swma}; 221296827Swma 222296827Swmastatic devclass_t xhci_devclass; 223296827Swma 224296827SwmaDRIVER_MODULE(xhci, simplebus, xhci_driver, xhci_devclass, 0, 0); 225296827SwmaMODULE_DEPEND(xhci, usb, 1, 1, 1); 226