ehci_cardbus.c revision 1.9
1/*	$OpenBSD: ehci_cardbus.c,v 1.9 2007/05/20 00:52:26 jsg Exp $ */
2/*	$NetBSD: ehci_cardbus.c,v 1.6.6.3 2004/09/21 13:27:25 skrll 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#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/rwlock.h>
45#include <sys/device.h>
46#include <sys/proc.h>
47
48#include <machine/bus.h>
49
50#include <dev/cardbus/cardbusvar.h>
51#include <dev/pci/pcidevs.h>
52
53#include <dev/usb/usb.h>
54#include <dev/usb/usbdi.h>
55#include <dev/usb/usbdivar.h>
56#include <dev/usb/usb_mem.h>
57
58#include <dev/usb/ehcireg.h>
59#include <dev/usb/ehcivar.h>
60
61#ifdef EHCI_DEBUG
62#define DPRINTF(x)	if (ehcidebug) printf x
63extern int ehcidebug;
64#else
65#define DPRINTF(x)
66#endif
67
68
69int	ehci_cardbus_match(struct device *, void *, void *);
70void	ehci_cardbus_attach(struct device *, struct device *, void *);
71int	ehci_cardbus_detach(struct device *, int);
72
73struct ehci_cardbus_softc {
74	ehci_softc_t		sc;
75	cardbus_chipset_tag_t	sc_cc;
76	cardbus_function_tag_t	sc_cf;
77	cardbus_devfunc_t	sc_ct;
78	void 			*sc_ih;		/* interrupt vectoring */
79};
80
81struct cfattach ehci_cardbus_ca = {
82	sizeof(struct ehci_cardbus_softc), ehci_cardbus_match,
83	    ehci_cardbus_attach, ehci_cardbus_detach, ehci_activate
84};
85
86#define CARDBUS_INTERFACE_EHCI PCI_INTERFACE_EHCI
87#define CARDBUS_CBMEM PCI_CBMEM
88#define cardbus_findvendor pci_findvendor
89#define cardbus_devinfo pci_devinfo
90
91int
92ehci_cardbus_match(struct device *parent, void *match, void *aux)
93{
94	struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
95
96	if (CARDBUS_CLASS(ca->ca_class) == CARDBUS_CLASS_SERIALBUS &&
97	    CARDBUS_SUBCLASS(ca->ca_class) == CARDBUS_SUBCLASS_SERIALBUS_USB &&
98	    CARDBUS_INTERFACE(ca->ca_class) == CARDBUS_INTERFACE_EHCI)
99		return (1);
100
101	return (0);
102}
103
104void
105ehci_cardbus_attach(struct device *parent, struct device *self, void *aux)
106{
107	struct ehci_cardbus_softc *sc = (struct ehci_cardbus_softc *)self;
108	struct cardbus_attach_args *ca = 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	cardbusreg_t csr;
113	char devinfo[256];
114	usbd_status r;
115	const char *vendor;
116	const char *devname = sc->sc.sc_bus.bdev.dv_xname;
117
118	cardbus_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
119	printf(" %s", devinfo);
120
121	/* Map I/O registers */
122	if (Cardbus_mapreg_map(ct, CARDBUS_CBMEM, CARDBUS_MAPREG_TYPE_MEM, 0,
123			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
124		printf("%s: can't map mem space\n", devname);
125		return;
126	}
127
128	sc->sc_cc = cc;
129	sc->sc_cf = cf;
130	sc->sc_ct = ct;
131	sc->sc.sc_bus.dmatag = ca->ca_dmat;
132
133	(ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
134	(ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
135
136	/* Enable the device. */
137	csr = cardbus_conf_read(cc, cf, ca->ca_tag,
138				CARDBUS_COMMAND_STATUS_REG);
139	cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG,
140		       csr | CARDBUS_COMMAND_MASTER_ENABLE
141			   | CARDBUS_COMMAND_MEM_ENABLE);
142
143	/* Disable interrupts, so we don't get any spurious ones. */
144	sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
145	DPRINTF((": offs=%d", devname, sc->sc.sc_offs));
146	EOWRITE2(&sc->sc, EHCI_USBINTR, 0);
147
148	sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline,
149					   IPL_USB, ehci_intr, sc, devname);
150	if (sc->sc_ih == NULL) {
151		printf(": unable to establish interrupt\n");
152		return;
153	}
154	printf(": irq %d\n", ca->ca_intrline);
155
156	/* Figure out vendor for root hub descriptor. */
157	vendor = cardbus_findvendor(ca->ca_id);
158	sc->sc.sc_id_vendor = CARDBUS_VENDOR(ca->ca_id);
159	if (vendor)
160		strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
161	else
162		snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
163		    "vendor 0x%04x", CARDBUS_VENDOR(ca->ca_id));
164
165	r = ehci_init(&sc->sc);
166	if (r != USBD_NORMAL_COMPLETION) {
167		printf("%s: init failed, error=%d\n", devname, r);
168
169		/* Avoid spurious interrupts. */
170		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
171		sc->sc_ih = NULL;
172
173		return;
174	}
175
176	sc->sc.sc_shutdownhook = shutdownhook_establish(ehci_shutdown, &sc->sc);
177
178	/* Attach usb device. */
179	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
180				       usbctlprint);
181}
182
183int
184ehci_cardbus_detach(struct device *self, int flags)
185{
186	struct ehci_cardbus_softc *sc = (struct ehci_cardbus_softc *)self;
187	struct cardbus_devfunc *ct = sc->sc_ct;
188	int rv;
189
190	rv = ehci_detach(&sc->sc, flags);
191	if (rv)
192		return (rv);
193	if (sc->sc_ih != NULL) {
194		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
195		sc->sc_ih = NULL;
196	}
197	if (sc->sc.sc_size) {
198		Cardbus_mapreg_unmap(ct, CARDBUS_CBMEM, sc->sc.iot,
199		    sc->sc.ioh, sc->sc.sc_size);
200		sc->sc.sc_size = 0;
201	}
202	return (0);
203}
204
205