Home | History | Annotate | Line # | Download | only in nvidia
tegra_pcie.c revision 1.38
      1  1.38   thorpej /* $NetBSD: tegra_pcie.c,v 1.38 2021/05/12 04:07:34 thorpej Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21   1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22   1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23   1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24   1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  jmcneill  * SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.1  jmcneill #include <sys/cdefs.h>
     30  1.38   thorpej __KERNEL_RCSID(0, "$NetBSD: tegra_pcie.c,v 1.38 2021/05/12 04:07:34 thorpej Exp $");
     31   1.1  jmcneill 
     32   1.1  jmcneill #include <sys/param.h>
     33  1.28     skrll 
     34   1.1  jmcneill #include <sys/bus.h>
     35   1.1  jmcneill #include <sys/device.h>
     36   1.1  jmcneill #include <sys/intr.h>
     37  1.28     skrll #include <sys/kmem.h>
     38   1.1  jmcneill #include <sys/kernel.h>
     39  1.29     skrll #include <sys/lwp.h>
     40  1.28     skrll #include <sys/mutex.h>
     41   1.1  jmcneill #include <sys/queue.h>
     42  1.28     skrll #include <sys/systm.h>
     43   1.1  jmcneill 
     44  1.24       ryo #include <machine/cpu.h>
     45  1.24       ryo 
     46   1.1  jmcneill #include <arm/cpufunc.h>
     47   1.1  jmcneill 
     48   1.1  jmcneill #include <dev/pci/pcireg.h>
     49   1.1  jmcneill #include <dev/pci/pcivar.h>
     50   1.1  jmcneill #include <dev/pci/pciconf.h>
     51   1.1  jmcneill 
     52   1.1  jmcneill #include <arm/nvidia/tegra_reg.h>
     53   1.1  jmcneill #include <arm/nvidia/tegra_pciereg.h>
     54  1.21  jmcneill #include <arm/nvidia/tegra_pmcreg.h>
     55   1.1  jmcneill #include <arm/nvidia/tegra_var.h>
     56   1.1  jmcneill 
     57  1.14  jmcneill #include <dev/fdt/fdtvar.h>
     58  1.14  jmcneill 
     59  1.17  jmcneill /* Interrupt handle flags */
     60  1.17  jmcneill #define	IH_MPSAFE	0x80000000
     61  1.17  jmcneill 
     62   1.1  jmcneill static int	tegra_pcie_match(device_t, cfdata_t, void *);
     63   1.1  jmcneill static void	tegra_pcie_attach(device_t, device_t, void *);
     64   1.1  jmcneill 
     65  1.12  jakllsch #define TEGRA_PCIE_NBUS 256
     66  1.12  jakllsch #define TEGRA_PCIE_ECFB (1<<(12 - 8))	/* extended conf frags per bus */
     67  1.12  jakllsch 
     68  1.33   thorpej enum tegra_pcie_type {
     69  1.33   thorpej 	TEGRA_PCIE_124		= 0,
     70  1.33   thorpej 	TEGRA_PCIE_210		= 1,
     71  1.33   thorpej };
     72  1.33   thorpej 
     73   1.1  jmcneill struct tegra_pcie_ih {
     74   1.1  jmcneill 	int			(*ih_callback)(void *);
     75   1.1  jmcneill 	void			*ih_arg;
     76   1.1  jmcneill 	int			ih_ipl;
     77  1.16  jmcneill 	int			ih_mpsafe;
     78   1.1  jmcneill 	TAILQ_ENTRY(tegra_pcie_ih) ih_entry;
     79   1.1  jmcneill };
     80   1.1  jmcneill 
     81   1.1  jmcneill struct tegra_pcie_softc {
     82   1.1  jmcneill 	device_t		sc_dev;
     83   1.1  jmcneill 	bus_dma_tag_t		sc_dmat;
     84   1.1  jmcneill 	bus_space_tag_t		sc_bst;
     85   1.1  jmcneill 	bus_space_handle_t	sc_bsh_afi;
     86  1.21  jmcneill 	bus_space_handle_t	sc_bsh_pads;
     87   1.9  jakllsch 	bus_space_handle_t	sc_bsh_rpconf;
     88  1.14  jmcneill 	int			sc_phandle;
     89  1.33   thorpej 	enum tegra_pcie_type	sc_type;
     90   1.1  jmcneill 
     91   1.1  jmcneill 	struct arm32_pci_chipset sc_pc;
     92   1.1  jmcneill 
     93   1.1  jmcneill 	void			*sc_ih;
     94   1.1  jmcneill 
     95   1.1  jmcneill 	kmutex_t		sc_lock;
     96   1.1  jmcneill 
     97   1.1  jmcneill 	TAILQ_HEAD(, tegra_pcie_ih) sc_intrs;
     98   1.1  jmcneill 	u_int			sc_intrgen;
     99  1.12  jakllsch 
    100  1.12  jakllsch 	bus_space_handle_t	sc_bsh_extc[TEGRA_PCIE_NBUS-1][TEGRA_PCIE_ECFB];
    101   1.1  jmcneill };
    102   1.1  jmcneill 
    103   1.1  jmcneill static int	tegra_pcie_intr(void *);
    104   1.1  jmcneill static void	tegra_pcie_init(pci_chipset_tag_t, void *);
    105   1.1  jmcneill static void	tegra_pcie_enable(struct tegra_pcie_softc *);
    106  1.21  jmcneill static void	tegra_pcie_enable_ports(struct tegra_pcie_softc *);
    107  1.20  jmcneill static void	tegra_pcie_enable_clocks(struct tegra_pcie_softc *);
    108  1.10  jakllsch static void	tegra_pcie_setup(struct tegra_pcie_softc * const);
    109  1.12  jakllsch static void	tegra_pcie_conf_frag_map(struct tegra_pcie_softc * const,
    110  1.12  jakllsch 					 uint, uint);
    111  1.12  jakllsch static void	tegra_pcie_conf_map_bus(struct tegra_pcie_softc * const, uint);
    112  1.12  jakllsch static void	tegra_pcie_conf_map_buses(struct tegra_pcie_softc * const);
    113   1.1  jmcneill 
    114   1.1  jmcneill static void	tegra_pcie_attach_hook(device_t, device_t,
    115   1.1  jmcneill 				       struct pcibus_attach_args *);
    116   1.1  jmcneill static int	tegra_pcie_bus_maxdevs(void *, int);
    117   1.1  jmcneill static pcitag_t	tegra_pcie_make_tag(void *, int, int, int);
    118   1.1  jmcneill static void	tegra_pcie_decompose_tag(void *, pcitag_t, int *, int *, int *);
    119   1.1  jmcneill static pcireg_t	tegra_pcie_conf_read(void *, pcitag_t, int);
    120   1.1  jmcneill static void	tegra_pcie_conf_write(void *, pcitag_t, int, pcireg_t);
    121   1.1  jmcneill static int	tegra_pcie_conf_hook(void *, int, int, int, pcireg_t);
    122   1.1  jmcneill static void	tegra_pcie_conf_interrupt(void *, int, int, int, int, int *);
    123   1.1  jmcneill 
    124   1.1  jmcneill static int	tegra_pcie_intr_map(const struct pci_attach_args *,
    125   1.1  jmcneill 				    pci_intr_handle_t *);
    126   1.1  jmcneill static const char *tegra_pcie_intr_string(void *, pci_intr_handle_t,
    127   1.1  jmcneill 					  char *, size_t);
    128   1.1  jmcneill const struct evcnt *tegra_pcie_intr_evcnt(void *, pci_intr_handle_t);
    129  1.16  jmcneill static int	tegra_pcie_intr_setattr(void *, pci_intr_handle_t *, int,
    130  1.16  jmcneill 					uint64_t);
    131   1.1  jmcneill static void *	tegra_pcie_intr_establish(void *, pci_intr_handle_t,
    132  1.25  jmcneill 					 int, int (*)(void *), void *,
    133  1.25  jmcneill 					 const char *);
    134   1.1  jmcneill static void	tegra_pcie_intr_disestablish(void *, void *);
    135   1.1  jmcneill 
    136   1.1  jmcneill CFATTACH_DECL_NEW(tegra_pcie, sizeof(struct tegra_pcie_softc),
    137   1.1  jmcneill 	tegra_pcie_match, tegra_pcie_attach, NULL, NULL);
    138   1.1  jmcneill 
    139  1.33   thorpej static const struct device_compatible_entry compat_data[] = {
    140  1.33   thorpej 	{ .compat = "nvidia,tegra210-pcie",	.value = TEGRA_PCIE_210 },
    141  1.33   thorpej 	{ .compat = "nvidia,tegra124-pcie",	.value = TEGRA_PCIE_124 },
    142  1.35   thorpej 	DEVICE_COMPAT_EOL
    143  1.33   thorpej };
    144  1.33   thorpej 
    145   1.1  jmcneill static int
    146   1.1  jmcneill tegra_pcie_match(device_t parent, cfdata_t cf, void *aux)
    147   1.1  jmcneill {
    148  1.14  jmcneill 	struct fdt_attach_args * const faa = aux;
    149  1.14  jmcneill 
    150  1.36   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    151   1.1  jmcneill }
    152   1.1  jmcneill 
    153   1.1  jmcneill static void
    154   1.1  jmcneill tegra_pcie_attach(device_t parent, device_t self, void *aux)
    155   1.1  jmcneill {
    156   1.1  jmcneill 	struct tegra_pcie_softc * const sc = device_private(self);
    157  1.14  jmcneill 	struct fdt_attach_args * const faa = aux;
    158  1.33   thorpej 	const struct device_compatible_entry *dce;
    159  1.31   thorpej 	struct pciconf_resources *pcires;
    160   1.1  jmcneill 	struct pcibus_attach_args pba;
    161  1.21  jmcneill 	bus_addr_t afi_addr, cs_addr, pads_addr;
    162  1.21  jmcneill 	bus_size_t afi_size, cs_size, pads_size;
    163  1.14  jmcneill 	char intrstr[128];
    164   1.1  jmcneill 	int error;
    165   1.1  jmcneill 
    166  1.21  jmcneill 	if (fdtbus_get_reg_byname(faa->faa_phandle, "afi", &afi_addr, &afi_size) != 0) {
    167  1.14  jmcneill 		aprint_error(": couldn't get afi registers\n");
    168  1.14  jmcneill 		return;
    169  1.14  jmcneill 	}
    170  1.21  jmcneill 	if (fdtbus_get_reg_byname(faa->faa_phandle, "pads", &pads_addr, &pads_size) != 0) {
    171  1.21  jmcneill 		aprint_error(": couldn't get pads registers\n");
    172  1.21  jmcneill 		return;
    173  1.21  jmcneill 	}
    174  1.14  jmcneill #if notyet
    175  1.14  jmcneill 	if (fdtbus_get_reg(faa->faa_phandle, 2, &cs_addr, &cs_size) != 0) {
    176  1.14  jmcneill 		aprint_error(": couldn't get cs registers\n");
    177  1.14  jmcneill 		return;
    178  1.14  jmcneill 	}
    179  1.14  jmcneill #else
    180  1.14  jmcneill 	cs_addr = TEGRA_PCIE_RPCONF_BASE;
    181  1.14  jmcneill 	cs_size = TEGRA_PCIE_RPCONF_SIZE;
    182  1.14  jmcneill #endif
    183  1.14  jmcneill 
    184   1.1  jmcneill 	sc->sc_dev = self;
    185  1.14  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    186  1.14  jmcneill 	sc->sc_bst = faa->faa_bst;
    187  1.14  jmcneill 	sc->sc_phandle = faa->faa_phandle;
    188  1.14  jmcneill 	error = bus_space_map(sc->sc_bst, afi_addr, afi_size, 0,
    189  1.14  jmcneill 	    &sc->sc_bsh_afi);
    190  1.14  jmcneill 	if (error) {
    191  1.14  jmcneill 		aprint_error(": couldn't map afi registers: %d\n", error);
    192  1.14  jmcneill 		return;
    193  1.14  jmcneill 	}
    194  1.21  jmcneill 	error = bus_space_map(sc->sc_bst, pads_addr, pads_size, 0,
    195  1.21  jmcneill 	    &sc->sc_bsh_pads);
    196  1.21  jmcneill 	if (error) {
    197  1.26  jakllsch 		aprint_error(": couldn't map pads registers: %d\n", error);
    198  1.21  jmcneill 		return;
    199  1.21  jmcneill 	}
    200  1.27  jmcneill 	error = bus_space_map(sc->sc_bst, cs_addr, cs_size,
    201  1.27  jmcneill 	    _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED, &sc->sc_bsh_rpconf);
    202  1.14  jmcneill 	if (error) {
    203  1.14  jmcneill 		aprint_error(": couldn't map cs registers: %d\n", error);
    204  1.14  jmcneill 		return;
    205  1.14  jmcneill 	}
    206  1.14  jmcneill 
    207  1.36   thorpej 	dce = of_compatible_lookup(faa->faa_phandle, compat_data);
    208  1.33   thorpej 	KASSERT(dce != NULL);
    209  1.33   thorpej 	sc->sc_type = dce->value;
    210  1.33   thorpej 
    211  1.12  jakllsch 	tegra_pcie_conf_map_buses(sc);
    212   1.1  jmcneill 
    213   1.1  jmcneill 	TAILQ_INIT(&sc->sc_intrs);
    214   1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    215   1.1  jmcneill 
    216   1.1  jmcneill 	aprint_naive("\n");
    217   1.1  jmcneill 	aprint_normal(": PCIE\n");
    218   1.1  jmcneill 
    219  1.21  jmcneill 	tegra_pmc_power(PMC_PARTID_PCX, true);
    220  1.21  jmcneill 	tegra_pmc_remove_clamping(PMC_PARTID_PCX);
    221  1.21  jmcneill 
    222  1.20  jmcneill 	tegra_pcie_enable_clocks(sc);
    223  1.20  jmcneill 
    224  1.14  jmcneill 	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
    225  1.14  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    226  1.14  jmcneill 		return;
    227  1.14  jmcneill 	}
    228  1.14  jmcneill 
    229  1.32  jmcneill 	sc->sc_ih = fdtbus_intr_establish_xname(faa->faa_phandle, 0, IPL_VM,
    230  1.32  jmcneill 	    FDT_INTR_MPSAFE, tegra_pcie_intr, sc, device_xname(self));
    231   1.1  jmcneill 	if (sc->sc_ih == NULL) {
    232  1.14  jmcneill 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    233  1.14  jmcneill 		    intrstr);
    234   1.1  jmcneill 		return;
    235   1.1  jmcneill 	}
    236  1.14  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    237   1.1  jmcneill 
    238  1.10  jakllsch 	tegra_pcie_setup(sc);
    239  1.10  jakllsch 
    240   1.1  jmcneill 	tegra_pcie_init(&sc->sc_pc, sc);
    241   1.1  jmcneill 
    242  1.31   thorpej 	pcires = pciconf_resource_init();
    243   1.1  jmcneill 
    244  1.31   thorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
    245  1.31   thorpej 	    TEGRA_PCIE_IO_BASE, TEGRA_PCIE_IO_SIZE);
    246  1.31   thorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
    247  1.31   thorpej 	    TEGRA_PCIE_MEM_BASE, TEGRA_PCIE_MEM_SIZE);
    248  1.31   thorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_PREFETCHABLE_MEM,
    249  1.31   thorpej 	    TEGRA_PCIE_PMEM_BASE, TEGRA_PCIE_PMEM_SIZE);
    250  1.31   thorpej 
    251  1.31   thorpej 	error = pci_configure_bus(&sc->sc_pc, pcires, 0,
    252   1.1  jmcneill 	    arm_dcache_align);
    253   1.1  jmcneill 
    254  1.31   thorpej 	pciconf_resource_fini(pcires);
    255   1.1  jmcneill 
    256   1.1  jmcneill 	if (error) {
    257   1.1  jmcneill 		aprint_error_dev(self, "configuration failed (%d)\n",
    258   1.1  jmcneill 		    error);
    259   1.1  jmcneill 		return;
    260   1.1  jmcneill 	}
    261   1.1  jmcneill 
    262   1.1  jmcneill 	tegra_pcie_enable(sc);
    263   1.1  jmcneill 
    264  1.21  jmcneill 	tegra_pcie_enable_ports(sc);
    265  1.21  jmcneill 
    266   1.1  jmcneill 	memset(&pba, 0, sizeof(pba));
    267   1.1  jmcneill 	pba.pba_flags = PCI_FLAGS_MRL_OKAY |
    268   1.1  jmcneill 			PCI_FLAGS_MRM_OKAY |
    269   1.1  jmcneill 			PCI_FLAGS_MWI_OKAY |
    270  1.10  jakllsch 			PCI_FLAGS_MEM_OKAY |
    271  1.10  jakllsch 			PCI_FLAGS_IO_OKAY;
    272  1.10  jakllsch 	pba.pba_iot = sc->sc_bst;
    273   1.1  jmcneill 	pba.pba_memt = sc->sc_bst;
    274   1.1  jmcneill 	pba.pba_dmat = sc->sc_dmat;
    275   1.1  jmcneill 	pba.pba_pc = &sc->sc_pc;
    276   1.1  jmcneill 	pba.pba_bus = 0;
    277   1.1  jmcneill 
    278  1.38   thorpej 	config_found(self, &pba, pcibusprint,
    279  1.38   thorpej 	    CFARG_DEVHANDLE, device_handle(self),
    280  1.38   thorpej 	    CFARG_EOL);
    281   1.1  jmcneill }
    282   1.1  jmcneill 
    283   1.1  jmcneill static int
    284   1.4  jmcneill tegra_pcie_legacy_intr(struct tegra_pcie_softc *sc)
    285   1.1  jmcneill {
    286   1.4  jmcneill 	const uint32_t msg = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
    287   1.4  jmcneill 	    AFI_MSG_REG);
    288   1.1  jmcneill 	struct tegra_pcie_ih *pcie_ih;
    289   1.4  jmcneill 	int rv = 0;
    290   1.1  jmcneill 
    291   1.4  jmcneill 	if (msg & (AFI_MSG_INT0|AFI_MSG_INT1)) {
    292   1.1  jmcneill 		mutex_enter(&sc->sc_lock);
    293   1.1  jmcneill 		const u_int lastgen = sc->sc_intrgen;
    294   1.1  jmcneill 		TAILQ_FOREACH(pcie_ih, &sc->sc_intrs, ih_entry) {
    295   1.1  jmcneill 			int (*callback)(void *) = pcie_ih->ih_callback;
    296   1.1  jmcneill 			void *arg = pcie_ih->ih_arg;
    297  1.16  jmcneill 			const int mpsafe = pcie_ih->ih_mpsafe;
    298   1.1  jmcneill 			mutex_exit(&sc->sc_lock);
    299  1.16  jmcneill 
    300  1.16  jmcneill 			if (!mpsafe)
    301  1.16  jmcneill 				KERNEL_LOCK(1, curlwp);
    302   1.4  jmcneill 			rv += callback(arg);
    303  1.16  jmcneill 			if (!mpsafe)
    304  1.16  jmcneill 				KERNEL_UNLOCK_ONE(curlwp);
    305  1.16  jmcneill 
    306   1.1  jmcneill 			mutex_enter(&sc->sc_lock);
    307   1.1  jmcneill 			if (lastgen != sc->sc_intrgen)
    308   1.1  jmcneill 				break;
    309   1.1  jmcneill 		}
    310   1.1  jmcneill 		mutex_exit(&sc->sc_lock);
    311   1.4  jmcneill 	} else if (msg & (AFI_MSG_PM_PME0|AFI_MSG_PM_PME1)) {
    312   1.4  jmcneill 		device_printf(sc->sc_dev, "PM PME message; AFI_MSG=%08x\n",
    313   1.4  jmcneill 		    msg);
    314   1.4  jmcneill 	} else {
    315   1.4  jmcneill 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_MSG_REG, msg);
    316   1.4  jmcneill 		rv = 1;
    317   1.4  jmcneill 	}
    318   1.4  jmcneill 
    319   1.4  jmcneill 	return rv;
    320   1.4  jmcneill }
    321   1.4  jmcneill 
    322   1.4  jmcneill static int
    323   1.4  jmcneill tegra_pcie_intr(void *priv)
    324   1.4  jmcneill {
    325   1.4  jmcneill 	struct tegra_pcie_softc *sc = priv;
    326  1.11  jakllsch 	int rv;
    327   1.4  jmcneill 
    328   1.4  jmcneill 	const uint32_t code = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
    329   1.4  jmcneill 	    AFI_INTR_CODE_REG);
    330   1.4  jmcneill 	const uint32_t sig = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
    331   1.4  jmcneill 	    AFI_INTR_SIGNATURE_REG);
    332   1.4  jmcneill 
    333   1.4  jmcneill 	switch (__SHIFTOUT(code, AFI_INTR_CODE_INT_CODE)) {
    334   1.4  jmcneill 	case AFI_INTR_CODE_SM_MSG:
    335  1.11  jakllsch 		rv = tegra_pcie_legacy_intr(sc);
    336  1.11  jakllsch 		break;
    337   1.1  jmcneill 	default:
    338   1.1  jmcneill 		device_printf(sc->sc_dev, "intr: code %#x sig %#x\n",
    339   1.1  jmcneill 		    code, sig);
    340  1.11  jakllsch 		rv = 1;
    341  1.11  jakllsch 		break;
    342   1.1  jmcneill 	}
    343  1.11  jakllsch 
    344  1.11  jakllsch 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_INTR_CODE_REG, 0);
    345  1.11  jakllsch 
    346  1.11  jakllsch 	return rv;
    347   1.1  jmcneill }
    348   1.1  jmcneill 
    349   1.1  jmcneill static void
    350  1.20  jmcneill tegra_pcie_enable_clocks(struct tegra_pcie_softc * const sc)
    351  1.20  jmcneill {
    352  1.20  jmcneill 	const char *clock_names[] = { "pex", "afi", "pll_e", "cml" };
    353  1.20  jmcneill 	const char *reset_names[] = { "pex", "afi", "pcie_x" };
    354  1.20  jmcneill 	struct fdtbus_reset *rst;
    355  1.20  jmcneill 	struct clk *clk;
    356  1.20  jmcneill 	int n;
    357  1.20  jmcneill 
    358  1.20  jmcneill 	for (n = 0; n < __arraycount(clock_names); n++) {
    359  1.20  jmcneill 		clk = fdtbus_clock_get(sc->sc_phandle, clock_names[n]);
    360  1.20  jmcneill 		if (clk == NULL || clk_enable(clk) != 0)
    361  1.20  jmcneill 			aprint_error_dev(sc->sc_dev, "couldn't enable clock %s\n",
    362  1.20  jmcneill 			    clock_names[n]);
    363  1.20  jmcneill 	}
    364  1.20  jmcneill 
    365  1.20  jmcneill 	for (n = 0; n < __arraycount(reset_names); n++) {
    366  1.20  jmcneill 		rst = fdtbus_reset_get(sc->sc_phandle, reset_names[n]);
    367  1.20  jmcneill 		if (rst == NULL || fdtbus_reset_deassert(rst) != 0)
    368  1.20  jmcneill 			aprint_error_dev(sc->sc_dev, "couldn't de-assert reset %s\n",
    369  1.20  jmcneill 			    reset_names[n]);
    370  1.20  jmcneill 	}
    371  1.20  jmcneill }
    372  1.20  jmcneill 
    373  1.23     skrll #if 0
    374  1.20  jmcneill static void
    375  1.21  jmcneill tegra_pcie_reset_port(struct tegra_pcie_softc * const sc, int index)
    376  1.21  jmcneill {
    377  1.21  jmcneill 	uint32_t val;
    378  1.21  jmcneill 
    379  1.21  jmcneill 	val = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PEXn_CTRL_REG(index));
    380  1.21  jmcneill 	val &= ~AFI_PEXn_CTRL_RST_L;
    381  1.21  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PEXn_CTRL_REG(index), val);
    382  1.21  jmcneill 
    383  1.21  jmcneill 	delay(2000);
    384  1.21  jmcneill 
    385  1.21  jmcneill 	val = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PEXn_CTRL_REG(index));
    386  1.21  jmcneill 	val |= AFI_PEXn_CTRL_RST_L;
    387  1.21  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PEXn_CTRL_REG(index), val);
    388  1.21  jmcneill }
    389  1.23     skrll #endif
    390  1.21  jmcneill 
    391  1.21  jmcneill static void
    392  1.21  jmcneill tegra_pcie_enable_ports(struct tegra_pcie_softc * const sc)
    393  1.21  jmcneill {
    394  1.22  jmcneill 	struct fdtbus_phy *phy;
    395  1.21  jmcneill 	const u_int *data;
    396  1.22  jmcneill 	int child, len, n;
    397  1.21  jmcneill 	uint32_t val;
    398  1.21  jmcneill 
    399  1.21  jmcneill 	for (child = OF_child(sc->sc_phandle); child; child = OF_peer(child)) {
    400  1.21  jmcneill 		if (!fdtbus_status_okay(child))
    401  1.21  jmcneill 			continue;
    402  1.22  jmcneill 
    403  1.22  jmcneill 		/* Enable PHYs */
    404  1.22  jmcneill 		for (n = 0; (phy = fdtbus_phy_get_index(child, n)) != NULL; n++)
    405  1.22  jmcneill 			if (fdtbus_phy_enable(phy, true) != 0)
    406  1.22  jmcneill 				aprint_error_dev(sc->sc_dev, "couldn't enable %s phy #%d\n",
    407  1.22  jmcneill 				    fdtbus_get_string(child, "name"), n);
    408  1.22  jmcneill 
    409  1.21  jmcneill 		data = fdtbus_get_prop(child, "reg", &len);
    410  1.21  jmcneill 		if (data == NULL || len < 4)
    411  1.21  jmcneill 			continue;
    412  1.21  jmcneill 		const u_int index = ((be32toh(data[0]) >> 11) & 0x1f) - 1;
    413  1.21  jmcneill 
    414  1.21  jmcneill 		val = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PEXn_CTRL_REG(index));
    415  1.21  jmcneill 		val |= AFI_PEXn_CTRL_CLKREQ_EN;
    416  1.21  jmcneill 		val |= AFI_PEXn_CTRL_REFCLK_EN;
    417  1.21  jmcneill 		val |= AFI_PEXn_CTRL_REFCLK_OVERRIDE_EN;
    418  1.21  jmcneill 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PEXn_CTRL_REG(index), val);
    419  1.21  jmcneill 
    420  1.23     skrll #if 0
    421  1.21  jmcneill 		tegra_pcie_reset_port(sc, index);
    422  1.23     skrll #endif
    423  1.22  jmcneill 
    424  1.21  jmcneill 	}
    425  1.21  jmcneill }
    426  1.21  jmcneill 
    427  1.21  jmcneill static void
    428  1.10  jakllsch tegra_pcie_setup(struct tegra_pcie_softc * const sc)
    429  1.10  jakllsch {
    430  1.21  jmcneill 	uint32_t val, cfg, lanes;
    431  1.21  jmcneill 	int child, len;
    432  1.21  jmcneill 	const u_int *data;
    433  1.10  jakllsch 	size_t i;
    434  1.10  jakllsch 
    435  1.21  jmcneill 	/* Enable PLLE control */
    436  1.21  jmcneill 	val = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PLLE_CONTROL_REG);
    437  1.21  jmcneill 	val &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
    438  1.21  jmcneill 	val |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
    439  1.21  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PLLE_CONTROL_REG, val);
    440  1.21  jmcneill 
    441  1.21  jmcneill 	/* Disable PEX clock bias pad power down */
    442  1.21  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PEXBIAS_CTRL_REG, 0);
    443  1.21  jmcneill 
    444  1.21  jmcneill 	/* Configure PCIE mode and enable ports */
    445  1.21  jmcneill 	cfg = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PCIE_CONFIG_REG);
    446  1.21  jmcneill 	cfg |= AFI_PCIE_CONFIG_PCIECn_DISABLE_DEVICE(0);
    447  1.21  jmcneill 	cfg |= AFI_PCIE_CONFIG_PCIECn_DISABLE_DEVICE(1);
    448  1.21  jmcneill 	cfg &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG;
    449  1.21  jmcneill 
    450  1.21  jmcneill 	lanes = 0;
    451  1.21  jmcneill 	for (child = OF_child(sc->sc_phandle); child; child = OF_peer(child)) {
    452  1.21  jmcneill 		if (!fdtbus_status_okay(child))
    453  1.21  jmcneill 			continue;
    454  1.21  jmcneill 		data = fdtbus_get_prop(child, "reg", &len);
    455  1.21  jmcneill 		if (data == NULL || len < 4)
    456  1.21  jmcneill 			continue;
    457  1.21  jmcneill 		const u_int index = ((be32toh(data[0]) >> 11) & 0x1f) - 1;
    458  1.21  jmcneill 		if (of_getprop_uint32(child, "nvidia,num-lanes", &val) != 0)
    459  1.21  jmcneill 			continue;
    460  1.21  jmcneill 		lanes |= (val << (index << 3));
    461  1.21  jmcneill 		cfg &= ~AFI_PCIE_CONFIG_PCIECn_DISABLE_DEVICE(index);
    462  1.21  jmcneill 	}
    463  1.21  jmcneill 
    464  1.21  jmcneill 	switch (lanes) {
    465  1.21  jmcneill 	case 0x0104:
    466  1.21  jmcneill 		aprint_normal_dev(sc->sc_dev, "lane config: x4 x1\n");
    467  1.21  jmcneill 		cfg |= __SHIFTIN(AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_4_1,
    468  1.21  jmcneill 				 AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG);
    469  1.21  jmcneill 		break;
    470  1.21  jmcneill 	case 0x0102:
    471  1.21  jmcneill 		aprint_normal_dev(sc->sc_dev, "lane config: x2 x1\n");
    472  1.21  jmcneill 		cfg |= __SHIFTIN(AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_2_1,
    473  1.21  jmcneill 				 AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG);
    474  1.21  jmcneill 		break;
    475  1.21  jmcneill 	}
    476  1.21  jmcneill 
    477  1.21  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PCIE_CONFIG_REG, cfg);
    478  1.21  jmcneill 
    479  1.21  jmcneill 	/* Configure refclk pad */
    480  1.33   thorpej 	if (sc->sc_type == TEGRA_PCIE_124) {
    481  1.33   thorpej 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_pads,
    482  1.33   thorpej 		    PADS_REFCLK_CFG0_REG, 0x44ac44ac);
    483  1.33   thorpej 	}
    484  1.33   thorpej 	if (sc->sc_type == TEGRA_PCIE_210) {
    485  1.33   thorpej 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_pads,
    486  1.33   thorpej 		    PADS_REFCLK_CFG0_REG, 0x90b890b8);
    487  1.33   thorpej 	}
    488  1.21  jmcneill 
    489  1.10  jakllsch 	/*
    490  1.10  jakllsch 	 * Map PCI address spaces into ARM address space via
    491  1.10  jakllsch 	 * HyperTransport-like "FPCI".
    492  1.10  jakllsch 	 */
    493  1.10  jakllsch 	static const struct { uint32_t size, base, fpci; } pcie_init_table[] = {
    494  1.10  jakllsch 		/*
    495  1.10  jakllsch 		 * === BEWARE ===
    496  1.10  jakllsch 		 *
    497  1.10  jakllsch 		 * We depend on our TEGRA_PCIE_IO window overlaping the
    498  1.10  jakllsch 		 * TEGRA_PCIE_A1 window to allow us to use the same
    499  1.10  jakllsch 		 * bus_space_tag for both PCI IO and Memory spaces.
    500  1.10  jakllsch 		 *
    501  1.10  jakllsch 		 * 0xfdfc000000-0xfdfdffffff is the FPCI/HyperTransport
    502  1.10  jakllsch 		 * mapping for 0x0000000-0x1ffffff of PCI IO space.
    503  1.10  jakllsch 		 */
    504  1.10  jakllsch 		{ TEGRA_PCIE_IO_SIZE >> 12, TEGRA_PCIE_IO_BASE,
    505  1.10  jakllsch 		  (0xfdfc000000 + TEGRA_PCIE_IO_BASE) >> 8 | 0, },
    506  1.10  jakllsch 
    507  1.10  jakllsch 		/* HyperTransport Technology Type 1 Address Format */
    508  1.10  jakllsch 		{ TEGRA_PCIE_CONF_SIZE >> 12, TEGRA_PCIE_CONF_BASE,
    509  1.10  jakllsch 		  0xfdff000000 >> 8 | 0, },
    510  1.10  jakllsch 
    511  1.10  jakllsch 		/* 1:1 MMIO mapping */
    512  1.10  jakllsch 		{ TEGRA_PCIE_MEM_SIZE >> 12, TEGRA_PCIE_MEM_BASE,
    513  1.10  jakllsch 		  TEGRA_PCIE_MEM_BASE >> 8 | 1, },
    514  1.10  jakllsch 
    515  1.10  jakllsch 		/* Extended HyperTransport Technology Type 1 Address Format */
    516  1.10  jakllsch 		{ TEGRA_PCIE_EXTC_SIZE >> 12, TEGRA_PCIE_EXTC_BASE,
    517  1.10  jakllsch 		  0xfe10000000 >> 8 | 0, },
    518  1.10  jakllsch 
    519  1.10  jakllsch 		/* 1:1 prefetchable MMIO mapping */
    520  1.10  jakllsch 		{ TEGRA_PCIE_PMEM_SIZE >> 12, TEGRA_PCIE_PMEM_BASE,
    521  1.10  jakllsch 		  TEGRA_PCIE_PMEM_BASE >> 8 | 1, },
    522  1.10  jakllsch 	};
    523  1.10  jakllsch 
    524  1.10  jakllsch 	for (i = 0; i < AFI_AXI_NBAR; i++) {
    525  1.10  jakllsch 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    526  1.10  jakllsch 		    AFI_AXI_BARi_SZ(i), 0);
    527  1.10  jakllsch 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    528  1.10  jakllsch 		    AFI_AXI_BARi_START(i), 0);
    529  1.10  jakllsch 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    530  1.10  jakllsch 		    AFI_FPCI_BARi(i), 0);
    531  1.10  jakllsch 	}
    532  1.10  jakllsch 
    533  1.10  jakllsch 	for (i = 0; i < __arraycount(pcie_init_table); i++) {
    534  1.10  jakllsch 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    535  1.10  jakllsch 		    AFI_AXI_BARi_START(i), pcie_init_table[i].base);
    536  1.10  jakllsch 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    537  1.10  jakllsch 		    AFI_FPCI_BARi(i), pcie_init_table[i].fpci);
    538  1.10  jakllsch 		bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    539  1.10  jakllsch 		    AFI_AXI_BARi_SZ(i), pcie_init_table[i].size);
    540  1.10  jakllsch 	}
    541  1.10  jakllsch }
    542  1.10  jakllsch 
    543  1.10  jakllsch static void
    544   1.1  jmcneill tegra_pcie_enable(struct tegra_pcie_softc *sc)
    545   1.1  jmcneill {
    546   1.4  jmcneill 	/* disable MSI */
    547   1.4  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    548   1.4  jmcneill 	    AFI_MSI_BAR_SZ_REG, 0);
    549   1.4  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    550   1.4  jmcneill 	    AFI_MSI_FPCI_BAR_ST_REG, 0);
    551   1.4  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    552   1.4  jmcneill 	    AFI_MSI_AXI_BAR_ST_REG, 0);
    553   1.4  jmcneill 
    554   1.1  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    555   1.1  jmcneill 	    AFI_SM_INTR_ENABLE_REG, 0xffffffff);
    556   1.1  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    557   1.1  jmcneill 	    AFI_AFI_INTR_ENABLE_REG, 0);
    558   1.1  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_INTR_CODE_REG, 0);
    559   1.1  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
    560   1.1  jmcneill 	    AFI_INTR_MASK_REG, AFI_INTR_MASK_INT);
    561   1.1  jmcneill }
    562   1.1  jmcneill 
    563  1.12  jakllsch static void
    564  1.12  jakllsch tegra_pcie_conf_frag_map(struct tegra_pcie_softc * const sc, uint bus,
    565  1.12  jakllsch     uint frg)
    566  1.12  jakllsch {
    567  1.12  jakllsch 	bus_addr_t a;
    568  1.12  jakllsch 
    569  1.12  jakllsch 	KASSERT(bus >= 1);
    570  1.12  jakllsch 	KASSERT(bus < TEGRA_PCIE_NBUS);
    571  1.12  jakllsch 	KASSERT(frg < TEGRA_PCIE_ECFB);
    572  1.12  jakllsch 
    573  1.12  jakllsch 	if (sc->sc_bsh_extc[bus-1][frg] != 0) {
    574  1.12  jakllsch 		device_printf(sc->sc_dev, "bus %u fragment %#x already "
    575  1.12  jakllsch 		    "mapped\n", bus, frg);
    576  1.12  jakllsch 		return;
    577  1.12  jakllsch 	}
    578  1.12  jakllsch 
    579  1.12  jakllsch 	a = TEGRA_PCIE_EXTC_BASE + (bus << 16) + (frg << 24);
    580  1.27  jmcneill 	if (bus_space_map(sc->sc_bst, a, 1 << 16,
    581  1.27  jmcneill 	    _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED,
    582  1.12  jakllsch 	    &sc->sc_bsh_extc[bus-1][frg]) != 0)
    583  1.12  jakllsch 		device_printf(sc->sc_dev, "couldn't map PCIE "
    584  1.12  jakllsch 		    "configuration for bus %u fragment %#x", bus, frg);
    585  1.12  jakllsch }
    586  1.12  jakllsch 
    587  1.12  jakllsch /* map non-non-extended configuration space for full bus range */
    588  1.12  jakllsch static void
    589  1.12  jakllsch tegra_pcie_conf_map_bus(struct tegra_pcie_softc * const sc, uint bus)
    590  1.12  jakllsch {
    591  1.12  jakllsch 	uint i;
    592  1.12  jakllsch 
    593  1.12  jakllsch 	for (i = 1; i < TEGRA_PCIE_ECFB; i++) {
    594  1.12  jakllsch 		tegra_pcie_conf_frag_map(sc, bus, i);
    595  1.12  jakllsch 	}
    596  1.12  jakllsch }
    597  1.12  jakllsch 
    598  1.12  jakllsch /* map non-extended configuration space for full bus range */
    599  1.12  jakllsch static void
    600  1.12  jakllsch tegra_pcie_conf_map_buses(struct tegra_pcie_softc * const sc)
    601  1.12  jakllsch {
    602  1.12  jakllsch 	uint b;
    603  1.12  jakllsch 
    604  1.12  jakllsch 	for (b = 1; b < TEGRA_PCIE_NBUS; b++) {
    605  1.12  jakllsch 		tegra_pcie_conf_frag_map(sc, b, 0);
    606  1.12  jakllsch 	}
    607  1.12  jakllsch }
    608  1.12  jakllsch 
    609   1.1  jmcneill void
    610   1.1  jmcneill tegra_pcie_init(pci_chipset_tag_t pc, void *priv)
    611   1.1  jmcneill {
    612   1.1  jmcneill 	pc->pc_conf_v = priv;
    613   1.1  jmcneill 	pc->pc_attach_hook = tegra_pcie_attach_hook;
    614   1.1  jmcneill 	pc->pc_bus_maxdevs = tegra_pcie_bus_maxdevs;
    615   1.1  jmcneill 	pc->pc_make_tag = tegra_pcie_make_tag;
    616   1.1  jmcneill 	pc->pc_decompose_tag = tegra_pcie_decompose_tag;
    617   1.1  jmcneill 	pc->pc_conf_read = tegra_pcie_conf_read;
    618   1.1  jmcneill 	pc->pc_conf_write = tegra_pcie_conf_write;
    619   1.1  jmcneill 	pc->pc_conf_hook = tegra_pcie_conf_hook;
    620   1.1  jmcneill 	pc->pc_conf_interrupt = tegra_pcie_conf_interrupt;
    621   1.1  jmcneill 
    622   1.1  jmcneill 	pc->pc_intr_v = priv;
    623   1.1  jmcneill 	pc->pc_intr_map = tegra_pcie_intr_map;
    624   1.1  jmcneill 	pc->pc_intr_string = tegra_pcie_intr_string;
    625   1.1  jmcneill 	pc->pc_intr_evcnt = tegra_pcie_intr_evcnt;
    626  1.16  jmcneill 	pc->pc_intr_setattr = tegra_pcie_intr_setattr;
    627   1.1  jmcneill 	pc->pc_intr_establish = tegra_pcie_intr_establish;
    628   1.1  jmcneill 	pc->pc_intr_disestablish = tegra_pcie_intr_disestablish;
    629   1.1  jmcneill }
    630   1.1  jmcneill 
    631   1.1  jmcneill static void
    632   1.1  jmcneill tegra_pcie_attach_hook(device_t parent, device_t self,
    633   1.1  jmcneill     struct pcibus_attach_args *pba)
    634   1.1  jmcneill {
    635  1.12  jakllsch 	const pci_chipset_tag_t pc = pba->pba_pc;
    636  1.12  jakllsch 	struct tegra_pcie_softc * const sc = pc->pc_conf_v;
    637  1.12  jakllsch 
    638  1.12  jakllsch 	if (pba->pba_bus >= 1) {
    639  1.12  jakllsch 		tegra_pcie_conf_map_bus(sc, pba->pba_bus);
    640  1.12  jakllsch 	}
    641   1.1  jmcneill }
    642   1.1  jmcneill 
    643   1.1  jmcneill static int
    644   1.1  jmcneill tegra_pcie_bus_maxdevs(void *v, int busno)
    645   1.1  jmcneill {
    646   1.1  jmcneill 	return busno == 0 ? 2 : 32;
    647   1.1  jmcneill }
    648   1.1  jmcneill 
    649   1.1  jmcneill static pcitag_t
    650   1.1  jmcneill tegra_pcie_make_tag(void *v, int b, int d, int f)
    651   1.1  jmcneill {
    652   1.1  jmcneill 	return (b << 16) | (d << 11) | (f << 8);
    653   1.1  jmcneill }
    654   1.1  jmcneill 
    655   1.1  jmcneill static void
    656   1.1  jmcneill tegra_pcie_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
    657   1.1  jmcneill {
    658   1.1  jmcneill 	if (bp)
    659   1.1  jmcneill 		*bp = (tag >> 16) & 0xff;
    660   1.1  jmcneill 	if (dp)
    661   1.1  jmcneill 		*dp = (tag >> 11) & 0x1f;
    662   1.1  jmcneill 	if (fp)
    663   1.1  jmcneill 		*fp = (tag >> 8) & 0x7;
    664   1.1  jmcneill }
    665   1.1  jmcneill 
    666   1.1  jmcneill static pcireg_t
    667   1.1  jmcneill tegra_pcie_conf_read(void *v, pcitag_t tag, int offset)
    668   1.1  jmcneill {
    669   1.1  jmcneill 	struct tegra_pcie_softc *sc = v;
    670   1.1  jmcneill 	bus_space_handle_t bsh;
    671   1.1  jmcneill 	int b, d, f;
    672   1.1  jmcneill 	u_int reg;
    673   1.1  jmcneill 
    674   1.3   msaitoh 	if ((unsigned int)offset >= PCI_EXTCONF_SIZE)
    675   1.3   msaitoh 		return (pcireg_t) -1;
    676   1.3   msaitoh 
    677   1.1  jmcneill 	tegra_pcie_decompose_tag(v, tag, &b, &d, &f);
    678   1.1  jmcneill 
    679  1.12  jakllsch 	if (b >= TEGRA_PCIE_NBUS)
    680  1.12  jakllsch 		return (pcireg_t) -1;
    681  1.12  jakllsch 
    682   1.1  jmcneill 	if (b == 0) {
    683   1.6  jakllsch 		if (d >= 2 || f != 0)
    684   1.6  jakllsch 			return (pcireg_t) -1;
    685   1.1  jmcneill 		reg = d * 0x1000 + offset;
    686   1.9  jakllsch 		bsh = sc->sc_bsh_rpconf;
    687   1.1  jmcneill 	} else {
    688  1.12  jakllsch 		reg = (d << 11) | (f << 8) | (offset & 0xff);
    689  1.12  jakllsch 		bsh = sc->sc_bsh_extc[b-1][(offset >> 8) & 0xf];
    690  1.12  jakllsch 		if (bsh == 0)
    691   1.7  jakllsch 			return (pcireg_t) -1;
    692   1.1  jmcneill 	}
    693   1.1  jmcneill 
    694   1.1  jmcneill 	return bus_space_read_4(sc->sc_bst, bsh, reg);
    695   1.1  jmcneill }
    696   1.1  jmcneill 
    697   1.1  jmcneill static void
    698   1.1  jmcneill tegra_pcie_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
    699   1.1  jmcneill {
    700   1.1  jmcneill 	struct tegra_pcie_softc *sc = v;
    701   1.1  jmcneill 	bus_space_handle_t bsh;
    702   1.1  jmcneill 	int b, d, f;
    703   1.1  jmcneill 	u_int reg;
    704   1.1  jmcneill 
    705   1.3   msaitoh 	if ((unsigned int)offset >= PCI_EXTCONF_SIZE)
    706   1.3   msaitoh 		return;
    707   1.3   msaitoh 
    708   1.1  jmcneill 	tegra_pcie_decompose_tag(v, tag, &b, &d, &f);
    709   1.1  jmcneill 
    710  1.12  jakllsch 	if (b >= TEGRA_PCIE_NBUS)
    711  1.12  jakllsch 		return;
    712  1.12  jakllsch 
    713   1.1  jmcneill 	if (b == 0) {
    714   1.6  jakllsch 		if (d >= 2 || f != 0)
    715   1.6  jakllsch 			return;
    716   1.1  jmcneill 		reg = d * 0x1000 + offset;
    717   1.9  jakllsch 		bsh = sc->sc_bsh_rpconf;
    718   1.1  jmcneill 	} else {
    719  1.12  jakllsch 		reg = (d << 11) | (f << 8) | (offset & 0xff);
    720  1.12  jakllsch 		bsh = sc->sc_bsh_extc[b-1][(offset >> 8) & 0xf];
    721  1.12  jakllsch 		if (bsh == 0)
    722   1.7  jakllsch 			return;
    723   1.1  jmcneill 	}
    724   1.1  jmcneill 
    725   1.1  jmcneill 	bus_space_write_4(sc->sc_bst, bsh, reg, val);
    726   1.1  jmcneill }
    727   1.1  jmcneill 
    728   1.1  jmcneill static int
    729   1.1  jmcneill tegra_pcie_conf_hook(void *v, int b, int d, int f, pcireg_t id)
    730   1.1  jmcneill {
    731  1.15  jakllsch 	return PCI_CONF_DEFAULT & ~PCI_CONF_ENABLE_BM;
    732   1.1  jmcneill }
    733   1.1  jmcneill 
    734   1.1  jmcneill static void
    735   1.1  jmcneill tegra_pcie_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz,
    736   1.1  jmcneill     int *ilinep)
    737   1.1  jmcneill {
    738  1.14  jmcneill 	*ilinep = 5;
    739   1.1  jmcneill }
    740   1.1  jmcneill 
    741   1.1  jmcneill static int
    742   1.1  jmcneill tegra_pcie_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
    743   1.1  jmcneill {
    744   1.1  jmcneill 	if (pa->pa_intrpin == 0)
    745   1.1  jmcneill 		return EINVAL;
    746   1.1  jmcneill 	*ih = pa->pa_intrpin;
    747   1.1  jmcneill 	return 0;
    748   1.1  jmcneill }
    749   1.5  jakllsch 
    750   1.1  jmcneill static const char *
    751   1.1  jmcneill tegra_pcie_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
    752   1.1  jmcneill {
    753   1.1  jmcneill 	struct tegra_pcie_softc *sc = v;
    754   1.1  jmcneill 
    755   1.1  jmcneill 	if (ih == PCI_INTERRUPT_PIN_NONE)
    756   1.1  jmcneill 		return NULL;
    757   1.1  jmcneill 
    758  1.14  jmcneill 	if (!fdtbus_intr_str(sc->sc_phandle, 0, buf, len))
    759  1.14  jmcneill 		return NULL;
    760  1.14  jmcneill 
    761   1.1  jmcneill 	return buf;
    762   1.1  jmcneill }
    763   1.1  jmcneill 
    764   1.1  jmcneill const struct evcnt *
    765   1.1  jmcneill tegra_pcie_intr_evcnt(void *v, pci_intr_handle_t ih)
    766   1.1  jmcneill {
    767   1.1  jmcneill 	return NULL;
    768   1.1  jmcneill }
    769   1.1  jmcneill 
    770  1.16  jmcneill static int
    771  1.16  jmcneill tegra_pcie_intr_setattr(void *v, pci_intr_handle_t *ih, int attr, uint64_t data)
    772  1.16  jmcneill {
    773  1.16  jmcneill 	switch (attr) {
    774  1.16  jmcneill 	case PCI_INTR_MPSAFE:
    775  1.17  jmcneill 		if (data)
    776  1.17  jmcneill 			*ih |= IH_MPSAFE;
    777  1.17  jmcneill 		else
    778  1.17  jmcneill 			*ih &= ~IH_MPSAFE;
    779  1.16  jmcneill 		return 0;
    780  1.16  jmcneill 	default:
    781  1.16  jmcneill 		return ENODEV;
    782  1.16  jmcneill 	}
    783  1.16  jmcneill }
    784  1.16  jmcneill 
    785   1.1  jmcneill static void *
    786   1.1  jmcneill tegra_pcie_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
    787  1.25  jmcneill     int (*callback)(void *), void *arg, const char *xname)
    788   1.1  jmcneill {
    789   1.1  jmcneill 	struct tegra_pcie_softc *sc = v;
    790   1.1  jmcneill 	struct tegra_pcie_ih *pcie_ih;
    791   1.1  jmcneill 
    792   1.1  jmcneill 	if (ih == 0)
    793   1.1  jmcneill 		return NULL;
    794   1.1  jmcneill 
    795   1.1  jmcneill 	pcie_ih = kmem_alloc(sizeof(*pcie_ih), KM_SLEEP);
    796   1.1  jmcneill 	pcie_ih->ih_callback = callback;
    797   1.1  jmcneill 	pcie_ih->ih_arg = arg;
    798   1.1  jmcneill 	pcie_ih->ih_ipl = ipl;
    799  1.17  jmcneill 	pcie_ih->ih_mpsafe = (ih & IH_MPSAFE) != 0;
    800   1.1  jmcneill 
    801   1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    802   1.1  jmcneill 	TAILQ_INSERT_TAIL(&sc->sc_intrs, pcie_ih, ih_entry);
    803   1.1  jmcneill 	sc->sc_intrgen++;
    804   1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    805   1.1  jmcneill 
    806   1.1  jmcneill 	return pcie_ih;
    807   1.1  jmcneill }
    808   1.1  jmcneill 
    809   1.1  jmcneill static void
    810   1.1  jmcneill tegra_pcie_intr_disestablish(void *v, void *vih)
    811   1.1  jmcneill {
    812   1.1  jmcneill 	struct tegra_pcie_softc *sc = v;
    813   1.1  jmcneill 	struct tegra_pcie_ih *pcie_ih = vih;
    814   1.1  jmcneill 
    815   1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    816   1.1  jmcneill 	TAILQ_REMOVE(&sc->sc_intrs, pcie_ih, ih_entry);
    817   1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    818   1.1  jmcneill 
    819   1.1  jmcneill 	kmem_free(pcie_ih, sizeof(*pcie_ih));
    820   1.1  jmcneill }
    821