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$");
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>
61229096Shselasky#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;
66229096Shselaskystatic 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),
73229096Shselasky	DEVMETHOD(device_suspend, bus_generic_suspend),
74229096Shselasky	DEVMETHOD(device_resume, bus_generic_resume),
75229096Shselasky	DEVMETHOD(device_shutdown, bus_generic_shutdown),
76229096Shselasky	DEVMETHOD(usb_take_controller, xhci_pci_take_controller),
77213379Shselasky
78229093Shselasky	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{
96238134Smav	uint32_t device_id = pci_get_devid(self);
97238134Smav
98238134Smav	switch (device_id) {
99238134Smav	case 0x01941033:
100238134Smav		return ("NEC uPD720200 USB 3.0 controller");
101238134Smav
102249624Smav	case 0x10421b21:
103249624Smav		return ("ASMedia ASM1042 USB 3.0 controller");
104249624Smav
105262365Shselasky	case 0x9c318086:
106238134Smav	case 0x1e318086:
107238134Smav		return ("Intel Panther Point USB 3.0 controller");
108242774Shselasky	case 0x8c318086:
109242774Shselasky		return ("Intel Lynx Point USB 3.0 controller");
110238134Smav
111238134Smav	default:
112238134Smav		break;
113238134Smav	}
114238134Smav
115213379Shselasky	if ((pci_get_class(self) == PCIC_SERIALBUS)
116213379Shselasky	    && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
117222018Sru	    && (pci_get_progif(self) == PCIP_SERIALBUS_USB_XHCI)) {
118213379Shselasky		return ("XHCI (generic) USB 3.0 controller");
119213379Shselasky	}
120213379Shselasky	return (NULL);			/* dunno */
121213379Shselasky}
122213379Shselasky
123213379Shselaskystatic int
124213379Shselaskyxhci_pci_probe(device_t self)
125213379Shselasky{
126213379Shselasky	const char *desc = xhci_pci_match(self);
127213379Shselasky
128213379Shselasky	if (desc) {
129213379Shselasky		device_set_desc(self, desc);
130213379Shselasky		return (0);
131213379Shselasky	} else {
132213379Shselasky		return (ENXIO);
133213379Shselasky	}
134213379Shselasky}
135213379Shselasky
136253464Skibstatic int xhci_use_msi = 1;
137253464SkibTUNABLE_INT("hw.usb.xhci.msi", &xhci_use_msi);
138253464Skib
139213379Shselaskystatic int
140255965Shselaskyxhci_pci_port_route(device_t self, uint32_t set, uint32_t clear)
141255965Shselasky{
142255965Shselasky	uint32_t temp;
143255965Shselasky
144255965Shselasky	temp = pci_read_config(self, PCI_XHCI_INTEL_USB3_PSSEN, 4) |
145255965Shselasky	    pci_read_config(self, PCI_XHCI_INTEL_XUSB2PR, 4);
146255965Shselasky
147255965Shselasky	temp |= set;
148255965Shselasky	temp &= ~clear;
149255965Shselasky
150255965Shselasky	pci_write_config(self, PCI_XHCI_INTEL_USB3_PSSEN, temp, 4);
151255965Shselasky	pci_write_config(self, PCI_XHCI_INTEL_XUSB2PR, temp, 4);
152255965Shselasky
153255965Shselasky	device_printf(self, "Port routing mask set to 0x%08x\n", temp);
154255965Shselasky
155255965Shselasky	return (0);
156255965Shselasky}
157255965Shselasky
158255965Shselaskystatic int
159213379Shselaskyxhci_pci_attach(device_t self)
160213379Shselasky{
161213379Shselasky	struct xhci_softc *sc = device_get_softc(self);
162253379Skib	int count, err, 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
184253379Skib	sc->sc_irq_rid = 0;
185253464Skib	if (xhci_use_msi) {
186253464Skib		count = pci_msi_count(self);
187253464Skib		if (count >= 1) {
188253464Skib			count = 1;
189253464Skib			if (pci_alloc_msi(self, &count) == 0) {
190253464Skib				if (bootverbose)
191253464Skib					device_printf(self, "MSI enabled\n");
192253464Skib				sc->sc_irq_rid = 1;
193253464Skib			}
194253379Skib		}
195253379Skib	}
196253379Skib	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ,
197253379Skib	    &sc->sc_irq_rid, RF_SHAREABLE | RF_ACTIVE);
198213379Shselasky	if (sc->sc_irq_res == NULL) {
199213379Shselasky		device_printf(self, "Could not allocate IRQ\n");
200213379Shselasky		goto error;
201213379Shselasky	}
202213379Shselasky	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
203213379Shselasky	if (sc->sc_bus.bdev == NULL) {
204213379Shselasky		device_printf(self, "Could not add USB device\n");
205213379Shselasky		goto error;
206213379Shselasky	}
207213379Shselasky	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
208213379Shselasky
209213379Shselasky	sprintf(sc->sc_vendor, "0x%04x", pci_get_vendor(self));
210213379Shselasky
211213379Shselasky#if (__FreeBSD_version >= 700031)
212213379Shselasky	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
213213379Shselasky	    NULL, (driver_intr_t *)xhci_interrupt, sc, &sc->sc_intr_hdl);
214213379Shselasky#else
215213379Shselasky	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
216213379Shselasky	    (driver_intr_t *)xhci_interrupt, sc, &sc->sc_intr_hdl);
217213379Shselasky#endif
218213379Shselasky	if (err) {
219213379Shselasky		device_printf(self, "Could not setup IRQ, err=%d\n", err);
220213379Shselasky		sc->sc_intr_hdl = NULL;
221213379Shselasky		goto error;
222213379Shselasky	}
223255965Shselasky	/* On Intel chipsets reroute ports from EHCI to XHCI controller. */
224255965Shselasky	switch (pci_get_devid(self)) {
225262365Shselasky	case 0x9c318086:	/* Panther Point */
226255965Shselasky	case 0x1e318086:	/* Panther Point */
227255965Shselasky	case 0x8c318086:	/* Lynx Point */
228255965Shselasky		sc->sc_port_route = &xhci_pci_port_route;
229265079Shselasky		sc->sc_imod_default = XHCI_IMOD_DEFAULT_LP;
230255965Shselasky		break;
231255965Shselasky	default:
232255965Shselasky		break;
233255965Shselasky	}
234255965Shselasky
235229096Shselasky	xhci_pci_take_controller(self);
236213379Shselasky
237213379Shselasky	err = xhci_halt_controller(sc);
238213379Shselasky
239213379Shselasky	if (err == 0)
240213379Shselasky		err = xhci_start_controller(sc);
241213379Shselasky
242213379Shselasky	if (err == 0)
243213379Shselasky		err = device_probe_and_attach(sc->sc_bus.bdev);
244213379Shselasky
245213379Shselasky	if (err) {
246213379Shselasky		device_printf(self, "XHCI halt/start/probe failed err=%d\n", err);
247213379Shselasky		goto error;
248213379Shselasky	}
249213379Shselasky	return (0);
250213379Shselasky
251213379Shselaskyerror:
252213379Shselasky	xhci_pci_detach(self);
253213379Shselasky	return (ENXIO);
254213379Shselasky}
255213379Shselasky
256213379Shselaskystatic int
257213379Shselaskyxhci_pci_detach(device_t self)
258213379Shselasky{
259213379Shselasky	struct xhci_softc *sc = device_get_softc(self);
260213379Shselasky	device_t bdev;
261213379Shselasky
262213379Shselasky	if (sc->sc_bus.bdev != NULL) {
263213379Shselasky		bdev = sc->sc_bus.bdev;
264213379Shselasky		device_detach(bdev);
265213379Shselasky		device_delete_child(self, bdev);
266213379Shselasky	}
267213379Shselasky	/* during module unload there are lots of children leftover */
268229118Shselasky	device_delete_children(self);
269213379Shselasky
270213379Shselasky	pci_disable_busmaster(self);
271213379Shselasky
272213379Shselasky	if (sc->sc_irq_res && sc->sc_intr_hdl) {
273213379Shselasky
274213379Shselasky		xhci_halt_controller(sc);
275213379Shselasky
276213379Shselasky		bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
277213379Shselasky		sc->sc_intr_hdl = NULL;
278213379Shselasky	}
279213379Shselasky	if (sc->sc_irq_res) {
280253379Skib		if (sc->sc_irq_rid == 1)
281253379Skib			pci_release_msi(self);
282253379Skib		bus_release_resource(self, SYS_RES_IRQ, sc->sc_irq_rid,
283253379Skib		    sc->sc_irq_res);
284213379Shselasky		sc->sc_irq_res = NULL;
285213379Shselasky	}
286213379Shselasky	if (sc->sc_io_res) {
287213379Shselasky		bus_release_resource(self, SYS_RES_MEMORY, PCI_XHCI_CBMEM,
288213379Shselasky		    sc->sc_io_res);
289213379Shselasky		sc->sc_io_res = NULL;
290213379Shselasky	}
291213379Shselasky
292213379Shselasky	xhci_uninit(sc);
293213379Shselasky
294213379Shselasky	return (0);
295213379Shselasky}
296213379Shselasky
297229096Shselaskystatic int
298229096Shselaskyxhci_pci_take_controller(device_t self)
299213379Shselasky{
300213379Shselasky	struct xhci_softc *sc = device_get_softc(self);
301213379Shselasky	uint32_t cparams;
302213379Shselasky	uint32_t eecp;
303213379Shselasky	uint32_t eec;
304213379Shselasky	uint16_t to;
305213379Shselasky	uint8_t bios_sem;
306213379Shselasky
307213379Shselasky	cparams = XREAD4(sc, capa, XHCI_HCSPARAMS0);
308213379Shselasky
309213379Shselasky	eec = -1;
310213379Shselasky
311213379Shselasky	/* Synchronise with the BIOS if it owns the controller. */
312213379Shselasky	for (eecp = XHCI_HCS0_XECP(cparams) << 2; eecp != 0 && XHCI_XECP_NEXT(eec);
313213379Shselasky	    eecp += XHCI_XECP_NEXT(eec) << 2) {
314213379Shselasky		eec = XREAD4(sc, capa, eecp);
315213379Shselasky
316213379Shselasky		if (XHCI_XECP_ID(eec) != XHCI_ID_USB_LEGACY)
317213379Shselasky			continue;
318213379Shselasky		bios_sem = XREAD1(sc, capa, eecp +
319213379Shselasky		    XHCI_XECP_BIOS_SEM);
320213379Shselasky		if (bios_sem == 0)
321213379Shselasky			continue;
322213379Shselasky		device_printf(sc->sc_bus.bdev, "waiting for BIOS "
323213379Shselasky		    "to give up control\n");
324213379Shselasky		XWRITE1(sc, capa, eecp +
325213379Shselasky		    XHCI_XECP_OS_SEM, 1);
326213379Shselasky		to = 500;
327213379Shselasky		while (1) {
328213379Shselasky			bios_sem = XREAD1(sc, capa, eecp +
329213379Shselasky			    XHCI_XECP_BIOS_SEM);
330213379Shselasky			if (bios_sem == 0)
331213379Shselasky				break;
332213379Shselasky
333213379Shselasky			if (--to == 0) {
334213379Shselasky				device_printf(sc->sc_bus.bdev,
335213379Shselasky				    "timed out waiting for BIOS\n");
336213379Shselasky				break;
337213379Shselasky			}
338213379Shselasky			usb_pause_mtx(NULL, hz / 100);	/* wait 10ms */
339213379Shselasky		}
340213379Shselasky	}
341229096Shselasky	return (0);
342213379Shselasky}
343