15b03aba6SOleksandr Tymoshenko /*- 25b03aba6SOleksandr Tymoshenko * Copyright (c) 2013 Oleksandr Tymoshenko <gonzo@freebsd.org> 35b03aba6SOleksandr Tymoshenko * 45b03aba6SOleksandr Tymoshenko * Redistribution and use in source and binary forms, with or without 55b03aba6SOleksandr Tymoshenko * modification, are permitted provided that the following conditions 65b03aba6SOleksandr Tymoshenko * are met: 75b03aba6SOleksandr Tymoshenko * 1. Redistributions of source code must retain the above copyright 85b03aba6SOleksandr Tymoshenko * notice, this list of conditions and the following disclaimer. 95b03aba6SOleksandr Tymoshenko * 2. Redistributions in binary form must reproduce the above copyright 105b03aba6SOleksandr Tymoshenko * notice, this list of conditions and the following disclaimer in the 115b03aba6SOleksandr Tymoshenko * documentation and/or other materials provided with the distribution. 125b03aba6SOleksandr Tymoshenko * 135b03aba6SOleksandr Tymoshenko * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 145b03aba6SOleksandr Tymoshenko * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 155b03aba6SOleksandr Tymoshenko * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 165b03aba6SOleksandr Tymoshenko * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 175b03aba6SOleksandr Tymoshenko * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 185b03aba6SOleksandr Tymoshenko * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 195b03aba6SOleksandr Tymoshenko * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 205b03aba6SOleksandr Tymoshenko * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 215b03aba6SOleksandr Tymoshenko * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 225b03aba6SOleksandr Tymoshenko * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 235b03aba6SOleksandr Tymoshenko * SUCH DAMAGE. 245b03aba6SOleksandr Tymoshenko */ 255b03aba6SOleksandr Tymoshenko 265b03aba6SOleksandr Tymoshenko #include <sys/cdefs.h> 275b03aba6SOleksandr Tymoshenko #include <sys/stdint.h> 285b03aba6SOleksandr Tymoshenko #include <sys/stddef.h> 295b03aba6SOleksandr Tymoshenko #include <sys/param.h> 305b03aba6SOleksandr Tymoshenko #include <sys/queue.h> 315b03aba6SOleksandr Tymoshenko #include <sys/types.h> 325b03aba6SOleksandr Tymoshenko #include <sys/systm.h> 335b03aba6SOleksandr Tymoshenko #include <sys/kernel.h> 345b03aba6SOleksandr Tymoshenko #include <sys/bus.h> 355b03aba6SOleksandr Tymoshenko #include <sys/module.h> 365b03aba6SOleksandr Tymoshenko #include <sys/lock.h> 375b03aba6SOleksandr Tymoshenko #include <sys/mutex.h> 385b03aba6SOleksandr Tymoshenko #include <sys/condvar.h> 395b03aba6SOleksandr Tymoshenko #include <sys/sysctl.h> 405b03aba6SOleksandr Tymoshenko #include <sys/sx.h> 415b03aba6SOleksandr Tymoshenko #include <sys/unistd.h> 425b03aba6SOleksandr Tymoshenko #include <sys/callout.h> 435b03aba6SOleksandr Tymoshenko #include <sys/malloc.h> 445b03aba6SOleksandr Tymoshenko #include <sys/priv.h> 455b03aba6SOleksandr Tymoshenko 465b03aba6SOleksandr Tymoshenko #include <dev/ofw/openfirm.h> 475b03aba6SOleksandr Tymoshenko #include <dev/ofw/ofw_bus.h> 485b03aba6SOleksandr Tymoshenko #include <dev/ofw/ofw_bus_subr.h> 495b03aba6SOleksandr Tymoshenko 505b03aba6SOleksandr Tymoshenko #include <dev/usb/usb.h> 515b03aba6SOleksandr Tymoshenko #include <dev/usb/usbdi.h> 525b03aba6SOleksandr Tymoshenko 535b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_core.h> 545b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_busdma.h> 555b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_process.h> 565b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_util.h> 575b03aba6SOleksandr Tymoshenko 585b03aba6SOleksandr Tymoshenko #define USB_DEBUG_VAR usbssdebug 595b03aba6SOleksandr Tymoshenko 605b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_controller.h> 615b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_bus.h> 625b03aba6SOleksandr Tymoshenko #include <dev/usb/controller/musb_otg.h> 635b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_debug.h> 645b03aba6SOleksandr Tymoshenko 655b03aba6SOleksandr Tymoshenko #include <sys/rman.h> 665b03aba6SOleksandr Tymoshenko 675b03aba6SOleksandr Tymoshenko #include <arm/ti/am335x/am335x_scm.h> 680050ea24SMichal Meloun #include <arm/ti/ti_sysc.h> 69be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h> 7062e8ccc3SEmmanuel Vadot #include <dev/syscon/syscon.h> 710050ea24SMichal Meloun #include "syscon_if.h" 725b03aba6SOleksandr Tymoshenko 735b03aba6SOleksandr Tymoshenko #define USBCTRL_REV 0x00 745b03aba6SOleksandr Tymoshenko #define USBCTRL_CTRL 0x14 755b03aba6SOleksandr Tymoshenko #define USBCTRL_STAT 0x18 765b03aba6SOleksandr Tymoshenko #define USBCTRL_IRQ_STAT0 0x30 775b03aba6SOleksandr Tymoshenko #define IRQ_STAT0_RXSHIFT 16 785b03aba6SOleksandr Tymoshenko #define IRQ_STAT0_TXSHIFT 0 795b03aba6SOleksandr Tymoshenko #define USBCTRL_IRQ_STAT1 0x34 805b03aba6SOleksandr Tymoshenko #define IRQ_STAT1_DRVVBUS (1 << 8) 815b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_SET0 0x38 825b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_SET1 0x3C 835b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_USB_ALL 0x1ff 845b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_USB_SOF (1 << 3) 855b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_CLR0 0x40 865b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_CLR1 0x44 875b03aba6SOleksandr Tymoshenko #define USBCTRL_UTMI 0xE0 885b03aba6SOleksandr Tymoshenko #define USBCTRL_UTMI_FSDATAEXT (1 << 1) 895b03aba6SOleksandr Tymoshenko #define USBCTRL_MODE 0xE8 905b03aba6SOleksandr Tymoshenko #define USBCTRL_MODE_IDDIG (1 << 8) 915b03aba6SOleksandr Tymoshenko #define USBCTRL_MODE_IDDIGMUX (1 << 7) 925b03aba6SOleksandr Tymoshenko 935b03aba6SOleksandr Tymoshenko /* USBSS resource + 2 MUSB ports */ 945b03aba6SOleksandr Tymoshenko 955b03aba6SOleksandr Tymoshenko #define RES_USBCORE 0 965b03aba6SOleksandr Tymoshenko #define RES_USBCTRL 1 975b03aba6SOleksandr Tymoshenko 985b03aba6SOleksandr Tymoshenko #define USB_WRITE4(sc, idx, reg, val) do { \ 995b03aba6SOleksandr Tymoshenko bus_write_4((sc)->sc_mem_res[idx], (reg), (val)); \ 1005b03aba6SOleksandr Tymoshenko } while (0) 1015b03aba6SOleksandr Tymoshenko 1025b03aba6SOleksandr Tymoshenko #define USB_READ4(sc, idx, reg) bus_read_4((sc)->sc_mem_res[idx], (reg)) 1035b03aba6SOleksandr Tymoshenko 1045b03aba6SOleksandr Tymoshenko #define USBCTRL_WRITE4(sc, reg, val) \ 1055b03aba6SOleksandr Tymoshenko USB_WRITE4((sc), RES_USBCTRL, (reg), (val)) 1065b03aba6SOleksandr Tymoshenko #define USBCTRL_READ4(sc, reg) \ 1075b03aba6SOleksandr Tymoshenko USB_READ4((sc), RES_USBCTRL, (reg)) 1085b03aba6SOleksandr Tymoshenko 1095b03aba6SOleksandr Tymoshenko static struct resource_spec am335x_musbotg_mem_spec[] = { 1105b03aba6SOleksandr Tymoshenko { SYS_RES_MEMORY, 0, RF_ACTIVE }, 1115b03aba6SOleksandr Tymoshenko { SYS_RES_MEMORY, 1, RF_ACTIVE }, 1125b03aba6SOleksandr Tymoshenko { -1, 0, 0 } 1135b03aba6SOleksandr Tymoshenko }; 1145b03aba6SOleksandr Tymoshenko 1155b03aba6SOleksandr Tymoshenko #ifdef USB_DEBUG 1165b03aba6SOleksandr Tymoshenko static int usbssdebug = 0; 1175b03aba6SOleksandr Tymoshenko 1187029da5cSPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, am335x_usbss, 1197029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1207029da5cSPawel Biernacki "AM335x USBSS"); 1215b03aba6SOleksandr Tymoshenko SYSCTL_INT(_hw_usb_am335x_usbss, OID_AUTO, debug, CTLFLAG_RW, 1225b03aba6SOleksandr Tymoshenko &usbssdebug, 0, "Debug level"); 1235b03aba6SOleksandr Tymoshenko #endif 1245b03aba6SOleksandr Tymoshenko 1255b03aba6SOleksandr Tymoshenko static device_probe_t musbotg_probe; 1265b03aba6SOleksandr Tymoshenko static device_attach_t musbotg_attach; 1275b03aba6SOleksandr Tymoshenko static device_detach_t musbotg_detach; 1285b03aba6SOleksandr Tymoshenko 1295b03aba6SOleksandr Tymoshenko struct musbotg_super_softc { 1305b03aba6SOleksandr Tymoshenko struct musbotg_softc sc_otg; 1315b03aba6SOleksandr Tymoshenko struct resource *sc_mem_res[2]; 1325b03aba6SOleksandr Tymoshenko int sc_irq_rid; 1330050ea24SMichal Meloun struct syscon *syscon; 1345b03aba6SOleksandr Tymoshenko }; 1355b03aba6SOleksandr Tymoshenko 1365b03aba6SOleksandr Tymoshenko static void 1375b03aba6SOleksandr Tymoshenko musbotg_vbus_poll(struct musbotg_super_softc *sc) 1385b03aba6SOleksandr Tymoshenko { 1395b03aba6SOleksandr Tymoshenko uint32_t stat; 1405b03aba6SOleksandr Tymoshenko 1415b03aba6SOleksandr Tymoshenko if (sc->sc_otg.sc_mode == MUSB2_DEVICE_MODE) 1425b03aba6SOleksandr Tymoshenko musbotg_vbus_interrupt(&sc->sc_otg, 1); 1435b03aba6SOleksandr Tymoshenko else { 1445b03aba6SOleksandr Tymoshenko stat = USBCTRL_READ4(sc, USBCTRL_STAT); 1455b03aba6SOleksandr Tymoshenko musbotg_vbus_interrupt(&sc->sc_otg, stat & 1); 1465b03aba6SOleksandr Tymoshenko } 1475b03aba6SOleksandr Tymoshenko } 1485b03aba6SOleksandr Tymoshenko 1495b03aba6SOleksandr Tymoshenko /* 1505b03aba6SOleksandr Tymoshenko * Arg to musbotg_clocks_on and musbot_clocks_off is 1515b03aba6SOleksandr Tymoshenko * a uint32_t * pointing to the SCM register offset. 1525b03aba6SOleksandr Tymoshenko */ 1535b03aba6SOleksandr Tymoshenko static uint32_t USB_CTRL[] = {SCM_USB_CTRL0, SCM_USB_CTRL1}; 1545b03aba6SOleksandr Tymoshenko 1555b03aba6SOleksandr Tymoshenko static void 1565b03aba6SOleksandr Tymoshenko musbotg_clocks_on(void *arg) 1575b03aba6SOleksandr Tymoshenko { 1585b03aba6SOleksandr Tymoshenko struct musbotg_softc *sc; 1590050ea24SMichal Meloun struct musbotg_super_softc *ssc; 1600050ea24SMichal Meloun uint32_t reg; 1615b03aba6SOleksandr Tymoshenko 1625b03aba6SOleksandr Tymoshenko sc = arg; 1630050ea24SMichal Meloun ssc = sc->sc_platform_data; 1645b03aba6SOleksandr Tymoshenko 1650050ea24SMichal Meloun reg = SYSCON_READ_4(ssc->syscon, USB_CTRL[sc->sc_id]); 1660050ea24SMichal Meloun reg &= ~3; /* Enable power */ 1670050ea24SMichal Meloun reg |= 1 << 19; /* VBUS detect enable */ 1680050ea24SMichal Meloun reg |= 1 << 20; /* Session end enable */ 1690050ea24SMichal Meloun 1700050ea24SMichal Meloun SYSCON_WRITE_4(ssc->syscon, USB_CTRL[sc->sc_id], reg); 1715b03aba6SOleksandr Tymoshenko } 1725b03aba6SOleksandr Tymoshenko 1735b03aba6SOleksandr Tymoshenko static void 1745b03aba6SOleksandr Tymoshenko musbotg_clocks_off(void *arg) 1755b03aba6SOleksandr Tymoshenko { 1765b03aba6SOleksandr Tymoshenko struct musbotg_softc *sc; 1770050ea24SMichal Meloun struct musbotg_super_softc *ssc; 1780050ea24SMichal Meloun uint32_t reg; 1795b03aba6SOleksandr Tymoshenko 1805b03aba6SOleksandr Tymoshenko sc = arg; 1810050ea24SMichal Meloun ssc = sc->sc_platform_data; 1825b03aba6SOleksandr Tymoshenko 1835b03aba6SOleksandr Tymoshenko /* Disable power to PHY */ 1840050ea24SMichal Meloun reg = SYSCON_READ_4(ssc->syscon, USB_CTRL[sc->sc_id]); 1850050ea24SMichal Meloun SYSCON_WRITE_4(ssc->syscon, USB_CTRL[sc->sc_id], reg | 3); 1865b03aba6SOleksandr Tymoshenko } 1875b03aba6SOleksandr Tymoshenko 1885b03aba6SOleksandr Tymoshenko static void 1895b03aba6SOleksandr Tymoshenko musbotg_ep_int_set(struct musbotg_softc *sc, int ep, int on) 1905b03aba6SOleksandr Tymoshenko { 1915b03aba6SOleksandr Tymoshenko struct musbotg_super_softc *ssc = sc->sc_platform_data; 1925b03aba6SOleksandr Tymoshenko uint32_t epmask; 1935b03aba6SOleksandr Tymoshenko 1945b03aba6SOleksandr Tymoshenko epmask = ((1 << ep) << IRQ_STAT0_RXSHIFT); 1955b03aba6SOleksandr Tymoshenko epmask |= ((1 << ep) << IRQ_STAT0_TXSHIFT); 1965b03aba6SOleksandr Tymoshenko if (on) 1975b03aba6SOleksandr Tymoshenko USBCTRL_WRITE4(ssc, USBCTRL_INTEN_SET0, epmask); 1985b03aba6SOleksandr Tymoshenko else 1995b03aba6SOleksandr Tymoshenko USBCTRL_WRITE4(ssc, USBCTRL_INTEN_CLR0, epmask); 2005b03aba6SOleksandr Tymoshenko } 2015b03aba6SOleksandr Tymoshenko 2025b03aba6SOleksandr Tymoshenko static void 2035b03aba6SOleksandr Tymoshenko musbotg_wrapper_interrupt(void *arg) 2045b03aba6SOleksandr Tymoshenko { 2055b03aba6SOleksandr Tymoshenko struct musbotg_softc *sc = arg; 2065b03aba6SOleksandr Tymoshenko struct musbotg_super_softc *ssc = sc->sc_platform_data; 2075b03aba6SOleksandr Tymoshenko uint32_t stat, stat0, stat1; 2085b03aba6SOleksandr Tymoshenko 2095b03aba6SOleksandr Tymoshenko stat = USBCTRL_READ4(ssc, USBCTRL_STAT); 2105b03aba6SOleksandr Tymoshenko stat0 = USBCTRL_READ4(ssc, USBCTRL_IRQ_STAT0); 2115b03aba6SOleksandr Tymoshenko stat1 = USBCTRL_READ4(ssc, USBCTRL_IRQ_STAT1); 2125b03aba6SOleksandr Tymoshenko if (stat0) 2135b03aba6SOleksandr Tymoshenko USBCTRL_WRITE4(ssc, USBCTRL_IRQ_STAT0, stat0); 2145b03aba6SOleksandr Tymoshenko if (stat1) 2155b03aba6SOleksandr Tymoshenko USBCTRL_WRITE4(ssc, USBCTRL_IRQ_STAT1, stat1); 2165b03aba6SOleksandr Tymoshenko 2175b03aba6SOleksandr Tymoshenko DPRINTFN(4, "port%d: stat0=%08x stat1=%08x, stat=%08x\n", 2185b03aba6SOleksandr Tymoshenko sc->sc_id, stat0, stat1, stat); 2195b03aba6SOleksandr Tymoshenko 2205b03aba6SOleksandr Tymoshenko if (stat1 & IRQ_STAT1_DRVVBUS) 2215b03aba6SOleksandr Tymoshenko musbotg_vbus_interrupt(sc, stat & 1); 2225b03aba6SOleksandr Tymoshenko 2235b03aba6SOleksandr Tymoshenko musbotg_interrupt(arg, ((stat0 >> 16) & 0xffff), 2245b03aba6SOleksandr Tymoshenko stat0 & 0xffff, stat1 & 0xff); 2255b03aba6SOleksandr Tymoshenko } 2265b03aba6SOleksandr Tymoshenko 2275b03aba6SOleksandr Tymoshenko static int 2285b03aba6SOleksandr Tymoshenko musbotg_probe(device_t dev) 2295b03aba6SOleksandr Tymoshenko { 2305b03aba6SOleksandr Tymoshenko if (!ofw_bus_status_okay(dev)) 2315b03aba6SOleksandr Tymoshenko return (ENXIO); 2325b03aba6SOleksandr Tymoshenko 2335b03aba6SOleksandr Tymoshenko if (!ofw_bus_is_compatible(dev, "ti,musb-am33xx")) 2345b03aba6SOleksandr Tymoshenko return (ENXIO); 2355b03aba6SOleksandr Tymoshenko 2365b03aba6SOleksandr Tymoshenko device_set_desc(dev, "TI AM33xx integrated USB OTG controller"); 2375b03aba6SOleksandr Tymoshenko 2385b03aba6SOleksandr Tymoshenko return (BUS_PROBE_DEFAULT); 2395b03aba6SOleksandr Tymoshenko } 2405b03aba6SOleksandr Tymoshenko 2415b03aba6SOleksandr Tymoshenko static int 2425b03aba6SOleksandr Tymoshenko musbotg_attach(device_t dev) 2435b03aba6SOleksandr Tymoshenko { 2445b03aba6SOleksandr Tymoshenko struct musbotg_super_softc *sc = device_get_softc(dev); 24533fe8378SLuiz Otavio O Souza char mode[16]; 2465b03aba6SOleksandr Tymoshenko int err; 2475b03aba6SOleksandr Tymoshenko uint32_t reg; 2480050ea24SMichal Meloun phandle_t opp_table; 2490050ea24SMichal Meloun clk_t clk_usbotg_fck; 2505b03aba6SOleksandr Tymoshenko 2515b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_id = device_get_unit(dev); 2525b03aba6SOleksandr Tymoshenko 2530050ea24SMichal Meloun /* FIXME: The devicetree needs to be updated to get a handle to the gate 2540050ea24SMichal Meloun * usbotg_fck@47c. see TRM 8.1.12.2 CM_WKUP CM_CLKDCOLDO_DPLL_PER. 2550050ea24SMichal Meloun */ 2560050ea24SMichal Meloun err = clk_get_by_name(dev, "usbotg_fck@47c", &clk_usbotg_fck); 2570050ea24SMichal Meloun if (err) { 2580050ea24SMichal Meloun device_printf(dev, "Can not find usbotg_fck@47c\n"); 2590050ea24SMichal Meloun return (ENXIO); 2600050ea24SMichal Meloun } 2610050ea24SMichal Meloun 2620050ea24SMichal Meloun err = clk_enable(clk_usbotg_fck); 2630050ea24SMichal Meloun if (err) { 2640050ea24SMichal Meloun device_printf(dev, "Can not enable usbotg_fck@47c\n"); 2650050ea24SMichal Meloun return (ENXIO); 2660050ea24SMichal Meloun } 2670050ea24SMichal Meloun 2680050ea24SMichal Meloun /* FIXME: For now; Go and kidnap syscon from opp-table */ 2690050ea24SMichal Meloun opp_table = OF_finddevice("/opp-table"); 2700050ea24SMichal Meloun if (opp_table == -1) { 2710050ea24SMichal Meloun device_printf(dev, "Cant find /opp-table\n"); 2720050ea24SMichal Meloun return (ENXIO); 2730050ea24SMichal Meloun } 2740050ea24SMichal Meloun if (!OF_hasprop(opp_table, "syscon")) { 2750050ea24SMichal Meloun device_printf(dev, "/opp-table missing syscon property\n"); 2760050ea24SMichal Meloun return (ENXIO); 2770050ea24SMichal Meloun } 2780050ea24SMichal Meloun err = syscon_get_by_ofw_property(dev, opp_table, "syscon", &sc->syscon); 2790050ea24SMichal Meloun if (err) { 2800050ea24SMichal Meloun device_printf(dev, "Failed to get syscon\n"); 2810050ea24SMichal Meloun return (ENXIO); 2820050ea24SMichal Meloun } 2830050ea24SMichal Meloun 2845b03aba6SOleksandr Tymoshenko /* Request the memory resources */ 2855b03aba6SOleksandr Tymoshenko err = bus_alloc_resources(dev, am335x_musbotg_mem_spec, 2865b03aba6SOleksandr Tymoshenko sc->sc_mem_res); 2875b03aba6SOleksandr Tymoshenko if (err) { 2885b03aba6SOleksandr Tymoshenko device_printf(dev, 2895b03aba6SOleksandr Tymoshenko "Error: could not allocate mem resources\n"); 2905b03aba6SOleksandr Tymoshenko return (ENXIO); 2915b03aba6SOleksandr Tymoshenko } 2925b03aba6SOleksandr Tymoshenko 2935b03aba6SOleksandr Tymoshenko /* Request the IRQ resources */ 2945b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, 2955b03aba6SOleksandr Tymoshenko &sc->sc_irq_rid, RF_ACTIVE); 2965b03aba6SOleksandr Tymoshenko if (sc->sc_otg.sc_irq_res == NULL) { 2975b03aba6SOleksandr Tymoshenko device_printf(dev, 2985b03aba6SOleksandr Tymoshenko "Error: could not allocate irq resources\n"); 2995b03aba6SOleksandr Tymoshenko return (ENXIO); 3005b03aba6SOleksandr Tymoshenko } 3015b03aba6SOleksandr Tymoshenko 3025b03aba6SOleksandr Tymoshenko /* setup MUSB OTG USB controller interface softc */ 3035b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_clocks_on = &musbotg_clocks_on; 3045b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_clocks_off = &musbotg_clocks_off; 3055b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_clocks_arg = &sc->sc_otg; 3065b03aba6SOleksandr Tymoshenko 3075b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_ep_int_set = musbotg_ep_int_set; 3085b03aba6SOleksandr Tymoshenko 3095b03aba6SOleksandr Tymoshenko /* initialise some bus fields */ 3105b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_bus.parent = dev; 3115b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_bus.devices = sc->sc_otg.sc_devices; 3125b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_bus.devices_max = MUSB2_MAX_DEVICES; 3135b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_bus.dma_bits = 32; 3145b03aba6SOleksandr Tymoshenko 3155b03aba6SOleksandr Tymoshenko /* get all DMA memory */ 3165b03aba6SOleksandr Tymoshenko if (usb_bus_mem_alloc_all(&sc->sc_otg.sc_bus, 3175b03aba6SOleksandr Tymoshenko USB_GET_DMA_TAG(dev), NULL)) { 3185b03aba6SOleksandr Tymoshenko device_printf(dev, 3195b03aba6SOleksandr Tymoshenko "Failed allocate bus mem for musb\n"); 3205b03aba6SOleksandr Tymoshenko return (ENOMEM); 3215b03aba6SOleksandr Tymoshenko } 3225b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_io_res = sc->sc_mem_res[RES_USBCORE]; 3235b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_io_tag = 3245b03aba6SOleksandr Tymoshenko rman_get_bustag(sc->sc_otg.sc_io_res); 3255b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_io_hdl = 3265b03aba6SOleksandr Tymoshenko rman_get_bushandle(sc->sc_otg.sc_io_res); 3275b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_io_size = 3285b03aba6SOleksandr Tymoshenko rman_get_size(sc->sc_otg.sc_io_res); 3295b03aba6SOleksandr Tymoshenko 330*5b56413dSWarner Losh sc->sc_otg.sc_bus.bdev = device_add_child(dev, "usbus", DEVICE_UNIT_ANY); 3315b03aba6SOleksandr Tymoshenko if (!(sc->sc_otg.sc_bus.bdev)) { 3325b03aba6SOleksandr Tymoshenko device_printf(dev, "No busdev for musb\n"); 3335b03aba6SOleksandr Tymoshenko goto error; 3345b03aba6SOleksandr Tymoshenko } 3355b03aba6SOleksandr Tymoshenko device_set_ivars(sc->sc_otg.sc_bus.bdev, 3365b03aba6SOleksandr Tymoshenko &sc->sc_otg.sc_bus); 3375b03aba6SOleksandr Tymoshenko 3385b03aba6SOleksandr Tymoshenko err = bus_setup_intr(dev, sc->sc_otg.sc_irq_res, 3395b03aba6SOleksandr Tymoshenko INTR_TYPE_BIO | INTR_MPSAFE, 3405b03aba6SOleksandr Tymoshenko NULL, (driver_intr_t *)musbotg_wrapper_interrupt, 3415b03aba6SOleksandr Tymoshenko &sc->sc_otg, &sc->sc_otg.sc_intr_hdl); 3425b03aba6SOleksandr Tymoshenko if (err) { 3435b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_intr_hdl = NULL; 3445b03aba6SOleksandr Tymoshenko device_printf(dev, 3455b03aba6SOleksandr Tymoshenko "Failed to setup interrupt for musb\n"); 3465b03aba6SOleksandr Tymoshenko goto error; 3475b03aba6SOleksandr Tymoshenko } 3485b03aba6SOleksandr Tymoshenko 3495b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_platform_data = sc; 35033fe8378SLuiz Otavio O Souza if (OF_getprop(ofw_bus_get_node(dev), "dr_mode", mode, 35133fe8378SLuiz Otavio O Souza sizeof(mode)) > 0) { 35233fe8378SLuiz Otavio O Souza if (strcasecmp(mode, "host") == 0) 35333fe8378SLuiz Otavio O Souza sc->sc_otg.sc_mode = MUSB2_HOST_MODE; 35433fe8378SLuiz Otavio O Souza else 35533fe8378SLuiz Otavio O Souza sc->sc_otg.sc_mode = MUSB2_DEVICE_MODE; 35633fe8378SLuiz Otavio O Souza } else { 35733fe8378SLuiz Otavio O Souza /* Beaglebone defaults: USB0 device, USB1 HOST. */ 3585b03aba6SOleksandr Tymoshenko if (sc->sc_otg.sc_id == 0) 3595b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_mode = MUSB2_DEVICE_MODE; 3605b03aba6SOleksandr Tymoshenko else 3615b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_mode = MUSB2_HOST_MODE; 36233fe8378SLuiz Otavio O Souza } 3635b03aba6SOleksandr Tymoshenko 3645b03aba6SOleksandr Tymoshenko /* 3655b03aba6SOleksandr Tymoshenko * software-controlled function 3665b03aba6SOleksandr Tymoshenko */ 3675b03aba6SOleksandr Tymoshenko 3685b03aba6SOleksandr Tymoshenko if (sc->sc_otg.sc_mode == MUSB2_HOST_MODE) { 3695b03aba6SOleksandr Tymoshenko reg = USBCTRL_READ4(sc, USBCTRL_MODE); 3705b03aba6SOleksandr Tymoshenko reg |= USBCTRL_MODE_IDDIGMUX; 3715b03aba6SOleksandr Tymoshenko reg &= ~USBCTRL_MODE_IDDIG; 3725b03aba6SOleksandr Tymoshenko USBCTRL_WRITE4(sc, USBCTRL_MODE, reg); 3735b03aba6SOleksandr Tymoshenko USBCTRL_WRITE4(sc, USBCTRL_UTMI, 3745b03aba6SOleksandr Tymoshenko USBCTRL_UTMI_FSDATAEXT); 3755b03aba6SOleksandr Tymoshenko } else { 3765b03aba6SOleksandr Tymoshenko reg = USBCTRL_READ4(sc, USBCTRL_MODE); 3775b03aba6SOleksandr Tymoshenko reg |= USBCTRL_MODE_IDDIGMUX; 3785b03aba6SOleksandr Tymoshenko reg |= USBCTRL_MODE_IDDIG; 3795b03aba6SOleksandr Tymoshenko USBCTRL_WRITE4(sc, USBCTRL_MODE, reg); 3805b03aba6SOleksandr Tymoshenko } 3815b03aba6SOleksandr Tymoshenko 3825b03aba6SOleksandr Tymoshenko reg = USBCTRL_INTEN_USB_ALL & ~USBCTRL_INTEN_USB_SOF; 3835b03aba6SOleksandr Tymoshenko USBCTRL_WRITE4(sc, USBCTRL_INTEN_SET1, reg); 3845b03aba6SOleksandr Tymoshenko USBCTRL_WRITE4(sc, USBCTRL_INTEN_CLR0, 0xffffffff); 3855b03aba6SOleksandr Tymoshenko 3865b03aba6SOleksandr Tymoshenko err = musbotg_init(&sc->sc_otg); 3875b03aba6SOleksandr Tymoshenko if (!err) 3885b03aba6SOleksandr Tymoshenko err = device_probe_and_attach(sc->sc_otg.sc_bus.bdev); 3895b03aba6SOleksandr Tymoshenko 3905b03aba6SOleksandr Tymoshenko if (err) 3915b03aba6SOleksandr Tymoshenko goto error; 3925b03aba6SOleksandr Tymoshenko 3935b03aba6SOleksandr Tymoshenko /* poll VBUS one time */ 3945b03aba6SOleksandr Tymoshenko musbotg_vbus_poll(sc); 3955b03aba6SOleksandr Tymoshenko 3965b03aba6SOleksandr Tymoshenko return (0); 3975b03aba6SOleksandr Tymoshenko 3985b03aba6SOleksandr Tymoshenko error: 3995b03aba6SOleksandr Tymoshenko musbotg_detach(dev); 4005b03aba6SOleksandr Tymoshenko return (ENXIO); 4015b03aba6SOleksandr Tymoshenko } 4025b03aba6SOleksandr Tymoshenko 4035b03aba6SOleksandr Tymoshenko static int 4045b03aba6SOleksandr Tymoshenko musbotg_detach(device_t dev) 4055b03aba6SOleksandr Tymoshenko { 4065b03aba6SOleksandr Tymoshenko struct musbotg_super_softc *sc = device_get_softc(dev); 4075b03aba6SOleksandr Tymoshenko 408d3bf5efcSHans Petter Selasky /* during module unload there are lots of children leftover */ 409d3bf5efcSHans Petter Selasky device_delete_children(dev); 4105b03aba6SOleksandr Tymoshenko 4115b03aba6SOleksandr Tymoshenko if (sc->sc_otg.sc_irq_res && sc->sc_otg.sc_intr_hdl) { 4125b03aba6SOleksandr Tymoshenko /* 4135b03aba6SOleksandr Tymoshenko * only call musbotg_uninit() after musbotg_init() 4145b03aba6SOleksandr Tymoshenko */ 4155b03aba6SOleksandr Tymoshenko musbotg_uninit(&sc->sc_otg); 4165b03aba6SOleksandr Tymoshenko 4170d46a96aSJohn Baldwin bus_teardown_intr(dev, sc->sc_otg.sc_irq_res, 4185b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_intr_hdl); 4195b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_intr_hdl = NULL; 4205b03aba6SOleksandr Tymoshenko } 4215b03aba6SOleksandr Tymoshenko 4225b03aba6SOleksandr Tymoshenko usb_bus_mem_free_all(&sc->sc_otg.sc_bus, NULL); 4235b03aba6SOleksandr Tymoshenko 4245b03aba6SOleksandr Tymoshenko /* Free resources if any */ 4255b03aba6SOleksandr Tymoshenko if (sc->sc_mem_res[0]) 4265b03aba6SOleksandr Tymoshenko bus_release_resources(dev, am335x_musbotg_mem_spec, 4275b03aba6SOleksandr Tymoshenko sc->sc_mem_res); 4285b03aba6SOleksandr Tymoshenko 4295b03aba6SOleksandr Tymoshenko if (sc->sc_otg.sc_irq_res) 4305b03aba6SOleksandr Tymoshenko bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid, 4315b03aba6SOleksandr Tymoshenko sc->sc_otg.sc_irq_res); 4325b03aba6SOleksandr Tymoshenko 4335b03aba6SOleksandr Tymoshenko return (0); 4345b03aba6SOleksandr Tymoshenko } 4355b03aba6SOleksandr Tymoshenko 4365b03aba6SOleksandr Tymoshenko static device_method_t musbotg_methods[] = { 4375b03aba6SOleksandr Tymoshenko /* Device interface */ 4385b03aba6SOleksandr Tymoshenko DEVMETHOD(device_probe, musbotg_probe), 4395b03aba6SOleksandr Tymoshenko DEVMETHOD(device_attach, musbotg_attach), 4405b03aba6SOleksandr Tymoshenko DEVMETHOD(device_detach, musbotg_detach), 4415b03aba6SOleksandr Tymoshenko DEVMETHOD(device_suspend, bus_generic_suspend), 4425b03aba6SOleksandr Tymoshenko DEVMETHOD(device_resume, bus_generic_resume), 4435b03aba6SOleksandr Tymoshenko DEVMETHOD(device_shutdown, bus_generic_shutdown), 4445b03aba6SOleksandr Tymoshenko 4455b03aba6SOleksandr Tymoshenko DEVMETHOD_END 4465b03aba6SOleksandr Tymoshenko }; 4475b03aba6SOleksandr Tymoshenko 4485b03aba6SOleksandr Tymoshenko static driver_t musbotg_driver = { 4495b03aba6SOleksandr Tymoshenko .name = "musbotg", 4505b03aba6SOleksandr Tymoshenko .methods = musbotg_methods, 4515b03aba6SOleksandr Tymoshenko .size = sizeof(struct musbotg_super_softc), 4525b03aba6SOleksandr Tymoshenko }; 4535b03aba6SOleksandr Tymoshenko 4548537e671SJohn Baldwin DRIVER_MODULE(musbotg, ti_sysc, musbotg_driver, 0, 0); 4550050ea24SMichal Meloun MODULE_DEPEND(musbotg, ti_sysc, 1, 1, 1); 4560050ea24SMichal Meloun MODULE_DEPEND(musbotg, ti_am3359_cppi41, 1, 1, 1); 4570050ea24SMichal Meloun MODULE_DEPEND(usbss, usb, 1, 1, 1); 458