ehci_cardbus.c revision 1.3.2.2 1 1.3.2.2 nathanw /* $NetBSD: ehci_cardbus.c,v 1.3.2.2 2001/11/14 19:14:01 nathanw Exp $ */
2 1.3.2.2 nathanw
3 1.3.2.2 nathanw /*
4 1.3.2.2 nathanw * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.3.2.2 nathanw * All rights reserved.
6 1.3.2.2 nathanw *
7 1.3.2.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 nathanw * by Lennart Augustsson (lennart (at) augustsson.net) at
9 1.3.2.2 nathanw * Carlstedt Research & Technology.
10 1.3.2.2 nathanw *
11 1.3.2.2 nathanw * Redistribution and use in source and binary forms, with or without
12 1.3.2.2 nathanw * modification, are permitted provided that the following conditions
13 1.3.2.2 nathanw * are met:
14 1.3.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
15 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer.
16 1.3.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
17 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
18 1.3.2.2 nathanw * documentation and/or other materials provided with the distribution.
19 1.3.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
20 1.3.2.2 nathanw * must display the following acknowledgement:
21 1.3.2.2 nathanw * This product includes software developed by the NetBSD
22 1.3.2.2 nathanw * Foundation, Inc. and its contributors.
23 1.3.2.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.3.2.2 nathanw * contributors may be used to endorse or promote products derived
25 1.3.2.2 nathanw * from this software without specific prior written permission.
26 1.3.2.2 nathanw *
27 1.3.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.3.2.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.3.2.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.3.2.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.3.2.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.3.2.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.3.2.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.3.2.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.3.2.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.3.2.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.3.2.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
38 1.3.2.2 nathanw */
39 1.3.2.2 nathanw
40 1.3.2.2 nathanw #include <sys/cdefs.h>
41 1.3.2.2 nathanw __KERNEL_RCSID(0, "$NetBSD: ehci_cardbus.c,v 1.3.2.2 2001/11/14 19:14:01 nathanw Exp $");
42 1.3.2.2 nathanw
43 1.3.2.2 nathanw #include <sys/param.h>
44 1.3.2.2 nathanw #include <sys/systm.h>
45 1.3.2.2 nathanw #include <sys/kernel.h>
46 1.3.2.2 nathanw #include <sys/device.h>
47 1.3.2.2 nathanw #include <sys/proc.h>
48 1.3.2.2 nathanw
49 1.3.2.2 nathanw #include <machine/bus.h>
50 1.3.2.2 nathanw
51 1.3.2.2 nathanw #if defined pciinc
52 1.3.2.2 nathanw #include <dev/pci/pcidevs.h>
53 1.3.2.2 nathanw #endif
54 1.3.2.2 nathanw
55 1.3.2.2 nathanw #include <dev/cardbus/cardbusvar.h>
56 1.3.2.2 nathanw #include <dev/cardbus/cardbusdevs.h>
57 1.3.2.2 nathanw
58 1.3.2.2 nathanw #include <dev/cardbus/usb_cardbus.h>
59 1.3.2.2 nathanw
60 1.3.2.2 nathanw #include <dev/usb/usb.h>
61 1.3.2.2 nathanw #include <dev/usb/usbdi.h>
62 1.3.2.2 nathanw #include <dev/usb/usbdivar.h>
63 1.3.2.2 nathanw #include <dev/usb/usb_mem.h>
64 1.3.2.2 nathanw
65 1.3.2.2 nathanw #include <dev/usb/ehcireg.h>
66 1.3.2.2 nathanw #include <dev/usb/ehcivar.h>
67 1.3.2.2 nathanw
68 1.3.2.2 nathanw #ifdef EHCI_DEBUG
69 1.3.2.2 nathanw #define DPRINTF(x) if (ehcidebug) printf x
70 1.3.2.2 nathanw extern int ehcidebug;
71 1.3.2.2 nathanw #else
72 1.3.2.2 nathanw #define DPRINTF(x)
73 1.3.2.2 nathanw #endif
74 1.3.2.2 nathanw
75 1.3.2.2 nathanw int ehci_cardbus_match(struct device *, struct cfdata *, void *);
76 1.3.2.2 nathanw void ehci_cardbus_attach(struct device *, struct device *, void *);
77 1.3.2.2 nathanw int ehci_cardbus_detach(device_ptr_t, int);
78 1.3.2.2 nathanw
79 1.3.2.2 nathanw struct ehci_cardbus_softc {
80 1.3.2.2 nathanw ehci_softc_t sc;
81 1.3.2.2 nathanw cardbus_chipset_tag_t sc_cc;
82 1.3.2.2 nathanw cardbus_function_tag_t sc_cf;
83 1.3.2.2 nathanw cardbus_devfunc_t sc_ct;
84 1.3.2.2 nathanw void *sc_ih; /* interrupt vectoring */
85 1.3.2.2 nathanw };
86 1.3.2.2 nathanw
87 1.3.2.2 nathanw struct cfattach ehci_cardbus_ca = {
88 1.3.2.2 nathanw sizeof(struct ehci_cardbus_softc), ehci_cardbus_match,
89 1.3.2.2 nathanw ehci_cardbus_attach, ehci_cardbus_detach, ehci_activate
90 1.3.2.2 nathanw };
91 1.3.2.2 nathanw
92 1.3.2.2 nathanw #define CARDBUS_INTERFACE_EHCI PCI_INTERFACE_EHCI
93 1.3.2.2 nathanw #define CARDBUS_CBMEM PCI_CBMEM
94 1.3.2.2 nathanw #define cardbus_findvendor pci_findvendor
95 1.3.2.2 nathanw #define cardbus_devinfo pci_devinfo
96 1.3.2.2 nathanw
97 1.3.2.2 nathanw static TAILQ_HEAD(, usb_cardbus) ehci_cardbus_alldevs =
98 1.3.2.2 nathanw TAILQ_HEAD_INITIALIZER(ehci_cardbus_alldevs);
99 1.3.2.2 nathanw
100 1.3.2.2 nathanw int
101 1.3.2.2 nathanw ehci_cardbus_match(struct device *parent, struct cfdata *match, void *aux)
102 1.3.2.2 nathanw {
103 1.3.2.2 nathanw struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
104 1.3.2.2 nathanw
105 1.3.2.2 nathanw if (CARDBUS_CLASS(ca->ca_class) == CARDBUS_CLASS_SERIALBUS &&
106 1.3.2.2 nathanw CARDBUS_SUBCLASS(ca->ca_class) == CARDBUS_SUBCLASS_SERIALBUS_USB &&
107 1.3.2.2 nathanw CARDBUS_INTERFACE(ca->ca_class) == CARDBUS_INTERFACE_EHCI)
108 1.3.2.2 nathanw return (1);
109 1.3.2.2 nathanw
110 1.3.2.2 nathanw return (0);
111 1.3.2.2 nathanw }
112 1.3.2.2 nathanw
113 1.3.2.2 nathanw void
114 1.3.2.2 nathanw ehci_cardbus_attach(struct device *parent, struct device *self, void *aux)
115 1.3.2.2 nathanw {
116 1.3.2.2 nathanw struct ehci_cardbus_softc *sc = (struct ehci_cardbus_softc *)self;
117 1.3.2.2 nathanw struct cardbus_attach_args *ca = aux;
118 1.3.2.2 nathanw cardbus_devfunc_t ct = ca->ca_ct;
119 1.3.2.2 nathanw cardbus_chipset_tag_t cc = ct->ct_cc;
120 1.3.2.2 nathanw cardbus_function_tag_t cf = ct->ct_cf;
121 1.3.2.2 nathanw cardbusreg_t csr;
122 1.3.2.2 nathanw char devinfo[256];
123 1.3.2.2 nathanw usbd_status r;
124 1.3.2.2 nathanw char *vendor;
125 1.3.2.2 nathanw u_int ncomp;
126 1.3.2.2 nathanw const char *devname = sc->sc.sc_bus.bdev.dv_xname;
127 1.3.2.2 nathanw struct usb_cardbus *up;
128 1.3.2.2 nathanw
129 1.3.2.2 nathanw cardbus_devinfo(ca->ca_id, ca->ca_class, 0, devinfo);
130 1.3.2.2 nathanw printf(": %s (rev. 0x%02x)\n", devinfo,
131 1.3.2.2 nathanw CARDBUS_REVISION(ca->ca_class));
132 1.3.2.2 nathanw
133 1.3.2.2 nathanw /* Map I/O registers */
134 1.3.2.2 nathanw if (Cardbus_mapreg_map(ct, CARDBUS_CBMEM, CARDBUS_MAPREG_TYPE_MEM, 0,
135 1.3.2.2 nathanw &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
136 1.3.2.2 nathanw printf("%s: can't map mem space\n", devname);
137 1.3.2.2 nathanw return;
138 1.3.2.2 nathanw }
139 1.3.2.2 nathanw
140 1.3.2.2 nathanw sc->sc_cc = cc;
141 1.3.2.2 nathanw sc->sc_cf = cf;
142 1.3.2.2 nathanw sc->sc_ct = ct;
143 1.3.2.2 nathanw sc->sc.sc_bus.dmatag = ca->ca_dmat;
144 1.3.2.2 nathanw
145 1.3.2.2 nathanw #if rbus
146 1.3.2.2 nathanw #else
147 1.3.2.2 nathanw XXX (ct->ct_cf->cardbus_mem_open)(cc, 0, iob, iob + 0x40);
148 1.3.2.2 nathanw #endif
149 1.3.2.2 nathanw (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
150 1.3.2.2 nathanw (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
151 1.3.2.2 nathanw
152 1.3.2.2 nathanw /* Enable the device. */
153 1.3.2.2 nathanw csr = cardbus_conf_read(cc, cf, ca->ca_tag,
154 1.3.2.2 nathanw CARDBUS_COMMAND_STATUS_REG);
155 1.3.2.2 nathanw cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG,
156 1.3.2.2 nathanw csr | CARDBUS_COMMAND_MASTER_ENABLE
157 1.3.2.2 nathanw | CARDBUS_COMMAND_MEM_ENABLE);
158 1.3.2.2 nathanw
159 1.3.2.2 nathanw /* Disable interrupts, so we don't get any spurious ones. */
160 1.3.2.2 nathanw sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
161 1.3.2.2 nathanw DPRINTF(("%s: offs=%d\n", devname, sc->sc.sc_offs));
162 1.3.2.2 nathanw EOWRITE2(&sc->sc, EHCI_USBINTR, 0);
163 1.3.2.2 nathanw
164 1.3.2.2 nathanw sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline,
165 1.3.2.2 nathanw IPL_USB, ehci_intr, sc);
166 1.3.2.2 nathanw if (sc->sc_ih == NULL) {
167 1.3.2.2 nathanw printf("%s: couldn't establish interrupt\n", devname);
168 1.3.2.2 nathanw return;
169 1.3.2.2 nathanw }
170 1.3.2.2 nathanw printf("%s: interrupting at %d\n", devname, ca->ca_intrline);
171 1.3.2.2 nathanw
172 1.3.2.2 nathanw /* Figure out vendor for root hub descriptor. */
173 1.3.2.2 nathanw vendor = cardbus_findvendor(ca->ca_id);
174 1.3.2.2 nathanw sc->sc.sc_id_vendor = CARDBUS_VENDOR(ca->ca_id);
175 1.3.2.2 nathanw if (vendor)
176 1.3.2.2 nathanw strncpy(sc->sc.sc_vendor, vendor,
177 1.3.2.2 nathanw sizeof(sc->sc.sc_vendor) - 1);
178 1.3.2.2 nathanw else
179 1.3.2.2 nathanw sprintf(sc->sc.sc_vendor, "vendor 0x%04x",
180 1.3.2.2 nathanw CARDBUS_VENDOR(ca->ca_id));
181 1.3.2.2 nathanw
182 1.3.2.2 nathanw /*
183 1.3.2.2 nathanw * Find companion controllers. According to the spec they always
184 1.3.2.2 nathanw * have lower function numbers so they should be enumerated already.
185 1.3.2.2 nathanw */
186 1.3.2.2 nathanw ncomp = 0;
187 1.3.2.2 nathanw TAILQ_FOREACH(up, &ehci_cardbus_alldevs, next) {
188 1.3.2.2 nathanw if (up->bus == ca->ca_bus && up->device == ca->ca_device) {
189 1.3.2.2 nathanw DPRINTF(("ehci_cardbus_attach: companion %s\n",
190 1.3.2.2 nathanw USBDEVNAME(up->usb->bdev)));
191 1.3.2.2 nathanw sc->sc.sc_comps[ncomp++] = up->usb;
192 1.3.2.2 nathanw if (ncomp >= EHCI_COMPANION_MAX)
193 1.3.2.2 nathanw break;
194 1.3.2.2 nathanw }
195 1.3.2.2 nathanw }
196 1.3.2.2 nathanw sc->sc.sc_ncomp = ncomp;
197 1.3.2.2 nathanw
198 1.3.2.2 nathanw r = ehci_init(&sc->sc);
199 1.3.2.2 nathanw if (r != USBD_NORMAL_COMPLETION) {
200 1.3.2.2 nathanw printf("%s: init failed, error=%d\n", devname, r);
201 1.3.2.2 nathanw
202 1.3.2.2 nathanw /* Avoid spurious interrupts. */
203 1.3.2.2 nathanw cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
204 1.3.2.2 nathanw sc->sc_ih = NULL;
205 1.3.2.2 nathanw
206 1.3.2.2 nathanw return;
207 1.3.2.2 nathanw }
208 1.3.2.2 nathanw
209 1.3.2.2 nathanw /* Attach usb device. */
210 1.3.2.2 nathanw sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
211 1.3.2.2 nathanw usbctlprint);
212 1.3.2.2 nathanw }
213 1.3.2.2 nathanw
214 1.3.2.2 nathanw int
215 1.3.2.2 nathanw ehci_cardbus_detach(device_ptr_t self, int flags)
216 1.3.2.2 nathanw {
217 1.3.2.2 nathanw struct ehci_cardbus_softc *sc = (struct ehci_cardbus_softc *)self;
218 1.3.2.2 nathanw struct cardbus_devfunc *ct = sc->sc_ct;
219 1.3.2.2 nathanw int rv;
220 1.3.2.2 nathanw
221 1.3.2.2 nathanw rv = ehci_detach(&sc->sc, flags);
222 1.3.2.2 nathanw if (rv)
223 1.3.2.2 nathanw return (rv);
224 1.3.2.2 nathanw if (sc->sc_ih != NULL) {
225 1.3.2.2 nathanw cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
226 1.3.2.2 nathanw sc->sc_ih = NULL;
227 1.3.2.2 nathanw }
228 1.3.2.2 nathanw if (sc->sc.sc_size) {
229 1.3.2.2 nathanw Cardbus_mapreg_unmap(ct, CARDBUS_CBMEM, sc->sc.iot,
230 1.3.2.2 nathanw sc->sc.ioh, sc->sc.sc_size);
231 1.3.2.2 nathanw sc->sc.sc_size = 0;
232 1.3.2.2 nathanw }
233 1.3.2.2 nathanw return (0);
234 1.3.2.2 nathanw }
235 1.3.2.2 nathanw
236 1.3.2.2 nathanw void
237 1.3.2.2 nathanw usb_cardbus_add(struct usb_cardbus *up, struct cardbus_attach_args *ca, struct usbd_bus *bu)
238 1.3.2.2 nathanw {
239 1.3.2.2 nathanw TAILQ_INSERT_TAIL(&ehci_cardbus_alldevs, up, next);
240 1.3.2.2 nathanw up->bus = ca->ca_bus;
241 1.3.2.2 nathanw up->device = ca->ca_device;
242 1.3.2.2 nathanw up->function = ca->ca_function;
243 1.3.2.2 nathanw up->usb = bu;
244 1.3.2.2 nathanw }
245 1.3.2.2 nathanw
246 1.3.2.2 nathanw void
247 1.3.2.2 nathanw usb_cardbus_rem(struct usb_cardbus *up)
248 1.3.2.2 nathanw {
249 1.3.2.2 nathanw TAILQ_REMOVE(&ehci_cardbus_alldevs, up, next);
250 1.3.2.2 nathanw }
251