xref: /openbsd-src/sys/dev/pci/ohci_pci.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: ohci_pci.c,v 1.18 2003/07/08 13:19:08 nate 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  * 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 /*
42  * USB Open Host Controller driver.
43  *
44  * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
45  * USB spec: http://www.teleport.com/cgi-bin/mailmerge.cgi/~usb/cgiform.tpl
46  */
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/device.h>
52 #include <sys/proc.h>
53 #include <sys/queue.h>
54 
55 #include <machine/bus.h>
56 
57 #include <dev/pci/pcivar.h>
58 
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdivar.h>
62 #include <dev/usb/usb_mem.h>
63 
64 #include <dev/usb/ohcireg.h>
65 #include <dev/usb/ohcivar.h>
66 
67 int	ohci_pci_match(struct device *, void *, void *);
68 void	ohci_pci_attach(struct device *, struct device *, void *);
69 int	ohci_pci_detach(device_ptr_t, int);
70 
71 struct ohci_pci_softc {
72 	ohci_softc_t		sc;
73 	pci_chipset_tag_t	sc_pc;
74 	void 			*sc_ih;		/* interrupt vectoring */
75 };
76 
77 struct cfattach ohci_pci_ca = {
78 	sizeof(struct ohci_pci_softc), ohci_pci_match, ohci_pci_attach,
79 	ohci_pci_detach, ohci_activate
80 };
81 
82 int
83 ohci_pci_match(struct device *parent, void *match, void *aux)
84 {
85 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
86 
87 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
88 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
89 	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_OHCI)
90 		return (1);
91 
92 	return (0);
93 }
94 
95 void
96 ohci_pci_attach(struct device *parent, struct device *self, void *aux)
97 {
98 	struct ohci_pci_softc *sc = (struct ohci_pci_softc *)self;
99 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
100 	pci_chipset_tag_t pc = pa->pa_pc;
101 	pcitag_t tag = pa->pa_tag;
102 	char const *intrstr;
103 	pci_intr_handle_t ih;
104 	pcireg_t csr;
105 	usbd_status r;
106 	int s;
107 	const char *vendor;
108 	char *devname = sc->sc.sc_bus.bdev.dv_xname;
109 
110 #if defined(__NetBSD__)
111 	char devinfo[256];
112 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
113 	printf(": %s (rev. 0x%02x)", devinfo, PCI_REVISION(pa->pa_class));
114 #endif
115 
116 	/* Map I/O registers */
117 	if (pci_mapreg_map(pa, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
118 		    &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size, 0)) {
119 		printf("%s: can't map mem space\n", devname);
120 		return;
121 	}
122 
123 	/* Disable interrupts, so we don't get any spurious ones. */
124 	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
125 			  OHCI_MIE);
126 
127 	sc->sc_pc = pc;
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 	bus_space_barrier(sc->sc.iot, sc->sc.ioh, 0, sc->sc.sc_size,
136 	    BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
137 	bus_space_write_4(sc->sc.iot, sc->sc.ioh,
138 	    OHCI_INTERRUPT_DISABLE, OHCI_MIE);
139 
140 	s = splusb();
141 	/* Map and establish the interrupt. */
142 	if (pci_intr_map(pa, &ih)) {
143 		printf(": couldn't map interrupt\n");
144 		splx(s);
145 		return;
146 	}
147 
148 	intrstr = pci_intr_string(pc, ih);
149 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ohci_intr, sc,
150 				       devname);
151 	if (sc->sc_ih == NULL) {
152 		printf("%s: couldn't establish interrupt", devname);
153 		if (intrstr != NULL)
154 			printf(" at %s", intrstr);
155 		printf("\n");
156 		splx(s);
157 		return;
158 	}
159 	printf(": interrupting at %s", intrstr);
160 
161 	/* Figure out vendor for root hub descriptor. */
162 	vendor = pci_findvendor(pa->pa_id);
163 	sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
164 	if (vendor)
165 		strncpy(sc->sc.sc_vendor, vendor,
166 			sizeof(sc->sc.sc_vendor) - 1);
167 	else
168 		snprintf(sc->sc.sc_vendor, sizeof sc->sc.sc_vendor,
169 		    "vendor 0x%04x", PCI_VENDOR(pa->pa_id));
170 
171 	r = ohci_init(&sc->sc);
172 	if (r != USBD_NORMAL_COMPLETION) {
173 		printf("%s: init failed, error=%d\n",
174 		    sc->sc.sc_bus.bdev.dv_xname, r);
175 		splx(s);
176 		return;
177 	}
178 	splx(s);
179 
180 	/* Attach usb device. */
181 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
182 				       usbctlprint);
183 }
184 
185 int
186 ohci_pci_detach(device_ptr_t self, int flags)
187 {
188 	struct ohci_pci_softc *sc = (struct ohci_pci_softc *)self;
189 	int rv;
190 
191 	rv = ohci_detach(&sc->sc, flags);
192 	if (rv)
193 		return (rv);
194 	if (sc->sc_ih != NULL) {
195 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
196 		sc->sc_ih = NULL;
197 	}
198 	if (sc->sc.sc_size) {
199 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
200 		sc->sc.sc_size = 0;
201 	}
202 	return (0);
203 }
204