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