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