xhci_pci.c revision 253094
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 253094 2013-07-09 12:55:01Z kib $");
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>
61228483Shselasky#include "usb_if.h"
62213379Shselasky
63213379Shselaskystatic device_probe_t xhci_pci_probe;
64213379Shselaskystatic device_attach_t xhci_pci_attach;
65213379Shselaskystatic device_detach_t xhci_pci_detach;
66228483Shselaskystatic usb_take_controller_t xhci_pci_take_controller;
67213379Shselasky
68213379Shselaskystatic device_method_t xhci_device_methods[] = {
69213379Shselasky	/* device interface */
70213379Shselasky	DEVMETHOD(device_probe, xhci_pci_probe),
71213379Shselasky	DEVMETHOD(device_attach, xhci_pci_attach),
72213379Shselasky	DEVMETHOD(device_detach, xhci_pci_detach),
73228483Shselasky	DEVMETHOD(device_suspend, bus_generic_suspend),
74228483Shselasky	DEVMETHOD(device_resume, bus_generic_resume),
75228483Shselasky	DEVMETHOD(device_shutdown, bus_generic_shutdown),
76228483Shselasky	DEVMETHOD(usb_take_controller, xhci_pci_take_controller),
77213379Shselasky
78227843Smarius	DEVMETHOD_END
79213379Shselasky};
80213379Shselasky
81213379Shselaskystatic driver_t xhci_driver = {
82213379Shselasky	.name = "xhci",
83213379Shselasky	.methods = xhci_device_methods,
84213379Shselasky	.size = sizeof(struct xhci_softc),
85213379Shselasky};
86213379Shselasky
87213379Shselaskystatic devclass_t xhci_devclass;
88213379Shselasky
89213379ShselaskyDRIVER_MODULE(xhci, pci, xhci_driver, xhci_devclass, 0, 0);
90213379ShselaskyMODULE_DEPEND(xhci, usb, 1, 1, 1);
91213379Shselasky
92213379Shselasky
93213379Shselaskystatic const char *
94213379Shselaskyxhci_pci_match(device_t self)
95213379Shselasky{
96238015Smav	uint32_t device_id = pci_get_devid(self);
97238015Smav
98238015Smav	switch (device_id) {
99238015Smav	case 0x01941033:
100238015Smav		return ("NEC uPD720200 USB 3.0 controller");
101238015Smav
102249336Smav	case 0x10421b21:
103249336Smav		return ("ASMedia ASM1042 USB 3.0 controller");
104249336Smav
105238015Smav	case 0x1e318086:
106238015Smav		return ("Intel Panther Point USB 3.0 controller");
107238551Smav	case 0x8c318086:
108238551Smav		return ("Intel Lynx Point USB 3.0 controller");
109238015Smav
110238015Smav	default:
111238015Smav		break;
112238015Smav	}
113238015Smav
114213379Shselasky	if ((pci_get_class(self) == PCIC_SERIALBUS)
115213379Shselasky	    && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
116222018Sru	    && (pci_get_progif(self) == PCIP_SERIALBUS_USB_XHCI)) {
117213379Shselasky		return ("XHCI (generic) USB 3.0 controller");
118213379Shselasky	}
119213379Shselasky	return (NULL);			/* dunno */
120213379Shselasky}
121213379Shselasky
122213379Shselaskystatic int
123213379Shselaskyxhci_pci_probe(device_t self)
124213379Shselasky{
125213379Shselasky	const char *desc = xhci_pci_match(self);
126213379Shselasky
127213379Shselasky	if (desc) {
128213379Shselasky		device_set_desc(self, desc);
129213379Shselasky		return (0);
130213379Shselasky	} else {
131213379Shselasky		return (ENXIO);
132213379Shselasky	}
133213379Shselasky}
134213379Shselasky
135251499Shselaskystatic void
136251499Shselaskyxhci_interrupt_poll(void *_sc)
137251499Shselasky{
138251499Shselasky	struct xhci_softc *sc = _sc;
139251499Shselasky	USB_BUS_UNLOCK(&sc->sc_bus);
140251499Shselasky	xhci_interrupt(sc);
141251499Shselasky	USB_BUS_LOCK(&sc->sc_bus);
142251499Shselasky	usb_callout_reset(&sc->sc_callout, 1, (void *)&xhci_interrupt_poll, sc);
143251499Shselasky}
144251499Shselasky
145213379Shselaskystatic int
146213379Shselaskyxhci_pci_attach(device_t self)
147213379Shselasky{
148213379Shselasky	struct xhci_softc *sc = device_get_softc(self);
149253094Skib	int count, err, rid;
150213379Shselasky
151213379Shselasky	/* XXX check for 64-bit capability */
152213379Shselasky
153213379Shselasky	if (xhci_init(sc, self)) {
154213379Shselasky		device_printf(self, "Could not initialize softc\n");
155213379Shselasky		goto error;
156213379Shselasky	}
157213379Shselasky
158213379Shselasky	pci_enable_busmaster(self);
159213379Shselasky
160213379Shselasky	rid = PCI_XHCI_CBMEM;
161213379Shselasky	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
162213379Shselasky	    RF_ACTIVE);
163213379Shselasky	if (!sc->sc_io_res) {
164213379Shselasky		device_printf(self, "Could not map memory\n");
165213379Shselasky		goto error;
166213379Shselasky	}
167213379Shselasky	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
168213379Shselasky	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
169213379Shselasky	sc->sc_io_size = rman_get_size(sc->sc_io_res);
170213379Shselasky
171251499Shselasky	usb_callout_init_mtx(&sc->sc_callout, &sc->sc_bus.bus_mtx, 0);
172251499Shselasky
173253094Skib	sc->sc_irq_rid = 0;
174253094Skib	count = pci_msi_count(self);
175253094Skib	if (count >= 1) {
176253094Skib		count = 1;
177253094Skib		if (pci_alloc_msi(self, &count) == 0) {
178253094Skib			if (bootverbose)
179253094Skib				device_printf(self, "MSI enabled\n");
180253094Skib			sc->sc_irq_rid = 1;
181253094Skib		}
182253094Skib	}
183253094Skib	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ,
184253094Skib	    &sc->sc_irq_rid, RF_SHAREABLE | RF_ACTIVE);
185213379Shselasky	if (sc->sc_irq_res == NULL) {
186213379Shselasky		device_printf(self, "Could not allocate IRQ\n");
187253094Skib		goto error;
188213379Shselasky	}
189213379Shselasky	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
190213379Shselasky	if (sc->sc_bus.bdev == NULL) {
191213379Shselasky		device_printf(self, "Could not add USB device\n");
192213379Shselasky		goto error;
193213379Shselasky	}
194213379Shselasky	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
195213379Shselasky
196213379Shselasky	sprintf(sc->sc_vendor, "0x%04x", pci_get_vendor(self));
197213379Shselasky
198251499Shselasky	if (sc->sc_irq_res != NULL) {
199251499Shselasky		err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
200251499Shselasky		    NULL, (driver_intr_t *)xhci_interrupt, sc, &sc->sc_intr_hdl);
201251499Shselasky		if (err != 0) {
202251499Shselasky			device_printf(self, "Could not setup IRQ, err=%d\n", err);
203251499Shselasky			sc->sc_intr_hdl = NULL;
204251499Shselasky		}
205213379Shselasky	}
206251499Shselasky	if (sc->sc_irq_res == NULL || sc->sc_intr_hdl == NULL ||
207251499Shselasky	    xhci_use_polling() != 0) {
208251499Shselasky		device_printf(self, "Interrupt polling at %dHz\n", hz);
209251499Shselasky		USB_BUS_LOCK(&sc->sc_bus);
210251499Shselasky		xhci_interrupt_poll(sc);
211251499Shselasky		USB_BUS_UNLOCK(&sc->sc_bus);
212251499Shselasky	}
213251499Shselasky
214228483Shselasky	xhci_pci_take_controller(self);
215213379Shselasky
216213379Shselasky	err = xhci_halt_controller(sc);
217213379Shselasky
218213379Shselasky	if (err == 0)
219213379Shselasky		err = xhci_start_controller(sc);
220213379Shselasky
221213379Shselasky	if (err == 0)
222213379Shselasky		err = device_probe_and_attach(sc->sc_bus.bdev);
223213379Shselasky
224213379Shselasky	if (err) {
225213379Shselasky		device_printf(self, "XHCI halt/start/probe failed err=%d\n", err);
226213379Shselasky		goto error;
227213379Shselasky	}
228213379Shselasky	return (0);
229213379Shselasky
230213379Shselaskyerror:
231213379Shselasky	xhci_pci_detach(self);
232213379Shselasky	return (ENXIO);
233213379Shselasky}
234213379Shselasky
235213379Shselaskystatic int
236213379Shselaskyxhci_pci_detach(device_t self)
237213379Shselasky{
238213379Shselasky	struct xhci_softc *sc = device_get_softc(self);
239213379Shselasky	device_t bdev;
240213379Shselasky
241213379Shselasky	if (sc->sc_bus.bdev != NULL) {
242213379Shselasky		bdev = sc->sc_bus.bdev;
243213379Shselasky		device_detach(bdev);
244213379Shselasky		device_delete_child(self, bdev);
245213379Shselasky	}
246213379Shselasky	/* during module unload there are lots of children leftover */
247227849Shselasky	device_delete_children(self);
248213379Shselasky
249251499Shselasky	if (sc->sc_io_res) {
250251499Shselasky		usb_callout_drain(&sc->sc_callout);
251251499Shselasky		xhci_halt_controller(sc);
252251499Shselasky	}
253251499Shselasky
254213379Shselasky	pci_disable_busmaster(self);
255213379Shselasky
256213379Shselasky	if (sc->sc_irq_res && sc->sc_intr_hdl) {
257213379Shselasky		bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
258213379Shselasky		sc->sc_intr_hdl = NULL;
259213379Shselasky	}
260213379Shselasky	if (sc->sc_irq_res) {
261253094Skib		if (sc->sc_irq_rid == 1)
262253094Skib			pci_release_msi(self);
263253094Skib		bus_release_resource(self, SYS_RES_IRQ, sc->sc_irq_rid,
264253094Skib		    sc->sc_irq_res);
265213379Shselasky		sc->sc_irq_res = NULL;
266213379Shselasky	}
267213379Shselasky	if (sc->sc_io_res) {
268213379Shselasky		bus_release_resource(self, SYS_RES_MEMORY, PCI_XHCI_CBMEM,
269213379Shselasky		    sc->sc_io_res);
270213379Shselasky		sc->sc_io_res = NULL;
271213379Shselasky	}
272213379Shselasky
273213379Shselasky	xhci_uninit(sc);
274213379Shselasky
275213379Shselasky	return (0);
276213379Shselasky}
277213379Shselasky
278228483Shselaskystatic int
279228483Shselaskyxhci_pci_take_controller(device_t self)
280213379Shselasky{
281213379Shselasky	struct xhci_softc *sc = device_get_softc(self);
282238551Smav	uint32_t device_id = pci_get_devid(self);
283213379Shselasky	uint32_t cparams;
284213379Shselasky	uint32_t eecp;
285213379Shselasky	uint32_t eec;
286213379Shselasky	uint16_t to;
287213379Shselasky	uint8_t bios_sem;
288213379Shselasky
289213379Shselasky	cparams = XREAD4(sc, capa, XHCI_HCSPARAMS0);
290213379Shselasky
291213379Shselasky	eec = -1;
292213379Shselasky
293213379Shselasky	/* Synchronise with the BIOS if it owns the controller. */
294213379Shselasky	for (eecp = XHCI_HCS0_XECP(cparams) << 2; eecp != 0 && XHCI_XECP_NEXT(eec);
295213379Shselasky	    eecp += XHCI_XECP_NEXT(eec) << 2) {
296213379Shselasky		eec = XREAD4(sc, capa, eecp);
297213379Shselasky
298213379Shselasky		if (XHCI_XECP_ID(eec) != XHCI_ID_USB_LEGACY)
299213379Shselasky			continue;
300213379Shselasky		bios_sem = XREAD1(sc, capa, eecp +
301213379Shselasky		    XHCI_XECP_BIOS_SEM);
302213379Shselasky		if (bios_sem == 0)
303213379Shselasky			continue;
304213379Shselasky		device_printf(sc->sc_bus.bdev, "waiting for BIOS "
305213379Shselasky		    "to give up control\n");
306213379Shselasky		XWRITE1(sc, capa, eecp +
307213379Shselasky		    XHCI_XECP_OS_SEM, 1);
308213379Shselasky		to = 500;
309213379Shselasky		while (1) {
310213379Shselasky			bios_sem = XREAD1(sc, capa, eecp +
311213379Shselasky			    XHCI_XECP_BIOS_SEM);
312213379Shselasky			if (bios_sem == 0)
313213379Shselasky				break;
314213379Shselasky
315213379Shselasky			if (--to == 0) {
316213379Shselasky				device_printf(sc->sc_bus.bdev,
317213379Shselasky				    "timed out waiting for BIOS\n");
318213379Shselasky				break;
319213379Shselasky			}
320213379Shselasky			usb_pause_mtx(NULL, hz / 100);	/* wait 10ms */
321213379Shselasky		}
322213379Shselasky	}
323238551Smav
324238551Smav	/* On Intel chipsets reroute ports from EHCI to XHCI controller. */
325238551Smav	if (device_id == 0x1e318086 /* Panther Point */ ||
326238551Smav	    device_id == 0x8c318086 /* Lynx Point */) {
327239617Shselasky		uint32_t temp = xhci_get_port_route();
328239617Shselasky		pci_write_config(self, PCI_XHCI_INTEL_USB3_PSSEN, temp, 4);
329239617Shselasky		pci_write_config(self, PCI_XHCI_INTEL_XUSB2PR, temp, 4);
330238551Smav	}
331228483Shselasky	return (0);
332213379Shselasky}
333