Home | History | Annotate | Line # | Download | only in cardbus
ehci_cardbus.c revision 1.27
      1  1.27    dyoung /*	$NetBSD: ehci_cardbus.c,v 1.27 2010/02/26 00:57:01 dyoung Exp $	*/
      2   1.1  augustss 
      3   1.1  augustss /*
      4   1.1  augustss  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5   1.1  augustss  * All rights reserved.
      6   1.1  augustss  *
      7   1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9   1.1  augustss  * Carlstedt Research & Technology.
     10   1.1  augustss  *
     11   1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12   1.1  augustss  * modification, are permitted provided that the following conditions
     13   1.1  augustss  * are met:
     14   1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15   1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16   1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18   1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19   1.1  augustss  *
     20   1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1  augustss  */
     32   1.3     lukem 
     33   1.3     lukem #include <sys/cdefs.h>
     34  1.27    dyoung __KERNEL_RCSID(0, "$NetBSD: ehci_cardbus.c,v 1.27 2010/02/26 00:57:01 dyoung Exp $");
     35   1.1  augustss 
     36   1.1  augustss #include <sys/param.h>
     37   1.1  augustss #include <sys/systm.h>
     38   1.1  augustss #include <sys/kernel.h>
     39   1.1  augustss #include <sys/device.h>
     40   1.1  augustss #include <sys/proc.h>
     41   1.1  augustss 
     42  1.17        ad #include <sys/bus.h>
     43   1.1  augustss 
     44   1.1  augustss #if defined pciinc
     45   1.1  augustss #include <dev/pci/pcidevs.h>
     46   1.1  augustss #endif
     47   1.1  augustss 
     48   1.1  augustss #include <dev/cardbus/cardbusvar.h>
     49  1.10   mycroft #include <dev/pci/pcidevs.h>
     50   1.1  augustss 
     51   1.1  augustss #include <dev/cardbus/usb_cardbus.h>
     52   1.1  augustss 
     53   1.1  augustss #include <dev/usb/usb.h>
     54   1.1  augustss #include <dev/usb/usbdi.h>
     55   1.1  augustss #include <dev/usb/usbdivar.h>
     56   1.1  augustss #include <dev/usb/usb_mem.h>
     57   1.1  augustss 
     58   1.1  augustss #include <dev/usb/ehcireg.h>
     59   1.1  augustss #include <dev/usb/ehcivar.h>
     60   1.1  augustss 
     61   1.2  augustss #ifdef EHCI_DEBUG
     62   1.2  augustss #define DPRINTF(x)	if (ehcidebug) printf x
     63   1.2  augustss extern int ehcidebug;
     64   1.2  augustss #else
     65   1.2  augustss #define DPRINTF(x)
     66   1.2  augustss #endif
     67   1.2  augustss 
     68  1.23    cegger int	ehci_cardbus_match(device_t, cfdata_t, void *);
     69  1.18    dyoung void	ehci_cardbus_attach(device_t, device_t, void *);
     70  1.18    dyoung int	ehci_cardbus_detach(device_t, int);
     71   1.1  augustss 
     72   1.1  augustss struct ehci_cardbus_softc {
     73   1.1  augustss 	ehci_softc_t		sc;
     74   1.1  augustss 	cardbus_chipset_tag_t	sc_cc;
     75   1.1  augustss 	cardbus_function_tag_t	sc_cf;
     76   1.1  augustss 	cardbus_devfunc_t	sc_ct;
     77   1.1  augustss 	void 			*sc_ih;		/* interrupt vectoring */
     78   1.1  augustss };
     79   1.1  augustss 
     80  1.19  drochner CFATTACH_DECL_NEW(ehci_cardbus, sizeof(struct ehci_cardbus_softc),
     81   1.6   thorpej     ehci_cardbus_match, ehci_cardbus_attach, ehci_cardbus_detach, ehci_activate);
     82   1.1  augustss 
     83   1.1  augustss static TAILQ_HEAD(, usb_cardbus) ehci_cardbus_alldevs =
     84   1.1  augustss 	TAILQ_HEAD_INITIALIZER(ehci_cardbus_alldevs);
     85   1.1  augustss 
     86   1.1  augustss int
     87  1.23    cegger ehci_cardbus_match(device_t parent, cfdata_t match, void *aux)
     88   1.1  augustss {
     89   1.1  augustss 	struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
     90   1.1  augustss 
     91  1.27    dyoung 	if (PCI_CLASS(ca->ca_class) == PCI_CLASS_SERIALBUS &&
     92  1.27    dyoung 	    PCI_SUBCLASS(ca->ca_class) == PCI_SUBCLASS_SERIALBUS_USB &&
     93  1.27    dyoung 	    PCI_INTERFACE(ca->ca_class) == PCI_INTERFACE_EHCI)
     94   1.1  augustss 		return (1);
     95  1.11     perry 
     96   1.1  augustss 	return (0);
     97   1.1  augustss }
     98   1.1  augustss 
     99  1.18    dyoung static bool
    100  1.25    dyoung ehci_cardbus_suspend(device_t dv, const pmf_qual_t *qual)
    101  1.18    dyoung {
    102  1.24    dyoung 	ehci_suspend(dv, qual);
    103  1.18    dyoung #if 0
    104  1.18    dyoung 	struct ehci_cardbus_softc *sc = device_private(dv);
    105  1.18    dyoung 	ehci_release_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
    106  1.18    dyoung #endif
    107  1.18    dyoung 
    108  1.18    dyoung 	return true;
    109  1.18    dyoung }
    110  1.18    dyoung 
    111  1.18    dyoung static bool
    112  1.25    dyoung ehci_cardbus_resume(device_t dv, const pmf_qual_t *qual)
    113  1.18    dyoung {
    114  1.18    dyoung #if 0
    115  1.18    dyoung 	struct ehci_cardbus_softc *sc = device_private(dv);
    116  1.18    dyoung 	ehci_get_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
    117  1.18    dyoung #endif
    118  1.24    dyoung 	return ehci_resume(dv, qual);
    119  1.18    dyoung }
    120  1.18    dyoung 
    121   1.1  augustss void
    122  1.18    dyoung ehci_cardbus_attach(device_t parent, device_t self, void *aux)
    123   1.1  augustss {
    124  1.14   thorpej 	struct ehci_cardbus_softc *sc = device_private(self);
    125   1.1  augustss 	struct cardbus_attach_args *ca = aux;
    126   1.1  augustss 	cardbus_devfunc_t ct = ca->ca_ct;
    127   1.1  augustss 	cardbus_chipset_tag_t cc = ct->ct_cc;
    128   1.1  augustss 	cardbus_function_tag_t cf = ct->ct_cf;
    129  1.25    dyoung 	pcireg_t csr;
    130   1.1  augustss 	char devinfo[256];
    131   1.1  augustss 	usbd_status r;
    132   1.9   mycroft 	const char *vendor;
    133   1.2  augustss 	u_int ncomp;
    134  1.19  drochner 	const char *devname = device_xname(self);
    135   1.2  augustss 	struct usb_cardbus *up;
    136   1.1  augustss 
    137  1.19  drochner 	sc->sc.sc_dev = self;
    138  1.20       skd 	sc->sc.sc_bus.hci_private = sc;
    139  1.19  drochner 
    140  1.27    dyoung 	pci_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
    141   1.1  augustss 	printf(": %s (rev. 0x%02x)\n", devinfo,
    142  1.27    dyoung 	       PCI_REVISION(ca->ca_class));
    143   1.1  augustss 
    144   1.1  augustss 	/* Map I/O registers */
    145  1.27    dyoung 	if (Cardbus_mapreg_map(ct, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
    146   1.1  augustss 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
    147   1.1  augustss 		printf("%s: can't map mem space\n", devname);
    148   1.1  augustss 		return;
    149   1.1  augustss 	}
    150   1.1  augustss 
    151   1.1  augustss 	sc->sc_cc = cc;
    152   1.1  augustss 	sc->sc_cf = cf;
    153   1.1  augustss 	sc->sc_ct = ct;
    154   1.1  augustss 	sc->sc.sc_bus.dmatag = ca->ca_dmat;
    155   1.1  augustss 
    156   1.1  augustss #if rbus
    157   1.1  augustss #else
    158   1.1  augustss XXX	(ct->ct_cf->cardbus_mem_open)(cc, 0, iob, iob + 0x40);
    159   1.1  augustss #endif
    160   1.1  augustss 
    161   1.1  augustss 	/* Enable the device. */
    162  1.27    dyoung 	csr = Cardbus_conf_read(ct, ca->ca_tag, PCI_COMMAND_STATUS_REG);
    163  1.27    dyoung 	Cardbus_conf_write(ct, ca->ca_tag, PCI_COMMAND_STATUS_REG,
    164  1.27    dyoung 		       csr | PCI_COMMAND_MASTER_ENABLE
    165  1.27    dyoung 			   | PCI_COMMAND_MEM_ENABLE);
    166   1.1  augustss 
    167   1.2  augustss 	/* Disable interrupts, so we don't get any spurious ones. */
    168   1.2  augustss 	sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
    169   1.2  augustss 	DPRINTF(("%s: offs=%d\n", devname, sc->sc.sc_offs));
    170   1.2  augustss 	EOWRITE2(&sc->sc, EHCI_USBINTR, 0);
    171   1.2  augustss 
    172   1.1  augustss 	sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline,
    173   1.1  augustss 					   IPL_USB, ehci_intr, sc);
    174   1.1  augustss 	if (sc->sc_ih == NULL) {
    175   1.1  augustss 		printf("%s: couldn't establish interrupt\n", devname);
    176   1.1  augustss 		return;
    177   1.1  augustss 	}
    178   1.1  augustss 
    179   1.1  augustss 	/* Figure out vendor for root hub descriptor. */
    180  1.27    dyoung 	vendor = pci_findvendor(ca->ca_id);
    181  1.27    dyoung 	sc->sc.sc_id_vendor = PCI_VENDOR(ca->ca_id);
    182   1.1  augustss 	if (vendor)
    183   1.7    itojun 		strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
    184   1.1  augustss 	else
    185   1.7    itojun 		snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
    186  1.27    dyoung 		    "vendor 0x%04x", PCI_VENDOR(ca->ca_id));
    187  1.11     perry 
    188   1.2  augustss 	/*
    189   1.2  augustss 	 * Find companion controllers.  According to the spec they always
    190   1.2  augustss 	 * have lower function numbers so they should be enumerated already.
    191   1.2  augustss 	 */
    192   1.2  augustss 	ncomp = 0;
    193   1.2  augustss 	TAILQ_FOREACH(up, &ehci_cardbus_alldevs, next) {
    194  1.12  drochner 		if (up->bus == ca->ca_bus) {
    195   1.2  augustss 			DPRINTF(("ehci_cardbus_attach: companion %s\n",
    196  1.19  drochner 				 device_xname(up->usb)));
    197   1.2  augustss 			sc->sc.sc_comps[ncomp++] = up->usb;
    198   1.2  augustss 			if (ncomp >= EHCI_COMPANION_MAX)
    199   1.2  augustss 				break;
    200   1.2  augustss 		}
    201   1.2  augustss 	}
    202   1.2  augustss 	sc->sc.sc_ncomp = ncomp;
    203   1.2  augustss 
    204   1.1  augustss 	r = ehci_init(&sc->sc);
    205   1.1  augustss 	if (r != USBD_NORMAL_COMPLETION) {
    206   1.1  augustss 		printf("%s: init failed, error=%d\n", devname, r);
    207   1.1  augustss 
    208   1.1  augustss 		/* Avoid spurious interrupts. */
    209   1.1  augustss 		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
    210   1.2  augustss 		sc->sc_ih = NULL;
    211   1.1  augustss 
    212   1.1  augustss 		return;
    213   1.1  augustss 	}
    214   1.1  augustss 
    215  1.18    dyoung 	if (!pmf_device_register1(self, ehci_cardbus_suspend,
    216  1.18    dyoung 	                          ehci_cardbus_resume, ehci_shutdown))
    217  1.18    dyoung 		aprint_error_dev(self, "couldn't establish power handler\n");
    218  1.18    dyoung 
    219   1.1  augustss 	/* Attach usb device. */
    220  1.18    dyoung 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
    221   1.1  augustss }
    222   1.1  augustss 
    223   1.1  augustss int
    224  1.18    dyoung ehci_cardbus_detach(device_t self, int flags)
    225   1.1  augustss {
    226  1.14   thorpej 	struct ehci_cardbus_softc *sc = device_private(self);
    227   1.1  augustss 	struct cardbus_devfunc *ct = sc->sc_ct;
    228   1.1  augustss 	int rv;
    229   1.1  augustss 
    230   1.1  augustss 	rv = ehci_detach(&sc->sc, flags);
    231   1.1  augustss 	if (rv)
    232   1.1  augustss 		return (rv);
    233   1.1  augustss 	if (sc->sc_ih != NULL) {
    234   1.1  augustss 		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
    235   1.1  augustss 		sc->sc_ih = NULL;
    236   1.1  augustss 	}
    237   1.1  augustss 	if (sc->sc.sc_size) {
    238  1.27    dyoung 		Cardbus_mapreg_unmap(ct, PCI_CBMEM, sc->sc.iot,
    239   1.1  augustss 		    sc->sc.ioh, sc->sc.sc_size);
    240   1.1  augustss 		sc->sc.sc_size = 0;
    241   1.1  augustss 	}
    242   1.1  augustss 	return (0);
    243   1.1  augustss }
    244   1.1  augustss 
    245   1.1  augustss void
    246  1.19  drochner usb_cardbus_add(struct usb_cardbus *up, struct cardbus_attach_args *ca, device_t bu)
    247   1.1  augustss {
    248   1.1  augustss 	TAILQ_INSERT_TAIL(&ehci_cardbus_alldevs, up, next);
    249   1.1  augustss 	up->bus = ca->ca_bus;
    250   1.1  augustss 	up->function = ca->ca_function;
    251   1.1  augustss 	up->usb = bu;
    252   1.1  augustss }
    253   1.1  augustss 
    254   1.1  augustss void
    255   1.1  augustss usb_cardbus_rem(struct usb_cardbus *up)
    256   1.1  augustss {
    257   1.1  augustss 	TAILQ_REMOVE(&ehci_cardbus_alldevs, up, next);
    258   1.1  augustss }
    259