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