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.
37 * $FreeBSD: src/sys/dev/usb/uhci_pci.c,v 1.57 2005/03/01 07:50:11 imp Exp $
38 * $DragonFly: src/sys/bus/usb/uhci_pci.c,v 1.9 2006/12/10 02:03:56 sephe Exp $
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
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/module.h>
58 #include <sys/queue.h>
59 #if defined(__FreeBSD__) || defined(__DragonFly__)
64 #include <bus/pci/pcivar.h>
65 #include <bus/pci/pcireg.h>
67 #include <bus/usb/usb.h>
68 #include <bus/usb/usbdi.h>
69 #include <bus/usb/usbdivar.h>
70 #include <bus/usb/usb_mem.h>
72 #include <bus/usb/uhcireg.h>
73 #include <bus/usb/uhcivar.h>
75 #define PCI_UHCI_VENDORID_INTEL 0x8086
76 #define PCI_UHCI_VENDORID_VIA 0x1106
78 #define PCI_UHCI_DEVICEID_PIIX3 0x70208086
79 static const char *uhci_device_piix3 = "Intel 82371SB (PIIX3) USB controller";
81 #define PCI_UHCI_DEVICEID_PIIX4 0x71128086
82 #define PCI_UHCI_DEVICEID_PIIX4E 0x71128086 /* no separate stepping */
83 static const char *uhci_device_piix4 = "Intel 82371AB/EB (PIIX4) USB controller";
85 #define PCI_UHCI_DEVICEID_ICH 0x24128086
86 static const char *uhci_device_ich = "Intel 82801AA (ICH) USB controller";
88 #define PCI_UHCI_DEVICEID_ICH0 0x24228086
89 static const char *uhci_device_ich0 = "Intel 82801AB (ICH0) USB controller";
91 #define PCI_UHCI_DEVICEID_ICH2_A 0x24428086
92 static const char *uhci_device_ich2_a = "Intel 82801BA/BAM (ICH2) USB controller USB-A";
94 #define PCI_UHCI_DEVICEID_ICH2_B 0x24448086
95 static const char *uhci_device_ich2_b = "Intel 82801BA/BAM (ICH2) USB controller USB-B";
97 #define PCI_UHCI_DEVICEID_ICH3_A 0x24828086
98 static const char *uhci_device_ich3_a = "Intel 82801CA/CAM (ICH3) USB controller USB-A";
100 #define PCI_UHCI_DEVICEID_ICH3_B 0x24848086
101 static const char *uhci_device_ich3_b = "Intel 82801CA/CAM (ICH3) USB controller USB-B";
103 #define PCI_UHCI_DEVICEID_ICH3_C 0x24878086
104 static const char *uhci_device_ich3_c = "Intel 82801CA/CAM (ICH3) USB controller USB-C";
106 #define PCI_UHCI_DEVICEID_ICH4_A 0x24c28086
107 static const char *uhci_device_ich4_a = "Intel 82801DB (ICH4) USB controller USB-A";
109 #define PCI_UHCI_DEVICEID_ICH4_B 0x24c48086
110 static const char *uhci_device_ich4_b = "Intel 82801DB (ICH4) USB controller USB-B";
112 #define PCI_UHCI_DEVICEID_ICH4_C 0x24c78086
113 static const char *uhci_device_ich4_c = "Intel 82801DB (ICH4) USB controller USB-C";
115 #define PCI_UHCI_DEVICEID_ICH5_A 0x24d28086
116 static const char *uhci_device_ich5_a = "Intel 82801EB (ICH5) USB controller USB-A";
118 #define PCI_UHCI_DEVICEID_ICH5_B 0x24d48086
119 static const char *uhci_device_ich5_b = "Intel 82801EB (ICH5) USB controller USB-B";
121 #define PCI_UHCI_DEVICEID_ICH5_C 0x24d78086
122 static const char *uhci_device_ich5_c = "Intel 82801EB (ICH5) USB controller USB-C";
124 #define PCI_UHCI_DEVICEID_ICH5_D 0x24de8086
125 static const char *uhci_device_ich5_d = "Intel 82801EB (ICH5) USB controller USB-D";
127 #define PCI_UHCI_DEVICEID_ICH6_A 0x26588086
128 static const char *uhci_device_ich6_a = "Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-A";
130 #define PCI_UHCI_DEVICEID_ICH6_B 0x26598086
131 static const char *uhci_device_ich6_b = "Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-B";
133 #define PCI_UHCI_DEVICEID_ICH6_C 0x265a8086
134 static const char *uhci_device_ich6_c = "Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-C";
136 #define PCI_UHCI_DEVICEID_ICH6_D 0x265b8086
137 static const char *uhci_device_ich6_d = "Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-D";
139 #define PCI_UHCI_DEVICEID_440MX 0x719a8086
140 static const char *uhci_device_440mx = "Intel 82443MX USB controller";
142 #define PCI_UHCI_DEVICEID_460GX 0x76028086
143 static const char *uhci_device_460gx = "Intel 82372FB/82468GX USB controller";
145 #define PCI_UHCI_DEVICEID_VT83C572 0x30381106
146 static const char *uhci_device_vt83c572 = "VIA 83C572 USB controller";
148 static const char *uhci_device_generic = "UHCI (generic) USB controller";
150 #define PCI_UHCI_BASE_REG 0x20
153 static int uhci_pci_attach(device_t self);
154 static int uhci_pci_detach(device_t self);
155 static int uhci_pci_suspend(device_t self);
156 static int uhci_pci_resume(device_t self);
160 uhci_pci_suspend(device_t self)
162 uhci_softc_t *sc = device_get_softc(self);
165 err = bus_generic_suspend(self);
168 uhci_power(PWR_SUSPEND, sc);
174 uhci_pci_resume(device_t self)
176 uhci_softc_t *sc = device_get_softc(self);
178 pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
180 uhci_power(PWR_RESUME, sc);
181 bus_generic_resume(self);
187 uhci_pci_match(device_t self)
189 u_int32_t device_id = pci_get_devid(self);
191 if (device_id == PCI_UHCI_DEVICEID_PIIX3) {
192 return (uhci_device_piix3);
193 } else if (device_id == PCI_UHCI_DEVICEID_PIIX4) {
194 return (uhci_device_piix4);
195 } else if (device_id == PCI_UHCI_DEVICEID_ICH) {
196 return (uhci_device_ich);
197 } else if (device_id == PCI_UHCI_DEVICEID_ICH0) {
198 return (uhci_device_ich0);
199 } else if (device_id == PCI_UHCI_DEVICEID_ICH2_A) {
200 return (uhci_device_ich2_a);
201 } else if (device_id == PCI_UHCI_DEVICEID_ICH2_B) {
202 return (uhci_device_ich2_b);
203 } else if (device_id == PCI_UHCI_DEVICEID_ICH3_A) {
204 return (uhci_device_ich3_a);
205 } else if (device_id == PCI_UHCI_DEVICEID_ICH3_B) {
206 return (uhci_device_ich3_b);
207 } else if (device_id == PCI_UHCI_DEVICEID_ICH3_C) {
208 return (uhci_device_ich3_c);
209 } else if (device_id == PCI_UHCI_DEVICEID_ICH4_A) {
210 return (uhci_device_ich4_a);
211 } else if (device_id == PCI_UHCI_DEVICEID_ICH4_B) {
212 return (uhci_device_ich4_b);
213 } else if (device_id == PCI_UHCI_DEVICEID_ICH4_C) {
214 return (uhci_device_ich4_c);
215 } else if (device_id == PCI_UHCI_DEVICEID_ICH5_A) {
216 return (uhci_device_ich5_a);
217 } else if (device_id == PCI_UHCI_DEVICEID_ICH5_B) {
218 return (uhci_device_ich5_b);
219 } else if (device_id == PCI_UHCI_DEVICEID_ICH5_C) {
220 return (uhci_device_ich5_c);
221 } else if (device_id == PCI_UHCI_DEVICEID_ICH5_D) {
222 return (uhci_device_ich5_d);
223 } else if (device_id == PCI_UHCI_DEVICEID_ICH6_A) {
224 return (uhci_device_ich6_a);
225 } else if (device_id == PCI_UHCI_DEVICEID_ICH6_B) {
226 return (uhci_device_ich6_b);
227 } else if (device_id == PCI_UHCI_DEVICEID_ICH6_C) {
228 return (uhci_device_ich6_c);
229 } else if (device_id == PCI_UHCI_DEVICEID_ICH6_D) {
230 return (uhci_device_ich6_d);
231 } else if (device_id == PCI_UHCI_DEVICEID_440MX) {
232 return (uhci_device_440mx);
233 } else if (device_id == PCI_UHCI_DEVICEID_460GX) {
234 return (uhci_device_460gx);
235 } else if (device_id == PCI_UHCI_DEVICEID_VT83C572) {
236 return (uhci_device_vt83c572);
238 if (pci_get_class(self) == PCIC_SERIALBUS
239 && pci_get_subclass(self) == PCIS_SERIALBUS_USB
240 && pci_get_progif(self) == PCI_INTERFACE_UHCI) {
241 return (uhci_device_generic);
245 return NULL; /* dunno... */
249 uhci_pci_probe(device_t self)
251 const char *desc = uhci_pci_match(self);
254 device_set_desc(self, desc);
262 uhci_pci_attach(device_t self)
264 uhci_softc_t *sc = device_get_softc(self);
268 pci_enable_busmaster(self);
270 rid = PCI_UHCI_BASE_REG;
271 sc->io_res = bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid,
274 device_printf(self, "Could not map ports\n");
277 sc->iot = rman_get_bustag(sc->io_res);
278 sc->ioh = rman_get_bushandle(sc->io_res);
280 /* disable interrupts */
281 bus_space_write_2(sc->iot, sc->ioh, UHCI_INTR, 0);
284 sc->irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
285 RF_SHAREABLE | RF_ACTIVE);
286 if (sc->irq_res == NULL) {
287 device_printf(self, "Could not allocate irq\n");
288 uhci_pci_detach(self);
291 sc->sc_bus.bdev = device_add_child(self, "usb", -1);
292 if (!sc->sc_bus.bdev) {
293 device_printf(self, "Could not add USB device\n");
294 uhci_pci_detach(self);
297 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
299 /* uhci_pci_match must never return NULL if uhci_pci_probe succeeded */
300 device_set_desc(sc->sc_bus.bdev, uhci_pci_match(self));
301 switch (pci_get_vendor(self)) {
302 case PCI_UHCI_VENDORID_INTEL:
303 sprintf(sc->sc_vendor, "Intel");
305 case PCI_UHCI_VENDORID_VIA:
306 sprintf(sc->sc_vendor, "VIA");
310 device_printf(self, "(New UHCI DeviceId=0x%08x)\n",
311 pci_get_devid(self));
312 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
315 switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USBREV_MASK) {
316 case PCI_USBREV_PRE_1_0:
317 sc->sc_bus.usbrev = USBREV_PRE_1_0;
320 sc->sc_bus.usbrev = USBREV_1_0;
323 sc->sc_bus.usbrev = USBREV_UNKNOWN;
327 err = bus_setup_intr(self, sc->irq_res, 0,
328 (driver_intr_t *) uhci_intr, sc, &sc->ih, NULL);
330 device_printf(self, "Could not setup irq, %d\n", err);
332 uhci_pci_detach(self);
336 * Set the PIRQD enable bit and switch off all the others. We don't
337 * want legacy support to interfere with us XXX Does this also mean
338 * that the BIOS won't touch the keyboard anymore if it is connected
339 * to the ports of the root hub?
342 if (pci_read_config(self, PCI_LEGSUP, 2) != PCI_LEGSUP_USBPIRQDEN)
343 device_printf(self, "LegSup = 0x%04x\n",
344 pci_read_config(self, PCI_LEGSUP, 2));
346 pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
350 sc->sc_flags |= UHCI_SCFLG_DONEINIT;
351 err = device_probe_and_attach(sc->sc_bus.bdev);
355 device_printf(self, "USB init failed\n");
356 uhci_pci_detach(self);
359 return 0; /* success */
363 uhci_pci_detach(device_t self)
365 uhci_softc_t *sc = device_get_softc(self);
367 if (sc->sc_flags & UHCI_SCFLG_DONEINIT) {
369 sc->sc_flags &= ~UHCI_SCFLG_DONEINIT;
373 if (sc->irq_res && sc->ih) {
374 int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
377 /* XXX or should we panic? */
378 device_printf(self, "Could not tear down irq, %d\n",
382 if (sc->sc_bus.bdev) {
383 device_delete_child(self, sc->sc_bus.bdev);
384 sc->sc_bus.bdev = NULL;
387 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
391 bus_release_resource(self, SYS_RES_IOPORT, PCI_UHCI_BASE_REG,
401 static device_method_t uhci_methods[] = {
402 /* Device interface */
403 DEVMETHOD(device_probe, uhci_pci_probe),
404 DEVMETHOD(device_attach, uhci_pci_attach),
405 DEVMETHOD(device_detach, uhci_pci_detach),
406 DEVMETHOD(device_suspend, uhci_pci_suspend),
407 DEVMETHOD(device_resume, uhci_pci_resume),
408 DEVMETHOD(device_shutdown, bus_generic_shutdown),
411 DEVMETHOD(bus_print_child, bus_generic_print_child),
416 static driver_t uhci_driver = {
419 sizeof(uhci_softc_t),
422 static devclass_t uhci_devclass;
424 DRIVER_MODULE(uhci, pci, uhci_driver, uhci_devclass, 0, 0);
425 DRIVER_MODULE(uhci, cardbus, uhci_driver, uhci_devclass, 0, 0);