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