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