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