xref: /netbsd-src/sys/dev/cardbus/ohci_cardbus.c (revision 82ad575716605df31379cf04a2f3efbc97b8a6f5)
1 /*	$NetBSD: ohci_cardbus.c,v 1.39 2011/08/01 11:20:28 drochner Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 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  *
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.
31  */
32 
33 /*
34  * USB Open Host Controller driver.
35  *
36  * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
37  * USB spec: http://www.teleport.com/cgi-bin/mailmerge.cgi/~usb/cgiform.tpl
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: ohci_cardbus.c,v 1.39 2011/08/01 11:20:28 drochner 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 
51 #include <sys/bus.h>
52 
53 #if defined pciinc
54 #include <dev/pci/pcidevs.h>
55 #endif
56 
57 #include <dev/cardbus/cardbusvar.h>
58 #include <dev/pci/pcidevs.h>
59 
60 #include <dev/cardbus/usb_cardbus.h>
61 
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbdi.h>
64 #include <dev/usb/usbdivar.h>
65 #include <dev/usb/usb_mem.h>
66 
67 #include <dev/usb/ohcireg.h>
68 #include <dev/usb/ohcivar.h>
69 
70 int	ohci_cardbus_match(device_t, cfdata_t, void *);
71 void	ohci_cardbus_attach(device_t, device_t, void *);
72 int	ohci_cardbus_detach(device_t, int);
73 
74 struct ohci_cardbus_softc {
75 	ohci_softc_t		sc;
76 #if NEHCI_CARDBUS > 0
77 	struct usb_cardbus	sc_cardbus;
78 #endif
79 	cardbus_chipset_tag_t	sc_cc;
80 	cardbus_function_tag_t	sc_cf;
81 	cardbus_devfunc_t	sc_ct;
82 	void 			*sc_ih;		/* interrupt vectoring */
83 };
84 
85 CFATTACH_DECL_NEW(ohci_cardbus, sizeof(struct ohci_cardbus_softc),
86     ohci_cardbus_match, ohci_cardbus_attach, ohci_cardbus_detach, ohci_activate);
87 
88 int
89 ohci_cardbus_match(device_t parent, cfdata_t match, void *aux)
90 {
91 	struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
92 
93 	if (PCI_CLASS(ca->ca_class) == PCI_CLASS_SERIALBUS &&
94 	    PCI_SUBCLASS(ca->ca_class) == PCI_SUBCLASS_SERIALBUS_USB &&
95 	    PCI_INTERFACE(ca->ca_class) == PCI_INTERFACE_OHCI)
96 		return (1);
97 
98 	return (0);
99 }
100 
101 void
102 ohci_cardbus_attach(device_t parent, device_t self, void *aux)
103 {
104 	struct ohci_cardbus_softc *sc = device_private(self);
105 	struct cardbus_attach_args *ca = aux;
106 	cardbus_devfunc_t ct = ca->ca_ct;
107 	cardbus_chipset_tag_t cc = ct->ct_cc;
108 	cardbus_function_tag_t cf = ct->ct_cf;
109 	pcireg_t csr;
110 	char devinfo[256];
111 	usbd_status r;
112 	const char *vendor;
113 	const char *devname = device_xname(self);
114 
115 	sc->sc.sc_dev = self;
116 	sc->sc.sc_bus.hci_private = sc;
117 
118 	pci_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
119 	printf(": %s (rev. 0x%02x)\n", devinfo,
120 	       PCI_REVISION(ca->ca_class));
121 
122 	/* Map I/O registers */
123 	if (Cardbus_mapreg_map(ct, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
124 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
125 		printf("%s: can't map mem space\n", devname);
126 		return;
127 	}
128 
129 	sc->sc_cc = cc;
130 	sc->sc_cf = cf;
131 	sc->sc_ct = ct;
132 	sc->sc.sc_bus.dmatag = ca->ca_dmat;
133 
134 	/* Enable the device. */
135 	csr = Cardbus_conf_read(ct, ca->ca_tag,
136 				PCI_COMMAND_STATUS_REG);
137 	Cardbus_conf_write(ct, ca->ca_tag, PCI_COMMAND_STATUS_REG,
138 		       csr | PCI_COMMAND_MASTER_ENABLE
139 			   | PCI_COMMAND_MEM_ENABLE);
140 
141 	/* Disable interrupts, so we don't can any spurious ones. */
142 	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
143 			  OHCI_ALL_INTRS);
144 
145 	sc->sc_ih = Cardbus_intr_establish(ct, IPL_USB, ohci_intr, sc);
146 	if (sc->sc_ih == NULL) {
147 		printf("%s: couldn't establish interrupt\n", devname);
148 		return;
149 	}
150 
151 	/* Figure out vendor for root hub descriptor. */
152 	vendor = pci_findvendor(ca->ca_id);
153 	sc->sc.sc_id_vendor = PCI_VENDOR(ca->ca_id);
154 	if (vendor)
155 		strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
156 	else
157 		snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
158 		    "vendor 0x%04x", PCI_VENDOR(ca->ca_id));
159 
160 	r = ohci_init(&sc->sc);
161 	if (r != USBD_NORMAL_COMPLETION) {
162 		printf("%s: init failed, error=%d\n", devname, r);
163 
164 		/* Avoid spurious interrupts. */
165 		Cardbus_intr_disestablish(ct, sc->sc_ih);
166 		sc->sc_ih = 0;
167 
168 		return;
169 	}
170 
171 #if NEHCI_CARDBUS > 0
172 	usb_cardbus_add(&sc->sc_cardbus, ca, self);
173 #endif
174 
175 	if (!pmf_device_register1(self, ohci_suspend, ohci_resume,
176 	                          ohci_shutdown))
177 		aprint_error_dev(self, "couldn't establish power handler\n");
178 
179 	/* Attach usb device. */
180 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
181 }
182 
183 int
184 ohci_cardbus_detach(device_t self, int flags)
185 {
186 	struct ohci_cardbus_softc *sc = device_private(self);
187 	struct cardbus_devfunc *ct = sc->sc_ct;
188 	int rv;
189 
190 	rv = ohci_detach(&sc->sc, flags);
191 	if (rv)
192 		return (rv);
193 	if (sc->sc_ih != NULL) {
194 		Cardbus_intr_disestablish(ct, sc->sc_ih);
195 		sc->sc_ih = NULL;
196 	}
197 	if (sc->sc.sc_size) {
198 		Cardbus_mapreg_unmap(ct, PCI_CBMEM, sc->sc.iot,
199 		    sc->sc.ioh, sc->sc.sc_size);
200 		sc->sc.sc_size = 0;
201 	}
202 #if NEHCI_CARDBUS > 0
203 	usb_cardbus_rem(&sc->sc_cardbus);
204 #endif
205 	return (0);
206 }
207