]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/cavium/usb/octusb_octeon.c
Upgrade to OpenSSH 7.7p1.
[FreeBSD/FreeBSD.git] / sys / mips / cavium / usb / octusb_octeon.c
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3
4 /*-
5  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6  *
7  * Copyright (c) 2007-2008 Hans Petter Selasky. All rights reserved.
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 #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>
38 #include <sys/bus.h>
39 #include <sys/module.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
44 #include <sys/sx.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/priv.h>
49 #include <sys/rman.h>
50
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
53
54 #include <dev/usb/usb_core.h>
55 #include <dev/usb/usb_busdma.h>
56 #include <dev/usb/usb_process.h>
57 #include <dev/usb/usb_util.h>
58
59 #include <dev/usb/usb_controller.h>
60 #include <dev/usb/usb_bus.h>
61
62 #include <contrib/octeon-sdk/cvmx.h>
63 #include <mips/cavium/octeon_irq.h>
64 #include <contrib/octeon-sdk/cvmx-usb.h>
65
66 #include <mips/cavium/usb/octusb.h>
67
68 #define MEM_RID 0
69
70 static device_identify_t octusb_octeon_identify;
71 static device_probe_t octusb_octeon_probe;
72 static device_attach_t octusb_octeon_attach;
73 static device_detach_t octusb_octeon_detach;
74
75 struct octusb_octeon_softc {
76         struct octusb_softc sc_dci;     /* must be first */
77 };
78
79 static void
80 octusb_octeon_identify(driver_t *drv, device_t parent)
81 {
82         if (octeon_has_feature(OCTEON_FEATURE_USB))
83                 BUS_ADD_CHILD(parent, 0, "octusb", 0);
84 }
85
86 static int
87 octusb_octeon_probe(device_t dev)
88 {
89         device_set_desc(dev, "Cavium Octeon USB controller");
90         return (0);
91 }
92
93 static int
94 octusb_octeon_attach(device_t dev)
95 {
96         struct octusb_octeon_softc *sc = device_get_softc(dev);
97         int err;
98         int rid;
99         int nports;
100         int i;
101
102         /* setup controller interface softc */
103
104         /* initialise some bus fields */
105         sc->sc_dci.sc_bus.parent = dev;
106         sc->sc_dci.sc_bus.devices = sc->sc_dci.sc_devices;
107         sc->sc_dci.sc_bus.devices_max = OCTUSB_MAX_DEVICES;
108         sc->sc_dci.sc_bus.dma_bits = 32;
109
110         /* get all DMA memory */
111         if (usb_bus_mem_alloc_all(&sc->sc_dci.sc_bus,
112             USB_GET_DMA_TAG(dev), NULL)) {
113                 return (ENOMEM);
114         }
115         nports = cvmx_usb_get_num_ports();
116         if (nports > OCTUSB_MAX_PORTS)
117                 panic("octusb: too many USB ports %d", nports);
118         for (i = 0; i < nports; i++) {
119                 rid = 0;
120                 sc->sc_dci.sc_irq_res[i] =
121                     bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
122                                OCTEON_IRQ_USB0 + i, OCTEON_IRQ_USB0 + i, 1, RF_ACTIVE);
123                 if (!(sc->sc_dci.sc_irq_res[i])) {
124                         goto error;
125                 }
126
127 #if (__FreeBSD_version >= 700031)
128                 err = bus_setup_intr(dev, sc->sc_dci.sc_irq_res[i], INTR_TYPE_BIO | INTR_MPSAFE,
129                     NULL, (driver_intr_t *)octusb_interrupt, sc, &sc->sc_dci.sc_intr_hdl[i]);
130 #else
131                 err = bus_setup_intr(dev, sc->sc_dci.sc_irq_res[i], INTR_TYPE_BIO | INTR_MPSAFE,
132                     (driver_intr_t *)octusb_interrupt, sc, &sc->sc_dci.sc_intr_hdl[i]);
133 #endif
134                 if (err) {
135                         sc->sc_dci.sc_intr_hdl[i] = NULL;
136                         goto error;
137                 }
138         }
139
140         sc->sc_dci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
141         if (!(sc->sc_dci.sc_bus.bdev)) {
142                 goto error;
143         }
144         device_set_ivars(sc->sc_dci.sc_bus.bdev, &sc->sc_dci.sc_bus);
145
146
147         err = octusb_init(&sc->sc_dci);
148         if (!err) {
149                 err = device_probe_and_attach(sc->sc_dci.sc_bus.bdev);
150         }
151         if (err) {
152                 goto error;
153         }
154         return (0);
155
156 error:
157         octusb_octeon_detach(dev);
158         return (ENXIO);
159 }
160
161 static int
162 octusb_octeon_detach(device_t dev)
163 {
164         struct octusb_octeon_softc *sc = device_get_softc(dev);
165         int err;
166         int nports;
167         int i;
168
169         /* during module unload there are lots of children leftover */
170         device_delete_children(dev);
171
172         if (sc->sc_dci.sc_irq_res[0] && sc->sc_dci.sc_intr_hdl[0])
173                 /*
174                  * only call octusb_octeon_uninit() after octusb_octeon_init()
175                  */
176                 octusb_uninit(&sc->sc_dci);
177
178         nports = cvmx_usb_get_num_ports();
179         if (nports > OCTUSB_MAX_PORTS)
180                 panic("octusb: too many USB ports %d", nports);
181         for (i = 0; i < nports; i++) {
182                 if (sc->sc_dci.sc_irq_res[0] && sc->sc_dci.sc_intr_hdl[0]) {
183                         err = bus_teardown_intr(dev, sc->sc_dci.sc_irq_res[i],
184                             sc->sc_dci.sc_intr_hdl[i]);
185                         sc->sc_dci.sc_intr_hdl[i] = NULL;
186                 }
187                 if (sc->sc_dci.sc_irq_res) {
188                         bus_release_resource(dev, SYS_RES_IRQ, 0,
189                             sc->sc_dci.sc_irq_res[i]);
190                         sc->sc_dci.sc_irq_res[i] = NULL;
191                 }
192         }
193         usb_bus_mem_free_all(&sc->sc_dci.sc_bus, NULL);
194
195         return (0);
196 }
197
198 static device_method_t octusb_octeon_methods[] = {
199         /* Device interface */
200         DEVMETHOD(device_identify, octusb_octeon_identify),
201         DEVMETHOD(device_probe, octusb_octeon_probe),
202         DEVMETHOD(device_attach, octusb_octeon_attach),
203         DEVMETHOD(device_detach, octusb_octeon_detach),
204         DEVMETHOD(device_resume, bus_generic_resume),
205         DEVMETHOD(device_suspend, bus_generic_suspend),
206         DEVMETHOD(device_shutdown, bus_generic_shutdown),
207
208         DEVMETHOD_END
209 };
210
211 static driver_t octusb_octeon_driver = {
212         .name = "octusb",
213         .methods = octusb_octeon_methods,
214         .size = sizeof(struct octusb_octeon_softc),
215 };
216
217 static devclass_t octusb_octeon_devclass;
218
219 DRIVER_MODULE(octusb, ciu, octusb_octeon_driver, octusb_octeon_devclass, 0, 0);