1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2012 Hans Petter Selasky. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include "opt_acpi.h" 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/condvar.h> 34 #include <sys/kernel.h> 35 #include <sys/lock.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/mutex.h> 39 #include <sys/rman.h> 40 41 #include <contrib/dev/acpica/include/acpi.h> 42 #include <contrib/dev/acpica/include/accommon.h> 43 44 #include <dev/acpica/acpivar.h> 45 46 #include <dev/usb/usb.h> 47 #include <dev/usb/usbdi.h> 48 49 #include <dev/usb/usb_core.h> 50 #include <dev/usb/usb_busdma.h> 51 #include <dev/usb/usb_process.h> 52 #include <dev/usb/usb_util.h> 53 54 #include <dev/usb/usb_controller.h> 55 #include <dev/usb/usb_bus.h> 56 57 #include <dev/usb/controller/dwc_otg.h> 58 59 static device_probe_t dwc_otg_probe; 60 static device_attach_t dwc_otg_attach; 61 static device_attach_t dwc_otg_detach; 62 63 static char *dwc_otg_ids[] = { 64 "BCM2848", 65 NULL 66 }; 67 68 static int 69 dwc_otg_probe(device_t dev) 70 { 71 int rv; 72 73 if (acpi_disabled("dwc_otg")) 74 return (ENXIO); 75 76 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, dwc_otg_ids, NULL); 77 if (rv > 0) 78 return (rv); 79 80 device_set_desc(dev, "DWC OTG 2.0 integrated USB controller"); 81 82 return (BUS_PROBE_DEFAULT); 83 } 84 85 static int 86 dwc_otg_attach(device_t dev) 87 { 88 struct dwc_otg_softc *sc = device_get_softc(dev); 89 int err; 90 int rid; 91 92 sc->sc_bus.parent = dev; 93 94 /* assume device mode (this is only used for the Raspberry Pi 4's 95 * USB-C port, which only works in device mode) */ 96 sc->sc_mode = DWC_MODE_DEVICE; 97 98 rid = 0; 99 sc->sc_io_res = 100 bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 101 102 if (sc->sc_io_res == NULL) 103 goto error; 104 105 rid = 0; 106 sc->sc_irq_res = 107 bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 108 if (sc->sc_irq_res == NULL) 109 goto error; 110 111 err = dwc_otg_init(sc); 112 if (err == 0) { 113 err = device_probe_and_attach(sc->sc_bus.bdev); 114 } 115 if (err) 116 goto error; 117 118 return (0); 119 120 error: 121 dwc_otg_detach(dev); 122 return (ENXIO); 123 } 124 125 static int 126 dwc_otg_detach(device_t dev) 127 { 128 struct dwc_otg_softc *sc = device_get_softc(dev); 129 int error; 130 131 /* during module unload there are lots of children leftover */ 132 error = bus_generic_detach(dev); 133 if (error != 0) 134 return (error); 135 136 if (sc->sc_irq_res && sc->sc_intr_hdl) { 137 /* 138 * only call dwc_otg_uninit() after dwc_otg_init() 139 */ 140 dwc_otg_uninit(sc); 141 142 bus_teardown_intr(dev, sc->sc_irq_res, 143 sc->sc_intr_hdl); 144 sc->sc_intr_hdl = NULL; 145 } 146 /* free IRQ channel, if any */ 147 if (sc->sc_irq_res) { 148 bus_release_resource(dev, SYS_RES_IRQ, 0, 149 sc->sc_irq_res); 150 sc->sc_irq_res = NULL; 151 } 152 /* free memory resource, if any */ 153 if (sc->sc_io_res) { 154 bus_release_resource(dev, SYS_RES_MEMORY, 0, 155 sc->sc_io_res); 156 sc->sc_io_res = NULL; 157 } 158 usb_bus_mem_free_all(&sc->sc_bus, NULL); 159 160 return (0); 161 } 162 163 static device_method_t dwc_otg_methods[] = { 164 /* Device interface */ 165 DEVMETHOD(device_probe, dwc_otg_probe), 166 DEVMETHOD(device_attach, dwc_otg_attach), 167 DEVMETHOD(device_detach, dwc_otg_detach), 168 DEVMETHOD(device_suspend, bus_generic_suspend), 169 DEVMETHOD(device_resume, bus_generic_resume), 170 DEVMETHOD(device_shutdown, bus_generic_shutdown), 171 172 DEVMETHOD_END 173 }; 174 175 static driver_t dwc_otg_driver = { 176 .name = "dwcotg", 177 .methods = dwc_otg_methods, 178 .size = sizeof(struct dwc_otg_softc), 179 }; 180 181 DRIVER_MODULE(dwcotg, acpi, dwc_otg_driver, 0, 0); 182 MODULE_DEPEND(dwcotg, usb, 1, 1, 1); 183