]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/usb/controller/ehci_pci.c
MFC r199058
[FreeBSD/stable/8.git] / sys / dev / usb / controller / 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
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
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 <sys/stdint.h>
56 #include <sys/stddef.h>
57 #include <sys/param.h>
58 #include <sys/queue.h>
59 #include <sys/types.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/bus.h>
63 #include <sys/linker_set.h>
64 #include <sys/module.h>
65 #include <sys/lock.h>
66 #include <sys/mutex.h>
67 #include <sys/condvar.h>
68 #include <sys/sysctl.h>
69 #include <sys/sx.h>
70 #include <sys/unistd.h>
71 #include <sys/callout.h>
72 #include <sys/malloc.h>
73 #include <sys/priv.h>
74
75 #include <dev/usb/usb.h>
76 #include <dev/usb/usbdi.h>
77
78 #include <dev/usb/usb_core.h>
79 #include <dev/usb/usb_busdma.h>
80 #include <dev/usb/usb_process.h>
81 #include <dev/usb/usb_util.h>
82
83 #include <dev/usb/usb_controller.h>
84 #include <dev/usb/usb_bus.h>
85 #include <dev/usb/usb_pci.h>
86 #include <dev/usb/controller/ehci.h>
87 #include <dev/usb/controller/ehcireg.h>
88
89 #define PCI_EHCI_VENDORID_ACERLABS      0x10b9
90 #define PCI_EHCI_VENDORID_AMD           0x1022
91 #define PCI_EHCI_VENDORID_APPLE         0x106b
92 #define PCI_EHCI_VENDORID_ATI           0x1002
93 #define PCI_EHCI_VENDORID_CMDTECH       0x1095
94 #define PCI_EHCI_VENDORID_INTEL         0x8086
95 #define PCI_EHCI_VENDORID_NEC           0x1033
96 #define PCI_EHCI_VENDORID_OPTI          0x1045
97 #define PCI_EHCI_VENDORID_PHILIPS       0x1131
98 #define PCI_EHCI_VENDORID_SIS           0x1039
99 #define PCI_EHCI_VENDORID_NVIDIA        0x12D2
100 #define PCI_EHCI_VENDORID_NVIDIA2       0x10DE
101 #define PCI_EHCI_VENDORID_VIA           0x1106
102
103 #define PCI_EHCI_BASE_REG       0x10
104
105 static void ehci_pci_takecontroller(device_t self);
106
107 static device_probe_t ehci_pci_probe;
108 static device_attach_t ehci_pci_attach;
109 static device_detach_t ehci_pci_detach;
110 static device_suspend_t ehci_pci_suspend;
111 static device_resume_t ehci_pci_resume;
112 static device_shutdown_t ehci_pci_shutdown;
113
114 static int
115 ehci_pci_suspend(device_t self)
116 {
117         ehci_softc_t *sc = device_get_softc(self);
118         int err;
119
120         err = bus_generic_suspend(self);
121         if (err)
122                 return (err);
123         ehci_suspend(sc);
124         return (0);
125 }
126
127 static int
128 ehci_pci_resume(device_t self)
129 {
130         ehci_softc_t *sc = device_get_softc(self);
131
132         ehci_pci_takecontroller(self);
133         ehci_resume(sc);
134
135         bus_generic_resume(self);
136
137         return (0);
138 }
139
140 static int
141 ehci_pci_shutdown(device_t self)
142 {
143         ehci_softc_t *sc = device_get_softc(self);
144         int err;
145
146         err = bus_generic_shutdown(self);
147         if (err)
148                 return (err);
149         ehci_shutdown(sc);
150
151         return (0);
152 }
153
154 static const char *
155 ehci_pci_match(device_t self)
156 {
157         uint32_t device_id = pci_get_devid(self);
158
159         switch (device_id) {
160         case 0x268c8086:
161                 return ("Intel 63XXESB USB 2.0 controller");
162
163         case 0x523910b9:
164                 return "ALi M5239 USB 2.0 controller";
165
166         case 0x10227463:
167                 return "AMD 8111 USB 2.0 controller";
168
169         case 0x20951022:
170                 return ("AMD CS5536 (Geode) USB 2.0 controller");
171
172         case 0x43451002:
173                 return "ATI SB200 USB 2.0 controller";
174         case 0x43731002:
175                 return "ATI SB400 USB 2.0 controller";
176
177         case 0x25ad8086:
178                 return "Intel 6300ESB USB 2.0 controller";
179         case 0x24cd8086:
180                 return "Intel 82801DB/L/M (ICH4) USB 2.0 controller";
181         case 0x24dd8086:
182                 return "Intel 82801EB/R (ICH5) USB 2.0 controller";
183         case 0x265c8086:
184                 return "Intel 82801FB (ICH6) USB 2.0 controller";
185         case 0x27cc8086:
186                 return "Intel 82801GB/R (ICH7) USB 2.0 controller";
187
188         case 0x28368086:
189                 return "Intel 82801H (ICH8) USB 2.0 controller USB2-A";
190         case 0x283a8086:
191                 return "Intel 82801H (ICH8) USB 2.0 controller USB2-B";
192         case 0x293a8086:
193                 return "Intel 82801I (ICH9) USB 2.0 controller";
194         case 0x293c8086:
195                 return "Intel 82801I (ICH9) USB 2.0 controller";
196
197         case 0x00e01033:
198                 return ("NEC uPD 720100 USB 2.0 controller");
199
200         case 0x006810de:
201                 return "NVIDIA nForce2 USB 2.0 controller";
202         case 0x008810de:
203                 return "NVIDIA nForce2 Ultra 400 USB 2.0 controller";
204         case 0x00d810de:
205                 return "NVIDIA nForce3 USB 2.0 controller";
206         case 0x00e810de:
207                 return "NVIDIA nForce3 250 USB 2.0 controller";
208         case 0x005b10de:
209                 return "NVIDIA nForce4 USB 2.0 controller";
210         case 0x03f210de:
211                 return "NVIDIA nForce MCP61 USB 2.0 controller";
212
213         case 0x15621131:
214                 return "Philips ISP156x USB 2.0 controller";
215
216         case 0x31041106:
217                 return ("VIA VT6202 USB 2.0 controller");
218
219         default:
220                 break;
221         }
222
223         if ((pci_get_class(self) == PCIC_SERIALBUS)
224             && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
225             && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) {
226                 return ("EHCI (generic) USB 2.0 controller");
227         }
228         return (NULL);                  /* dunno */
229 }
230
231 static int
232 ehci_pci_probe(device_t self)
233 {
234         const char *desc = ehci_pci_match(self);
235
236         if (desc) {
237                 device_set_desc(self, desc);
238                 return (0);
239         } else {
240                 return (ENXIO);
241         }
242 }
243
244 static void
245 ehci_pci_ati_quirk(device_t self, uint8_t is_sb700)
246 {
247         device_t smbdev;
248         uint32_t val;
249
250         if (is_sb700) {
251                 /* Lookup SMBUS PCI device */
252                 smbdev = pci_find_device(PCI_EHCI_VENDORID_ATI, 0x4385);
253                 if (smbdev == NULL)
254                         return;
255                 val = pci_get_revid(smbdev);
256                 if (val != 0x3a && val != 0x3b)
257                         return;
258         }
259
260         /*
261          * Note: this bit is described as reserved in SB700
262          * Register Reference Guide.
263          */
264         val = pci_read_config(self, 0x53, 1);
265         if (!(val & 0x8)) {
266                 val |= 0x8;
267                 pci_write_config(self, 0x53, val, 1);
268                 device_printf(self, "AMD SB600/700 quirk applied\n");
269         }
270 }
271
272 static void
273 ehci_pci_via_quirk(device_t self)
274 {
275         uint32_t val;
276
277         if ((pci_get_device(self) == 0x3104) && 
278             ((pci_get_revid(self) & 0xf0) == 0x60)) {
279                 /* Correct schedule sleep time to 10us */
280                 val = pci_read_config(self, 0x4b, 1);
281                 if (val & 0x20)
282                         return;
283                 pci_write_config(self, 0x4b, val, 1);
284                 device_printf(self, "VIA-quirk applied\n");
285         }
286 }
287
288 static int
289 ehci_pci_attach(device_t self)
290 {
291         ehci_softc_t *sc = device_get_softc(self);
292         int err;
293         int rid;
294
295         /* initialise some bus fields */
296         sc->sc_bus.parent = self;
297         sc->sc_bus.devices = sc->sc_devices;
298         sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
299
300         /* get all DMA memory */
301         if (usb_bus_mem_alloc_all(&sc->sc_bus,
302             USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
303                 return (ENOMEM);
304         }
305
306         pci_enable_busmaster(self);
307
308         switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
309         case PCI_USB_REV_PRE_1_0:
310         case PCI_USB_REV_1_0:
311         case PCI_USB_REV_1_1:
312                 /*
313                  * NOTE: some EHCI USB controllers have the wrong USB
314                  * revision number. It appears those controllers are
315                  * fully compliant so we just ignore this value in
316                  * some common cases.
317                  */
318                 device_printf(self, "pre-2.0 USB revision (ignored)\n");
319                 /* fallthrough */
320         case PCI_USB_REV_2_0:
321                 sc->sc_bus.usbrev = USB_REV_2_0;
322                 break;
323         default:
324                 /* Quirk for Parallels Desktop 4.0 */
325                 device_printf(self, "USB revision is unknown. Assuming v2.0.\n");
326                 sc->sc_bus.usbrev = USB_REV_2_0;
327                 break;
328         }
329
330         rid = PCI_CBMEM;
331         sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
332             RF_ACTIVE);
333         if (!sc->sc_io_res) {
334                 device_printf(self, "Could not map memory\n");
335                 goto error;
336         }
337         sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
338         sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
339         sc->sc_io_size = rman_get_size(sc->sc_io_res);
340
341         rid = 0;
342         sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
343             RF_SHAREABLE | RF_ACTIVE);
344         if (sc->sc_irq_res == NULL) {
345                 device_printf(self, "Could not allocate irq\n");
346                 goto error;
347         }
348         sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
349         if (!sc->sc_bus.bdev) {
350                 device_printf(self, "Could not add USB device\n");
351                 goto error;
352         }
353         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
354
355         /*
356          * ehci_pci_match will never return NULL if ehci_pci_probe
357          * succeeded
358          */
359         device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
360         switch (pci_get_vendor(self)) {
361         case PCI_EHCI_VENDORID_ACERLABS:
362                 sprintf(sc->sc_vendor, "AcerLabs");
363                 break;
364         case PCI_EHCI_VENDORID_AMD:
365                 sprintf(sc->sc_vendor, "AMD");
366                 break;
367         case PCI_EHCI_VENDORID_APPLE:
368                 sprintf(sc->sc_vendor, "Apple");
369                 break;
370         case PCI_EHCI_VENDORID_ATI:
371                 sprintf(sc->sc_vendor, "ATI");
372                 break;
373         case PCI_EHCI_VENDORID_CMDTECH:
374                 sprintf(sc->sc_vendor, "CMDTECH");
375                 break;
376         case PCI_EHCI_VENDORID_INTEL:
377                 sprintf(sc->sc_vendor, "Intel");
378                 break;
379         case PCI_EHCI_VENDORID_NEC:
380                 sprintf(sc->sc_vendor, "NEC");
381                 break;
382         case PCI_EHCI_VENDORID_OPTI:
383                 sprintf(sc->sc_vendor, "OPTi");
384                 break;
385         case PCI_EHCI_VENDORID_PHILIPS:
386                 sprintf(sc->sc_vendor, "Philips");
387                 break;
388         case PCI_EHCI_VENDORID_SIS:
389                 sprintf(sc->sc_vendor, "SiS");
390                 break;
391         case PCI_EHCI_VENDORID_NVIDIA:
392         case PCI_EHCI_VENDORID_NVIDIA2:
393                 sprintf(sc->sc_vendor, "nVidia");
394                 break;
395         case PCI_EHCI_VENDORID_VIA:
396                 sprintf(sc->sc_vendor, "VIA");
397                 break;
398         default:
399                 if (bootverbose)
400                         device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
401                             pci_get_devid(self));
402                 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
403         }
404
405 #if (__FreeBSD_version >= 700031)
406         err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
407             NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
408 #else
409         err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
410             (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
411 #endif
412         if (err) {
413                 device_printf(self, "Could not setup irq, %d\n", err);
414                 sc->sc_intr_hdl = NULL;
415                 goto error;
416         }
417         ehci_pci_takecontroller(self);
418
419         /* Undocumented quirks taken from Linux */
420
421         switch (pci_get_vendor(self)) {
422         case PCI_EHCI_VENDORID_ATI:
423                 /* SB600 and SB700 EHCI quirk */
424                 switch (pci_get_device(self)) {
425                 case 0x4386:
426                         ehci_pci_ati_quirk(self, 0);
427                         break;
428                 case 0x4396:
429                         ehci_pci_ati_quirk(self, 1);
430                         break;
431                 default:
432                         break;
433                 }
434                 break;
435
436         case PCI_EHCI_VENDORID_VIA:
437                 ehci_pci_via_quirk(self);
438                 break;
439
440         default:
441                 break;
442         }
443
444         /* Dropped interrupts workaround */
445         switch (pci_get_vendor(self)) {
446         case PCI_EHCI_VENDORID_ATI:
447         case PCI_EHCI_VENDORID_VIA:
448                 sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
449                 if (bootverbose)
450                         device_printf(self,
451                             "Dropped interrupts workaround enabled\n");
452                 break;
453         default:
454                 break;
455         }
456
457         err = ehci_init(sc);
458         if (!err) {
459                 err = device_probe_and_attach(sc->sc_bus.bdev);
460         }
461         if (err) {
462                 device_printf(self, "USB init failed err=%d\n", err);
463                 goto error;
464         }
465         return (0);
466
467 error:
468         ehci_pci_detach(self);
469         return (ENXIO);
470 }
471
472 static int
473 ehci_pci_detach(device_t self)
474 {
475         ehci_softc_t *sc = device_get_softc(self);
476         device_t bdev;
477
478         if (sc->sc_bus.bdev) {
479                 bdev = sc->sc_bus.bdev;
480                 device_detach(bdev);
481                 device_delete_child(self, bdev);
482         }
483         /* during module unload there are lots of children leftover */
484         device_delete_all_children(self);
485
486         pci_disable_busmaster(self);
487
488         /*
489          * disable interrupts that might have been switched on in ehci_init
490          */
491         if (sc->sc_io_res) {
492                 EWRITE4(sc, EHCI_USBINTR, 0);
493         }
494         if (sc->sc_irq_res && sc->sc_intr_hdl) {
495                 /*
496                  * only call ehci_detach() after ehci_init()
497                  */
498                 ehci_detach(sc);
499
500                 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
501
502                 if (err)
503                         /* XXX or should we panic? */
504                         device_printf(self, "Could not tear down irq, %d\n",
505                             err);
506                 sc->sc_intr_hdl = NULL;
507         }
508         if (sc->sc_irq_res) {
509                 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
510                 sc->sc_irq_res = NULL;
511         }
512         if (sc->sc_io_res) {
513                 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
514                     sc->sc_io_res);
515                 sc->sc_io_res = NULL;
516         }
517         usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
518
519         return (0);
520 }
521
522 static void
523 ehci_pci_takecontroller(device_t self)
524 {
525         ehci_softc_t *sc = device_get_softc(self);
526         uint32_t cparams;
527         uint32_t eec;
528         uint16_t to;
529         uint8_t eecp;
530         uint8_t bios_sem;
531
532         cparams = EREAD4(sc, EHCI_HCCPARAMS);
533
534         /* Synchronise with the BIOS if it owns the controller. */
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                 }
541                 bios_sem = pci_read_config(self, eecp +
542                     EHCI_LEGSUP_BIOS_SEM, 1);
543                 if (bios_sem == 0) {
544                         continue;
545                 }
546                 device_printf(sc->sc_bus.bdev, "waiting for BIOS "
547                     "to give up control\n");
548                 pci_write_config(self, eecp +
549                     EHCI_LEGSUP_OS_SEM, 1, 1);
550                 to = 500;
551                 while (1) {
552                         bios_sem = pci_read_config(self, eecp +
553                             EHCI_LEGSUP_BIOS_SEM, 1);
554                         if (bios_sem == 0)
555                                 break;
556
557                         if (--to == 0) {
558                                 device_printf(sc->sc_bus.bdev,
559                                     "timed out waiting for BIOS\n");
560                                 break;
561                         }
562                         usb_pause_mtx(NULL, hz / 100);  /* wait 10ms */
563                 }
564         }
565 }
566
567 static driver_t ehci_driver =
568 {
569         .name = "ehci",
570         .methods = (device_method_t[]){
571                 /* device interface */
572                 DEVMETHOD(device_probe, ehci_pci_probe),
573                 DEVMETHOD(device_attach, ehci_pci_attach),
574                 DEVMETHOD(device_detach, ehci_pci_detach),
575                 DEVMETHOD(device_suspend, ehci_pci_suspend),
576                 DEVMETHOD(device_resume, ehci_pci_resume),
577                 DEVMETHOD(device_shutdown, ehci_pci_shutdown),
578                 /* bus interface */
579                 DEVMETHOD(bus_print_child, bus_generic_print_child),
580
581                 {0, 0}
582         },
583         .size = sizeof(struct ehci_softc),
584 };
585
586 static devclass_t ehci_devclass;
587
588 DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
589 MODULE_DEPEND(ehci, usb, 1, 1, 1);