xref: /openbsd-src/sys/dev/pci/ohci_pci.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: ohci_pci.c,v 1.33 2008/06/26 05:42:17 ray Exp $	*/
2 /*	$NetBSD: ohci_pci.c,v 1.23 2002/10/02 16:51:47 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  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * USB Open Host Controller driver.
36  *
37  * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
38  * USB spec: http://www.teleport.com/cgi-bin/mailmerge.cgi/~usb/cgiform.tpl
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/ohcireg.h>
58 #include <dev/usb/ohcivar.h>
59 
60 int	ohci_pci_match(struct device *, void *, void *);
61 void	ohci_pci_attach(struct device *, struct device *, void *);
62 void	ohci_pci_attach_deferred(struct device *);
63 int	ohci_pci_detach(struct device *, int);
64 
65 struct ohci_pci_softc {
66 	ohci_softc_t		sc;
67 	pci_chipset_tag_t	sc_pc;
68 	void 			*sc_ih;		/* interrupt vectoring */
69 };
70 
71 struct cfattach ohci_pci_ca = {
72 	sizeof(struct ohci_pci_softc), ohci_pci_match, ohci_pci_attach,
73 	ohci_pci_detach, ohci_activate
74 };
75 
76 int
77 ohci_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_OHCI)
84 		return (1);
85 
86 	return (0);
87 }
88 
89 void
90 ohci_pci_attach(struct device *parent, struct device *self, void *aux)
91 {
92 	struct ohci_pci_softc *sc = (struct ohci_pci_softc *)self;
93 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
94 	pci_chipset_tag_t pc = pa->pa_pc;
95 	char const *intrstr;
96 	pci_intr_handle_t ih;
97 	int s;
98 	const char *vendor;
99 	char *devname = sc->sc.sc_bus.bdev.dv_xname;
100 
101 	/* Map I/O registers */
102 	if (pci_mapreg_map(pa, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
103 		    &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size, 0)) {
104 		printf(": can't map mem space\n");
105 		return;
106 	}
107 
108 	/* Record what interrupts were enabled by SMM/BIOS. */
109 	sc->sc.sc_intre = bus_space_read_4(sc->sc.iot, sc->sc.ioh,
110 	    OHCI_INTERRUPT_ENABLE);
111 
112 	/* Disable interrupts, so we don't get any spurious ones. */
113 	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
114 			  OHCI_MIE);
115 
116 	sc->sc_pc = pc;
117 	sc->sc.sc_bus.dmatag = pa->pa_dmat;
118 
119 	bus_space_barrier(sc->sc.iot, sc->sc.ioh, 0, sc->sc.sc_size,
120 	    BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
121 	bus_space_write_4(sc->sc.iot, sc->sc.ioh,
122 	    OHCI_INTERRUPT_DISABLE, OHCI_MIE);
123 
124 	s = splusb();
125 	/* Map and establish the interrupt. */
126 	if (pci_intr_map(pa, &ih)) {
127 		printf(": couldn't map interrupt\n");
128 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
129 		splx(s);
130 		return;
131 	}
132 
133 	intrstr = pci_intr_string(pc, ih);
134 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ohci_intr, sc, devname);
135 	if (sc->sc_ih == NULL) {
136 		printf(": couldn't establish interrupt");
137 		if (intrstr != NULL)
138 			printf(" at %s", intrstr);
139 		printf("\n");
140 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
141 		splx(s);
142 		return;
143 	}
144 	printf(": %s", intrstr);
145 
146 	/* Figure out vendor for root hub descriptor. */
147 	vendor = pci_findvendor(pa->pa_id);
148 	sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
149 	if (vendor)
150 		strlcpy(sc->sc.sc_vendor, vendor, sizeof (sc->sc.sc_vendor));
151 	else
152 		snprintf(sc->sc.sc_vendor, sizeof (sc->sc.sc_vendor),
153 		    "vendor 0x%04x", PCI_VENDOR(pa->pa_id));
154 
155 	/* Display revision and perform legacy emulation handover. */
156 	if (ohci_checkrev(&sc->sc) != USBD_NORMAL_COMPLETION ||
157 	    ohci_handover(&sc->sc) != USBD_NORMAL_COMPLETION) {
158 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
159 		splx(s);
160 		return;
161 	}
162 
163 	/* Ignore interrupts for now */
164 	sc->sc.sc_dying = 1;
165 
166 	config_defer(self, ohci_pci_attach_deferred);
167 
168 	splx(s);
169 
170 	return;
171 }
172 
173 void
174 ohci_pci_attach_deferred(struct device *self)
175 {
176 	struct ohci_pci_softc *sc = (struct ohci_pci_softc *)self;
177 	usbd_status r;
178 	int s;
179 
180 	s = splusb();
181 
182 	sc->sc.sc_dying = 0;
183 
184 	r = ohci_init(&sc->sc);
185 	if (r != USBD_NORMAL_COMPLETION) {
186 		printf("%s: init failed, error=%d\n",
187 		    sc->sc.sc_bus.bdev.dv_xname, r);
188 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
189 		splx(s);
190 		return;
191 	}
192 
193 	sc->sc.sc_powerhook = powerhook_establish(ohci_power, &sc->sc);
194 	if (sc->sc.sc_powerhook == NULL)
195 		printf("%s: unable to establish powerhook\n",
196 		    sc->sc.sc_bus.bdev.dv_xname);
197 
198 	splx(s);
199 
200 	/* Attach usb device. */
201 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
202 				       usbctlprint);
203 }
204 
205 int
206 ohci_pci_detach(struct device *self, int flags)
207 {
208 	struct ohci_pci_softc *sc = (struct ohci_pci_softc *)self;
209 	int rv;
210 
211 	rv = ohci_detach(&sc->sc, flags);
212 	if (rv)
213 		return (rv);
214 
215 	if (sc->sc.sc_powerhook != NULL)
216 		powerhook_disestablish(sc->sc.sc_powerhook);
217 
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 	return (0);
227 }
228