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