xref: /netbsd-src/sys/dev/pci/uhci_pci.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: uhci_pci.c,v 1.43 2008/03/28 17:14:46 drochner 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.43 2008/03/28 17:14:46 drochner 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 	sc->sc.sc_dev = self;
106 	sc->sc.sc_bus.hci_private = sc;
107 
108 	aprint_naive("\n");
109 
110 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
111 	aprint_normal(": %s (rev. 0x%02x)\n",
112 		      devinfo, PCI_REVISION(pa->pa_class));
113 
114 	/* Map I/O registers */
115 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
116 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
117 		aprint_error_dev(self, "can't map i/o space\n");
118 		return;
119 	}
120 
121 	/*
122 	 * Disable interrupts, so we don't get any spurious ones.
123 	 * Acknowledge all pending interrupts.
124 	 */
125 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
126 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_STS,
127 	    bus_space_read_2(sc->sc.iot, sc->sc.ioh, UHCI_STS));
128 
129 	sc->sc_pc = pc;
130 	sc->sc_tag = tag;
131 	sc->sc.sc_bus.dmatag = pa->pa_dmat;
132 
133 	/* Enable the device. */
134 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
135 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
136 		       csr | PCI_COMMAND_MASTER_ENABLE);
137 
138 	/* Map and establish the interrupt. */
139 	if (pci_intr_map(pa, &ih)) {
140 		aprint_error_dev(self, "couldn't map interrupt\n");
141 		return;
142 	}
143 	intrstr = pci_intr_string(pc, ih);
144 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, uhci_intr, sc);
145 	if (sc->sc_ih == NULL) {
146 		aprint_error_dev(self, "couldn't establish interrupt");
147 		if (intrstr != NULL)
148 			aprint_normal(" at %s", intrstr);
149 		aprint_normal("\n");
150 		return;
151 	}
152 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
153 
154 	/*
155 	 * Set LEGSUP register to its default value.
156 	 * This can re-enable or trigger interrupts, so protect against
157 	 * them and explicitly disable and ACK them afterwards.
158 	 */
159 	s = splhardusb();
160 	pci_conf_write(pc, tag, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN);
161 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
162 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_STS,
163 	    bus_space_read_2(sc->sc.iot, sc->sc.ioh, UHCI_STS));
164 	splx(s);
165 
166 	switch(pci_conf_read(pc, tag, PCI_USBREV) & PCI_USBREV_MASK) {
167 	case PCI_USBREV_PRE_1_0:
168 		sc->sc.sc_bus.usbrev = USBREV_PRE_1_0;
169 		break;
170 	case PCI_USBREV_1_0:
171 		sc->sc.sc_bus.usbrev = USBREV_1_0;
172 		break;
173 	case PCI_USBREV_1_1:
174 		sc->sc.sc_bus.usbrev = USBREV_1_1;
175 		break;
176 	default:
177 		sc->sc.sc_bus.usbrev = USBREV_UNKNOWN;
178 		break;
179 	}
180 
181 	/* Figure out vendor for root hub descriptor. */
182 	vendor = pci_findvendor(pa->pa_id);
183 	sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
184 	if (vendor)
185 		strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
186 	else
187 		snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
188 		    "vendor 0x%04x", PCI_VENDOR(pa->pa_id));
189 
190 	r = uhci_init(&sc->sc);
191 	if (r != USBD_NORMAL_COMPLETION) {
192 		aprint_error_dev(self, "init failed, error=%d\n", r);
193 		return;
194 	}
195 
196 #if NEHCI > 0
197 	usb_pci_add(&sc->sc_pci, pa, self);
198 #endif
199 
200 	if (!pmf_device_register(self, uhci_suspend, uhci_pci_resume))
201 		aprint_error_dev(self, "couldn't establish power handler\n");
202 
203 	/* Attach usb device. */
204 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
205 }
206 
207 static int
208 uhci_pci_detach(device_t self, int flags)
209 {
210 	struct uhci_pci_softc *sc = device_private(self);
211 	int rv;
212 
213 	pmf_device_deregister(self);
214 
215 	rv = uhci_detach(&sc->sc, flags);
216 	if (rv)
217 		return (rv);
218 	if (sc->sc_ih != NULL) {
219 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
220 		sc->sc_ih = NULL;
221 	}
222 	if (sc->sc.sc_size) {
223 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
224 		sc->sc.sc_size = 0;
225 	}
226 #if NEHCI > 0
227 	usb_pci_rem(&sc->sc_pci);
228 #endif
229 	return (0);
230 }
231 
232 static bool
233 uhci_pci_resume(device_t dv PMF_FN_ARGS)
234 {
235 	struct uhci_pci_softc *sc = device_private(dv);
236 
237 	/* Set LEGSUP register to its default value. */
238 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_LEGSUP,
239 	    PCI_LEGSUP_USBPIRQDEN);
240 
241 	return uhci_resume(dv PMF_FN_CALL);
242 }
243 
244 CFATTACH_DECL2_NEW(uhci_pci, sizeof(struct uhci_pci_softc),
245     uhci_pci_match, uhci_pci_attach, uhci_pci_detach, uhci_activate,
246     NULL, uhci_childdet);
247