5 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7 * Copyright (c) 2007-2008 Hans Petter Selasky. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
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>
39 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
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>
59 #include <dev/usb/usb_controller.h>
60 #include <dev/usb/usb_bus.h>
62 #include <contrib/octeon-sdk/cvmx.h>
63 #include <mips/cavium/octeon_irq.h>
64 #include <contrib/octeon-sdk/cvmx-usb.h>
66 #include <mips/cavium/usb/octusb.h>
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;
75 struct octusb_octeon_softc {
76 struct octusb_softc sc_dci; /* must be first */
80 octusb_octeon_identify(driver_t *drv, device_t parent)
82 if (octeon_has_feature(OCTEON_FEATURE_USB))
83 BUS_ADD_CHILD(parent, 0, "octusb", 0);
87 octusb_octeon_probe(device_t dev)
89 device_set_desc(dev, "Cavium Octeon USB controller");
94 octusb_octeon_attach(device_t dev)
96 struct octusb_octeon_softc *sc = device_get_softc(dev);
102 /* setup controller interface softc */
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;
110 /* get all DMA memory */
111 if (usb_bus_mem_alloc_all(&sc->sc_dci.sc_bus,
112 USB_GET_DMA_TAG(dev), NULL)) {
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++) {
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])) {
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]);
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]);
135 sc->sc_dci.sc_intr_hdl[i] = NULL;
140 sc->sc_dci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
141 if (!(sc->sc_dci.sc_bus.bdev)) {
144 device_set_ivars(sc->sc_dci.sc_bus.bdev, &sc->sc_dci.sc_bus);
147 err = octusb_init(&sc->sc_dci);
149 err = device_probe_and_attach(sc->sc_dci.sc_bus.bdev);
157 octusb_octeon_detach(dev);
162 octusb_octeon_detach(device_t dev)
164 struct octusb_octeon_softc *sc = device_get_softc(dev);
169 /* during module unload there are lots of children leftover */
170 device_delete_children(dev);
172 if (sc->sc_dci.sc_irq_res[0] && sc->sc_dci.sc_intr_hdl[0])
174 * only call octusb_octeon_uninit() after octusb_octeon_init()
176 octusb_uninit(&sc->sc_dci);
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;
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;
193 usb_bus_mem_free_all(&sc->sc_dci.sc_bus, NULL);
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),
211 static driver_t octusb_octeon_driver = {
213 .methods = octusb_octeon_methods,
214 .size = sizeof(struct octusb_octeon_softc),
217 static devclass_t octusb_octeon_devclass;
219 DRIVER_MODULE(octusb, ciu, octusb_octeon_driver, octusb_octeon_devclass, 0, 0);