xref: /freebsd/sys/dev/usb/controller/ehci_pci.c (revision a3cf0ef5a295c885c895fabfd56470c0d1db322d)
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  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 /*
35  * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
36  *
37  * The EHCI 1.0 spec can be found at
38  * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
39  * and the USB 2.0 spec at
40  * http://www.usb.org/developers/docs/usb_20.zip
41  */
42 
43 /* The low level controller code for EHCI has been split into
44  * PCI probes and EHCI specific code. This was done to facilitate the
45  * sharing of code between *BSD's
46  */
47 
48 #include <sys/stdint.h>
49 #include <sys/stddef.h>
50 #include <sys/param.h>
51 #include <sys/queue.h>
52 #include <sys/types.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/bus.h>
56 #include <sys/linker_set.h>
57 #include <sys/module.h>
58 #include <sys/lock.h>
59 #include <sys/mutex.h>
60 #include <sys/condvar.h>
61 #include <sys/sysctl.h>
62 #include <sys/sx.h>
63 #include <sys/unistd.h>
64 #include <sys/callout.h>
65 #include <sys/malloc.h>
66 #include <sys/priv.h>
67 
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 
71 #include <dev/usb/usb_core.h>
72 #include <dev/usb/usb_busdma.h>
73 #include <dev/usb/usb_process.h>
74 #include <dev/usb/usb_util.h>
75 
76 #include <dev/usb/usb_controller.h>
77 #include <dev/usb/usb_bus.h>
78 #include <dev/usb/usb_pci.h>
79 #include <dev/usb/controller/ehci.h>
80 #include <dev/usb/controller/ehcireg.h>
81 
82 #define	PCI_EHCI_VENDORID_ACERLABS	0x10b9
83 #define	PCI_EHCI_VENDORID_AMD		0x1022
84 #define	PCI_EHCI_VENDORID_APPLE		0x106b
85 #define	PCI_EHCI_VENDORID_ATI		0x1002
86 #define	PCI_EHCI_VENDORID_CMDTECH	0x1095
87 #define	PCI_EHCI_VENDORID_INTEL		0x8086
88 #define	PCI_EHCI_VENDORID_NEC		0x1033
89 #define	PCI_EHCI_VENDORID_OPTI		0x1045
90 #define	PCI_EHCI_VENDORID_PHILIPS	0x1131
91 #define	PCI_EHCI_VENDORID_SIS		0x1039
92 #define	PCI_EHCI_VENDORID_NVIDIA	0x12D2
93 #define	PCI_EHCI_VENDORID_NVIDIA2	0x10DE
94 #define	PCI_EHCI_VENDORID_VIA		0x1106
95 
96 static void ehci_pci_takecontroller(device_t self);
97 
98 static device_probe_t ehci_pci_probe;
99 static device_attach_t ehci_pci_attach;
100 static device_detach_t ehci_pci_detach;
101 static device_suspend_t ehci_pci_suspend;
102 static device_resume_t ehci_pci_resume;
103 static device_shutdown_t ehci_pci_shutdown;
104 
105 static int
106 ehci_pci_suspend(device_t self)
107 {
108 	ehci_softc_t *sc = device_get_softc(self);
109 	int err;
110 
111 	err = bus_generic_suspend(self);
112 	if (err)
113 		return (err);
114 	ehci_suspend(sc);
115 	return (0);
116 }
117 
118 static int
119 ehci_pci_resume(device_t self)
120 {
121 	ehci_softc_t *sc = device_get_softc(self);
122 
123 	ehci_pci_takecontroller(self);
124 	ehci_resume(sc);
125 
126 	bus_generic_resume(self);
127 
128 	return (0);
129 }
130 
131 static int
132 ehci_pci_shutdown(device_t self)
133 {
134 	ehci_softc_t *sc = device_get_softc(self);
135 	int err;
136 
137 	err = bus_generic_shutdown(self);
138 	if (err)
139 		return (err);
140 	ehci_shutdown(sc);
141 
142 	return (0);
143 }
144 
145 static const char *
146 ehci_pci_match(device_t self)
147 {
148 	uint32_t device_id = pci_get_devid(self);
149 
150 	switch (device_id) {
151 	case 0x268c8086:
152 		return ("Intel 63XXESB USB 2.0 controller");
153 
154 	case 0x523910b9:
155 		return "ALi M5239 USB 2.0 controller";
156 
157 	case 0x10227463:
158 		return "AMD 8111 USB 2.0 controller";
159 
160 	case 0x20951022:
161 		return ("AMD CS5536 (Geode) USB 2.0 controller");
162 
163 	case 0x43451002:
164 		return "ATI SB200 USB 2.0 controller";
165 	case 0x43731002:
166 		return "ATI SB400 USB 2.0 controller";
167 
168 	case 0x25ad8086:
169 		return "Intel 6300ESB USB 2.0 controller";
170 	case 0x24cd8086:
171 		return "Intel 82801DB/L/M (ICH4) USB 2.0 controller";
172 	case 0x24dd8086:
173 		return "Intel 82801EB/R (ICH5) USB 2.0 controller";
174 	case 0x265c8086:
175 		return "Intel 82801FB (ICH6) USB 2.0 controller";
176 	case 0x27cc8086:
177 		return "Intel 82801GB/R (ICH7) USB 2.0 controller";
178 
179 	case 0x28368086:
180 		return "Intel 82801H (ICH8) USB 2.0 controller USB2-A";
181 	case 0x283a8086:
182 		return "Intel 82801H (ICH8) USB 2.0 controller USB2-B";
183 	case 0x293a8086:
184 		return "Intel 82801I (ICH9) USB 2.0 controller";
185 	case 0x293c8086:
186 		return "Intel 82801I (ICH9) USB 2.0 controller";
187 	case 0x3a3a8086:
188 		return "Intel 82801JI (ICH10) USB 2.0 controller USB-A";
189 	case 0x3a3c8086:
190 		return "Intel 82801JI (ICH10) USB 2.0 controller USB-B";
191 	case 0x3b348086:
192 		return ("Intel PCH USB 2.0 controller USB-A");
193 	case 0x3b3c8086:
194 		return ("Intel PCH USB 2.0 controller USB-B");
195 
196 	case 0x00e01033:
197 		return ("NEC uPD 720100 USB 2.0 controller");
198 
199 	case 0x006810de:
200 		return "NVIDIA nForce2 USB 2.0 controller";
201 	case 0x008810de:
202 		return "NVIDIA nForce2 Ultra 400 USB 2.0 controller";
203 	case 0x00d810de:
204 		return "NVIDIA nForce3 USB 2.0 controller";
205 	case 0x00e810de:
206 		return "NVIDIA nForce3 250 USB 2.0 controller";
207 	case 0x005b10de:
208 		return "NVIDIA nForce4 USB 2.0 controller";
209 	case 0x036d10de:
210 		return "NVIDIA nForce MCP55 USB 2.0 controller";
211 	case 0x03f210de:
212 		return "NVIDIA nForce MCP61 USB 2.0 controller";
213 	case 0x0aa610de:
214 		return "NVIDIA nForce MCP79 USB 2.0 controller";
215 	case 0x0aa910de:
216 		return "NVIDIA nForce MCP79 USB 2.0 controller";
217 	case 0x0aaa10de:
218 		return "NVIDIA nForce MCP79 USB 2.0 controller";
219 
220 	case 0x15621131:
221 		return "Philips ISP156x USB 2.0 controller";
222 
223 	case 0x31041106:
224 		return ("VIA VT6202 USB 2.0 controller");
225 
226 	default:
227 		break;
228 	}
229 
230 	if ((pci_get_class(self) == PCIC_SERIALBUS)
231 	    && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
232 	    && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) {
233 		return ("EHCI (generic) USB 2.0 controller");
234 	}
235 	return (NULL);			/* dunno */
236 }
237 
238 static int
239 ehci_pci_probe(device_t self)
240 {
241 	const char *desc = ehci_pci_match(self);
242 
243 	if (desc) {
244 		device_set_desc(self, desc);
245 		return (0);
246 	} else {
247 		return (ENXIO);
248 	}
249 }
250 
251 static void
252 ehci_pci_ati_quirk(device_t self, uint8_t is_sb700)
253 {
254 	device_t smbdev;
255 	uint32_t val;
256 
257 	if (is_sb700) {
258 		/* Lookup SMBUS PCI device */
259 		smbdev = pci_find_device(PCI_EHCI_VENDORID_ATI, 0x4385);
260 		if (smbdev == NULL)
261 			return;
262 		val = pci_get_revid(smbdev);
263 		if (val != 0x3a && val != 0x3b)
264 			return;
265 	}
266 
267 	/*
268 	 * Note: this bit is described as reserved in SB700
269 	 * Register Reference Guide.
270 	 */
271 	val = pci_read_config(self, 0x53, 1);
272 	if (!(val & 0x8)) {
273 		val |= 0x8;
274 		pci_write_config(self, 0x53, val, 1);
275 		device_printf(self, "AMD SB600/700 quirk applied\n");
276 	}
277 }
278 
279 static void
280 ehci_pci_via_quirk(device_t self)
281 {
282 	uint32_t val;
283 
284 	if ((pci_get_device(self) == 0x3104) &&
285 	    ((pci_get_revid(self) & 0xf0) == 0x60)) {
286 		/* Correct schedule sleep time to 10us */
287 		val = pci_read_config(self, 0x4b, 1);
288 		if (val & 0x20)
289 			return;
290 		pci_write_config(self, 0x4b, val, 1);
291 		device_printf(self, "VIA-quirk applied\n");
292 	}
293 }
294 
295 static int
296 ehci_pci_attach(device_t self)
297 {
298 	ehci_softc_t *sc = device_get_softc(self);
299 	int err;
300 	int rid;
301 
302 	/* initialise some bus fields */
303 	sc->sc_bus.parent = self;
304 	sc->sc_bus.devices = sc->sc_devices;
305 	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
306 
307 	/* get all DMA memory */
308 	if (usb_bus_mem_alloc_all(&sc->sc_bus,
309 	    USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
310 		return (ENOMEM);
311 	}
312 
313 	pci_enable_busmaster(self);
314 
315 	switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
316 	case PCI_USB_REV_PRE_1_0:
317 	case PCI_USB_REV_1_0:
318 	case PCI_USB_REV_1_1:
319 		/*
320 		 * NOTE: some EHCI USB controllers have the wrong USB
321 		 * revision number. It appears those controllers are
322 		 * fully compliant so we just ignore this value in
323 		 * some common cases.
324 		 */
325 		device_printf(self, "pre-2.0 USB revision (ignored)\n");
326 		/* fallthrough */
327 	case PCI_USB_REV_2_0:
328 		break;
329 	default:
330 		/* Quirk for Parallels Desktop 4.0 */
331 		device_printf(self, "USB revision is unknown. Assuming v2.0.\n");
332 		break;
333 	}
334 
335 	rid = PCI_CBMEM;
336 	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
337 	    RF_ACTIVE);
338 	if (!sc->sc_io_res) {
339 		device_printf(self, "Could not map memory\n");
340 		goto error;
341 	}
342 	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
343 	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
344 	sc->sc_io_size = rman_get_size(sc->sc_io_res);
345 
346 	rid = 0;
347 	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
348 	    RF_SHAREABLE | RF_ACTIVE);
349 	if (sc->sc_irq_res == NULL) {
350 		device_printf(self, "Could not allocate irq\n");
351 		goto error;
352 	}
353 	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
354 	if (!sc->sc_bus.bdev) {
355 		device_printf(self, "Could not add USB device\n");
356 		goto error;
357 	}
358 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
359 
360 	/*
361 	 * ehci_pci_match will never return NULL if ehci_pci_probe
362 	 * succeeded
363 	 */
364 	device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
365 	switch (pci_get_vendor(self)) {
366 	case PCI_EHCI_VENDORID_ACERLABS:
367 		sprintf(sc->sc_vendor, "AcerLabs");
368 		break;
369 	case PCI_EHCI_VENDORID_AMD:
370 		sprintf(sc->sc_vendor, "AMD");
371 		break;
372 	case PCI_EHCI_VENDORID_APPLE:
373 		sprintf(sc->sc_vendor, "Apple");
374 		break;
375 	case PCI_EHCI_VENDORID_ATI:
376 		sprintf(sc->sc_vendor, "ATI");
377 		break;
378 	case PCI_EHCI_VENDORID_CMDTECH:
379 		sprintf(sc->sc_vendor, "CMDTECH");
380 		break;
381 	case PCI_EHCI_VENDORID_INTEL:
382 		sprintf(sc->sc_vendor, "Intel");
383 		break;
384 	case PCI_EHCI_VENDORID_NEC:
385 		sprintf(sc->sc_vendor, "NEC");
386 		break;
387 	case PCI_EHCI_VENDORID_OPTI:
388 		sprintf(sc->sc_vendor, "OPTi");
389 		break;
390 	case PCI_EHCI_VENDORID_PHILIPS:
391 		sprintf(sc->sc_vendor, "Philips");
392 		break;
393 	case PCI_EHCI_VENDORID_SIS:
394 		sprintf(sc->sc_vendor, "SiS");
395 		break;
396 	case PCI_EHCI_VENDORID_NVIDIA:
397 	case PCI_EHCI_VENDORID_NVIDIA2:
398 		sprintf(sc->sc_vendor, "nVidia");
399 		break;
400 	case PCI_EHCI_VENDORID_VIA:
401 		sprintf(sc->sc_vendor, "VIA");
402 		break;
403 	default:
404 		if (bootverbose)
405 			device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
406 			    pci_get_devid(self));
407 		sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
408 	}
409 
410 #if (__FreeBSD_version >= 700031)
411 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
412 	    NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
413 #else
414 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
415 	    (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
416 #endif
417 	if (err) {
418 		device_printf(self, "Could not setup irq, %d\n", err);
419 		sc->sc_intr_hdl = NULL;
420 		goto error;
421 	}
422 	ehci_pci_takecontroller(self);
423 
424 	/* Undocumented quirks taken from Linux */
425 
426 	switch (pci_get_vendor(self)) {
427 	case PCI_EHCI_VENDORID_ATI:
428 		/* SB600 and SB700 EHCI quirk */
429 		switch (pci_get_device(self)) {
430 		case 0x4386:
431 			ehci_pci_ati_quirk(self, 0);
432 			break;
433 		case 0x4396:
434 			ehci_pci_ati_quirk(self, 1);
435 			break;
436 		default:
437 			break;
438 		}
439 		break;
440 
441 	case PCI_EHCI_VENDORID_VIA:
442 		ehci_pci_via_quirk(self);
443 		break;
444 
445 	default:
446 		break;
447 	}
448 
449 	/* Dropped interrupts workaround */
450 	switch (pci_get_vendor(self)) {
451 	case PCI_EHCI_VENDORID_ATI:
452 	case PCI_EHCI_VENDORID_VIA:
453 		sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
454 		if (bootverbose)
455 			device_printf(self,
456 			    "Dropped interrupts workaround enabled\n");
457 		break;
458 	default:
459 		break;
460 	}
461 
462 	/* Doorbell feature workaround */
463 	switch (pci_get_vendor(self)) {
464 	case PCI_EHCI_VENDORID_NVIDIA:
465 	case PCI_EHCI_VENDORID_NVIDIA2:
466 		sc->sc_flags |= EHCI_SCFLG_IAADBUG;
467 		if (bootverbose)
468 			device_printf(self,
469 			    "Doorbell workaround enabled\n");
470 		break;
471 	default:
472 		break;
473 	}
474 
475 	err = ehci_init(sc);
476 	if (!err) {
477 		err = device_probe_and_attach(sc->sc_bus.bdev);
478 	}
479 	if (err) {
480 		device_printf(self, "USB init failed err=%d\n", err);
481 		goto error;
482 	}
483 	return (0);
484 
485 error:
486 	ehci_pci_detach(self);
487 	return (ENXIO);
488 }
489 
490 static int
491 ehci_pci_detach(device_t self)
492 {
493 	ehci_softc_t *sc = device_get_softc(self);
494 	device_t bdev;
495 
496 	if (sc->sc_bus.bdev) {
497 		bdev = sc->sc_bus.bdev;
498 		device_detach(bdev);
499 		device_delete_child(self, bdev);
500 	}
501 	/* during module unload there are lots of children leftover */
502 	device_delete_all_children(self);
503 
504 	pci_disable_busmaster(self);
505 
506 	/*
507 	 * disable interrupts that might have been switched on in ehci_init
508 	 */
509 	if (sc->sc_io_res) {
510 		EWRITE4(sc, EHCI_USBINTR, 0);
511 	}
512 	if (sc->sc_irq_res && sc->sc_intr_hdl) {
513 		/*
514 		 * only call ehci_detach() after ehci_init()
515 		 */
516 		ehci_detach(sc);
517 
518 		int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
519 
520 		if (err)
521 			/* XXX or should we panic? */
522 			device_printf(self, "Could not tear down irq, %d\n",
523 			    err);
524 		sc->sc_intr_hdl = NULL;
525 	}
526 	if (sc->sc_irq_res) {
527 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
528 		sc->sc_irq_res = NULL;
529 	}
530 	if (sc->sc_io_res) {
531 		bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
532 		    sc->sc_io_res);
533 		sc->sc_io_res = NULL;
534 	}
535 	usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
536 
537 	return (0);
538 }
539 
540 static void
541 ehci_pci_takecontroller(device_t self)
542 {
543 	ehci_softc_t *sc = device_get_softc(self);
544 	uint32_t cparams;
545 	uint32_t eec;
546 	uint16_t to;
547 	uint8_t eecp;
548 	uint8_t bios_sem;
549 
550 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
551 
552 	/* Synchronise with the BIOS if it owns the controller. */
553 	for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
554 	    eecp = EHCI_EECP_NEXT(eec)) {
555 		eec = pci_read_config(self, eecp, 4);
556 		if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) {
557 			continue;
558 		}
559 		bios_sem = pci_read_config(self, eecp +
560 		    EHCI_LEGSUP_BIOS_SEM, 1);
561 		if (bios_sem == 0) {
562 			continue;
563 		}
564 		device_printf(sc->sc_bus.bdev, "waiting for BIOS "
565 		    "to give up control\n");
566 		pci_write_config(self, eecp +
567 		    EHCI_LEGSUP_OS_SEM, 1, 1);
568 		to = 500;
569 		while (1) {
570 			bios_sem = pci_read_config(self, eecp +
571 			    EHCI_LEGSUP_BIOS_SEM, 1);
572 			if (bios_sem == 0)
573 				break;
574 
575 			if (--to == 0) {
576 				device_printf(sc->sc_bus.bdev,
577 				    "timed out waiting for BIOS\n");
578 				break;
579 			}
580 			usb_pause_mtx(NULL, hz / 100);	/* wait 10ms */
581 		}
582 	}
583 }
584 
585 static driver_t ehci_driver =
586 {
587 	.name = "ehci",
588 	.methods = (device_method_t[]){
589 		/* device interface */
590 		DEVMETHOD(device_probe, ehci_pci_probe),
591 		DEVMETHOD(device_attach, ehci_pci_attach),
592 		DEVMETHOD(device_detach, ehci_pci_detach),
593 		DEVMETHOD(device_suspend, ehci_pci_suspend),
594 		DEVMETHOD(device_resume, ehci_pci_resume),
595 		DEVMETHOD(device_shutdown, ehci_pci_shutdown),
596 		/* bus interface */
597 		DEVMETHOD(bus_print_child, bus_generic_print_child),
598 
599 		{0, 0}
600 	},
601 	.size = sizeof(struct ehci_softc),
602 };
603 
604 static devclass_t ehci_devclass;
605 
606 DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
607 MODULE_DEPEND(ehci, usb, 1, 1, 1);
608