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