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