Home | History | Annotate | Line # | Download | only in pci
xhci_pci.c revision 1.2
      1  1.2     skrll /*	$NetBSD: xhci_pci.c,v 1.2 2014/03/10 13:10:41 skrll Exp $	*/
      2  1.1  jakllsch 
      3  1.1  jakllsch /*
      4  1.1  jakllsch  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1  jakllsch  * All rights reserved.
      6  1.1  jakllsch  *
      7  1.1  jakllsch  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  jakllsch  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  1.1  jakllsch  * Carlstedt Research & Technology.
     10  1.1  jakllsch  *
     11  1.1  jakllsch  * Redistribution and use in source and binary forms, with or without
     12  1.1  jakllsch  * modification, are permitted provided that the following conditions
     13  1.1  jakllsch  * are met:
     14  1.1  jakllsch  * 1. Redistributions of source code must retain the above copyright
     15  1.1  jakllsch  *    notice, this list of conditions and the following disclaimer.
     16  1.1  jakllsch  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  jakllsch  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  jakllsch  *    documentation and/or other materials provided with the distribution.
     19  1.1  jakllsch  *
     20  1.1  jakllsch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.1  jakllsch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1  jakllsch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1  jakllsch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.1  jakllsch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.1  jakllsch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.1  jakllsch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1  jakllsch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1  jakllsch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1  jakllsch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1  jakllsch  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1  jakllsch  */
     32  1.1  jakllsch 
     33  1.1  jakllsch #include <sys/cdefs.h>
     34  1.2     skrll __KERNEL_RCSID(0, "$NetBSD: xhci_pci.c,v 1.2 2014/03/10 13:10:41 skrll Exp $");
     35  1.1  jakllsch 
     36  1.1  jakllsch #include <sys/param.h>
     37  1.1  jakllsch #include <sys/systm.h>
     38  1.1  jakllsch #include <sys/kernel.h>
     39  1.1  jakllsch #include <sys/device.h>
     40  1.1  jakllsch #include <sys/proc.h>
     41  1.1  jakllsch #include <sys/queue.h>
     42  1.1  jakllsch 
     43  1.1  jakllsch #include <sys/bus.h>
     44  1.1  jakllsch 
     45  1.1  jakllsch #include <dev/pci/pcivar.h>
     46  1.1  jakllsch 
     47  1.1  jakllsch #include <dev/usb/usb.h>
     48  1.1  jakllsch #include <dev/usb/usbdi.h>
     49  1.1  jakllsch #include <dev/usb/usbdivar.h>
     50  1.1  jakllsch #include <dev/usb/usb_mem.h>
     51  1.1  jakllsch 
     52  1.1  jakllsch #include <dev/usb/xhcireg.h>
     53  1.1  jakllsch #include <dev/usb/xhcivar.h>
     54  1.1  jakllsch 
     55  1.1  jakllsch struct xhci_pci_softc {
     56  1.1  jakllsch 	struct xhci_softc	sc_xhci;
     57  1.1  jakllsch 	pci_chipset_tag_t	sc_pc;
     58  1.1  jakllsch 	pcitag_t		sc_tag;
     59  1.1  jakllsch };
     60  1.1  jakllsch 
     61  1.1  jakllsch static int
     62  1.1  jakllsch xhci_pci_match(device_t parent, cfdata_t match, void *aux)
     63  1.1  jakllsch {
     64  1.1  jakllsch 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
     65  1.1  jakllsch 
     66  1.1  jakllsch 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
     67  1.1  jakllsch 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
     68  1.1  jakllsch 	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_XHCI)
     69  1.1  jakllsch 		return 1;
     70  1.1  jakllsch 
     71  1.1  jakllsch 	return 0;
     72  1.1  jakllsch }
     73  1.1  jakllsch 
     74  1.1  jakllsch static void
     75  1.1  jakllsch xhci_pci_attach(device_t parent, device_t self, void *aux)
     76  1.1  jakllsch {
     77  1.1  jakllsch 	struct xhci_pci_softc * const psc = device_private(self);
     78  1.1  jakllsch 	struct xhci_softc * const sc = &psc->sc_xhci;
     79  1.1  jakllsch 	struct pci_attach_args *const pa = (struct pci_attach_args *)aux;
     80  1.1  jakllsch 	const pci_chipset_tag_t pc = pa->pa_pc;
     81  1.1  jakllsch 	const pcitag_t tag = pa->pa_tag;
     82  1.1  jakllsch 	char const *intrstr;
     83  1.1  jakllsch 	pci_intr_handle_t ih;
     84  1.1  jakllsch 	pcireg_t csr, memtype;
     85  1.2     skrll 	int err;
     86  1.1  jakllsch 	//const char *vendor;
     87  1.1  jakllsch 	uint32_t hccparams;
     88  1.1  jakllsch 
     89  1.1  jakllsch 	sc->sc_dev = self;
     90  1.1  jakllsch 	sc->sc_bus.hci_private = sc;
     91  1.1  jakllsch 
     92  1.1  jakllsch 	pci_aprint_devinfo(pa, "USB Controller");
     93  1.1  jakllsch 
     94  1.1  jakllsch 	/* check if memory space access is enabled */
     95  1.1  jakllsch 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
     96  1.1  jakllsch #ifdef DEBUG
     97  1.1  jakllsch 	printf("csr: %08x\n", csr);
     98  1.1  jakllsch #endif
     99  1.1  jakllsch 	if ((csr & PCI_COMMAND_MEM_ENABLE) == 0) {
    100  1.1  jakllsch 		aprint_error_dev(self, "memory access is disabled\n");
    101  1.1  jakllsch 		return;
    102  1.1  jakllsch 	}
    103  1.1  jakllsch 
    104  1.1  jakllsch 	/* map MMIO registers */
    105  1.1  jakllsch 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_CBMEM);
    106  1.1  jakllsch 	switch (memtype) {
    107  1.1  jakllsch 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    108  1.1  jakllsch 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    109  1.1  jakllsch 		if (pci_mapreg_map(pa, PCI_CBMEM, memtype, 0,
    110  1.1  jakllsch 			   &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_ios)) {
    111  1.1  jakllsch 			sc->sc_ios = 0;
    112  1.1  jakllsch 			aprint_error_dev(self, "can't map mem space\n");
    113  1.1  jakllsch 			return;
    114  1.1  jakllsch 		}
    115  1.1  jakllsch 		break;
    116  1.1  jakllsch 	default:
    117  1.1  jakllsch 		aprint_error_dev(self, "BAR not 64 or 32-bit MMIO\n");
    118  1.1  jakllsch 		return;
    119  1.1  jakllsch 		break;
    120  1.1  jakllsch 	}
    121  1.1  jakllsch 
    122  1.1  jakllsch 	psc->sc_pc = pc;
    123  1.1  jakllsch 	psc->sc_tag = tag;
    124  1.1  jakllsch 
    125  1.1  jakllsch 	hccparams = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0x10);
    126  1.1  jakllsch 
    127  1.1  jakllsch 	if (pci_dma64_available(pa) && ((hccparams&1)==1))
    128  1.1  jakllsch 		sc->sc_bus.dmatag = pa->pa_dmat64;
    129  1.1  jakllsch 	else
    130  1.1  jakllsch 		sc->sc_bus.dmatag = pa->pa_dmat;
    131  1.1  jakllsch 
    132  1.1  jakllsch 	/* Enable the device. */
    133  1.1  jakllsch 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
    134  1.1  jakllsch 		       csr | PCI_COMMAND_MASTER_ENABLE);
    135  1.1  jakllsch 
    136  1.1  jakllsch 	/* Map and establish the interrupt. */
    137  1.1  jakllsch 	if (pci_intr_map(pa, &ih)) {
    138  1.1  jakllsch 		aprint_error_dev(self, "couldn't map interrupt\n");
    139  1.1  jakllsch 		goto fail;
    140  1.1  jakllsch 	}
    141  1.1  jakllsch 
    142  1.1  jakllsch 	/*
    143  1.1  jakllsch 	 * Allocate IRQ
    144  1.1  jakllsch 	 */
    145  1.1  jakllsch 	intrstr = pci_intr_string(pc, ih);
    146  1.1  jakllsch 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, xhci_intr, sc);
    147  1.1  jakllsch 	if (sc->sc_ih == NULL) {
    148  1.1  jakllsch 		aprint_error_dev(self, "couldn't establish interrupt");
    149  1.1  jakllsch 		if (intrstr != NULL)
    150  1.1  jakllsch 			aprint_error(" at %s", intrstr);
    151  1.1  jakllsch 		aprint_error("\n");
    152  1.1  jakllsch 		goto fail;
    153  1.1  jakllsch 	}
    154  1.1  jakllsch 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    155  1.1  jakllsch 
    156  1.1  jakllsch #if 0
    157  1.1  jakllsch 	/* Figure out vendor for root hub descriptor. */
    158  1.1  jakllsch 	vendor = pci_findvendor(pa->pa_id);
    159  1.1  jakllsch 	sc->sc_id_vendor = PCI_VENDOR(pa->pa_id);
    160  1.1  jakllsch 	if (vendor)
    161  1.1  jakllsch 		strlcpy(sc->sc_vendor, vendor, sizeof(sc->sc_vendor));
    162  1.1  jakllsch 	else
    163  1.1  jakllsch 		snprintf(sc->sc_vendor, sizeof(sc->sc_vendor),
    164  1.1  jakllsch 		    "vendor 0x%04x", PCI_VENDOR(pa->pa_id));
    165  1.1  jakllsch #endif
    166  1.1  jakllsch 
    167  1.2     skrll 	err = xhci_init(sc);
    168  1.2     skrll 	if (err) {
    169  1.2     skrll 		aprint_error_dev(self, "init failed, error=%d\n", err);
    170  1.1  jakllsch 		goto fail;
    171  1.1  jakllsch 	}
    172  1.1  jakllsch 
    173  1.1  jakllsch 	if (!pmf_device_register1(self, xhci_suspend, xhci_resume,
    174  1.1  jakllsch 	                          xhci_shutdown))
    175  1.1  jakllsch 		aprint_error_dev(self, "couldn't establish power handler\n");
    176  1.1  jakllsch 
    177  1.1  jakllsch 	/* Attach usb device. */
    178  1.1  jakllsch 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
    179  1.1  jakllsch 	return;
    180  1.1  jakllsch 
    181  1.1  jakllsch fail:
    182  1.1  jakllsch 	if (sc->sc_ih) {
    183  1.1  jakllsch 		pci_intr_disestablish(psc->sc_pc, sc->sc_ih);
    184  1.1  jakllsch 		sc->sc_ih = NULL;
    185  1.1  jakllsch 	}
    186  1.1  jakllsch 	if (sc->sc_ios) {
    187  1.1  jakllsch 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    188  1.1  jakllsch 		sc->sc_ios = 0;
    189  1.1  jakllsch 	}
    190  1.1  jakllsch 	return;
    191  1.1  jakllsch }
    192  1.1  jakllsch 
    193  1.1  jakllsch static int
    194  1.1  jakllsch xhci_pci_detach(device_t self, int flags)
    195  1.1  jakllsch {
    196  1.1  jakllsch 	struct xhci_pci_softc * const psc = device_private(self);
    197  1.1  jakllsch 	struct xhci_softc * const sc = &psc->sc_xhci;
    198  1.1  jakllsch 	int rv;
    199  1.1  jakllsch 
    200  1.1  jakllsch 	rv = xhci_detach(sc, flags);
    201  1.1  jakllsch 	if (rv)
    202  1.1  jakllsch 		return rv;
    203  1.1  jakllsch 
    204  1.1  jakllsch 	pmf_device_deregister(self);
    205  1.1  jakllsch 
    206  1.1  jakllsch 	xhci_shutdown(self, flags);
    207  1.1  jakllsch 
    208  1.1  jakllsch 	if (sc->sc_ios) {
    209  1.1  jakllsch #if 0
    210  1.1  jakllsch 		/* Disable interrupts, so we don't get any spurious ones. */
    211  1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    212  1.1  jakllsch 				  OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
    213  1.1  jakllsch #endif
    214  1.1  jakllsch 	}
    215  1.1  jakllsch 
    216  1.1  jakllsch 	if (sc->sc_ih != NULL) {
    217  1.1  jakllsch 		pci_intr_disestablish(psc->sc_pc, sc->sc_ih);
    218  1.1  jakllsch 		sc->sc_ih = NULL;
    219  1.1  jakllsch 	}
    220  1.1  jakllsch 	if (sc->sc_ios) {
    221  1.1  jakllsch 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    222  1.1  jakllsch 		sc->sc_ios = 0;
    223  1.1  jakllsch 	}
    224  1.1  jakllsch 
    225  1.1  jakllsch 	return 0;
    226  1.1  jakllsch }
    227  1.1  jakllsch 
    228  1.1  jakllsch CFATTACH_DECL3_NEW(xhci_pci, sizeof(struct xhci_pci_softc),
    229  1.1  jakllsch     xhci_pci_match, xhci_pci_attach, xhci_pci_detach, xhci_activate, NULL,
    230  1.1  jakllsch     xhci_childdet, DVF_DETACH_SHUTDOWN);
    231