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