xref: /netbsd-src/sys/dev/pci/uhci_pci.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: uhci_pci.c,v 1.40 2007/12/09 20:28:13 jmcneill 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.40 2007/12/09 20:28:13 jmcneill 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);
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(struct device *parent, struct cfdata *match,
79     void *aux)
80 {
81 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
82 
83 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
84 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
85 	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_UHCI)
86 		return (1);
87 
88 	return (0);
89 }
90 
91 static void
92 uhci_pci_attach(struct device *parent, struct device *self, void *aux)
93 {
94 	struct uhci_pci_softc *sc = (struct uhci_pci_softc *)self;
95 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
96 	pci_chipset_tag_t pc = pa->pa_pc;
97 	pcitag_t tag = pa->pa_tag;
98 	char const *intrstr;
99 	pci_intr_handle_t ih;
100 	pcireg_t csr;
101 	const char *vendor;
102 	const char *devname = sc->sc.sc_bus.bdev.dv_xname;
103 	char devinfo[256];
104 	usbd_status r;
105 	int s;
106 
107 	aprint_naive("\n");
108 
109 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
110 	aprint_normal(": %s (rev. 0x%02x)\n",
111 		      devinfo, PCI_REVISION(pa->pa_class));
112 
113 	/* Map I/O registers */
114 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
115 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
116 		aprint_error("%s: can't map i/o space\n", devname);
117 		return;
118 	}
119 
120 	/*
121 	 * Disable interrupts, so we don't get any spurious ones.
122 	 * Acknowledge all pending interrupts.
123 	 */
124 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
125 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_STS,
126 	    bus_space_read_2(sc->sc.iot, sc->sc.ioh, UHCI_STS));
127 
128 	sc->sc_pc = pc;
129 	sc->sc_tag = tag;
130 	sc->sc.sc_bus.dmatag = pa->pa_dmat;
131 
132 	/* Enable the device. */
133 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
134 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
135 		       csr | PCI_COMMAND_MASTER_ENABLE);
136 
137 	/* Map and establish the interrupt. */
138 	if (pci_intr_map(pa, &ih)) {
139 		aprint_error("%s: couldn't map interrupt\n", devname);
140 		return;
141 	}
142 	intrstr = pci_intr_string(pc, ih);
143 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, uhci_intr, sc);
144 	if (sc->sc_ih == NULL) {
145 		aprint_error("%s: couldn't establish interrupt", devname);
146 		if (intrstr != NULL)
147 			aprint_normal(" at %s", intrstr);
148 		aprint_normal("\n");
149 		return;
150 	}
151 	aprint_normal("%s: interrupting at %s\n", devname, intrstr);
152 
153 	/*
154 	 * Set LEGSUP register to its default value.
155 	 * This can re-enable or trigger interrupts, so protect against
156 	 * them and explicitly disable and ACK them afterwards.
157 	 */
158 	s = splhardusb();
159 	pci_conf_write(pc, tag, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN);
160 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
161 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_STS,
162 	    bus_space_read_2(sc->sc.iot, sc->sc.ioh, UHCI_STS));
163 	splx(s);
164 
165 	switch(pci_conf_read(pc, tag, PCI_USBREV) & PCI_USBREV_MASK) {
166 	case PCI_USBREV_PRE_1_0:
167 		sc->sc.sc_bus.usbrev = USBREV_PRE_1_0;
168 		break;
169 	case PCI_USBREV_1_0:
170 		sc->sc.sc_bus.usbrev = USBREV_1_0;
171 		break;
172 	case PCI_USBREV_1_1:
173 		sc->sc.sc_bus.usbrev = USBREV_1_1;
174 		break;
175 	default:
176 		sc->sc.sc_bus.usbrev = USBREV_UNKNOWN;
177 		break;
178 	}
179 
180 	/* Figure out vendor for root hub descriptor. */
181 	vendor = pci_findvendor(pa->pa_id);
182 	sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
183 	if (vendor)
184 		strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
185 	else
186 		snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
187 		    "vendor 0x%04x", PCI_VENDOR(pa->pa_id));
188 
189 	r = uhci_init(&sc->sc);
190 	if (r != USBD_NORMAL_COMPLETION) {
191 		aprint_error("%s: init failed, error=%d\n", devname, r);
192 		return;
193 	}
194 
195 #if NEHCI > 0
196 	usb_pci_add(&sc->sc_pci, pa, &sc->sc.sc_bus);
197 #endif
198 
199 	if (!pmf_device_register(self, uhci_suspend, uhci_pci_resume))
200 		aprint_error_dev(self, "couldn't establish power handler\n");
201 
202 	/* Attach usb device. */
203 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
204 				       usbctlprint);
205 }
206 
207 static int
208 uhci_pci_detach(device_ptr_t self, int flags)
209 {
210 	struct uhci_pci_softc *sc = (struct uhci_pci_softc *)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)
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);
242 }
243 
244 CFATTACH_DECL(uhci_pci, sizeof(struct uhci_pci_softc),
245     uhci_pci_match, uhci_pci_attach, uhci_pci_detach, uhci_activate);
246