1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Qualcomm PCIe root complex driver 4 * 5 * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. 6 * Copyright 2015 Linaro Limited. 7 * 8 * Author: Stanimir Varbanov <svarbanov@mm-sol.com> 9 */ 10 11 #include <linux/clk.h> 12 #include <linux/crc8.h> 13 #include <linux/debugfs.h> 14 #include <linux/delay.h> 15 #include <linux/gpio/consumer.h> 16 #include <linux/interconnect.h> 17 #include <linux/interrupt.h> 18 #include <linux/io.h> 19 #include <linux/iopoll.h> 20 #include <linux/kernel.h> 21 #include <linux/limits.h> 22 #include <linux/init.h> 23 #include <linux/mutex.h> 24 #include <linux/of.h> 25 #include <linux/of_pci.h> 26 #include <linux/pci.h> 27 #include <linux/pci-ecam.h> 28 #include <linux/pci-pwrctrl.h> 29 #include <linux/pm_opp.h> 30 #include <linux/pm_runtime.h> 31 #include <linux/platform_device.h> 32 #include <linux/phy/pcie.h> 33 #include <linux/phy/phy.h> 34 #include <linux/regulator/consumer.h> 35 #include <linux/reset.h> 36 #include <linux/slab.h> 37 #include <linux/types.h> 38 #include <linux/units.h> 39 40 #include "../../pci.h" 41 #include "../pci-host-common.h" 42 #include "pcie-designware.h" 43 #include "pcie-qcom-common.h" 44 45 /* PARF registers */ 46 #define PARF_SYS_CTRL 0x00 47 #define PARF_PM_CTRL 0x20 48 #define PARF_PCS_DEEMPH 0x34 49 #define PARF_PCS_SWING 0x38 50 #define PARF_PHY_CTRL 0x40 51 #define PARF_PHY_REFCLK 0x4c 52 #define PARF_CONFIG_BITS 0x50 53 #define PARF_DBI_BASE_ADDR 0x168 54 #define PARF_SLV_ADDR_SPACE_SIZE 0x16c 55 #define PARF_MHI_CLOCK_RESET_CTRL 0x174 56 #define PARF_AXI_MSTR_WR_ADDR_HALT 0x178 57 #define PARF_AXI_MSTR_WR_ADDR_HALT_V2 0x1a8 58 #define PARF_Q2A_FLUSH 0x1ac 59 #define PARF_LTSSM 0x1b0 60 #define PARF_INT_ALL_STATUS 0x224 61 #define PARF_INT_ALL_CLEAR 0x228 62 #define PARF_INT_ALL_MASK 0x22c 63 #define PARF_STATUS 0x230 64 #define PARF_SID_OFFSET 0x234 65 #define PARF_BDF_TRANSLATE_CFG 0x24c 66 #define PARF_DBI_BASE_ADDR_V2 0x350 67 #define PARF_DBI_BASE_ADDR_V2_HI 0x354 68 #define PARF_SLV_ADDR_SPACE_SIZE_V2 0x358 69 #define PARF_SLV_ADDR_SPACE_SIZE_V2_HI 0x35c 70 #define PARF_NO_SNOOP_OVERRIDE 0x3d4 71 #define PARF_ATU_BASE_ADDR 0x634 72 #define PARF_ATU_BASE_ADDR_HI 0x638 73 #define PARF_DEVICE_TYPE 0x1000 74 #define PARF_BDF_TO_SID_TABLE_N 0x2000 75 #define PARF_BDF_TO_SID_CFG 0x2c00 76 77 /* 78 * Outbound blocker range registers. The blocked range has a write pair 79 * (WR_BASE/WR_LIMIT) and a read pair (RD_BASE/RD_LIMIT); each address is 80 * split into a low (32-bit) and a HI (upper 32-bit) register. 81 */ 82 #define PARF_BLOCK_SLV_AXI_WR_BASE 0x360 83 #define PARF_BLOCK_SLV_AXI_WR_BASE_HI 0x364 84 #define PARF_BLOCK_SLV_AXI_WR_LIMIT 0x368 85 #define PARF_BLOCK_SLV_AXI_WR_LIMIT_HI 0x36c 86 #define PARF_BLOCK_SLV_AXI_RD_BASE 0x370 87 #define PARF_BLOCK_SLV_AXI_RD_BASE_HI 0x374 88 #define PARF_BLOCK_SLV_AXI_RD_LIMIT 0x378 89 #define PARF_BLOCK_SLV_AXI_RD_LIMIT_HI 0x37c 90 91 #define PARF_ECAM_BASE 0x380 92 #define PARF_ECAM_BASE_HI 0x384 93 94 /* ELBI registers */ 95 #define ELBI_SYS_CTRL 0x04 96 #define ELBI_SYS_STTS 0x08 97 98 /* DBI registers */ 99 #define AXI_MSTR_RESP_COMP_CTRL0 0x818 100 #define AXI_MSTR_RESP_COMP_CTRL1 0x81c 101 102 /* MHI registers */ 103 #define PARF_DEBUG_CNT_PM_LINKST_IN_L2 0xc04 104 #define PARF_DEBUG_CNT_PM_LINKST_IN_L1 0xc0c 105 #define PARF_DEBUG_CNT_PM_LINKST_IN_L0S 0xc10 106 #define PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L1 0xc84 107 #define PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2 0xc88 108 109 /* PARF_SYS_CTRL register fields */ 110 #define MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN BIT(29) 111 #define OUTBOUND_BLOCKER_EN BIT(26) 112 #define MST_WAKEUP_EN BIT(13) 113 #define SLV_WAKEUP_EN BIT(12) 114 #define MSTR_ACLK_CGC_DIS BIT(10) 115 #define SLV_ACLK_CGC_DIS BIT(9) 116 #define CORE_CLK_CGC_DIS BIT(6) 117 #define AUX_PWR_DET BIT(4) 118 #define L23_CLK_RMV_DIS BIT(2) 119 #define L1_CLK_RMV_DIS BIT(1) 120 121 /* PARF_PM_CTRL register fields */ 122 #define REQ_NOT_ENTR_L1 BIT(5) 123 124 /* PARF_PCS_DEEMPH register fields */ 125 #define PCS_DEEMPH_TX_DEEMPH_GEN1(x) FIELD_PREP(GENMASK(21, 16), x) 126 #define PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(x) FIELD_PREP(GENMASK(13, 8), x) 127 #define PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(x) FIELD_PREP(GENMASK(5, 0), x) 128 129 /* PARF_PCS_SWING register fields */ 130 #define PCS_SWING_TX_SWING_FULL(x) FIELD_PREP(GENMASK(14, 8), x) 131 #define PCS_SWING_TX_SWING_LOW(x) FIELD_PREP(GENMASK(6, 0), x) 132 133 /* PARF_PHY_CTRL register fields */ 134 #define PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK GENMASK(20, 16) 135 #define PHY_CTRL_PHY_TX0_TERM_OFFSET(x) FIELD_PREP(PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK, x) 136 #define PHY_TEST_PWR_DOWN BIT(0) 137 138 /* PARF_PHY_REFCLK register fields */ 139 #define PHY_REFCLK_SSP_EN BIT(16) 140 #define PHY_REFCLK_USE_PAD BIT(12) 141 142 /* PARF_CONFIG_BITS register fields */ 143 #define PHY_RX0_EQ(x) FIELD_PREP(GENMASK(26, 24), x) 144 145 /* PARF_SLV_ADDR_SPACE_SIZE register value */ 146 #define SLV_ADDR_SPACE_SZ 0x80000000 147 148 /* PARF_MHI_CLOCK_RESET_CTRL register fields */ 149 #define AHB_CLK_EN BIT(0) 150 #define MSTR_AXI_CLK_EN BIT(1) 151 #define BYPASS BIT(4) 152 153 /* PARF_AXI_MSTR_WR_ADDR_HALT register fields */ 154 #define EN BIT(31) 155 156 /* PARF_LTSSM register fields */ 157 #define LTSSM_EN BIT(8) 158 #define PARF_LTSSM_STATE_MASK GENMASK(5, 0) 159 #define SW_CLEAR_FLUSH_MODE BIT(10) 160 #define FLUSH_MODE BIT(11) 161 162 /* PARF_INT_ALL_{STATUS/CLEAR/MASK} register fields */ 163 #define INT_ALL_LINK_DOWN 1 164 #define PARF_INT_ALL_LINK_DOWN BIT(INT_ALL_LINK_DOWN) 165 #define PARF_INT_MSI_DEV_0_7 GENMASK(30, 23) 166 167 /* PARF_NO_SNOOP_OVERRIDE register fields */ 168 #define WR_NO_SNOOP_OVERRIDE_EN BIT(1) 169 #define RD_NO_SNOOP_OVERRIDE_EN BIT(3) 170 171 /* PARF_DEVICE_TYPE register fields */ 172 #define DEVICE_TYPE_RC 0x4 173 174 /* PARF_BDF_TO_SID_CFG fields */ 175 #define BDF_TO_SID_BYPASS BIT(0) 176 177 /* PARF_STATUS fields */ 178 #define FLUSH_COMPLETED BIT(8) 179 180 /* ELBI_SYS_CTRL register fields */ 181 #define ELBI_SYS_CTRL_LT_ENABLE BIT(0) 182 #define ELBI_SYS_CTRL_PME_TURNOFF_MSG BIT(4) 183 184 /* ELBI_SYS_STTS register fields */ 185 #define ELBI_SYS_STTS_LTSSM_STATE_MASK GENMASK(17, 12) 186 187 /* AXI_MSTR_RESP_COMP_CTRL0 register fields */ 188 #define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K 0x4 189 #define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_4K 0x5 190 191 /* AXI_MSTR_RESP_COMP_CTRL1 register fields */ 192 #define CFG_BRIDGE_SB_INIT BIT(0) 193 194 /* PCI_EXP_SLTCAP register fields */ 195 #define PCIE_CAP_SLOT_POWER_LIMIT_VAL FIELD_PREP(PCI_EXP_SLTCAP_SPLV, 250) 196 #define PCIE_CAP_SLOT_POWER_LIMIT_SCALE FIELD_PREP(PCI_EXP_SLTCAP_SPLS, 1) 197 #define PCIE_CAP_SLOT_VAL (PCI_EXP_SLTCAP_ABP | \ 198 PCI_EXP_SLTCAP_PCP | \ 199 PCI_EXP_SLTCAP_MRLSP | \ 200 PCI_EXP_SLTCAP_AIP | \ 201 PCI_EXP_SLTCAP_PIP | \ 202 PCI_EXP_SLTCAP_HPS | \ 203 PCI_EXP_SLTCAP_EIP | \ 204 PCIE_CAP_SLOT_POWER_LIMIT_VAL | \ 205 PCIE_CAP_SLOT_POWER_LIMIT_SCALE) 206 207 #define PERST_DELAY_US 1000 208 #define FLUSH_TIMEOUT_US 100 209 210 #define QCOM_PCIE_CRC8_POLYNOMIAL (BIT(2) | BIT(1) | BIT(0)) 211 212 #define QCOM_PCIE_LINK_SPEED_TO_BW(speed) \ 213 Mbps_to_icc(PCIE_SPEED2MBS_ENC(pcie_get_link_speed(speed))) 214 215 struct qcom_pcie_resources_1_0_0 { 216 struct clk_bulk_data *clks; 217 int num_clks; 218 struct reset_control *core; 219 struct regulator *vdda; 220 }; 221 222 #define QCOM_PCIE_2_1_0_MAX_RESETS 6 223 #define QCOM_PCIE_2_1_0_MAX_SUPPLY 3 224 struct qcom_pcie_resources_2_1_0 { 225 struct clk_bulk_data *clks; 226 int num_clks; 227 struct reset_control_bulk_data resets[QCOM_PCIE_2_1_0_MAX_RESETS]; 228 int num_resets; 229 struct regulator_bulk_data supplies[QCOM_PCIE_2_1_0_MAX_SUPPLY]; 230 }; 231 232 #define QCOM_PCIE_2_3_2_MAX_SUPPLY 2 233 struct qcom_pcie_resources_2_3_2 { 234 struct clk_bulk_data *clks; 235 int num_clks; 236 struct regulator_bulk_data supplies[QCOM_PCIE_2_3_2_MAX_SUPPLY]; 237 }; 238 239 #define QCOM_PCIE_2_3_3_MAX_RESETS 7 240 struct qcom_pcie_resources_2_3_3 { 241 struct clk_bulk_data *clks; 242 int num_clks; 243 struct reset_control_bulk_data rst[QCOM_PCIE_2_3_3_MAX_RESETS]; 244 }; 245 246 #define QCOM_PCIE_2_4_0_MAX_RESETS 12 247 struct qcom_pcie_resources_2_4_0 { 248 struct clk_bulk_data *clks; 249 int num_clks; 250 struct reset_control_bulk_data resets[QCOM_PCIE_2_4_0_MAX_RESETS]; 251 int num_resets; 252 }; 253 254 #define QCOM_PCIE_2_7_0_MAX_SUPPLIES 2 255 struct qcom_pcie_resources_2_7_0 { 256 struct clk_bulk_data *clks; 257 int num_clks; 258 struct regulator_bulk_data supplies[QCOM_PCIE_2_7_0_MAX_SUPPLIES]; 259 struct reset_control *rst; 260 }; 261 262 struct qcom_pcie_resources_2_9_0 { 263 struct clk_bulk_data *clks; 264 int num_clks; 265 struct reset_control *rst; 266 }; 267 268 union qcom_pcie_resources { 269 struct qcom_pcie_resources_1_0_0 v1_0_0; 270 struct qcom_pcie_resources_2_1_0 v2_1_0; 271 struct qcom_pcie_resources_2_3_2 v2_3_2; 272 struct qcom_pcie_resources_2_3_3 v2_3_3; 273 struct qcom_pcie_resources_2_4_0 v2_4_0; 274 struct qcom_pcie_resources_2_7_0 v2_7_0; 275 struct qcom_pcie_resources_2_9_0 v2_9_0; 276 }; 277 278 struct qcom_pcie; 279 280 struct qcom_pcie_ops { 281 int (*get_resources)(struct qcom_pcie *pcie); 282 int (*init)(struct qcom_pcie *pcie); 283 int (*post_init)(struct qcom_pcie *pcie); 284 void (*host_post_init)(struct qcom_pcie *pcie); 285 void (*deinit)(struct qcom_pcie *pcie); 286 void (*ltssm_enable)(struct qcom_pcie *pcie); 287 int (*config_sid)(struct qcom_pcie *pcie); 288 enum dw_pcie_ltssm (*get_ltssm)(struct qcom_pcie *pcie); 289 }; 290 291 /** 292 * struct qcom_pcie_cfg - Per SoC config struct 293 * @ops: qcom PCIe ops structure 294 * @override_no_snoop: Override NO_SNOOP attribute in TLP to enable cache 295 * snooping 296 * @firmware_managed: Set if the Root Complex is firmware managed 297 */ 298 struct qcom_pcie_cfg { 299 const struct qcom_pcie_ops *ops; 300 bool override_no_snoop; 301 bool firmware_managed; 302 bool no_l0s; 303 }; 304 305 struct qcom_pcie_perst { 306 struct list_head list; 307 struct gpio_desc *desc; 308 }; 309 310 struct qcom_pcie_port { 311 struct list_head list; 312 struct phy *phy; 313 u32 l1ss_t_power_on; 314 struct list_head perst; 315 }; 316 317 struct qcom_pcie { 318 struct dw_pcie *pci; 319 void __iomem *parf; /* DT parf */ 320 void __iomem *mhi; 321 union qcom_pcie_resources res; 322 struct icc_path *icc_mem; 323 struct icc_path *icc_cpu; 324 const struct qcom_pcie_cfg *cfg; 325 struct dentry *debugfs; 326 struct list_head ports; 327 struct gpio_desc *reset; 328 int global_irq; 329 bool use_pm_opp; 330 struct mutex reset_lock; 331 }; 332 333 #define to_qcom_pcie(x) dev_get_drvdata((x)->dev) 334 static int qcom_pcie_reset_root_port(struct pci_host_bridge *bridge, 335 struct pci_dev *pdev); 336 337 static void __qcom_pcie_perst_assert(struct qcom_pcie *pcie, bool assert) 338 { 339 struct qcom_pcie_perst *perst; 340 struct qcom_pcie_port *port; 341 int val = assert ? 1 : 0; 342 343 list_for_each_entry(port, &pcie->ports, list) { 344 list_for_each_entry(perst, &port->perst, list) 345 gpiod_set_value_cansleep(perst->desc, val); 346 } 347 348 usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500); 349 } 350 351 static void qcom_pcie_perst_assert(struct qcom_pcie *pcie) 352 { 353 __qcom_pcie_perst_assert(pcie, true); 354 } 355 356 static void qcom_pcie_perst_deassert(struct qcom_pcie *pcie) 357 { 358 /* Ensure that PERST# has been asserted for at least 100 ms */ 359 msleep(PCIE_T_PVPERL_MS); 360 __qcom_pcie_perst_assert(pcie, false); 361 } 362 363 static int qcom_pcie_start_link(struct dw_pcie *pci) 364 { 365 struct qcom_pcie *pcie = to_qcom_pcie(pci); 366 367 qcom_pcie_common_set_equalization(pci); 368 369 if (pcie_get_link_speed(pci->max_link_speed) == PCIE_SPEED_16_0GT) 370 qcom_pcie_common_set_16gt_lane_margining(pci); 371 372 /* Enable Link Training state machine */ 373 if (pcie->cfg->ops->ltssm_enable) 374 pcie->cfg->ops->ltssm_enable(pcie); 375 376 return 0; 377 } 378 379 static void qcom_pcie_clear_aspm_l0s(struct dw_pcie *pci) 380 { 381 struct qcom_pcie *pcie = to_qcom_pcie(pci); 382 u16 offset; 383 u32 val; 384 385 if (!pcie->cfg->no_l0s) 386 return; 387 388 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP); 389 390 dw_pcie_dbi_ro_wr_en(pci); 391 392 val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP); 393 val &= ~PCI_EXP_LNKCAP_ASPM_L0S; 394 writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP); 395 396 dw_pcie_dbi_ro_wr_dis(pci); 397 } 398 399 static void qcom_pcie_set_slot_cap(struct dw_pcie *pci) 400 { 401 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP); 402 u32 val; 403 404 dw_pcie_dbi_ro_wr_en(pci); 405 406 /* 407 * Qcom PCIe Root Ports do not support generating command completion 408 * notifications for the Hot-Plug commands. So set the NCCS field to 409 * avoid waiting for the completions. 410 */ 411 val = readl(pci->dbi_base + offset + PCI_EXP_SLTCAP); 412 val |= PCI_EXP_SLTCAP_NCCS; 413 414 /* 415 * Qcom PCIe Root Ports do not support Attention Button, so clear 416 * Attention Button Present in Slot Capabilities. 417 */ 418 val &= ~PCI_EXP_SLTCAP_ABP; 419 writel(val, pci->dbi_base + offset + PCI_EXP_SLTCAP); 420 421 dw_pcie_dbi_ro_wr_dis(pci); 422 } 423 424 static void qcom_pcie_configure_dbi_base(struct qcom_pcie *pcie) 425 { 426 struct dw_pcie *pci = pcie->pci; 427 428 if (pci->dbi_phys_addr) { 429 /* 430 * PARF_DBI_BASE_ADDR register is in CPU domain and require to 431 * be programmed with CPU physical address. 432 */ 433 writel(lower_32_bits(pci->dbi_phys_addr), pcie->parf + 434 PARF_DBI_BASE_ADDR); 435 writel(SLV_ADDR_SPACE_SZ, pcie->parf + 436 PARF_SLV_ADDR_SPACE_SIZE); 437 } 438 } 439 440 static void qcom_pcie_configure_dbi_atu_base(struct qcom_pcie *pcie) 441 { 442 struct dw_pcie *pci = pcie->pci; 443 444 if (pci->dbi_phys_addr) { 445 /* 446 * PARF_DBI_BASE_ADDR_V2 and PARF_ATU_BASE_ADDR registers are 447 * in CPU domain and require to be programmed with CPU 448 * physical addresses. 449 */ 450 writel(lower_32_bits(pci->dbi_phys_addr), pcie->parf + 451 PARF_DBI_BASE_ADDR_V2); 452 writel(upper_32_bits(pci->dbi_phys_addr), pcie->parf + 453 PARF_DBI_BASE_ADDR_V2_HI); 454 455 if (pci->atu_phys_addr) { 456 writel(lower_32_bits(pci->atu_phys_addr), pcie->parf + 457 PARF_ATU_BASE_ADDR); 458 writel(upper_32_bits(pci->atu_phys_addr), pcie->parf + 459 PARF_ATU_BASE_ADDR_HI); 460 } 461 462 writel(0x0, pcie->parf + PARF_SLV_ADDR_SPACE_SIZE_V2); 463 writel(SLV_ADDR_SPACE_SZ, pcie->parf + 464 PARF_SLV_ADDR_SPACE_SIZE_V2_HI); 465 } 466 } 467 468 static void qcom_pcie_init_outbound_blocker(struct qcom_pcie *pcie) 469 { 470 struct dw_pcie *pci = pcie->pci; 471 472 /* ECAM base must match the DBI base address */ 473 writel(lower_32_bits(pci->dbi_phys_addr), pcie->parf + PARF_ECAM_BASE); 474 writel(upper_32_bits(pci->dbi_phys_addr), pcie->parf + PARF_ECAM_BASE_HI); 475 476 writel(0, pcie->parf + PARF_BLOCK_SLV_AXI_WR_BASE); 477 writel(0, pcie->parf + PARF_BLOCK_SLV_AXI_WR_BASE_HI); 478 writel(U32_MAX, pcie->parf + PARF_BLOCK_SLV_AXI_WR_LIMIT); 479 writel(U32_MAX, pcie->parf + PARF_BLOCK_SLV_AXI_WR_LIMIT_HI); 480 481 writel(0, pcie->parf + PARF_BLOCK_SLV_AXI_RD_BASE); 482 writel(0, pcie->parf + PARF_BLOCK_SLV_AXI_RD_BASE_HI); 483 writel(U32_MAX, pcie->parf + PARF_BLOCK_SLV_AXI_RD_LIMIT); 484 writel(U32_MAX, pcie->parf + PARF_BLOCK_SLV_AXI_RD_LIMIT_HI); 485 } 486 487 static void qcom_pcie_2_1_0_ltssm_enable(struct qcom_pcie *pcie) 488 { 489 struct dw_pcie *pci = pcie->pci; 490 u32 val; 491 492 if (!pci->elbi_base) { 493 dev_err(pci->dev, "ELBI is not present\n"); 494 return; 495 } 496 /* enable link training */ 497 val = readl(pci->elbi_base + ELBI_SYS_CTRL); 498 val |= ELBI_SYS_CTRL_LT_ENABLE; 499 writel(val, pci->elbi_base + ELBI_SYS_CTRL); 500 } 501 502 static enum dw_pcie_ltssm qcom_pcie_2_1_0_get_ltssm(struct qcom_pcie *pcie) 503 { 504 struct dw_pcie *pci = pcie->pci; 505 u32 val; 506 507 val = readl(pci->elbi_base + ELBI_SYS_STTS); 508 return (enum dw_pcie_ltssm)FIELD_GET(ELBI_SYS_STTS_LTSSM_STATE_MASK, val); 509 } 510 511 static int qcom_pcie_get_resources_2_1_0(struct qcom_pcie *pcie) 512 { 513 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0; 514 struct dw_pcie *pci = pcie->pci; 515 struct device *dev = pci->dev; 516 bool is_apq = of_device_is_compatible(dev->of_node, "qcom,pcie-apq8064"); 517 int ret; 518 519 res->supplies[0].supply = "vdda"; 520 res->supplies[1].supply = "vdda_phy"; 521 res->supplies[2].supply = "vdda_refclk"; 522 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies), 523 res->supplies); 524 if (ret) 525 return ret; 526 527 res->num_clks = devm_clk_bulk_get_all(dev, &res->clks); 528 if (res->num_clks < 0) { 529 dev_err(dev, "Failed to get clocks\n"); 530 return res->num_clks; 531 } 532 533 res->resets[0].id = "pci"; 534 res->resets[1].id = "axi"; 535 res->resets[2].id = "ahb"; 536 res->resets[3].id = "por"; 537 res->resets[4].id = "phy"; 538 res->resets[5].id = "ext"; 539 540 /* ext is optional on APQ8016 */ 541 res->num_resets = is_apq ? 5 : 6; 542 ret = devm_reset_control_bulk_get_exclusive(dev, res->num_resets, res->resets); 543 if (ret < 0) 544 return ret; 545 546 return 0; 547 } 548 549 static void qcom_pcie_deinit_2_1_0(struct qcom_pcie *pcie) 550 { 551 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0; 552 553 clk_bulk_disable_unprepare(res->num_clks, res->clks); 554 reset_control_bulk_assert(res->num_resets, res->resets); 555 556 writel(1, pcie->parf + PARF_PHY_CTRL); 557 558 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies); 559 } 560 561 static int qcom_pcie_init_2_1_0(struct qcom_pcie *pcie) 562 { 563 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0; 564 struct dw_pcie *pci = pcie->pci; 565 struct device *dev = pci->dev; 566 int ret; 567 568 /* reset the PCIe interface as uboot can leave it undefined state */ 569 ret = reset_control_bulk_assert(res->num_resets, res->resets); 570 if (ret < 0) { 571 dev_err(dev, "cannot assert resets\n"); 572 return ret; 573 } 574 575 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies); 576 if (ret < 0) { 577 dev_err(dev, "cannot enable regulators\n"); 578 return ret; 579 } 580 581 ret = reset_control_bulk_deassert(res->num_resets, res->resets); 582 if (ret < 0) { 583 dev_err(dev, "cannot deassert resets\n"); 584 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies); 585 return ret; 586 } 587 588 return 0; 589 } 590 591 static int qcom_pcie_post_init_2_1_0(struct qcom_pcie *pcie) 592 { 593 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0; 594 struct dw_pcie *pci = pcie->pci; 595 struct device *dev = pci->dev; 596 struct device_node *node = dev->of_node; 597 u32 val; 598 int ret; 599 600 /* Force PHY out of lowest power state */ 601 val = readl(pcie->parf + PARF_PHY_CTRL); 602 val &= ~PHY_TEST_PWR_DOWN; 603 writel(val, pcie->parf + PARF_PHY_CTRL); 604 605 ret = clk_bulk_prepare_enable(res->num_clks, res->clks); 606 if (ret) 607 return ret; 608 609 if (of_device_is_compatible(node, "qcom,pcie-ipq8064") || 610 of_device_is_compatible(node, "qcom,pcie-ipq8064-v2")) { 611 writel(PCS_DEEMPH_TX_DEEMPH_GEN1(24) | 612 PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(24) | 613 PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(34), 614 pcie->parf + PARF_PCS_DEEMPH); 615 writel(PCS_SWING_TX_SWING_FULL(120) | 616 PCS_SWING_TX_SWING_LOW(120), 617 pcie->parf + PARF_PCS_SWING); 618 writel(PHY_RX0_EQ(4), pcie->parf + PARF_CONFIG_BITS); 619 } 620 621 if (of_device_is_compatible(node, "qcom,pcie-ipq8064")) { 622 /* set TX termination offset */ 623 val = readl(pcie->parf + PARF_PHY_CTRL); 624 val &= ~PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK; 625 val |= PHY_CTRL_PHY_TX0_TERM_OFFSET(7); 626 writel(val, pcie->parf + PARF_PHY_CTRL); 627 } 628 629 /* enable external reference clock */ 630 val = readl(pcie->parf + PARF_PHY_REFCLK); 631 /* USE_PAD is required only for ipq806x */ 632 if (!of_device_is_compatible(node, "qcom,pcie-apq8064")) 633 val &= ~PHY_REFCLK_USE_PAD; 634 val |= PHY_REFCLK_SSP_EN; 635 writel(val, pcie->parf + PARF_PHY_REFCLK); 636 637 /* wait for clock acquisition */ 638 usleep_range(1000, 1500); 639 640 /* Set the Max TLP size to 2K, instead of using default of 4K */ 641 writel(CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K, 642 pci->dbi_base + AXI_MSTR_RESP_COMP_CTRL0); 643 writel(CFG_BRIDGE_SB_INIT, 644 pci->dbi_base + AXI_MSTR_RESP_COMP_CTRL1); 645 646 qcom_pcie_set_slot_cap(pcie->pci); 647 648 return 0; 649 } 650 651 static int qcom_pcie_get_resources_1_0_0(struct qcom_pcie *pcie) 652 { 653 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0; 654 struct dw_pcie *pci = pcie->pci; 655 struct device *dev = pci->dev; 656 657 res->vdda = devm_regulator_get(dev, "vdda"); 658 if (IS_ERR(res->vdda)) 659 return PTR_ERR(res->vdda); 660 661 res->num_clks = devm_clk_bulk_get_all(dev, &res->clks); 662 if (res->num_clks < 0) { 663 dev_err(dev, "Failed to get clocks\n"); 664 return res->num_clks; 665 } 666 667 res->core = devm_reset_control_get_exclusive(dev, "core"); 668 return PTR_ERR_OR_ZERO(res->core); 669 } 670 671 static void qcom_pcie_deinit_1_0_0(struct qcom_pcie *pcie) 672 { 673 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0; 674 675 reset_control_assert(res->core); 676 clk_bulk_disable_unprepare(res->num_clks, res->clks); 677 regulator_disable(res->vdda); 678 } 679 680 static int qcom_pcie_init_1_0_0(struct qcom_pcie *pcie) 681 { 682 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0; 683 struct dw_pcie *pci = pcie->pci; 684 struct device *dev = pci->dev; 685 int ret; 686 687 ret = reset_control_deassert(res->core); 688 if (ret) { 689 dev_err(dev, "cannot deassert core reset\n"); 690 return ret; 691 } 692 693 ret = clk_bulk_prepare_enable(res->num_clks, res->clks); 694 if (ret) { 695 dev_err(dev, "cannot prepare/enable clocks\n"); 696 goto err_assert_reset; 697 } 698 699 ret = regulator_enable(res->vdda); 700 if (ret) { 701 dev_err(dev, "cannot enable vdda regulator\n"); 702 goto err_disable_clks; 703 } 704 705 return 0; 706 707 err_disable_clks: 708 clk_bulk_disable_unprepare(res->num_clks, res->clks); 709 err_assert_reset: 710 reset_control_assert(res->core); 711 712 return ret; 713 } 714 715 static int qcom_pcie_post_init_1_0_0(struct qcom_pcie *pcie) 716 { 717 qcom_pcie_configure_dbi_base(pcie); 718 719 if (IS_ENABLED(CONFIG_PCI_MSI)) { 720 u32 val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT); 721 722 val |= EN; 723 writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT); 724 } 725 726 qcom_pcie_set_slot_cap(pcie->pci); 727 728 return 0; 729 } 730 731 static void qcom_pcie_2_3_2_ltssm_enable(struct qcom_pcie *pcie) 732 { 733 u32 val; 734 735 /* enable link training */ 736 val = readl(pcie->parf + PARF_LTSSM); 737 val |= LTSSM_EN; 738 writel(val, pcie->parf + PARF_LTSSM); 739 } 740 741 static int qcom_pcie_get_resources_2_3_2(struct qcom_pcie *pcie) 742 { 743 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2; 744 struct dw_pcie *pci = pcie->pci; 745 struct device *dev = pci->dev; 746 int ret; 747 748 res->supplies[0].supply = "vdda"; 749 res->supplies[1].supply = "vddpe-3v3"; 750 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies), 751 res->supplies); 752 if (ret) 753 return ret; 754 755 res->num_clks = devm_clk_bulk_get_all(dev, &res->clks); 756 if (res->num_clks < 0) { 757 dev_err(dev, "Failed to get clocks\n"); 758 return res->num_clks; 759 } 760 761 return 0; 762 } 763 764 static void qcom_pcie_deinit_2_3_2(struct qcom_pcie *pcie) 765 { 766 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2; 767 u32 val; 768 769 /* Force PHY to lowest power state*/ 770 val = readl(pcie->parf + PARF_PHY_CTRL); 771 val |= PHY_TEST_PWR_DOWN; 772 writel(val, pcie->parf + PARF_PHY_CTRL); 773 774 clk_bulk_disable_unprepare(res->num_clks, res->clks); 775 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies); 776 } 777 778 static int qcom_pcie_init_2_3_2(struct qcom_pcie *pcie) 779 { 780 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2; 781 struct dw_pcie *pci = pcie->pci; 782 struct device *dev = pci->dev; 783 int ret; 784 785 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies); 786 if (ret < 0) { 787 dev_err(dev, "cannot enable regulators\n"); 788 return ret; 789 } 790 791 ret = clk_bulk_prepare_enable(res->num_clks, res->clks); 792 if (ret) { 793 dev_err(dev, "cannot prepare/enable clocks\n"); 794 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies); 795 return ret; 796 } 797 798 return 0; 799 } 800 801 static int qcom_pcie_post_init_2_3_2(struct qcom_pcie *pcie) 802 { 803 u32 val; 804 805 /* Force PHY out of lowest power state */ 806 val = readl(pcie->parf + PARF_PHY_CTRL); 807 val &= ~PHY_TEST_PWR_DOWN; 808 writel(val, pcie->parf + PARF_PHY_CTRL); 809 810 qcom_pcie_configure_dbi_base(pcie); 811 812 /* MAC PHY_POWERDOWN MUX DISABLE */ 813 val = readl(pcie->parf + PARF_SYS_CTRL); 814 val &= ~MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN; 815 writel(val, pcie->parf + PARF_SYS_CTRL); 816 817 val = readl(pcie->parf + PARF_MHI_CLOCK_RESET_CTRL); 818 val |= BYPASS; 819 writel(val, pcie->parf + PARF_MHI_CLOCK_RESET_CTRL); 820 821 val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2); 822 val |= EN; 823 writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2); 824 825 qcom_pcie_set_slot_cap(pcie->pci); 826 827 return 0; 828 } 829 830 static int qcom_pcie_get_resources_2_4_0(struct qcom_pcie *pcie) 831 { 832 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0; 833 struct dw_pcie *pci = pcie->pci; 834 struct device *dev = pci->dev; 835 bool is_ipq = of_device_is_compatible(dev->of_node, "qcom,pcie-ipq4019"); 836 int ret; 837 838 res->num_clks = devm_clk_bulk_get_all(dev, &res->clks); 839 if (res->num_clks < 0) { 840 dev_err(dev, "Failed to get clocks\n"); 841 return res->num_clks; 842 } 843 844 res->resets[0].id = "axi_m"; 845 res->resets[1].id = "axi_s"; 846 res->resets[2].id = "axi_m_sticky"; 847 res->resets[3].id = "pipe_sticky"; 848 res->resets[4].id = "pwr"; 849 res->resets[5].id = "ahb"; 850 res->resets[6].id = "pipe"; 851 res->resets[7].id = "axi_m_vmid"; 852 res->resets[8].id = "axi_s_xpu"; 853 res->resets[9].id = "parf"; 854 res->resets[10].id = "phy"; 855 res->resets[11].id = "phy_ahb"; 856 857 res->num_resets = is_ipq ? 12 : 6; 858 859 ret = devm_reset_control_bulk_get_exclusive(dev, res->num_resets, res->resets); 860 if (ret < 0) 861 return ret; 862 863 return 0; 864 } 865 866 static void qcom_pcie_deinit_2_4_0(struct qcom_pcie *pcie) 867 { 868 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0; 869 u32 val; 870 871 /* Force PHY to lowest power state*/ 872 val = readl(pcie->parf + PARF_PHY_CTRL); 873 val |= PHY_TEST_PWR_DOWN; 874 writel(val, pcie->parf + PARF_PHY_CTRL); 875 876 reset_control_bulk_assert(res->num_resets, res->resets); 877 clk_bulk_disable_unprepare(res->num_clks, res->clks); 878 } 879 880 static int qcom_pcie_init_2_4_0(struct qcom_pcie *pcie) 881 { 882 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0; 883 struct dw_pcie *pci = pcie->pci; 884 struct device *dev = pci->dev; 885 int ret; 886 887 ret = reset_control_bulk_assert(res->num_resets, res->resets); 888 if (ret < 0) { 889 dev_err(dev, "cannot assert resets\n"); 890 return ret; 891 } 892 893 usleep_range(10000, 12000); 894 895 ret = reset_control_bulk_deassert(res->num_resets, res->resets); 896 if (ret < 0) { 897 dev_err(dev, "cannot deassert resets\n"); 898 return ret; 899 } 900 901 usleep_range(10000, 12000); 902 903 ret = clk_bulk_prepare_enable(res->num_clks, res->clks); 904 if (ret) { 905 reset_control_bulk_assert(res->num_resets, res->resets); 906 return ret; 907 } 908 909 return 0; 910 } 911 912 static int qcom_pcie_get_resources_2_3_3(struct qcom_pcie *pcie) 913 { 914 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3; 915 struct dw_pcie *pci = pcie->pci; 916 struct device *dev = pci->dev; 917 int ret; 918 919 res->num_clks = devm_clk_bulk_get_all(dev, &res->clks); 920 if (res->num_clks < 0) { 921 dev_err(dev, "Failed to get clocks\n"); 922 return res->num_clks; 923 } 924 925 res->rst[0].id = "axi_m"; 926 res->rst[1].id = "axi_s"; 927 res->rst[2].id = "pipe"; 928 res->rst[3].id = "axi_m_sticky"; 929 res->rst[4].id = "sticky"; 930 res->rst[5].id = "ahb"; 931 res->rst[6].id = "sleep"; 932 933 ret = devm_reset_control_bulk_get_exclusive(dev, ARRAY_SIZE(res->rst), res->rst); 934 if (ret < 0) 935 return ret; 936 937 return 0; 938 } 939 940 static void qcom_pcie_deinit_2_3_3(struct qcom_pcie *pcie) 941 { 942 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3; 943 u32 val; 944 945 /* Force PHY to lowest power state */ 946 val = readl(pcie->parf + PARF_PHY_CTRL); 947 val |= PHY_TEST_PWR_DOWN; 948 writel(val, pcie->parf + PARF_PHY_CTRL); 949 950 clk_bulk_disable_unprepare(res->num_clks, res->clks); 951 } 952 953 static int qcom_pcie_init_2_3_3(struct qcom_pcie *pcie) 954 { 955 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3; 956 struct dw_pcie *pci = pcie->pci; 957 struct device *dev = pci->dev; 958 int ret; 959 960 ret = reset_control_bulk_assert(ARRAY_SIZE(res->rst), res->rst); 961 if (ret < 0) { 962 dev_err(dev, "cannot assert resets\n"); 963 return ret; 964 } 965 966 usleep_range(2000, 2500); 967 968 ret = reset_control_bulk_deassert(ARRAY_SIZE(res->rst), res->rst); 969 if (ret < 0) { 970 dev_err(dev, "cannot deassert resets\n"); 971 return ret; 972 } 973 974 /* 975 * Don't have a way to see if the reset has completed. 976 * Wait for some time. 977 */ 978 usleep_range(2000, 2500); 979 980 ret = clk_bulk_prepare_enable(res->num_clks, res->clks); 981 if (ret) { 982 dev_err(dev, "cannot prepare/enable clocks\n"); 983 goto err_assert_resets; 984 } 985 986 return 0; 987 988 err_assert_resets: 989 /* 990 * Not checking for failure, will anyway return 991 * the original failure in 'ret'. 992 */ 993 reset_control_bulk_assert(ARRAY_SIZE(res->rst), res->rst); 994 995 return ret; 996 } 997 998 static int qcom_pcie_post_init_2_3_3(struct qcom_pcie *pcie) 999 { 1000 struct dw_pcie *pci = pcie->pci; 1001 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP); 1002 u32 val; 1003 1004 /* Force PHY out of lowest power state */ 1005 val = readl(pcie->parf + PARF_PHY_CTRL); 1006 val &= ~PHY_TEST_PWR_DOWN; 1007 writel(val, pcie->parf + PARF_PHY_CTRL); 1008 1009 qcom_pcie_configure_dbi_atu_base(pcie); 1010 1011 writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS 1012 | SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS | 1013 AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS, 1014 pcie->parf + PARF_SYS_CTRL); 1015 writel(0, pcie->parf + PARF_Q2A_FLUSH); 1016 1017 writel(PCI_COMMAND_MASTER, pci->dbi_base + PCI_COMMAND); 1018 1019 dw_pcie_dbi_ro_wr_en(pci); 1020 1021 writel(PCIE_CAP_SLOT_VAL, pci->dbi_base + offset + PCI_EXP_SLTCAP); 1022 1023 val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP); 1024 val &= ~PCI_EXP_LNKCAP_ASPMS; 1025 writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP); 1026 1027 writel(PCI_EXP_DEVCTL2_COMP_TMOUT_DIS, pci->dbi_base + offset + 1028 PCI_EXP_DEVCTL2); 1029 1030 dw_pcie_dbi_ro_wr_dis(pci); 1031 1032 qcom_pcie_init_outbound_blocker(pcie); 1033 1034 return 0; 1035 } 1036 1037 static int qcom_pcie_get_resources_2_7_0(struct qcom_pcie *pcie) 1038 { 1039 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0; 1040 struct dw_pcie *pci = pcie->pci; 1041 struct device *dev = pci->dev; 1042 int ret; 1043 1044 res->rst = devm_reset_control_array_get_exclusive(dev); 1045 if (IS_ERR(res->rst)) 1046 return PTR_ERR(res->rst); 1047 1048 res->supplies[0].supply = "vdda"; 1049 res->supplies[1].supply = "vddpe-3v3"; 1050 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies), 1051 res->supplies); 1052 if (ret) 1053 return ret; 1054 1055 res->num_clks = devm_clk_bulk_get_all(dev, &res->clks); 1056 if (res->num_clks < 0) { 1057 dev_err(dev, "Failed to get clocks\n"); 1058 return res->num_clks; 1059 } 1060 1061 return 0; 1062 } 1063 1064 static int qcom_pcie_init_2_7_0(struct qcom_pcie *pcie) 1065 { 1066 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0; 1067 struct dw_pcie *pci = pcie->pci; 1068 struct device *dev = pci->dev; 1069 u32 val; 1070 int ret; 1071 1072 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies); 1073 if (ret < 0) { 1074 dev_err(dev, "cannot enable regulators\n"); 1075 return ret; 1076 } 1077 1078 ret = clk_bulk_prepare_enable(res->num_clks, res->clks); 1079 if (ret < 0) 1080 goto err_disable_regulators; 1081 1082 ret = reset_control_assert(res->rst); 1083 if (ret) { 1084 dev_err(dev, "reset assert failed (%d)\n", ret); 1085 goto err_disable_clocks; 1086 } 1087 1088 usleep_range(1000, 1500); 1089 1090 ret = reset_control_deassert(res->rst); 1091 if (ret) { 1092 dev_err(dev, "reset deassert failed (%d)\n", ret); 1093 goto err_disable_clocks; 1094 } 1095 1096 /* Wait for reset to complete, required on SM8450 */ 1097 usleep_range(1000, 1500); 1098 1099 /* configure PCIe to RC mode */ 1100 writel(DEVICE_TYPE_RC, pcie->parf + PARF_DEVICE_TYPE); 1101 1102 /* Force PHY out of lowest power state */ 1103 val = readl(pcie->parf + PARF_PHY_CTRL); 1104 val &= ~PHY_TEST_PWR_DOWN; 1105 writel(val, pcie->parf + PARF_PHY_CTRL); 1106 1107 qcom_pcie_configure_dbi_atu_base(pcie); 1108 1109 /* MAC PHY_POWERDOWN MUX DISABLE */ 1110 val = readl(pcie->parf + PARF_SYS_CTRL); 1111 val &= ~MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN; 1112 writel(val, pcie->parf + PARF_SYS_CTRL); 1113 1114 val = readl(pcie->parf + PARF_MHI_CLOCK_RESET_CTRL); 1115 val |= BYPASS; 1116 writel(val, pcie->parf + PARF_MHI_CLOCK_RESET_CTRL); 1117 1118 /* Enable L1 and L1SS */ 1119 val = readl(pcie->parf + PARF_PM_CTRL); 1120 val &= ~REQ_NOT_ENTR_L1; 1121 writel(val, pcie->parf + PARF_PM_CTRL); 1122 1123 pci->l1ss_support = true; 1124 1125 val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2); 1126 val |= EN; 1127 writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2); 1128 1129 return 0; 1130 err_disable_clocks: 1131 clk_bulk_disable_unprepare(res->num_clks, res->clks); 1132 err_disable_regulators: 1133 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies); 1134 1135 return ret; 1136 } 1137 1138 static int qcom_pcie_post_init_2_7_0(struct qcom_pcie *pcie) 1139 { 1140 const struct qcom_pcie_cfg *pcie_cfg = pcie->cfg; 1141 1142 if (pcie_cfg->override_no_snoop) 1143 writel(WR_NO_SNOOP_OVERRIDE_EN | RD_NO_SNOOP_OVERRIDE_EN, 1144 pcie->parf + PARF_NO_SNOOP_OVERRIDE); 1145 1146 qcom_pcie_set_slot_cap(pcie->pci); 1147 1148 qcom_pcie_init_outbound_blocker(pcie); 1149 1150 return 0; 1151 } 1152 1153 static int qcom_pcie_enable_aspm(struct pci_dev *pdev, void *userdata) 1154 { 1155 /* 1156 * Downstream devices need to be in D0 state before enabling PCI PM 1157 * substates. 1158 */ 1159 pci_set_power_state_locked(pdev, PCI_D0); 1160 pci_enable_link_state_locked(pdev, PCIE_LINK_STATE_ALL); 1161 1162 return 0; 1163 } 1164 1165 static void qcom_pcie_host_post_init_2_7_0(struct qcom_pcie *pcie) 1166 { 1167 struct dw_pcie_rp *pp = &pcie->pci->pp; 1168 1169 pci_walk_bus(pp->bridge->bus, qcom_pcie_enable_aspm, NULL); 1170 } 1171 1172 static void qcom_pcie_deinit_2_7_0(struct qcom_pcie *pcie) 1173 { 1174 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0; 1175 u32 val; 1176 1177 /* Force PHY to lowest power state */ 1178 val = readl(pcie->parf + PARF_PHY_CTRL); 1179 val |= PHY_TEST_PWR_DOWN; 1180 writel(val, pcie->parf + PARF_PHY_CTRL); 1181 1182 clk_bulk_disable_unprepare(res->num_clks, res->clks); 1183 1184 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies); 1185 } 1186 1187 static int qcom_pcie_config_sid_1_9_0(struct qcom_pcie *pcie) 1188 { 1189 void __iomem *bdf_to_sid_base = pcie->parf + PARF_BDF_TO_SID_TABLE_N; 1190 struct device *dev = pcie->pci->dev; 1191 struct device_node *iommu_np; 1192 u8 qcom_pcie_crc8_table[CRC8_TABLE_SIZE]; 1193 const __be32 *map; 1194 u32 iommu_cells, entry_cells, phandle, smmu_sid_base; 1195 int i, nr_cells, nr_map, size = 0; 1196 u32 val; 1197 1198 map = of_get_property(dev->of_node, "iommu-map", &size); 1199 if (!map || !size) 1200 return 0; 1201 1202 if (size % sizeof(*map)) { 1203 dev_err(dev, "Malformed iommu-map property\n"); 1204 return -EINVAL; 1205 } 1206 nr_cells = size / sizeof(*map); 1207 1208 /* 1209 * Each iommu-map entry is: rid-base (1 cell), phandle (1 cell), 1210 * IOMMU specifier (#iommu-cells cells), length (1 cell). Read 1211 * #iommu-cells from the IOMMU provider referenced by the first 1212 * entry to compute the per-entry stride. 1213 */ 1214 phandle = be32_to_cpu(map[1]); 1215 iommu_np = of_find_node_by_phandle(phandle); 1216 if (!iommu_np) { 1217 dev_err(dev, "Failed to find IOMMU node in iommu-map\n"); 1218 return -ENODEV; 1219 } 1220 1221 if (of_property_read_u32(iommu_np, "#iommu-cells", &iommu_cells)) 1222 iommu_cells = 1; 1223 of_node_put(iommu_np); 1224 1225 entry_cells = 3 + iommu_cells; 1226 1227 /* 1228 * Retain backward compatibility with DTs that describe iommu-map 1229 * with 4-cell entries against an IOMMU declaring #iommu-cells = 2, 1230 * matching the fallback in drivers/of/base.c::of_check_bad_map(). 1231 */ 1232 if (iommu_cells == 2 && !(nr_cells % 4)) { 1233 bool legacy = true; 1234 1235 for (i = 0; i < nr_cells; i += 4) { 1236 if (be32_to_cpu(map[i + 1]) != phandle || 1237 be32_to_cpu(map[i + 3]) != 1) { 1238 legacy = false; 1239 break; 1240 } 1241 } 1242 1243 if (legacy) { 1244 dev_warn_once(dev, "iommu-map has 1-cell entries with #iommu-cells=2, using 1-cell\n"); 1245 entry_cells = 4; 1246 } 1247 } 1248 1249 if (nr_cells % entry_cells) { 1250 dev_err(dev, "Malformed iommu-map property\n"); 1251 return -EINVAL; 1252 } 1253 nr_map = nr_cells / entry_cells; 1254 1255 /* Enable BDF to SID translation by disabling bypass mode (default) */ 1256 val = readl(pcie->parf + PARF_BDF_TO_SID_CFG); 1257 val &= ~BDF_TO_SID_BYPASS; 1258 writel(val, pcie->parf + PARF_BDF_TO_SID_CFG); 1259 1260 crc8_populate_msb(qcom_pcie_crc8_table, QCOM_PCIE_CRC8_POLYNOMIAL); 1261 1262 /* Registers need to be zero out first */ 1263 memset_io(bdf_to_sid_base, 0, CRC8_TABLE_SIZE * sizeof(u32)); 1264 1265 /* Extract the SMMU SID base from the first entry of iommu-map */ 1266 smmu_sid_base = be32_to_cpu(map[2]); 1267 1268 /* Look for an available entry to hold the mapping */ 1269 for (i = 0; i < nr_map; i++) { 1270 u32 bdf = be32_to_cpu(map[i * entry_cells]); 1271 u32 sid = be32_to_cpu(map[i * entry_cells + 2]); 1272 __be16 bdf_be = cpu_to_be16(bdf); 1273 u8 hash; 1274 1275 hash = crc8(qcom_pcie_crc8_table, (u8 *)&bdf_be, sizeof(bdf_be), 0); 1276 1277 val = readl(bdf_to_sid_base + hash * sizeof(u32)); 1278 1279 /* If the register is already populated, look for next available entry */ 1280 while (val) { 1281 u8 current_hash = hash++; 1282 u8 next_mask = 0xff; 1283 1284 /* If NEXT field is NULL then update it with next hash */ 1285 if (!(val & next_mask)) { 1286 val |= (u32)hash; 1287 writel(val, bdf_to_sid_base + current_hash * sizeof(u32)); 1288 } 1289 1290 val = readl(bdf_to_sid_base + hash * sizeof(u32)); 1291 } 1292 1293 /* BDF [31:16] | SID [15:8] | NEXT [7:0] */ 1294 val = bdf << 16 | (sid - smmu_sid_base) << 8 | 0; 1295 writel(val, bdf_to_sid_base + hash * sizeof(u32)); 1296 } 1297 1298 return 0; 1299 } 1300 1301 static int qcom_pcie_get_resources_2_9_0(struct qcom_pcie *pcie) 1302 { 1303 struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0; 1304 struct dw_pcie *pci = pcie->pci; 1305 struct device *dev = pci->dev; 1306 1307 res->num_clks = devm_clk_bulk_get_all(dev, &res->clks); 1308 if (res->num_clks < 0) { 1309 dev_err(dev, "Failed to get clocks\n"); 1310 return res->num_clks; 1311 } 1312 1313 res->rst = devm_reset_control_array_get_exclusive(dev); 1314 if (IS_ERR(res->rst)) 1315 return PTR_ERR(res->rst); 1316 1317 return 0; 1318 } 1319 1320 static void qcom_pcie_deinit_2_9_0(struct qcom_pcie *pcie) 1321 { 1322 struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0; 1323 u32 val; 1324 1325 /* Force PHY to lowest power state */ 1326 val = readl(pcie->parf + PARF_PHY_CTRL); 1327 val |= PHY_TEST_PWR_DOWN; 1328 writel(val, pcie->parf + PARF_PHY_CTRL); 1329 1330 clk_bulk_disable_unprepare(res->num_clks, res->clks); 1331 } 1332 1333 static int qcom_pcie_init_2_9_0(struct qcom_pcie *pcie) 1334 { 1335 struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0; 1336 struct device *dev = pcie->pci->dev; 1337 int ret; 1338 1339 ret = reset_control_assert(res->rst); 1340 if (ret) { 1341 dev_err(dev, "reset assert failed (%d)\n", ret); 1342 return ret; 1343 } 1344 1345 /* 1346 * Delay periods before and after reset deassert are working values 1347 * from downstream Codeaurora kernel 1348 */ 1349 usleep_range(2000, 2500); 1350 1351 ret = reset_control_deassert(res->rst); 1352 if (ret) { 1353 dev_err(dev, "reset deassert failed (%d)\n", ret); 1354 return ret; 1355 } 1356 1357 usleep_range(2000, 2500); 1358 1359 return clk_bulk_prepare_enable(res->num_clks, res->clks); 1360 } 1361 1362 static int qcom_pcie_post_init_2_9_0(struct qcom_pcie *pcie) 1363 { 1364 struct dw_pcie *pci = pcie->pci; 1365 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP); 1366 u32 val; 1367 int i; 1368 1369 /* Force PHY out of lowest power state */ 1370 val = readl(pcie->parf + PARF_PHY_CTRL); 1371 val &= ~PHY_TEST_PWR_DOWN; 1372 writel(val, pcie->parf + PARF_PHY_CTRL); 1373 1374 qcom_pcie_configure_dbi_atu_base(pcie); 1375 1376 writel(DEVICE_TYPE_RC, pcie->parf + PARF_DEVICE_TYPE); 1377 writel(BYPASS | MSTR_AXI_CLK_EN | AHB_CLK_EN, 1378 pcie->parf + PARF_MHI_CLOCK_RESET_CTRL); 1379 writel(GEN3_CTRL_RXEQ_RGRDLESS_RXTS | 1380 GEN3_CTRL_GEN3_ZRXDC_NONCOMPL, 1381 pci->dbi_base + GEN3_CTRL); 1382 1383 writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS | 1384 SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS | 1385 AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS, 1386 pcie->parf + PARF_SYS_CTRL); 1387 1388 writel(0, pcie->parf + PARF_Q2A_FLUSH); 1389 1390 dw_pcie_dbi_ro_wr_en(pci); 1391 1392 writel(PCIE_CAP_SLOT_VAL, pci->dbi_base + offset + PCI_EXP_SLTCAP); 1393 1394 val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP); 1395 val &= ~PCI_EXP_LNKCAP_ASPMS; 1396 writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP); 1397 1398 writel(PCI_EXP_DEVCTL2_COMP_TMOUT_DIS, pci->dbi_base + offset + 1399 PCI_EXP_DEVCTL2); 1400 1401 dw_pcie_dbi_ro_wr_dis(pci); 1402 1403 for (i = 0; i < 256; i++) 1404 writel(0, pcie->parf + PARF_BDF_TO_SID_TABLE_N + (4 * i)); 1405 1406 qcom_pcie_init_outbound_blocker(pcie); 1407 1408 return 0; 1409 } 1410 1411 static bool qcom_pcie_link_up(struct dw_pcie *pci) 1412 { 1413 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP); 1414 u16 val = readw(pci->dbi_base + offset + PCI_EXP_LNKSTA); 1415 1416 return val & PCI_EXP_LNKSTA_DLLLA; 1417 } 1418 1419 static enum dw_pcie_ltssm qcom_pcie_get_ltssm(struct dw_pcie *pci) 1420 { 1421 struct qcom_pcie *pcie = to_qcom_pcie(pci); 1422 u32 val; 1423 1424 if (pcie->cfg->ops->get_ltssm) 1425 return pcie->cfg->ops->get_ltssm(pcie); 1426 1427 val = readl(pcie->parf + PARF_LTSSM); 1428 1429 return (enum dw_pcie_ltssm)FIELD_GET(PARF_LTSSM_STATE_MASK, val); 1430 } 1431 1432 static void qcom_pcie_phy_power_off(struct qcom_pcie *pcie) 1433 { 1434 struct qcom_pcie_port *port; 1435 1436 list_for_each_entry(port, &pcie->ports, list) 1437 phy_power_off(port->phy); 1438 } 1439 1440 static int qcom_pcie_phy_power_on(struct qcom_pcie *pcie) 1441 { 1442 struct qcom_pcie_port *port; 1443 int ret; 1444 1445 list_for_each_entry(port, &pcie->ports, list) { 1446 ret = phy_set_mode_ext(port->phy, PHY_MODE_PCIE, PHY_MODE_PCIE_RC); 1447 if (ret) 1448 return ret; 1449 1450 ret = phy_power_on(port->phy); 1451 if (ret) { 1452 qcom_pcie_phy_power_off(pcie); 1453 return ret; 1454 } 1455 } 1456 1457 return 0; 1458 } 1459 1460 static void qcom_pcie_configure_ports(struct qcom_pcie *pcie) 1461 { 1462 struct qcom_pcie_port *port; 1463 1464 list_for_each_entry(port, &pcie->ports, list) 1465 dw_pcie_program_t_power_on(pcie->pci, port->l1ss_t_power_on); 1466 } 1467 1468 static void qcom_pcie_enable_outbound_blocker(struct qcom_pcie *pcie) 1469 { 1470 u32 sys_ctrl; 1471 1472 sys_ctrl = readl(pcie->parf + PARF_SYS_CTRL); 1473 sys_ctrl |= OUTBOUND_BLOCKER_EN; 1474 writel(sys_ctrl, pcie->parf + PARF_SYS_CTRL); 1475 1476 /* Flush the write so the blocker is enabled before this function returns */ 1477 readl(pcie->parf + PARF_SYS_CTRL); 1478 } 1479 1480 static int qcom_pcie_host_init(struct dw_pcie_rp *pp) 1481 { 1482 struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 1483 struct qcom_pcie *pcie = to_qcom_pcie(pci); 1484 int ret; 1485 1486 qcom_pcie_perst_assert(pcie); 1487 1488 ret = pcie->cfg->ops->init(pcie); 1489 if (ret) 1490 return ret; 1491 1492 ret = qcom_pcie_phy_power_on(pcie); 1493 if (ret) 1494 goto err_deinit; 1495 1496 if (!pci->suspended) { 1497 ret = pci_pwrctrl_create_devices(pci->dev); 1498 if (ret) 1499 goto err_disable_phy; 1500 } 1501 1502 if (!pp->skip_pwrctrl_off) { 1503 ret = pci_pwrctrl_power_on_devices(pci->dev); 1504 if (ret) 1505 goto err_pwrctrl_destroy; 1506 } 1507 1508 if (pcie->cfg->ops->post_init) { 1509 ret = pcie->cfg->ops->post_init(pcie); 1510 if (ret) 1511 goto err_pwrctrl_power_off; 1512 } 1513 1514 qcom_pcie_clear_aspm_l0s(pcie->pci); 1515 dw_pcie_remove_capability(pcie->pci, PCI_CAP_ID_MSIX); 1516 dw_pcie_remove_ext_capability(pcie->pci, PCI_EXT_CAP_ID_DPC); 1517 1518 qcom_pcie_configure_ports(pcie); 1519 1520 qcom_pcie_perst_deassert(pcie); 1521 1522 if (pcie->cfg->ops->config_sid) { 1523 ret = pcie->cfg->ops->config_sid(pcie); 1524 if (ret) 1525 goto err_assert_reset; 1526 } 1527 1528 pp->bridge->reset_root_port = qcom_pcie_reset_root_port; 1529 1530 return 0; 1531 1532 err_assert_reset: 1533 qcom_pcie_perst_assert(pcie); 1534 err_pwrctrl_power_off: 1535 if (!pp->skip_pwrctrl_off) 1536 pci_pwrctrl_power_off_devices(pci->dev); 1537 err_pwrctrl_destroy: 1538 if (ret != -EPROBE_DEFER && !pci->suspended) 1539 pci_pwrctrl_destroy_devices(pci->dev); 1540 err_disable_phy: 1541 qcom_pcie_phy_power_off(pcie); 1542 err_deinit: 1543 pcie->cfg->ops->deinit(pcie); 1544 1545 return ret; 1546 } 1547 1548 static void qcom_pcie_host_deinit(struct dw_pcie_rp *pp) 1549 { 1550 struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 1551 struct qcom_pcie *pcie = to_qcom_pcie(pci); 1552 1553 qcom_pcie_perst_assert(pcie); 1554 1555 if (!pci->pp.skip_pwrctrl_off) { 1556 /* 1557 * No need to destroy pwrctrl devices as this function only 1558 * gets called during system suspend as of now. 1559 */ 1560 pci_pwrctrl_power_off_devices(pci->dev); 1561 } 1562 1563 qcom_pcie_phy_power_off(pcie); 1564 pcie->cfg->ops->deinit(pcie); 1565 } 1566 1567 static void qcom_pcie_host_post_init(struct dw_pcie_rp *pp) 1568 { 1569 struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 1570 struct qcom_pcie *pcie = to_qcom_pcie(pci); 1571 1572 /* 1573 * During system suspend, the Qcom RC driver may turn off the 1574 * analog circuitry of PHY and remove controller votes to save 1575 * power. If the link is in L1SS and the endpoint asserts CLKREQ# 1576 * to exit L1SS, the time required to wake the system and restore 1577 * the PHY/REFCLK may exceed the L1SS exit timing (L10_REFCLK_ON + 1578 * T_COMMONMODE), resulting in Link Down (LDn) and a reset of the 1579 * endpoint. Set this flag to indicate this limitation to client 1580 * drivers so that they can avoid relying on device state being 1581 * preserved during system suspend. 1582 */ 1583 pp->bridge->broken_l1ss_resume = true; 1584 1585 if (pcie->cfg->ops->host_post_init) 1586 pcie->cfg->ops->host_post_init(pcie); 1587 } 1588 1589 static void qcom_pcie_host_pme_turn_off(struct dw_pcie_rp *pp) 1590 { 1591 struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 1592 1593 writel(ELBI_SYS_CTRL_PME_TURNOFF_MSG, pci->elbi_base + ELBI_SYS_CTRL); 1594 } 1595 1596 static const struct dw_pcie_host_ops qcom_pcie_dw_ops = { 1597 .init = qcom_pcie_host_init, 1598 .deinit = qcom_pcie_host_deinit, 1599 .post_init = qcom_pcie_host_post_init, 1600 .pme_turn_off = qcom_pcie_host_pme_turn_off, 1601 }; 1602 1603 /* Qcom IP rev.: 2.1.0 Synopsys IP rev.: 4.01a */ 1604 static const struct qcom_pcie_ops ops_2_1_0 = { 1605 .get_resources = qcom_pcie_get_resources_2_1_0, 1606 .init = qcom_pcie_init_2_1_0, 1607 .post_init = qcom_pcie_post_init_2_1_0, 1608 .deinit = qcom_pcie_deinit_2_1_0, 1609 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable, 1610 .get_ltssm = qcom_pcie_2_1_0_get_ltssm, 1611 }; 1612 1613 /* Qcom IP rev.: 1.0.0 Synopsys IP rev.: 4.11a */ 1614 static const struct qcom_pcie_ops ops_1_0_0 = { 1615 .get_resources = qcom_pcie_get_resources_1_0_0, 1616 .init = qcom_pcie_init_1_0_0, 1617 .post_init = qcom_pcie_post_init_1_0_0, 1618 .deinit = qcom_pcie_deinit_1_0_0, 1619 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable, 1620 .get_ltssm = qcom_pcie_2_1_0_get_ltssm, 1621 }; 1622 1623 /* Qcom IP rev.: 2.3.2 Synopsys IP rev.: 4.21a */ 1624 static const struct qcom_pcie_ops ops_2_3_2 = { 1625 .get_resources = qcom_pcie_get_resources_2_3_2, 1626 .init = qcom_pcie_init_2_3_2, 1627 .post_init = qcom_pcie_post_init_2_3_2, 1628 .deinit = qcom_pcie_deinit_2_3_2, 1629 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable, 1630 }; 1631 1632 /* Qcom IP rev.: 2.4.0 Synopsys IP rev.: 4.20a */ 1633 static const struct qcom_pcie_ops ops_2_4_0 = { 1634 .get_resources = qcom_pcie_get_resources_2_4_0, 1635 .init = qcom_pcie_init_2_4_0, 1636 .post_init = qcom_pcie_post_init_2_3_2, 1637 .deinit = qcom_pcie_deinit_2_4_0, 1638 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable, 1639 }; 1640 1641 /* Qcom IP rev.: 2.3.3 Synopsys IP rev.: 4.30a */ 1642 static const struct qcom_pcie_ops ops_2_3_3 = { 1643 .get_resources = qcom_pcie_get_resources_2_3_3, 1644 .init = qcom_pcie_init_2_3_3, 1645 .post_init = qcom_pcie_post_init_2_3_3, 1646 .deinit = qcom_pcie_deinit_2_3_3, 1647 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable, 1648 }; 1649 1650 /* Qcom IP rev.: 2.7.0 Synopsys IP rev.: 4.30a */ 1651 static const struct qcom_pcie_ops ops_2_7_0 = { 1652 .get_resources = qcom_pcie_get_resources_2_7_0, 1653 .init = qcom_pcie_init_2_7_0, 1654 .post_init = qcom_pcie_post_init_2_7_0, 1655 .deinit = qcom_pcie_deinit_2_7_0, 1656 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable, 1657 }; 1658 1659 /* Qcom IP rev.: 1.9.0 */ 1660 static const struct qcom_pcie_ops ops_1_9_0 = { 1661 .get_resources = qcom_pcie_get_resources_2_7_0, 1662 .init = qcom_pcie_init_2_7_0, 1663 .post_init = qcom_pcie_post_init_2_7_0, 1664 .host_post_init = qcom_pcie_host_post_init_2_7_0, 1665 .deinit = qcom_pcie_deinit_2_7_0, 1666 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable, 1667 .config_sid = qcom_pcie_config_sid_1_9_0, 1668 }; 1669 1670 /* Qcom IP rev.: 1.21.0 Synopsys IP rev.: 5.60a */ 1671 static const struct qcom_pcie_ops ops_1_21_0 = { 1672 .get_resources = qcom_pcie_get_resources_2_7_0, 1673 .init = qcom_pcie_init_2_7_0, 1674 .post_init = qcom_pcie_post_init_2_7_0, 1675 .host_post_init = qcom_pcie_host_post_init_2_7_0, 1676 .deinit = qcom_pcie_deinit_2_7_0, 1677 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable, 1678 }; 1679 1680 /* Qcom IP rev.: 2.9.0 Synopsys IP rev.: 5.00a */ 1681 static const struct qcom_pcie_ops ops_2_9_0 = { 1682 .get_resources = qcom_pcie_get_resources_2_9_0, 1683 .init = qcom_pcie_init_2_9_0, 1684 .post_init = qcom_pcie_post_init_2_9_0, 1685 .deinit = qcom_pcie_deinit_2_9_0, 1686 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable, 1687 }; 1688 1689 static const struct qcom_pcie_cfg cfg_1_0_0 = { 1690 .ops = &ops_1_0_0, 1691 }; 1692 1693 static const struct qcom_pcie_cfg cfg_1_9_0 = { 1694 .ops = &ops_1_9_0, 1695 }; 1696 1697 static const struct qcom_pcie_cfg cfg_1_34_0 = { 1698 .ops = &ops_1_9_0, 1699 .override_no_snoop = true, 1700 .no_l0s = true, 1701 }; 1702 1703 static const struct qcom_pcie_cfg cfg_2_1_0 = { 1704 .ops = &ops_2_1_0, 1705 }; 1706 1707 static const struct qcom_pcie_cfg cfg_2_3_2 = { 1708 .ops = &ops_2_3_2, 1709 .no_l0s = true, 1710 }; 1711 1712 static const struct qcom_pcie_cfg cfg_2_3_3 = { 1713 .ops = &ops_2_3_3, 1714 }; 1715 1716 static const struct qcom_pcie_cfg cfg_2_4_0 = { 1717 .ops = &ops_2_4_0, 1718 }; 1719 1720 static const struct qcom_pcie_cfg cfg_2_7_0 = { 1721 .ops = &ops_2_7_0, 1722 }; 1723 1724 static const struct qcom_pcie_cfg cfg_2_9_0 = { 1725 .ops = &ops_2_9_0, 1726 }; 1727 1728 static const struct qcom_pcie_cfg cfg_sc8280xp = { 1729 .ops = &ops_1_21_0, 1730 .no_l0s = true, 1731 }; 1732 1733 static const struct qcom_pcie_cfg cfg_fw_managed = { 1734 .firmware_managed = true, 1735 }; 1736 1737 static const struct dw_pcie_ops dw_pcie_ops = { 1738 .link_up = qcom_pcie_link_up, 1739 .start_link = qcom_pcie_start_link, 1740 .get_ltssm = qcom_pcie_get_ltssm, 1741 }; 1742 1743 static int qcom_pcie_icc_init(struct qcom_pcie *pcie) 1744 { 1745 struct dw_pcie *pci = pcie->pci; 1746 int ret; 1747 1748 pcie->icc_mem = devm_of_icc_get(pci->dev, "pcie-mem"); 1749 if (IS_ERR(pcie->icc_mem)) 1750 return PTR_ERR(pcie->icc_mem); 1751 1752 pcie->icc_cpu = devm_of_icc_get(pci->dev, "cpu-pcie"); 1753 if (IS_ERR(pcie->icc_cpu)) 1754 return PTR_ERR(pcie->icc_cpu); 1755 /* 1756 * Some Qualcomm platforms require interconnect bandwidth constraints 1757 * to be set before enabling interconnect clocks. 1758 * 1759 * Set an initial peak bandwidth corresponding to single-lane Gen 1 1760 * for the pcie-mem path. 1761 */ 1762 ret = icc_set_bw(pcie->icc_mem, 0, QCOM_PCIE_LINK_SPEED_TO_BW(1)); 1763 if (ret) { 1764 dev_err(pci->dev, "Failed to set bandwidth for PCIe-MEM interconnect path: %d\n", 1765 ret); 1766 return ret; 1767 } 1768 1769 /* 1770 * Since the CPU-PCIe path is only used for activities like register 1771 * access of the host controller and endpoint Config/BAR space access, 1772 * HW team has recommended to use a minimal bandwidth of 1KBps just to 1773 * keep the path active. 1774 */ 1775 ret = icc_set_bw(pcie->icc_cpu, 0, kBps_to_icc(1)); 1776 if (ret) { 1777 dev_err(pci->dev, "Failed to set bandwidth for CPU-PCIe interconnect path: %d\n", 1778 ret); 1779 icc_set_bw(pcie->icc_mem, 0, 0); 1780 return ret; 1781 } 1782 1783 return 0; 1784 } 1785 1786 static void qcom_pcie_icc_opp_update(struct qcom_pcie *pcie) 1787 { 1788 u32 offset, status, width, speed; 1789 struct dw_pcie *pci = pcie->pci; 1790 struct dev_pm_opp_key key = {}; 1791 unsigned long freq_kbps; 1792 struct dev_pm_opp *opp; 1793 int ret, freq_mbps; 1794 1795 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP); 1796 status = readw(pci->dbi_base + offset + PCI_EXP_LNKSTA); 1797 1798 /* Only update constraints if link is up. */ 1799 if (!(status & PCI_EXP_LNKSTA_DLLLA)) 1800 return; 1801 1802 speed = FIELD_GET(PCI_EXP_LNKSTA_CLS, status); 1803 width = FIELD_GET(PCI_EXP_LNKSTA_NLW, status); 1804 1805 if (pcie->icc_mem) { 1806 ret = icc_set_bw(pcie->icc_mem, 0, 1807 width * QCOM_PCIE_LINK_SPEED_TO_BW(speed)); 1808 if (ret) { 1809 dev_err(pci->dev, "Failed to set bandwidth for PCIe-MEM interconnect path: %d\n", 1810 ret); 1811 } 1812 } else if (pcie->use_pm_opp) { 1813 freq_mbps = pcie_dev_speed_mbps(pcie_get_link_speed(speed)); 1814 if (freq_mbps < 0) 1815 return; 1816 1817 freq_kbps = freq_mbps * KILO; 1818 opp = dev_pm_opp_find_level_exact(pci->dev, speed); 1819 if (IS_ERR(opp)) { 1820 /* opp-level is not defined use only frequency */ 1821 opp = dev_pm_opp_find_freq_exact(pci->dev, freq_kbps * width, 1822 true); 1823 } else { 1824 /* put opp-level OPP */ 1825 dev_pm_opp_put(opp); 1826 1827 key.freq = freq_kbps * width; 1828 key.level = speed; 1829 key.bw = 0; 1830 opp = dev_pm_opp_find_key_exact(pci->dev, &key, true); 1831 } 1832 if (!IS_ERR(opp)) { 1833 ret = dev_pm_opp_set_opp(pci->dev, opp); 1834 if (ret) 1835 dev_err(pci->dev, "Failed to set OPP for freq (%lu): %d\n", 1836 freq_kbps * width, ret); 1837 dev_pm_opp_put(opp); 1838 } 1839 } 1840 } 1841 1842 static int qcom_pcie_set_max_opp(struct device *dev) 1843 { 1844 unsigned long max_freq = ULONG_MAX; 1845 struct dev_pm_opp *opp; 1846 int ret; 1847 1848 opp = dev_pm_opp_find_freq_floor(dev, &max_freq); 1849 if (IS_ERR(opp)) 1850 return PTR_ERR(opp); 1851 1852 ret = dev_pm_opp_set_opp(dev, opp); 1853 dev_pm_opp_put(opp); 1854 1855 return ret; 1856 } 1857 1858 /* 1859 * Qcom PCIe controllers only support one Root Port per controller instance. So 1860 * this function ignores the 'pci_dev' associated with the Root Port and just 1861 * resets the host bridge, which in turn resets the Root Port also. 1862 */ 1863 static int qcom_pcie_reset_root_port(struct pci_host_bridge *bridge, 1864 struct pci_dev *pdev) 1865 { 1866 struct device *dev = bridge->dev.parent; 1867 struct qcom_pcie *pcie = dev_get_drvdata(dev); 1868 struct dw_pcie *pci = pcie->pci; 1869 struct dw_pcie_rp *pp = &pci->pp; 1870 u32 val; 1871 int ret; 1872 1873 guard(mutex)(&pcie->reset_lock); 1874 1875 /* Wait for the pending transactions to be completed */ 1876 ret = readl_relaxed_poll_timeout(pcie->parf + PARF_STATUS, val, 1877 val & FLUSH_COMPLETED, 10, 1878 FLUSH_TIMEOUT_US); 1879 if (ret) { 1880 dev_err(dev, "Flush completion failed: %d\n", ret); 1881 return ret; 1882 } 1883 1884 /* Clear the FLUSH_MODE to allow the core to be reset */ 1885 val = readl(pcie->parf + PARF_LTSSM); 1886 val |= SW_CLEAR_FLUSH_MODE; 1887 writel(val, pcie->parf + PARF_LTSSM); 1888 1889 /* Wait for the FLUSH_MODE to clear */ 1890 ret = readl_relaxed_poll_timeout(pcie->parf + PARF_LTSSM, val, 1891 !(val & FLUSH_MODE), 10, 1892 FLUSH_TIMEOUT_US); 1893 if (ret) { 1894 dev_err(dev, "Flush mode clear failed: %d\n", ret); 1895 return ret; 1896 } 1897 1898 qcom_pcie_host_deinit(pp); 1899 1900 ret = qcom_pcie_host_init(pp); 1901 if (ret) { 1902 dev_err(dev, "Host init failed\n"); 1903 return ret; 1904 } 1905 1906 ret = dw_pcie_setup_rc(pp); 1907 if (ret) 1908 return ret; 1909 1910 /* 1911 * Re-enable global IRQ events as the PARF_INT_ALL_MASK register is 1912 * non-sticky. 1913 */ 1914 if (pcie->global_irq) 1915 writel_relaxed(PARF_INT_ALL_LINK_DOWN | PARF_INT_MSI_DEV_0_7, 1916 pcie->parf + PARF_INT_ALL_MASK); 1917 1918 qcom_pcie_start_link(pci); 1919 1920 ret = dw_pcie_wait_for_link(pci); 1921 /* This linkdown might have been a hot-unplug */ 1922 if (ret == -ENODEV) 1923 ret = 0; 1924 if (ret) 1925 return ret; 1926 1927 dev_dbg(dev, "Root Port reset completed\n"); 1928 1929 return 0; 1930 } 1931 1932 static int qcom_pcie_link_transition_count(struct seq_file *s, void *data) 1933 { 1934 struct qcom_pcie *pcie = (struct qcom_pcie *)dev_get_drvdata(s->private); 1935 1936 seq_printf(s, "L0s transition count: %u\n", 1937 readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_PM_LINKST_IN_L0S)); 1938 1939 seq_printf(s, "L1 transition count: %u\n", 1940 readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_PM_LINKST_IN_L1)); 1941 1942 seq_printf(s, "L1.1 transition count: %u\n", 1943 readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L1)); 1944 1945 seq_printf(s, "L1.2 transition count: %u\n", 1946 readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2)); 1947 1948 seq_printf(s, "L2 transition count: %u\n", 1949 readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_PM_LINKST_IN_L2)); 1950 1951 return 0; 1952 } 1953 1954 static void qcom_pcie_init_debugfs(struct qcom_pcie *pcie) 1955 { 1956 struct dw_pcie *pci = pcie->pci; 1957 struct device *dev = pci->dev; 1958 char *name; 1959 1960 name = devm_kasprintf(dev, GFP_KERNEL, "%pOFP", dev->of_node); 1961 if (!name) 1962 return; 1963 1964 pcie->debugfs = debugfs_create_dir(name, NULL); 1965 debugfs_create_devm_seqfile(dev, "link_transition_count", pcie->debugfs, 1966 qcom_pcie_link_transition_count); 1967 } 1968 1969 static irqreturn_t qcom_pcie_global_irq_thread(int irq, void *data) 1970 { 1971 struct qcom_pcie *pcie = data; 1972 struct dw_pcie_rp *pp = &pcie->pci->pp; 1973 struct device *dev = pcie->pci->dev; 1974 struct pci_dev *port; 1975 unsigned long status = readl_relaxed(pcie->parf + PARF_INT_ALL_STATUS); 1976 1977 writel_relaxed(status, pcie->parf + PARF_INT_ALL_CLEAR); 1978 1979 if (test_and_clear_bit(INT_ALL_LINK_DOWN, &status)) { 1980 dev_dbg(dev, "Received Link down event\n"); 1981 1982 mutex_lock(&pcie->reset_lock); 1983 qcom_pcie_enable_outbound_blocker(pcie); 1984 mutex_unlock(&pcie->reset_lock); 1985 1986 for_each_pci_bridge(port, pp->bridge->bus) { 1987 if (pci_pcie_type(port) == PCI_EXP_TYPE_ROOT_PORT) 1988 pci_host_handle_link_down(port); 1989 } 1990 } 1991 1992 return IRQ_HANDLED; 1993 } 1994 1995 static void qcom_pci_free_msi(void *ptr) 1996 { 1997 struct dw_pcie_rp *pp = (struct dw_pcie_rp *)ptr; 1998 1999 if (pp && pp->use_imsi_rx) 2000 dw_pcie_free_msi(pp); 2001 } 2002 2003 static int qcom_pcie_ecam_host_init(struct pci_config_window *cfg) 2004 { 2005 struct device *dev = cfg->parent; 2006 struct dw_pcie_rp *pp; 2007 struct dw_pcie *pci; 2008 int ret; 2009 2010 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL); 2011 if (!pci) 2012 return -ENOMEM; 2013 2014 pci->dev = dev; 2015 pp = &pci->pp; 2016 pci->dbi_base = cfg->win; 2017 pp->num_vectors = MSI_DEF_NUM_VECTORS; 2018 2019 /* 2020 * dw_pcie_msi_host_init() is called directly here, bypassing 2021 * dw_pcie_host_init() where pp->lock is normally initialized. 2022 */ 2023 raw_spin_lock_init(&pp->lock); 2024 2025 pp->use_imsi_rx = !pci_host_of_has_msi_map(dev); 2026 if (pp->use_imsi_rx) { 2027 ret = dw_pcie_msi_host_init(pp); 2028 if (ret) 2029 return ret; 2030 } 2031 2032 dw_pcie_msi_init(pp); 2033 2034 return devm_add_action_or_reset(dev, qcom_pci_free_msi, pp); 2035 } 2036 2037 static const struct pci_ecam_ops pci_qcom_ecam_ops = { 2038 .init = qcom_pcie_ecam_host_init, 2039 .pci_ops = { 2040 .map_bus = pci_ecam_map_bus, 2041 .read = pci_generic_config_read, 2042 .write = pci_generic_config_write, 2043 } 2044 }; 2045 2046 /* Check if @node is a child of @dev in DT */ 2047 static bool qcom_pcie_is_child_node(struct device *dev, 2048 struct device_node *node) 2049 { 2050 struct device_node *parent; 2051 2052 for (parent = of_get_parent(node); parent; 2053 parent = of_get_next_parent(parent)) { 2054 if (parent == dev->of_node) { 2055 of_node_put(parent); 2056 return true; 2057 } 2058 } 2059 2060 return false; 2061 } 2062 2063 /* Collect PERST# GPIOs from PCI bridge nodes depth-first, starting at @np */ 2064 static int qcom_pcie_parse_perst(struct qcom_pcie *pcie, 2065 struct qcom_pcie_port *port, 2066 struct device_node *np) 2067 { 2068 struct device *dev = pcie->pci->dev; 2069 struct qcom_pcie_perst *perst; 2070 struct device_node *gpio_np; 2071 struct gpio_desc *reset; 2072 int ret; 2073 2074 if (pcie->reset) { 2075 dev_warn_once(dev, 2076 "Reusing PERST# from Root Complex node. DT needs to be fixed!\n"); 2077 reset = pcie->reset; 2078 goto skip_perst_parsing; 2079 } 2080 2081 if (!of_find_property(np, "reset-gpios", NULL)) 2082 goto parse_child_node; 2083 2084 /* 2085 * Skip GPIOs provided by a PCIe device which is a child of the Root 2086 * Complex (e.g., a PCIe switch with GPIO controller capability). Such 2087 * controllers won't be available at RC probe time and their PERST# 2088 * should be controlled by the respective PCI client driver 2089 * implementation. 2090 */ 2091 gpio_np = of_parse_phandle(np, "reset-gpios", 0); 2092 if (!gpio_np) { 2093 dev_err(dev, "Failed to parse GPIO provider\n"); 2094 return -EINVAL; 2095 } 2096 2097 if (qcom_pcie_is_child_node(dev, gpio_np)) { 2098 of_node_put(gpio_np); 2099 goto parse_child_node; 2100 } 2101 of_node_put(gpio_np); 2102 2103 reset = devm_fwnode_gpiod_get(dev, of_fwnode_handle(np), "reset", 2104 GPIOD_OUT_HIGH, "PERST#"); 2105 if (IS_ERR(reset)) { 2106 /* 2107 * FIXME: GPIOLIB currently supports exclusive GPIO access only. 2108 * Non exclusive access is broken. But shared PERST# requires 2109 * non-exclusive access. So once GPIOLIB properly supports it, 2110 * implement it here. 2111 */ 2112 if (PTR_ERR(reset) == -EBUSY) 2113 dev_err(dev, "Shared PERST# is not supported\n"); 2114 2115 return PTR_ERR(reset); 2116 } 2117 2118 skip_perst_parsing: 2119 perst = devm_kzalloc(dev, sizeof(*perst), GFP_KERNEL); 2120 if (!perst) 2121 return -ENOMEM; 2122 2123 INIT_LIST_HEAD(&perst->list); 2124 perst->desc = reset; 2125 list_add_tail(&perst->list, &port->perst); 2126 2127 parse_child_node: 2128 for_each_available_child_of_node_scoped(np, child) { 2129 if (!of_node_is_type(child, "pci")) 2130 continue; 2131 2132 ret = qcom_pcie_parse_perst(pcie, port, child); 2133 if (ret) 2134 return ret; 2135 } 2136 2137 return 0; 2138 } 2139 2140 static int qcom_pcie_parse_port(struct qcom_pcie *pcie, struct device_node *node) 2141 { 2142 struct device *dev = pcie->pci->dev; 2143 struct qcom_pcie_port *port; 2144 struct phy *phy; 2145 int ret; 2146 2147 phy = devm_of_phy_get(dev, node, NULL); 2148 if (IS_ERR(phy)) 2149 return PTR_ERR(phy); 2150 2151 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL); 2152 if (!port) 2153 return -ENOMEM; 2154 2155 ret = phy_init(phy); 2156 if (ret) 2157 return ret; 2158 2159 INIT_LIST_HEAD(&port->perst); 2160 2161 ret = qcom_pcie_parse_perst(pcie, port, node); 2162 if (ret) 2163 return ret; 2164 2165 /* TODO: Move to DWC core after multi Root Port support is added */ 2166 of_property_read_u32(node, "t-power-on-us", &port->l1ss_t_power_on); 2167 2168 port->phy = phy; 2169 INIT_LIST_HEAD(&port->list); 2170 list_add_tail(&port->list, &pcie->ports); 2171 2172 return 0; 2173 } 2174 2175 static int qcom_pcie_parse_ports(struct qcom_pcie *pcie) 2176 { 2177 struct qcom_pcie_perst *perst, *tmp_perst; 2178 struct qcom_pcie_port *port, *tmp_port; 2179 struct device *dev = pcie->pci->dev; 2180 int ret = -ENODEV; 2181 2182 if (of_find_property(dev->of_node, "perst-gpios", NULL)) { 2183 pcie->reset = devm_gpiod_get_optional(dev, "perst", 2184 GPIOD_OUT_HIGH); 2185 if (IS_ERR(pcie->reset)) 2186 return PTR_ERR(pcie->reset); 2187 } 2188 2189 for_each_available_child_of_node_scoped(dev->of_node, of_port) { 2190 if (!of_node_is_type(of_port, "pci")) 2191 continue; 2192 ret = qcom_pcie_parse_port(pcie, of_port); 2193 if (ret) 2194 goto err_port_del; 2195 } 2196 2197 return ret; 2198 2199 err_port_del: 2200 list_for_each_entry_safe(port, tmp_port, &pcie->ports, list) { 2201 list_for_each_entry_safe(perst, tmp_perst, &port->perst, list) 2202 list_del(&perst->list); 2203 phy_exit(port->phy); 2204 list_del(&port->list); 2205 } 2206 2207 return ret; 2208 } 2209 2210 static int qcom_pcie_parse_legacy_binding(struct qcom_pcie *pcie) 2211 { 2212 struct device *dev = pcie->pci->dev; 2213 struct qcom_pcie_perst *perst; 2214 struct qcom_pcie_port *port; 2215 struct phy *phy; 2216 int ret; 2217 2218 phy = devm_phy_optional_get(dev, "pciephy"); 2219 if (IS_ERR(phy)) 2220 return PTR_ERR(phy); 2221 2222 ret = phy_init(phy); 2223 if (ret) 2224 return ret; 2225 2226 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL); 2227 if (!port) 2228 return -ENOMEM; 2229 2230 perst = devm_kzalloc(dev, sizeof(*perst), GFP_KERNEL); 2231 if (!perst) 2232 return -ENOMEM; 2233 2234 port->phy = phy; 2235 INIT_LIST_HEAD(&port->list); 2236 list_add_tail(&port->list, &pcie->ports); 2237 2238 perst->desc = pcie->reset; 2239 INIT_LIST_HEAD(&port->perst); 2240 INIT_LIST_HEAD(&perst->list); 2241 list_add_tail(&perst->list, &port->perst); 2242 2243 return 0; 2244 } 2245 2246 static int qcom_pcie_probe(struct platform_device *pdev) 2247 { 2248 struct qcom_pcie_perst *perst, *tmp_perst; 2249 struct qcom_pcie_port *port, *tmp_port; 2250 const struct qcom_pcie_cfg *pcie_cfg; 2251 struct device *dev = &pdev->dev; 2252 struct qcom_pcie *pcie; 2253 struct dw_pcie_rp *pp; 2254 struct resource *res; 2255 struct dw_pcie *pci; 2256 int ret, irq; 2257 2258 pcie_cfg = of_device_get_match_data(dev); 2259 if (!pcie_cfg) { 2260 dev_err(dev, "No platform data\n"); 2261 return -ENODATA; 2262 } 2263 2264 if (!pcie_cfg->firmware_managed && !pcie_cfg->ops) { 2265 dev_err(dev, "No platform ops\n"); 2266 return -ENODATA; 2267 } 2268 2269 pm_runtime_enable(dev); 2270 ret = pm_runtime_get_sync(dev); 2271 if (ret < 0) 2272 goto err_pm_runtime_put; 2273 2274 if (pcie_cfg->firmware_managed) { 2275 struct pci_host_bridge *bridge; 2276 struct pci_config_window *cfg; 2277 2278 bridge = devm_pci_alloc_host_bridge(dev, 0); 2279 if (!bridge) { 2280 ret = -ENOMEM; 2281 goto err_pm_runtime_put; 2282 } 2283 2284 /* Parse and map our ECAM configuration space area */ 2285 cfg = pci_host_common_ecam_create(dev, bridge, 2286 &pci_qcom_ecam_ops); 2287 if (IS_ERR(cfg)) { 2288 ret = PTR_ERR(cfg); 2289 goto err_pm_runtime_put; 2290 } 2291 2292 bridge->sysdata = cfg; 2293 bridge->ops = (struct pci_ops *)&pci_qcom_ecam_ops.pci_ops; 2294 bridge->msi_domain = true; 2295 2296 ret = pci_host_probe(bridge); 2297 if (ret) 2298 goto err_pm_runtime_put; 2299 2300 return 0; 2301 } 2302 2303 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL); 2304 if (!pcie) { 2305 ret = -ENOMEM; 2306 goto err_pm_runtime_put; 2307 } 2308 2309 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL); 2310 if (!pci) { 2311 ret = -ENOMEM; 2312 goto err_pm_runtime_put; 2313 } 2314 2315 INIT_LIST_HEAD(&pcie->ports); 2316 2317 ret = devm_mutex_init(dev, &pcie->reset_lock); 2318 if (ret) 2319 goto err_pm_runtime_put; 2320 2321 pci->dev = dev; 2322 pci->ops = &dw_pcie_ops; 2323 pp = &pci->pp; 2324 2325 pcie->pci = pci; 2326 2327 pcie->cfg = pcie_cfg; 2328 2329 pcie->parf = devm_platform_ioremap_resource_byname(pdev, "parf"); 2330 if (IS_ERR(pcie->parf)) { 2331 ret = PTR_ERR(pcie->parf); 2332 goto err_pm_runtime_put; 2333 } 2334 2335 /* MHI region is optional */ 2336 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mhi"); 2337 if (res) { 2338 pcie->mhi = devm_ioremap_resource(dev, res); 2339 if (IS_ERR(pcie->mhi)) { 2340 ret = PTR_ERR(pcie->mhi); 2341 goto err_pm_runtime_put; 2342 } 2343 } 2344 2345 /* OPP table is optional */ 2346 ret = devm_pm_opp_of_add_table(dev); 2347 if (ret && ret != -ENODEV) { 2348 dev_err_probe(dev, ret, "Failed to add OPP table\n"); 2349 goto err_pm_runtime_put; 2350 } 2351 2352 /* 2353 * Before the PCIe link is initialized, vote for highest OPP in the OPP 2354 * table, so that we are voting for maximum voltage corner for the 2355 * link to come up in maximum supported speed. At the end of the 2356 * probe(), OPP will be updated using qcom_pcie_icc_opp_update(). 2357 */ 2358 if (!ret) { 2359 ret = qcom_pcie_set_max_opp(dev); 2360 if (ret) { 2361 dev_err_probe(dev, ret, "Failed to set max OPP\n"); 2362 goto err_pm_runtime_put; 2363 } 2364 2365 pcie->use_pm_opp = true; 2366 } else { 2367 /* Skip ICC init if OPP is supported as it is handled by OPP */ 2368 ret = qcom_pcie_icc_init(pcie); 2369 if (ret) 2370 goto err_pm_runtime_put; 2371 } 2372 2373 ret = pcie->cfg->ops->get_resources(pcie); 2374 if (ret) 2375 goto err_pm_runtime_put; 2376 2377 pp->ops = &qcom_pcie_dw_ops; 2378 2379 ret = qcom_pcie_parse_ports(pcie); 2380 if (ret) { 2381 if (ret != -ENODEV) { 2382 dev_err_probe(pci->dev, ret, 2383 "Failed to parse Root Port: %d\n", ret); 2384 goto err_pm_runtime_put; 2385 } 2386 2387 /* 2388 * In the case of properties not populated in Root Port node, 2389 * fallback to the legacy method of parsing the Host Bridge 2390 * node. This is to maintain DT backwards compatibility. 2391 */ 2392 ret = qcom_pcie_parse_legacy_binding(pcie); 2393 if (ret) 2394 goto err_pm_runtime_put; 2395 } 2396 2397 platform_set_drvdata(pdev, pcie); 2398 2399 ret = dw_pcie_host_init(pp); 2400 if (ret) { 2401 dev_err_probe(dev, ret, "cannot initialize host\n"); 2402 goto err_phy_exit; 2403 } 2404 2405 irq = platform_get_irq_byname_optional(pdev, "global"); 2406 if (irq > 0) { 2407 const char *name; 2408 2409 name = devm_kasprintf(dev, GFP_KERNEL, "qcom_pcie_global_irq%d", 2410 pci_domain_nr(pp->bridge->bus)); 2411 if (!name) { 2412 ret = -ENOMEM; 2413 goto err_host_deinit; 2414 } 2415 2416 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, 2417 qcom_pcie_global_irq_thread, 2418 IRQF_ONESHOT, name, pcie); 2419 if (ret) { 2420 dev_err_probe(&pdev->dev, ret, 2421 "Failed to request Global IRQ\n"); 2422 goto err_host_deinit; 2423 } 2424 2425 writel_relaxed(PARF_INT_ALL_LINK_DOWN | PARF_INT_MSI_DEV_0_7, 2426 pcie->parf + PARF_INT_ALL_MASK); 2427 2428 pcie->global_irq = irq; 2429 } 2430 2431 qcom_pcie_icc_opp_update(pcie); 2432 2433 if (pcie->mhi) 2434 qcom_pcie_init_debugfs(pcie); 2435 2436 return 0; 2437 2438 err_host_deinit: 2439 dw_pcie_host_deinit(pp); 2440 err_phy_exit: 2441 list_for_each_entry_safe(port, tmp_port, &pcie->ports, list) { 2442 list_for_each_entry_safe(perst, tmp_perst, &port->perst, list) 2443 list_del(&perst->list); 2444 phy_exit(port->phy); 2445 list_del(&port->list); 2446 } 2447 err_pm_runtime_put: 2448 pm_runtime_put(dev); 2449 pm_runtime_disable(dev); 2450 2451 return ret; 2452 } 2453 2454 static int qcom_pcie_suspend_noirq(struct device *dev) 2455 { 2456 struct qcom_pcie *pcie; 2457 int ret = 0; 2458 const struct qcom_pcie_cfg *pcie_cfg = of_device_get_match_data(dev); 2459 2460 if (pcie_cfg && pcie_cfg->firmware_managed) 2461 return 0; 2462 2463 pcie = dev_get_drvdata(dev); 2464 if (!pcie) 2465 return 0; 2466 2467 ret = dw_pcie_suspend_noirq(pcie->pci); 2468 if (ret) 2469 return ret; 2470 2471 if (pcie->pci->suspended) { 2472 ret = icc_disable(pcie->icc_mem); 2473 if (ret) 2474 dev_err(dev, "Failed to disable PCIe-MEM interconnect path: %d\n", ret); 2475 2476 ret = icc_disable(pcie->icc_cpu); 2477 if (ret) 2478 dev_err(dev, "Failed to disable CPU-PCIe interconnect path: %d\n", ret); 2479 2480 if (pcie->use_pm_opp) 2481 dev_pm_opp_set_opp(pcie->pci->dev, NULL); 2482 } else { 2483 /* 2484 * Set minimum bandwidth required to keep data path 2485 * functional during suspend. 2486 */ 2487 if (pcie->icc_mem) { 2488 ret = icc_set_bw(pcie->icc_mem, 0, kBps_to_icc(1)); 2489 if (ret) { 2490 dev_err(dev, 2491 "Failed to set bandwidth for PCIe-MEM interconnect path: %d\n", 2492 ret); 2493 return ret; 2494 } 2495 } 2496 2497 /* 2498 * Only disable CPU-PCIe interconnect path if the suspend 2499 * is non-S2RAM. On some platforms, DBI access can happen 2500 * very late during S2RAM and a non-active CPU-PCIe 2501 * interconnect path may lead to NoC error. 2502 */ 2503 if (pm_suspend_target_state != PM_SUSPEND_MEM) { 2504 ret = icc_disable(pcie->icc_cpu); 2505 if (ret) 2506 dev_err(dev, "Failed to disable CPU-PCIe interconnect path: %d\n", 2507 ret); 2508 2509 if (pcie->use_pm_opp) 2510 dev_pm_opp_set_opp(pcie->pci->dev, NULL); 2511 } 2512 } 2513 return ret; 2514 } 2515 2516 static int qcom_pcie_resume_noirq(struct device *dev) 2517 { 2518 struct qcom_pcie *pcie; 2519 int ret; 2520 const struct qcom_pcie_cfg *pcie_cfg = of_device_get_match_data(dev); 2521 2522 if (pcie_cfg && pcie_cfg->firmware_managed) 2523 return 0; 2524 2525 pcie = dev_get_drvdata(dev); 2526 if (!pcie) 2527 return 0; 2528 2529 if (pcie->pci->suspended) { 2530 if (pcie->use_pm_opp) { 2531 ret = qcom_pcie_set_max_opp(dev); 2532 if (ret) { 2533 dev_err(dev, "Failed to set max OPP: %d\n", ret); 2534 return ret; 2535 } 2536 } 2537 2538 ret = icc_enable(pcie->icc_cpu); 2539 if (ret) { 2540 dev_err(dev, "Failed to enable CPU-PCIe interconnect path: %d\n", ret); 2541 return ret; 2542 } 2543 2544 ret = icc_enable(pcie->icc_mem); 2545 if (ret) { 2546 dev_err(dev, "Failed to enable PCIe-MEM interconnect path: %d\n", ret); 2547 goto disable_icc_cpu; 2548 } 2549 2550 /* 2551 * Ignore -ENODEV & -EIO here since it is expected when no 2552 * endpoint is connected to the PCIe link. 2553 */ 2554 ret = dw_pcie_resume_noirq(pcie->pci); 2555 if (ret && ret != -ENODEV && ret != -EIO) 2556 goto disable_icc_mem; 2557 } else { 2558 if (pm_suspend_target_state != PM_SUSPEND_MEM) { 2559 if (pcie->use_pm_opp) { 2560 ret = qcom_pcie_set_max_opp(dev); 2561 if (ret) { 2562 dev_err(dev, "Failed to set max OPP: %d\n", ret); 2563 return ret; 2564 } 2565 } 2566 2567 ret = icc_enable(pcie->icc_cpu); 2568 if (ret) { 2569 dev_err(dev, "Failed to enable CPU-PCIe interconnect path: %d\n", 2570 ret); 2571 return ret; 2572 } 2573 } 2574 } 2575 2576 qcom_pcie_icc_opp_update(pcie); 2577 2578 return 0; 2579 disable_icc_mem: 2580 icc_disable(pcie->icc_mem); 2581 disable_icc_cpu: 2582 icc_disable(pcie->icc_cpu); 2583 2584 return ret; 2585 } 2586 2587 static const struct of_device_id qcom_pcie_match[] = { 2588 { .compatible = "qcom,hawi-pcie", .data = &cfg_1_9_0 }, 2589 { .compatible = "qcom,pcie-apq8064", .data = &cfg_2_1_0 }, 2590 { .compatible = "qcom,pcie-apq8084", .data = &cfg_1_0_0 }, 2591 { .compatible = "qcom,pcie-ipq4019", .data = &cfg_2_4_0 }, 2592 { .compatible = "qcom,pcie-ipq5018", .data = &cfg_2_9_0 }, 2593 { .compatible = "qcom,pcie-ipq6018", .data = &cfg_2_9_0 }, 2594 { .compatible = "qcom,pcie-ipq8064", .data = &cfg_2_1_0 }, 2595 { .compatible = "qcom,pcie-ipq8064-v2", .data = &cfg_2_1_0 }, 2596 { .compatible = "qcom,pcie-ipq8074", .data = &cfg_2_3_3 }, 2597 { .compatible = "qcom,pcie-ipq8074-gen3", .data = &cfg_2_9_0 }, 2598 { .compatible = "qcom,pcie-ipq9574", .data = &cfg_2_9_0 }, 2599 { .compatible = "qcom,pcie-msm8996", .data = &cfg_2_3_2 }, 2600 { .compatible = "qcom,pcie-qcs404", .data = &cfg_2_4_0 }, 2601 { .compatible = "qcom,pcie-sa8255p", .data = &cfg_fw_managed }, 2602 { .compatible = "qcom,pcie-sa8540p", .data = &cfg_sc8280xp }, 2603 { .compatible = "qcom,pcie-sa8775p", .data = &cfg_1_34_0}, 2604 { .compatible = "qcom,pcie-sc7280", .data = &cfg_1_9_0 }, 2605 { .compatible = "qcom,pcie-sc8180x", .data = &cfg_1_9_0 }, 2606 { .compatible = "qcom,pcie-sc8280xp", .data = &cfg_sc8280xp }, 2607 { .compatible = "qcom,pcie-sdm845", .data = &cfg_2_7_0 }, 2608 { .compatible = "qcom,pcie-sdx55", .data = &cfg_1_9_0 }, 2609 { .compatible = "qcom,pcie-sm8150", .data = &cfg_1_9_0 }, 2610 { .compatible = "qcom,pcie-sm8250", .data = &cfg_1_9_0 }, 2611 { .compatible = "qcom,pcie-sm8350", .data = &cfg_1_9_0 }, 2612 { .compatible = "qcom,pcie-sm8450-pcie0", .data = &cfg_1_9_0 }, 2613 { .compatible = "qcom,pcie-sm8450-pcie1", .data = &cfg_1_9_0 }, 2614 { .compatible = "qcom,pcie-sm8550", .data = &cfg_1_9_0 }, 2615 { .compatible = "qcom,pcie-x1e80100", .data = &cfg_sc8280xp }, 2616 { } 2617 }; 2618 2619 static void qcom_fixup_class(struct pci_dev *dev) 2620 { 2621 dev->class = PCI_CLASS_BRIDGE_PCI_NORMAL; 2622 } 2623 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0101, qcom_fixup_class); 2624 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0104, qcom_fixup_class); 2625 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0106, qcom_fixup_class); 2626 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0107, qcom_fixup_class); 2627 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0302, qcom_fixup_class); 2628 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1000, qcom_fixup_class); 2629 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1001, qcom_fixup_class); 2630 2631 static const struct dev_pm_ops qcom_pcie_pm_ops = { 2632 NOIRQ_SYSTEM_SLEEP_PM_OPS(qcom_pcie_suspend_noirq, qcom_pcie_resume_noirq) 2633 }; 2634 2635 static struct platform_driver qcom_pcie_driver = { 2636 .probe = qcom_pcie_probe, 2637 .driver = { 2638 .name = "qcom-pcie", 2639 .suppress_bind_attrs = true, 2640 .of_match_table = qcom_pcie_match, 2641 .pm = &qcom_pcie_pm_ops, 2642 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 2643 }, 2644 }; 2645 builtin_platform_driver(qcom_pcie_driver); 2646