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