xref: /freebsd/sys/dev/usb/controller/ohci_pci.c (revision b1f9167f94059fd55c630891d359bcff987bd7eb)
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 #include "usb_if.h"
78 
79 #define	PCI_OHCI_VENDORID_ACERLABS	0x10b9
80 #define	PCI_OHCI_VENDORID_AMD		0x1022
81 #define	PCI_OHCI_VENDORID_APPLE		0x106b
82 #define	PCI_OHCI_VENDORID_ATI		0x1002
83 #define	PCI_OHCI_VENDORID_CMDTECH	0x1095
84 #define	PCI_OHCI_VENDORID_NEC		0x1033
85 #define	PCI_OHCI_VENDORID_NVIDIA	0x12D2
86 #define	PCI_OHCI_VENDORID_NVIDIA2	0x10DE
87 #define	PCI_OHCI_VENDORID_OPTI		0x1045
88 #define	PCI_OHCI_VENDORID_SIS		0x1039
89 #define	PCI_OHCI_VENDORID_SUN		0x108e
90 
91 #define	PCI_OHCI_BASE_REG	0x10
92 
93 static device_probe_t ohci_pci_probe;
94 static device_attach_t ohci_pci_attach;
95 static device_detach_t ohci_pci_detach;
96 static usb_take_controller_t ohci_pci_take_controller;
97 
98 static int
99 ohci_pci_take_controller(device_t self)
100 {
101 	uint32_t reg;
102 	uint32_t int_line;
103 
104 	if (pci_get_powerstate(self) != PCI_POWERSTATE_D0) {
105 		device_printf(self, "chip is in D%d mode "
106 		    "-- setting to D0\n", pci_get_powerstate(self));
107 		reg = pci_read_config(self, PCI_CBMEM, 4);
108 		int_line = pci_read_config(self, PCIR_INTLINE, 4);
109 		pci_set_powerstate(self, PCI_POWERSTATE_D0);
110 		pci_write_config(self, PCI_CBMEM, reg, 4);
111 		pci_write_config(self, PCIR_INTLINE, int_line, 4);
112 	}
113 	return (0);
114 }
115 
116 static const char *
117 ohci_pci_match(device_t self)
118 {
119 	uint32_t device_id = pci_get_devid(self);
120 
121 	switch (device_id) {
122 	case 0x523710b9:
123 		return ("AcerLabs M5237 (Aladdin-V) USB controller");
124 
125 	case 0x740c1022:
126 		return ("AMD-756 USB Controller");
127 
128 	case 0x74141022:
129 		return ("AMD-766 USB Controller");
130 
131 	case 0x43741002:
132 		return "ATI SB400 USB Controller";
133 	case 0x43751002:
134 		return "ATI SB400 USB Controller";
135 	case 0x43971002:
136 		return ("AMD SB7x0/SB8x0/SB9x0 USB controller");
137 	case 0x43981002:
138 		return ("AMD SB7x0/SB8x0/SB9x0 USB controller");
139 	case 0x43991002:
140 		return ("AMD SB7x0/SB8x0/SB9x0 USB controller");
141 
142 	case 0x06701095:
143 		return ("CMD Tech 670 (USB0670) USB controller");
144 
145 	case 0x06731095:
146 		return ("CMD Tech 673 (USB0673) USB controller");
147 
148 	case 0xc8611045:
149 		return ("OPTi 82C861 (FireLink) USB controller");
150 
151 	case 0x00351033:
152 		return ("NEC uPD 9210 USB controller");
153 
154 	case 0x00d710de:
155 		return ("nVidia nForce3 USB Controller");
156 
157 	case 0x005a10de:
158 		return ("nVidia nForce CK804 USB Controller");
159 	case 0x036c10de:
160 		return ("nVidia nForce MCP55 USB Controller");
161 	case 0x03f110de:
162 		return ("nVidia nForce MCP61 USB Controller");
163 	case 0x0aa510de:
164 		return ("nVidia nForce MCP79 USB Controller");
165 	case 0x0aa710de:
166 		return ("nVidia nForce MCP79 USB Controller");
167 	case 0x0aa810de:
168 		return ("nVidia nForce MCP79 USB Controller");
169 
170 	case 0x70011039:
171 		return ("SiS 5571 USB controller");
172 
173 	case 0x1103108e:
174 		return "Sun PCIO-2 USB controller";
175 
176 	case 0x0019106b:
177 		return ("Apple KeyLargo USB controller");
178 
179 	default:
180 		break;
181 	}
182 	if ((pci_get_class(self) == PCIC_SERIALBUS) &&
183 	    (pci_get_subclass(self) == PCIS_SERIALBUS_USB) &&
184 	    (pci_get_progif(self) == PCI_INTERFACE_OHCI)) {
185 		return ("OHCI (generic) USB controller");
186 	}
187 	return (NULL);
188 }
189 
190 static int
191 ohci_pci_probe(device_t self)
192 {
193 	const char *desc = ohci_pci_match(self);
194 
195 	if (desc) {
196 		device_set_desc(self, desc);
197 		return (0);
198 	} else {
199 		return (ENXIO);
200 	}
201 }
202 
203 static int
204 ohci_pci_attach(device_t self)
205 {
206 	ohci_softc_t *sc = device_get_softc(self);
207 	int rid;
208 	int err;
209 
210 	/* initialise some bus fields */
211 	sc->sc_bus.parent = self;
212 	sc->sc_bus.devices = sc->sc_devices;
213 	sc->sc_bus.devices_max = OHCI_MAX_DEVICES;
214 
215 	/* get all DMA memory */
216 	if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
217 	    &ohci_iterate_hw_softc)) {
218 		return (ENOMEM);
219 	}
220 	sc->sc_dev = self;
221 
222 	pci_enable_busmaster(self);
223 
224 	/*
225 	 * Some Sun PCIO-2 USB controllers have their intpin register
226 	 * bogusly set to 0, although it should be 4.  Correct that.
227 	 */
228 	if (pci_get_devid(self) == 0x1103108e && pci_get_intpin(self) == 0)
229 		pci_set_intpin(self, 4);
230 
231 	rid = PCI_CBMEM;
232 	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
233 	    RF_ACTIVE);
234 	if (!sc->sc_io_res) {
235 		device_printf(self, "Could not map memory\n");
236 		goto error;
237 	}
238 	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
239 	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
240 	sc->sc_io_size = rman_get_size(sc->sc_io_res);
241 
242 	rid = 0;
243 	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
244 	    RF_SHAREABLE | RF_ACTIVE);
245 	if (sc->sc_irq_res == NULL) {
246 		device_printf(self, "Could not allocate irq\n");
247 		goto error;
248 	}
249 	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
250 	if (!sc->sc_bus.bdev) {
251 		device_printf(self, "Could not add USB device\n");
252 		goto error;
253 	}
254 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
255 
256 	/*
257 	 * ohci_pci_match will never return NULL if ohci_pci_probe
258 	 * succeeded
259 	 */
260 	device_set_desc(sc->sc_bus.bdev, ohci_pci_match(self));
261 	switch (pci_get_vendor(self)) {
262 	case PCI_OHCI_VENDORID_ACERLABS:
263 		sprintf(sc->sc_vendor, "AcerLabs");
264 		break;
265 	case PCI_OHCI_VENDORID_AMD:
266 		sprintf(sc->sc_vendor, "AMD");
267 		break;
268 	case PCI_OHCI_VENDORID_APPLE:
269 		sprintf(sc->sc_vendor, "Apple");
270 		break;
271 	case PCI_OHCI_VENDORID_ATI:
272 		sprintf(sc->sc_vendor, "ATI");
273 		break;
274 	case PCI_OHCI_VENDORID_CMDTECH:
275 		sprintf(sc->sc_vendor, "CMDTECH");
276 		break;
277 	case PCI_OHCI_VENDORID_NEC:
278 		sprintf(sc->sc_vendor, "NEC");
279 		break;
280 	case PCI_OHCI_VENDORID_NVIDIA:
281 	case PCI_OHCI_VENDORID_NVIDIA2:
282 		sprintf(sc->sc_vendor, "nVidia");
283 		break;
284 	case PCI_OHCI_VENDORID_OPTI:
285 		sprintf(sc->sc_vendor, "OPTi");
286 		break;
287 	case PCI_OHCI_VENDORID_SIS:
288 		sprintf(sc->sc_vendor, "SiS");
289 		break;
290 	case PCI_OHCI_VENDORID_SUN:
291 		sprintf(sc->sc_vendor, "SUN");
292 		break;
293 	default:
294 		if (bootverbose) {
295 			device_printf(self, "(New OHCI DeviceId=0x%08x)\n",
296 			    pci_get_devid(self));
297 		}
298 		sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
299 	}
300 
301 	/* sc->sc_bus.usbrev; set by ohci_init() */
302 
303 #if (__FreeBSD_version >= 700031)
304 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
305 	    NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
306 #else
307 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
308 	    (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
309 #endif
310 	if (err) {
311 		device_printf(self, "Could not setup irq, %d\n", err);
312 		sc->sc_intr_hdl = NULL;
313 		goto error;
314 	}
315 	err = ohci_init(sc);
316 	if (!err) {
317 		err = device_probe_and_attach(sc->sc_bus.bdev);
318 	}
319 	if (err) {
320 		device_printf(self, "USB init failed\n");
321 		goto error;
322 	}
323 	return (0);
324 
325 error:
326 	ohci_pci_detach(self);
327 	return (ENXIO);
328 }
329 
330 static int
331 ohci_pci_detach(device_t self)
332 {
333 	ohci_softc_t *sc = device_get_softc(self);
334 	device_t bdev;
335 
336 	if (sc->sc_bus.bdev) {
337 		bdev = sc->sc_bus.bdev;
338 		device_detach(bdev);
339 		device_delete_child(self, bdev);
340 	}
341 	/* during module unload there are lots of children leftover */
342 	device_delete_children(self);
343 
344 	pci_disable_busmaster(self);
345 
346 	if (sc->sc_irq_res && sc->sc_intr_hdl) {
347 		/*
348 		 * only call ohci_detach() after ohci_init()
349 		 */
350 		ohci_detach(sc);
351 
352 		int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
353 
354 		if (err) {
355 			/* XXX or should we panic? */
356 			device_printf(self, "Could not tear down irq, %d\n",
357 			    err);
358 		}
359 		sc->sc_intr_hdl = NULL;
360 	}
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;
364 	}
365 	if (sc->sc_io_res) {
366 		bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
367 		    sc->sc_io_res);
368 		sc->sc_io_res = NULL;
369 	}
370 	usb_bus_mem_free_all(&sc->sc_bus, &ohci_iterate_hw_softc);
371 
372 	return (0);
373 }
374 
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),
384 
385 	DEVMETHOD_END
386 };
387 
388 static driver_t ohci_driver = {
389 	.name = "ohci",
390 	.methods = ohci_pci_methods,
391 	.size = sizeof(struct ohci_softc),
392 };
393 
394 static devclass_t ohci_devclass;
395 
396 DRIVER_MODULE(ohci, pci, ohci_driver, ohci_devclass, 0, 0);
397 MODULE_DEPEND(ohci, usb, 1, 1, 1);
398