ehci_cardbus.c revision 1.3.2.3 1 1.3.2.3 nathanw /* $NetBSD: ehci_cardbus.c,v 1.3.2.3 2002/10/18 02:41:34 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.3 nathanw __KERNEL_RCSID(0, "$NetBSD: ehci_cardbus.c,v 1.3.2.3 2002/10/18 02:41:34 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.3 nathanw CFATTACH_DECL(ehci_cardbus, sizeof(struct ehci_cardbus_softc),
88 1.3.2.3 nathanw ehci_cardbus_match, ehci_cardbus_attach, ehci_cardbus_detach, ehci_activate);
89 1.3.2.2 nathanw
90 1.3.2.2 nathanw #define CARDBUS_INTERFACE_EHCI PCI_INTERFACE_EHCI
91 1.3.2.2 nathanw #define CARDBUS_CBMEM PCI_CBMEM
92 1.3.2.2 nathanw #define cardbus_findvendor pci_findvendor
93 1.3.2.2 nathanw #define cardbus_devinfo pci_devinfo
94 1.3.2.2 nathanw
95 1.3.2.2 nathanw static TAILQ_HEAD(, usb_cardbus) ehci_cardbus_alldevs =
96 1.3.2.2 nathanw TAILQ_HEAD_INITIALIZER(ehci_cardbus_alldevs);
97 1.3.2.2 nathanw
98 1.3.2.2 nathanw int
99 1.3.2.2 nathanw ehci_cardbus_match(struct device *parent, struct cfdata *match, void *aux)
100 1.3.2.2 nathanw {
101 1.3.2.2 nathanw struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
102 1.3.2.2 nathanw
103 1.3.2.2 nathanw if (CARDBUS_CLASS(ca->ca_class) == CARDBUS_CLASS_SERIALBUS &&
104 1.3.2.2 nathanw CARDBUS_SUBCLASS(ca->ca_class) == CARDBUS_SUBCLASS_SERIALBUS_USB &&
105 1.3.2.2 nathanw CARDBUS_INTERFACE(ca->ca_class) == CARDBUS_INTERFACE_EHCI)
106 1.3.2.2 nathanw return (1);
107 1.3.2.2 nathanw
108 1.3.2.2 nathanw return (0);
109 1.3.2.2 nathanw }
110 1.3.2.2 nathanw
111 1.3.2.2 nathanw void
112 1.3.2.2 nathanw ehci_cardbus_attach(struct device *parent, struct device *self, void *aux)
113 1.3.2.2 nathanw {
114 1.3.2.2 nathanw struct ehci_cardbus_softc *sc = (struct ehci_cardbus_softc *)self;
115 1.3.2.2 nathanw struct cardbus_attach_args *ca = aux;
116 1.3.2.2 nathanw cardbus_devfunc_t ct = ca->ca_ct;
117 1.3.2.2 nathanw cardbus_chipset_tag_t cc = ct->ct_cc;
118 1.3.2.2 nathanw cardbus_function_tag_t cf = ct->ct_cf;
119 1.3.2.2 nathanw cardbusreg_t csr;
120 1.3.2.2 nathanw char devinfo[256];
121 1.3.2.2 nathanw usbd_status r;
122 1.3.2.2 nathanw char *vendor;
123 1.3.2.2 nathanw u_int ncomp;
124 1.3.2.2 nathanw const char *devname = sc->sc.sc_bus.bdev.dv_xname;
125 1.3.2.2 nathanw struct usb_cardbus *up;
126 1.3.2.2 nathanw
127 1.3.2.2 nathanw cardbus_devinfo(ca->ca_id, ca->ca_class, 0, devinfo);
128 1.3.2.2 nathanw printf(": %s (rev. 0x%02x)\n", devinfo,
129 1.3.2.2 nathanw CARDBUS_REVISION(ca->ca_class));
130 1.3.2.2 nathanw
131 1.3.2.2 nathanw /* Map I/O registers */
132 1.3.2.2 nathanw if (Cardbus_mapreg_map(ct, CARDBUS_CBMEM, CARDBUS_MAPREG_TYPE_MEM, 0,
133 1.3.2.2 nathanw &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
134 1.3.2.2 nathanw printf("%s: can't map mem space\n", devname);
135 1.3.2.2 nathanw return;
136 1.3.2.2 nathanw }
137 1.3.2.2 nathanw
138 1.3.2.2 nathanw sc->sc_cc = cc;
139 1.3.2.2 nathanw sc->sc_cf = cf;
140 1.3.2.2 nathanw sc->sc_ct = ct;
141 1.3.2.2 nathanw sc->sc.sc_bus.dmatag = ca->ca_dmat;
142 1.3.2.2 nathanw
143 1.3.2.2 nathanw #if rbus
144 1.3.2.2 nathanw #else
145 1.3.2.2 nathanw XXX (ct->ct_cf->cardbus_mem_open)(cc, 0, iob, iob + 0x40);
146 1.3.2.2 nathanw #endif
147 1.3.2.2 nathanw (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
148 1.3.2.2 nathanw (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
149 1.3.2.2 nathanw
150 1.3.2.2 nathanw /* Enable the device. */
151 1.3.2.2 nathanw csr = cardbus_conf_read(cc, cf, ca->ca_tag,
152 1.3.2.2 nathanw CARDBUS_COMMAND_STATUS_REG);
153 1.3.2.2 nathanw cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG,
154 1.3.2.2 nathanw csr | CARDBUS_COMMAND_MASTER_ENABLE
155 1.3.2.2 nathanw | CARDBUS_COMMAND_MEM_ENABLE);
156 1.3.2.2 nathanw
157 1.3.2.2 nathanw /* Disable interrupts, so we don't get any spurious ones. */
158 1.3.2.2 nathanw sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
159 1.3.2.2 nathanw DPRINTF(("%s: offs=%d\n", devname, sc->sc.sc_offs));
160 1.3.2.2 nathanw EOWRITE2(&sc->sc, EHCI_USBINTR, 0);
161 1.3.2.2 nathanw
162 1.3.2.2 nathanw sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline,
163 1.3.2.2 nathanw IPL_USB, ehci_intr, sc);
164 1.3.2.2 nathanw if (sc->sc_ih == NULL) {
165 1.3.2.2 nathanw printf("%s: couldn't establish interrupt\n", devname);
166 1.3.2.2 nathanw return;
167 1.3.2.2 nathanw }
168 1.3.2.2 nathanw printf("%s: interrupting at %d\n", devname, ca->ca_intrline);
169 1.3.2.2 nathanw
170 1.3.2.2 nathanw /* Figure out vendor for root hub descriptor. */
171 1.3.2.2 nathanw vendor = cardbus_findvendor(ca->ca_id);
172 1.3.2.2 nathanw sc->sc.sc_id_vendor = CARDBUS_VENDOR(ca->ca_id);
173 1.3.2.2 nathanw if (vendor)
174 1.3.2.2 nathanw strncpy(sc->sc.sc_vendor, vendor,
175 1.3.2.2 nathanw sizeof(sc->sc.sc_vendor) - 1);
176 1.3.2.2 nathanw else
177 1.3.2.2 nathanw sprintf(sc->sc.sc_vendor, "vendor 0x%04x",
178 1.3.2.2 nathanw CARDBUS_VENDOR(ca->ca_id));
179 1.3.2.2 nathanw
180 1.3.2.2 nathanw /*
181 1.3.2.2 nathanw * Find companion controllers. According to the spec they always
182 1.3.2.2 nathanw * have lower function numbers so they should be enumerated already.
183 1.3.2.2 nathanw */
184 1.3.2.2 nathanw ncomp = 0;
185 1.3.2.2 nathanw TAILQ_FOREACH(up, &ehci_cardbus_alldevs, next) {
186 1.3.2.2 nathanw if (up->bus == ca->ca_bus && up->device == ca->ca_device) {
187 1.3.2.2 nathanw DPRINTF(("ehci_cardbus_attach: companion %s\n",
188 1.3.2.2 nathanw USBDEVNAME(up->usb->bdev)));
189 1.3.2.2 nathanw sc->sc.sc_comps[ncomp++] = up->usb;
190 1.3.2.2 nathanw if (ncomp >= EHCI_COMPANION_MAX)
191 1.3.2.2 nathanw break;
192 1.3.2.2 nathanw }
193 1.3.2.2 nathanw }
194 1.3.2.2 nathanw sc->sc.sc_ncomp = ncomp;
195 1.3.2.2 nathanw
196 1.3.2.2 nathanw r = ehci_init(&sc->sc);
197 1.3.2.2 nathanw if (r != USBD_NORMAL_COMPLETION) {
198 1.3.2.2 nathanw printf("%s: init failed, error=%d\n", devname, r);
199 1.3.2.2 nathanw
200 1.3.2.2 nathanw /* Avoid spurious interrupts. */
201 1.3.2.2 nathanw cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
202 1.3.2.2 nathanw sc->sc_ih = NULL;
203 1.3.2.2 nathanw
204 1.3.2.2 nathanw return;
205 1.3.2.2 nathanw }
206 1.3.2.2 nathanw
207 1.3.2.2 nathanw /* Attach usb device. */
208 1.3.2.2 nathanw sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
209 1.3.2.2 nathanw usbctlprint);
210 1.3.2.2 nathanw }
211 1.3.2.2 nathanw
212 1.3.2.2 nathanw int
213 1.3.2.2 nathanw ehci_cardbus_detach(device_ptr_t self, int flags)
214 1.3.2.2 nathanw {
215 1.3.2.2 nathanw struct ehci_cardbus_softc *sc = (struct ehci_cardbus_softc *)self;
216 1.3.2.2 nathanw struct cardbus_devfunc *ct = sc->sc_ct;
217 1.3.2.2 nathanw int rv;
218 1.3.2.2 nathanw
219 1.3.2.2 nathanw rv = ehci_detach(&sc->sc, flags);
220 1.3.2.2 nathanw if (rv)
221 1.3.2.2 nathanw return (rv);
222 1.3.2.2 nathanw if (sc->sc_ih != NULL) {
223 1.3.2.2 nathanw cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
224 1.3.2.2 nathanw sc->sc_ih = NULL;
225 1.3.2.2 nathanw }
226 1.3.2.2 nathanw if (sc->sc.sc_size) {
227 1.3.2.2 nathanw Cardbus_mapreg_unmap(ct, CARDBUS_CBMEM, sc->sc.iot,
228 1.3.2.2 nathanw sc->sc.ioh, sc->sc.sc_size);
229 1.3.2.2 nathanw sc->sc.sc_size = 0;
230 1.3.2.2 nathanw }
231 1.3.2.2 nathanw return (0);
232 1.3.2.2 nathanw }
233 1.3.2.2 nathanw
234 1.3.2.2 nathanw void
235 1.3.2.2 nathanw usb_cardbus_add(struct usb_cardbus *up, struct cardbus_attach_args *ca, struct usbd_bus *bu)
236 1.3.2.2 nathanw {
237 1.3.2.2 nathanw TAILQ_INSERT_TAIL(&ehci_cardbus_alldevs, up, next);
238 1.3.2.2 nathanw up->bus = ca->ca_bus;
239 1.3.2.2 nathanw up->device = ca->ca_device;
240 1.3.2.2 nathanw up->function = ca->ca_function;
241 1.3.2.2 nathanw up->usb = bu;
242 1.3.2.2 nathanw }
243 1.3.2.2 nathanw
244 1.3.2.2 nathanw void
245 1.3.2.2 nathanw usb_cardbus_rem(struct usb_cardbus *up)
246 1.3.2.2 nathanw {
247 1.3.2.2 nathanw TAILQ_REMOVE(&ehci_cardbus_alldevs, up, next);
248 1.3.2.2 nathanw }
249