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