Linux Audio

Check our new training course

Loading...
v6.13.7
   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/of.h>
 
  24#include <linux/pci.h>
  25#include <linux/pm_opp.h>
  26#include <linux/pm_runtime.h>
  27#include <linux/platform_device.h>
  28#include <linux/phy/pcie.h>
  29#include <linux/phy/phy.h>
  30#include <linux/regulator/consumer.h>
  31#include <linux/reset.h>
  32#include <linux/slab.h>
  33#include <linux/types.h>
  34#include <linux/units.h>
  35
  36#include "../../pci.h"
  37#include "pcie-designware.h"
  38#include "pcie-qcom-common.h"
  39
  40/* PARF registers */
  41#define PARF_SYS_CTRL				0x00
  42#define PARF_PM_CTRL				0x20
  43#define PARF_PCS_DEEMPH				0x34
  44#define PARF_PCS_SWING				0x38
  45#define PARF_PHY_CTRL				0x40
  46#define PARF_PHY_REFCLK				0x4c
  47#define PARF_CONFIG_BITS			0x50
  48#define PARF_DBI_BASE_ADDR			0x168
  49#define PARF_SLV_ADDR_SPACE_SIZE		0x16c
  50#define PARF_MHI_CLOCK_RESET_CTRL		0x174
  51#define PARF_AXI_MSTR_WR_ADDR_HALT		0x178
  52#define PARF_AXI_MSTR_WR_ADDR_HALT_V2		0x1a8
  53#define PARF_Q2A_FLUSH				0x1ac
  54#define PARF_LTSSM				0x1b0
  55#define PARF_INT_ALL_STATUS			0x224
  56#define PARF_INT_ALL_CLEAR			0x228
  57#define PARF_INT_ALL_MASK			0x22c
  58#define PARF_SID_OFFSET				0x234
  59#define PARF_BDF_TRANSLATE_CFG			0x24c
  60#define PARF_DBI_BASE_ADDR_V2			0x350
  61#define PARF_DBI_BASE_ADDR_V2_HI		0x354
  62#define PARF_SLV_ADDR_SPACE_SIZE_V2		0x358
  63#define PARF_SLV_ADDR_SPACE_SIZE_V2_HI		0x35c
  64#define PARF_NO_SNOOP_OVERIDE			0x3d4
  65#define PARF_ATU_BASE_ADDR			0x634
  66#define PARF_ATU_BASE_ADDR_HI			0x638
  67#define PARF_DEVICE_TYPE			0x1000
  68#define PARF_BDF_TO_SID_TABLE_N			0x2000
  69#define PARF_BDF_TO_SID_CFG			0x2c00
  70
  71/* ELBI registers */
  72#define ELBI_SYS_CTRL				0x04
  73
  74/* DBI registers */
  75#define AXI_MSTR_RESP_COMP_CTRL0		0x818
  76#define AXI_MSTR_RESP_COMP_CTRL1		0x81c
  77
  78/* MHI registers */
  79#define PARF_DEBUG_CNT_PM_LINKST_IN_L2		0xc04
  80#define PARF_DEBUG_CNT_PM_LINKST_IN_L1		0xc0c
  81#define PARF_DEBUG_CNT_PM_LINKST_IN_L0S		0xc10
  82#define PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L1	0xc84
  83#define PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2	0xc88
  84
  85/* PARF_SYS_CTRL register fields */
  86#define MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN	BIT(29)
  87#define MST_WAKEUP_EN				BIT(13)
  88#define SLV_WAKEUP_EN				BIT(12)
  89#define MSTR_ACLK_CGC_DIS			BIT(10)
  90#define SLV_ACLK_CGC_DIS			BIT(9)
  91#define CORE_CLK_CGC_DIS			BIT(6)
  92#define AUX_PWR_DET				BIT(4)
  93#define L23_CLK_RMV_DIS				BIT(2)
  94#define L1_CLK_RMV_DIS				BIT(1)
  95
  96/* PARF_PM_CTRL register fields */
  97#define REQ_NOT_ENTR_L1				BIT(5)
  98
  99/* PARF_PCS_DEEMPH register fields */
 100#define PCS_DEEMPH_TX_DEEMPH_GEN1(x)		FIELD_PREP(GENMASK(21, 16), x)
 101#define PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(x)	FIELD_PREP(GENMASK(13, 8), x)
 102#define PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(x)	FIELD_PREP(GENMASK(5, 0), x)
 103
 104/* PARF_PCS_SWING register fields */
 105#define PCS_SWING_TX_SWING_FULL(x)		FIELD_PREP(GENMASK(14, 8), x)
 106#define PCS_SWING_TX_SWING_LOW(x)		FIELD_PREP(GENMASK(6, 0), x)
 107
 108/* PARF_PHY_CTRL register fields */
 109#define PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK	GENMASK(20, 16)
 110#define PHY_CTRL_PHY_TX0_TERM_OFFSET(x)		FIELD_PREP(PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK, x)
 111#define PHY_TEST_PWR_DOWN			BIT(0)
 112
 113/* PARF_PHY_REFCLK register fields */
 114#define PHY_REFCLK_SSP_EN			BIT(16)
 115#define PHY_REFCLK_USE_PAD			BIT(12)
 116
 117/* PARF_CONFIG_BITS register fields */
 118#define PHY_RX0_EQ(x)				FIELD_PREP(GENMASK(26, 24), x)
 119
 120/* PARF_SLV_ADDR_SPACE_SIZE register value */
 121#define SLV_ADDR_SPACE_SZ			0x80000000
 122
 123/* PARF_MHI_CLOCK_RESET_CTRL register fields */
 124#define AHB_CLK_EN				BIT(0)
 125#define MSTR_AXI_CLK_EN				BIT(1)
 126#define BYPASS					BIT(4)
 127
 128/* PARF_AXI_MSTR_WR_ADDR_HALT register fields */
 129#define EN					BIT(31)
 130
 131/* PARF_LTSSM register fields */
 132#define LTSSM_EN				BIT(8)
 133
 134/* PARF_INT_ALL_{STATUS/CLEAR/MASK} register fields */
 135#define PARF_INT_ALL_LINK_UP			BIT(13)
 136#define PARF_INT_MSI_DEV_0_7			GENMASK(30, 23)
 137
 138/* PARF_NO_SNOOP_OVERIDE register fields */
 139#define WR_NO_SNOOP_OVERIDE_EN			BIT(1)
 140#define RD_NO_SNOOP_OVERIDE_EN			BIT(3)
 141
 142/* PARF_DEVICE_TYPE register fields */
 143#define DEVICE_TYPE_RC				0x4
 144
 145/* PARF_BDF_TO_SID_CFG fields */
 146#define BDF_TO_SID_BYPASS			BIT(0)
 147
 148/* ELBI_SYS_CTRL register fields */
 149#define ELBI_SYS_CTRL_LT_ENABLE			BIT(0)
 150
 151/* AXI_MSTR_RESP_COMP_CTRL0 register fields */
 152#define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K	0x4
 153#define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_4K	0x5
 154
 155/* AXI_MSTR_RESP_COMP_CTRL1 register fields */
 156#define CFG_BRIDGE_SB_INIT			BIT(0)
 157
 158/* PCI_EXP_SLTCAP register fields */
 159#define PCIE_CAP_SLOT_POWER_LIMIT_VAL		FIELD_PREP(PCI_EXP_SLTCAP_SPLV, 250)
 160#define PCIE_CAP_SLOT_POWER_LIMIT_SCALE		FIELD_PREP(PCI_EXP_SLTCAP_SPLS, 1)
 161#define PCIE_CAP_SLOT_VAL			(PCI_EXP_SLTCAP_ABP | \
 162						PCI_EXP_SLTCAP_PCP | \
 163						PCI_EXP_SLTCAP_MRLSP | \
 164						PCI_EXP_SLTCAP_AIP | \
 165						PCI_EXP_SLTCAP_PIP | \
 166						PCI_EXP_SLTCAP_HPS | \
 167						PCI_EXP_SLTCAP_EIP | \
 168						PCIE_CAP_SLOT_POWER_LIMIT_VAL | \
 169						PCIE_CAP_SLOT_POWER_LIMIT_SCALE)
 170
 171#define PERST_DELAY_US				1000
 172
 173#define QCOM_PCIE_CRC8_POLYNOMIAL		(BIT(2) | BIT(1) | BIT(0))
 174
 175#define QCOM_PCIE_LINK_SPEED_TO_BW(speed) \
 176		Mbps_to_icc(PCIE_SPEED2MBS_ENC(pcie_link_speed[speed]))
 177
 
 178struct qcom_pcie_resources_1_0_0 {
 179	struct clk_bulk_data *clks;
 180	int num_clks;
 181	struct reset_control *core;
 182	struct regulator *vdda;
 183};
 184
 
 185#define QCOM_PCIE_2_1_0_MAX_RESETS		6
 186#define QCOM_PCIE_2_1_0_MAX_SUPPLY		3
 187struct qcom_pcie_resources_2_1_0 {
 188	struct clk_bulk_data *clks;
 189	int num_clks;
 190	struct reset_control_bulk_data resets[QCOM_PCIE_2_1_0_MAX_RESETS];
 191	int num_resets;
 192	struct regulator_bulk_data supplies[QCOM_PCIE_2_1_0_MAX_SUPPLY];
 193};
 194
 
 195#define QCOM_PCIE_2_3_2_MAX_SUPPLY		2
 196struct qcom_pcie_resources_2_3_2 {
 197	struct clk_bulk_data *clks;
 198	int num_clks;
 199	struct regulator_bulk_data supplies[QCOM_PCIE_2_3_2_MAX_SUPPLY];
 200};
 201
 
 202#define QCOM_PCIE_2_3_3_MAX_RESETS		7
 203struct qcom_pcie_resources_2_3_3 {
 204	struct clk_bulk_data *clks;
 205	int num_clks;
 206	struct reset_control_bulk_data rst[QCOM_PCIE_2_3_3_MAX_RESETS];
 207};
 208
 
 209#define QCOM_PCIE_2_4_0_MAX_RESETS		12
 210struct qcom_pcie_resources_2_4_0 {
 211	struct clk_bulk_data *clks;
 212	int num_clks;
 213	struct reset_control_bulk_data resets[QCOM_PCIE_2_4_0_MAX_RESETS];
 214	int num_resets;
 215};
 216
 
 217#define QCOM_PCIE_2_7_0_MAX_SUPPLIES		2
 218struct qcom_pcie_resources_2_7_0 {
 219	struct clk_bulk_data *clks;
 220	int num_clks;
 221	struct regulator_bulk_data supplies[QCOM_PCIE_2_7_0_MAX_SUPPLIES];
 222	struct reset_control *rst;
 223};
 224
 
 225struct qcom_pcie_resources_2_9_0 {
 226	struct clk_bulk_data *clks;
 227	int num_clks;
 228	struct reset_control *rst;
 229};
 230
 231union qcom_pcie_resources {
 232	struct qcom_pcie_resources_1_0_0 v1_0_0;
 233	struct qcom_pcie_resources_2_1_0 v2_1_0;
 234	struct qcom_pcie_resources_2_3_2 v2_3_2;
 235	struct qcom_pcie_resources_2_3_3 v2_3_3;
 236	struct qcom_pcie_resources_2_4_0 v2_4_0;
 237	struct qcom_pcie_resources_2_7_0 v2_7_0;
 238	struct qcom_pcie_resources_2_9_0 v2_9_0;
 239};
 240
 241struct qcom_pcie;
 242
 243struct qcom_pcie_ops {
 244	int (*get_resources)(struct qcom_pcie *pcie);
 245	int (*init)(struct qcom_pcie *pcie);
 246	int (*post_init)(struct qcom_pcie *pcie);
 247	void (*host_post_init)(struct qcom_pcie *pcie);
 248	void (*deinit)(struct qcom_pcie *pcie);
 249	void (*ltssm_enable)(struct qcom_pcie *pcie);
 250	int (*config_sid)(struct qcom_pcie *pcie);
 251};
 252
 253 /**
 254  * struct qcom_pcie_cfg - Per SoC config struct
 255  * @ops: qcom PCIe ops structure
 256  * @override_no_snoop: Override NO_SNOOP attribute in TLP to enable cache
 257  * snooping
 258  */
 259struct qcom_pcie_cfg {
 260	const struct qcom_pcie_ops *ops;
 261	bool override_no_snoop;
 262	bool no_l0s;
 263};
 264
 265struct qcom_pcie {
 266	struct dw_pcie *pci;
 267	void __iomem *parf;			/* DT parf */
 268	void __iomem *elbi;			/* DT elbi */
 269	void __iomem *mhi;
 270	union qcom_pcie_resources res;
 271	struct phy *phy;
 272	struct gpio_desc *reset;
 273	struct icc_path *icc_mem;
 274	struct icc_path *icc_cpu;
 275	const struct qcom_pcie_cfg *cfg;
 276	struct dentry *debugfs;
 277	bool suspended;
 278	bool use_pm_opp;
 279};
 280
 281#define to_qcom_pcie(x)		dev_get_drvdata((x)->dev)
 282
 283static void qcom_ep_reset_assert(struct qcom_pcie *pcie)
 284{
 285	gpiod_set_value_cansleep(pcie->reset, 1);
 286	usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
 287}
 288
 289static void qcom_ep_reset_deassert(struct qcom_pcie *pcie)
 290{
 291	/* Ensure that PERST has been asserted for at least 100 ms */
 292	msleep(100);
 293	gpiod_set_value_cansleep(pcie->reset, 0);
 294	usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
 295}
 296
 297static int qcom_pcie_start_link(struct dw_pcie *pci)
 298{
 299	struct qcom_pcie *pcie = to_qcom_pcie(pci);
 300
 301	if (pcie_link_speed[pci->max_link_speed] == PCIE_SPEED_16_0GT) {
 302		qcom_pcie_common_set_16gt_equalization(pci);
 303		qcom_pcie_common_set_16gt_lane_margining(pci);
 304	}
 305
 306	/* Enable Link Training state machine */
 307	if (pcie->cfg->ops->ltssm_enable)
 308		pcie->cfg->ops->ltssm_enable(pcie);
 309
 310	return 0;
 311}
 312
 313static void qcom_pcie_clear_aspm_l0s(struct dw_pcie *pci)
 314{
 315	struct qcom_pcie *pcie = to_qcom_pcie(pci);
 316	u16 offset;
 317	u32 val;
 318
 319	if (!pcie->cfg->no_l0s)
 320		return;
 321
 322	offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
 323
 324	dw_pcie_dbi_ro_wr_en(pci);
 325
 326	val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP);
 327	val &= ~PCI_EXP_LNKCAP_ASPM_L0S;
 328	writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP);
 329
 330	dw_pcie_dbi_ro_wr_dis(pci);
 331}
 332
 333static void qcom_pcie_clear_hpc(struct dw_pcie *pci)
 334{
 335	u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
 336	u32 val;
 337
 338	dw_pcie_dbi_ro_wr_en(pci);
 339
 340	val = readl(pci->dbi_base + offset + PCI_EXP_SLTCAP);
 341	val &= ~PCI_EXP_SLTCAP_HPC;
 342	writel(val, pci->dbi_base + offset + PCI_EXP_SLTCAP);
 343
 344	dw_pcie_dbi_ro_wr_dis(pci);
 345}
 346
 347static void qcom_pcie_configure_dbi_base(struct qcom_pcie *pcie)
 348{
 349	struct dw_pcie *pci = pcie->pci;
 350
 351	if (pci->dbi_phys_addr) {
 352		/*
 353		 * PARF_DBI_BASE_ADDR register is in CPU domain and require to
 354		 * be programmed with CPU physical address.
 355		 */
 356		writel(lower_32_bits(pci->dbi_phys_addr), pcie->parf +
 357							PARF_DBI_BASE_ADDR);
 358		writel(SLV_ADDR_SPACE_SZ, pcie->parf +
 359						PARF_SLV_ADDR_SPACE_SIZE);
 360	}
 361}
 362
 363static void qcom_pcie_configure_dbi_atu_base(struct qcom_pcie *pcie)
 364{
 365	struct dw_pcie *pci = pcie->pci;
 366
 367	if (pci->dbi_phys_addr) {
 368		/*
 369		 * PARF_DBI_BASE_ADDR_V2 and PARF_ATU_BASE_ADDR registers are
 370		 * in CPU domain and require to be programmed with CPU
 371		 * physical addresses.
 372		 */
 373		writel(lower_32_bits(pci->dbi_phys_addr), pcie->parf +
 374							PARF_DBI_BASE_ADDR_V2);
 375		writel(upper_32_bits(pci->dbi_phys_addr), pcie->parf +
 376						PARF_DBI_BASE_ADDR_V2_HI);
 377
 378		if (pci->atu_phys_addr) {
 379			writel(lower_32_bits(pci->atu_phys_addr), pcie->parf +
 380							PARF_ATU_BASE_ADDR);
 381			writel(upper_32_bits(pci->atu_phys_addr), pcie->parf +
 382							PARF_ATU_BASE_ADDR_HI);
 383		}
 384
 385		writel(0x0, pcie->parf + PARF_SLV_ADDR_SPACE_SIZE_V2);
 386		writel(SLV_ADDR_SPACE_SZ, pcie->parf +
 387					PARF_SLV_ADDR_SPACE_SIZE_V2_HI);
 388	}
 389}
 390
 391static void qcom_pcie_2_1_0_ltssm_enable(struct qcom_pcie *pcie)
 392{
 393	u32 val;
 394
 395	/* enable link training */
 396	val = readl(pcie->elbi + ELBI_SYS_CTRL);
 397	val |= ELBI_SYS_CTRL_LT_ENABLE;
 398	writel(val, pcie->elbi + ELBI_SYS_CTRL);
 399}
 400
 401static int qcom_pcie_get_resources_2_1_0(struct qcom_pcie *pcie)
 402{
 403	struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
 404	struct dw_pcie *pci = pcie->pci;
 405	struct device *dev = pci->dev;
 406	bool is_apq = of_device_is_compatible(dev->of_node, "qcom,pcie-apq8064");
 407	int ret;
 408
 409	res->supplies[0].supply = "vdda";
 410	res->supplies[1].supply = "vdda_phy";
 411	res->supplies[2].supply = "vdda_refclk";
 412	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
 413				      res->supplies);
 414	if (ret)
 415		return ret;
 416
 417	res->num_clks = devm_clk_bulk_get_all(dev, &res->clks);
 418	if (res->num_clks < 0) {
 419		dev_err(dev, "Failed to get clocks\n");
 420		return res->num_clks;
 421	}
 
 
 
 
 
 
 
 
 
 
 422
 423	res->resets[0].id = "pci";
 424	res->resets[1].id = "axi";
 425	res->resets[2].id = "ahb";
 426	res->resets[3].id = "por";
 427	res->resets[4].id = "phy";
 428	res->resets[5].id = "ext";
 429
 430	/* ext is optional on APQ8016 */
 431	res->num_resets = is_apq ? 5 : 6;
 432	ret = devm_reset_control_bulk_get_exclusive(dev, res->num_resets, res->resets);
 433	if (ret < 0)
 434		return ret;
 435
 436	return 0;
 437}
 438
 439static void qcom_pcie_deinit_2_1_0(struct qcom_pcie *pcie)
 440{
 441	struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
 442
 443	clk_bulk_disable_unprepare(res->num_clks, res->clks);
 444	reset_control_bulk_assert(res->num_resets, res->resets);
 445
 446	writel(1, pcie->parf + PARF_PHY_CTRL);
 447
 448	regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
 449}
 450
 451static int qcom_pcie_init_2_1_0(struct qcom_pcie *pcie)
 452{
 453	struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
 454	struct dw_pcie *pci = pcie->pci;
 455	struct device *dev = pci->dev;
 456	int ret;
 457
 458	/* reset the PCIe interface as uboot can leave it undefined state */
 459	ret = reset_control_bulk_assert(res->num_resets, res->resets);
 460	if (ret < 0) {
 461		dev_err(dev, "cannot assert resets\n");
 462		return ret;
 463	}
 464
 465	ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
 466	if (ret < 0) {
 467		dev_err(dev, "cannot enable regulators\n");
 468		return ret;
 469	}
 470
 471	ret = reset_control_bulk_deassert(res->num_resets, res->resets);
 472	if (ret < 0) {
 473		dev_err(dev, "cannot deassert resets\n");
 474		regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
 475		return ret;
 476	}
 477
 478	return 0;
 479}
 480
 481static int qcom_pcie_post_init_2_1_0(struct qcom_pcie *pcie)
 482{
 483	struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
 484	struct dw_pcie *pci = pcie->pci;
 485	struct device *dev = pci->dev;
 486	struct device_node *node = dev->of_node;
 487	u32 val;
 488	int ret;
 489
 490	/* enable PCIe clocks and resets */
 491	val = readl(pcie->parf + PARF_PHY_CTRL);
 492	val &= ~PHY_TEST_PWR_DOWN;
 493	writel(val, pcie->parf + PARF_PHY_CTRL);
 494
 495	ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
 496	if (ret)
 497		return ret;
 498
 499	if (of_device_is_compatible(node, "qcom,pcie-ipq8064") ||
 500	    of_device_is_compatible(node, "qcom,pcie-ipq8064-v2")) {
 501		writel(PCS_DEEMPH_TX_DEEMPH_GEN1(24) |
 502			       PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(24) |
 503			       PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(34),
 504		       pcie->parf + PARF_PCS_DEEMPH);
 505		writel(PCS_SWING_TX_SWING_FULL(120) |
 506			       PCS_SWING_TX_SWING_LOW(120),
 507		       pcie->parf + PARF_PCS_SWING);
 508		writel(PHY_RX0_EQ(4), pcie->parf + PARF_CONFIG_BITS);
 509	}
 510
 511	if (of_device_is_compatible(node, "qcom,pcie-ipq8064")) {
 512		/* set TX termination offset */
 513		val = readl(pcie->parf + PARF_PHY_CTRL);
 514		val &= ~PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK;
 515		val |= PHY_CTRL_PHY_TX0_TERM_OFFSET(7);
 516		writel(val, pcie->parf + PARF_PHY_CTRL);
 517	}
 518
 519	/* enable external reference clock */
 520	val = readl(pcie->parf + PARF_PHY_REFCLK);
 521	/* USE_PAD is required only for ipq806x */
 522	if (!of_device_is_compatible(node, "qcom,pcie-apq8064"))
 523		val &= ~PHY_REFCLK_USE_PAD;
 524	val |= PHY_REFCLK_SSP_EN;
 525	writel(val, pcie->parf + PARF_PHY_REFCLK);
 526
 527	/* wait for clock acquisition */
 528	usleep_range(1000, 1500);
 529
 530	/* Set the Max TLP size to 2K, instead of using default of 4K */
 531	writel(CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K,
 532	       pci->dbi_base + AXI_MSTR_RESP_COMP_CTRL0);
 533	writel(CFG_BRIDGE_SB_INIT,
 534	       pci->dbi_base + AXI_MSTR_RESP_COMP_CTRL1);
 535
 536	qcom_pcie_clear_hpc(pcie->pci);
 537
 538	return 0;
 539}
 540
 541static int qcom_pcie_get_resources_1_0_0(struct qcom_pcie *pcie)
 542{
 543	struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
 544	struct dw_pcie *pci = pcie->pci;
 545	struct device *dev = pci->dev;
 
 546
 547	res->vdda = devm_regulator_get(dev, "vdda");
 548	if (IS_ERR(res->vdda))
 549		return PTR_ERR(res->vdda);
 550
 551	res->num_clks = devm_clk_bulk_get_all(dev, &res->clks);
 552	if (res->num_clks < 0) {
 553		dev_err(dev, "Failed to get clocks\n");
 554		return res->num_clks;
 555	}
 
 
 
 556
 557	res->core = devm_reset_control_get_exclusive(dev, "core");
 558	return PTR_ERR_OR_ZERO(res->core);
 559}
 560
 561static void qcom_pcie_deinit_1_0_0(struct qcom_pcie *pcie)
 562{
 563	struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
 564
 565	reset_control_assert(res->core);
 566	clk_bulk_disable_unprepare(res->num_clks, res->clks);
 567	regulator_disable(res->vdda);
 568}
 569
 570static int qcom_pcie_init_1_0_0(struct qcom_pcie *pcie)
 571{
 572	struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
 573	struct dw_pcie *pci = pcie->pci;
 574	struct device *dev = pci->dev;
 575	int ret;
 576
 577	ret = reset_control_deassert(res->core);
 578	if (ret) {
 579		dev_err(dev, "cannot deassert core reset\n");
 580		return ret;
 581	}
 582
 583	ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
 584	if (ret) {
 585		dev_err(dev, "cannot prepare/enable clocks\n");
 586		goto err_assert_reset;
 587	}
 588
 589	ret = regulator_enable(res->vdda);
 590	if (ret) {
 591		dev_err(dev, "cannot enable vdda regulator\n");
 592		goto err_disable_clks;
 593	}
 594
 595	return 0;
 596
 597err_disable_clks:
 598	clk_bulk_disable_unprepare(res->num_clks, res->clks);
 599err_assert_reset:
 600	reset_control_assert(res->core);
 601
 602	return ret;
 603}
 604
 605static int qcom_pcie_post_init_1_0_0(struct qcom_pcie *pcie)
 606{
 607	qcom_pcie_configure_dbi_base(pcie);
 
 608
 609	if (IS_ENABLED(CONFIG_PCI_MSI)) {
 610		u32 val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT);
 611
 612		val |= EN;
 613		writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT);
 614	}
 615
 616	qcom_pcie_clear_hpc(pcie->pci);
 617
 618	return 0;
 619}
 620
 621static void qcom_pcie_2_3_2_ltssm_enable(struct qcom_pcie *pcie)
 622{
 623	u32 val;
 624
 625	/* enable link training */
 626	val = readl(pcie->parf + PARF_LTSSM);
 627	val |= LTSSM_EN;
 628	writel(val, pcie->parf + PARF_LTSSM);
 629}
 630
 631static int qcom_pcie_get_resources_2_3_2(struct qcom_pcie *pcie)
 632{
 633	struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
 634	struct dw_pcie *pci = pcie->pci;
 635	struct device *dev = pci->dev;
 636	int ret;
 637
 638	res->supplies[0].supply = "vdda";
 639	res->supplies[1].supply = "vddpe-3v3";
 640	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
 641				      res->supplies);
 642	if (ret)
 643		return ret;
 644
 645	res->num_clks = devm_clk_bulk_get_all(dev, &res->clks);
 646	if (res->num_clks < 0) {
 647		dev_err(dev, "Failed to get clocks\n");
 648		return res->num_clks;
 649	}
 
 
 
 650
 651	return 0;
 652}
 653
 654static void qcom_pcie_deinit_2_3_2(struct qcom_pcie *pcie)
 655{
 656	struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
 657
 658	clk_bulk_disable_unprepare(res->num_clks, res->clks);
 659	regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
 660}
 661
 662static int qcom_pcie_init_2_3_2(struct qcom_pcie *pcie)
 663{
 664	struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
 665	struct dw_pcie *pci = pcie->pci;
 666	struct device *dev = pci->dev;
 667	int ret;
 668
 669	ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
 670	if (ret < 0) {
 671		dev_err(dev, "cannot enable regulators\n");
 672		return ret;
 673	}
 674
 675	ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
 676	if (ret) {
 677		dev_err(dev, "cannot prepare/enable clocks\n");
 678		regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
 679		return ret;
 680	}
 681
 682	return 0;
 683}
 684
 685static int qcom_pcie_post_init_2_3_2(struct qcom_pcie *pcie)
 686{
 687	u32 val;
 688
 689	/* enable PCIe clocks and resets */
 690	val = readl(pcie->parf + PARF_PHY_CTRL);
 691	val &= ~PHY_TEST_PWR_DOWN;
 692	writel(val, pcie->parf + PARF_PHY_CTRL);
 693
 694	qcom_pcie_configure_dbi_base(pcie);
 
 695
 696	/* MAC PHY_POWERDOWN MUX DISABLE  */
 697	val = readl(pcie->parf + PARF_SYS_CTRL);
 698	val &= ~MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN;
 699	writel(val, pcie->parf + PARF_SYS_CTRL);
 700
 701	val = readl(pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
 702	val |= BYPASS;
 703	writel(val, pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
 704
 705	val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
 706	val |= EN;
 707	writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
 708
 709	qcom_pcie_clear_hpc(pcie->pci);
 710
 711	return 0;
 712}
 713
 714static int qcom_pcie_get_resources_2_4_0(struct qcom_pcie *pcie)
 715{
 716	struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
 717	struct dw_pcie *pci = pcie->pci;
 718	struct device *dev = pci->dev;
 719	bool is_ipq = of_device_is_compatible(dev->of_node, "qcom,pcie-ipq4019");
 720	int ret;
 721
 722	res->num_clks = devm_clk_bulk_get_all(dev, &res->clks);
 723	if (res->num_clks < 0) {
 724		dev_err(dev, "Failed to get clocks\n");
 725		return res->num_clks;
 726	}
 
 
 
 
 
 
 727
 728	res->resets[0].id = "axi_m";
 729	res->resets[1].id = "axi_s";
 730	res->resets[2].id = "axi_m_sticky";
 731	res->resets[3].id = "pipe_sticky";
 732	res->resets[4].id = "pwr";
 733	res->resets[5].id = "ahb";
 734	res->resets[6].id = "pipe";
 735	res->resets[7].id = "axi_m_vmid";
 736	res->resets[8].id = "axi_s_xpu";
 737	res->resets[9].id = "parf";
 738	res->resets[10].id = "phy";
 739	res->resets[11].id = "phy_ahb";
 740
 741	res->num_resets = is_ipq ? 12 : 6;
 742
 743	ret = devm_reset_control_bulk_get_exclusive(dev, res->num_resets, res->resets);
 744	if (ret < 0)
 745		return ret;
 746
 747	return 0;
 748}
 749
 750static void qcom_pcie_deinit_2_4_0(struct qcom_pcie *pcie)
 751{
 752	struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
 753
 754	reset_control_bulk_assert(res->num_resets, res->resets);
 755	clk_bulk_disable_unprepare(res->num_clks, res->clks);
 756}
 757
 758static int qcom_pcie_init_2_4_0(struct qcom_pcie *pcie)
 759{
 760	struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
 761	struct dw_pcie *pci = pcie->pci;
 762	struct device *dev = pci->dev;
 763	int ret;
 764
 765	ret = reset_control_bulk_assert(res->num_resets, res->resets);
 766	if (ret < 0) {
 767		dev_err(dev, "cannot assert resets\n");
 768		return ret;
 769	}
 770
 771	usleep_range(10000, 12000);
 772
 773	ret = reset_control_bulk_deassert(res->num_resets, res->resets);
 774	if (ret < 0) {
 775		dev_err(dev, "cannot deassert resets\n");
 776		return ret;
 777	}
 778
 779	usleep_range(10000, 12000);
 780
 781	ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
 782	if (ret) {
 783		reset_control_bulk_assert(res->num_resets, res->resets);
 784		return ret;
 785	}
 786
 787	return 0;
 788}
 789
 790static int qcom_pcie_get_resources_2_3_3(struct qcom_pcie *pcie)
 791{
 792	struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
 793	struct dw_pcie *pci = pcie->pci;
 794	struct device *dev = pci->dev;
 795	int ret;
 796
 797	res->num_clks = devm_clk_bulk_get_all(dev, &res->clks);
 798	if (res->num_clks < 0) {
 799		dev_err(dev, "Failed to get clocks\n");
 800		return res->num_clks;
 801	}
 
 
 
 
 802
 803	res->rst[0].id = "axi_m";
 804	res->rst[1].id = "axi_s";
 805	res->rst[2].id = "pipe";
 806	res->rst[3].id = "axi_m_sticky";
 807	res->rst[4].id = "sticky";
 808	res->rst[5].id = "ahb";
 809	res->rst[6].id = "sleep";
 810
 811	ret = devm_reset_control_bulk_get_exclusive(dev, ARRAY_SIZE(res->rst), res->rst);
 812	if (ret < 0)
 813		return ret;
 814
 815	return 0;
 816}
 817
 818static void qcom_pcie_deinit_2_3_3(struct qcom_pcie *pcie)
 819{
 820	struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
 821
 822	clk_bulk_disable_unprepare(res->num_clks, res->clks);
 823}
 824
 825static int qcom_pcie_init_2_3_3(struct qcom_pcie *pcie)
 826{
 827	struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
 828	struct dw_pcie *pci = pcie->pci;
 829	struct device *dev = pci->dev;
 830	int ret;
 831
 832	ret = reset_control_bulk_assert(ARRAY_SIZE(res->rst), res->rst);
 833	if (ret < 0) {
 834		dev_err(dev, "cannot assert resets\n");
 835		return ret;
 836	}
 837
 838	usleep_range(2000, 2500);
 839
 840	ret = reset_control_bulk_deassert(ARRAY_SIZE(res->rst), res->rst);
 841	if (ret < 0) {
 842		dev_err(dev, "cannot deassert resets\n");
 843		return ret;
 844	}
 845
 846	/*
 847	 * Don't have a way to see if the reset has completed.
 848	 * Wait for some time.
 849	 */
 850	usleep_range(2000, 2500);
 851
 852	ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
 853	if (ret) {
 854		dev_err(dev, "cannot prepare/enable clocks\n");
 855		goto err_assert_resets;
 856	}
 857
 858	return 0;
 859
 860err_assert_resets:
 861	/*
 862	 * Not checking for failure, will anyway return
 863	 * the original failure in 'ret'.
 864	 */
 865	reset_control_bulk_assert(ARRAY_SIZE(res->rst), res->rst);
 866
 867	return ret;
 868}
 869
 870static int qcom_pcie_post_init_2_3_3(struct qcom_pcie *pcie)
 871{
 872	struct dw_pcie *pci = pcie->pci;
 873	u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
 874	u32 val;
 875
 
 
 876	val = readl(pcie->parf + PARF_PHY_CTRL);
 877	val &= ~PHY_TEST_PWR_DOWN;
 878	writel(val, pcie->parf + PARF_PHY_CTRL);
 879
 880	qcom_pcie_configure_dbi_atu_base(pcie);
 881
 882	writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS
 883		| SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS |
 884		AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS,
 885		pcie->parf + PARF_SYS_CTRL);
 886	writel(0, pcie->parf + PARF_Q2A_FLUSH);
 887
 888	writel(PCI_COMMAND_MASTER, pci->dbi_base + PCI_COMMAND);
 889
 890	dw_pcie_dbi_ro_wr_en(pci);
 891
 892	writel(PCIE_CAP_SLOT_VAL, pci->dbi_base + offset + PCI_EXP_SLTCAP);
 893
 894	val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP);
 895	val &= ~PCI_EXP_LNKCAP_ASPMS;
 896	writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP);
 897
 898	writel(PCI_EXP_DEVCTL2_COMP_TMOUT_DIS, pci->dbi_base + offset +
 899		PCI_EXP_DEVCTL2);
 900
 901	dw_pcie_dbi_ro_wr_dis(pci);
 902
 903	return 0;
 904}
 905
 906static int qcom_pcie_get_resources_2_7_0(struct qcom_pcie *pcie)
 907{
 908	struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
 909	struct dw_pcie *pci = pcie->pci;
 910	struct device *dev = pci->dev;
 
 
 911	int ret;
 912
 913	res->rst = devm_reset_control_array_get_exclusive(dev);
 914	if (IS_ERR(res->rst))
 915		return PTR_ERR(res->rst);
 916
 917	res->supplies[0].supply = "vdda";
 918	res->supplies[1].supply = "vddpe-3v3";
 919	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
 920				      res->supplies);
 921	if (ret)
 922		return ret;
 923
 924	res->num_clks = devm_clk_bulk_get_all(dev, &res->clks);
 925	if (res->num_clks < 0) {
 926		dev_err(dev, "Failed to get clocks\n");
 927		return res->num_clks;
 928	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 929
 930	return 0;
 931}
 932
 933static int qcom_pcie_init_2_7_0(struct qcom_pcie *pcie)
 934{
 935	struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
 936	struct dw_pcie *pci = pcie->pci;
 937	struct device *dev = pci->dev;
 938	u32 val;
 939	int ret;
 940
 941	ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
 942	if (ret < 0) {
 943		dev_err(dev, "cannot enable regulators\n");
 944		return ret;
 945	}
 946
 947	ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
 948	if (ret < 0)
 949		goto err_disable_regulators;
 950
 951	ret = reset_control_assert(res->rst);
 952	if (ret) {
 953		dev_err(dev, "reset assert failed (%d)\n", ret);
 954		goto err_disable_clocks;
 955	}
 956
 957	usleep_range(1000, 1500);
 958
 959	ret = reset_control_deassert(res->rst);
 960	if (ret) {
 961		dev_err(dev, "reset deassert failed (%d)\n", ret);
 962		goto err_disable_clocks;
 963	}
 964
 965	/* Wait for reset to complete, required on SM8450 */
 966	usleep_range(1000, 1500);
 967
 968	/* configure PCIe to RC mode */
 969	writel(DEVICE_TYPE_RC, pcie->parf + PARF_DEVICE_TYPE);
 970
 971	/* enable PCIe clocks and resets */
 972	val = readl(pcie->parf + PARF_PHY_CTRL);
 973	val &= ~PHY_TEST_PWR_DOWN;
 974	writel(val, pcie->parf + PARF_PHY_CTRL);
 975
 976	qcom_pcie_configure_dbi_atu_base(pcie);
 
 977
 978	/* MAC PHY_POWERDOWN MUX DISABLE  */
 979	val = readl(pcie->parf + PARF_SYS_CTRL);
 980	val &= ~MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN;
 981	writel(val, pcie->parf + PARF_SYS_CTRL);
 982
 983	val = readl(pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
 984	val |= BYPASS;
 985	writel(val, pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
 986
 987	/* Enable L1 and L1SS */
 988	val = readl(pcie->parf + PARF_PM_CTRL);
 989	val &= ~REQ_NOT_ENTR_L1;
 990	writel(val, pcie->parf + PARF_PM_CTRL);
 991
 992	val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
 993	val |= EN;
 994	writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
 995
 996	return 0;
 997err_disable_clocks:
 998	clk_bulk_disable_unprepare(res->num_clks, res->clks);
 999err_disable_regulators:
1000	regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
1001
1002	return ret;
1003}
1004
1005static int qcom_pcie_post_init_2_7_0(struct qcom_pcie *pcie)
1006{
1007	const struct qcom_pcie_cfg *pcie_cfg = pcie->cfg;
1008
1009	if (pcie_cfg->override_no_snoop)
1010		writel(WR_NO_SNOOP_OVERIDE_EN | RD_NO_SNOOP_OVERIDE_EN,
1011				pcie->parf + PARF_NO_SNOOP_OVERIDE);
1012
1013	qcom_pcie_clear_aspm_l0s(pcie->pci);
1014	qcom_pcie_clear_hpc(pcie->pci);
1015
1016	return 0;
1017}
1018
1019static int qcom_pcie_enable_aspm(struct pci_dev *pdev, void *userdata)
1020{
1021	/*
1022	 * Downstream devices need to be in D0 state before enabling PCI PM
1023	 * substates.
1024	 */
1025	pci_set_power_state_locked(pdev, PCI_D0);
1026	pci_enable_link_state_locked(pdev, PCIE_LINK_STATE_ALL);
1027
1028	return 0;
1029}
1030
1031static void qcom_pcie_host_post_init_2_7_0(struct qcom_pcie *pcie)
1032{
1033	struct dw_pcie_rp *pp = &pcie->pci->pp;
1034
1035	pci_walk_bus(pp->bridge->bus, qcom_pcie_enable_aspm, NULL);
1036}
1037
1038static void qcom_pcie_deinit_2_7_0(struct qcom_pcie *pcie)
1039{
1040	struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
1041
1042	clk_bulk_disable_unprepare(res->num_clks, res->clks);
1043
1044	regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
1045}
1046
1047static int qcom_pcie_config_sid_1_9_0(struct qcom_pcie *pcie)
1048{
1049	/* iommu map structure */
1050	struct {
1051		u32 bdf;
1052		u32 phandle;
1053		u32 smmu_sid;
1054		u32 smmu_sid_len;
1055	} *map;
1056	void __iomem *bdf_to_sid_base = pcie->parf + PARF_BDF_TO_SID_TABLE_N;
1057	struct device *dev = pcie->pci->dev;
1058	u8 qcom_pcie_crc8_table[CRC8_TABLE_SIZE];
1059	int i, nr_map, size = 0;
1060	u32 smmu_sid_base;
1061	u32 val;
1062
1063	of_get_property(dev->of_node, "iommu-map", &size);
1064	if (!size)
1065		return 0;
1066
1067	/* Enable BDF to SID translation by disabling bypass mode (default) */
1068	val = readl(pcie->parf + PARF_BDF_TO_SID_CFG);
1069	val &= ~BDF_TO_SID_BYPASS;
1070	writel(val, pcie->parf + PARF_BDF_TO_SID_CFG);
1071
1072	map = kzalloc(size, GFP_KERNEL);
1073	if (!map)
1074		return -ENOMEM;
1075
1076	of_property_read_u32_array(dev->of_node, "iommu-map", (u32 *)map,
1077				   size / sizeof(u32));
1078
1079	nr_map = size / (sizeof(*map));
1080
1081	crc8_populate_msb(qcom_pcie_crc8_table, QCOM_PCIE_CRC8_POLYNOMIAL);
1082
1083	/* Registers need to be zero out first */
1084	memset_io(bdf_to_sid_base, 0, CRC8_TABLE_SIZE * sizeof(u32));
1085
1086	/* Extract the SMMU SID base from the first entry of iommu-map */
1087	smmu_sid_base = map[0].smmu_sid;
1088
1089	/* Look for an available entry to hold the mapping */
1090	for (i = 0; i < nr_map; i++) {
1091		__be16 bdf_be = cpu_to_be16(map[i].bdf);
1092		u32 val;
1093		u8 hash;
1094
1095		hash = crc8(qcom_pcie_crc8_table, (u8 *)&bdf_be, sizeof(bdf_be), 0);
1096
1097		val = readl(bdf_to_sid_base + hash * sizeof(u32));
1098
1099		/* If the register is already populated, look for next available entry */
1100		while (val) {
1101			u8 current_hash = hash++;
1102			u8 next_mask = 0xff;
1103
1104			/* If NEXT field is NULL then update it with next hash */
1105			if (!(val & next_mask)) {
1106				val |= (u32)hash;
1107				writel(val, bdf_to_sid_base + current_hash * sizeof(u32));
1108			}
1109
1110			val = readl(bdf_to_sid_base + hash * sizeof(u32));
1111		}
1112
1113		/* BDF [31:16] | SID [15:8] | NEXT [7:0] */
1114		val = map[i].bdf << 16 | (map[i].smmu_sid - smmu_sid_base) << 8 | 0;
1115		writel(val, bdf_to_sid_base + hash * sizeof(u32));
1116	}
1117
1118	kfree(map);
1119
1120	return 0;
1121}
1122
1123static int qcom_pcie_get_resources_2_9_0(struct qcom_pcie *pcie)
1124{
1125	struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1126	struct dw_pcie *pci = pcie->pci;
1127	struct device *dev = pci->dev;
 
1128
1129	res->num_clks = devm_clk_bulk_get_all(dev, &res->clks);
1130	if (res->num_clks < 0) {
1131		dev_err(dev, "Failed to get clocks\n");
1132		return res->num_clks;
1133	}
 
 
 
 
1134
1135	res->rst = devm_reset_control_array_get_exclusive(dev);
1136	if (IS_ERR(res->rst))
1137		return PTR_ERR(res->rst);
1138
1139	return 0;
1140}
1141
1142static void qcom_pcie_deinit_2_9_0(struct qcom_pcie *pcie)
1143{
1144	struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1145
1146	clk_bulk_disable_unprepare(res->num_clks, res->clks);
1147}
1148
1149static int qcom_pcie_init_2_9_0(struct qcom_pcie *pcie)
1150{
1151	struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1152	struct device *dev = pcie->pci->dev;
1153	int ret;
1154
1155	ret = reset_control_assert(res->rst);
1156	if (ret) {
1157		dev_err(dev, "reset assert failed (%d)\n", ret);
1158		return ret;
1159	}
1160
1161	/*
1162	 * Delay periods before and after reset deassert are working values
1163	 * from downstream Codeaurora kernel
1164	 */
1165	usleep_range(2000, 2500);
1166
1167	ret = reset_control_deassert(res->rst);
1168	if (ret) {
1169		dev_err(dev, "reset deassert failed (%d)\n", ret);
1170		return ret;
1171	}
1172
1173	usleep_range(2000, 2500);
1174
1175	return clk_bulk_prepare_enable(res->num_clks, res->clks);
1176}
1177
1178static int qcom_pcie_post_init_2_9_0(struct qcom_pcie *pcie)
1179{
1180	struct dw_pcie *pci = pcie->pci;
1181	u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1182	u32 val;
1183	int i;
1184
 
 
 
1185	val = readl(pcie->parf + PARF_PHY_CTRL);
1186	val &= ~PHY_TEST_PWR_DOWN;
1187	writel(val, pcie->parf + PARF_PHY_CTRL);
1188
1189	qcom_pcie_configure_dbi_atu_base(pcie);
1190
1191	writel(DEVICE_TYPE_RC, pcie->parf + PARF_DEVICE_TYPE);
1192	writel(BYPASS | MSTR_AXI_CLK_EN | AHB_CLK_EN,
1193		pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
1194	writel(GEN3_RELATED_OFF_RXEQ_RGRDLESS_RXTS |
1195		GEN3_RELATED_OFF_GEN3_ZRXDC_NONCOMPL,
1196		pci->dbi_base + GEN3_RELATED_OFF);
1197
1198	writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS |
1199		SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS |
1200		AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS,
1201		pcie->parf + PARF_SYS_CTRL);
1202
1203	writel(0, pcie->parf + PARF_Q2A_FLUSH);
1204
1205	dw_pcie_dbi_ro_wr_en(pci);
1206
1207	writel(PCIE_CAP_SLOT_VAL, pci->dbi_base + offset + PCI_EXP_SLTCAP);
1208
1209	val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP);
1210	val &= ~PCI_EXP_LNKCAP_ASPMS;
1211	writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP);
1212
1213	writel(PCI_EXP_DEVCTL2_COMP_TMOUT_DIS, pci->dbi_base + offset +
1214			PCI_EXP_DEVCTL2);
1215
1216	dw_pcie_dbi_ro_wr_dis(pci);
1217
1218	for (i = 0; i < 256; i++)
1219		writel(0, pcie->parf + PARF_BDF_TO_SID_TABLE_N + (4 * i));
1220
1221	return 0;
1222}
1223
1224static int qcom_pcie_link_up(struct dw_pcie *pci)
1225{
1226	u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1227	u16 val = readw(pci->dbi_base + offset + PCI_EXP_LNKSTA);
1228
1229	return !!(val & PCI_EXP_LNKSTA_DLLLA);
1230}
1231
1232static int qcom_pcie_host_init(struct dw_pcie_rp *pp)
1233{
1234	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1235	struct qcom_pcie *pcie = to_qcom_pcie(pci);
1236	int ret;
1237
1238	qcom_ep_reset_assert(pcie);
1239
1240	ret = pcie->cfg->ops->init(pcie);
1241	if (ret)
1242		return ret;
1243
1244	ret = phy_set_mode_ext(pcie->phy, PHY_MODE_PCIE, PHY_MODE_PCIE_RC);
1245	if (ret)
1246		goto err_deinit;
1247
1248	ret = phy_power_on(pcie->phy);
1249	if (ret)
1250		goto err_deinit;
1251
1252	if (pcie->cfg->ops->post_init) {
1253		ret = pcie->cfg->ops->post_init(pcie);
1254		if (ret)
1255			goto err_disable_phy;
1256	}
1257
1258	qcom_ep_reset_deassert(pcie);
1259
1260	if (pcie->cfg->ops->config_sid) {
1261		ret = pcie->cfg->ops->config_sid(pcie);
1262		if (ret)
1263			goto err_assert_reset;
1264	}
1265
1266	return 0;
1267
1268err_assert_reset:
1269	qcom_ep_reset_assert(pcie);
1270err_disable_phy:
1271	phy_power_off(pcie->phy);
1272err_deinit:
1273	pcie->cfg->ops->deinit(pcie);
1274
1275	return ret;
1276}
1277
1278static void qcom_pcie_host_deinit(struct dw_pcie_rp *pp)
1279{
1280	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1281	struct qcom_pcie *pcie = to_qcom_pcie(pci);
1282
1283	qcom_ep_reset_assert(pcie);
1284	phy_power_off(pcie->phy);
1285	pcie->cfg->ops->deinit(pcie);
1286}
1287
1288static void qcom_pcie_host_post_init(struct dw_pcie_rp *pp)
1289{
1290	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1291	struct qcom_pcie *pcie = to_qcom_pcie(pci);
1292
1293	if (pcie->cfg->ops->host_post_init)
1294		pcie->cfg->ops->host_post_init(pcie);
1295}
1296
1297static const struct dw_pcie_host_ops qcom_pcie_dw_ops = {
1298	.init		= qcom_pcie_host_init,
1299	.deinit		= qcom_pcie_host_deinit,
1300	.post_init	= qcom_pcie_host_post_init,
1301};
1302
1303/* Qcom IP rev.: 2.1.0	Synopsys IP rev.: 4.01a */
1304static const struct qcom_pcie_ops ops_2_1_0 = {
1305	.get_resources = qcom_pcie_get_resources_2_1_0,
1306	.init = qcom_pcie_init_2_1_0,
1307	.post_init = qcom_pcie_post_init_2_1_0,
1308	.deinit = qcom_pcie_deinit_2_1_0,
1309	.ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
1310};
1311
1312/* Qcom IP rev.: 1.0.0	Synopsys IP rev.: 4.11a */
1313static const struct qcom_pcie_ops ops_1_0_0 = {
1314	.get_resources = qcom_pcie_get_resources_1_0_0,
1315	.init = qcom_pcie_init_1_0_0,
1316	.post_init = qcom_pcie_post_init_1_0_0,
1317	.deinit = qcom_pcie_deinit_1_0_0,
1318	.ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
1319};
1320
1321/* Qcom IP rev.: 2.3.2	Synopsys IP rev.: 4.21a */
1322static const struct qcom_pcie_ops ops_2_3_2 = {
1323	.get_resources = qcom_pcie_get_resources_2_3_2,
1324	.init = qcom_pcie_init_2_3_2,
1325	.post_init = qcom_pcie_post_init_2_3_2,
1326	.deinit = qcom_pcie_deinit_2_3_2,
1327	.ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1328};
1329
1330/* Qcom IP rev.: 2.4.0	Synopsys IP rev.: 4.20a */
1331static const struct qcom_pcie_ops ops_2_4_0 = {
1332	.get_resources = qcom_pcie_get_resources_2_4_0,
1333	.init = qcom_pcie_init_2_4_0,
1334	.post_init = qcom_pcie_post_init_2_3_2,
1335	.deinit = qcom_pcie_deinit_2_4_0,
1336	.ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1337};
1338
1339/* Qcom IP rev.: 2.3.3	Synopsys IP rev.: 4.30a */
1340static const struct qcom_pcie_ops ops_2_3_3 = {
1341	.get_resources = qcom_pcie_get_resources_2_3_3,
1342	.init = qcom_pcie_init_2_3_3,
1343	.post_init = qcom_pcie_post_init_2_3_3,
1344	.deinit = qcom_pcie_deinit_2_3_3,
1345	.ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1346};
1347
1348/* Qcom IP rev.: 2.7.0	Synopsys IP rev.: 4.30a */
1349static const struct qcom_pcie_ops ops_2_7_0 = {
1350	.get_resources = qcom_pcie_get_resources_2_7_0,
1351	.init = qcom_pcie_init_2_7_0,
1352	.post_init = qcom_pcie_post_init_2_7_0,
1353	.deinit = qcom_pcie_deinit_2_7_0,
1354	.ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1355};
1356
1357/* Qcom IP rev.: 1.9.0 */
1358static const struct qcom_pcie_ops ops_1_9_0 = {
1359	.get_resources = qcom_pcie_get_resources_2_7_0,
1360	.init = qcom_pcie_init_2_7_0,
1361	.post_init = qcom_pcie_post_init_2_7_0,
1362	.host_post_init = qcom_pcie_host_post_init_2_7_0,
1363	.deinit = qcom_pcie_deinit_2_7_0,
1364	.ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1365	.config_sid = qcom_pcie_config_sid_1_9_0,
1366};
1367
1368/* Qcom IP rev.: 1.21.0  Synopsys IP rev.: 5.60a */
1369static const struct qcom_pcie_ops ops_1_21_0 = {
1370	.get_resources = qcom_pcie_get_resources_2_7_0,
1371	.init = qcom_pcie_init_2_7_0,
1372	.post_init = qcom_pcie_post_init_2_7_0,
1373	.host_post_init = qcom_pcie_host_post_init_2_7_0,
1374	.deinit = qcom_pcie_deinit_2_7_0,
1375	.ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1376};
1377
1378/* Qcom IP rev.: 2.9.0  Synopsys IP rev.: 5.00a */
1379static const struct qcom_pcie_ops ops_2_9_0 = {
1380	.get_resources = qcom_pcie_get_resources_2_9_0,
1381	.init = qcom_pcie_init_2_9_0,
1382	.post_init = qcom_pcie_post_init_2_9_0,
1383	.deinit = qcom_pcie_deinit_2_9_0,
1384	.ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1385};
1386
1387static const struct qcom_pcie_cfg cfg_1_0_0 = {
1388	.ops = &ops_1_0_0,
1389};
1390
1391static const struct qcom_pcie_cfg cfg_1_9_0 = {
1392	.ops = &ops_1_9_0,
1393};
1394
1395static const struct qcom_pcie_cfg cfg_1_34_0 = {
1396	.ops = &ops_1_9_0,
1397	.override_no_snoop = true,
1398};
1399
1400static const struct qcom_pcie_cfg cfg_2_1_0 = {
1401	.ops = &ops_2_1_0,
1402};
1403
1404static const struct qcom_pcie_cfg cfg_2_3_2 = {
1405	.ops = &ops_2_3_2,
1406};
1407
1408static const struct qcom_pcie_cfg cfg_2_3_3 = {
1409	.ops = &ops_2_3_3,
1410};
1411
1412static const struct qcom_pcie_cfg cfg_2_4_0 = {
1413	.ops = &ops_2_4_0,
1414};
1415
1416static const struct qcom_pcie_cfg cfg_2_7_0 = {
1417	.ops = &ops_2_7_0,
1418};
1419
1420static const struct qcom_pcie_cfg cfg_2_9_0 = {
1421	.ops = &ops_2_9_0,
1422};
1423
1424static const struct qcom_pcie_cfg cfg_sc8280xp = {
1425	.ops = &ops_1_21_0,
1426	.no_l0s = true,
1427};
1428
1429static const struct dw_pcie_ops dw_pcie_ops = {
1430	.link_up = qcom_pcie_link_up,
1431	.start_link = qcom_pcie_start_link,
1432};
1433
1434static int qcom_pcie_icc_init(struct qcom_pcie *pcie)
1435{
1436	struct dw_pcie *pci = pcie->pci;
1437	int ret;
1438
1439	pcie->icc_mem = devm_of_icc_get(pci->dev, "pcie-mem");
1440	if (IS_ERR(pcie->icc_mem))
1441		return PTR_ERR(pcie->icc_mem);
1442
1443	pcie->icc_cpu = devm_of_icc_get(pci->dev, "cpu-pcie");
1444	if (IS_ERR(pcie->icc_cpu))
1445		return PTR_ERR(pcie->icc_cpu);
1446	/*
1447	 * Some Qualcomm platforms require interconnect bandwidth constraints
1448	 * to be set before enabling interconnect clocks.
1449	 *
1450	 * Set an initial peak bandwidth corresponding to single-lane Gen 1
1451	 * for the pcie-mem path.
1452	 */
1453	ret = icc_set_bw(pcie->icc_mem, 0, QCOM_PCIE_LINK_SPEED_TO_BW(1));
1454	if (ret) {
1455		dev_err(pci->dev, "Failed to set bandwidth for PCIe-MEM interconnect path: %d\n",
1456			ret);
1457		return ret;
1458	}
1459
1460	/*
1461	 * Since the CPU-PCIe path is only used for activities like register
1462	 * access of the host controller and endpoint Config/BAR space access,
1463	 * HW team has recommended to use a minimal bandwidth of 1KBps just to
1464	 * keep the path active.
1465	 */
1466	ret = icc_set_bw(pcie->icc_cpu, 0, kBps_to_icc(1));
1467	if (ret) {
1468		dev_err(pci->dev, "Failed to set bandwidth for CPU-PCIe interconnect path: %d\n",
1469			ret);
1470		icc_set_bw(pcie->icc_mem, 0, 0);
1471		return ret;
1472	}
1473
1474	return 0;
1475}
1476
1477static void qcom_pcie_icc_opp_update(struct qcom_pcie *pcie)
1478{
1479	u32 offset, status, width, speed;
1480	struct dw_pcie *pci = pcie->pci;
1481	unsigned long freq_kbps;
1482	struct dev_pm_opp *opp;
1483	int ret, freq_mbps;
 
 
 
1484
1485	offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1486	status = readw(pci->dbi_base + offset + PCI_EXP_LNKSTA);
1487
1488	/* Only update constraints if link is up. */
1489	if (!(status & PCI_EXP_LNKSTA_DLLLA))
1490		return;
1491
1492	speed = FIELD_GET(PCI_EXP_LNKSTA_CLS, status);
1493	width = FIELD_GET(PCI_EXP_LNKSTA_NLW, status);
1494
1495	if (pcie->icc_mem) {
1496		ret = icc_set_bw(pcie->icc_mem, 0,
1497				 width * QCOM_PCIE_LINK_SPEED_TO_BW(speed));
1498		if (ret) {
1499			dev_err(pci->dev, "Failed to set bandwidth for PCIe-MEM interconnect path: %d\n",
1500				ret);
1501		}
1502	} else if (pcie->use_pm_opp) {
1503		freq_mbps = pcie_dev_speed_mbps(pcie_link_speed[speed]);
1504		if (freq_mbps < 0)
1505			return;
1506
1507		freq_kbps = freq_mbps * KILO;
1508		opp = dev_pm_opp_find_freq_exact(pci->dev, freq_kbps * width,
1509						 true);
1510		if (!IS_ERR(opp)) {
1511			ret = dev_pm_opp_set_opp(pci->dev, opp);
1512			if (ret)
1513				dev_err(pci->dev, "Failed to set OPP for freq (%lu): %d\n",
1514					freq_kbps * width, ret);
1515			dev_pm_opp_put(opp);
1516		}
1517	}
1518}
1519
1520static int qcom_pcie_link_transition_count(struct seq_file *s, void *data)
1521{
1522	struct qcom_pcie *pcie = (struct qcom_pcie *)dev_get_drvdata(s->private);
1523
1524	seq_printf(s, "L0s transition count: %u\n",
1525		   readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_PM_LINKST_IN_L0S));
1526
1527	seq_printf(s, "L1 transition count: %u\n",
1528		   readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_PM_LINKST_IN_L1));
1529
1530	seq_printf(s, "L1.1 transition count: %u\n",
1531		   readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L1));
1532
1533	seq_printf(s, "L1.2 transition count: %u\n",
1534		   readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2));
1535
1536	seq_printf(s, "L2 transition count: %u\n",
1537		   readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_PM_LINKST_IN_L2));
1538
1539	return 0;
1540}
1541
1542static void qcom_pcie_init_debugfs(struct qcom_pcie *pcie)
1543{
1544	struct dw_pcie *pci = pcie->pci;
1545	struct device *dev = pci->dev;
1546	char *name;
1547
1548	name = devm_kasprintf(dev, GFP_KERNEL, "%pOFP", dev->of_node);
1549	if (!name)
1550		return;
1551
1552	pcie->debugfs = debugfs_create_dir(name, NULL);
1553	debugfs_create_devm_seqfile(dev, "link_transition_count", pcie->debugfs,
1554				    qcom_pcie_link_transition_count);
1555}
1556
1557static irqreturn_t qcom_pcie_global_irq_thread(int irq, void *data)
1558{
1559	struct qcom_pcie *pcie = data;
1560	struct dw_pcie_rp *pp = &pcie->pci->pp;
1561	struct device *dev = pcie->pci->dev;
1562	u32 status = readl_relaxed(pcie->parf + PARF_INT_ALL_STATUS);
1563
1564	writel_relaxed(status, pcie->parf + PARF_INT_ALL_CLEAR);
1565
1566	if (FIELD_GET(PARF_INT_ALL_LINK_UP, status)) {
1567		dev_dbg(dev, "Received Link up event. Starting enumeration!\n");
1568		/* Rescan the bus to enumerate endpoint devices */
1569		pci_lock_rescan_remove();
1570		pci_rescan_bus(pp->bridge->bus);
1571		pci_unlock_rescan_remove();
1572
1573		qcom_pcie_icc_opp_update(pcie);
1574	} else {
1575		dev_WARN_ONCE(dev, 1, "Received unknown event. INT_STATUS: 0x%08x\n",
1576			      status);
1577	}
1578
1579	return IRQ_HANDLED;
1580}
1581
1582static int qcom_pcie_probe(struct platform_device *pdev)
1583{
1584	const struct qcom_pcie_cfg *pcie_cfg;
1585	unsigned long max_freq = ULONG_MAX;
1586	struct device *dev = &pdev->dev;
1587	struct dev_pm_opp *opp;
1588	struct qcom_pcie *pcie;
1589	struct dw_pcie_rp *pp;
1590	struct resource *res;
1591	struct dw_pcie *pci;
1592	int ret, irq;
1593	char *name;
1594
1595	pcie_cfg = of_device_get_match_data(dev);
1596	if (!pcie_cfg || !pcie_cfg->ops) {
1597		dev_err(dev, "Invalid platform data\n");
1598		return -EINVAL;
1599	}
1600
1601	pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
1602	if (!pcie)
1603		return -ENOMEM;
1604
1605	pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
1606	if (!pci)
1607		return -ENOMEM;
1608
1609	pm_runtime_enable(dev);
1610	ret = pm_runtime_get_sync(dev);
1611	if (ret < 0)
1612		goto err_pm_runtime_put;
1613
1614	pci->dev = dev;
1615	pci->ops = &dw_pcie_ops;
1616	pp = &pci->pp;
1617
1618	pcie->pci = pci;
1619
1620	pcie->cfg = pcie_cfg;
1621
1622	pcie->reset = devm_gpiod_get_optional(dev, "perst", GPIOD_OUT_HIGH);
1623	if (IS_ERR(pcie->reset)) {
1624		ret = PTR_ERR(pcie->reset);
1625		goto err_pm_runtime_put;
1626	}
1627
1628	pcie->parf = devm_platform_ioremap_resource_byname(pdev, "parf");
1629	if (IS_ERR(pcie->parf)) {
1630		ret = PTR_ERR(pcie->parf);
1631		goto err_pm_runtime_put;
1632	}
1633
1634	pcie->elbi = devm_platform_ioremap_resource_byname(pdev, "elbi");
1635	if (IS_ERR(pcie->elbi)) {
1636		ret = PTR_ERR(pcie->elbi);
1637		goto err_pm_runtime_put;
1638	}
1639
1640	/* MHI region is optional */
1641	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mhi");
1642	if (res) {
1643		pcie->mhi = devm_ioremap_resource(dev, res);
1644		if (IS_ERR(pcie->mhi)) {
1645			ret = PTR_ERR(pcie->mhi);
1646			goto err_pm_runtime_put;
1647		}
1648	}
1649
1650	pcie->phy = devm_phy_optional_get(dev, "pciephy");
1651	if (IS_ERR(pcie->phy)) {
1652		ret = PTR_ERR(pcie->phy);
1653		goto err_pm_runtime_put;
1654	}
1655
1656	/* OPP table is optional */
1657	ret = devm_pm_opp_of_add_table(dev);
1658	if (ret && ret != -ENODEV) {
1659		dev_err_probe(dev, ret, "Failed to add OPP table\n");
1660		goto err_pm_runtime_put;
1661	}
1662
1663	/*
1664	 * Before the PCIe link is initialized, vote for highest OPP in the OPP
1665	 * table, so that we are voting for maximum voltage corner for the
1666	 * link to come up in maximum supported speed. At the end of the
1667	 * probe(), OPP will be updated using qcom_pcie_icc_opp_update().
1668	 */
1669	if (!ret) {
1670		opp = dev_pm_opp_find_freq_floor(dev, &max_freq);
1671		if (IS_ERR(opp)) {
1672			ret = PTR_ERR(opp);
1673			dev_err_probe(pci->dev, ret,
1674				      "Unable to find max freq OPP\n");
1675			goto err_pm_runtime_put;
1676		} else {
1677			ret = dev_pm_opp_set_opp(dev, opp);
1678		}
1679
1680		dev_pm_opp_put(opp);
1681		if (ret) {
1682			dev_err_probe(pci->dev, ret,
1683				      "Failed to set OPP for freq %lu\n",
1684				      max_freq);
1685			goto err_pm_runtime_put;
1686		}
1687
1688		pcie->use_pm_opp = true;
1689	} else {
1690		/* Skip ICC init if OPP is supported as it is handled by OPP */
1691		ret = qcom_pcie_icc_init(pcie);
1692		if (ret)
1693			goto err_pm_runtime_put;
1694	}
1695
1696	ret = pcie->cfg->ops->get_resources(pcie);
1697	if (ret)
1698		goto err_pm_runtime_put;
1699
1700	pp->ops = &qcom_pcie_dw_ops;
1701
1702	ret = phy_init(pcie->phy);
1703	if (ret)
1704		goto err_pm_runtime_put;
1705
1706	platform_set_drvdata(pdev, pcie);
1707
1708	ret = dw_pcie_host_init(pp);
1709	if (ret) {
1710		dev_err(dev, "cannot initialize host\n");
1711		goto err_phy_exit;
1712	}
1713
1714	name = devm_kasprintf(dev, GFP_KERNEL, "qcom_pcie_global_irq%d",
1715			      pci_domain_nr(pp->bridge->bus));
1716	if (!name) {
1717		ret = -ENOMEM;
1718		goto err_host_deinit;
1719	}
1720
1721	irq = platform_get_irq_byname_optional(pdev, "global");
1722	if (irq > 0) {
1723		ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1724						qcom_pcie_global_irq_thread,
1725						IRQF_ONESHOT, name, pcie);
1726		if (ret) {
1727			dev_err_probe(&pdev->dev, ret,
1728				      "Failed to request Global IRQ\n");
1729			goto err_host_deinit;
1730		}
1731
1732		writel_relaxed(PARF_INT_ALL_LINK_UP | PARF_INT_MSI_DEV_0_7,
1733			       pcie->parf + PARF_INT_ALL_MASK);
1734	}
1735
1736	qcom_pcie_icc_opp_update(pcie);
1737
1738	if (pcie->mhi)
1739		qcom_pcie_init_debugfs(pcie);
1740
1741	return 0;
1742
1743err_host_deinit:
1744	dw_pcie_host_deinit(pp);
1745err_phy_exit:
1746	phy_exit(pcie->phy);
1747err_pm_runtime_put:
1748	pm_runtime_put(dev);
1749	pm_runtime_disable(dev);
1750
1751	return ret;
1752}
1753
1754static int qcom_pcie_suspend_noirq(struct device *dev)
1755{
1756	struct qcom_pcie *pcie = dev_get_drvdata(dev);
1757	int ret = 0;
1758
1759	/*
1760	 * Set minimum bandwidth required to keep data path functional during
1761	 * suspend.
1762	 */
1763	if (pcie->icc_mem) {
1764		ret = icc_set_bw(pcie->icc_mem, 0, kBps_to_icc(1));
1765		if (ret) {
1766			dev_err(dev,
1767				"Failed to set bandwidth for PCIe-MEM interconnect path: %d\n",
1768				ret);
1769			return ret;
1770		}
1771	}
1772
1773	/*
1774	 * Turn OFF the resources only for controllers without active PCIe
1775	 * devices. For controllers with active devices, the resources are kept
1776	 * ON and the link is expected to be in L0/L1 (sub)states.
1777	 *
1778	 * Turning OFF the resources for controllers with active PCIe devices
1779	 * will trigger access violation during the end of the suspend cycle,
1780	 * as kernel tries to access the PCIe devices config space for masking
1781	 * MSIs.
1782	 *
1783	 * Also, it is not desirable to put the link into L2/L3 state as that
1784	 * implies VDD supply will be removed and the devices may go into
1785	 * powerdown state. This will affect the lifetime of the storage devices
1786	 * like NVMe.
1787	 */
1788	if (!dw_pcie_link_up(pcie->pci)) {
1789		qcom_pcie_host_deinit(&pcie->pci->pp);
1790		pcie->suspended = true;
1791	}
1792
1793	/*
1794	 * Only disable CPU-PCIe interconnect path if the suspend is non-S2RAM.
1795	 * Because on some platforms, DBI access can happen very late during the
1796	 * S2RAM and a non-active CPU-PCIe interconnect path may lead to NoC
1797	 * error.
1798	 */
1799	if (pm_suspend_target_state != PM_SUSPEND_MEM) {
1800		ret = icc_disable(pcie->icc_cpu);
1801		if (ret)
1802			dev_err(dev, "Failed to disable CPU-PCIe interconnect path: %d\n", ret);
1803
1804		if (pcie->use_pm_opp)
1805			dev_pm_opp_set_opp(pcie->pci->dev, NULL);
1806	}
1807	return ret;
1808}
1809
1810static int qcom_pcie_resume_noirq(struct device *dev)
1811{
1812	struct qcom_pcie *pcie = dev_get_drvdata(dev);
1813	int ret;
1814
1815	if (pm_suspend_target_state != PM_SUSPEND_MEM) {
1816		ret = icc_enable(pcie->icc_cpu);
1817		if (ret) {
1818			dev_err(dev, "Failed to enable CPU-PCIe interconnect path: %d\n", ret);
1819			return ret;
1820		}
1821	}
1822
1823	if (pcie->suspended) {
1824		ret = qcom_pcie_host_init(&pcie->pci->pp);
1825		if (ret)
1826			return ret;
1827
1828		pcie->suspended = false;
1829	}
1830
1831	qcom_pcie_icc_opp_update(pcie);
1832
1833	return 0;
1834}
1835
1836static const struct of_device_id qcom_pcie_match[] = {
1837	{ .compatible = "qcom,pcie-apq8064", .data = &cfg_2_1_0 },
1838	{ .compatible = "qcom,pcie-apq8084", .data = &cfg_1_0_0 },
1839	{ .compatible = "qcom,pcie-ipq4019", .data = &cfg_2_4_0 },
1840	{ .compatible = "qcom,pcie-ipq6018", .data = &cfg_2_9_0 },
1841	{ .compatible = "qcom,pcie-ipq8064", .data = &cfg_2_1_0 },
1842	{ .compatible = "qcom,pcie-ipq8064-v2", .data = &cfg_2_1_0 },
1843	{ .compatible = "qcom,pcie-ipq8074", .data = &cfg_2_3_3 },
1844	{ .compatible = "qcom,pcie-ipq8074-gen3", .data = &cfg_2_9_0 },
1845	{ .compatible = "qcom,pcie-ipq9574", .data = &cfg_2_9_0 },
1846	{ .compatible = "qcom,pcie-msm8996", .data = &cfg_2_3_2 },
1847	{ .compatible = "qcom,pcie-qcs404", .data = &cfg_2_4_0 },
1848	{ .compatible = "qcom,pcie-sa8540p", .data = &cfg_sc8280xp },
1849	{ .compatible = "qcom,pcie-sa8775p", .data = &cfg_1_34_0},
1850	{ .compatible = "qcom,pcie-sc7280", .data = &cfg_1_9_0 },
1851	{ .compatible = "qcom,pcie-sc8180x", .data = &cfg_1_9_0 },
1852	{ .compatible = "qcom,pcie-sc8280xp", .data = &cfg_sc8280xp },
1853	{ .compatible = "qcom,pcie-sdm845", .data = &cfg_2_7_0 },
1854	{ .compatible = "qcom,pcie-sdx55", .data = &cfg_1_9_0 },
1855	{ .compatible = "qcom,pcie-sm8150", .data = &cfg_1_9_0 },
1856	{ .compatible = "qcom,pcie-sm8250", .data = &cfg_1_9_0 },
1857	{ .compatible = "qcom,pcie-sm8350", .data = &cfg_1_9_0 },
1858	{ .compatible = "qcom,pcie-sm8450-pcie0", .data = &cfg_1_9_0 },
1859	{ .compatible = "qcom,pcie-sm8450-pcie1", .data = &cfg_1_9_0 },
1860	{ .compatible = "qcom,pcie-sm8550", .data = &cfg_1_9_0 },
1861	{ .compatible = "qcom,pcie-x1e80100", .data = &cfg_sc8280xp },
1862	{ }
1863};
1864
1865static void qcom_fixup_class(struct pci_dev *dev)
1866{
1867	dev->class = PCI_CLASS_BRIDGE_PCI_NORMAL;
1868}
1869DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0101, qcom_fixup_class);
1870DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0104, qcom_fixup_class);
1871DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0106, qcom_fixup_class);
1872DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0107, qcom_fixup_class);
1873DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0302, qcom_fixup_class);
1874DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1000, qcom_fixup_class);
1875DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1001, qcom_fixup_class);
1876
1877static const struct dev_pm_ops qcom_pcie_pm_ops = {
1878	NOIRQ_SYSTEM_SLEEP_PM_OPS(qcom_pcie_suspend_noirq, qcom_pcie_resume_noirq)
1879};
1880
1881static struct platform_driver qcom_pcie_driver = {
1882	.probe = qcom_pcie_probe,
1883	.driver = {
1884		.name = "qcom-pcie",
1885		.suppress_bind_attrs = true,
1886		.of_match_table = qcom_pcie_match,
1887		.pm = &qcom_pcie_pm_ops,
1888		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1889	},
1890};
1891builtin_platform_driver(qcom_pcie_driver);
v6.8
   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/init.h>
  22#include <linux/of.h>
  23#include <linux/of_gpio.h>
  24#include <linux/pci.h>
 
  25#include <linux/pm_runtime.h>
  26#include <linux/platform_device.h>
  27#include <linux/phy/pcie.h>
  28#include <linux/phy/phy.h>
  29#include <linux/regulator/consumer.h>
  30#include <linux/reset.h>
  31#include <linux/slab.h>
  32#include <linux/types.h>
 
  33
  34#include "../../pci.h"
  35#include "pcie-designware.h"
 
  36
  37/* PARF registers */
  38#define PARF_SYS_CTRL				0x00
  39#define PARF_PM_CTRL				0x20
  40#define PARF_PCS_DEEMPH				0x34
  41#define PARF_PCS_SWING				0x38
  42#define PARF_PHY_CTRL				0x40
  43#define PARF_PHY_REFCLK				0x4c
  44#define PARF_CONFIG_BITS			0x50
  45#define PARF_DBI_BASE_ADDR			0x168
 
  46#define PARF_MHI_CLOCK_RESET_CTRL		0x174
  47#define PARF_AXI_MSTR_WR_ADDR_HALT		0x178
  48#define PARF_AXI_MSTR_WR_ADDR_HALT_V2		0x1a8
  49#define PARF_Q2A_FLUSH				0x1ac
  50#define PARF_LTSSM				0x1b0
 
 
 
  51#define PARF_SID_OFFSET				0x234
  52#define PARF_BDF_TRANSLATE_CFG			0x24c
  53#define PARF_SLV_ADDR_SPACE_SIZE		0x358
 
 
 
 
 
 
  54#define PARF_DEVICE_TYPE			0x1000
  55#define PARF_BDF_TO_SID_TABLE_N			0x2000
 
  56
  57/* ELBI registers */
  58#define ELBI_SYS_CTRL				0x04
  59
  60/* DBI registers */
  61#define AXI_MSTR_RESP_COMP_CTRL0		0x818
  62#define AXI_MSTR_RESP_COMP_CTRL1		0x81c
  63
  64/* MHI registers */
  65#define PARF_DEBUG_CNT_PM_LINKST_IN_L2		0xc04
  66#define PARF_DEBUG_CNT_PM_LINKST_IN_L1		0xc0c
  67#define PARF_DEBUG_CNT_PM_LINKST_IN_L0S		0xc10
  68#define PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L1	0xc84
  69#define PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2	0xc88
  70
  71/* PARF_SYS_CTRL register fields */
  72#define MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN	BIT(29)
  73#define MST_WAKEUP_EN				BIT(13)
  74#define SLV_WAKEUP_EN				BIT(12)
  75#define MSTR_ACLK_CGC_DIS			BIT(10)
  76#define SLV_ACLK_CGC_DIS			BIT(9)
  77#define CORE_CLK_CGC_DIS			BIT(6)
  78#define AUX_PWR_DET				BIT(4)
  79#define L23_CLK_RMV_DIS				BIT(2)
  80#define L1_CLK_RMV_DIS				BIT(1)
  81
  82/* PARF_PM_CTRL register fields */
  83#define REQ_NOT_ENTR_L1				BIT(5)
  84
  85/* PARF_PCS_DEEMPH register fields */
  86#define PCS_DEEMPH_TX_DEEMPH_GEN1(x)		FIELD_PREP(GENMASK(21, 16), x)
  87#define PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(x)	FIELD_PREP(GENMASK(13, 8), x)
  88#define PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(x)	FIELD_PREP(GENMASK(5, 0), x)
  89
  90/* PARF_PCS_SWING register fields */
  91#define PCS_SWING_TX_SWING_FULL(x)		FIELD_PREP(GENMASK(14, 8), x)
  92#define PCS_SWING_TX_SWING_LOW(x)		FIELD_PREP(GENMASK(6, 0), x)
  93
  94/* PARF_PHY_CTRL register fields */
  95#define PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK	GENMASK(20, 16)
  96#define PHY_CTRL_PHY_TX0_TERM_OFFSET(x)		FIELD_PREP(PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK, x)
  97#define PHY_TEST_PWR_DOWN			BIT(0)
  98
  99/* PARF_PHY_REFCLK register fields */
 100#define PHY_REFCLK_SSP_EN			BIT(16)
 101#define PHY_REFCLK_USE_PAD			BIT(12)
 102
 103/* PARF_CONFIG_BITS register fields */
 104#define PHY_RX0_EQ(x)				FIELD_PREP(GENMASK(26, 24), x)
 105
 106/* PARF_SLV_ADDR_SPACE_SIZE register value */
 107#define SLV_ADDR_SPACE_SZ			0x10000000
 108
 109/* PARF_MHI_CLOCK_RESET_CTRL register fields */
 110#define AHB_CLK_EN				BIT(0)
 111#define MSTR_AXI_CLK_EN				BIT(1)
 112#define BYPASS					BIT(4)
 113
 114/* PARF_AXI_MSTR_WR_ADDR_HALT register fields */
 115#define EN					BIT(31)
 116
 117/* PARF_LTSSM register fields */
 118#define LTSSM_EN				BIT(8)
 119
 
 
 
 
 
 
 
 
 120/* PARF_DEVICE_TYPE register fields */
 121#define DEVICE_TYPE_RC				0x4
 122
 
 
 
 123/* ELBI_SYS_CTRL register fields */
 124#define ELBI_SYS_CTRL_LT_ENABLE			BIT(0)
 125
 126/* AXI_MSTR_RESP_COMP_CTRL0 register fields */
 127#define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K	0x4
 128#define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_4K	0x5
 129
 130/* AXI_MSTR_RESP_COMP_CTRL1 register fields */
 131#define CFG_BRIDGE_SB_INIT			BIT(0)
 132
 133/* PCI_EXP_SLTCAP register fields */
 134#define PCIE_CAP_SLOT_POWER_LIMIT_VAL		FIELD_PREP(PCI_EXP_SLTCAP_SPLV, 250)
 135#define PCIE_CAP_SLOT_POWER_LIMIT_SCALE		FIELD_PREP(PCI_EXP_SLTCAP_SPLS, 1)
 136#define PCIE_CAP_SLOT_VAL			(PCI_EXP_SLTCAP_ABP | \
 137						PCI_EXP_SLTCAP_PCP | \
 138						PCI_EXP_SLTCAP_MRLSP | \
 139						PCI_EXP_SLTCAP_AIP | \
 140						PCI_EXP_SLTCAP_PIP | \
 141						PCI_EXP_SLTCAP_HPS | \
 142						PCI_EXP_SLTCAP_EIP | \
 143						PCIE_CAP_SLOT_POWER_LIMIT_VAL | \
 144						PCIE_CAP_SLOT_POWER_LIMIT_SCALE)
 145
 146#define PERST_DELAY_US				1000
 147
 148#define QCOM_PCIE_CRC8_POLYNOMIAL		(BIT(2) | BIT(1) | BIT(0))
 149
 150#define QCOM_PCIE_LINK_SPEED_TO_BW(speed) \
 151		Mbps_to_icc(PCIE_SPEED2MBS_ENC(pcie_link_speed[speed]))
 152
 153#define QCOM_PCIE_1_0_0_MAX_CLOCKS		4
 154struct qcom_pcie_resources_1_0_0 {
 155	struct clk_bulk_data clks[QCOM_PCIE_1_0_0_MAX_CLOCKS];
 
 156	struct reset_control *core;
 157	struct regulator *vdda;
 158};
 159
 160#define QCOM_PCIE_2_1_0_MAX_CLOCKS		5
 161#define QCOM_PCIE_2_1_0_MAX_RESETS		6
 162#define QCOM_PCIE_2_1_0_MAX_SUPPLY		3
 163struct qcom_pcie_resources_2_1_0 {
 164	struct clk_bulk_data clks[QCOM_PCIE_2_1_0_MAX_CLOCKS];
 
 165	struct reset_control_bulk_data resets[QCOM_PCIE_2_1_0_MAX_RESETS];
 166	int num_resets;
 167	struct regulator_bulk_data supplies[QCOM_PCIE_2_1_0_MAX_SUPPLY];
 168};
 169
 170#define QCOM_PCIE_2_3_2_MAX_CLOCKS		4
 171#define QCOM_PCIE_2_3_2_MAX_SUPPLY		2
 172struct qcom_pcie_resources_2_3_2 {
 173	struct clk_bulk_data clks[QCOM_PCIE_2_3_2_MAX_CLOCKS];
 
 174	struct regulator_bulk_data supplies[QCOM_PCIE_2_3_2_MAX_SUPPLY];
 175};
 176
 177#define QCOM_PCIE_2_3_3_MAX_CLOCKS		5
 178#define QCOM_PCIE_2_3_3_MAX_RESETS		7
 179struct qcom_pcie_resources_2_3_3 {
 180	struct clk_bulk_data clks[QCOM_PCIE_2_3_3_MAX_CLOCKS];
 
 181	struct reset_control_bulk_data rst[QCOM_PCIE_2_3_3_MAX_RESETS];
 182};
 183
 184#define QCOM_PCIE_2_4_0_MAX_CLOCKS		4
 185#define QCOM_PCIE_2_4_0_MAX_RESETS		12
 186struct qcom_pcie_resources_2_4_0 {
 187	struct clk_bulk_data clks[QCOM_PCIE_2_4_0_MAX_CLOCKS];
 188	int num_clks;
 189	struct reset_control_bulk_data resets[QCOM_PCIE_2_4_0_MAX_RESETS];
 190	int num_resets;
 191};
 192
 193#define QCOM_PCIE_2_7_0_MAX_CLOCKS		15
 194#define QCOM_PCIE_2_7_0_MAX_SUPPLIES		2
 195struct qcom_pcie_resources_2_7_0 {
 196	struct clk_bulk_data clks[QCOM_PCIE_2_7_0_MAX_CLOCKS];
 197	int num_clks;
 198	struct regulator_bulk_data supplies[QCOM_PCIE_2_7_0_MAX_SUPPLIES];
 199	struct reset_control *rst;
 200};
 201
 202#define QCOM_PCIE_2_9_0_MAX_CLOCKS		5
 203struct qcom_pcie_resources_2_9_0 {
 204	struct clk_bulk_data clks[QCOM_PCIE_2_9_0_MAX_CLOCKS];
 
 205	struct reset_control *rst;
 206};
 207
 208union qcom_pcie_resources {
 209	struct qcom_pcie_resources_1_0_0 v1_0_0;
 210	struct qcom_pcie_resources_2_1_0 v2_1_0;
 211	struct qcom_pcie_resources_2_3_2 v2_3_2;
 212	struct qcom_pcie_resources_2_3_3 v2_3_3;
 213	struct qcom_pcie_resources_2_4_0 v2_4_0;
 214	struct qcom_pcie_resources_2_7_0 v2_7_0;
 215	struct qcom_pcie_resources_2_9_0 v2_9_0;
 216};
 217
 218struct qcom_pcie;
 219
 220struct qcom_pcie_ops {
 221	int (*get_resources)(struct qcom_pcie *pcie);
 222	int (*init)(struct qcom_pcie *pcie);
 223	int (*post_init)(struct qcom_pcie *pcie);
 224	void (*host_post_init)(struct qcom_pcie *pcie);
 225	void (*deinit)(struct qcom_pcie *pcie);
 226	void (*ltssm_enable)(struct qcom_pcie *pcie);
 227	int (*config_sid)(struct qcom_pcie *pcie);
 228};
 229
 
 
 
 
 
 
 230struct qcom_pcie_cfg {
 231	const struct qcom_pcie_ops *ops;
 
 
 232};
 233
 234struct qcom_pcie {
 235	struct dw_pcie *pci;
 236	void __iomem *parf;			/* DT parf */
 237	void __iomem *elbi;			/* DT elbi */
 238	void __iomem *mhi;
 239	union qcom_pcie_resources res;
 240	struct phy *phy;
 241	struct gpio_desc *reset;
 242	struct icc_path *icc_mem;
 
 243	const struct qcom_pcie_cfg *cfg;
 244	struct dentry *debugfs;
 245	bool suspended;
 
 246};
 247
 248#define to_qcom_pcie(x)		dev_get_drvdata((x)->dev)
 249
 250static void qcom_ep_reset_assert(struct qcom_pcie *pcie)
 251{
 252	gpiod_set_value_cansleep(pcie->reset, 1);
 253	usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
 254}
 255
 256static void qcom_ep_reset_deassert(struct qcom_pcie *pcie)
 257{
 258	/* Ensure that PERST has been asserted for at least 100 ms */
 259	msleep(100);
 260	gpiod_set_value_cansleep(pcie->reset, 0);
 261	usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
 262}
 263
 264static int qcom_pcie_start_link(struct dw_pcie *pci)
 265{
 266	struct qcom_pcie *pcie = to_qcom_pcie(pci);
 267
 
 
 
 
 
 268	/* Enable Link Training state machine */
 269	if (pcie->cfg->ops->ltssm_enable)
 270		pcie->cfg->ops->ltssm_enable(pcie);
 271
 272	return 0;
 273}
 274
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 275static void qcom_pcie_clear_hpc(struct dw_pcie *pci)
 276{
 277	u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
 278	u32 val;
 279
 280	dw_pcie_dbi_ro_wr_en(pci);
 281
 282	val = readl(pci->dbi_base + offset + PCI_EXP_SLTCAP);
 283	val &= ~PCI_EXP_SLTCAP_HPC;
 284	writel(val, pci->dbi_base + offset + PCI_EXP_SLTCAP);
 285
 286	dw_pcie_dbi_ro_wr_dis(pci);
 287}
 288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 289static void qcom_pcie_2_1_0_ltssm_enable(struct qcom_pcie *pcie)
 290{
 291	u32 val;
 292
 293	/* enable link training */
 294	val = readl(pcie->elbi + ELBI_SYS_CTRL);
 295	val |= ELBI_SYS_CTRL_LT_ENABLE;
 296	writel(val, pcie->elbi + ELBI_SYS_CTRL);
 297}
 298
 299static int qcom_pcie_get_resources_2_1_0(struct qcom_pcie *pcie)
 300{
 301	struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
 302	struct dw_pcie *pci = pcie->pci;
 303	struct device *dev = pci->dev;
 304	bool is_apq = of_device_is_compatible(dev->of_node, "qcom,pcie-apq8064");
 305	int ret;
 306
 307	res->supplies[0].supply = "vdda";
 308	res->supplies[1].supply = "vdda_phy";
 309	res->supplies[2].supply = "vdda_refclk";
 310	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
 311				      res->supplies);
 312	if (ret)
 313		return ret;
 314
 315	res->clks[0].id = "iface";
 316	res->clks[1].id = "core";
 317	res->clks[2].id = "phy";
 318	res->clks[3].id = "aux";
 319	res->clks[4].id = "ref";
 320
 321	/* iface, core, phy are required */
 322	ret = devm_clk_bulk_get(dev, 3, res->clks);
 323	if (ret < 0)
 324		return ret;
 325
 326	/* aux, ref are optional */
 327	ret = devm_clk_bulk_get_optional(dev, 2, res->clks + 3);
 328	if (ret < 0)
 329		return ret;
 330
 331	res->resets[0].id = "pci";
 332	res->resets[1].id = "axi";
 333	res->resets[2].id = "ahb";
 334	res->resets[3].id = "por";
 335	res->resets[4].id = "phy";
 336	res->resets[5].id = "ext";
 337
 338	/* ext is optional on APQ8016 */
 339	res->num_resets = is_apq ? 5 : 6;
 340	ret = devm_reset_control_bulk_get_exclusive(dev, res->num_resets, res->resets);
 341	if (ret < 0)
 342		return ret;
 343
 344	return 0;
 345}
 346
 347static void qcom_pcie_deinit_2_1_0(struct qcom_pcie *pcie)
 348{
 349	struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
 350
 351	clk_bulk_disable_unprepare(ARRAY_SIZE(res->clks), res->clks);
 352	reset_control_bulk_assert(res->num_resets, res->resets);
 353
 354	writel(1, pcie->parf + PARF_PHY_CTRL);
 355
 356	regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
 357}
 358
 359static int qcom_pcie_init_2_1_0(struct qcom_pcie *pcie)
 360{
 361	struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
 362	struct dw_pcie *pci = pcie->pci;
 363	struct device *dev = pci->dev;
 364	int ret;
 365
 366	/* reset the PCIe interface as uboot can leave it undefined state */
 367	ret = reset_control_bulk_assert(res->num_resets, res->resets);
 368	if (ret < 0) {
 369		dev_err(dev, "cannot assert resets\n");
 370		return ret;
 371	}
 372
 373	ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
 374	if (ret < 0) {
 375		dev_err(dev, "cannot enable regulators\n");
 376		return ret;
 377	}
 378
 379	ret = reset_control_bulk_deassert(res->num_resets, res->resets);
 380	if (ret < 0) {
 381		dev_err(dev, "cannot deassert resets\n");
 382		regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
 383		return ret;
 384	}
 385
 386	return 0;
 387}
 388
 389static int qcom_pcie_post_init_2_1_0(struct qcom_pcie *pcie)
 390{
 391	struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
 392	struct dw_pcie *pci = pcie->pci;
 393	struct device *dev = pci->dev;
 394	struct device_node *node = dev->of_node;
 395	u32 val;
 396	int ret;
 397
 398	/* enable PCIe clocks and resets */
 399	val = readl(pcie->parf + PARF_PHY_CTRL);
 400	val &= ~PHY_TEST_PWR_DOWN;
 401	writel(val, pcie->parf + PARF_PHY_CTRL);
 402
 403	ret = clk_bulk_prepare_enable(ARRAY_SIZE(res->clks), res->clks);
 404	if (ret)
 405		return ret;
 406
 407	if (of_device_is_compatible(node, "qcom,pcie-ipq8064") ||
 408	    of_device_is_compatible(node, "qcom,pcie-ipq8064-v2")) {
 409		writel(PCS_DEEMPH_TX_DEEMPH_GEN1(24) |
 410			       PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(24) |
 411			       PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(34),
 412		       pcie->parf + PARF_PCS_DEEMPH);
 413		writel(PCS_SWING_TX_SWING_FULL(120) |
 414			       PCS_SWING_TX_SWING_LOW(120),
 415		       pcie->parf + PARF_PCS_SWING);
 416		writel(PHY_RX0_EQ(4), pcie->parf + PARF_CONFIG_BITS);
 417	}
 418
 419	if (of_device_is_compatible(node, "qcom,pcie-ipq8064")) {
 420		/* set TX termination offset */
 421		val = readl(pcie->parf + PARF_PHY_CTRL);
 422		val &= ~PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK;
 423		val |= PHY_CTRL_PHY_TX0_TERM_OFFSET(7);
 424		writel(val, pcie->parf + PARF_PHY_CTRL);
 425	}
 426
 427	/* enable external reference clock */
 428	val = readl(pcie->parf + PARF_PHY_REFCLK);
 429	/* USE_PAD is required only for ipq806x */
 430	if (!of_device_is_compatible(node, "qcom,pcie-apq8064"))
 431		val &= ~PHY_REFCLK_USE_PAD;
 432	val |= PHY_REFCLK_SSP_EN;
 433	writel(val, pcie->parf + PARF_PHY_REFCLK);
 434
 435	/* wait for clock acquisition */
 436	usleep_range(1000, 1500);
 437
 438	/* Set the Max TLP size to 2K, instead of using default of 4K */
 439	writel(CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K,
 440	       pci->dbi_base + AXI_MSTR_RESP_COMP_CTRL0);
 441	writel(CFG_BRIDGE_SB_INIT,
 442	       pci->dbi_base + AXI_MSTR_RESP_COMP_CTRL1);
 443
 444	qcom_pcie_clear_hpc(pcie->pci);
 445
 446	return 0;
 447}
 448
 449static int qcom_pcie_get_resources_1_0_0(struct qcom_pcie *pcie)
 450{
 451	struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
 452	struct dw_pcie *pci = pcie->pci;
 453	struct device *dev = pci->dev;
 454	int ret;
 455
 456	res->vdda = devm_regulator_get(dev, "vdda");
 457	if (IS_ERR(res->vdda))
 458		return PTR_ERR(res->vdda);
 459
 460	res->clks[0].id = "iface";
 461	res->clks[1].id = "aux";
 462	res->clks[2].id = "master_bus";
 463	res->clks[3].id = "slave_bus";
 464
 465	ret = devm_clk_bulk_get(dev, ARRAY_SIZE(res->clks), res->clks);
 466	if (ret < 0)
 467		return ret;
 468
 469	res->core = devm_reset_control_get_exclusive(dev, "core");
 470	return PTR_ERR_OR_ZERO(res->core);
 471}
 472
 473static void qcom_pcie_deinit_1_0_0(struct qcom_pcie *pcie)
 474{
 475	struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
 476
 477	reset_control_assert(res->core);
 478	clk_bulk_disable_unprepare(ARRAY_SIZE(res->clks), res->clks);
 479	regulator_disable(res->vdda);
 480}
 481
 482static int qcom_pcie_init_1_0_0(struct qcom_pcie *pcie)
 483{
 484	struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
 485	struct dw_pcie *pci = pcie->pci;
 486	struct device *dev = pci->dev;
 487	int ret;
 488
 489	ret = reset_control_deassert(res->core);
 490	if (ret) {
 491		dev_err(dev, "cannot deassert core reset\n");
 492		return ret;
 493	}
 494
 495	ret = clk_bulk_prepare_enable(ARRAY_SIZE(res->clks), res->clks);
 496	if (ret) {
 497		dev_err(dev, "cannot prepare/enable clocks\n");
 498		goto err_assert_reset;
 499	}
 500
 501	ret = regulator_enable(res->vdda);
 502	if (ret) {
 503		dev_err(dev, "cannot enable vdda regulator\n");
 504		goto err_disable_clks;
 505	}
 506
 507	return 0;
 508
 509err_disable_clks:
 510	clk_bulk_disable_unprepare(ARRAY_SIZE(res->clks), res->clks);
 511err_assert_reset:
 512	reset_control_assert(res->core);
 513
 514	return ret;
 515}
 516
 517static int qcom_pcie_post_init_1_0_0(struct qcom_pcie *pcie)
 518{
 519	/* change DBI base address */
 520	writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
 521
 522	if (IS_ENABLED(CONFIG_PCI_MSI)) {
 523		u32 val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT);
 524
 525		val |= EN;
 526		writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT);
 527	}
 528
 529	qcom_pcie_clear_hpc(pcie->pci);
 530
 531	return 0;
 532}
 533
 534static void qcom_pcie_2_3_2_ltssm_enable(struct qcom_pcie *pcie)
 535{
 536	u32 val;
 537
 538	/* enable link training */
 539	val = readl(pcie->parf + PARF_LTSSM);
 540	val |= LTSSM_EN;
 541	writel(val, pcie->parf + PARF_LTSSM);
 542}
 543
 544static int qcom_pcie_get_resources_2_3_2(struct qcom_pcie *pcie)
 545{
 546	struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
 547	struct dw_pcie *pci = pcie->pci;
 548	struct device *dev = pci->dev;
 549	int ret;
 550
 551	res->supplies[0].supply = "vdda";
 552	res->supplies[1].supply = "vddpe-3v3";
 553	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
 554				      res->supplies);
 555	if (ret)
 556		return ret;
 557
 558	res->clks[0].id = "aux";
 559	res->clks[1].id = "cfg";
 560	res->clks[2].id = "bus_master";
 561	res->clks[3].id = "bus_slave";
 562
 563	ret = devm_clk_bulk_get(dev, ARRAY_SIZE(res->clks), res->clks);
 564	if (ret < 0)
 565		return ret;
 566
 567	return 0;
 568}
 569
 570static void qcom_pcie_deinit_2_3_2(struct qcom_pcie *pcie)
 571{
 572	struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
 573
 574	clk_bulk_disable_unprepare(ARRAY_SIZE(res->clks), res->clks);
 575	regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
 576}
 577
 578static int qcom_pcie_init_2_3_2(struct qcom_pcie *pcie)
 579{
 580	struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
 581	struct dw_pcie *pci = pcie->pci;
 582	struct device *dev = pci->dev;
 583	int ret;
 584
 585	ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
 586	if (ret < 0) {
 587		dev_err(dev, "cannot enable regulators\n");
 588		return ret;
 589	}
 590
 591	ret = clk_bulk_prepare_enable(ARRAY_SIZE(res->clks), res->clks);
 592	if (ret) {
 593		dev_err(dev, "cannot prepare/enable clocks\n");
 594		regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
 595		return ret;
 596	}
 597
 598	return 0;
 599}
 600
 601static int qcom_pcie_post_init_2_3_2(struct qcom_pcie *pcie)
 602{
 603	u32 val;
 604
 605	/* enable PCIe clocks and resets */
 606	val = readl(pcie->parf + PARF_PHY_CTRL);
 607	val &= ~PHY_TEST_PWR_DOWN;
 608	writel(val, pcie->parf + PARF_PHY_CTRL);
 609
 610	/* change DBI base address */
 611	writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
 612
 613	/* MAC PHY_POWERDOWN MUX DISABLE  */
 614	val = readl(pcie->parf + PARF_SYS_CTRL);
 615	val &= ~MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN;
 616	writel(val, pcie->parf + PARF_SYS_CTRL);
 617
 618	val = readl(pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
 619	val |= BYPASS;
 620	writel(val, pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
 621
 622	val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
 623	val |= EN;
 624	writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
 625
 626	qcom_pcie_clear_hpc(pcie->pci);
 627
 628	return 0;
 629}
 630
 631static int qcom_pcie_get_resources_2_4_0(struct qcom_pcie *pcie)
 632{
 633	struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
 634	struct dw_pcie *pci = pcie->pci;
 635	struct device *dev = pci->dev;
 636	bool is_ipq = of_device_is_compatible(dev->of_node, "qcom,pcie-ipq4019");
 637	int ret;
 638
 639	res->clks[0].id = "aux";
 640	res->clks[1].id = "master_bus";
 641	res->clks[2].id = "slave_bus";
 642	res->clks[3].id = "iface";
 643
 644	/* qcom,pcie-ipq4019 is defined without "iface" */
 645	res->num_clks = is_ipq ? 3 : 4;
 646
 647	ret = devm_clk_bulk_get(dev, res->num_clks, res->clks);
 648	if (ret < 0)
 649		return ret;
 650
 651	res->resets[0].id = "axi_m";
 652	res->resets[1].id = "axi_s";
 653	res->resets[2].id = "axi_m_sticky";
 654	res->resets[3].id = "pipe_sticky";
 655	res->resets[4].id = "pwr";
 656	res->resets[5].id = "ahb";
 657	res->resets[6].id = "pipe";
 658	res->resets[7].id = "axi_m_vmid";
 659	res->resets[8].id = "axi_s_xpu";
 660	res->resets[9].id = "parf";
 661	res->resets[10].id = "phy";
 662	res->resets[11].id = "phy_ahb";
 663
 664	res->num_resets = is_ipq ? 12 : 6;
 665
 666	ret = devm_reset_control_bulk_get_exclusive(dev, res->num_resets, res->resets);
 667	if (ret < 0)
 668		return ret;
 669
 670	return 0;
 671}
 672
 673static void qcom_pcie_deinit_2_4_0(struct qcom_pcie *pcie)
 674{
 675	struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
 676
 677	reset_control_bulk_assert(res->num_resets, res->resets);
 678	clk_bulk_disable_unprepare(res->num_clks, res->clks);
 679}
 680
 681static int qcom_pcie_init_2_4_0(struct qcom_pcie *pcie)
 682{
 683	struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
 684	struct dw_pcie *pci = pcie->pci;
 685	struct device *dev = pci->dev;
 686	int ret;
 687
 688	ret = reset_control_bulk_assert(res->num_resets, res->resets);
 689	if (ret < 0) {
 690		dev_err(dev, "cannot assert resets\n");
 691		return ret;
 692	}
 693
 694	usleep_range(10000, 12000);
 695
 696	ret = reset_control_bulk_deassert(res->num_resets, res->resets);
 697	if (ret < 0) {
 698		dev_err(dev, "cannot deassert resets\n");
 699		return ret;
 700	}
 701
 702	usleep_range(10000, 12000);
 703
 704	ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
 705	if (ret) {
 706		reset_control_bulk_assert(res->num_resets, res->resets);
 707		return ret;
 708	}
 709
 710	return 0;
 711}
 712
 713static int qcom_pcie_get_resources_2_3_3(struct qcom_pcie *pcie)
 714{
 715	struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
 716	struct dw_pcie *pci = pcie->pci;
 717	struct device *dev = pci->dev;
 718	int ret;
 719
 720	res->clks[0].id = "iface";
 721	res->clks[1].id = "axi_m";
 722	res->clks[2].id = "axi_s";
 723	res->clks[3].id = "ahb";
 724	res->clks[4].id = "aux";
 725
 726	ret = devm_clk_bulk_get(dev, ARRAY_SIZE(res->clks), res->clks);
 727	if (ret < 0)
 728		return ret;
 729
 730	res->rst[0].id = "axi_m";
 731	res->rst[1].id = "axi_s";
 732	res->rst[2].id = "pipe";
 733	res->rst[3].id = "axi_m_sticky";
 734	res->rst[4].id = "sticky";
 735	res->rst[5].id = "ahb";
 736	res->rst[6].id = "sleep";
 737
 738	ret = devm_reset_control_bulk_get_exclusive(dev, ARRAY_SIZE(res->rst), res->rst);
 739	if (ret < 0)
 740		return ret;
 741
 742	return 0;
 743}
 744
 745static void qcom_pcie_deinit_2_3_3(struct qcom_pcie *pcie)
 746{
 747	struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
 748
 749	clk_bulk_disable_unprepare(ARRAY_SIZE(res->clks), res->clks);
 750}
 751
 752static int qcom_pcie_init_2_3_3(struct qcom_pcie *pcie)
 753{
 754	struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
 755	struct dw_pcie *pci = pcie->pci;
 756	struct device *dev = pci->dev;
 757	int ret;
 758
 759	ret = reset_control_bulk_assert(ARRAY_SIZE(res->rst), res->rst);
 760	if (ret < 0) {
 761		dev_err(dev, "cannot assert resets\n");
 762		return ret;
 763	}
 764
 765	usleep_range(2000, 2500);
 766
 767	ret = reset_control_bulk_deassert(ARRAY_SIZE(res->rst), res->rst);
 768	if (ret < 0) {
 769		dev_err(dev, "cannot deassert resets\n");
 770		return ret;
 771	}
 772
 773	/*
 774	 * Don't have a way to see if the reset has completed.
 775	 * Wait for some time.
 776	 */
 777	usleep_range(2000, 2500);
 778
 779	ret = clk_bulk_prepare_enable(ARRAY_SIZE(res->clks), res->clks);
 780	if (ret) {
 781		dev_err(dev, "cannot prepare/enable clocks\n");
 782		goto err_assert_resets;
 783	}
 784
 785	return 0;
 786
 787err_assert_resets:
 788	/*
 789	 * Not checking for failure, will anyway return
 790	 * the original failure in 'ret'.
 791	 */
 792	reset_control_bulk_assert(ARRAY_SIZE(res->rst), res->rst);
 793
 794	return ret;
 795}
 796
 797static int qcom_pcie_post_init_2_3_3(struct qcom_pcie *pcie)
 798{
 799	struct dw_pcie *pci = pcie->pci;
 800	u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
 801	u32 val;
 802
 803	writel(SLV_ADDR_SPACE_SZ, pcie->parf + PARF_SLV_ADDR_SPACE_SIZE);
 804
 805	val = readl(pcie->parf + PARF_PHY_CTRL);
 806	val &= ~PHY_TEST_PWR_DOWN;
 807	writel(val, pcie->parf + PARF_PHY_CTRL);
 808
 809	writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
 810
 811	writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS
 812		| SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS |
 813		AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS,
 814		pcie->parf + PARF_SYS_CTRL);
 815	writel(0, pcie->parf + PARF_Q2A_FLUSH);
 816
 817	writel(PCI_COMMAND_MASTER, pci->dbi_base + PCI_COMMAND);
 818
 819	dw_pcie_dbi_ro_wr_en(pci);
 820
 821	writel(PCIE_CAP_SLOT_VAL, pci->dbi_base + offset + PCI_EXP_SLTCAP);
 822
 823	val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP);
 824	val &= ~PCI_EXP_LNKCAP_ASPMS;
 825	writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP);
 826
 827	writel(PCI_EXP_DEVCTL2_COMP_TMOUT_DIS, pci->dbi_base + offset +
 828		PCI_EXP_DEVCTL2);
 829
 830	dw_pcie_dbi_ro_wr_dis(pci);
 831
 832	return 0;
 833}
 834
 835static int qcom_pcie_get_resources_2_7_0(struct qcom_pcie *pcie)
 836{
 837	struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
 838	struct dw_pcie *pci = pcie->pci;
 839	struct device *dev = pci->dev;
 840	unsigned int num_clks, num_opt_clks;
 841	unsigned int idx;
 842	int ret;
 843
 844	res->rst = devm_reset_control_array_get_exclusive(dev);
 845	if (IS_ERR(res->rst))
 846		return PTR_ERR(res->rst);
 847
 848	res->supplies[0].supply = "vdda";
 849	res->supplies[1].supply = "vddpe-3v3";
 850	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
 851				      res->supplies);
 852	if (ret)
 853		return ret;
 854
 855	idx = 0;
 856	res->clks[idx++].id = "aux";
 857	res->clks[idx++].id = "cfg";
 858	res->clks[idx++].id = "bus_master";
 859	res->clks[idx++].id = "bus_slave";
 860	res->clks[idx++].id = "slave_q2a";
 861
 862	num_clks = idx;
 863
 864	ret = devm_clk_bulk_get(dev, num_clks, res->clks);
 865	if (ret < 0)
 866		return ret;
 867
 868	res->clks[idx++].id = "tbu";
 869	res->clks[idx++].id = "ddrss_sf_tbu";
 870	res->clks[idx++].id = "aggre0";
 871	res->clks[idx++].id = "aggre1";
 872	res->clks[idx++].id = "noc_aggr";
 873	res->clks[idx++].id = "noc_aggr_4";
 874	res->clks[idx++].id = "noc_aggr_south_sf";
 875	res->clks[idx++].id = "cnoc_qx";
 876	res->clks[idx++].id = "sleep";
 877	res->clks[idx++].id = "cnoc_sf_axi";
 878
 879	num_opt_clks = idx - num_clks;
 880	res->num_clks = idx;
 881
 882	ret = devm_clk_bulk_get_optional(dev, num_opt_clks, res->clks + num_clks);
 883	if (ret < 0)
 884		return ret;
 885
 886	return 0;
 887}
 888
 889static int qcom_pcie_init_2_7_0(struct qcom_pcie *pcie)
 890{
 891	struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
 892	struct dw_pcie *pci = pcie->pci;
 893	struct device *dev = pci->dev;
 894	u32 val;
 895	int ret;
 896
 897	ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
 898	if (ret < 0) {
 899		dev_err(dev, "cannot enable regulators\n");
 900		return ret;
 901	}
 902
 903	ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
 904	if (ret < 0)
 905		goto err_disable_regulators;
 906
 907	ret = reset_control_assert(res->rst);
 908	if (ret) {
 909		dev_err(dev, "reset assert failed (%d)\n", ret);
 910		goto err_disable_clocks;
 911	}
 912
 913	usleep_range(1000, 1500);
 914
 915	ret = reset_control_deassert(res->rst);
 916	if (ret) {
 917		dev_err(dev, "reset deassert failed (%d)\n", ret);
 918		goto err_disable_clocks;
 919	}
 920
 921	/* Wait for reset to complete, required on SM8450 */
 922	usleep_range(1000, 1500);
 923
 924	/* configure PCIe to RC mode */
 925	writel(DEVICE_TYPE_RC, pcie->parf + PARF_DEVICE_TYPE);
 926
 927	/* enable PCIe clocks and resets */
 928	val = readl(pcie->parf + PARF_PHY_CTRL);
 929	val &= ~PHY_TEST_PWR_DOWN;
 930	writel(val, pcie->parf + PARF_PHY_CTRL);
 931
 932	/* change DBI base address */
 933	writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
 934
 935	/* MAC PHY_POWERDOWN MUX DISABLE  */
 936	val = readl(pcie->parf + PARF_SYS_CTRL);
 937	val &= ~MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN;
 938	writel(val, pcie->parf + PARF_SYS_CTRL);
 939
 940	val = readl(pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
 941	val |= BYPASS;
 942	writel(val, pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
 943
 944	/* Enable L1 and L1SS */
 945	val = readl(pcie->parf + PARF_PM_CTRL);
 946	val &= ~REQ_NOT_ENTR_L1;
 947	writel(val, pcie->parf + PARF_PM_CTRL);
 948
 949	val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
 950	val |= EN;
 951	writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
 952
 953	return 0;
 954err_disable_clocks:
 955	clk_bulk_disable_unprepare(res->num_clks, res->clks);
 956err_disable_regulators:
 957	regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
 958
 959	return ret;
 960}
 961
 962static int qcom_pcie_post_init_2_7_0(struct qcom_pcie *pcie)
 963{
 
 
 
 
 
 
 
 964	qcom_pcie_clear_hpc(pcie->pci);
 965
 966	return 0;
 967}
 968
 969static int qcom_pcie_enable_aspm(struct pci_dev *pdev, void *userdata)
 970{
 971	/*
 972	 * Downstream devices need to be in D0 state before enabling PCI PM
 973	 * substates.
 974	 */
 975	pci_set_power_state_locked(pdev, PCI_D0);
 976	pci_enable_link_state_locked(pdev, PCIE_LINK_STATE_ALL);
 977
 978	return 0;
 979}
 980
 981static void qcom_pcie_host_post_init_2_7_0(struct qcom_pcie *pcie)
 982{
 983	struct dw_pcie_rp *pp = &pcie->pci->pp;
 984
 985	pci_walk_bus(pp->bridge->bus, qcom_pcie_enable_aspm, NULL);
 986}
 987
 988static void qcom_pcie_deinit_2_7_0(struct qcom_pcie *pcie)
 989{
 990	struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
 991
 992	clk_bulk_disable_unprepare(res->num_clks, res->clks);
 993
 994	regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
 995}
 996
 997static int qcom_pcie_config_sid_1_9_0(struct qcom_pcie *pcie)
 998{
 999	/* iommu map structure */
1000	struct {
1001		u32 bdf;
1002		u32 phandle;
1003		u32 smmu_sid;
1004		u32 smmu_sid_len;
1005	} *map;
1006	void __iomem *bdf_to_sid_base = pcie->parf + PARF_BDF_TO_SID_TABLE_N;
1007	struct device *dev = pcie->pci->dev;
1008	u8 qcom_pcie_crc8_table[CRC8_TABLE_SIZE];
1009	int i, nr_map, size = 0;
1010	u32 smmu_sid_base;
 
1011
1012	of_get_property(dev->of_node, "iommu-map", &size);
1013	if (!size)
1014		return 0;
1015
 
 
 
 
 
1016	map = kzalloc(size, GFP_KERNEL);
1017	if (!map)
1018		return -ENOMEM;
1019
1020	of_property_read_u32_array(dev->of_node, "iommu-map", (u32 *)map,
1021				   size / sizeof(u32));
1022
1023	nr_map = size / (sizeof(*map));
1024
1025	crc8_populate_msb(qcom_pcie_crc8_table, QCOM_PCIE_CRC8_POLYNOMIAL);
1026
1027	/* Registers need to be zero out first */
1028	memset_io(bdf_to_sid_base, 0, CRC8_TABLE_SIZE * sizeof(u32));
1029
1030	/* Extract the SMMU SID base from the first entry of iommu-map */
1031	smmu_sid_base = map[0].smmu_sid;
1032
1033	/* Look for an available entry to hold the mapping */
1034	for (i = 0; i < nr_map; i++) {
1035		__be16 bdf_be = cpu_to_be16(map[i].bdf);
1036		u32 val;
1037		u8 hash;
1038
1039		hash = crc8(qcom_pcie_crc8_table, (u8 *)&bdf_be, sizeof(bdf_be), 0);
1040
1041		val = readl(bdf_to_sid_base + hash * sizeof(u32));
1042
1043		/* If the register is already populated, look for next available entry */
1044		while (val) {
1045			u8 current_hash = hash++;
1046			u8 next_mask = 0xff;
1047
1048			/* If NEXT field is NULL then update it with next hash */
1049			if (!(val & next_mask)) {
1050				val |= (u32)hash;
1051				writel(val, bdf_to_sid_base + current_hash * sizeof(u32));
1052			}
1053
1054			val = readl(bdf_to_sid_base + hash * sizeof(u32));
1055		}
1056
1057		/* BDF [31:16] | SID [15:8] | NEXT [7:0] */
1058		val = map[i].bdf << 16 | (map[i].smmu_sid - smmu_sid_base) << 8 | 0;
1059		writel(val, bdf_to_sid_base + hash * sizeof(u32));
1060	}
1061
1062	kfree(map);
1063
1064	return 0;
1065}
1066
1067static int qcom_pcie_get_resources_2_9_0(struct qcom_pcie *pcie)
1068{
1069	struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1070	struct dw_pcie *pci = pcie->pci;
1071	struct device *dev = pci->dev;
1072	int ret;
1073
1074	res->clks[0].id = "iface";
1075	res->clks[1].id = "axi_m";
1076	res->clks[2].id = "axi_s";
1077	res->clks[3].id = "axi_bridge";
1078	res->clks[4].id = "rchng";
1079
1080	ret = devm_clk_bulk_get(dev, ARRAY_SIZE(res->clks), res->clks);
1081	if (ret < 0)
1082		return ret;
1083
1084	res->rst = devm_reset_control_array_get_exclusive(dev);
1085	if (IS_ERR(res->rst))
1086		return PTR_ERR(res->rst);
1087
1088	return 0;
1089}
1090
1091static void qcom_pcie_deinit_2_9_0(struct qcom_pcie *pcie)
1092{
1093	struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1094
1095	clk_bulk_disable_unprepare(ARRAY_SIZE(res->clks), res->clks);
1096}
1097
1098static int qcom_pcie_init_2_9_0(struct qcom_pcie *pcie)
1099{
1100	struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1101	struct device *dev = pcie->pci->dev;
1102	int ret;
1103
1104	ret = reset_control_assert(res->rst);
1105	if (ret) {
1106		dev_err(dev, "reset assert failed (%d)\n", ret);
1107		return ret;
1108	}
1109
1110	/*
1111	 * Delay periods before and after reset deassert are working values
1112	 * from downstream Codeaurora kernel
1113	 */
1114	usleep_range(2000, 2500);
1115
1116	ret = reset_control_deassert(res->rst);
1117	if (ret) {
1118		dev_err(dev, "reset deassert failed (%d)\n", ret);
1119		return ret;
1120	}
1121
1122	usleep_range(2000, 2500);
1123
1124	return clk_bulk_prepare_enable(ARRAY_SIZE(res->clks), res->clks);
1125}
1126
1127static int qcom_pcie_post_init_2_9_0(struct qcom_pcie *pcie)
1128{
1129	struct dw_pcie *pci = pcie->pci;
1130	u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1131	u32 val;
1132	int i;
1133
1134	writel(SLV_ADDR_SPACE_SZ,
1135		pcie->parf + PARF_SLV_ADDR_SPACE_SIZE);
1136
1137	val = readl(pcie->parf + PARF_PHY_CTRL);
1138	val &= ~PHY_TEST_PWR_DOWN;
1139	writel(val, pcie->parf + PARF_PHY_CTRL);
1140
1141	writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
1142
1143	writel(DEVICE_TYPE_RC, pcie->parf + PARF_DEVICE_TYPE);
1144	writel(BYPASS | MSTR_AXI_CLK_EN | AHB_CLK_EN,
1145		pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
1146	writel(GEN3_RELATED_OFF_RXEQ_RGRDLESS_RXTS |
1147		GEN3_RELATED_OFF_GEN3_ZRXDC_NONCOMPL,
1148		pci->dbi_base + GEN3_RELATED_OFF);
1149
1150	writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS |
1151		SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS |
1152		AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS,
1153		pcie->parf + PARF_SYS_CTRL);
1154
1155	writel(0, pcie->parf + PARF_Q2A_FLUSH);
1156
1157	dw_pcie_dbi_ro_wr_en(pci);
1158
1159	writel(PCIE_CAP_SLOT_VAL, pci->dbi_base + offset + PCI_EXP_SLTCAP);
1160
1161	val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP);
1162	val &= ~PCI_EXP_LNKCAP_ASPMS;
1163	writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP);
1164
1165	writel(PCI_EXP_DEVCTL2_COMP_TMOUT_DIS, pci->dbi_base + offset +
1166			PCI_EXP_DEVCTL2);
1167
1168	dw_pcie_dbi_ro_wr_dis(pci);
1169
1170	for (i = 0; i < 256; i++)
1171		writel(0, pcie->parf + PARF_BDF_TO_SID_TABLE_N + (4 * i));
1172
1173	return 0;
1174}
1175
1176static int qcom_pcie_link_up(struct dw_pcie *pci)
1177{
1178	u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1179	u16 val = readw(pci->dbi_base + offset + PCI_EXP_LNKSTA);
1180
1181	return !!(val & PCI_EXP_LNKSTA_DLLLA);
1182}
1183
1184static int qcom_pcie_host_init(struct dw_pcie_rp *pp)
1185{
1186	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1187	struct qcom_pcie *pcie = to_qcom_pcie(pci);
1188	int ret;
1189
1190	qcom_ep_reset_assert(pcie);
1191
1192	ret = pcie->cfg->ops->init(pcie);
1193	if (ret)
1194		return ret;
1195
1196	ret = phy_set_mode_ext(pcie->phy, PHY_MODE_PCIE, PHY_MODE_PCIE_RC);
1197	if (ret)
1198		goto err_deinit;
1199
1200	ret = phy_power_on(pcie->phy);
1201	if (ret)
1202		goto err_deinit;
1203
1204	if (pcie->cfg->ops->post_init) {
1205		ret = pcie->cfg->ops->post_init(pcie);
1206		if (ret)
1207			goto err_disable_phy;
1208	}
1209
1210	qcom_ep_reset_deassert(pcie);
1211
1212	if (pcie->cfg->ops->config_sid) {
1213		ret = pcie->cfg->ops->config_sid(pcie);
1214		if (ret)
1215			goto err_assert_reset;
1216	}
1217
1218	return 0;
1219
1220err_assert_reset:
1221	qcom_ep_reset_assert(pcie);
1222err_disable_phy:
1223	phy_power_off(pcie->phy);
1224err_deinit:
1225	pcie->cfg->ops->deinit(pcie);
1226
1227	return ret;
1228}
1229
1230static void qcom_pcie_host_deinit(struct dw_pcie_rp *pp)
1231{
1232	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1233	struct qcom_pcie *pcie = to_qcom_pcie(pci);
1234
1235	qcom_ep_reset_assert(pcie);
1236	phy_power_off(pcie->phy);
1237	pcie->cfg->ops->deinit(pcie);
1238}
1239
1240static void qcom_pcie_host_post_init(struct dw_pcie_rp *pp)
1241{
1242	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1243	struct qcom_pcie *pcie = to_qcom_pcie(pci);
1244
1245	if (pcie->cfg->ops->host_post_init)
1246		pcie->cfg->ops->host_post_init(pcie);
1247}
1248
1249static const struct dw_pcie_host_ops qcom_pcie_dw_ops = {
1250	.init		= qcom_pcie_host_init,
1251	.deinit		= qcom_pcie_host_deinit,
1252	.post_init	= qcom_pcie_host_post_init,
1253};
1254
1255/* Qcom IP rev.: 2.1.0	Synopsys IP rev.: 4.01a */
1256static const struct qcom_pcie_ops ops_2_1_0 = {
1257	.get_resources = qcom_pcie_get_resources_2_1_0,
1258	.init = qcom_pcie_init_2_1_0,
1259	.post_init = qcom_pcie_post_init_2_1_0,
1260	.deinit = qcom_pcie_deinit_2_1_0,
1261	.ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
1262};
1263
1264/* Qcom IP rev.: 1.0.0	Synopsys IP rev.: 4.11a */
1265static const struct qcom_pcie_ops ops_1_0_0 = {
1266	.get_resources = qcom_pcie_get_resources_1_0_0,
1267	.init = qcom_pcie_init_1_0_0,
1268	.post_init = qcom_pcie_post_init_1_0_0,
1269	.deinit = qcom_pcie_deinit_1_0_0,
1270	.ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
1271};
1272
1273/* Qcom IP rev.: 2.3.2	Synopsys IP rev.: 4.21a */
1274static const struct qcom_pcie_ops ops_2_3_2 = {
1275	.get_resources = qcom_pcie_get_resources_2_3_2,
1276	.init = qcom_pcie_init_2_3_2,
1277	.post_init = qcom_pcie_post_init_2_3_2,
1278	.deinit = qcom_pcie_deinit_2_3_2,
1279	.ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1280};
1281
1282/* Qcom IP rev.: 2.4.0	Synopsys IP rev.: 4.20a */
1283static const struct qcom_pcie_ops ops_2_4_0 = {
1284	.get_resources = qcom_pcie_get_resources_2_4_0,
1285	.init = qcom_pcie_init_2_4_0,
1286	.post_init = qcom_pcie_post_init_2_3_2,
1287	.deinit = qcom_pcie_deinit_2_4_0,
1288	.ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1289};
1290
1291/* Qcom IP rev.: 2.3.3	Synopsys IP rev.: 4.30a */
1292static const struct qcom_pcie_ops ops_2_3_3 = {
1293	.get_resources = qcom_pcie_get_resources_2_3_3,
1294	.init = qcom_pcie_init_2_3_3,
1295	.post_init = qcom_pcie_post_init_2_3_3,
1296	.deinit = qcom_pcie_deinit_2_3_3,
1297	.ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1298};
1299
1300/* Qcom IP rev.: 2.7.0	Synopsys IP rev.: 4.30a */
1301static const struct qcom_pcie_ops ops_2_7_0 = {
1302	.get_resources = qcom_pcie_get_resources_2_7_0,
1303	.init = qcom_pcie_init_2_7_0,
1304	.post_init = qcom_pcie_post_init_2_7_0,
1305	.deinit = qcom_pcie_deinit_2_7_0,
1306	.ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1307};
1308
1309/* Qcom IP rev.: 1.9.0 */
1310static const struct qcom_pcie_ops ops_1_9_0 = {
1311	.get_resources = qcom_pcie_get_resources_2_7_0,
1312	.init = qcom_pcie_init_2_7_0,
1313	.post_init = qcom_pcie_post_init_2_7_0,
1314	.host_post_init = qcom_pcie_host_post_init_2_7_0,
1315	.deinit = qcom_pcie_deinit_2_7_0,
1316	.ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1317	.config_sid = qcom_pcie_config_sid_1_9_0,
1318};
1319
 
 
 
 
 
 
 
 
 
 
1320/* Qcom IP rev.: 2.9.0  Synopsys IP rev.: 5.00a */
1321static const struct qcom_pcie_ops ops_2_9_0 = {
1322	.get_resources = qcom_pcie_get_resources_2_9_0,
1323	.init = qcom_pcie_init_2_9_0,
1324	.post_init = qcom_pcie_post_init_2_9_0,
1325	.deinit = qcom_pcie_deinit_2_9_0,
1326	.ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1327};
1328
1329static const struct qcom_pcie_cfg cfg_1_0_0 = {
1330	.ops = &ops_1_0_0,
1331};
1332
1333static const struct qcom_pcie_cfg cfg_1_9_0 = {
1334	.ops = &ops_1_9_0,
1335};
1336
 
 
 
 
 
1337static const struct qcom_pcie_cfg cfg_2_1_0 = {
1338	.ops = &ops_2_1_0,
1339};
1340
1341static const struct qcom_pcie_cfg cfg_2_3_2 = {
1342	.ops = &ops_2_3_2,
1343};
1344
1345static const struct qcom_pcie_cfg cfg_2_3_3 = {
1346	.ops = &ops_2_3_3,
1347};
1348
1349static const struct qcom_pcie_cfg cfg_2_4_0 = {
1350	.ops = &ops_2_4_0,
1351};
1352
1353static const struct qcom_pcie_cfg cfg_2_7_0 = {
1354	.ops = &ops_2_7_0,
1355};
1356
1357static const struct qcom_pcie_cfg cfg_2_9_0 = {
1358	.ops = &ops_2_9_0,
1359};
1360
 
 
 
 
 
1361static const struct dw_pcie_ops dw_pcie_ops = {
1362	.link_up = qcom_pcie_link_up,
1363	.start_link = qcom_pcie_start_link,
1364};
1365
1366static int qcom_pcie_icc_init(struct qcom_pcie *pcie)
1367{
1368	struct dw_pcie *pci = pcie->pci;
1369	int ret;
1370
1371	pcie->icc_mem = devm_of_icc_get(pci->dev, "pcie-mem");
1372	if (IS_ERR(pcie->icc_mem))
1373		return PTR_ERR(pcie->icc_mem);
1374
 
 
 
1375	/*
1376	 * Some Qualcomm platforms require interconnect bandwidth constraints
1377	 * to be set before enabling interconnect clocks.
1378	 *
1379	 * Set an initial peak bandwidth corresponding to single-lane Gen 1
1380	 * for the pcie-mem path.
1381	 */
1382	ret = icc_set_bw(pcie->icc_mem, 0, QCOM_PCIE_LINK_SPEED_TO_BW(1));
1383	if (ret) {
1384		dev_err(pci->dev, "failed to set interconnect bandwidth: %d\n",
1385			ret);
1386		return ret;
1387	}
1388
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1389	return 0;
1390}
1391
1392static void qcom_pcie_icc_update(struct qcom_pcie *pcie)
1393{
 
1394	struct dw_pcie *pci = pcie->pci;
1395	u32 offset, status;
1396	int speed, width;
1397	int ret;
1398
1399	if (!pcie->icc_mem)
1400		return;
1401
1402	offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1403	status = readw(pci->dbi_base + offset + PCI_EXP_LNKSTA);
1404
1405	/* Only update constraints if link is up. */
1406	if (!(status & PCI_EXP_LNKSTA_DLLLA))
1407		return;
1408
1409	speed = FIELD_GET(PCI_EXP_LNKSTA_CLS, status);
1410	width = FIELD_GET(PCI_EXP_LNKSTA_NLW, status);
1411
1412	ret = icc_set_bw(pcie->icc_mem, 0, width * QCOM_PCIE_LINK_SPEED_TO_BW(speed));
1413	if (ret) {
1414		dev_err(pci->dev, "failed to set interconnect bandwidth: %d\n",
1415			ret);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1416	}
1417}
1418
1419static int qcom_pcie_link_transition_count(struct seq_file *s, void *data)
1420{
1421	struct qcom_pcie *pcie = (struct qcom_pcie *)dev_get_drvdata(s->private);
1422
1423	seq_printf(s, "L0s transition count: %u\n",
1424		   readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_PM_LINKST_IN_L0S));
1425
1426	seq_printf(s, "L1 transition count: %u\n",
1427		   readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_PM_LINKST_IN_L1));
1428
1429	seq_printf(s, "L1.1 transition count: %u\n",
1430		   readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L1));
1431
1432	seq_printf(s, "L1.2 transition count: %u\n",
1433		   readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2));
1434
1435	seq_printf(s, "L2 transition count: %u\n",
1436		   readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_PM_LINKST_IN_L2));
1437
1438	return 0;
1439}
1440
1441static void qcom_pcie_init_debugfs(struct qcom_pcie *pcie)
1442{
1443	struct dw_pcie *pci = pcie->pci;
1444	struct device *dev = pci->dev;
1445	char *name;
1446
1447	name = devm_kasprintf(dev, GFP_KERNEL, "%pOFP", dev->of_node);
1448	if (!name)
1449		return;
1450
1451	pcie->debugfs = debugfs_create_dir(name, NULL);
1452	debugfs_create_devm_seqfile(dev, "link_transition_count", pcie->debugfs,
1453				    qcom_pcie_link_transition_count);
1454}
1455
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1456static int qcom_pcie_probe(struct platform_device *pdev)
1457{
1458	const struct qcom_pcie_cfg *pcie_cfg;
 
1459	struct device *dev = &pdev->dev;
 
1460	struct qcom_pcie *pcie;
1461	struct dw_pcie_rp *pp;
1462	struct resource *res;
1463	struct dw_pcie *pci;
1464	int ret;
 
1465
1466	pcie_cfg = of_device_get_match_data(dev);
1467	if (!pcie_cfg || !pcie_cfg->ops) {
1468		dev_err(dev, "Invalid platform data\n");
1469		return -EINVAL;
1470	}
1471
1472	pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
1473	if (!pcie)
1474		return -ENOMEM;
1475
1476	pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
1477	if (!pci)
1478		return -ENOMEM;
1479
1480	pm_runtime_enable(dev);
1481	ret = pm_runtime_get_sync(dev);
1482	if (ret < 0)
1483		goto err_pm_runtime_put;
1484
1485	pci->dev = dev;
1486	pci->ops = &dw_pcie_ops;
1487	pp = &pci->pp;
1488
1489	pcie->pci = pci;
1490
1491	pcie->cfg = pcie_cfg;
1492
1493	pcie->reset = devm_gpiod_get_optional(dev, "perst", GPIOD_OUT_HIGH);
1494	if (IS_ERR(pcie->reset)) {
1495		ret = PTR_ERR(pcie->reset);
1496		goto err_pm_runtime_put;
1497	}
1498
1499	pcie->parf = devm_platform_ioremap_resource_byname(pdev, "parf");
1500	if (IS_ERR(pcie->parf)) {
1501		ret = PTR_ERR(pcie->parf);
1502		goto err_pm_runtime_put;
1503	}
1504
1505	pcie->elbi = devm_platform_ioremap_resource_byname(pdev, "elbi");
1506	if (IS_ERR(pcie->elbi)) {
1507		ret = PTR_ERR(pcie->elbi);
1508		goto err_pm_runtime_put;
1509	}
1510
1511	/* MHI region is optional */
1512	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mhi");
1513	if (res) {
1514		pcie->mhi = devm_ioremap_resource(dev, res);
1515		if (IS_ERR(pcie->mhi)) {
1516			ret = PTR_ERR(pcie->mhi);
1517			goto err_pm_runtime_put;
1518		}
1519	}
1520
1521	pcie->phy = devm_phy_optional_get(dev, "pciephy");
1522	if (IS_ERR(pcie->phy)) {
1523		ret = PTR_ERR(pcie->phy);
1524		goto err_pm_runtime_put;
1525	}
1526
1527	ret = qcom_pcie_icc_init(pcie);
1528	if (ret)
 
 
1529		goto err_pm_runtime_put;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1530
1531	ret = pcie->cfg->ops->get_resources(pcie);
1532	if (ret)
1533		goto err_pm_runtime_put;
1534
1535	pp->ops = &qcom_pcie_dw_ops;
1536
1537	ret = phy_init(pcie->phy);
1538	if (ret)
1539		goto err_pm_runtime_put;
1540
1541	platform_set_drvdata(pdev, pcie);
1542
1543	ret = dw_pcie_host_init(pp);
1544	if (ret) {
1545		dev_err(dev, "cannot initialize host\n");
1546		goto err_phy_exit;
1547	}
1548
1549	qcom_pcie_icc_update(pcie);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1550
1551	if (pcie->mhi)
1552		qcom_pcie_init_debugfs(pcie);
1553
1554	return 0;
1555
 
 
1556err_phy_exit:
1557	phy_exit(pcie->phy);
1558err_pm_runtime_put:
1559	pm_runtime_put(dev);
1560	pm_runtime_disable(dev);
1561
1562	return ret;
1563}
1564
1565static int qcom_pcie_suspend_noirq(struct device *dev)
1566{
1567	struct qcom_pcie *pcie = dev_get_drvdata(dev);
1568	int ret;
1569
1570	/*
1571	 * Set minimum bandwidth required to keep data path functional during
1572	 * suspend.
1573	 */
1574	ret = icc_set_bw(pcie->icc_mem, 0, kBps_to_icc(1));
1575	if (ret) {
1576		dev_err(dev, "Failed to set interconnect bandwidth: %d\n", ret);
1577		return ret;
 
 
 
 
1578	}
1579
1580	/*
1581	 * Turn OFF the resources only for controllers without active PCIe
1582	 * devices. For controllers with active devices, the resources are kept
1583	 * ON and the link is expected to be in L0/L1 (sub)states.
1584	 *
1585	 * Turning OFF the resources for controllers with active PCIe devices
1586	 * will trigger access violation during the end of the suspend cycle,
1587	 * as kernel tries to access the PCIe devices config space for masking
1588	 * MSIs.
1589	 *
1590	 * Also, it is not desirable to put the link into L2/L3 state as that
1591	 * implies VDD supply will be removed and the devices may go into
1592	 * powerdown state. This will affect the lifetime of the storage devices
1593	 * like NVMe.
1594	 */
1595	if (!dw_pcie_link_up(pcie->pci)) {
1596		qcom_pcie_host_deinit(&pcie->pci->pp);
1597		pcie->suspended = true;
1598	}
1599
1600	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1601}
1602
1603static int qcom_pcie_resume_noirq(struct device *dev)
1604{
1605	struct qcom_pcie *pcie = dev_get_drvdata(dev);
1606	int ret;
1607
 
 
 
 
 
 
 
 
1608	if (pcie->suspended) {
1609		ret = qcom_pcie_host_init(&pcie->pci->pp);
1610		if (ret)
1611			return ret;
1612
1613		pcie->suspended = false;
1614	}
1615
1616	qcom_pcie_icc_update(pcie);
1617
1618	return 0;
1619}
1620
1621static const struct of_device_id qcom_pcie_match[] = {
1622	{ .compatible = "qcom,pcie-apq8064", .data = &cfg_2_1_0 },
1623	{ .compatible = "qcom,pcie-apq8084", .data = &cfg_1_0_0 },
1624	{ .compatible = "qcom,pcie-ipq4019", .data = &cfg_2_4_0 },
1625	{ .compatible = "qcom,pcie-ipq6018", .data = &cfg_2_9_0 },
1626	{ .compatible = "qcom,pcie-ipq8064", .data = &cfg_2_1_0 },
1627	{ .compatible = "qcom,pcie-ipq8064-v2", .data = &cfg_2_1_0 },
1628	{ .compatible = "qcom,pcie-ipq8074", .data = &cfg_2_3_3 },
1629	{ .compatible = "qcom,pcie-ipq8074-gen3", .data = &cfg_2_9_0 },
 
1630	{ .compatible = "qcom,pcie-msm8996", .data = &cfg_2_3_2 },
1631	{ .compatible = "qcom,pcie-qcs404", .data = &cfg_2_4_0 },
1632	{ .compatible = "qcom,pcie-sa8540p", .data = &cfg_1_9_0 },
1633	{ .compatible = "qcom,pcie-sa8775p", .data = &cfg_1_9_0},
1634	{ .compatible = "qcom,pcie-sc7280", .data = &cfg_1_9_0 },
1635	{ .compatible = "qcom,pcie-sc8180x", .data = &cfg_1_9_0 },
1636	{ .compatible = "qcom,pcie-sc8280xp", .data = &cfg_1_9_0 },
1637	{ .compatible = "qcom,pcie-sdm845", .data = &cfg_2_7_0 },
1638	{ .compatible = "qcom,pcie-sdx55", .data = &cfg_1_9_0 },
1639	{ .compatible = "qcom,pcie-sm8150", .data = &cfg_1_9_0 },
1640	{ .compatible = "qcom,pcie-sm8250", .data = &cfg_1_9_0 },
1641	{ .compatible = "qcom,pcie-sm8350", .data = &cfg_1_9_0 },
1642	{ .compatible = "qcom,pcie-sm8450-pcie0", .data = &cfg_1_9_0 },
1643	{ .compatible = "qcom,pcie-sm8450-pcie1", .data = &cfg_1_9_0 },
1644	{ .compatible = "qcom,pcie-sm8550", .data = &cfg_1_9_0 },
 
1645	{ }
1646};
1647
1648static void qcom_fixup_class(struct pci_dev *dev)
1649{
1650	dev->class = PCI_CLASS_BRIDGE_PCI_NORMAL;
1651}
1652DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0101, qcom_fixup_class);
1653DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0104, qcom_fixup_class);
1654DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0106, qcom_fixup_class);
1655DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0107, qcom_fixup_class);
1656DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0302, qcom_fixup_class);
1657DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1000, qcom_fixup_class);
1658DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1001, qcom_fixup_class);
1659
1660static const struct dev_pm_ops qcom_pcie_pm_ops = {
1661	NOIRQ_SYSTEM_SLEEP_PM_OPS(qcom_pcie_suspend_noirq, qcom_pcie_resume_noirq)
1662};
1663
1664static struct platform_driver qcom_pcie_driver = {
1665	.probe = qcom_pcie_probe,
1666	.driver = {
1667		.name = "qcom-pcie",
1668		.suppress_bind_attrs = true,
1669		.of_match_table = qcom_pcie_match,
1670		.pm = &qcom_pcie_pm_ops,
1671		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1672	},
1673};
1674builtin_platform_driver(qcom_pcie_driver);