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