Merge from vendor branch GCC:
[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.2 2004/01/01 00:29:25 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_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
87
88 #define PCI_EHCI_DEVICEID_NEC           0x00e01033
89 static const char *ehci_device_nec = "NEC uPD 720100 USB 2.0 controller";
90
91 static const char *ehci_device_generic = "EHCI (generic) USB 2.0 controller";
92
93 #define PCI_EHCI_BASE_REG       0x10
94
95
96 static int ehci_pci_attach(device_t self);
97 static int ehci_pci_detach(device_t self);
98
99 static const char *
100 ehci_pci_match(device_t self)
101 {
102         u_int32_t device_id = pci_get_devid(self);
103
104         switch (device_id) {
105         case PCI_EHCI_DEVICEID_NEC:
106                 return (ehci_device_nec);
107         default:
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);
112                 }
113         }
114
115         return NULL;            /* dunno */
116 }
117
118 static int
119 ehci_pci_probe(device_t self)
120 {
121         const char *desc = ehci_pci_match(self);
122
123         if (desc) {
124                 device_set_desc(self, desc);
125                 return 0;
126         } else {
127                 return ENXIO;
128         }
129 }
130
131 static int
132 ehci_pci_attach(device_t self)
133 {
134         ehci_softc_t *sc = device_get_softc(self);
135         device_t parent;
136         device_t *neighbors;
137         device_t *nbus;
138         struct usbd_bus *bsc;
139         int err;
140         int rid;
141         int ncomp;
142         int count, buscount;
143         int slot, function;
144         int res;
145         int i;
146
147         switch(pci_read_config(self, PCI_USBREV, 1) & PCI_USBREV_MASK) {
148         case PCI_USBREV_PRE_1_0:
149         case PCI_USBREV_1_0:
150         case PCI_USBREV_1_1:
151                 sc->sc_bus.usbrev = USBREV_UNKNOWN;
152                 printf("pre-2.0 USB rev\n");
153                 return ENXIO;
154         case PCI_USBREV_2_0:
155                 sc->sc_bus.usbrev = USBREV_2_0;
156                 break;
157         default:
158                 sc->sc_bus.usbrev = USBREV_UNKNOWN;
159                 break;
160         }
161
162         pci_enable_busmaster(self);
163
164         rid = PCI_CBMEM;
165         sc->io_res = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
166             0, ~0, 1, RF_ACTIVE);
167         if (!sc->io_res) {
168                 device_printf(self, "Could not map memory\n");
169                 return ENXIO;
170         }
171         sc->iot = rman_get_bustag(sc->io_res);
172         sc->ioh = rman_get_bushandle(sc->io_res);
173
174         rid = 0;
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);
180                 return ENXIO;
181         }
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);
186                 return ENOMEM;
187         }
188         device_set_ivars(sc->sc_bus.bdev, sc);
189
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");
195                 break;
196         case PCI_EHCI_VENDORID_AMD:
197                 sprintf(sc->sc_vendor, "AMD");
198                 break;
199         case PCI_EHCI_VENDORID_APPLE:
200                 sprintf(sc->sc_vendor, "Apple");
201                 break;
202         case PCI_EHCI_VENDORID_CMDTECH:
203                 sprintf(sc->sc_vendor, "CMDTECH");
204                 break;
205         case PCI_EHCI_VENDORID_NEC:
206                 sprintf(sc->sc_vendor, "NEC");
207                 break;
208         case PCI_EHCI_VENDORID_OPTI:
209                 sprintf(sc->sc_vendor, "OPTi");
210                 break;
211         case PCI_EHCI_VENDORID_SIS:
212                 sprintf(sc->sc_vendor, "SiS");
213                 break;
214         case PCI_EHCI_VENDORID_NVIDIA:
215         case PCI_EHCI_VENDORID_NVIDIA2:
216                 sprintf(sc->sc_vendor, "nVidia");
217                 break;
218         default:
219                 if (bootverbose)
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));
223         }
224
225         err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_BIO,
226             (driver_intr_t *) ehci_intr, sc, &sc->ih);
227         if (err) {
228                 device_printf(self, "Could not setup irq, %d\n", err);
229                 sc->ih = NULL;
230                 ehci_pci_detach(self);
231                 return ENXIO;
232         }
233
234         /*
235          * Find companion controllers.  According to the spec they always
236          * have lower function numbers so they should be enumerated already.
237          */
238         parent = device_get_parent(self);
239         res = device_get_children(parent, &neighbors, &count);
240         if (res != 0) {
241                 device_printf(self, "Error finding companion busses\n");
242                 ehci_pci_detach(self);
243                 return ENXIO;
244         }
245         ncomp = 0;
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],
252                                 &nbus, &buscount);
253                         if (res != 0 || buscount != 1)
254                                 continue;
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)
260                                 break;
261                 }
262         }
263         sc->sc_ncomp = ncomp;
264
265         err = ehci_init(sc);
266         if (!err)
267                 err = device_probe_and_attach(sc->sc_bus.bdev);
268
269         if (err) {
270                 device_printf(self, "USB init failed err=%d\n", err);
271 #if 0 /* TODO */
272                 ehci_pci_detach(self);
273 #endif
274                 return EIO;
275         }
276         return 0;
277 }
278
279 static int
280 ehci_pci_detach(device_t self)
281 {
282         ehci_softc_t *sc = device_get_softc(self);
283
284         /*
285          * XXX this code is not yet fit to be used as detach for the EHCI
286          * controller
287          */
288
289         /*
290          * disable interrupts that might have been switched on in ehci_init
291          */
292         if (sc->iot && sc->ioh)
293                 bus_space_write_4(sc->iot, sc->ioh, EHCI_USBINTR, 0);
294
295         if (sc->irq_res && sc->ih) {
296                 int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
297
298                 if (err)
299                         /* XXX or should we panic? */
300                         device_printf(self, "Could not tear down irq, %d\n",
301                             err);
302                 sc->ih = NULL;
303         }
304         if (sc->sc_bus.bdev) {
305                 device_delete_child(self, sc->sc_bus.bdev);
306                 sc->sc_bus.bdev = NULL;
307         }
308         if (sc->irq_res) {
309                 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
310                 sc->irq_res = NULL;
311         }
312         if (sc->io_res) {
313                 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM, sc->io_res);
314                 sc->io_res = NULL;
315                 sc->iot = 0;
316                 sc->ioh = 0;
317         }
318         return 0;
319 }
320
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),
326
327         /* Bus interface */
328         DEVMETHOD(bus_print_child, bus_generic_print_child),
329
330         {0, 0}
331 };
332
333 static driver_t ehci_driver = {
334         "ehci",
335         ehci_methods,
336         sizeof(ehci_softc_t),
337 };
338
339 static devclass_t ehci_devclass;
340
341 DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
342 DRIVER_MODULE(ehci, cardbus, ehci_driver, ehci_devclass, 0, 0);