ehci_cardbus.c revision 1.17
1/*	$OpenBSD: ehci_cardbus.c,v 1.17 2013/04/15 09:23:00 mglocker 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 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>
37#include <sys/rwlock.h>
38#include <sys/device.h>
39#include <sys/proc.h>
40
41#include <machine/bus.h>
42
43#include <dev/cardbus/cardbusvar.h>
44#include <dev/pci/pcidevs.h>
45
46#include <dev/usb/usb.h>
47#include <dev/usb/usbdi.h>
48#include <dev/usb/usbdivar.h>
49#include <dev/usb/usb_mem.h>
50
51#include <dev/usb/ehcireg.h>
52#include <dev/usb/ehcivar.h>
53
54#ifdef EHCI_DEBUG
55#define DPRINTF(x)	if (ehcidebug) printf x
56extern int ehcidebug;
57#else
58#define DPRINTF(x)
59#endif
60
61
62int	ehci_cardbus_match(struct device *, void *, void *);
63void	ehci_cardbus_attach(struct device *, struct device *, void *);
64int	ehci_cardbus_detach(struct device *, int);
65
66struct ehci_cardbus_softc {
67	struct ehci_softc	sc;
68	cardbus_chipset_tag_t	sc_cc;
69	cardbus_function_tag_t	sc_cf;
70	cardbus_devfunc_t	sc_ct;
71	void 			*sc_ih;		/* interrupt vectoring */
72};
73
74struct cfattach ehci_cardbus_ca = {
75	sizeof(struct ehci_cardbus_softc), ehci_cardbus_match,
76	ehci_cardbus_attach, ehci_cardbus_detach, ehci_activate
77};
78
79#define CARDBUS_CBMEM PCI_CBMEM
80#define cardbus_findvendor pci_findvendor
81
82int
83ehci_cardbus_match(struct device *parent, void *match, void *aux)
84{
85	struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
86
87	if (PCI_CLASS(ca->ca_class) == PCI_CLASS_SERIALBUS &&
88	    PCI_SUBCLASS(ca->ca_class) == PCI_SUBCLASS_SERIALBUS_USB &&
89	    PCI_INTERFACE(ca->ca_class) == PCI_INTERFACE_EHCI)
90		return (1);
91
92	return (0);
93}
94
95void
96ehci_cardbus_attach(struct device *parent, struct device *self, void *aux)
97{
98	struct ehci_cardbus_softc *sc = (struct ehci_cardbus_softc *)self;
99	struct cardbus_attach_args *ca = aux;
100	cardbus_devfunc_t ct = ca->ca_ct;
101	cardbus_chipset_tag_t cc = ct->ct_cc;
102	pci_chipset_tag_t pc = ca->ca_pc;
103	cardbus_function_tag_t cf = ct->ct_cf;
104	pcireg_t csr;
105	usbd_status r;
106	const char *vendor;
107	const char *devname = sc->sc.sc_bus.bdev.dv_xname;
108
109	/* Map I/O registers */
110	if (Cardbus_mapreg_map(ct, CARDBUS_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
111			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
112		printf(": can't map mem space\n");
113		return;
114	}
115
116	sc->sc_cc = cc;
117	sc->sc_cf = cf;
118	sc->sc_ct = ct;
119	sc->sc.sc_bus.dmatag = ca->ca_dmat;
120
121	(ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
122	(ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
123
124	/* Enable the device. */
125	csr = pci_conf_read(pc, ca->ca_tag,
126				PCI_COMMAND_STATUS_REG);
127	pci_conf_write(pc, ca->ca_tag, PCI_COMMAND_STATUS_REG,
128		       csr | PCI_COMMAND_MASTER_ENABLE
129			   | PCI_COMMAND_MEM_ENABLE);
130
131	/* Disable interrupts, so we don't get any spurious ones. */
132	sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
133	DPRINTF((": offs=%d", devname, sc->sc.sc_offs));
134	EOWRITE2(&sc->sc, EHCI_USBINTR, 0);
135
136	sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline,
137					   IPL_USB, ehci_intr, sc, devname);
138	if (sc->sc_ih == NULL) {
139		printf(": unable to establish interrupt\n");
140		return;
141	}
142	printf(": irq %d\n", ca->ca_intrline);
143
144	/* Figure out vendor for root hub descriptor. */
145	vendor = cardbus_findvendor(ca->ca_id);
146	sc->sc.sc_id_vendor = PCI_VENDOR(ca->ca_id);
147	if (vendor)
148		strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
149	else
150		snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
151		    "vendor 0x%04x", PCI_VENDOR(ca->ca_id));
152
153	r = ehci_init(&sc->sc);
154	if (r != USBD_NORMAL_COMPLETION) {
155		printf("%s: init failed, error=%d\n", devname, r);
156
157		/* Avoid spurious interrupts. */
158		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
159		sc->sc_ih = NULL;
160
161		return;
162	}
163
164	/* Attach usb device. */
165	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
166				       usbctlprint);
167}
168
169int
170ehci_cardbus_detach(struct device *self, int flags)
171{
172	struct ehci_cardbus_softc *sc = (struct ehci_cardbus_softc *)self;
173	struct cardbus_devfunc *ct = sc->sc_ct;
174	int rv;
175
176	rv = ehci_detach(&sc->sc, flags);
177	if (rv)
178		return (rv);
179	if (sc->sc_ih != NULL) {
180		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
181		sc->sc_ih = NULL;
182	}
183	if (sc->sc.sc_size) {
184		Cardbus_mapreg_unmap(ct, CARDBUS_CBMEM, sc->sc.iot,
185		    sc->sc.ioh, sc->sc.sc_size);
186		sc->sc.sc_size = 0;
187	}
188	return (0);
189}
190