]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/controller/saf1761_otg_fdt.c
Import tzdata 2018c
[FreeBSD/FreeBSD.git] / sys / dev / usb / controller / saf1761_otg_fdt.c
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2014 Hans Petter Selasky <hselasky@FreeBSD.org>
4  * All rights reserved.
5  *
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.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31
32 #ifdef USB_GLOBAL_INCLUDE_FILE
33 #include USB_GLOBAL_INCLUDE_FILE
34 #else
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>
42 #include <sys/bus.h>
43 #include <sys/module.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/condvar.h>
47 #include <sys/sysctl.h>
48 #include <sys/sx.h>
49 #include <sys/unistd.h>
50 #include <sys/callout.h>
51 #include <sys/malloc.h>
52 #include <sys/priv.h>
53 #include <sys/rman.h>
54
55 #include <dev/fdt/fdt_common.h>
56
57 #include <dev/ofw/openfirm.h>
58 #include <dev/ofw/ofw_bus.h>
59 #include <dev/ofw/ofw_bus_subr.h>
60
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbdi.h>
63
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>
71
72 #include <dev/usb/usb_controller.h>
73 #include <dev/usb/usb_bus.h>
74 #endif                                  /* USB_GLOBAL_INCLUDE_FILE */
75
76 #include <dev/usb/controller/saf1761_otg.h>
77 #include <dev/usb/controller/saf1761_otg_reg.h>
78
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;
82
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),
91
92         DEVMETHOD_END
93 };
94
95 static driver_t saf1761_otg_driver = {
96         .name = "saf1761otg",
97         .methods = saf1761_otg_methods,
98         .size = sizeof(struct saf1761_otg_softc),
99 };
100
101 static devclass_t saf1761_otg_devclass;
102
103 DRIVER_MODULE(saf1761otg, simplebus, saf1761_otg_driver, saf1761_otg_devclass, 0, 0);
104 MODULE_DEPEND(saf1761otg, usb, 1, 1, 1);
105
106 static int
107 saf1761_otg_fdt_probe(device_t dev)
108 {
109         if (!ofw_bus_status_okay(dev))
110                 return (ENXIO);
111
112         if (!ofw_bus_is_compatible(dev, "nxp,usb-isp1761"))
113                 return (ENXIO);
114
115         device_set_desc(dev, "ISP1761/SAF1761 DCI USB 2.0 Device Controller");
116
117         return (0);
118 }
119
120 static int
121 saf1761_otg_fdt_attach(device_t dev)
122 {
123         struct saf1761_otg_softc *sc = device_get_softc(dev);
124         char param[24];
125         int err;
126         int rid;
127
128         /* get configuration from FDT */
129
130         /* get bus-width, if any */
131         if (OF_getprop(ofw_bus_get_node(dev), "bus-width",
132             &param, 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;
136         } else {
137                 /* assume 32-bit data bus */
138                 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH;
139         }
140
141         /* get analog over-current setting */
142         if (OF_getprop(ofw_bus_get_node(dev), "analog-oc",
143             &param, sizeof(param)) > 0) {
144                 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_ANA_DIGI_OC;
145         }
146
147         /* get DACK polarity */
148         if (OF_getprop(ofw_bus_get_node(dev), "dack-polarity",
149             &param, sizeof(param)) > 0) {
150                 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DACK_POL;
151         }
152
153         /* get DREQ polarity */
154         if (OF_getprop(ofw_bus_get_node(dev), "dreq-polarity",
155             &param, sizeof(param)) > 0) {
156                 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DREQ_POL;
157         }
158
159         /* get IRQ polarity */
160         if (OF_getprop(ofw_bus_get_node(dev), "int-polarity",
161             &param, sizeof(param)) > 0) {
162                 sc->sc_interrupt_cfg |= SOTG_INTERRUPT_CFG_INTPOL;
163                 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_INTR_POL;
164         }
165
166         /* get IRQ level triggering */
167         if (OF_getprop(ofw_bus_get_node(dev), "int-level",
168             &param, sizeof(param)) > 0) {
169                 sc->sc_interrupt_cfg |= SOTG_INTERRUPT_CFG_INTLVL;
170                 sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_INTR_LEVEL;
171         }
172
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;
178
179         /* get all DMA memory */
180         if (usb_bus_mem_alloc_all(&sc->sc_bus,
181             USB_GET_DMA_TAG(dev), NULL)) {
182                 return (ENOMEM);
183         }
184         rid = 0;
185         sc->sc_io_res =
186             bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
187
188         if (sc->sc_io_res == NULL) 
189                 goto error;
190
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);
194
195         /* try to allocate the HC interrupt first */
196         rid = 1;
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 */
201                 rid = 0;
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)
205                         goto error;
206         }
207
208         sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
209         if (sc->sc_bus.bdev == NULL)
210                 goto error;
211
212         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
213
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);
216         if (err) {
217                 sc->sc_intr_hdl = NULL;
218                 goto error;
219         }
220         err = saf1761_otg_init(sc);
221         if (err) {
222                 device_printf(dev, "Init failed\n");
223                 goto error;
224         }
225         err = device_probe_and_attach(sc->sc_bus.bdev);
226         if (err) {
227                 device_printf(dev, "USB probe and attach failed\n");
228                 goto error;
229         }
230         return (0);
231
232 error:
233         saf1761_otg_fdt_detach(dev);
234         return (ENXIO);
235 }
236
237 static int
238 saf1761_otg_fdt_detach(device_t dev)
239 {
240         struct saf1761_otg_softc *sc = device_get_softc(dev);
241         int err;
242
243         /* during module unload there are lots of children leftover */
244         device_delete_children(dev);
245
246         if (sc->sc_irq_res && sc->sc_intr_hdl) {
247                 /*
248                  * Only call uninit() after init()
249                  */
250                 saf1761_otg_uninit(sc);
251
252                 err = bus_teardown_intr(dev, sc->sc_irq_res,
253                     sc->sc_intr_hdl);
254                 sc->sc_intr_hdl = NULL;
255         }
256         if (sc->sc_irq_res) {
257                 bus_release_resource(dev, SYS_RES_IRQ, 0,
258                     sc->sc_irq_res);
259                 sc->sc_irq_res = NULL;
260         }
261         if (sc->sc_io_res) {
262                 bus_release_resource(dev, SYS_RES_MEMORY, 0,
263                     sc->sc_io_res);
264                 sc->sc_io_res = NULL;
265         }
266         usb_bus_mem_free_all(&sc->sc_bus, NULL);
267
268         return (0);
269 }