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