xref: /netbsd-src/sys/dev/cardbus/uhci_cardbus.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: uhci_cardbus.c,v 1.6 2007/10/19 11:59:40 ad 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@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.6 2007/10/19 11:59:40 ad 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(struct device *, struct cfdata *, void *);
78 static void	uhci_cardbus_attach(struct device *, struct device *, void *);
79 static int	uhci_cardbus_detach(device_ptr_t, int);
80 
81 CFATTACH_DECL(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(struct device *parent,
91     struct cfdata *match, void *aux)
92 {
93 	struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
94 
95 	if (CARDBUS_CLASS(ca->ca_class) == CARDBUS_CLASS_SERIALBUS &&
96 	    CARDBUS_SUBCLASS(ca->ca_class) == CARDBUS_SUBCLASS_SERIALBUS_USB &&
97 	    CARDBUS_INTERFACE(ca->ca_class) == CARDBUS_INTERFACE_UHCI)
98 		return (1);
99 
100 	return (0);
101 }
102 
103 static void
104 uhci_cardbus_attach(struct device *parent, struct device *self,
105     void *aux)
106 {
107 	struct uhci_cardbus_softc *sc = device_private(self);
108 	struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
109 	cardbus_devfunc_t ct = ca->ca_ct;
110 	cardbus_chipset_tag_t cc = ct->ct_cc;
111 	cardbus_function_tag_t cf = ct->ct_cf;
112 	cardbustag_t tag = ca->ca_tag;
113 	cardbusreg_t csr;
114 	const char *vendor;
115 	const char *devname = sc->sc.sc_bus.bdev.dv_xname;
116 	char devinfo[256];
117 	usbd_status r;
118 
119 	cardbus_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
120 	printf(": %s (rev. 0x%02x)\n", devinfo, CARDBUS_REVISION(ca->ca_class));
121 
122 	/* Map I/O registers */
123 	if (Cardbus_mapreg_map(ct, CARDBUS_CBIO, CARDBUS_MAPREG_TYPE_IO, 0,
124 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
125 		printf("%s: can't map i/o space\n", devname);
126 		return;
127 	}
128 
129 	/* Disable interrupts, so we don't get any spurious ones. */
130 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
131 
132 	sc->sc_cc = cc;
133 	sc->sc_cf = cf;
134 	sc->sc_ct = ct;
135 	sc->sc_tag = tag;
136 	sc->sc.sc_bus.dmatag = ca->ca_dmat;
137 
138 #if rbus
139 #else
140 XXX	(ct->ct_cf->cardbus_io_open)(cc, 0, iob, iob + 0x40);
141 #endif
142 	(ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
143 	(ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
144 
145 	/* Enable the device. */
146 	csr = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
147 	cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
148 		       csr | CARDBUS_COMMAND_MASTER_ENABLE
149 			   | CARDBUS_COMMAND_IO_ENABLE);
150 
151 	/* Map and establish the interrupt. */
152 	sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline,
153 					   IPL_USB, uhci_intr, sc);
154 	if (sc->sc_ih == NULL) {
155 		printf("%s: couldn't establish interrupt\n", devname);
156 		return;
157 	}
158 	printf("%s: interrupting at %d\n", devname, ca->ca_intrline);
159 
160 	/* Set LEGSUP register to its default value. */
161 	cardbus_conf_write(cc, cf, tag, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN);
162 
163 	switch(cardbus_conf_read(cc, cf, tag, PCI_USBREV) & PCI_USBREV_MASK) {
164 	case PCI_USBREV_PRE_1_0:
165 		sc->sc.sc_bus.usbrev = USBREV_PRE_1_0;
166 		break;
167 	case PCI_USBREV_1_0:
168 		sc->sc.sc_bus.usbrev = USBREV_1_0;
169 		break;
170 	case PCI_USBREV_1_1:
171 		sc->sc.sc_bus.usbrev = USBREV_1_1;
172 		break;
173 	default:
174 		sc->sc.sc_bus.usbrev = USBREV_UNKNOWN;
175 		break;
176 	}
177 
178 	/* Figure out vendor for root hub descriptor. */
179 	vendor = cardbus_findvendor(ca->ca_id);
180 	sc->sc.sc_id_vendor = CARDBUS_VENDOR(ca->ca_id);
181 	if (vendor)
182 		strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
183 	else
184 		snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
185 		    "vendor 0x%04x", CARDBUS_VENDOR(ca->ca_id));
186 
187 	r = uhci_init(&sc->sc);
188 	if (r != USBD_NORMAL_COMPLETION) {
189 		printf("%s: init failed, error=%d\n", devname, r);
190 
191 		/* Avoid spurious interrupts. */
192 		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
193 		sc->sc_ih = NULL;
194 
195 		return;
196 	}
197 
198 #if NEHCI_CARDBUS > 0
199 	usb_cardbus_add(&sc->sc_cardbus, ca, &sc->sc.sc_bus);
200 #endif
201 
202 	/* Attach usb device. */
203 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
204 				       usbctlprint);
205 }
206 
207 static int
208 uhci_cardbus_detach(device_ptr_t self, int flags)
209 {
210 	struct uhci_cardbus_softc *sc = device_private(self);
211 	struct cardbus_devfunc *ct = sc->sc_ct;
212 	int rv;
213 
214 	rv = uhci_detach(&sc->sc, flags);
215 	if (rv)
216 		return (rv);
217 	if (sc->sc_ih != NULL) {
218 		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
219 		sc->sc_ih = NULL;
220 	}
221 	if (sc->sc.sc_size) {
222 		Cardbus_mapreg_unmap(ct, CARDBUS_CBIO, sc->sc.iot,
223 		    sc->sc.ioh, sc->sc.sc_size);
224 		sc->sc.sc_size = 0;
225 	}
226 #if NEHCI_CARDBUS > 0
227 	usb_cardbus_rem(&sc->sc_cardbus);
228 #endif
229 	return (0);
230 }
231