1 /*-
2 * Copyright (c) 2014 Hans Petter Selasky <hselasky@FreeBSD.org>
3 * All rights reserved.
4 *
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #ifdef USB_GLOBAL_INCLUDE_FILE
32 #include USB_GLOBAL_INCLUDE_FILE
33 #else
34 #include <sys/stdint.h>
35 #include <sys/stddef.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/bus.h>
42 #include <sys/module.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/condvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/sx.h>
48 #include <sys/unistd.h>
49 #include <sys/callout.h>
50 #include <sys/malloc.h>
51 #include <sys/priv.h>
52 #include <sys/rman.h>
53
54 #include <dev/fdt/fdt_common.h>
55
56 #include <dev/ofw/openfirm.h>
57 #include <dev/ofw/ofw_bus.h>
58 #include <dev/ofw/ofw_bus_subr.h>
59
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62
63 #include <dev/usb/usb_core.h>
64 #include <dev/usb/usb_busdma.h>
65 #include <dev/usb/usb_process.h>
66 #include <dev/usb/usb_transfer.h>
67 #include <dev/usb/usb_device.h>
68 #include <dev/usb/usb_hub.h>
69 #include <dev/usb/usb_util.h>
70
71 #include <dev/usb/usb_controller.h>
72 #include <dev/usb/usb_bus.h>
73 #endif /* USB_GLOBAL_INCLUDE_FILE */
74
75 #include <dev/usb/controller/saf1761_otg.h>
76 #include <dev/usb/controller/saf1761_otg_reg.h>
77
78 static device_probe_t saf1761_otg_fdt_probe;
79 static device_attach_t saf1761_otg_fdt_attach;
80 static device_detach_t saf1761_otg_fdt_detach;
81
82 static device_method_t saf1761_otg_methods[] = {
83 /* Device interface */
84 DEVMETHOD(device_probe, saf1761_otg_fdt_probe),
85 DEVMETHOD(device_attach, saf1761_otg_fdt_attach),
86 DEVMETHOD(device_detach, saf1761_otg_fdt_detach),
87 DEVMETHOD(device_suspend, bus_generic_suspend),
88 DEVMETHOD(device_resume, bus_generic_resume),
89 DEVMETHOD(device_shutdown, bus_generic_shutdown),
90
91 DEVMETHOD_END
92 };
93
94 static driver_t saf1761_otg_driver = {
95 .name = "saf1761otg",
96 .methods = saf1761_otg_methods,
97 .size = sizeof(struct saf1761_otg_softc),
98 };
99
100 static devclass_t saf1761_otg_devclass;
101
102 DRIVER_MODULE(saf1761otg, simplebus, saf1761_otg_driver, saf1761_otg_devclass, 0, 0);
103 MODULE_DEPEND(saf1761otg, usb, 1, 1, 1);
104
105 static int
saf1761_otg_fdt_probe(device_t dev)106 saf1761_otg_fdt_probe(device_t dev)
107 {
108 if (!ofw_bus_status_okay(dev))
109 return (ENXIO);
110
111 if (!ofw_bus_is_compatible(dev, "nxp,usb-isp1761"))
112 return (ENXIO);
113
114 device_set_desc(dev, "ISP1761/SAF1761 DCI USB 2.0 Device Controller");
115
116 return (0);
117 }
118
119 static int
saf1761_otg_fdt_attach(device_t dev)120 saf1761_otg_fdt_attach(device_t dev)
121 {
122 struct saf1761_otg_softc *sc = device_get_softc(dev);
123 char param[24];
124 int err;
125 int rid;
126
127 /* get configuration from FDT */
128
129 /* get bus-width, if any */
130 if (OF_getprop(ofw_bus_get_node(dev), "bus-width",
131 ¶m, sizeof(param)) > 0) {
132 param[sizeof(param) - 1] = 0;
133 if (strcmp(param, "32") == 0)
134 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH;
135 } else {
136 /* assume 32-bit data bus */
137 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH;
138 }
139
140 /* get analog over-current setting */
141 if (OF_getprop(ofw_bus_get_node(dev), "analog-oc",
142 ¶m, sizeof(param)) > 0) {
143 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_ANA_DIGI_OC;
144 }
145
146 /* get DACK polarity */
147 if (OF_getprop(ofw_bus_get_node(dev), "dack-polarity",
148 ¶m, sizeof(param)) > 0) {
149 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DACK_POL;
150 }
151
152 /* get DREQ polarity */
153 if (OF_getprop(ofw_bus_get_node(dev), "dreq-polarity",
154 ¶m, sizeof(param)) > 0) {
155 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DREQ_POL;
156 }
157
158 /* get IRQ polarity */
159 if (OF_getprop(ofw_bus_get_node(dev), "int-polarity",
160 ¶m, sizeof(param)) > 0) {
161 sc->sc_interrupt_cfg |= SOTG_INTERRUPT_CFG_INTPOL;
162 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_INTR_POL;
163 }
164
165 /* get IRQ level triggering */
166 if (OF_getprop(ofw_bus_get_node(dev), "int-level",
167 ¶m, sizeof(param)) > 0) {
168 sc->sc_interrupt_cfg |= SOTG_INTERRUPT_CFG_INTLVL;
169 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_INTR_LEVEL;
170 }
171
172 /* initialise some bus fields */
173 sc->sc_bus.parent = dev;
174 sc->sc_bus.devices = sc->sc_devices;
175 sc->sc_bus.devices_max = SOTG_MAX_DEVICES;
176 sc->sc_bus.dma_bits = 32;
177
178 /* get all DMA memory */
179 if (usb_bus_mem_alloc_all(&sc->sc_bus,
180 USB_GET_DMA_TAG(dev), NULL)) {
181 return (ENOMEM);
182 }
183 rid = 0;
184 sc->sc_io_res =
185 bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
186
187 if (sc->sc_io_res == NULL)
188 goto error;
189
190 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
191 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
192 sc->sc_io_size = rman_get_size(sc->sc_io_res);
193
194 /* try to allocate the HC interrupt first */
195 rid = 1;
196 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
197 RF_SHAREABLE | RF_ACTIVE);
198 if (sc->sc_irq_res == NULL) {
199 /* try to allocate a common IRQ second */
200 rid = 0;
201 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
202 RF_SHAREABLE | RF_ACTIVE);
203 if (sc->sc_irq_res == NULL)
204 goto error;
205 }
206
207 sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
208 if (sc->sc_bus.bdev == NULL)
209 goto error;
210
211 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
212
213 err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_TTY | INTR_MPSAFE,
214 &saf1761_otg_filter_interrupt, &saf1761_otg_interrupt, sc, &sc->sc_intr_hdl);
215 if (err) {
216 sc->sc_intr_hdl = NULL;
217 goto error;
218 }
219 err = saf1761_otg_init(sc);
220 if (err) {
221 device_printf(dev, "Init failed\n");
222 goto error;
223 }
224 err = device_probe_and_attach(sc->sc_bus.bdev);
225 if (err) {
226 device_printf(dev, "USB probe and attach failed\n");
227 goto error;
228 }
229 return (0);
230
231 error:
232 saf1761_otg_fdt_detach(dev);
233 return (ENXIO);
234 }
235
236 static int
saf1761_otg_fdt_detach(device_t dev)237 saf1761_otg_fdt_detach(device_t dev)
238 {
239 struct saf1761_otg_softc *sc = device_get_softc(dev);
240 int err;
241
242 /* during module unload there are lots of children leftover */
243 device_delete_children(dev);
244
245 if (sc->sc_irq_res && sc->sc_intr_hdl) {
246 /*
247 * Only call uninit() after init()
248 */
249 saf1761_otg_uninit(sc);
250
251 err = bus_teardown_intr(dev, sc->sc_irq_res,
252 sc->sc_intr_hdl);
253 sc->sc_intr_hdl = NULL;
254 }
255 if (sc->sc_irq_res) {
256 bus_release_resource(dev, SYS_RES_IRQ, 0,
257 sc->sc_irq_res);
258 sc->sc_irq_res = NULL;
259 }
260 if (sc->sc_io_res) {
261 bus_release_resource(dev, SYS_RES_MEMORY, 0,
262 sc->sc_io_res);
263 sc->sc_io_res = NULL;
264 }
265 usb_bus_mem_free_all(&sc->sc_bus, NULL);
266
267 return (0);
268 }
269