ehci_pci.c revision 1.3.2.2 1 1.3.2.2 bouyer /* $NetBSD: ehci_pci.c,v 1.3.2.2 2001/01/05 17:36:03 bouyer Exp $ */
2 1.3.2.2 bouyer
3 1.3.2.2 bouyer /*
4 1.3.2.2 bouyer * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.3.2.2 bouyer * All rights reserved.
6 1.3.2.2 bouyer *
7 1.3.2.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 bouyer * by Lennart Augustsson (lennart (at) augustsson.net).
9 1.3.2.2 bouyer *
10 1.3.2.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.3.2.2 bouyer * modification, are permitted provided that the following conditions
12 1.3.2.2 bouyer * are met:
13 1.3.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.3.2.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.3.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.3.2.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.3.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
19 1.3.2.2 bouyer * must display the following acknowledgement:
20 1.3.2.2 bouyer * This product includes software developed by the NetBSD
21 1.3.2.2 bouyer * Foundation, Inc. and its contributors.
22 1.3.2.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3.2.2 bouyer * contributors may be used to endorse or promote products derived
24 1.3.2.2 bouyer * from this software without specific prior written permission.
25 1.3.2.2 bouyer *
26 1.3.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3.2.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3.2.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3.2.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3.2.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3.2.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3.2.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3.2.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3.2.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3.2.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.3.2.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
37 1.3.2.2 bouyer */
38 1.3.2.2 bouyer
39 1.3.2.2 bouyer #include <sys/param.h>
40 1.3.2.2 bouyer #include <sys/systm.h>
41 1.3.2.2 bouyer #include <sys/kernel.h>
42 1.3.2.2 bouyer #include <sys/device.h>
43 1.3.2.2 bouyer #include <sys/proc.h>
44 1.3.2.2 bouyer #include <sys/queue.h>
45 1.3.2.2 bouyer
46 1.3.2.2 bouyer #include <machine/bus.h>
47 1.3.2.2 bouyer
48 1.3.2.2 bouyer #include <dev/pci/pcivar.h>
49 1.3.2.2 bouyer
50 1.3.2.2 bouyer #include <dev/usb/usb.h>
51 1.3.2.2 bouyer #include <dev/usb/usbdi.h>
52 1.3.2.2 bouyer #include <dev/usb/usbdivar.h>
53 1.3.2.2 bouyer #include <dev/usb/usb_mem.h>
54 1.3.2.2 bouyer
55 1.3.2.2 bouyer #include <dev/usb/ehcireg.h>
56 1.3.2.2 bouyer #include <dev/usb/ehcivar.h>
57 1.3.2.2 bouyer
58 1.3.2.2 bouyer int ehci_pci_match(struct device *, struct cfdata *, void *);
59 1.3.2.2 bouyer void ehci_pci_attach(struct device *, struct device *, void *);
60 1.3.2.2 bouyer int ehci_pci_detach(device_ptr_t, int);
61 1.3.2.2 bouyer
62 1.3.2.2 bouyer struct ehci_pci_softc {
63 1.3.2.2 bouyer ehci_softc_t sc;
64 1.3.2.2 bouyer pci_chipset_tag_t sc_pc;
65 1.3.2.2 bouyer pcitag_t sc_tag;
66 1.3.2.2 bouyer void *sc_ih; /* interrupt vectoring */
67 1.3.2.2 bouyer };
68 1.3.2.2 bouyer
69 1.3.2.2 bouyer struct cfattach ehci_pci_ca = {
70 1.3.2.2 bouyer sizeof(struct ehci_pci_softc), ehci_pci_match, ehci_pci_attach,
71 1.3.2.2 bouyer ehci_pci_detach, ehci_activate
72 1.3.2.2 bouyer };
73 1.3.2.2 bouyer
74 1.3.2.2 bouyer int
75 1.3.2.2 bouyer ehci_pci_match(struct device *parent, struct cfdata *match, void *aux)
76 1.3.2.2 bouyer {
77 1.3.2.2 bouyer struct pci_attach_args *pa = (struct pci_attach_args *) aux;
78 1.3.2.2 bouyer
79 1.3.2.2 bouyer if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
80 1.3.2.2 bouyer PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
81 1.3.2.2 bouyer PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_EHCI)
82 1.3.2.2 bouyer return (1);
83 1.3.2.2 bouyer
84 1.3.2.2 bouyer return (0);
85 1.3.2.2 bouyer }
86 1.3.2.2 bouyer
87 1.3.2.2 bouyer void
88 1.3.2.2 bouyer ehci_pci_attach(struct device *parent, struct device *self, void *aux)
89 1.3.2.2 bouyer {
90 1.3.2.2 bouyer struct ehci_pci_softc *sc = (struct ehci_pci_softc *)self;
91 1.3.2.2 bouyer struct pci_attach_args *pa = (struct pci_attach_args *)aux;
92 1.3.2.2 bouyer pci_chipset_tag_t pc = pa->pa_pc;
93 1.3.2.2 bouyer pcitag_t tag = pa->pa_tag;
94 1.3.2.2 bouyer char const *intrstr;
95 1.3.2.2 bouyer pci_intr_handle_t ih;
96 1.3.2.2 bouyer pcireg_t csr;
97 1.3.2.2 bouyer char *vendor;
98 1.3.2.2 bouyer char *devname = sc->sc.sc_bus.bdev.dv_xname;
99 1.3.2.2 bouyer char devinfo[256];
100 1.3.2.2 bouyer usbd_status r;
101 1.3.2.2 bouyer
102 1.3.2.2 bouyer pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
103 1.3.2.2 bouyer printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
104 1.3.2.2 bouyer
105 1.3.2.2 bouyer /* Map I/O registers */
106 1.3.2.2 bouyer if (pci_mapreg_map(pa, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
107 1.3.2.2 bouyer &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
108 1.3.2.2 bouyer printf("%s: can't map i/o space\n", devname);
109 1.3.2.2 bouyer return;
110 1.3.2.2 bouyer }
111 1.3.2.2 bouyer
112 1.3.2.2 bouyer /* Disable interrupts, so we don't get any spurious ones. */
113 1.3.2.2 bouyer /* bus_space_write_2(sc->sc.iot, sc->sc.ioh, EHCI_INTR, 0); */
114 1.3.2.2 bouyer
115 1.3.2.2 bouyer sc->sc_pc = pc;
116 1.3.2.2 bouyer sc->sc_tag = tag;
117 1.3.2.2 bouyer sc->sc.sc_bus.dmatag = pa->pa_dmat;
118 1.3.2.2 bouyer
119 1.3.2.2 bouyer /* Enable the device. */
120 1.3.2.2 bouyer csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
121 1.3.2.2 bouyer pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
122 1.3.2.2 bouyer csr | PCI_COMMAND_MASTER_ENABLE);
123 1.3.2.2 bouyer
124 1.3.2.2 bouyer /* Map and establish the interrupt. */
125 1.3.2.2 bouyer if (pci_intr_map(pa, &ih)) {
126 1.3.2.2 bouyer printf("%s: couldn't map interrupt\n", devname);
127 1.3.2.2 bouyer return;
128 1.3.2.2 bouyer }
129 1.3.2.2 bouyer intrstr = pci_intr_string(pc, ih);
130 1.3.2.2 bouyer sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ehci_intr, sc);
131 1.3.2.2 bouyer if (sc->sc_ih == NULL) {
132 1.3.2.2 bouyer printf("%s: couldn't establish interrupt", devname);
133 1.3.2.2 bouyer if (intrstr != NULL)
134 1.3.2.2 bouyer printf(" at %s", intrstr);
135 1.3.2.2 bouyer printf("\n");
136 1.3.2.2 bouyer return;
137 1.3.2.2 bouyer }
138 1.3.2.2 bouyer printf("%s: interrupting at %s\n", devname, intrstr);
139 1.3.2.2 bouyer
140 1.3.2.2 bouyer switch(pci_conf_read(pc, tag, PCI_USBREV) & PCI_USBREV_MASK) {
141 1.3.2.2 bouyer case PCI_USBREV_PRE_1_0:
142 1.3.2.2 bouyer sc->sc.sc_bus.usbrev = USBREV_PRE_1_0;
143 1.3.2.2 bouyer break;
144 1.3.2.2 bouyer case PCI_USBREV_1_0:
145 1.3.2.2 bouyer sc->sc.sc_bus.usbrev = USBREV_1_0;
146 1.3.2.2 bouyer break;
147 1.3.2.2 bouyer case PCI_USBREV_1_1:
148 1.3.2.2 bouyer sc->sc.sc_bus.usbrev = USBREV_1_1;
149 1.3.2.2 bouyer break;
150 1.3.2.2 bouyer case PCI_USBREV_2_0:
151 1.3.2.2 bouyer sc->sc.sc_bus.usbrev = USBREV_2_0;
152 1.3.2.2 bouyer break;
153 1.3.2.2 bouyer default:
154 1.3.2.2 bouyer sc->sc.sc_bus.usbrev = USBREV_UNKNOWN;
155 1.3.2.2 bouyer break;
156 1.3.2.2 bouyer }
157 1.3.2.2 bouyer
158 1.3.2.2 bouyer /* Figure out vendor for root hub descriptor. */
159 1.3.2.2 bouyer vendor = pci_findvendor(pa->pa_id);
160 1.3.2.2 bouyer sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
161 1.3.2.2 bouyer if (vendor)
162 1.3.2.2 bouyer strncpy(sc->sc.sc_vendor, vendor,
163 1.3.2.2 bouyer sizeof(sc->sc.sc_vendor) - 1);
164 1.3.2.2 bouyer else
165 1.3.2.2 bouyer sprintf(sc->sc.sc_vendor, "vendor 0x%04x",
166 1.3.2.2 bouyer PCI_VENDOR(pa->pa_id));
167 1.3.2.2 bouyer
168 1.3.2.2 bouyer r = ehci_init(&sc->sc);
169 1.3.2.2 bouyer if (r != USBD_NORMAL_COMPLETION) {
170 1.3.2.2 bouyer printf("%s: init failed, error=%d\n", devname, r);
171 1.3.2.2 bouyer return;
172 1.3.2.2 bouyer }
173 1.3.2.2 bouyer
174 1.3.2.2 bouyer /* Attach usb device. */
175 1.3.2.2 bouyer sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
176 1.3.2.2 bouyer usbctlprint);
177 1.3.2.2 bouyer }
178 1.3.2.2 bouyer
179 1.3.2.2 bouyer int
180 1.3.2.2 bouyer ehci_pci_detach(device_ptr_t self, int flags)
181 1.3.2.2 bouyer {
182 1.3.2.2 bouyer struct ehci_pci_softc *sc = (struct ehci_pci_softc *)self;
183 1.3.2.2 bouyer int rv;
184 1.3.2.2 bouyer
185 1.3.2.2 bouyer rv = ehci_detach(&sc->sc, flags);
186 1.3.2.2 bouyer if (rv)
187 1.3.2.2 bouyer return (rv);
188 1.3.2.2 bouyer if (sc->sc_ih != NULL) {
189 1.3.2.2 bouyer pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
190 1.3.2.2 bouyer sc->sc_ih = NULL;
191 1.3.2.2 bouyer }
192 1.3.2.2 bouyer if (sc->sc.sc_size) {
193 1.3.2.2 bouyer bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
194 1.3.2.2 bouyer sc->sc.sc_size = 0;
195 1.3.2.2 bouyer }
196 1.3.2.2 bouyer return (0);
197 1.3.2.2 bouyer }
198