1 /*- 2 * Copyright (c) 2016 Michal Meloun <mmel@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 /* 31 * Nvidia Integrated PCI/PCI-Express controller driver. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/module.h> 40 #include <sys/mutex.h> 41 #include <sys/queue.h> 42 #include <sys/bus.h> 43 #include <sys/rman.h> 44 #include <sys/endian.h> 45 46 #include <machine/intr.h> 47 48 #include <vm/vm.h> 49 #include <vm/pmap.h> 50 51 #include <dev/extres/clk/clk.h> 52 #include <dev/extres/hwreset/hwreset.h> 53 #include <dev/extres/phy/phy.h> 54 #include <dev/extres/regulator/regulator.h> 55 #include <dev/fdt/fdt_common.h> 56 #include <dev/ofw/ofw_bus.h> 57 #include <dev/ofw/ofw_bus_subr.h> 58 #include <dev/ofw/ofw_pci.h> 59 #include <dev/pci/pcivar.h> 60 #include <dev/pci/pcireg.h> 61 #include <dev/pci/pcib_private.h> 62 63 #include <machine/devmap.h> 64 #include <machine/resource.h> 65 #include <machine/bus.h> 66 67 #include "ofw_bus_if.h" 68 #include "pcib_if.h" 69 70 #include <arm/nvidia/tegra_pmc.h> 71 72 /* --- Move to ofw_pci.c/.h ----------------------- */ 73 74 struct tegra_pci_range { 75 /* parsed phys.hi */ 76 int nonrelocatable; 77 int prefetchable; 78 int aliased; 79 int space_code; /* In native format (not shifted)*/ 80 int bus; 81 int device; 82 int function; 83 int reg; 84 pci_addr_t pci_addr; /* PCI Address */ 85 bus_addr_t host_addr; /* Host bus address*/ 86 bus_size_t size; /* Range size */ 87 }; 88 89 static int 90 tegra_pci_get_ranges(phandle_t node, struct tegra_pci_range **ranges) 91 { 92 int host_address_cells, pci_address_cells, size_cells; 93 cell_t *base_ranges; 94 ssize_t nbase_ranges; 95 int nranges; 96 int i, j, k; 97 uint32_t flags; 98 uint64_t tmp; 99 100 host_address_cells = 1; 101 pci_address_cells = 3; 102 size_cells = 2; 103 OF_getencprop(OF_parent(node), "#address-cells", &host_address_cells, 104 sizeof(host_address_cells)); 105 OF_getencprop(node, "#address-cells", &pci_address_cells, 106 sizeof(pci_address_cells)); 107 OF_getencprop(node, "#size-cells", &size_cells, sizeof(size_cells)); 108 109 nbase_ranges = OF_getproplen(node, "ranges"); 110 if (nbase_ranges <= 0) 111 return (-1); 112 nranges = nbase_ranges / sizeof(cell_t) / 113 (pci_address_cells + host_address_cells + size_cells); 114 115 *ranges = malloc(nranges * sizeof(struct tegra_pci_range), 116 M_DEVBUF, M_WAITOK); 117 base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK); 118 OF_getencprop(node, "ranges", base_ranges, nbase_ranges); 119 120 for (i = 0, j = 0; i < nranges; i++) { 121 flags = base_ranges[j++]; 122 (*ranges)[i].nonrelocatable = 123 flags & OFW_PCI_PHYS_HI_NONRELOCATABLE ? 1 : 0; 124 (*ranges)[i].prefetchable = 125 flags & OFW_PCI_PHYS_HI_PREFETCHABLE ? 1 : 0; 126 (*ranges)[i].aliased = 127 flags & OFW_PCI_PHYS_HI_ALIASED ? 1 : 0; 128 (*ranges)[i].space_code = flags & OFW_PCI_PHYS_HI_SPACEMASK; 129 (*ranges)[i].bus = OFW_PCI_PHYS_HI_BUS(flags); 130 (*ranges)[i].device = OFW_PCI_PHYS_HI_DEVICE(flags); 131 (*ranges)[i].function = OFW_PCI_PHYS_HI_FUNCTION(flags); 132 (*ranges)[i].reg = flags & OFW_PCI_PHYS_HI_REGISTERMASK; 133 134 tmp = 0; 135 for (k = 0; k < pci_address_cells - 1; k++) { 136 tmp <<= 32; 137 tmp |= base_ranges[j++]; 138 } 139 (*ranges)[i].pci_addr = (pci_addr_t)tmp; 140 141 tmp = 0; 142 for (k = 0; k < host_address_cells; k++) { 143 tmp <<= 32; 144 tmp |= base_ranges[j++]; 145 } 146 (*ranges)[i].host_addr = (bus_addr_t)tmp; 147 tmp = 0; 148 149 for (k = 0; k < size_cells; k++) { 150 tmp <<= 32; 151 tmp |= base_ranges[j++]; 152 } 153 (*ranges)[i].size = (bus_size_t)tmp; 154 } 155 156 free(base_ranges, M_DEVBUF); 157 return (nranges); 158 } 159 160 /* -------------------------------------------------------------------------- */ 161 #define AFI_AXI_BAR0_SZ 0x000 162 #define AFI_AXI_BAR1_SZ 0x004 163 #define AFI_AXI_BAR2_SZ 0x008 164 #define AFI_AXI_BAR3_SZ 0x00c 165 #define AFI_AXI_BAR4_SZ 0x010 166 #define AFI_AXI_BAR5_SZ 0x014 167 #define AFI_AXI_BAR0_START 0x018 168 #define AFI_AXI_BAR1_START 0x01c 169 #define AFI_AXI_BAR2_START 0x020 170 #define AFI_AXI_BAR3_START 0x024 171 #define AFI_AXI_BAR4_START 0x028 172 #define AFI_AXI_BAR5_START 0x02c 173 #define AFI_FPCI_BAR0 0x030 174 #define AFI_FPCI_BAR1 0x034 175 #define AFI_FPCI_BAR2 0x038 176 #define AFI_FPCI_BAR3 0x03c 177 #define AFI_FPCI_BAR4 0x040 178 #define AFI_FPCI_BAR5 0x044 179 #define AFI_MSI_BAR_SZ 0x060 180 #define AFI_MSI_FPCI_BAR_ST 0x064 181 #define AFI_MSI_AXI_BAR_ST 0x068 182 183 184 #define AFI_AXI_BAR6_SZ 0x134 185 #define AFI_AXI_BAR7_SZ 0x138 186 #define AFI_AXI_BAR8_SZ 0x13c 187 #define AFI_AXI_BAR6_START 0x140 188 #define AFI_AXI_BAR7_START 0x144 189 #define AFI_AXI_BAR8_START 0x148 190 #define AFI_FPCI_BAR6 0x14c 191 #define AFI_FPCI_BAR7 0x150 192 #define AFI_FPCI_BAR8 0x154 193 194 #define AFI_CONFIGURATION 0x0ac 195 #define AFI_CONFIGURATION_EN_FPCI (1 << 0) 196 197 #define AFI_FPCI_ERROR_MASKS 0x0b0 198 #define AFI_INTR_MASK 0x0b4 199 #define AFI_INTR_MASK_MSI_MASK (1 << 8) 200 #define AFI_INTR_MASK_INT_MASK (1 << 0) 201 202 #define AFI_INTR_CODE 0x0b8 203 #define AFI_INTR_CODE_MASK 0xf 204 #define AFI_INTR_CODE_INT_CODE_INI_SLVERR 1 205 #define AFI_INTR_CODE_INT_CODE_INI_DECERR 2 206 #define AFI_INTR_CODE_INT_CODE_TGT_SLVERR 3 207 #define AFI_INTR_CODE_INT_CODE_TGT_DECERR 4 208 #define AFI_INTR_CODE_INT_CODE_TGT_WRERR 5 209 #define AFI_INTR_CODE_INT_CODE_SM_MSG 6 210 #define AFI_INTR_CODE_INT_CODE_DFPCI_DECERR 7 211 #define AFI_INTR_CODE_INT_CODE_AXI_DECERR 8 212 #define AFI_INTR_CODE_INT_CODE_FPCI_TIMEOUT 9 213 #define AFI_INTR_CODE_INT_CODE_PE_PRSNT_SENSE 10 214 #define AFI_INTR_CODE_INT_CODE_PE_CLKREQ_SENSE 11 215 #define AFI_INTR_CODE_INT_CODE_CLKCLAMP_SENSE 12 216 #define AFI_INTR_CODE_INT_CODE_RDY4PD_SENSE 13 217 #define AFI_INTR_CODE_INT_CODE_P2P_ERROR 14 218 219 220 #define AFI_INTR_SIGNATURE 0x0bc 221 #define AFI_UPPER_FPCI_ADDRESS 0x0c0 222 #define AFI_SM_INTR_ENABLE 0x0c4 223 #define AFI_SM_INTR_RP_DEASSERT (1 << 14) 224 #define AFI_SM_INTR_RP_ASSERT (1 << 13) 225 #define AFI_SM_INTR_HOTPLUG (1 << 12) 226 #define AFI_SM_INTR_PME (1 << 11) 227 #define AFI_SM_INTR_FATAL_ERROR (1 << 10) 228 #define AFI_SM_INTR_UNCORR_ERROR (1 << 9) 229 #define AFI_SM_INTR_CORR_ERROR (1 << 8) 230 #define AFI_SM_INTR_INTD_DEASSERT (1 << 7) 231 #define AFI_SM_INTR_INTC_DEASSERT (1 << 6) 232 #define AFI_SM_INTR_INTB_DEASSERT (1 << 5) 233 #define AFI_SM_INTR_INTA_DEASSERT (1 << 4) 234 #define AFI_SM_INTR_INTD_ASSERT (1 << 3) 235 #define AFI_SM_INTR_INTC_ASSERT (1 << 2) 236 #define AFI_SM_INTR_INTB_ASSERT (1 << 1) 237 #define AFI_SM_INTR_INTA_ASSERT (1 << 0) 238 239 #define AFI_AFI_INTR_ENABLE 0x0c8 240 #define AFI_AFI_INTR_ENABLE_CODE(code) (1 << (code)) 241 242 #define AFI_PCIE_CONFIG 0x0f8 243 #define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1)) 244 #define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0x6 245 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20) 246 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_XBAR2_1 (0x0 << 20) 247 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_XBAR4_1 (0x1 << 20) 248 249 #define AFI_FUSE 0x104 250 #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2) 251 252 #define AFI_PEX0_CTRL 0x110 253 #define AFI_PEX1_CTRL 0x118 254 #define AFI_PEX2_CTRL 0x128 255 #define AFI_PEX_CTRL_OVERRIDE_EN (1 << 4) 256 #define AFI_PEX_CTRL_REFCLK_EN (1 << 3) 257 #define AFI_PEX_CTRL_CLKREQ_EN (1 << 1) 258 #define AFI_PEX_CTRL_RST_L (1 << 0) 259 260 #define AFI_AXI_BAR6_SZ 0x134 261 #define AFI_AXI_BAR7_SZ 0x138 262 #define AFI_AXI_BAR8_SZ 0x13c 263 #define AFI_AXI_BAR6_START 0x140 264 #define AFI_AXI_BAR7_START 0x144 265 #define AFI_AXI_BAR8_START 0x148 266 #define AFI_FPCI_BAR6 0x14c 267 #define AFI_FPCI_BAR7 0x150 268 #define AFI_FPCI_BAR8 0x154 269 #define AFI_PLLE_CONTROL 0x160 270 #define AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9) 271 #define AFI_PLLE_CONTROL_BYPASS_PCIE2PLLE_CONTROL (1 << 8) 272 #define AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1) 273 #define AFI_PLLE_CONTROL_PCIE2PLLE_CONTROL_EN (1 << 0) 274 275 #define AFI_PEXBIAS_CTRL 0x168 276 277 /* FPCI Address space */ 278 #define FPCI_MAP_IO 0xfdfc000000ULL 279 #define FPCI_MAP_TYPE0_CONFIG 0xfdfc000000ULL 280 #define FPCI_MAP_TYPE1_CONFIG 0xfdff000000ULL 281 #define FPCI_MAP_EXT_TYPE0_CONFIG 0xfe00000000ULL 282 #define FPCI_MAP_EXT_TYPE1_CONFIG 0xfe10000000ULL 283 284 /* Configuration space */ 285 #define RP_VEND_XP 0x00000F00 286 #define RP_VEND_XP_DL_UP (1 << 30) 287 288 #define RP_PRIV_MISC 0x00000FE0 289 #define RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xE << 0) 290 #define RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xF << 0) 291 292 #define RP_LINK_CONTROL_STATUS 0x00000090 293 #define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000 294 #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000 295 296 #define TEGRA_PCIE_LINKUP_TIMEOUT 200 297 298 #define DEBUG 299 #ifdef DEBUG 300 #define debugf(fmt, args...) do { printf(fmt,##args); } while (0) 301 #else 302 #define debugf(fmt, args...) 303 #endif 304 305 /* 306 * Configuration space format: 307 * [27:24] extended register 308 * [23:16] bus 309 * [15:11] slot (device) 310 * [10: 8] function 311 * [ 7: 0] register 312 */ 313 #define PCI_CFG_EXT_REG(reg) ((((reg) >> 8) & 0x0f) << 24) 314 #define PCI_CFG_BUS(bus) (((bus) & 0xff) << 16) 315 #define PCI_CFG_DEV(dev) (((dev) & 0x1f) << 11) 316 #define PCI_CFG_FUN(fun) (((fun) & 0x07) << 8) 317 #define PCI_CFG_BASE_REG(reg) ((reg) & 0xff) 318 319 #define PADS_WR4(_sc, _r, _v) bus_write_4((_sc)-pads_mem_res, (_r), (_v)) 320 #define PADS_RD4(_sc, _r) bus_read_4((_sc)->pads_mem_res, (_r)) 321 #define AFI_WR4(_sc, _r, _v) bus_write_4((_sc)->afi_mem_res, (_r), (_v)) 322 #define AFI_RD4(_sc, _r) bus_read_4((_sc)->afi_mem_res, (_r)) 323 324 static struct { 325 bus_size_t axi_start; 326 bus_size_t fpci_start; 327 bus_size_t size; 328 } bars[] = { 329 {AFI_AXI_BAR0_START, AFI_FPCI_BAR0, AFI_AXI_BAR0_SZ}, /* BAR 0 */ 330 {AFI_AXI_BAR1_START, AFI_FPCI_BAR1, AFI_AXI_BAR1_SZ}, /* BAR 1 */ 331 {AFI_AXI_BAR2_START, AFI_FPCI_BAR2, AFI_AXI_BAR2_SZ}, /* BAR 2 */ 332 {AFI_AXI_BAR3_START, AFI_FPCI_BAR3, AFI_AXI_BAR3_SZ}, /* BAR 3 */ 333 {AFI_AXI_BAR4_START, AFI_FPCI_BAR4, AFI_AXI_BAR4_SZ}, /* BAR 4 */ 334 {AFI_AXI_BAR5_START, AFI_FPCI_BAR5, AFI_AXI_BAR5_SZ}, /* BAR 5 */ 335 {AFI_AXI_BAR6_START, AFI_FPCI_BAR6, AFI_AXI_BAR6_SZ}, /* BAR 6 */ 336 {AFI_AXI_BAR7_START, AFI_FPCI_BAR7, AFI_AXI_BAR7_SZ}, /* BAR 7 */ 337 {AFI_AXI_BAR8_START, AFI_FPCI_BAR8, AFI_AXI_BAR8_SZ}, /* BAR 8 */ 338 {AFI_MSI_AXI_BAR_ST, AFI_MSI_FPCI_BAR_ST, AFI_MSI_BAR_SZ}, /* MSI 9 */ 339 }; 340 341 /* Compatible devices. */ 342 static struct ofw_compat_data compat_data[] = { 343 {"nvidia,tegra124-pcie", 1}, 344 {NULL, 0}, 345 }; 346 347 struct tegra_pcib_port { 348 int enabled; 349 int port_idx; /* chip port index */ 350 int num_lanes; /* number of lanes */ 351 bus_size_t afi_pex_ctrl; /* offset of afi_pex_ctrl */ 352 353 /* Config space properties. */ 354 bus_addr_t rp_base_addr; /* PA of config window */ 355 bus_size_t rp_size; /* size of config window */ 356 bus_space_handle_t cfg_handle; /* handle of config window */ 357 }; 358 359 #define TEGRA_PCIB_MAX_PORTS 3 360 struct tegra_pcib_softc { 361 device_t dev; 362 struct mtx mtx; 363 struct ofw_bus_iinfo pci_iinfo; 364 struct rman pref_mem_rman; 365 struct rman mem_rman; 366 struct rman io_rman; 367 struct resource *pads_mem_res; 368 struct resource *afi_mem_res; 369 struct resource *cfg_mem_res; 370 struct resource *irq_res; 371 struct resource *msi_irq_res; 372 void *intr_cookie; 373 void *msi_intr_cookie; 374 375 struct tegra_pci_range mem_range; 376 struct tegra_pci_range pref_mem_range; 377 struct tegra_pci_range io_range; 378 379 phy_t phy; 380 clk_t clk_pex; 381 clk_t clk_afi; 382 clk_t clk_pll_e; 383 clk_t clk_cml; 384 hwreset_t hwreset_pex; 385 hwreset_t hwreset_afi; 386 hwreset_t hwreset_pcie_x; 387 regulator_t supply_avddio_pex; 388 regulator_t supply_dvddio_pex; 389 regulator_t supply_avdd_pex_pll; 390 regulator_t supply_hvdd_pex; 391 regulator_t supply_hvdd_pex_pll_e; 392 regulator_t supply_vddio_pex_ctl; 393 regulator_t supply_avdd_pll_erefe; 394 395 int busnr; /* host bridge bus number */ 396 uint32_t msi_bitmap; 397 bus_addr_t cfg_base_addr; /* base address of config */ 398 bus_size_t cfg_cur_offs; /* currently mapped window */ 399 bus_space_handle_t cfg_handle; /* handle of config window */ 400 bus_space_tag_t bus_tag; /* tag of config window */ 401 int lanes_cfg; 402 int num_ports; 403 struct tegra_pcib_port *ports[TEGRA_PCIB_MAX_PORTS]; 404 }; 405 406 /* ------------------------------------------------------------------------- */ 407 /* 408 * Resource manager 409 */ 410 static int 411 tegra_pcib_rman_init(struct tegra_pcib_softc *sc) 412 { 413 int err; 414 char buf[64]; 415 416 /* Memory management. */ 417 sc->pref_mem_rman.rm_type = RMAN_ARRAY; 418 snprintf(buf, sizeof(buf), "%s prefetchable memory space", 419 device_get_nameunit(sc->dev)); 420 sc->pref_mem_rman.rm_descr = strdup(buf, M_DEVBUF); 421 err = rman_init(&sc->pref_mem_rman); 422 if (err) 423 return (err); 424 425 sc->mem_rman.rm_type = RMAN_ARRAY; 426 snprintf(buf, sizeof(buf), "%s non prefetchable memory space", 427 device_get_nameunit(sc->dev)); 428 sc->mem_rman.rm_descr = strdup(buf, M_DEVBUF); 429 err = rman_init(&sc->mem_rman); 430 if (err) 431 return (err); 432 433 sc->io_rman.rm_type = RMAN_ARRAY; 434 snprintf(buf, sizeof(buf), "%s I/O space", 435 device_get_nameunit(sc->dev)); 436 sc->io_rman.rm_descr = strdup(buf, M_DEVBUF); 437 err = rman_init(&sc->io_rman); 438 if (err) { 439 rman_fini(&sc->mem_rman); 440 return (err); 441 } 442 443 err = rman_manage_region(&sc->pref_mem_rman, 444 sc->pref_mem_range.host_addr, 445 sc->pref_mem_range.host_addr + sc->pref_mem_range.size - 1); 446 if (err) 447 goto error; 448 err = rman_manage_region(&sc->mem_rman, 449 sc->mem_range.host_addr, 450 sc->mem_range.host_addr + sc->mem_range.size - 1); 451 if (err) 452 goto error; 453 err = rman_manage_region(&sc->io_rman, 454 sc->io_range.pci_addr, 455 sc->io_range.pci_addr + sc->io_range.size - 1); 456 if (err) 457 goto error; 458 return (0); 459 460 error: 461 rman_fini(&sc->pref_mem_rman); 462 rman_fini(&sc->mem_rman); 463 rman_fini(&sc->io_rman); 464 return (err); 465 } 466 467 static struct rman * 468 tegra_pcib_rman(struct tegra_pcib_softc *sc, int type, u_int flags) 469 { 470 471 switch (type) { 472 case SYS_RES_IOPORT: 473 return (&sc->io_rman); 474 case SYS_RES_MEMORY: 475 if (flags & RF_PREFETCHABLE) 476 return (&sc->pref_mem_rman); 477 else 478 return (&sc->mem_rman); 479 default: 480 break; 481 } 482 483 return (NULL); 484 } 485 486 static struct resource * 487 tegra_pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 488 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 489 { 490 struct tegra_pcib_softc *sc; 491 struct rman *rm; 492 struct resource *res; 493 494 debugf("%s: enter %d start %lx end %lx count %lx\n", __func__, 495 type, start, end, count); 496 sc = device_get_softc(dev); 497 498 #if defined(NEW_PCIB) && defined(PCI_RES_BUS) 499 if (type == PCI_RES_BUS) { 500 return (pci_domain_alloc_bus(0, child, rid, start, end, count, 501 flags)); 502 } 503 #endif 504 505 rm = tegra_pcib_rman(sc, type, flags); 506 507 if (rm == NULL) { 508 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 509 type, rid, start, end, count, flags); 510 511 return (res); 512 } 513 514 if (bootverbose) { 515 device_printf(dev, 516 "rman_reserve_resource: start=%#lx, end=%#lx, count=%#lx\n", 517 start, end, count); 518 } 519 520 res = rman_reserve_resource(rm, start, end, count, flags, child); 521 if (res == NULL) 522 goto fail; 523 rman_set_rid(res, *rid); 524 if (flags & RF_ACTIVE) { 525 if (bus_activate_resource(child, type, *rid, res)) { 526 rman_release_resource(res); 527 goto fail; 528 } 529 } 530 return (res); 531 532 fail: 533 if (bootverbose) { 534 device_printf(dev, "%s FAIL: type=%d, rid=%d, " 535 "start=%016lx, end=%016lx, count=%016lx, flags=%x\n", 536 __func__, type, *rid, start, end, count, flags); 537 } 538 539 return (NULL); 540 } 541 542 static int 543 tegra_pcib_release_resource(device_t dev, device_t child, int type, int rid, 544 struct resource *res) 545 { 546 struct tegra_pcib_softc *sc; 547 struct rman *rm; 548 549 sc = device_get_softc(dev); 550 debugf("%s: %d rid %x\n", __func__, type, rid); 551 552 #if defined(NEW_PCIB) && defined(PCI_RES_BUS) 553 if (type == PCI_RES_BUS) 554 return (pci_domain_release_bus(0, child, rid, res)); 555 #endif 556 557 rm = tegra_pcib_rman(sc, type, rman_get_flags(res)); 558 if (rm != NULL) { 559 KASSERT(rman_is_region_manager(res, rm), ("rman mismatch")); 560 rman_release_resource(res); 561 } 562 563 return (bus_generic_release_resource(dev, child, type, rid, res)); 564 } 565 566 static int 567 tegra_pcib_adjust_resource(device_t dev, device_t child, int type, 568 struct resource *res, u_long start, u_long end) 569 { 570 struct tegra_pcib_softc *sc; 571 struct rman *rm; 572 573 sc = device_get_softc(dev); 574 debugf("%s: %d start %lx end %lx \n", __func__, 575 type, start, end); 576 577 #if defined(NEW_PCIB) && defined(PCI_RES_BUS) 578 if (type == PCI_RES_BUS) 579 return (pci_domain_adjust_bus(0, child, res, start, end)); 580 #endif 581 582 rm = tegra_pcib_rman(sc, type, rman_get_flags(res)); 583 if (rm != NULL) 584 return (rman_adjust_resource(res, start, end)); 585 return (bus_generic_adjust_resource(dev, child, type, res, start, end)); 586 } 587 extern bus_space_tag_t fdtbus_bs_tag; 588 static int 589 tegra_pcib_pcie_activate_resource(device_t dev, device_t child, int type, 590 int rid, struct resource *r) 591 { 592 struct tegra_pcib_softc *sc; 593 vm_offset_t start; 594 void *p; 595 int rv; 596 597 sc = device_get_softc(dev); 598 rv = rman_activate_resource(r); 599 if (rv != 0) 600 return (rv); 601 switch(type) { 602 case SYS_RES_IOPORT: 603 start = rman_get_start(r) + sc->io_range.host_addr; 604 break; 605 default: 606 start = rman_get_start(r); 607 rman_get_start(r); 608 break; 609 } 610 611 if (bootverbose) 612 printf("%s: start %zx, len %ld\n", __func__, start, 613 rman_get_size(r)); 614 615 p = pmap_mapdev(start, (vm_size_t)rman_get_size(r)); 616 rman_set_virtual(r, p); 617 rman_set_bustag(r, fdtbus_bs_tag); 618 rman_set_bushandle(r, (u_long)p); 619 return (0); 620 } 621 622 /* ------------------------------------------------------------------------- */ 623 /* 624 * IVARs 625 */ 626 static int 627 tegra_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 628 { 629 struct tegra_pcib_softc *sc = device_get_softc(dev); 630 631 switch (which) { 632 case PCIB_IVAR_BUS: 633 *result = sc->busnr; 634 return (0); 635 case PCIB_IVAR_DOMAIN: 636 *result = device_get_unit(dev); 637 return (0); 638 } 639 640 return (ENOENT); 641 } 642 643 static int 644 tegra_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 645 { 646 struct tegra_pcib_softc *sc = device_get_softc(dev); 647 648 switch (which) { 649 case PCIB_IVAR_BUS: 650 sc->busnr = value; 651 return (0); 652 } 653 654 return (ENOENT); 655 } 656 657 static int 658 tegra_pcib_maxslots(device_t dev) 659 { 660 return (16); 661 } 662 663 664 static int 665 tegra_pcib_route_interrupt(device_t bus, device_t dev, int pin) 666 { 667 struct tegra_pcib_softc *sc; 668 669 sc = device_get_softc(bus); 670 device_printf(bus, "route pin %d for device %d.%d to %lu\n", 671 pin, pci_get_slot(dev), pci_get_function(dev), 672 rman_get_start(sc->irq_res)); 673 674 return (rman_get_start(sc->irq_res)); 675 } 676 677 static int 678 tegra_pcbib_map_cfg(struct tegra_pcib_softc *sc, u_int bus, u_int slot, 679 u_int func, u_int reg) 680 { 681 bus_size_t offs; 682 int rv; 683 684 offs = sc->cfg_base_addr; 685 offs |= PCI_CFG_BUS(bus) | PCI_CFG_DEV(slot) | PCI_CFG_FUN(func) | 686 PCI_CFG_EXT_REG(reg); 687 if ((sc->cfg_handle != 0) && (sc->cfg_cur_offs == offs)) 688 return (0); 689 if (sc->cfg_handle != 0) 690 bus_space_unmap(sc->bus_tag, sc->cfg_handle, 0x800); 691 692 rv = bus_space_map(sc->bus_tag, offs, 0x800, 0, &sc->cfg_handle); 693 if (rv != 0) 694 device_printf(sc->dev, "Cannot map config space\n"); 695 else 696 sc->cfg_cur_offs = offs; 697 return (rv); 698 } 699 700 static uint32_t 701 tegra_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func, 702 u_int reg, int bytes) 703 { 704 struct tegra_pcib_softc *sc; 705 bus_space_handle_t hndl; 706 uint32_t off; 707 uint32_t val; 708 int rv, i; 709 710 sc = device_get_softc(dev); 711 if (bus == 0) { 712 if (func != 0) 713 return (0xFFFFFFFF); 714 for (i = 0; i < TEGRA_PCIB_MAX_PORTS; i++) { 715 if ((sc->ports[i] != NULL) && 716 (sc->ports[i]->port_idx == slot)) { 717 hndl = sc->ports[i]->cfg_handle; 718 off = reg & 0xFFF; 719 break; 720 } 721 } 722 if (i >= TEGRA_PCIB_MAX_PORTS) 723 return (0xFFFFFFFF); 724 } else { 725 rv = tegra_pcbib_map_cfg(sc, bus, slot, func, reg); 726 if (rv != 0) 727 return (0xFFFFFFFF); 728 hndl = sc->cfg_handle; 729 off = PCI_CFG_BASE_REG(reg); 730 } 731 732 val = bus_space_read_4(sc->bus_tag, hndl, off & ~3); 733 switch (bytes) { 734 case 4: 735 break; 736 case 2: 737 if (off & 3) 738 val >>= 16; 739 val &= 0xffff; 740 break; 741 case 1: 742 val >>= ((off & 3) << 3); 743 val &= 0xff; 744 break; 745 } 746 return val; 747 } 748 749 static void 750 tegra_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func, 751 u_int reg, uint32_t val, int bytes) 752 { 753 struct tegra_pcib_softc *sc; 754 bus_space_handle_t hndl; 755 uint32_t off; 756 uint32_t val2; 757 int rv, i; 758 759 sc = device_get_softc(dev); 760 if (bus == 0) { 761 if (func != 0) 762 return; 763 for (i = 0; i < TEGRA_PCIB_MAX_PORTS; i++) { 764 if ((sc->ports[i] != NULL) && 765 (sc->ports[i]->port_idx == slot)) { 766 hndl = sc->ports[i]->cfg_handle; 767 off = reg & 0xFFF; 768 break; 769 } 770 } 771 if (i >= TEGRA_PCIB_MAX_PORTS) 772 return; 773 } else { 774 rv = tegra_pcbib_map_cfg(sc, bus, slot, func, reg); 775 if (rv != 0) 776 return; 777 hndl = sc->cfg_handle; 778 off = PCI_CFG_BASE_REG(reg); 779 } 780 781 switch (bytes) { 782 case 4: 783 bus_space_write_4(sc->bus_tag, hndl, off, val); 784 break; 785 case 2: 786 val2 = bus_space_read_4(sc->bus_tag, hndl, off & ~3); 787 val2 &= ~(0xffff << ((off & 3) << 3)); 788 val2 |= ((val & 0xffff) << ((off & 3) << 3)); 789 bus_space_write_4(sc->bus_tag, hndl, off & ~3, val2); 790 break; 791 case 1: 792 val2 = bus_space_read_4(sc->bus_tag, hndl, off & ~3); 793 val2 &= ~(0xff << ((off & 3) << 3)); 794 val2 |= ((val & 0xff) << ((off & 3) << 3)); 795 bus_space_write_4(sc->bus_tag, hndl, off & ~3, val2); 796 break; 797 } 798 } 799 800 static int tegra_pci_intr(void *arg) 801 { 802 struct tegra_pcib_softc *sc = arg; 803 uint32_t code, signature; 804 805 code = bus_read_4(sc->afi_mem_res, AFI_INTR_CODE) & AFI_INTR_CODE_MASK; 806 signature = bus_read_4(sc->afi_mem_res, AFI_INTR_SIGNATURE); 807 bus_write_4(sc->afi_mem_res, AFI_INTR_CODE, 0); 808 if (code == AFI_INTR_CODE_INT_CODE_SM_MSG) 809 return(FILTER_STRAY); 810 811 printf("tegra_pci_intr: code %x sig %x\n", code, signature); 812 return (FILTER_HANDLED); 813 } 814 815 #if defined(TEGRA_PCI_MSI) 816 static int 817 tegra_pcib_map_msi(device_t dev, device_t child, int irq, uint64_t *addr, 818 uint32_t *data) 819 { 820 struct tegra_pcib_softc *sc; 821 822 sc = device_get_softc(dev); 823 irq = irq - MSI_IRQ; 824 825 /* validate parameters */ 826 if (isclr(&sc->msi_bitmap, irq)) { 827 device_printf(dev, "invalid MSI 0x%x\n", irq); 828 return (EINVAL); 829 } 830 831 tegra_msi_data(irq, addr, data); 832 833 debugf("%s: irq: %d addr: %jx data: %x\n", 834 __func__, irq, *addr, *data); 835 836 return (0); 837 } 838 839 static int 840 tegra_pcib_alloc_msi(device_t dev, device_t child, int count, 841 int maxcount __unused, int *irqs) 842 { 843 struct tegra_pcib_softc *sc; 844 u_int start = 0, i; 845 846 if (powerof2(count) == 0 || count > MSI_IRQ_NUM) 847 return (EINVAL); 848 849 sc = device_get_softc(dev); 850 mtx_lock(&sc->mtx); 851 852 for (start = 0; (start + count) < MSI_IRQ_NUM; start++) { 853 for (i = start; i < start + count; i++) { 854 if (isset(&sc->msi_bitmap, i)) 855 break; 856 } 857 if (i == start + count) 858 break; 859 } 860 861 if ((start + count) == MSI_IRQ_NUM) { 862 mtx_unlock(&sc->mtx); 863 return (ENXIO); 864 } 865 866 for (i = start; i < start + count; i++) { 867 setbit(&sc->msi_bitmap, i); 868 irqs[i] = MSI_IRQ + i; 869 } 870 debugf("%s: start: %x count: %x\n", __func__, start, count); 871 872 mtx_unlock(&sc->mtx); 873 return (0); 874 } 875 876 static int 877 tegra_pcib_release_msi(device_t dev, device_t child, int count, int *irqs) 878 { 879 struct tegra_pcib_softc *sc; 880 u_int i; 881 882 sc = device_get_softc(dev); 883 mtx_lock(&sc->mtx); 884 885 for (i = 0; i < count; i++) 886 clrbit(&sc->msi_bitmap, irqs[i] - MSI_IRQ); 887 888 mtx_unlock(&sc->mtx); 889 return (0); 890 } 891 #endif 892 893 static bus_size_t 894 tegra_pcib_pex_ctrl(struct tegra_pcib_softc *sc, int port) 895 { 896 if (port >= TEGRA_PCIB_MAX_PORTS) 897 panic("invalid port number: %d\n", port); 898 899 if (port == 0) 900 return (AFI_PEX0_CTRL); 901 else if (port == 1) 902 return (AFI_PEX1_CTRL); 903 else if (port == 2) 904 return (AFI_PEX2_CTRL); 905 else 906 panic("invalid port number: %d\n", port); 907 } 908 909 static int 910 tegra_pcib_enable_fdt_resources(struct tegra_pcib_softc *sc) 911 { 912 int rv; 913 914 rv = hwreset_assert(sc->hwreset_pcie_x); 915 if (rv != 0) { 916 device_printf(sc->dev, "Cannot assert 'pcie_x' reset\n"); 917 return (rv); 918 } 919 rv = hwreset_assert(sc->hwreset_afi); 920 if (rv != 0) { 921 device_printf(sc->dev, "Cannot assert 'afi' reset\n"); 922 return (rv); 923 } 924 rv = hwreset_assert(sc->hwreset_pex); 925 if (rv != 0) { 926 device_printf(sc->dev, "Cannot assert 'pex' reset\n"); 927 return (rv); 928 } 929 930 tegra_powergate_power_off(TEGRA_POWERGATE_PCX); 931 932 /* Power supplies. */ 933 rv = regulator_enable(sc->supply_avddio_pex); 934 if (rv != 0) { 935 device_printf(sc->dev, 936 "Cannot enable 'avddio_pex' regulator\n"); 937 return (rv); 938 } 939 rv = regulator_enable(sc->supply_dvddio_pex); 940 if (rv != 0) { 941 device_printf(sc->dev, 942 "Cannot enable 'dvddio_pex' regulator\n"); 943 return (rv); 944 } 945 rv = regulator_enable(sc->supply_avdd_pex_pll); 946 if (rv != 0) { 947 device_printf(sc->dev, 948 "Cannot enable 'avdd-pex-pll' regulator\n"); 949 return (rv); 950 } 951 952 rv = regulator_enable(sc->supply_hvdd_pex); 953 if (rv != 0) { 954 device_printf(sc->dev, 955 "Cannot enable 'hvdd-pex-supply' regulator\n"); 956 return (rv); 957 } 958 rv = regulator_enable(sc->supply_hvdd_pex_pll_e); 959 if (rv != 0) { 960 device_printf(sc->dev, 961 "Cannot enable 'hvdd-pex-pll-e-supply' regulator\n"); 962 return (rv); 963 } 964 rv = regulator_enable(sc->supply_vddio_pex_ctl); 965 if (rv != 0) { 966 device_printf(sc->dev, 967 "Cannot enable 'vddio-pex-ctl' regulator\n"); 968 return (rv); 969 } 970 rv = regulator_enable(sc->supply_avdd_pll_erefe); 971 if (rv != 0) { 972 device_printf(sc->dev, 973 "Cannot enable 'avdd-pll-erefe-supply' regulator\n"); 974 return (rv); 975 } 976 977 rv = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCX, 978 sc->clk_pex, sc->hwreset_pex); 979 if (rv != 0) { 980 device_printf(sc->dev, "Cannot enable 'PCX' powergate\n"); 981 return (rv); 982 } 983 984 rv = hwreset_deassert(sc->hwreset_afi); 985 if (rv != 0) { 986 device_printf(sc->dev, "Cannot unreset 'afi' reset\n"); 987 return (rv); 988 } 989 990 rv = clk_enable(sc->clk_afi); 991 if (rv != 0) { 992 device_printf(sc->dev, "Cannot enable 'afi' clock\n"); 993 return (rv); 994 } 995 996 rv = clk_enable(sc->clk_cml); 997 if (rv != 0) { 998 device_printf(sc->dev, "Cannot enable 'cml' clock\n"); 999 return (rv); 1000 } 1001 1002 rv = clk_enable(sc->clk_pll_e); 1003 if (rv != 0) { 1004 device_printf(sc->dev, "Cannot enable 'pll_e' clock\n"); 1005 return (rv); 1006 } 1007 return (0); 1008 } 1009 1010 static struct tegra_pcib_port * 1011 tegra_pcib_parse_port(struct tegra_pcib_softc *sc, phandle_t node) 1012 { 1013 struct tegra_pcib_port *port; 1014 uint32_t tmp[5]; 1015 char tmpstr[6]; 1016 int rv; 1017 1018 port = malloc(sizeof(struct tegra_pcib_port), M_DEVBUF, M_WAITOK); 1019 1020 rv = OF_getprop(node, "status", tmpstr, sizeof(tmpstr)); 1021 if (rv <= 0 || strcmp(tmpstr, "okay") == 0 || 1022 strcmp(tmpstr, "ok") == 0) 1023 port->enabled = 1; 1024 else 1025 port->enabled = 0; 1026 1027 rv = OF_getencprop(node, "assigned-addresses", tmp, sizeof(tmp)); 1028 if (rv != sizeof(tmp)) { 1029 device_printf(sc->dev, "Cannot parse assigned-address: %d\n", 1030 rv); 1031 goto fail; 1032 } 1033 port->rp_base_addr = tmp[2]; 1034 port->rp_size = tmp[4]; 1035 port->port_idx = OFW_PCI_PHYS_HI_DEVICE(tmp[0]) - 1; 1036 if (port->port_idx >= TEGRA_PCIB_MAX_PORTS) { 1037 device_printf(sc->dev, "Invalid port index: %d\n", 1038 port->port_idx); 1039 goto fail; 1040 } 1041 /* XXX - TODO: 1042 * Implement proper function for parsing pci "reg" property: 1043 * - it have PCI bus format 1044 * - its relative to matching "assigned-addresses" 1045 */ 1046 rv = OF_getencprop(node, "reg", tmp, sizeof(tmp)); 1047 if (rv != sizeof(tmp)) { 1048 device_printf(sc->dev, "Cannot parse reg: %d\n", rv); 1049 goto fail; 1050 } 1051 port->rp_base_addr += tmp[2]; 1052 1053 rv = OF_getencprop(node, "nvidia,num-lanes", &port->num_lanes, 1054 sizeof(port->num_lanes)); 1055 if (rv != sizeof(port->num_lanes)) { 1056 device_printf(sc->dev, "Cannot parse nvidia,num-lanes: %d\n", 1057 rv); 1058 goto fail; 1059 } 1060 if (port->num_lanes > 4) { 1061 device_printf(sc->dev, "Invalid nvidia,num-lanes: %d\n", 1062 port->num_lanes); 1063 goto fail; 1064 } 1065 1066 port->afi_pex_ctrl = tegra_pcib_pex_ctrl(sc, port->port_idx); 1067 sc->lanes_cfg |= port->num_lanes << (4 * port->port_idx); 1068 1069 return (port); 1070 fail: 1071 free(port, M_DEVBUF); 1072 return (NULL); 1073 } 1074 1075 1076 static int 1077 tegra_pcib_parse_fdt_resources(struct tegra_pcib_softc *sc, phandle_t node) 1078 { 1079 phandle_t child; 1080 struct tegra_pcib_port *port; 1081 int rv; 1082 1083 /* Power supplies. */ 1084 rv = regulator_get_by_ofw_property(sc->dev, "avddio-pex-supply", 1085 &sc->supply_avddio_pex); 1086 if (rv != 0) { 1087 device_printf(sc->dev, 1088 "Cannot get 'avddio-pex' regulator\n"); 1089 return (ENXIO); 1090 } 1091 rv = regulator_get_by_ofw_property(sc->dev, "dvddio-pex-supply", 1092 &sc->supply_dvddio_pex); 1093 if (rv != 0) { 1094 device_printf(sc->dev, 1095 "Cannot get 'dvddio-pex' regulator\n"); 1096 return (ENXIO); 1097 } 1098 rv = regulator_get_by_ofw_property(sc->dev, "avdd-pex-pll-supply", 1099 &sc->supply_avdd_pex_pll); 1100 if (rv != 0) { 1101 device_printf(sc->dev, 1102 "Cannot get 'avdd-pex-pll' regulator\n"); 1103 return (ENXIO); 1104 } 1105 rv = regulator_get_by_ofw_property(sc->dev, "hvdd-pex-supply", 1106 &sc->supply_hvdd_pex); 1107 if (rv != 0) { 1108 device_printf(sc->dev, 1109 "Cannot get 'hvdd-pex' regulator\n"); 1110 return (ENXIO); 1111 } 1112 rv = regulator_get_by_ofw_property(sc->dev, "hvdd-pex-pll-e-supply", 1113 &sc->supply_hvdd_pex_pll_e); 1114 if (rv != 0) { 1115 device_printf(sc->dev, 1116 "Cannot get 'hvdd-pex-pll-e' regulator\n"); 1117 return (ENXIO); 1118 } 1119 rv = regulator_get_by_ofw_property(sc->dev, "vddio-pex-ctl-supply", 1120 &sc->supply_vddio_pex_ctl); 1121 if (rv != 0) { 1122 device_printf(sc->dev, 1123 "Cannot get 'vddio-pex-ctl' regulator\n"); 1124 return (ENXIO); 1125 } 1126 rv = regulator_get_by_ofw_property(sc->dev, "avdd-pll-erefe-supply", 1127 &sc->supply_avdd_pll_erefe); 1128 if (rv != 0) { 1129 device_printf(sc->dev, 1130 "Cannot get 'avdd-pll-erefe' regulator\n"); 1131 return (ENXIO); 1132 } 1133 1134 /* Resets. */ 1135 rv = hwreset_get_by_ofw_name(sc->dev, "pex", &sc->hwreset_pex); 1136 if (rv != 0) { 1137 device_printf(sc->dev, "Cannot get 'pex' reset\n"); 1138 return (ENXIO); 1139 } 1140 rv = hwreset_get_by_ofw_name(sc->dev, "afi", &sc->hwreset_afi); 1141 if (rv != 0) { 1142 device_printf(sc->dev, "Cannot get 'afi' reset\n"); 1143 return (ENXIO); 1144 } 1145 rv = hwreset_get_by_ofw_name(sc->dev, "pcie_x", &sc->hwreset_pcie_x); 1146 if (rv != 0) { 1147 device_printf(sc->dev, "Cannot get 'pcie_x' reset\n"); 1148 return (ENXIO); 1149 } 1150 1151 /* Clocks. */ 1152 rv = clk_get_by_ofw_name(sc->dev, "pex", &sc->clk_pex); 1153 if (rv != 0) { 1154 device_printf(sc->dev, "Cannot get 'pex' clock\n"); 1155 return (ENXIO); 1156 } 1157 rv = clk_get_by_ofw_name(sc->dev, "afi", &sc->clk_afi); 1158 if (rv != 0) { 1159 device_printf(sc->dev, "Cannot get 'afi' clock\n"); 1160 return (ENXIO); 1161 } 1162 rv = clk_get_by_ofw_name(sc->dev, "pll_e", &sc->clk_pll_e); 1163 if (rv != 0) { 1164 device_printf(sc->dev, "Cannot get 'pll_e' clock\n"); 1165 return (ENXIO); 1166 } 1167 rv = clk_get_by_ofw_name(sc->dev, "cml", &sc->clk_cml); 1168 if (rv != 0) { 1169 device_printf(sc->dev, "Cannot get 'cml' clock\n"); 1170 return (ENXIO); 1171 } 1172 1173 /* Phy. */ 1174 rv = phy_get_by_ofw_name(sc->dev, "pcie", &sc->phy); 1175 if (rv != 0) { 1176 device_printf(sc->dev, "Cannot get 'pcie' phy\n"); 1177 return (ENXIO); 1178 } 1179 1180 /* Ports */ 1181 sc->num_ports = 0; 1182 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 1183 port = tegra_pcib_parse_port(sc, child); 1184 if (port == NULL) { 1185 device_printf(sc->dev, "Cannot parse PCIe port node\n"); 1186 return (ENXIO); 1187 } 1188 sc->ports[sc->num_ports++] = port; 1189 } 1190 1191 return (0); 1192 } 1193 1194 static int 1195 tegra_pcib_decode_ranges(struct tegra_pcib_softc *sc, 1196 struct tegra_pci_range *ranges, int nranges) 1197 { 1198 int i; 1199 1200 for (i = 2; i < nranges; i++) { 1201 if (ranges[i].space_code == OFW_PCI_PHYS_HI_SPACE_IO) { 1202 if (sc->io_range.size != 0) { 1203 device_printf(sc->dev, 1204 "Duplicated IO range found in DT\n"); 1205 return (ENXIO); 1206 } 1207 sc->io_range = ranges[i]; 1208 } 1209 if ((ranges[i].space_code == OFW_PCI_PHYS_HI_SPACE_MEM32) && 1210 !ranges[i].prefetchable) { 1211 if (sc->mem_range.size != 0) { 1212 device_printf(sc->dev, 1213 "Duplicated memory range found in DT\n"); 1214 return (ENXIO); 1215 } 1216 sc->mem_range = ranges[i]; 1217 } 1218 if ((ranges[i].space_code == OFW_PCI_PHYS_HI_SPACE_MEM32) && 1219 ranges[i].prefetchable) { 1220 if (sc->pref_mem_range.size != 0) { 1221 device_printf(sc->dev, 1222 "Duplicated memory range found in DT\n"); 1223 return (ENXIO); 1224 } 1225 sc->pref_mem_range = ranges[i]; 1226 } 1227 } 1228 if ((sc->io_range.size == 0) || (sc->mem_range.size == 0) 1229 || (sc->pref_mem_range.size == 0)) { 1230 device_printf(sc->dev, 1231 " Not all required ranges are found in DT\n"); 1232 return (ENXIO); 1233 } 1234 return (0); 1235 } 1236 1237 /* 1238 * Hardware config. 1239 */ 1240 static int 1241 tegra_pcib_wait_for_link(struct tegra_pcib_softc *sc, 1242 struct tegra_pcib_port *port) 1243 { 1244 uint32_t reg; 1245 int i; 1246 1247 1248 /* Setup link detection. */ 1249 reg = tegra_pcib_read_config(sc->dev, 0, port->port_idx, 0, 1250 RP_PRIV_MISC, 4); 1251 reg &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT; 1252 reg |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT; 1253 tegra_pcib_write_config(sc->dev, 0, port->port_idx, 0, 1254 RP_PRIV_MISC, reg, 4); 1255 1256 for (i = TEGRA_PCIE_LINKUP_TIMEOUT; i > 0; i--) { 1257 reg = tegra_pcib_read_config(sc->dev, 0, port->port_idx, 0, 1258 RP_VEND_XP, 4); 1259 if (reg & RP_VEND_XP_DL_UP) 1260 break; 1261 1262 } 1263 if (i <= 0) 1264 return (ETIMEDOUT); 1265 1266 for (i = TEGRA_PCIE_LINKUP_TIMEOUT; i > 0; i--) { 1267 reg = tegra_pcib_read_config(sc->dev, 0, port->port_idx, 0, 1268 RP_LINK_CONTROL_STATUS, 4); 1269 if (reg & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE) 1270 break; 1271 1272 } 1273 if (i <= 0) 1274 return (ETIMEDOUT); 1275 return (0); 1276 } 1277 1278 static void 1279 tegra_pcib_port_enable(struct tegra_pcib_softc *sc, int port_num) 1280 { 1281 struct tegra_pcib_port *port; 1282 uint32_t reg; 1283 int rv; 1284 1285 port = sc->ports[port_num]; 1286 1287 /* Put port to reset. */ 1288 reg = AFI_RD4(sc, port->afi_pex_ctrl); 1289 reg &= ~AFI_PEX_CTRL_RST_L; 1290 AFI_WR4(sc, port->afi_pex_ctrl, reg); 1291 AFI_RD4(sc, port->afi_pex_ctrl); 1292 DELAY(10); 1293 1294 /* Enable clocks. */ 1295 reg |= AFI_PEX_CTRL_REFCLK_EN; 1296 reg |= AFI_PEX_CTRL_CLKREQ_EN; 1297 reg |= AFI_PEX_CTRL_OVERRIDE_EN; 1298 AFI_WR4(sc, port->afi_pex_ctrl, reg); 1299 AFI_RD4(sc, port->afi_pex_ctrl); 1300 DELAY(100); 1301 1302 /* Release reset. */ 1303 reg |= AFI_PEX_CTRL_RST_L; 1304 AFI_WR4(sc, port->afi_pex_ctrl, reg); 1305 1306 rv = tegra_pcib_wait_for_link(sc, port); 1307 if (bootverbose) 1308 device_printf(sc->dev, " port %d (%d lane%s): Link is %s\n", 1309 port->port_idx, port->num_lanes, 1310 port->num_lanes > 1 ? "s": "", 1311 rv == 0 ? "up": "down"); 1312 } 1313 1314 1315 static void 1316 tegra_pcib_port_disable(struct tegra_pcib_softc *sc, uint32_t port_num) 1317 { 1318 struct tegra_pcib_port *port; 1319 uint32_t reg; 1320 1321 port = sc->ports[port_num]; 1322 1323 /* Put port to reset. */ 1324 reg = AFI_RD4(sc, port->afi_pex_ctrl); 1325 reg &= ~AFI_PEX_CTRL_RST_L; 1326 AFI_WR4(sc, port->afi_pex_ctrl, reg); 1327 AFI_RD4(sc, port->afi_pex_ctrl); 1328 DELAY(10); 1329 1330 /* Disable clocks. */ 1331 reg &= ~AFI_PEX_CTRL_CLKREQ_EN; 1332 reg &= ~AFI_PEX_CTRL_REFCLK_EN; 1333 AFI_WR4(sc, port->afi_pex_ctrl, reg); 1334 1335 if (bootverbose) 1336 device_printf(sc->dev, " port %d (%d lane%s): Disabled\n", 1337 port->port_idx, port->num_lanes, 1338 port->num_lanes > 1 ? "s": ""); 1339 } 1340 1341 static void 1342 tegra_pcib_set_bar(struct tegra_pcib_softc *sc, int bar, uint32_t axi, 1343 uint64_t fpci, uint32_t size, int is_memory) 1344 { 1345 uint32_t fpci_reg; 1346 uint32_t axi_reg; 1347 uint32_t size_reg; 1348 1349 axi_reg = axi & ~0xFFF; 1350 size_reg = size >> 12; 1351 fpci_reg = (uint32_t)(fpci >> 8) & ~0xF; 1352 fpci_reg |= is_memory ? 0x1 : 0x0; 1353 AFI_WR4(sc, bars[bar].axi_start, axi_reg); 1354 AFI_WR4(sc, bars[bar].size, size_reg); 1355 AFI_WR4(sc, bars[bar].fpci_start, fpci_reg); 1356 } 1357 1358 static int 1359 tegra_pcib_enable(struct tegra_pcib_softc *sc, uint32_t port) 1360 { 1361 int rv; 1362 int i; 1363 uint32_t reg; 1364 1365 rv = tegra_pcib_enable_fdt_resources(sc); 1366 if (rv != 0) { 1367 device_printf(sc->dev, "Cannot enable FDT resources\n"); 1368 return (rv); 1369 } 1370 /* Enable PLLE control. */ 1371 reg = AFI_RD4(sc, AFI_PLLE_CONTROL); 1372 reg &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL; 1373 reg |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN; 1374 AFI_WR4(sc, AFI_PLLE_CONTROL, reg); 1375 1376 /* Set bias pad. */ 1377 AFI_WR4(sc, AFI_PEXBIAS_CTRL, 0); 1378 1379 /* Configure mode and ports. */ 1380 reg = AFI_RD4(sc, AFI_PCIE_CONFIG); 1381 reg &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK; 1382 if (sc->lanes_cfg == 0x14) { 1383 if (bootverbose) 1384 device_printf(sc->dev, 1385 "Using x1,x4 configuration\n"); 1386 reg |= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_XBAR4_1; 1387 } else if (sc->lanes_cfg == 0x12) { 1388 if (bootverbose) 1389 device_printf(sc->dev, 1390 "Using x1,x2 configuration\n"); 1391 reg |= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_XBAR2_1; 1392 } else { 1393 device_printf(sc->dev, 1394 "Unsupported lanes configuration: 0x%X\n", sc->lanes_cfg); 1395 } 1396 reg |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL; 1397 for (i = 0; i < TEGRA_PCIB_MAX_PORTS; i++) { 1398 if ((sc->ports[i] != NULL)) 1399 reg &= 1400 ~AFI_PCIE_CONFIG_PCIE_DISABLE(sc->ports[i]->port_idx); 1401 } 1402 AFI_WR4(sc, AFI_PCIE_CONFIG, reg); 1403 1404 /* Enable Gen2 support. */ 1405 reg = AFI_RD4(sc, AFI_FUSE); 1406 reg &= ~AFI_FUSE_PCIE_T0_GEN2_DIS; 1407 AFI_WR4(sc, AFI_FUSE, reg); 1408 1409 /* Enable PCIe phy. */ 1410 rv = phy_enable(sc->dev, sc->phy); 1411 if (rv != 0) { 1412 device_printf(sc->dev, "Cannot enable phy\n"); 1413 return (rv); 1414 } 1415 1416 rv = hwreset_deassert(sc->hwreset_pcie_x); 1417 if (rv != 0) { 1418 device_printf(sc->dev, "Cannot unreset 'pci_x' reset\n"); 1419 return (rv); 1420 } 1421 1422 /* Enable config space. */ 1423 reg = AFI_RD4(sc, AFI_CONFIGURATION); 1424 reg |= AFI_CONFIGURATION_EN_FPCI; 1425 AFI_WR4(sc, AFI_CONFIGURATION, reg); 1426 1427 /* Enable AFI errors. */ 1428 reg = 0; 1429 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_INI_SLVERR); 1430 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_INI_DECERR); 1431 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_TGT_SLVERR); 1432 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_TGT_DECERR); 1433 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_TGT_WRERR); 1434 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_SM_MSG); 1435 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_DFPCI_DECERR); 1436 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_AXI_DECERR); 1437 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_FPCI_TIMEOUT); 1438 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_PE_PRSNT_SENSE); 1439 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_PE_CLKREQ_SENSE); 1440 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_CLKCLAMP_SENSE); 1441 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_RDY4PD_SENSE); 1442 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_P2P_ERROR); 1443 AFI_WR4(sc, AFI_AFI_INTR_ENABLE, reg); 1444 AFI_WR4(sc, AFI_SM_INTR_ENABLE, 0xffffffff); 1445 1446 /* Enable INT, disable MSI. */ 1447 AFI_WR4(sc, AFI_INTR_MASK, AFI_INTR_MASK_INT_MASK); 1448 1449 /* Mask all FPCI errors. */ 1450 AFI_WR4(sc, AFI_FPCI_ERROR_MASKS, 0); 1451 1452 /* Setup AFI translation windows. */ 1453 /* BAR 0 - type 1 extended configuration. */ 1454 tegra_pcib_set_bar(sc, 0, rman_get_start(sc->cfg_mem_res), 1455 FPCI_MAP_EXT_TYPE1_CONFIG, rman_get_size(sc->cfg_mem_res), 0); 1456 1457 /* BAR 1 - downstream I/O. */ 1458 tegra_pcib_set_bar(sc, 1, sc->io_range.host_addr, FPCI_MAP_IO, 1459 sc->io_range.size, 0); 1460 1461 /* BAR 2 - downstream prefetchable memory 1:1. */ 1462 tegra_pcib_set_bar(sc, 2, sc->pref_mem_range.host_addr, 1463 sc->pref_mem_range.host_addr, sc->pref_mem_range.size, 1); 1464 1465 /* BAR 3 - downstream not prefetchable memory 1:1 .*/ 1466 tegra_pcib_set_bar(sc, 3, sc->mem_range.host_addr, 1467 sc->mem_range.host_addr, sc->mem_range.size, 1); 1468 1469 /* BAR 3-8 clear. */ 1470 tegra_pcib_set_bar(sc, 4, 0, 0, 0, 0); 1471 tegra_pcib_set_bar(sc, 5, 0, 0, 0, 0); 1472 tegra_pcib_set_bar(sc, 6, 0, 0, 0, 0); 1473 tegra_pcib_set_bar(sc, 7, 0, 0, 0, 0); 1474 tegra_pcib_set_bar(sc, 8, 0, 0, 0, 0); 1475 1476 /* MSI BAR - clear. */ 1477 tegra_pcib_set_bar(sc, 9, 0, 0, 0, 0); 1478 return(0); 1479 } 1480 1481 static int 1482 tegra_pcib_probe(device_t dev) 1483 { 1484 if (!ofw_bus_status_okay(dev)) 1485 return (ENXIO); 1486 1487 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) { 1488 device_set_desc(dev, "Nvidia Integrated PCI/PCI-E Controller"); 1489 return (BUS_PROBE_DEFAULT); 1490 } 1491 return (ENXIO); 1492 } 1493 1494 static int 1495 tegra_pcib_attach(device_t dev) 1496 { 1497 struct tegra_pcib_softc *sc; 1498 phandle_t node; 1499 uint32_t unit; 1500 int rv; 1501 int rid; 1502 int nranges; 1503 struct tegra_pci_range *ranges; 1504 struct tegra_pcib_port *port; 1505 int i; 1506 1507 sc = device_get_softc(dev); 1508 sc->dev = dev; 1509 unit = fdt_get_unit(dev); 1510 mtx_init(&sc->mtx, "msi_mtx", NULL, MTX_DEF); 1511 1512 1513 node = ofw_bus_get_node(dev); 1514 1515 rv = tegra_pcib_parse_fdt_resources(sc, node); 1516 if (rv != 0) { 1517 device_printf(dev, "Cannot get FDT resources\n"); 1518 return (rv); 1519 } 1520 1521 nranges = tegra_pci_get_ranges(node, &ranges); 1522 if (nranges != 5) { 1523 device_printf(sc->dev, "Unexpected number of ranges: %d\n", 1524 nranges); 1525 rv = ENXIO; 1526 goto out; 1527 } 1528 1529 /* Allocate bus_space resources. */ 1530 rid = 0; 1531 sc->pads_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 1532 RF_ACTIVE); 1533 if (sc->pads_mem_res == NULL) { 1534 device_printf(dev, "Cannot allocate PADS register\n"); 1535 rv = ENXIO; 1536 goto out; 1537 } 1538 /* 1539 * XXX - FIXME 1540 * tag for config space is not filled when RF_ALLOCATED flag is used. 1541 */ 1542 sc->bus_tag = rman_get_bustag(sc->pads_mem_res); 1543 1544 rid = 1; 1545 sc->afi_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 1546 RF_ACTIVE); 1547 if (sc->afi_mem_res == NULL) { 1548 device_printf(dev, "Cannot allocate AFI register\n"); 1549 rv = ENXIO; 1550 goto out; 1551 } 1552 1553 rid = 2; 1554 sc->cfg_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 1555 RF_ALLOCATED); 1556 if (sc->cfg_mem_res == NULL) { 1557 device_printf(dev, "Cannot allocate config space memory\n"); 1558 rv = ENXIO; 1559 goto out; 1560 } 1561 sc->cfg_base_addr = rman_get_start(sc->cfg_mem_res); 1562 1563 1564 /* Map RP slots */ 1565 for (i = 0; i < TEGRA_PCIB_MAX_PORTS; i++) { 1566 if (sc->ports[i] == NULL) 1567 continue; 1568 port = sc->ports[i]; 1569 rv = bus_space_map(sc->bus_tag, port->rp_base_addr, 1570 port->rp_size, 0, &port->cfg_handle); 1571 if (rv != 0) { 1572 device_printf(sc->dev, "Cannot allocate memory for " 1573 "port: %d\n", i); 1574 rv = ENXIO; 1575 goto out; 1576 } 1577 } 1578 1579 /* 1580 * Get PCI interrupt info. 1581 */ 1582 ofw_bus_setup_iinfo(node, &sc->pci_iinfo, sizeof(pcell_t)); 1583 rid = 0; 1584 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1585 RF_ACTIVE | RF_SHAREABLE); 1586 if (sc->irq_res == NULL) { 1587 device_printf(dev, "Cannot allocate IRQ resources\n"); 1588 rv = ENXIO; 1589 goto out; 1590 } 1591 1592 rid = 1; 1593 sc->msi_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1594 RF_ACTIVE); 1595 if (sc->irq_res == NULL) { 1596 device_printf(dev, "Cannot allocate MSI IRQ resources\n"); 1597 rv = ENXIO; 1598 goto out; 1599 } 1600 1601 if (bus_setup_intr(dev, sc->irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 1602 tegra_pci_intr, NULL, sc, &sc->intr_cookie)) { 1603 device_printf(dev, "cannot setup interrupt handler\n"); 1604 rv = ENXIO; 1605 goto out; 1606 } 1607 1608 /* Memory management. */ 1609 rv = tegra_pcib_decode_ranges(sc, ranges, nranges); 1610 if (rv != 0) 1611 goto out; 1612 1613 rv = tegra_pcib_rman_init(sc); 1614 if (rv != 0) 1615 goto out; 1616 free(ranges, M_DEVBUF); 1617 ranges = NULL; 1618 1619 /* 1620 * Enable PCIE device. 1621 */ 1622 rv = tegra_pcib_enable(sc, unit); 1623 if (rv != 0) 1624 goto out; 1625 for (i = 0; i < TEGRA_PCIB_MAX_PORTS; i++) { 1626 if (sc->ports[i] == NULL) 1627 continue; 1628 if (sc->ports[i]->enabled) 1629 tegra_pcib_port_enable(sc, i); 1630 else 1631 tegra_pcib_port_disable(sc, i); 1632 } 1633 1634 device_add_child(dev, "pci", -1); 1635 1636 return (bus_generic_attach(dev)); 1637 1638 out: 1639 if (ranges != NULL) 1640 free(ranges, M_DEVBUF); 1641 1642 return (rv); 1643 } 1644 1645 1646 static device_method_t tegra_pcib_methods[] = { 1647 /* Device interface */ 1648 DEVMETHOD(device_probe, tegra_pcib_probe), 1649 DEVMETHOD(device_attach, tegra_pcib_attach), 1650 1651 /* Bus interface */ 1652 DEVMETHOD(bus_read_ivar, tegra_pcib_read_ivar), 1653 DEVMETHOD(bus_write_ivar, tegra_pcib_write_ivar), 1654 DEVMETHOD(bus_alloc_resource, tegra_pcib_alloc_resource), 1655 DEVMETHOD(bus_adjust_resource, tegra_pcib_adjust_resource), 1656 DEVMETHOD(bus_release_resource, tegra_pcib_release_resource), 1657 DEVMETHOD(bus_activate_resource, tegra_pcib_pcie_activate_resource), 1658 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 1659 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 1660 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 1661 1662 /* pcib interface */ 1663 DEVMETHOD(pcib_maxslots, tegra_pcib_maxslots), 1664 DEVMETHOD(pcib_read_config, tegra_pcib_read_config), 1665 DEVMETHOD(pcib_write_config, tegra_pcib_write_config), 1666 DEVMETHOD(pcib_route_interrupt, tegra_pcib_route_interrupt), 1667 1668 #if defined(TEGRA_PCI_MSI) 1669 DEVMETHOD(pcib_alloc_msi, tegra_pcib_alloc_msi), 1670 DEVMETHOD(pcib_release_msi, tegra_pcib_release_msi), 1671 DEVMETHOD(pcib_map_msi, tegra_pcib_map_msi), 1672 #endif 1673 1674 /* OFW bus interface */ 1675 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 1676 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 1677 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 1678 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 1679 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 1680 1681 DEVMETHOD_END 1682 }; 1683 1684 static driver_t tegra_pcib_driver = { 1685 "pcib", 1686 tegra_pcib_methods, 1687 sizeof(struct tegra_pcib_softc), 1688 }; 1689 1690 devclass_t pcib_devclass; 1691 1692 DRIVER_MODULE(pcib, simplebus, tegra_pcib_driver, pcib_devclass, 0, 0);