xref: /freebsd/sys/arm/ti/am335x/am335x_musb.c (revision 5b03aba6c8aecab2d9c302392ae2c791cd13f4c6)
1*5b03aba6SOleksandr Tymoshenko /*-
2*5b03aba6SOleksandr Tymoshenko  * Copyright (c) 2013 Oleksandr Tymoshenko <gonzo@freebsd.org>
3*5b03aba6SOleksandr Tymoshenko  *
4*5b03aba6SOleksandr Tymoshenko  * Redistribution and use in source and binary forms, with or without
5*5b03aba6SOleksandr Tymoshenko  * modification, are permitted provided that the following conditions
6*5b03aba6SOleksandr Tymoshenko  * are met:
7*5b03aba6SOleksandr Tymoshenko  * 1. Redistributions of source code must retain the above copyright
8*5b03aba6SOleksandr Tymoshenko  *    notice, this list of conditions and the following disclaimer.
9*5b03aba6SOleksandr Tymoshenko  * 2. Redistributions in binary form must reproduce the above copyright
10*5b03aba6SOleksandr Tymoshenko  *    notice, this list of conditions and the following disclaimer in the
11*5b03aba6SOleksandr Tymoshenko  *    documentation and/or other materials provided with the distribution.
12*5b03aba6SOleksandr Tymoshenko  *
13*5b03aba6SOleksandr Tymoshenko  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*5b03aba6SOleksandr Tymoshenko  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*5b03aba6SOleksandr Tymoshenko  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*5b03aba6SOleksandr Tymoshenko  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*5b03aba6SOleksandr Tymoshenko  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*5b03aba6SOleksandr Tymoshenko  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*5b03aba6SOleksandr Tymoshenko  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*5b03aba6SOleksandr Tymoshenko  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*5b03aba6SOleksandr Tymoshenko  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*5b03aba6SOleksandr Tymoshenko  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*5b03aba6SOleksandr Tymoshenko  * SUCH DAMAGE.
24*5b03aba6SOleksandr Tymoshenko  */
25*5b03aba6SOleksandr Tymoshenko 
26*5b03aba6SOleksandr Tymoshenko #include <sys/cdefs.h>
27*5b03aba6SOleksandr Tymoshenko __FBSDID("$FreeBSD$");
28*5b03aba6SOleksandr Tymoshenko 
29*5b03aba6SOleksandr Tymoshenko #include <sys/stdint.h>
30*5b03aba6SOleksandr Tymoshenko #include <sys/stddef.h>
31*5b03aba6SOleksandr Tymoshenko #include <sys/param.h>
32*5b03aba6SOleksandr Tymoshenko #include <sys/queue.h>
33*5b03aba6SOleksandr Tymoshenko #include <sys/types.h>
34*5b03aba6SOleksandr Tymoshenko #include <sys/systm.h>
35*5b03aba6SOleksandr Tymoshenko #include <sys/kernel.h>
36*5b03aba6SOleksandr Tymoshenko #include <sys/bus.h>
37*5b03aba6SOleksandr Tymoshenko #include <sys/module.h>
38*5b03aba6SOleksandr Tymoshenko #include <sys/lock.h>
39*5b03aba6SOleksandr Tymoshenko #include <sys/mutex.h>
40*5b03aba6SOleksandr Tymoshenko #include <sys/condvar.h>
41*5b03aba6SOleksandr Tymoshenko #include <sys/sysctl.h>
42*5b03aba6SOleksandr Tymoshenko #include <sys/sx.h>
43*5b03aba6SOleksandr Tymoshenko #include <sys/unistd.h>
44*5b03aba6SOleksandr Tymoshenko #include <sys/callout.h>
45*5b03aba6SOleksandr Tymoshenko #include <sys/malloc.h>
46*5b03aba6SOleksandr Tymoshenko #include <sys/priv.h>
47*5b03aba6SOleksandr Tymoshenko 
48*5b03aba6SOleksandr Tymoshenko #include <dev/fdt/fdt_common.h>
49*5b03aba6SOleksandr Tymoshenko #include <dev/ofw/openfirm.h>
50*5b03aba6SOleksandr Tymoshenko #include <dev/ofw/ofw_bus.h>
51*5b03aba6SOleksandr Tymoshenko #include <dev/ofw/ofw_bus_subr.h>
52*5b03aba6SOleksandr Tymoshenko 
53*5b03aba6SOleksandr Tymoshenko #include <dev/usb/usb.h>
54*5b03aba6SOleksandr Tymoshenko #include <dev/usb/usbdi.h>
55*5b03aba6SOleksandr Tymoshenko 
56*5b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_core.h>
57*5b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_busdma.h>
58*5b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_process.h>
59*5b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_util.h>
60*5b03aba6SOleksandr Tymoshenko 
61*5b03aba6SOleksandr Tymoshenko #define	USB_DEBUG_VAR usbssdebug
62*5b03aba6SOleksandr Tymoshenko 
63*5b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_controller.h>
64*5b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_bus.h>
65*5b03aba6SOleksandr Tymoshenko #include <dev/usb/controller/musb_otg.h>
66*5b03aba6SOleksandr Tymoshenko #include <dev/usb/usb_debug.h>
67*5b03aba6SOleksandr Tymoshenko 
68*5b03aba6SOleksandr Tymoshenko #include <sys/rman.h>
69*5b03aba6SOleksandr Tymoshenko 
70*5b03aba6SOleksandr Tymoshenko #include <arm/ti/ti_prcm.h>
71*5b03aba6SOleksandr Tymoshenko #include <arm/ti/ti_scm.h>
72*5b03aba6SOleksandr Tymoshenko #include <arm/ti/am335x/am335x_scm.h>
73*5b03aba6SOleksandr Tymoshenko 
74*5b03aba6SOleksandr Tymoshenko #define USBCTRL_REV		0x00
75*5b03aba6SOleksandr Tymoshenko #define USBCTRL_CTRL		0x14
76*5b03aba6SOleksandr Tymoshenko #define USBCTRL_STAT		0x18
77*5b03aba6SOleksandr Tymoshenko #define USBCTRL_IRQ_STAT0	0x30
78*5b03aba6SOleksandr Tymoshenko #define		IRQ_STAT0_RXSHIFT	16
79*5b03aba6SOleksandr Tymoshenko #define		IRQ_STAT0_TXSHIFT	0
80*5b03aba6SOleksandr Tymoshenko #define USBCTRL_IRQ_STAT1	0x34
81*5b03aba6SOleksandr Tymoshenko #define 	IRQ_STAT1_DRVVBUS	(1 << 8)
82*5b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_SET0	0x38
83*5b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_SET1	0x3C
84*5b03aba6SOleksandr Tymoshenko #define 	USBCTRL_INTEN_USB_ALL	0x1ff
85*5b03aba6SOleksandr Tymoshenko #define 	USBCTRL_INTEN_USB_SOF	(1 << 3)
86*5b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_CLR0	0x40
87*5b03aba6SOleksandr Tymoshenko #define USBCTRL_INTEN_CLR1	0x44
88*5b03aba6SOleksandr Tymoshenko #define USBCTRL_UTMI		0xE0
89*5b03aba6SOleksandr Tymoshenko #define		USBCTRL_UTMI_FSDATAEXT		(1 << 1)
90*5b03aba6SOleksandr Tymoshenko #define USBCTRL_MODE		0xE8
91*5b03aba6SOleksandr Tymoshenko #define 	USBCTRL_MODE_IDDIG		(1 << 8)
92*5b03aba6SOleksandr Tymoshenko #define 	USBCTRL_MODE_IDDIGMUX		(1 << 7)
93*5b03aba6SOleksandr Tymoshenko 
94*5b03aba6SOleksandr Tymoshenko /* USBSS resource + 2 MUSB ports */
95*5b03aba6SOleksandr Tymoshenko 
96*5b03aba6SOleksandr Tymoshenko #define RES_USBCORE	0
97*5b03aba6SOleksandr Tymoshenko #define RES_USBCTRL	1
98*5b03aba6SOleksandr Tymoshenko 
99*5b03aba6SOleksandr Tymoshenko #define	USB_WRITE4(sc, idx, reg, val)	do {		\
100*5b03aba6SOleksandr Tymoshenko 	bus_write_4((sc)->sc_mem_res[idx], (reg), (val));	\
101*5b03aba6SOleksandr Tymoshenko } while (0)
102*5b03aba6SOleksandr Tymoshenko 
103*5b03aba6SOleksandr Tymoshenko #define	USB_READ4(sc, idx, reg) bus_read_4((sc)->sc_mem_res[idx], (reg))
104*5b03aba6SOleksandr Tymoshenko 
105*5b03aba6SOleksandr Tymoshenko #define	USBCTRL_WRITE4(sc, reg, val)	\
106*5b03aba6SOleksandr Tymoshenko     USB_WRITE4((sc), RES_USBCTRL, (reg), (val))
107*5b03aba6SOleksandr Tymoshenko #define	USBCTRL_READ4(sc, reg)		\
108*5b03aba6SOleksandr Tymoshenko     USB_READ4((sc), RES_USBCTRL, (reg))
109*5b03aba6SOleksandr Tymoshenko 
110*5b03aba6SOleksandr Tymoshenko static struct resource_spec am335x_musbotg_mem_spec[] = {
111*5b03aba6SOleksandr Tymoshenko 	{ SYS_RES_MEMORY,   0,  RF_ACTIVE },
112*5b03aba6SOleksandr Tymoshenko 	{ SYS_RES_MEMORY,   1,  RF_ACTIVE },
113*5b03aba6SOleksandr Tymoshenko 	{ -1,               0,  0 }
114*5b03aba6SOleksandr Tymoshenko };
115*5b03aba6SOleksandr Tymoshenko 
116*5b03aba6SOleksandr Tymoshenko #ifdef USB_DEBUG
117*5b03aba6SOleksandr Tymoshenko static int usbssdebug = 0;
118*5b03aba6SOleksandr Tymoshenko 
119*5b03aba6SOleksandr Tymoshenko static SYSCTL_NODE(_hw_usb, OID_AUTO, am335x_usbss, CTLFLAG_RW, 0, "AM335x USBSS");
120*5b03aba6SOleksandr Tymoshenko SYSCTL_INT(_hw_usb_am335x_usbss, OID_AUTO, debug, CTLFLAG_RW,
121*5b03aba6SOleksandr Tymoshenko     &usbssdebug, 0, "Debug level");
122*5b03aba6SOleksandr Tymoshenko #endif
123*5b03aba6SOleksandr Tymoshenko 
124*5b03aba6SOleksandr Tymoshenko static device_probe_t musbotg_probe;
125*5b03aba6SOleksandr Tymoshenko static device_attach_t musbotg_attach;
126*5b03aba6SOleksandr Tymoshenko static device_detach_t musbotg_detach;
127*5b03aba6SOleksandr Tymoshenko 
128*5b03aba6SOleksandr Tymoshenko struct musbotg_super_softc {
129*5b03aba6SOleksandr Tymoshenko 	struct musbotg_softc	sc_otg;
130*5b03aba6SOleksandr Tymoshenko 	struct resource		*sc_mem_res[2];
131*5b03aba6SOleksandr Tymoshenko 	int			sc_irq_rid;
132*5b03aba6SOleksandr Tymoshenko };
133*5b03aba6SOleksandr Tymoshenko 
134*5b03aba6SOleksandr Tymoshenko static void
135*5b03aba6SOleksandr Tymoshenko musbotg_vbus_poll(struct musbotg_super_softc *sc)
136*5b03aba6SOleksandr Tymoshenko {
137*5b03aba6SOleksandr Tymoshenko 	uint32_t stat;
138*5b03aba6SOleksandr Tymoshenko 
139*5b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_mode == MUSB2_DEVICE_MODE)
140*5b03aba6SOleksandr Tymoshenko 		musbotg_vbus_interrupt(&sc->sc_otg, 1);
141*5b03aba6SOleksandr Tymoshenko 	else {
142*5b03aba6SOleksandr Tymoshenko 		stat = USBCTRL_READ4(sc, USBCTRL_STAT);
143*5b03aba6SOleksandr Tymoshenko 		musbotg_vbus_interrupt(&sc->sc_otg, stat & 1);
144*5b03aba6SOleksandr Tymoshenko 	}
145*5b03aba6SOleksandr Tymoshenko }
146*5b03aba6SOleksandr Tymoshenko 
147*5b03aba6SOleksandr Tymoshenko /*
148*5b03aba6SOleksandr Tymoshenko  * Arg to musbotg_clocks_on and musbot_clocks_off is
149*5b03aba6SOleksandr Tymoshenko  * a uint32_t * pointing to the SCM register offset.
150*5b03aba6SOleksandr Tymoshenko  */
151*5b03aba6SOleksandr Tymoshenko static uint32_t USB_CTRL[] = {SCM_USB_CTRL0, SCM_USB_CTRL1};
152*5b03aba6SOleksandr Tymoshenko 
153*5b03aba6SOleksandr Tymoshenko static void
154*5b03aba6SOleksandr Tymoshenko musbotg_clocks_on(void *arg)
155*5b03aba6SOleksandr Tymoshenko {
156*5b03aba6SOleksandr Tymoshenko 	struct musbotg_softc *sc;
157*5b03aba6SOleksandr Tymoshenko 	uint32_t c, reg;
158*5b03aba6SOleksandr Tymoshenko 
159*5b03aba6SOleksandr Tymoshenko 	sc = arg;
160*5b03aba6SOleksandr Tymoshenko         reg = USB_CTRL[sc->sc_id];
161*5b03aba6SOleksandr Tymoshenko 
162*5b03aba6SOleksandr Tymoshenko 	ti_scm_reg_read_4(reg, &c);
163*5b03aba6SOleksandr Tymoshenko 	c &= ~3; /* Enable power */
164*5b03aba6SOleksandr Tymoshenko 	c |= 1 << 19; /* VBUS detect enable */
165*5b03aba6SOleksandr Tymoshenko 	c |= 1 << 20; /* Session end enable */
166*5b03aba6SOleksandr Tymoshenko 	ti_scm_reg_write_4(reg, c);
167*5b03aba6SOleksandr Tymoshenko }
168*5b03aba6SOleksandr Tymoshenko 
169*5b03aba6SOleksandr Tymoshenko static void
170*5b03aba6SOleksandr Tymoshenko musbotg_clocks_off(void *arg)
171*5b03aba6SOleksandr Tymoshenko {
172*5b03aba6SOleksandr Tymoshenko 	struct musbotg_softc *sc;
173*5b03aba6SOleksandr Tymoshenko 	uint32_t c, reg;
174*5b03aba6SOleksandr Tymoshenko 
175*5b03aba6SOleksandr Tymoshenko 	sc = arg;
176*5b03aba6SOleksandr Tymoshenko         reg = USB_CTRL[sc->sc_id];
177*5b03aba6SOleksandr Tymoshenko 
178*5b03aba6SOleksandr Tymoshenko 	/* Disable power to PHY */
179*5b03aba6SOleksandr Tymoshenko 	ti_scm_reg_read_4(reg, &c);
180*5b03aba6SOleksandr Tymoshenko 	ti_scm_reg_write_4(reg, c | 3);
181*5b03aba6SOleksandr Tymoshenko }
182*5b03aba6SOleksandr Tymoshenko 
183*5b03aba6SOleksandr Tymoshenko static void
184*5b03aba6SOleksandr Tymoshenko musbotg_ep_int_set(struct musbotg_softc *sc, int ep, int on)
185*5b03aba6SOleksandr Tymoshenko {
186*5b03aba6SOleksandr Tymoshenko 	struct musbotg_super_softc *ssc = sc->sc_platform_data;
187*5b03aba6SOleksandr Tymoshenko 	uint32_t epmask;
188*5b03aba6SOleksandr Tymoshenko 
189*5b03aba6SOleksandr Tymoshenko 	epmask = ((1 << ep) << IRQ_STAT0_RXSHIFT);
190*5b03aba6SOleksandr Tymoshenko 	epmask |= ((1 << ep) << IRQ_STAT0_TXSHIFT);
191*5b03aba6SOleksandr Tymoshenko 	if (on)
192*5b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(ssc, USBCTRL_INTEN_SET0, epmask);
193*5b03aba6SOleksandr Tymoshenko 	else
194*5b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(ssc, USBCTRL_INTEN_CLR0, epmask);
195*5b03aba6SOleksandr Tymoshenko }
196*5b03aba6SOleksandr Tymoshenko 
197*5b03aba6SOleksandr Tymoshenko static void
198*5b03aba6SOleksandr Tymoshenko musbotg_wrapper_interrupt(void *arg)
199*5b03aba6SOleksandr Tymoshenko {
200*5b03aba6SOleksandr Tymoshenko 	struct musbotg_softc *sc = arg;
201*5b03aba6SOleksandr Tymoshenko 	struct musbotg_super_softc *ssc = sc->sc_platform_data;
202*5b03aba6SOleksandr Tymoshenko 	uint32_t stat, stat0, stat1;
203*5b03aba6SOleksandr Tymoshenko 
204*5b03aba6SOleksandr Tymoshenko 	stat = USBCTRL_READ4(ssc, USBCTRL_STAT);
205*5b03aba6SOleksandr Tymoshenko 	stat0 = USBCTRL_READ4(ssc, USBCTRL_IRQ_STAT0);
206*5b03aba6SOleksandr Tymoshenko 	stat1 = USBCTRL_READ4(ssc, USBCTRL_IRQ_STAT1);
207*5b03aba6SOleksandr Tymoshenko 	if (stat0)
208*5b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(ssc, USBCTRL_IRQ_STAT0, stat0);
209*5b03aba6SOleksandr Tymoshenko 	if (stat1)
210*5b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(ssc, USBCTRL_IRQ_STAT1, stat1);
211*5b03aba6SOleksandr Tymoshenko 
212*5b03aba6SOleksandr Tymoshenko 	DPRINTFN(4, "port%d: stat0=%08x stat1=%08x, stat=%08x\n",
213*5b03aba6SOleksandr Tymoshenko 	    sc->sc_id, stat0, stat1, stat);
214*5b03aba6SOleksandr Tymoshenko 
215*5b03aba6SOleksandr Tymoshenko 	if (stat1 & IRQ_STAT1_DRVVBUS)
216*5b03aba6SOleksandr Tymoshenko 		musbotg_vbus_interrupt(sc, stat & 1);
217*5b03aba6SOleksandr Tymoshenko 
218*5b03aba6SOleksandr Tymoshenko 	musbotg_interrupt(arg, ((stat0 >> 16) & 0xffff),
219*5b03aba6SOleksandr Tymoshenko 	    stat0 & 0xffff, stat1 & 0xff);
220*5b03aba6SOleksandr Tymoshenko }
221*5b03aba6SOleksandr Tymoshenko 
222*5b03aba6SOleksandr Tymoshenko static int
223*5b03aba6SOleksandr Tymoshenko musbotg_probe(device_t dev)
224*5b03aba6SOleksandr Tymoshenko {
225*5b03aba6SOleksandr Tymoshenko 	if (!ofw_bus_status_okay(dev))
226*5b03aba6SOleksandr Tymoshenko 		return (ENXIO);
227*5b03aba6SOleksandr Tymoshenko 
228*5b03aba6SOleksandr Tymoshenko 	if (!ofw_bus_is_compatible(dev, "ti,musb-am33xx"))
229*5b03aba6SOleksandr Tymoshenko 		return (ENXIO);
230*5b03aba6SOleksandr Tymoshenko 
231*5b03aba6SOleksandr Tymoshenko 	device_set_desc(dev, "TI AM33xx integrated USB OTG controller");
232*5b03aba6SOleksandr Tymoshenko 
233*5b03aba6SOleksandr Tymoshenko 	return (BUS_PROBE_DEFAULT);
234*5b03aba6SOleksandr Tymoshenko }
235*5b03aba6SOleksandr Tymoshenko 
236*5b03aba6SOleksandr Tymoshenko static int
237*5b03aba6SOleksandr Tymoshenko musbotg_attach(device_t dev)
238*5b03aba6SOleksandr Tymoshenko {
239*5b03aba6SOleksandr Tymoshenko 	struct musbotg_super_softc *sc = device_get_softc(dev);
240*5b03aba6SOleksandr Tymoshenko 	int err;
241*5b03aba6SOleksandr Tymoshenko 	uint32_t reg;
242*5b03aba6SOleksandr Tymoshenko 
243*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_id = device_get_unit(dev);
244*5b03aba6SOleksandr Tymoshenko 
245*5b03aba6SOleksandr Tymoshenko 	/* Request the memory resources */
246*5b03aba6SOleksandr Tymoshenko 	err = bus_alloc_resources(dev, am335x_musbotg_mem_spec,
247*5b03aba6SOleksandr Tymoshenko 		sc->sc_mem_res);
248*5b03aba6SOleksandr Tymoshenko 	if (err) {
249*5b03aba6SOleksandr Tymoshenko 		device_printf(dev,
250*5b03aba6SOleksandr Tymoshenko 		    "Error: could not allocate mem resources\n");
251*5b03aba6SOleksandr Tymoshenko 		return (ENXIO);
252*5b03aba6SOleksandr Tymoshenko 	}
253*5b03aba6SOleksandr Tymoshenko 
254*5b03aba6SOleksandr Tymoshenko 	/* Request the IRQ resources */
255*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
256*5b03aba6SOleksandr Tymoshenko 	    &sc->sc_irq_rid, RF_ACTIVE);
257*5b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_irq_res == NULL) {
258*5b03aba6SOleksandr Tymoshenko 		device_printf(dev,
259*5b03aba6SOleksandr Tymoshenko 		    "Error: could not allocate irq resources\n");
260*5b03aba6SOleksandr Tymoshenko 		return (ENXIO);
261*5b03aba6SOleksandr Tymoshenko 	}
262*5b03aba6SOleksandr Tymoshenko 
263*5b03aba6SOleksandr Tymoshenko 	/* setup MUSB OTG USB controller interface softc */
264*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_clocks_on = &musbotg_clocks_on;
265*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_clocks_off = &musbotg_clocks_off;
266*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_clocks_arg = &sc->sc_otg;
267*5b03aba6SOleksandr Tymoshenko 
268*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_ep_int_set = musbotg_ep_int_set;
269*5b03aba6SOleksandr Tymoshenko 
270*5b03aba6SOleksandr Tymoshenko 	/* initialise some bus fields */
271*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_bus.parent = dev;
272*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_bus.devices = sc->sc_otg.sc_devices;
273*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_bus.devices_max = MUSB2_MAX_DEVICES;
274*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_bus.dma_bits = 32;
275*5b03aba6SOleksandr Tymoshenko 
276*5b03aba6SOleksandr Tymoshenko 	/* get all DMA memory */
277*5b03aba6SOleksandr Tymoshenko 	if (usb_bus_mem_alloc_all(&sc->sc_otg.sc_bus,
278*5b03aba6SOleksandr Tymoshenko 	    USB_GET_DMA_TAG(dev), NULL)) {
279*5b03aba6SOleksandr Tymoshenko 		device_printf(dev,
280*5b03aba6SOleksandr Tymoshenko 		    "Failed allocate bus mem for musb\n");
281*5b03aba6SOleksandr Tymoshenko 		return (ENOMEM);
282*5b03aba6SOleksandr Tymoshenko 	}
283*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_io_res = sc->sc_mem_res[RES_USBCORE];
284*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_io_tag =
285*5b03aba6SOleksandr Tymoshenko 	    rman_get_bustag(sc->sc_otg.sc_io_res);
286*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_io_hdl =
287*5b03aba6SOleksandr Tymoshenko 	    rman_get_bushandle(sc->sc_otg.sc_io_res);
288*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_io_size =
289*5b03aba6SOleksandr Tymoshenko 	    rman_get_size(sc->sc_otg.sc_io_res);
290*5b03aba6SOleksandr Tymoshenko 
291*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_bus.bdev = device_add_child(dev, "usbus", -1);
292*5b03aba6SOleksandr Tymoshenko 	if (!(sc->sc_otg.sc_bus.bdev)) {
293*5b03aba6SOleksandr Tymoshenko 		device_printf(dev, "No busdev for musb\n");
294*5b03aba6SOleksandr Tymoshenko 		goto error;
295*5b03aba6SOleksandr Tymoshenko 	}
296*5b03aba6SOleksandr Tymoshenko 	device_set_ivars(sc->sc_otg.sc_bus.bdev,
297*5b03aba6SOleksandr Tymoshenko 	    &sc->sc_otg.sc_bus);
298*5b03aba6SOleksandr Tymoshenko 
299*5b03aba6SOleksandr Tymoshenko 	err = bus_setup_intr(dev, sc->sc_otg.sc_irq_res,
300*5b03aba6SOleksandr Tymoshenko 	    INTR_TYPE_BIO | INTR_MPSAFE,
301*5b03aba6SOleksandr Tymoshenko 	    NULL, (driver_intr_t *)musbotg_wrapper_interrupt,
302*5b03aba6SOleksandr Tymoshenko 	    &sc->sc_otg, &sc->sc_otg.sc_intr_hdl);
303*5b03aba6SOleksandr Tymoshenko 	if (err) {
304*5b03aba6SOleksandr Tymoshenko 		sc->sc_otg.sc_intr_hdl = NULL;
305*5b03aba6SOleksandr Tymoshenko 		device_printf(dev,
306*5b03aba6SOleksandr Tymoshenko 		    "Failed to setup interrupt for musb\n");
307*5b03aba6SOleksandr Tymoshenko 		goto error;
308*5b03aba6SOleksandr Tymoshenko 	}
309*5b03aba6SOleksandr Tymoshenko 
310*5b03aba6SOleksandr Tymoshenko 	sc->sc_otg.sc_platform_data = sc;
311*5b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_id == 0)
312*5b03aba6SOleksandr Tymoshenko 		sc->sc_otg.sc_mode = MUSB2_DEVICE_MODE;
313*5b03aba6SOleksandr Tymoshenko 	else
314*5b03aba6SOleksandr Tymoshenko 		sc->sc_otg.sc_mode = MUSB2_HOST_MODE;
315*5b03aba6SOleksandr Tymoshenko 
316*5b03aba6SOleksandr Tymoshenko 	/*
317*5b03aba6SOleksandr Tymoshenko 	 * software-controlled function
318*5b03aba6SOleksandr Tymoshenko 	 */
319*5b03aba6SOleksandr Tymoshenko 
320*5b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_mode == MUSB2_HOST_MODE) {
321*5b03aba6SOleksandr Tymoshenko 		reg = USBCTRL_READ4(sc, USBCTRL_MODE);
322*5b03aba6SOleksandr Tymoshenko 		reg |= USBCTRL_MODE_IDDIGMUX;
323*5b03aba6SOleksandr Tymoshenko 		reg &= ~USBCTRL_MODE_IDDIG;
324*5b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(sc, USBCTRL_MODE, reg);
325*5b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(sc, USBCTRL_UTMI,
326*5b03aba6SOleksandr Tymoshenko 		    USBCTRL_UTMI_FSDATAEXT);
327*5b03aba6SOleksandr Tymoshenko 	} else {
328*5b03aba6SOleksandr Tymoshenko 		reg = USBCTRL_READ4(sc, USBCTRL_MODE);
329*5b03aba6SOleksandr Tymoshenko 		reg |= USBCTRL_MODE_IDDIGMUX;
330*5b03aba6SOleksandr Tymoshenko 		reg |= USBCTRL_MODE_IDDIG;
331*5b03aba6SOleksandr Tymoshenko 		USBCTRL_WRITE4(sc, USBCTRL_MODE, reg);
332*5b03aba6SOleksandr Tymoshenko 	}
333*5b03aba6SOleksandr Tymoshenko 
334*5b03aba6SOleksandr Tymoshenko 	reg = USBCTRL_INTEN_USB_ALL & ~USBCTRL_INTEN_USB_SOF;
335*5b03aba6SOleksandr Tymoshenko 	USBCTRL_WRITE4(sc, USBCTRL_INTEN_SET1, reg);
336*5b03aba6SOleksandr Tymoshenko 	USBCTRL_WRITE4(sc, USBCTRL_INTEN_CLR0, 0xffffffff);
337*5b03aba6SOleksandr Tymoshenko 
338*5b03aba6SOleksandr Tymoshenko 	err = musbotg_init(&sc->sc_otg);
339*5b03aba6SOleksandr Tymoshenko 	if (!err)
340*5b03aba6SOleksandr Tymoshenko 		err = device_probe_and_attach(sc->sc_otg.sc_bus.bdev);
341*5b03aba6SOleksandr Tymoshenko 
342*5b03aba6SOleksandr Tymoshenko 	if (err)
343*5b03aba6SOleksandr Tymoshenko 		goto error;
344*5b03aba6SOleksandr Tymoshenko 
345*5b03aba6SOleksandr Tymoshenko 	/* poll VBUS one time */
346*5b03aba6SOleksandr Tymoshenko 	musbotg_vbus_poll(sc);
347*5b03aba6SOleksandr Tymoshenko 
348*5b03aba6SOleksandr Tymoshenko 	return (0);
349*5b03aba6SOleksandr Tymoshenko 
350*5b03aba6SOleksandr Tymoshenko error:
351*5b03aba6SOleksandr Tymoshenko 	musbotg_detach(dev);
352*5b03aba6SOleksandr Tymoshenko 	return (ENXIO);
353*5b03aba6SOleksandr Tymoshenko }
354*5b03aba6SOleksandr Tymoshenko 
355*5b03aba6SOleksandr Tymoshenko static int
356*5b03aba6SOleksandr Tymoshenko musbotg_detach(device_t dev)
357*5b03aba6SOleksandr Tymoshenko {
358*5b03aba6SOleksandr Tymoshenko 	struct musbotg_super_softc *sc = device_get_softc(dev);
359*5b03aba6SOleksandr Tymoshenko 	device_t bdev;
360*5b03aba6SOleksandr Tymoshenko 	int err;
361*5b03aba6SOleksandr Tymoshenko 
362*5b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_bus.bdev) {
363*5b03aba6SOleksandr Tymoshenko 		bdev = sc->sc_otg.sc_bus.bdev;
364*5b03aba6SOleksandr Tymoshenko 		device_detach(bdev);
365*5b03aba6SOleksandr Tymoshenko 		device_delete_child(dev, bdev);
366*5b03aba6SOleksandr Tymoshenko 	}
367*5b03aba6SOleksandr Tymoshenko 
368*5b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_irq_res && sc->sc_otg.sc_intr_hdl) {
369*5b03aba6SOleksandr Tymoshenko 		/*
370*5b03aba6SOleksandr Tymoshenko 		 * only call musbotg_uninit() after musbotg_init()
371*5b03aba6SOleksandr Tymoshenko 		 */
372*5b03aba6SOleksandr Tymoshenko 		musbotg_uninit(&sc->sc_otg);
373*5b03aba6SOleksandr Tymoshenko 
374*5b03aba6SOleksandr Tymoshenko 		err = bus_teardown_intr(dev, sc->sc_otg.sc_irq_res,
375*5b03aba6SOleksandr Tymoshenko 		    sc->sc_otg.sc_intr_hdl);
376*5b03aba6SOleksandr Tymoshenko 		sc->sc_otg.sc_intr_hdl = NULL;
377*5b03aba6SOleksandr Tymoshenko 	}
378*5b03aba6SOleksandr Tymoshenko 
379*5b03aba6SOleksandr Tymoshenko 	usb_bus_mem_free_all(&sc->sc_otg.sc_bus, NULL);
380*5b03aba6SOleksandr Tymoshenko 
381*5b03aba6SOleksandr Tymoshenko 	/* Free resources if any */
382*5b03aba6SOleksandr Tymoshenko 	if (sc->sc_mem_res[0])
383*5b03aba6SOleksandr Tymoshenko 		bus_release_resources(dev, am335x_musbotg_mem_spec,
384*5b03aba6SOleksandr Tymoshenko 		    sc->sc_mem_res);
385*5b03aba6SOleksandr Tymoshenko 
386*5b03aba6SOleksandr Tymoshenko 	if (sc->sc_otg.sc_irq_res)
387*5b03aba6SOleksandr Tymoshenko 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid,
388*5b03aba6SOleksandr Tymoshenko 		    sc->sc_otg.sc_irq_res);
389*5b03aba6SOleksandr Tymoshenko 
390*5b03aba6SOleksandr Tymoshenko 	/* during module unload there are lots of children leftover */
391*5b03aba6SOleksandr Tymoshenko 	device_delete_children(dev);
392*5b03aba6SOleksandr Tymoshenko 
393*5b03aba6SOleksandr Tymoshenko 	return (0);
394*5b03aba6SOleksandr Tymoshenko }
395*5b03aba6SOleksandr Tymoshenko 
396*5b03aba6SOleksandr Tymoshenko static device_method_t musbotg_methods[] = {
397*5b03aba6SOleksandr Tymoshenko 	/* Device interface */
398*5b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_probe, musbotg_probe),
399*5b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_attach, musbotg_attach),
400*5b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_detach, musbotg_detach),
401*5b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_suspend, bus_generic_suspend),
402*5b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_resume, bus_generic_resume),
403*5b03aba6SOleksandr Tymoshenko 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
404*5b03aba6SOleksandr Tymoshenko 
405*5b03aba6SOleksandr Tymoshenko 	DEVMETHOD_END
406*5b03aba6SOleksandr Tymoshenko };
407*5b03aba6SOleksandr Tymoshenko 
408*5b03aba6SOleksandr Tymoshenko static driver_t musbotg_driver = {
409*5b03aba6SOleksandr Tymoshenko 	.name = "musbotg",
410*5b03aba6SOleksandr Tymoshenko 	.methods = musbotg_methods,
411*5b03aba6SOleksandr Tymoshenko 	.size = sizeof(struct musbotg_super_softc),
412*5b03aba6SOleksandr Tymoshenko };
413*5b03aba6SOleksandr Tymoshenko 
414*5b03aba6SOleksandr Tymoshenko static devclass_t musbotg_devclass;
415*5b03aba6SOleksandr Tymoshenko 
416*5b03aba6SOleksandr Tymoshenko DRIVER_MODULE(musbotg, usbss, musbotg_driver, musbotg_devclass, 0, 0);
417*5b03aba6SOleksandr Tymoshenko MODULE_DEPEND(musbotg, usbss, 1, 1, 1);
418