uhci_pci.c revision 194677
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 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *        This product includes software developed by the NetBSD
20 *        Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 *    contributors may be used to endorse or promote products derived
23 *    from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/sys/dev/usb/controller/uhci_pci.c 194677 2009-06-23 02:19:59Z thompsa $");
40
41/* Universal Host Controller Interface
42 *
43 * UHCI spec: http://www.intel.com/
44 */
45
46/* The low level controller code for UHCI has been split into
47 * PCI probes and UHCI specific code. This was done to facilitate the
48 * sharing of code between *BSD's
49 */
50
51#include <sys/stdint.h>
52#include <sys/stddef.h>
53#include <sys/param.h>
54#include <sys/queue.h>
55#include <sys/types.h>
56#include <sys/systm.h>
57#include <sys/kernel.h>
58#include <sys/bus.h>
59#include <sys/linker_set.h>
60#include <sys/module.h>
61#include <sys/lock.h>
62#include <sys/mutex.h>
63#include <sys/condvar.h>
64#include <sys/sysctl.h>
65#include <sys/sx.h>
66#include <sys/unistd.h>
67#include <sys/callout.h>
68#include <sys/malloc.h>
69#include <sys/priv.h>
70
71#include <dev/usb/usb.h>
72#include <dev/usb/usbdi.h>
73
74#include <dev/usb/usb_core.h>
75#include <dev/usb/usb_busdma.h>
76#include <dev/usb/usb_process.h>
77#include <dev/usb/usb_util.h>
78#include <dev/usb/usb_debug.h>
79
80#include <dev/usb/usb_controller.h>
81#include <dev/usb/usb_bus.h>
82#include <dev/usb/usb_pci.h>
83#include <dev/usb/controller/uhci.h>
84
85#define	PCI_UHCI_VENDORID_INTEL		0x8086
86#define	PCI_UHCI_VENDORID_VIA		0x1106
87
88/* PIIX4E has no separate stepping */
89
90#define	PCI_UHCI_BASE_REG               0x20
91
92static device_probe_t uhci_pci_probe;
93static device_attach_t uhci_pci_attach;
94static device_detach_t uhci_pci_detach;
95static device_suspend_t uhci_pci_suspend;
96static device_resume_t uhci_pci_resume;
97
98static int
99uhci_pci_suspend(device_t self)
100{
101	uhci_softc_t *sc = device_get_softc(self);
102	int err;
103
104	err = bus_generic_suspend(self);
105	if (err) {
106		return (err);
107	}
108	uhci_suspend(sc);
109	return (0);
110}
111
112static int
113uhci_pci_resume(device_t self)
114{
115	uhci_softc_t *sc = device_get_softc(self);
116
117	pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
118
119	uhci_resume(sc);
120
121	bus_generic_resume(self);
122	return (0);
123}
124
125static const char *
126uhci_pci_match(device_t self)
127{
128	uint32_t device_id = pci_get_devid(self);
129
130	switch (device_id) {
131	case 0x26888086:
132		return ("Intel 631XESB/632XESB/3100 USB controller USB-1");
133
134	case 0x26898086:
135		return ("Intel 631XESB/632XESB/3100 USB controller USB-2");
136
137	case 0x268a8086:
138		return ("Intel 631XESB/632XESB/3100 USB controller USB-3");
139
140	case 0x268b8086:
141		return ("Intel 631XESB/632XESB/3100 USB controller USB-4");
142
143	case 0x70208086:
144		return ("Intel 82371SB (PIIX3) USB controller");
145
146	case 0x71128086:
147		return ("Intel 82371AB/EB (PIIX4) USB controller");
148
149	case 0x24128086:
150		return ("Intel 82801AA (ICH) USB controller");
151
152	case 0x24228086:
153		return ("Intel 82801AB (ICH0) USB controller");
154
155	case 0x24428086:
156		return ("Intel 82801BA/BAM (ICH2) USB controller USB-A");
157
158	case 0x24448086:
159		return ("Intel 82801BA/BAM (ICH2) USB controller USB-B");
160
161	case 0x24828086:
162		return ("Intel 82801CA/CAM (ICH3) USB controller USB-A");
163
164	case 0x24848086:
165		return ("Intel 82801CA/CAM (ICH3) USB controller USB-B");
166
167	case 0x24878086:
168		return ("Intel 82801CA/CAM (ICH3) USB controller USB-C");
169
170	case 0x24c28086:
171		return ("Intel 82801DB (ICH4) USB controller USB-A");
172
173	case 0x24c48086:
174		return ("Intel 82801DB (ICH4) USB controller USB-B");
175
176	case 0x24c78086:
177		return ("Intel 82801DB (ICH4) USB controller USB-C");
178
179	case 0x24d28086:
180		return ("Intel 82801EB (ICH5) USB controller USB-A");
181
182	case 0x24d48086:
183		return ("Intel 82801EB (ICH5) USB controller USB-B");
184
185	case 0x24d78086:
186		return ("Intel 82801EB (ICH5) USB controller USB-C");
187
188	case 0x24de8086:
189		return ("Intel 82801EB (ICH5) USB controller USB-D");
190
191	case 0x26588086:
192		return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-A");
193
194	case 0x26598086:
195		return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-B");
196
197	case 0x265a8086:
198		return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-C");
199
200	case 0x265b8086:
201		return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-D");
202
203	case 0x28308086:
204		return ("Intel 82801H (ICH8) USB controller USB-A");
205	case 0x28318086:
206		return ("Intel 82801H (ICH8) USB controller USB-B");
207	case 0x28328086:
208		return ("Intel 82801H (ICH8) USB controller USB-C");
209	case 0x28348086:
210		return ("Intel 82801H (ICH8) USB controller USB-D");
211	case 0x28358086:
212		return ("Intel 82801H (ICH8) USB controller USB-E");
213	case 0x29348086:
214		return ("Intel 82801I (ICH9) USB controller");
215	case 0x29358086:
216		return ("Intel 82801I (ICH9) USB controller");
217	case 0x29368086:
218		return ("Intel 82801I (ICH9) USB controller");
219	case 0x29378086:
220		return ("Intel 82801I (ICH9) USB controller");
221	case 0x29388086:
222		return ("Intel 82801I (ICH9) USB controller");
223	case 0x29398086:
224		return ("Intel 82801I (ICH9) USB controller");
225
226	case 0x719a8086:
227		return ("Intel 82443MX USB controller");
228
229	case 0x76028086:
230		return ("Intel 82372FB/82468GX USB controller");
231
232	case 0x30381106:
233		return ("VIA 83C572 USB controller");
234
235	default:
236		break;
237	}
238
239	if ((pci_get_class(self) == PCIC_SERIALBUS) &&
240	    (pci_get_subclass(self) == PCIS_SERIALBUS_USB) &&
241	    (pci_get_progif(self) == PCI_INTERFACE_UHCI)) {
242		return ("UHCI (generic) USB controller");
243	}
244	return (NULL);
245}
246
247static int
248uhci_pci_probe(device_t self)
249{
250	const char *desc = uhci_pci_match(self);
251
252	if (desc) {
253		device_set_desc(self, desc);
254		return (0);
255	} else {
256		return (ENXIO);
257	}
258}
259
260static int
261uhci_pci_attach(device_t self)
262{
263	uhci_softc_t *sc = device_get_softc(self);
264	int rid;
265	int err;
266
267	/* initialise some bus fields */
268	sc->sc_bus.parent = self;
269	sc->sc_bus.devices = sc->sc_devices;
270	sc->sc_bus.devices_max = UHCI_MAX_DEVICES;
271
272	/* get all DMA memory */
273	if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
274	    &uhci_iterate_hw_softc)) {
275		return ENOMEM;
276	}
277	sc->sc_dev = self;
278
279	pci_enable_busmaster(self);
280
281	rid = PCI_UHCI_BASE_REG;
282	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid,
283	    RF_ACTIVE);
284	if (!sc->sc_io_res) {
285		device_printf(self, "Could not map ports\n");
286		goto error;
287	}
288	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
289	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
290	sc->sc_io_size = rman_get_size(sc->sc_io_res);
291
292	/* disable interrupts */
293	bus_space_write_2(sc->sc_io_tag, sc->sc_io_hdl, UHCI_INTR, 0);
294
295	rid = 0;
296	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
297	    RF_SHAREABLE | RF_ACTIVE);
298	if (sc->sc_irq_res == NULL) {
299		device_printf(self, "Could not allocate irq\n");
300		goto error;
301	}
302	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
303	if (!sc->sc_bus.bdev) {
304		device_printf(self, "Could not add USB device\n");
305		goto error;
306	}
307	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
308
309	/*
310	 * uhci_pci_match must never return NULL if uhci_pci_probe
311	 * succeeded
312	 */
313	device_set_desc(sc->sc_bus.bdev, uhci_pci_match(self));
314	switch (pci_get_vendor(self)) {
315	case PCI_UHCI_VENDORID_INTEL:
316		sprintf(sc->sc_vendor, "Intel");
317		break;
318	case PCI_UHCI_VENDORID_VIA:
319		sprintf(sc->sc_vendor, "VIA");
320		break;
321	default:
322		if (bootverbose) {
323			device_printf(self, "(New UHCI DeviceId=0x%08x)\n",
324			    pci_get_devid(self));
325		}
326		sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
327	}
328
329	switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
330	case PCI_USB_REV_PRE_1_0:
331		sc->sc_bus.usbrev = USB_REV_PRE_1_0;
332		break;
333	case PCI_USB_REV_1_0:
334		sc->sc_bus.usbrev = USB_REV_1_0;
335		break;
336	default:
337		/* Quirk for Parallels Desktop 4.0 */
338		device_printf(self, "USB revision is unknown. Assuming v1.1.\n");
339		sc->sc_bus.usbrev = USB_REV_1_1;
340		break;
341	}
342
343#if (__FreeBSD_version >= 700031)
344	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
345	    NULL, (driver_intr_t *)uhci_interrupt, sc, &sc->sc_intr_hdl);
346#else
347	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
348	    (driver_intr_t *)uhci_interrupt, sc, &sc->sc_intr_hdl);
349#endif
350
351	if (err) {
352		device_printf(self, "Could not setup irq, %d\n", err);
353		sc->sc_intr_hdl = NULL;
354		goto error;
355	}
356	/*
357	 * Set the PIRQD enable bit and switch off all the others. We don't
358	 * want legacy support to interfere with us XXX Does this also mean
359	 * that the BIOS won't touch the keyboard anymore if it is connected
360	 * to the ports of the root hub?
361	 */
362#ifdef USB_DEBUG
363	if (pci_read_config(self, PCI_LEGSUP, 2) != PCI_LEGSUP_USBPIRQDEN) {
364		device_printf(self, "LegSup = 0x%04x\n",
365		    pci_read_config(self, PCI_LEGSUP, 2));
366	}
367#endif
368	pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
369
370	err = uhci_init(sc);
371	if (!err) {
372		err = device_probe_and_attach(sc->sc_bus.bdev);
373	}
374	if (err) {
375		device_printf(self, "USB init failed\n");
376		goto error;
377	}
378	return (0);
379
380error:
381	uhci_pci_detach(self);
382	return (ENXIO);
383}
384
385int
386uhci_pci_detach(device_t self)
387{
388	uhci_softc_t *sc = device_get_softc(self);
389	device_t bdev;
390
391	if (sc->sc_bus.bdev) {
392		bdev = sc->sc_bus.bdev;
393		device_detach(bdev);
394		device_delete_child(self, bdev);
395	}
396	/* during module unload there are lots of children leftover */
397	device_delete_all_children(self);
398
399	/*
400	 * disable interrupts that might have been switched on in
401	 * uhci_init.
402	 */
403	if (sc->sc_io_res) {
404		USB_BUS_LOCK(&sc->sc_bus);
405
406		/* stop the controller */
407		uhci_reset(sc);
408
409		USB_BUS_UNLOCK(&sc->sc_bus);
410	}
411	pci_disable_busmaster(self);
412
413	if (sc->sc_irq_res && sc->sc_intr_hdl) {
414		int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
415
416		if (err) {
417			/* XXX or should we panic? */
418			device_printf(self, "Could not tear down irq, %d\n",
419			    err);
420		}
421		sc->sc_intr_hdl = NULL;
422	}
423	if (sc->sc_irq_res) {
424		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
425		sc->sc_irq_res = NULL;
426	}
427	if (sc->sc_io_res) {
428		bus_release_resource(self, SYS_RES_IOPORT, PCI_UHCI_BASE_REG,
429		    sc->sc_io_res);
430		sc->sc_io_res = NULL;
431	}
432	usb_bus_mem_free_all(&sc->sc_bus, &uhci_iterate_hw_softc);
433
434	return (0);
435}
436
437static driver_t uhci_driver =
438{
439	.name = "uhci",
440	.methods = (device_method_t[]){
441		/* device interface */
442		DEVMETHOD(device_probe, uhci_pci_probe),
443		DEVMETHOD(device_attach, uhci_pci_attach),
444		DEVMETHOD(device_detach, uhci_pci_detach),
445
446		DEVMETHOD(device_suspend, uhci_pci_suspend),
447		DEVMETHOD(device_resume, uhci_pci_resume),
448		DEVMETHOD(device_shutdown, bus_generic_shutdown),
449
450		/* Bus interface */
451		DEVMETHOD(bus_print_child, bus_generic_print_child),
452		{0, 0}
453	},
454	.size = sizeof(struct uhci_softc),
455};
456
457static devclass_t uhci_devclass;
458
459DRIVER_MODULE(uhci, pci, uhci_driver, uhci_devclass, 0, 0);
460MODULE_DEPEND(uhci, usb, 1, 1, 1);
461