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