Merge from vendor branch OPENSSH:
[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.9 2005/10/12 17:35:49 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_ATI           0x1002
82 #define PCI_EHCI_VENDORID_CMDTECH       0x1095
83 #define PCI_EHCI_VENDORID_INTEL         0x8086
84 #define PCI_EHCI_VENDORID_NEC           0x1033
85 #define PCI_EHCI_VENDORID_OPTI          0x1045
86 #define PCI_EHCI_VENDORID_PHILIPS       0x1131
87 #define PCI_EHCI_VENDORID_SIS           0x1039
88 #define PCI_EHCI_VENDORID_NVIDIA        0x12D2
89 #define PCI_EHCI_VENDORID_NVIDIA2       0x10DE
90
91 /* AcerLabs/ALi */
92 #define PCI_EHCI_DEVICEID_M5239         0x523910b9
93 static const char *ehci_device_m5239 = "ALi M5239 USB 2.0 controller";
94
95 /* AMD */
96 #define PCI_EHCI_DEVICEID_8111          0x10227463
97 static const char *ehci_device_8111 = "AMD 8111 USB 2.0 controller";
98
99 /* ATI */
100 #define PCI_EHCI_DEVICEID_SB200         0x43451002
101 static const char *ehci_device_sb200 = "ATI SB200 USB 2.0 controller";
102 #define PCI_EHCI_DEVICEID_SB400         0x43731002
103 static const char *ehci_device_sb400 = "ATI SB400 USB 2.0 controller";
104
105 /* Intel */
106 #define PCI_EHCI_DEVICEID_6300          0x25ad8086
107 static const char *ehci_device_6300 = "Intel 6300ESB USB 2.0 controller";
108 #define PCI_EHCI_DEVICEID_ICH4          0x24cd8086
109 static const char *ehci_device_ich4 = "Intel 82801DB/L/M USB 2.0 controller";
110 #define PCI_EHCI_DEVICEID_ICH5          0x24dd8086
111 static const char *ehci_device_ich5 = "Intel 82801EB/R USB 2.0 controller";
112 #define PCI_EHCI_DEVICEID_ICH6          0x265c8086
113 static const char *ehci_device_ich6 = "Intel 82801FB USB 2.0 controller";
114 #define PCI_EHCI_DEVICEID_ICH7          0x27cc8086
115 static const char *ehci_device_ich7 = "Intel 82801GB/R USB 2.0 controller";
116
117 /* NEC */
118 #define PCI_EHCI_DEVICEID_NEC           0x00e01033
119 static const char *ehci_device_nec = "NEC uPD 720100 USB 2.0 controller";
120
121 /* NVIDIA */
122 #define PCI_EHCI_DEVICEID_NF2           0x006810de
123 static const char *ehci_device_nf2 = "NVIDIA nForce2 USB 2.0 controller";
124 #define PCI_EHCI_DEVICEID_NF2_400       0x008810de
125 static const char *ehci_device_nf2_400 = "NVIDIA nForce2 Ultra 400 USB 2.0 controller";
126 #define PCI_EHCI_DEVICEID_NF3           0x00d810de
127 static const char *ehci_device_nf3 = "NVIDIA nForce3 USB 2.0 controller";
128 #define PCI_EHCI_DEVICEID_NF3_250       0x00e810de
129 static const char *ehci_device_nf3_250 = "NVIDIA nForce3 250 USB 2.0 controller";
130 #define PCI_EHCI_DEVICEID_NF4           0x005b10de
131 static const char *ehci_device_nf4 = "NVIDIA nForce4 USB 2.0 controller";
132
133 /* Philips */
134 #define PCI_EHCI_DEVICEID_ISP156X       0x15621131
135 static const char *ehci_device_isp156x = "Philips ISP156x USB 2.0 controller";
136
137 static const char *ehci_device_generic = "EHCI (generic) USB 2.0 controller";
138
139 #define PCI_EHCI_BASE_REG       0x10
140
141
142 static int ehci_pci_attach(device_t self);
143 static int ehci_pci_detach(device_t self);
144
145 static const char *
146 ehci_pci_match(device_t self)
147 {
148         u_int32_t device_id = pci_get_devid(self);
149
150         switch (device_id) {
151         case PCI_EHCI_DEVICEID_M5239:
152                 return (ehci_device_m5239);
153         case PCI_EHCI_DEVICEID_8111:
154                 return (ehci_device_8111);
155         case PCI_EHCI_DEVICEID_SB200:
156                 return (ehci_device_sb200);
157         case PCI_EHCI_DEVICEID_SB400:
158                 return (ehci_device_sb400);
159         case PCI_EHCI_DEVICEID_6300:
160                 return (ehci_device_6300);
161         case PCI_EHCI_DEVICEID_ICH4:
162                 return (ehci_device_ich4);
163         case PCI_EHCI_DEVICEID_ICH5:
164                 return (ehci_device_ich5);
165         case PCI_EHCI_DEVICEID_ICH6:
166                 return (ehci_device_ich6);
167         case PCI_EHCI_DEVICEID_ICH7:
168                 return (ehci_device_ich7);
169         case PCI_EHCI_DEVICEID_NEC:
170                 return (ehci_device_nec);
171         case PCI_EHCI_DEVICEID_NF2:
172                 return (ehci_device_nf2);
173         case PCI_EHCI_DEVICEID_NF2_400:
174                 return (ehci_device_nf2_400);
175         case PCI_EHCI_DEVICEID_NF3:
176                 return (ehci_device_nf3);
177         case PCI_EHCI_DEVICEID_NF3_250:
178                 return (ehci_device_nf3_250);
179         case PCI_EHCI_DEVICEID_NF4:
180                 return (ehci_device_nf4);
181         case PCI_EHCI_DEVICEID_ISP156X:
182                 return (ehci_device_isp156x);
183         default:
184                 if (pci_get_class(self) == PCIC_SERIALBUS
185                     && pci_get_subclass(self) == PCIS_SERIALBUS_USB
186                     && pci_get_progif(self) == PCI_INTERFACE_EHCI) {
187                         return (ehci_device_generic);
188                 }
189         }
190
191         return NULL;            /* dunno */
192 }
193
194 static int
195 ehci_pci_probe(device_t self)
196 {
197         const char *desc = ehci_pci_match(self);
198
199         if (desc) {
200                 device_set_desc(self, desc);
201                 return 0;
202         } else {
203                 return ENXIO;
204         }
205 }
206
207 static int
208 ehci_pci_attach(device_t self)
209 {
210         ehci_softc_t *sc = device_get_softc(self);
211         device_t parent;
212         device_t *neighbors;
213         device_t *nbus;
214         struct usbd_bus *bsc;
215         int err;
216         int rid;
217         int ncomp;
218         int count, buscount;
219         int slot, function;
220         int res;
221         int i;
222
223         switch(pci_read_config(self, PCI_USBREV, 1) & PCI_USBREV_MASK) {
224         case PCI_USBREV_PRE_1_0:
225         case PCI_USBREV_1_0:
226         case PCI_USBREV_1_1:
227                 sc->sc_bus.usbrev = USBREV_UNKNOWN;
228                 printf("pre-2.0 USB rev\n");
229                 return ENXIO;
230         case PCI_USBREV_2_0:
231                 sc->sc_bus.usbrev = USBREV_2_0;
232                 break;
233         default:
234                 sc->sc_bus.usbrev = USBREV_UNKNOWN;
235                 break;
236         }
237
238         pci_enable_busmaster(self);
239
240         rid = PCI_CBMEM;
241         sc->io_res = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
242             0, ~0, 1, RF_ACTIVE);
243         if (!sc->io_res) {
244                 device_printf(self, "Could not map memory\n");
245                 return ENXIO;
246         }
247         sc->iot = rman_get_bustag(sc->io_res);
248         sc->ioh = rman_get_bushandle(sc->io_res);
249
250         rid = 0;
251         sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1,
252             RF_SHAREABLE | RF_ACTIVE);
253         if (sc->irq_res == NULL) {
254                 device_printf(self, "Could not allocate irq\n");
255                 ehci_pci_detach(self);
256                 return ENXIO;
257         }
258         sc->sc_bus.bdev = device_add_child(self, "usb", -1);
259         if (!sc->sc_bus.bdev) {
260                 device_printf(self, "Could not add USB device\n");
261                 ehci_pci_detach(self);
262                 return ENOMEM;
263         }
264         device_set_ivars(sc->sc_bus.bdev, sc);
265
266         /* ehci_pci_match will never return NULL if ehci_pci_probe succeeded */
267         device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
268         switch (pci_get_vendor(self)) {
269         case PCI_EHCI_VENDORID_ACERLABS:
270                 sprintf(sc->sc_vendor, "AcerLabs");
271                 break;
272         case PCI_EHCI_VENDORID_AMD:
273                 sprintf(sc->sc_vendor, "AMD");
274                 break;
275         case PCI_EHCI_VENDORID_APPLE:
276                 sprintf(sc->sc_vendor, "Apple");
277                 break;
278         case PCI_EHCI_VENDORID_CMDTECH:
279                 sprintf(sc->sc_vendor, "CMDTECH");
280                 break;
281         case PCI_EHCI_VENDORID_NEC:
282                 sprintf(sc->sc_vendor, "NEC");
283                 break;
284         case PCI_EHCI_VENDORID_OPTI:
285                 sprintf(sc->sc_vendor, "OPTi");
286                 break;
287         case PCI_EHCI_VENDORID_SIS:
288                 sprintf(sc->sc_vendor, "SiS");
289                 break;
290         case PCI_EHCI_VENDORID_NVIDIA:
291         case PCI_EHCI_VENDORID_NVIDIA2:
292                 sprintf(sc->sc_vendor, "nVidia");
293                 break;
294         default:
295                 if (bootverbose)
296                         device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
297                             pci_get_devid(self));
298                 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
299         }
300
301         err = bus_setup_intr(self, sc->irq_res, 0,
302             (driver_intr_t *) ehci_intr, sc, &sc->ih, NULL);
303         if (err) {
304                 device_printf(self, "Could not setup irq, %d\n", err);
305                 sc->ih = NULL;
306                 ehci_pci_detach(self);
307                 return ENXIO;
308         }
309
310         /*
311          * Find companion controllers.  According to the spec they always
312          * have lower function numbers so they should be enumerated already.
313          */
314         parent = device_get_parent(self);
315         res = device_get_children(parent, &neighbors, &count);
316         if (res != 0) {
317                 device_printf(self, "Error finding companion busses\n");
318                 ehci_pci_detach(self);
319                 return ENXIO;
320         }
321         ncomp = 0;
322         slot = pci_get_slot(self);
323         function = pci_get_function(self);
324         for (i = 0; i < count; i++) {
325                 if (pci_get_slot(neighbors[i]) == slot && \
326                         pci_get_function(neighbors[i]) < function) {
327                         res = device_get_children(neighbors[i],
328                                 &nbus, &buscount);
329                         if (res != 0 || buscount != 1)
330                                 continue;
331                         bsc = device_get_softc(nbus[0]);
332                         printf("ehci_pci_attach: companion %s\n",
333                         USBDEVNAME(bsc->bdev));
334                         sc->sc_comps[ncomp++] = bsc;
335                         if (ncomp >= EHCI_COMPANION_MAX)
336                                 break;
337                 }
338         }
339         sc->sc_ncomp = ncomp;
340
341         err = ehci_init(sc);
342         if (!err)
343                 err = device_probe_and_attach(sc->sc_bus.bdev);
344
345         if (err) {
346                 device_printf(self, "USB init failed err=%d\n", err);
347 #if 0 /* TODO */
348                 ehci_pci_detach(self);
349 #endif
350                 return EIO;
351         }
352         ehci_init_intrs(sc);
353         return 0;
354 }
355
356 static int
357 ehci_pci_detach(device_t self)
358 {
359         ehci_softc_t *sc = device_get_softc(self);
360
361         /*
362          * XXX this code is not yet fit to be used as detach for the EHCI
363          * controller
364          */
365
366         /*
367          * disable interrupts that might have been switched on in ehci_init
368          */
369         if (sc->iot && sc->ioh)
370                 bus_space_write_4(sc->iot, sc->ioh, EHCI_USBINTR, 0);
371
372         if (sc->irq_res && sc->ih) {
373                 int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
374
375                 if (err)
376                         /* XXX or should we panic? */
377                         device_printf(self, "Could not tear down irq, %d\n",
378                             err);
379                 sc->ih = NULL;
380         }
381         if (sc->sc_bus.bdev) {
382                 device_delete_child(self, sc->sc_bus.bdev);
383                 sc->sc_bus.bdev = NULL;
384         }
385         if (sc->irq_res) {
386                 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
387                 sc->irq_res = NULL;
388         }
389         if (sc->io_res) {
390                 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM, sc->io_res);
391                 sc->io_res = NULL;
392                 sc->iot = 0;
393                 sc->ioh = 0;
394         }
395         return 0;
396 }
397
398 static device_method_t ehci_methods[] = {
399         /* Device interface */
400         DEVMETHOD(device_probe, ehci_pci_probe),
401         DEVMETHOD(device_attach, ehci_pci_attach),
402         DEVMETHOD(device_shutdown, bus_generic_shutdown),
403
404         /* Bus interface */
405         DEVMETHOD(bus_print_child, bus_generic_print_child),
406
407         {0, 0}
408 };
409
410 static driver_t ehci_driver = {
411         "ehci",
412         ehci_methods,
413         sizeof(ehci_softc_t),
414 };
415
416 static devclass_t ehci_devclass;
417
418 DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
419 DRIVER_MODULE(ehci, cardbus, ehci_driver, ehci_devclass, 0, 0);