1/*- 2 * Copyright (C) 2009 Yohanes Nugroho <yohanes@gmail.com> 3 * based on ehci_mbus.c 4 * Copyright (C) 2008 MARVELL INTERNATIONAL LTD. 5 * All rights reserved. 6 * 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of MARVELL nor the names of contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 34#include <sys/cdefs.h> 35__FBSDID("$FreeBSD: stable/11/sys/arm/cavium/cns11xx/ehci_ebus.c 346524 2019-04-22 04:56:41Z ian $"); 36 37#include "opt_bus.h" 38 39#include <machine/resource.h> 40 41#include <sys/stdint.h> 42#include <sys/stddef.h> 43#include <sys/param.h> 44#include <sys/queue.h> 45#include <sys/types.h> 46#include <sys/systm.h> 47#include <sys/kernel.h> 48#include <sys/bus.h> 49#include <sys/module.h> 50#include <sys/lock.h> 51#include <sys/mutex.h> 52#include <sys/condvar.h> 53#include <sys/sysctl.h> 54#include <sys/sx.h> 55#include <sys/unistd.h> 56#include <sys/callout.h> 57#include <sys/malloc.h> 58#include <sys/priv.h> 59 60#include <sys/rman.h> 61 62#include <dev/usb/usb.h> 63#include <dev/usb/usbdi.h> 64 65#include <dev/usb/usb_core.h> 66#include <dev/usb/usb_busdma.h> 67#include <dev/usb/usb_process.h> 68#include <dev/usb/usb_util.h> 69 70#include <dev/usb/usb_controller.h> 71#include <dev/usb/usb_bus.h> 72#include <dev/usb/controller/ehci.h> 73#include <dev/usb/controller/ehcireg.h> 74 75 76static device_attach_t ehci_ebus_attach; 77static device_detach_t ehci_ebus_detach; 78 79static void *ih_err; 80 81#define EHCI_HC_DEVSTR "CNS11XX USB EHCI" 82#define USB_BRIDGE_INTR_MASK 0x214 83 84static int 85ehci_ebus_probe(device_t self) 86{ 87 88 device_set_desc(self, EHCI_HC_DEVSTR); 89 90 return (BUS_PROBE_DEFAULT); 91} 92 93static int 94ehci_ebus_attach(device_t self) 95{ 96 ehci_softc_t *sc = device_get_softc(self); 97 bus_space_handle_t bsh; 98 int err; 99 int rid; 100 101 /* initialise some bus fields */ 102 sc->sc_bus.parent = self; 103 sc->sc_bus.devices = sc->sc_devices; 104 sc->sc_bus.devices_max = EHCI_MAX_DEVICES; 105 sc->sc_bus.dma_bits = 32; 106 107 /* get all DMA memory */ 108 if (usb_bus_mem_alloc_all(&sc->sc_bus, 109 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) { 110 return (ENOMEM); 111 } 112 113 sc->sc_bus.usbrev = USB_REV_2_0; 114 115 rid = 0; 116 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, 117 &rid, RF_ACTIVE); 118 if (!sc->sc_io_res) { 119 device_printf(self, "Could not map memory\n"); 120 goto error; 121 } 122 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 123 bsh = rman_get_bushandle(sc->sc_io_res); 124 125 /*magic, undocumented initialization*/ 126 bus_space_write_4((sc)->sc_io_tag, bsh, 0x04, 0x106); 127 128 bus_space_write_4((sc)->sc_io_tag, bsh, 0x40, (3 << 5)|0x2000); 129 130 DELAY(1000); 131 132 sc->sc_io_size = 4096; 133 134 if (bus_space_subregion(sc->sc_io_tag, bsh, 0x4000000, 135 sc->sc_io_size, &sc->sc_io_hdl) != 0) 136 panic("%s: unable to subregion USB host registers", 137 device_get_name(self)); 138 139 rid = 0; 140 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 141 RF_SHAREABLE | RF_ACTIVE); 142 if (sc->sc_irq_res == NULL) { 143 device_printf(self, "Could not allocate irq\n"); 144 ehci_ebus_detach(self); 145 return (ENXIO); 146 } 147 148 sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 149 if (!sc->sc_bus.bdev) { 150 device_printf(self, "Could not add USB device\n"); 151 goto error; 152 } 153 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 154 device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR); 155 156 sprintf(sc->sc_vendor, "Cavium"); 157 158 err = bus_setup_intr(self,sc->sc_irq_res, 159 INTR_TYPE_BIO | INTR_MPSAFE, NULL, 160 (driver_intr_t *)ehci_interrupt, sc, 161 &sc->sc_intr_hdl); 162 if (err) { 163 device_printf(self, "Could not setup error irq, %d\n", err); 164 ih_err = NULL; 165 goto error; 166 } 167 168 err = ehci_init(sc); 169 if (!err) { 170 err = device_probe_and_attach(sc->sc_bus.bdev); 171 } 172 if (err) { 173 device_printf(self, "USB init failed err=%d\n", err); 174 goto error; 175 } 176 return (0); 177 178error: 179 ehci_ebus_detach(self); 180 return (ENXIO); 181} 182 183static int 184ehci_ebus_detach(device_t self) 185{ 186 ehci_softc_t *sc = device_get_softc(self); 187 int err; 188 189 /* during module unload there are lots of children leftover */ 190 device_delete_children(self); 191 192 /* 193 * disable interrupts that might have been switched on in 194 * ehci_ebus_attach() 195 */ 196 if (sc->sc_io_res) { 197 EWRITE4(sc, USB_BRIDGE_INTR_MASK, 0); 198 } 199 if (sc->sc_irq_res && sc->sc_intr_hdl) { 200 /* 201 * only call ehci_detach() after ehci_init() 202 */ 203 ehci_detach(sc); 204 205 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 206 207 if (err) 208 /* XXX or should we panic? */ 209 device_printf(self, "Could not tear down irq, %d\n", 210 err); 211 sc->sc_intr_hdl = NULL; 212 } 213 if (sc->sc_irq_res) { 214 bus_release_resource(self, SYS_RES_IRQ, 1, sc->sc_irq_res); 215 sc->sc_irq_res = NULL; 216 } 217 if (sc->sc_io_res) { 218 bus_release_resource(self, SYS_RES_MEMORY, 0, 219 sc->sc_io_res); 220 sc->sc_io_res = NULL; 221 } 222 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc); 223 224 return (0); 225} 226 227static device_method_t ehci_methods[] = { 228 /* Device interface */ 229 DEVMETHOD(device_probe, ehci_ebus_probe), 230 DEVMETHOD(device_attach, ehci_ebus_attach), 231 DEVMETHOD(device_detach, ehci_ebus_detach), 232 DEVMETHOD(device_suspend, bus_generic_suspend), 233 DEVMETHOD(device_resume, bus_generic_resume), 234 DEVMETHOD(device_shutdown, bus_generic_shutdown), 235 236 DEVMETHOD_END 237}; 238 239static driver_t ehci_driver = { 240 .name = "ehci", 241 .methods = ehci_methods, 242 .size = sizeof(ehci_softc_t), 243}; 244 245static devclass_t ehci_devclass; 246 247DRIVER_MODULE(ebus_ehci, econaarm, ehci_driver, ehci_devclass, 0, 0); 248MODULE_DEPEND(ebus_ehci, usb, 1, 1, 1); 249