1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
5 * Copyright (c) 2010, Aleksandr Rybalko <ray@ddteam.net>
6 * All rights reserved.
7 *
8 * Developed by Semihalf.
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 * 3. Neither the name of MARVELL nor the names of contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 /*
39 * BHND attachment driver for the USB Enhanced Host Controller.
40 * Ported from ZRouter with insignificant adaptations for FreeBSD11.
41 */
42
43 #include "opt_bus.h"
44
45 #include <sys/stdint.h>
46 #include <sys/stddef.h>
47 #include <sys/param.h>
48 #include <sys/queue.h>
49 #include <sys/rman.h>
50 #include <sys/types.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/bus.h>
54 #include <sys/linker_set.h>
55 #include <sys/module.h>
56 #include <sys/lock.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/sx.h>
61 #include <sys/unistd.h>
62 #include <sys/callout.h>
63 #include <sys/malloc.h>
64 #include <sys/priv.h>
65
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68
69 #include <dev/usb/usb_core.h>
70 #include <dev/usb/usb_busdma.h>
71 #include <dev/usb/usb_process.h>
72 #include <dev/usb/usb_util.h>
73
74 #include <dev/usb/usb_controller.h>
75 #include <dev/usb/usb_bus.h>
76 #include <dev/usb/controller/ehci.h>
77 #include <dev/usb/controller/ehcireg.h>
78
79 #include <dev/bhnd/bhnd.h>
80
81 #define EHCI_HC_DEVSTR "Broadcom EHCI"
82
83 #define USB_BRIDGE_INTR_CAUSE 0x210
84 #define USB_BRIDGE_INTR_MASK 0x214
85
86 static device_attach_t bhnd_ehci_attach;
87 static device_detach_t bhnd_ehci_detach;
88
89 static int bhnd_ehci_probe(device_t self);
90 static void bhnd_ehci_post_reset(struct ehci_softc *ehci_softc);
91
92 static int
93 bhnd_ehci_probe(device_t self)
94 {
95
96 device_set_desc(self, EHCI_HC_DEVSTR);
97
98 return (BUS_PROBE_DEFAULT);
99 }
100
101 static void
102 bhnd_ehci_post_reset(struct ehci_softc *ehci_softc)
103 {
104 uint32_t usbmode;
105
106 /* Force HOST mode */
107 usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_NOLPM);
108 usbmode &= ~EHCI_UM_CM;
109 usbmode |= EHCI_UM_CM_HOST;
110 EOWRITE4(ehci_softc, EHCI_USBMODE_NOLPM, usbmode);
111 }
112
113 static int
114 bhnd_ehci_attach(device_t self)
115 {
116 ehci_softc_t *sc;
117 int err;
118 int rid;
119
120 sc = device_get_softc(self);
121 /* initialise some bus fields */
122 sc->sc_bus.parent = self;
123 sc->sc_bus.devices = sc->sc_devices;
124 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
125 sc->sc_bus.usbrev = USB_REV_2_0;
126 sc->sc_bus.dma_bits = 32;
127
128 /* get all DMA memory */
129 if ((err = usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
130 &ehci_iterate_hw_softc)) != 0) {
131 BHND_ERROR_DEV(self, "can't allocate DMA memory: %d", err);
132 return (ENOMEM);
133 }
134
135 rid = 0;
136 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
137 RF_ACTIVE);
138 if (!sc->sc_io_res) {
139 BHND_ERROR_DEV(self, "Could not map memory");
140 goto error;
141 }
142 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
143 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
144 sc->sc_io_size = rman_get_size(sc->sc_io_res);
145
146 rid = 0;
147 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
148 RF_SHAREABLE | RF_ACTIVE);
149
150 if (sc->sc_irq_res == NULL) {
151 BHND_ERROR_DEV(self, "Could not allocate error irq");
152 bhnd_ehci_detach(self);
153 return (ENXIO);
154 }
155
156 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
157 if (!sc->sc_bus.bdev) {
158 BHND_ERROR_DEV(self, "Could not add USB device");
159 goto error;
160 }
161 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
162 device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR);
163
164 sprintf(sc->sc_vendor, "Broadcom");
165
166 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
167 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
168 if (err) {
169 BHND_ERROR_DEV(self, "Could not setup irq, %d", err);
170 sc->sc_intr_hdl = NULL;
171 goto error;
172 }
173
174 sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
175 sc->sc_vendor_post_reset = bhnd_ehci_post_reset;
176
177 err = ehci_init(sc);
178 if (!err) {
179 err = device_probe_and_attach(sc->sc_bus.bdev);
180 }
181 if (err) {
182 BHND_ERROR_DEV(self, "USB init failed err=%d", err);
183 goto error;
184 }
185 return (0);
186
187 error:
188 bhnd_ehci_detach(self);
189 return (ENXIO);
190 }
191
192 static int
193 bhnd_ehci_detach(device_t self)
194 {
195 ehci_softc_t *sc;
196 int err;
197
198 sc = device_get_softc(self);
199
200 /* during module unload there are lots of children leftover */
201 device_delete_children(self);
202
203 /*
204 * disable interrupts that might have been switched on in ehci_init
205 */
206 #ifdef notyet
207 if (sc->sc_io_res) {
208 EWRITE4(sc, EHCI_USBINTR, 0);
209 EWRITE4(sc, USB_BRIDGE_INTR_MASK, 0);
210 }
211 #endif
212 if (sc->sc_irq_res && sc->sc_intr_hdl) {
213 /*
214 * only call ehci_detach() after ehci_init()
215 */
216 ehci_detach(sc);
217
218 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
219
220 if (err)
221 /* XXX or should we panic? */
222 BHND_ERROR_DEV(self, "Could not tear down irq, %d", err);
223
224 sc->sc_intr_hdl = NULL;
225 }
226 if (sc->sc_irq_res) {
227 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
228 sc->sc_irq_res = NULL;
229 }
230 if (sc->sc_io_res) {
231 bus_release_resource(self, SYS_RES_MEMORY, 0, sc->sc_io_res);
232 sc->sc_io_res = NULL;
233 }
234 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
235
236 return (0);
237 }
238
239 static device_method_t ehci_methods[] = {
240 /* Device interface */
241 DEVMETHOD(device_probe, bhnd_ehci_probe),
242 DEVMETHOD(device_attach, bhnd_ehci_attach),
243 DEVMETHOD(device_detach, bhnd_ehci_detach),
244 DEVMETHOD(device_suspend, bus_generic_suspend),
245 DEVMETHOD(device_resume, bus_generic_resume),
246 DEVMETHOD(device_shutdown, bus_generic_shutdown),
247
248 /* Bus interface */
249 DEVMETHOD(bus_print_child, bus_generic_print_child),
250 {0, 0}
251 };
252
253 static driver_t ehci_driver = {
254 "ehci",
255 ehci_methods,
256 sizeof(ehci_softc_t),
257 };
258
259 DRIVER_MODULE(ehci, bhnd_usb, ehci_driver, 0, 0);
260 MODULE_DEPEND(ehci, usb, 1, 1, 1);
Cache object: 1a1dd21f5f63aa50ffc0a7a8654005be
|