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