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