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