3 * Copyright (c) 2014 Hans Petter Selasky <hselasky@FreeBSD.org>
6 * This software was developed by SRI International and the University of
7 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8 * ("CTSRD"), as part of the DARPA CRASH research programme.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #ifdef USB_GLOBAL_INCLUDE_FILE
33 #include USB_GLOBAL_INCLUDE_FILE
35 #include <sys/stdint.h>
36 #include <sys/stddef.h>
37 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/types.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/condvar.h>
47 #include <sys/sysctl.h>
49 #include <sys/unistd.h>
50 #include <sys/callout.h>
51 #include <sys/malloc.h>
55 #include <dev/fdt/fdt_common.h>
57 #include <dev/ofw/openfirm.h>
58 #include <dev/ofw/ofw_bus.h>
59 #include <dev/ofw/ofw_bus_subr.h>
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbdi.h>
64 #include <dev/usb/usb_core.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_process.h>
67 #include <dev/usb/usb_transfer.h>
68 #include <dev/usb/usb_device.h>
69 #include <dev/usb/usb_hub.h>
70 #include <dev/usb/usb_util.h>
72 #include <dev/usb/usb_controller.h>
73 #include <dev/usb/usb_bus.h>
74 #endif /* USB_GLOBAL_INCLUDE_FILE */
76 #include <dev/usb/controller/saf1761_otg.h>
77 #include <dev/usb/controller/saf1761_otg_reg.h>
79 static device_probe_t saf1761_otg_fdt_probe;
80 static device_attach_t saf1761_otg_fdt_attach;
81 static device_detach_t saf1761_otg_fdt_detach;
83 static device_method_t saf1761_otg_methods[] = {
84 /* Device interface */
85 DEVMETHOD(device_probe, saf1761_otg_fdt_probe),
86 DEVMETHOD(device_attach, saf1761_otg_fdt_attach),
87 DEVMETHOD(device_detach, saf1761_otg_fdt_detach),
88 DEVMETHOD(device_suspend, bus_generic_suspend),
89 DEVMETHOD(device_resume, bus_generic_resume),
90 DEVMETHOD(device_shutdown, bus_generic_shutdown),
95 static driver_t saf1761_otg_driver = {
97 .methods = saf1761_otg_methods,
98 .size = sizeof(struct saf1761_otg_softc),
101 static devclass_t saf1761_otg_devclass;
103 DRIVER_MODULE(saf1761otg, simplebus, saf1761_otg_driver, saf1761_otg_devclass, 0, 0);
104 MODULE_DEPEND(saf1761otg, usb, 1, 1, 1);
107 saf1761_otg_fdt_probe(device_t dev)
109 if (!ofw_bus_status_okay(dev))
112 if (!ofw_bus_is_compatible(dev, "nxp,usb-isp1761"))
115 device_set_desc(dev, "ISP1761/SAF1761 DCI USB 2.0 Device Controller");
121 saf1761_otg_fdt_attach(device_t dev)
123 struct saf1761_otg_softc *sc = device_get_softc(dev);
128 /* get configuration from FDT */
130 /* get bus-width, if any */
131 if (OF_getprop(ofw_bus_get_node(dev), "bus-width",
132 ¶m, sizeof(param)) > 0) {
133 param[sizeof(param) - 1] = 0;
134 if (strcmp(param, "32") == 0)
135 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH;
137 /* assume 32-bit data bus */
138 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH;
141 /* get analog over-current setting */
142 if (OF_getprop(ofw_bus_get_node(dev), "analog-oc",
143 ¶m, sizeof(param)) > 0) {
144 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_ANA_DIGI_OC;
147 /* get DACK polarity */
148 if (OF_getprop(ofw_bus_get_node(dev), "dack-polarity",
149 ¶m, sizeof(param)) > 0) {
150 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DACK_POL;
153 /* get DREQ polarity */
154 if (OF_getprop(ofw_bus_get_node(dev), "dreq-polarity",
155 ¶m, sizeof(param)) > 0) {
156 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DREQ_POL;
159 /* get IRQ polarity */
160 if (OF_getprop(ofw_bus_get_node(dev), "int-polarity",
161 ¶m, sizeof(param)) > 0) {
162 sc->sc_interrupt_cfg |= SOTG_INTERRUPT_CFG_INTPOL;
163 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_INTR_POL;
166 /* get IRQ level triggering */
167 if (OF_getprop(ofw_bus_get_node(dev), "int-level",
168 ¶m, sizeof(param)) > 0) {
169 sc->sc_interrupt_cfg |= SOTG_INTERRUPT_CFG_INTLVL;
170 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_INTR_LEVEL;
173 /* initialise some bus fields */
174 sc->sc_bus.parent = dev;
175 sc->sc_bus.devices = sc->sc_devices;
176 sc->sc_bus.devices_max = SOTG_MAX_DEVICES;
177 sc->sc_bus.dma_bits = 32;
179 /* get all DMA memory */
180 if (usb_bus_mem_alloc_all(&sc->sc_bus,
181 USB_GET_DMA_TAG(dev), NULL)) {
186 bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
188 if (sc->sc_io_res == NULL)
191 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
192 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
193 sc->sc_io_size = rman_get_size(sc->sc_io_res);
195 /* try to allocate the HC interrupt first */
197 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
198 RF_SHAREABLE | RF_ACTIVE);
199 if (sc->sc_irq_res == NULL) {
200 /* try to allocate a common IRQ second */
202 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
203 RF_SHAREABLE | RF_ACTIVE);
204 if (sc->sc_irq_res == NULL)
208 sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
209 if (sc->sc_bus.bdev == NULL)
212 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
214 err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_TTY | INTR_MPSAFE,
215 &saf1761_otg_filter_interrupt, &saf1761_otg_interrupt, sc, &sc->sc_intr_hdl);
217 sc->sc_intr_hdl = NULL;
220 err = saf1761_otg_init(sc);
222 device_printf(dev, "Init failed\n");
225 err = device_probe_and_attach(sc->sc_bus.bdev);
227 device_printf(dev, "USB probe and attach failed\n");
233 saf1761_otg_fdt_detach(dev);
238 saf1761_otg_fdt_detach(device_t dev)
240 struct saf1761_otg_softc *sc = device_get_softc(dev);
243 /* during module unload there are lots of children leftover */
244 device_delete_children(dev);
246 if (sc->sc_irq_res && sc->sc_intr_hdl) {
248 * Only call uninit() after init()
250 saf1761_otg_uninit(sc);
252 err = bus_teardown_intr(dev, sc->sc_irq_res,
254 sc->sc_intr_hdl = NULL;
256 if (sc->sc_irq_res) {
257 bus_release_resource(dev, SYS_RES_IRQ, 0,
259 sc->sc_irq_res = NULL;
262 bus_release_resource(dev, SYS_RES_MEMORY, 0,
264 sc->sc_io_res = NULL;
266 usb_bus_mem_free_all(&sc->sc_bus, NULL);