ehci_pci.c revision 194228
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/usb/controller/ehci_pci.c 194228 2009-06-15 01:02:43Z 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/usb/usb_mfunc.h>
56#include <dev/usb/usb.h>
57
58#include <dev/usb/usb_core.h>
59#include <dev/usb/usb_busdma.h>
60#include <dev/usb/usb_process.h>
61#include <dev/usb/usb_util.h>
62
63#include <dev/usb/usb_controller.h>
64#include <dev/usb/usb_bus.h>
65#include <dev/usb/usb_pci.h>
66#include <dev/usb/controller/ehci.h>
67
68#define	PCI_EHCI_VENDORID_ACERLABS	0x10b9
69#define	PCI_EHCI_VENDORID_AMD		0x1022
70#define	PCI_EHCI_VENDORID_APPLE		0x106b
71#define	PCI_EHCI_VENDORID_ATI		0x1002
72#define	PCI_EHCI_VENDORID_CMDTECH	0x1095
73#define	PCI_EHCI_VENDORID_INTEL		0x8086
74#define	PCI_EHCI_VENDORID_NEC		0x1033
75#define	PCI_EHCI_VENDORID_OPTI		0x1045
76#define	PCI_EHCI_VENDORID_PHILIPS	0x1131
77#define	PCI_EHCI_VENDORID_SIS		0x1039
78#define	PCI_EHCI_VENDORID_NVIDIA	0x12D2
79#define	PCI_EHCI_VENDORID_NVIDIA2	0x10DE
80#define	PCI_EHCI_VENDORID_VIA		0x1106
81
82#define	PCI_EHCI_BASE_REG	0x10
83
84static void ehci_pci_takecontroller(device_t self);
85
86static device_probe_t ehci_pci_probe;
87static device_attach_t ehci_pci_attach;
88static device_detach_t ehci_pci_detach;
89static device_suspend_t ehci_pci_suspend;
90static device_resume_t ehci_pci_resume;
91static device_shutdown_t ehci_pci_shutdown;
92
93static int
94ehci_pci_suspend(device_t self)
95{
96	ehci_softc_t *sc = device_get_softc(self);
97	int err;
98
99	err = bus_generic_suspend(self);
100	if (err)
101		return (err);
102	ehci_suspend(sc);
103	return (0);
104}
105
106static int
107ehci_pci_resume(device_t self)
108{
109	ehci_softc_t *sc = device_get_softc(self);
110
111	ehci_pci_takecontroller(self);
112	ehci_resume(sc);
113
114	bus_generic_resume(self);
115
116	return (0);
117}
118
119static int
120ehci_pci_shutdown(device_t self)
121{
122	ehci_softc_t *sc = device_get_softc(self);
123	int err;
124
125	err = bus_generic_shutdown(self);
126	if (err)
127		return (err);
128	ehci_shutdown(sc);
129
130	return (0);
131}
132
133static const char *
134ehci_pci_match(device_t self)
135{
136	uint32_t device_id = pci_get_devid(self);
137
138	switch (device_id) {
139	case 0x268c8086:
140		return ("Intel 63XXESB USB 2.0 controller");
141
142	case 0x523910b9:
143		return "ALi M5239 USB 2.0 controller";
144
145	case 0x10227463:
146		return "AMD 8111 USB 2.0 controller";
147
148	case 0x20951022:
149		return ("AMD CS5536 (Geode) USB 2.0 controller");
150
151	case 0x43451002:
152		return "ATI SB200 USB 2.0 controller";
153	case 0x43731002:
154		return "ATI SB400 USB 2.0 controller";
155
156	case 0x25ad8086:
157		return "Intel 6300ESB USB 2.0 controller";
158	case 0x24cd8086:
159		return "Intel 82801DB/L/M (ICH4) USB 2.0 controller";
160	case 0x24dd8086:
161		return "Intel 82801EB/R (ICH5) USB 2.0 controller";
162	case 0x265c8086:
163		return "Intel 82801FB (ICH6) USB 2.0 controller";
164	case 0x27cc8086:
165		return "Intel 82801GB/R (ICH7) USB 2.0 controller";
166
167	case 0x28368086:
168		return "Intel 82801H (ICH8) USB 2.0 controller USB2-A";
169	case 0x283a8086:
170		return "Intel 82801H (ICH8) USB 2.0 controller USB2-B";
171	case 0x293a8086:
172		return "Intel 82801I (ICH9) USB 2.0 controller";
173	case 0x293c8086:
174		return "Intel 82801I (ICH9) USB 2.0 controller";
175
176	case 0x00e01033:
177		return ("NEC uPD 720100 USB 2.0 controller");
178
179	case 0x006810de:
180		return "NVIDIA nForce2 USB 2.0 controller";
181	case 0x008810de:
182		return "NVIDIA nForce2 Ultra 400 USB 2.0 controller";
183	case 0x00d810de:
184		return "NVIDIA nForce3 USB 2.0 controller";
185	case 0x00e810de:
186		return "NVIDIA nForce3 250 USB 2.0 controller";
187	case 0x005b10de:
188		return "NVIDIA nForce4 USB 2.0 controller";
189
190	case 0x15621131:
191		return "Philips ISP156x USB 2.0 controller";
192
193	case 0x31041106:
194		return ("VIA VT6202 USB 2.0 controller");
195
196	default:
197		break;
198	}
199
200	if ((pci_get_class(self) == PCIC_SERIALBUS)
201	    && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
202	    && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) {
203		return ("EHCI (generic) USB 2.0 controller");
204	}
205	return (NULL);			/* dunno */
206}
207
208static int
209ehci_pci_probe(device_t self)
210{
211	const char *desc = ehci_pci_match(self);
212
213	if (desc) {
214		device_set_desc(self, desc);
215		return (0);
216	} else {
217		return (ENXIO);
218	}
219}
220
221static int
222ehci_pci_attach(device_t self)
223{
224	ehci_softc_t *sc = device_get_softc(self);
225	int err;
226	int rid;
227
228	/* initialise some bus fields */
229	sc->sc_bus.parent = self;
230	sc->sc_bus.devices = sc->sc_devices;
231	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
232
233	/* get all DMA memory */
234	if (usb_bus_mem_alloc_all(&sc->sc_bus,
235	    USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
236		return (ENOMEM);
237	}
238
239	pci_enable_busmaster(self);
240
241	switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
242	case PCI_USB_REV_PRE_1_0:
243	case PCI_USB_REV_1_0:
244	case PCI_USB_REV_1_1:
245		/*
246		 * NOTE: some EHCI USB controllers have the wrong USB
247		 * revision number. It appears those controllers are
248		 * fully compliant so we just ignore this value in
249		 * some common cases.
250		 */
251		device_printf(self, "pre-2.0 USB revision (ignored)\n");
252		/* fallthrough */
253	case PCI_USB_REV_2_0:
254		sc->sc_bus.usbrev = USB_REV_2_0;
255		break;
256	default:
257		/* Quirk for Parallels Desktop 4.0 */
258		device_printf(self, "USB revision is unknown. Assuming v2.0.\n");
259		sc->sc_bus.usbrev = USB_REV_2_0;
260                break;
261	}
262
263	rid = PCI_CBMEM;
264	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
265	    RF_ACTIVE);
266	if (!sc->sc_io_res) {
267		device_printf(self, "Could not map memory\n");
268		goto error;
269	}
270	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
271	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
272	sc->sc_io_size = rman_get_size(sc->sc_io_res);
273
274	rid = 0;
275	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
276	    RF_SHAREABLE | RF_ACTIVE);
277	if (sc->sc_irq_res == NULL) {
278		device_printf(self, "Could not allocate irq\n");
279		goto error;
280	}
281	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
282	if (!sc->sc_bus.bdev) {
283		device_printf(self, "Could not add USB device\n");
284		goto error;
285	}
286	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
287
288	/*
289	 * ehci_pci_match will never return NULL if ehci_pci_probe
290	 * succeeded
291	 */
292	device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
293	switch (pci_get_vendor(self)) {
294	case PCI_EHCI_VENDORID_ACERLABS:
295		sprintf(sc->sc_vendor, "AcerLabs");
296		break;
297	case PCI_EHCI_VENDORID_AMD:
298		sprintf(sc->sc_vendor, "AMD");
299		break;
300	case PCI_EHCI_VENDORID_APPLE:
301		sprintf(sc->sc_vendor, "Apple");
302		break;
303	case PCI_EHCI_VENDORID_ATI:
304		sprintf(sc->sc_vendor, "ATI");
305		break;
306	case PCI_EHCI_VENDORID_CMDTECH:
307		sprintf(sc->sc_vendor, "CMDTECH");
308		break;
309	case PCI_EHCI_VENDORID_INTEL:
310		sprintf(sc->sc_vendor, "Intel");
311		break;
312	case PCI_EHCI_VENDORID_NEC:
313		sprintf(sc->sc_vendor, "NEC");
314		break;
315	case PCI_EHCI_VENDORID_OPTI:
316		sprintf(sc->sc_vendor, "OPTi");
317		break;
318	case PCI_EHCI_VENDORID_PHILIPS:
319		sprintf(sc->sc_vendor, "Philips");
320		break;
321	case PCI_EHCI_VENDORID_SIS:
322		sprintf(sc->sc_vendor, "SiS");
323		break;
324	case PCI_EHCI_VENDORID_NVIDIA:
325	case PCI_EHCI_VENDORID_NVIDIA2:
326		sprintf(sc->sc_vendor, "nVidia");
327		break;
328	case PCI_EHCI_VENDORID_VIA:
329		sprintf(sc->sc_vendor, "VIA");
330		break;
331	default:
332		if (bootverbose)
333			device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
334			    pci_get_devid(self));
335		sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
336	}
337
338#if (__FreeBSD_version >= 700031)
339	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
340	    NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
341#else
342	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
343	    (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
344#endif
345	if (err) {
346		device_printf(self, "Could not setup irq, %d\n", err);
347		sc->sc_intr_hdl = NULL;
348		goto error;
349	}
350	ehci_pci_takecontroller(self);
351	err = ehci_init(sc);
352	if (!err) {
353		err = device_probe_and_attach(sc->sc_bus.bdev);
354	}
355	if (err) {
356		device_printf(self, "USB init failed err=%d\n", err);
357		goto error;
358	}
359	return (0);
360
361error:
362	ehci_pci_detach(self);
363	return (ENXIO);
364}
365
366static int
367ehci_pci_detach(device_t self)
368{
369	ehci_softc_t *sc = device_get_softc(self);
370	device_t bdev;
371
372	if (sc->sc_bus.bdev) {
373		bdev = sc->sc_bus.bdev;
374		device_detach(bdev);
375		device_delete_child(self, bdev);
376	}
377	/* during module unload there are lots of children leftover */
378	device_delete_all_children(self);
379
380	pci_disable_busmaster(self);
381
382	/*
383	 * disable interrupts that might have been switched on in ehci_init
384	 */
385	if (sc->sc_io_res) {
386		EWRITE4(sc, EHCI_USBINTR, 0);
387	}
388	if (sc->sc_irq_res && sc->sc_intr_hdl) {
389		/*
390		 * only call ehci_detach() after ehci_init()
391		 */
392		ehci_detach(sc);
393
394		int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
395
396		if (err)
397			/* XXX or should we panic? */
398			device_printf(self, "Could not tear down irq, %d\n",
399			    err);
400		sc->sc_intr_hdl = NULL;
401	}
402	if (sc->sc_irq_res) {
403		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
404		sc->sc_irq_res = NULL;
405	}
406	if (sc->sc_io_res) {
407		bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
408		    sc->sc_io_res);
409		sc->sc_io_res = NULL;
410	}
411	usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
412
413	return (0);
414}
415
416static void
417ehci_pci_takecontroller(device_t self)
418{
419	ehci_softc_t *sc = device_get_softc(self);
420	uint32_t cparams;
421	uint32_t eec;
422	uint16_t to;
423	uint8_t eecp;
424	uint8_t bios_sem;
425
426	cparams = EREAD4(sc, EHCI_HCCPARAMS);
427
428	/* Synchronise with the BIOS if it owns the controller. */
429	for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
430	    eecp = EHCI_EECP_NEXT(eec)) {
431		eec = pci_read_config(self, eecp, 4);
432		if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) {
433			continue;
434		}
435		bios_sem = pci_read_config(self, eecp +
436		    EHCI_LEGSUP_BIOS_SEM, 1);
437		if (bios_sem == 0) {
438			continue;
439		}
440		device_printf(sc->sc_bus.bdev, "waiting for BIOS "
441		    "to give up control\n");
442		pci_write_config(self, eecp +
443		    EHCI_LEGSUP_OS_SEM, 1, 1);
444		to = 500;
445		while (1) {
446			bios_sem = pci_read_config(self, eecp +
447			    EHCI_LEGSUP_BIOS_SEM, 1);
448			if (bios_sem == 0)
449				break;
450
451			if (--to == 0) {
452				device_printf(sc->sc_bus.bdev,
453				    "timed out waiting for BIOS\n");
454				break;
455			}
456			usb_pause_mtx(NULL, hz / 100);	/* wait 10ms */
457		}
458	}
459}
460
461static driver_t ehci_driver =
462{
463	.name = "ehci",
464	.methods = (device_method_t[]){
465		/* device interface */
466		DEVMETHOD(device_probe, ehci_pci_probe),
467		DEVMETHOD(device_attach, ehci_pci_attach),
468		DEVMETHOD(device_detach, ehci_pci_detach),
469		DEVMETHOD(device_suspend, ehci_pci_suspend),
470		DEVMETHOD(device_resume, ehci_pci_resume),
471		DEVMETHOD(device_shutdown, ehci_pci_shutdown),
472		/* bus interface */
473		DEVMETHOD(bus_print_child, bus_generic_print_child),
474
475		{0, 0}
476	},
477	.size = sizeof(struct ehci_softc),
478};
479
480static devclass_t ehci_devclass;
481
482DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
483MODULE_DEPEND(ehci, usb, 1, 1, 1);
484