Home | History | Annotate | Line # | Download | only in pci
ehci_pci.c revision 1.63
      1 /*	$NetBSD: ehci_pci.c,v 1.63 2016/04/23 10:15:31 skrll 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: ehci_pci.c,v 1.63 2016/04/23 10:15:31 skrll Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/device.h>
     39 #include <sys/proc.h>
     40 #include <sys/queue.h>
     41 
     42 #include <sys/bus.h>
     43 
     44 #include <dev/pci/pcidevs.h>
     45 #include <dev/pci/pcivar.h>
     46 #include <dev/pci/usb_pci.h>
     47 
     48 #include <dev/usb/usb.h>
     49 #include <dev/usb/usbdi.h>
     50 #include <dev/usb/usbdivar.h>
     51 #include <dev/usb/usb_mem.h>
     52 
     53 #include <dev/usb/ehcireg.h>
     54 #include <dev/usb/ehcivar.h>
     55 
     56 #ifdef EHCI_DEBUG
     57 #define DPRINTF(x)	if (ehcidebug) printf x
     58 extern int ehcidebug;
     59 #else
     60 #define DPRINTF(x)
     61 #endif
     62 
     63 enum ehci_pci_quirk_flags {
     64 	EHCI_PCI_QUIRK_AMD_SB600 = 0x1,	/* always need a quirk */
     65 	EHCI_PCI_QUIRK_AMD_SB700 = 0x2,	/* depends on the SMB revision */
     66 };
     67 
     68 static const struct pci_quirkdata ehci_pci_quirks[] = {
     69 	{ PCI_VENDOR_ATI, PCI_PRODUCT_ATI_SB600_USB_EHCI,
     70 	    EHCI_PCI_QUIRK_AMD_SB600 },
     71 	{ PCI_VENDOR_ATI, PCI_PRODUCT_ATI_SB700_USB_EHCI,
     72 	    EHCI_PCI_QUIRK_AMD_SB700 },
     73 };
     74 
     75 static void ehci_release_ownership(ehci_softc_t *sc, pci_chipset_tag_t pc,
     76 				   pcitag_t tag);
     77 static void ehci_get_ownership(ehci_softc_t *sc, pci_chipset_tag_t pc,
     78 			       pcitag_t tag);
     79 static bool ehci_pci_suspend(device_t, const pmf_qual_t *);
     80 static bool ehci_pci_resume(device_t, const pmf_qual_t *);
     81 
     82 struct ehci_pci_softc {
     83 	ehci_softc_t		sc;
     84 	pci_chipset_tag_t	sc_pc;
     85 	pcitag_t		sc_tag;
     86 	void 			*sc_ih;		/* interrupt vectoring */
     87 };
     88 
     89 static int ehci_sb700_match(const struct pci_attach_args *pa);
     90 static int ehci_apply_amd_quirks(struct ehci_pci_softc *sc);
     91 enum ehci_pci_quirk_flags ehci_pci_lookup_quirkdata(pci_vendor_id_t,
     92 	pci_product_id_t);
     93 
     94 #define EHCI_MAX_BIOS_WAIT		100 /* ms*10 */
     95 #define EHCI_SBx00_WORKAROUND_REG	0x50
     96 #define EHCI_SBx00_WORKAROUND_ENABLE	__BIT(27)
     97 
     98 
     99 static int
    100 ehci_pci_match(device_t parent, cfdata_t match, void *aux)
    101 {
    102 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
    103 
    104 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
    105 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
    106 	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_EHCI)
    107 		return 1;
    108 
    109 	return 0;
    110 }
    111 
    112 static void
    113 ehci_pci_attach(device_t parent, device_t self, void *aux)
    114 {
    115 	struct ehci_pci_softc *sc = device_private(self);
    116 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    117 	pci_chipset_tag_t pc = pa->pa_pc;
    118 	pcitag_t tag = pa->pa_tag;
    119 	char const *intrstr;
    120 	pci_intr_handle_t ih;
    121 	pcireg_t csr;
    122 	int ncomp;
    123 	struct usb_pci *up;
    124 	int quirk;
    125 	char intrbuf[PCI_INTRSTR_LEN];
    126 
    127 	sc->sc.sc_dev = self;
    128 	sc->sc.sc_bus.ub_hcpriv = sc;
    129 
    130 	pci_aprint_devinfo(pa, "USB controller");
    131 
    132 	/* Check for quirks */
    133 	quirk = ehci_pci_lookup_quirkdata(PCI_VENDOR(pa->pa_id),
    134 					   PCI_PRODUCT(pa->pa_id));
    135 
    136 	/* Map I/O registers */
    137 	if (pci_mapreg_map(pa, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
    138 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
    139 		sc->sc.sc_size = 0;
    140 		aprint_error_dev(self, "can't map memory space\n");
    141 		return;
    142 	}
    143 
    144 	sc->sc_pc = pc;
    145 	sc->sc_tag = tag;
    146 	sc->sc.sc_bus.ub_dmatag = pa->pa_dmat;
    147 
    148 	/* Disable interrupts, so we don't get any spurious ones. */
    149 	sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
    150 	DPRINTF(("%s: offs=%d\n", device_xname(self), sc->sc.sc_offs));
    151 	EOWRITE4(&sc->sc, EHCI_USBINTR, 0);
    152 
    153 	/* Handle quirks */
    154 	switch (quirk) {
    155 	case EHCI_PCI_QUIRK_AMD_SB600:
    156 		ehci_apply_amd_quirks(sc);
    157 		break;
    158 	case EHCI_PCI_QUIRK_AMD_SB700:
    159 		if (pci_find_device(NULL, ehci_sb700_match))
    160 			ehci_apply_amd_quirks(sc);
    161 		break;
    162 	}
    163 
    164 	/* Enable the device. */
    165 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    166 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
    167 		       csr | PCI_COMMAND_MASTER_ENABLE);
    168 
    169 	/* Map and establish the interrupt. */
    170 	if (pci_intr_map(pa, &ih)) {
    171 		aprint_error_dev(self, "couldn't map interrupt\n");
    172 		goto fail;
    173 	}
    174 
    175 	/*
    176 	 * Allocate IRQ
    177 	 */
    178 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
    179 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ehci_intr, sc);
    180 	if (sc->sc_ih == NULL) {
    181 		aprint_error_dev(self, "couldn't establish interrupt");
    182 		if (intrstr != NULL)
    183 			aprint_error(" at %s", intrstr);
    184 		aprint_error("\n");
    185 		goto fail;
    186 	}
    187 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    188 
    189 	switch(pci_conf_read(pc, tag, PCI_USBREV) & PCI_USBREV_MASK) {
    190 	case PCI_USBREV_PRE_1_0:
    191 	case PCI_USBREV_1_0:
    192 	case PCI_USBREV_1_1:
    193 		sc->sc.sc_bus.ub_revision = USBREV_UNKNOWN;
    194 		aprint_verbose_dev(self, "pre-2.0 USB rev\n");
    195 		goto fail;
    196 	case PCI_USBREV_2_0:
    197 		sc->sc.sc_bus.ub_revision = USBREV_2_0;
    198 		break;
    199 	default:
    200 		sc->sc.sc_bus.ub_revision = USBREV_UNKNOWN;
    201 		break;
    202 	}
    203 
    204 	/* Figure out vendor for root hub descriptor. */
    205 	sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
    206 	pci_findvendor(sc->sc.sc_vendor,
    207 	    sizeof(sc->sc.sc_vendor), sc->sc.sc_id_vendor);
    208 	/* Enable workaround for dropped interrupts as required */
    209 	switch (sc->sc.sc_id_vendor) {
    210 	case PCI_VENDOR_ATI:
    211 	case PCI_VENDOR_VIATECH:
    212 		sc->sc.sc_flags |= EHCIF_DROPPED_INTR_WORKAROUND;
    213 		aprint_normal_dev(self, "dropped intr workaround enabled\n");
    214 		break;
    215 	default:
    216 		break;
    217 	}
    218 
    219 	/*
    220 	 * Find companion controllers.  According to the spec they always
    221 	 * have lower function numbers so they should be enumerated already.
    222 	 */
    223 	const u_int maxncomp = EHCI_HCS_N_CC(EREAD4(&sc->sc, EHCI_HCSPARAMS));
    224 	KASSERT(maxncomp <= EHCI_COMPANION_MAX);
    225 	ncomp = 0;
    226 	TAILQ_FOREACH(up, &ehci_pci_alldevs, next) {
    227 		if (up->bus == pa->pa_bus && up->device == pa->pa_device
    228 		    && !up->claimed) {
    229 			DPRINTF(("ehci_pci_attach: companion %s\n",
    230 				 device_xname(up->usb)));
    231 			sc->sc.sc_comps[ncomp++] = up->usb;
    232 			up->claimed = true;
    233 			if (ncomp == maxncomp)
    234 				break;
    235 		}
    236 	}
    237 	sc->sc.sc_ncomp = ncomp;
    238 
    239 	ehci_get_ownership(&sc->sc, pc, tag);
    240 
    241 	int err = ehci_init(&sc->sc);
    242 	if (err) {
    243 		aprint_error_dev(self, "init failed, error=%d\n", err);
    244 		goto fail;
    245 	}
    246 
    247 	if (!pmf_device_register1(self, ehci_pci_suspend, ehci_pci_resume,
    248 	                          ehci_shutdown))
    249 		aprint_error_dev(self, "couldn't establish power handler\n");
    250 
    251 	/* Attach usb device. */
    252 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
    253 	return;
    254 
    255 fail:
    256 	if (sc->sc_ih) {
    257 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    258 		sc->sc_ih = NULL;
    259 	}
    260 	if (sc->sc.sc_size) {
    261 		ehci_release_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
    262 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
    263 		sc->sc.sc_size = 0;
    264 	}
    265 	return;
    266 }
    267 
    268 static int
    269 ehci_pci_detach(device_t self, int flags)
    270 {
    271 	struct ehci_pci_softc *sc = device_private(self);
    272 	int rv;
    273 
    274 	rv = ehci_detach(&sc->sc, flags);
    275 	if (rv)
    276 		return rv;
    277 
    278 	pmf_device_deregister(self);
    279 	ehci_shutdown(self, flags);
    280 
    281 	/* disable interrupts */
    282 	EOWRITE4(&sc->sc, EHCI_USBINTR, 0);
    283 	/* XXX grotty hack to flush the write */
    284 	(void)EOREAD4(&sc->sc, EHCI_USBINTR);
    285 
    286 	if (sc->sc_ih != NULL) {
    287 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    288 		sc->sc_ih = NULL;
    289 	}
    290 	if (sc->sc.sc_size) {
    291 		ehci_release_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
    292 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
    293 		sc->sc.sc_size = 0;
    294 	}
    295 
    296 #if 1
    297 	/* XXX created in ehci.c */
    298 	mutex_destroy(&sc->sc.sc_lock);
    299 	mutex_destroy(&sc->sc.sc_intr_lock);
    300 
    301 	softint_disestablish(sc->sc.sc_doorbell_si);
    302 	softint_disestablish(sc->sc.sc_pcd_si);
    303 #endif
    304 
    305 	return 0;
    306 }
    307 
    308 CFATTACH_DECL3_NEW(ehci_pci, sizeof(struct ehci_pci_softc),
    309     ehci_pci_match, ehci_pci_attach, ehci_pci_detach, ehci_activate, NULL,
    310     ehci_childdet, DVF_DETACH_SHUTDOWN);
    311 
    312 #ifdef EHCI_DEBUG
    313 static void
    314 ehci_dump_caps(ehci_softc_t *sc, pci_chipset_tag_t pc, pcitag_t tag)
    315 {
    316 	uint32_t cparams, legctlsts, addr, cap, id;
    317 	int maxdump = 10;
    318 
    319 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
    320 	addr = EHCI_HCC_EECP(cparams);
    321 	while (addr != 0) {
    322 		cap = pci_conf_read(pc, tag, addr);
    323 		id = EHCI_CAP_GET_ID(cap);
    324 		switch (id) {
    325 		case EHCI_CAP_ID_LEGACY:
    326 			legctlsts = pci_conf_read(pc, tag,
    327 			    addr + PCI_EHCI_USBLEGCTLSTS);
    328 			printf("ehci_dump_caps: legsup=0x%08x "
    329 			       "legctlsts=0x%08x\n", cap, legctlsts);
    330 			break;
    331 		default:
    332 			printf("ehci_dump_caps: cap=0x%08x\n", cap);
    333 			break;
    334 		}
    335 		if (--maxdump < 0)
    336 			break;
    337 		addr = EHCI_CAP_GET_NEXT(cap);
    338 	}
    339 }
    340 #endif
    341 
    342 static void
    343 ehci_release_ownership(ehci_softc_t *sc, pci_chipset_tag_t pc, pcitag_t tag)
    344 {
    345 	const char *devname = device_xname(sc->sc_dev);
    346 	uint32_t cparams, addr, cap;
    347 	pcireg_t legsup;
    348 	int maxcap = 10;
    349 
    350 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
    351 	addr = EHCI_HCC_EECP(cparams);
    352 	while (addr != 0) {
    353 		cap = pci_conf_read(pc, tag, addr);
    354 		if (EHCI_CAP_GET_ID(cap) != EHCI_CAP_ID_LEGACY)
    355 			goto next;
    356 		legsup = pci_conf_read(pc, tag, addr + PCI_EHCI_USBLEGSUP);
    357 		pci_conf_write(pc, tag, addr + PCI_EHCI_USBLEGSUP,
    358 		    legsup & ~EHCI_LEG_HC_OS_OWNED);
    359 
    360 next:
    361 		if (--maxcap < 0) {
    362 			aprint_normal("%s: broken extended capabilities "
    363 				      "ignored\n", devname);
    364 			return;
    365 		}
    366 		addr = EHCI_CAP_GET_NEXT(cap);
    367 	}
    368 }
    369 
    370 static void
    371 ehci_get_ownership(ehci_softc_t *sc, pci_chipset_tag_t pc, pcitag_t tag)
    372 {
    373 	const char *devname = device_xname(sc->sc_dev);
    374 	uint32_t cparams, addr, cap;
    375 	pcireg_t legsup;
    376 	int maxcap = 10;
    377 	int ms;
    378 
    379 #ifdef EHCI_DEBUG
    380 	if (ehcidebug)
    381 		ehci_dump_caps(sc, pc, tag);
    382 #endif
    383 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
    384 	addr = EHCI_HCC_EECP(cparams);
    385 	while (addr != 0) {
    386 		cap = pci_conf_read(pc, tag, addr);
    387 		if (EHCI_CAP_GET_ID(cap) != EHCI_CAP_ID_LEGACY)
    388 			goto next;
    389 		legsup = pci_conf_read(pc, tag, addr + PCI_EHCI_USBLEGSUP);
    390 		if (legsup & EHCI_LEG_HC_BIOS_OWNED) {
    391 			/* Ask BIOS to give up ownership */
    392 			pci_conf_write(pc, tag, addr + PCI_EHCI_USBLEGSUP,
    393 			    legsup | EHCI_LEG_HC_OS_OWNED);
    394 			for (ms = 0; ms < EHCI_MAX_BIOS_WAIT; ms++) {
    395 				legsup = pci_conf_read(pc, tag,
    396 				    addr + PCI_EHCI_USBLEGSUP);
    397 				if (!(legsup & EHCI_LEG_HC_BIOS_OWNED))
    398 					break;
    399 				delay(10000);
    400 			}
    401 			if (ms == EHCI_MAX_BIOS_WAIT) {
    402 				aprint_normal("%s: BIOS refuses to give up "
    403 				    "ownership, using force\n", devname);
    404 				pci_conf_write(pc, tag,
    405 				    addr + PCI_EHCI_USBLEGSUP, 0);
    406 			} else
    407 				aprint_verbose("%s: BIOS has given up "
    408 				    "ownership\n", devname);
    409 		}
    410 
    411 		/* Disable SMIs */
    412 		pci_conf_write(pc, tag, addr + PCI_EHCI_USBLEGCTLSTS, 0);
    413 
    414 next:
    415 		if (--maxcap < 0) {
    416 			aprint_normal("%s: broken extended capabilities "
    417 				      "ignored\n", devname);
    418 			return;
    419 		}
    420 		addr = EHCI_CAP_GET_NEXT(cap);
    421 	}
    422 
    423 }
    424 
    425 static bool
    426 ehci_pci_suspend(device_t dv, const pmf_qual_t *qual)
    427 {
    428 	struct ehci_pci_softc *sc = device_private(dv);
    429 
    430 	ehci_suspend(dv, qual);
    431 	ehci_release_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
    432 
    433 	return true;
    434 }
    435 
    436 static bool
    437 ehci_pci_resume(device_t dv, const pmf_qual_t *qual)
    438 {
    439 	struct ehci_pci_softc *sc = device_private(dv);
    440 
    441 	ehci_get_ownership(&sc->sc, sc->sc_pc, sc->sc_tag);
    442 	return ehci_resume(dv, qual);
    443 }
    444 
    445 static int
    446 ehci_sb700_match(const struct pci_attach_args *pa)
    447 {
    448 	if (!(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ATI &&
    449 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_SB600_SMB))
    450 		return 0;
    451 
    452 	switch (PCI_REVISION(pa->pa_class)) {
    453 	case 0x3a:
    454 	case 0x3b:
    455 		return 1;
    456 	}
    457 
    458 	return 0;
    459 }
    460 
    461 static int
    462 ehci_apply_amd_quirks(struct ehci_pci_softc *sc)
    463 {
    464 	pcireg_t value;
    465 
    466 	aprint_normal_dev(sc->sc.sc_dev,
    467 	    "applying AMD SB600/SB700 USB freeze workaround\n");
    468 	value = pci_conf_read(sc->sc_pc, sc->sc_tag, EHCI_SBx00_WORKAROUND_REG);
    469 	pci_conf_write(sc->sc_pc, sc->sc_tag, EHCI_SBx00_WORKAROUND_REG,
    470 	    value | EHCI_SBx00_WORKAROUND_ENABLE);
    471 
    472 	return 0;
    473 }
    474 
    475 enum ehci_pci_quirk_flags
    476 ehci_pci_lookup_quirkdata(pci_vendor_id_t vendor, pci_product_id_t product)
    477 {
    478 	int i;
    479 
    480 	for (i = 0; i < __arraycount(ehci_pci_quirks); i++) {
    481 		if (vendor == ehci_pci_quirks[i].vendor &&
    482 		    product == ehci_pci_quirks[i].product)
    483 			return ehci_pci_quirks[i].quirks;
    484 	}
    485 	return 0;
    486 }
    487 
    488