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.
36 * $FreeBSD: src/sys/dev/usb/ehci_pci.c,v 1.9 2003/12/17 17:15:41 peter Exp $
37 * $DragonFly: src/sys/bus/usb/ehci_pci.c,v 1.2 2004/01/01 00:29:25 dillon Exp $
41 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
43 * The EHCI 1.0 spec can be found at
44 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
45 * and the USB 2.0 spec at
46 * http://www.usb.org/developers/docs/usb_20.zip
49 /* The low level controller code for EHCI has been split into
50 * PCI probes and EHCI specific code. This was done to facilitate the
51 * sharing of code between *BSD's
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/module.h>
61 #include <sys/queue.h>
63 #include <machine/bus.h>
65 #include <machine/resource.h>
67 #include <bus/pci/pcivar.h>
68 #include <bus/pci/pcireg.h>
70 #include <bus/usb/usb.h>
71 #include <bus/usb/usbdi.h>
72 #include <bus/usb/usbdivar.h>
73 #include <bus/usb/usb_mem.h>
75 #include <bus/usb/ehcireg.h>
76 #include <bus/usb/ehcivar.h>
78 #define PCI_EHCI_VENDORID_ACERLABS 0x10b9
79 #define PCI_EHCI_VENDORID_AMD 0x1022
80 #define PCI_EHCI_VENDORID_APPLE 0x106b
81 #define PCI_EHCI_VENDORID_CMDTECH 0x1095
82 #define PCI_EHCI_VENDORID_NEC 0x1033
83 #define PCI_EHCI_VENDORID_OPTI 0x1045
84 #define PCI_EHCI_VENDORID_SIS 0x1039
85 #define PCI_EHCI_VENDORID_NVIDIA 0x12D2
86 #define PCI_EHCI_VENDORID_NVIDIA2 0x10DE
88 #define PCI_EHCI_DEVICEID_NEC 0x00e01033
89 static const char *ehci_device_nec = "NEC uPD 720100 USB 2.0 controller";
91 static const char *ehci_device_generic = "EHCI (generic) USB 2.0 controller";
93 #define PCI_EHCI_BASE_REG 0x10
96 static int ehci_pci_attach(device_t self);
97 static int ehci_pci_detach(device_t self);
100 ehci_pci_match(device_t self)
102 u_int32_t device_id = pci_get_devid(self);
105 case PCI_EHCI_DEVICEID_NEC:
106 return (ehci_device_nec);
108 if (pci_get_class(self) == PCIC_SERIALBUS
109 && pci_get_subclass(self) == PCIS_SERIALBUS_USB
110 && pci_get_progif(self) == PCI_INTERFACE_EHCI) {
111 return (ehci_device_generic);
115 return NULL; /* dunno */
119 ehci_pci_probe(device_t self)
121 const char *desc = ehci_pci_match(self);
124 device_set_desc(self, desc);
132 ehci_pci_attach(device_t self)
134 ehci_softc_t *sc = device_get_softc(self);
138 struct usbd_bus *bsc;
147 switch(pci_read_config(self, PCI_USBREV, 1) & PCI_USBREV_MASK) {
148 case PCI_USBREV_PRE_1_0:
151 sc->sc_bus.usbrev = USBREV_UNKNOWN;
152 printf("pre-2.0 USB rev\n");
155 sc->sc_bus.usbrev = USBREV_2_0;
158 sc->sc_bus.usbrev = USBREV_UNKNOWN;
162 pci_enable_busmaster(self);
165 sc->io_res = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
166 0, ~0, 1, RF_ACTIVE);
168 device_printf(self, "Could not map memory\n");
171 sc->iot = rman_get_bustag(sc->io_res);
172 sc->ioh = rman_get_bushandle(sc->io_res);
175 sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1,
176 RF_SHAREABLE | RF_ACTIVE);
177 if (sc->irq_res == NULL) {
178 device_printf(self, "Could not allocate irq\n");
179 ehci_pci_detach(self);
182 sc->sc_bus.bdev = device_add_child(self, "usb", -1);
183 if (!sc->sc_bus.bdev) {
184 device_printf(self, "Could not add USB device\n");
185 ehci_pci_detach(self);
188 device_set_ivars(sc->sc_bus.bdev, sc);
190 /* ehci_pci_match will never return NULL if ehci_pci_probe succeeded */
191 device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
192 switch (pci_get_vendor(self)) {
193 case PCI_EHCI_VENDORID_ACERLABS:
194 sprintf(sc->sc_vendor, "AcerLabs");
196 case PCI_EHCI_VENDORID_AMD:
197 sprintf(sc->sc_vendor, "AMD");
199 case PCI_EHCI_VENDORID_APPLE:
200 sprintf(sc->sc_vendor, "Apple");
202 case PCI_EHCI_VENDORID_CMDTECH:
203 sprintf(sc->sc_vendor, "CMDTECH");
205 case PCI_EHCI_VENDORID_NEC:
206 sprintf(sc->sc_vendor, "NEC");
208 case PCI_EHCI_VENDORID_OPTI:
209 sprintf(sc->sc_vendor, "OPTi");
211 case PCI_EHCI_VENDORID_SIS:
212 sprintf(sc->sc_vendor, "SiS");
214 case PCI_EHCI_VENDORID_NVIDIA:
215 case PCI_EHCI_VENDORID_NVIDIA2:
216 sprintf(sc->sc_vendor, "nVidia");
220 device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
221 pci_get_devid(self));
222 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
225 err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_BIO,
226 (driver_intr_t *) ehci_intr, sc, &sc->ih);
228 device_printf(self, "Could not setup irq, %d\n", err);
230 ehci_pci_detach(self);
235 * Find companion controllers. According to the spec they always
236 * have lower function numbers so they should be enumerated already.
238 parent = device_get_parent(self);
239 res = device_get_children(parent, &neighbors, &count);
241 device_printf(self, "Error finding companion busses\n");
242 ehci_pci_detach(self);
246 slot = pci_get_slot(self);
247 function = pci_get_function(self);
248 for (i = 0; i < count; i++) {
249 if (pci_get_slot(neighbors[i]) == slot && \
250 pci_get_function(neighbors[i]) < function) {
251 res = device_get_children(neighbors[i],
253 if (res != 0 || buscount != 1)
255 bsc = device_get_softc(nbus[0]);
256 printf("ehci_pci_attach: companion %s\n",
257 USBDEVNAME(bsc->bdev));
258 sc->sc_comps[ncomp++] = bsc;
259 if (ncomp >= EHCI_COMPANION_MAX)
263 sc->sc_ncomp = ncomp;
267 err = device_probe_and_attach(sc->sc_bus.bdev);
270 device_printf(self, "USB init failed err=%d\n", err);
272 ehci_pci_detach(self);
280 ehci_pci_detach(device_t self)
282 ehci_softc_t *sc = device_get_softc(self);
285 * XXX this code is not yet fit to be used as detach for the EHCI
290 * disable interrupts that might have been switched on in ehci_init
292 if (sc->iot && sc->ioh)
293 bus_space_write_4(sc->iot, sc->ioh, EHCI_USBINTR, 0);
295 if (sc->irq_res && sc->ih) {
296 int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
299 /* XXX or should we panic? */
300 device_printf(self, "Could not tear down irq, %d\n",
304 if (sc->sc_bus.bdev) {
305 device_delete_child(self, sc->sc_bus.bdev);
306 sc->sc_bus.bdev = NULL;
309 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
313 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM, sc->io_res);
321 static device_method_t ehci_methods[] = {
322 /* Device interface */
323 DEVMETHOD(device_probe, ehci_pci_probe),
324 DEVMETHOD(device_attach, ehci_pci_attach),
325 DEVMETHOD(device_shutdown, bus_generic_shutdown),
328 DEVMETHOD(bus_print_child, bus_generic_print_child),
333 static driver_t ehci_driver = {
336 sizeof(ehci_softc_t),
339 static devclass_t ehci_devclass;
341 DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
342 DRIVER_MODULE(ehci, cardbus, ehci_driver, ehci_devclass, 0, 0);