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
octusb_octeon_identify(driver_t * drv,device_t parent)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
octusb_octeon_probe(device_t dev)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
octusb_octeon_attach(device_t dev)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 	err = octusb_init(&sc->sc_dci);
147 	if (!err) {
148 		err = device_probe_and_attach(sc->sc_dci.sc_bus.bdev);
149 	}
150 	if (err) {
151 		goto error;
152 	}
153 	return (0);
154 
155 error:
156 	octusb_octeon_detach(dev);
157 	return (ENXIO);
158 }
159 
160 static int
octusb_octeon_detach(device_t dev)161 octusb_octeon_detach(device_t dev)
162 {
163 	struct octusb_octeon_softc *sc = device_get_softc(dev);
164 	int err;
165 	int nports;
166 	int i;
167 
168 	/* during module unload there are lots of children leftover */
169 	device_delete_children(dev);
170 
171 	if (sc->sc_dci.sc_irq_res[0] && sc->sc_dci.sc_intr_hdl[0])
172 		/*
173 	 	 * only call octusb_octeon_uninit() after octusb_octeon_init()
174 	 	 */
175 		octusb_uninit(&sc->sc_dci);
176 
177 	nports = cvmx_usb_get_num_ports();
178 	if (nports > OCTUSB_MAX_PORTS)
179 		panic("octusb: too many USB ports %d", nports);
180 	for (i = 0; i < nports; i++) {
181 		if (sc->sc_dci.sc_irq_res[i] && sc->sc_dci.sc_intr_hdl[i]) {
182 			err = bus_teardown_intr(dev, sc->sc_dci.sc_irq_res[i],
183 			    sc->sc_dci.sc_intr_hdl[i]);
184 			sc->sc_dci.sc_intr_hdl[i] = NULL;
185 		}
186 		if (sc->sc_dci.sc_irq_res[i]) {
187 			bus_release_resource(dev, SYS_RES_IRQ, 0,
188 			    sc->sc_dci.sc_irq_res[i]);
189 			sc->sc_dci.sc_irq_res[i] = NULL;
190 		}
191 	}
192 	usb_bus_mem_free_all(&sc->sc_dci.sc_bus, NULL);
193 
194 	return (0);
195 }
196 
197 static device_method_t octusb_octeon_methods[] = {
198 	/* Device interface */
199 	DEVMETHOD(device_identify, octusb_octeon_identify),
200 	DEVMETHOD(device_probe, octusb_octeon_probe),
201 	DEVMETHOD(device_attach, octusb_octeon_attach),
202 	DEVMETHOD(device_detach, octusb_octeon_detach),
203 	DEVMETHOD(device_resume, bus_generic_resume),
204 	DEVMETHOD(device_suspend, bus_generic_suspend),
205 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
206 
207 	DEVMETHOD_END
208 };
209 
210 static driver_t octusb_octeon_driver = {
211 	.name = "octusb",
212 	.methods = octusb_octeon_methods,
213 	.size = sizeof(struct octusb_octeon_softc),
214 };
215 
216 static devclass_t octusb_octeon_devclass;
217 
218 DRIVER_MODULE(octusb, ciu, octusb_octeon_driver, octusb_octeon_devclass, 0, 0);
219