Rename printf -> kprintf in sys/ and add some defines where necessary
[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  * $DragonFly: src/sys/bus/usb/ehci_pci.c,v 1.14 2006/12/22 23:12:17 swildner Exp $
39  */
40
41 /*
42  * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
43  *
44  * The EHCI 1.0 spec can be found at
45  * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
46  * and the USB 2.0 spec at
47  * http://www.usb.org/developers/docs/usb_20.zip
48  */
49
50 /* The low level controller code for EHCI has been split into
51  * PCI probes and EHCI specific code. This was done to facilitate the
52  * sharing of code between *BSD's
53  */
54
55 #include "opt_bus.h"
56
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/module.h>
61 #include <sys/bus.h>
62 #include <sys/queue.h>
63 #include <sys/lock.h>
64 #include <sys/rman.h>
65
66 #include <bus/pci/pcivar.h>
67 #include <bus/pci/pcireg.h>
68
69 #include <bus/usb/usb.h>
70 #include <bus/usb/usbdi.h>
71 #include <bus/usb/usbdivar.h>
72 #include <bus/usb/usb_mem.h>
73
74 #include <bus/usb/ehcireg.h>
75 #include <bus/usb/ehcivar.h>
76
77 #define PCI_EHCI_VENDORID_ACERLABS      0x10b9
78 #define PCI_EHCI_VENDORID_AMD           0x1022
79 #define PCI_EHCI_VENDORID_APPLE         0x106b
80 #define PCI_EHCI_VENDORID_ATI           0x1002
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_PHILIPS       0x1131
86 #define PCI_EHCI_VENDORID_SIS           0x1039
87 #define PCI_EHCI_VENDORID_NVIDIA        0x12D2
88 #define PCI_EHCI_VENDORID_NVIDIA2       0x10DE
89 #define PCI_EHCI_VENDORID_VIA           0x1106
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 /* VIA */
138 #define PCI_EHCI_DEVICEID_VIA           0x31041106
139 static const char *ehci_device_via = "VIA VT6202 USB 2.0 controller";
140
141 /* Generic */
142 static const char *ehci_device_generic = "EHCI (generic) USB 2.0 controller";
143
144 #define PCI_EHCI_BASE_REG       0x10
145
146 #ifdef USB_DEBUG
147 #define EHCI_DEBUG USB_DEBUG
148 #define DPRINTF(x)      do { if (ehcidebug) logprintf x; } while (0)
149 extern int ehcidebug;
150 #else
151 #define DPRINTF(x)
152 #endif
153
154 static int ehci_pci_attach(device_t self);
155 static int ehci_pci_detach(device_t self);
156 static int ehci_pci_shutdown(device_t self);
157 static int ehci_pci_suspend(device_t self);
158 static int ehci_pci_resume(device_t self);
159 static void ehci_pci_givecontroller(device_t self);
160 static void ehci_pci_takecontroller(device_t self);
161
162 static int
163 ehci_pci_suspend(device_t self)
164 {
165         ehci_softc_t *sc = device_get_softc(self);
166         int err;
167
168         err = bus_generic_suspend(self);
169         if (err)
170                 return (err);
171         ehci_power(PWR_SUSPEND, sc);
172
173         return 0;
174 }
175
176 static int
177 ehci_pci_resume(device_t self)
178 {
179         ehci_softc_t *sc = device_get_softc(self);
180
181         ehci_pci_takecontroller(self);
182         ehci_power(PWR_RESUME, sc);
183         bus_generic_resume(self);
184
185         return 0;
186 }
187
188 static int
189 ehci_pci_shutdown(device_t self)
190 {
191         ehci_softc_t *sc = device_get_softc(self);
192         int err;
193
194         err = bus_generic_shutdown(self);
195         if (err)
196                 return (err);
197         ehci_shutdown(sc);
198         ehci_pci_givecontroller(self);
199
200         return 0;
201 }
202
203 static const char *
204 ehci_pci_match(device_t self)
205 {
206         u_int32_t device_id = pci_get_devid(self);
207
208         switch (device_id) {
209         case PCI_EHCI_DEVICEID_M5239:
210                 return (ehci_device_m5239);
211         case PCI_EHCI_DEVICEID_8111:
212                 return (ehci_device_8111);
213         case PCI_EHCI_DEVICEID_SB200:
214                 return (ehci_device_sb200);
215         case PCI_EHCI_DEVICEID_SB400:
216                 return (ehci_device_sb400);
217         case PCI_EHCI_DEVICEID_6300:
218                 return (ehci_device_6300);
219         case PCI_EHCI_DEVICEID_ICH4:
220                 return (ehci_device_ich4);
221         case PCI_EHCI_DEVICEID_ICH5:
222                 return (ehci_device_ich5);
223         case PCI_EHCI_DEVICEID_ICH6:
224                 return (ehci_device_ich6);
225         case PCI_EHCI_DEVICEID_ICH7:
226                 return (ehci_device_ich7);
227         case PCI_EHCI_DEVICEID_NEC:
228                 return (ehci_device_nec);
229         case PCI_EHCI_DEVICEID_NF2:
230                 return (ehci_device_nf2);
231         case PCI_EHCI_DEVICEID_NF2_400:
232                 return (ehci_device_nf2_400);
233         case PCI_EHCI_DEVICEID_NF3:
234                 return (ehci_device_nf3);
235         case PCI_EHCI_DEVICEID_NF3_250:
236                 return (ehci_device_nf3_250);
237         case PCI_EHCI_DEVICEID_NF4:
238                 return (ehci_device_nf4);
239         case PCI_EHCI_DEVICEID_ISP156X:
240                 return (ehci_device_isp156x);
241         case PCI_EHCI_DEVICEID_VIA:
242                 return (ehci_device_via);
243         default:
244                 if (pci_get_class(self) == PCIC_SERIALBUS
245                     && pci_get_subclass(self) == PCIS_SERIALBUS_USB
246                     && pci_get_progif(self) == PCI_INTERFACE_EHCI) {
247                         return (ehci_device_generic);
248                 }
249         }
250
251         return NULL;            /* dunno */
252 }
253
254 static int
255 ehci_pci_probe(device_t self)
256 {
257         const char *desc = ehci_pci_match(self);
258
259         if (desc) {
260                 device_set_desc(self, desc);
261                 return 0;
262         } else {
263                 return ENXIO;
264         }
265 }
266
267 static int
268 ehci_pci_attach(device_t self)
269 {
270         ehci_softc_t *sc = device_get_softc(self);
271         device_t parent;
272         device_t *neighbors;
273         device_t *nbus;
274         struct usbd_bus *bsc;
275         int err;
276         int rid;
277         int ncomp;
278         int count, buscount;
279         int slot, function;
280         int res;
281         int i;
282
283         switch(pci_read_config(self, PCI_USBREV, 1) & PCI_USBREV_MASK) {
284         case PCI_USBREV_PRE_1_0:
285         case PCI_USBREV_1_0:
286         case PCI_USBREV_1_1:
287                 sc->sc_bus.usbrev = USBREV_UNKNOWN;
288                 kprintf("pre-2.0 USB rev\n");
289                 return ENXIO;
290         case PCI_USBREV_2_0:
291                 sc->sc_bus.usbrev = USBREV_2_0;
292                 break;
293         default:
294                 sc->sc_bus.usbrev = USBREV_UNKNOWN;
295                 break;
296         }
297
298         pci_enable_busmaster(self);
299
300         rid = PCI_CBMEM;
301         sc->io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
302             RF_ACTIVE);
303         if (!sc->io_res) {
304                 device_printf(self, "Could not map memory\n");
305                 return ENXIO;
306         }
307         sc->iot = rman_get_bustag(sc->io_res);
308         sc->ioh = rman_get_bushandle(sc->io_res);
309
310         rid = 0;
311         sc->irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
312             RF_SHAREABLE | RF_ACTIVE);
313         if (sc->irq_res == NULL) {
314                 device_printf(self, "Could not allocate irq\n");
315                 ehci_pci_detach(self);
316                 return ENXIO;
317         }
318         sc->sc_bus.bdev = device_add_child(self, "usb", -1);
319         if (!sc->sc_bus.bdev) {
320                 device_printf(self, "Could not add USB device\n");
321                 ehci_pci_detach(self);
322                 return ENOMEM;
323         }
324         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
325
326         /* ehci_pci_match will never return NULL if ehci_pci_probe succeeded */
327         device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
328         switch (pci_get_vendor(self)) {
329         case PCI_EHCI_VENDORID_ACERLABS:
330                 ksprintf(sc->sc_vendor, "AcerLabs");
331                 break;
332         case PCI_EHCI_VENDORID_AMD:
333                 ksprintf(sc->sc_vendor, "AMD");
334                 break;
335         case PCI_EHCI_VENDORID_APPLE:
336                 ksprintf(sc->sc_vendor, "Apple");
337                 break;
338         case PCI_EHCI_VENDORID_ATI:
339                 ksprintf(sc->sc_vendor, "ATI");
340                 break;
341         case PCI_EHCI_VENDORID_CMDTECH:
342                 ksprintf(sc->sc_vendor, "CMDTECH");
343                 break;
344         case PCI_EHCI_VENDORID_INTEL:
345                 ksprintf(sc->sc_vendor, "Intel");
346                 break;
347         case PCI_EHCI_VENDORID_NEC:
348                 ksprintf(sc->sc_vendor, "NEC");
349                 break;
350         case PCI_EHCI_VENDORID_OPTI:
351                 ksprintf(sc->sc_vendor, "OPTi");
352                 break;
353         case PCI_EHCI_VENDORID_SIS:
354                 ksprintf(sc->sc_vendor, "SiS");
355                 break;
356         case PCI_EHCI_VENDORID_NVIDIA:
357         case PCI_EHCI_VENDORID_NVIDIA2:
358                 ksprintf(sc->sc_vendor, "nVidia");
359                 break;
360         case PCI_EHCI_VENDORID_VIA:
361                 ksprintf(sc->sc_vendor, "VIA");
362                 break;
363         default:
364                 if (bootverbose)
365                         device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
366                             pci_get_devid(self));
367                 ksprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
368         }
369
370         err = bus_setup_intr(self, sc->irq_res, 0,
371             (driver_intr_t *) ehci_intr, sc, &sc->ih, NULL);
372         if (err) {
373                 device_printf(self, "Could not setup irq, %d\n", err);
374                 sc->ih = NULL;
375                 ehci_pci_detach(self);
376                 return ENXIO;
377         }
378
379         /* Enable workaround for dropped interrupts as required */
380         switch (pci_get_vendor(self)) {
381         case PCI_EHCI_VENDORID_ATI:
382         case PCI_EHCI_VENDORID_VIA:
383                 sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
384                 if (bootverbose)
385                         device_printf(self,
386                             "Dropped interrupts workaround enabled\n");
387                 break;
388         default:
389                 break;
390         }
391
392         /*
393          * Find companion controllers.  According to the spec they always
394          * have lower function numbers so they should be enumerated already.
395          */
396         parent = device_get_parent(self);
397         res = device_get_children(parent, &neighbors, &count);
398         if (res != 0) {
399                 device_printf(self, "Error finding companion busses\n");
400                 ehci_pci_detach(self);
401                 return ENXIO;
402         }
403         ncomp = 0;
404         slot = pci_get_slot(self);
405         function = pci_get_function(self);
406         for (i = 0; i < count; i++) {
407                 if (pci_get_slot(neighbors[i]) == slot && \
408                         pci_get_function(neighbors[i]) < function) {
409                         res = device_get_children(neighbors[i],
410                                 &nbus, &buscount);
411                         if (res != 0 || buscount != 1)
412                                 continue;
413                         bsc = device_get_softc(nbus[0]);
414                         DPRINTF(("ehci_pci_attach: companion %s\n",
415                             USBDEVNAME(bsc->bdev)));
416                         sc->sc_comps[ncomp++] = bsc;
417                         if (ncomp >= EHCI_COMPANION_MAX)
418                                 break;
419                 }
420         }
421         sc->sc_ncomp = ncomp;
422
423         ehci_pci_takecontroller(self);
424         err = ehci_init(sc);
425         if (!err) {
426                 sc->sc_flags |= EHCI_SCFLG_DONEINIT;
427                 err = device_probe_and_attach(sc->sc_bus.bdev);
428         }
429
430         if (err) {
431                 device_printf(self, "USB init failed err=%d\n", err);
432                 ehci_pci_detach(self);
433                 return EIO;
434         }
435         return 0;
436 }
437
438 static int
439 ehci_pci_detach(device_t self)
440 {
441         ehci_softc_t *sc = device_get_softc(self);
442
443         if (sc->sc_flags & EHCI_SCFLG_DONEINIT) {
444                 ehci_detach(sc, 0);
445                 sc->sc_flags &= ~EHCI_SCFLG_DONEINIT;
446         }
447
448         /*
449          * disable interrupts that might have been switched on in ehci_init
450          */
451         if (sc->iot && sc->ioh)
452                 bus_space_write_4(sc->iot, sc->ioh, EHCI_USBINTR, 0);
453
454         if (sc->irq_res && sc->ih) {
455                 int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
456
457                 if (err)
458                         /* XXX or should we panic? */
459                         device_printf(self, "Could not tear down irq, %d\n",
460                             err);
461                 sc->ih = NULL;
462         }
463         if (sc->sc_bus.bdev) {
464                 device_delete_child(self, sc->sc_bus.bdev);
465                 sc->sc_bus.bdev = NULL;
466         }
467         if (sc->irq_res) {
468                 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
469                 sc->irq_res = NULL;
470         }
471         if (sc->io_res) {
472                 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM, sc->io_res);
473                 sc->io_res = NULL;
474                 sc->iot = 0;
475                 sc->ioh = 0;
476         }
477         return 0;
478 }
479
480 static void
481 ehci_pci_takecontroller(device_t self)
482 {
483         ehci_softc_t *sc = device_get_softc(self);
484         u_int32_t cparams, eec, legsup;
485         int eecp, i;
486
487         cparams = EREAD4(sc, EHCI_HCCPARAMS);
488
489         /* Synchronise with the BIOS if it owns the controller. */
490         for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
491              eecp = EHCI_EECP_NEXT(eec)) {
492                 eec = pci_read_config(self, eecp, 4);
493                 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP)
494                         continue;
495                 legsup = eec;
496                 pci_write_config(self, eecp, legsup | EHCI_LEGSUP_OSOWNED, 4);
497                 if (legsup & EHCI_LEGSUP_BIOSOWNED) {
498                         kprintf("%s: waiting for BIOS to give up control\n",
499                             USBDEVNAME(sc->sc_bus.bdev));
500                         for (i = 0; i < 5000; i++) {
501                                 legsup = pci_read_config(self, eecp, 4);
502                                 if ((legsup & EHCI_LEGSUP_BIOSOWNED) == 0)
503                                         break;
504                                 DELAY(1000);
505                         }
506                         if (legsup & EHCI_LEGSUP_BIOSOWNED)
507                                 kprintf("%s: timed out waiting for BIOS\n",
508                                     USBDEVNAME(sc->sc_bus.bdev));
509                 }
510         }
511 }
512
513 static void
514 ehci_pci_givecontroller(device_t self)
515 {
516         ehci_softc_t *sc = device_get_softc(self);
517         u_int32_t cparams, eec, legsup;
518         int eecp;
519
520         cparams = EREAD4(sc, EHCI_HCCPARAMS);
521         for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
522              eecp = EHCI_EECP_NEXT(eec)) {
523                 eec = pci_read_config(self, eecp, 4);
524                 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP)
525                         continue;
526                 legsup = eec;
527                 pci_write_config(self, eecp, legsup & ~EHCI_LEGSUP_OSOWNED, 4);
528         }
529 }
530
531 static device_method_t ehci_methods[] = {
532         /* Device interface */
533         DEVMETHOD(device_probe, ehci_pci_probe),
534         DEVMETHOD(device_attach, ehci_pci_attach),
535         DEVMETHOD(device_detach, ehci_pci_detach),
536         DEVMETHOD(device_suspend, ehci_pci_suspend),
537         DEVMETHOD(device_resume, ehci_pci_resume),
538         DEVMETHOD(device_shutdown, ehci_pci_shutdown),
539
540         /* Bus interface */
541         DEVMETHOD(bus_print_child, bus_generic_print_child),
542
543         {0, 0}
544 };
545
546 static driver_t ehci_driver = {
547         "ehci",
548         ehci_methods,
549         sizeof(ehci_softc_t),
550 };
551
552 static devclass_t ehci_devclass;
553
554 DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
555 DRIVER_MODULE(ehci, cardbus, ehci_driver, ehci_devclass, 0, 0);