f41d5b5eba8ab48fc3b2f08d355e06fc5c60cf28
[dragonfly.git] / sys / bus / usb / ehci_pci.c
1 /*-
2  * Copyright (c) 1998 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Lennart Augustsson (augustss@carlstedt.se) at
7  * Carlstedt Research & Technology.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
24  *
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.6 2005/05/24 20:58:54 dillon Exp $
38  */
39
40 /*
41  * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
42  *
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
47  */
48
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
52  */
53
54 #include "opt_bus.h"
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/module.h>
60 #include <sys/bus.h>
61 #include <sys/queue.h>
62 #include <sys/lock.h>
63 #include <machine/bus.h>
64 #include <sys/rman.h>
65 #include <machine/resource.h>
66
67 #include <bus/pci/pcivar.h>
68 #include <bus/pci/pcireg.h>
69
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>
74
75 #include <bus/usb/ehcireg.h>
76 #include <bus/usb/ehcivar.h>
77
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_INTEL         0x8086
83 #define PCI_EHCI_VENDORID_NEC           0x1033
84 #define PCI_EHCI_VENDORID_OPTI          0x1045
85 #define PCI_EHCI_VENDORID_SIS           0x1039
86 #define PCI_EHCI_VENDORID_NVIDIA        0x12D2
87 #define PCI_EHCI_VENDORID_NVIDIA2       0x10DE
88
89 #define PCI_EHCI_DEVICEID_ICH6          0x265c8086
90 static const char *ehci_device_ich6 = "Intel 82801FB USB 2.0 controller";
91
92 #define PCI_EHCI_DEVICEID_NEC           0x00e01033
93 static const char *ehci_device_nec = "NEC uPD 720100 USB 2.0 controller";
94
95 static const char *ehci_device_generic = "EHCI (generic) USB 2.0 controller";
96
97 #define PCI_EHCI_BASE_REG       0x10
98
99
100 static int ehci_pci_attach(device_t self);
101 static int ehci_pci_detach(device_t self);
102
103 static const char *
104 ehci_pci_match(device_t self)
105 {
106         u_int32_t device_id = pci_get_devid(self);
107
108         switch (device_id) {
109         case PCI_EHCI_DEVICEID_ICH6:
110                 return (ehci_device_ich6);
111         case PCI_EHCI_DEVICEID_NEC:
112                 return (ehci_device_nec);
113         default:
114                 if (pci_get_class(self) == PCIC_SERIALBUS
115                     && pci_get_subclass(self) == PCIS_SERIALBUS_USB
116                     && pci_get_progif(self) == PCI_INTERFACE_EHCI) {
117                         return (ehci_device_generic);
118                 }
119         }
120
121         return NULL;            /* dunno */
122 }
123
124 static int
125 ehci_pci_probe(device_t self)
126 {
127         const char *desc = ehci_pci_match(self);
128
129         if (desc) {
130                 device_set_desc(self, desc);
131                 return 0;
132         } else {
133                 return ENXIO;
134         }
135 }
136
137 static int
138 ehci_pci_attach(device_t self)
139 {
140         ehci_softc_t *sc = device_get_softc(self);
141         device_t parent;
142         device_t *neighbors;
143         device_t *nbus;
144         struct usbd_bus *bsc;
145         int err;
146         int rid;
147         int ncomp;
148         int count, buscount;
149         int slot, function;
150         int res;
151         int i;
152
153         switch(pci_read_config(self, PCI_USBREV, 1) & PCI_USBREV_MASK) {
154         case PCI_USBREV_PRE_1_0:
155         case PCI_USBREV_1_0:
156         case PCI_USBREV_1_1:
157                 sc->sc_bus.usbrev = USBREV_UNKNOWN;
158                 printf("pre-2.0 USB rev\n");
159                 return ENXIO;
160         case PCI_USBREV_2_0:
161                 sc->sc_bus.usbrev = USBREV_2_0;
162                 break;
163         default:
164                 sc->sc_bus.usbrev = USBREV_UNKNOWN;
165                 break;
166         }
167
168         pci_enable_busmaster(self);
169
170         rid = PCI_CBMEM;
171         sc->io_res = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
172             0, ~0, 1, RF_ACTIVE);
173         if (!sc->io_res) {
174                 device_printf(self, "Could not map memory\n");
175                 return ENXIO;
176         }
177         sc->iot = rman_get_bustag(sc->io_res);
178         sc->ioh = rman_get_bushandle(sc->io_res);
179
180         rid = 0;
181         sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1,
182             RF_SHAREABLE | RF_ACTIVE);
183         if (sc->irq_res == NULL) {
184                 device_printf(self, "Could not allocate irq\n");
185                 ehci_pci_detach(self);
186                 return ENXIO;
187         }
188         sc->sc_bus.bdev = device_add_child(self, "usb", -1);
189         if (!sc->sc_bus.bdev) {
190                 device_printf(self, "Could not add USB device\n");
191                 ehci_pci_detach(self);
192                 return ENOMEM;
193         }
194         device_set_ivars(sc->sc_bus.bdev, sc);
195
196         /* ehci_pci_match will never return NULL if ehci_pci_probe succeeded */
197         device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
198         switch (pci_get_vendor(self)) {
199         case PCI_EHCI_VENDORID_ACERLABS:
200                 sprintf(sc->sc_vendor, "AcerLabs");
201                 break;
202         case PCI_EHCI_VENDORID_AMD:
203                 sprintf(sc->sc_vendor, "AMD");
204                 break;
205         case PCI_EHCI_VENDORID_APPLE:
206                 sprintf(sc->sc_vendor, "Apple");
207                 break;
208         case PCI_EHCI_VENDORID_CMDTECH:
209                 sprintf(sc->sc_vendor, "CMDTECH");
210                 break;
211         case PCI_EHCI_VENDORID_NEC:
212                 sprintf(sc->sc_vendor, "NEC");
213                 break;
214         case PCI_EHCI_VENDORID_OPTI:
215                 sprintf(sc->sc_vendor, "OPTi");
216                 break;
217         case PCI_EHCI_VENDORID_SIS:
218                 sprintf(sc->sc_vendor, "SiS");
219                 break;
220         case PCI_EHCI_VENDORID_NVIDIA:
221         case PCI_EHCI_VENDORID_NVIDIA2:
222                 sprintf(sc->sc_vendor, "nVidia");
223                 break;
224         default:
225                 if (bootverbose)
226                         device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
227                             pci_get_devid(self));
228                 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
229         }
230
231         err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_BIO,
232             (driver_intr_t *) ehci_intr, sc, &sc->ih, NULL);
233         if (err) {
234                 device_printf(self, "Could not setup irq, %d\n", err);
235                 sc->ih = NULL;
236                 ehci_pci_detach(self);
237                 return ENXIO;
238         }
239
240         /*
241          * Find companion controllers.  According to the spec they always
242          * have lower function numbers so they should be enumerated already.
243          */
244         parent = device_get_parent(self);
245         res = device_get_children(parent, &neighbors, &count);
246         if (res != 0) {
247                 device_printf(self, "Error finding companion busses\n");
248                 ehci_pci_detach(self);
249                 return ENXIO;
250         }
251         ncomp = 0;
252         slot = pci_get_slot(self);
253         function = pci_get_function(self);
254         for (i = 0; i < count; i++) {
255                 if (pci_get_slot(neighbors[i]) == slot && \
256                         pci_get_function(neighbors[i]) < function) {
257                         res = device_get_children(neighbors[i],
258                                 &nbus, &buscount);
259                         if (res != 0 || buscount != 1)
260                                 continue;
261                         bsc = device_get_softc(nbus[0]);
262                         printf("ehci_pci_attach: companion %s\n",
263                         USBDEVNAME(bsc->bdev));
264                         sc->sc_comps[ncomp++] = bsc;
265                         if (ncomp >= EHCI_COMPANION_MAX)
266                                 break;
267                 }
268         }
269         sc->sc_ncomp = ncomp;
270
271         err = ehci_init(sc);
272         if (!err)
273                 err = device_probe_and_attach(sc->sc_bus.bdev);
274
275         if (err) {
276                 device_printf(self, "USB init failed err=%d\n", err);
277 #if 0 /* TODO */
278                 ehci_pci_detach(self);
279 #endif
280                 return EIO;
281         }
282         return 0;
283 }
284
285 static int
286 ehci_pci_detach(device_t self)
287 {
288         ehci_softc_t *sc = device_get_softc(self);
289
290         /*
291          * XXX this code is not yet fit to be used as detach for the EHCI
292          * controller
293          */
294
295         /*
296          * disable interrupts that might have been switched on in ehci_init
297          */
298         if (sc->iot && sc->ioh)
299                 bus_space_write_4(sc->iot, sc->ioh, EHCI_USBINTR, 0);
300
301         if (sc->irq_res && sc->ih) {
302                 int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
303
304                 if (err)
305                         /* XXX or should we panic? */
306                         device_printf(self, "Could not tear down irq, %d\n",
307                             err);
308                 sc->ih = NULL;
309         }
310         if (sc->sc_bus.bdev) {
311                 device_delete_child(self, sc->sc_bus.bdev);
312                 sc->sc_bus.bdev = NULL;
313         }
314         if (sc->irq_res) {
315                 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
316                 sc->irq_res = NULL;
317         }
318         if (sc->io_res) {
319                 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM, sc->io_res);
320                 sc->io_res = NULL;
321                 sc->iot = 0;
322                 sc->ioh = 0;
323         }
324         return 0;
325 }
326
327 static device_method_t ehci_methods[] = {
328         /* Device interface */
329         DEVMETHOD(device_probe, ehci_pci_probe),
330         DEVMETHOD(device_attach, ehci_pci_attach),
331         DEVMETHOD(device_shutdown, bus_generic_shutdown),
332
333         /* Bus interface */
334         DEVMETHOD(bus_print_child, bus_generic_print_child),
335
336         {0, 0}
337 };
338
339 static driver_t ehci_driver = {
340         "ehci",
341         ehci_methods,
342         sizeof(ehci_softc_t),
343 };
344
345 static devclass_t ehci_devclass;
346
347 DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
348 DRIVER_MODULE(ehci, cardbus, ehci_driver, ehci_devclass, 0, 0);