1184610Salfred/*-
2184610Salfred * Copyright (c) 1998 The NetBSD Foundation, Inc.
3184610Salfred * All rights reserved.
4184610Salfred *
5184610Salfred * This code is derived from software contributed to The NetBSD Foundation
6184610Salfred * by Lennart Augustsson (augustss@carlstedt.se) at
7184610Salfred * Carlstedt Research & Technology.
8184610Salfred *
9184610Salfred * Redistribution and use in source and binary forms, with or without
10184610Salfred * modification, are permitted provided that the following conditions
11184610Salfred * are met:
12184610Salfred * 1. Redistributions of source code must retain the above copyright
13184610Salfred *    notice, this list of conditions and the following disclaimer.
14184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
15184610Salfred *    notice, this list of conditions and the following disclaimer in the
16184610Salfred *    documentation and/or other materials provided with the distribution.
17184610Salfred *
18184610Salfred * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19184610Salfred * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20184610Salfred * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21184610Salfred * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22184610Salfred * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23184610Salfred * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24184610Salfred * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25184610Salfred * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26184610Salfred * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27184610Salfred * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28184610Salfred * POSSIBILITY OF SUCH DAMAGE.
29184610Salfred */
30184610Salfred
31184610Salfred#include <sys/cdefs.h>
32184610Salfred__FBSDID("$FreeBSD: releng/10.2/sys/dev/usb/controller/ehci_pci.c 278278 2015-02-05 20:03:02Z hselasky $");
33184610Salfred
34184610Salfred/*
35184610Salfred * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
36184610Salfred *
37184610Salfred * The EHCI 1.0 spec can be found at
38184610Salfred * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
39184610Salfred * and the USB 2.0 spec at
40184610Salfred * http://www.usb.org/developers/docs/usb_20.zip
41184610Salfred */
42184610Salfred
43184610Salfred/* The low level controller code for EHCI has been split into
44184610Salfred * PCI probes and EHCI specific code. This was done to facilitate the
45184610Salfred * sharing of code between *BSD's
46184610Salfred */
47184610Salfred
48194677Sthompsa#include <sys/stdint.h>
49194677Sthompsa#include <sys/stddef.h>
50194677Sthompsa#include <sys/param.h>
51194677Sthompsa#include <sys/queue.h>
52194677Sthompsa#include <sys/types.h>
53194677Sthompsa#include <sys/systm.h>
54194677Sthompsa#include <sys/kernel.h>
55194677Sthompsa#include <sys/bus.h>
56194677Sthompsa#include <sys/module.h>
57194677Sthompsa#include <sys/lock.h>
58194677Sthompsa#include <sys/mutex.h>
59194677Sthompsa#include <sys/condvar.h>
60194677Sthompsa#include <sys/sysctl.h>
61194677Sthompsa#include <sys/sx.h>
62194677Sthompsa#include <sys/unistd.h>
63194677Sthompsa#include <sys/callout.h>
64194677Sthompsa#include <sys/malloc.h>
65194677Sthompsa#include <sys/priv.h>
66194677Sthompsa
67188942Sthompsa#include <dev/usb/usb.h>
68194677Sthompsa#include <dev/usb/usbdi.h>
69184610Salfred
70188942Sthompsa#include <dev/usb/usb_core.h>
71188942Sthompsa#include <dev/usb/usb_busdma.h>
72188942Sthompsa#include <dev/usb/usb_process.h>
73188942Sthompsa#include <dev/usb/usb_util.h>
74184610Salfred
75188942Sthompsa#include <dev/usb/usb_controller.h>
76188942Sthompsa#include <dev/usb/usb_bus.h>
77188942Sthompsa#include <dev/usb/usb_pci.h>
78188942Sthompsa#include <dev/usb/controller/ehci.h>
79198151Sthompsa#include <dev/usb/controller/ehcireg.h>
80228483Shselasky#include "usb_if.h"
81184610Salfred
82184610Salfred#define	PCI_EHCI_VENDORID_ACERLABS	0x10b9
83184610Salfred#define	PCI_EHCI_VENDORID_AMD		0x1022
84184610Salfred#define	PCI_EHCI_VENDORID_APPLE		0x106b
85184610Salfred#define	PCI_EHCI_VENDORID_ATI		0x1002
86184610Salfred#define	PCI_EHCI_VENDORID_CMDTECH	0x1095
87184610Salfred#define	PCI_EHCI_VENDORID_INTEL		0x8086
88184610Salfred#define	PCI_EHCI_VENDORID_NEC		0x1033
89184610Salfred#define	PCI_EHCI_VENDORID_OPTI		0x1045
90184610Salfred#define	PCI_EHCI_VENDORID_PHILIPS	0x1131
91184610Salfred#define	PCI_EHCI_VENDORID_SIS		0x1039
92184610Salfred#define	PCI_EHCI_VENDORID_NVIDIA	0x12D2
93184610Salfred#define	PCI_EHCI_VENDORID_NVIDIA2	0x10DE
94184610Salfred#define	PCI_EHCI_VENDORID_VIA		0x1106
95184610Salfred
96184610Salfredstatic device_probe_t ehci_pci_probe;
97184610Salfredstatic device_attach_t ehci_pci_attach;
98184610Salfredstatic device_detach_t ehci_pci_detach;
99228483Shselaskystatic usb_take_controller_t ehci_pci_take_controller;
100184610Salfred
101184610Salfredstatic const char *
102184610Salfredehci_pci_match(device_t self)
103184610Salfred{
104184610Salfred	uint32_t device_id = pci_get_devid(self);
105184610Salfred
106184610Salfred	switch (device_id) {
107184610Salfred	case 0x523910b9:
108184610Salfred		return "ALi M5239 USB 2.0 controller";
109184610Salfred
110184610Salfred	case 0x10227463:
111184610Salfred		return "AMD 8111 USB 2.0 controller";
112184610Salfred
113184610Salfred	case 0x20951022:
114184610Salfred		return ("AMD CS5536 (Geode) USB 2.0 controller");
115184610Salfred
116184610Salfred	case 0x43451002:
117184610Salfred		return "ATI SB200 USB 2.0 controller";
118184610Salfred	case 0x43731002:
119184610Salfred		return "ATI SB400 USB 2.0 controller";
120238015Smav	case 0x43961002:
121238015Smav		return ("AMD SB7x0/SB8x0/SB9x0 USB 2.0 controller");
122184610Salfred
123276968Shselasky	case 0x0f348086:
124276968Shselasky		return ("Intel BayTrail USB 2.0 controller");
125276968Shselasky	case 0x1d268086:
126276968Shselasky		return ("Intel Patsburg USB 2.0 controller");
127276968Shselasky	case 0x1d2d8086:
128276968Shselasky		return ("Intel Patsburg USB 2.0 controller");
129238015Smav	case 0x1e268086:
130238015Smav		return ("Intel Panther Point USB 2.0 controller");
131238015Smav	case 0x1e2d8086:
132238015Smav		return ("Intel Panther Point USB 2.0 controller");
133260348Smav	case 0x1f2c8086:
134260348Smav		return ("Intel Avoton USB 2.0 controller");
135184610Salfred	case 0x25ad8086:
136184610Salfred		return "Intel 6300ESB USB 2.0 controller";
137184610Salfred	case 0x24cd8086:
138184610Salfred		return "Intel 82801DB/L/M (ICH4) USB 2.0 controller";
139184610Salfred	case 0x24dd8086:
140184610Salfred		return "Intel 82801EB/R (ICH5) USB 2.0 controller";
141184610Salfred	case 0x265c8086:
142184610Salfred		return "Intel 82801FB (ICH6) USB 2.0 controller";
143238015Smav	case 0x268c8086:
144238015Smav		return ("Intel 63XXESB USB 2.0 controller");
145184610Salfred	case 0x27cc8086:
146184610Salfred		return "Intel 82801GB/R (ICH7) USB 2.0 controller";
147184610Salfred	case 0x28368086:
148184610Salfred		return "Intel 82801H (ICH8) USB 2.0 controller USB2-A";
149184610Salfred	case 0x283a8086:
150184610Salfred		return "Intel 82801H (ICH8) USB 2.0 controller USB2-B";
151184610Salfred	case 0x293a8086:
152184610Salfred		return "Intel 82801I (ICH9) USB 2.0 controller";
153184610Salfred	case 0x293c8086:
154184610Salfred		return "Intel 82801I (ICH9) USB 2.0 controller";
155200087Sthompsa	case 0x3a3a8086:
156200305Sthompsa		return "Intel 82801JI (ICH10) USB 2.0 controller USB-A";
157200087Sthompsa	case 0x3a3c8086:
158200305Sthompsa		return "Intel 82801JI (ICH10) USB 2.0 controller USB-B";
159200822Sthompsa	case 0x3b348086:
160200822Sthompsa		return ("Intel PCH USB 2.0 controller USB-A");
161200822Sthompsa	case 0x3b3c8086:
162200822Sthompsa		return ("Intel PCH USB 2.0 controller USB-B");
163269864Shselasky	case 0x8c268086:
164269864Shselasky		return ("Intel Lynx Point USB 2.0 controller USB-A");
165269864Shselasky	case 0x8c2d8086:
166269864Shselasky		return ("Intel Lynx Point USB 2.0 controller USB-B");
167275439Smav	case 0x8ca68086:
168275439Smav		return ("Intel Wildcat Point USB 2.0 controller USB-A");
169275439Smav	case 0x8cad8086:
170275439Smav		return ("Intel Wildcat Point USB 2.0 controller USB-B");
171184610Salfred
172184610Salfred	case 0x00e01033:
173184610Salfred		return ("NEC uPD 720100 USB 2.0 controller");
174184610Salfred
175184610Salfred	case 0x006810de:
176184610Salfred		return "NVIDIA nForce2 USB 2.0 controller";
177184610Salfred	case 0x008810de:
178184610Salfred		return "NVIDIA nForce2 Ultra 400 USB 2.0 controller";
179184610Salfred	case 0x00d810de:
180184610Salfred		return "NVIDIA nForce3 USB 2.0 controller";
181184610Salfred	case 0x00e810de:
182184610Salfred		return "NVIDIA nForce3 250 USB 2.0 controller";
183184610Salfred	case 0x005b10de:
184254438Shselasky		return "NVIDIA nForce CK804 USB 2.0 controller";
185205802Sthompsa	case 0x036d10de:
186205802Sthompsa		return "NVIDIA nForce MCP55 USB 2.0 controller";
187195958Salfred	case 0x03f210de:
188195958Salfred		return "NVIDIA nForce MCP61 USB 2.0 controller";
189200822Sthompsa	case 0x0aa610de:
190200822Sthompsa		return "NVIDIA nForce MCP79 USB 2.0 controller";
191200822Sthompsa	case 0x0aa910de:
192200822Sthompsa		return "NVIDIA nForce MCP79 USB 2.0 controller";
193200822Sthompsa	case 0x0aaa10de:
194200822Sthompsa		return "NVIDIA nForce MCP79 USB 2.0 controller";
195184610Salfred
196184610Salfred	case 0x15621131:
197184610Salfred		return "Philips ISP156x USB 2.0 controller";
198184610Salfred
199184610Salfred	case 0x31041106:
200184610Salfred		return ("VIA VT6202 USB 2.0 controller");
201184610Salfred
202184610Salfred	default:
203184610Salfred		break;
204184610Salfred	}
205184610Salfred
206184610Salfred	if ((pci_get_class(self) == PCIC_SERIALBUS)
207184610Salfred	    && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
208184610Salfred	    && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) {
209184610Salfred		return ("EHCI (generic) USB 2.0 controller");
210184610Salfred	}
211184610Salfred	return (NULL);			/* dunno */
212184610Salfred}
213184610Salfred
214184610Salfredstatic int
215184610Salfredehci_pci_probe(device_t self)
216184610Salfred{
217184610Salfred	const char *desc = ehci_pci_match(self);
218184610Salfred
219184610Salfred	if (desc) {
220184610Salfred		device_set_desc(self, desc);
221184610Salfred		return (0);
222184610Salfred	} else {
223184610Salfred		return (ENXIO);
224184610Salfred	}
225184610Salfred}
226184610Salfred
227197554Sthompsastatic void
228197554Sthompsaehci_pci_ati_quirk(device_t self, uint8_t is_sb700)
229197554Sthompsa{
230197554Sthompsa	device_t smbdev;
231197554Sthompsa	uint32_t val;
232197554Sthompsa
233197554Sthompsa	if (is_sb700) {
234197554Sthompsa		/* Lookup SMBUS PCI device */
235197554Sthompsa		smbdev = pci_find_device(PCI_EHCI_VENDORID_ATI, 0x4385);
236197554Sthompsa		if (smbdev == NULL)
237197554Sthompsa			return;
238197554Sthompsa		val = pci_get_revid(smbdev);
239197554Sthompsa		if (val != 0x3a && val != 0x3b)
240197554Sthompsa			return;
241197554Sthompsa	}
242197554Sthompsa
243197554Sthompsa	/*
244197554Sthompsa	 * Note: this bit is described as reserved in SB700
245197554Sthompsa	 * Register Reference Guide.
246197554Sthompsa	 */
247197554Sthompsa	val = pci_read_config(self, 0x53, 1);
248197554Sthompsa	if (!(val & 0x8)) {
249197554Sthompsa		val |= 0x8;
250197554Sthompsa		pci_write_config(self, 0x53, val, 1);
251197554Sthompsa		device_printf(self, "AMD SB600/700 quirk applied\n");
252197554Sthompsa	}
253197554Sthompsa}
254197554Sthompsa
255197554Sthompsastatic void
256197554Sthompsaehci_pci_via_quirk(device_t self)
257197554Sthompsa{
258197554Sthompsa	uint32_t val;
259197554Sthompsa
260197554Sthompsa	if ((pci_get_device(self) == 0x3104) &&
261197554Sthompsa	    ((pci_get_revid(self) & 0xf0) == 0x60)) {
262197554Sthompsa		/* Correct schedule sleep time to 10us */
263197554Sthompsa		val = pci_read_config(self, 0x4b, 1);
264197554Sthompsa		if (val & 0x20)
265197554Sthompsa			return;
266236073Smarius		val |= 0x20;
267236073Smarius		pci_write_config(self, 0x4b, val, 1);
268197554Sthompsa		device_printf(self, "VIA-quirk applied\n");
269197554Sthompsa	}
270197554Sthompsa}
271197554Sthompsa
272184610Salfredstatic int
273184610Salfredehci_pci_attach(device_t self)
274184610Salfred{
275184610Salfred	ehci_softc_t *sc = device_get_softc(self);
276184610Salfred	int err;
277184610Salfred	int rid;
278184610Salfred
279187170Sthompsa	/* initialise some bus fields */
280187170Sthompsa	sc->sc_bus.parent = self;
281187170Sthompsa	sc->sc_bus.devices = sc->sc_devices;
282187170Sthompsa	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
283278278Shselasky	sc->sc_bus.dma_bits = 32;
284187170Sthompsa
285184610Salfred	/* get all DMA memory */
286194228Sthompsa	if (usb_bus_mem_alloc_all(&sc->sc_bus,
287184610Salfred	    USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
288187170Sthompsa		return (ENOMEM);
289184610Salfred	}
290184610Salfred
291184610Salfred	pci_enable_busmaster(self);
292184610Salfred
293184610Salfred	switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
294184610Salfred	case PCI_USB_REV_PRE_1_0:
295184610Salfred	case PCI_USB_REV_1_0:
296184610Salfred	case PCI_USB_REV_1_1:
297184610Salfred		/*
298184610Salfred		 * NOTE: some EHCI USB controllers have the wrong USB
299184610Salfred		 * revision number. It appears those controllers are
300184610Salfred		 * fully compliant so we just ignore this value in
301184610Salfred		 * some common cases.
302184610Salfred		 */
303184610Salfred		device_printf(self, "pre-2.0 USB revision (ignored)\n");
304184610Salfred		/* fallthrough */
305184610Salfred	case PCI_USB_REV_2_0:
306184610Salfred		break;
307184610Salfred	default:
308187186Sthompsa		/* Quirk for Parallels Desktop 4.0 */
309187186Sthompsa		device_printf(self, "USB revision is unknown. Assuming v2.0.\n");
310199057Sthompsa		break;
311184610Salfred	}
312184610Salfred
313184610Salfred	rid = PCI_CBMEM;
314184610Salfred	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
315184610Salfred	    RF_ACTIVE);
316184610Salfred	if (!sc->sc_io_res) {
317184610Salfred		device_printf(self, "Could not map memory\n");
318184610Salfred		goto error;
319184610Salfred	}
320184610Salfred	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
321184610Salfred	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
322184610Salfred	sc->sc_io_size = rman_get_size(sc->sc_io_res);
323184610Salfred
324184610Salfred	rid = 0;
325184610Salfred	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
326184610Salfred	    RF_SHAREABLE | RF_ACTIVE);
327184610Salfred	if (sc->sc_irq_res == NULL) {
328184610Salfred		device_printf(self, "Could not allocate irq\n");
329184610Salfred		goto error;
330184610Salfred	}
331184610Salfred	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
332184610Salfred	if (!sc->sc_bus.bdev) {
333184610Salfred		device_printf(self, "Could not add USB device\n");
334184610Salfred		goto error;
335184610Salfred	}
336184610Salfred	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
337184610Salfred
338184610Salfred	/*
339184610Salfred	 * ehci_pci_match will never return NULL if ehci_pci_probe
340184610Salfred	 * succeeded
341184610Salfred	 */
342184610Salfred	device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
343184610Salfred	switch (pci_get_vendor(self)) {
344184610Salfred	case PCI_EHCI_VENDORID_ACERLABS:
345184610Salfred		sprintf(sc->sc_vendor, "AcerLabs");
346184610Salfred		break;
347184610Salfred	case PCI_EHCI_VENDORID_AMD:
348184610Salfred		sprintf(sc->sc_vendor, "AMD");
349184610Salfred		break;
350184610Salfred	case PCI_EHCI_VENDORID_APPLE:
351184610Salfred		sprintf(sc->sc_vendor, "Apple");
352184610Salfred		break;
353184610Salfred	case PCI_EHCI_VENDORID_ATI:
354184610Salfred		sprintf(sc->sc_vendor, "ATI");
355184610Salfred		break;
356184610Salfred	case PCI_EHCI_VENDORID_CMDTECH:
357184610Salfred		sprintf(sc->sc_vendor, "CMDTECH");
358184610Salfred		break;
359184610Salfred	case PCI_EHCI_VENDORID_INTEL:
360184610Salfred		sprintf(sc->sc_vendor, "Intel");
361184610Salfred		break;
362184610Salfred	case PCI_EHCI_VENDORID_NEC:
363184610Salfred		sprintf(sc->sc_vendor, "NEC");
364184610Salfred		break;
365184610Salfred	case PCI_EHCI_VENDORID_OPTI:
366184610Salfred		sprintf(sc->sc_vendor, "OPTi");
367184610Salfred		break;
368184610Salfred	case PCI_EHCI_VENDORID_PHILIPS:
369184610Salfred		sprintf(sc->sc_vendor, "Philips");
370184610Salfred		break;
371184610Salfred	case PCI_EHCI_VENDORID_SIS:
372184610Salfred		sprintf(sc->sc_vendor, "SiS");
373184610Salfred		break;
374184610Salfred	case PCI_EHCI_VENDORID_NVIDIA:
375184610Salfred	case PCI_EHCI_VENDORID_NVIDIA2:
376184610Salfred		sprintf(sc->sc_vendor, "nVidia");
377184610Salfred		break;
378184610Salfred	case PCI_EHCI_VENDORID_VIA:
379184610Salfred		sprintf(sc->sc_vendor, "VIA");
380184610Salfred		break;
381184610Salfred	default:
382184610Salfred		if (bootverbose)
383184610Salfred			device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
384184610Salfred			    pci_get_devid(self));
385184610Salfred		sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
386184610Salfred	}
387184610Salfred
388184610Salfred#if (__FreeBSD_version >= 700031)
389184610Salfred	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
390190183Sthompsa	    NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
391184610Salfred#else
392184610Salfred	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
393190183Sthompsa	    (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
394184610Salfred#endif
395184610Salfred	if (err) {
396184610Salfred		device_printf(self, "Could not setup irq, %d\n", err);
397184610Salfred		sc->sc_intr_hdl = NULL;
398184610Salfred		goto error;
399184610Salfred	}
400228483Shselasky	ehci_pci_take_controller(self);
401197554Sthompsa
402197554Sthompsa	/* Undocumented quirks taken from Linux */
403197554Sthompsa
404197554Sthompsa	switch (pci_get_vendor(self)) {
405197554Sthompsa	case PCI_EHCI_VENDORID_ATI:
406197554Sthompsa		/* SB600 and SB700 EHCI quirk */
407197554Sthompsa		switch (pci_get_device(self)) {
408197554Sthompsa		case 0x4386:
409197554Sthompsa			ehci_pci_ati_quirk(self, 0);
410197554Sthompsa			break;
411197554Sthompsa		case 0x4396:
412197554Sthompsa			ehci_pci_ati_quirk(self, 1);
413197554Sthompsa			break;
414197554Sthompsa		default:
415197554Sthompsa			break;
416197554Sthompsa		}
417197554Sthompsa		break;
418197554Sthompsa
419197554Sthompsa	case PCI_EHCI_VENDORID_VIA:
420197554Sthompsa		ehci_pci_via_quirk(self);
421197554Sthompsa		break;
422197554Sthompsa
423197554Sthompsa	default:
424197554Sthompsa		break;
425197554Sthompsa	}
426197554Sthompsa
427199058Sthompsa	/* Dropped interrupts workaround */
428199058Sthompsa	switch (pci_get_vendor(self)) {
429199058Sthompsa	case PCI_EHCI_VENDORID_ATI:
430199058Sthompsa	case PCI_EHCI_VENDORID_VIA:
431199058Sthompsa		sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
432199058Sthompsa		if (bootverbose)
433199058Sthompsa			device_printf(self,
434199058Sthompsa			    "Dropped interrupts workaround enabled\n");
435199058Sthompsa		break;
436199058Sthompsa	default:
437199058Sthompsa		break;
438199058Sthompsa	}
439199058Sthompsa
440203693Sthompsa	/* Doorbell feature workaround */
441203693Sthompsa	switch (pci_get_vendor(self)) {
442203693Sthompsa	case PCI_EHCI_VENDORID_NVIDIA:
443203693Sthompsa	case PCI_EHCI_VENDORID_NVIDIA2:
444203693Sthompsa		sc->sc_flags |= EHCI_SCFLG_IAADBUG;
445203693Sthompsa		if (bootverbose)
446203693Sthompsa			device_printf(self,
447203693Sthompsa			    "Doorbell workaround enabled\n");
448203693Sthompsa		break;
449203693Sthompsa	default:
450203693Sthompsa		break;
451203693Sthompsa	}
452203693Sthompsa
453184610Salfred	err = ehci_init(sc);
454184610Salfred	if (!err) {
455184610Salfred		err = device_probe_and_attach(sc->sc_bus.bdev);
456184610Salfred	}
457184610Salfred	if (err) {
458184610Salfred		device_printf(self, "USB init failed err=%d\n", err);
459184610Salfred		goto error;
460184610Salfred	}
461184610Salfred	return (0);
462184610Salfred
463184610Salfrederror:
464184610Salfred	ehci_pci_detach(self);
465184610Salfred	return (ENXIO);
466184610Salfred}
467184610Salfred
468184610Salfredstatic int
469184610Salfredehci_pci_detach(device_t self)
470184610Salfred{
471184610Salfred	ehci_softc_t *sc = device_get_softc(self);
472184610Salfred	device_t bdev;
473184610Salfred
474184610Salfred	if (sc->sc_bus.bdev) {
475184610Salfred		bdev = sc->sc_bus.bdev;
476184610Salfred		device_detach(bdev);
477184610Salfred		device_delete_child(self, bdev);
478184610Salfred	}
479184610Salfred	/* during module unload there are lots of children leftover */
480227849Shselasky	device_delete_children(self);
481184610Salfred
482184610Salfred	pci_disable_busmaster(self);
483184610Salfred
484184610Salfred	if (sc->sc_irq_res && sc->sc_intr_hdl) {
485184610Salfred		/*
486184610Salfred		 * only call ehci_detach() after ehci_init()
487184610Salfred		 */
488184610Salfred		ehci_detach(sc);
489184610Salfred
490184610Salfred		int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
491184610Salfred
492184610Salfred		if (err)
493184610Salfred			/* XXX or should we panic? */
494184610Salfred			device_printf(self, "Could not tear down irq, %d\n",
495184610Salfred			    err);
496184610Salfred		sc->sc_intr_hdl = NULL;
497184610Salfred	}
498184610Salfred	if (sc->sc_irq_res) {
499184610Salfred		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
500184610Salfred		sc->sc_irq_res = NULL;
501184610Salfred	}
502184610Salfred	if (sc->sc_io_res) {
503184610Salfred		bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
504184610Salfred		    sc->sc_io_res);
505184610Salfred		sc->sc_io_res = NULL;
506184610Salfred	}
507194228Sthompsa	usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
508184610Salfred
509184610Salfred	return (0);
510184610Salfred}
511184610Salfred
512228483Shselaskystatic int
513228483Shselaskyehci_pci_take_controller(device_t self)
514198501Sthompsa{
515198501Sthompsa	ehci_softc_t *sc = device_get_softc(self);
516198501Sthompsa	uint32_t cparams;
517198501Sthompsa	uint32_t eec;
518198501Sthompsa	uint16_t to;
519198501Sthompsa	uint8_t eecp;
520198501Sthompsa	uint8_t bios_sem;
521198501Sthompsa
522198501Sthompsa	cparams = EREAD4(sc, EHCI_HCCPARAMS);
523198501Sthompsa
524198501Sthompsa	/* Synchronise with the BIOS if it owns the controller. */
525198501Sthompsa	for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
526198501Sthompsa	    eecp = EHCI_EECP_NEXT(eec)) {
527198501Sthompsa		eec = pci_read_config(self, eecp, 4);
528198501Sthompsa		if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) {
529198501Sthompsa			continue;
530198501Sthompsa		}
531198501Sthompsa		bios_sem = pci_read_config(self, eecp +
532198501Sthompsa		    EHCI_LEGSUP_BIOS_SEM, 1);
533198501Sthompsa		if (bios_sem == 0) {
534198501Sthompsa			continue;
535198501Sthompsa		}
536198501Sthompsa		device_printf(sc->sc_bus.bdev, "waiting for BIOS "
537198501Sthompsa		    "to give up control\n");
538198501Sthompsa		pci_write_config(self, eecp +
539198501Sthompsa		    EHCI_LEGSUP_OS_SEM, 1, 1);
540198501Sthompsa		to = 500;
541198501Sthompsa		while (1) {
542198501Sthompsa			bios_sem = pci_read_config(self, eecp +
543198501Sthompsa			    EHCI_LEGSUP_BIOS_SEM, 1);
544198501Sthompsa			if (bios_sem == 0)
545198501Sthompsa				break;
546198501Sthompsa
547198501Sthompsa			if (--to == 0) {
548198501Sthompsa				device_printf(sc->sc_bus.bdev,
549198501Sthompsa				    "timed out waiting for BIOS\n");
550198501Sthompsa				break;
551198501Sthompsa			}
552198501Sthompsa			usb_pause_mtx(NULL, hz / 100);	/* wait 10ms */
553198501Sthompsa		}
554198501Sthompsa	}
555228483Shselasky	return (0);
556198501Sthompsa}
557198501Sthompsa
558228483Shselaskystatic device_method_t ehci_pci_methods[] = {
559228483Shselasky	/* Device interface */
560228483Shselasky	DEVMETHOD(device_probe, ehci_pci_probe),
561228483Shselasky	DEVMETHOD(device_attach, ehci_pci_attach),
562228483Shselasky	DEVMETHOD(device_detach, ehci_pci_detach),
563228483Shselasky	DEVMETHOD(device_suspend, bus_generic_suspend),
564228483Shselasky	DEVMETHOD(device_resume, bus_generic_resume),
565228483Shselasky	DEVMETHOD(device_shutdown, bus_generic_shutdown),
566228483Shselasky	DEVMETHOD(usb_take_controller, ehci_pci_take_controller),
567228483Shselasky
568228483Shselasky	DEVMETHOD_END
569228483Shselasky};
570228483Shselasky
571228483Shselaskystatic driver_t ehci_driver = {
572184610Salfred	.name = "ehci",
573228483Shselasky	.methods = ehci_pci_methods,
574184610Salfred	.size = sizeof(struct ehci_softc),
575184610Salfred};
576184610Salfred
577184610Salfredstatic devclass_t ehci_devclass;
578184610Salfred
579184610SalfredDRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
580188942SthompsaMODULE_DEPEND(ehci, usb, 1, 1, 1);
581