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