ehci_pci.c revision 187186
1/*-
2 * Copyright (c) 1998 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Lennart Augustsson (augustss@carlstedt.se) at
7 * Carlstedt Research & Technology.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *        This product includes software developed by the NetBSD
20 *        Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 *    contributors may be used to endorse or promote products derived
23 *    from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/sys/dev/usb2/controller/ehci2_pci.c 187186 2009-01-13 19:14:51Z thompsa $");
40
41/*
42 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
43 *
44 * The EHCI 1.0 spec can be found at
45 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
46 * and the USB 2.0 spec at
47 * http://www.usb.org/developers/docs/usb_20.zip
48 */
49
50/* The low level controller code for EHCI has been split into
51 * PCI probes and EHCI specific code. This was done to facilitate the
52 * sharing of code between *BSD's
53 */
54
55#include <dev/usb2/include/usb2_mfunc.h>
56#include <dev/usb2/include/usb2_defs.h>
57#include <dev/usb2/include/usb2_standard.h>
58
59#include <dev/usb2/core/usb2_core.h>
60#include <dev/usb2/core/usb2_busdma.h>
61#include <dev/usb2/core/usb2_process.h>
62#include <dev/usb2/core/usb2_sw_transfer.h>
63#include <dev/usb2/core/usb2_util.h>
64
65#include <dev/usb2/controller/usb2_controller.h>
66#include <dev/usb2/controller/usb2_bus.h>
67#include <dev/usb2/controller/usb2_pci.h>
68#include <dev/usb2/controller/ehci2.h>
69
70#define	PCI_EHCI_VENDORID_ACERLABS	0x10b9
71#define	PCI_EHCI_VENDORID_AMD		0x1022
72#define	PCI_EHCI_VENDORID_APPLE		0x106b
73#define	PCI_EHCI_VENDORID_ATI		0x1002
74#define	PCI_EHCI_VENDORID_CMDTECH	0x1095
75#define	PCI_EHCI_VENDORID_INTEL		0x8086
76#define	PCI_EHCI_VENDORID_NEC		0x1033
77#define	PCI_EHCI_VENDORID_OPTI		0x1045
78#define	PCI_EHCI_VENDORID_PHILIPS	0x1131
79#define	PCI_EHCI_VENDORID_SIS		0x1039
80#define	PCI_EHCI_VENDORID_NVIDIA	0x12D2
81#define	PCI_EHCI_VENDORID_NVIDIA2	0x10DE
82#define	PCI_EHCI_VENDORID_VIA		0x1106
83
84#define	PCI_EHCI_BASE_REG	0x10
85
86static void ehci_pci_takecontroller(device_t self);
87
88static device_probe_t ehci_pci_probe;
89static device_attach_t ehci_pci_attach;
90static device_detach_t ehci_pci_detach;
91static device_suspend_t ehci_pci_suspend;
92static device_resume_t ehci_pci_resume;
93static device_shutdown_t ehci_pci_shutdown;
94
95static int
96ehci_pci_suspend(device_t self)
97{
98	ehci_softc_t *sc = device_get_softc(self);
99	int err;
100
101	err = bus_generic_suspend(self);
102	if (err)
103		return (err);
104	ehci_suspend(sc);
105	return (0);
106}
107
108static int
109ehci_pci_resume(device_t self)
110{
111	ehci_softc_t *sc = device_get_softc(self);
112
113	ehci_pci_takecontroller(self);
114	ehci_resume(sc);
115
116	bus_generic_resume(self);
117
118	return (0);
119}
120
121static int
122ehci_pci_shutdown(device_t self)
123{
124	ehci_softc_t *sc = device_get_softc(self);
125	int err;
126
127	err = bus_generic_shutdown(self);
128	if (err)
129		return (err);
130	ehci_shutdown(sc);
131
132	return (0);
133}
134
135static const char *
136ehci_pci_match(device_t self)
137{
138	uint32_t device_id = pci_get_devid(self);
139
140	switch (device_id) {
141	case 0x268c8086:
142		return ("Intel 63XXESB USB 2.0 controller");
143
144	case 0x523910b9:
145		return "ALi M5239 USB 2.0 controller";
146
147	case 0x10227463:
148		return "AMD 8111 USB 2.0 controller";
149
150	case 0x20951022:
151		return ("AMD CS5536 (Geode) USB 2.0 controller");
152
153	case 0x43451002:
154		return "ATI SB200 USB 2.0 controller";
155	case 0x43731002:
156		return "ATI SB400 USB 2.0 controller";
157
158	case 0x25ad8086:
159		return "Intel 6300ESB USB 2.0 controller";
160	case 0x24cd8086:
161		return "Intel 82801DB/L/M (ICH4) USB 2.0 controller";
162	case 0x24dd8086:
163		return "Intel 82801EB/R (ICH5) USB 2.0 controller";
164	case 0x265c8086:
165		return "Intel 82801FB (ICH6) USB 2.0 controller";
166	case 0x27cc8086:
167		return "Intel 82801GB/R (ICH7) USB 2.0 controller";
168
169	case 0x28368086:
170		return "Intel 82801H (ICH8) USB 2.0 controller USB2-A";
171	case 0x283a8086:
172		return "Intel 82801H (ICH8) USB 2.0 controller USB2-B";
173	case 0x293a8086:
174		return "Intel 82801I (ICH9) USB 2.0 controller";
175	case 0x293c8086:
176		return "Intel 82801I (ICH9) USB 2.0 controller";
177
178	case 0x00e01033:
179		return ("NEC uPD 720100 USB 2.0 controller");
180
181	case 0x006810de:
182		return "NVIDIA nForce2 USB 2.0 controller";
183	case 0x008810de:
184		return "NVIDIA nForce2 Ultra 400 USB 2.0 controller";
185	case 0x00d810de:
186		return "NVIDIA nForce3 USB 2.0 controller";
187	case 0x00e810de:
188		return "NVIDIA nForce3 250 USB 2.0 controller";
189	case 0x005b10de:
190		return "NVIDIA nForce4 USB 2.0 controller";
191
192	case 0x15621131:
193		return "Philips ISP156x USB 2.0 controller";
194
195	case 0x31041106:
196		return ("VIA VT6202 USB 2.0 controller");
197
198	default:
199		break;
200	}
201
202	if ((pci_get_class(self) == PCIC_SERIALBUS)
203	    && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
204	    && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) {
205		return ("EHCI (generic) USB 2.0 controller");
206	}
207	return (NULL);			/* dunno */
208}
209
210static int
211ehci_pci_probe(device_t self)
212{
213	const char *desc = ehci_pci_match(self);
214
215	if (desc) {
216		device_set_desc(self, desc);
217		return (0);
218	} else {
219		return (ENXIO);
220	}
221}
222
223static int
224ehci_pci_attach(device_t self)
225{
226	ehci_softc_t *sc = device_get_softc(self);
227	int err;
228	int rid;
229
230	if (sc == NULL) {
231		device_printf(self, "Could not allocate sc\n");
232		return (ENXIO);
233	}
234	/* initialise some bus fields */
235	sc->sc_bus.parent = self;
236	sc->sc_bus.devices = sc->sc_devices;
237	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
238
239	/* get all DMA memory */
240	if (usb2_bus_mem_alloc_all(&sc->sc_bus,
241	    USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
242		return (ENOMEM);
243	}
244	sc->sc_dev = self;
245
246	pci_enable_busmaster(self);
247
248	switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
249	case PCI_USB_REV_PRE_1_0:
250	case PCI_USB_REV_1_0:
251	case PCI_USB_REV_1_1:
252		/*
253		 * NOTE: some EHCI USB controllers have the wrong USB
254		 * revision number. It appears those controllers are
255		 * fully compliant so we just ignore this value in
256		 * some common cases.
257		 */
258		device_printf(self, "pre-2.0 USB revision (ignored)\n");
259		/* fallthrough */
260	case PCI_USB_REV_2_0:
261		sc->sc_bus.usbrev = USB_REV_2_0;
262		break;
263	default:
264		/* Quirk for Parallels Desktop 4.0 */
265		device_printf(self, "USB revision is unknown. Assuming v2.0.\n");
266		sc->sc_bus.usbrev = USB_REV_2_0;
267                break;
268	}
269
270	rid = PCI_CBMEM;
271	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
272	    RF_ACTIVE);
273	if (!sc->sc_io_res) {
274		device_printf(self, "Could not map memory\n");
275		goto error;
276	}
277	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
278	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
279	sc->sc_io_size = rman_get_size(sc->sc_io_res);
280
281	rid = 0;
282	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
283	    RF_SHAREABLE | RF_ACTIVE);
284	if (sc->sc_irq_res == NULL) {
285		device_printf(self, "Could not allocate irq\n");
286		goto error;
287	}
288	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
289	if (!sc->sc_bus.bdev) {
290		device_printf(self, "Could not add USB device\n");
291		goto error;
292	}
293	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
294
295	/*
296	 * ehci_pci_match will never return NULL if ehci_pci_probe
297	 * succeeded
298	 */
299	device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
300	switch (pci_get_vendor(self)) {
301	case PCI_EHCI_VENDORID_ACERLABS:
302		sprintf(sc->sc_vendor, "AcerLabs");
303		break;
304	case PCI_EHCI_VENDORID_AMD:
305		sprintf(sc->sc_vendor, "AMD");
306		break;
307	case PCI_EHCI_VENDORID_APPLE:
308		sprintf(sc->sc_vendor, "Apple");
309		break;
310	case PCI_EHCI_VENDORID_ATI:
311		sprintf(sc->sc_vendor, "ATI");
312		break;
313	case PCI_EHCI_VENDORID_CMDTECH:
314		sprintf(sc->sc_vendor, "CMDTECH");
315		break;
316	case PCI_EHCI_VENDORID_INTEL:
317		sprintf(sc->sc_vendor, "Intel");
318		break;
319	case PCI_EHCI_VENDORID_NEC:
320		sprintf(sc->sc_vendor, "NEC");
321		break;
322	case PCI_EHCI_VENDORID_OPTI:
323		sprintf(sc->sc_vendor, "OPTi");
324		break;
325	case PCI_EHCI_VENDORID_PHILIPS:
326		sprintf(sc->sc_vendor, "Philips");
327		break;
328	case PCI_EHCI_VENDORID_SIS:
329		sprintf(sc->sc_vendor, "SiS");
330		break;
331	case PCI_EHCI_VENDORID_NVIDIA:
332	case PCI_EHCI_VENDORID_NVIDIA2:
333		sprintf(sc->sc_vendor, "nVidia");
334		break;
335	case PCI_EHCI_VENDORID_VIA:
336		sprintf(sc->sc_vendor, "VIA");
337		break;
338	default:
339		if (bootverbose)
340			device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
341			    pci_get_devid(self));
342		sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
343	}
344
345#if (__FreeBSD_version >= 700031)
346	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
347	    NULL, (void *)(void *)ehci_interrupt, sc, &sc->sc_intr_hdl);
348#else
349	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
350	    (void *)(void *)ehci_interrupt, sc, &sc->sc_intr_hdl);
351#endif
352	if (err) {
353		device_printf(self, "Could not setup irq, %d\n", err);
354		sc->sc_intr_hdl = NULL;
355		goto error;
356	}
357	ehci_pci_takecontroller(self);
358	err = ehci_init(sc);
359	if (!err) {
360		err = device_probe_and_attach(sc->sc_bus.bdev);
361	}
362	if (err) {
363		device_printf(self, "USB init failed err=%d\n", err);
364		goto error;
365	}
366	return (0);
367
368error:
369	ehci_pci_detach(self);
370	return (ENXIO);
371}
372
373static int
374ehci_pci_detach(device_t self)
375{
376	ehci_softc_t *sc = device_get_softc(self);
377	device_t bdev;
378
379	if (sc->sc_bus.bdev) {
380		bdev = sc->sc_bus.bdev;
381		device_detach(bdev);
382		device_delete_child(self, bdev);
383	}
384	/* during module unload there are lots of children leftover */
385	device_delete_all_children(self);
386
387	pci_disable_busmaster(self);
388
389	/*
390	 * disable interrupts that might have been switched on in ehci_init
391	 */
392	if (sc->sc_io_res) {
393		EWRITE4(sc, EHCI_USBINTR, 0);
394	}
395	if (sc->sc_irq_res && sc->sc_intr_hdl) {
396		/*
397		 * only call ehci_detach() after ehci_init()
398		 */
399		ehci_detach(sc);
400
401		int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
402
403		if (err)
404			/* XXX or should we panic? */
405			device_printf(self, "Could not tear down irq, %d\n",
406			    err);
407		sc->sc_intr_hdl = NULL;
408	}
409	if (sc->sc_irq_res) {
410		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
411		sc->sc_irq_res = NULL;
412	}
413	if (sc->sc_io_res) {
414		bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
415		    sc->sc_io_res);
416		sc->sc_io_res = NULL;
417	}
418	usb2_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
419
420	return (0);
421}
422
423static void
424ehci_pci_takecontroller(device_t self)
425{
426	ehci_softc_t *sc = device_get_softc(self);
427	uint32_t cparams;
428	uint32_t eec;
429	uint16_t to;
430	uint8_t eecp;
431	uint8_t bios_sem;
432
433	cparams = EREAD4(sc, EHCI_HCCPARAMS);
434
435	/* Synchronise with the BIOS if it owns the controller. */
436	for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
437	    eecp = EHCI_EECP_NEXT(eec)) {
438		eec = pci_read_config(self, eecp, 4);
439		if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) {
440			continue;
441		}
442		bios_sem = pci_read_config(self, eecp +
443		    EHCI_LEGSUP_BIOS_SEM, 1);
444		if (bios_sem == 0) {
445			continue;
446		}
447		device_printf(sc->sc_bus.bdev, "waiting for BIOS "
448		    "to give up control\n");
449		pci_write_config(self, eecp +
450		    EHCI_LEGSUP_OS_SEM, 1, 1);
451		to = 500;
452		while (1) {
453			bios_sem = pci_read_config(self, eecp +
454			    EHCI_LEGSUP_BIOS_SEM, 1);
455			if (bios_sem == 0)
456				break;
457
458			if (--to == 0) {
459				device_printf(sc->sc_bus.bdev,
460				    "timed out waiting for BIOS\n");
461				break;
462			}
463			usb2_pause_mtx(NULL, 10);	/* wait 10ms */
464		}
465	}
466}
467
468static driver_t ehci_driver =
469{
470	.name = "ehci",
471	.methods = (device_method_t[]){
472		/* device interface */
473		DEVMETHOD(device_probe, ehci_pci_probe),
474		DEVMETHOD(device_attach, ehci_pci_attach),
475		DEVMETHOD(device_detach, ehci_pci_detach),
476		DEVMETHOD(device_suspend, ehci_pci_suspend),
477		DEVMETHOD(device_resume, ehci_pci_resume),
478		DEVMETHOD(device_shutdown, ehci_pci_shutdown),
479		/* bus interface */
480		DEVMETHOD(bus_print_child, bus_generic_print_child),
481
482		{0, 0}
483	},
484	.size = sizeof(struct ehci_softc),
485};
486
487static devclass_t ehci_devclass;
488
489DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
490DRIVER_MODULE(ehci, cardbus, ehci_driver, ehci_devclass, 0, 0);
491MODULE_DEPEND(ehci, usb2_controller, 1, 1, 1);
492MODULE_DEPEND(ehci, usb2_core, 1, 1, 1);
493