Merge branch 'vendor/TCPDUMP'
[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  *
37  * $FreeBSD: src/sys/dev/usb/ehci_pci.c,v 1.18.2.1 2006/01/26 01:43:13 iedowse 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 <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/module.h>
58 #include <sys/bus.h>
59 #include <sys/queue.h>
60 #include <sys/lock.h>
61 #include <sys/rman.h>
62
63 #include <bus/pci/pcivar.h>
64 #include <bus/pci/pcireg.h>
65
66 #include <bus/usb/usb.h>
67 #include <bus/usb/usbdi.h>
68 #include <bus/usb/usbdivar.h>
69 #include <bus/usb/usb_mem.h>
70
71 #include <bus/usb/ehcireg.h>
72 #include <bus/usb/ehcivar.h>
73
74 #define PCI_EHCI_VENDORID_ACERLABS      0x10b9
75 #define PCI_EHCI_VENDORID_AMD           0x1022
76 #define PCI_EHCI_VENDORID_APPLE         0x106b
77 #define PCI_EHCI_VENDORID_ATI           0x1002
78 #define PCI_EHCI_VENDORID_CMDTECH       0x1095
79 #define PCI_EHCI_VENDORID_INTEL         0x8086
80 #define PCI_EHCI_VENDORID_NEC           0x1033
81 #define PCI_EHCI_VENDORID_OPTI          0x1045
82 #define PCI_EHCI_VENDORID_PHILIPS       0x1131
83 #define PCI_EHCI_VENDORID_SIS           0x1039
84 #define PCI_EHCI_VENDORID_NVIDIA        0x12D2
85 #define PCI_EHCI_VENDORID_NVIDIA2       0x10DE
86 #define PCI_EHCI_VENDORID_VIA           0x1106
87
88 /* AcerLabs/ALi */
89 #define PCI_EHCI_DEVICEID_M5239         0x523910b9
90 static const char *ehci_device_m5239 = "ALi M5239 USB 2.0 controller";
91
92 /* AMD */
93 #define PCI_EHCI_DEVICEID_8111          0x74631022
94 #define PCI_EHCI_DEVICEID_CS5536        0x20951022
95 static const char *ehci_device_8111 = "AMD 8111 USB 2.0 controller";
96 static const char *ehci_device_CS5536 = "AMD CS5536 USB 2.0 controller";
97
98 /* ATI */
99 #define PCI_EHCI_DEVICEID_SB200         0x43451002
100 static const char *ehci_device_sb200 = "ATI SB200 USB 2.0 controller";
101 #define PCI_EHCI_DEVICEID_SB400         0x43731002
102 static const char *ehci_device_sb400 = "ATI SB400 USB 2.0 controller";
103
104 /* Intel */
105 #define PCI_EHCI_DEVICEID_6300          0x25ad8086
106 static const char *ehci_device_6300 = "Intel 6300ESB USB 2.0 controller";
107 #define PCI_EHCI_DEVICEID_ICH4          0x24cd8086
108 static const char *ehci_device_ich4 = "Intel 82801DB/L/M USB 2.0 controller";
109 #define PCI_EHCI_DEVICEID_ICH5          0x24dd8086
110 static const char *ehci_device_ich5 = "Intel 82801EB/R USB 2.0 controller";
111 #define PCI_EHCI_DEVICEID_ICH6          0x265c8086
112 static const char *ehci_device_ich6 = "Intel 82801FB USB 2.0 controller";
113 #define PCI_EHCI_DEVICEID_ICH7          0x27cc8086
114 static const char *ehci_device_ich7 = "Intel 82801GB/R USB 2.0 controller";
115  
116 /* NEC */
117 #define PCI_EHCI_DEVICEID_NEC           0x00e01033
118 static const char *ehci_device_nec = "NEC uPD 720100 USB 2.0 controller";
119
120 /* NVIDIA */
121 #define PCI_EHCI_DEVICEID_NF2           0x006810de
122 static const char *ehci_device_nf2 = "NVIDIA nForce2 USB 2.0 controller";
123 #define PCI_EHCI_DEVICEID_NF2_400       0x008810de
124 static const char *ehci_device_nf2_400 = "NVIDIA nForce2 Ultra 400 USB 2.0 controller";
125 #define PCI_EHCI_DEVICEID_NF3           0x00d810de
126 static const char *ehci_device_nf3 = "NVIDIA nForce3 USB 2.0 controller";
127 #define PCI_EHCI_DEVICEID_NF3_250       0x00e810de
128 static const char *ehci_device_nf3_250 = "NVIDIA nForce3 250 USB 2.0 controller";
129 #define PCI_EHCI_DEVICEID_NF4           0x005b10de
130 static const char *ehci_device_nf4 = "NVIDIA nForce4 USB 2.0 controller";
131
132 /* Philips */
133 #define PCI_EHCI_DEVICEID_ISP156X       0x15621131
134 static const char *ehci_device_isp156x = "Philips ISP156x USB 2.0 controller";
135
136 /* VIA */
137 #define PCI_EHCI_DEVICEID_VIA           0x31041106
138 static const char *ehci_device_via = "VIA VT6202 USB 2.0 controller";
139
140 /* Generic */
141 static const char *ehci_device_generic = "EHCI (generic) USB 2.0 controller";
142
143 #define PCI_EHCI_BASE_REG       0x10
144
145 #ifdef USB_DEBUG
146 #define EHCI_DEBUG USB_DEBUG
147 #define DPRINTF(x)      do { if (ehcidebug) kprintf x; } while (0)
148 extern int ehcidebug;
149 #else
150 #define DPRINTF(x)
151 #endif
152
153 static int ehci_pci_attach(device_t self);
154 static int ehci_pci_detach(device_t self);
155 static int ehci_pci_shutdown(device_t self);
156 static int ehci_pci_suspend(device_t self);
157 static int ehci_pci_resume(device_t self);
158 static void ehci_pci_givecontroller(device_t self);
159 static void ehci_pci_takecontroller(device_t self);
160
161 static int
162 ehci_pci_suspend(device_t self)
163 {
164         ehci_softc_t *sc = device_get_softc(self);
165         int err;
166
167         err = bus_generic_suspend(self);
168         if (err)
169                 return (err);
170         ehci_power(PWR_SUSPEND, sc);
171
172         return 0;
173 }
174
175 static int
176 ehci_pci_resume(device_t self)
177 {
178         ehci_softc_t *sc = device_get_softc(self);
179
180         ehci_pci_takecontroller(self);
181         ehci_power(PWR_RESUME, sc);
182         bus_generic_resume(self);
183
184         return 0;
185 }
186
187 static int
188 ehci_pci_shutdown(device_t self)
189 {
190         ehci_softc_t *sc = device_get_softc(self);
191         int err;
192
193         err = bus_generic_shutdown(self);
194         if (sc->sc_flags & EHCI_SCFLG_DONEINIT) {
195                 ehci_shutdown(sc);
196                 ehci_pci_givecontroller(self);
197         }
198
199         return err;
200 }
201
202 static const char *
203 ehci_pci_match(device_t self)
204 {
205         u_int32_t device_id = pci_get_devid(self);
206
207         switch (device_id) {
208         case PCI_EHCI_DEVICEID_M5239:
209                 return (ehci_device_m5239);
210         case PCI_EHCI_DEVICEID_8111:
211                 return (ehci_device_8111);
212         case PCI_EHCI_DEVICEID_CS5536:
213                 return (ehci_device_CS5536);
214         case PCI_EHCI_DEVICEID_SB200:
215                 return (ehci_device_sb200);
216         case PCI_EHCI_DEVICEID_SB400:
217                 return (ehci_device_sb400);
218         case PCI_EHCI_DEVICEID_6300:
219                 return (ehci_device_6300);
220         case PCI_EHCI_DEVICEID_ICH4:
221                 return (ehci_device_ich4);
222         case PCI_EHCI_DEVICEID_ICH5:
223                 return (ehci_device_ich5);
224         case PCI_EHCI_DEVICEID_ICH6:
225                 return (ehci_device_ich6);
226         case PCI_EHCI_DEVICEID_ICH7:
227                 return (ehci_device_ich7);
228         case PCI_EHCI_DEVICEID_NEC:
229                 return (ehci_device_nec);
230         case PCI_EHCI_DEVICEID_NF2:
231                 return (ehci_device_nf2);
232         case PCI_EHCI_DEVICEID_NF2_400:
233                 return (ehci_device_nf2_400);
234         case PCI_EHCI_DEVICEID_NF3:
235                 return (ehci_device_nf3);
236         case PCI_EHCI_DEVICEID_NF3_250:
237                 return (ehci_device_nf3_250);
238         case PCI_EHCI_DEVICEID_NF4:
239                 return (ehci_device_nf4);
240         case PCI_EHCI_DEVICEID_ISP156X:
241                 return (ehci_device_isp156x);
242         case PCI_EHCI_DEVICEID_VIA:
243                 return (ehci_device_via);
244         default:
245                 if (pci_get_class(self) == PCIC_SERIALBUS
246                     && pci_get_subclass(self) == PCIS_SERIALBUS_USB
247                     && pci_get_progif(self) == PCI_INTERFACE_EHCI) {
248                         return (ehci_device_generic);
249                 }
250         }
251
252         return NULL;            /* dunno */
253 }
254
255 static int
256 ehci_pci_probe(device_t self)
257 {
258         const char *desc = ehci_pci_match(self);
259
260         if (desc) {
261                 device_set_desc(self, desc);
262                 device_set_async_attach(self, TRUE);
263                 return 0;
264         } else {
265                 return ENXIO;
266         }
267 }
268
269 static int
270 ehci_pci_attach(device_t self)
271 {
272         ehci_softc_t *sc = device_get_softc(self);
273         device_t parent;
274         device_t *neighbors;
275         device_t *nbus;
276         struct usb_softc *usb_sc;
277         struct usbd_bus *bsc;
278         int err;
279         int rid;
280         int ncomp;
281         int count, buscount;
282         int slot, function;
283         int res;
284         int i;
285
286         switch(pci_read_config(self, PCI_USBREV, 1) & PCI_USBREV_MASK) {
287         case PCI_USBREV_PRE_1_0:
288         case PCI_USBREV_1_0:
289         case PCI_USBREV_1_1:
290                 sc->sc_bus.usbrev = USBREV_UNKNOWN;
291                 kprintf("pre-2.0 USB rev\n");
292                 return ENXIO;
293         case PCI_USBREV_2_0:
294                 sc->sc_bus.usbrev = USBREV_2_0;
295                 break;
296         default:
297                 sc->sc_bus.usbrev = USBREV_UNKNOWN;
298                 break;
299         }
300
301         pci_enable_busmaster(self);
302
303         rid = PCI_CBMEM;
304         sc->io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
305             RF_ACTIVE);
306         if (!sc->io_res) {
307                 device_printf(self, "Could not map memory\n");
308                 return ENXIO;
309         }
310         sc->iot = rman_get_bustag(sc->io_res);
311         sc->ioh = rman_get_bushandle(sc->io_res);
312
313         rid = 0;
314         sc->irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
315             RF_SHAREABLE | RF_ACTIVE);
316         if (sc->irq_res == NULL) {
317                 device_printf(self, "Could not allocate irq\n");
318                 ehci_pci_detach(self);
319                 return ENXIO;
320         }
321         sc->sc_bus.bdev = device_add_child(self, "usb", -1);
322         if (!sc->sc_bus.bdev) {
323                 device_printf(self, "Could not add USB device\n");
324                 ehci_pci_detach(self);
325                 return ENOMEM;
326         }
327         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
328
329         /* ehci_pci_match will never return NULL if ehci_pci_probe succeeded */
330         device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
331         switch (pci_get_vendor(self)) {
332         case PCI_EHCI_VENDORID_ACERLABS:
333                 ksprintf(sc->sc_vendor, "AcerLabs");
334                 break;
335         case PCI_EHCI_VENDORID_AMD:
336                 ksprintf(sc->sc_vendor, "AMD");
337                 break;
338         case PCI_EHCI_VENDORID_APPLE:
339                 ksprintf(sc->sc_vendor, "Apple");
340                 break;
341         case PCI_EHCI_VENDORID_ATI:
342                 ksprintf(sc->sc_vendor, "ATI");
343                 break;
344         case PCI_EHCI_VENDORID_CMDTECH:
345                 ksprintf(sc->sc_vendor, "CMDTECH");
346                 break;
347         case PCI_EHCI_VENDORID_INTEL:
348                 ksprintf(sc->sc_vendor, "Intel");
349                 break;
350         case PCI_EHCI_VENDORID_NEC:
351                 ksprintf(sc->sc_vendor, "NEC");
352                 break;
353         case PCI_EHCI_VENDORID_OPTI:
354                 ksprintf(sc->sc_vendor, "OPTi");
355                 break;
356         case PCI_EHCI_VENDORID_SIS:
357                 ksprintf(sc->sc_vendor, "SiS");
358                 break;
359         case PCI_EHCI_VENDORID_NVIDIA:
360         case PCI_EHCI_VENDORID_NVIDIA2:
361                 ksprintf(sc->sc_vendor, "nVidia");
362                 break;
363         case PCI_EHCI_VENDORID_VIA:
364                 ksprintf(sc->sc_vendor, "VIA");
365                 break;
366         default:
367                 if (bootverbose)
368                         device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
369                             pci_get_devid(self));
370                 ksprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
371         }
372
373         err = bus_setup_intr(self, sc->irq_res, 0,
374             (driver_intr_t *) ehci_intr, sc, &sc->ih, NULL);
375         if (err) {
376                 device_printf(self, "Could not setup irq, %d\n", err);
377                 sc->ih = NULL;
378                 ehci_pci_detach(self);
379                 return ENXIO;
380         }
381
382         /* Enable workaround for dropped interrupts as required */
383         switch (pci_get_vendor(self)) {
384         case PCI_EHCI_VENDORID_ATI:
385         case PCI_EHCI_VENDORID_VIA:
386                 sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
387                 if (bootverbose)
388                         device_printf(self,
389                             "Dropped interrupts workaround enabled\n");
390                 break;
391         default:
392                 break;
393         }
394
395         /*
396          * Find companion controllers.  According to the spec they always
397          * have lower function numbers so they should be enumerated already.
398          */
399         parent = device_get_parent(self);
400         res = device_get_children(parent, &neighbors, &count);
401         if (res != 0) {
402                 device_printf(self, "Error finding companion busses\n");
403                 ehci_pci_detach(self);
404                 return ENXIO;
405         }
406         ncomp = 0;
407         slot = pci_get_slot(self);
408         function = pci_get_function(self);
409         for (i = 0; i < count; i++) {
410                 if (pci_get_slot(neighbors[i]) == slot && \
411                         pci_get_function(neighbors[i]) < function) {
412                         res = device_get_children(neighbors[i],
413                                 &nbus, &buscount);
414                         if (res != 0 || buscount != 1)
415                                 continue;
416                         usb_sc = device_get_softc(nbus[0]);
417                         if (usb_sc == NULL)
418                                 continue;
419                         bsc = usb_getbushandle(usb_sc);
420                         if (bsc == NULL)
421                                 continue;
422                         DPRINTF(("ehci_pci_attach: companion %s\n",
423                             device_get_nameunit(bsc->bdev)));
424                         sc->sc_comps[ncomp++] = bsc;
425                         if (ncomp >= EHCI_COMPANION_MAX)
426                                 break;
427                 }
428         }
429         sc->sc_ncomp = ncomp;
430
431         ehci_pci_takecontroller(self);
432         err = ehci_init(sc);
433         if (err == 0)
434                 err = device_probe_and_attach(sc->sc_bus.bdev);
435
436         if (err) {
437                 device_printf(self, "USB init failed err=%d\n", err);
438                 ehci_pci_detach(self);
439                 return EIO;
440         }
441         return 0;
442 }
443
444 static int
445 ehci_pci_detach(device_t self)
446 {
447         ehci_softc_t *sc = device_get_softc(self);
448
449         if (sc->sc_flags & EHCI_SCFLG_DONEINIT) {
450                 ehci_detach(sc, 0);
451                 sc->sc_flags &= ~EHCI_SCFLG_DONEINIT;
452         }
453
454         /*
455          * disable interrupts that might have been switched on in ehci_init
456          */
457         if (sc->iot && sc->ioh)
458                 bus_space_write_4(sc->iot, sc->ioh, EHCI_USBINTR, 0);
459
460         if (sc->irq_res && sc->ih) {
461                 int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
462
463                 if (err)
464                         /* XXX or should we panic? */
465                         device_printf(self, "Could not tear down irq, %d\n",
466                             err);
467                 sc->ih = NULL;
468         }
469         if (sc->sc_bus.bdev) {
470                 device_delete_child(self, sc->sc_bus.bdev);
471                 sc->sc_bus.bdev = NULL;
472         }
473         if (sc->irq_res) {
474                 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
475                 sc->irq_res = NULL;
476         }
477         if (sc->io_res) {
478                 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM, sc->io_res);
479                 sc->io_res = NULL;
480                 sc->iot = 0;
481                 sc->ioh = 0;
482         }
483         return 0;
484 }
485
486 static void
487 ehci_pci_takecontroller(device_t self)
488 {
489         ehci_softc_t *sc = device_get_softc(self);
490         u_int32_t cparams, eec, legsup;
491         int eecp, i;
492
493         cparams = EREAD4(sc, EHCI_HCCPARAMS);
494
495         /* Synchronise with the BIOS if it owns the controller. */
496         for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
497              eecp = EHCI_EECP_NEXT(eec)) {
498                 eec = pci_read_config(self, eecp, 4);
499                 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP)
500                         continue;
501                 legsup = eec;
502                 pci_write_config(self, eecp, legsup | EHCI_LEGSUP_OSOWNED, 4);
503                 if (legsup & EHCI_LEGSUP_BIOSOWNED) {
504                         device_printf(sc->sc_bus.bdev,
505                             "waiting for BIOS to give up control\n");
506                         for (i = 0; i < 5000; i++) {
507                                 legsup = pci_read_config(self, eecp, 4);
508                                 if ((legsup & EHCI_LEGSUP_BIOSOWNED) == 0)
509                                         break;
510                                 DELAY(1000);
511                         }
512                         if (legsup & EHCI_LEGSUP_BIOSOWNED)
513                                 device_printf(sc->sc_bus.bdev,
514                                     "timed out waiting for BIOS\n");
515                 }
516         }
517 }
518
519 /*
520  * Return the controller to the BIOS.  Do we really need to do this?
521  *
522  * One thing we do need to do is give the chip reset (from the shutdown)
523  * time to finish before handing anything back.  This fixes a machine
524  * lockup.
525  */
526 static void
527 ehci_pci_givecontroller(device_t self)
528 {
529         ehci_softc_t *sc = device_get_softc(self);
530         u_int32_t cparams, eec, legsup;
531         int eecp;
532
533         DELAY(1000);
534         cparams = EREAD4(sc, EHCI_HCCPARAMS);
535         for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
536              eecp = EHCI_EECP_NEXT(eec)) {
537                 eec = pci_read_config(self, eecp, 4);
538                 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP)
539                         continue;
540                 legsup = eec;
541                 pci_write_config(self, eecp, legsup & ~EHCI_LEGSUP_OSOWNED, 4);
542         }
543 }
544
545 static device_method_t ehci_methods[] = {
546         /* Device interface */
547         DEVMETHOD(device_probe, ehci_pci_probe),
548         DEVMETHOD(device_attach, ehci_pci_attach),
549         DEVMETHOD(device_detach, ehci_pci_detach),
550         DEVMETHOD(device_suspend, ehci_pci_suspend),
551         DEVMETHOD(device_resume, ehci_pci_resume),
552         DEVMETHOD(device_shutdown, ehci_pci_shutdown),
553
554         /* Bus interface */
555         DEVMETHOD(bus_print_child, bus_generic_print_child),
556
557         {0, 0}
558 };
559
560 static driver_t ehci_driver = {
561         "ehci",
562         ehci_methods,
563         sizeof(ehci_softc_t),
564 };
565
566 static devclass_t ehci_devclass;
567
568 DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, NULL, NULL);
569 DRIVER_MODULE(ehci, cardbus, ehci_driver, ehci_devclass, NULL, NULL);