xhci_pci.c revision 227849
1213379Shselasky/*-
2213379Shselasky * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
3213379Shselasky *
4213379Shselasky * Redistribution and use in source and binary forms, with or without
5213379Shselasky * modification, are permitted provided that the following conditions
6213379Shselasky * are met:
7213379Shselasky * 1. Redistributions of source code must retain the above copyright
8213379Shselasky *    notice, this list of conditions and the following disclaimer.
9213379Shselasky * 2. Redistributions in binary form must reproduce the above copyright
10213379Shselasky *    notice, this list of conditions and the following disclaimer in the
11213379Shselasky *    documentation and/or other materials provided with the distribution.
12213379Shselasky *
13213379Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14213379Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15213379Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16213379Shselasky * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17213379Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18213379Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19213379Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20213379Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21213379Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22213379Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23213379Shselasky * SUCH DAMAGE.
24213379Shselasky */
25213379Shselasky
26213379Shselasky#include <sys/cdefs.h>
27213379Shselasky__FBSDID("$FreeBSD: head/sys/dev/usb/controller/xhci_pci.c 227849 2011-11-22 21:56:55Z hselasky $");
28213379Shselasky
29213379Shselasky#include <sys/stdint.h>
30213379Shselasky#include <sys/stddef.h>
31213379Shselasky#include <sys/param.h>
32213379Shselasky#include <sys/queue.h>
33213379Shselasky#include <sys/types.h>
34213379Shselasky#include <sys/systm.h>
35213379Shselasky#include <sys/kernel.h>
36213379Shselasky#include <sys/bus.h>
37213379Shselasky#include <sys/module.h>
38213379Shselasky#include <sys/lock.h>
39213379Shselasky#include <sys/mutex.h>
40213379Shselasky#include <sys/condvar.h>
41213379Shselasky#include <sys/sysctl.h>
42213379Shselasky#include <sys/sx.h>
43213379Shselasky#include <sys/unistd.h>
44213379Shselasky#include <sys/callout.h>
45213379Shselasky#include <sys/malloc.h>
46213379Shselasky#include <sys/priv.h>
47213379Shselasky
48213379Shselasky#include <dev/usb/usb.h>
49213379Shselasky#include <dev/usb/usbdi.h>
50213379Shselasky
51213379Shselasky#include <dev/usb/usb_core.h>
52213379Shselasky#include <dev/usb/usb_busdma.h>
53213379Shselasky#include <dev/usb/usb_process.h>
54213379Shselasky#include <dev/usb/usb_util.h>
55213379Shselasky
56213379Shselasky#include <dev/usb/usb_controller.h>
57213379Shselasky#include <dev/usb/usb_bus.h>
58213379Shselasky#include <dev/usb/usb_pci.h>
59213379Shselasky#include <dev/usb/controller/xhci.h>
60213379Shselasky#include <dev/usb/controller/xhcireg.h>
61213379Shselasky
62213379Shselaskystatic device_probe_t xhci_pci_probe;
63213379Shselaskystatic device_attach_t xhci_pci_attach;
64213379Shselaskystatic device_detach_t xhci_pci_detach;
65213379Shselaskystatic device_suspend_t xhci_pci_suspend;
66213379Shselaskystatic device_resume_t xhci_pci_resume;
67213379Shselaskystatic device_shutdown_t xhci_pci_shutdown;
68213379Shselaskystatic void xhci_pci_takecontroller(device_t);
69213379Shselasky
70213379Shselaskystatic device_method_t xhci_device_methods[] = {
71213379Shselasky	/* device interface */
72213379Shselasky	DEVMETHOD(device_probe, xhci_pci_probe),
73213379Shselasky	DEVMETHOD(device_attach, xhci_pci_attach),
74213379Shselasky	DEVMETHOD(device_detach, xhci_pci_detach),
75213379Shselasky	DEVMETHOD(device_suspend, xhci_pci_suspend),
76213379Shselasky	DEVMETHOD(device_resume, xhci_pci_resume),
77213379Shselasky	DEVMETHOD(device_shutdown, xhci_pci_shutdown),
78213379Shselasky
79227843Smarius	DEVMETHOD_END
80213379Shselasky};
81213379Shselasky
82213379Shselaskystatic driver_t xhci_driver = {
83213379Shselasky	.name = "xhci",
84213379Shselasky	.methods = xhci_device_methods,
85213379Shselasky	.size = sizeof(struct xhci_softc),
86213379Shselasky};
87213379Shselasky
88213379Shselaskystatic devclass_t xhci_devclass;
89213379Shselasky
90213379ShselaskyDRIVER_MODULE(xhci, pci, xhci_driver, xhci_devclass, 0, 0);
91213379ShselaskyMODULE_DEPEND(xhci, usb, 1, 1, 1);
92213379Shselasky
93213379Shselaskystatic int
94213379Shselaskyxhci_pci_suspend(device_t self)
95213379Shselasky{
96213379Shselasky	struct xhci_softc *sc = device_get_softc(self);
97213379Shselasky	int err;
98213379Shselasky
99213379Shselasky	err = bus_generic_suspend(self);
100213379Shselasky	if (err)
101213379Shselasky		return (err);
102213379Shselasky	xhci_suspend(sc);
103213379Shselasky	return (0);
104213379Shselasky}
105213379Shselasky
106213379Shselaskystatic int
107213379Shselaskyxhci_pci_resume(device_t self)
108213379Shselasky{
109213379Shselasky	struct xhci_softc *sc = device_get_softc(self);
110213379Shselasky
111213379Shselasky	xhci_pci_takecontroller(self);
112213379Shselasky	xhci_resume(sc);
113213379Shselasky
114213379Shselasky	bus_generic_resume(self);
115213379Shselasky
116213379Shselasky	return (0);
117213379Shselasky}
118213379Shselasky
119213379Shselaskystatic int
120213379Shselaskyxhci_pci_shutdown(device_t self)
121213379Shselasky{
122213379Shselasky	struct xhci_softc *sc = device_get_softc(self);
123213379Shselasky	int err;
124213379Shselasky
125213379Shselasky	err = bus_generic_shutdown(self);
126213379Shselasky	if (err)
127213379Shselasky		return (err);
128213379Shselasky	xhci_shutdown(sc);
129213379Shselasky
130213379Shselasky	return (0);
131213379Shselasky}
132213379Shselasky
133213379Shselaskystatic const char *
134213379Shselaskyxhci_pci_match(device_t self)
135213379Shselasky{
136213379Shselasky	if ((pci_get_class(self) == PCIC_SERIALBUS)
137213379Shselasky	    && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
138222018Sru	    && (pci_get_progif(self) == PCIP_SERIALBUS_USB_XHCI)) {
139213379Shselasky		return ("XHCI (generic) USB 3.0 controller");
140213379Shselasky	}
141213379Shselasky	return (NULL);			/* dunno */
142213379Shselasky}
143213379Shselasky
144213379Shselaskystatic int
145213379Shselaskyxhci_pci_probe(device_t self)
146213379Shselasky{
147213379Shselasky	const char *desc = xhci_pci_match(self);
148213379Shselasky
149213379Shselasky	if (desc) {
150213379Shselasky		device_set_desc(self, desc);
151213379Shselasky		return (0);
152213379Shselasky	} else {
153213379Shselasky		return (ENXIO);
154213379Shselasky	}
155213379Shselasky}
156213379Shselasky
157213379Shselaskystatic int
158213379Shselaskyxhci_pci_attach(device_t self)
159213379Shselasky{
160213379Shselasky	struct xhci_softc *sc = device_get_softc(self);
161213379Shselasky	int err;
162213379Shselasky	int rid;
163213379Shselasky
164213379Shselasky	/* XXX check for 64-bit capability */
165213379Shselasky
166213379Shselasky	if (xhci_init(sc, self)) {
167213379Shselasky		device_printf(self, "Could not initialize softc\n");
168213379Shselasky		goto error;
169213379Shselasky	}
170213379Shselasky
171213379Shselasky	pci_enable_busmaster(self);
172213379Shselasky
173213379Shselasky	rid = PCI_XHCI_CBMEM;
174213379Shselasky	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
175213379Shselasky	    RF_ACTIVE);
176213379Shselasky	if (!sc->sc_io_res) {
177213379Shselasky		device_printf(self, "Could not map memory\n");
178213379Shselasky		goto error;
179213379Shselasky	}
180213379Shselasky	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
181213379Shselasky	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
182213379Shselasky	sc->sc_io_size = rman_get_size(sc->sc_io_res);
183213379Shselasky
184213379Shselasky	rid = 0;
185213379Shselasky	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
186213379Shselasky	    RF_SHAREABLE | RF_ACTIVE);
187213379Shselasky	if (sc->sc_irq_res == NULL) {
188213379Shselasky		device_printf(self, "Could not allocate IRQ\n");
189213379Shselasky		goto error;
190213379Shselasky	}
191213379Shselasky	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
192213379Shselasky	if (sc->sc_bus.bdev == NULL) {
193213379Shselasky		device_printf(self, "Could not add USB device\n");
194213379Shselasky		goto error;
195213379Shselasky	}
196213379Shselasky	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
197213379Shselasky
198213379Shselasky	sprintf(sc->sc_vendor, "0x%04x", pci_get_vendor(self));
199213379Shselasky
200213379Shselasky#if (__FreeBSD_version >= 700031)
201213379Shselasky	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
202213379Shselasky	    NULL, (driver_intr_t *)xhci_interrupt, sc, &sc->sc_intr_hdl);
203213379Shselasky#else
204213379Shselasky	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
205213379Shselasky	    (driver_intr_t *)xhci_interrupt, sc, &sc->sc_intr_hdl);
206213379Shselasky#endif
207213379Shselasky	if (err) {
208213379Shselasky		device_printf(self, "Could not setup IRQ, err=%d\n", err);
209213379Shselasky		sc->sc_intr_hdl = NULL;
210213379Shselasky		goto error;
211213379Shselasky	}
212213379Shselasky	xhci_pci_takecontroller(self);
213213379Shselasky
214213379Shselasky	err = xhci_halt_controller(sc);
215213379Shselasky
216213379Shselasky	if (err == 0)
217213379Shselasky		err = xhci_start_controller(sc);
218213379Shselasky
219213379Shselasky	if (err == 0)
220213379Shselasky		err = device_probe_and_attach(sc->sc_bus.bdev);
221213379Shselasky
222213379Shselasky	if (err) {
223213379Shselasky		device_printf(self, "XHCI halt/start/probe failed err=%d\n", err);
224213379Shselasky		goto error;
225213379Shselasky	}
226213379Shselasky	return (0);
227213379Shselasky
228213379Shselaskyerror:
229213379Shselasky	xhci_pci_detach(self);
230213379Shselasky	return (ENXIO);
231213379Shselasky}
232213379Shselasky
233213379Shselaskystatic int
234213379Shselaskyxhci_pci_detach(device_t self)
235213379Shselasky{
236213379Shselasky	struct xhci_softc *sc = device_get_softc(self);
237213379Shselasky	device_t bdev;
238213379Shselasky
239213379Shselasky	if (sc->sc_bus.bdev != NULL) {
240213379Shselasky		bdev = sc->sc_bus.bdev;
241213379Shselasky		device_detach(bdev);
242213379Shselasky		device_delete_child(self, bdev);
243213379Shselasky	}
244213379Shselasky	/* during module unload there are lots of children leftover */
245227849Shselasky	device_delete_children(self);
246213379Shselasky
247213379Shselasky	pci_disable_busmaster(self);
248213379Shselasky
249213379Shselasky	if (sc->sc_irq_res && sc->sc_intr_hdl) {
250213379Shselasky
251213379Shselasky		xhci_halt_controller(sc);
252213379Shselasky
253213379Shselasky		bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
254213379Shselasky		sc->sc_intr_hdl = NULL;
255213379Shselasky	}
256213379Shselasky	if (sc->sc_irq_res) {
257213379Shselasky		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
258213379Shselasky		sc->sc_irq_res = NULL;
259213379Shselasky	}
260213379Shselasky	if (sc->sc_io_res) {
261213379Shselasky		bus_release_resource(self, SYS_RES_MEMORY, PCI_XHCI_CBMEM,
262213379Shselasky		    sc->sc_io_res);
263213379Shselasky		sc->sc_io_res = NULL;
264213379Shselasky	}
265213379Shselasky
266213379Shselasky	xhci_uninit(sc);
267213379Shselasky
268213379Shselasky	return (0);
269213379Shselasky}
270213379Shselasky
271213379Shselaskystatic void
272213379Shselaskyxhci_pci_takecontroller(device_t self)
273213379Shselasky{
274213379Shselasky	struct xhci_softc *sc = device_get_softc(self);
275213379Shselasky	uint32_t cparams;
276213379Shselasky	uint32_t eecp;
277213379Shselasky	uint32_t eec;
278213379Shselasky	uint16_t to;
279213379Shselasky	uint8_t bios_sem;
280213379Shselasky
281213379Shselasky	cparams = XREAD4(sc, capa, XHCI_HCSPARAMS0);
282213379Shselasky
283213379Shselasky	eec = -1;
284213379Shselasky
285213379Shselasky	/* Synchronise with the BIOS if it owns the controller. */
286213379Shselasky	for (eecp = XHCI_HCS0_XECP(cparams) << 2; eecp != 0 && XHCI_XECP_NEXT(eec);
287213379Shselasky	    eecp += XHCI_XECP_NEXT(eec) << 2) {
288213379Shselasky		eec = XREAD4(sc, capa, eecp);
289213379Shselasky
290213379Shselasky		if (XHCI_XECP_ID(eec) != XHCI_ID_USB_LEGACY)
291213379Shselasky			continue;
292213379Shselasky		bios_sem = XREAD1(sc, capa, eecp +
293213379Shselasky		    XHCI_XECP_BIOS_SEM);
294213379Shselasky		if (bios_sem == 0)
295213379Shselasky			continue;
296213379Shselasky		device_printf(sc->sc_bus.bdev, "waiting for BIOS "
297213379Shselasky		    "to give up control\n");
298213379Shselasky		XWRITE1(sc, capa, eecp +
299213379Shselasky		    XHCI_XECP_OS_SEM, 1);
300213379Shselasky		to = 500;
301213379Shselasky		while (1) {
302213379Shselasky			bios_sem = XREAD1(sc, capa, eecp +
303213379Shselasky			    XHCI_XECP_BIOS_SEM);
304213379Shselasky			if (bios_sem == 0)
305213379Shselasky				break;
306213379Shselasky
307213379Shselasky			if (--to == 0) {
308213379Shselasky				device_printf(sc->sc_bus.bdev,
309213379Shselasky				    "timed out waiting for BIOS\n");
310213379Shselasky				break;
311213379Shselasky			}
312213379Shselasky			usb_pause_mtx(NULL, hz / 100);	/* wait 10ms */
313213379Shselasky		}
314213379Shselasky	}
315213379Shselasky}
316