]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/usb/controller/ohci_pci.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / usb / controller / ohci_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
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 /*
42  * USB Open Host Controller driver.
43  *
44  * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
45  */
46
47 /* The low level controller code for OHCI has been split into
48  * PCI probes and OHCI specific code. This was done to facilitate the
49  * sharing of code between *BSD's
50  */
51
52 #include <sys/stdint.h>
53 #include <sys/stddef.h>
54 #include <sys/param.h>
55 #include <sys/queue.h>
56 #include <sys/types.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/bus.h>
60 #include <sys/linker_set.h>
61 #include <sys/module.h>
62 #include <sys/lock.h>
63 #include <sys/mutex.h>
64 #include <sys/condvar.h>
65 #include <sys/sysctl.h>
66 #include <sys/sx.h>
67 #include <sys/unistd.h>
68 #include <sys/callout.h>
69 #include <sys/malloc.h>
70 #include <sys/priv.h>
71
72 #include <dev/usb/usb.h>
73 #include <dev/usb/usbdi.h>
74
75 #include <dev/usb/usb_core.h>
76 #include <dev/usb/usb_busdma.h>
77 #include <dev/usb/usb_process.h>
78 #include <dev/usb/usb_util.h>
79
80 #include <dev/usb/usb_controller.h>
81 #include <dev/usb/usb_bus.h>
82 #include <dev/usb/usb_pci.h>
83 #include <dev/usb/controller/ohci.h>
84 #include <dev/usb/controller/ohcireg.h>
85
86 #define PCI_OHCI_VENDORID_ACERLABS      0x10b9
87 #define PCI_OHCI_VENDORID_AMD           0x1022
88 #define PCI_OHCI_VENDORID_APPLE         0x106b
89 #define PCI_OHCI_VENDORID_ATI           0x1002
90 #define PCI_OHCI_VENDORID_CMDTECH       0x1095
91 #define PCI_OHCI_VENDORID_NEC           0x1033
92 #define PCI_OHCI_VENDORID_NVIDIA        0x12D2
93 #define PCI_OHCI_VENDORID_NVIDIA2       0x10DE
94 #define PCI_OHCI_VENDORID_OPTI          0x1045
95 #define PCI_OHCI_VENDORID_SIS           0x1039
96 #define PCI_OHCI_VENDORID_SUN           0x108e
97
98 #define PCI_OHCI_BASE_REG       0x10
99
100 static device_probe_t ohci_pci_probe;
101 static device_attach_t ohci_pci_attach;
102 static device_detach_t ohci_pci_detach;
103 static device_suspend_t ohci_pci_suspend;
104 static device_resume_t ohci_pci_resume;
105
106 static int
107 ohci_pci_suspend(device_t self)
108 {
109         ohci_softc_t *sc = device_get_softc(self);
110         int err;
111
112         err = bus_generic_suspend(self);
113         if (err) {
114                 return (err);
115         }
116         ohci_suspend(sc);
117         return (0);
118 }
119
120 static int
121 ohci_pci_resume(device_t self)
122 {
123         ohci_softc_t *sc = device_get_softc(self);
124         uint32_t reg, int_line;
125
126         if (pci_get_powerstate(self) != PCI_POWERSTATE_D0) {
127                 device_printf(self, "chip is in D%d mode "
128                     "-- setting to D0\n", pci_get_powerstate(self));
129                 reg = pci_read_config(self, PCI_CBMEM, 4);
130                 int_line = pci_read_config(self, PCIR_INTLINE, 4);
131                 pci_set_powerstate(self, PCI_POWERSTATE_D0);
132                 pci_write_config(self, PCI_CBMEM, reg, 4);
133                 pci_write_config(self, PCIR_INTLINE, int_line, 4);
134         }
135         ohci_resume(sc);
136
137         bus_generic_resume(self);
138         return (0);
139 }
140
141 static const char *
142 ohci_pci_match(device_t self)
143 {
144         uint32_t device_id = pci_get_devid(self);
145
146         switch (device_id) {
147         case 0x523710b9:
148                 return ("AcerLabs M5237 (Aladdin-V) USB controller");
149
150         case 0x740c1022:
151                 return ("AMD-756 USB Controller");
152
153         case 0x74141022:
154                 return ("AMD-766 USB Controller");
155
156         case 0x43741002:
157                 return "ATI SB400 USB Controller";
158         case 0x43751002:
159                 return "ATI SB400 USB Controller";
160
161         case 0x06701095:
162                 return ("CMD Tech 670 (USB0670) USB controller");
163
164         case 0x06731095:
165                 return ("CMD Tech 673 (USB0673) USB controller");
166
167         case 0xc8611045:
168                 return ("OPTi 82C861 (FireLink) USB controller");
169
170         case 0x00351033:
171                 return ("NEC uPD 9210 USB controller");
172
173         case 0x00d710de:
174                 return ("nVidia nForce3 USB Controller");
175
176         case 0x036c10de:
177                 return ("nVidia nForce MCP55 USB Controller");
178         case 0x03f110de:
179                 return ("nVidia nForce MCP61 USB Controller");
180         case 0x0aa510de:
181                 return ("nVidia nForce MCP79 USB Controller");
182         case 0x0aa710de:
183                 return ("nVidia nForce MCP79 USB Controller");
184         case 0x0aa810de:
185                 return ("nVidia nForce MCP79 USB Controller");
186
187         case 0x70011039:
188                 return ("SiS 5571 USB controller");
189
190         case 0x1103108e:
191                 return "Sun PCIO-2 USB controller";
192
193         case 0x0019106b:
194                 return ("Apple KeyLargo USB controller");
195
196         default:
197                 break;
198         }
199         if ((pci_get_class(self) == PCIC_SERIALBUS) &&
200             (pci_get_subclass(self) == PCIS_SERIALBUS_USB) &&
201             (pci_get_progif(self) == PCI_INTERFACE_OHCI)) {
202                 return ("OHCI (generic) USB controller");
203         }
204         return (NULL);
205 }
206
207 static int
208 ohci_pci_probe(device_t self)
209 {
210         const char *desc = ohci_pci_match(self);
211
212         if (desc) {
213                 device_set_desc(self, desc);
214                 return (0);
215         } else {
216                 return (ENXIO);
217         }
218 }
219
220 static int
221 ohci_pci_attach(device_t self)
222 {
223         ohci_softc_t *sc = device_get_softc(self);
224         int rid;
225         int err;
226
227         /* initialise some bus fields */
228         sc->sc_bus.parent = self;
229         sc->sc_bus.devices = sc->sc_devices;
230         sc->sc_bus.devices_max = OHCI_MAX_DEVICES;
231
232         /* get all DMA memory */
233         if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
234             &ohci_iterate_hw_softc)) {
235                 return (ENOMEM);
236         }
237         sc->sc_dev = self;
238
239         pci_enable_busmaster(self);
240
241         /*
242          * Some Sun PCIO-2 USB controllers have their intpin register
243          * bogusly set to 0, although it should be 4.  Correct that.
244          */
245         if (pci_get_devid(self) == 0x1103108e && pci_get_intpin(self) == 0)
246                 pci_set_intpin(self, 4);
247
248         rid = PCI_CBMEM;
249         sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
250             RF_ACTIVE);
251         if (!sc->sc_io_res) {
252                 device_printf(self, "Could not map memory\n");
253                 goto error;
254         }
255         sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
256         sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
257         sc->sc_io_size = rman_get_size(sc->sc_io_res);
258
259         rid = 0;
260         sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
261             RF_SHAREABLE | RF_ACTIVE);
262         if (sc->sc_irq_res == NULL) {
263                 device_printf(self, "Could not allocate irq\n");
264                 goto error;
265         }
266         sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
267         if (!sc->sc_bus.bdev) {
268                 device_printf(self, "Could not add USB device\n");
269                 goto error;
270         }
271         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
272
273         /*
274          * ohci_pci_match will never return NULL if ohci_pci_probe
275          * succeeded
276          */
277         device_set_desc(sc->sc_bus.bdev, ohci_pci_match(self));
278         switch (pci_get_vendor(self)) {
279         case PCI_OHCI_VENDORID_ACERLABS:
280                 sprintf(sc->sc_vendor, "AcerLabs");
281                 break;
282         case PCI_OHCI_VENDORID_AMD:
283                 sprintf(sc->sc_vendor, "AMD");
284                 break;
285         case PCI_OHCI_VENDORID_APPLE:
286                 sprintf(sc->sc_vendor, "Apple");
287                 break;
288         case PCI_OHCI_VENDORID_ATI:
289                 sprintf(sc->sc_vendor, "ATI");
290                 break;
291         case PCI_OHCI_VENDORID_CMDTECH:
292                 sprintf(sc->sc_vendor, "CMDTECH");
293                 break;
294         case PCI_OHCI_VENDORID_NEC:
295                 sprintf(sc->sc_vendor, "NEC");
296                 break;
297         case PCI_OHCI_VENDORID_NVIDIA:
298         case PCI_OHCI_VENDORID_NVIDIA2:
299                 sprintf(sc->sc_vendor, "nVidia");
300                 break;
301         case PCI_OHCI_VENDORID_OPTI:
302                 sprintf(sc->sc_vendor, "OPTi");
303                 break;
304         case PCI_OHCI_VENDORID_SIS:
305                 sprintf(sc->sc_vendor, "SiS");
306                 break;
307         case PCI_OHCI_VENDORID_SUN:
308                 sprintf(sc->sc_vendor, "SUN");
309                 break;
310         default:
311                 if (bootverbose) {
312                         device_printf(self, "(New OHCI DeviceId=0x%08x)\n",
313                             pci_get_devid(self));
314                 }
315                 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
316         }
317
318         /* sc->sc_bus.usbrev; set by ohci_init() */
319
320 #if (__FreeBSD_version >= 700031)
321         err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
322             NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
323 #else
324         err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
325             (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
326 #endif
327         if (err) {
328                 device_printf(self, "Could not setup irq, %d\n", err);
329                 sc->sc_intr_hdl = NULL;
330                 goto error;
331         }
332         err = ohci_init(sc);
333         if (!err) {
334                 err = device_probe_and_attach(sc->sc_bus.bdev);
335         }
336         if (err) {
337                 device_printf(self, "USB init failed\n");
338                 goto error;
339         }
340         return (0);
341
342 error:
343         ohci_pci_detach(self);
344         return (ENXIO);
345 }
346
347 static int
348 ohci_pci_detach(device_t self)
349 {
350         ohci_softc_t *sc = device_get_softc(self);
351         device_t bdev;
352
353         if (sc->sc_bus.bdev) {
354                 bdev = sc->sc_bus.bdev;
355                 device_detach(bdev);
356                 device_delete_child(self, bdev);
357         }
358         /* during module unload there are lots of children leftover */
359         device_delete_all_children(self);
360
361         pci_disable_busmaster(self);
362
363         if (sc->sc_irq_res && sc->sc_intr_hdl) {
364                 /*
365                  * only call ohci_detach() after ohci_init()
366                  */
367                 ohci_detach(sc);
368
369                 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
370
371                 if (err) {
372                         /* XXX or should we panic? */
373                         device_printf(self, "Could not tear down irq, %d\n",
374                             err);
375                 }
376                 sc->sc_intr_hdl = NULL;
377         }
378         if (sc->sc_irq_res) {
379                 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
380                 sc->sc_irq_res = NULL;
381         }
382         if (sc->sc_io_res) {
383                 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
384                     sc->sc_io_res);
385                 sc->sc_io_res = NULL;
386         }
387         usb_bus_mem_free_all(&sc->sc_bus, &ohci_iterate_hw_softc);
388
389         return (0);
390 }
391
392 static driver_t ohci_driver =
393 {
394         .name = "ohci",
395         .methods = (device_method_t[]){
396                 /* device interface */
397                 DEVMETHOD(device_probe, ohci_pci_probe),
398                 DEVMETHOD(device_attach, ohci_pci_attach),
399                 DEVMETHOD(device_detach, ohci_pci_detach),
400                 DEVMETHOD(device_suspend, ohci_pci_suspend),
401                 DEVMETHOD(device_resume, ohci_pci_resume),
402                 DEVMETHOD(device_shutdown, bus_generic_shutdown),
403
404                 /* bus interface */
405                 DEVMETHOD(bus_print_child, bus_generic_print_child),
406
407                 {0, 0}
408         },
409         .size = sizeof(struct ohci_softc),
410 };
411
412 static devclass_t ohci_devclass;
413
414 DRIVER_MODULE(ohci, pci, ohci_driver, ohci_devclass, 0, 0);
415 MODULE_DEPEND(ohci, usb, 1, 1, 1);