Merge from vendor branch FILE:
[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.11 2007/06/27 12:27:59 hasso 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/bus.h>
60 #include <sys/rman.h>
61
62 #include <bus/pci/pcivar.h>
63 #include <bus/pci/pcireg.h>
64
65 #include <bus/usb/usb.h>
66 #include <bus/usb/usbdi.h>
67 #include <bus/usb/usbdivar.h>
68 #include <bus/usb/usb_mem.h>
69
70 #include <bus/usb/uhcireg.h>
71 #include <bus/usb/uhcivar.h>
72
73 #define PCI_UHCI_VENDORID_INTEL         0x8086
74 #define PCI_UHCI_VENDORID_VIA           0x1106
75
76 #define PCI_UHCI_DEVICEID_PIIX3         0x70208086
77 static const char *uhci_device_piix3 = "Intel 82371SB (PIIX3) USB controller";
78
79 #define PCI_UHCI_DEVICEID_PIIX4         0x71128086
80 #define PCI_UHCI_DEVICEID_PIIX4E        0x71128086      /* no separate stepping */
81 static const char *uhci_device_piix4 = "Intel 82371AB/EB (PIIX4) USB controller";
82
83 #define PCI_UHCI_DEVICEID_ICH           0x24128086
84 static const char *uhci_device_ich = "Intel 82801AA (ICH) USB controller";
85
86 #define PCI_UHCI_DEVICEID_ICH0          0x24228086
87 static const char *uhci_device_ich0 = "Intel 82801AB (ICH0) USB controller";
88
89 #define PCI_UHCI_DEVICEID_ICH2_A        0x24428086
90 static const char *uhci_device_ich2_a = "Intel 82801BA/BAM (ICH2) USB controller USB-A";
91
92 #define PCI_UHCI_DEVICEID_ICH2_B        0x24448086
93 static const char *uhci_device_ich2_b = "Intel 82801BA/BAM (ICH2) USB controller USB-B";
94
95 #define PCI_UHCI_DEVICEID_ICH3_A        0x24828086
96 static const char *uhci_device_ich3_a = "Intel 82801CA/CAM (ICH3) USB controller USB-A";
97
98 #define PCI_UHCI_DEVICEID_ICH3_B        0x24848086
99 static const char *uhci_device_ich3_b = "Intel 82801CA/CAM (ICH3) USB controller USB-B";
100
101 #define PCI_UHCI_DEVICEID_ICH3_C        0x24878086
102 static const char *uhci_device_ich3_c = "Intel 82801CA/CAM (ICH3) USB controller USB-C";
103
104 #define PCI_UHCI_DEVICEID_ICH4_A        0x24c28086
105 static const char *uhci_device_ich4_a = "Intel 82801DB (ICH4) USB controller USB-A";
106
107 #define PCI_UHCI_DEVICEID_ICH4_B        0x24c48086
108 static const char *uhci_device_ich4_b = "Intel 82801DB (ICH4) USB controller USB-B";
109
110 #define PCI_UHCI_DEVICEID_ICH4_C        0x24c78086
111 static const char *uhci_device_ich4_c = "Intel 82801DB (ICH4) USB controller USB-C";
112
113 #define PCI_UHCI_DEVICEID_ICH5_A        0x24d28086
114 static const char *uhci_device_ich5_a = "Intel 82801EB (ICH5) USB controller USB-A";
115
116 #define PCI_UHCI_DEVICEID_ICH5_B        0x24d48086
117 static const char *uhci_device_ich5_b = "Intel 82801EB (ICH5) USB controller USB-B";
118
119 #define PCI_UHCI_DEVICEID_ICH5_C        0x24d78086
120 static const char *uhci_device_ich5_c = "Intel 82801EB (ICH5) USB controller USB-C";
121
122 #define PCI_UHCI_DEVICEID_ICH5_D        0x24de8086
123 static const char *uhci_device_ich5_d = "Intel 82801EB (ICH5) USB controller USB-D";
124
125 #define PCI_UHCI_DEVICEID_ICH6_A        0x26588086
126 static const char *uhci_device_ich6_a = "Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-A";
127
128 #define PCI_UHCI_DEVICEID_ICH6_B        0x26598086
129 static const char *uhci_device_ich6_b = "Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-B";
130
131 #define PCI_UHCI_DEVICEID_ICH6_C        0x265a8086
132 static const char *uhci_device_ich6_c = "Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-C";
133
134 #define PCI_UHCI_DEVICEID_ICH6_D        0x265b8086
135 static const char *uhci_device_ich6_d = "Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-D";
136
137 #define PCI_UHCI_DEVICEID_440MX         0x719a8086
138 static const char *uhci_device_440mx = "Intel 82443MX USB controller";
139
140 #define PCI_UHCI_DEVICEID_460GX         0x76028086
141 static const char *uhci_device_460gx = "Intel 82372FB/82468GX USB controller";
142
143 #define PCI_UHCI_DEVICEID_VT83C572      0x30381106
144 static const char *uhci_device_vt83c572 = "VIA 83C572 USB controller";
145
146 static const char *uhci_device_generic = "UHCI (generic) USB controller";
147
148 #define PCI_UHCI_BASE_REG               0x20
149
150
151 static int uhci_pci_attach(device_t self);
152 static int uhci_pci_detach(device_t self);
153 static int uhci_pci_suspend(device_t self);
154 static int uhci_pci_resume(device_t self);
155
156
157 static int
158 uhci_pci_suspend(device_t self)
159 {
160         uhci_softc_t *sc = device_get_softc(self);
161         int err;
162
163         err = bus_generic_suspend(self);
164         if (err)
165                 return err;
166         uhci_power(PWR_SUSPEND, sc);
167
168         return 0;
169 }
170
171 static int
172 uhci_pci_resume(device_t self)
173 {
174         uhci_softc_t *sc = device_get_softc(self);
175
176         pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
177
178         uhci_power(PWR_RESUME, sc);
179         bus_generic_resume(self);
180
181         return 0;
182 }
183
184 static const char *
185 uhci_pci_match(device_t self)
186 {
187         u_int32_t device_id = pci_get_devid(self);
188
189         if (device_id == PCI_UHCI_DEVICEID_PIIX3) {
190                 return (uhci_device_piix3);
191         } else if (device_id == PCI_UHCI_DEVICEID_PIIX4) {
192                 return (uhci_device_piix4);
193         } else if (device_id == PCI_UHCI_DEVICEID_ICH) {
194                 return (uhci_device_ich);
195         } else if (device_id == PCI_UHCI_DEVICEID_ICH0) {
196                 return (uhci_device_ich0);
197         } else if (device_id == PCI_UHCI_DEVICEID_ICH2_A) {
198                 return (uhci_device_ich2_a);
199         } else if (device_id == PCI_UHCI_DEVICEID_ICH2_B) {
200                 return (uhci_device_ich2_b);
201         } else if (device_id == PCI_UHCI_DEVICEID_ICH3_A) {
202                 return (uhci_device_ich3_a);
203         } else if (device_id == PCI_UHCI_DEVICEID_ICH3_B) {
204                 return (uhci_device_ich3_b);
205         } else if (device_id == PCI_UHCI_DEVICEID_ICH3_C) {
206                 return (uhci_device_ich3_c);
207         } else if (device_id == PCI_UHCI_DEVICEID_ICH4_A) {
208                 return (uhci_device_ich4_a);
209         } else if (device_id == PCI_UHCI_DEVICEID_ICH4_B) {
210                 return (uhci_device_ich4_b);
211         } else if (device_id == PCI_UHCI_DEVICEID_ICH4_C) {
212                 return (uhci_device_ich4_c);
213         } else if (device_id == PCI_UHCI_DEVICEID_ICH5_A) {
214                 return (uhci_device_ich5_a);
215         } else if (device_id == PCI_UHCI_DEVICEID_ICH5_B) {
216                 return (uhci_device_ich5_b);
217         } else if (device_id == PCI_UHCI_DEVICEID_ICH5_C) {
218                 return (uhci_device_ich5_c);
219         } else if (device_id == PCI_UHCI_DEVICEID_ICH5_D) {
220                 return (uhci_device_ich5_d);
221         } else if (device_id == PCI_UHCI_DEVICEID_ICH6_A) {
222                 return (uhci_device_ich6_a);
223         } else if (device_id == PCI_UHCI_DEVICEID_ICH6_B) {
224                 return (uhci_device_ich6_b);
225         } else if (device_id == PCI_UHCI_DEVICEID_ICH6_C) {
226                 return (uhci_device_ich6_c);
227         } else if (device_id == PCI_UHCI_DEVICEID_ICH6_D) {
228                 return (uhci_device_ich6_d);
229         } else if (device_id == PCI_UHCI_DEVICEID_440MX) {
230                 return (uhci_device_440mx);
231         } else if (device_id == PCI_UHCI_DEVICEID_460GX) {
232                 return (uhci_device_460gx);
233         } else if (device_id == PCI_UHCI_DEVICEID_VT83C572) {
234                 return (uhci_device_vt83c572);
235         } else {
236                 if (pci_get_class(self) == PCIC_SERIALBUS
237                     && pci_get_subclass(self) == PCIS_SERIALBUS_USB
238                     && pci_get_progif(self) == PCI_INTERFACE_UHCI) {
239                         return (uhci_device_generic);
240                 }
241         }
242
243         return NULL;            /* dunno... */
244 }
245
246 static int
247 uhci_pci_probe(device_t self)
248 {
249         const char *desc = uhci_pci_match(self);
250
251         if (desc) {
252                 device_set_desc(self, desc);
253                 return 0;
254         } else {
255                 return ENXIO;
256         }
257 }
258
259 static int
260 uhci_pci_attach(device_t self)
261 {
262         uhci_softc_t *sc = device_get_softc(self);
263         int rid;
264         int err;
265
266         pci_enable_busmaster(self);
267
268         rid = PCI_UHCI_BASE_REG;
269         sc->io_res = bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid,
270             RF_ACTIVE);
271         if (!sc->io_res) {
272                 device_printf(self, "Could not map ports\n");
273                 return ENXIO;
274         }
275         sc->iot = rman_get_bustag(sc->io_res);
276         sc->ioh = rman_get_bushandle(sc->io_res);
277
278         /* disable interrupts */
279         bus_space_write_2(sc->iot, sc->ioh, UHCI_INTR, 0);
280
281         rid = 0;
282         sc->irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
283             RF_SHAREABLE | RF_ACTIVE);
284         if (sc->irq_res == NULL) {
285                 device_printf(self, "Could not allocate irq\n");
286                 uhci_pci_detach(self);
287                 return ENXIO;
288         }
289         sc->sc_bus.bdev = device_add_child(self, "usb", -1);
290         if (!sc->sc_bus.bdev) {
291                 device_printf(self, "Could not add USB device\n");
292                 uhci_pci_detach(self);
293                 return ENOMEM;
294         }
295         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
296
297         /* uhci_pci_match must never return NULL if uhci_pci_probe succeeded */
298         device_set_desc(sc->sc_bus.bdev, uhci_pci_match(self));
299         switch (pci_get_vendor(self)) {
300         case PCI_UHCI_VENDORID_INTEL:
301                 ksprintf(sc->sc_vendor, "Intel");
302                 break;
303         case PCI_UHCI_VENDORID_VIA:
304                 ksprintf(sc->sc_vendor, "VIA");
305                 break;
306         default:
307                 if (bootverbose)
308                         device_printf(self, "(New UHCI DeviceId=0x%08x)\n",
309                             pci_get_devid(self));
310                 ksprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
311         }
312
313         switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USBREV_MASK) {
314         case PCI_USBREV_PRE_1_0:
315                 sc->sc_bus.usbrev = USBREV_PRE_1_0;
316                 break;
317         case PCI_USBREV_1_0:
318                 sc->sc_bus.usbrev = USBREV_1_0;
319                 break;
320         default:
321                 sc->sc_bus.usbrev = USBREV_UNKNOWN;
322                 break;
323         }
324
325         err = bus_setup_intr(self, sc->irq_res, 0,
326             (driver_intr_t *) uhci_intr, sc, &sc->ih, NULL);
327         if (err) {
328                 device_printf(self, "Could not setup irq, %d\n", err);
329                 sc->ih = NULL;
330                 uhci_pci_detach(self);
331                 return ENXIO;
332         }
333         /*
334          * Set the PIRQD enable bit and switch off all the others. We don't
335          * want legacy support to interfere with us XXX Does this also mean
336          * that the BIOS won't touch the keyboard anymore if it is connected
337          * to the ports of the root hub?
338          */
339 #ifdef USB_DEBUG
340         if (pci_read_config(self, PCI_LEGSUP, 2) != PCI_LEGSUP_USBPIRQDEN)
341                 device_printf(self, "LegSup = 0x%04x\n",
342                     pci_read_config(self, PCI_LEGSUP, 2));
343 #endif
344         pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
345
346         err = uhci_init(sc);
347         if (!err) {
348                 sc->sc_flags |= UHCI_SCFLG_DONEINIT;
349                 err = device_probe_and_attach(sc->sc_bus.bdev);
350         }
351
352         if (err) {
353                 device_printf(self, "USB init failed\n");
354                 uhci_pci_detach(self);
355                 return EIO;
356         }
357         return 0;               /* success */
358 }
359
360 int
361 uhci_pci_detach(device_t self)
362 {
363         uhci_softc_t *sc = device_get_softc(self);
364
365         if (sc->sc_flags & UHCI_SCFLG_DONEINIT) {
366                 uhci_detach(sc, 0);
367                 sc->sc_flags &= ~UHCI_SCFLG_DONEINIT;
368         }
369
370
371         if (sc->irq_res && sc->ih) {
372                 int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
373
374                 if (err)
375                         /* XXX or should we panic? */
376                         device_printf(self, "Could not tear down irq, %d\n",
377                             err);
378                 sc->ih = NULL;
379         }
380         if (sc->sc_bus.bdev) {
381                 device_delete_child(self, sc->sc_bus.bdev);
382                 sc->sc_bus.bdev = NULL;
383         }
384         if (sc->irq_res) {
385                 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
386                 sc->irq_res = NULL;
387         }
388         if (sc->io_res) {
389                 bus_release_resource(self, SYS_RES_IOPORT, PCI_UHCI_BASE_REG,
390                     sc->io_res);
391                 sc->io_res = NULL;
392                 sc->iot = 0;
393                 sc->ioh = 0;
394         }
395         return 0;
396 }
397
398
399 static device_method_t uhci_methods[] = {
400         /* Device interface */
401         DEVMETHOD(device_probe, uhci_pci_probe),
402         DEVMETHOD(device_attach, uhci_pci_attach),
403         DEVMETHOD(device_detach, uhci_pci_detach),
404         DEVMETHOD(device_suspend, uhci_pci_suspend),
405         DEVMETHOD(device_resume, uhci_pci_resume),
406         DEVMETHOD(device_shutdown, bus_generic_shutdown),
407
408         /* Bus interface */
409         DEVMETHOD(bus_print_child, bus_generic_print_child),
410
411         {0, 0}
412 };
413
414 static driver_t uhci_driver = {
415         "uhci",
416         uhci_methods,
417         sizeof(uhci_softc_t),
418 };
419
420 static devclass_t uhci_devclass;
421
422 DRIVER_MODULE(uhci, pci, uhci_driver, uhci_devclass, 0, 0);
423 DRIVER_MODULE(uhci, cardbus, uhci_driver, uhci_devclass, 0, 0);