uhci_cardbus.c revision 1.9 1 /* $NetBSD: uhci_cardbus.c,v 1.9 2008/04/09 11:32:26 skd Exp $ */
2
3 /*
4 * Copyright (c) 1998-2005 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/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: uhci_cardbus.c,v 1.9 2008/04/09 11:32:26 skd Exp $");
42
43 #include "ehci_cardbus.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/device.h>
49 #include <sys/proc.h>
50 #include <sys/queue.h>
51
52 #include <sys/bus.h>
53
54 #include <dev/cardbus/cardbusvar.h>
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/uhcireg.h>
63 #include <dev/usb/uhcivar.h>
64
65 struct uhci_cardbus_softc {
66 uhci_softc_t sc;
67 #if NEHCI_CARDBUS > 0
68 struct usb_cardbus sc_cardbus;
69 #endif
70 cardbus_chipset_tag_t sc_cc;
71 cardbus_function_tag_t sc_cf;
72 cardbus_devfunc_t sc_ct;
73 cardbustag_t sc_tag;
74 void *sc_ih; /* interrupt vectoring */
75 };
76
77 static int uhci_cardbus_match(device_t, struct cfdata *, void *);
78 static void uhci_cardbus_attach(device_t, device_t, void *);
79 static int uhci_cardbus_detach(device_t, int);
80
81 CFATTACH_DECL_NEW(uhci_cardbus, sizeof(struct uhci_cardbus_softc),
82 uhci_cardbus_match, uhci_cardbus_attach, uhci_cardbus_detach, uhci_activate);
83
84 #define CARDBUS_INTERFACE_UHCI PCI_INTERFACE_UHCI
85 #define CARDBUS_CBIO PCI_CBIO
86 #define cardbus_findvendor pci_findvendor
87 #define cardbus_devinfo pci_devinfo
88
89 static int
90 uhci_cardbus_match(device_t parent, struct cfdata *match, void *aux)
91 {
92 struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
93
94 if (CARDBUS_CLASS(ca->ca_class) == CARDBUS_CLASS_SERIALBUS &&
95 CARDBUS_SUBCLASS(ca->ca_class) == CARDBUS_SUBCLASS_SERIALBUS_USB &&
96 CARDBUS_INTERFACE(ca->ca_class) == CARDBUS_INTERFACE_UHCI)
97 return (1);
98
99 return (0);
100 }
101
102 static void
103 uhci_cardbus_attach(device_t parent, device_t self,
104 void *aux)
105 {
106 struct uhci_cardbus_softc *sc = device_private(self);
107 struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
108 cardbus_devfunc_t ct = ca->ca_ct;
109 cardbus_chipset_tag_t cc = ct->ct_cc;
110 cardbus_function_tag_t cf = ct->ct_cf;
111 cardbustag_t tag = ca->ca_tag;
112 cardbusreg_t csr;
113 const char *vendor;
114 const char *devname = device_xname(self);
115 char devinfo[256];
116 usbd_status r;
117
118 sc->sc.sc_dev = self;
119 sc->sc.sc_bus.hci_private = sc;
120
121 cardbus_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
122 printf(": %s (rev. 0x%02x)\n", devinfo, CARDBUS_REVISION(ca->ca_class));
123
124 /* Map I/O registers */
125 if (Cardbus_mapreg_map(ct, CARDBUS_CBIO, CARDBUS_MAPREG_TYPE_IO, 0,
126 &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
127 printf("%s: can't map i/o space\n", devname);
128 return;
129 }
130
131 /* Disable interrupts, so we don't get any spurious ones. */
132 bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
133
134 sc->sc_cc = cc;
135 sc->sc_cf = cf;
136 sc->sc_ct = ct;
137 sc->sc_tag = tag;
138 sc->sc.sc_bus.dmatag = ca->ca_dmat;
139
140 #if rbus
141 #else
142 XXX (ct->ct_cf->cardbus_io_open)(cc, 0, iob, iob + 0x40);
143 #endif
144 (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
145 (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
146
147 /* Enable the device. */
148 csr = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
149 cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
150 csr | CARDBUS_COMMAND_MASTER_ENABLE
151 | CARDBUS_COMMAND_IO_ENABLE);
152
153 /* Map and establish the interrupt. */
154 sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline,
155 IPL_USB, uhci_intr, sc);
156 if (sc->sc_ih == NULL) {
157 printf("%s: couldn't establish interrupt\n", devname);
158 return;
159 }
160 printf("%s: interrupting at %d\n", devname, ca->ca_intrline);
161
162 /* Set LEGSUP register to its default value. */
163 cardbus_conf_write(cc, cf, tag, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN);
164
165 switch(cardbus_conf_read(cc, cf, tag, PCI_USBREV) & PCI_USBREV_MASK) {
166 case PCI_USBREV_PRE_1_0:
167 sc->sc.sc_bus.usbrev = USBREV_PRE_1_0;
168 break;
169 case PCI_USBREV_1_0:
170 sc->sc.sc_bus.usbrev = USBREV_1_0;
171 break;
172 case PCI_USBREV_1_1:
173 sc->sc.sc_bus.usbrev = USBREV_1_1;
174 break;
175 default:
176 sc->sc.sc_bus.usbrev = USBREV_UNKNOWN;
177 break;
178 }
179
180 /* Figure out vendor for root hub descriptor. */
181 vendor = cardbus_findvendor(ca->ca_id);
182 sc->sc.sc_id_vendor = CARDBUS_VENDOR(ca->ca_id);
183 if (vendor)
184 strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
185 else
186 snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
187 "vendor 0x%04x", CARDBUS_VENDOR(ca->ca_id));
188
189 r = uhci_init(&sc->sc);
190 if (r != USBD_NORMAL_COMPLETION) {
191 printf("%s: init failed, error=%d\n", devname, r);
192
193 /* Avoid spurious interrupts. */
194 cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
195 sc->sc_ih = NULL;
196
197 return;
198 }
199
200 #if NEHCI_CARDBUS > 0
201 usb_cardbus_add(&sc->sc_cardbus, ca, self);
202 #endif
203
204 /* Attach usb device. */
205 sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
206 }
207
208 static int
209 uhci_cardbus_detach(device_t self, int flags)
210 {
211 struct uhci_cardbus_softc *sc = device_private(self);
212 struct cardbus_devfunc *ct = sc->sc_ct;
213 int rv;
214
215 rv = uhci_detach(&sc->sc, flags);
216 if (rv)
217 return (rv);
218 if (sc->sc_ih != NULL) {
219 cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
220 sc->sc_ih = NULL;
221 }
222 if (sc->sc.sc_size) {
223 Cardbus_mapreg_unmap(ct, CARDBUS_CBIO, sc->sc.iot,
224 sc->sc.ioh, sc->sc.sc_size);
225 sc->sc.sc_size = 0;
226 }
227 #if NEHCI_CARDBUS > 0
228 usb_cardbus_rem(&sc->sc_cardbus);
229 #endif
230 return (0);
231 }
232