xref: /openbsd-src/sys/dev/pci/ohci_pci.c (revision 8445c53715e7030056b779e8ab40efb7820981f2)
1 /*	$OpenBSD: ohci_pci.c,v 1.14 2001/09/15 20:57:33 drahn Exp $	*/
2 /*	$NetBSD: ohci_pci.c,v 1.9 1999/05/20 09:52:35 augustss 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 __P((struct device *, void *, void *));
68 void	ohci_pci_attach __P((struct device *, struct device *, void *));
69 int	ohci_pci_detach __P((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(parent, match, aux)
84 	struct device *parent;
85 	void *match, *aux;
86 {
87 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
88 
89 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
90 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
91 	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_OHCI)
92 		return (1);
93 
94 	return (0);
95 }
96 
97 void
98 ohci_pci_attach(parent, self, aux)
99 	struct device *parent;
100 	struct device *self;
101 	void *aux;
102 {
103 	struct ohci_pci_softc *sc = (struct ohci_pci_softc *)self;
104 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
105 	pci_chipset_tag_t pc = pa->pa_pc;
106 	char const *intrstr;
107 	pci_intr_handle_t ih;
108 	pcireg_t csr;
109 	usbd_status r;
110 
111 	/* Map I/O registers */
112 	if (pci_mapreg_map(pa, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
113 	    &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size, 0)) {
114 		printf(": can't map mem space\n");
115 		return;
116 	}
117 
118 	/* Disable interrupts, so we don't get any spurious ones. */
119 	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
120 			  OHCI_ALL_INTRS);
121 
122 	sc->sc_pc = pc;
123 	sc->sc.sc_bus.dmatag = pa->pa_dmat;
124 
125 	/* Enable the device. */
126 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
127 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
128 		       csr | PCI_COMMAND_MASTER_ENABLE);
129 
130 	bus_space_barrier(sc->sc.iot, sc->sc.ioh, 0, sc->sc.sc_size,
131 	    BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
132 	bus_space_write_4(sc->sc.iot, sc->sc.ioh,
133 	    OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
134 
135 	/* Map and establish the interrupt. */
136 	if (pci_intr_map(pa, &ih)) {
137 		printf(": couldn't map interrupt\n");
138 		return;
139 	}
140 
141 #if defined(__OpenBSD__)
142 	timeout_set(&sc->sc.sc_tmo_rhsc, ohci_rhsc_enable, sc);
143 #endif
144 
145 	intrstr = pci_intr_string(pc, ih);
146 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ohci_intr, sc,
147 	    sc->sc.sc_bus.bdev.dv_xname);
148 	if (sc->sc_ih == NULL) {
149 		printf(": couldn't establish interrupt");
150 		if (intrstr != NULL)
151 			printf(" at %s", intrstr);
152 		printf("\n");
153 		return;
154 	}
155 	printf(": %s", intrstr);
156 
157 	r = ohci_init(&sc->sc);
158 	if (r != USBD_NORMAL_COMPLETION) {
159 		printf("%s: init failed, error=%d\n",
160 		    sc->sc.sc_bus.bdev.dv_xname, r);
161 		return;
162 	}
163 
164 	/* Attach usb device. */
165 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
166 	    usbctlprint);
167 }
168 
169 int
170 ohci_pci_detach(self, flags)
171 	device_ptr_t self;
172 	int flags;
173 {
174 	struct ohci_pci_softc *sc = (struct ohci_pci_softc *)self;
175 	int rv;
176 
177 	rv = ohci_detach(&sc->sc, flags);
178 	if (rv)
179 		return (rv);
180 	if (sc->sc_ih != NULL) {
181 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
182 		sc->sc_ih = NULL;
183 	}
184 	if (sc->sc.sc_size) {
185 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
186 		sc->sc.sc_size = 0;
187 	}
188 	return (0);
189 }
190