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