1// SPDX-License-Identifier: GPL-2.0+ 2/* 3 * PCIe host controller driver for Tegra194 SoC 4 * 5 * Copyright (C) 2019 NVIDIA Corporation. 6 * 7 * Author: Vidya Sagar <vidyas@nvidia.com> 8 */ 9 10#include <linux/bitfield.h> 11#include <linux/clk.h> 12#include <linux/debugfs.h> 13#include <linux/delay.h> 14#include <linux/gpio.h> 15#include <linux/gpio/consumer.h> 16#include <linux/interrupt.h> 17#include <linux/iopoll.h> 18#include <linux/kernel.h> 19#include <linux/module.h> 20#include <linux/of.h> 21#include <linux/of_device.h> 22#include <linux/of_gpio.h> 23#include <linux/of_irq.h> 24#include <linux/of_pci.h> 25#include <linux/pci.h> 26#include <linux/phy/phy.h> 27#include <linux/pinctrl/consumer.h> 28#include <linux/platform_device.h> 29#include <linux/pm_runtime.h> 30#include <linux/random.h> 31#include <linux/reset.h> 32#include <linux/resource.h> 33#include <linux/types.h> 34#include "pcie-designware.h" 35#include <soc/tegra/bpmp.h> 36#include <soc/tegra/bpmp-abi.h> 37#include "../../pci.h" 38 39#define APPL_PINMUX 0x0 40#define APPL_PINMUX_PEX_RST BIT(0) 41#define APPL_PINMUX_CLKREQ_OVERRIDE_EN BIT(2) 42#define APPL_PINMUX_CLKREQ_OVERRIDE BIT(3) 43#define APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE_EN BIT(4) 44#define APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE BIT(5) 45 46#define APPL_CTRL 0x4 47#define APPL_CTRL_SYS_PRE_DET_STATE BIT(6) 48#define APPL_CTRL_LTSSM_EN BIT(7) 49#define APPL_CTRL_HW_HOT_RST_EN BIT(20) 50#define APPL_CTRL_HW_HOT_RST_MODE_MASK GENMASK(1, 0) 51#define APPL_CTRL_HW_HOT_RST_MODE_SHIFT 22 52#define APPL_CTRL_HW_HOT_RST_MODE_IMDT_RST 0x1 53 54#define APPL_INTR_EN_L0_0 0x8 55#define APPL_INTR_EN_L0_0_LINK_STATE_INT_EN BIT(0) 56#define APPL_INTR_EN_L0_0_MSI_RCV_INT_EN BIT(4) 57#define APPL_INTR_EN_L0_0_INT_INT_EN BIT(8) 58#define APPL_INTR_EN_L0_0_PCI_CMD_EN_INT_EN BIT(15) 59#define APPL_INTR_EN_L0_0_CDM_REG_CHK_INT_EN BIT(19) 60#define APPL_INTR_EN_L0_0_SYS_INTR_EN BIT(30) 61#define APPL_INTR_EN_L0_0_SYS_MSI_INTR_EN BIT(31) 62 63#define APPL_INTR_STATUS_L0 0xC 64#define APPL_INTR_STATUS_L0_LINK_STATE_INT BIT(0) 65#define APPL_INTR_STATUS_L0_INT_INT BIT(8) 66#define APPL_INTR_STATUS_L0_PCI_CMD_EN_INT BIT(15) 67#define APPL_INTR_STATUS_L0_PEX_RST_INT BIT(16) 68#define APPL_INTR_STATUS_L0_CDM_REG_CHK_INT BIT(18) 69 70#define APPL_INTR_EN_L1_0_0 0x1C 71#define APPL_INTR_EN_L1_0_0_LINK_REQ_RST_NOT_INT_EN BIT(1) 72#define APPL_INTR_EN_L1_0_0_RDLH_LINK_UP_INT_EN BIT(3) 73#define APPL_INTR_EN_L1_0_0_HOT_RESET_DONE_INT_EN BIT(30) 74 75#define APPL_INTR_STATUS_L1_0_0 0x20 76#define APPL_INTR_STATUS_L1_0_0_LINK_REQ_RST_NOT_CHGED BIT(1) 77#define APPL_INTR_STATUS_L1_0_0_RDLH_LINK_UP_CHGED BIT(3) 78#define APPL_INTR_STATUS_L1_0_0_HOT_RESET_DONE BIT(30) 79 80#define APPL_INTR_STATUS_L1_1 0x2C 81#define APPL_INTR_STATUS_L1_2 0x30 82#define APPL_INTR_STATUS_L1_3 0x34 83#define APPL_INTR_STATUS_L1_6 0x3C 84#define APPL_INTR_STATUS_L1_7 0x40 85#define APPL_INTR_STATUS_L1_15_CFG_BME_CHGED BIT(1) 86 87#define APPL_INTR_EN_L1_8_0 0x44 88#define APPL_INTR_EN_L1_8_BW_MGT_INT_EN BIT(2) 89#define APPL_INTR_EN_L1_8_AUTO_BW_INT_EN BIT(3) 90#define APPL_INTR_EN_L1_8_INTX_EN BIT(11) 91#define APPL_INTR_EN_L1_8_AER_INT_EN BIT(15) 92 93#define APPL_INTR_STATUS_L1_8_0 0x4C 94#define APPL_INTR_STATUS_L1_8_0_EDMA_INT_MASK GENMASK(11, 6) 95#define APPL_INTR_STATUS_L1_8_0_BW_MGT_INT_STS BIT(2) 96#define APPL_INTR_STATUS_L1_8_0_AUTO_BW_INT_STS BIT(3) 97 98#define APPL_INTR_STATUS_L1_9 0x54 99#define APPL_INTR_STATUS_L1_10 0x58 100#define APPL_INTR_STATUS_L1_11 0x64 101#define APPL_INTR_STATUS_L1_13 0x74 102#define APPL_INTR_STATUS_L1_14 0x78 103#define APPL_INTR_STATUS_L1_15 0x7C 104#define APPL_INTR_STATUS_L1_17 0x88 105 106#define APPL_INTR_EN_L1_18 0x90 107#define APPL_INTR_EN_L1_18_CDM_REG_CHK_CMPLT BIT(2) 108#define APPL_INTR_EN_L1_18_CDM_REG_CHK_CMP_ERR BIT(1) 109#define APPL_INTR_EN_L1_18_CDM_REG_CHK_LOGIC_ERR BIT(0) 110 111#define APPL_INTR_STATUS_L1_18 0x94 112#define APPL_INTR_STATUS_L1_18_CDM_REG_CHK_CMPLT BIT(2) 113#define APPL_INTR_STATUS_L1_18_CDM_REG_CHK_CMP_ERR BIT(1) 114#define APPL_INTR_STATUS_L1_18_CDM_REG_CHK_LOGIC_ERR BIT(0) 115 116#define APPL_MSI_CTRL_1 0xAC 117 118#define APPL_MSI_CTRL_2 0xB0 119 120#define APPL_LEGACY_INTX 0xB8 121 122#define APPL_LTR_MSG_1 0xC4 123#define LTR_MSG_REQ BIT(15) 124#define LTR_MST_NO_SNOOP_SHIFT 16 125 126#define APPL_LTR_MSG_2 0xC8 127#define APPL_LTR_MSG_2_LTR_MSG_REQ_STATE BIT(3) 128 129#define APPL_LINK_STATUS 0xCC 130#define APPL_LINK_STATUS_RDLH_LINK_UP BIT(0) 131 132#define APPL_DEBUG 0xD0 133#define APPL_DEBUG_PM_LINKST_IN_L2_LAT BIT(21) 134#define APPL_DEBUG_PM_LINKST_IN_L0 0x11 135#define APPL_DEBUG_LTSSM_STATE_MASK GENMASK(8, 3) 136#define APPL_DEBUG_LTSSM_STATE_SHIFT 3 137#define LTSSM_STATE_PRE_DETECT 5 138 139#define APPL_RADM_STATUS 0xE4 140#define APPL_PM_XMT_TURNOFF_STATE BIT(0) 141 142#define APPL_DM_TYPE 0x100 143#define APPL_DM_TYPE_MASK GENMASK(3, 0) 144#define APPL_DM_TYPE_RP 0x4 145#define APPL_DM_TYPE_EP 0x0 146 147#define APPL_CFG_BASE_ADDR 0x104 148#define APPL_CFG_BASE_ADDR_MASK GENMASK(31, 12) 149 150#define APPL_CFG_IATU_DMA_BASE_ADDR 0x108 151#define APPL_CFG_IATU_DMA_BASE_ADDR_MASK GENMASK(31, 18) 152 153#define APPL_CFG_MISC 0x110 154#define APPL_CFG_MISC_SLV_EP_MODE BIT(14) 155#define APPL_CFG_MISC_ARCACHE_MASK GENMASK(13, 10) 156#define APPL_CFG_MISC_ARCACHE_SHIFT 10 157#define APPL_CFG_MISC_ARCACHE_VAL 3 158 159#define APPL_CFG_SLCG_OVERRIDE 0x114 160#define APPL_CFG_SLCG_OVERRIDE_SLCG_EN_MASTER BIT(0) 161 162#define APPL_CAR_RESET_OVRD 0x12C 163#define APPL_CAR_RESET_OVRD_CYA_OVERRIDE_CORE_RST_N BIT(0) 164 165#define IO_BASE_IO_DECODE BIT(0) 166#define IO_BASE_IO_DECODE_BIT8 BIT(8) 167 168#define CFG_PREF_MEM_LIMIT_BASE_MEM_DECODE BIT(0) 169#define CFG_PREF_MEM_LIMIT_BASE_MEM_LIMIT_DECODE BIT(16) 170 171#define CFG_TIMER_CTRL_MAX_FUNC_NUM_OFF 0x718 172#define CFG_TIMER_CTRL_ACK_NAK_SHIFT (19) 173 174#define EVENT_COUNTER_ALL_CLEAR 0x3 175#define EVENT_COUNTER_ENABLE_ALL 0x7 176#define EVENT_COUNTER_ENABLE_SHIFT 2 177#define EVENT_COUNTER_EVENT_SEL_MASK GENMASK(7, 0) 178#define EVENT_COUNTER_EVENT_SEL_SHIFT 16 179#define EVENT_COUNTER_EVENT_Tx_L0S 0x2 180#define EVENT_COUNTER_EVENT_Rx_L0S 0x3 181#define EVENT_COUNTER_EVENT_L1 0x5 182#define EVENT_COUNTER_EVENT_L1_1 0x7 183#define EVENT_COUNTER_EVENT_L1_2 0x8 184#define EVENT_COUNTER_GROUP_SEL_SHIFT 24 185#define EVENT_COUNTER_GROUP_5 0x5 186 187#define N_FTS_VAL 52 188#define FTS_VAL 52 189 190#define PORT_LOGIC_MSI_CTRL_INT_0_EN 0x828 191 192#define GEN3_EQ_CONTROL_OFF 0x8a8 193#define GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_SHIFT 8 194#define GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_MASK GENMASK(23, 8) 195#define GEN3_EQ_CONTROL_OFF_FB_MODE_MASK GENMASK(3, 0) 196 197#define GEN3_RELATED_OFF 0x890 198#define GEN3_RELATED_OFF_GEN3_ZRXDC_NONCOMPL BIT(0) 199#define GEN3_RELATED_OFF_GEN3_EQ_DISABLE BIT(16) 200#define GEN3_RELATED_OFF_RATE_SHADOW_SEL_SHIFT 24 201#define GEN3_RELATED_OFF_RATE_SHADOW_SEL_MASK GENMASK(25, 24) 202 203#define PORT_LOGIC_AMBA_ERROR_RESPONSE_DEFAULT 0x8D0 204#define AMBA_ERROR_RESPONSE_CRS_SHIFT 3 205#define AMBA_ERROR_RESPONSE_CRS_MASK GENMASK(1, 0) 206#define AMBA_ERROR_RESPONSE_CRS_OKAY 0 207#define AMBA_ERROR_RESPONSE_CRS_OKAY_FFFFFFFF 1 208#define AMBA_ERROR_RESPONSE_CRS_OKAY_FFFF0001 2 209 210#define MSIX_ADDR_MATCH_LOW_OFF 0x940 211#define MSIX_ADDR_MATCH_LOW_OFF_EN BIT(0) 212#define MSIX_ADDR_MATCH_LOW_OFF_MASK GENMASK(31, 2) 213 214#define MSIX_ADDR_MATCH_HIGH_OFF 0x944 215#define MSIX_ADDR_MATCH_HIGH_OFF_MASK GENMASK(31, 0) 216 217#define PORT_LOGIC_MSIX_DOORBELL 0x948 218 219#define CAP_SPCIE_CAP_OFF 0x154 220#define CAP_SPCIE_CAP_OFF_DSP_TX_PRESET0_MASK GENMASK(3, 0) 221#define CAP_SPCIE_CAP_OFF_USP_TX_PRESET0_MASK GENMASK(11, 8) 222#define CAP_SPCIE_CAP_OFF_USP_TX_PRESET0_SHIFT 8 223 224#define PME_ACK_TIMEOUT 10000 225 226#define LTSSM_TIMEOUT 50000 /* 50ms */ 227 228#define GEN3_GEN4_EQ_PRESET_INIT 5 229 230#define GEN1_CORE_CLK_FREQ 62500000 231#define GEN2_CORE_CLK_FREQ 125000000 232#define GEN3_CORE_CLK_FREQ 250000000 233#define GEN4_CORE_CLK_FREQ 500000000 234 235#define LTR_MSG_TIMEOUT (100 * 1000) 236 237#define PERST_DEBOUNCE_TIME (5 * 1000) 238 239#define EP_STATE_DISABLED 0 240#define EP_STATE_ENABLED 1 241 242static const unsigned int pcie_gen_freq[] = { 243 GEN1_CORE_CLK_FREQ, /* PCI_EXP_LNKSTA_CLS == 0; undefined */ 244 GEN1_CORE_CLK_FREQ, 245 GEN2_CORE_CLK_FREQ, 246 GEN3_CORE_CLK_FREQ, 247 GEN4_CORE_CLK_FREQ 248}; 249 250static const u32 event_cntr_ctrl_offset[] = { 251 0x1d8, 252 0x1a8, 253 0x1a8, 254 0x1a8, 255 0x1c4, 256 0x1d8 257}; 258 259static const u32 event_cntr_data_offset[] = { 260 0x1dc, 261 0x1ac, 262 0x1ac, 263 0x1ac, 264 0x1c8, 265 0x1dc 266}; 267 268struct tegra_pcie_dw { 269 struct device *dev; 270 struct resource *appl_res; 271 struct resource *dbi_res; 272 struct resource *atu_dma_res; 273 void __iomem *appl_base; 274 struct clk *core_clk; 275 struct reset_control *core_apb_rst; 276 struct reset_control *core_rst; 277 struct dw_pcie pci; 278 struct tegra_bpmp *bpmp; 279 280 enum dw_pcie_device_mode mode; 281 282 bool supports_clkreq; 283 bool enable_cdm_check; 284 bool link_state; 285 bool update_fc_fixup; 286 u8 init_link_width; 287 u32 msi_ctrl_int; 288 u32 num_lanes; 289 u32 cid; 290 u32 cfg_link_cap_l1sub; 291 u32 pcie_cap_base; 292 u32 aspm_cmrt; 293 u32 aspm_pwr_on_t; 294 u32 aspm_l0s_enter_lat; 295 296 struct regulator *pex_ctl_supply; 297 struct regulator *slot_ctl_3v3; 298 struct regulator *slot_ctl_12v; 299 300 unsigned int phy_count; 301 struct phy **phys; 302 303 struct dentry *debugfs; 304 305 /* Endpoint mode specific */ 306 struct gpio_desc *pex_rst_gpiod; 307 struct gpio_desc *pex_refclk_sel_gpiod; 308 unsigned int pex_rst_irq; 309 int ep_state; 310}; 311 312struct tegra_pcie_dw_of_data { 313 enum dw_pcie_device_mode mode; 314}; 315 316static inline struct tegra_pcie_dw *to_tegra_pcie(struct dw_pcie *pci) 317{ 318 return container_of(pci, struct tegra_pcie_dw, pci); 319} 320 321static inline void appl_writel(struct tegra_pcie_dw *pcie, const u32 value, 322 const u32 reg) 323{ 324 writel_relaxed(value, pcie->appl_base + reg); 325} 326 327static inline u32 appl_readl(struct tegra_pcie_dw *pcie, const u32 reg) 328{ 329 return readl_relaxed(pcie->appl_base + reg); 330} 331 332struct tegra_pcie_soc { 333 enum dw_pcie_device_mode mode; 334}; 335 336static void apply_bad_link_workaround(struct pcie_port *pp) 337{ 338 struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 339 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci); 340 u32 current_link_width; 341 u16 val; 342 343 /* 344 * NOTE:- Since this scenario is uncommon and link as such is not 345 * stable anyway, not waiting to confirm if link is really 346 * transitioning to Gen-2 speed 347 */ 348 val = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA); 349 if (val & PCI_EXP_LNKSTA_LBMS) { 350 current_link_width = FIELD_GET(PCI_EXP_LNKSTA_NLW, val); 351 if (pcie->init_link_width > current_link_width) { 352 dev_warn(pci->dev, "PCIe link is bad, width reduced\n"); 353 val = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + 354 PCI_EXP_LNKCTL2); 355 val &= ~PCI_EXP_LNKCTL2_TLS; 356 val |= PCI_EXP_LNKCTL2_TLS_2_5GT; 357 dw_pcie_writew_dbi(pci, pcie->pcie_cap_base + 358 PCI_EXP_LNKCTL2, val); 359 360 val = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + 361 PCI_EXP_LNKCTL); 362 val |= PCI_EXP_LNKCTL_RL; 363 dw_pcie_writew_dbi(pci, pcie->pcie_cap_base + 364 PCI_EXP_LNKCTL, val); 365 } 366 } 367} 368 369static irqreturn_t tegra_pcie_rp_irq_handler(int irq, void *arg) 370{ 371 struct tegra_pcie_dw *pcie = arg; 372 struct dw_pcie *pci = &pcie->pci; 373 struct pcie_port *pp = &pci->pp; 374 u32 val, status_l0, status_l1; 375 u16 val_w; 376 377 status_l0 = appl_readl(pcie, APPL_INTR_STATUS_L0); 378 if (status_l0 & APPL_INTR_STATUS_L0_LINK_STATE_INT) { 379 status_l1 = appl_readl(pcie, APPL_INTR_STATUS_L1_0_0); 380 appl_writel(pcie, status_l1, APPL_INTR_STATUS_L1_0_0); 381 if (status_l1 & APPL_INTR_STATUS_L1_0_0_LINK_REQ_RST_NOT_CHGED) { 382 /* SBR & Surprise Link Down WAR */ 383 val = appl_readl(pcie, APPL_CAR_RESET_OVRD); 384 val &= ~APPL_CAR_RESET_OVRD_CYA_OVERRIDE_CORE_RST_N; 385 appl_writel(pcie, val, APPL_CAR_RESET_OVRD); 386 udelay(1); 387 val = appl_readl(pcie, APPL_CAR_RESET_OVRD); 388 val |= APPL_CAR_RESET_OVRD_CYA_OVERRIDE_CORE_RST_N; 389 appl_writel(pcie, val, APPL_CAR_RESET_OVRD); 390 391 val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL); 392 val |= PORT_LOGIC_SPEED_CHANGE; 393 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val); 394 } 395 } 396 397 if (status_l0 & APPL_INTR_STATUS_L0_INT_INT) { 398 status_l1 = appl_readl(pcie, APPL_INTR_STATUS_L1_8_0); 399 if (status_l1 & APPL_INTR_STATUS_L1_8_0_AUTO_BW_INT_STS) { 400 appl_writel(pcie, 401 APPL_INTR_STATUS_L1_8_0_AUTO_BW_INT_STS, 402 APPL_INTR_STATUS_L1_8_0); 403 apply_bad_link_workaround(pp); 404 } 405 if (status_l1 & APPL_INTR_STATUS_L1_8_0_BW_MGT_INT_STS) { 406 appl_writel(pcie, 407 APPL_INTR_STATUS_L1_8_0_BW_MGT_INT_STS, 408 APPL_INTR_STATUS_L1_8_0); 409 410 val_w = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + 411 PCI_EXP_LNKSTA); 412 dev_dbg(pci->dev, "Link Speed : Gen-%u\n", val_w & 413 PCI_EXP_LNKSTA_CLS); 414 } 415 } 416 417 if (status_l0 & APPL_INTR_STATUS_L0_CDM_REG_CHK_INT) { 418 status_l1 = appl_readl(pcie, APPL_INTR_STATUS_L1_18); 419 val = dw_pcie_readl_dbi(pci, PCIE_PL_CHK_REG_CONTROL_STATUS); 420 if (status_l1 & APPL_INTR_STATUS_L1_18_CDM_REG_CHK_CMPLT) { 421 dev_info(pci->dev, "CDM check complete\n"); 422 val |= PCIE_PL_CHK_REG_CHK_REG_COMPLETE; 423 } 424 if (status_l1 & APPL_INTR_STATUS_L1_18_CDM_REG_CHK_CMP_ERR) { 425 dev_err(pci->dev, "CDM comparison mismatch\n"); 426 val |= PCIE_PL_CHK_REG_CHK_REG_COMPARISON_ERROR; 427 } 428 if (status_l1 & APPL_INTR_STATUS_L1_18_CDM_REG_CHK_LOGIC_ERR) { 429 dev_err(pci->dev, "CDM Logic error\n"); 430 val |= PCIE_PL_CHK_REG_CHK_REG_LOGIC_ERROR; 431 } 432 dw_pcie_writel_dbi(pci, PCIE_PL_CHK_REG_CONTROL_STATUS, val); 433 val = dw_pcie_readl_dbi(pci, PCIE_PL_CHK_REG_ERR_ADDR); 434 dev_err(pci->dev, "CDM Error Address Offset = 0x%08X\n", val); 435 } 436 437 return IRQ_HANDLED; 438} 439 440static void pex_ep_event_hot_rst_done(struct tegra_pcie_dw *pcie) 441{ 442 u32 val; 443 444 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L0); 445 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_0_0); 446 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_1); 447 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_2); 448 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_3); 449 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_6); 450 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_7); 451 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_8_0); 452 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_9); 453 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_10); 454 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_11); 455 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_13); 456 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_14); 457 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_15); 458 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_17); 459 appl_writel(pcie, 0xFFFFFFFF, APPL_MSI_CTRL_2); 460 461 val = appl_readl(pcie, APPL_CTRL); 462 val |= APPL_CTRL_LTSSM_EN; 463 appl_writel(pcie, val, APPL_CTRL); 464} 465 466static irqreturn_t tegra_pcie_ep_irq_thread(int irq, void *arg) 467{ 468 struct tegra_pcie_dw *pcie = arg; 469 struct dw_pcie *pci = &pcie->pci; 470 u32 val, speed; 471 472 speed = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA) & 473 PCI_EXP_LNKSTA_CLS; 474 475 if (speed >= ARRAY_SIZE(pcie_gen_freq)) 476 speed = 0; 477 478 clk_set_rate(pcie->core_clk, pcie_gen_freq[speed]); 479 480 /* If EP doesn't advertise L1SS, just return */ 481 val = dw_pcie_readl_dbi(pci, pcie->cfg_link_cap_l1sub); 482 if (!(val & (PCI_L1SS_CAP_ASPM_L1_1 | PCI_L1SS_CAP_ASPM_L1_2))) 483 return IRQ_HANDLED; 484 485 /* Check if BME is set to '1' */ 486 val = dw_pcie_readl_dbi(pci, PCI_COMMAND); 487 if (val & PCI_COMMAND_MASTER) { 488 ktime_t timeout; 489 490 /* 110us for both snoop and no-snoop */ 491 val = 110 | (2 << PCI_LTR_SCALE_SHIFT) | LTR_MSG_REQ; 492 val |= (val << LTR_MST_NO_SNOOP_SHIFT); 493 appl_writel(pcie, val, APPL_LTR_MSG_1); 494 495 /* Send LTR upstream */ 496 val = appl_readl(pcie, APPL_LTR_MSG_2); 497 val |= APPL_LTR_MSG_2_LTR_MSG_REQ_STATE; 498 appl_writel(pcie, val, APPL_LTR_MSG_2); 499 500 timeout = ktime_add_us(ktime_get(), LTR_MSG_TIMEOUT); 501 for (;;) { 502 val = appl_readl(pcie, APPL_LTR_MSG_2); 503 if (!(val & APPL_LTR_MSG_2_LTR_MSG_REQ_STATE)) 504 break; 505 if (ktime_after(ktime_get(), timeout)) 506 break; 507 usleep_range(1000, 1100); 508 } 509 if (val & APPL_LTR_MSG_2_LTR_MSG_REQ_STATE) 510 dev_err(pcie->dev, "Failed to send LTR message\n"); 511 } 512 513 return IRQ_HANDLED; 514} 515 516static irqreturn_t tegra_pcie_ep_hard_irq(int irq, void *arg) 517{ 518 struct tegra_pcie_dw *pcie = arg; 519 struct dw_pcie_ep *ep = &pcie->pci.ep; 520 int spurious = 1; 521 u32 status_l0, status_l1, link_status; 522 523 status_l0 = appl_readl(pcie, APPL_INTR_STATUS_L0); 524 if (status_l0 & APPL_INTR_STATUS_L0_LINK_STATE_INT) { 525 status_l1 = appl_readl(pcie, APPL_INTR_STATUS_L1_0_0); 526 appl_writel(pcie, status_l1, APPL_INTR_STATUS_L1_0_0); 527 528 if (status_l1 & APPL_INTR_STATUS_L1_0_0_HOT_RESET_DONE) 529 pex_ep_event_hot_rst_done(pcie); 530 531 if (status_l1 & APPL_INTR_STATUS_L1_0_0_RDLH_LINK_UP_CHGED) { 532 link_status = appl_readl(pcie, APPL_LINK_STATUS); 533 if (link_status & APPL_LINK_STATUS_RDLH_LINK_UP) { 534 dev_dbg(pcie->dev, "Link is up with Host\n"); 535 dw_pcie_ep_linkup(ep); 536 } 537 } 538 539 spurious = 0; 540 } 541 542 if (status_l0 & APPL_INTR_STATUS_L0_PCI_CMD_EN_INT) { 543 status_l1 = appl_readl(pcie, APPL_INTR_STATUS_L1_15); 544 appl_writel(pcie, status_l1, APPL_INTR_STATUS_L1_15); 545 546 if (status_l1 & APPL_INTR_STATUS_L1_15_CFG_BME_CHGED) 547 return IRQ_WAKE_THREAD; 548 549 spurious = 0; 550 } 551 552 if (spurious) { 553 dev_warn(pcie->dev, "Random interrupt (STATUS = 0x%08X)\n", 554 status_l0); 555 appl_writel(pcie, status_l0, APPL_INTR_STATUS_L0); 556 } 557 558 return IRQ_HANDLED; 559} 560 561static int tegra_pcie_dw_rd_own_conf(struct pci_bus *bus, u32 devfn, int where, 562 int size, u32 *val) 563{ 564 /* 565 * This is an endpoint mode specific register happen to appear even 566 * when controller is operating in root port mode and system hangs 567 * when it is accessed with link being in ASPM-L1 state. 568 * So skip accessing it altogether 569 */ 570 if (!PCI_SLOT(devfn) && where == PORT_LOGIC_MSIX_DOORBELL) { 571 *val = 0x00000000; 572 return PCIBIOS_SUCCESSFUL; 573 } 574 575 return pci_generic_config_read(bus, devfn, where, size, val); 576} 577 578static int tegra_pcie_dw_wr_own_conf(struct pci_bus *bus, u32 devfn, int where, 579 int size, u32 val) 580{ 581 /* 582 * This is an endpoint mode specific register happen to appear even 583 * when controller is operating in root port mode and system hangs 584 * when it is accessed with link being in ASPM-L1 state. 585 * So skip accessing it altogether 586 */ 587 if (!PCI_SLOT(devfn) && where == PORT_LOGIC_MSIX_DOORBELL) 588 return PCIBIOS_SUCCESSFUL; 589 590 return pci_generic_config_write(bus, devfn, where, size, val); 591} 592 593static struct pci_ops tegra_pci_ops = { 594 .map_bus = dw_pcie_own_conf_map_bus, 595 .read = tegra_pcie_dw_rd_own_conf, 596 .write = tegra_pcie_dw_wr_own_conf, 597}; 598 599#if defined(CONFIG_PCIEASPM) 600static void disable_aspm_l11(struct tegra_pcie_dw *pcie) 601{ 602 u32 val; 603 604 val = dw_pcie_readl_dbi(&pcie->pci, pcie->cfg_link_cap_l1sub); 605 val &= ~PCI_L1SS_CAP_ASPM_L1_1; 606 dw_pcie_writel_dbi(&pcie->pci, pcie->cfg_link_cap_l1sub, val); 607} 608 609static void disable_aspm_l12(struct tegra_pcie_dw *pcie) 610{ 611 u32 val; 612 613 val = dw_pcie_readl_dbi(&pcie->pci, pcie->cfg_link_cap_l1sub); 614 val &= ~PCI_L1SS_CAP_ASPM_L1_2; 615 dw_pcie_writel_dbi(&pcie->pci, pcie->cfg_link_cap_l1sub, val); 616} 617 618static inline u32 event_counter_prog(struct tegra_pcie_dw *pcie, u32 event) 619{ 620 u32 val; 621 622 val = dw_pcie_readl_dbi(&pcie->pci, event_cntr_ctrl_offset[pcie->cid]); 623 val &= ~(EVENT_COUNTER_EVENT_SEL_MASK << EVENT_COUNTER_EVENT_SEL_SHIFT); 624 val |= EVENT_COUNTER_GROUP_5 << EVENT_COUNTER_GROUP_SEL_SHIFT; 625 val |= event << EVENT_COUNTER_EVENT_SEL_SHIFT; 626 val |= EVENT_COUNTER_ENABLE_ALL << EVENT_COUNTER_ENABLE_SHIFT; 627 dw_pcie_writel_dbi(&pcie->pci, event_cntr_ctrl_offset[pcie->cid], val); 628 val = dw_pcie_readl_dbi(&pcie->pci, event_cntr_data_offset[pcie->cid]); 629 630 return val; 631} 632 633static int aspm_state_cnt(struct seq_file *s, void *data) 634{ 635 struct tegra_pcie_dw *pcie = (struct tegra_pcie_dw *) 636 dev_get_drvdata(s->private); 637 u32 val; 638 639 seq_printf(s, "Tx L0s entry count : %u\n", 640 event_counter_prog(pcie, EVENT_COUNTER_EVENT_Tx_L0S)); 641 642 seq_printf(s, "Rx L0s entry count : %u\n", 643 event_counter_prog(pcie, EVENT_COUNTER_EVENT_Rx_L0S)); 644 645 seq_printf(s, "Link L1 entry count : %u\n", 646 event_counter_prog(pcie, EVENT_COUNTER_EVENT_L1)); 647 648 seq_printf(s, "Link L1.1 entry count : %u\n", 649 event_counter_prog(pcie, EVENT_COUNTER_EVENT_L1_1)); 650 651 seq_printf(s, "Link L1.2 entry count : %u\n", 652 event_counter_prog(pcie, EVENT_COUNTER_EVENT_L1_2)); 653 654 /* Clear all counters */ 655 dw_pcie_writel_dbi(&pcie->pci, event_cntr_ctrl_offset[pcie->cid], 656 EVENT_COUNTER_ALL_CLEAR); 657 658 /* Re-enable counting */ 659 val = EVENT_COUNTER_ENABLE_ALL << EVENT_COUNTER_ENABLE_SHIFT; 660 val |= EVENT_COUNTER_GROUP_5 << EVENT_COUNTER_GROUP_SEL_SHIFT; 661 dw_pcie_writel_dbi(&pcie->pci, event_cntr_ctrl_offset[pcie->cid], val); 662 663 return 0; 664} 665 666static void init_host_aspm(struct tegra_pcie_dw *pcie) 667{ 668 struct dw_pcie *pci = &pcie->pci; 669 u32 val; 670 671 val = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_L1SS); 672 pcie->cfg_link_cap_l1sub = val + PCI_L1SS_CAP; 673 674 /* Enable ASPM counters */ 675 val = EVENT_COUNTER_ENABLE_ALL << EVENT_COUNTER_ENABLE_SHIFT; 676 val |= EVENT_COUNTER_GROUP_5 << EVENT_COUNTER_GROUP_SEL_SHIFT; 677 dw_pcie_writel_dbi(pci, event_cntr_ctrl_offset[pcie->cid], val); 678 679 /* Program T_cmrt and T_pwr_on values */ 680 val = dw_pcie_readl_dbi(pci, pcie->cfg_link_cap_l1sub); 681 val &= ~(PCI_L1SS_CAP_CM_RESTORE_TIME | PCI_L1SS_CAP_P_PWR_ON_VALUE); 682 val |= (pcie->aspm_cmrt << 8); 683 val |= (pcie->aspm_pwr_on_t << 19); 684 dw_pcie_writel_dbi(pci, pcie->cfg_link_cap_l1sub, val); 685 686 /* Program L0s and L1 entrance latencies */ 687 val = dw_pcie_readl_dbi(pci, PCIE_PORT_AFR); 688 val &= ~PORT_AFR_L0S_ENTRANCE_LAT_MASK; 689 val |= (pcie->aspm_l0s_enter_lat << PORT_AFR_L0S_ENTRANCE_LAT_SHIFT); 690 val |= PORT_AFR_ENTER_ASPM; 691 dw_pcie_writel_dbi(pci, PCIE_PORT_AFR, val); 692} 693 694static void init_debugfs(struct tegra_pcie_dw *pcie) 695{ 696 debugfs_create_devm_seqfile(pcie->dev, "aspm_state_cnt", pcie->debugfs, 697 aspm_state_cnt); 698} 699#else 700static inline void disable_aspm_l12(struct tegra_pcie_dw *pcie) { return; } 701static inline void disable_aspm_l11(struct tegra_pcie_dw *pcie) { return; } 702static inline void init_host_aspm(struct tegra_pcie_dw *pcie) { return; } 703static inline void init_debugfs(struct tegra_pcie_dw *pcie) { return; } 704#endif 705 706static void tegra_pcie_enable_system_interrupts(struct pcie_port *pp) 707{ 708 struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 709 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci); 710 u32 val; 711 u16 val_w; 712 713 val = appl_readl(pcie, APPL_INTR_EN_L0_0); 714 val |= APPL_INTR_EN_L0_0_LINK_STATE_INT_EN; 715 appl_writel(pcie, val, APPL_INTR_EN_L0_0); 716 717 val = appl_readl(pcie, APPL_INTR_EN_L1_0_0); 718 val |= APPL_INTR_EN_L1_0_0_LINK_REQ_RST_NOT_INT_EN; 719 appl_writel(pcie, val, APPL_INTR_EN_L1_0_0); 720 721 if (pcie->enable_cdm_check) { 722 val = appl_readl(pcie, APPL_INTR_EN_L0_0); 723 val |= APPL_INTR_EN_L0_0_CDM_REG_CHK_INT_EN; 724 appl_writel(pcie, val, APPL_INTR_EN_L0_0); 725 726 val = appl_readl(pcie, APPL_INTR_EN_L1_18); 727 val |= APPL_INTR_EN_L1_18_CDM_REG_CHK_CMP_ERR; 728 val |= APPL_INTR_EN_L1_18_CDM_REG_CHK_LOGIC_ERR; 729 appl_writel(pcie, val, APPL_INTR_EN_L1_18); 730 } 731 732 val_w = dw_pcie_readw_dbi(&pcie->pci, pcie->pcie_cap_base + 733 PCI_EXP_LNKSTA); 734 pcie->init_link_width = FIELD_GET(PCI_EXP_LNKSTA_NLW, val_w); 735 736 val_w = dw_pcie_readw_dbi(&pcie->pci, pcie->pcie_cap_base + 737 PCI_EXP_LNKCTL); 738 val_w |= PCI_EXP_LNKCTL_LBMIE; 739 dw_pcie_writew_dbi(&pcie->pci, pcie->pcie_cap_base + PCI_EXP_LNKCTL, 740 val_w); 741} 742 743static void tegra_pcie_enable_legacy_interrupts(struct pcie_port *pp) 744{ 745 struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 746 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci); 747 u32 val; 748 749 /* Enable legacy interrupt generation */ 750 val = appl_readl(pcie, APPL_INTR_EN_L0_0); 751 val |= APPL_INTR_EN_L0_0_SYS_INTR_EN; 752 val |= APPL_INTR_EN_L0_0_INT_INT_EN; 753 appl_writel(pcie, val, APPL_INTR_EN_L0_0); 754 755 val = appl_readl(pcie, APPL_INTR_EN_L1_8_0); 756 val |= APPL_INTR_EN_L1_8_INTX_EN; 757 val |= APPL_INTR_EN_L1_8_AUTO_BW_INT_EN; 758 val |= APPL_INTR_EN_L1_8_BW_MGT_INT_EN; 759 if (IS_ENABLED(CONFIG_PCIEAER)) 760 val |= APPL_INTR_EN_L1_8_AER_INT_EN; 761 appl_writel(pcie, val, APPL_INTR_EN_L1_8_0); 762} 763 764static void tegra_pcie_enable_msi_interrupts(struct pcie_port *pp) 765{ 766 struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 767 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci); 768 u32 val; 769 770 dw_pcie_msi_init(pp); 771 772 /* Enable MSI interrupt generation */ 773 val = appl_readl(pcie, APPL_INTR_EN_L0_0); 774 val |= APPL_INTR_EN_L0_0_SYS_MSI_INTR_EN; 775 val |= APPL_INTR_EN_L0_0_MSI_RCV_INT_EN; 776 appl_writel(pcie, val, APPL_INTR_EN_L0_0); 777} 778 779static void tegra_pcie_enable_interrupts(struct pcie_port *pp) 780{ 781 struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 782 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci); 783 784 /* Clear interrupt statuses before enabling interrupts */ 785 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L0); 786 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_0_0); 787 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_1); 788 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_2); 789 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_3); 790 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_6); 791 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_7); 792 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_8_0); 793 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_9); 794 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_10); 795 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_11); 796 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_13); 797 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_14); 798 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_15); 799 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_17); 800 801 tegra_pcie_enable_system_interrupts(pp); 802 tegra_pcie_enable_legacy_interrupts(pp); 803 if (IS_ENABLED(CONFIG_PCI_MSI)) 804 tegra_pcie_enable_msi_interrupts(pp); 805} 806 807static void config_gen3_gen4_eq_presets(struct tegra_pcie_dw *pcie) 808{ 809 struct dw_pcie *pci = &pcie->pci; 810 u32 val, offset, i; 811 812 /* Program init preset */ 813 for (i = 0; i < pcie->num_lanes; i++) { 814 val = dw_pcie_readw_dbi(pci, CAP_SPCIE_CAP_OFF + (i * 2)); 815 val &= ~CAP_SPCIE_CAP_OFF_DSP_TX_PRESET0_MASK; 816 val |= GEN3_GEN4_EQ_PRESET_INIT; 817 val &= ~CAP_SPCIE_CAP_OFF_USP_TX_PRESET0_MASK; 818 val |= (GEN3_GEN4_EQ_PRESET_INIT << 819 CAP_SPCIE_CAP_OFF_USP_TX_PRESET0_SHIFT); 820 dw_pcie_writew_dbi(pci, CAP_SPCIE_CAP_OFF + (i * 2), val); 821 822 offset = dw_pcie_find_ext_capability(pci, 823 PCI_EXT_CAP_ID_PL_16GT) + 824 PCI_PL_16GT_LE_CTRL; 825 val = dw_pcie_readb_dbi(pci, offset + i); 826 val &= ~PCI_PL_16GT_LE_CTRL_DSP_TX_PRESET_MASK; 827 val |= GEN3_GEN4_EQ_PRESET_INIT; 828 val &= ~PCI_PL_16GT_LE_CTRL_USP_TX_PRESET_MASK; 829 val |= (GEN3_GEN4_EQ_PRESET_INIT << 830 PCI_PL_16GT_LE_CTRL_USP_TX_PRESET_SHIFT); 831 dw_pcie_writeb_dbi(pci, offset + i, val); 832 } 833 834 val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF); 835 val &= ~GEN3_RELATED_OFF_RATE_SHADOW_SEL_MASK; 836 dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val); 837 838 val = dw_pcie_readl_dbi(pci, GEN3_EQ_CONTROL_OFF); 839 val &= ~GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_MASK; 840 val |= (0x3ff << GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_SHIFT); 841 val &= ~GEN3_EQ_CONTROL_OFF_FB_MODE_MASK; 842 dw_pcie_writel_dbi(pci, GEN3_EQ_CONTROL_OFF, val); 843 844 val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF); 845 val &= ~GEN3_RELATED_OFF_RATE_SHADOW_SEL_MASK; 846 val |= (0x1 << GEN3_RELATED_OFF_RATE_SHADOW_SEL_SHIFT); 847 dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val); 848 849 val = dw_pcie_readl_dbi(pci, GEN3_EQ_CONTROL_OFF); 850 val &= ~GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_MASK; 851 val |= (0x360 << GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC_SHIFT); 852 val &= ~GEN3_EQ_CONTROL_OFF_FB_MODE_MASK; 853 dw_pcie_writel_dbi(pci, GEN3_EQ_CONTROL_OFF, val); 854 855 val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF); 856 val &= ~GEN3_RELATED_OFF_RATE_SHADOW_SEL_MASK; 857 dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val); 858} 859 860static void tegra_pcie_prepare_host(struct pcie_port *pp) 861{ 862 struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 863 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci); 864 u32 val; 865 866 val = dw_pcie_readl_dbi(pci, PCI_IO_BASE); 867 val &= ~(IO_BASE_IO_DECODE | IO_BASE_IO_DECODE_BIT8); 868 dw_pcie_writel_dbi(pci, PCI_IO_BASE, val); 869 870 val = dw_pcie_readl_dbi(pci, PCI_PREF_MEMORY_BASE); 871 val |= CFG_PREF_MEM_LIMIT_BASE_MEM_DECODE; 872 val |= CFG_PREF_MEM_LIMIT_BASE_MEM_LIMIT_DECODE; 873 dw_pcie_writel_dbi(pci, PCI_PREF_MEMORY_BASE, val); 874 875 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0); 876 877 /* Enable as 0xFFFF0001 response for CRS */ 878 val = dw_pcie_readl_dbi(pci, PORT_LOGIC_AMBA_ERROR_RESPONSE_DEFAULT); 879 val &= ~(AMBA_ERROR_RESPONSE_CRS_MASK << AMBA_ERROR_RESPONSE_CRS_SHIFT); 880 val |= (AMBA_ERROR_RESPONSE_CRS_OKAY_FFFF0001 << 881 AMBA_ERROR_RESPONSE_CRS_SHIFT); 882 dw_pcie_writel_dbi(pci, PORT_LOGIC_AMBA_ERROR_RESPONSE_DEFAULT, val); 883 884 /* Configure Max lane width from DT */ 885 val = dw_pcie_readl_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKCAP); 886 val &= ~PCI_EXP_LNKCAP_MLW; 887 val |= FIELD_PREP(PCI_EXP_LNKCAP_MLW, pcie->num_lanes); 888 dw_pcie_writel_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKCAP, val); 889 890 config_gen3_gen4_eq_presets(pcie); 891 892 init_host_aspm(pcie); 893 894 val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF); 895 val &= ~GEN3_RELATED_OFF_GEN3_ZRXDC_NONCOMPL; 896 dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val); 897 898 if (pcie->update_fc_fixup) { 899 val = dw_pcie_readl_dbi(pci, CFG_TIMER_CTRL_MAX_FUNC_NUM_OFF); 900 val |= 0x1 << CFG_TIMER_CTRL_ACK_NAK_SHIFT; 901 dw_pcie_writel_dbi(pci, CFG_TIMER_CTRL_MAX_FUNC_NUM_OFF, val); 902 } 903 904 dw_pcie_setup_rc(pp); 905 906 clk_set_rate(pcie->core_clk, GEN4_CORE_CLK_FREQ); 907 908 /* Assert RST */ 909 val = appl_readl(pcie, APPL_PINMUX); 910 val &= ~APPL_PINMUX_PEX_RST; 911 appl_writel(pcie, val, APPL_PINMUX); 912 913 usleep_range(100, 200); 914 915 /* Enable LTSSM */ 916 val = appl_readl(pcie, APPL_CTRL); 917 val |= APPL_CTRL_LTSSM_EN; 918 appl_writel(pcie, val, APPL_CTRL); 919 920 /* De-assert RST */ 921 val = appl_readl(pcie, APPL_PINMUX); 922 val |= APPL_PINMUX_PEX_RST; 923 appl_writel(pcie, val, APPL_PINMUX); 924 925 msleep(100); 926} 927 928static int tegra_pcie_dw_host_init(struct pcie_port *pp) 929{ 930 struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 931 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci); 932 u32 val, tmp, offset, speed; 933 934 pp->bridge->ops = &tegra_pci_ops; 935 936 tegra_pcie_prepare_host(pp); 937 938 if (dw_pcie_wait_for_link(pci)) { 939 /* 940 * There are some endpoints which can't get the link up if 941 * root port has Data Link Feature (DLF) enabled. 942 * Refer Spec rev 4.0 ver 1.0 sec 3.4.2 & 7.7.4 for more info 943 * on Scaled Flow Control and DLF. 944 * So, need to confirm that is indeed the case here and attempt 945 * link up once again with DLF disabled. 946 */ 947 val = appl_readl(pcie, APPL_DEBUG); 948 val &= APPL_DEBUG_LTSSM_STATE_MASK; 949 val >>= APPL_DEBUG_LTSSM_STATE_SHIFT; 950 tmp = appl_readl(pcie, APPL_LINK_STATUS); 951 tmp &= APPL_LINK_STATUS_RDLH_LINK_UP; 952 if (!(val == 0x11 && !tmp)) { 953 /* Link is down for all good reasons */ 954 return 0; 955 } 956 957 dev_info(pci->dev, "Link is down in DLL"); 958 dev_info(pci->dev, "Trying again with DLFE disabled\n"); 959 /* Disable LTSSM */ 960 val = appl_readl(pcie, APPL_CTRL); 961 val &= ~APPL_CTRL_LTSSM_EN; 962 appl_writel(pcie, val, APPL_CTRL); 963 964 reset_control_assert(pcie->core_rst); 965 reset_control_deassert(pcie->core_rst); 966 967 offset = dw_pcie_find_ext_capability(pci, PCI_EXT_CAP_ID_DLF); 968 val = dw_pcie_readl_dbi(pci, offset + PCI_DLF_CAP); 969 val &= ~PCI_DLF_EXCHANGE_ENABLE; 970 dw_pcie_writel_dbi(pci, offset + PCI_DLF_CAP, val); 971 972 tegra_pcie_prepare_host(pp); 973 974 if (dw_pcie_wait_for_link(pci)) 975 return 0; 976 } 977 978 speed = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA) & 979 PCI_EXP_LNKSTA_CLS; 980 981 if (speed >= ARRAY_SIZE(pcie_gen_freq)) 982 speed = 0; 983 984 clk_set_rate(pcie->core_clk, pcie_gen_freq[speed]); 985 986 tegra_pcie_enable_interrupts(pp); 987 988 return 0; 989} 990 991static int tegra_pcie_dw_link_up(struct dw_pcie *pci) 992{ 993 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci); 994 u32 val = dw_pcie_readw_dbi(pci, pcie->pcie_cap_base + PCI_EXP_LNKSTA); 995 996 return !!(val & PCI_EXP_LNKSTA_DLLLA); 997} 998 999static void tegra_pcie_set_msi_vec_num(struct pcie_port *pp) 1000{ 1001 pp->num_vectors = MAX_MSI_IRQS; 1002} 1003 1004static int tegra_pcie_dw_start_link(struct dw_pcie *pci) 1005{ 1006 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci); 1007 1008 enable_irq(pcie->pex_rst_irq); 1009 1010 return 0; 1011} 1012 1013static void tegra_pcie_dw_stop_link(struct dw_pcie *pci) 1014{ 1015 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci); 1016 1017 disable_irq(pcie->pex_rst_irq); 1018} 1019 1020static const struct dw_pcie_ops tegra_dw_pcie_ops = { 1021 .link_up = tegra_pcie_dw_link_up, 1022 .start_link = tegra_pcie_dw_start_link, 1023 .stop_link = tegra_pcie_dw_stop_link, 1024}; 1025 1026static struct dw_pcie_host_ops tegra_pcie_dw_host_ops = { 1027 .host_init = tegra_pcie_dw_host_init, 1028 .set_num_vectors = tegra_pcie_set_msi_vec_num, 1029}; 1030 1031static void tegra_pcie_disable_phy(struct tegra_pcie_dw *pcie) 1032{ 1033 unsigned int phy_count = pcie->phy_count; 1034 1035 while (phy_count--) { 1036 phy_power_off(pcie->phys[phy_count]); 1037 phy_exit(pcie->phys[phy_count]); 1038 } 1039} 1040 1041static int tegra_pcie_enable_phy(struct tegra_pcie_dw *pcie) 1042{ 1043 unsigned int i; 1044 int ret; 1045 1046 for (i = 0; i < pcie->phy_count; i++) { 1047 ret = phy_init(pcie->phys[i]); 1048 if (ret < 0) 1049 goto phy_power_off; 1050 1051 ret = phy_power_on(pcie->phys[i]); 1052 if (ret < 0) 1053 goto phy_exit; 1054 } 1055 1056 return 0; 1057 1058phy_power_off: 1059 while (i--) { 1060 phy_power_off(pcie->phys[i]); 1061phy_exit: 1062 phy_exit(pcie->phys[i]); 1063 } 1064 1065 return ret; 1066} 1067 1068static int tegra_pcie_dw_parse_dt(struct tegra_pcie_dw *pcie) 1069{ 1070 struct device_node *np = pcie->dev->of_node; 1071 int ret; 1072 1073 ret = of_property_read_u32(np, "nvidia,aspm-cmrt-us", &pcie->aspm_cmrt); 1074 if (ret < 0) { 1075 dev_info(pcie->dev, "Failed to read ASPM T_cmrt: %d\n", ret); 1076 return ret; 1077 } 1078 1079 ret = of_property_read_u32(np, "nvidia,aspm-pwr-on-t-us", 1080 &pcie->aspm_pwr_on_t); 1081 if (ret < 0) 1082 dev_info(pcie->dev, "Failed to read ASPM Power On time: %d\n", 1083 ret); 1084 1085 ret = of_property_read_u32(np, "nvidia,aspm-l0s-entrance-latency-us", 1086 &pcie->aspm_l0s_enter_lat); 1087 if (ret < 0) 1088 dev_info(pcie->dev, 1089 "Failed to read ASPM L0s Entrance latency: %d\n", ret); 1090 1091 ret = of_property_read_u32(np, "num-lanes", &pcie->num_lanes); 1092 if (ret < 0) { 1093 dev_err(pcie->dev, "Failed to read num-lanes: %d\n", ret); 1094 return ret; 1095 } 1096 1097 ret = of_property_read_u32_index(np, "nvidia,bpmp", 1, &pcie->cid); 1098 if (ret) { 1099 dev_err(pcie->dev, "Failed to read Controller-ID: %d\n", ret); 1100 return ret; 1101 } 1102 1103 ret = of_property_count_strings(np, "phy-names"); 1104 if (ret < 0) { 1105 dev_err(pcie->dev, "Failed to find PHY entries: %d\n", 1106 ret); 1107 return ret; 1108 } 1109 pcie->phy_count = ret; 1110 1111 if (of_property_read_bool(np, "nvidia,update-fc-fixup")) 1112 pcie->update_fc_fixup = true; 1113 1114 pcie->supports_clkreq = 1115 of_property_read_bool(pcie->dev->of_node, "supports-clkreq"); 1116 1117 pcie->enable_cdm_check = 1118 of_property_read_bool(np, "snps,enable-cdm-check"); 1119 1120 if (pcie->mode == DW_PCIE_RC_TYPE) 1121 return 0; 1122 1123 /* Endpoint mode specific DT entries */ 1124 pcie->pex_rst_gpiod = devm_gpiod_get(pcie->dev, "reset", GPIOD_IN); 1125 if (IS_ERR(pcie->pex_rst_gpiod)) { 1126 int err = PTR_ERR(pcie->pex_rst_gpiod); 1127 const char *level = KERN_ERR; 1128 1129 if (err == -EPROBE_DEFER) 1130 level = KERN_DEBUG; 1131 1132 dev_printk(level, pcie->dev, 1133 dev_fmt("Failed to get PERST GPIO: %d\n"), 1134 err); 1135 return err; 1136 } 1137 1138 pcie->pex_refclk_sel_gpiod = devm_gpiod_get(pcie->dev, 1139 "nvidia,refclk-select", 1140 GPIOD_OUT_HIGH); 1141 if (IS_ERR(pcie->pex_refclk_sel_gpiod)) { 1142 int err = PTR_ERR(pcie->pex_refclk_sel_gpiod); 1143 const char *level = KERN_ERR; 1144 1145 if (err == -EPROBE_DEFER) 1146 level = KERN_DEBUG; 1147 1148 dev_printk(level, pcie->dev, 1149 dev_fmt("Failed to get REFCLK select GPIOs: %d\n"), 1150 err); 1151 pcie->pex_refclk_sel_gpiod = NULL; 1152 } 1153 1154 return 0; 1155} 1156 1157static int tegra_pcie_bpmp_set_ctrl_state(struct tegra_pcie_dw *pcie, 1158 bool enable) 1159{ 1160 struct mrq_uphy_response resp; 1161 struct tegra_bpmp_message msg; 1162 struct mrq_uphy_request req; 1163 1164 /* Controller-5 doesn't need to have its state set by BPMP-FW */ 1165 if (pcie->cid == 5) 1166 return 0; 1167 1168 memset(&req, 0, sizeof(req)); 1169 memset(&resp, 0, sizeof(resp)); 1170 1171 req.cmd = CMD_UPHY_PCIE_CONTROLLER_STATE; 1172 req.controller_state.pcie_controller = pcie->cid; 1173 req.controller_state.enable = enable; 1174 1175 memset(&msg, 0, sizeof(msg)); 1176 msg.mrq = MRQ_UPHY; 1177 msg.tx.data = &req; 1178 msg.tx.size = sizeof(req); 1179 msg.rx.data = &resp; 1180 msg.rx.size = sizeof(resp); 1181 1182 return tegra_bpmp_transfer(pcie->bpmp, &msg); 1183} 1184 1185static int tegra_pcie_bpmp_set_pll_state(struct tegra_pcie_dw *pcie, 1186 bool enable) 1187{ 1188 struct mrq_uphy_response resp; 1189 struct tegra_bpmp_message msg; 1190 struct mrq_uphy_request req; 1191 1192 memset(&req, 0, sizeof(req)); 1193 memset(&resp, 0, sizeof(resp)); 1194 1195 if (enable) { 1196 req.cmd = CMD_UPHY_PCIE_EP_CONTROLLER_PLL_INIT; 1197 req.ep_ctrlr_pll_init.ep_controller = pcie->cid; 1198 } else { 1199 req.cmd = CMD_UPHY_PCIE_EP_CONTROLLER_PLL_OFF; 1200 req.ep_ctrlr_pll_off.ep_controller = pcie->cid; 1201 } 1202 1203 memset(&msg, 0, sizeof(msg)); 1204 msg.mrq = MRQ_UPHY; 1205 msg.tx.data = &req; 1206 msg.tx.size = sizeof(req); 1207 msg.rx.data = &resp; 1208 msg.rx.size = sizeof(resp); 1209 1210 return tegra_bpmp_transfer(pcie->bpmp, &msg); 1211} 1212 1213static void tegra_pcie_downstream_dev_to_D0(struct tegra_pcie_dw *pcie) 1214{ 1215 struct pcie_port *pp = &pcie->pci.pp; 1216 struct pci_bus *child, *root_bus = NULL; 1217 struct pci_dev *pdev; 1218 1219 /* 1220 * link doesn't go into L2 state with some of the endpoints with Tegra 1221 * if they are not in D0 state. So, need to make sure that immediate 1222 * downstream devices are in D0 state before sending PME_TurnOff to put 1223 * link into L2 state. 1224 * This is as per PCI Express Base r4.0 v1.0 September 27-2017, 1225 * 5.2 Link State Power Management (Page #428). 1226 */ 1227 1228 list_for_each_entry(child, &pp->bridge->bus->children, node) { 1229 /* Bring downstream devices to D0 if they are not already in */ 1230 if (child->parent == pp->bridge->bus) { 1231 root_bus = child; 1232 break; 1233 } 1234 } 1235 1236 if (!root_bus) { 1237 dev_err(pcie->dev, "Failed to find downstream devices\n"); 1238 return; 1239 } 1240 1241 list_for_each_entry(pdev, &root_bus->devices, bus_list) { 1242 if (PCI_SLOT(pdev->devfn) == 0) { 1243 if (pci_set_power_state(pdev, PCI_D0)) 1244 dev_err(pcie->dev, 1245 "Failed to transition %s to D0 state\n", 1246 dev_name(&pdev->dev)); 1247 } 1248 } 1249} 1250 1251static int tegra_pcie_get_slot_regulators(struct tegra_pcie_dw *pcie) 1252{ 1253 pcie->slot_ctl_3v3 = devm_regulator_get_optional(pcie->dev, "vpcie3v3"); 1254 if (IS_ERR(pcie->slot_ctl_3v3)) { 1255 if (PTR_ERR(pcie->slot_ctl_3v3) != -ENODEV) 1256 return PTR_ERR(pcie->slot_ctl_3v3); 1257 1258 pcie->slot_ctl_3v3 = NULL; 1259 } 1260 1261 pcie->slot_ctl_12v = devm_regulator_get_optional(pcie->dev, "vpcie12v"); 1262 if (IS_ERR(pcie->slot_ctl_12v)) { 1263 if (PTR_ERR(pcie->slot_ctl_12v) != -ENODEV) 1264 return PTR_ERR(pcie->slot_ctl_12v); 1265 1266 pcie->slot_ctl_12v = NULL; 1267 } 1268 1269 return 0; 1270} 1271 1272static int tegra_pcie_enable_slot_regulators(struct tegra_pcie_dw *pcie) 1273{ 1274 int ret; 1275 1276 if (pcie->slot_ctl_3v3) { 1277 ret = regulator_enable(pcie->slot_ctl_3v3); 1278 if (ret < 0) { 1279 dev_err(pcie->dev, 1280 "Failed to enable 3.3V slot supply: %d\n", ret); 1281 return ret; 1282 } 1283 } 1284 1285 if (pcie->slot_ctl_12v) { 1286 ret = regulator_enable(pcie->slot_ctl_12v); 1287 if (ret < 0) { 1288 dev_err(pcie->dev, 1289 "Failed to enable 12V slot supply: %d\n", ret); 1290 goto fail_12v_enable; 1291 } 1292 } 1293 1294 /* 1295 * According to PCI Express Card Electromechanical Specification 1296 * Revision 1.1, Table-2.4, T_PVPERL (Power stable to PERST# inactive) 1297 * should be a minimum of 100ms. 1298 */ 1299 if (pcie->slot_ctl_3v3 || pcie->slot_ctl_12v) 1300 msleep(100); 1301 1302 return 0; 1303 1304fail_12v_enable: 1305 if (pcie->slot_ctl_3v3) 1306 regulator_disable(pcie->slot_ctl_3v3); 1307 return ret; 1308} 1309 1310static void tegra_pcie_disable_slot_regulators(struct tegra_pcie_dw *pcie) 1311{ 1312 if (pcie->slot_ctl_12v) 1313 regulator_disable(pcie->slot_ctl_12v); 1314 if (pcie->slot_ctl_3v3) 1315 regulator_disable(pcie->slot_ctl_3v3); 1316} 1317 1318static int tegra_pcie_config_controller(struct tegra_pcie_dw *pcie, 1319 bool en_hw_hot_rst) 1320{ 1321 int ret; 1322 u32 val; 1323 1324 ret = tegra_pcie_bpmp_set_ctrl_state(pcie, true); 1325 if (ret) { 1326 dev_err(pcie->dev, 1327 "Failed to enable controller %u: %d\n", pcie->cid, ret); 1328 return ret; 1329 } 1330 1331 ret = tegra_pcie_enable_slot_regulators(pcie); 1332 if (ret < 0) 1333 goto fail_slot_reg_en; 1334 1335 ret = regulator_enable(pcie->pex_ctl_supply); 1336 if (ret < 0) { 1337 dev_err(pcie->dev, "Failed to enable regulator: %d\n", ret); 1338 goto fail_reg_en; 1339 } 1340 1341 ret = clk_prepare_enable(pcie->core_clk); 1342 if (ret) { 1343 dev_err(pcie->dev, "Failed to enable core clock: %d\n", ret); 1344 goto fail_core_clk; 1345 } 1346 1347 ret = reset_control_deassert(pcie->core_apb_rst); 1348 if (ret) { 1349 dev_err(pcie->dev, "Failed to deassert core APB reset: %d\n", 1350 ret); 1351 goto fail_core_apb_rst; 1352 } 1353 1354 if (en_hw_hot_rst) { 1355 /* Enable HW_HOT_RST mode */ 1356 val = appl_readl(pcie, APPL_CTRL); 1357 val &= ~(APPL_CTRL_HW_HOT_RST_MODE_MASK << 1358 APPL_CTRL_HW_HOT_RST_MODE_SHIFT); 1359 val |= APPL_CTRL_HW_HOT_RST_EN; 1360 appl_writel(pcie, val, APPL_CTRL); 1361 } 1362 1363 ret = tegra_pcie_enable_phy(pcie); 1364 if (ret) { 1365 dev_err(pcie->dev, "Failed to enable PHY: %d\n", ret); 1366 goto fail_phy; 1367 } 1368 1369 /* Update CFG base address */ 1370 appl_writel(pcie, pcie->dbi_res->start & APPL_CFG_BASE_ADDR_MASK, 1371 APPL_CFG_BASE_ADDR); 1372 1373 /* Configure this core for RP mode operation */ 1374 appl_writel(pcie, APPL_DM_TYPE_RP, APPL_DM_TYPE); 1375 1376 appl_writel(pcie, 0x0, APPL_CFG_SLCG_OVERRIDE); 1377 1378 val = appl_readl(pcie, APPL_CTRL); 1379 appl_writel(pcie, val | APPL_CTRL_SYS_PRE_DET_STATE, APPL_CTRL); 1380 1381 val = appl_readl(pcie, APPL_CFG_MISC); 1382 val |= (APPL_CFG_MISC_ARCACHE_VAL << APPL_CFG_MISC_ARCACHE_SHIFT); 1383 appl_writel(pcie, val, APPL_CFG_MISC); 1384 1385 if (!pcie->supports_clkreq) { 1386 val = appl_readl(pcie, APPL_PINMUX); 1387 val |= APPL_PINMUX_CLKREQ_OVERRIDE_EN; 1388 val &= ~APPL_PINMUX_CLKREQ_OVERRIDE; 1389 appl_writel(pcie, val, APPL_PINMUX); 1390 } 1391 1392 /* Update iATU_DMA base address */ 1393 appl_writel(pcie, 1394 pcie->atu_dma_res->start & APPL_CFG_IATU_DMA_BASE_ADDR_MASK, 1395 APPL_CFG_IATU_DMA_BASE_ADDR); 1396 1397 reset_control_deassert(pcie->core_rst); 1398 1399 pcie->pcie_cap_base = dw_pcie_find_capability(&pcie->pci, 1400 PCI_CAP_ID_EXP); 1401 1402 /* Disable ASPM-L1SS advertisement as there is no CLKREQ routing */ 1403 if (!pcie->supports_clkreq) { 1404 disable_aspm_l11(pcie); 1405 disable_aspm_l12(pcie); 1406 } 1407 1408 return ret; 1409 1410fail_phy: 1411 reset_control_assert(pcie->core_apb_rst); 1412fail_core_apb_rst: 1413 clk_disable_unprepare(pcie->core_clk); 1414fail_core_clk: 1415 regulator_disable(pcie->pex_ctl_supply); 1416fail_reg_en: 1417 tegra_pcie_disable_slot_regulators(pcie); 1418fail_slot_reg_en: 1419 tegra_pcie_bpmp_set_ctrl_state(pcie, false); 1420 1421 return ret; 1422} 1423 1424static int __deinit_controller(struct tegra_pcie_dw *pcie) 1425{ 1426 int ret; 1427 1428 ret = reset_control_assert(pcie->core_rst); 1429 if (ret) { 1430 dev_err(pcie->dev, "Failed to assert \"core\" reset: %d\n", 1431 ret); 1432 return ret; 1433 } 1434 1435 tegra_pcie_disable_phy(pcie); 1436 1437 ret = reset_control_assert(pcie->core_apb_rst); 1438 if (ret) { 1439 dev_err(pcie->dev, "Failed to assert APB reset: %d\n", ret); 1440 return ret; 1441 } 1442 1443 clk_disable_unprepare(pcie->core_clk); 1444 1445 ret = regulator_disable(pcie->pex_ctl_supply); 1446 if (ret) { 1447 dev_err(pcie->dev, "Failed to disable regulator: %d\n", ret); 1448 return ret; 1449 } 1450 1451 tegra_pcie_disable_slot_regulators(pcie); 1452 1453 ret = tegra_pcie_bpmp_set_ctrl_state(pcie, false); 1454 if (ret) { 1455 dev_err(pcie->dev, "Failed to disable controller %d: %d\n", 1456 pcie->cid, ret); 1457 return ret; 1458 } 1459 1460 return ret; 1461} 1462 1463static int tegra_pcie_init_controller(struct tegra_pcie_dw *pcie) 1464{ 1465 struct dw_pcie *pci = &pcie->pci; 1466 struct pcie_port *pp = &pci->pp; 1467 int ret; 1468 1469 ret = tegra_pcie_config_controller(pcie, false); 1470 if (ret < 0) 1471 return ret; 1472 1473 pp->ops = &tegra_pcie_dw_host_ops; 1474 1475 ret = dw_pcie_host_init(pp); 1476 if (ret < 0) { 1477 dev_err(pcie->dev, "Failed to add PCIe port: %d\n", ret); 1478 goto fail_host_init; 1479 } 1480 1481 return 0; 1482 1483fail_host_init: 1484 return __deinit_controller(pcie); 1485} 1486 1487static int tegra_pcie_try_link_l2(struct tegra_pcie_dw *pcie) 1488{ 1489 u32 val; 1490 1491 if (!tegra_pcie_dw_link_up(&pcie->pci)) 1492 return 0; 1493 1494 val = appl_readl(pcie, APPL_RADM_STATUS); 1495 val |= APPL_PM_XMT_TURNOFF_STATE; 1496 appl_writel(pcie, val, APPL_RADM_STATUS); 1497 1498 return readl_poll_timeout_atomic(pcie->appl_base + APPL_DEBUG, val, 1499 val & APPL_DEBUG_PM_LINKST_IN_L2_LAT, 1500 1, PME_ACK_TIMEOUT); 1501} 1502 1503static void tegra_pcie_dw_pme_turnoff(struct tegra_pcie_dw *pcie) 1504{ 1505 u32 data; 1506 int err; 1507 1508 if (!tegra_pcie_dw_link_up(&pcie->pci)) { 1509 dev_dbg(pcie->dev, "PCIe link is not up...!\n"); 1510 return; 1511 } 1512 1513 if (tegra_pcie_try_link_l2(pcie)) { 1514 dev_info(pcie->dev, "Link didn't transition to L2 state\n"); 1515 /* 1516 * TX lane clock freq will reset to Gen1 only if link is in L2 1517 * or detect state. 1518 * So apply pex_rst to end point to force RP to go into detect 1519 * state 1520 */ 1521 data = appl_readl(pcie, APPL_PINMUX); 1522 data &= ~APPL_PINMUX_PEX_RST; 1523 appl_writel(pcie, data, APPL_PINMUX); 1524 1525 err = readl_poll_timeout_atomic(pcie->appl_base + APPL_DEBUG, 1526 data, 1527 ((data & 1528 APPL_DEBUG_LTSSM_STATE_MASK) >> 1529 APPL_DEBUG_LTSSM_STATE_SHIFT) == 1530 LTSSM_STATE_PRE_DETECT, 1531 1, LTSSM_TIMEOUT); 1532 if (err) { 1533 dev_info(pcie->dev, "Link didn't go to detect state\n"); 1534 } else { 1535 /* Disable LTSSM after link is in detect state */ 1536 data = appl_readl(pcie, APPL_CTRL); 1537 data &= ~APPL_CTRL_LTSSM_EN; 1538 appl_writel(pcie, data, APPL_CTRL); 1539 } 1540 } 1541 /* 1542 * DBI registers may not be accessible after this as PLL-E would be 1543 * down depending on how CLKREQ is pulled by end point 1544 */ 1545 data = appl_readl(pcie, APPL_PINMUX); 1546 data |= (APPL_PINMUX_CLKREQ_OVERRIDE_EN | APPL_PINMUX_CLKREQ_OVERRIDE); 1547 /* Cut REFCLK to slot */ 1548 data |= APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE_EN; 1549 data &= ~APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE; 1550 appl_writel(pcie, data, APPL_PINMUX); 1551} 1552 1553static int tegra_pcie_deinit_controller(struct tegra_pcie_dw *pcie) 1554{ 1555 tegra_pcie_downstream_dev_to_D0(pcie); 1556 dw_pcie_host_deinit(&pcie->pci.pp); 1557 tegra_pcie_dw_pme_turnoff(pcie); 1558 1559 return __deinit_controller(pcie); 1560} 1561 1562static int tegra_pcie_config_rp(struct tegra_pcie_dw *pcie) 1563{ 1564 struct pcie_port *pp = &pcie->pci.pp; 1565 struct device *dev = pcie->dev; 1566 char *name; 1567 int ret; 1568 1569 if (IS_ENABLED(CONFIG_PCI_MSI)) { 1570 pp->msi_irq = of_irq_get_byname(dev->of_node, "msi"); 1571 if (!pp->msi_irq) { 1572 dev_err(dev, "Failed to get MSI interrupt\n"); 1573 return -ENODEV; 1574 } 1575 } 1576 1577 pm_runtime_enable(dev); 1578 1579 ret = pm_runtime_get_sync(dev); 1580 if (ret < 0) { 1581 dev_err(dev, "Failed to get runtime sync for PCIe dev: %d\n", 1582 ret); 1583 goto fail_pm_get_sync; 1584 } 1585 1586 ret = pinctrl_pm_select_default_state(dev); 1587 if (ret < 0) { 1588 dev_err(dev, "Failed to configure sideband pins: %d\n", ret); 1589 goto fail_pm_get_sync; 1590 } 1591 1592 tegra_pcie_init_controller(pcie); 1593 1594 pcie->link_state = tegra_pcie_dw_link_up(&pcie->pci); 1595 if (!pcie->link_state) { 1596 ret = -ENOMEDIUM; 1597 goto fail_host_init; 1598 } 1599 1600 name = devm_kasprintf(dev, GFP_KERNEL, "%pOFP", dev->of_node); 1601 if (!name) { 1602 ret = -ENOMEM; 1603 goto fail_host_init; 1604 } 1605 1606 pcie->debugfs = debugfs_create_dir(name, NULL); 1607 init_debugfs(pcie); 1608 1609 return ret; 1610 1611fail_host_init: 1612 tegra_pcie_deinit_controller(pcie); 1613fail_pm_get_sync: 1614 pm_runtime_put_sync(dev); 1615 pm_runtime_disable(dev); 1616 return ret; 1617} 1618 1619static void pex_ep_event_pex_rst_assert(struct tegra_pcie_dw *pcie) 1620{ 1621 u32 val; 1622 int ret; 1623 1624 if (pcie->ep_state == EP_STATE_DISABLED) 1625 return; 1626 1627 /* Disable LTSSM */ 1628 val = appl_readl(pcie, APPL_CTRL); 1629 val &= ~APPL_CTRL_LTSSM_EN; 1630 appl_writel(pcie, val, APPL_CTRL); 1631 1632 ret = readl_poll_timeout(pcie->appl_base + APPL_DEBUG, val, 1633 ((val & APPL_DEBUG_LTSSM_STATE_MASK) >> 1634 APPL_DEBUG_LTSSM_STATE_SHIFT) == 1635 LTSSM_STATE_PRE_DETECT, 1636 1, LTSSM_TIMEOUT); 1637 if (ret) 1638 dev_err(pcie->dev, "Failed to go Detect state: %d\n", ret); 1639 1640 reset_control_assert(pcie->core_rst); 1641 1642 tegra_pcie_disable_phy(pcie); 1643 1644 reset_control_assert(pcie->core_apb_rst); 1645 1646 clk_disable_unprepare(pcie->core_clk); 1647 1648 pm_runtime_put_sync(pcie->dev); 1649 1650 ret = tegra_pcie_bpmp_set_pll_state(pcie, false); 1651 if (ret) 1652 dev_err(pcie->dev, "Failed to turn off UPHY: %d\n", ret); 1653 1654 pcie->ep_state = EP_STATE_DISABLED; 1655 dev_dbg(pcie->dev, "Uninitialization of endpoint is completed\n"); 1656} 1657 1658static void pex_ep_event_pex_rst_deassert(struct tegra_pcie_dw *pcie) 1659{ 1660 struct dw_pcie *pci = &pcie->pci; 1661 struct dw_pcie_ep *ep = &pci->ep; 1662 struct device *dev = pcie->dev; 1663 u32 val; 1664 int ret; 1665 1666 if (pcie->ep_state == EP_STATE_ENABLED) 1667 return; 1668 1669 ret = pm_runtime_resume_and_get(dev); 1670 if (ret < 0) { 1671 dev_err(dev, "Failed to get runtime sync for PCIe dev: %d\n", 1672 ret); 1673 return; 1674 } 1675 1676 ret = tegra_pcie_bpmp_set_pll_state(pcie, true); 1677 if (ret) { 1678 dev_err(dev, "Failed to init UPHY for PCIe EP: %d\n", ret); 1679 goto fail_pll_init; 1680 } 1681 1682 ret = clk_prepare_enable(pcie->core_clk); 1683 if (ret) { 1684 dev_err(dev, "Failed to enable core clock: %d\n", ret); 1685 goto fail_core_clk_enable; 1686 } 1687 1688 ret = reset_control_deassert(pcie->core_apb_rst); 1689 if (ret) { 1690 dev_err(dev, "Failed to deassert core APB reset: %d\n", ret); 1691 goto fail_core_apb_rst; 1692 } 1693 1694 ret = tegra_pcie_enable_phy(pcie); 1695 if (ret) { 1696 dev_err(dev, "Failed to enable PHY: %d\n", ret); 1697 goto fail_phy; 1698 } 1699 1700 /* Clear any stale interrupt statuses */ 1701 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L0); 1702 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_0_0); 1703 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_1); 1704 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_2); 1705 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_3); 1706 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_6); 1707 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_7); 1708 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_8_0); 1709 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_9); 1710 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_10); 1711 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_11); 1712 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_13); 1713 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_14); 1714 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_15); 1715 appl_writel(pcie, 0xFFFFFFFF, APPL_INTR_STATUS_L1_17); 1716 1717 /* configure this core for EP mode operation */ 1718 val = appl_readl(pcie, APPL_DM_TYPE); 1719 val &= ~APPL_DM_TYPE_MASK; 1720 val |= APPL_DM_TYPE_EP; 1721 appl_writel(pcie, val, APPL_DM_TYPE); 1722 1723 appl_writel(pcie, 0x0, APPL_CFG_SLCG_OVERRIDE); 1724 1725 val = appl_readl(pcie, APPL_CTRL); 1726 val |= APPL_CTRL_SYS_PRE_DET_STATE; 1727 val |= APPL_CTRL_HW_HOT_RST_EN; 1728 appl_writel(pcie, val, APPL_CTRL); 1729 1730 val = appl_readl(pcie, APPL_CFG_MISC); 1731 val |= APPL_CFG_MISC_SLV_EP_MODE; 1732 val |= (APPL_CFG_MISC_ARCACHE_VAL << APPL_CFG_MISC_ARCACHE_SHIFT); 1733 appl_writel(pcie, val, APPL_CFG_MISC); 1734 1735 val = appl_readl(pcie, APPL_PINMUX); 1736 val |= APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE_EN; 1737 val |= APPL_PINMUX_CLK_OUTPUT_IN_OVERRIDE; 1738 appl_writel(pcie, val, APPL_PINMUX); 1739 1740 appl_writel(pcie, pcie->dbi_res->start & APPL_CFG_BASE_ADDR_MASK, 1741 APPL_CFG_BASE_ADDR); 1742 1743 appl_writel(pcie, pcie->atu_dma_res->start & 1744 APPL_CFG_IATU_DMA_BASE_ADDR_MASK, 1745 APPL_CFG_IATU_DMA_BASE_ADDR); 1746 1747 val = appl_readl(pcie, APPL_INTR_EN_L0_0); 1748 val |= APPL_INTR_EN_L0_0_SYS_INTR_EN; 1749 val |= APPL_INTR_EN_L0_0_LINK_STATE_INT_EN; 1750 val |= APPL_INTR_EN_L0_0_PCI_CMD_EN_INT_EN; 1751 appl_writel(pcie, val, APPL_INTR_EN_L0_0); 1752 1753 val = appl_readl(pcie, APPL_INTR_EN_L1_0_0); 1754 val |= APPL_INTR_EN_L1_0_0_HOT_RESET_DONE_INT_EN; 1755 val |= APPL_INTR_EN_L1_0_0_RDLH_LINK_UP_INT_EN; 1756 appl_writel(pcie, val, APPL_INTR_EN_L1_0_0); 1757 1758 reset_control_deassert(pcie->core_rst); 1759 1760 if (pcie->update_fc_fixup) { 1761 val = dw_pcie_readl_dbi(pci, CFG_TIMER_CTRL_MAX_FUNC_NUM_OFF); 1762 val |= 0x1 << CFG_TIMER_CTRL_ACK_NAK_SHIFT; 1763 dw_pcie_writel_dbi(pci, CFG_TIMER_CTRL_MAX_FUNC_NUM_OFF, val); 1764 } 1765 1766 config_gen3_gen4_eq_presets(pcie); 1767 1768 init_host_aspm(pcie); 1769 1770 /* Disable ASPM-L1SS advertisement if there is no CLKREQ routing */ 1771 if (!pcie->supports_clkreq) { 1772 disable_aspm_l11(pcie); 1773 disable_aspm_l12(pcie); 1774 } 1775 1776 val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF); 1777 val &= ~GEN3_RELATED_OFF_GEN3_ZRXDC_NONCOMPL; 1778 dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val); 1779 1780 pcie->pcie_cap_base = dw_pcie_find_capability(&pcie->pci, 1781 PCI_CAP_ID_EXP); 1782 clk_set_rate(pcie->core_clk, GEN4_CORE_CLK_FREQ); 1783 1784 val = (ep->msi_mem_phys & MSIX_ADDR_MATCH_LOW_OFF_MASK); 1785 val |= MSIX_ADDR_MATCH_LOW_OFF_EN; 1786 dw_pcie_writel_dbi(pci, MSIX_ADDR_MATCH_LOW_OFF, val); 1787 val = (upper_32_bits(ep->msi_mem_phys) & MSIX_ADDR_MATCH_HIGH_OFF_MASK); 1788 dw_pcie_writel_dbi(pci, MSIX_ADDR_MATCH_HIGH_OFF, val); 1789 1790 ret = dw_pcie_ep_init_complete(ep); 1791 if (ret) { 1792 dev_err(dev, "Failed to complete initialization: %d\n", ret); 1793 goto fail_init_complete; 1794 } 1795 1796 dw_pcie_ep_init_notify(ep); 1797 1798 /* Enable LTSSM */ 1799 val = appl_readl(pcie, APPL_CTRL); 1800 val |= APPL_CTRL_LTSSM_EN; 1801 appl_writel(pcie, val, APPL_CTRL); 1802 1803 pcie->ep_state = EP_STATE_ENABLED; 1804 dev_dbg(dev, "Initialization of endpoint is completed\n"); 1805 1806 return; 1807 1808fail_init_complete: 1809 reset_control_assert(pcie->core_rst); 1810 tegra_pcie_disable_phy(pcie); 1811fail_phy: 1812 reset_control_assert(pcie->core_apb_rst); 1813fail_core_apb_rst: 1814 clk_disable_unprepare(pcie->core_clk); 1815fail_core_clk_enable: 1816 tegra_pcie_bpmp_set_pll_state(pcie, false); 1817fail_pll_init: 1818 pm_runtime_put_sync(dev); 1819} 1820 1821static irqreturn_t tegra_pcie_ep_pex_rst_irq(int irq, void *arg) 1822{ 1823 struct tegra_pcie_dw *pcie = arg; 1824 1825 if (gpiod_get_value(pcie->pex_rst_gpiod)) 1826 pex_ep_event_pex_rst_assert(pcie); 1827 else 1828 pex_ep_event_pex_rst_deassert(pcie); 1829 1830 return IRQ_HANDLED; 1831} 1832 1833static int tegra_pcie_ep_raise_legacy_irq(struct tegra_pcie_dw *pcie, u16 irq) 1834{ 1835 /* Tegra194 supports only INTA */ 1836 if (irq > 1) 1837 return -EINVAL; 1838 1839 appl_writel(pcie, 1, APPL_LEGACY_INTX); 1840 usleep_range(1000, 2000); 1841 appl_writel(pcie, 0, APPL_LEGACY_INTX); 1842 return 0; 1843} 1844 1845static int tegra_pcie_ep_raise_msi_irq(struct tegra_pcie_dw *pcie, u16 irq) 1846{ 1847 if (unlikely(irq > 31)) 1848 return -EINVAL; 1849 1850 appl_writel(pcie, BIT(irq), APPL_MSI_CTRL_1); 1851 1852 return 0; 1853} 1854 1855static int tegra_pcie_ep_raise_msix_irq(struct tegra_pcie_dw *pcie, u16 irq) 1856{ 1857 struct dw_pcie_ep *ep = &pcie->pci.ep; 1858 1859 writel(irq, ep->msi_mem); 1860 1861 return 0; 1862} 1863 1864static int tegra_pcie_ep_raise_irq(struct dw_pcie_ep *ep, u8 func_no, 1865 enum pci_epc_irq_type type, 1866 u16 interrupt_num) 1867{ 1868 struct dw_pcie *pci = to_dw_pcie_from_ep(ep); 1869 struct tegra_pcie_dw *pcie = to_tegra_pcie(pci); 1870 1871 switch (type) { 1872 case PCI_EPC_IRQ_LEGACY: 1873 return tegra_pcie_ep_raise_legacy_irq(pcie, interrupt_num); 1874 1875 case PCI_EPC_IRQ_MSI: 1876 return tegra_pcie_ep_raise_msi_irq(pcie, interrupt_num); 1877 1878 case PCI_EPC_IRQ_MSIX: 1879 return tegra_pcie_ep_raise_msix_irq(pcie, interrupt_num); 1880 1881 default: 1882 dev_err(pci->dev, "Unknown IRQ type\n"); 1883 return -EPERM; 1884 } 1885 1886 return 0; 1887} 1888 1889static const struct pci_epc_features tegra_pcie_epc_features = { 1890 .linkup_notifier = true, 1891 .core_init_notifier = true, 1892 .msi_capable = false, 1893 .msix_capable = false, 1894 .reserved_bar = 1 << BAR_2 | 1 << BAR_3 | 1 << BAR_4 | 1 << BAR_5, 1895 .bar_fixed_64bit = 1 << BAR_0, 1896 .bar_fixed_size[0] = SZ_1M, 1897}; 1898 1899static const struct pci_epc_features* 1900tegra_pcie_ep_get_features(struct dw_pcie_ep *ep) 1901{ 1902 return &tegra_pcie_epc_features; 1903} 1904 1905static struct dw_pcie_ep_ops pcie_ep_ops = { 1906 .raise_irq = tegra_pcie_ep_raise_irq, 1907 .get_features = tegra_pcie_ep_get_features, 1908}; 1909 1910static int tegra_pcie_config_ep(struct tegra_pcie_dw *pcie, 1911 struct platform_device *pdev) 1912{ 1913 struct dw_pcie *pci = &pcie->pci; 1914 struct device *dev = pcie->dev; 1915 struct dw_pcie_ep *ep; 1916 struct resource *res; 1917 char *name; 1918 int ret; 1919 1920 ep = &pci->ep; 1921 ep->ops = &pcie_ep_ops; 1922 1923 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space"); 1924 if (!res) 1925 return -EINVAL; 1926 1927 ep->phys_base = res->start; 1928 ep->addr_size = resource_size(res); 1929 ep->page_size = SZ_64K; 1930 1931 ret = gpiod_set_debounce(pcie->pex_rst_gpiod, PERST_DEBOUNCE_TIME); 1932 if (ret < 0) { 1933 dev_err(dev, "Failed to set PERST GPIO debounce time: %d\n", 1934 ret); 1935 return ret; 1936 } 1937 1938 ret = gpiod_to_irq(pcie->pex_rst_gpiod); 1939 if (ret < 0) { 1940 dev_err(dev, "Failed to get IRQ for PERST GPIO: %d\n", ret); 1941 return ret; 1942 } 1943 pcie->pex_rst_irq = (unsigned int)ret; 1944 1945 name = devm_kasprintf(dev, GFP_KERNEL, "tegra_pcie_%u_pex_rst_irq", 1946 pcie->cid); 1947 if (!name) { 1948 dev_err(dev, "Failed to create PERST IRQ string\n"); 1949 return -ENOMEM; 1950 } 1951 1952 irq_set_status_flags(pcie->pex_rst_irq, IRQ_NOAUTOEN); 1953 1954 pcie->ep_state = EP_STATE_DISABLED; 1955 1956 ret = devm_request_threaded_irq(dev, pcie->pex_rst_irq, NULL, 1957 tegra_pcie_ep_pex_rst_irq, 1958 IRQF_TRIGGER_RISING | 1959 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 1960 name, (void *)pcie); 1961 if (ret < 0) { 1962 dev_err(dev, "Failed to request IRQ for PERST: %d\n", ret); 1963 return ret; 1964 } 1965 1966 name = devm_kasprintf(dev, GFP_KERNEL, "tegra_pcie_%u_ep_work", 1967 pcie->cid); 1968 if (!name) { 1969 dev_err(dev, "Failed to create PCIe EP work thread string\n"); 1970 return -ENOMEM; 1971 } 1972 1973 pm_runtime_enable(dev); 1974 1975 ret = dw_pcie_ep_init(ep); 1976 if (ret) { 1977 dev_err(dev, "Failed to initialize DWC Endpoint subsystem: %d\n", 1978 ret); 1979 pm_runtime_disable(dev); 1980 return ret; 1981 } 1982 1983 return 0; 1984} 1985 1986static int tegra_pcie_dw_probe(struct platform_device *pdev) 1987{ 1988 const struct tegra_pcie_dw_of_data *data; 1989 struct device *dev = &pdev->dev; 1990 struct resource *atu_dma_res; 1991 struct tegra_pcie_dw *pcie; 1992 struct resource *dbi_res; 1993 struct pcie_port *pp; 1994 struct dw_pcie *pci; 1995 struct phy **phys; 1996 char *name; 1997 int ret; 1998 u32 i; 1999 2000 data = of_device_get_match_data(dev); 2001 2002 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL); 2003 if (!pcie) 2004 return -ENOMEM; 2005 2006 pci = &pcie->pci; 2007 pci->dev = &pdev->dev; 2008 pci->ops = &tegra_dw_pcie_ops; 2009 pci->n_fts[0] = N_FTS_VAL; 2010 pci->n_fts[1] = FTS_VAL; 2011 2012 pp = &pci->pp; 2013 pcie->dev = &pdev->dev; 2014 pcie->mode = (enum dw_pcie_device_mode)data->mode; 2015 2016 ret = tegra_pcie_dw_parse_dt(pcie); 2017 if (ret < 0) { 2018 const char *level = KERN_ERR; 2019 2020 if (ret == -EPROBE_DEFER) 2021 level = KERN_DEBUG; 2022 2023 dev_printk(level, dev, 2024 dev_fmt("Failed to parse device tree: %d\n"), 2025 ret); 2026 return ret; 2027 } 2028 2029 ret = tegra_pcie_get_slot_regulators(pcie); 2030 if (ret < 0) { 2031 const char *level = KERN_ERR; 2032 2033 if (ret == -EPROBE_DEFER) 2034 level = KERN_DEBUG; 2035 2036 dev_printk(level, dev, 2037 dev_fmt("Failed to get slot regulators: %d\n"), 2038 ret); 2039 return ret; 2040 } 2041 2042 if (pcie->pex_refclk_sel_gpiod) 2043 gpiod_set_value(pcie->pex_refclk_sel_gpiod, 1); 2044 2045 pcie->pex_ctl_supply = devm_regulator_get(dev, "vddio-pex-ctl"); 2046 if (IS_ERR(pcie->pex_ctl_supply)) { 2047 ret = PTR_ERR(pcie->pex_ctl_supply); 2048 if (ret != -EPROBE_DEFER) 2049 dev_err(dev, "Failed to get regulator: %ld\n", 2050 PTR_ERR(pcie->pex_ctl_supply)); 2051 return ret; 2052 } 2053 2054 pcie->core_clk = devm_clk_get(dev, "core"); 2055 if (IS_ERR(pcie->core_clk)) { 2056 dev_err(dev, "Failed to get core clock: %ld\n", 2057 PTR_ERR(pcie->core_clk)); 2058 return PTR_ERR(pcie->core_clk); 2059 } 2060 2061 pcie->appl_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 2062 "appl"); 2063 if (!pcie->appl_res) { 2064 dev_err(dev, "Failed to find \"appl\" region\n"); 2065 return -ENODEV; 2066 } 2067 2068 pcie->appl_base = devm_ioremap_resource(dev, pcie->appl_res); 2069 if (IS_ERR(pcie->appl_base)) 2070 return PTR_ERR(pcie->appl_base); 2071 2072 pcie->core_apb_rst = devm_reset_control_get(dev, "apb"); 2073 if (IS_ERR(pcie->core_apb_rst)) { 2074 dev_err(dev, "Failed to get APB reset: %ld\n", 2075 PTR_ERR(pcie->core_apb_rst)); 2076 return PTR_ERR(pcie->core_apb_rst); 2077 } 2078 2079 phys = devm_kcalloc(dev, pcie->phy_count, sizeof(*phys), GFP_KERNEL); 2080 if (!phys) 2081 return -ENOMEM; 2082 2083 for (i = 0; i < pcie->phy_count; i++) { 2084 name = kasprintf(GFP_KERNEL, "p2u-%u", i); 2085 if (!name) { 2086 dev_err(dev, "Failed to create P2U string\n"); 2087 return -ENOMEM; 2088 } 2089 phys[i] = devm_phy_get(dev, name); 2090 kfree(name); 2091 if (IS_ERR(phys[i])) { 2092 ret = PTR_ERR(phys[i]); 2093 if (ret != -EPROBE_DEFER) 2094 dev_err(dev, "Failed to get PHY: %d\n", ret); 2095 return ret; 2096 } 2097 } 2098 2099 pcie->phys = phys; 2100 2101 dbi_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi"); 2102 if (!dbi_res) { 2103 dev_err(dev, "Failed to find \"dbi\" region\n"); 2104 return -ENODEV; 2105 } 2106 pcie->dbi_res = dbi_res; 2107 2108 pci->dbi_base = devm_ioremap_resource(dev, dbi_res); 2109 if (IS_ERR(pci->dbi_base)) 2110 return PTR_ERR(pci->dbi_base); 2111 2112 /* Tegra HW locates DBI2 at a fixed offset from DBI */ 2113 pci->dbi_base2 = pci->dbi_base + 0x1000; 2114 2115 atu_dma_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 2116 "atu_dma"); 2117 if (!atu_dma_res) { 2118 dev_err(dev, "Failed to find \"atu_dma\" region\n"); 2119 return -ENODEV; 2120 } 2121 pcie->atu_dma_res = atu_dma_res; 2122 2123 pci->atu_base = devm_ioremap_resource(dev, atu_dma_res); 2124 if (IS_ERR(pci->atu_base)) 2125 return PTR_ERR(pci->atu_base); 2126 2127 pcie->core_rst = devm_reset_control_get(dev, "core"); 2128 if (IS_ERR(pcie->core_rst)) { 2129 dev_err(dev, "Failed to get core reset: %ld\n", 2130 PTR_ERR(pcie->core_rst)); 2131 return PTR_ERR(pcie->core_rst); 2132 } 2133 2134 pp->irq = platform_get_irq_byname(pdev, "intr"); 2135 if (pp->irq < 0) 2136 return pp->irq; 2137 2138 pcie->bpmp = tegra_bpmp_get(dev); 2139 if (IS_ERR(pcie->bpmp)) 2140 return PTR_ERR(pcie->bpmp); 2141 2142 platform_set_drvdata(pdev, pcie); 2143 2144 switch (pcie->mode) { 2145 case DW_PCIE_RC_TYPE: 2146 ret = devm_request_irq(dev, pp->irq, tegra_pcie_rp_irq_handler, 2147 IRQF_SHARED, "tegra-pcie-intr", pcie); 2148 if (ret) { 2149 dev_err(dev, "Failed to request IRQ %d: %d\n", pp->irq, 2150 ret); 2151 goto fail; 2152 } 2153 2154 ret = tegra_pcie_config_rp(pcie); 2155 if (ret && ret != -ENOMEDIUM) 2156 goto fail; 2157 else 2158 return 0; 2159 break; 2160 2161 case DW_PCIE_EP_TYPE: 2162 ret = devm_request_threaded_irq(dev, pp->irq, 2163 tegra_pcie_ep_hard_irq, 2164 tegra_pcie_ep_irq_thread, 2165 IRQF_SHARED | IRQF_ONESHOT, 2166 "tegra-pcie-ep-intr", pcie); 2167 if (ret) { 2168 dev_err(dev, "Failed to request IRQ %d: %d\n", pp->irq, 2169 ret); 2170 goto fail; 2171 } 2172 2173 ret = tegra_pcie_config_ep(pcie, pdev); 2174 if (ret < 0) 2175 goto fail; 2176 break; 2177 2178 default: 2179 dev_err(dev, "Invalid PCIe device type %d\n", pcie->mode); 2180 } 2181 2182fail: 2183 tegra_bpmp_put(pcie->bpmp); 2184 return ret; 2185} 2186 2187static int tegra_pcie_dw_remove(struct platform_device *pdev) 2188{ 2189 struct tegra_pcie_dw *pcie = platform_get_drvdata(pdev); 2190 2191 if (!pcie->link_state) 2192 return 0; 2193 2194 debugfs_remove_recursive(pcie->debugfs); 2195 tegra_pcie_deinit_controller(pcie); 2196 pm_runtime_put_sync(pcie->dev); 2197 pm_runtime_disable(pcie->dev); 2198 tegra_bpmp_put(pcie->bpmp); 2199 if (pcie->pex_refclk_sel_gpiod) 2200 gpiod_set_value(pcie->pex_refclk_sel_gpiod, 0); 2201 2202 return 0; 2203} 2204 2205static int tegra_pcie_dw_suspend_late(struct device *dev) 2206{ 2207 struct tegra_pcie_dw *pcie = dev_get_drvdata(dev); 2208 u32 val; 2209 2210 if (!pcie->link_state) 2211 return 0; 2212 2213 /* Enable HW_HOT_RST mode */ 2214 val = appl_readl(pcie, APPL_CTRL); 2215 val &= ~(APPL_CTRL_HW_HOT_RST_MODE_MASK << 2216 APPL_CTRL_HW_HOT_RST_MODE_SHIFT); 2217 val |= APPL_CTRL_HW_HOT_RST_EN; 2218 appl_writel(pcie, val, APPL_CTRL); 2219 2220 return 0; 2221} 2222 2223static int tegra_pcie_dw_suspend_noirq(struct device *dev) 2224{ 2225 struct tegra_pcie_dw *pcie = dev_get_drvdata(dev); 2226 2227 if (!pcie->link_state) 2228 return 0; 2229 2230 /* Save MSI interrupt vector */ 2231 pcie->msi_ctrl_int = dw_pcie_readl_dbi(&pcie->pci, 2232 PORT_LOGIC_MSI_CTRL_INT_0_EN); 2233 tegra_pcie_downstream_dev_to_D0(pcie); 2234 tegra_pcie_dw_pme_turnoff(pcie); 2235 2236 return __deinit_controller(pcie); 2237} 2238 2239static int tegra_pcie_dw_resume_noirq(struct device *dev) 2240{ 2241 struct tegra_pcie_dw *pcie = dev_get_drvdata(dev); 2242 int ret; 2243 2244 if (!pcie->link_state) 2245 return 0; 2246 2247 ret = tegra_pcie_config_controller(pcie, true); 2248 if (ret < 0) 2249 return ret; 2250 2251 ret = tegra_pcie_dw_host_init(&pcie->pci.pp); 2252 if (ret < 0) { 2253 dev_err(dev, "Failed to init host: %d\n", ret); 2254 goto fail_host_init; 2255 } 2256 2257 /* Restore MSI interrupt vector */ 2258 dw_pcie_writel_dbi(&pcie->pci, PORT_LOGIC_MSI_CTRL_INT_0_EN, 2259 pcie->msi_ctrl_int); 2260 2261 return 0; 2262 2263fail_host_init: 2264 return __deinit_controller(pcie); 2265} 2266 2267static int tegra_pcie_dw_resume_early(struct device *dev) 2268{ 2269 struct tegra_pcie_dw *pcie = dev_get_drvdata(dev); 2270 u32 val; 2271 2272 if (!pcie->link_state) 2273 return 0; 2274 2275 /* Disable HW_HOT_RST mode */ 2276 val = appl_readl(pcie, APPL_CTRL); 2277 val &= ~(APPL_CTRL_HW_HOT_RST_MODE_MASK << 2278 APPL_CTRL_HW_HOT_RST_MODE_SHIFT); 2279 val |= APPL_CTRL_HW_HOT_RST_MODE_IMDT_RST << 2280 APPL_CTRL_HW_HOT_RST_MODE_SHIFT; 2281 val &= ~APPL_CTRL_HW_HOT_RST_EN; 2282 appl_writel(pcie, val, APPL_CTRL); 2283 2284 return 0; 2285} 2286 2287static void tegra_pcie_dw_shutdown(struct platform_device *pdev) 2288{ 2289 struct tegra_pcie_dw *pcie = platform_get_drvdata(pdev); 2290 2291 if (!pcie->link_state) 2292 return; 2293 2294 debugfs_remove_recursive(pcie->debugfs); 2295 tegra_pcie_downstream_dev_to_D0(pcie); 2296 2297 disable_irq(pcie->pci.pp.irq); 2298 if (IS_ENABLED(CONFIG_PCI_MSI)) 2299 disable_irq(pcie->pci.pp.msi_irq); 2300 2301 tegra_pcie_dw_pme_turnoff(pcie); 2302 __deinit_controller(pcie); 2303} 2304 2305static const struct tegra_pcie_dw_of_data tegra_pcie_dw_rc_of_data = { 2306 .mode = DW_PCIE_RC_TYPE, 2307}; 2308 2309static const struct tegra_pcie_dw_of_data tegra_pcie_dw_ep_of_data = { 2310 .mode = DW_PCIE_EP_TYPE, 2311}; 2312 2313static const struct of_device_id tegra_pcie_dw_of_match[] = { 2314 { 2315 .compatible = "nvidia,tegra194-pcie", 2316 .data = &tegra_pcie_dw_rc_of_data, 2317 }, 2318 { 2319 .compatible = "nvidia,tegra194-pcie-ep", 2320 .data = &tegra_pcie_dw_ep_of_data, 2321 }, 2322 {}, 2323}; 2324 2325static const struct dev_pm_ops tegra_pcie_dw_pm_ops = { 2326 .suspend_late = tegra_pcie_dw_suspend_late, 2327 .suspend_noirq = tegra_pcie_dw_suspend_noirq, 2328 .resume_noirq = tegra_pcie_dw_resume_noirq, 2329 .resume_early = tegra_pcie_dw_resume_early, 2330}; 2331 2332static struct platform_driver tegra_pcie_dw_driver = { 2333 .probe = tegra_pcie_dw_probe, 2334 .remove = tegra_pcie_dw_remove, 2335 .shutdown = tegra_pcie_dw_shutdown, 2336 .driver = { 2337 .name = "tegra194-pcie", 2338 .pm = &tegra_pcie_dw_pm_ops, 2339 .of_match_table = tegra_pcie_dw_of_match, 2340 }, 2341}; 2342module_platform_driver(tegra_pcie_dw_driver); 2343 2344MODULE_DEVICE_TABLE(of, tegra_pcie_dw_of_match); 2345 2346MODULE_AUTHOR("Vidya Sagar <vidyas@nvidia.com>"); 2347MODULE_DESCRIPTION("NVIDIA PCIe host controller driver"); 2348MODULE_LICENSE("GPL v2"); 2349