xref: /openbsd-src/sys/dev/pci/ohci_pci.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: ohci_pci.c,v 1.12 2001/06/13 07:43:35 deraadt 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(pc, pa->pa_intrtag, pa->pa_intrpin,
137 	    pa->pa_intrline, &ih)) {
138 		printf(": couldn't map interrupt\n");
139 		return;
140 	}
141 
142 	intrstr = pci_intr_string(pc, ih);
143 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ohci_intr, sc,
144 	    sc->sc.sc_bus.bdev.dv_xname);
145 	if (sc->sc_ih == NULL) {
146 		printf(": couldn't establish interrupt");
147 		if (intrstr != NULL)
148 			printf(" at %s", intrstr);
149 		printf("\n");
150 		return;
151 	}
152 	printf(": %s", intrstr);
153 
154 	r = ohci_init(&sc->sc);
155 	if (r != USBD_NORMAL_COMPLETION) {
156 		printf("%s: init failed, error=%d\n",
157 		    sc->sc.sc_bus.bdev.dv_xname, r);
158 		return;
159 	}
160 
161 	/* Attach usb device. */
162 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
163 	    usbctlprint);
164 }
165 
166 int
167 ohci_pci_detach(self, flags)
168 	device_ptr_t self;
169 	int flags;
170 {
171 	struct ohci_pci_softc *sc = (struct ohci_pci_softc *)self;
172 	int rv;
173 
174 	rv = ohci_detach(&sc->sc, flags);
175 	if (rv)
176 		return (rv);
177 	if (sc->sc_ih != NULL) {
178 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
179 		sc->sc_ih = NULL;
180 	}
181 	if (sc->sc.sc_size) {
182 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
183 		sc->sc.sc_size = 0;
184 	}
185 	return (0);
186 }
187