ehci_pci.c revision 228483
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: head/sys/dev/usb/controller/ehci_pci.c 228483 2011-12-14 00:28:54Z 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 0x268c8086:
108184610Salfred		return ("Intel 63XXESB USB 2.0 controller");
109184610Salfred
110184610Salfred	case 0x523910b9:
111184610Salfred		return "ALi M5239 USB 2.0 controller";
112184610Salfred
113184610Salfred	case 0x10227463:
114184610Salfred		return "AMD 8111 USB 2.0 controller";
115184610Salfred
116184610Salfred	case 0x20951022:
117184610Salfred		return ("AMD CS5536 (Geode) USB 2.0 controller");
118184610Salfred
119184610Salfred	case 0x43451002:
120184610Salfred		return "ATI SB200 USB 2.0 controller";
121184610Salfred	case 0x43731002:
122184610Salfred		return "ATI SB400 USB 2.0 controller";
123184610Salfred
124184610Salfred	case 0x25ad8086:
125184610Salfred		return "Intel 6300ESB USB 2.0 controller";
126184610Salfred	case 0x24cd8086:
127184610Salfred		return "Intel 82801DB/L/M (ICH4) USB 2.0 controller";
128184610Salfred	case 0x24dd8086:
129184610Salfred		return "Intel 82801EB/R (ICH5) USB 2.0 controller";
130184610Salfred	case 0x265c8086:
131184610Salfred		return "Intel 82801FB (ICH6) USB 2.0 controller";
132184610Salfred	case 0x27cc8086:
133184610Salfred		return "Intel 82801GB/R (ICH7) USB 2.0 controller";
134184610Salfred
135184610Salfred	case 0x28368086:
136184610Salfred		return "Intel 82801H (ICH8) USB 2.0 controller USB2-A";
137184610Salfred	case 0x283a8086:
138184610Salfred		return "Intel 82801H (ICH8) USB 2.0 controller USB2-B";
139184610Salfred	case 0x293a8086:
140184610Salfred		return "Intel 82801I (ICH9) USB 2.0 controller";
141184610Salfred	case 0x293c8086:
142184610Salfred		return "Intel 82801I (ICH9) USB 2.0 controller";
143200087Sthompsa	case 0x3a3a8086:
144200305Sthompsa		return "Intel 82801JI (ICH10) USB 2.0 controller USB-A";
145200087Sthompsa	case 0x3a3c8086:
146200305Sthompsa		return "Intel 82801JI (ICH10) USB 2.0 controller USB-B";
147200822Sthompsa	case 0x3b348086:
148200822Sthompsa		return ("Intel PCH USB 2.0 controller USB-A");
149200822Sthompsa	case 0x3b3c8086:
150200822Sthompsa		return ("Intel PCH USB 2.0 controller USB-B");
151184610Salfred
152184610Salfred	case 0x00e01033:
153184610Salfred		return ("NEC uPD 720100 USB 2.0 controller");
154184610Salfred
155184610Salfred	case 0x006810de:
156184610Salfred		return "NVIDIA nForce2 USB 2.0 controller";
157184610Salfred	case 0x008810de:
158184610Salfred		return "NVIDIA nForce2 Ultra 400 USB 2.0 controller";
159184610Salfred	case 0x00d810de:
160184610Salfred		return "NVIDIA nForce3 USB 2.0 controller";
161184610Salfred	case 0x00e810de:
162184610Salfred		return "NVIDIA nForce3 250 USB 2.0 controller";
163184610Salfred	case 0x005b10de:
164184610Salfred		return "NVIDIA nForce4 USB 2.0 controller";
165205802Sthompsa	case 0x036d10de:
166205802Sthompsa		return "NVIDIA nForce MCP55 USB 2.0 controller";
167195958Salfred	case 0x03f210de:
168195958Salfred		return "NVIDIA nForce MCP61 USB 2.0 controller";
169200822Sthompsa	case 0x0aa610de:
170200822Sthompsa		return "NVIDIA nForce MCP79 USB 2.0 controller";
171200822Sthompsa	case 0x0aa910de:
172200822Sthompsa		return "NVIDIA nForce MCP79 USB 2.0 controller";
173200822Sthompsa	case 0x0aaa10de:
174200822Sthompsa		return "NVIDIA nForce MCP79 USB 2.0 controller";
175184610Salfred
176184610Salfred	case 0x15621131:
177184610Salfred		return "Philips ISP156x USB 2.0 controller";
178184610Salfred
179184610Salfred	case 0x31041106:
180184610Salfred		return ("VIA VT6202 USB 2.0 controller");
181184610Salfred
182184610Salfred	default:
183184610Salfred		break;
184184610Salfred	}
185184610Salfred
186184610Salfred	if ((pci_get_class(self) == PCIC_SERIALBUS)
187184610Salfred	    && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
188184610Salfred	    && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) {
189184610Salfred		return ("EHCI (generic) USB 2.0 controller");
190184610Salfred	}
191184610Salfred	return (NULL);			/* dunno */
192184610Salfred}
193184610Salfred
194184610Salfredstatic int
195184610Salfredehci_pci_probe(device_t self)
196184610Salfred{
197184610Salfred	const char *desc = ehci_pci_match(self);
198184610Salfred
199184610Salfred	if (desc) {
200184610Salfred		device_set_desc(self, desc);
201184610Salfred		return (0);
202184610Salfred	} else {
203184610Salfred		return (ENXIO);
204184610Salfred	}
205184610Salfred}
206184610Salfred
207197554Sthompsastatic void
208197554Sthompsaehci_pci_ati_quirk(device_t self, uint8_t is_sb700)
209197554Sthompsa{
210197554Sthompsa	device_t smbdev;
211197554Sthompsa	uint32_t val;
212197554Sthompsa
213197554Sthompsa	if (is_sb700) {
214197554Sthompsa		/* Lookup SMBUS PCI device */
215197554Sthompsa		smbdev = pci_find_device(PCI_EHCI_VENDORID_ATI, 0x4385);
216197554Sthompsa		if (smbdev == NULL)
217197554Sthompsa			return;
218197554Sthompsa		val = pci_get_revid(smbdev);
219197554Sthompsa		if (val != 0x3a && val != 0x3b)
220197554Sthompsa			return;
221197554Sthompsa	}
222197554Sthompsa
223197554Sthompsa	/*
224197554Sthompsa	 * Note: this bit is described as reserved in SB700
225197554Sthompsa	 * Register Reference Guide.
226197554Sthompsa	 */
227197554Sthompsa	val = pci_read_config(self, 0x53, 1);
228197554Sthompsa	if (!(val & 0x8)) {
229197554Sthompsa		val |= 0x8;
230197554Sthompsa		pci_write_config(self, 0x53, val, 1);
231197554Sthompsa		device_printf(self, "AMD SB600/700 quirk applied\n");
232197554Sthompsa	}
233197554Sthompsa}
234197554Sthompsa
235197554Sthompsastatic void
236197554Sthompsaehci_pci_via_quirk(device_t self)
237197554Sthompsa{
238197554Sthompsa	uint32_t val;
239197554Sthompsa
240197554Sthompsa	if ((pci_get_device(self) == 0x3104) &&
241197554Sthompsa	    ((pci_get_revid(self) & 0xf0) == 0x60)) {
242197554Sthompsa		/* Correct schedule sleep time to 10us */
243197554Sthompsa		val = pci_read_config(self, 0x4b, 1);
244197554Sthompsa		if (val & 0x20)
245197554Sthompsa			return;
246197554Sthompsa		pci_write_config(self, 0x4b, val, 1);
247197554Sthompsa		device_printf(self, "VIA-quirk applied\n");
248197554Sthompsa	}
249197554Sthompsa}
250197554Sthompsa
251184610Salfredstatic int
252184610Salfredehci_pci_attach(device_t self)
253184610Salfred{
254184610Salfred	ehci_softc_t *sc = device_get_softc(self);
255184610Salfred	int err;
256184610Salfred	int rid;
257184610Salfred
258187170Sthompsa	/* initialise some bus fields */
259187170Sthompsa	sc->sc_bus.parent = self;
260187170Sthompsa	sc->sc_bus.devices = sc->sc_devices;
261187170Sthompsa	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
262187170Sthompsa
263184610Salfred	/* get all DMA memory */
264194228Sthompsa	if (usb_bus_mem_alloc_all(&sc->sc_bus,
265184610Salfred	    USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
266187170Sthompsa		return (ENOMEM);
267184610Salfred	}
268184610Salfred
269184610Salfred	pci_enable_busmaster(self);
270184610Salfred
271184610Salfred	switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
272184610Salfred	case PCI_USB_REV_PRE_1_0:
273184610Salfred	case PCI_USB_REV_1_0:
274184610Salfred	case PCI_USB_REV_1_1:
275184610Salfred		/*
276184610Salfred		 * NOTE: some EHCI USB controllers have the wrong USB
277184610Salfred		 * revision number. It appears those controllers are
278184610Salfred		 * fully compliant so we just ignore this value in
279184610Salfred		 * some common cases.
280184610Salfred		 */
281184610Salfred		device_printf(self, "pre-2.0 USB revision (ignored)\n");
282184610Salfred		/* fallthrough */
283184610Salfred	case PCI_USB_REV_2_0:
284184610Salfred		break;
285184610Salfred	default:
286187186Sthompsa		/* Quirk for Parallels Desktop 4.0 */
287187186Sthompsa		device_printf(self, "USB revision is unknown. Assuming v2.0.\n");
288199057Sthompsa		break;
289184610Salfred	}
290184610Salfred
291184610Salfred	rid = PCI_CBMEM;
292184610Salfred	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
293184610Salfred	    RF_ACTIVE);
294184610Salfred	if (!sc->sc_io_res) {
295184610Salfred		device_printf(self, "Could not map memory\n");
296184610Salfred		goto error;
297184610Salfred	}
298184610Salfred	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
299184610Salfred	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
300184610Salfred	sc->sc_io_size = rman_get_size(sc->sc_io_res);
301184610Salfred
302184610Salfred	rid = 0;
303184610Salfred	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
304184610Salfred	    RF_SHAREABLE | RF_ACTIVE);
305184610Salfred	if (sc->sc_irq_res == NULL) {
306184610Salfred		device_printf(self, "Could not allocate irq\n");
307184610Salfred		goto error;
308184610Salfred	}
309184610Salfred	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
310184610Salfred	if (!sc->sc_bus.bdev) {
311184610Salfred		device_printf(self, "Could not add USB device\n");
312184610Salfred		goto error;
313184610Salfred	}
314184610Salfred	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
315184610Salfred
316184610Salfred	/*
317184610Salfred	 * ehci_pci_match will never return NULL if ehci_pci_probe
318184610Salfred	 * succeeded
319184610Salfred	 */
320184610Salfred	device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
321184610Salfred	switch (pci_get_vendor(self)) {
322184610Salfred	case PCI_EHCI_VENDORID_ACERLABS:
323184610Salfred		sprintf(sc->sc_vendor, "AcerLabs");
324184610Salfred		break;
325184610Salfred	case PCI_EHCI_VENDORID_AMD:
326184610Salfred		sprintf(sc->sc_vendor, "AMD");
327184610Salfred		break;
328184610Salfred	case PCI_EHCI_VENDORID_APPLE:
329184610Salfred		sprintf(sc->sc_vendor, "Apple");
330184610Salfred		break;
331184610Salfred	case PCI_EHCI_VENDORID_ATI:
332184610Salfred		sprintf(sc->sc_vendor, "ATI");
333184610Salfred		break;
334184610Salfred	case PCI_EHCI_VENDORID_CMDTECH:
335184610Salfred		sprintf(sc->sc_vendor, "CMDTECH");
336184610Salfred		break;
337184610Salfred	case PCI_EHCI_VENDORID_INTEL:
338184610Salfred		sprintf(sc->sc_vendor, "Intel");
339184610Salfred		break;
340184610Salfred	case PCI_EHCI_VENDORID_NEC:
341184610Salfred		sprintf(sc->sc_vendor, "NEC");
342184610Salfred		break;
343184610Salfred	case PCI_EHCI_VENDORID_OPTI:
344184610Salfred		sprintf(sc->sc_vendor, "OPTi");
345184610Salfred		break;
346184610Salfred	case PCI_EHCI_VENDORID_PHILIPS:
347184610Salfred		sprintf(sc->sc_vendor, "Philips");
348184610Salfred		break;
349184610Salfred	case PCI_EHCI_VENDORID_SIS:
350184610Salfred		sprintf(sc->sc_vendor, "SiS");
351184610Salfred		break;
352184610Salfred	case PCI_EHCI_VENDORID_NVIDIA:
353184610Salfred	case PCI_EHCI_VENDORID_NVIDIA2:
354184610Salfred		sprintf(sc->sc_vendor, "nVidia");
355184610Salfred		break;
356184610Salfred	case PCI_EHCI_VENDORID_VIA:
357184610Salfred		sprintf(sc->sc_vendor, "VIA");
358184610Salfred		break;
359184610Salfred	default:
360184610Salfred		if (bootverbose)
361184610Salfred			device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
362184610Salfred			    pci_get_devid(self));
363184610Salfred		sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
364184610Salfred	}
365184610Salfred
366184610Salfred#if (__FreeBSD_version >= 700031)
367184610Salfred	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
368190183Sthompsa	    NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
369184610Salfred#else
370184610Salfred	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
371190183Sthompsa	    (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
372184610Salfred#endif
373184610Salfred	if (err) {
374184610Salfred		device_printf(self, "Could not setup irq, %d\n", err);
375184610Salfred		sc->sc_intr_hdl = NULL;
376184610Salfred		goto error;
377184610Salfred	}
378228483Shselasky	ehci_pci_take_controller(self);
379197554Sthompsa
380197554Sthompsa	/* Undocumented quirks taken from Linux */
381197554Sthompsa
382197554Sthompsa	switch (pci_get_vendor(self)) {
383197554Sthompsa	case PCI_EHCI_VENDORID_ATI:
384197554Sthompsa		/* SB600 and SB700 EHCI quirk */
385197554Sthompsa		switch (pci_get_device(self)) {
386197554Sthompsa		case 0x4386:
387197554Sthompsa			ehci_pci_ati_quirk(self, 0);
388197554Sthompsa			break;
389197554Sthompsa		case 0x4396:
390197554Sthompsa			ehci_pci_ati_quirk(self, 1);
391197554Sthompsa			break;
392197554Sthompsa		default:
393197554Sthompsa			break;
394197554Sthompsa		}
395197554Sthompsa		break;
396197554Sthompsa
397197554Sthompsa	case PCI_EHCI_VENDORID_VIA:
398197554Sthompsa		ehci_pci_via_quirk(self);
399197554Sthompsa		break;
400197554Sthompsa
401197554Sthompsa	default:
402197554Sthompsa		break;
403197554Sthompsa	}
404197554Sthompsa
405199058Sthompsa	/* Dropped interrupts workaround */
406199058Sthompsa	switch (pci_get_vendor(self)) {
407199058Sthompsa	case PCI_EHCI_VENDORID_ATI:
408199058Sthompsa	case PCI_EHCI_VENDORID_VIA:
409199058Sthompsa		sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
410199058Sthompsa		if (bootverbose)
411199058Sthompsa			device_printf(self,
412199058Sthompsa			    "Dropped interrupts workaround enabled\n");
413199058Sthompsa		break;
414199058Sthompsa	default:
415199058Sthompsa		break;
416199058Sthompsa	}
417199058Sthompsa
418203693Sthompsa	/* Doorbell feature workaround */
419203693Sthompsa	switch (pci_get_vendor(self)) {
420203693Sthompsa	case PCI_EHCI_VENDORID_NVIDIA:
421203693Sthompsa	case PCI_EHCI_VENDORID_NVIDIA2:
422203693Sthompsa		sc->sc_flags |= EHCI_SCFLG_IAADBUG;
423203693Sthompsa		if (bootverbose)
424203693Sthompsa			device_printf(self,
425203693Sthompsa			    "Doorbell workaround enabled\n");
426203693Sthompsa		break;
427203693Sthompsa	default:
428203693Sthompsa		break;
429203693Sthompsa	}
430203693Sthompsa
431184610Salfred	err = ehci_init(sc);
432184610Salfred	if (!err) {
433184610Salfred		err = device_probe_and_attach(sc->sc_bus.bdev);
434184610Salfred	}
435184610Salfred	if (err) {
436184610Salfred		device_printf(self, "USB init failed err=%d\n", err);
437184610Salfred		goto error;
438184610Salfred	}
439184610Salfred	return (0);
440184610Salfred
441184610Salfrederror:
442184610Salfred	ehci_pci_detach(self);
443184610Salfred	return (ENXIO);
444184610Salfred}
445184610Salfred
446184610Salfredstatic int
447184610Salfredehci_pci_detach(device_t self)
448184610Salfred{
449184610Salfred	ehci_softc_t *sc = device_get_softc(self);
450184610Salfred	device_t bdev;
451184610Salfred
452184610Salfred	if (sc->sc_bus.bdev) {
453184610Salfred		bdev = sc->sc_bus.bdev;
454184610Salfred		device_detach(bdev);
455184610Salfred		device_delete_child(self, bdev);
456184610Salfred	}
457184610Salfred	/* during module unload there are lots of children leftover */
458227849Shselasky	device_delete_children(self);
459184610Salfred
460184610Salfred	pci_disable_busmaster(self);
461184610Salfred
462184610Salfred	if (sc->sc_irq_res && sc->sc_intr_hdl) {
463184610Salfred		/*
464184610Salfred		 * only call ehci_detach() after ehci_init()
465184610Salfred		 */
466184610Salfred		ehci_detach(sc);
467184610Salfred
468184610Salfred		int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
469184610Salfred
470184610Salfred		if (err)
471184610Salfred			/* XXX or should we panic? */
472184610Salfred			device_printf(self, "Could not tear down irq, %d\n",
473184610Salfred			    err);
474184610Salfred		sc->sc_intr_hdl = NULL;
475184610Salfred	}
476184610Salfred	if (sc->sc_irq_res) {
477184610Salfred		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
478184610Salfred		sc->sc_irq_res = NULL;
479184610Salfred	}
480184610Salfred	if (sc->sc_io_res) {
481184610Salfred		bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
482184610Salfred		    sc->sc_io_res);
483184610Salfred		sc->sc_io_res = NULL;
484184610Salfred	}
485194228Sthompsa	usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
486184610Salfred
487184610Salfred	return (0);
488184610Salfred}
489184610Salfred
490228483Shselaskystatic int
491228483Shselaskyehci_pci_take_controller(device_t self)
492198501Sthompsa{
493198501Sthompsa	ehci_softc_t *sc = device_get_softc(self);
494198501Sthompsa	uint32_t cparams;
495198501Sthompsa	uint32_t eec;
496198501Sthompsa	uint16_t to;
497198501Sthompsa	uint8_t eecp;
498198501Sthompsa	uint8_t bios_sem;
499198501Sthompsa
500198501Sthompsa	cparams = EREAD4(sc, EHCI_HCCPARAMS);
501198501Sthompsa
502198501Sthompsa	/* Synchronise with the BIOS if it owns the controller. */
503198501Sthompsa	for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
504198501Sthompsa	    eecp = EHCI_EECP_NEXT(eec)) {
505198501Sthompsa		eec = pci_read_config(self, eecp, 4);
506198501Sthompsa		if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) {
507198501Sthompsa			continue;
508198501Sthompsa		}
509198501Sthompsa		bios_sem = pci_read_config(self, eecp +
510198501Sthompsa		    EHCI_LEGSUP_BIOS_SEM, 1);
511198501Sthompsa		if (bios_sem == 0) {
512198501Sthompsa			continue;
513198501Sthompsa		}
514198501Sthompsa		device_printf(sc->sc_bus.bdev, "waiting for BIOS "
515198501Sthompsa		    "to give up control\n");
516198501Sthompsa		pci_write_config(self, eecp +
517198501Sthompsa		    EHCI_LEGSUP_OS_SEM, 1, 1);
518198501Sthompsa		to = 500;
519198501Sthompsa		while (1) {
520198501Sthompsa			bios_sem = pci_read_config(self, eecp +
521198501Sthompsa			    EHCI_LEGSUP_BIOS_SEM, 1);
522198501Sthompsa			if (bios_sem == 0)
523198501Sthompsa				break;
524198501Sthompsa
525198501Sthompsa			if (--to == 0) {
526198501Sthompsa				device_printf(sc->sc_bus.bdev,
527198501Sthompsa				    "timed out waiting for BIOS\n");
528198501Sthompsa				break;
529198501Sthompsa			}
530198501Sthompsa			usb_pause_mtx(NULL, hz / 100);	/* wait 10ms */
531198501Sthompsa		}
532198501Sthompsa	}
533228483Shselasky	return (0);
534198501Sthompsa}
535198501Sthompsa
536228483Shselaskystatic device_method_t ehci_pci_methods[] = {
537228483Shselasky	/* Device interface */
538228483Shselasky	DEVMETHOD(device_probe, ehci_pci_probe),
539228483Shselasky	DEVMETHOD(device_attach, ehci_pci_attach),
540228483Shselasky	DEVMETHOD(device_detach, ehci_pci_detach),
541228483Shselasky	DEVMETHOD(device_suspend, bus_generic_suspend),
542228483Shselasky	DEVMETHOD(device_resume, bus_generic_resume),
543228483Shselasky	DEVMETHOD(device_shutdown, bus_generic_shutdown),
544228483Shselasky	DEVMETHOD(usb_take_controller, ehci_pci_take_controller),
545228483Shselasky
546228483Shselasky	DEVMETHOD_END
547228483Shselasky};
548228483Shselasky
549228483Shselaskystatic driver_t ehci_driver = {
550184610Salfred	.name = "ehci",
551228483Shselasky	.methods = ehci_pci_methods,
552184610Salfred	.size = sizeof(struct ehci_softc),
553184610Salfred};
554184610Salfred
555184610Salfredstatic devclass_t ehci_devclass;
556184610Salfred
557184610SalfredDRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
558188942SthompsaMODULE_DEPEND(ehci, usb, 1, 1, 1);
559