2 * Copyright (c) 2013 Oleksandr Tymoshenko <gonzo@freebsd.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
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>
37 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/sysctl.h>
43 #include <sys/unistd.h>
44 #include <sys/callout.h>
45 #include <sys/malloc.h>
48 #include <dev/fdt/simplebus.h>
49 #include <dev/ofw/openfirm.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usb_core.h>
57 #include <dev/usb/usb_busdma.h>
58 #include <dev/usb/usb_process.h>
59 #include <dev/usb/usb_util.h>
61 #include <dev/usb/usb_controller.h>
62 #include <dev/usb/usb_bus.h>
63 #include <dev/usb/controller/musb_otg.h>
64 #include <dev/usb/usb_debug.h>
68 #include <arm/ti/ti_prcm.h>
69 #include <arm/ti/ti_scm.h>
70 #include <arm/ti/am335x/am335x_scm.h>
72 #define AM335X_USB_PORTS 2
74 #define USBSS_REVREG 0x00
75 #define USBSS_SYSCONFIG 0x10
76 #define USBSS_SYSCONFIG_SRESET 1
78 #define USBCTRL_REV 0x00
79 #define USBCTRL_CTRL 0x14
80 #define USBCTRL_STAT 0x18
81 #define USBCTRL_IRQ_STAT0 0x30
82 #define IRQ_STAT0_RXSHIFT 16
83 #define IRQ_STAT0_TXSHIFT 0
84 #define USBCTRL_IRQ_STAT1 0x34
85 #define IRQ_STAT1_DRVVBUS (1 << 8)
86 #define USBCTRL_INTEN_SET0 0x38
87 #define USBCTRL_INTEN_SET1 0x3C
88 #define USBCTRL_INTEN_USB_ALL 0x1ff
89 #define USBCTRL_INTEN_USB_SOF (1 << 3)
90 #define USBCTRL_INTEN_CLR0 0x40
91 #define USBCTRL_INTEN_CLR1 0x44
92 #define USBCTRL_UTMI 0xE0
93 #define USBCTRL_UTMI_FSDATAEXT (1 << 1)
94 #define USBCTRL_MODE 0xE8
95 #define USBCTRL_MODE_IDDIG (1 << 8)
96 #define USBCTRL_MODE_IDDIGMUX (1 << 7)
98 #define USBSS_WRITE4(sc, reg, val) \
99 bus_write_4((sc)->sc_mem_res, (reg), (val))
100 #define USBSS_READ4(sc, reg) \
101 bus_read_4((sc)->sc_mem_res, (reg))
103 static device_probe_t usbss_probe;
104 static device_attach_t usbss_attach;
105 static device_detach_t usbss_detach;
108 struct simplebus_softc simplebus_sc;
109 struct resource *sc_mem_res;
114 usbss_probe(device_t dev)
117 if (!ofw_bus_status_okay(dev))
120 if (!ofw_bus_is_compatible(dev, "ti,am33xx-usb"))
123 device_set_desc(dev, "TI AM33xx integrated USB OTG controller");
125 return (BUS_PROBE_DEFAULT);
129 usbss_attach(device_t dev)
131 struct usbss_softc *sc = device_get_softc(dev);
136 /* Request the memory resources */
137 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
138 &sc->sc_mem_rid, RF_ACTIVE);
139 if (sc->sc_mem_res == NULL) {
141 "Error: could not allocate mem resources\n");
145 /* Enable device clocks. */
146 ti_prcm_clk_enable(MUSB0_CLK);
149 * Reset USBSS, USB0 and USB1.
150 * The registers of USB subsystem must not be accessed while the
151 * reset pulse is active (200ns).
153 USBSS_WRITE4(sc, USBSS_SYSCONFIG, USBSS_SYSCONFIG_SRESET);
156 while (USBSS_READ4(sc, USBSS_SYSCONFIG) & USBSS_SYSCONFIG_SRESET) {
159 device_printf(dev, "reset timeout.\n");
164 /* Read the module revision. */
165 rev = USBSS_READ4(sc, USBSS_REVREG);
166 device_printf(dev, "TI AM335X USBSS v%d.%d.%d\n",
167 (rev >> 8) & 7, (rev >> 6) & 3, rev & 63);
169 node = ofw_bus_get_node(dev);
176 simplebus_init(dev, node);
179 * Allow devices to identify.
181 bus_generic_probe(dev);
184 * Now walk the OFW tree and attach top-level devices.
186 for (node = OF_child(node); node > 0; node = OF_peer(node))
187 simplebus_add_device(dev, node, 0, NULL, -1, NULL);
189 return (bus_generic_attach(dev));
193 usbss_detach(device_t dev)
195 struct usbss_softc *sc = device_get_softc(dev);
197 /* Free resources if any */
199 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_mem_rid,
202 /* during module unload there are lots of children leftover */
203 device_delete_children(dev);
208 static device_method_t usbss_methods[] = {
209 /* Device interface */
210 DEVMETHOD(device_probe, usbss_probe),
211 DEVMETHOD(device_attach, usbss_attach),
212 DEVMETHOD(device_detach, usbss_detach),
213 DEVMETHOD(device_suspend, bus_generic_suspend),
214 DEVMETHOD(device_resume, bus_generic_resume),
215 DEVMETHOD(device_shutdown, bus_generic_shutdown),
220 DEFINE_CLASS_1(usbss, usbss_driver, usbss_methods,
221 sizeof(struct usbss_softc), simplebus_driver);
222 static devclass_t usbss_devclass;
223 DRIVER_MODULE(usbss, simplebus, usbss_driver, usbss_devclass, 0, 0);
224 MODULE_DEPEND(usbss, usb, 1, 1, 1);