• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
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: releng/12.2/sys/dev/usb/controller/xhci_pci.c 358016 2020-02-17 09:57:03Z hselasky $");
30 #include "controller/xhci.h"
31 #include "controller/xhcireg.h"
32 
33 #include <linux/kernel.h>
34 
35 static device_probe_t xhci_pci_probe;
36 static device_attach_t xhci_pci_attach;
37 static device_detach_t xhci_pci_detach;
38 static usb_take_controller_t xhci_pci_take_controller;
39 
40 static device_method_t xhci_device_methods[] = {
41         /* device interface */
42         DEVMETHOD(device_probe, xhci_pci_probe),
43         DEVMETHOD(device_attach, xhci_pci_attach),
44         DEVMETHOD(device_detach, xhci_pci_detach),
45         DEVMETHOD(device_suspend, bus_generic_suspend),
46         DEVMETHOD(device_resume, bus_generic_resume),
47         DEVMETHOD(device_shutdown, bus_generic_shutdown),
48         DEVMETHOD(usb_take_controller, xhci_pci_take_controller),
49 
50         DEVMETHOD_END
51 };
52 
53 static driver_t xhci_driver = {
54         .name = "xhci",
55         .methods = xhci_device_methods,
56         .size = sizeof(struct xhci_softc),
57 };
58 
59 static devclass_t xhci_devclass;
60 
61 DRIVER_MODULE(xhci, nexus, xhci_driver, xhci_devclass, NULL, NULL);
62 
63 static const char *
xhci_pci_match(device_t self)64 xhci_pci_match(device_t self)
65 {
66 	(void)self;
67 	return ("XHCI (generic) USB 3.0 controller");
68 }
69 
70 static int
xhci_pci_probe(device_t self)71 xhci_pci_probe(device_t self)
72 {
73 	const char *desc = xhci_pci_match(self);
74 
75 	if (desc) {
76 		device_set_desc(self, desc);
77 		return (BUS_PROBE_DEFAULT);
78 	} else {
79 		return (ENXIO);
80 	}
81 }
82 
83 static int
xhci_pci_attach(device_t self)84 xhci_pci_attach(device_t self)
85 {
86 	struct resource *res;
87 	struct xhci_softc *sc = device_get_softc(self);;
88 	int err = ENXIO;
89 	uint8_t usedma32 = 0;
90 	int unit = device_get_unit(self);
91 
92 	usb_debug("+\n");
93 
94 	res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &unit, 0);
95 	if (res == NULL) {
96 		goto error;
97 	}
98 
99 	sc->sc_io_res = ioremap(res->start, res->count);
100 	if (!sc->sc_io_res) {
101 		goto error;
102 	}
103 	sc->sc_io_tag = (void *)sc->sc_io_res;
104 	sc->sc_io_hdl = (uintptr_t)sc->sc_io_res;
105 	sc->sc_io_size = res->count;
106 	usb_debug("bus_setup_intr\n");
107 
108 	res = bus_alloc_resource_any(self, SYS_RES_IRQ, &unit, 0);
109 	if (res == NULL) {
110 		goto error;
111 	}
112 
113 	sc->sc_irq_res = res;
114 	err = bus_setup_intr(res->start, 0, (driver_intr_t *)xhci_interrupt, sc);
115 	if (err) {
116 		goto error;
117 	}
118 
119 	usb_debug("xhci_init\n");
120 	HiUsb3StartHcd();
121 	if (xhci_init(sc, self, usedma32)) {
122 		device_printf(self, "Could not initialize softc\n");
123 		goto error;
124 	}
125 	callout_init_mtx(&sc->sc_callout, &sc->sc_bus.bus_mtx, 0);
126 
127 	usb_debug("add child to usbus\n");
128 	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
129 	if (sc->sc_bus.bdev == NULL) {
130 		device_printf(self, "Could not add USB device\n");
131 		goto error;
132 	}
133 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
134 
135 	usb_debug("xhci halt and start controller\n");
136 	err = xhci_halt_controller(sc);
137 
138 	if (err == 0)
139 		err = xhci_start_controller(sc);
140 
141 	usb_debug("device_probe_and_attach bus_dev\n");
142 	if (err == 0)
143 		err = device_probe_and_attach(sc->sc_bus.bdev);
144 
145 	if (err) {
146 		device_printf(self, "XHCI halt/start/probe failed err=%d\n", err);
147 		goto error;
148 	}
149 
150 	usb_debug("-\n");
151 
152 	return (0);
153 error:
154 	device_printf(self, "XHCI halt/start/probe failed err=%d\n", err);
155 	if (sc) {
156 		iounmap((void *)sc->sc_io_res);
157 	}
158 	(void)xhci_pci_detach(self);
159 	return (err);
160 }
161 
162 static int
xhci_pci_detach(device_t self)163 xhci_pci_detach(device_t self)
164 {
165 
166 	struct xhci_softc *sc = device_get_softc(self);
167 
168 	/* during module unload there are lots of children leftover */
169 	(void)device_delete_children(self);
170 
171 	callout_drain(&sc->sc_callout);
172 	(void)xhci_halt_controller(sc);
173 	(void)xhci_reset_controller(sc);
174 
175 	// release resouce
176 	if (sc->sc_irq_res) {
177 		(void)bus_teardown_intr(sc->sc_irq_res->start, sc);
178 		sc->sc_irq_res = NULL;
179 	}
180 	if (sc->sc_io_res) {
181 		iounmap((void *)sc->sc_io_res);
182 		sc->sc_io_res = NULL;
183 		sc->sc_io_tag = NULL;
184 		sc->sc_io_hdl = (uintptr_t)NULL;
185 		sc->sc_io_size = 0;
186 	}
187 
188 	xhci_uninit(sc);
189 
190 	return (0);
191 }
192 
193 static int
xhci_pci_take_controller(device_t self)194 xhci_pci_take_controller(device_t self)
195 {
196 	struct xhci_softc *sc = device_get_softc(self);
197 	uint32_t cparams;
198 	uint32_t eecp;
199 	uint32_t eec;
200 	uint16_t to;
201 	uint8_t bios_sem;
202 
203 	cparams = XREAD4(sc, capa, XHCI_HCSPARAMS0);
204 
205 	eec = (uint32_t)(-1);
206 
207 	/* Synchronise with the BIOS if it owns the controller. */
208 	for (eecp = XHCI_HCS0_XECP(cparams) << 2; (eecp != 0) && XHCI_XECP_NEXT(eec);
209 	    eecp += (XHCI_XECP_NEXT(eec) << 2)) {
210 		eec = XREAD4(sc, capa, eecp);
211 
212 		if (XHCI_XECP_ID(eec) != XHCI_ID_USB_LEGACY)
213 			continue;
214 		bios_sem = XREAD1(sc, capa, eecp +
215 		    XHCI_XECP_BIOS_SEM);
216 		if (bios_sem == 0)
217 			continue;
218 		device_printf(sc->sc_bus.bdev, "waiting for BIOS "
219 		    "to give up control\n");
220 		XWRITE1(sc, capa, eecp +
221 		    XHCI_XECP_OS_SEM, 1);
222 		to = 500;
223 		while (1) {
224 			bios_sem = XREAD1(sc, capa, eecp +
225 			    XHCI_XECP_BIOS_SEM);
226 			if (bios_sem == 0)
227 				break;
228 
229 			if (--to == 0) {
230 				device_printf(sc->sc_bus.bdev,
231 				    "timed out waiting for BIOS\n");
232 				break;
233 			}
234 			usb_pause_mtx(NULL, hz / 100);	/* wait 10ms */
235 		}
236 	}
237 	return (0);
238 }
239 
240 int
hixhci_init(void)241 hixhci_init(void)
242 {
243 	DPRINTF("hixhci_init");
244 	return driver_module_handler(NULL, MOD_LOAD, &xhci_nexus_driver_mod);
245 }
246 
247 void
hixhci_exit(void)248 hixhci_exit(void)
249 {
250 	DPRINTF("hixhci_exit");
251 	(void)driver_module_handler(NULL, MOD_UNLOAD, &xhci_nexus_driver_mod);
252 }
253