Home | History | Annotate | Line # | Download | only in pci
ehci_pci.c revision 1.3.4.2
      1  1.3.4.2   nathanw /*	$NetBSD: ehci_pci.c,v 1.3.4.2 2002/01/11 23:39:17 nathanw Exp $	*/
      2      1.1  augustss 
      3      1.1  augustss /*
      4      1.1  augustss  * Copyright (c) 2000 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).
      9      1.1  augustss  *
     10      1.1  augustss  * Redistribution and use in source and binary forms, with or without
     11      1.1  augustss  * modification, are permitted provided that the following conditions
     12      1.1  augustss  * are met:
     13      1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     14      1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     15      1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  augustss  *    documentation and/or other materials provided with the distribution.
     18      1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     19      1.1  augustss  *    must display the following acknowledgement:
     20      1.1  augustss  *        This product includes software developed by the NetBSD
     21      1.1  augustss  *        Foundation, Inc. and its contributors.
     22      1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.1  augustss  *    contributors may be used to endorse or promote products derived
     24      1.1  augustss  *    from this software without specific prior written permission.
     25      1.1  augustss  *
     26      1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     37      1.1  augustss  */
     38      1.1  augustss 
     39  1.3.4.1   nathanw #include <sys/cdefs.h>
     40  1.3.4.2   nathanw __KERNEL_RCSID(0, "$NetBSD: ehci_pci.c,v 1.3.4.2 2002/01/11 23:39:17 nathanw Exp $");
     41  1.3.4.1   nathanw 
     42      1.1  augustss #include <sys/param.h>
     43      1.1  augustss #include <sys/systm.h>
     44      1.1  augustss #include <sys/kernel.h>
     45      1.1  augustss #include <sys/device.h>
     46      1.1  augustss #include <sys/proc.h>
     47      1.1  augustss #include <sys/queue.h>
     48      1.1  augustss 
     49      1.1  augustss #include <machine/bus.h>
     50      1.1  augustss 
     51      1.1  augustss #include <dev/pci/pcivar.h>
     52  1.3.4.1   nathanw #include <dev/pci/usb_pci.h>
     53      1.1  augustss 
     54      1.1  augustss #include <dev/usb/usb.h>
     55      1.1  augustss #include <dev/usb/usbdi.h>
     56      1.1  augustss #include <dev/usb/usbdivar.h>
     57      1.1  augustss #include <dev/usb/usb_mem.h>
     58      1.1  augustss 
     59      1.1  augustss #include <dev/usb/ehcireg.h>
     60      1.1  augustss #include <dev/usb/ehcivar.h>
     61      1.1  augustss 
     62  1.3.4.1   nathanw #ifdef EHCI_DEBUG
     63  1.3.4.1   nathanw #define DPRINTF(x)	if (ehcidebug) printf x
     64  1.3.4.1   nathanw extern int ehcidebug;
     65  1.3.4.1   nathanw #else
     66  1.3.4.1   nathanw #define DPRINTF(x)
     67  1.3.4.1   nathanw #endif
     68  1.3.4.1   nathanw 
     69      1.1  augustss int	ehci_pci_match(struct device *, struct cfdata *, void *);
     70      1.1  augustss void	ehci_pci_attach(struct device *, struct device *, void *);
     71      1.1  augustss int	ehci_pci_detach(device_ptr_t, int);
     72      1.1  augustss 
     73      1.1  augustss struct ehci_pci_softc {
     74      1.1  augustss 	ehci_softc_t		sc;
     75      1.1  augustss 	pci_chipset_tag_t	sc_pc;
     76      1.1  augustss 	pcitag_t		sc_tag;
     77      1.1  augustss 	void 			*sc_ih;		/* interrupt vectoring */
     78      1.1  augustss };
     79      1.1  augustss 
     80      1.1  augustss struct cfattach ehci_pci_ca = {
     81      1.1  augustss 	sizeof(struct ehci_pci_softc), ehci_pci_match, ehci_pci_attach,
     82      1.1  augustss 	ehci_pci_detach, ehci_activate
     83      1.1  augustss };
     84      1.1  augustss 
     85      1.1  augustss int
     86      1.1  augustss ehci_pci_match(struct device *parent, struct cfdata *match, void *aux)
     87      1.1  augustss {
     88      1.1  augustss 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
     89      1.1  augustss 
     90      1.1  augustss 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
     91      1.1  augustss 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
     92      1.1  augustss 	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_EHCI)
     93      1.1  augustss 		return (1);
     94      1.1  augustss 
     95      1.1  augustss 	return (0);
     96      1.1  augustss }
     97      1.1  augustss 
     98      1.1  augustss void
     99      1.1  augustss ehci_pci_attach(struct device *parent, struct device *self, void *aux)
    100      1.1  augustss {
    101      1.1  augustss 	struct ehci_pci_softc *sc = (struct ehci_pci_softc *)self;
    102      1.1  augustss 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    103      1.1  augustss 	pci_chipset_tag_t pc = pa->pa_pc;
    104      1.1  augustss 	pcitag_t tag = pa->pa_tag;
    105      1.1  augustss 	char const *intrstr;
    106      1.1  augustss 	pci_intr_handle_t ih;
    107      1.1  augustss 	pcireg_t csr;
    108      1.1  augustss 	char *vendor;
    109      1.1  augustss 	char *devname = sc->sc.sc_bus.bdev.dv_xname;
    110      1.1  augustss 	char devinfo[256];
    111      1.1  augustss 	usbd_status r;
    112  1.3.4.1   nathanw 	int ncomp;
    113  1.3.4.1   nathanw 	struct usb_pci *up;
    114      1.1  augustss 
    115      1.1  augustss 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    116      1.1  augustss 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
    117      1.1  augustss 
    118      1.1  augustss 	/* Map I/O registers */
    119      1.1  augustss 	if (pci_mapreg_map(pa, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
    120      1.1  augustss 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
    121      1.1  augustss 		printf("%s: can't map i/o space\n", devname);
    122      1.1  augustss 		return;
    123      1.1  augustss 	}
    124      1.1  augustss 
    125      1.1  augustss 	sc->sc_pc = pc;
    126      1.1  augustss 	sc->sc_tag = tag;
    127      1.1  augustss 	sc->sc.sc_bus.dmatag = pa->pa_dmat;
    128      1.1  augustss 
    129      1.1  augustss 	/* Enable the device. */
    130      1.1  augustss 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    131      1.1  augustss 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
    132      1.1  augustss 		       csr | PCI_COMMAND_MASTER_ENABLE);
    133      1.1  augustss 
    134  1.3.4.1   nathanw 	/* Disable interrupts, so we don't get any spurious ones. */
    135  1.3.4.1   nathanw 	sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
    136  1.3.4.1   nathanw 	DPRINTF(("%s: offs=%d\n", devname, sc->sc.sc_offs));
    137  1.3.4.1   nathanw 	EOWRITE2(&sc->sc, EHCI_USBINTR, 0);
    138  1.3.4.1   nathanw 
    139      1.1  augustss 	/* Map and establish the interrupt. */
    140      1.3  sommerfe 	if (pci_intr_map(pa, &ih)) {
    141      1.1  augustss 		printf("%s: couldn't map interrupt\n", devname);
    142      1.1  augustss 		return;
    143      1.1  augustss 	}
    144      1.1  augustss 	intrstr = pci_intr_string(pc, ih);
    145      1.1  augustss 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ehci_intr, sc);
    146      1.1  augustss 	if (sc->sc_ih == NULL) {
    147      1.1  augustss 		printf("%s: couldn't establish interrupt", devname);
    148      1.1  augustss 		if (intrstr != NULL)
    149      1.1  augustss 			printf(" at %s", intrstr);
    150      1.1  augustss 		printf("\n");
    151      1.1  augustss 		return;
    152      1.1  augustss 	}
    153      1.1  augustss 	printf("%s: interrupting at %s\n", devname, intrstr);
    154      1.1  augustss 
    155      1.1  augustss 	switch(pci_conf_read(pc, tag, PCI_USBREV) & PCI_USBREV_MASK) {
    156      1.1  augustss 	case PCI_USBREV_PRE_1_0:
    157      1.1  augustss 	case PCI_USBREV_1_0:
    158      1.1  augustss 	case PCI_USBREV_1_1:
    159  1.3.4.1   nathanw 		sc->sc.sc_bus.usbrev = USBREV_UNKNOWN;
    160  1.3.4.1   nathanw 		printf("%s: pre-2.0 USB rev\n", devname);
    161  1.3.4.1   nathanw 		return;
    162      1.1  augustss 	case PCI_USBREV_2_0:
    163      1.1  augustss 		sc->sc.sc_bus.usbrev = USBREV_2_0;
    164      1.1  augustss 		break;
    165      1.1  augustss 	default:
    166      1.1  augustss 		sc->sc.sc_bus.usbrev = USBREV_UNKNOWN;
    167      1.1  augustss 		break;
    168      1.1  augustss 	}
    169      1.1  augustss 
    170      1.1  augustss 	/* Figure out vendor for root hub descriptor. */
    171      1.1  augustss 	vendor = pci_findvendor(pa->pa_id);
    172      1.1  augustss 	sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
    173      1.1  augustss 	if (vendor)
    174      1.1  augustss 		strncpy(sc->sc.sc_vendor, vendor,
    175      1.1  augustss 			sizeof(sc->sc.sc_vendor) - 1);
    176      1.1  augustss 	else
    177      1.1  augustss 		sprintf(sc->sc.sc_vendor, "vendor 0x%04x",
    178      1.1  augustss 			PCI_VENDOR(pa->pa_id));
    179      1.1  augustss 
    180  1.3.4.1   nathanw 	/*
    181  1.3.4.1   nathanw 	 * Find companion controllers.  According to the spec they always
    182  1.3.4.1   nathanw 	 * have lower function numbers so they should be enumerated already.
    183  1.3.4.1   nathanw 	 */
    184  1.3.4.1   nathanw 	ncomp = 0;
    185  1.3.4.1   nathanw 	TAILQ_FOREACH(up, &ehci_pci_alldevs, next) {
    186  1.3.4.1   nathanw 		if (up->bus == pa->pa_bus && up->device == pa->pa_device) {
    187  1.3.4.1   nathanw 			DPRINTF(("ehci_pci_attach: companion %s\n",
    188  1.3.4.1   nathanw 				 USBDEVNAME(up->usb->bdev)));
    189  1.3.4.1   nathanw 			sc->sc.sc_comps[ncomp++] = up->usb;
    190  1.3.4.1   nathanw 			if (ncomp >= EHCI_COMPANION_MAX)
    191  1.3.4.1   nathanw 				break;
    192  1.3.4.1   nathanw 		}
    193  1.3.4.1   nathanw 	}
    194  1.3.4.1   nathanw 	sc->sc.sc_ncomp = ncomp;
    195  1.3.4.1   nathanw 
    196      1.1  augustss 	r = ehci_init(&sc->sc);
    197      1.1  augustss 	if (r != USBD_NORMAL_COMPLETION) {
    198      1.1  augustss 		printf("%s: init failed, error=%d\n", devname, r);
    199      1.1  augustss 		return;
    200      1.1  augustss 	}
    201      1.1  augustss 
    202      1.1  augustss 	/* Attach usb device. */
    203      1.1  augustss 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
    204      1.1  augustss 				       usbctlprint);
    205      1.1  augustss }
    206      1.1  augustss 
    207      1.1  augustss int
    208      1.1  augustss ehci_pci_detach(device_ptr_t self, int flags)
    209      1.1  augustss {
    210      1.1  augustss 	struct ehci_pci_softc *sc = (struct ehci_pci_softc *)self;
    211      1.1  augustss 	int rv;
    212      1.1  augustss 
    213      1.1  augustss 	rv = ehci_detach(&sc->sc, flags);
    214      1.1  augustss 	if (rv)
    215      1.1  augustss 		return (rv);
    216      1.1  augustss 	if (sc->sc_ih != NULL) {
    217      1.1  augustss 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    218      1.1  augustss 		sc->sc_ih = NULL;
    219      1.1  augustss 	}
    220      1.1  augustss 	if (sc->sc.sc_size) {
    221      1.1  augustss 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
    222      1.1  augustss 		sc->sc.sc_size = 0;
    223      1.1  augustss 	}
    224      1.1  augustss 	return (0);
    225      1.1  augustss }
    226