Home | History | Annotate | Line # | Download | only in cardbus
ehci_cardbus.c revision 1.3
      1 /*	$NetBSD: ehci_cardbus.c,v 1.3 2001/11/13 12:51:12 lukem 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 (at) 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: ehci_cardbus.c,v 1.3 2001/11/13 12:51:12 lukem Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/device.h>
     47 #include <sys/proc.h>
     48 
     49 #include <machine/bus.h>
     50 
     51 #if defined pciinc
     52 #include <dev/pci/pcidevs.h>
     53 #endif
     54 
     55 #include <dev/cardbus/cardbusvar.h>
     56 #include <dev/cardbus/cardbusdevs.h>
     57 
     58 #include <dev/cardbus/usb_cardbus.h>
     59 
     60 #include <dev/usb/usb.h>
     61 #include <dev/usb/usbdi.h>
     62 #include <dev/usb/usbdivar.h>
     63 #include <dev/usb/usb_mem.h>
     64 
     65 #include <dev/usb/ehcireg.h>
     66 #include <dev/usb/ehcivar.h>
     67 
     68 #ifdef EHCI_DEBUG
     69 #define DPRINTF(x)	if (ehcidebug) printf x
     70 extern int ehcidebug;
     71 #else
     72 #define DPRINTF(x)
     73 #endif
     74 
     75 int	ehci_cardbus_match(struct device *, struct cfdata *, void *);
     76 void	ehci_cardbus_attach(struct device *, struct device *, void *);
     77 int	ehci_cardbus_detach(device_ptr_t, int);
     78 
     79 struct ehci_cardbus_softc {
     80 	ehci_softc_t		sc;
     81 	cardbus_chipset_tag_t	sc_cc;
     82 	cardbus_function_tag_t	sc_cf;
     83 	cardbus_devfunc_t	sc_ct;
     84 	void 			*sc_ih;		/* interrupt vectoring */
     85 };
     86 
     87 struct cfattach ehci_cardbus_ca = {
     88 	sizeof(struct ehci_cardbus_softc), ehci_cardbus_match,
     89 	ehci_cardbus_attach, ehci_cardbus_detach, ehci_activate
     90 };
     91 
     92 #define CARDBUS_INTERFACE_EHCI PCI_INTERFACE_EHCI
     93 #define CARDBUS_CBMEM PCI_CBMEM
     94 #define cardbus_findvendor pci_findvendor
     95 #define cardbus_devinfo pci_devinfo
     96 
     97 static TAILQ_HEAD(, usb_cardbus) ehci_cardbus_alldevs =
     98 	TAILQ_HEAD_INITIALIZER(ehci_cardbus_alldevs);
     99 
    100 int
    101 ehci_cardbus_match(struct device *parent, struct cfdata *match, void *aux)
    102 {
    103 	struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
    104 
    105 	if (CARDBUS_CLASS(ca->ca_class) == CARDBUS_CLASS_SERIALBUS &&
    106 	    CARDBUS_SUBCLASS(ca->ca_class) == CARDBUS_SUBCLASS_SERIALBUS_USB &&
    107 	    CARDBUS_INTERFACE(ca->ca_class) == CARDBUS_INTERFACE_EHCI)
    108 		return (1);
    109 
    110 	return (0);
    111 }
    112 
    113 void
    114 ehci_cardbus_attach(struct device *parent, struct device *self, void *aux)
    115 {
    116 	struct ehci_cardbus_softc *sc = (struct ehci_cardbus_softc *)self;
    117 	struct cardbus_attach_args *ca = aux;
    118 	cardbus_devfunc_t ct = ca->ca_ct;
    119 	cardbus_chipset_tag_t cc = ct->ct_cc;
    120 	cardbus_function_tag_t cf = ct->ct_cf;
    121 	cardbusreg_t csr;
    122 	char devinfo[256];
    123 	usbd_status r;
    124 	char *vendor;
    125 	u_int ncomp;
    126 	const char *devname = sc->sc.sc_bus.bdev.dv_xname;
    127 	struct usb_cardbus *up;
    128 
    129 	cardbus_devinfo(ca->ca_id, ca->ca_class, 0, devinfo);
    130 	printf(": %s (rev. 0x%02x)\n", devinfo,
    131 	       CARDBUS_REVISION(ca->ca_class));
    132 
    133 	/* Map I/O registers */
    134 	if (Cardbus_mapreg_map(ct, CARDBUS_CBMEM, CARDBUS_MAPREG_TYPE_MEM, 0,
    135 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
    136 		printf("%s: can't map mem space\n", devname);
    137 		return;
    138 	}
    139 
    140 	sc->sc_cc = cc;
    141 	sc->sc_cf = cf;
    142 	sc->sc_ct = ct;
    143 	sc->sc.sc_bus.dmatag = ca->ca_dmat;
    144 
    145 #if rbus
    146 #else
    147 XXX	(ct->ct_cf->cardbus_mem_open)(cc, 0, iob, iob + 0x40);
    148 #endif
    149 	(ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
    150 	(ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    151 
    152 	/* Enable the device. */
    153 	csr = cardbus_conf_read(cc, cf, ca->ca_tag,
    154 				CARDBUS_COMMAND_STATUS_REG);
    155 	cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG,
    156 		       csr | CARDBUS_COMMAND_MASTER_ENABLE
    157 			   | CARDBUS_COMMAND_MEM_ENABLE);
    158 
    159 	/* Disable interrupts, so we don't get any spurious ones. */
    160 	sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
    161 	DPRINTF(("%s: offs=%d\n", devname, sc->sc.sc_offs));
    162 	EOWRITE2(&sc->sc, EHCI_USBINTR, 0);
    163 
    164 	sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline,
    165 					   IPL_USB, ehci_intr, sc);
    166 	if (sc->sc_ih == NULL) {
    167 		printf("%s: couldn't establish interrupt\n", devname);
    168 		return;
    169 	}
    170 	printf("%s: interrupting at %d\n", devname, ca->ca_intrline);
    171 
    172 	/* Figure out vendor for root hub descriptor. */
    173 	vendor = cardbus_findvendor(ca->ca_id);
    174 	sc->sc.sc_id_vendor = CARDBUS_VENDOR(ca->ca_id);
    175 	if (vendor)
    176 		strncpy(sc->sc.sc_vendor, vendor,
    177 			sizeof(sc->sc.sc_vendor) - 1);
    178 	else
    179 		sprintf(sc->sc.sc_vendor, "vendor 0x%04x",
    180 			CARDBUS_VENDOR(ca->ca_id));
    181 
    182 	/*
    183 	 * Find companion controllers.  According to the spec they always
    184 	 * have lower function numbers so they should be enumerated already.
    185 	 */
    186 	ncomp = 0;
    187 	TAILQ_FOREACH(up, &ehci_cardbus_alldevs, next) {
    188 		if (up->bus == ca->ca_bus && up->device == ca->ca_device) {
    189 			DPRINTF(("ehci_cardbus_attach: companion %s\n",
    190 				 USBDEVNAME(up->usb->bdev)));
    191 			sc->sc.sc_comps[ncomp++] = up->usb;
    192 			if (ncomp >= EHCI_COMPANION_MAX)
    193 				break;
    194 		}
    195 	}
    196 	sc->sc.sc_ncomp = ncomp;
    197 
    198 	r = ehci_init(&sc->sc);
    199 	if (r != USBD_NORMAL_COMPLETION) {
    200 		printf("%s: init failed, error=%d\n", devname, r);
    201 
    202 		/* Avoid spurious interrupts. */
    203 		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
    204 		sc->sc_ih = NULL;
    205 
    206 		return;
    207 	}
    208 
    209 	/* Attach usb device. */
    210 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
    211 				       usbctlprint);
    212 }
    213 
    214 int
    215 ehci_cardbus_detach(device_ptr_t self, int flags)
    216 {
    217 	struct ehci_cardbus_softc *sc = (struct ehci_cardbus_softc *)self;
    218 	struct cardbus_devfunc *ct = sc->sc_ct;
    219 	int rv;
    220 
    221 	rv = ehci_detach(&sc->sc, flags);
    222 	if (rv)
    223 		return (rv);
    224 	if (sc->sc_ih != NULL) {
    225 		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
    226 		sc->sc_ih = NULL;
    227 	}
    228 	if (sc->sc.sc_size) {
    229 		Cardbus_mapreg_unmap(ct, CARDBUS_CBMEM, sc->sc.iot,
    230 		    sc->sc.ioh, sc->sc.sc_size);
    231 		sc->sc.sc_size = 0;
    232 	}
    233 	return (0);
    234 }
    235 
    236 void
    237 usb_cardbus_add(struct usb_cardbus *up, struct cardbus_attach_args *ca, struct usbd_bus *bu)
    238 {
    239 	TAILQ_INSERT_TAIL(&ehci_cardbus_alldevs, up, next);
    240 	up->bus = ca->ca_bus;
    241 	up->device = ca->ca_device;
    242 	up->function = ca->ca_function;
    243 	up->usb = bu;
    244 }
    245 
    246 void
    247 usb_cardbus_rem(struct usb_cardbus *up)
    248 {
    249 	TAILQ_REMOVE(&ehci_cardbus_alldevs, up, next);
    250 }
    251