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.
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 /* Universal Host Controller Interface
36 * UHCI spec: http://www.intel.com/
39 /* The low level controller code for UHCI has been split into
40 * PCI probes and UHCI specific code. This was done to facilitate the
41 * sharing of code between *BSD's
44 #include <sys/stdint.h>
45 #include <sys/stddef.h>
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
52 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/condvar.h>
56 #include <sys/sysctl.h>
58 #include <sys/unistd.h>
59 #include <sys/callout.h>
60 #include <sys/malloc.h>
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usb_core.h>
67 #include <dev/usb/usb_busdma.h>
68 #include <dev/usb/usb_process.h>
69 #include <dev/usb/usb_util.h>
70 #include <dev/usb/usb_debug.h>
72 #include <dev/usb/usb_controller.h>
73 #include <dev/usb/usb_bus.h>
74 #include <dev/usb/usb_pci.h>
75 #include <dev/usb/controller/uhci.h>
76 #include <dev/usb/controller/uhcireg.h>
79 #define PCI_UHCI_VENDORID_INTEL 0x8086
80 #define PCI_UHCI_VENDORID_VIA 0x1106
82 /* PIIX4E has no separate stepping */
84 static device_probe_t uhci_pci_probe;
85 static device_attach_t uhci_pci_attach;
86 static device_detach_t uhci_pci_detach;
87 static usb_take_controller_t uhci_pci_take_controller;
90 uhci_pci_take_controller(device_t self)
92 pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
98 uhci_pci_match(device_t self)
100 uint32_t device_id = pci_get_devid(self);
104 return ("Intel 631XESB/632XESB/3100 USB controller USB-1");
107 return ("Intel 631XESB/632XESB/3100 USB controller USB-2");
110 return ("Intel 631XESB/632XESB/3100 USB controller USB-3");
113 return ("Intel 631XESB/632XESB/3100 USB controller USB-4");
116 return ("Intel 82371SB (PIIX3) USB controller");
119 return ("Intel 82371AB/EB (PIIX4) USB controller");
122 return ("Intel 82801AA (ICH) USB controller");
125 return ("Intel 82801AB (ICH0) USB controller");
128 return ("Intel 82801BA/BAM (ICH2) USB controller USB-A");
131 return ("Intel 82801BA/BAM (ICH2) USB controller USB-B");
134 return ("Intel 82801CA/CAM (ICH3) USB controller USB-A");
137 return ("Intel 82801CA/CAM (ICH3) USB controller USB-B");
140 return ("Intel 82801CA/CAM (ICH3) USB controller USB-C");
143 return ("Intel 82801DB (ICH4) USB controller USB-A");
146 return ("Intel 82801DB (ICH4) USB controller USB-B");
149 return ("Intel 82801DB (ICH4) USB controller USB-C");
152 return ("Intel 82801EB (ICH5) USB controller USB-A");
155 return ("Intel 82801EB (ICH5) USB controller USB-B");
158 return ("Intel 82801EB (ICH5) USB controller USB-C");
161 return ("Intel 82801EB (ICH5) USB controller USB-D");
164 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-A");
167 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-B");
170 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-C");
173 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-D");
176 return ("Intel 82801G (ICH7) USB controller USB-A");
178 return ("Intel 82801G (ICH7) USB controller USB-B");
180 return ("Intel 82801G (ICH7) USB controller USB-C");
182 return ("Intel 82801G (ICH7) USB controller USB-D");
185 return ("Intel 82801H (ICH8) USB controller USB-A");
187 return ("Intel 82801H (ICH8) USB controller USB-B");
189 return ("Intel 82801H (ICH8) USB controller USB-C");
191 return ("Intel 82801H (ICH8) USB controller USB-D");
193 return ("Intel 82801H (ICH8) USB controller USB-E");
195 return ("Intel 82801I (ICH9) USB controller");
197 return ("Intel 82801I (ICH9) USB controller");
199 return ("Intel 82801I (ICH9) USB controller");
201 return ("Intel 82801I (ICH9) USB controller");
203 return ("Intel 82801I (ICH9) USB controller");
205 return ("Intel 82801I (ICH9) USB controller");
207 return ("Intel 82801JI (ICH10) USB controller USB-A");
209 return ("Intel 82801JI (ICH10) USB controller USB-B");
211 return ("Intel 82801JI (ICH10) USB controller USB-C");
213 return ("Intel 82801JI (ICH10) USB controller USB-D");
215 return ("Intel 82801JI (ICH10) USB controller USB-E");
217 return ("Intel 82801JI (ICH10) USB controller USB-F");
220 return ("Intel 82443MX USB controller");
223 return ("Intel 82372FB/82468GX USB controller");
226 return ("VIA 83C572 USB controller");
232 if ((pci_get_class(self) == PCIC_SERIALBUS) &&
233 (pci_get_subclass(self) == PCIS_SERIALBUS_USB) &&
234 (pci_get_progif(self) == PCI_INTERFACE_UHCI)) {
235 return ("UHCI (generic) USB controller");
241 uhci_pci_probe(device_t self)
243 const char *desc = uhci_pci_match(self);
246 device_set_desc(self, desc);
254 uhci_pci_attach(device_t self)
256 uhci_softc_t *sc = device_get_softc(self);
260 /* initialise some bus fields */
261 sc->sc_bus.parent = self;
262 sc->sc_bus.devices = sc->sc_devices;
263 sc->sc_bus.devices_max = UHCI_MAX_DEVICES;
265 /* get all DMA memory */
266 if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
267 &uhci_iterate_hw_softc)) {
272 pci_enable_busmaster(self);
274 rid = PCI_UHCI_BASE_REG;
275 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid,
277 if (!sc->sc_io_res) {
278 device_printf(self, "Could not map ports\n");
281 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
282 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
283 sc->sc_io_size = rman_get_size(sc->sc_io_res);
285 /* disable interrupts */
286 bus_space_write_2(sc->sc_io_tag, sc->sc_io_hdl, UHCI_INTR, 0);
289 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
290 RF_SHAREABLE | RF_ACTIVE);
291 if (sc->sc_irq_res == NULL) {
292 device_printf(self, "Could not allocate irq\n");
295 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
296 if (!sc->sc_bus.bdev) {
297 device_printf(self, "Could not add USB device\n");
300 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
303 * uhci_pci_match must never return NULL if uhci_pci_probe
306 device_set_desc(sc->sc_bus.bdev, uhci_pci_match(self));
307 switch (pci_get_vendor(self)) {
308 case PCI_UHCI_VENDORID_INTEL:
309 sprintf(sc->sc_vendor, "Intel");
311 case PCI_UHCI_VENDORID_VIA:
312 sprintf(sc->sc_vendor, "VIA");
316 device_printf(self, "(New UHCI DeviceId=0x%08x)\n",
317 pci_get_devid(self));
319 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
322 switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
323 case PCI_USB_REV_PRE_1_0:
324 sc->sc_bus.usbrev = USB_REV_PRE_1_0;
326 case PCI_USB_REV_1_0:
327 sc->sc_bus.usbrev = USB_REV_1_0;
330 /* Quirk for Parallels Desktop 4.0 */
331 device_printf(self, "USB revision is unknown. Assuming v1.1.\n");
332 sc->sc_bus.usbrev = USB_REV_1_1;
336 #if (__FreeBSD_version >= 700031)
337 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
338 NULL, (driver_intr_t *)uhci_interrupt, sc, &sc->sc_intr_hdl);
340 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
341 (driver_intr_t *)uhci_interrupt, sc, &sc->sc_intr_hdl);
345 device_printf(self, "Could not setup irq, %d\n", err);
346 sc->sc_intr_hdl = NULL;
350 * Set the PIRQD enable bit and switch off all the others. We don't
351 * want legacy support to interfere with us XXX Does this also mean
352 * that the BIOS won't touch the keyboard anymore if it is connected
353 * to the ports of the root hub?
356 if (pci_read_config(self, PCI_LEGSUP, 2) != PCI_LEGSUP_USBPIRQDEN) {
357 device_printf(self, "LegSup = 0x%04x\n",
358 pci_read_config(self, PCI_LEGSUP, 2));
361 pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
365 err = device_probe_and_attach(sc->sc_bus.bdev);
368 device_printf(self, "USB init failed\n");
374 uhci_pci_detach(self);
379 uhci_pci_detach(device_t self)
381 uhci_softc_t *sc = device_get_softc(self);
384 if (sc->sc_bus.bdev) {
385 bdev = sc->sc_bus.bdev;
387 device_delete_child(self, bdev);
389 /* during module unload there are lots of children leftover */
390 device_delete_children(self);
393 * disable interrupts that might have been switched on in
397 USB_BUS_LOCK(&sc->sc_bus);
399 /* stop the controller */
402 USB_BUS_UNLOCK(&sc->sc_bus);
404 pci_disable_busmaster(self);
406 if (sc->sc_irq_res && sc->sc_intr_hdl) {
407 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
410 /* XXX or should we panic? */
411 device_printf(self, "Could not tear down irq, %d\n",
414 sc->sc_intr_hdl = NULL;
416 if (sc->sc_irq_res) {
417 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
418 sc->sc_irq_res = NULL;
421 bus_release_resource(self, SYS_RES_IOPORT, PCI_UHCI_BASE_REG,
423 sc->sc_io_res = NULL;
425 usb_bus_mem_free_all(&sc->sc_bus, &uhci_iterate_hw_softc);
430 static device_method_t uhci_pci_methods[] = {
431 /* Device interface */
432 DEVMETHOD(device_probe, uhci_pci_probe),
433 DEVMETHOD(device_attach, uhci_pci_attach),
434 DEVMETHOD(device_detach, uhci_pci_detach),
435 DEVMETHOD(device_suspend, bus_generic_suspend),
436 DEVMETHOD(device_resume, bus_generic_resume),
437 DEVMETHOD(device_shutdown, bus_generic_shutdown),
438 DEVMETHOD(usb_take_controller, uhci_pci_take_controller),
443 static driver_t uhci_driver = {
445 .methods = uhci_pci_methods,
446 .size = sizeof(struct uhci_softc),
449 static devclass_t uhci_devclass;
451 DRIVER_MODULE(uhci, pci, uhci_driver, uhci_devclass, 0, 0);
452 MODULE_DEPEND(uhci, usb, 1, 1, 1);