Home | History | Annotate | Line # | Download | only in cardbus
ehci_cardbus.c revision 1.26
      1 /*	$NetBSD: ehci_cardbus.c,v 1.26 2010/02/25 23:40:39 dyoung 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  *
     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 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: ehci_cardbus.c,v 1.26 2010/02/25 23:40:39 dyoung Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/device.h>
     40 #include <sys/proc.h>
     41 
     42 #include <sys/bus.h>
     43 
     44 #if defined pciinc
     45 #include <dev/pci/pcidevs.h>
     46 #endif
     47 
     48 #include <dev/cardbus/cardbusvar.h>
     49 #include <dev/pci/pcidevs.h>
     50 
     51 #include <dev/cardbus/usb_cardbus.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
     63 extern int ehcidebug;
     64 #else
     65 #define DPRINTF(x)
     66 #endif
     67 
     68 int	ehci_cardbus_match(device_t, cfdata_t, void *);
     69 void	ehci_cardbus_attach(device_t, device_t, void *);
     70 int	ehci_cardbus_detach(device_t, int);
     71 
     72 struct ehci_cardbus_softc {
     73 	ehci_softc_t		sc;
     74 	cardbus_chipset_tag_t	sc_cc;
     75 	cardbus_function_tag_t	sc_cf;
     76 	cardbus_devfunc_t	sc_ct;
     77 	void 			*sc_ih;		/* interrupt vectoring */
     78 };
     79 
     80 CFATTACH_DECL_NEW(ehci_cardbus, sizeof(struct ehci_cardbus_softc),
     81     ehci_cardbus_match, ehci_cardbus_attach, ehci_cardbus_detach, ehci_activate);
     82 
     83 #define CARDBUS_INTERFACE_EHCI PCI_INTERFACE_EHCI
     84 #define CARDBUS_CBMEM PCI_CBMEM
     85 #define cardbus_findvendor pci_findvendor
     86 #define cardbus_devinfo pci_devinfo
     87 
     88 static TAILQ_HEAD(, usb_cardbus) ehci_cardbus_alldevs =
     89 	TAILQ_HEAD_INITIALIZER(ehci_cardbus_alldevs);
     90 
     91 int
     92 ehci_cardbus_match(device_t parent, cfdata_t 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 
    104 static bool
    105 ehci_cardbus_suspend(device_t dv, const pmf_qual_t *qual)
    106 {
    107 	ehci_suspend(dv, qual);
    108 #if 0
    109 	struct ehci_cardbus_softc *sc = device_private(dv);
    110 	ehci_release_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
    111 #endif
    112 
    113 	return true;
    114 }
    115 
    116 static bool
    117 ehci_cardbus_resume(device_t dv, const pmf_qual_t *qual)
    118 {
    119 #if 0
    120 	struct ehci_cardbus_softc *sc = device_private(dv);
    121 	ehci_get_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
    122 #endif
    123 	return ehci_resume(dv, qual);
    124 }
    125 
    126 void
    127 ehci_cardbus_attach(device_t parent, device_t self, void *aux)
    128 {
    129 	struct ehci_cardbus_softc *sc = device_private(self);
    130 	struct cardbus_attach_args *ca = aux;
    131 	cardbus_devfunc_t ct = ca->ca_ct;
    132 	cardbus_chipset_tag_t cc = ct->ct_cc;
    133 	cardbus_function_tag_t cf = ct->ct_cf;
    134 	pcireg_t csr;
    135 	char devinfo[256];
    136 	usbd_status r;
    137 	const char *vendor;
    138 	u_int ncomp;
    139 	const char *devname = device_xname(self);
    140 	struct usb_cardbus *up;
    141 
    142 	sc->sc.sc_dev = self;
    143 	sc->sc.sc_bus.hci_private = sc;
    144 
    145 	cardbus_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
    146 	printf(": %s (rev. 0x%02x)\n", devinfo,
    147 	       CARDBUS_REVISION(ca->ca_class));
    148 
    149 	/* Map I/O registers */
    150 	if (Cardbus_mapreg_map(ct, CARDBUS_CBMEM, CARDBUS_MAPREG_TYPE_MEM, 0,
    151 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
    152 		printf("%s: can't map mem space\n", devname);
    153 		return;
    154 	}
    155 
    156 	sc->sc_cc = cc;
    157 	sc->sc_cf = cf;
    158 	sc->sc_ct = ct;
    159 	sc->sc.sc_bus.dmatag = ca->ca_dmat;
    160 
    161 #if rbus
    162 #else
    163 XXX	(ct->ct_cf->cardbus_mem_open)(cc, 0, iob, iob + 0x40);
    164 #endif
    165 
    166 	/* Enable the device. */
    167 	csr = cardbus_conf_read(cc, cf, ca->ca_tag,
    168 				CARDBUS_COMMAND_STATUS_REG);
    169 	cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG,
    170 		       csr | CARDBUS_COMMAND_MASTER_ENABLE
    171 			   | CARDBUS_COMMAND_MEM_ENABLE);
    172 
    173 	/* Disable interrupts, so we don't get any spurious ones. */
    174 	sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
    175 	DPRINTF(("%s: offs=%d\n", devname, sc->sc.sc_offs));
    176 	EOWRITE2(&sc->sc, EHCI_USBINTR, 0);
    177 
    178 	sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline,
    179 					   IPL_USB, ehci_intr, sc);
    180 	if (sc->sc_ih == NULL) {
    181 		printf("%s: couldn't establish interrupt\n", devname);
    182 		return;
    183 	}
    184 
    185 	/* Figure out vendor for root hub descriptor. */
    186 	vendor = cardbus_findvendor(ca->ca_id);
    187 	sc->sc.sc_id_vendor = CARDBUS_VENDOR(ca->ca_id);
    188 	if (vendor)
    189 		strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
    190 	else
    191 		snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
    192 		    "vendor 0x%04x", CARDBUS_VENDOR(ca->ca_id));
    193 
    194 	/*
    195 	 * Find companion controllers.  According to the spec they always
    196 	 * have lower function numbers so they should be enumerated already.
    197 	 */
    198 	ncomp = 0;
    199 	TAILQ_FOREACH(up, &ehci_cardbus_alldevs, next) {
    200 		if (up->bus == ca->ca_bus) {
    201 			DPRINTF(("ehci_cardbus_attach: companion %s\n",
    202 				 device_xname(up->usb)));
    203 			sc->sc.sc_comps[ncomp++] = up->usb;
    204 			if (ncomp >= EHCI_COMPANION_MAX)
    205 				break;
    206 		}
    207 	}
    208 	sc->sc.sc_ncomp = ncomp;
    209 
    210 	r = ehci_init(&sc->sc);
    211 	if (r != USBD_NORMAL_COMPLETION) {
    212 		printf("%s: init failed, error=%d\n", devname, r);
    213 
    214 		/* Avoid spurious interrupts. */
    215 		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
    216 		sc->sc_ih = NULL;
    217 
    218 		return;
    219 	}
    220 
    221 	if (!pmf_device_register1(self, ehci_cardbus_suspend,
    222 	                          ehci_cardbus_resume, ehci_shutdown))
    223 		aprint_error_dev(self, "couldn't establish power handler\n");
    224 
    225 	/* Attach usb device. */
    226 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
    227 }
    228 
    229 int
    230 ehci_cardbus_detach(device_t self, int flags)
    231 {
    232 	struct ehci_cardbus_softc *sc = device_private(self);
    233 	struct cardbus_devfunc *ct = sc->sc_ct;
    234 	int rv;
    235 
    236 	rv = ehci_detach(&sc->sc, flags);
    237 	if (rv)
    238 		return (rv);
    239 	if (sc->sc_ih != NULL) {
    240 		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
    241 		sc->sc_ih = NULL;
    242 	}
    243 	if (sc->sc.sc_size) {
    244 		Cardbus_mapreg_unmap(ct, CARDBUS_CBMEM, sc->sc.iot,
    245 		    sc->sc.ioh, sc->sc.sc_size);
    246 		sc->sc.sc_size = 0;
    247 	}
    248 	return (0);
    249 }
    250 
    251 void
    252 usb_cardbus_add(struct usb_cardbus *up, struct cardbus_attach_args *ca, device_t bu)
    253 {
    254 	TAILQ_INSERT_TAIL(&ehci_cardbus_alldevs, up, next);
    255 	up->bus = ca->ca_bus;
    256 	up->function = ca->ca_function;
    257 	up->usb = bu;
    258 }
    259 
    260 void
    261 usb_cardbus_rem(struct usb_cardbus *up)
    262 {
    263 	TAILQ_REMOVE(&ehci_cardbus_alldevs, up, next);
    264 }
    265