uhci_cardbus.c revision 1.3.6.4 1 1.3.6.4 yamt /* $NetBSD: uhci_cardbus.c,v 1.3.6.4 2007/10/27 11:30:12 yamt Exp $ */
2 1.3.6.2 yamt
3 1.3.6.2 yamt /*
4 1.3.6.2 yamt * Copyright (c) 1998-2005 The NetBSD Foundation, Inc.
5 1.3.6.2 yamt * All rights reserved.
6 1.3.6.2 yamt *
7 1.3.6.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.3.6.2 yamt * by Lennart Augustsson (lennart (at) augustsson.net) at
9 1.3.6.2 yamt * Carlstedt Research & Technology.
10 1.3.6.2 yamt *
11 1.3.6.2 yamt * Redistribution and use in source and binary forms, with or without
12 1.3.6.2 yamt * modification, are permitted provided that the following conditions
13 1.3.6.2 yamt * are met:
14 1.3.6.2 yamt * 1. Redistributions of source code must retain the above copyright
15 1.3.6.2 yamt * notice, this list of conditions and the following disclaimer.
16 1.3.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
17 1.3.6.2 yamt * notice, this list of conditions and the following disclaimer in the
18 1.3.6.2 yamt * documentation and/or other materials provided with the distribution.
19 1.3.6.2 yamt * 3. All advertising materials mentioning features or use of this software
20 1.3.6.2 yamt * must display the following acknowledgement:
21 1.3.6.2 yamt * This product includes software developed by the NetBSD
22 1.3.6.2 yamt * Foundation, Inc. and its contributors.
23 1.3.6.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.3.6.2 yamt * contributors may be used to endorse or promote products derived
25 1.3.6.2 yamt * from this software without specific prior written permission.
26 1.3.6.2 yamt *
27 1.3.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.3.6.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.3.6.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.3.6.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.3.6.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.3.6.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.3.6.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.3.6.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.3.6.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.3.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.3.6.2 yamt * POSSIBILITY OF SUCH DAMAGE.
38 1.3.6.2 yamt */
39 1.3.6.2 yamt
40 1.3.6.2 yamt #include <sys/cdefs.h>
41 1.3.6.4 yamt __KERNEL_RCSID(0, "$NetBSD: uhci_cardbus.c,v 1.3.6.4 2007/10/27 11:30:12 yamt Exp $");
42 1.3.6.2 yamt
43 1.3.6.2 yamt #include "ehci_cardbus.h"
44 1.3.6.2 yamt
45 1.3.6.2 yamt #include <sys/param.h>
46 1.3.6.2 yamt #include <sys/systm.h>
47 1.3.6.2 yamt #include <sys/kernel.h>
48 1.3.6.2 yamt #include <sys/device.h>
49 1.3.6.2 yamt #include <sys/proc.h>
50 1.3.6.2 yamt #include <sys/queue.h>
51 1.3.6.2 yamt
52 1.3.6.4 yamt #include <sys/bus.h>
53 1.3.6.2 yamt
54 1.3.6.2 yamt #include <dev/cardbus/cardbusvar.h>
55 1.3.6.2 yamt #include <dev/cardbus/usb_cardbus.h>
56 1.3.6.2 yamt
57 1.3.6.2 yamt #include <dev/usb/usb.h>
58 1.3.6.2 yamt #include <dev/usb/usbdi.h>
59 1.3.6.2 yamt #include <dev/usb/usbdivar.h>
60 1.3.6.2 yamt #include <dev/usb/usb_mem.h>
61 1.3.6.2 yamt
62 1.3.6.2 yamt #include <dev/usb/uhcireg.h>
63 1.3.6.2 yamt #include <dev/usb/uhcivar.h>
64 1.3.6.2 yamt
65 1.3.6.2 yamt struct uhci_cardbus_softc {
66 1.3.6.2 yamt uhci_softc_t sc;
67 1.3.6.2 yamt #if NEHCI_CARDBUS > 0
68 1.3.6.2 yamt struct usb_cardbus sc_cardbus;
69 1.3.6.2 yamt #endif
70 1.3.6.2 yamt cardbus_chipset_tag_t sc_cc;
71 1.3.6.2 yamt cardbus_function_tag_t sc_cf;
72 1.3.6.2 yamt cardbus_devfunc_t sc_ct;
73 1.3.6.2 yamt cardbustag_t sc_tag;
74 1.3.6.2 yamt void *sc_ih; /* interrupt vectoring */
75 1.3.6.2 yamt };
76 1.3.6.2 yamt
77 1.3.6.2 yamt static int uhci_cardbus_match(struct device *, struct cfdata *, void *);
78 1.3.6.2 yamt static void uhci_cardbus_attach(struct device *, struct device *, void *);
79 1.3.6.2 yamt static int uhci_cardbus_detach(device_ptr_t, int);
80 1.3.6.2 yamt
81 1.3.6.2 yamt CFATTACH_DECL(uhci_cardbus, sizeof(struct uhci_cardbus_softc),
82 1.3.6.2 yamt uhci_cardbus_match, uhci_cardbus_attach, uhci_cardbus_detach, uhci_activate);
83 1.3.6.2 yamt
84 1.3.6.2 yamt #define CARDBUS_INTERFACE_UHCI PCI_INTERFACE_UHCI
85 1.3.6.2 yamt #define CARDBUS_CBIO PCI_CBIO
86 1.3.6.2 yamt #define cardbus_findvendor pci_findvendor
87 1.3.6.2 yamt #define cardbus_devinfo pci_devinfo
88 1.3.6.2 yamt
89 1.3.6.2 yamt static int
90 1.3.6.3 yamt uhci_cardbus_match(struct device *parent,
91 1.3.6.3 yamt struct cfdata *match, void *aux)
92 1.3.6.2 yamt {
93 1.3.6.2 yamt struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
94 1.3.6.2 yamt
95 1.3.6.2 yamt if (CARDBUS_CLASS(ca->ca_class) == CARDBUS_CLASS_SERIALBUS &&
96 1.3.6.2 yamt CARDBUS_SUBCLASS(ca->ca_class) == CARDBUS_SUBCLASS_SERIALBUS_USB &&
97 1.3.6.2 yamt CARDBUS_INTERFACE(ca->ca_class) == CARDBUS_INTERFACE_UHCI)
98 1.3.6.2 yamt return (1);
99 1.3.6.2 yamt
100 1.3.6.2 yamt return (0);
101 1.3.6.2 yamt }
102 1.3.6.2 yamt
103 1.3.6.2 yamt static void
104 1.3.6.3 yamt uhci_cardbus_attach(struct device *parent, struct device *self,
105 1.3.6.3 yamt void *aux)
106 1.3.6.2 yamt {
107 1.3.6.2 yamt struct uhci_cardbus_softc *sc = device_private(self);
108 1.3.6.2 yamt struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
109 1.3.6.2 yamt cardbus_devfunc_t ct = ca->ca_ct;
110 1.3.6.2 yamt cardbus_chipset_tag_t cc = ct->ct_cc;
111 1.3.6.2 yamt cardbus_function_tag_t cf = ct->ct_cf;
112 1.3.6.2 yamt cardbustag_t tag = ca->ca_tag;
113 1.3.6.2 yamt cardbusreg_t csr;
114 1.3.6.2 yamt const char *vendor;
115 1.3.6.2 yamt const char *devname = sc->sc.sc_bus.bdev.dv_xname;
116 1.3.6.2 yamt char devinfo[256];
117 1.3.6.2 yamt usbd_status r;
118 1.3.6.2 yamt
119 1.3.6.2 yamt cardbus_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
120 1.3.6.2 yamt printf(": %s (rev. 0x%02x)\n", devinfo, CARDBUS_REVISION(ca->ca_class));
121 1.3.6.2 yamt
122 1.3.6.2 yamt /* Map I/O registers */
123 1.3.6.2 yamt if (Cardbus_mapreg_map(ct, CARDBUS_CBIO, CARDBUS_MAPREG_TYPE_IO, 0,
124 1.3.6.2 yamt &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
125 1.3.6.2 yamt printf("%s: can't map i/o space\n", devname);
126 1.3.6.2 yamt return;
127 1.3.6.2 yamt }
128 1.3.6.2 yamt
129 1.3.6.2 yamt /* Disable interrupts, so we don't get any spurious ones. */
130 1.3.6.2 yamt bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
131 1.3.6.2 yamt
132 1.3.6.2 yamt sc->sc_cc = cc;
133 1.3.6.2 yamt sc->sc_cf = cf;
134 1.3.6.2 yamt sc->sc_ct = ct;
135 1.3.6.2 yamt sc->sc_tag = tag;
136 1.3.6.2 yamt sc->sc.sc_bus.dmatag = ca->ca_dmat;
137 1.3.6.2 yamt
138 1.3.6.2 yamt #if rbus
139 1.3.6.2 yamt #else
140 1.3.6.2 yamt XXX (ct->ct_cf->cardbus_io_open)(cc, 0, iob, iob + 0x40);
141 1.3.6.2 yamt #endif
142 1.3.6.2 yamt (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
143 1.3.6.2 yamt (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
144 1.3.6.2 yamt
145 1.3.6.2 yamt /* Enable the device. */
146 1.3.6.2 yamt csr = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
147 1.3.6.2 yamt cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
148 1.3.6.2 yamt csr | CARDBUS_COMMAND_MASTER_ENABLE
149 1.3.6.2 yamt | CARDBUS_COMMAND_IO_ENABLE);
150 1.3.6.2 yamt
151 1.3.6.2 yamt /* Map and establish the interrupt. */
152 1.3.6.2 yamt sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline,
153 1.3.6.2 yamt IPL_USB, uhci_intr, sc);
154 1.3.6.2 yamt if (sc->sc_ih == NULL) {
155 1.3.6.2 yamt printf("%s: couldn't establish interrupt\n", devname);
156 1.3.6.2 yamt return;
157 1.3.6.2 yamt }
158 1.3.6.2 yamt printf("%s: interrupting at %d\n", devname, ca->ca_intrline);
159 1.3.6.2 yamt
160 1.3.6.2 yamt /* Set LEGSUP register to its default value. */
161 1.3.6.2 yamt cardbus_conf_write(cc, cf, tag, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN);
162 1.3.6.2 yamt
163 1.3.6.2 yamt switch(cardbus_conf_read(cc, cf, tag, PCI_USBREV) & PCI_USBREV_MASK) {
164 1.3.6.2 yamt case PCI_USBREV_PRE_1_0:
165 1.3.6.2 yamt sc->sc.sc_bus.usbrev = USBREV_PRE_1_0;
166 1.3.6.2 yamt break;
167 1.3.6.2 yamt case PCI_USBREV_1_0:
168 1.3.6.2 yamt sc->sc.sc_bus.usbrev = USBREV_1_0;
169 1.3.6.2 yamt break;
170 1.3.6.2 yamt case PCI_USBREV_1_1:
171 1.3.6.2 yamt sc->sc.sc_bus.usbrev = USBREV_1_1;
172 1.3.6.2 yamt break;
173 1.3.6.2 yamt default:
174 1.3.6.2 yamt sc->sc.sc_bus.usbrev = USBREV_UNKNOWN;
175 1.3.6.2 yamt break;
176 1.3.6.2 yamt }
177 1.3.6.2 yamt
178 1.3.6.2 yamt /* Figure out vendor for root hub descriptor. */
179 1.3.6.2 yamt vendor = cardbus_findvendor(ca->ca_id);
180 1.3.6.2 yamt sc->sc.sc_id_vendor = CARDBUS_VENDOR(ca->ca_id);
181 1.3.6.2 yamt if (vendor)
182 1.3.6.2 yamt strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
183 1.3.6.2 yamt else
184 1.3.6.2 yamt snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
185 1.3.6.2 yamt "vendor 0x%04x", CARDBUS_VENDOR(ca->ca_id));
186 1.3.6.2 yamt
187 1.3.6.2 yamt r = uhci_init(&sc->sc);
188 1.3.6.2 yamt if (r != USBD_NORMAL_COMPLETION) {
189 1.3.6.2 yamt printf("%s: init failed, error=%d\n", devname, r);
190 1.3.6.2 yamt
191 1.3.6.2 yamt /* Avoid spurious interrupts. */
192 1.3.6.2 yamt cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
193 1.3.6.2 yamt sc->sc_ih = NULL;
194 1.3.6.2 yamt
195 1.3.6.2 yamt return;
196 1.3.6.2 yamt }
197 1.3.6.2 yamt
198 1.3.6.2 yamt #if NEHCI_CARDBUS > 0
199 1.3.6.2 yamt usb_cardbus_add(&sc->sc_cardbus, ca, &sc->sc.sc_bus);
200 1.3.6.2 yamt #endif
201 1.3.6.2 yamt
202 1.3.6.2 yamt /* Attach usb device. */
203 1.3.6.2 yamt sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
204 1.3.6.2 yamt usbctlprint);
205 1.3.6.2 yamt }
206 1.3.6.2 yamt
207 1.3.6.2 yamt static int
208 1.3.6.2 yamt uhci_cardbus_detach(device_ptr_t self, int flags)
209 1.3.6.2 yamt {
210 1.3.6.2 yamt struct uhci_cardbus_softc *sc = device_private(self);
211 1.3.6.2 yamt struct cardbus_devfunc *ct = sc->sc_ct;
212 1.3.6.2 yamt int rv;
213 1.3.6.2 yamt
214 1.3.6.2 yamt rv = uhci_detach(&sc->sc, flags);
215 1.3.6.2 yamt if (rv)
216 1.3.6.2 yamt return (rv);
217 1.3.6.2 yamt if (sc->sc_ih != NULL) {
218 1.3.6.2 yamt cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
219 1.3.6.2 yamt sc->sc_ih = NULL;
220 1.3.6.2 yamt }
221 1.3.6.2 yamt if (sc->sc.sc_size) {
222 1.3.6.2 yamt Cardbus_mapreg_unmap(ct, CARDBUS_CBIO, sc->sc.iot,
223 1.3.6.2 yamt sc->sc.ioh, sc->sc.sc_size);
224 1.3.6.2 yamt sc->sc.sc_size = 0;
225 1.3.6.2 yamt }
226 1.3.6.2 yamt #if NEHCI_CARDBUS > 0
227 1.3.6.2 yamt usb_cardbus_rem(&sc->sc_cardbus);
228 1.3.6.2 yamt #endif
229 1.3.6.2 yamt return (0);
230 1.3.6.2 yamt }
231