xref: /freebsd/sys/dev/usb/controller/uhci_pci.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 /* Universal Host Controller Interface
37  *
38  * UHCI spec: http://www.intel.com/
39  */
40 
41 /* The low level controller code for UHCI has been split into
42  * PCI probes and UHCI specific code. This was done to facilitate the
43  * sharing of code between *BSD's
44  */
45 
46 #include <sys/stdint.h>
47 #include <sys/stddef.h>
48 #include <sys/param.h>
49 #include <sys/queue.h>
50 #include <sys/types.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/bus.h>
54 #include <sys/module.h>
55 #include <sys/lock.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/sx.h>
60 #include <sys/unistd.h>
61 #include <sys/callout.h>
62 #include <sys/malloc.h>
63 #include <sys/priv.h>
64 
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 
68 #include <dev/usb/usb_core.h>
69 #include <dev/usb/usb_busdma.h>
70 #include <dev/usb/usb_process.h>
71 #include <dev/usb/usb_util.h>
72 #include <dev/usb/usb_debug.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/uhci.h>
78 #include <dev/usb/controller/uhcireg.h>
79 #include "usb_if.h"
80 
81 #define	PCI_UHCI_VENDORID_INTEL		0x8086
82 #define	PCI_UHCI_VENDORID_HP		0x103c
83 #define	PCI_UHCI_VENDORID_VIA		0x1106
84 #define	PCI_UHCI_VENDORID_VMWARE	0x15ad
85 #define	PCI_UHCI_VENDORID_ZHAOXIN	0x1d17
86 
87 /* PIIX4E has no separate stepping */
88 
89 static device_probe_t uhci_pci_probe;
90 static device_attach_t uhci_pci_attach;
91 static device_detach_t uhci_pci_detach;
92 static usb_take_controller_t uhci_pci_take_controller;
93 
94 static int
95 uhci_pci_take_controller(device_t self)
96 {
97 	pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
98 
99 	return (0);
100 }
101 
102 static const char *
103 uhci_pci_match(device_t self)
104 {
105 	uint32_t device_id = pci_get_devid(self);
106 
107 	switch (device_id) {
108 	case 0x26888086:
109 		return ("Intel 631XESB/632XESB/3100 USB controller USB-1");
110 
111 	case 0x26898086:
112 		return ("Intel 631XESB/632XESB/3100 USB controller USB-2");
113 
114 	case 0x268a8086:
115 		return ("Intel 631XESB/632XESB/3100 USB controller USB-3");
116 
117 	case 0x268b8086:
118 		return ("Intel 631XESB/632XESB/3100 USB controller USB-4");
119 
120 	case 0x70208086:
121 		return ("Intel 82371SB (PIIX3) USB controller");
122 
123 	case 0x71128086:
124 		return ("Intel 82371AB/EB (PIIX4) USB controller");
125 
126 	case 0x24128086:
127 		return ("Intel 82801AA (ICH) USB controller");
128 
129 	case 0x24228086:
130 		return ("Intel 82801AB (ICH0) USB controller");
131 
132 	case 0x24428086:
133 		return ("Intel 82801BA/BAM (ICH2) USB controller USB-A");
134 
135 	case 0x24448086:
136 		return ("Intel 82801BA/BAM (ICH2) USB controller USB-B");
137 
138 	case 0x24828086:
139 		return ("Intel 82801CA/CAM (ICH3) USB controller USB-A");
140 
141 	case 0x24848086:
142 		return ("Intel 82801CA/CAM (ICH3) USB controller USB-B");
143 
144 	case 0x24878086:
145 		return ("Intel 82801CA/CAM (ICH3) USB controller USB-C");
146 
147 	case 0x24c28086:
148 		return ("Intel 82801DB (ICH4) USB controller USB-A");
149 
150 	case 0x24c48086:
151 		return ("Intel 82801DB (ICH4) USB controller USB-B");
152 
153 	case 0x24c78086:
154 		return ("Intel 82801DB (ICH4) USB controller USB-C");
155 
156 	case 0x24d28086:
157 		return ("Intel 82801EB (ICH5) USB controller USB-A");
158 
159 	case 0x24d48086:
160 		return ("Intel 82801EB (ICH5) USB controller USB-B");
161 
162 	case 0x24d78086:
163 		return ("Intel 82801EB (ICH5) USB controller USB-C");
164 
165 	case 0x24de8086:
166 		return ("Intel 82801EB (ICH5) USB controller USB-D");
167 
168 	case 0x25a98086:
169 		return ("Intel 6300ESB USB controller USB-A");
170 
171 	case 0x25aa8086:
172 		return ("Intel 6300ESB USB controller USB-B");
173 
174 	case 0x26588086:
175 		return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-A");
176 
177 	case 0x26598086:
178 		return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-B");
179 
180 	case 0x265a8086:
181 		return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-C");
182 
183 	case 0x265b8086:
184 		return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-D");
185 
186 	case 0x27c88086:
187 		return ("Intel 82801G (ICH7) USB controller USB-A");
188 	case 0x27c98086:
189 		return ("Intel 82801G (ICH7) USB controller USB-B");
190 	case 0x27ca8086:
191 		return ("Intel 82801G (ICH7) USB controller USB-C");
192 	case 0x27cb8086:
193 		return ("Intel 82801G (ICH7) USB controller USB-D");
194 
195 	case 0x28308086:
196 		return ("Intel 82801H (ICH8) USB controller USB-A");
197 	case 0x28318086:
198 		return ("Intel 82801H (ICH8) USB controller USB-B");
199 	case 0x28328086:
200 		return ("Intel 82801H (ICH8) USB controller USB-C");
201 	case 0x28348086:
202 		return ("Intel 82801H (ICH8) USB controller USB-D");
203 	case 0x28358086:
204 		return ("Intel 82801H (ICH8) USB controller USB-E");
205 	case 0x29348086:
206 		return ("Intel 82801I (ICH9) USB controller");
207 	case 0x29358086:
208 		return ("Intel 82801I (ICH9) USB controller");
209 	case 0x29368086:
210 		return ("Intel 82801I (ICH9) USB controller");
211 	case 0x29378086:
212 		return ("Intel 82801I (ICH9) USB controller");
213 	case 0x29388086:
214 		return ("Intel 82801I (ICH9) USB controller");
215 	case 0x29398086:
216 		return ("Intel 82801I (ICH9) USB controller");
217 	case 0x3a348086:
218 		return ("Intel 82801JI (ICH10) USB controller USB-A");
219 	case 0x3a358086:
220 		return ("Intel 82801JI (ICH10) USB controller USB-B");
221 	case 0x3a368086:
222 		return ("Intel 82801JI (ICH10) USB controller USB-C");
223 	case 0x3a378086:
224 		return ("Intel 82801JI (ICH10) USB controller USB-D");
225 	case 0x3a388086:
226 		return ("Intel 82801JI (ICH10) USB controller USB-E");
227 	case 0x3a398086:
228 		return ("Intel 82801JI (ICH10) USB controller USB-F");
229 
230 	case 0x719a8086:
231 		return ("Intel 82443MX USB controller");
232 
233 	case 0x76028086:
234 		return ("Intel 82372FB/82468GX USB controller");
235 
236 	case 0x3300103c:
237 		return ("HP iLO Standard Virtual USB controller");
238 
239 	case 0x30381106:
240 		return ("VIA 83C572 USB controller");
241 
242 	case 0x077415ad:
243 		return ("VMware USB controller");
244 
245 	case 0x30381d17:
246 		return ("Zhaoxin ZX-100/ZX-200/ZX-E USB controller");
247 
248 	default:
249 		break;
250 	}
251 
252 	if ((pci_get_class(self) == PCIC_SERIALBUS) &&
253 	    (pci_get_subclass(self) == PCIS_SERIALBUS_USB) &&
254 	    (pci_get_progif(self) == PCI_INTERFACE_UHCI)) {
255 		return ("UHCI (generic) USB controller");
256 	}
257 	return (NULL);
258 }
259 
260 static int
261 uhci_pci_probe(device_t self)
262 {
263 	const char *desc = uhci_pci_match(self);
264 
265 	if (desc) {
266 		device_set_desc(self, desc);
267 		return (BUS_PROBE_DEFAULT);
268 	} else {
269 		return (ENXIO);
270 	}
271 }
272 
273 static int
274 uhci_pci_attach(device_t self)
275 {
276 	uhci_softc_t *sc = device_get_softc(self);
277 	int rid;
278 	int err;
279 
280 	/* initialise some bus fields */
281 	sc->sc_bus.parent = self;
282 	sc->sc_bus.devices = sc->sc_devices;
283 	sc->sc_bus.devices_max = UHCI_MAX_DEVICES;
284 	sc->sc_bus.dma_bits = 32;
285 
286 	/* get all DMA memory */
287 	if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
288 	    &uhci_iterate_hw_softc)) {
289 		return ENOMEM;
290 	}
291 	sc->sc_dev = self;
292 
293 	pci_enable_busmaster(self);
294 
295 	rid = PCI_UHCI_BASE_REG;
296 	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid,
297 	    RF_ACTIVE);
298 	if (!sc->sc_io_res) {
299 		device_printf(self, "Could not map ports\n");
300 		goto error;
301 	}
302 	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
303 	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
304 	sc->sc_io_size = rman_get_size(sc->sc_io_res);
305 
306 	/* disable interrupts */
307 	bus_space_write_2(sc->sc_io_tag, sc->sc_io_hdl, UHCI_INTR, 0);
308 
309 	rid = 0;
310 	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
311 	    RF_SHAREABLE | RF_ACTIVE);
312 	if (sc->sc_irq_res == NULL) {
313 		device_printf(self, "Could not allocate irq\n");
314 		goto error;
315 	}
316 	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
317 	if (!sc->sc_bus.bdev) {
318 		device_printf(self, "Could not add USB device\n");
319 		goto error;
320 	}
321 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
322 
323 	/*
324 	 * uhci_pci_match must never return NULL if uhci_pci_probe
325 	 * succeeded
326 	 */
327 	device_set_desc(sc->sc_bus.bdev, uhci_pci_match(self));
328 	switch (pci_get_vendor(self)) {
329 	case PCI_UHCI_VENDORID_INTEL:
330 		sprintf(sc->sc_vendor, "Intel");
331 		break;
332 	case PCI_UHCI_VENDORID_HP:
333 		sprintf(sc->sc_vendor, "HP");
334 		break;
335 	case PCI_UHCI_VENDORID_VIA:
336 		sprintf(sc->sc_vendor, "VIA");
337 		break;
338 	case PCI_UHCI_VENDORID_VMWARE:
339 		sprintf(sc->sc_vendor, "VMware");
340 		break;
341 	case PCI_UHCI_VENDORID_ZHAOXIN:
342 		sprintf(sc->sc_vendor, "Zhaoxin");
343 		break;
344 	default:
345 		if (bootverbose) {
346 			device_printf(self, "(New UHCI DeviceId=0x%08x)\n",
347 			    pci_get_devid(self));
348 		}
349 		sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
350 	}
351 
352 	switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
353 	case PCI_USB_REV_PRE_1_0:
354 		sc->sc_bus.usbrev = USB_REV_PRE_1_0;
355 		break;
356 	case PCI_USB_REV_1_0:
357 		sc->sc_bus.usbrev = USB_REV_1_0;
358 		break;
359 	default:
360 		/* Quirk for Parallels Desktop 4.0 */
361 		device_printf(self, "USB revision is unknown. Assuming v1.1.\n");
362 		sc->sc_bus.usbrev = USB_REV_1_1;
363 		break;
364 	}
365 
366 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
367 	    NULL, (driver_intr_t *)uhci_interrupt, sc, &sc->sc_intr_hdl);
368 
369 	if (err) {
370 		device_printf(self, "Could not setup irq, %d\n", err);
371 		sc->sc_intr_hdl = NULL;
372 		goto error;
373 	}
374 	/*
375 	 * Set the PIRQD enable bit and switch off all the others. We don't
376 	 * want legacy support to interfere with us XXX Does this also mean
377 	 * that the BIOS won't touch the keyboard anymore if it is connected
378 	 * to the ports of the root hub?
379 	 */
380 #ifdef USB_DEBUG
381 	if (pci_read_config(self, PCI_LEGSUP, 2) != PCI_LEGSUP_USBPIRQDEN) {
382 		device_printf(self, "LegSup = 0x%04x\n",
383 		    pci_read_config(self, PCI_LEGSUP, 2));
384 	}
385 #endif
386 	pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
387 
388 	err = uhci_init(sc);
389 	if (!err) {
390 		err = device_probe_and_attach(sc->sc_bus.bdev);
391 	}
392 	if (err) {
393 		device_printf(self, "USB init failed\n");
394 		goto error;
395 	}
396 	return (0);
397 
398 error:
399 	uhci_pci_detach(self);
400 	return (ENXIO);
401 }
402 
403 int
404 uhci_pci_detach(device_t self)
405 {
406 	uhci_softc_t *sc = device_get_softc(self);
407 
408 	/* during module unload there are lots of children leftover */
409 	device_delete_children(self);
410 
411 	/*
412 	 * disable interrupts that might have been switched on in
413 	 * uhci_init.
414 	 */
415 	if (sc->sc_io_res) {
416 		USB_BUS_LOCK(&sc->sc_bus);
417 
418 		/* stop the controller */
419 		uhci_reset(sc);
420 
421 		USB_BUS_UNLOCK(&sc->sc_bus);
422 	}
423 	pci_disable_busmaster(self);
424 
425 	if (sc->sc_irq_res && sc->sc_intr_hdl) {
426 		int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
427 
428 		if (err) {
429 			/* XXX or should we panic? */
430 			device_printf(self, "Could not tear down irq, %d\n",
431 			    err);
432 		}
433 		sc->sc_intr_hdl = NULL;
434 	}
435 	if (sc->sc_irq_res) {
436 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
437 		sc->sc_irq_res = NULL;
438 	}
439 	if (sc->sc_io_res) {
440 		bus_release_resource(self, SYS_RES_IOPORT, PCI_UHCI_BASE_REG,
441 		    sc->sc_io_res);
442 		sc->sc_io_res = NULL;
443 	}
444 	usb_bus_mem_free_all(&sc->sc_bus, &uhci_iterate_hw_softc);
445 
446 	return (0);
447 }
448 
449 static device_method_t uhci_pci_methods[] = {
450 	/* Device interface */
451 	DEVMETHOD(device_probe, uhci_pci_probe),
452 	DEVMETHOD(device_attach, uhci_pci_attach),
453 	DEVMETHOD(device_detach, uhci_pci_detach),
454 	DEVMETHOD(device_suspend, bus_generic_suspend),
455 	DEVMETHOD(device_resume, bus_generic_resume),
456 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
457 	DEVMETHOD(usb_take_controller, uhci_pci_take_controller),
458 
459 	DEVMETHOD_END
460 };
461 
462 static driver_t uhci_driver = {
463 	.name = "uhci",
464 	.methods = uhci_pci_methods,
465 	.size = sizeof(struct uhci_softc),
466 };
467 
468 DRIVER_MODULE(uhci, pci, uhci_driver, 0, 0);
469 MODULE_DEPEND(uhci, usb, 1, 1, 1);
470