xref: /freebsd/sys/arm/ti/am335x/am335x_musb.c (revision d3bf5efc1ff19b102b176512a66110ec84ac982d)
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 __FBSDID("$FreeBSD$");
285b03aba6SOleksandr Tymoshenko 
295b03aba6SOleksandr Tymoshenko #include <sys/stdint.h>
305b03aba6SOleksandr Tymoshenko #include <sys/stddef.h>
315b03aba6SOleksandr Tymoshenko #include <sys/param.h>
325b03aba6SOleksandr Tymoshenko #include <sys/queue.h>
335b03aba6SOleksandr Tymoshenko #include <sys/types.h>
345b03aba6SOleksandr Tymoshenko #include <sys/systm.h>
355b03aba6SOleksandr Tymoshenko #include <sys/kernel.h>
365b03aba6SOleksandr Tymoshenko #include <sys/bus.h>
375b03aba6SOleksandr Tymoshenko #include <sys/module.h>
385b03aba6SOleksandr Tymoshenko #include <sys/lock.h>
395b03aba6SOleksandr Tymoshenko #include <sys/mutex.h>
405b03aba6SOleksandr Tymoshenko #include <sys/condvar.h>
415b03aba6SOleksandr Tymoshenko #include <sys/sysctl.h>
425b03aba6SOleksandr Tymoshenko #include <sys/sx.h>
435b03aba6SOleksandr Tymoshenko #include <sys/unistd.h>
445b03aba6SOleksandr Tymoshenko #include <sys/callout.h>
455b03aba6SOleksandr Tymoshenko #include <sys/malloc.h>
465b03aba6SOleksandr Tymoshenko #include <sys/priv.h>
475b03aba6SOleksandr Tymoshenko 
485b03aba6SOleksandr Tymoshenko #include <dev/fdt/fdt_common.h>
495b03aba6SOleksandr Tymoshenko #include <dev/ofw/openfirm.h>
505b03aba6SOleksandr Tymoshenko #include <dev/ofw/ofw_bus.h>
515b03aba6SOleksandr Tymoshenko #include <dev/ofw/ofw_bus_subr.h>
525b03aba6SOleksandr Tymoshenko 
535b03aba6SOleksandr Tymoshenko #include <dev/usb/usb.h>
545b03aba6SOleksandr Tymoshenko #include <dev/usb/usbdi.h>
555b03aba6SOleksandr Tymoshenko 
565b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_core.h>
575b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_busdma.h>
585b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_process.h>
595b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_util.h>
605b03aba6SOleksandr Tymoshenko 
615b03aba6SOleksandr Tymoshenko #define	USB_DEBUG_VAR usbssdebug
625b03aba6SOleksandr Tymoshenko 
635b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_controller.h>
645b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_bus.h>
655b03aba6SOleksandr Tymoshenko #include <dev/usb/controller/musb_otg.h>
665b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_debug.h>
675b03aba6SOleksandr Tymoshenko 
685b03aba6SOleksandr Tymoshenko #include <sys/rman.h>
695b03aba6SOleksandr Tymoshenko 
705b03aba6SOleksandr Tymoshenko #include <arm/ti/ti_prcm.h>
715b03aba6SOleksandr Tymoshenko #include <arm/ti/ti_scm.h>
725b03aba6SOleksandr Tymoshenko #include <arm/ti/am335x/am335x_scm.h>
735b03aba6SOleksandr Tymoshenko 
745b03aba6SOleksandr Tymoshenko #define USBCTRL_REV		0x00
755b03aba6SOleksandr Tymoshenko #define USBCTRL_CTRL		0x14
765b03aba6SOleksandr Tymoshenko #define USBCTRL_STAT		0x18
775b03aba6SOleksandr Tymoshenko #define USBCTRL_IRQ_STAT0	0x30
785b03aba6SOleksandr Tymoshenko #define		IRQ_STAT0_RXSHIFT	16
795b03aba6SOleksandr Tymoshenko #define		IRQ_STAT0_TXSHIFT	0
805b03aba6SOleksandr Tymoshenko #define USBCTRL_IRQ_STAT1	0x34
815b03aba6SOleksandr Tymoshenko #define 	IRQ_STAT1_DRVVBUS	(1 << 8)
825b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_SET0	0x38
835b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_SET1	0x3C
845b03aba6SOleksandr Tymoshenko #define 	USBCTRL_INTEN_USB_ALL	0x1ff
855b03aba6SOleksandr Tymoshenko #define 	USBCTRL_INTEN_USB_SOF	(1 << 3)
865b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_CLR0	0x40
875b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_CLR1	0x44
885b03aba6SOleksandr Tymoshenko #define USBCTRL_UTMI		0xE0
895b03aba6SOleksandr Tymoshenko #define		USBCTRL_UTMI_FSDATAEXT		(1 << 1)
905b03aba6SOleksandr Tymoshenko #define USBCTRL_MODE		0xE8
915b03aba6SOleksandr Tymoshenko #define 	USBCTRL_MODE_IDDIG		(1 << 8)
925b03aba6SOleksandr Tymoshenko #define 	USBCTRL_MODE_IDDIGMUX		(1 << 7)
935b03aba6SOleksandr Tymoshenko 
945b03aba6SOleksandr Tymoshenko /* USBSS resource + 2 MUSB ports */
955b03aba6SOleksandr Tymoshenko 
965b03aba6SOleksandr Tymoshenko #define RES_USBCORE	0
975b03aba6SOleksandr Tymoshenko #define RES_USBCTRL	1
985b03aba6SOleksandr Tymoshenko 
995b03aba6SOleksandr Tymoshenko #define	USB_WRITE4(sc, idx, reg, val)	do {		\
1005b03aba6SOleksandr Tymoshenko 	bus_write_4((sc)->sc_mem_res[idx], (reg), (val));	\
1015b03aba6SOleksandr Tymoshenko } while (0)
1025b03aba6SOleksandr Tymoshenko 
1035b03aba6SOleksandr Tymoshenko #define	USB_READ4(sc, idx, reg) bus_read_4((sc)->sc_mem_res[idx], (reg))
1045b03aba6SOleksandr Tymoshenko 
1055b03aba6SOleksandr Tymoshenko #define	USBCTRL_WRITE4(sc, reg, val)	\
1065b03aba6SOleksandr Tymoshenko     USB_WRITE4((sc), RES_USBCTRL, (reg), (val))
1075b03aba6SOleksandr Tymoshenko #define	USBCTRL_READ4(sc, reg)		\
1085b03aba6SOleksandr Tymoshenko     USB_READ4((sc), RES_USBCTRL, (reg))
1095b03aba6SOleksandr Tymoshenko 
1105b03aba6SOleksandr Tymoshenko static struct resource_spec am335x_musbotg_mem_spec[] = {
1115b03aba6SOleksandr Tymoshenko 	{ SYS_RES_MEMORY,   0,  RF_ACTIVE },
1125b03aba6SOleksandr Tymoshenko 	{ SYS_RES_MEMORY,   1,  RF_ACTIVE },
1135b03aba6SOleksandr Tymoshenko 	{ -1,               0,  0 }
1145b03aba6SOleksandr Tymoshenko };
1155b03aba6SOleksandr Tymoshenko 
1165b03aba6SOleksandr Tymoshenko #ifdef USB_DEBUG
1175b03aba6SOleksandr Tymoshenko static int usbssdebug = 0;
1185b03aba6SOleksandr Tymoshenko 
1195b03aba6SOleksandr Tymoshenko static SYSCTL_NODE(_hw_usb, OID_AUTO, am335x_usbss, CTLFLAG_RW, 0, "AM335x USBSS");
1205b03aba6SOleksandr Tymoshenko SYSCTL_INT(_hw_usb_am335x_usbss, OID_AUTO, debug, CTLFLAG_RW,
1215b03aba6SOleksandr Tymoshenko     &usbssdebug, 0, "Debug level");
1225b03aba6SOleksandr Tymoshenko #endif
1235b03aba6SOleksandr Tymoshenko 
1245b03aba6SOleksandr Tymoshenko static device_probe_t musbotg_probe;
1255b03aba6SOleksandr Tymoshenko static device_attach_t musbotg_attach;
1265b03aba6SOleksandr Tymoshenko static device_detach_t musbotg_detach;
1275b03aba6SOleksandr Tymoshenko 
1285b03aba6SOleksandr Tymoshenko struct musbotg_super_softc {
1295b03aba6SOleksandr Tymoshenko 	struct musbotg_softc	sc_otg;
1305b03aba6SOleksandr Tymoshenko 	struct resource		*sc_mem_res[2];
1315b03aba6SOleksandr Tymoshenko 	int			sc_irq_rid;
1325b03aba6SOleksandr Tymoshenko };
1335b03aba6SOleksandr Tymoshenko 
1345b03aba6SOleksandr Tymoshenko static void
1355b03aba6SOleksandr Tymoshenko musbotg_vbus_poll(struct musbotg_super_softc *sc)
1365b03aba6SOleksandr Tymoshenko {
1375b03aba6SOleksandr Tymoshenko 	uint32_t stat;
1385b03aba6SOleksandr Tymoshenko 
1395b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_mode == MUSB2_DEVICE_MODE)
1405b03aba6SOleksandr Tymoshenko 		musbotg_vbus_interrupt(&sc->sc_otg, 1);
1415b03aba6SOleksandr Tymoshenko 	else {
1425b03aba6SOleksandr Tymoshenko 		stat = USBCTRL_READ4(sc, USBCTRL_STAT);
1435b03aba6SOleksandr Tymoshenko 		musbotg_vbus_interrupt(&sc->sc_otg, stat & 1);
1445b03aba6SOleksandr Tymoshenko 	}
1455b03aba6SOleksandr Tymoshenko }
1465b03aba6SOleksandr Tymoshenko 
1475b03aba6SOleksandr Tymoshenko /*
1485b03aba6SOleksandr Tymoshenko  * Arg to musbotg_clocks_on and musbot_clocks_off is
1495b03aba6SOleksandr Tymoshenko  * a uint32_t * pointing to the SCM register offset.
1505b03aba6SOleksandr Tymoshenko  */
1515b03aba6SOleksandr Tymoshenko static uint32_t USB_CTRL[] = {SCM_USB_CTRL0, SCM_USB_CTRL1};
1525b03aba6SOleksandr Tymoshenko 
1535b03aba6SOleksandr Tymoshenko static void
1545b03aba6SOleksandr Tymoshenko musbotg_clocks_on(void *arg)
1555b03aba6SOleksandr Tymoshenko {
1565b03aba6SOleksandr Tymoshenko 	struct musbotg_softc *sc;
1575b03aba6SOleksandr Tymoshenko 	uint32_t c, reg;
1585b03aba6SOleksandr Tymoshenko 
1595b03aba6SOleksandr Tymoshenko 	sc = arg;
1605b03aba6SOleksandr Tymoshenko         reg = USB_CTRL[sc->sc_id];
1615b03aba6SOleksandr Tymoshenko 
1625b03aba6SOleksandr Tymoshenko 	ti_scm_reg_read_4(reg, &c);
1635b03aba6SOleksandr Tymoshenko 	c &= ~3; /* Enable power */
1645b03aba6SOleksandr Tymoshenko 	c |= 1 << 19; /* VBUS detect enable */
1655b03aba6SOleksandr Tymoshenko 	c |= 1 << 20; /* Session end enable */
1665b03aba6SOleksandr Tymoshenko 	ti_scm_reg_write_4(reg, c);
1675b03aba6SOleksandr Tymoshenko }
1685b03aba6SOleksandr Tymoshenko 
1695b03aba6SOleksandr Tymoshenko static void
1705b03aba6SOleksandr Tymoshenko musbotg_clocks_off(void *arg)
1715b03aba6SOleksandr Tymoshenko {
1725b03aba6SOleksandr Tymoshenko 	struct musbotg_softc *sc;
1735b03aba6SOleksandr Tymoshenko 	uint32_t c, reg;
1745b03aba6SOleksandr Tymoshenko 
1755b03aba6SOleksandr Tymoshenko 	sc = arg;
1765b03aba6SOleksandr Tymoshenko         reg = USB_CTRL[sc->sc_id];
1775b03aba6SOleksandr Tymoshenko 
1785b03aba6SOleksandr Tymoshenko 	/* Disable power to PHY */
1795b03aba6SOleksandr Tymoshenko 	ti_scm_reg_read_4(reg, &c);
1805b03aba6SOleksandr Tymoshenko 	ti_scm_reg_write_4(reg, c | 3);
1815b03aba6SOleksandr Tymoshenko }
1825b03aba6SOleksandr Tymoshenko 
1835b03aba6SOleksandr Tymoshenko static void
1845b03aba6SOleksandr Tymoshenko musbotg_ep_int_set(struct musbotg_softc *sc, int ep, int on)
1855b03aba6SOleksandr Tymoshenko {
1865b03aba6SOleksandr Tymoshenko 	struct musbotg_super_softc *ssc = sc->sc_platform_data;
1875b03aba6SOleksandr Tymoshenko 	uint32_t epmask;
1885b03aba6SOleksandr Tymoshenko 
1895b03aba6SOleksandr Tymoshenko 	epmask = ((1 << ep) << IRQ_STAT0_RXSHIFT);
1905b03aba6SOleksandr Tymoshenko 	epmask |= ((1 << ep) << IRQ_STAT0_TXSHIFT);
1915b03aba6SOleksandr Tymoshenko 	if (on)
1925b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(ssc, USBCTRL_INTEN_SET0, epmask);
1935b03aba6SOleksandr Tymoshenko 	else
1945b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(ssc, USBCTRL_INTEN_CLR0, epmask);
1955b03aba6SOleksandr Tymoshenko }
1965b03aba6SOleksandr Tymoshenko 
1975b03aba6SOleksandr Tymoshenko static void
1985b03aba6SOleksandr Tymoshenko musbotg_wrapper_interrupt(void *arg)
1995b03aba6SOleksandr Tymoshenko {
2005b03aba6SOleksandr Tymoshenko 	struct musbotg_softc *sc = arg;
2015b03aba6SOleksandr Tymoshenko 	struct musbotg_super_softc *ssc = sc->sc_platform_data;
2025b03aba6SOleksandr Tymoshenko 	uint32_t stat, stat0, stat1;
2035b03aba6SOleksandr Tymoshenko 
2045b03aba6SOleksandr Tymoshenko 	stat = USBCTRL_READ4(ssc, USBCTRL_STAT);
2055b03aba6SOleksandr Tymoshenko 	stat0 = USBCTRL_READ4(ssc, USBCTRL_IRQ_STAT0);
2065b03aba6SOleksandr Tymoshenko 	stat1 = USBCTRL_READ4(ssc, USBCTRL_IRQ_STAT1);
2075b03aba6SOleksandr Tymoshenko 	if (stat0)
2085b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(ssc, USBCTRL_IRQ_STAT0, stat0);
2095b03aba6SOleksandr Tymoshenko 	if (stat1)
2105b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(ssc, USBCTRL_IRQ_STAT1, stat1);
2115b03aba6SOleksandr Tymoshenko 
2125b03aba6SOleksandr Tymoshenko 	DPRINTFN(4, "port%d: stat0=%08x stat1=%08x, stat=%08x\n",
2135b03aba6SOleksandr Tymoshenko 	    sc->sc_id, stat0, stat1, stat);
2145b03aba6SOleksandr Tymoshenko 
2155b03aba6SOleksandr Tymoshenko 	if (stat1 & IRQ_STAT1_DRVVBUS)
2165b03aba6SOleksandr Tymoshenko 		musbotg_vbus_interrupt(sc, stat & 1);
2175b03aba6SOleksandr Tymoshenko 
2185b03aba6SOleksandr Tymoshenko 	musbotg_interrupt(arg, ((stat0 >> 16) & 0xffff),
2195b03aba6SOleksandr Tymoshenko 	    stat0 & 0xffff, stat1 & 0xff);
2205b03aba6SOleksandr Tymoshenko }
2215b03aba6SOleksandr Tymoshenko 
2225b03aba6SOleksandr Tymoshenko static int
2235b03aba6SOleksandr Tymoshenko musbotg_probe(device_t dev)
2245b03aba6SOleksandr Tymoshenko {
2255b03aba6SOleksandr Tymoshenko 	if (!ofw_bus_status_okay(dev))
2265b03aba6SOleksandr Tymoshenko 		return (ENXIO);
2275b03aba6SOleksandr Tymoshenko 
2285b03aba6SOleksandr Tymoshenko 	if (!ofw_bus_is_compatible(dev, "ti,musb-am33xx"))
2295b03aba6SOleksandr Tymoshenko 		return (ENXIO);
2305b03aba6SOleksandr Tymoshenko 
2315b03aba6SOleksandr Tymoshenko 	device_set_desc(dev, "TI AM33xx integrated USB OTG controller");
2325b03aba6SOleksandr Tymoshenko 
2335b03aba6SOleksandr Tymoshenko 	return (BUS_PROBE_DEFAULT);
2345b03aba6SOleksandr Tymoshenko }
2355b03aba6SOleksandr Tymoshenko 
2365b03aba6SOleksandr Tymoshenko static int
2375b03aba6SOleksandr Tymoshenko musbotg_attach(device_t dev)
2385b03aba6SOleksandr Tymoshenko {
2395b03aba6SOleksandr Tymoshenko 	struct musbotg_super_softc *sc = device_get_softc(dev);
24033fe8378SLuiz Otavio O Souza 	char mode[16];
2415b03aba6SOleksandr Tymoshenko 	int err;
2425b03aba6SOleksandr Tymoshenko 	uint32_t reg;
2435b03aba6SOleksandr Tymoshenko 
2445b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_id = device_get_unit(dev);
2455b03aba6SOleksandr Tymoshenko 
2465b03aba6SOleksandr Tymoshenko 	/* Request the memory resources */
2475b03aba6SOleksandr Tymoshenko 	err = bus_alloc_resources(dev, am335x_musbotg_mem_spec,
2485b03aba6SOleksandr Tymoshenko 		sc->sc_mem_res);
2495b03aba6SOleksandr Tymoshenko 	if (err) {
2505b03aba6SOleksandr Tymoshenko 		device_printf(dev,
2515b03aba6SOleksandr Tymoshenko 		    "Error: could not allocate mem resources\n");
2525b03aba6SOleksandr Tymoshenko 		return (ENXIO);
2535b03aba6SOleksandr Tymoshenko 	}
2545b03aba6SOleksandr Tymoshenko 
2555b03aba6SOleksandr Tymoshenko 	/* Request the IRQ resources */
2565b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
2575b03aba6SOleksandr Tymoshenko 	    &sc->sc_irq_rid, RF_ACTIVE);
2585b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_irq_res == NULL) {
2595b03aba6SOleksandr Tymoshenko 		device_printf(dev,
2605b03aba6SOleksandr Tymoshenko 		    "Error: could not allocate irq resources\n");
2615b03aba6SOleksandr Tymoshenko 		return (ENXIO);
2625b03aba6SOleksandr Tymoshenko 	}
2635b03aba6SOleksandr Tymoshenko 
2645b03aba6SOleksandr Tymoshenko 	/* setup MUSB OTG USB controller interface softc */
2655b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_clocks_on = &musbotg_clocks_on;
2665b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_clocks_off = &musbotg_clocks_off;
2675b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_clocks_arg = &sc->sc_otg;
2685b03aba6SOleksandr Tymoshenko 
2695b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_ep_int_set = musbotg_ep_int_set;
2705b03aba6SOleksandr Tymoshenko 
2715b03aba6SOleksandr Tymoshenko 	/* initialise some bus fields */
2725b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_bus.parent = dev;
2735b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_bus.devices = sc->sc_otg.sc_devices;
2745b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_bus.devices_max = MUSB2_MAX_DEVICES;
2755b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_bus.dma_bits = 32;
2765b03aba6SOleksandr Tymoshenko 
2775b03aba6SOleksandr Tymoshenko 	/* get all DMA memory */
2785b03aba6SOleksandr Tymoshenko 	if (usb_bus_mem_alloc_all(&sc->sc_otg.sc_bus,
2795b03aba6SOleksandr Tymoshenko 	    USB_GET_DMA_TAG(dev), NULL)) {
2805b03aba6SOleksandr Tymoshenko 		device_printf(dev,
2815b03aba6SOleksandr Tymoshenko 		    "Failed allocate bus mem for musb\n");
2825b03aba6SOleksandr Tymoshenko 		return (ENOMEM);
2835b03aba6SOleksandr Tymoshenko 	}
2845b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_io_res = sc->sc_mem_res[RES_USBCORE];
2855b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_io_tag =
2865b03aba6SOleksandr Tymoshenko 	    rman_get_bustag(sc->sc_otg.sc_io_res);
2875b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_io_hdl =
2885b03aba6SOleksandr Tymoshenko 	    rman_get_bushandle(sc->sc_otg.sc_io_res);
2895b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_io_size =
2905b03aba6SOleksandr Tymoshenko 	    rman_get_size(sc->sc_otg.sc_io_res);
2915b03aba6SOleksandr Tymoshenko 
2925b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_bus.bdev = device_add_child(dev, "usbus", -1);
2935b03aba6SOleksandr Tymoshenko 	if (!(sc->sc_otg.sc_bus.bdev)) {
2945b03aba6SOleksandr Tymoshenko 		device_printf(dev, "No busdev for musb\n");
2955b03aba6SOleksandr Tymoshenko 		goto error;
2965b03aba6SOleksandr Tymoshenko 	}
2975b03aba6SOleksandr Tymoshenko 	device_set_ivars(sc->sc_otg.sc_bus.bdev,
2985b03aba6SOleksandr Tymoshenko 	    &sc->sc_otg.sc_bus);
2995b03aba6SOleksandr Tymoshenko 
3005b03aba6SOleksandr Tymoshenko 	err = bus_setup_intr(dev, sc->sc_otg.sc_irq_res,
3015b03aba6SOleksandr Tymoshenko 	    INTR_TYPE_BIO | INTR_MPSAFE,
3025b03aba6SOleksandr Tymoshenko 	    NULL, (driver_intr_t *)musbotg_wrapper_interrupt,
3035b03aba6SOleksandr Tymoshenko 	    &sc->sc_otg, &sc->sc_otg.sc_intr_hdl);
3045b03aba6SOleksandr Tymoshenko 	if (err) {
3055b03aba6SOleksandr Tymoshenko 		sc->sc_otg.sc_intr_hdl = NULL;
3065b03aba6SOleksandr Tymoshenko 		device_printf(dev,
3075b03aba6SOleksandr Tymoshenko 		    "Failed to setup interrupt for musb\n");
3085b03aba6SOleksandr Tymoshenko 		goto error;
3095b03aba6SOleksandr Tymoshenko 	}
3105b03aba6SOleksandr Tymoshenko 
3115b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_platform_data = sc;
31233fe8378SLuiz Otavio O Souza 	if (OF_getprop(ofw_bus_get_node(dev), "dr_mode", mode,
31333fe8378SLuiz Otavio O Souza 	    sizeof(mode)) > 0) {
31433fe8378SLuiz Otavio O Souza 		if (strcasecmp(mode, "host") == 0)
31533fe8378SLuiz Otavio O Souza 			sc->sc_otg.sc_mode = MUSB2_HOST_MODE;
31633fe8378SLuiz Otavio O Souza 		else
31733fe8378SLuiz Otavio O Souza 			sc->sc_otg.sc_mode = MUSB2_DEVICE_MODE;
31833fe8378SLuiz Otavio O Souza 	} else {
31933fe8378SLuiz Otavio O Souza 		/* Beaglebone defaults: USB0 device, USB1 HOST. */
3205b03aba6SOleksandr Tymoshenko 		if (sc->sc_otg.sc_id == 0)
3215b03aba6SOleksandr Tymoshenko 			sc->sc_otg.sc_mode = MUSB2_DEVICE_MODE;
3225b03aba6SOleksandr Tymoshenko 		else
3235b03aba6SOleksandr Tymoshenko 			sc->sc_otg.sc_mode = MUSB2_HOST_MODE;
32433fe8378SLuiz Otavio O Souza 	}
3255b03aba6SOleksandr Tymoshenko 
3265b03aba6SOleksandr Tymoshenko 	/*
3275b03aba6SOleksandr Tymoshenko 	 * software-controlled function
3285b03aba6SOleksandr Tymoshenko 	 */
3295b03aba6SOleksandr Tymoshenko 
3305b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_mode == MUSB2_HOST_MODE) {
3315b03aba6SOleksandr Tymoshenko 		reg = USBCTRL_READ4(sc, USBCTRL_MODE);
3325b03aba6SOleksandr Tymoshenko 		reg |= USBCTRL_MODE_IDDIGMUX;
3335b03aba6SOleksandr Tymoshenko 		reg &= ~USBCTRL_MODE_IDDIG;
3345b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(sc, USBCTRL_MODE, reg);
3355b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(sc, USBCTRL_UTMI,
3365b03aba6SOleksandr Tymoshenko 		    USBCTRL_UTMI_FSDATAEXT);
3375b03aba6SOleksandr Tymoshenko 	} else {
3385b03aba6SOleksandr Tymoshenko 		reg = USBCTRL_READ4(sc, USBCTRL_MODE);
3395b03aba6SOleksandr Tymoshenko 		reg |= USBCTRL_MODE_IDDIGMUX;
3405b03aba6SOleksandr Tymoshenko 		reg |= USBCTRL_MODE_IDDIG;
3415b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(sc, USBCTRL_MODE, reg);
3425b03aba6SOleksandr Tymoshenko 	}
3435b03aba6SOleksandr Tymoshenko 
3445b03aba6SOleksandr Tymoshenko 	reg = USBCTRL_INTEN_USB_ALL & ~USBCTRL_INTEN_USB_SOF;
3455b03aba6SOleksandr Tymoshenko 	USBCTRL_WRITE4(sc, USBCTRL_INTEN_SET1, reg);
3465b03aba6SOleksandr Tymoshenko 	USBCTRL_WRITE4(sc, USBCTRL_INTEN_CLR0, 0xffffffff);
3475b03aba6SOleksandr Tymoshenko 
3485b03aba6SOleksandr Tymoshenko 	err = musbotg_init(&sc->sc_otg);
3495b03aba6SOleksandr Tymoshenko 	if (!err)
3505b03aba6SOleksandr Tymoshenko 		err = device_probe_and_attach(sc->sc_otg.sc_bus.bdev);
3515b03aba6SOleksandr Tymoshenko 
3525b03aba6SOleksandr Tymoshenko 	if (err)
3535b03aba6SOleksandr Tymoshenko 		goto error;
3545b03aba6SOleksandr Tymoshenko 
3555b03aba6SOleksandr Tymoshenko 	/* poll VBUS one time */
3565b03aba6SOleksandr Tymoshenko 	musbotg_vbus_poll(sc);
3575b03aba6SOleksandr Tymoshenko 
3585b03aba6SOleksandr Tymoshenko 	return (0);
3595b03aba6SOleksandr Tymoshenko 
3605b03aba6SOleksandr Tymoshenko error:
3615b03aba6SOleksandr Tymoshenko 	musbotg_detach(dev);
3625b03aba6SOleksandr Tymoshenko 	return (ENXIO);
3635b03aba6SOleksandr Tymoshenko }
3645b03aba6SOleksandr Tymoshenko 
3655b03aba6SOleksandr Tymoshenko static int
3665b03aba6SOleksandr Tymoshenko musbotg_detach(device_t dev)
3675b03aba6SOleksandr Tymoshenko {
3685b03aba6SOleksandr Tymoshenko 	struct musbotg_super_softc *sc = device_get_softc(dev);
3695b03aba6SOleksandr Tymoshenko 	int err;
3705b03aba6SOleksandr Tymoshenko 
371*d3bf5efcSHans Petter Selasky 	/* during module unload there are lots of children leftover */
372*d3bf5efcSHans Petter Selasky 	device_delete_children(dev);
3735b03aba6SOleksandr Tymoshenko 
3745b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_irq_res && sc->sc_otg.sc_intr_hdl) {
3755b03aba6SOleksandr Tymoshenko 		/*
3765b03aba6SOleksandr Tymoshenko 		 * only call musbotg_uninit() after musbotg_init()
3775b03aba6SOleksandr Tymoshenko 		 */
3785b03aba6SOleksandr Tymoshenko 		musbotg_uninit(&sc->sc_otg);
3795b03aba6SOleksandr Tymoshenko 
3805b03aba6SOleksandr Tymoshenko 		err = bus_teardown_intr(dev, sc->sc_otg.sc_irq_res,
3815b03aba6SOleksandr Tymoshenko 		    sc->sc_otg.sc_intr_hdl);
3825b03aba6SOleksandr Tymoshenko 		sc->sc_otg.sc_intr_hdl = NULL;
3835b03aba6SOleksandr Tymoshenko 	}
3845b03aba6SOleksandr Tymoshenko 
3855b03aba6SOleksandr Tymoshenko 	usb_bus_mem_free_all(&sc->sc_otg.sc_bus, NULL);
3865b03aba6SOleksandr Tymoshenko 
3875b03aba6SOleksandr Tymoshenko 	/* Free resources if any */
3885b03aba6SOleksandr Tymoshenko 	if (sc->sc_mem_res[0])
3895b03aba6SOleksandr Tymoshenko 		bus_release_resources(dev, am335x_musbotg_mem_spec,
3905b03aba6SOleksandr Tymoshenko 		    sc->sc_mem_res);
3915b03aba6SOleksandr Tymoshenko 
3925b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_irq_res)
3935b03aba6SOleksandr Tymoshenko 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid,
3945b03aba6SOleksandr Tymoshenko 		    sc->sc_otg.sc_irq_res);
3955b03aba6SOleksandr Tymoshenko 
3965b03aba6SOleksandr Tymoshenko 	return (0);
3975b03aba6SOleksandr Tymoshenko }
3985b03aba6SOleksandr Tymoshenko 
3995b03aba6SOleksandr Tymoshenko static device_method_t musbotg_methods[] = {
4005b03aba6SOleksandr Tymoshenko 	/* Device interface */
4015b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_probe, musbotg_probe),
4025b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_attach, musbotg_attach),
4035b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_detach, musbotg_detach),
4045b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_suspend, bus_generic_suspend),
4055b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_resume, bus_generic_resume),
4065b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
4075b03aba6SOleksandr Tymoshenko 
4085b03aba6SOleksandr Tymoshenko 	DEVMETHOD_END
4095b03aba6SOleksandr Tymoshenko };
4105b03aba6SOleksandr Tymoshenko 
4115b03aba6SOleksandr Tymoshenko static driver_t musbotg_driver = {
4125b03aba6SOleksandr Tymoshenko 	.name = "musbotg",
4135b03aba6SOleksandr Tymoshenko 	.methods = musbotg_methods,
4145b03aba6SOleksandr Tymoshenko 	.size = sizeof(struct musbotg_super_softc),
4155b03aba6SOleksandr Tymoshenko };
4165b03aba6SOleksandr Tymoshenko 
4175b03aba6SOleksandr Tymoshenko static devclass_t musbotg_devclass;
4185b03aba6SOleksandr Tymoshenko 
4195b03aba6SOleksandr Tymoshenko DRIVER_MODULE(musbotg, usbss, musbotg_driver, musbotg_devclass, 0, 0);
4205b03aba6SOleksandr Tymoshenko MODULE_DEPEND(musbotg, usbss, 1, 1, 1);
421