xref: /openbsd/sys/dev/pci/ohci_pci.c (revision d415bd75)
1 /*	$OpenBSD: ohci_pci.c,v 1.42 2022/03/11 18:00:51 mpi 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/timeout.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 	struct ohci_softc	sc;
67 	pci_chipset_tag_t	sc_pc;
68 	void 			*sc_ih;		/* interrupt vectoring */
69 };
70 
71 const 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 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
160 		splx(s);
161 		return;
162 	}
163 
164 	/* Ignore interrupts for now */
165 	sc->sc.sc_bus.dying = 1;
166 
167 	config_defer(self, ohci_pci_attach_deferred);
168 
169 	splx(s);
170 
171 	return;
172 }
173 
174 void
175 ohci_pci_attach_deferred(struct device *self)
176 {
177 	struct ohci_pci_softc *sc = (struct ohci_pci_softc *)self;
178 	usbd_status r;
179 	int s;
180 
181 	s = splusb();
182 
183 	sc->sc.sc_bus.dying = 0;
184 
185 	r = ohci_init(&sc->sc);
186 	if (r != USBD_NORMAL_COMPLETION) {
187 		printf("%s: init failed, error=%d\n",
188 		    sc->sc.sc_bus.bdev.dv_xname, r);
189 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
190 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
191 		splx(s);
192 		return;
193 	}
194 	splx(s);
195 
196 	/* Attach usb device. */
197 	config_found(self, &sc->sc.sc_bus, usbctlprint);
198 }
199 
200 int
201 ohci_pci_detach(struct device *self, int flags)
202 {
203 	struct ohci_pci_softc *sc = (struct ohci_pci_softc *)self;
204 	int rv;
205 
206 	rv = ohci_detach(self, flags);
207 	if (rv)
208 		return (rv);
209 
210 	if (sc->sc_ih != NULL) {
211 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
212 		sc->sc_ih = NULL;
213 	}
214 	if (sc->sc.sc_size) {
215 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
216 		sc->sc.sc_size = 0;
217 	}
218 	return (0);
219 }
220