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