xref: /netbsd-src/sys/dev/pci/uhci_pci.c (revision e6d6e05cb173f30287ab619b21120b27baa66ad6)
1 /*	$NetBSD: uhci_pci.c,v 1.41 2008/02/22 23:04:52 dyoung Exp $	*/
2 
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 (lennart@augustsson.net) 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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: uhci_pci.c,v 1.41 2008/02/22 23:04:52 dyoung Exp $");
42 
43 #include "ehci.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/device.h>
49 #include <sys/proc.h>
50 #include <sys/queue.h>
51 
52 #include <sys/bus.h>
53 
54 #include <dev/pci/pcivar.h>
55 #include <dev/pci/usb_pci.h>
56 
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdivar.h>
60 #include <dev/usb/usb_mem.h>
61 
62 #include <dev/usb/uhcireg.h>
63 #include <dev/usb/uhcivar.h>
64 
65 static bool	uhci_pci_resume(device_t PMF_FN_PROTO);
66 
67 struct uhci_pci_softc {
68 	uhci_softc_t		sc;
69 #if NEHCI > 0
70 	struct usb_pci		sc_pci;
71 #endif
72 	pci_chipset_tag_t	sc_pc;
73 	pcitag_t		sc_tag;
74 	void 			*sc_ih;		/* interrupt vectoring */
75 };
76 
77 static int
78 uhci_pci_match(device_t parent, struct cfdata *match, void *aux)
79 {
80 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
81 
82 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
83 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
84 	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_UHCI)
85 		return (1);
86 
87 	return (0);
88 }
89 
90 static void
91 uhci_pci_attach(device_t parent, device_t self, void *aux)
92 {
93 	struct uhci_pci_softc *sc = device_private(self);
94 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
95 	pci_chipset_tag_t pc = pa->pa_pc;
96 	pcitag_t tag = pa->pa_tag;
97 	char const *intrstr;
98 	pci_intr_handle_t ih;
99 	pcireg_t csr;
100 	const char *vendor;
101 	char devinfo[256];
102 	usbd_status r;
103 	int s;
104 
105 	aprint_naive("\n");
106 
107 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
108 	aprint_normal(": %s (rev. 0x%02x)\n",
109 		      devinfo, PCI_REVISION(pa->pa_class));
110 
111 	/* Map I/O registers */
112 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
113 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
114 		aprint_error_dev(self, "can't map i/o space\n");
115 		return;
116 	}
117 
118 	/*
119 	 * Disable interrupts, so we don't get any spurious ones.
120 	 * Acknowledge all pending interrupts.
121 	 */
122 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
123 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_STS,
124 	    bus_space_read_2(sc->sc.iot, sc->sc.ioh, UHCI_STS));
125 
126 	sc->sc_pc = pc;
127 	sc->sc_tag = tag;
128 	sc->sc.sc_bus.dmatag = pa->pa_dmat;
129 
130 	/* Enable the device. */
131 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
132 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
133 		       csr | PCI_COMMAND_MASTER_ENABLE);
134 
135 	/* Map and establish the interrupt. */
136 	if (pci_intr_map(pa, &ih)) {
137 		aprint_error_dev(self, "couldn't map interrupt\n");
138 		return;
139 	}
140 	intrstr = pci_intr_string(pc, ih);
141 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, uhci_intr, sc);
142 	if (sc->sc_ih == NULL) {
143 		aprint_error_dev(self, "couldn't establish interrupt");
144 		if (intrstr != NULL)
145 			aprint_normal(" at %s", intrstr);
146 		aprint_normal("\n");
147 		return;
148 	}
149 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
150 
151 	/*
152 	 * Set LEGSUP register to its default value.
153 	 * This can re-enable or trigger interrupts, so protect against
154 	 * them and explicitly disable and ACK them afterwards.
155 	 */
156 	s = splhardusb();
157 	pci_conf_write(pc, tag, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN);
158 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
159 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_STS,
160 	    bus_space_read_2(sc->sc.iot, sc->sc.ioh, UHCI_STS));
161 	splx(s);
162 
163 	switch(pci_conf_read(pc, tag, PCI_USBREV) & PCI_USBREV_MASK) {
164 	case PCI_USBREV_PRE_1_0:
165 		sc->sc.sc_bus.usbrev = USBREV_PRE_1_0;
166 		break;
167 	case PCI_USBREV_1_0:
168 		sc->sc.sc_bus.usbrev = USBREV_1_0;
169 		break;
170 	case PCI_USBREV_1_1:
171 		sc->sc.sc_bus.usbrev = USBREV_1_1;
172 		break;
173 	default:
174 		sc->sc.sc_bus.usbrev = USBREV_UNKNOWN;
175 		break;
176 	}
177 
178 	/* Figure out vendor for root hub descriptor. */
179 	vendor = pci_findvendor(pa->pa_id);
180 	sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
181 	if (vendor)
182 		strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
183 	else
184 		snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
185 		    "vendor 0x%04x", PCI_VENDOR(pa->pa_id));
186 
187 	r = uhci_init(&sc->sc);
188 	if (r != USBD_NORMAL_COMPLETION) {
189 		aprint_error_dev(self, "init failed, error=%d\n", r);
190 		return;
191 	}
192 
193 #if NEHCI > 0
194 	usb_pci_add(&sc->sc_pci, pa, &sc->sc.sc_bus);
195 #endif
196 
197 	if (!pmf_device_register(self, uhci_suspend, uhci_pci_resume))
198 		aprint_error_dev(self, "couldn't establish power handler\n");
199 
200 	/* Attach usb device. */
201 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
202 				       usbctlprint);
203 }
204 
205 static int
206 uhci_pci_detach(device_t self, int flags)
207 {
208 	struct uhci_pci_softc *sc = device_private(self);
209 	int rv;
210 
211 	pmf_device_deregister(self);
212 
213 	rv = uhci_detach(&sc->sc, flags);
214 	if (rv)
215 		return (rv);
216 	if (sc->sc_ih != NULL) {
217 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
218 		sc->sc_ih = NULL;
219 	}
220 	if (sc->sc.sc_size) {
221 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
222 		sc->sc.sc_size = 0;
223 	}
224 #if NEHCI > 0
225 	usb_pci_rem(&sc->sc_pci);
226 #endif
227 	return (0);
228 }
229 
230 static bool
231 uhci_pci_resume(device_t dv PMF_FN_ARGS)
232 {
233 	struct uhci_pci_softc *sc = device_private(dv);
234 
235 	/* Set LEGSUP register to its default value. */
236 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_LEGSUP,
237 	    PCI_LEGSUP_USBPIRQDEN);
238 
239 	return uhci_resume(dv PMF_FN_CALL);
240 }
241 
242 CFATTACH_DECL2(uhci_pci, sizeof(struct uhci_pci_softc),
243     uhci_pci_match, uhci_pci_attach, uhci_pci_detach, uhci_activate,
244     NULL, uhci_childdet);
245