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