]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/ti/am335x/am335x_usbss.c
Update to zstd 1.3.2
[FreeBSD/FreeBSD.git] / sys / arm / ti / am335x / am335x_usbss.c
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/fdt/simplebus.h>
49 #include <dev/ofw/openfirm.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
52
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbdi.h>
55
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>
60
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>
65
66 #include <sys/rman.h>
67
68 #include <arm/ti/ti_prcm.h>
69 #include <arm/ti/ti_scm.h>
70 #include <arm/ti/am335x/am335x_scm.h>
71
72 #define AM335X_USB_PORTS        2
73
74 #define USBSS_REVREG            0x00
75 #define USBSS_SYSCONFIG         0x10
76 #define         USBSS_SYSCONFIG_SRESET          1
77
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)
97
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))
102
103 static device_probe_t usbss_probe;
104 static device_attach_t usbss_attach;
105 static device_detach_t usbss_detach;
106
107 struct usbss_softc {
108         struct simplebus_softc  simplebus_sc;
109         struct resource         *sc_mem_res;
110         int                     sc_mem_rid;
111 };
112
113 static int
114 usbss_probe(device_t dev)
115 {
116
117         if (!ofw_bus_status_okay(dev))
118                 return (ENXIO);
119
120         if (!ofw_bus_is_compatible(dev, "ti,am33xx-usb"))
121                 return (ENXIO);
122
123         device_set_desc(dev, "TI AM33xx integrated USB OTG controller");
124         
125         return (BUS_PROBE_DEFAULT);
126 }
127
128 static int
129 usbss_attach(device_t dev)
130 {
131         struct usbss_softc *sc = device_get_softc(dev);
132         int i;
133         uint32_t rev;
134         phandle_t node;
135
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) {
140                 device_printf(dev,
141                     "Error: could not allocate mem resources\n");
142                 return (ENXIO);
143         }
144
145         /* Enable device clocks. */
146         ti_prcm_clk_enable(MUSB0_CLK);
147
148         /*
149          * Reset USBSS, USB0 and USB1.
150          * The registers of USB subsystem must not be accessed while the
151          * reset pulse is active (200ns).
152          */
153         USBSS_WRITE4(sc, USBSS_SYSCONFIG, USBSS_SYSCONFIG_SRESET);
154         DELAY(100);
155         i = 10;
156         while (USBSS_READ4(sc, USBSS_SYSCONFIG) & USBSS_SYSCONFIG_SRESET) {
157                 DELAY(100);
158                 if (i-- == 0) {
159                         device_printf(dev, "reset timeout.\n");
160                         return (ENXIO);
161                 }
162         }
163
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);
168
169         node = ofw_bus_get_node(dev);
170
171         if (node == -1) {
172                 usbss_detach(dev);
173                 return (ENXIO);
174         }
175
176         simplebus_init(dev, node);
177
178         /*
179          * Allow devices to identify.
180          */
181         bus_generic_probe(dev);
182
183         /*
184          * Now walk the OFW tree and attach top-level devices.
185          */
186         for (node = OF_child(node); node > 0; node = OF_peer(node))
187                 simplebus_add_device(dev, node, 0, NULL, -1, NULL);
188
189         return (bus_generic_attach(dev));
190 }
191
192 static int
193 usbss_detach(device_t dev)
194 {
195         struct usbss_softc *sc = device_get_softc(dev);
196
197         /* Free resources if any */
198         if (sc->sc_mem_res)
199                 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_mem_rid,
200                     sc->sc_mem_res);
201
202         /* during module unload there are lots of children leftover */
203         device_delete_children(dev);
204
205         return (0);
206 }
207
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),
216
217         DEVMETHOD_END
218 };
219
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);