1 /*- 2 * Copyright (C) 2008 MARVELL INTERNATIONAL LTD. 3 * All rights reserved. 4 * 5 * Developed by Semihalf. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of MARVELL nor the names of contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * FDT attachment driver for the USB Enhanced Host Controller. 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include "opt_bus.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 <dev/ofw/ofw_bus.h> 61 #include <dev/ofw/ofw_bus_subr.h> 62 63 #include <dev/usb/usb.h> 64 #include <dev/usb/usbdi.h> 65 66 #include <dev/usb/usb_core.h> 67 #include <dev/usb/usb_busdma.h> 68 #include <dev/usb/usb_process.h> 69 #include <dev/usb/usb_util.h> 70 71 #include <dev/usb/usb_controller.h> 72 #include <dev/usb/usb_bus.h> 73 #include <dev/usb/controller/ehci.h> 74 #include <dev/usb/controller/ehcireg.h> 75 76 #include <arm/mv/mvreg.h> 77 #include <arm/mv/mvvar.h> 78 79 #define EHCI_VENDORID_MRVL 0x1286 80 #define EHCI_HC_DEVSTR "Marvell Integrated USB 2.0 controller" 81 82 static device_attach_t mv_ehci_attach; 83 static device_detach_t mv_ehci_detach; 84 static device_shutdown_t mv_ehci_shutdown; 85 static device_suspend_t mv_ehci_suspend; 86 static device_resume_t mv_ehci_resume; 87 88 static int err_intr(void *arg); 89 90 static struct resource *irq_err; 91 static void *ih_err; 92 93 /* EHCI HC regs start at this offset within USB range */ 94 #define MV_USB_HOST_OFST 0x0100 95 96 #define USB_BRIDGE_INTR_CAUSE 0x210 97 #define USB_BRIDGE_INTR_MASK 0x214 98 99 #define MV_USB_ADDR_DECODE_ERR (1 << 0) 100 #define MV_USB_HOST_UNDERFLOW (1 << 1) 101 #define MV_USB_HOST_OVERFLOW (1 << 2) 102 #define MV_USB_DEVICE_UNDERFLOW (1 << 3) 103 104 static int 105 mv_ehci_suspend(device_t self) 106 { 107 ehci_softc_t *sc = device_get_softc(self); 108 int err; 109 110 err = bus_generic_suspend(self); 111 if (err) 112 return (err); 113 ehci_suspend(sc); 114 return (0); 115 } 116 117 static int 118 mv_ehci_resume(device_t self) 119 { 120 ehci_softc_t *sc = device_get_softc(self); 121 122 ehci_resume(sc); 123 124 bus_generic_resume(self); 125 126 return (0); 127 } 128 129 static int 130 mv_ehci_shutdown(device_t self) 131 { 132 ehci_softc_t *sc = device_get_softc(self); 133 int err; 134 135 err = bus_generic_shutdown(self); 136 if (err) 137 return (err); 138 ehci_shutdown(sc); 139 140 return (0); 141 } 142 143 static int 144 mv_ehci_probe(device_t self) 145 { 146 147 if (!ofw_bus_is_compatible(self, "mrvl,usb-ehci")) 148 return (ENXIO); 149 150 device_set_desc(self, EHCI_HC_DEVSTR); 151 152 return (BUS_PROBE_DEFAULT); 153 } 154 155 static int 156 mv_ehci_attach(device_t self) 157 { 158 ehci_softc_t *sc = device_get_softc(self); 159 bus_space_handle_t bsh; 160 int err; 161 int rid; 162 163 /* initialise some bus fields */ 164 sc->sc_bus.parent = self; 165 sc->sc_bus.devices = sc->sc_devices; 166 sc->sc_bus.devices_max = EHCI_MAX_DEVICES; 167 168 /* get all DMA memory */ 169 if (usb_bus_mem_alloc_all(&sc->sc_bus, 170 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) { 171 return (ENOMEM); 172 } 173 174 rid = 0; 175 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE); 176 if (!sc->sc_io_res) { 177 device_printf(self, "Could not map memory\n"); 178 goto error; 179 } 180 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 181 bsh = rman_get_bushandle(sc->sc_io_res); 182 sc->sc_io_size = rman_get_size(sc->sc_io_res) - MV_USB_HOST_OFST; 183 184 /* 185 * Marvell EHCI host controller registers start at certain offset 186 * within the whole USB registers range, so create a subregion for the 187 * host mode configuration purposes. 188 */ 189 190 if (bus_space_subregion(sc->sc_io_tag, bsh, MV_USB_HOST_OFST, 191 sc->sc_io_size, &sc->sc_io_hdl) != 0) 192 panic("%s: unable to subregion USB host registers", 193 device_get_name(self)); 194 195 rid = 0; 196 irq_err = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 197 RF_SHAREABLE | RF_ACTIVE); 198 if (irq_err == NULL) { 199 device_printf(self, "Could not allocate error irq\n"); 200 mv_ehci_detach(self); 201 return (ENXIO); 202 } 203 204 /* 205 * Notice: Marvell EHCI controller has TWO interrupt lines, so make 206 * sure to use the correct rid for the main one (controller interrupt) 207 * -- refer to DTS for the right resource number to use here. 208 */ 209 rid = 1; 210 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 211 RF_SHAREABLE | RF_ACTIVE); 212 if (sc->sc_irq_res == NULL) { 213 device_printf(self, "Could not allocate irq\n"); 214 goto error; 215 } 216 217 sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 218 if (!sc->sc_bus.bdev) { 219 device_printf(self, "Could not add USB device\n"); 220 goto error; 221 } 222 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 223 device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR); 224 225 sprintf(sc->sc_vendor, "Marvell"); 226 227 err = bus_setup_intr(self, irq_err, INTR_TYPE_BIO, 228 err_intr, NULL, sc, &ih_err); 229 if (err) { 230 device_printf(self, "Could not setup error irq, %d\n", err); 231 ih_err = NULL; 232 goto error; 233 } 234 235 EWRITE4(sc, USB_BRIDGE_INTR_MASK, MV_USB_ADDR_DECODE_ERR | 236 MV_USB_HOST_UNDERFLOW | MV_USB_HOST_OVERFLOW | 237 MV_USB_DEVICE_UNDERFLOW); 238 239 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 240 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl); 241 if (err) { 242 device_printf(self, "Could not setup irq, %d\n", err); 243 sc->sc_intr_hdl = NULL; 244 goto error; 245 } 246 247 /* 248 * Workaround for Marvell integrated EHCI controller: reset of 249 * the EHCI core clears the USBMODE register, which sets the core in 250 * an undefined state (neither host nor agent), so it needs to be set 251 * again for proper operation. 252 * 253 * Refer to errata document MV-S500832-00D.pdf (p. 5.24 GL USB-2) for 254 * details. 255 */ 256 sc->sc_flags |= EHCI_SCFLG_SETMODE; 257 if (bootverbose) 258 device_printf(self, "5.24 GL USB-2 workaround enabled\n"); 259 260 /* XXX all MV chips need it? */ 261 sc->sc_flags |= EHCI_SCFLG_FORCESPEED | EHCI_SCFLG_NORESTERM; 262 263 err = ehci_init(sc); 264 if (!err) { 265 err = device_probe_and_attach(sc->sc_bus.bdev); 266 } 267 if (err) { 268 device_printf(self, "USB init failed err=%d\n", err); 269 goto error; 270 } 271 return (0); 272 273 error: 274 mv_ehci_detach(self); 275 return (ENXIO); 276 } 277 278 static int 279 mv_ehci_detach(device_t self) 280 { 281 ehci_softc_t *sc = device_get_softc(self); 282 device_t bdev; 283 int err; 284 285 if (sc->sc_bus.bdev) { 286 bdev = sc->sc_bus.bdev; 287 device_detach(bdev); 288 device_delete_child(self, bdev); 289 } 290 /* during module unload there are lots of children leftover */ 291 device_delete_all_children(self); 292 293 /* 294 * disable interrupts that might have been switched on in ehci_init 295 */ 296 if (sc->sc_io_res) { 297 EWRITE4(sc, EHCI_USBINTR, 0); 298 EWRITE4(sc, USB_BRIDGE_INTR_MASK, 0); 299 } 300 if (sc->sc_irq_res && sc->sc_intr_hdl) { 301 /* 302 * only call ehci_detach() after ehci_init() 303 */ 304 ehci_detach(sc); 305 306 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 307 308 if (err) 309 /* XXX or should we panic? */ 310 device_printf(self, "Could not tear down irq, %d\n", 311 err); 312 sc->sc_intr_hdl = NULL; 313 } 314 if (irq_err && ih_err) { 315 err = bus_teardown_intr(self, irq_err, ih_err); 316 317 if (err) 318 device_printf(self, "Could not tear down irq, %d\n", 319 err); 320 ih_err = NULL; 321 } 322 if (irq_err) { 323 bus_release_resource(self, SYS_RES_IRQ, 0, irq_err); 324 irq_err = NULL; 325 } 326 if (sc->sc_irq_res) { 327 bus_release_resource(self, SYS_RES_IRQ, 1, sc->sc_irq_res); 328 sc->sc_irq_res = NULL; 329 } 330 if (sc->sc_io_res) { 331 bus_release_resource(self, SYS_RES_MEMORY, 0, 332 sc->sc_io_res); 333 sc->sc_io_res = NULL; 334 } 335 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc); 336 337 return (0); 338 } 339 340 static int 341 err_intr(void *arg) 342 { 343 ehci_softc_t *sc = arg; 344 unsigned int cause; 345 346 cause = EREAD4(sc, USB_BRIDGE_INTR_CAUSE); 347 if (cause) { 348 printf("IRQ ERR: cause: 0x%08x\n", cause); 349 if (cause & MV_USB_ADDR_DECODE_ERR) 350 printf("IRQ ERR: Address decoding error\n"); 351 if (cause & MV_USB_HOST_UNDERFLOW) 352 printf("IRQ ERR: USB Host Underflow\n"); 353 if (cause & MV_USB_HOST_OVERFLOW) 354 printf("IRQ ERR: USB Host Overflow\n"); 355 if (cause & MV_USB_DEVICE_UNDERFLOW) 356 printf("IRQ ERR: USB Device Underflow\n"); 357 if (cause & ~(MV_USB_ADDR_DECODE_ERR | MV_USB_HOST_UNDERFLOW | 358 MV_USB_HOST_OVERFLOW | MV_USB_DEVICE_UNDERFLOW)) 359 printf("IRQ ERR: Unknown error\n"); 360 361 EWRITE4(sc, USB_BRIDGE_INTR_CAUSE, 0); 362 } 363 return (FILTER_HANDLED); 364 } 365 366 static device_method_t ehci_methods[] = { 367 /* Device interface */ 368 DEVMETHOD(device_probe, mv_ehci_probe), 369 DEVMETHOD(device_attach, mv_ehci_attach), 370 DEVMETHOD(device_detach, mv_ehci_detach), 371 DEVMETHOD(device_suspend, mv_ehci_suspend), 372 DEVMETHOD(device_resume, mv_ehci_resume), 373 DEVMETHOD(device_shutdown, mv_ehci_shutdown), 374 375 /* Bus interface */ 376 DEVMETHOD(bus_print_child, bus_generic_print_child), 377 378 {0, 0} 379 }; 380 381 static driver_t ehci_driver = { 382 "ehci", 383 ehci_methods, 384 sizeof(ehci_softc_t), 385 }; 386 387 static devclass_t ehci_devclass; 388 389 DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0); 390 MODULE_DEPEND(ehci, usb, 1, 1, 1); 391