xref: /openbsd-src/sys/dev/pci/uhci_pci.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: uhci_pci.c,v 1.14 2003/07/08 13:19:08 nate Exp $	*/
2 /*	$NetBSD: uhci_pci.c,v 1.24 2002/10/02 16:51:58 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/device.h>
45 #include <sys/proc.h>
46 #include <sys/queue.h>
47 
48 #include <machine/bus.h>
49 
50 #include <dev/pci/pcivar.h>
51 
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdivar.h>
55 #include <dev/usb/usb_mem.h>
56 
57 #include <dev/usb/uhcireg.h>
58 #include <dev/usb/uhcivar.h>
59 
60 int	uhci_pci_match(struct device *, void *, void *);
61 void	uhci_pci_attach(struct device *, struct device *, void *);
62 int	uhci_pci_detach(device_ptr_t, int);
63 
64 struct uhci_pci_softc {
65 	uhci_softc_t		sc;
66 	pci_chipset_tag_t	sc_pc;
67 	pcitag_t		sc_tag;
68 	void 			*sc_ih;		/* interrupt vectoring */
69 };
70 
71 struct cfattach uhci_pci_ca = {
72 	sizeof(struct uhci_pci_softc), uhci_pci_match, uhci_pci_attach,
73 	uhci_pci_detach, uhci_activate
74 };
75 
76 int
77 uhci_pci_match(struct device *parent, void *match, void *aux)
78 {
79 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
80 
81 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
82 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
83 	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_UHCI)
84 		return (1);
85 
86 	return (0);
87 }
88 
89 void
90 uhci_pci_attach(struct device *parent, struct device *self, void *aux)
91 {
92 	struct uhci_pci_softc *sc = (struct uhci_pci_softc *)self;
93 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
94 	pci_chipset_tag_t pc = pa->pa_pc;
95 	pcitag_t tag = pa->pa_tag;
96 	char const *intrstr;
97 	pci_intr_handle_t ih;
98 	pcireg_t csr;
99 	const char *vendor;
100 	char *devname = sc->sc.sc_bus.bdev.dv_xname;
101 	usbd_status r;
102 
103 #if defined(__NetBSD__)
104 	char devinfo[256];
105 
106 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
107 	printf(": %s (rev. 0x%02x)", devinfo, PCI_REVISION(pa->pa_class));
108 #endif
109 
110 	/* Map I/O registers */
111 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
112 		    &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size, 0)) {
113 		printf("%s: can't map i/o space\n", devname);
114 		return;
115 	}
116 
117 	/* Disable interrupts, so we don't get any spurious ones. */
118 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
119 
120 	sc->sc_pc = pc;
121 	sc->sc_tag = tag;
122 	sc->sc.sc_bus.dmatag = pa->pa_dmat;
123 
124 	/* Enable the device. */
125 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
126 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
127 		       csr | PCI_COMMAND_MASTER_ENABLE);
128 
129 	/* Map and establish the interrupt. */
130 	if (pci_intr_map(pa, &ih)) {
131 		printf("%s: couldn't map interrupt\n", devname);
132 		return;
133 	}
134 	intrstr = pci_intr_string(pc, ih);
135 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, uhci_intr, sc,
136 				       devname);
137 	if (sc->sc_ih == NULL) {
138 		printf(": couldn't establish interrupt", devname);
139 		if (intrstr != NULL)
140 			printf(" at %s", intrstr);
141 		printf("\n");
142 		return;
143 	}
144 	printf(": interrupting at %s\n", intrstr);
145 
146 	/* Set LEGSUP register to its default value. */
147 	pci_conf_write(pc, tag, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN);
148 
149 	switch(pci_conf_read(pc, tag, PCI_USBREV) & PCI_USBREV_MASK) {
150 	case PCI_USBREV_PRE_1_0:
151 		sc->sc.sc_bus.usbrev = USBREV_PRE_1_0;
152 		break;
153 	case PCI_USBREV_1_0:
154 		sc->sc.sc_bus.usbrev = USBREV_1_0;
155 		break;
156 	case PCI_USBREV_1_1:
157 		sc->sc.sc_bus.usbrev = USBREV_1_1;
158 		break;
159 	default:
160 		sc->sc.sc_bus.usbrev = USBREV_UNKNOWN;
161 		break;
162 	}
163 
164 	uhci_run(&sc->sc, 0);			/* stop the controller */
165 						/* disable interrupts */
166 	bus_space_barrier(sc->sc.iot, sc->sc.ioh, 0, sc->sc.sc_size,
167 	    BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
168 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
169 
170 	/* Figure out vendor for root hub descriptor. */
171 	vendor = pci_findvendor(pa->pa_id);
172 	sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
173 	if (vendor)
174 		strncpy(sc->sc.sc_vendor, vendor,
175 			sizeof(sc->sc.sc_vendor) - 1);
176 	else
177 		snprintf(sc->sc.sc_vendor, sizeof sc->sc.sc_vendor,
178 			"vendor 0x%04x", PCI_VENDOR(pa->pa_id));
179 
180 	r = uhci_init(&sc->sc);
181 	if (r != USBD_NORMAL_COMPLETION) {
182 		printf("%s: init failed, error=%d\n", devname, r);
183 		return;
184 	}
185 
186 	/* Attach usb device. */
187 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
188 				       usbctlprint);
189 }
190 
191 int
192 uhci_pci_detach(device_ptr_t self, int flags)
193 {
194 	struct uhci_pci_softc *sc = (struct uhci_pci_softc *)self;
195 	int rv;
196 
197 	rv = uhci_detach(&sc->sc, flags);
198 	if (rv)
199 		return (rv);
200 	if (sc->sc_ih != NULL) {
201 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
202 		sc->sc_ih = NULL;
203 	}
204 	if (sc->sc.sc_size) {
205 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
206 		sc->sc.sc_size = 0;
207 	}
208 
209 	return (0);
210 }
211