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