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