xref: /freebsd/sys/dev/usb/controller/ohci_pci.c (revision 2f513db72b034fd5ef7f080b11be5c711c15186a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
3  *
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson (augustss@carlstedt.se) at
9  * Carlstedt Research & Technology.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 /*
37  * USB Open Host Controller driver.
38  *
39  * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
40  */
41 
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
45  */
46 
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>
54 #include <sys/bus.h>
55 #include <sys/module.h>
56 #include <sys/lock.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/sx.h>
61 #include <sys/unistd.h>
62 #include <sys/callout.h>
63 #include <sys/malloc.h>
64 #include <sys/priv.h>
65 
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68 
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>
73 
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>
79 #include "usb_if.h"
80 
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_HYGON		0x1d94
87 #define	PCI_OHCI_VENDORID_NEC		0x1033
88 #define	PCI_OHCI_VENDORID_NVIDIA	0x12D2
89 #define	PCI_OHCI_VENDORID_NVIDIA2	0x10DE
90 #define	PCI_OHCI_VENDORID_OPTI		0x1045
91 #define	PCI_OHCI_VENDORID_SIS		0x1039
92 #define	PCI_OHCI_VENDORID_SUN		0x108e
93 
94 #define	PCI_OHCI_BASE_REG	0x10
95 
96 static device_probe_t ohci_pci_probe;
97 static device_attach_t ohci_pci_attach;
98 static device_detach_t ohci_pci_detach;
99 static usb_take_controller_t ohci_pci_take_controller;
100 
101 static int
102 ohci_pci_take_controller(device_t self)
103 {
104 	uint32_t reg;
105 	uint32_t int_line;
106 
107 	if (pci_get_powerstate(self) != PCI_POWERSTATE_D0) {
108 		device_printf(self, "chip is in D%d mode "
109 		    "-- setting to D0\n", pci_get_powerstate(self));
110 		reg = pci_read_config(self, PCI_CBMEM, 4);
111 		int_line = pci_read_config(self, PCIR_INTLINE, 4);
112 		pci_set_powerstate(self, PCI_POWERSTATE_D0);
113 		pci_write_config(self, PCI_CBMEM, reg, 4);
114 		pci_write_config(self, PCIR_INTLINE, int_line, 4);
115 	}
116 	return (0);
117 }
118 
119 static const char *
120 ohci_pci_match(device_t self)
121 {
122 	uint32_t device_id = pci_get_devid(self);
123 
124 	switch (device_id) {
125 	case 0x523710b9:
126 		return ("AcerLabs M5237 (Aladdin-V) USB controller");
127 
128 	case 0x740c1022:
129 		return ("AMD-756 USB Controller");
130 	case 0x74141022:
131 		return ("AMD-766 USB Controller");
132 	case 0x78071022:
133 		return ("AMD FCH USB Controller");
134 
135 	case 0x43741002:
136 		return "ATI SB400 USB Controller";
137 	case 0x43751002:
138 		return "ATI SB400 USB Controller";
139 	case 0x43971002:
140 		return ("AMD SB7x0/SB8x0/SB9x0 USB controller");
141 	case 0x43981002:
142 		return ("AMD SB7x0/SB8x0/SB9x0 USB controller");
143 	case 0x43991002:
144 		return ("AMD SB7x0/SB8x0/SB9x0 USB controller");
145 
146 	case 0x06701095:
147 		return ("CMD Tech 670 (USB0670) USB controller");
148 
149 	case 0x06731095:
150 		return ("CMD Tech 673 (USB0673) USB controller");
151 
152 	case 0xc8611045:
153 		return ("OPTi 82C861 (FireLink) USB controller");
154 
155 	case 0x00351033:
156 		return ("NEC uPD 9210 USB controller");
157 
158 	case 0x00d710de:
159 		return ("nVidia nForce3 USB Controller");
160 
161 	case 0x005a10de:
162 		return ("nVidia nForce CK804 USB Controller");
163 	case 0x036c10de:
164 		return ("nVidia nForce MCP55 USB Controller");
165 	case 0x03f110de:
166 		return ("nVidia nForce MCP61 USB Controller");
167 	case 0x0aa510de:
168 		return ("nVidia nForce MCP79 USB Controller");
169 	case 0x0aa710de:
170 		return ("nVidia nForce MCP79 USB Controller");
171 	case 0x0aa810de:
172 		return ("nVidia nForce MCP79 USB Controller");
173 
174 	case 0x70011039:
175 		return ("SiS 5571 USB controller");
176 
177 	case 0x1103108e:
178 		return "Sun PCIO-2 USB controller";
179 
180 	case 0x0019106b:
181 		return ("Apple KeyLargo USB controller");
182 	case 0x003f106b:
183 		return ("Apple KeyLargo/Intrepid USB controller");
184 
185 	default:
186 		break;
187 	}
188 	if ((pci_get_class(self) == PCIC_SERIALBUS) &&
189 	    (pci_get_subclass(self) == PCIS_SERIALBUS_USB) &&
190 	    (pci_get_progif(self) == PCI_INTERFACE_OHCI)) {
191 		return ("OHCI (generic) USB controller");
192 	}
193 	return (NULL);
194 }
195 
196 static int
197 ohci_pci_probe(device_t self)
198 {
199 	const char *desc = ohci_pci_match(self);
200 
201 	if (desc) {
202 		device_set_desc(self, desc);
203 		return (0);
204 	} else {
205 		return (ENXIO);
206 	}
207 }
208 
209 static int
210 ohci_pci_attach(device_t self)
211 {
212 	ohci_softc_t *sc = device_get_softc(self);
213 	int rid;
214 	int err;
215 
216 	/* initialise some bus fields */
217 	sc->sc_bus.parent = self;
218 	sc->sc_bus.devices = sc->sc_devices;
219 	sc->sc_bus.devices_max = OHCI_MAX_DEVICES;
220 	sc->sc_bus.dma_bits = 32;
221 
222 	/* get all DMA memory */
223 	if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
224 	    &ohci_iterate_hw_softc)) {
225 		return (ENOMEM);
226 	}
227 	sc->sc_dev = self;
228 
229 	pci_enable_busmaster(self);
230 
231 	/*
232 	 * Some Sun PCIO-2 USB controllers have their intpin register
233 	 * bogusly set to 0, although it should be 4.  Correct that.
234 	 */
235 	if (pci_get_devid(self) == 0x1103108e && pci_get_intpin(self) == 0)
236 		pci_set_intpin(self, 4);
237 
238 	rid = PCI_CBMEM;
239 	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
240 	    RF_ACTIVE);
241 	if (!sc->sc_io_res) {
242 		device_printf(self, "Could not map memory\n");
243 		goto error;
244 	}
245 	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
246 	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
247 	sc->sc_io_size = rman_get_size(sc->sc_io_res);
248 
249 	rid = 0;
250 	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
251 	    RF_SHAREABLE | RF_ACTIVE);
252 	if (sc->sc_irq_res == NULL) {
253 		device_printf(self, "Could not allocate irq\n");
254 		goto error;
255 	}
256 	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
257 	if (!sc->sc_bus.bdev) {
258 		device_printf(self, "Could not add USB device\n");
259 		goto error;
260 	}
261 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
262 
263 	/*
264 	 * ohci_pci_match will never return NULL if ohci_pci_probe
265 	 * succeeded
266 	 */
267 	device_set_desc(sc->sc_bus.bdev, ohci_pci_match(self));
268 	switch (pci_get_vendor(self)) {
269 	case PCI_OHCI_VENDORID_ACERLABS:
270 		sprintf(sc->sc_vendor, "AcerLabs");
271 		break;
272 	case PCI_OHCI_VENDORID_AMD:
273 		sprintf(sc->sc_vendor, "AMD");
274 		break;
275 	case PCI_OHCI_VENDORID_APPLE:
276 		sprintf(sc->sc_vendor, "Apple");
277 		break;
278 	case PCI_OHCI_VENDORID_ATI:
279 		sprintf(sc->sc_vendor, "ATI");
280 		break;
281 	case PCI_OHCI_VENDORID_CMDTECH:
282 		sprintf(sc->sc_vendor, "CMDTECH");
283 		break;
284 	case PCI_OHCI_VENDORID_HYGON:
285 		sprintf(sc->sc_vendor, "Hygon");
286 		break;
287 	case PCI_OHCI_VENDORID_NEC:
288 		sprintf(sc->sc_vendor, "NEC");
289 		break;
290 	case PCI_OHCI_VENDORID_NVIDIA:
291 	case PCI_OHCI_VENDORID_NVIDIA2:
292 		sprintf(sc->sc_vendor, "nVidia");
293 		break;
294 	case PCI_OHCI_VENDORID_OPTI:
295 		sprintf(sc->sc_vendor, "OPTi");
296 		break;
297 	case PCI_OHCI_VENDORID_SIS:
298 		sprintf(sc->sc_vendor, "SiS");
299 		break;
300 	case PCI_OHCI_VENDORID_SUN:
301 		sprintf(sc->sc_vendor, "SUN");
302 		break;
303 	default:
304 		if (bootverbose) {
305 			device_printf(self, "(New OHCI DeviceId=0x%08x)\n",
306 			    pci_get_devid(self));
307 		}
308 		sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
309 	}
310 
311 	/* sc->sc_bus.usbrev; set by ohci_init() */
312 
313 #if (__FreeBSD_version >= 700031)
314 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
315 	    NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
316 #else
317 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
318 	    (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
319 #endif
320 	if (err) {
321 		device_printf(self, "Could not setup irq, %d\n", err);
322 		sc->sc_intr_hdl = NULL;
323 		goto error;
324 	}
325 	err = ohci_init(sc);
326 	if (!err) {
327 		err = device_probe_and_attach(sc->sc_bus.bdev);
328 	}
329 	if (err) {
330 		device_printf(self, "USB init failed\n");
331 		goto error;
332 	}
333 	return (0);
334 
335 error:
336 	ohci_pci_detach(self);
337 	return (ENXIO);
338 }
339 
340 static int
341 ohci_pci_detach(device_t self)
342 {
343 	ohci_softc_t *sc = device_get_softc(self);
344 
345 	/* during module unload there are lots of children leftover */
346 	device_delete_children(self);
347 
348 	pci_disable_busmaster(self);
349 
350 	if (sc->sc_irq_res && sc->sc_intr_hdl) {
351 		/*
352 		 * only call ohci_detach() after ohci_init()
353 		 */
354 		ohci_detach(sc);
355 
356 		int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
357 
358 		if (err) {
359 			/* XXX or should we panic? */
360 			device_printf(self, "Could not tear down irq, %d\n",
361 			    err);
362 		}
363 		sc->sc_intr_hdl = NULL;
364 	}
365 	if (sc->sc_irq_res) {
366 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
367 		sc->sc_irq_res = NULL;
368 	}
369 	if (sc->sc_io_res) {
370 		bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
371 		    sc->sc_io_res);
372 		sc->sc_io_res = NULL;
373 	}
374 	usb_bus_mem_free_all(&sc->sc_bus, &ohci_iterate_hw_softc);
375 
376 	return (0);
377 }
378 
379 static device_method_t ohci_pci_methods[] = {
380 	/* Device interface */
381 	DEVMETHOD(device_probe, ohci_pci_probe),
382 	DEVMETHOD(device_attach, ohci_pci_attach),
383 	DEVMETHOD(device_detach, ohci_pci_detach),
384 	DEVMETHOD(device_suspend, bus_generic_suspend),
385 	DEVMETHOD(device_resume, bus_generic_resume),
386 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
387 	DEVMETHOD(usb_take_controller, ohci_pci_take_controller),
388 
389 	DEVMETHOD_END
390 };
391 
392 static driver_t ohci_driver = {
393 	.name = "ohci",
394 	.methods = ohci_pci_methods,
395 	.size = sizeof(struct ohci_softc),
396 };
397 
398 static devclass_t ohci_devclass;
399 
400 DRIVER_MODULE(ohci, pci, ohci_driver, ohci_devclass, 0, 0);
401 MODULE_DEPEND(ohci, usb, 1, 1, 1);
402