]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/usb/controller/uhci_pci.c
MFC r219048, r219004, r218475 and r204632.
[FreeBSD/stable/8.git] / sys / dev / usb / controller / uhci_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  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 /* Universal Host Controller Interface
35  *
36  * UHCI spec: http://www.intel.com/
37  */
38
39 /* The low level controller code for UHCI has been split into
40  * PCI probes and UHCI specific code. This was done to facilitate the
41  * sharing of code between *BSD's
42  */
43
44 #include <sys/stdint.h>
45 #include <sys/stddef.h>
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/bus.h>
52 #include <sys/linker_set.h>
53 #include <sys/module.h>
54 #include <sys/lock.h>
55 #include <sys/mutex.h>
56 #include <sys/condvar.h>
57 #include <sys/sysctl.h>
58 #include <sys/sx.h>
59 #include <sys/unistd.h>
60 #include <sys/callout.h>
61 #include <sys/malloc.h>
62 #include <sys/priv.h>
63
64 #include <dev/usb/usb.h>
65 #include <dev/usb/usbdi.h>
66
67 #include <dev/usb/usb_core.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_process.h>
70 #include <dev/usb/usb_util.h>
71 #include <dev/usb/usb_debug.h>
72
73 #include <dev/usb/usb_controller.h>
74 #include <dev/usb/usb_bus.h>
75 #include <dev/usb/usb_pci.h>
76 #include <dev/usb/controller/uhci.h>
77 #include <dev/usb/controller/uhcireg.h>
78
79 #define PCI_UHCI_VENDORID_INTEL         0x8086
80 #define PCI_UHCI_VENDORID_VIA           0x1106
81
82 /* PIIX4E has no separate stepping */
83
84 static device_probe_t uhci_pci_probe;
85 static device_attach_t uhci_pci_attach;
86 static device_detach_t uhci_pci_detach;
87 static device_suspend_t uhci_pci_suspend;
88 static device_resume_t uhci_pci_resume;
89
90 static int
91 uhci_pci_suspend(device_t self)
92 {
93         uhci_softc_t *sc = device_get_softc(self);
94         int err;
95
96         err = bus_generic_suspend(self);
97         if (err) {
98                 return (err);
99         }
100         uhci_suspend(sc);
101         return (0);
102 }
103
104 static int
105 uhci_pci_resume(device_t self)
106 {
107         uhci_softc_t *sc = device_get_softc(self);
108
109         pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
110
111         uhci_resume(sc);
112
113         bus_generic_resume(self);
114         return (0);
115 }
116
117 static const char *
118 uhci_pci_match(device_t self)
119 {
120         uint32_t device_id = pci_get_devid(self);
121
122         switch (device_id) {
123         case 0x26888086:
124                 return ("Intel 631XESB/632XESB/3100 USB controller USB-1");
125
126         case 0x26898086:
127                 return ("Intel 631XESB/632XESB/3100 USB controller USB-2");
128
129         case 0x268a8086:
130                 return ("Intel 631XESB/632XESB/3100 USB controller USB-3");
131
132         case 0x268b8086:
133                 return ("Intel 631XESB/632XESB/3100 USB controller USB-4");
134
135         case 0x70208086:
136                 return ("Intel 82371SB (PIIX3) USB controller");
137
138         case 0x71128086:
139                 return ("Intel 82371AB/EB (PIIX4) USB controller");
140
141         case 0x24128086:
142                 return ("Intel 82801AA (ICH) USB controller");
143
144         case 0x24228086:
145                 return ("Intel 82801AB (ICH0) USB controller");
146
147         case 0x24428086:
148                 return ("Intel 82801BA/BAM (ICH2) USB controller USB-A");
149
150         case 0x24448086:
151                 return ("Intel 82801BA/BAM (ICH2) USB controller USB-B");
152
153         case 0x24828086:
154                 return ("Intel 82801CA/CAM (ICH3) USB controller USB-A");
155
156         case 0x24848086:
157                 return ("Intel 82801CA/CAM (ICH3) USB controller USB-B");
158
159         case 0x24878086:
160                 return ("Intel 82801CA/CAM (ICH3) USB controller USB-C");
161
162         case 0x24c28086:
163                 return ("Intel 82801DB (ICH4) USB controller USB-A");
164
165         case 0x24c48086:
166                 return ("Intel 82801DB (ICH4) USB controller USB-B");
167
168         case 0x24c78086:
169                 return ("Intel 82801DB (ICH4) USB controller USB-C");
170
171         case 0x24d28086:
172                 return ("Intel 82801EB (ICH5) USB controller USB-A");
173
174         case 0x24d48086:
175                 return ("Intel 82801EB (ICH5) USB controller USB-B");
176
177         case 0x24d78086:
178                 return ("Intel 82801EB (ICH5) USB controller USB-C");
179
180         case 0x24de8086:
181                 return ("Intel 82801EB (ICH5) USB controller USB-D");
182
183         case 0x26588086:
184                 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-A");
185
186         case 0x26598086:
187                 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-B");
188
189         case 0x265a8086:
190                 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-C");
191
192         case 0x265b8086:
193                 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-D");
194
195         case 0x27c88086:
196                 return ("Intel 82801G (ICH7) USB controller USB-A");
197         case 0x27c98086:
198                 return ("Intel 82801G (ICH7) USB controller USB-B");
199         case 0x27ca8086:
200                 return ("Intel 82801G (ICH7) USB controller USB-C");
201         case 0x27cb8086:
202                 return ("Intel 82801G (ICH7) USB controller USB-D");
203
204         case 0x28308086:
205                 return ("Intel 82801H (ICH8) USB controller USB-A");
206         case 0x28318086:
207                 return ("Intel 82801H (ICH8) USB controller USB-B");
208         case 0x28328086:
209                 return ("Intel 82801H (ICH8) USB controller USB-C");
210         case 0x28348086:
211                 return ("Intel 82801H (ICH8) USB controller USB-D");
212         case 0x28358086:
213                 return ("Intel 82801H (ICH8) USB controller USB-E");
214         case 0x29348086:
215                 return ("Intel 82801I (ICH9) USB controller");
216         case 0x29358086:
217                 return ("Intel 82801I (ICH9) USB controller");
218         case 0x29368086:
219                 return ("Intel 82801I (ICH9) USB controller");
220         case 0x29378086:
221                 return ("Intel 82801I (ICH9) USB controller");
222         case 0x29388086:
223                 return ("Intel 82801I (ICH9) USB controller");
224         case 0x29398086:
225                 return ("Intel 82801I (ICH9) USB controller");
226         case 0x3a348086:
227                 return ("Intel 82801JI (ICH10) USB controller USB-A");
228         case 0x3a358086:
229                 return ("Intel 82801JI (ICH10) USB controller USB-B");
230         case 0x3a368086:
231                 return ("Intel 82801JI (ICH10) USB controller USB-C");
232         case 0x3a378086:
233                 return ("Intel 82801JI (ICH10) USB controller USB-D");
234         case 0x3a388086:
235                 return ("Intel 82801JI (ICH10) USB controller USB-E");
236         case 0x3a398086:
237                 return ("Intel 82801JI (ICH10) USB controller USB-F");
238
239         case 0x719a8086:
240                 return ("Intel 82443MX USB controller");
241
242         case 0x76028086:
243                 return ("Intel 82372FB/82468GX USB controller");
244
245         case 0x30381106:
246                 return ("VIA 83C572 USB controller");
247
248         default:
249                 break;
250         }
251
252         if ((pci_get_class(self) == PCIC_SERIALBUS) &&
253             (pci_get_subclass(self) == PCIS_SERIALBUS_USB) &&
254             (pci_get_progif(self) == PCI_INTERFACE_UHCI)) {
255                 return ("UHCI (generic) USB controller");
256         }
257         return (NULL);
258 }
259
260 static int
261 uhci_pci_probe(device_t self)
262 {
263         const char *desc = uhci_pci_match(self);
264
265         if (desc) {
266                 device_set_desc(self, desc);
267                 return (0);
268         } else {
269                 return (ENXIO);
270         }
271 }
272
273 static int
274 uhci_pci_attach(device_t self)
275 {
276         uhci_softc_t *sc = device_get_softc(self);
277         int rid;
278         int err;
279
280         /* initialise some bus fields */
281         sc->sc_bus.parent = self;
282         sc->sc_bus.devices = sc->sc_devices;
283         sc->sc_bus.devices_max = UHCI_MAX_DEVICES;
284
285         /* get all DMA memory */
286         if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
287             &uhci_iterate_hw_softc)) {
288                 return ENOMEM;
289         }
290         sc->sc_dev = self;
291
292         pci_enable_busmaster(self);
293
294         rid = PCI_UHCI_BASE_REG;
295         sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid,
296             RF_ACTIVE);
297         if (!sc->sc_io_res) {
298                 device_printf(self, "Could not map ports\n");
299                 goto error;
300         }
301         sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
302         sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
303         sc->sc_io_size = rman_get_size(sc->sc_io_res);
304
305         /* disable interrupts */
306         bus_space_write_2(sc->sc_io_tag, sc->sc_io_hdl, UHCI_INTR, 0);
307
308         rid = 0;
309         sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
310             RF_SHAREABLE | RF_ACTIVE);
311         if (sc->sc_irq_res == NULL) {
312                 device_printf(self, "Could not allocate irq\n");
313                 goto error;
314         }
315         sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
316         if (!sc->sc_bus.bdev) {
317                 device_printf(self, "Could not add USB device\n");
318                 goto error;
319         }
320         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
321
322         /*
323          * uhci_pci_match must never return NULL if uhci_pci_probe
324          * succeeded
325          */
326         device_set_desc(sc->sc_bus.bdev, uhci_pci_match(self));
327         switch (pci_get_vendor(self)) {
328         case PCI_UHCI_VENDORID_INTEL:
329                 sprintf(sc->sc_vendor, "Intel");
330                 break;
331         case PCI_UHCI_VENDORID_VIA:
332                 sprintf(sc->sc_vendor, "VIA");
333                 break;
334         default:
335                 if (bootverbose) {
336                         device_printf(self, "(New UHCI DeviceId=0x%08x)\n",
337                             pci_get_devid(self));
338                 }
339                 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
340         }
341
342         switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
343         case PCI_USB_REV_PRE_1_0:
344                 sc->sc_bus.usbrev = USB_REV_PRE_1_0;
345                 break;
346         case PCI_USB_REV_1_0:
347                 sc->sc_bus.usbrev = USB_REV_1_0;
348                 break;
349         default:
350                 /* Quirk for Parallels Desktop 4.0 */
351                 device_printf(self, "USB revision is unknown. Assuming v1.1.\n");
352                 sc->sc_bus.usbrev = USB_REV_1_1;
353                 break;
354         }
355
356 #if (__FreeBSD_version >= 700031)
357         err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
358             NULL, (driver_intr_t *)uhci_interrupt, sc, &sc->sc_intr_hdl);
359 #else
360         err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
361             (driver_intr_t *)uhci_interrupt, sc, &sc->sc_intr_hdl);
362 #endif
363
364         if (err) {
365                 device_printf(self, "Could not setup irq, %d\n", err);
366                 sc->sc_intr_hdl = NULL;
367                 goto error;
368         }
369         /*
370          * Set the PIRQD enable bit and switch off all the others. We don't
371          * want legacy support to interfere with us XXX Does this also mean
372          * that the BIOS won't touch the keyboard anymore if it is connected
373          * to the ports of the root hub?
374          */
375 #ifdef USB_DEBUG
376         if (pci_read_config(self, PCI_LEGSUP, 2) != PCI_LEGSUP_USBPIRQDEN) {
377                 device_printf(self, "LegSup = 0x%04x\n",
378                     pci_read_config(self, PCI_LEGSUP, 2));
379         }
380 #endif
381         pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
382
383         err = uhci_init(sc);
384         if (!err) {
385                 err = device_probe_and_attach(sc->sc_bus.bdev);
386         }
387         if (err) {
388                 device_printf(self, "USB init failed\n");
389                 goto error;
390         }
391         return (0);
392
393 error:
394         uhci_pci_detach(self);
395         return (ENXIO);
396 }
397
398 int
399 uhci_pci_detach(device_t self)
400 {
401         uhci_softc_t *sc = device_get_softc(self);
402         device_t bdev;
403
404         if (sc->sc_bus.bdev) {
405                 bdev = sc->sc_bus.bdev;
406                 device_detach(bdev);
407                 device_delete_child(self, bdev);
408         }
409         /* during module unload there are lots of children leftover */
410         device_delete_all_children(self);
411
412         /*
413          * disable interrupts that might have been switched on in
414          * uhci_init.
415          */
416         if (sc->sc_io_res) {
417                 USB_BUS_LOCK(&sc->sc_bus);
418
419                 /* stop the controller */
420                 uhci_reset(sc);
421
422                 USB_BUS_UNLOCK(&sc->sc_bus);
423         }
424         pci_disable_busmaster(self);
425
426         if (sc->sc_irq_res && sc->sc_intr_hdl) {
427                 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
428
429                 if (err) {
430                         /* XXX or should we panic? */
431                         device_printf(self, "Could not tear down irq, %d\n",
432                             err);
433                 }
434                 sc->sc_intr_hdl = NULL;
435         }
436         if (sc->sc_irq_res) {
437                 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
438                 sc->sc_irq_res = NULL;
439         }
440         if (sc->sc_io_res) {
441                 bus_release_resource(self, SYS_RES_IOPORT, PCI_UHCI_BASE_REG,
442                     sc->sc_io_res);
443                 sc->sc_io_res = NULL;
444         }
445         usb_bus_mem_free_all(&sc->sc_bus, &uhci_iterate_hw_softc);
446
447         return (0);
448 }
449
450 static driver_t uhci_driver =
451 {
452         .name = "uhci",
453         .methods = (device_method_t[]){
454                 /* device interface */
455                 DEVMETHOD(device_probe, uhci_pci_probe),
456                 DEVMETHOD(device_attach, uhci_pci_attach),
457                 DEVMETHOD(device_detach, uhci_pci_detach),
458
459                 DEVMETHOD(device_suspend, uhci_pci_suspend),
460                 DEVMETHOD(device_resume, uhci_pci_resume),
461                 DEVMETHOD(device_shutdown, bus_generic_shutdown),
462
463                 /* Bus interface */
464                 DEVMETHOD(bus_print_child, bus_generic_print_child),
465                 {0, 0}
466         },
467         .size = sizeof(struct uhci_softc),
468 };
469
470 static devclass_t uhci_devclass;
471
472 DRIVER_MODULE(uhci, pci, uhci_driver, uhci_devclass, 0, 0);
473 MODULE_DEPEND(uhci, usb, 1, 1, 1);