xref: /freebsd/sys/dev/bhnd/cores/usb/bhnd_ohci.c (revision bdd1243d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * Copyright (c) 2010, Aleksandr Rybalko <ray@ddteam.net>
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (augustss@carlstedt.se) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 /*
38  * USB Open Host Controller driver.
39  *
40  * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
41  */
42 
43 /* The low level controller code for OHCI has been split into
44  * SIBA probes and OHCI specific code. This was done to facilitate the
45  * sharing of code between *BSD's
46  */
47 
48 #include <sys/stdint.h>
49 #include <sys/stddef.h>
50 #include <sys/param.h>
51 #include <sys/queue.h>
52 #include <sys/rman.h>
53 #include <sys/types.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/bus.h>
57 #include <sys/linker_set.h>
58 #include <sys/module.h>
59 #include <sys/lock.h>
60 #include <sys/mutex.h>
61 #include <sys/condvar.h>
62 #include <sys/sysctl.h>
63 #include <sys/sx.h>
64 #include <sys/unistd.h>
65 #include <sys/callout.h>
66 #include <sys/malloc.h>
67 #include <sys/priv.h>
68 
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbdi.h>
71 
72 #include <dev/usb/usb_core.h>
73 #include <dev/usb/usb_busdma.h>
74 #include <dev/usb/usb_process.h>
75 #include <dev/usb/usb_util.h>
76 
77 #include <dev/usb/usb_controller.h>
78 #include <dev/usb/usb_bus.h>
79 #include <dev/usb/controller/ohci.h>
80 #include <dev/usb/controller/ohcireg.h>
81 
82 static device_probe_t bhnd_ohci_probe;
83 static device_attach_t bhnd_ohci_attach;
84 static device_detach_t bhnd_ohci_detach;
85 
86 static int
87 bhnd_ohci_probe(device_t self)
88 {
89 	device_set_desc(self, "Broadcom OHCI");
90 	return (0);
91 }
92 
93 static int
94 bhnd_ohci_attach(device_t self)
95 {
96 	ohci_softc_t	*sc;
97 	int		 rid;
98 	int		 err;
99 
100 	sc = device_get_softc(self);
101 	/* initialise some bus fields */
102 	sc->sc_bus.parent = self;
103 	sc->sc_bus.devices = sc->sc_devices;
104 	sc->sc_bus.devices_max = OHCI_MAX_DEVICES;
105 	sc->sc_bus.dma_bits = 32;
106 
107 	/* get all DMA memory */
108 	if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
109 	    &ohci_iterate_hw_softc)) {
110 		return (ENOMEM);
111 	}
112 	sc->sc_dev = self;
113 
114 	rid = 0;
115 	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
116 	    RF_ACTIVE);
117 	if (!sc->sc_io_res) {
118 		device_printf(self, "Could not map memory\n");
119 		goto error;
120 	}
121 	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
122 	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
123 	sc->sc_io_size = rman_get_size(sc->sc_io_res);
124 
125 	rid = 0;
126 	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
127 	    RF_SHAREABLE | RF_ACTIVE);
128 	if (sc->sc_irq_res == NULL) {
129 		device_printf(self, "Could not allocate irq\n");
130 		goto error;
131 	}
132 	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
133 	if (!sc->sc_bus.bdev) {
134 		device_printf(self, "Could not add USB device\n");
135 		goto error;
136 	}
137 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
138 
139 	strlcpy(sc->sc_vendor, "Broadcom", sizeof(sc->sc_vendor));
140 
141 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
142 	    NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
143 
144 	if (err) {
145 		device_printf(self, "Could not setup irq, %d\n", err);
146 		sc->sc_intr_hdl = NULL;
147 		goto error;
148 	}
149 	err = ohci_init(sc);
150 	if (!err) {
151 		err = device_probe_and_attach(sc->sc_bus.bdev);
152 	}
153 	if (err) {
154 		device_printf(self, "USB init failed\n");
155 		goto error;
156 	}
157 	return (0);
158 
159 error:
160 	bhnd_ohci_detach(self);
161 	return (ENXIO);
162 }
163 
164 static int
165 bhnd_ohci_detach(device_t self)
166 {
167 	ohci_softc_t	*sc;
168 
169 	sc = device_get_softc(self);
170 
171 	/* during module unload there are lots of children leftover */
172 	device_delete_children(self);
173 
174 	if (sc->sc_irq_res && sc->sc_intr_hdl) {
175 		/*
176 		 * only call ohci_detach() after ohci_init()
177 		 */
178 		ohci_detach(sc);
179 
180 		int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
181 
182 		if (err) {
183 			/* XXX or should we panic? */
184 			device_printf(self, "Could not tear down irq, %d\n",
185 			    err);
186 		}
187 		sc->sc_intr_hdl = NULL;
188 	}
189 	if (sc->sc_irq_res) {
190 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
191 		sc->sc_irq_res = NULL;
192 	}
193 	if (sc->sc_io_res) {
194 		bus_release_resource(self, SYS_RES_MEMORY, 0,
195 		    sc->sc_io_res);
196 		sc->sc_io_res = NULL;
197 	}
198 	usb_bus_mem_free_all(&sc->sc_bus, &ohci_iterate_hw_softc);
199 
200 	return (0);
201 }
202 
203 static device_method_t bhnd_ohci_methods[] = {
204 	/* Device interface */
205 	DEVMETHOD(device_probe,		bhnd_ohci_probe),
206 	DEVMETHOD(device_attach,	bhnd_ohci_attach),
207 	DEVMETHOD(device_detach,	bhnd_ohci_detach),
208 	DEVMETHOD(device_suspend,	bus_generic_suspend),
209 	DEVMETHOD(device_resume,	bus_generic_resume),
210 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
211 
212 	DEVMETHOD_END
213 };
214 
215 static driver_t ohci_driver = {
216 	.name = "ohci",
217 	.methods = bhnd_ohci_methods,
218 	.size = sizeof(struct ohci_softc),
219 };
220 
221 DRIVER_MODULE(ohci, bhnd_usb, ohci_driver, 0, 0);
222 MODULE_DEPEND(ohci, usb, 1, 1, 1);
223