2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 Oleksandr Tymoshenko <gonzo@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/stdint.h>
32 #include <sys/stddef.h>
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
50 #include <dev/fdt/simplebus.h>
51 #include <dev/ofw/openfirm.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_busdma.h>
60 #include <dev/usb/usb_process.h>
61 #include <dev/usb/usb_util.h>
63 #include <dev/usb/usb_controller.h>
64 #include <dev/usb/usb_bus.h>
65 #include <dev/usb/controller/musb_otg.h>
66 #include <dev/usb/usb_debug.h>
70 #include <arm/ti/ti_prcm.h>
71 #include <arm/ti/ti_scm.h>
72 #include <arm/ti/am335x/am335x_scm.h>
74 #define AM335X_USB_PORTS 2
76 #define USBSS_REVREG 0x00
77 #define USBSS_SYSCONFIG 0x10
78 #define USBSS_SYSCONFIG_SRESET 1
80 #define USBCTRL_REV 0x00
81 #define USBCTRL_CTRL 0x14
82 #define USBCTRL_STAT 0x18
83 #define USBCTRL_IRQ_STAT0 0x30
84 #define IRQ_STAT0_RXSHIFT 16
85 #define IRQ_STAT0_TXSHIFT 0
86 #define USBCTRL_IRQ_STAT1 0x34
87 #define IRQ_STAT1_DRVVBUS (1 << 8)
88 #define USBCTRL_INTEN_SET0 0x38
89 #define USBCTRL_INTEN_SET1 0x3C
90 #define USBCTRL_INTEN_USB_ALL 0x1ff
91 #define USBCTRL_INTEN_USB_SOF (1 << 3)
92 #define USBCTRL_INTEN_CLR0 0x40
93 #define USBCTRL_INTEN_CLR1 0x44
94 #define USBCTRL_UTMI 0xE0
95 #define USBCTRL_UTMI_FSDATAEXT (1 << 1)
96 #define USBCTRL_MODE 0xE8
97 #define USBCTRL_MODE_IDDIG (1 << 8)
98 #define USBCTRL_MODE_IDDIGMUX (1 << 7)
100 #define USBSS_WRITE4(sc, reg, val) \
101 bus_write_4((sc)->sc_mem_res, (reg), (val))
102 #define USBSS_READ4(sc, reg) \
103 bus_read_4((sc)->sc_mem_res, (reg))
105 static device_probe_t usbss_probe;
106 static device_attach_t usbss_attach;
107 static device_detach_t usbss_detach;
110 struct simplebus_softc simplebus_sc;
111 struct resource *sc_mem_res;
116 usbss_probe(device_t dev)
119 if (!ofw_bus_status_okay(dev))
122 if (!ofw_bus_is_compatible(dev, "ti,am33xx-usb"))
125 device_set_desc(dev, "TI AM33xx integrated USB OTG controller");
127 return (BUS_PROBE_DEFAULT);
131 usbss_attach(device_t dev)
133 struct usbss_softc *sc = device_get_softc(dev);
138 /* Request the memory resources */
139 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
140 &sc->sc_mem_rid, RF_ACTIVE);
141 if (sc->sc_mem_res == NULL) {
143 "Error: could not allocate mem resources\n");
147 /* Enable device clocks. */
148 ti_prcm_clk_enable(MUSB0_CLK);
151 * Reset USBSS, USB0 and USB1.
152 * The registers of USB subsystem must not be accessed while the
153 * reset pulse is active (200ns).
155 USBSS_WRITE4(sc, USBSS_SYSCONFIG, USBSS_SYSCONFIG_SRESET);
158 while (USBSS_READ4(sc, USBSS_SYSCONFIG) & USBSS_SYSCONFIG_SRESET) {
161 device_printf(dev, "reset timeout.\n");
166 /* Read the module revision. */
167 rev = USBSS_READ4(sc, USBSS_REVREG);
168 device_printf(dev, "TI AM335X USBSS v%d.%d.%d\n",
169 (rev >> 8) & 7, (rev >> 6) & 3, rev & 63);
171 node = ofw_bus_get_node(dev);
178 simplebus_init(dev, node);
181 * Allow devices to identify.
183 bus_generic_probe(dev);
186 * Now walk the OFW tree and attach top-level devices.
188 for (node = OF_child(node); node > 0; node = OF_peer(node))
189 simplebus_add_device(dev, node, 0, NULL, -1, NULL);
191 return (bus_generic_attach(dev));
195 usbss_detach(device_t dev)
197 struct usbss_softc *sc = device_get_softc(dev);
199 /* Free resources if any */
201 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_mem_rid,
204 /* during module unload there are lots of children leftover */
205 device_delete_children(dev);
210 static device_method_t usbss_methods[] = {
211 /* Device interface */
212 DEVMETHOD(device_probe, usbss_probe),
213 DEVMETHOD(device_attach, usbss_attach),
214 DEVMETHOD(device_detach, usbss_detach),
215 DEVMETHOD(device_suspend, bus_generic_suspend),
216 DEVMETHOD(device_resume, bus_generic_resume),
217 DEVMETHOD(device_shutdown, bus_generic_shutdown),
222 DEFINE_CLASS_1(usbss, usbss_driver, usbss_methods,
223 sizeof(struct usbss_softc), simplebus_driver);
224 static devclass_t usbss_devclass;
225 DRIVER_MODULE(usbss, simplebus, usbss_driver, usbss_devclass, 0, 0);
226 MODULE_DEPEND(usbss, usb, 1, 1, 1);