xref: /freebsd/sys/dev/usb/controller/ehci_pci.c (revision ab0b9f6b3073e6c4d1dfbf07444d7db67a189a96)
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/module.h>
57 #include <sys/lock.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/sysctl.h>
61 #include <sys/sx.h>
62 #include <sys/unistd.h>
63 #include <sys/callout.h>
64 #include <sys/malloc.h>
65 #include <sys/priv.h>
66 
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
69 
70 #include <dev/usb/usb_core.h>
71 #include <dev/usb/usb_busdma.h>
72 #include <dev/usb/usb_process.h>
73 #include <dev/usb/usb_util.h>
74 
75 #include <dev/usb/usb_controller.h>
76 #include <dev/usb/usb_bus.h>
77 #include <dev/usb/usb_pci.h>
78 #include <dev/usb/controller/ehci.h>
79 #include <dev/usb/controller/ehcireg.h>
80 #include "usb_if.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 device_probe_t ehci_pci_probe;
97 static device_attach_t ehci_pci_attach;
98 static device_detach_t ehci_pci_detach;
99 static usb_take_controller_t ehci_pci_take_controller;
100 
101 static const char *
102 ehci_pci_match(device_t self)
103 {
104 	uint32_t device_id = pci_get_devid(self);
105 
106 	switch (device_id) {
107 	case 0x523910b9:
108 		return "ALi M5239 USB 2.0 controller";
109 
110 	case 0x10227463:
111 		return "AMD 8111 USB 2.0 controller";
112 
113 	case 0x20951022:
114 		return ("AMD CS5536 (Geode) USB 2.0 controller");
115 
116 	case 0x43451002:
117 		return "ATI SB200 USB 2.0 controller";
118 	case 0x43731002:
119 		return "ATI SB400 USB 2.0 controller";
120 	case 0x43961002:
121 		return ("AMD SB7x0/SB8x0/SB9x0 USB 2.0 controller");
122 
123 	case 0x1e268086:
124 		return ("Intel Panther Point USB 2.0 controller");
125 	case 0x1e2d8086:
126 		return ("Intel Panther Point USB 2.0 controller");
127 	case 0x1f2c8086:
128 		return ("Intel Avoton USB 2.0 controller");
129 	case 0x25ad8086:
130 		return "Intel 6300ESB USB 2.0 controller";
131 	case 0x24cd8086:
132 		return "Intel 82801DB/L/M (ICH4) USB 2.0 controller";
133 	case 0x24dd8086:
134 		return "Intel 82801EB/R (ICH5) USB 2.0 controller";
135 	case 0x265c8086:
136 		return "Intel 82801FB (ICH6) USB 2.0 controller";
137 	case 0x268c8086:
138 		return ("Intel 63XXESB USB 2.0 controller");
139 	case 0x27cc8086:
140 		return "Intel 82801GB/R (ICH7) USB 2.0 controller";
141 	case 0x28368086:
142 		return "Intel 82801H (ICH8) USB 2.0 controller USB2-A";
143 	case 0x283a8086:
144 		return "Intel 82801H (ICH8) USB 2.0 controller USB2-B";
145 	case 0x293a8086:
146 		return "Intel 82801I (ICH9) USB 2.0 controller";
147 	case 0x293c8086:
148 		return "Intel 82801I (ICH9) USB 2.0 controller";
149 	case 0x3a3a8086:
150 		return "Intel 82801JI (ICH10) USB 2.0 controller USB-A";
151 	case 0x3a3c8086:
152 		return "Intel 82801JI (ICH10) USB 2.0 controller USB-B";
153 	case 0x3b348086:
154 		return ("Intel PCH USB 2.0 controller USB-A");
155 	case 0x3b3c8086:
156 		return ("Intel PCH USB 2.0 controller USB-B");
157 	case 0x8c268086:
158 		return ("Intel Lynx Point USB 2.0 controller USB-A");
159 	case 0x8c2d8086:
160 		return ("Intel Lynx Point USB 2.0 controller USB-B");
161 
162 	case 0x00e01033:
163 		return ("NEC uPD 720100 USB 2.0 controller");
164 
165 	case 0x006810de:
166 		return "NVIDIA nForce2 USB 2.0 controller";
167 	case 0x008810de:
168 		return "NVIDIA nForce2 Ultra 400 USB 2.0 controller";
169 	case 0x00d810de:
170 		return "NVIDIA nForce3 USB 2.0 controller";
171 	case 0x00e810de:
172 		return "NVIDIA nForce3 250 USB 2.0 controller";
173 	case 0x005b10de:
174 		return "NVIDIA nForce CK804 USB 2.0 controller";
175 	case 0x036d10de:
176 		return "NVIDIA nForce MCP55 USB 2.0 controller";
177 	case 0x03f210de:
178 		return "NVIDIA nForce MCP61 USB 2.0 controller";
179 	case 0x0aa610de:
180 		return "NVIDIA nForce MCP79 USB 2.0 controller";
181 	case 0x0aa910de:
182 		return "NVIDIA nForce MCP79 USB 2.0 controller";
183 	case 0x0aaa10de:
184 		return "NVIDIA nForce MCP79 USB 2.0 controller";
185 
186 	case 0x15621131:
187 		return "Philips ISP156x USB 2.0 controller";
188 
189 	case 0x31041106:
190 		return ("VIA VT6202 USB 2.0 controller");
191 
192 	default:
193 		break;
194 	}
195 
196 	if ((pci_get_class(self) == PCIC_SERIALBUS)
197 	    && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
198 	    && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) {
199 		return ("EHCI (generic) USB 2.0 controller");
200 	}
201 	return (NULL);			/* dunno */
202 }
203 
204 static int
205 ehci_pci_probe(device_t self)
206 {
207 	const char *desc = ehci_pci_match(self);
208 
209 	if (desc) {
210 		device_set_desc(self, desc);
211 		return (0);
212 	} else {
213 		return (ENXIO);
214 	}
215 }
216 
217 static void
218 ehci_pci_ati_quirk(device_t self, uint8_t is_sb700)
219 {
220 	device_t smbdev;
221 	uint32_t val;
222 
223 	if (is_sb700) {
224 		/* Lookup SMBUS PCI device */
225 		smbdev = pci_find_device(PCI_EHCI_VENDORID_ATI, 0x4385);
226 		if (smbdev == NULL)
227 			return;
228 		val = pci_get_revid(smbdev);
229 		if (val != 0x3a && val != 0x3b)
230 			return;
231 	}
232 
233 	/*
234 	 * Note: this bit is described as reserved in SB700
235 	 * Register Reference Guide.
236 	 */
237 	val = pci_read_config(self, 0x53, 1);
238 	if (!(val & 0x8)) {
239 		val |= 0x8;
240 		pci_write_config(self, 0x53, val, 1);
241 		device_printf(self, "AMD SB600/700 quirk applied\n");
242 	}
243 }
244 
245 static void
246 ehci_pci_via_quirk(device_t self)
247 {
248 	uint32_t val;
249 
250 	if ((pci_get_device(self) == 0x3104) &&
251 	    ((pci_get_revid(self) & 0xf0) == 0x60)) {
252 		/* Correct schedule sleep time to 10us */
253 		val = pci_read_config(self, 0x4b, 1);
254 		if (val & 0x20)
255 			return;
256 		val |= 0x20;
257 		pci_write_config(self, 0x4b, val, 1);
258 		device_printf(self, "VIA-quirk applied\n");
259 	}
260 }
261 
262 static int
263 ehci_pci_attach(device_t self)
264 {
265 	ehci_softc_t *sc = device_get_softc(self);
266 	int err;
267 	int rid;
268 
269 	/* initialise some bus fields */
270 	sc->sc_bus.parent = self;
271 	sc->sc_bus.devices = sc->sc_devices;
272 	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
273 
274 	/* get all DMA memory */
275 	if (usb_bus_mem_alloc_all(&sc->sc_bus,
276 	    USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
277 		return (ENOMEM);
278 	}
279 
280 	pci_enable_busmaster(self);
281 
282 	switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
283 	case PCI_USB_REV_PRE_1_0:
284 	case PCI_USB_REV_1_0:
285 	case PCI_USB_REV_1_1:
286 		/*
287 		 * NOTE: some EHCI USB controllers have the wrong USB
288 		 * revision number. It appears those controllers are
289 		 * fully compliant so we just ignore this value in
290 		 * some common cases.
291 		 */
292 		device_printf(self, "pre-2.0 USB revision (ignored)\n");
293 		/* fallthrough */
294 	case PCI_USB_REV_2_0:
295 		break;
296 	default:
297 		/* Quirk for Parallels Desktop 4.0 */
298 		device_printf(self, "USB revision is unknown. Assuming v2.0.\n");
299 		break;
300 	}
301 
302 	rid = PCI_CBMEM;
303 	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
304 	    RF_ACTIVE);
305 	if (!sc->sc_io_res) {
306 		device_printf(self, "Could not map memory\n");
307 		goto error;
308 	}
309 	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
310 	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
311 	sc->sc_io_size = rman_get_size(sc->sc_io_res);
312 
313 	rid = 0;
314 	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
315 	    RF_SHAREABLE | RF_ACTIVE);
316 	if (sc->sc_irq_res == NULL) {
317 		device_printf(self, "Could not allocate irq\n");
318 		goto error;
319 	}
320 	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
321 	if (!sc->sc_bus.bdev) {
322 		device_printf(self, "Could not add USB device\n");
323 		goto error;
324 	}
325 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
326 
327 	/*
328 	 * ehci_pci_match will never return NULL if ehci_pci_probe
329 	 * succeeded
330 	 */
331 	device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
332 	switch (pci_get_vendor(self)) {
333 	case PCI_EHCI_VENDORID_ACERLABS:
334 		sprintf(sc->sc_vendor, "AcerLabs");
335 		break;
336 	case PCI_EHCI_VENDORID_AMD:
337 		sprintf(sc->sc_vendor, "AMD");
338 		break;
339 	case PCI_EHCI_VENDORID_APPLE:
340 		sprintf(sc->sc_vendor, "Apple");
341 		break;
342 	case PCI_EHCI_VENDORID_ATI:
343 		sprintf(sc->sc_vendor, "ATI");
344 		break;
345 	case PCI_EHCI_VENDORID_CMDTECH:
346 		sprintf(sc->sc_vendor, "CMDTECH");
347 		break;
348 	case PCI_EHCI_VENDORID_INTEL:
349 		sprintf(sc->sc_vendor, "Intel");
350 		break;
351 	case PCI_EHCI_VENDORID_NEC:
352 		sprintf(sc->sc_vendor, "NEC");
353 		break;
354 	case PCI_EHCI_VENDORID_OPTI:
355 		sprintf(sc->sc_vendor, "OPTi");
356 		break;
357 	case PCI_EHCI_VENDORID_PHILIPS:
358 		sprintf(sc->sc_vendor, "Philips");
359 		break;
360 	case PCI_EHCI_VENDORID_SIS:
361 		sprintf(sc->sc_vendor, "SiS");
362 		break;
363 	case PCI_EHCI_VENDORID_NVIDIA:
364 	case PCI_EHCI_VENDORID_NVIDIA2:
365 		sprintf(sc->sc_vendor, "nVidia");
366 		break;
367 	case PCI_EHCI_VENDORID_VIA:
368 		sprintf(sc->sc_vendor, "VIA");
369 		break;
370 	default:
371 		if (bootverbose)
372 			device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
373 			    pci_get_devid(self));
374 		sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
375 	}
376 
377 #if (__FreeBSD_version >= 700031)
378 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
379 	    NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
380 #else
381 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
382 	    (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
383 #endif
384 	if (err) {
385 		device_printf(self, "Could not setup irq, %d\n", err);
386 		sc->sc_intr_hdl = NULL;
387 		goto error;
388 	}
389 	ehci_pci_take_controller(self);
390 
391 	/* Undocumented quirks taken from Linux */
392 
393 	switch (pci_get_vendor(self)) {
394 	case PCI_EHCI_VENDORID_ATI:
395 		/* SB600 and SB700 EHCI quirk */
396 		switch (pci_get_device(self)) {
397 		case 0x4386:
398 			ehci_pci_ati_quirk(self, 0);
399 			break;
400 		case 0x4396:
401 			ehci_pci_ati_quirk(self, 1);
402 			break;
403 		default:
404 			break;
405 		}
406 		break;
407 
408 	case PCI_EHCI_VENDORID_VIA:
409 		ehci_pci_via_quirk(self);
410 		break;
411 
412 	default:
413 		break;
414 	}
415 
416 	/* Dropped interrupts workaround */
417 	switch (pci_get_vendor(self)) {
418 	case PCI_EHCI_VENDORID_ATI:
419 	case PCI_EHCI_VENDORID_VIA:
420 		sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
421 		if (bootverbose)
422 			device_printf(self,
423 			    "Dropped interrupts workaround enabled\n");
424 		break;
425 	default:
426 		break;
427 	}
428 
429 	/* Doorbell feature workaround */
430 	switch (pci_get_vendor(self)) {
431 	case PCI_EHCI_VENDORID_NVIDIA:
432 	case PCI_EHCI_VENDORID_NVIDIA2:
433 		sc->sc_flags |= EHCI_SCFLG_IAADBUG;
434 		if (bootverbose)
435 			device_printf(self,
436 			    "Doorbell workaround enabled\n");
437 		break;
438 	default:
439 		break;
440 	}
441 
442 	err = ehci_init(sc);
443 	if (!err) {
444 		err = device_probe_and_attach(sc->sc_bus.bdev);
445 	}
446 	if (err) {
447 		device_printf(self, "USB init failed err=%d\n", err);
448 		goto error;
449 	}
450 	return (0);
451 
452 error:
453 	ehci_pci_detach(self);
454 	return (ENXIO);
455 }
456 
457 static int
458 ehci_pci_detach(device_t self)
459 {
460 	ehci_softc_t *sc = device_get_softc(self);
461 	device_t bdev;
462 
463 	if (sc->sc_bus.bdev) {
464 		bdev = sc->sc_bus.bdev;
465 		device_detach(bdev);
466 		device_delete_child(self, bdev);
467 	}
468 	/* during module unload there are lots of children leftover */
469 	device_delete_children(self);
470 
471 	pci_disable_busmaster(self);
472 
473 	if (sc->sc_irq_res && sc->sc_intr_hdl) {
474 		/*
475 		 * only call ehci_detach() after ehci_init()
476 		 */
477 		ehci_detach(sc);
478 
479 		int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
480 
481 		if (err)
482 			/* XXX or should we panic? */
483 			device_printf(self, "Could not tear down irq, %d\n",
484 			    err);
485 		sc->sc_intr_hdl = NULL;
486 	}
487 	if (sc->sc_irq_res) {
488 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
489 		sc->sc_irq_res = NULL;
490 	}
491 	if (sc->sc_io_res) {
492 		bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
493 		    sc->sc_io_res);
494 		sc->sc_io_res = NULL;
495 	}
496 	usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
497 
498 	return (0);
499 }
500 
501 static int
502 ehci_pci_take_controller(device_t self)
503 {
504 	ehci_softc_t *sc = device_get_softc(self);
505 	uint32_t cparams;
506 	uint32_t eec;
507 	uint16_t to;
508 	uint8_t eecp;
509 	uint8_t bios_sem;
510 
511 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
512 
513 	/* Synchronise with the BIOS if it owns the controller. */
514 	for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
515 	    eecp = EHCI_EECP_NEXT(eec)) {
516 		eec = pci_read_config(self, eecp, 4);
517 		if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) {
518 			continue;
519 		}
520 		bios_sem = pci_read_config(self, eecp +
521 		    EHCI_LEGSUP_BIOS_SEM, 1);
522 		if (bios_sem == 0) {
523 			continue;
524 		}
525 		device_printf(sc->sc_bus.bdev, "waiting for BIOS "
526 		    "to give up control\n");
527 		pci_write_config(self, eecp +
528 		    EHCI_LEGSUP_OS_SEM, 1, 1);
529 		to = 500;
530 		while (1) {
531 			bios_sem = pci_read_config(self, eecp +
532 			    EHCI_LEGSUP_BIOS_SEM, 1);
533 			if (bios_sem == 0)
534 				break;
535 
536 			if (--to == 0) {
537 				device_printf(sc->sc_bus.bdev,
538 				    "timed out waiting for BIOS\n");
539 				break;
540 			}
541 			usb_pause_mtx(NULL, hz / 100);	/* wait 10ms */
542 		}
543 	}
544 	return (0);
545 }
546 
547 static device_method_t ehci_pci_methods[] = {
548 	/* Device interface */
549 	DEVMETHOD(device_probe, ehci_pci_probe),
550 	DEVMETHOD(device_attach, ehci_pci_attach),
551 	DEVMETHOD(device_detach, ehci_pci_detach),
552 	DEVMETHOD(device_suspend, bus_generic_suspend),
553 	DEVMETHOD(device_resume, bus_generic_resume),
554 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
555 	DEVMETHOD(usb_take_controller, ehci_pci_take_controller),
556 
557 	DEVMETHOD_END
558 };
559 
560 static driver_t ehci_driver = {
561 	.name = "ehci",
562 	.methods = ehci_pci_methods,
563 	.size = sizeof(struct ehci_softc),
564 };
565 
566 static devclass_t ehci_devclass;
567 
568 DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
569 MODULE_DEPEND(ehci, usb, 1, 1, 1);
570