1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2010-2022 Hans Petter Selasky
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include "implementation/global_implementation.h"
29 __FBSDID("$FreeBSD$");
30 #include "controller/xhci.h"
31 #include "controller/xhcireg.h"
32
33 #include <linux/kernel.h>
34
35 #define PCI_XHCI_VENDORID_AMD 0x1022
36 #define PCI_XHCI_VENDORID_INTEL 0x8086
37
38 static device_probe_t xhci_pci_probe;
39 static device_attach_t xhci_pci_attach;
40 static device_detach_t xhci_pci_detach;
41 static usb_take_controller_t xhci_pci_take_controller;
42
43 static device_method_t xhci_device_methods[] = {
44 /* device interface */
45 DEVMETHOD(device_probe, xhci_pci_probe),
46 DEVMETHOD(device_attach, xhci_pci_attach),
47 DEVMETHOD(device_detach, xhci_pci_detach),
48 DEVMETHOD(device_suspend, bus_generic_suspend),
49 DEVMETHOD(device_resume, bus_generic_resume),
50 DEVMETHOD(device_shutdown, bus_generic_shutdown),
51 DEVMETHOD(usb_take_controller, xhci_pci_take_controller),
52
53 DEVMETHOD_END
54 };
55
56 static driver_t xhci_driver = {
57 .name = "xhci",
58 .methods = xhci_device_methods,
59 .size = sizeof(struct xhci_softc),
60 };
61
62 static devclass_t xhci_devclass;
63
64 DRIVER_MODULE(xhci, nexus, xhci_driver, xhci_devclass, NULL, NULL);
65
66 static const char *
xhci_pci_match(device_t self)67 xhci_pci_match(device_t self)
68 {
69 (void)self;
70 return ("XHCI (generic) USB 3.0 controller");
71 }
72
73 static int
xhci_pci_probe(device_t self)74 xhci_pci_probe(device_t self)
75 {
76 const char *desc = xhci_pci_match(self);
77
78 if (desc) {
79 device_set_desc(self, desc);
80 return (BUS_PROBE_DEFAULT);
81 } else {
82 return (ENXIO);
83 }
84 }
85
86 static int
xhci_pci_attach(device_t self)87 xhci_pci_attach(device_t self)
88 {
89 struct resource *res;
90 struct xhci_softc *sc = device_get_softc(self);;
91 int err = ENXIO;
92 uint8_t usedma32 = 0;
93 int unit = device_get_unit(self);
94
95 usb_debug("+\n");
96
97 res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &unit, 0);
98 if (res == NULL) {
99 goto error;
100 }
101
102 sc->sc_io_res = ioremap(res->start, res->count);
103 if (!sc->sc_io_res) {
104 goto error;
105 }
106 sc->sc_io_tag = (void *)sc->sc_io_res;
107 sc->sc_io_hdl = (uintptr_t)sc->sc_io_res;
108 sc->sc_io_size = res->count;
109 usb_debug("bus_setup_intr\n");
110
111 res = bus_alloc_resource_any(self, SYS_RES_IRQ, &unit, 0);
112 if (res == NULL) {
113 goto error;
114 }
115
116 sc->sc_irq_res = res;
117 err = bus_setup_intr(res->start, 0, (driver_intr_t *)xhci_interrupt, sc);
118 if (err) {
119 goto error;
120 }
121
122 usb_debug("xhci_init\n");
123 HiUsb3StartHcd();
124 if (xhci_init(sc, self, usedma32)) {
125 device_printf(self, "Could not initialize softc\n");
126 goto error;
127 }
128 callout_init_mtx(&sc->sc_callout, &sc->sc_bus.bus_mtx, 0);
129
130 usb_debug("add child to usbus\n");
131 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
132 if (sc->sc_bus.bdev == NULL) {
133 device_printf(self, "Could not add USB device\n");
134 goto error;
135 }
136 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
137
138 usb_debug("xhci halt and start controller\n");
139 err = xhci_halt_controller(sc);
140
141 if (err == 0)
142 err = xhci_start_controller(sc);
143
144 usb_debug("device_probe_and_attach bus_dev\n");
145 if (err == 0)
146 err = device_probe_and_attach(sc->sc_bus.bdev);
147
148 if (err) {
149 device_printf(self, "XHCI halt/start/probe failed err=%d\n", err);
150 goto error;
151 }
152
153 usb_debug("-\n");
154
155 return (0);
156 error:
157 device_printf(self, "XHCI halt/start/probe failed err=%d\n", err);
158 if (sc) {
159 iounmap((void *)sc->sc_io_res);
160 }
161 (void)xhci_pci_detach(self);
162 return (err);
163 }
164
165 static int
xhci_pci_detach(device_t self)166 xhci_pci_detach(device_t self)
167 {
168
169 struct xhci_softc *sc = device_get_softc(self);
170
171 /* during module unload there are lots of children leftover */
172 (void)device_delete_children(self);
173
174 callout_drain(&sc->sc_callout);
175 (void)xhci_halt_controller(sc);
176 (void)xhci_reset_controller(sc);
177
178 // release resouce
179 if (sc->sc_irq_res) {
180 (void)bus_teardown_intr(sc->sc_irq_res->start, sc);
181 sc->sc_irq_res = NULL;
182 }
183 if (sc->sc_io_res) {
184 iounmap((void *)sc->sc_io_res);
185 sc->sc_io_res = NULL;
186 sc->sc_io_tag = NULL;
187 sc->sc_io_hdl = (uintptr_t)NULL;
188 sc->sc_io_size = 0;
189 }
190
191 xhci_uninit(sc);
192
193 return (0);
194 }
195
196 static int
xhci_pci_take_controller(device_t self)197 xhci_pci_take_controller(device_t self)
198 {
199 struct xhci_softc *sc = device_get_softc(self);
200 uint32_t cparams;
201 uint32_t eecp;
202 uint32_t eec;
203 uint16_t to;
204 uint8_t bios_sem;
205
206 cparams = XREAD4(sc, capa, XHCI_HCSPARAMS0);
207
208 eec = (uint32_t)(-1);
209
210 /* Synchronise with the BIOS if it owns the controller. */
211 for (eecp = XHCI_HCS0_XECP(cparams) << 2; (eecp != 0) && XHCI_XECP_NEXT(eec);
212 eecp += (XHCI_XECP_NEXT(eec) << 2)) {
213 eec = XREAD4(sc, capa, eecp);
214
215 if (XHCI_XECP_ID(eec) != XHCI_ID_USB_LEGACY)
216 continue;
217 bios_sem = XREAD1(sc, capa, eecp +
218 XHCI_XECP_BIOS_SEM);
219 if (bios_sem == 0)
220 continue;
221 device_printf(sc->sc_bus.bdev, "waiting for BIOS "
222 "to give up control\n");
223 XWRITE1(sc, capa, eecp +
224 XHCI_XECP_OS_SEM, 1);
225 to = 500;
226 while (1) {
227 bios_sem = XREAD1(sc, capa, eecp +
228 XHCI_XECP_BIOS_SEM);
229 if (bios_sem == 0)
230 break;
231
232 if (--to == 0) {
233 device_printf(sc->sc_bus.bdev,
234 "timed out waiting for BIOS\n");
235 break;
236 }
237 usb_pause_mtx(NULL, hz / 100); /* wait 10ms */
238 }
239 }
240 return (0);
241 }
242
243 int
hixhci_init(void)244 hixhci_init(void)
245 {
246 DPRINTF("hixhci_init");
247 return driver_module_handler(NULL, MOD_LOAD, &xhci_nexus_driver_mod);
248 }
249
250 void
hixhci_exit(void)251 hixhci_exit(void)
252 {
253 DPRINTF("hixhci_exit");
254 (void)driver_module_handler(NULL, MOD_UNLOAD, &xhci_nexus_driver_mod);
255 }
256