1 /* $NetBSD: uhci_cardbus.c,v 1.12 2008/07/11 21:02:53 dyoung Exp $ */
4 * Copyright (c) 1998-2005 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net) at
9 * Carlstedt Research & Technology.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: uhci_cardbus.c,v 1.12 2008/07/11 21:02:53 dyoung Exp $");
36 #include "ehci_cardbus.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
43 #include <sys/queue.h>
47 #include <dev/cardbus/cardbusvar.h>
48 #include <dev/cardbus/usb_cardbus.h>
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52 #include <dev/usb/usbdivar.h>
53 #include <dev/usb/usb_mem.h>
55 #include <dev/usb/uhcireg.h>
56 #include <dev/usb/uhcivar.h>
58 struct uhci_cardbus_softc
{
61 struct usb_cardbus sc_cardbus
;
63 cardbus_chipset_tag_t sc_cc
;
64 cardbus_function_tag_t sc_cf
;
65 cardbus_devfunc_t sc_ct
;
67 void *sc_ih
; /* interrupt vectoring */
70 static int uhci_cardbus_match(device_t
, cfdata_t
, void *);
71 static void uhci_cardbus_attach(device_t
, device_t
, void *);
72 static int uhci_cardbus_detach(device_t
, int);
74 CFATTACH_DECL_NEW(uhci_cardbus
, sizeof(struct uhci_cardbus_softc
),
75 uhci_cardbus_match
, uhci_cardbus_attach
, uhci_cardbus_detach
, uhci_activate
);
77 #define CARDBUS_INTERFACE_UHCI PCI_INTERFACE_UHCI
78 #define CARDBUS_CBIO PCI_CBIO
79 #define cardbus_findvendor pci_findvendor
80 #define cardbus_devinfo pci_devinfo
83 uhci_cardbus_match(device_t parent
, cfdata_t match
, void *aux
)
85 struct cardbus_attach_args
*ca
= (struct cardbus_attach_args
*)aux
;
87 if (CARDBUS_CLASS(ca
->ca_class
) == CARDBUS_CLASS_SERIALBUS
&&
88 CARDBUS_SUBCLASS(ca
->ca_class
) == CARDBUS_SUBCLASS_SERIALBUS_USB
&&
89 CARDBUS_INTERFACE(ca
->ca_class
) == CARDBUS_INTERFACE_UHCI
)
96 uhci_cardbus_attach(device_t parent
, device_t self
,
99 struct uhci_cardbus_softc
*sc
= device_private(self
);
100 struct cardbus_attach_args
*ca
= (struct cardbus_attach_args
*)aux
;
101 cardbus_devfunc_t ct
= ca
->ca_ct
;
102 cardbus_chipset_tag_t cc
= ct
->ct_cc
;
103 cardbus_function_tag_t cf
= ct
->ct_cf
;
104 cardbustag_t tag
= ca
->ca_tag
;
107 const char *devname
= device_xname(self
);
111 sc
->sc
.sc_dev
= self
;
112 sc
->sc
.sc_bus
.hci_private
= sc
;
114 cardbus_devinfo(ca
->ca_id
, ca
->ca_class
, 0, devinfo
, sizeof(devinfo
));
115 printf(": %s (rev. 0x%02x)\n", devinfo
, CARDBUS_REVISION(ca
->ca_class
));
117 /* Map I/O registers */
118 if (Cardbus_mapreg_map(ct
, CARDBUS_CBIO
, CARDBUS_MAPREG_TYPE_IO
, 0,
119 &sc
->sc
.iot
, &sc
->sc
.ioh
, NULL
, &sc
->sc
.sc_size
)) {
120 printf("%s: can't map i/o space\n", devname
);
128 sc
->sc
.sc_bus
.dmatag
= ca
->ca_dmat
;
132 XXX (ct
->ct_cf
->cardbus_io_open
)(cc
, 0, iob
, iob
+ 0x40);
134 (ct
->ct_cf
->cardbus_ctrl
)(cc
, CARDBUS_IO_ENABLE
);
135 (ct
->ct_cf
->cardbus_ctrl
)(cc
, CARDBUS_BM_ENABLE
);
137 /* Enable the device. */
138 csr
= cardbus_conf_read(cc
, cf
, tag
, CARDBUS_COMMAND_STATUS_REG
);
139 cardbus_conf_write(cc
, cf
, tag
, CARDBUS_COMMAND_STATUS_REG
,
140 csr
| CARDBUS_COMMAND_MASTER_ENABLE
141 | CARDBUS_COMMAND_IO_ENABLE
);
143 /* Disable interrupts, so we don't get any spurious ones. */
144 bus_space_write_2(sc
->sc
.iot
, sc
->sc
.ioh
, UHCI_INTR
, 0);
146 /* Map and establish the interrupt. */
147 sc
->sc_ih
= cardbus_intr_establish(cc
, cf
, ca
->ca_intrline
,
148 IPL_USB
, uhci_intr
, sc
);
149 if (sc
->sc_ih
== NULL
) {
150 printf("%s: couldn't establish interrupt\n", devname
);
154 /* Set LEGSUP register to its default value. */
155 cardbus_conf_write(cc
, cf
, tag
, PCI_LEGSUP
, PCI_LEGSUP_USBPIRQDEN
);
157 switch(cardbus_conf_read(cc
, cf
, tag
, PCI_USBREV
) & PCI_USBREV_MASK
) {
158 case PCI_USBREV_PRE_1_0
:
159 sc
->sc
.sc_bus
.usbrev
= USBREV_PRE_1_0
;
162 sc
->sc
.sc_bus
.usbrev
= USBREV_1_0
;
165 sc
->sc
.sc_bus
.usbrev
= USBREV_1_1
;
168 sc
->sc
.sc_bus
.usbrev
= USBREV_UNKNOWN
;
172 /* Figure out vendor for root hub descriptor. */
173 vendor
= cardbus_findvendor(ca
->ca_id
);
174 sc
->sc
.sc_id_vendor
= CARDBUS_VENDOR(ca
->ca_id
);
176 strlcpy(sc
->sc
.sc_vendor
, vendor
, sizeof(sc
->sc
.sc_vendor
));
178 snprintf(sc
->sc
.sc_vendor
, sizeof(sc
->sc
.sc_vendor
),
179 "vendor 0x%04x", CARDBUS_VENDOR(ca
->ca_id
));
181 r
= uhci_init(&sc
->sc
);
182 if (r
!= USBD_NORMAL_COMPLETION
) {
183 printf("%s: init failed, error=%d\n", devname
, r
);
185 /* Avoid spurious interrupts. */
186 cardbus_intr_disestablish(sc
->sc_cc
, sc
->sc_cf
, sc
->sc_ih
);
192 #if NEHCI_CARDBUS > 0
193 usb_cardbus_add(&sc
->sc_cardbus
, ca
, self
);
196 /* Attach usb device. */
197 sc
->sc
.sc_child
= config_found(self
, &sc
->sc
.sc_bus
, usbctlprint
);
201 uhci_cardbus_detach(device_t self
, int flags
)
203 struct uhci_cardbus_softc
*sc
= device_private(self
);
204 struct cardbus_devfunc
*ct
= sc
->sc_ct
;
207 rv
= uhci_detach(&sc
->sc
, flags
);
210 if (sc
->sc_ih
!= NULL
) {
211 cardbus_intr_disestablish(sc
->sc_cc
, sc
->sc_cf
, sc
->sc_ih
);
214 if (sc
->sc
.sc_size
) {
215 Cardbus_mapreg_unmap(ct
, CARDBUS_CBIO
, sc
->sc
.iot
,
216 sc
->sc
.ioh
, sc
->sc
.sc_size
);
219 #if NEHCI_CARDBUS > 0
220 usb_cardbus_rem(&sc
->sc_cardbus
);