ehci_pci.c revision 260348
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: stable/10/sys/dev/usb/controller/ehci_pci.c 260348 2014-01-05 22:49:54Z mav $");
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
123238015Smav	case 0x1e268086:
124238015Smav		return ("Intel Panther Point USB 2.0 controller");
125238015Smav	case 0x1e2d8086:
126238015Smav		return ("Intel Panther Point USB 2.0 controller");
127260348Smav	case 0x1f2c8086:
128260348Smav		return ("Intel Avoton USB 2.0 controller");
129184610Salfred	case 0x25ad8086:
130184610Salfred		return "Intel 6300ESB USB 2.0 controller";
131184610Salfred	case 0x24cd8086:
132184610Salfred		return "Intel 82801DB/L/M (ICH4) USB 2.0 controller";
133184610Salfred	case 0x24dd8086:
134184610Salfred		return "Intel 82801EB/R (ICH5) USB 2.0 controller";
135184610Salfred	case 0x265c8086:
136184610Salfred		return "Intel 82801FB (ICH6) USB 2.0 controller";
137238015Smav	case 0x268c8086:
138238015Smav		return ("Intel 63XXESB USB 2.0 controller");
139184610Salfred	case 0x27cc8086:
140184610Salfred		return "Intel 82801GB/R (ICH7) USB 2.0 controller";
141184610Salfred	case 0x28368086:
142184610Salfred		return "Intel 82801H (ICH8) USB 2.0 controller USB2-A";
143184610Salfred	case 0x283a8086:
144184610Salfred		return "Intel 82801H (ICH8) USB 2.0 controller USB2-B";
145184610Salfred	case 0x293a8086:
146184610Salfred		return "Intel 82801I (ICH9) USB 2.0 controller";
147184610Salfred	case 0x293c8086:
148184610Salfred		return "Intel 82801I (ICH9) USB 2.0 controller";
149200087Sthompsa	case 0x3a3a8086:
150200305Sthompsa		return "Intel 82801JI (ICH10) USB 2.0 controller USB-A";
151200087Sthompsa	case 0x3a3c8086:
152200305Sthompsa		return "Intel 82801JI (ICH10) USB 2.0 controller USB-B";
153200822Sthompsa	case 0x3b348086:
154200822Sthompsa		return ("Intel PCH USB 2.0 controller USB-A");
155200822Sthompsa	case 0x3b3c8086:
156200822Sthompsa		return ("Intel PCH USB 2.0 controller USB-B");
157184610Salfred
158184610Salfred	case 0x00e01033:
159184610Salfred		return ("NEC uPD 720100 USB 2.0 controller");
160184610Salfred
161184610Salfred	case 0x006810de:
162184610Salfred		return "NVIDIA nForce2 USB 2.0 controller";
163184610Salfred	case 0x008810de:
164184610Salfred		return "NVIDIA nForce2 Ultra 400 USB 2.0 controller";
165184610Salfred	case 0x00d810de:
166184610Salfred		return "NVIDIA nForce3 USB 2.0 controller";
167184610Salfred	case 0x00e810de:
168184610Salfred		return "NVIDIA nForce3 250 USB 2.0 controller";
169184610Salfred	case 0x005b10de:
170254438Shselasky		return "NVIDIA nForce CK804 USB 2.0 controller";
171205802Sthompsa	case 0x036d10de:
172205802Sthompsa		return "NVIDIA nForce MCP55 USB 2.0 controller";
173195958Salfred	case 0x03f210de:
174195958Salfred		return "NVIDIA nForce MCP61 USB 2.0 controller";
175200822Sthompsa	case 0x0aa610de:
176200822Sthompsa		return "NVIDIA nForce MCP79 USB 2.0 controller";
177200822Sthompsa	case 0x0aa910de:
178200822Sthompsa		return "NVIDIA nForce MCP79 USB 2.0 controller";
179200822Sthompsa	case 0x0aaa10de:
180200822Sthompsa		return "NVIDIA nForce MCP79 USB 2.0 controller";
181184610Salfred
182184610Salfred	case 0x15621131:
183184610Salfred		return "Philips ISP156x USB 2.0 controller";
184184610Salfred
185184610Salfred	case 0x31041106:
186184610Salfred		return ("VIA VT6202 USB 2.0 controller");
187184610Salfred
188184610Salfred	default:
189184610Salfred		break;
190184610Salfred	}
191184610Salfred
192184610Salfred	if ((pci_get_class(self) == PCIC_SERIALBUS)
193184610Salfred	    && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
194184610Salfred	    && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) {
195184610Salfred		return ("EHCI (generic) USB 2.0 controller");
196184610Salfred	}
197184610Salfred	return (NULL);			/* dunno */
198184610Salfred}
199184610Salfred
200184610Salfredstatic int
201184610Salfredehci_pci_probe(device_t self)
202184610Salfred{
203184610Salfred	const char *desc = ehci_pci_match(self);
204184610Salfred
205184610Salfred	if (desc) {
206184610Salfred		device_set_desc(self, desc);
207184610Salfred		return (0);
208184610Salfred	} else {
209184610Salfred		return (ENXIO);
210184610Salfred	}
211184610Salfred}
212184610Salfred
213197554Sthompsastatic void
214197554Sthompsaehci_pci_ati_quirk(device_t self, uint8_t is_sb700)
215197554Sthompsa{
216197554Sthompsa	device_t smbdev;
217197554Sthompsa	uint32_t val;
218197554Sthompsa
219197554Sthompsa	if (is_sb700) {
220197554Sthompsa		/* Lookup SMBUS PCI device */
221197554Sthompsa		smbdev = pci_find_device(PCI_EHCI_VENDORID_ATI, 0x4385);
222197554Sthompsa		if (smbdev == NULL)
223197554Sthompsa			return;
224197554Sthompsa		val = pci_get_revid(smbdev);
225197554Sthompsa		if (val != 0x3a && val != 0x3b)
226197554Sthompsa			return;
227197554Sthompsa	}
228197554Sthompsa
229197554Sthompsa	/*
230197554Sthompsa	 * Note: this bit is described as reserved in SB700
231197554Sthompsa	 * Register Reference Guide.
232197554Sthompsa	 */
233197554Sthompsa	val = pci_read_config(self, 0x53, 1);
234197554Sthompsa	if (!(val & 0x8)) {
235197554Sthompsa		val |= 0x8;
236197554Sthompsa		pci_write_config(self, 0x53, val, 1);
237197554Sthompsa		device_printf(self, "AMD SB600/700 quirk applied\n");
238197554Sthompsa	}
239197554Sthompsa}
240197554Sthompsa
241197554Sthompsastatic void
242197554Sthompsaehci_pci_via_quirk(device_t self)
243197554Sthompsa{
244197554Sthompsa	uint32_t val;
245197554Sthompsa
246197554Sthompsa	if ((pci_get_device(self) == 0x3104) &&
247197554Sthompsa	    ((pci_get_revid(self) & 0xf0) == 0x60)) {
248197554Sthompsa		/* Correct schedule sleep time to 10us */
249197554Sthompsa		val = pci_read_config(self, 0x4b, 1);
250197554Sthompsa		if (val & 0x20)
251197554Sthompsa			return;
252236073Smarius		val |= 0x20;
253236073Smarius		pci_write_config(self, 0x4b, val, 1);
254197554Sthompsa		device_printf(self, "VIA-quirk applied\n");
255197554Sthompsa	}
256197554Sthompsa}
257197554Sthompsa
258184610Salfredstatic int
259184610Salfredehci_pci_attach(device_t self)
260184610Salfred{
261184610Salfred	ehci_softc_t *sc = device_get_softc(self);
262184610Salfred	int err;
263184610Salfred	int rid;
264184610Salfred
265187170Sthompsa	/* initialise some bus fields */
266187170Sthompsa	sc->sc_bus.parent = self;
267187170Sthompsa	sc->sc_bus.devices = sc->sc_devices;
268187170Sthompsa	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
269187170Sthompsa
270184610Salfred	/* get all DMA memory */
271194228Sthompsa	if (usb_bus_mem_alloc_all(&sc->sc_bus,
272184610Salfred	    USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
273187170Sthompsa		return (ENOMEM);
274184610Salfred	}
275184610Salfred
276184610Salfred	pci_enable_busmaster(self);
277184610Salfred
278184610Salfred	switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
279184610Salfred	case PCI_USB_REV_PRE_1_0:
280184610Salfred	case PCI_USB_REV_1_0:
281184610Salfred	case PCI_USB_REV_1_1:
282184610Salfred		/*
283184610Salfred		 * NOTE: some EHCI USB controllers have the wrong USB
284184610Salfred		 * revision number. It appears those controllers are
285184610Salfred		 * fully compliant so we just ignore this value in
286184610Salfred		 * some common cases.
287184610Salfred		 */
288184610Salfred		device_printf(self, "pre-2.0 USB revision (ignored)\n");
289184610Salfred		/* fallthrough */
290184610Salfred	case PCI_USB_REV_2_0:
291184610Salfred		break;
292184610Salfred	default:
293187186Sthompsa		/* Quirk for Parallels Desktop 4.0 */
294187186Sthompsa		device_printf(self, "USB revision is unknown. Assuming v2.0.\n");
295199057Sthompsa		break;
296184610Salfred	}
297184610Salfred
298184610Salfred	rid = PCI_CBMEM;
299184610Salfred	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
300184610Salfred	    RF_ACTIVE);
301184610Salfred	if (!sc->sc_io_res) {
302184610Salfred		device_printf(self, "Could not map memory\n");
303184610Salfred		goto error;
304184610Salfred	}
305184610Salfred	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
306184610Salfred	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
307184610Salfred	sc->sc_io_size = rman_get_size(sc->sc_io_res);
308184610Salfred
309184610Salfred	rid = 0;
310184610Salfred	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
311184610Salfred	    RF_SHAREABLE | RF_ACTIVE);
312184610Salfred	if (sc->sc_irq_res == NULL) {
313184610Salfred		device_printf(self, "Could not allocate irq\n");
314184610Salfred		goto error;
315184610Salfred	}
316184610Salfred	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
317184610Salfred	if (!sc->sc_bus.bdev) {
318184610Salfred		device_printf(self, "Could not add USB device\n");
319184610Salfred		goto error;
320184610Salfred	}
321184610Salfred	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
322184610Salfred
323184610Salfred	/*
324184610Salfred	 * ehci_pci_match will never return NULL if ehci_pci_probe
325184610Salfred	 * succeeded
326184610Salfred	 */
327184610Salfred	device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
328184610Salfred	switch (pci_get_vendor(self)) {
329184610Salfred	case PCI_EHCI_VENDORID_ACERLABS:
330184610Salfred		sprintf(sc->sc_vendor, "AcerLabs");
331184610Salfred		break;
332184610Salfred	case PCI_EHCI_VENDORID_AMD:
333184610Salfred		sprintf(sc->sc_vendor, "AMD");
334184610Salfred		break;
335184610Salfred	case PCI_EHCI_VENDORID_APPLE:
336184610Salfred		sprintf(sc->sc_vendor, "Apple");
337184610Salfred		break;
338184610Salfred	case PCI_EHCI_VENDORID_ATI:
339184610Salfred		sprintf(sc->sc_vendor, "ATI");
340184610Salfred		break;
341184610Salfred	case PCI_EHCI_VENDORID_CMDTECH:
342184610Salfred		sprintf(sc->sc_vendor, "CMDTECH");
343184610Salfred		break;
344184610Salfred	case PCI_EHCI_VENDORID_INTEL:
345184610Salfred		sprintf(sc->sc_vendor, "Intel");
346184610Salfred		break;
347184610Salfred	case PCI_EHCI_VENDORID_NEC:
348184610Salfred		sprintf(sc->sc_vendor, "NEC");
349184610Salfred		break;
350184610Salfred	case PCI_EHCI_VENDORID_OPTI:
351184610Salfred		sprintf(sc->sc_vendor, "OPTi");
352184610Salfred		break;
353184610Salfred	case PCI_EHCI_VENDORID_PHILIPS:
354184610Salfred		sprintf(sc->sc_vendor, "Philips");
355184610Salfred		break;
356184610Salfred	case PCI_EHCI_VENDORID_SIS:
357184610Salfred		sprintf(sc->sc_vendor, "SiS");
358184610Salfred		break;
359184610Salfred	case PCI_EHCI_VENDORID_NVIDIA:
360184610Salfred	case PCI_EHCI_VENDORID_NVIDIA2:
361184610Salfred		sprintf(sc->sc_vendor, "nVidia");
362184610Salfred		break;
363184610Salfred	case PCI_EHCI_VENDORID_VIA:
364184610Salfred		sprintf(sc->sc_vendor, "VIA");
365184610Salfred		break;
366184610Salfred	default:
367184610Salfred		if (bootverbose)
368184610Salfred			device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
369184610Salfred			    pci_get_devid(self));
370184610Salfred		sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
371184610Salfred	}
372184610Salfred
373184610Salfred#if (__FreeBSD_version >= 700031)
374184610Salfred	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
375190183Sthompsa	    NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
376184610Salfred#else
377184610Salfred	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
378190183Sthompsa	    (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
379184610Salfred#endif
380184610Salfred	if (err) {
381184610Salfred		device_printf(self, "Could not setup irq, %d\n", err);
382184610Salfred		sc->sc_intr_hdl = NULL;
383184610Salfred		goto error;
384184610Salfred	}
385228483Shselasky	ehci_pci_take_controller(self);
386197554Sthompsa
387197554Sthompsa	/* Undocumented quirks taken from Linux */
388197554Sthompsa
389197554Sthompsa	switch (pci_get_vendor(self)) {
390197554Sthompsa	case PCI_EHCI_VENDORID_ATI:
391197554Sthompsa		/* SB600 and SB700 EHCI quirk */
392197554Sthompsa		switch (pci_get_device(self)) {
393197554Sthompsa		case 0x4386:
394197554Sthompsa			ehci_pci_ati_quirk(self, 0);
395197554Sthompsa			break;
396197554Sthompsa		case 0x4396:
397197554Sthompsa			ehci_pci_ati_quirk(self, 1);
398197554Sthompsa			break;
399197554Sthompsa		default:
400197554Sthompsa			break;
401197554Sthompsa		}
402197554Sthompsa		break;
403197554Sthompsa
404197554Sthompsa	case PCI_EHCI_VENDORID_VIA:
405197554Sthompsa		ehci_pci_via_quirk(self);
406197554Sthompsa		break;
407197554Sthompsa
408197554Sthompsa	default:
409197554Sthompsa		break;
410197554Sthompsa	}
411197554Sthompsa
412199058Sthompsa	/* Dropped interrupts workaround */
413199058Sthompsa	switch (pci_get_vendor(self)) {
414199058Sthompsa	case PCI_EHCI_VENDORID_ATI:
415199058Sthompsa	case PCI_EHCI_VENDORID_VIA:
416199058Sthompsa		sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
417199058Sthompsa		if (bootverbose)
418199058Sthompsa			device_printf(self,
419199058Sthompsa			    "Dropped interrupts workaround enabled\n");
420199058Sthompsa		break;
421199058Sthompsa	default:
422199058Sthompsa		break;
423199058Sthompsa	}
424199058Sthompsa
425203693Sthompsa	/* Doorbell feature workaround */
426203693Sthompsa	switch (pci_get_vendor(self)) {
427203693Sthompsa	case PCI_EHCI_VENDORID_NVIDIA:
428203693Sthompsa	case PCI_EHCI_VENDORID_NVIDIA2:
429203693Sthompsa		sc->sc_flags |= EHCI_SCFLG_IAADBUG;
430203693Sthompsa		if (bootverbose)
431203693Sthompsa			device_printf(self,
432203693Sthompsa			    "Doorbell workaround enabled\n");
433203693Sthompsa		break;
434203693Sthompsa	default:
435203693Sthompsa		break;
436203693Sthompsa	}
437203693Sthompsa
438184610Salfred	err = ehci_init(sc);
439184610Salfred	if (!err) {
440184610Salfred		err = device_probe_and_attach(sc->sc_bus.bdev);
441184610Salfred	}
442184610Salfred	if (err) {
443184610Salfred		device_printf(self, "USB init failed err=%d\n", err);
444184610Salfred		goto error;
445184610Salfred	}
446184610Salfred	return (0);
447184610Salfred
448184610Salfrederror:
449184610Salfred	ehci_pci_detach(self);
450184610Salfred	return (ENXIO);
451184610Salfred}
452184610Salfred
453184610Salfredstatic int
454184610Salfredehci_pci_detach(device_t self)
455184610Salfred{
456184610Salfred	ehci_softc_t *sc = device_get_softc(self);
457184610Salfred	device_t bdev;
458184610Salfred
459184610Salfred	if (sc->sc_bus.bdev) {
460184610Salfred		bdev = sc->sc_bus.bdev;
461184610Salfred		device_detach(bdev);
462184610Salfred		device_delete_child(self, bdev);
463184610Salfred	}
464184610Salfred	/* during module unload there are lots of children leftover */
465227849Shselasky	device_delete_children(self);
466184610Salfred
467184610Salfred	pci_disable_busmaster(self);
468184610Salfred
469184610Salfred	if (sc->sc_irq_res && sc->sc_intr_hdl) {
470184610Salfred		/*
471184610Salfred		 * only call ehci_detach() after ehci_init()
472184610Salfred		 */
473184610Salfred		ehci_detach(sc);
474184610Salfred
475184610Salfred		int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
476184610Salfred
477184610Salfred		if (err)
478184610Salfred			/* XXX or should we panic? */
479184610Salfred			device_printf(self, "Could not tear down irq, %d\n",
480184610Salfred			    err);
481184610Salfred		sc->sc_intr_hdl = NULL;
482184610Salfred	}
483184610Salfred	if (sc->sc_irq_res) {
484184610Salfred		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
485184610Salfred		sc->sc_irq_res = NULL;
486184610Salfred	}
487184610Salfred	if (sc->sc_io_res) {
488184610Salfred		bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
489184610Salfred		    sc->sc_io_res);
490184610Salfred		sc->sc_io_res = NULL;
491184610Salfred	}
492194228Sthompsa	usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
493184610Salfred
494184610Salfred	return (0);
495184610Salfred}
496184610Salfred
497228483Shselaskystatic int
498228483Shselaskyehci_pci_take_controller(device_t self)
499198501Sthompsa{
500198501Sthompsa	ehci_softc_t *sc = device_get_softc(self);
501198501Sthompsa	uint32_t cparams;
502198501Sthompsa	uint32_t eec;
503198501Sthompsa	uint16_t to;
504198501Sthompsa	uint8_t eecp;
505198501Sthompsa	uint8_t bios_sem;
506198501Sthompsa
507198501Sthompsa	cparams = EREAD4(sc, EHCI_HCCPARAMS);
508198501Sthompsa
509198501Sthompsa	/* Synchronise with the BIOS if it owns the controller. */
510198501Sthompsa	for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
511198501Sthompsa	    eecp = EHCI_EECP_NEXT(eec)) {
512198501Sthompsa		eec = pci_read_config(self, eecp, 4);
513198501Sthompsa		if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) {
514198501Sthompsa			continue;
515198501Sthompsa		}
516198501Sthompsa		bios_sem = pci_read_config(self, eecp +
517198501Sthompsa		    EHCI_LEGSUP_BIOS_SEM, 1);
518198501Sthompsa		if (bios_sem == 0) {
519198501Sthompsa			continue;
520198501Sthompsa		}
521198501Sthompsa		device_printf(sc->sc_bus.bdev, "waiting for BIOS "
522198501Sthompsa		    "to give up control\n");
523198501Sthompsa		pci_write_config(self, eecp +
524198501Sthompsa		    EHCI_LEGSUP_OS_SEM, 1, 1);
525198501Sthompsa		to = 500;
526198501Sthompsa		while (1) {
527198501Sthompsa			bios_sem = pci_read_config(self, eecp +
528198501Sthompsa			    EHCI_LEGSUP_BIOS_SEM, 1);
529198501Sthompsa			if (bios_sem == 0)
530198501Sthompsa				break;
531198501Sthompsa
532198501Sthompsa			if (--to == 0) {
533198501Sthompsa				device_printf(sc->sc_bus.bdev,
534198501Sthompsa				    "timed out waiting for BIOS\n");
535198501Sthompsa				break;
536198501Sthompsa			}
537198501Sthompsa			usb_pause_mtx(NULL, hz / 100);	/* wait 10ms */
538198501Sthompsa		}
539198501Sthompsa	}
540228483Shselasky	return (0);
541198501Sthompsa}
542198501Sthompsa
543228483Shselaskystatic device_method_t ehci_pci_methods[] = {
544228483Shselasky	/* Device interface */
545228483Shselasky	DEVMETHOD(device_probe, ehci_pci_probe),
546228483Shselasky	DEVMETHOD(device_attach, ehci_pci_attach),
547228483Shselasky	DEVMETHOD(device_detach, ehci_pci_detach),
548228483Shselasky	DEVMETHOD(device_suspend, bus_generic_suspend),
549228483Shselasky	DEVMETHOD(device_resume, bus_generic_resume),
550228483Shselasky	DEVMETHOD(device_shutdown, bus_generic_shutdown),
551228483Shselasky	DEVMETHOD(usb_take_controller, ehci_pci_take_controller),
552228483Shselasky
553228483Shselasky	DEVMETHOD_END
554228483Shselasky};
555228483Shselasky
556228483Shselaskystatic driver_t ehci_driver = {
557184610Salfred	.name = "ehci",
558228483Shselasky	.methods = ehci_pci_methods,
559184610Salfred	.size = sizeof(struct ehci_softc),
560184610Salfred};
561184610Salfred
562184610Salfredstatic devclass_t ehci_devclass;
563184610Salfred
564184610SalfredDRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
565188942SthompsaMODULE_DEPEND(ehci, usb, 1, 1, 1);
566