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