Home | History | Annotate | Line # | Download | only in pci
ehci_pci.c revision 1.29.2.5
      1 /*	$NetBSD: ehci_pci.c,v 1.29.2.5 2007/10/26 15:45:58 joerg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2002 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).
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: ehci_pci.c,v 1.29.2.5 2007/10/26 15:45:58 joerg Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/device.h>
     46 #include <sys/proc.h>
     47 #include <sys/queue.h>
     48 
     49 #include <sys/bus.h>
     50 
     51 #include <dev/pci/pcidevs.h>
     52 #include <dev/pci/pcivar.h>
     53 #include <dev/pci/usb_pci.h>
     54 
     55 #include <dev/usb/usb.h>
     56 #include <dev/usb/usbdi.h>
     57 #include <dev/usb/usbdivar.h>
     58 #include <dev/usb/usb_mem.h>
     59 
     60 #include <dev/usb/ehcireg.h>
     61 #include <dev/usb/ehcivar.h>
     62 
     63 #ifdef EHCI_DEBUG
     64 #define DPRINTF(x)	if (ehcidebug) printf x
     65 extern int ehcidebug;
     66 #else
     67 #define DPRINTF(x)
     68 #endif
     69 
     70 static void ehci_get_ownership(ehci_softc_t *sc, pci_chipset_tag_t pc,
     71 			       pcitag_t tag);
     72 static void ehci_pci_resume(device_t);
     73 
     74 struct ehci_pci_softc {
     75 	ehci_softc_t		sc;
     76 	pci_chipset_tag_t	sc_pc;
     77 	pcitag_t		sc_tag;
     78 	void 			*sc_ih;		/* interrupt vectoring */
     79 };
     80 
     81 #define EHCI_MAX_BIOS_WAIT		1000 /* ms */
     82 
     83 static int
     84 ehci_pci_match(struct device *parent, struct cfdata *match,
     85     void *aux)
     86 {
     87 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
     88 
     89 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
     90 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
     91 	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_EHCI)
     92 		return (1);
     93 
     94 	return (0);
     95 }
     96 
     97 static void
     98 ehci_pci_attach(struct device *parent, struct device *self, void *aux)
     99 {
    100 	struct ehci_pci_softc *sc = (struct ehci_pci_softc *)self;
    101 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    102 	pci_chipset_tag_t pc = pa->pa_pc;
    103 	pcitag_t tag = pa->pa_tag;
    104 	pnp_status_t status;
    105 	char const *intrstr;
    106 	pci_intr_handle_t ih;
    107 	pcireg_t csr;
    108 	const char *vendor;
    109 	const char *devname = sc->sc.sc_bus.bdev.dv_xname;
    110 	char devinfo[256];
    111 	usbd_status r;
    112 	int ncomp;
    113 	struct usb_pci *up;
    114 
    115 	aprint_naive(": USB controller\n");
    116 
    117 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    118 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
    119 	    PCI_REVISION(pa->pa_class));
    120 
    121 	/* Map I/O registers */
    122 	if (pci_mapreg_map(pa, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
    123 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
    124 		aprint_error("%s: can't map memory space\n", devname);
    125 		return;
    126 	}
    127 
    128 	sc->sc_pc = pc;
    129 	sc->sc_tag = tag;
    130 	sc->sc.sc_bus.dmatag = pa->pa_dmat;
    131 
    132 	/* Enable the device. */
    133 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    134 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
    135 		       csr | PCI_COMMAND_MASTER_ENABLE);
    136 
    137 	/* Disable interrupts, so we don't get any spurious ones. */
    138 	sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
    139 	DPRINTF(("%s: offs=%d\n", devname, sc->sc.sc_offs));
    140 	EOWRITE2(&sc->sc, EHCI_USBINTR, 0);
    141 
    142 	/* Map and establish the interrupt. */
    143 	if (pci_intr_map(pa, &ih)) {
    144 		aprint_error("%s: couldn't map interrupt\n", devname);
    145 		return;
    146 	}
    147 	intrstr = pci_intr_string(pc, ih);
    148 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ehci_intr, sc);
    149 	if (sc->sc_ih == NULL) {
    150 		aprint_error("%s: couldn't establish interrupt", devname);
    151 		if (intrstr != NULL)
    152 			aprint_normal(" at %s", intrstr);
    153 		aprint_normal("\n");
    154 		return;
    155 	}
    156 	aprint_normal("%s: interrupting at %s\n", devname, intrstr);
    157 
    158 	switch(pci_conf_read(pc, tag, PCI_USBREV) & PCI_USBREV_MASK) {
    159 	case PCI_USBREV_PRE_1_0:
    160 	case PCI_USBREV_1_0:
    161 	case PCI_USBREV_1_1:
    162 		sc->sc.sc_bus.usbrev = USBREV_UNKNOWN;
    163 		aprint_verbose("%s: pre-2.0 USB rev\n", devname);
    164 		return;
    165 	case PCI_USBREV_2_0:
    166 		sc->sc.sc_bus.usbrev = USBREV_2_0;
    167 		break;
    168 	default:
    169 		sc->sc.sc_bus.usbrev = USBREV_UNKNOWN;
    170 		break;
    171 	}
    172 
    173 	/* Figure out vendor for root hub descriptor. */
    174 	vendor = pci_findvendor(pa->pa_id);
    175 	sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
    176 	if (vendor)
    177 		strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
    178 	else
    179 		snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
    180 		    "vendor 0x%04x", PCI_VENDOR(pa->pa_id));
    181 
    182 	/* Enable workaround for dropped interrupts as required */
    183 	switch (sc->sc.sc_id_vendor) {
    184 	case PCI_VENDOR_ATI:
    185 	case PCI_VENDOR_VIATECH:
    186 		sc->sc.sc_flags |= EHCIF_DROPPED_INTR_WORKAROUND;
    187 		aprint_normal("%s: dropped intr workaround enabled\n", devname);
    188 		break;
    189 	default:
    190 		break;
    191 	}
    192 
    193 	/*
    194 	 * Find companion controllers.  According to the spec they always
    195 	 * have lower function numbers so they should be enumerated already.
    196 	 */
    197 	ncomp = 0;
    198 	TAILQ_FOREACH(up, &ehci_pci_alldevs, next) {
    199 		if (up->bus == pa->pa_bus && up->device == pa->pa_device) {
    200 			DPRINTF(("ehci_pci_attach: companion %s\n",
    201 				 USBDEVNAME(up->usb->bdev)));
    202 			sc->sc.sc_comps[ncomp++] = up->usb;
    203 			if (ncomp >= EHCI_COMPANION_MAX)
    204 				break;
    205 		}
    206 	}
    207 	sc->sc.sc_ncomp = ncomp;
    208 
    209 	ehci_get_ownership(&sc->sc, pc, tag);
    210 
    211 	r = ehci_init(&sc->sc);
    212 	if (r != USBD_NORMAL_COMPLETION) {
    213 		aprint_error("%s: init failed, error=%d\n", devname, r);
    214 		return;
    215 	}
    216 
    217 	status = pci_generic_power_register(self, pa->pa_pc, pa->pa_tag,
    218 	    ehci_suspend, ehci_pci_resume);
    219 	if (status != PNP_STATUS_SUCCESS) {
    220 		aprint_error("%s: couldn't establish power handler\n",
    221 		    device_xname(self));
    222 	}
    223 
    224 	/* Attach usb device. */
    225 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
    226 				       usbctlprint);
    227 }
    228 
    229 static int
    230 ehci_pci_detach(device_ptr_t self, int flags)
    231 {
    232 	struct ehci_pci_softc *sc = (struct ehci_pci_softc *)self;
    233 	int rv;
    234 
    235 	pci_generic_power_deregister(self);
    236 	rv = ehci_detach(&sc->sc, flags);
    237 	if (rv)
    238 		return (rv);
    239 	if (sc->sc_ih != NULL) {
    240 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    241 		sc->sc_ih = NULL;
    242 	}
    243 	if (sc->sc.sc_size) {
    244 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
    245 		sc->sc.sc_size = 0;
    246 	}
    247 	return (0);
    248 }
    249 
    250 CFATTACH_DECL(ehci_pci, sizeof(struct ehci_pci_softc),
    251     ehci_pci_match, ehci_pci_attach, ehci_pci_detach, ehci_activate);
    252 
    253 #ifdef EHCI_DEBUG
    254 static void
    255 ehci_dump_caps(ehci_softc_t *sc, pci_chipset_tag_t pc, pcitag_t tag)
    256 {
    257 	u_int32_t cparams, legctlsts, addr, cap, id;
    258 	int maxdump = 10;
    259 
    260 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
    261 	addr = EHCI_HCC_EECP(cparams);
    262 	while (addr != 0) {
    263 		cap = pci_conf_read(pc, tag, addr);
    264 		id = EHCI_CAP_GET_ID(cap);
    265 		switch (id) {
    266 		case EHCI_CAP_ID_LEGACY:
    267 			legctlsts = pci_conf_read(pc, tag,
    268 						  addr + PCI_EHCI_USBLEGCTLSTS);
    269 			printf("ehci_dump_caps: legsup=0x%08x "
    270 			       "legctlsts=0x%08x\n", cap, legctlsts);
    271 			break;
    272 		default:
    273 			printf("ehci_dump_caps: cap=0x%08x\n", cap);
    274 			break;
    275 		}
    276 		if (--maxdump < 0)
    277 			break;
    278 		addr = EHCI_CAP_GET_NEXT(cap);
    279 	}
    280 }
    281 #endif
    282 
    283 static void
    284 ehci_get_ownership(ehci_softc_t *sc, pci_chipset_tag_t pc, pcitag_t tag)
    285 {
    286 	const char *devname = sc->sc_bus.bdev.dv_xname;
    287 	u_int32_t cparams, addr, cap, legsup;
    288 	int maxcap = 10;
    289 	int ms;
    290 
    291 #ifdef EHCI_DEBUG
    292 	if (ehcidebug)
    293 		ehci_dump_caps(sc, pc, tag);
    294 #endif
    295 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
    296 	addr = EHCI_HCC_EECP(cparams);
    297 	while (addr != 0) {
    298 		cap = pci_conf_read(pc, tag, addr);
    299 		if (EHCI_CAP_GET_ID(cap) == EHCI_CAP_ID_LEGACY)
    300 			break;
    301 		if (--maxcap < 0) {
    302 			aprint_normal("%s: broken extended capabilities "
    303 				      "ignored\n", devname);
    304 			return;
    305 		}
    306 		addr = EHCI_CAP_GET_NEXT(cap);
    307 	}
    308 
    309 	/* If the USB legacy capability is not specified, we are done */
    310 	if (addr == 0)
    311 		return;
    312 
    313 	legsup = pci_conf_read(pc, tag, addr + PCI_EHCI_USBLEGSUP);
    314 	/* Ask BIOS to give up ownership */
    315 	legsup |= EHCI_LEG_HC_OS_OWNED;
    316 	pci_conf_write(pc, tag, addr + PCI_EHCI_USBLEGSUP, legsup);
    317 	for (ms = 0; ms < EHCI_MAX_BIOS_WAIT; ms++) {
    318 		legsup = pci_conf_read(pc, tag, addr + PCI_EHCI_USBLEGSUP);
    319 		if (!(legsup & EHCI_LEG_HC_BIOS_OWNED))
    320 			break;
    321 		delay(1000);
    322 	}
    323 	if (ms == EHCI_MAX_BIOS_WAIT) {
    324 		aprint_normal("%s: BIOS refuses to give up ownership, "
    325 			      "using force\n", devname);
    326 		pci_conf_write(pc, tag, addr + PCI_EHCI_USBLEGSUP, 0);
    327 		pci_conf_write(pc, tag, addr + PCI_EHCI_USBLEGCTLSTS, 0);
    328 	} else {
    329 		aprint_verbose("%s: BIOS has given up ownership\n", devname);
    330 	}
    331 }
    332 
    333 static void
    334 ehci_pci_resume(device_t dv)
    335 {
    336 	struct ehci_pci_softc *sc = device_private(dv);
    337 
    338 	ehci_get_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
    339 	ehci_resume(dv);
    340 }
    341