2 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss@carlstedt.se) at
9 * Carlstedt Research & Technology.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * USB Open Host Controller driver.
39 * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
42 /* The low level controller code for OHCI has been split into
43 * PCI probes and OHCI specific code. This was done to facilitate the
44 * sharing of code between *BSD's
47 #include <sys/stdint.h>
48 #include <sys/stddef.h>
49 #include <sys/param.h>
50 #include <sys/queue.h>
51 #include <sys/types.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
55 #include <sys/module.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/sysctl.h>
61 #include <sys/unistd.h>
62 #include <sys/callout.h>
63 #include <sys/malloc.h>
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
69 #include <dev/usb/usb_core.h>
70 #include <dev/usb/usb_busdma.h>
71 #include <dev/usb/usb_process.h>
72 #include <dev/usb/usb_util.h>
74 #include <dev/usb/usb_controller.h>
75 #include <dev/usb/usb_bus.h>
76 #include <dev/usb/usb_pci.h>
77 #include <dev/usb/controller/ohci.h>
78 #include <dev/usb/controller/ohcireg.h>
81 #define PCI_OHCI_VENDORID_ACERLABS 0x10b9
82 #define PCI_OHCI_VENDORID_AMD 0x1022
83 #define PCI_OHCI_VENDORID_APPLE 0x106b
84 #define PCI_OHCI_VENDORID_ATI 0x1002
85 #define PCI_OHCI_VENDORID_CMDTECH 0x1095
86 #define PCI_OHCI_VENDORID_NEC 0x1033
87 #define PCI_OHCI_VENDORID_NVIDIA 0x12D2
88 #define PCI_OHCI_VENDORID_NVIDIA2 0x10DE
89 #define PCI_OHCI_VENDORID_OPTI 0x1045
90 #define PCI_OHCI_VENDORID_SIS 0x1039
91 #define PCI_OHCI_VENDORID_SUN 0x108e
93 #define PCI_OHCI_BASE_REG 0x10
95 static device_probe_t ohci_pci_probe;
96 static device_attach_t ohci_pci_attach;
97 static device_detach_t ohci_pci_detach;
98 static usb_take_controller_t ohci_pci_take_controller;
101 ohci_pci_take_controller(device_t self)
106 if (pci_get_powerstate(self) != PCI_POWERSTATE_D0) {
107 device_printf(self, "chip is in D%d mode "
108 "-- setting to D0\n", pci_get_powerstate(self));
109 reg = pci_read_config(self, PCI_CBMEM, 4);
110 int_line = pci_read_config(self, PCIR_INTLINE, 4);
111 pci_set_powerstate(self, PCI_POWERSTATE_D0);
112 pci_write_config(self, PCI_CBMEM, reg, 4);
113 pci_write_config(self, PCIR_INTLINE, int_line, 4);
119 ohci_pci_match(device_t self)
121 uint32_t device_id = pci_get_devid(self);
125 return ("AcerLabs M5237 (Aladdin-V) USB controller");
128 return ("AMD-756 USB Controller");
130 return ("AMD-766 USB Controller");
132 return ("AMD FCH USB Controller");
135 return "ATI SB400 USB Controller";
137 return "ATI SB400 USB Controller";
139 return ("AMD SB7x0/SB8x0/SB9x0 USB controller");
141 return ("AMD SB7x0/SB8x0/SB9x0 USB controller");
143 return ("AMD SB7x0/SB8x0/SB9x0 USB controller");
146 return ("CMD Tech 670 (USB0670) USB controller");
149 return ("CMD Tech 673 (USB0673) USB controller");
152 return ("OPTi 82C861 (FireLink) USB controller");
155 return ("NEC uPD 9210 USB controller");
158 return ("nVidia nForce3 USB Controller");
161 return ("nVidia nForce CK804 USB Controller");
163 return ("nVidia nForce MCP55 USB Controller");
165 return ("nVidia nForce MCP61 USB Controller");
167 return ("nVidia nForce MCP79 USB Controller");
169 return ("nVidia nForce MCP79 USB Controller");
171 return ("nVidia nForce MCP79 USB Controller");
174 return ("SiS 5571 USB controller");
177 return "Sun PCIO-2 USB controller";
180 return ("Apple KeyLargo USB controller");
182 return ("Apple KeyLargo/Intrepid USB controller");
187 if ((pci_get_class(self) == PCIC_SERIALBUS) &&
188 (pci_get_subclass(self) == PCIS_SERIALBUS_USB) &&
189 (pci_get_progif(self) == PCI_INTERFACE_OHCI)) {
190 return ("OHCI (generic) USB controller");
196 ohci_pci_probe(device_t self)
198 const char *desc = ohci_pci_match(self);
201 device_set_desc(self, desc);
209 ohci_pci_attach(device_t self)
211 ohci_softc_t *sc = device_get_softc(self);
215 /* initialise some bus fields */
216 sc->sc_bus.parent = self;
217 sc->sc_bus.devices = sc->sc_devices;
218 sc->sc_bus.devices_max = OHCI_MAX_DEVICES;
219 sc->sc_bus.dma_bits = 32;
221 /* get all DMA memory */
222 if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
223 &ohci_iterate_hw_softc)) {
228 pci_enable_busmaster(self);
231 * Some Sun PCIO-2 USB controllers have their intpin register
232 * bogusly set to 0, although it should be 4. Correct that.
234 if (pci_get_devid(self) == 0x1103108e && pci_get_intpin(self) == 0)
235 pci_set_intpin(self, 4);
238 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
240 if (!sc->sc_io_res) {
241 device_printf(self, "Could not map memory\n");
244 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
245 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
246 sc->sc_io_size = rman_get_size(sc->sc_io_res);
249 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
250 RF_SHAREABLE | RF_ACTIVE);
251 if (sc->sc_irq_res == NULL) {
252 device_printf(self, "Could not allocate irq\n");
255 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
256 if (!sc->sc_bus.bdev) {
257 device_printf(self, "Could not add USB device\n");
260 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
263 * ohci_pci_match will never return NULL if ohci_pci_probe
266 device_set_desc(sc->sc_bus.bdev, ohci_pci_match(self));
267 switch (pci_get_vendor(self)) {
268 case PCI_OHCI_VENDORID_ACERLABS:
269 sprintf(sc->sc_vendor, "AcerLabs");
271 case PCI_OHCI_VENDORID_AMD:
272 sprintf(sc->sc_vendor, "AMD");
274 case PCI_OHCI_VENDORID_APPLE:
275 sprintf(sc->sc_vendor, "Apple");
277 case PCI_OHCI_VENDORID_ATI:
278 sprintf(sc->sc_vendor, "ATI");
280 case PCI_OHCI_VENDORID_CMDTECH:
281 sprintf(sc->sc_vendor, "CMDTECH");
283 case PCI_OHCI_VENDORID_NEC:
284 sprintf(sc->sc_vendor, "NEC");
286 case PCI_OHCI_VENDORID_NVIDIA:
287 case PCI_OHCI_VENDORID_NVIDIA2:
288 sprintf(sc->sc_vendor, "nVidia");
290 case PCI_OHCI_VENDORID_OPTI:
291 sprintf(sc->sc_vendor, "OPTi");
293 case PCI_OHCI_VENDORID_SIS:
294 sprintf(sc->sc_vendor, "SiS");
296 case PCI_OHCI_VENDORID_SUN:
297 sprintf(sc->sc_vendor, "SUN");
301 device_printf(self, "(New OHCI DeviceId=0x%08x)\n",
302 pci_get_devid(self));
304 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
307 /* sc->sc_bus.usbrev; set by ohci_init() */
309 #if (__FreeBSD_version >= 700031)
310 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
311 NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
313 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
314 (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
317 device_printf(self, "Could not setup irq, %d\n", err);
318 sc->sc_intr_hdl = NULL;
323 err = device_probe_and_attach(sc->sc_bus.bdev);
326 device_printf(self, "USB init failed\n");
332 ohci_pci_detach(self);
337 ohci_pci_detach(device_t self)
339 ohci_softc_t *sc = device_get_softc(self);
341 /* during module unload there are lots of children leftover */
342 device_delete_children(self);
344 pci_disable_busmaster(self);
346 if (sc->sc_irq_res && sc->sc_intr_hdl) {
348 * only call ohci_detach() after ohci_init()
352 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
355 /* XXX or should we panic? */
356 device_printf(self, "Could not tear down irq, %d\n",
359 sc->sc_intr_hdl = NULL;
361 if (sc->sc_irq_res) {
362 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
363 sc->sc_irq_res = NULL;
366 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
368 sc->sc_io_res = NULL;
370 usb_bus_mem_free_all(&sc->sc_bus, &ohci_iterate_hw_softc);
375 static device_method_t ohci_pci_methods[] = {
376 /* Device interface */
377 DEVMETHOD(device_probe, ohci_pci_probe),
378 DEVMETHOD(device_attach, ohci_pci_attach),
379 DEVMETHOD(device_detach, ohci_pci_detach),
380 DEVMETHOD(device_suspend, bus_generic_suspend),
381 DEVMETHOD(device_resume, bus_generic_resume),
382 DEVMETHOD(device_shutdown, bus_generic_shutdown),
383 DEVMETHOD(usb_take_controller, ohci_pci_take_controller),
388 static driver_t ohci_driver = {
390 .methods = ohci_pci_methods,
391 .size = sizeof(struct ohci_softc),
394 static devclass_t ohci_devclass;
396 DRIVER_MODULE(ohci, pci, ohci_driver, ohci_devclass, 0, 0);
397 MODULE_DEPEND(ohci, usb, 1, 1, 1);