xref: /openbsd-src/sys/dev/pci/uhci_pci.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: uhci_pci.c,v 1.11 2001/06/12 19:11:59 mickey Exp $	*/
2 /*	$NetBSD: uhci_pci.c,v 1.14 2000/01/25 11:26:06 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 #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/uhcireg.h>
58 #include <dev/usb/uhcivar.h>
59 
60 int	uhci_pci_match __P((struct device *, void *, void *));
61 void	uhci_pci_attach __P((struct device *, struct device *, void *));
62 int	uhci_pci_detach __P((device_ptr_t, int));
63 
64 struct uhci_pci_softc {
65 	uhci_softc_t		sc;
66 	pci_chipset_tag_t	sc_pc;
67 	void 			*sc_ih;		/* interrupt vectoring */
68 };
69 
70 struct cfattach uhci_pci_ca = {
71 	sizeof(struct uhci_pci_softc), uhci_pci_match, uhci_pci_attach,
72 	uhci_pci_detach, uhci_activate
73 };
74 
75 int
76 uhci_pci_match(parent, match, aux)
77 	struct device *parent;
78 	void *match, *aux;
79 {
80 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
81 
82 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
83 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
84 	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_UHCI)
85 		return (1);
86 
87 	return (0);
88 }
89 
90 void
91 uhci_pci_attach(parent, self, aux)
92 	struct device *parent;
93 	struct device *self;
94 	void *aux;
95 {
96 	struct uhci_pci_softc *sc = (struct uhci_pci_softc *)self;
97 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
98 	pci_chipset_tag_t pc = pa->pa_pc;
99 	char const *intrstr;
100 	pci_intr_handle_t ih;
101 	pcireg_t csr, legsup;
102 	usbd_status r;
103 
104 
105 	/* Map I/O registers */
106 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
107 	    &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size, 0)) {
108 		printf(": can't map i/o space\n");
109 		return;
110 	}
111 
112 	/* Disable interrupts, so we don't get any spurious ones. */
113 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
114 
115 	sc->sc_pc = pc;
116 	sc->sc.sc_bus.dmatag = pa->pa_dmat;
117 
118 	/* Enable the device. */
119 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
120 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
121 		       csr | PCI_COMMAND_MASTER_ENABLE);
122 
123 	/* Verify that the PIRQD enable bit is set, some BIOS's don't do that*/
124 	legsup = pci_conf_read(pc, pa->pa_tag, PCI_LEGSUP);
125 	if (!(legsup & PCI_LEGSUP_USBPIRQDEN)) {
126 		legsup = PCI_LEGSUP_USBPIRQDEN;
127 		pci_conf_write(pc, pa->pa_tag, PCI_LEGSUP, legsup);
128 	}
129 
130 	switch(pci_conf_read(pc, pa->pa_tag, PCI_USBREV) & PCI_USBREV_MASK) {
131 	case PCI_USBREV_PRE_1_0:
132 		sc->sc.sc_bus.usbrev = USBREV_PRE_1_0;
133 		break;
134 	case PCI_USBREV_1_0:
135 		sc->sc.sc_bus.usbrev = USBREV_1_0;
136 		break;
137 	case PCI_USBREV_1_1:
138 		sc->sc.sc_bus.usbrev = USBREV_1_1;
139 		break;
140 	default:
141 		sc->sc.sc_bus.usbrev = USBREV_UNKNOWN;
142 		break;
143 	}
144 
145 	uhci_run(&sc->sc, 0);			/* stop the controller */
146 						/* disable interrupts */
147 	bus_space_barrier(sc->sc.iot, sc->sc.ioh, 0, sc->sc.sc_size,
148 	    BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
149 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
150 
151 	/* Map and establish the interrupt. */
152 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
153 	    pa->pa_intrline, &ih)) {
154 		printf(": couldn't map interrupt\n");
155 		return;
156 	}
157 	intrstr = pci_intr_string(pc, ih);
158 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, uhci_intr, sc,
159 	    sc->sc.sc_bus.bdev.dv_xname);
160 	if (sc->sc_ih == NULL) {
161 		printf(": couldn't establish interrupt");
162 		if (intrstr != NULL)
163 			printf(" at %s", intrstr);
164 		printf("\n");
165 		return;
166 	}
167 	printf(": %s\n", intrstr);
168 
169 	r = uhci_init(&sc->sc);
170 	if (r != USBD_NORMAL_COMPLETION) {
171 		printf(": init failed, error=%d\n", r);
172 		return;
173 	}
174 
175 	/* Attach usb device. */
176 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
177 	    usbctlprint);
178 }
179 
180 int
181 uhci_pci_detach(self, flags)
182 	device_ptr_t self;
183 	int flags;
184 {
185 	struct uhci_pci_softc *sc = (struct uhci_pci_softc *)self;
186 	int rv;
187 
188 	rv = uhci_detach(&sc->sc, flags);
189 	if (rv)
190 		return (rv);
191 	if (sc->sc_ih != NULL) {
192 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
193 		sc->sc_ih = NULL;
194 	}
195 	if (sc->sc.sc_size) {
196 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
197 		sc->sc.sc_size = 0;
198 	}
199 	return (0);
200 }
201