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