xref: /freebsd/sys/arm/ti/am335x/am335x_musb.c (revision 0050ea241584f931c6089f7b7a7aca3804131397)
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/ofw/openfirm.h>
495b03aba6SOleksandr Tymoshenko #include <dev/ofw/ofw_bus.h>
505b03aba6SOleksandr Tymoshenko #include <dev/ofw/ofw_bus_subr.h>
515b03aba6SOleksandr Tymoshenko 
525b03aba6SOleksandr Tymoshenko #include <dev/usb/usb.h>
535b03aba6SOleksandr Tymoshenko #include <dev/usb/usbdi.h>
545b03aba6SOleksandr Tymoshenko 
555b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_core.h>
565b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_busdma.h>
575b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_process.h>
585b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_util.h>
595b03aba6SOleksandr Tymoshenko 
605b03aba6SOleksandr Tymoshenko #define	USB_DEBUG_VAR usbssdebug
615b03aba6SOleksandr Tymoshenko 
625b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_controller.h>
635b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_bus.h>
645b03aba6SOleksandr Tymoshenko #include <dev/usb/controller/musb_otg.h>
655b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_debug.h>
665b03aba6SOleksandr Tymoshenko 
675b03aba6SOleksandr Tymoshenko #include <sys/rman.h>
685b03aba6SOleksandr Tymoshenko 
695b03aba6SOleksandr Tymoshenko #include <arm/ti/am335x/am335x_scm.h>
70*0050ea24SMichal Meloun #include <arm/ti/ti_sysc.h>
71*0050ea24SMichal Meloun #include <dev/extres/clk/clk.h>
72*0050ea24SMichal Meloun #include <dev/extres/syscon/syscon.h>
73*0050ea24SMichal Meloun #include "syscon_if.h"
745b03aba6SOleksandr Tymoshenko 
755b03aba6SOleksandr Tymoshenko #define USBCTRL_REV		0x00
765b03aba6SOleksandr Tymoshenko #define USBCTRL_CTRL		0x14
775b03aba6SOleksandr Tymoshenko #define USBCTRL_STAT		0x18
785b03aba6SOleksandr Tymoshenko #define USBCTRL_IRQ_STAT0	0x30
795b03aba6SOleksandr Tymoshenko #define		IRQ_STAT0_RXSHIFT	16
805b03aba6SOleksandr Tymoshenko #define		IRQ_STAT0_TXSHIFT	0
815b03aba6SOleksandr Tymoshenko #define USBCTRL_IRQ_STAT1	0x34
825b03aba6SOleksandr Tymoshenko #define 	IRQ_STAT1_DRVVBUS	(1 << 8)
835b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_SET0	0x38
845b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_SET1	0x3C
855b03aba6SOleksandr Tymoshenko #define 	USBCTRL_INTEN_USB_ALL	0x1ff
865b03aba6SOleksandr Tymoshenko #define 	USBCTRL_INTEN_USB_SOF	(1 << 3)
875b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_CLR0	0x40
885b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_CLR1	0x44
895b03aba6SOleksandr Tymoshenko #define USBCTRL_UTMI		0xE0
905b03aba6SOleksandr Tymoshenko #define		USBCTRL_UTMI_FSDATAEXT		(1 << 1)
915b03aba6SOleksandr Tymoshenko #define USBCTRL_MODE		0xE8
925b03aba6SOleksandr Tymoshenko #define 	USBCTRL_MODE_IDDIG		(1 << 8)
935b03aba6SOleksandr Tymoshenko #define 	USBCTRL_MODE_IDDIGMUX		(1 << 7)
945b03aba6SOleksandr Tymoshenko 
955b03aba6SOleksandr Tymoshenko /* USBSS resource + 2 MUSB ports */
965b03aba6SOleksandr Tymoshenko 
975b03aba6SOleksandr Tymoshenko #define RES_USBCORE	0
985b03aba6SOleksandr Tymoshenko #define RES_USBCTRL	1
995b03aba6SOleksandr Tymoshenko 
1005b03aba6SOleksandr Tymoshenko #define	USB_WRITE4(sc, idx, reg, val)	do {		\
1015b03aba6SOleksandr Tymoshenko 	bus_write_4((sc)->sc_mem_res[idx], (reg), (val));	\
1025b03aba6SOleksandr Tymoshenko } while (0)
1035b03aba6SOleksandr Tymoshenko 
1045b03aba6SOleksandr Tymoshenko #define	USB_READ4(sc, idx, reg) bus_read_4((sc)->sc_mem_res[idx], (reg))
1055b03aba6SOleksandr Tymoshenko 
1065b03aba6SOleksandr Tymoshenko #define	USBCTRL_WRITE4(sc, reg, val)	\
1075b03aba6SOleksandr Tymoshenko     USB_WRITE4((sc), RES_USBCTRL, (reg), (val))
1085b03aba6SOleksandr Tymoshenko #define	USBCTRL_READ4(sc, reg)		\
1095b03aba6SOleksandr Tymoshenko     USB_READ4((sc), RES_USBCTRL, (reg))
1105b03aba6SOleksandr Tymoshenko 
1115b03aba6SOleksandr Tymoshenko static struct resource_spec am335x_musbotg_mem_spec[] = {
1125b03aba6SOleksandr Tymoshenko 	{ SYS_RES_MEMORY,   0,  RF_ACTIVE },
1135b03aba6SOleksandr Tymoshenko 	{ SYS_RES_MEMORY,   1,  RF_ACTIVE },
1145b03aba6SOleksandr Tymoshenko 	{ -1,               0,  0 }
1155b03aba6SOleksandr Tymoshenko };
1165b03aba6SOleksandr Tymoshenko 
1175b03aba6SOleksandr Tymoshenko #ifdef USB_DEBUG
1185b03aba6SOleksandr Tymoshenko static int usbssdebug = 0;
1195b03aba6SOleksandr Tymoshenko 
1207029da5cSPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, am335x_usbss,
1217029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1227029da5cSPawel Biernacki     "AM335x USBSS");
1235b03aba6SOleksandr Tymoshenko SYSCTL_INT(_hw_usb_am335x_usbss, OID_AUTO, debug, CTLFLAG_RW,
1245b03aba6SOleksandr Tymoshenko     &usbssdebug, 0, "Debug level");
1255b03aba6SOleksandr Tymoshenko #endif
1265b03aba6SOleksandr Tymoshenko 
1275b03aba6SOleksandr Tymoshenko static device_probe_t musbotg_probe;
1285b03aba6SOleksandr Tymoshenko static device_attach_t musbotg_attach;
1295b03aba6SOleksandr Tymoshenko static device_detach_t musbotg_detach;
1305b03aba6SOleksandr Tymoshenko 
1315b03aba6SOleksandr Tymoshenko struct musbotg_super_softc {
1325b03aba6SOleksandr Tymoshenko 	struct musbotg_softc	sc_otg;
1335b03aba6SOleksandr Tymoshenko 	struct resource		*sc_mem_res[2];
1345b03aba6SOleksandr Tymoshenko 	int			sc_irq_rid;
135*0050ea24SMichal Meloun 	struct syscon		*syscon;
1365b03aba6SOleksandr Tymoshenko };
1375b03aba6SOleksandr Tymoshenko 
1385b03aba6SOleksandr Tymoshenko static void
1395b03aba6SOleksandr Tymoshenko musbotg_vbus_poll(struct musbotg_super_softc *sc)
1405b03aba6SOleksandr Tymoshenko {
1415b03aba6SOleksandr Tymoshenko 	uint32_t stat;
1425b03aba6SOleksandr Tymoshenko 
1435b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_mode == MUSB2_DEVICE_MODE)
1445b03aba6SOleksandr Tymoshenko 		musbotg_vbus_interrupt(&sc->sc_otg, 1);
1455b03aba6SOleksandr Tymoshenko 	else {
1465b03aba6SOleksandr Tymoshenko 		stat = USBCTRL_READ4(sc, USBCTRL_STAT);
1475b03aba6SOleksandr Tymoshenko 		musbotg_vbus_interrupt(&sc->sc_otg, stat & 1);
1485b03aba6SOleksandr Tymoshenko 	}
1495b03aba6SOleksandr Tymoshenko }
1505b03aba6SOleksandr Tymoshenko 
1515b03aba6SOleksandr Tymoshenko /*
1525b03aba6SOleksandr Tymoshenko  * Arg to musbotg_clocks_on and musbot_clocks_off is
1535b03aba6SOleksandr Tymoshenko  * a uint32_t * pointing to the SCM register offset.
1545b03aba6SOleksandr Tymoshenko  */
1555b03aba6SOleksandr Tymoshenko static uint32_t USB_CTRL[] = {SCM_USB_CTRL0, SCM_USB_CTRL1};
1565b03aba6SOleksandr Tymoshenko 
1575b03aba6SOleksandr Tymoshenko static void
1585b03aba6SOleksandr Tymoshenko musbotg_clocks_on(void *arg)
1595b03aba6SOleksandr Tymoshenko {
1605b03aba6SOleksandr Tymoshenko 	struct musbotg_softc *sc;
161*0050ea24SMichal Meloun 	struct musbotg_super_softc *ssc;
162*0050ea24SMichal Meloun 	uint32_t reg;
1635b03aba6SOleksandr Tymoshenko 
1645b03aba6SOleksandr Tymoshenko 	sc = arg;
165*0050ea24SMichal Meloun 	ssc = sc->sc_platform_data;
1665b03aba6SOleksandr Tymoshenko 
167*0050ea24SMichal Meloun 	reg = SYSCON_READ_4(ssc->syscon, USB_CTRL[sc->sc_id]);
168*0050ea24SMichal Meloun 	reg &= ~3; /* Enable power */
169*0050ea24SMichal Meloun 	reg |= 1 << 19; /* VBUS detect enable */
170*0050ea24SMichal Meloun 	reg |= 1 << 20; /* Session end enable */
171*0050ea24SMichal Meloun 
172*0050ea24SMichal Meloun 	SYSCON_WRITE_4(ssc->syscon, USB_CTRL[sc->sc_id], reg);
1735b03aba6SOleksandr Tymoshenko }
1745b03aba6SOleksandr Tymoshenko 
1755b03aba6SOleksandr Tymoshenko static void
1765b03aba6SOleksandr Tymoshenko musbotg_clocks_off(void *arg)
1775b03aba6SOleksandr Tymoshenko {
1785b03aba6SOleksandr Tymoshenko 	struct musbotg_softc *sc;
179*0050ea24SMichal Meloun 	struct musbotg_super_softc *ssc;
180*0050ea24SMichal Meloun 	uint32_t reg;
1815b03aba6SOleksandr Tymoshenko 
1825b03aba6SOleksandr Tymoshenko 	sc = arg;
183*0050ea24SMichal Meloun 	ssc = sc->sc_platform_data;
1845b03aba6SOleksandr Tymoshenko 
1855b03aba6SOleksandr Tymoshenko 	/* Disable power to PHY */
186*0050ea24SMichal Meloun 	reg = SYSCON_READ_4(ssc->syscon, USB_CTRL[sc->sc_id]);
187*0050ea24SMichal Meloun 	SYSCON_WRITE_4(ssc->syscon, USB_CTRL[sc->sc_id], reg | 3);
1885b03aba6SOleksandr Tymoshenko }
1895b03aba6SOleksandr Tymoshenko 
1905b03aba6SOleksandr Tymoshenko static void
1915b03aba6SOleksandr Tymoshenko musbotg_ep_int_set(struct musbotg_softc *sc, int ep, int on)
1925b03aba6SOleksandr Tymoshenko {
1935b03aba6SOleksandr Tymoshenko 	struct musbotg_super_softc *ssc = sc->sc_platform_data;
1945b03aba6SOleksandr Tymoshenko 	uint32_t epmask;
1955b03aba6SOleksandr Tymoshenko 
1965b03aba6SOleksandr Tymoshenko 	epmask = ((1 << ep) << IRQ_STAT0_RXSHIFT);
1975b03aba6SOleksandr Tymoshenko 	epmask |= ((1 << ep) << IRQ_STAT0_TXSHIFT);
1985b03aba6SOleksandr Tymoshenko 	if (on)
1995b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(ssc, USBCTRL_INTEN_SET0, epmask);
2005b03aba6SOleksandr Tymoshenko 	else
2015b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(ssc, USBCTRL_INTEN_CLR0, epmask);
2025b03aba6SOleksandr Tymoshenko }
2035b03aba6SOleksandr Tymoshenko 
2045b03aba6SOleksandr Tymoshenko static void
2055b03aba6SOleksandr Tymoshenko musbotg_wrapper_interrupt(void *arg)
2065b03aba6SOleksandr Tymoshenko {
2075b03aba6SOleksandr Tymoshenko 	struct musbotg_softc *sc = arg;
2085b03aba6SOleksandr Tymoshenko 	struct musbotg_super_softc *ssc = sc->sc_platform_data;
2095b03aba6SOleksandr Tymoshenko 	uint32_t stat, stat0, stat1;
2105b03aba6SOleksandr Tymoshenko 
2115b03aba6SOleksandr Tymoshenko 	stat = USBCTRL_READ4(ssc, USBCTRL_STAT);
2125b03aba6SOleksandr Tymoshenko 	stat0 = USBCTRL_READ4(ssc, USBCTRL_IRQ_STAT0);
2135b03aba6SOleksandr Tymoshenko 	stat1 = USBCTRL_READ4(ssc, USBCTRL_IRQ_STAT1);
2145b03aba6SOleksandr Tymoshenko 	if (stat0)
2155b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(ssc, USBCTRL_IRQ_STAT0, stat0);
2165b03aba6SOleksandr Tymoshenko 	if (stat1)
2175b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(ssc, USBCTRL_IRQ_STAT1, stat1);
2185b03aba6SOleksandr Tymoshenko 
2195b03aba6SOleksandr Tymoshenko 	DPRINTFN(4, "port%d: stat0=%08x stat1=%08x, stat=%08x\n",
2205b03aba6SOleksandr Tymoshenko 	    sc->sc_id, stat0, stat1, stat);
2215b03aba6SOleksandr Tymoshenko 
2225b03aba6SOleksandr Tymoshenko 	if (stat1 & IRQ_STAT1_DRVVBUS)
2235b03aba6SOleksandr Tymoshenko 		musbotg_vbus_interrupt(sc, stat & 1);
2245b03aba6SOleksandr Tymoshenko 
2255b03aba6SOleksandr Tymoshenko 	musbotg_interrupt(arg, ((stat0 >> 16) & 0xffff),
2265b03aba6SOleksandr Tymoshenko 	    stat0 & 0xffff, stat1 & 0xff);
2275b03aba6SOleksandr Tymoshenko }
2285b03aba6SOleksandr Tymoshenko 
2295b03aba6SOleksandr Tymoshenko static int
2305b03aba6SOleksandr Tymoshenko musbotg_probe(device_t dev)
2315b03aba6SOleksandr Tymoshenko {
2325b03aba6SOleksandr Tymoshenko 	if (!ofw_bus_status_okay(dev))
2335b03aba6SOleksandr Tymoshenko 		return (ENXIO);
2345b03aba6SOleksandr Tymoshenko 
2355b03aba6SOleksandr Tymoshenko 	if (!ofw_bus_is_compatible(dev, "ti,musb-am33xx"))
2365b03aba6SOleksandr Tymoshenko 		return (ENXIO);
2375b03aba6SOleksandr Tymoshenko 
2385b03aba6SOleksandr Tymoshenko 	device_set_desc(dev, "TI AM33xx integrated USB OTG controller");
2395b03aba6SOleksandr Tymoshenko 
2405b03aba6SOleksandr Tymoshenko 	return (BUS_PROBE_DEFAULT);
2415b03aba6SOleksandr Tymoshenko }
2425b03aba6SOleksandr Tymoshenko 
2435b03aba6SOleksandr Tymoshenko static int
2445b03aba6SOleksandr Tymoshenko musbotg_attach(device_t dev)
2455b03aba6SOleksandr Tymoshenko {
2465b03aba6SOleksandr Tymoshenko 	struct musbotg_super_softc *sc = device_get_softc(dev);
24733fe8378SLuiz Otavio O Souza 	char mode[16];
2485b03aba6SOleksandr Tymoshenko 	int err;
2495b03aba6SOleksandr Tymoshenko 	uint32_t reg;
250*0050ea24SMichal Meloun 	phandle_t opp_table;
251*0050ea24SMichal Meloun 	clk_t clk_usbotg_fck;
2525b03aba6SOleksandr Tymoshenko 
2535b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_id = device_get_unit(dev);
2545b03aba6SOleksandr Tymoshenko 
255*0050ea24SMichal Meloun 	/* FIXME: The devicetree needs to be updated to get a handle to the gate
256*0050ea24SMichal Meloun 	 * usbotg_fck@47c. see TRM 8.1.12.2 CM_WKUP CM_CLKDCOLDO_DPLL_PER.
257*0050ea24SMichal Meloun 	 */
258*0050ea24SMichal Meloun 	err = clk_get_by_name(dev, "usbotg_fck@47c", &clk_usbotg_fck);
259*0050ea24SMichal Meloun 	if (err) {
260*0050ea24SMichal Meloun 		device_printf(dev, "Can not find usbotg_fck@47c\n");
261*0050ea24SMichal Meloun 		return (ENXIO);
262*0050ea24SMichal Meloun 	}
263*0050ea24SMichal Meloun 
264*0050ea24SMichal Meloun 	err = clk_enable(clk_usbotg_fck);
265*0050ea24SMichal Meloun 	if (err) {
266*0050ea24SMichal Meloun 		device_printf(dev, "Can not enable usbotg_fck@47c\n");
267*0050ea24SMichal Meloun 		return (ENXIO);
268*0050ea24SMichal Meloun 	}
269*0050ea24SMichal Meloun 
270*0050ea24SMichal Meloun 	/* FIXME: For now; Go and kidnap syscon from opp-table */
271*0050ea24SMichal Meloun 	opp_table = OF_finddevice("/opp-table");
272*0050ea24SMichal Meloun 	if (opp_table == -1) {
273*0050ea24SMichal Meloun 		device_printf(dev, "Cant find /opp-table\n");
274*0050ea24SMichal Meloun 		return (ENXIO);
275*0050ea24SMichal Meloun 	}
276*0050ea24SMichal Meloun 	if (!OF_hasprop(opp_table, "syscon")) {
277*0050ea24SMichal Meloun 		device_printf(dev, "/opp-table missing syscon property\n");
278*0050ea24SMichal Meloun 		return (ENXIO);
279*0050ea24SMichal Meloun 	}
280*0050ea24SMichal Meloun 	err = syscon_get_by_ofw_property(dev, opp_table, "syscon", &sc->syscon);
281*0050ea24SMichal Meloun 	if (err) {
282*0050ea24SMichal Meloun 		device_printf(dev, "Failed to get syscon\n");
283*0050ea24SMichal Meloun 		return (ENXIO);
284*0050ea24SMichal Meloun 	}
285*0050ea24SMichal Meloun 
2865b03aba6SOleksandr Tymoshenko 	/* Request the memory resources */
2875b03aba6SOleksandr Tymoshenko 	err = bus_alloc_resources(dev, am335x_musbotg_mem_spec,
2885b03aba6SOleksandr Tymoshenko 		sc->sc_mem_res);
2895b03aba6SOleksandr Tymoshenko 	if (err) {
2905b03aba6SOleksandr Tymoshenko 		device_printf(dev,
2915b03aba6SOleksandr Tymoshenko 		    "Error: could not allocate mem resources\n");
2925b03aba6SOleksandr Tymoshenko 		return (ENXIO);
2935b03aba6SOleksandr Tymoshenko 	}
2945b03aba6SOleksandr Tymoshenko 
2955b03aba6SOleksandr Tymoshenko 	/* Request the IRQ resources */
2965b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
2975b03aba6SOleksandr Tymoshenko 	    &sc->sc_irq_rid, RF_ACTIVE);
2985b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_irq_res == NULL) {
2995b03aba6SOleksandr Tymoshenko 		device_printf(dev,
3005b03aba6SOleksandr Tymoshenko 		    "Error: could not allocate irq resources\n");
3015b03aba6SOleksandr Tymoshenko 		return (ENXIO);
3025b03aba6SOleksandr Tymoshenko 	}
3035b03aba6SOleksandr Tymoshenko 
3045b03aba6SOleksandr Tymoshenko 	/* setup MUSB OTG USB controller interface softc */
3055b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_clocks_on = &musbotg_clocks_on;
3065b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_clocks_off = &musbotg_clocks_off;
3075b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_clocks_arg = &sc->sc_otg;
3085b03aba6SOleksandr Tymoshenko 
3095b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_ep_int_set = musbotg_ep_int_set;
3105b03aba6SOleksandr Tymoshenko 
3115b03aba6SOleksandr Tymoshenko 	/* initialise some bus fields */
3125b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_bus.parent = dev;
3135b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_bus.devices = sc->sc_otg.sc_devices;
3145b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_bus.devices_max = MUSB2_MAX_DEVICES;
3155b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_bus.dma_bits = 32;
3165b03aba6SOleksandr Tymoshenko 
3175b03aba6SOleksandr Tymoshenko 	/* get all DMA memory */
3185b03aba6SOleksandr Tymoshenko 	if (usb_bus_mem_alloc_all(&sc->sc_otg.sc_bus,
3195b03aba6SOleksandr Tymoshenko 	    USB_GET_DMA_TAG(dev), NULL)) {
3205b03aba6SOleksandr Tymoshenko 		device_printf(dev,
3215b03aba6SOleksandr Tymoshenko 		    "Failed allocate bus mem for musb\n");
3225b03aba6SOleksandr Tymoshenko 		return (ENOMEM);
3235b03aba6SOleksandr Tymoshenko 	}
3245b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_io_res = sc->sc_mem_res[RES_USBCORE];
3255b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_io_tag =
3265b03aba6SOleksandr Tymoshenko 	    rman_get_bustag(sc->sc_otg.sc_io_res);
3275b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_io_hdl =
3285b03aba6SOleksandr Tymoshenko 	    rman_get_bushandle(sc->sc_otg.sc_io_res);
3295b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_io_size =
3305b03aba6SOleksandr Tymoshenko 	    rman_get_size(sc->sc_otg.sc_io_res);
3315b03aba6SOleksandr Tymoshenko 
3325b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_bus.bdev = device_add_child(dev, "usbus", -1);
3335b03aba6SOleksandr Tymoshenko 	if (!(sc->sc_otg.sc_bus.bdev)) {
3345b03aba6SOleksandr Tymoshenko 		device_printf(dev, "No busdev for musb\n");
3355b03aba6SOleksandr Tymoshenko 		goto error;
3365b03aba6SOleksandr Tymoshenko 	}
3375b03aba6SOleksandr Tymoshenko 	device_set_ivars(sc->sc_otg.sc_bus.bdev,
3385b03aba6SOleksandr Tymoshenko 	    &sc->sc_otg.sc_bus);
3395b03aba6SOleksandr Tymoshenko 
3405b03aba6SOleksandr Tymoshenko 	err = bus_setup_intr(dev, sc->sc_otg.sc_irq_res,
3415b03aba6SOleksandr Tymoshenko 	    INTR_TYPE_BIO | INTR_MPSAFE,
3425b03aba6SOleksandr Tymoshenko 	    NULL, (driver_intr_t *)musbotg_wrapper_interrupt,
3435b03aba6SOleksandr Tymoshenko 	    &sc->sc_otg, &sc->sc_otg.sc_intr_hdl);
3445b03aba6SOleksandr Tymoshenko 	if (err) {
3455b03aba6SOleksandr Tymoshenko 		sc->sc_otg.sc_intr_hdl = NULL;
3465b03aba6SOleksandr Tymoshenko 		device_printf(dev,
3475b03aba6SOleksandr Tymoshenko 		    "Failed to setup interrupt for musb\n");
3485b03aba6SOleksandr Tymoshenko 		goto error;
3495b03aba6SOleksandr Tymoshenko 	}
3505b03aba6SOleksandr Tymoshenko 
3515b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_platform_data = sc;
35233fe8378SLuiz Otavio O Souza 	if (OF_getprop(ofw_bus_get_node(dev), "dr_mode", mode,
35333fe8378SLuiz Otavio O Souza 	    sizeof(mode)) > 0) {
35433fe8378SLuiz Otavio O Souza 		if (strcasecmp(mode, "host") == 0)
35533fe8378SLuiz Otavio O Souza 			sc->sc_otg.sc_mode = MUSB2_HOST_MODE;
35633fe8378SLuiz Otavio O Souza 		else
35733fe8378SLuiz Otavio O Souza 			sc->sc_otg.sc_mode = MUSB2_DEVICE_MODE;
35833fe8378SLuiz Otavio O Souza 	} else {
35933fe8378SLuiz Otavio O Souza 		/* Beaglebone defaults: USB0 device, USB1 HOST. */
3605b03aba6SOleksandr Tymoshenko 		if (sc->sc_otg.sc_id == 0)
3615b03aba6SOleksandr Tymoshenko 			sc->sc_otg.sc_mode = MUSB2_DEVICE_MODE;
3625b03aba6SOleksandr Tymoshenko 		else
3635b03aba6SOleksandr Tymoshenko 			sc->sc_otg.sc_mode = MUSB2_HOST_MODE;
36433fe8378SLuiz Otavio O Souza 	}
3655b03aba6SOleksandr Tymoshenko 
3665b03aba6SOleksandr Tymoshenko 	/*
3675b03aba6SOleksandr Tymoshenko 	 * software-controlled function
3685b03aba6SOleksandr Tymoshenko 	 */
3695b03aba6SOleksandr Tymoshenko 
3705b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_mode == MUSB2_HOST_MODE) {
3715b03aba6SOleksandr Tymoshenko 		reg = USBCTRL_READ4(sc, USBCTRL_MODE);
3725b03aba6SOleksandr Tymoshenko 		reg |= USBCTRL_MODE_IDDIGMUX;
3735b03aba6SOleksandr Tymoshenko 		reg &= ~USBCTRL_MODE_IDDIG;
3745b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(sc, USBCTRL_MODE, reg);
3755b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(sc, USBCTRL_UTMI,
3765b03aba6SOleksandr Tymoshenko 		    USBCTRL_UTMI_FSDATAEXT);
3775b03aba6SOleksandr Tymoshenko 	} else {
3785b03aba6SOleksandr Tymoshenko 		reg = USBCTRL_READ4(sc, USBCTRL_MODE);
3795b03aba6SOleksandr Tymoshenko 		reg |= USBCTRL_MODE_IDDIGMUX;
3805b03aba6SOleksandr Tymoshenko 		reg |= USBCTRL_MODE_IDDIG;
3815b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(sc, USBCTRL_MODE, reg);
3825b03aba6SOleksandr Tymoshenko 	}
3835b03aba6SOleksandr Tymoshenko 
3845b03aba6SOleksandr Tymoshenko 	reg = USBCTRL_INTEN_USB_ALL & ~USBCTRL_INTEN_USB_SOF;
3855b03aba6SOleksandr Tymoshenko 	USBCTRL_WRITE4(sc, USBCTRL_INTEN_SET1, reg);
3865b03aba6SOleksandr Tymoshenko 	USBCTRL_WRITE4(sc, USBCTRL_INTEN_CLR0, 0xffffffff);
3875b03aba6SOleksandr Tymoshenko 
3885b03aba6SOleksandr Tymoshenko 	err = musbotg_init(&sc->sc_otg);
3895b03aba6SOleksandr Tymoshenko 	if (!err)
3905b03aba6SOleksandr Tymoshenko 		err = device_probe_and_attach(sc->sc_otg.sc_bus.bdev);
3915b03aba6SOleksandr Tymoshenko 
3925b03aba6SOleksandr Tymoshenko 	if (err)
3935b03aba6SOleksandr Tymoshenko 		goto error;
3945b03aba6SOleksandr Tymoshenko 
3955b03aba6SOleksandr Tymoshenko 	/* poll VBUS one time */
3965b03aba6SOleksandr Tymoshenko 	musbotg_vbus_poll(sc);
3975b03aba6SOleksandr Tymoshenko 
3985b03aba6SOleksandr Tymoshenko 	return (0);
3995b03aba6SOleksandr Tymoshenko 
4005b03aba6SOleksandr Tymoshenko error:
4015b03aba6SOleksandr Tymoshenko 	musbotg_detach(dev);
4025b03aba6SOleksandr Tymoshenko 	return (ENXIO);
4035b03aba6SOleksandr Tymoshenko }
4045b03aba6SOleksandr Tymoshenko 
4055b03aba6SOleksandr Tymoshenko static int
4065b03aba6SOleksandr Tymoshenko musbotg_detach(device_t dev)
4075b03aba6SOleksandr Tymoshenko {
4085b03aba6SOleksandr Tymoshenko 	struct musbotg_super_softc *sc = device_get_softc(dev);
4095b03aba6SOleksandr Tymoshenko 	int err;
4105b03aba6SOleksandr Tymoshenko 
411d3bf5efcSHans Petter Selasky 	/* during module unload there are lots of children leftover */
412d3bf5efcSHans Petter Selasky 	device_delete_children(dev);
4135b03aba6SOleksandr Tymoshenko 
4145b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_irq_res && sc->sc_otg.sc_intr_hdl) {
4155b03aba6SOleksandr Tymoshenko 		/*
4165b03aba6SOleksandr Tymoshenko 		 * only call musbotg_uninit() after musbotg_init()
4175b03aba6SOleksandr Tymoshenko 		 */
4185b03aba6SOleksandr Tymoshenko 		musbotg_uninit(&sc->sc_otg);
4195b03aba6SOleksandr Tymoshenko 
4205b03aba6SOleksandr Tymoshenko 		err = bus_teardown_intr(dev, sc->sc_otg.sc_irq_res,
4215b03aba6SOleksandr Tymoshenko 		    sc->sc_otg.sc_intr_hdl);
4225b03aba6SOleksandr Tymoshenko 		sc->sc_otg.sc_intr_hdl = NULL;
4235b03aba6SOleksandr Tymoshenko 	}
4245b03aba6SOleksandr Tymoshenko 
4255b03aba6SOleksandr Tymoshenko 	usb_bus_mem_free_all(&sc->sc_otg.sc_bus, NULL);
4265b03aba6SOleksandr Tymoshenko 
4275b03aba6SOleksandr Tymoshenko 	/* Free resources if any */
4285b03aba6SOleksandr Tymoshenko 	if (sc->sc_mem_res[0])
4295b03aba6SOleksandr Tymoshenko 		bus_release_resources(dev, am335x_musbotg_mem_spec,
4305b03aba6SOleksandr Tymoshenko 		    sc->sc_mem_res);
4315b03aba6SOleksandr Tymoshenko 
4325b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_irq_res)
4335b03aba6SOleksandr Tymoshenko 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid,
4345b03aba6SOleksandr Tymoshenko 		    sc->sc_otg.sc_irq_res);
4355b03aba6SOleksandr Tymoshenko 
4365b03aba6SOleksandr Tymoshenko 	return (0);
4375b03aba6SOleksandr Tymoshenko }
4385b03aba6SOleksandr Tymoshenko 
4395b03aba6SOleksandr Tymoshenko static device_method_t musbotg_methods[] = {
4405b03aba6SOleksandr Tymoshenko 	/* Device interface */
4415b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_probe, musbotg_probe),
4425b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_attach, musbotg_attach),
4435b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_detach, musbotg_detach),
4445b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_suspend, bus_generic_suspend),
4455b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_resume, bus_generic_resume),
4465b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
4475b03aba6SOleksandr Tymoshenko 
4485b03aba6SOleksandr Tymoshenko 	DEVMETHOD_END
4495b03aba6SOleksandr Tymoshenko };
4505b03aba6SOleksandr Tymoshenko 
4515b03aba6SOleksandr Tymoshenko static driver_t musbotg_driver = {
4525b03aba6SOleksandr Tymoshenko 	.name = "musbotg",
4535b03aba6SOleksandr Tymoshenko 	.methods = musbotg_methods,
4545b03aba6SOleksandr Tymoshenko 	.size = sizeof(struct musbotg_super_softc),
4555b03aba6SOleksandr Tymoshenko };
4565b03aba6SOleksandr Tymoshenko 
4575b03aba6SOleksandr Tymoshenko static devclass_t musbotg_devclass;
4585b03aba6SOleksandr Tymoshenko 
459*0050ea24SMichal Meloun DRIVER_MODULE(musbotg, ti_sysc, musbotg_driver, musbotg_devclass, 0, 0);
460*0050ea24SMichal Meloun MODULE_DEPEND(musbotg, ti_sysc, 1, 1, 1);
461*0050ea24SMichal Meloun MODULE_DEPEND(musbotg, ti_am3359_cppi41, 1, 1, 1);
462*0050ea24SMichal Meloun MODULE_DEPEND(usbss, usb, 1, 1, 1);
463