1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2011 5 * Ben Gray <ben.r.gray@gmail.com>. 6 * All rights reserved. 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 * 17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/conf.h> 35 #include <sys/kernel.h> 36 #include <sys/rman.h> 37 #include <sys/module.h> 38 #include <sys/proc.h> 39 #include <sys/condvar.h> 40 41 #include <dev/fdt/simplebus.h> 42 #include <dev/fdt/fdt_common.h> 43 #include <dev/ofw/ofw_bus_subr.h> 44 45 #include <dev/usb/usb.h> 46 #include <dev/usb/usbdi.h> 47 48 #include <dev/usb/usb_core.h> 49 #include <dev/usb/usb_busdma.h> 50 #include <dev/usb/usb_process.h> 51 #include <dev/usb/usb_util.h> 52 53 #include <dev/usb/usb_controller.h> 54 #include <dev/usb/usb_bus.h> 55 #include <dev/usb/controller/ehci.h> 56 #include <dev/usb/controller/ehcireg.h> 57 58 #include <machine/bus.h> 59 60 #include <arm/ti/usb/omap_usb.h> 61 62 #include <arm/ti/omap4/pandaboard/pandaboard.h> 63 64 /* EHCI */ 65 #define OMAP_USBHOST_HCCAPBASE 0x0000 66 #define OMAP_USBHOST_HCSPARAMS 0x0004 67 #define OMAP_USBHOST_HCCPARAMS 0x0008 68 #define OMAP_USBHOST_USBCMD 0x0010 69 #define OMAP_USBHOST_USBSTS 0x0014 70 #define OMAP_USBHOST_USBINTR 0x0018 71 #define OMAP_USBHOST_FRINDEX 0x001C 72 #define OMAP_USBHOST_CTRLDSSEGMENT 0x0020 73 #define OMAP_USBHOST_PERIODICLISTBASE 0x0024 74 #define OMAP_USBHOST_ASYNCLISTADDR 0x0028 75 #define OMAP_USBHOST_CONFIGFLAG 0x0050 76 #define OMAP_USBHOST_PORTSC(i) (0x0054 + (0x04 * (i))) 77 #define OMAP_USBHOST_INSNREG00 0x0090 78 #define OMAP_USBHOST_INSNREG01 0x0094 79 #define OMAP_USBHOST_INSNREG02 0x0098 80 #define OMAP_USBHOST_INSNREG03 0x009C 81 #define OMAP_USBHOST_INSNREG04 0x00A0 82 #define OMAP_USBHOST_INSNREG05_UTMI 0x00A4 83 #define OMAP_USBHOST_INSNREG05_ULPI 0x00A4 84 #define OMAP_USBHOST_INSNREG06 0x00A8 85 #define OMAP_USBHOST_INSNREG07 0x00AC 86 #define OMAP_USBHOST_INSNREG08 0x00B0 87 88 #define OMAP_USBHOST_INSNREG04_DISABLE_UNSUSPEND (1 << 5) 89 90 #define OMAP_USBHOST_INSNREG05_ULPI_CONTROL_SHIFT 31 91 #define OMAP_USBHOST_INSNREG05_ULPI_PORTSEL_SHIFT 24 92 #define OMAP_USBHOST_INSNREG05_ULPI_OPSEL_SHIFT 22 93 #define OMAP_USBHOST_INSNREG05_ULPI_REGADD_SHIFT 16 94 #define OMAP_USBHOST_INSNREG05_ULPI_EXTREGADD_SHIFT 8 95 #define OMAP_USBHOST_INSNREG05_ULPI_WRDATA_SHIFT 0 96 97 #define ULPI_FUNC_CTRL_RESET (1 << 5) 98 99 /*-------------------------------------------------------------------------*/ 100 101 /* 102 * Macros for Set and Clear 103 * See ULPI 1.1 specification to find the registers with Set and Clear offsets 104 */ 105 #define ULPI_SET(a) (a + 1) 106 #define ULPI_CLR(a) (a + 2) 107 108 /*-------------------------------------------------------------------------*/ 109 110 /* 111 * Register Map 112 */ 113 #define ULPI_VENDOR_ID_LOW 0x00 114 #define ULPI_VENDOR_ID_HIGH 0x01 115 #define ULPI_PRODUCT_ID_LOW 0x02 116 #define ULPI_PRODUCT_ID_HIGH 0x03 117 #define ULPI_FUNC_CTRL 0x04 118 #define ULPI_IFC_CTRL 0x07 119 #define ULPI_OTG_CTRL 0x0a 120 #define ULPI_USB_INT_EN_RISE 0x0d 121 #define ULPI_USB_INT_EN_FALL 0x10 122 #define ULPI_USB_INT_STS 0x13 123 #define ULPI_USB_INT_LATCH 0x14 124 #define ULPI_DEBUG 0x15 125 #define ULPI_SCRATCH 0x16 126 127 #define OMAP_EHCI_HC_DEVSTR "TI OMAP USB 2.0 controller" 128 129 struct omap_ehci_softc { 130 ehci_softc_t base; /* storage for EHCI code */ 131 device_t sc_dev; 132 }; 133 134 static device_attach_t omap_ehci_attach; 135 static device_detach_t omap_ehci_detach; 136 137 /** 138 * omap_ehci_read_4 - read a 32-bit value from the EHCI registers 139 * omap_ehci_write_4 - write a 32-bit value from the EHCI registers 140 * @sc: omap ehci device context 141 * @off: byte offset within the register set to read from 142 * @val: the value to write into the register 143 * 144 * 145 * LOCKING: 146 * None 147 * 148 * RETURNS: 149 * nothing in case of write function, if read function returns the value read. 150 */ 151 static inline uint32_t 152 omap_ehci_read_4(struct omap_ehci_softc *sc, bus_size_t off) 153 { 154 return (bus_read_4(sc->base.sc_io_res, off)); 155 } 156 157 static inline void 158 omap_ehci_write_4(struct omap_ehci_softc *sc, bus_size_t off, uint32_t val) 159 { 160 bus_write_4(sc->base.sc_io_res, off, val); 161 } 162 163 /** 164 * omap_ehci_soft_phy_reset - resets the phy using the reset command 165 * @isc: omap ehci device context 166 * @port: port to send the reset over 167 * 168 * 169 * LOCKING: 170 * none 171 * 172 * RETURNS: 173 * nothing 174 */ 175 static void 176 omap_ehci_soft_phy_reset(struct omap_ehci_softc *isc, unsigned int port) 177 { 178 unsigned long timeout = (hz < 10) ? 1 : ((100 * hz) / 1000); 179 uint32_t reg; 180 181 reg = ULPI_FUNC_CTRL_RESET 182 /* FUNCTION_CTRL_SET register */ 183 | (ULPI_SET(ULPI_FUNC_CTRL) << OMAP_USBHOST_INSNREG05_ULPI_REGADD_SHIFT) 184 /* Write */ 185 | (2 << OMAP_USBHOST_INSNREG05_ULPI_OPSEL_SHIFT) 186 /* PORTn */ 187 | ((port + 1) << OMAP_USBHOST_INSNREG05_ULPI_PORTSEL_SHIFT) 188 /* start ULPI access*/ 189 | (1 << OMAP_USBHOST_INSNREG05_ULPI_CONTROL_SHIFT); 190 191 omap_ehci_write_4(isc, OMAP_USBHOST_INSNREG05_ULPI, reg); 192 193 /* Wait for ULPI access completion */ 194 while ((omap_ehci_read_4(isc, OMAP_USBHOST_INSNREG05_ULPI) 195 & (1 << OMAP_USBHOST_INSNREG05_ULPI_CONTROL_SHIFT))) { 196 197 /* Sleep for a tick */ 198 pause("USBPHY_RESET", 1); 199 200 if (timeout-- == 0) { 201 device_printf(isc->sc_dev, "PHY reset operation timed out\n"); 202 break; 203 } 204 } 205 } 206 207 /** 208 * omap_ehci_init - initialises the USB host EHCI controller 209 * @isc: omap ehci device context 210 * 211 * This initialisation routine is quite heavily based on the work done by the 212 * OMAP Linux team (for which I thank them very much). The init sequence is 213 * almost identical, diverging only for the FreeBSD specifics. 214 * 215 * LOCKING: 216 * none 217 * 218 * RETURNS: 219 * 0 on success, a negative error code on failure. 220 */ 221 static int 222 omap_ehci_init(struct omap_ehci_softc *isc) 223 { 224 uint32_t reg = 0; 225 int i; 226 device_t uhh_dev; 227 228 uhh_dev = device_get_parent(isc->sc_dev); 229 device_printf(isc->sc_dev, "Starting TI EHCI USB Controller\n"); 230 231 /* Set the interrupt threshold control, it controls the maximum rate at 232 * which the host controller issues interrupts. We set it to 1 microframe 233 * at startup - the default is 8 mircoframes (equates to 1ms). 234 */ 235 reg = omap_ehci_read_4(isc, OMAP_USBHOST_USBCMD); 236 reg &= 0xff00ffff; 237 reg |= (1 << 16); 238 omap_ehci_write_4(isc, OMAP_USBHOST_USBCMD, reg); 239 240 /* Soft reset the PHY using PHY reset command over ULPI */ 241 for (i = 0; i < OMAP_HS_USB_PORTS; i++) { 242 if (omap_usb_port_mode(uhh_dev, i) == EHCI_HCD_OMAP_MODE_PHY) 243 omap_ehci_soft_phy_reset(isc, i); 244 245 } 246 247 return(0); 248 } 249 250 /** 251 * omap_ehci_probe - starts the given command 252 * @dev: 253 * 254 * Effectively boilerplate EHCI resume code. 255 * 256 * LOCKING: 257 * Caller should be holding the OMAP3_MMC lock. 258 * 259 * RETURNS: 260 * EH_HANDLED or EH_NOT_HANDLED 261 */ 262 static int 263 omap_ehci_probe(device_t dev) 264 { 265 if (!ofw_bus_status_okay(dev)) 266 return (ENXIO); 267 268 if (!ofw_bus_is_compatible(dev, "ti,ehci-omap")) 269 return (ENXIO); 270 271 device_set_desc(dev, OMAP_EHCI_HC_DEVSTR); 272 273 return (BUS_PROBE_DEFAULT); 274 } 275 276 /** 277 * omap_ehci_attach - driver entry point, sets up the ECHI controller/driver 278 * @dev: the new device handle 279 * 280 * Sets up bus spaces, interrupt handles, etc for the EHCI controller. It also 281 * parses the resource hints and calls omap_ehci_init() to initialise the 282 * H/W. 283 * 284 * LOCKING: 285 * none 286 * 287 * RETURNS: 288 * 0 on success or a positive error code on failure. 289 */ 290 static int 291 omap_ehci_attach(device_t dev) 292 { 293 struct omap_ehci_softc *isc = device_get_softc(dev); 294 ehci_softc_t *sc = &isc->base; 295 #ifdef SOC_OMAP4 296 phandle_t root; 297 #endif 298 int err; 299 int rid; 300 301 #ifdef SOC_OMAP4 302 /* 303 * If we're running a Pandaboard, run Pandaboard-specific 304 * init code. 305 */ 306 root = OF_finddevice("/"); 307 if (ofw_bus_node_is_compatible(root, "ti,omap4-panda")) 308 pandaboard_usb_hub_init(); 309 #endif 310 311 /* initialise some bus fields */ 312 sc->sc_bus.parent = dev; 313 sc->sc_bus.devices = sc->sc_devices; 314 sc->sc_bus.devices_max = EHCI_MAX_DEVICES; 315 sc->sc_bus.dma_bits = 32; 316 317 sprintf(sc->sc_vendor, "Texas Instruments"); 318 319 /* save the device */ 320 isc->sc_dev = dev; 321 322 /* get all DMA memory */ 323 if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev), 324 &ehci_iterate_hw_softc)) { 325 return (ENOMEM); 326 } 327 328 /* Allocate resource for the EHCI register set */ 329 rid = 0; 330 sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 331 if (!sc->sc_io_res) { 332 device_printf(dev, "Error: Could not map EHCI memory\n"); 333 goto error; 334 } 335 /* Request an interrupt resource */ 336 rid = 0; 337 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 338 if (sc->sc_irq_res == NULL) { 339 device_printf(dev, "Error: could not allocate irq\n"); 340 goto error; 341 } 342 343 /* Add this device as a child of the USBus device */ 344 sc->sc_bus.bdev = device_add_child(dev, "usbus", -1); 345 if (!sc->sc_bus.bdev) { 346 device_printf(dev, "Error: could not add USB device\n"); 347 goto error; 348 } 349 350 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 351 device_set_desc(sc->sc_bus.bdev, OMAP_EHCI_HC_DEVSTR); 352 353 /* Initialise the ECHI registers */ 354 err = omap_ehci_init(isc); 355 if (err) { 356 device_printf(dev, "Error: could not setup OMAP EHCI, %d\n", err); 357 goto error; 358 } 359 360 /* Set the tag and size of the register set in the EHCI context */ 361 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 362 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 363 sc->sc_io_size = rman_get_size(sc->sc_io_res); 364 365 /* Setup the interrupt */ 366 err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 367 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl); 368 if (err) { 369 device_printf(dev, "Error: could not setup irq, %d\n", err); 370 sc->sc_intr_hdl = NULL; 371 goto error; 372 } 373 374 /* Finally we are ready to kick off the ECHI host controller */ 375 err = ehci_init(sc); 376 if (err == 0) { 377 err = device_probe_and_attach(sc->sc_bus.bdev); 378 } 379 if (err) { 380 device_printf(dev, "Error: USB init failed err=%d\n", err); 381 goto error; 382 } 383 384 return (0); 385 386 error: 387 omap_ehci_detach(dev); 388 return (ENXIO); 389 } 390 391 /** 392 * omap_ehci_detach - detach the device and cleanup the driver 393 * @dev: device handle 394 * 395 * Clean-up routine where everything initialised in omap_ehci_attach is 396 * freed and cleaned up. This function calls omap_ehci_fini() to shutdown 397 * the on-chip module. 398 * 399 * LOCKING: 400 * none 401 * 402 * RETURNS: 403 * Always returns 0 (success). 404 */ 405 static int 406 omap_ehci_detach(device_t dev) 407 { 408 struct omap_ehci_softc *isc = device_get_softc(dev); 409 ehci_softc_t *sc = &isc->base; 410 int err; 411 412 /* during module unload there are lots of children leftover */ 413 device_delete_children(dev); 414 415 /* 416 * disable interrupts that might have been switched on in ehci_init 417 */ 418 if (sc->sc_io_res) { 419 EWRITE4(sc, EHCI_USBINTR, 0); 420 } 421 422 if (sc->sc_irq_res && sc->sc_intr_hdl) { 423 /* 424 * only call ehci_detach() after ehci_init() 425 */ 426 ehci_detach(sc); 427 428 err = bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hdl); 429 if (err) 430 device_printf(dev, "Error: could not tear down irq, %d\n", err); 431 sc->sc_intr_hdl = NULL; 432 } 433 434 /* Free the resources stored in the base EHCI handler */ 435 if (sc->sc_irq_res) { 436 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res); 437 sc->sc_irq_res = NULL; 438 } 439 if (sc->sc_io_res) { 440 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_io_res); 441 sc->sc_io_res = NULL; 442 } 443 444 return (0); 445 } 446 447 static device_method_t ehci_methods[] = { 448 /* Device interface */ 449 DEVMETHOD(device_probe, omap_ehci_probe), 450 DEVMETHOD(device_attach, omap_ehci_attach), 451 DEVMETHOD(device_detach, omap_ehci_detach), 452 453 DEVMETHOD(device_suspend, bus_generic_suspend), 454 DEVMETHOD(device_resume, bus_generic_resume), 455 DEVMETHOD(device_shutdown, bus_generic_shutdown), 456 457 /* Bus interface */ 458 DEVMETHOD(bus_print_child, bus_generic_print_child), 459 460 {0, 0} 461 }; 462 463 static driver_t ehci_driver = { 464 "ehci", 465 ehci_methods, 466 sizeof(struct omap_ehci_softc), 467 }; 468 469 static devclass_t ehci_devclass; 470 471 DRIVER_MODULE(omap_ehci, omap_uhh, ehci_driver, ehci_devclass, 0, 0); 472