2 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Lennart Augustsson (augustss@carlstedt.se) at
7 * Carlstedt Research & Technology.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 /* Universal Host Controller Interface
43 * UHCI spec: http://www.intel.com/
46 /* The low level controller code for UHCI has been split into
47 * PCI probes and UHCI specific code. This was done to facilitate the
48 * sharing of code between *BSD's
51 #include <sys/stdint.h>
52 #include <sys/stddef.h>
53 #include <sys/param.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
59 #include <sys/linker_set.h>
60 #include <sys/module.h>
62 #include <sys/mutex.h>
63 #include <sys/condvar.h>
64 #include <sys/sysctl.h>
66 #include <sys/unistd.h>
67 #include <sys/callout.h>
68 #include <sys/malloc.h>
71 #include <dev/usb/usb.h>
72 #include <dev/usb/usbdi.h>
74 #include <dev/usb/usb_core.h>
75 #include <dev/usb/usb_busdma.h>
76 #include <dev/usb/usb_process.h>
77 #include <dev/usb/usb_util.h>
78 #include <dev/usb/usb_debug.h>
80 #include <dev/usb/usb_controller.h>
81 #include <dev/usb/usb_bus.h>
82 #include <dev/usb/usb_pci.h>
83 #include <dev/usb/controller/uhci.h>
85 #define PCI_UHCI_VENDORID_INTEL 0x8086
86 #define PCI_UHCI_VENDORID_VIA 0x1106
88 /* PIIX4E has no separate stepping */
90 #define PCI_UHCI_BASE_REG 0x20
92 static device_probe_t uhci_pci_probe;
93 static device_attach_t uhci_pci_attach;
94 static device_detach_t uhci_pci_detach;
95 static device_suspend_t uhci_pci_suspend;
96 static device_resume_t uhci_pci_resume;
99 uhci_pci_suspend(device_t self)
101 uhci_softc_t *sc = device_get_softc(self);
104 err = bus_generic_suspend(self);
113 uhci_pci_resume(device_t self)
115 uhci_softc_t *sc = device_get_softc(self);
117 pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
121 bus_generic_resume(self);
126 uhci_pci_match(device_t self)
128 uint32_t device_id = pci_get_devid(self);
132 return ("Intel 631XESB/632XESB/3100 USB controller USB-1");
135 return ("Intel 631XESB/632XESB/3100 USB controller USB-2");
138 return ("Intel 631XESB/632XESB/3100 USB controller USB-3");
141 return ("Intel 631XESB/632XESB/3100 USB controller USB-4");
144 return ("Intel 82371SB (PIIX3) USB controller");
147 return ("Intel 82371AB/EB (PIIX4) USB controller");
150 return ("Intel 82801AA (ICH) USB controller");
153 return ("Intel 82801AB (ICH0) USB controller");
156 return ("Intel 82801BA/BAM (ICH2) USB controller USB-A");
159 return ("Intel 82801BA/BAM (ICH2) USB controller USB-B");
162 return ("Intel 82801CA/CAM (ICH3) USB controller USB-A");
165 return ("Intel 82801CA/CAM (ICH3) USB controller USB-B");
168 return ("Intel 82801CA/CAM (ICH3) USB controller USB-C");
171 return ("Intel 82801DB (ICH4) USB controller USB-A");
174 return ("Intel 82801DB (ICH4) USB controller USB-B");
177 return ("Intel 82801DB (ICH4) USB controller USB-C");
180 return ("Intel 82801EB (ICH5) USB controller USB-A");
183 return ("Intel 82801EB (ICH5) USB controller USB-B");
186 return ("Intel 82801EB (ICH5) USB controller USB-C");
189 return ("Intel 82801EB (ICH5) USB controller USB-D");
192 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-A");
195 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-B");
198 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-C");
201 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-D");
204 return ("Intel 82801G (ICH7) USB controller USB-A");
206 return ("Intel 82801G (ICH7) USB controller USB-B");
208 return ("Intel 82801G (ICH7) USB controller USB-C");
210 return ("Intel 82801G (ICH7) USB controller USB-D");
213 return ("Intel 82801H (ICH8) USB controller USB-A");
215 return ("Intel 82801H (ICH8) USB controller USB-B");
217 return ("Intel 82801H (ICH8) USB controller USB-C");
219 return ("Intel 82801H (ICH8) USB controller USB-D");
221 return ("Intel 82801H (ICH8) USB controller USB-E");
223 return ("Intel 82801I (ICH9) USB controller");
225 return ("Intel 82801I (ICH9) USB controller");
227 return ("Intel 82801I (ICH9) USB controller");
229 return ("Intel 82801I (ICH9) USB controller");
231 return ("Intel 82801I (ICH9) USB controller");
233 return ("Intel 82801I (ICH9) USB controller");
236 return ("Intel 82443MX USB controller");
239 return ("Intel 82372FB/82468GX USB controller");
242 return ("VIA 83C572 USB controller");
248 if ((pci_get_class(self) == PCIC_SERIALBUS) &&
249 (pci_get_subclass(self) == PCIS_SERIALBUS_USB) &&
250 (pci_get_progif(self) == PCI_INTERFACE_UHCI)) {
251 return ("UHCI (generic) USB controller");
257 uhci_pci_probe(device_t self)
259 const char *desc = uhci_pci_match(self);
262 device_set_desc(self, desc);
270 uhci_pci_attach(device_t self)
272 uhci_softc_t *sc = device_get_softc(self);
276 /* initialise some bus fields */
277 sc->sc_bus.parent = self;
278 sc->sc_bus.devices = sc->sc_devices;
279 sc->sc_bus.devices_max = UHCI_MAX_DEVICES;
281 /* get all DMA memory */
282 if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
283 &uhci_iterate_hw_softc)) {
288 pci_enable_busmaster(self);
290 rid = PCI_UHCI_BASE_REG;
291 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid,
293 if (!sc->sc_io_res) {
294 device_printf(self, "Could not map ports\n");
297 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
298 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
299 sc->sc_io_size = rman_get_size(sc->sc_io_res);
301 /* disable interrupts */
302 bus_space_write_2(sc->sc_io_tag, sc->sc_io_hdl, UHCI_INTR, 0);
305 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
306 RF_SHAREABLE | RF_ACTIVE);
307 if (sc->sc_irq_res == NULL) {
308 device_printf(self, "Could not allocate irq\n");
311 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
312 if (!sc->sc_bus.bdev) {
313 device_printf(self, "Could not add USB device\n");
316 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
319 * uhci_pci_match must never return NULL if uhci_pci_probe
322 device_set_desc(sc->sc_bus.bdev, uhci_pci_match(self));
323 switch (pci_get_vendor(self)) {
324 case PCI_UHCI_VENDORID_INTEL:
325 sprintf(sc->sc_vendor, "Intel");
327 case PCI_UHCI_VENDORID_VIA:
328 sprintf(sc->sc_vendor, "VIA");
332 device_printf(self, "(New UHCI DeviceId=0x%08x)\n",
333 pci_get_devid(self));
335 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
338 switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
339 case PCI_USB_REV_PRE_1_0:
340 sc->sc_bus.usbrev = USB_REV_PRE_1_0;
342 case PCI_USB_REV_1_0:
343 sc->sc_bus.usbrev = USB_REV_1_0;
346 /* Quirk for Parallels Desktop 4.0 */
347 device_printf(self, "USB revision is unknown. Assuming v1.1.\n");
348 sc->sc_bus.usbrev = USB_REV_1_1;
352 #if (__FreeBSD_version >= 700031)
353 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
354 NULL, (driver_intr_t *)uhci_interrupt, sc, &sc->sc_intr_hdl);
356 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
357 (driver_intr_t *)uhci_interrupt, sc, &sc->sc_intr_hdl);
361 device_printf(self, "Could not setup irq, %d\n", err);
362 sc->sc_intr_hdl = NULL;
366 * Set the PIRQD enable bit and switch off all the others. We don't
367 * want legacy support to interfere with us XXX Does this also mean
368 * that the BIOS won't touch the keyboard anymore if it is connected
369 * to the ports of the root hub?
372 if (pci_read_config(self, PCI_LEGSUP, 2) != PCI_LEGSUP_USBPIRQDEN) {
373 device_printf(self, "LegSup = 0x%04x\n",
374 pci_read_config(self, PCI_LEGSUP, 2));
377 pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
381 err = device_probe_and_attach(sc->sc_bus.bdev);
384 device_printf(self, "USB init failed\n");
390 uhci_pci_detach(self);
395 uhci_pci_detach(device_t self)
397 uhci_softc_t *sc = device_get_softc(self);
400 if (sc->sc_bus.bdev) {
401 bdev = sc->sc_bus.bdev;
403 device_delete_child(self, bdev);
405 /* during module unload there are lots of children leftover */
406 device_delete_all_children(self);
409 * disable interrupts that might have been switched on in
413 USB_BUS_LOCK(&sc->sc_bus);
415 /* stop the controller */
418 USB_BUS_UNLOCK(&sc->sc_bus);
420 pci_disable_busmaster(self);
422 if (sc->sc_irq_res && sc->sc_intr_hdl) {
423 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
426 /* XXX or should we panic? */
427 device_printf(self, "Could not tear down irq, %d\n",
430 sc->sc_intr_hdl = NULL;
432 if (sc->sc_irq_res) {
433 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
434 sc->sc_irq_res = NULL;
437 bus_release_resource(self, SYS_RES_IOPORT, PCI_UHCI_BASE_REG,
439 sc->sc_io_res = NULL;
441 usb_bus_mem_free_all(&sc->sc_bus, &uhci_iterate_hw_softc);
446 static driver_t uhci_driver =
449 .methods = (device_method_t[]){
450 /* device interface */
451 DEVMETHOD(device_probe, uhci_pci_probe),
452 DEVMETHOD(device_attach, uhci_pci_attach),
453 DEVMETHOD(device_detach, uhci_pci_detach),
455 DEVMETHOD(device_suspend, uhci_pci_suspend),
456 DEVMETHOD(device_resume, uhci_pci_resume),
457 DEVMETHOD(device_shutdown, bus_generic_shutdown),
460 DEVMETHOD(bus_print_child, bus_generic_print_child),
463 .size = sizeof(struct uhci_softc),
466 static devclass_t uhci_devclass;
468 DRIVER_MODULE(uhci, pci, uhci_driver, uhci_devclass, 0, 0);
469 MODULE_DEPEND(uhci, usb, 1, 1, 1);