Merge branch 'vendor/MPFR'
[dragonfly.git] / sys / bus / usb / 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  * $FreeBSD: src/sys/dev/usb/uhci_pci.c,v 1.57 2005/03/01 07:50:11 imp Exp $
38  */
39
40 /* Universal Host Controller Interface
41  *
42  * UHCI spec: http://www.intel.com/
43  */
44
45 /* The low level controller code for UHCI has been split into
46  * PCI probes and UHCI specific code. This was done to facilitate the
47  * sharing of code between *BSD's
48  */
49
50 #include "opt_bus.h"
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/module.h>
56 #include <sys/bus.h>
57 #include <sys/queue.h>
58 #include <sys/rman.h>
59
60 #include <bus/pci/pcivar.h>
61 #include <bus/pci/pcireg.h>
62
63 #include <bus/usb/usb.h>
64 #include <bus/usb/usbdi.h>
65 #include <bus/usb/usbdivar.h>
66 #include <bus/usb/usb_mem.h>
67
68 #include <bus/usb/uhcireg.h>
69 #include <bus/usb/uhcivar.h>
70
71 #define PCI_UHCI_VENDORID_INTEL         0x8086
72 #define PCI_UHCI_VENDORID_VIA           0x1106
73
74 #define PCI_UHCI_DEVICEID_PIIX3         0x70208086
75 static const char *uhci_device_piix3 = "Intel 82371SB (PIIX3) USB controller";
76
77 #define PCI_UHCI_DEVICEID_PIIX4         0x71128086
78 #define PCI_UHCI_DEVICEID_PIIX4E        0x71128086      /* no separate stepping */
79 static const char *uhci_device_piix4 = "Intel 82371AB/EB (PIIX4) USB controller";
80
81 #define PCI_UHCI_DEVICEID_ICH           0x24128086
82 static const char *uhci_device_ich = "Intel 82801AA (ICH) USB controller";
83
84 #define PCI_UHCI_DEVICEID_ICH0          0x24228086
85 static const char *uhci_device_ich0 = "Intel 82801AB (ICH0) USB controller";
86
87 #define PCI_UHCI_DEVICEID_ICH2_A        0x24428086
88 static const char *uhci_device_ich2_a = "Intel 82801BA/BAM (ICH2) USB controller USB-A";
89
90 #define PCI_UHCI_DEVICEID_ICH2_B        0x24448086
91 static const char *uhci_device_ich2_b = "Intel 82801BA/BAM (ICH2) USB controller USB-B";
92
93 #define PCI_UHCI_DEVICEID_ICH3_A        0x24828086
94 static const char *uhci_device_ich3_a = "Intel 82801CA/CAM (ICH3) USB controller USB-A";
95
96 #define PCI_UHCI_DEVICEID_ICH3_B        0x24848086
97 static const char *uhci_device_ich3_b = "Intel 82801CA/CAM (ICH3) USB controller USB-B";
98
99 #define PCI_UHCI_DEVICEID_ICH3_C        0x24878086
100 static const char *uhci_device_ich3_c = "Intel 82801CA/CAM (ICH3) USB controller USB-C";
101
102 #define PCI_UHCI_DEVICEID_ICH4_A        0x24c28086
103 static const char *uhci_device_ich4_a = "Intel 82801DB (ICH4) USB controller USB-A";
104
105 #define PCI_UHCI_DEVICEID_ICH4_B        0x24c48086
106 static const char *uhci_device_ich4_b = "Intel 82801DB (ICH4) USB controller USB-B";
107
108 #define PCI_UHCI_DEVICEID_ICH4_C        0x24c78086
109 static const char *uhci_device_ich4_c = "Intel 82801DB (ICH4) USB controller USB-C";
110
111 #define PCI_UHCI_DEVICEID_ICH5_A        0x24d28086
112 static const char *uhci_device_ich5_a = "Intel 82801EB (ICH5) USB controller USB-A";
113
114 #define PCI_UHCI_DEVICEID_ICH5_B        0x24d48086
115 static const char *uhci_device_ich5_b = "Intel 82801EB (ICH5) USB controller USB-B";
116
117 #define PCI_UHCI_DEVICEID_ICH5_C        0x24d78086
118 static const char *uhci_device_ich5_c = "Intel 82801EB (ICH5) USB controller USB-C";
119
120 #define PCI_UHCI_DEVICEID_ICH5_D        0x24de8086
121 static const char *uhci_device_ich5_d = "Intel 82801EB (ICH5) USB controller USB-D";
122
123 #define PCI_UHCI_DEVICEID_ICH6_A        0x26588086
124 static const char *uhci_device_ich6_a = "Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-A";
125
126 #define PCI_UHCI_DEVICEID_ICH6_B        0x26598086
127 static const char *uhci_device_ich6_b = "Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-B";
128
129 #define PCI_UHCI_DEVICEID_ICH6_C        0x265a8086
130 static const char *uhci_device_ich6_c = "Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-C";
131
132 #define PCI_UHCI_DEVICEID_ICH6_D        0x265b8086
133 static const char *uhci_device_ich6_d = "Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-D";
134
135 #define PCI_UHCI_DEVICEID_63XXESB_1     0x26888086
136 static const char *uhci_device_esb_1 = "Intel 631XESB/632XESB/3100 USB controller USB-1";
137
138 #define PCI_UHCI_DEVICEID_63XXESB_2     0x26898086
139 static const char *uhci_device_esb_2 = "Intel 631XESB/632XESB/3100 USB controller USB-2";
140
141 #define PCI_UHCI_DEVICEID_63XXESB_3     0x268a8086
142 static const char *uhci_device_esb_3 = "Intel 631XESB/632XESB/3100 USB controller USB-3";
143
144 #define PCI_UHCI_DEVICEID_63XXESB_4     0x268b8086
145 static const char *uhci_device_esb_4 = "Intel 631XESB/632XESB/3100 USB controller USB-4";
146
147 #define PCI_UHCI_DEVICEID_ICH7_A        0x27c88086
148 #define PCI_UHCI_DEVICEID_ICH7_B        0x27c98086
149 #define PCI_UHCI_DEVICEID_ICH7_C        0x27ca8086
150 #define PCI_UHCI_DEVICEID_ICH7_D        0x27cb8086
151 static const char *uhci_device_ich7 = "Intel 82801G (ICH7) USB controller";
152
153 #define PCI_UHCI_DEVICEID_ICH8_A        0x28308086
154 static const char *uhci_device_ich8_a = "Intel 82801H (ICH8) USB controller USB-A";
155
156 #define PCI_UHCI_DEVICEID_ICH8_B        0x28318086
157 static const char *uhci_device_ich8_b = "Intel 82801H (ICH8) USB controller USB-B";
158
159 #define PCI_UHCI_DEVICEID_ICH8_C        0x28328086
160 static const char *uhci_device_ich8_c = "Intel 82801H (ICH8) USB controller USB-C";
161
162 #define PCI_UHCI_DEVICEID_ICH8_D        0x28348086
163 static const char *uhci_device_ich8_d = "Intel 82801H (ICH8) USB controller USB-D";
164
165 #define PCI_UHCI_DEVICEID_ICH8_E        0x28358086
166 static const char *uhci_device_ich8_e = "Intel 82801H (ICH8) USB controller USB-E";
167
168 #define PCI_UHCI_DEVICEID_ICH9_A        0x29348086
169 #define PCI_UHCI_DEVICEID_ICH9_B        0x29358086
170 #define PCI_UHCI_DEVICEID_ICH9_C        0x29368086
171 #define PCI_UHCI_DEVICEID_ICH9_D        0x29378086
172 #define PCI_UHCI_DEVICEID_ICH9_E        0x29388086
173 #define PCI_UHCI_DEVICEID_ICH9_F        0x29398086
174 static const char *uhci_device_ich9 = "Intel 82801I (ICH9) USB controller";
175
176 #define PCI_UHCI_DEVICEID_440MX         0x719a8086
177 static const char *uhci_device_440mx = "Intel 82443MX USB controller";
178
179 #define PCI_UHCI_DEVICEID_460GX         0x76028086
180 static const char *uhci_device_460gx = "Intel 82372FB/82468GX USB controller";
181
182 #define PCI_UHCI_DEVICEID_VT83C572      0x30381106
183 static const char *uhci_device_vt83c572 = "VIA 83C572 USB controller";
184
185 static const char *uhci_device_generic = "UHCI (generic) USB controller";
186
187 #define PCI_UHCI_BASE_REG               0x20
188
189
190 static int uhci_pci_attach(device_t self);
191 static int uhci_pci_detach(device_t self);
192 static int uhci_pci_suspend(device_t self);
193 static int uhci_pci_resume(device_t self);
194
195
196 static int
197 uhci_pci_suspend(device_t self)
198 {
199         uhci_softc_t *sc = device_get_softc(self);
200         int err;
201
202         err = bus_generic_suspend(self);
203         if (err)
204                 return err;
205         uhci_power(PWR_SUSPEND, sc);
206
207         return 0;
208 }
209
210 static int
211 uhci_pci_resume(device_t self)
212 {
213         uhci_softc_t *sc = device_get_softc(self);
214
215         pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
216
217         uhci_power(PWR_RESUME, sc);
218         bus_generic_resume(self);
219
220         return 0;
221 }
222
223 static const char *
224 uhci_pci_match(device_t self)
225 {
226         u_int32_t device_id = pci_get_devid(self);
227
228         if (device_id == PCI_UHCI_DEVICEID_PIIX3) {
229                 return (uhci_device_piix3);
230         } else if (device_id == PCI_UHCI_DEVICEID_PIIX4) {
231                 return (uhci_device_piix4);
232         } else if (device_id == PCI_UHCI_DEVICEID_ICH) {
233                 return (uhci_device_ich);
234         } else if (device_id == PCI_UHCI_DEVICEID_ICH0) {
235                 return (uhci_device_ich0);
236         } else if (device_id == PCI_UHCI_DEVICEID_ICH2_A) {
237                 return (uhci_device_ich2_a);
238         } else if (device_id == PCI_UHCI_DEVICEID_ICH2_B) {
239                 return (uhci_device_ich2_b);
240         } else if (device_id == PCI_UHCI_DEVICEID_ICH3_A) {
241                 return (uhci_device_ich3_a);
242         } else if (device_id == PCI_UHCI_DEVICEID_ICH3_B) {
243                 return (uhci_device_ich3_b);
244         } else if (device_id == PCI_UHCI_DEVICEID_ICH3_C) {
245                 return (uhci_device_ich3_c);
246         } else if (device_id == PCI_UHCI_DEVICEID_ICH4_A) {
247                 return (uhci_device_ich4_a);
248         } else if (device_id == PCI_UHCI_DEVICEID_ICH4_B) {
249                 return (uhci_device_ich4_b);
250         } else if (device_id == PCI_UHCI_DEVICEID_ICH4_C) {
251                 return (uhci_device_ich4_c);
252         } else if (device_id == PCI_UHCI_DEVICEID_ICH5_A) {
253                 return (uhci_device_ich5_a);
254         } else if (device_id == PCI_UHCI_DEVICEID_ICH5_B) {
255                 return (uhci_device_ich5_b);
256         } else if (device_id == PCI_UHCI_DEVICEID_ICH5_C) {
257                 return (uhci_device_ich5_c);
258         } else if (device_id == PCI_UHCI_DEVICEID_ICH5_D) {
259                 return (uhci_device_ich5_d);
260         } else if (device_id == PCI_UHCI_DEVICEID_ICH6_A) {
261                 return (uhci_device_ich6_a);
262         } else if (device_id == PCI_UHCI_DEVICEID_ICH6_B) {
263                 return (uhci_device_ich6_b);
264         } else if (device_id == PCI_UHCI_DEVICEID_ICH6_C) {
265                 return (uhci_device_ich6_c);
266         } else if (device_id == PCI_UHCI_DEVICEID_ICH6_D) {
267                 return (uhci_device_ich6_d);
268         } else if (device_id == PCI_UHCI_DEVICEID_63XXESB_1) {
269                 return (uhci_device_esb_1);
270         } else if (device_id == PCI_UHCI_DEVICEID_63XXESB_2) {
271                 return (uhci_device_esb_2);
272         } else if (device_id == PCI_UHCI_DEVICEID_63XXESB_3) {
273                 return (uhci_device_esb_3);
274         } else if (device_id == PCI_UHCI_DEVICEID_63XXESB_4) {
275                 return (uhci_device_esb_4);
276         } else if (device_id == PCI_UHCI_DEVICEID_ICH7_A) {
277                 return (uhci_device_ich7);
278         } else if (device_id == PCI_UHCI_DEVICEID_ICH7_B) {
279                 return (uhci_device_ich7);
280         } else if (device_id == PCI_UHCI_DEVICEID_ICH7_C) {
281                 return (uhci_device_ich7);
282         } else if (device_id == PCI_UHCI_DEVICEID_ICH7_D) {
283                 return (uhci_device_ich7);
284         } else if (device_id == PCI_UHCI_DEVICEID_ICH8_A) {
285                 return (uhci_device_ich8_a);
286         } else if (device_id == PCI_UHCI_DEVICEID_ICH8_B) {
287                 return (uhci_device_ich8_b);
288         } else if (device_id == PCI_UHCI_DEVICEID_ICH8_C) {
289                 return (uhci_device_ich8_c);
290         } else if (device_id == PCI_UHCI_DEVICEID_ICH8_D) {
291                 return (uhci_device_ich8_d);
292         } else if (device_id == PCI_UHCI_DEVICEID_ICH8_E) {
293                 return (uhci_device_ich8_e);
294         } else if (device_id == PCI_UHCI_DEVICEID_ICH9_A) {
295                 return (uhci_device_ich9);
296         } else if (device_id == PCI_UHCI_DEVICEID_ICH9_B) {
297                 return (uhci_device_ich9);
298         } else if (device_id == PCI_UHCI_DEVICEID_ICH9_C) {
299                 return (uhci_device_ich9);
300         } else if (device_id == PCI_UHCI_DEVICEID_ICH9_D) {
301                 return (uhci_device_ich9);
302         } else if (device_id == PCI_UHCI_DEVICEID_ICH9_E) {
303                 return (uhci_device_ich9);
304         } else if (device_id == PCI_UHCI_DEVICEID_ICH9_F) {
305                 return (uhci_device_ich9);
306         } else if (device_id == PCI_UHCI_DEVICEID_440MX) {
307                 return (uhci_device_440mx);
308         } else if (device_id == PCI_UHCI_DEVICEID_460GX) {
309                 return (uhci_device_460gx);
310         } else if (device_id == PCI_UHCI_DEVICEID_VT83C572) {
311                 return (uhci_device_vt83c572);
312         } else {
313                 if (pci_get_class(self) == PCIC_SERIALBUS
314                     && pci_get_subclass(self) == PCIS_SERIALBUS_USB
315                     && pci_get_progif(self) == PCI_INTERFACE_UHCI) {
316                         return (uhci_device_generic);
317                 }
318         }
319
320         return NULL;            /* dunno... */
321 }
322
323 static int
324 uhci_pci_probe(device_t self)
325 {
326         const char *desc = uhci_pci_match(self);
327
328         if (desc) {
329                 device_set_desc(self, desc);
330                 return 0;
331         } else {
332                 return ENXIO;
333         }
334 }
335
336 static int
337 uhci_pci_attach(device_t self)
338 {
339         uhci_softc_t *sc = device_get_softc(self);
340         int rid;
341         int err;
342
343         pci_enable_busmaster(self);
344
345         rid = PCI_UHCI_BASE_REG;
346         sc->io_res = bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid,
347             RF_ACTIVE);
348         if (!sc->io_res) {
349                 device_printf(self, "Could not map ports\n");
350                 return ENXIO;
351         }
352         sc->iot = rman_get_bustag(sc->io_res);
353         sc->ioh = rman_get_bushandle(sc->io_res);
354
355         /* disable interrupts */
356         bus_space_write_2(sc->iot, sc->ioh, UHCI_INTR, 0);
357
358         rid = 0;
359         sc->irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
360             RF_SHAREABLE | RF_ACTIVE);
361         if (sc->irq_res == NULL) {
362                 device_printf(self, "Could not allocate irq\n");
363                 uhci_pci_detach(self);
364                 return ENXIO;
365         }
366         sc->sc_bus.bdev = device_add_child(self, "usb", -1);
367         if (!sc->sc_bus.bdev) {
368                 device_printf(self, "Could not add USB device\n");
369                 uhci_pci_detach(self);
370                 return ENOMEM;
371         }
372         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
373
374         /* uhci_pci_match must never return NULL if uhci_pci_probe succeeded */
375         device_set_desc(sc->sc_bus.bdev, uhci_pci_match(self));
376         switch (pci_get_vendor(self)) {
377         case PCI_UHCI_VENDORID_INTEL:
378                 ksprintf(sc->sc_vendor, "Intel");
379                 break;
380         case PCI_UHCI_VENDORID_VIA:
381                 ksprintf(sc->sc_vendor, "VIA");
382                 break;
383         default:
384                 if (bootverbose)
385                         device_printf(self, "(New UHCI DeviceId=0x%08x)\n",
386                             pci_get_devid(self));
387                 ksprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
388         }
389
390         switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USBREV_MASK) {
391         case PCI_USBREV_PRE_1_0:
392                 sc->sc_bus.usbrev = USBREV_PRE_1_0;
393                 break;
394         case PCI_USBREV_1_0:
395                 sc->sc_bus.usbrev = USBREV_1_0;
396                 break;
397         default:
398                 sc->sc_bus.usbrev = USBREV_UNKNOWN;
399                 break;
400         }
401
402         err = bus_setup_intr(self, sc->irq_res, 0,
403             (driver_intr_t *) uhci_intr, sc, &sc->ih, NULL);
404         if (err) {
405                 device_printf(self, "Could not setup irq, %d\n", err);
406                 sc->ih = NULL;
407                 uhci_pci_detach(self);
408                 return ENXIO;
409         }
410         /*
411          * Set the PIRQD enable bit and switch off all the others. We don't
412          * want legacy support to interfere with us XXX Does this also mean
413          * that the BIOS won't touch the keyboard anymore if it is connected
414          * to the ports of the root hub?
415          */
416 #ifdef USB_DEBUG
417         if (pci_read_config(self, PCI_LEGSUP, 2) != PCI_LEGSUP_USBPIRQDEN)
418                 device_printf(self, "LegSup = 0x%04x\n",
419                     pci_read_config(self, PCI_LEGSUP, 2));
420 #endif
421         pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
422
423         err = uhci_init(sc);
424         if (err == 0)
425                 err = device_probe_and_attach(sc->sc_bus.bdev);
426
427         if (err) {
428                 device_printf(self, "USB init failed\n");
429                 uhci_pci_detach(self);
430                 return EIO;
431         }
432         return 0;               /* success */
433 }
434
435 int
436 uhci_pci_detach(device_t self)
437 {
438         uhci_softc_t *sc = device_get_softc(self);
439
440         if (sc->sc_flags & UHCI_SCFLG_DONEINIT) {
441                 uhci_detach(sc, 0);
442                 sc->sc_flags &= ~UHCI_SCFLG_DONEINIT;
443         }
444
445
446         if (sc->irq_res && sc->ih) {
447                 int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
448
449                 if (err)
450                         /* XXX or should we panic? */
451                         device_printf(self, "Could not tear down irq, %d\n",
452                             err);
453                 sc->ih = NULL;
454         }
455         if (sc->sc_bus.bdev) {
456                 device_delete_child(self, sc->sc_bus.bdev);
457                 sc->sc_bus.bdev = NULL;
458         }
459         if (sc->irq_res) {
460                 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
461                 sc->irq_res = NULL;
462         }
463         if (sc->io_res) {
464                 bus_release_resource(self, SYS_RES_IOPORT, PCI_UHCI_BASE_REG,
465                     sc->io_res);
466                 sc->io_res = NULL;
467                 sc->iot = 0;
468                 sc->ioh = 0;
469         }
470         return 0;
471 }
472
473
474 static device_method_t uhci_methods[] = {
475         /* Device interface */
476         DEVMETHOD(device_probe, uhci_pci_probe),
477         DEVMETHOD(device_attach, uhci_pci_attach),
478         DEVMETHOD(device_detach, uhci_pci_detach),
479         DEVMETHOD(device_suspend, uhci_pci_suspend),
480         DEVMETHOD(device_resume, uhci_pci_resume),
481         DEVMETHOD(device_shutdown, bus_generic_shutdown),
482
483         /* Bus interface */
484         DEVMETHOD(bus_print_child, bus_generic_print_child),
485
486         {0, 0}
487 };
488
489 static driver_t uhci_driver = {
490         "uhci",
491         uhci_methods,
492         sizeof(uhci_softc_t),
493 };
494
495 static devclass_t uhci_devclass;
496
497 DRIVER_MODULE(uhci, pci, uhci_driver, uhci_devclass, NULL, NULL);
498 DRIVER_MODULE(uhci, cardbus, uhci_driver, uhci_devclass, NULL, NULL);